blob: 7829c590eeac64478a7855537e5c3fef90f92133 [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04002/*
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04003 *
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 *
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040011 */
12
13 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
14 /* Note that there are handle based routines which must be */
15 /* treated slightly differently for reconnection purposes since we never */
16 /* want to reuse a stale file handle and only the caller knows the file info */
17
18#include <linux/fs.h>
19#include <linux/kernel.h>
20#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070021#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040022#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020023#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070024#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040025#include <linux/xattr.h>
26#include "smb2pdu.h"
27#include "cifsglob.h"
28#include "cifsacl.h"
29#include "cifsproto.h"
30#include "smb2proto.h"
31#include "cifs_unicode.h"
32#include "cifs_debug.h"
33#include "ntlmssp.h"
34#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070035#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070036#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050037#include "cifs_spnego.h"
Long Lidb223a52017-11-22 17:38:45 -070038#include "smbdirect.h"
Steve Frencheccb4422018-05-17 21:16:55 -050039#include "trace.h"
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -020040#ifdef CONFIG_CIFS_DFS_UPCALL
41#include "dfs_cache.h"
42#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040043
44/*
45 * The following table defines the expected "StructureSize" of SMB2 requests
46 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
47 *
48 * Note that commands are defined in smb2pdu.h in le16 but the array below is
49 * indexed by command in host byte order.
50 */
51static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
52 /* SMB2_NEGOTIATE */ 36,
53 /* SMB2_SESSION_SETUP */ 25,
54 /* SMB2_LOGOFF */ 4,
55 /* SMB2_TREE_CONNECT */ 9,
56 /* SMB2_TREE_DISCONNECT */ 4,
57 /* SMB2_CREATE */ 57,
58 /* SMB2_CLOSE */ 24,
59 /* SMB2_FLUSH */ 24,
60 /* SMB2_READ */ 49,
61 /* SMB2_WRITE */ 49,
62 /* SMB2_LOCK */ 48,
63 /* SMB2_IOCTL */ 57,
64 /* SMB2_CANCEL */ 4,
65 /* SMB2_ECHO */ 4,
66 /* SMB2_QUERY_DIRECTORY */ 33,
67 /* SMB2_CHANGE_NOTIFY */ 32,
68 /* SMB2_QUERY_INFO */ 41,
69 /* SMB2_SET_INFO */ 33,
70 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
71};
72
Ronnie Sahlberg730928c2018-08-08 15:07:49 +100073int smb3_encryption_required(const struct cifs_tcon *tcon)
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070074{
Steve Frenchedb16132020-05-31 14:36:56 -050075 if (!tcon || !tcon->ses)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080076 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070077 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
78 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
79 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080080 if (tcon->seal &&
81 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
82 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070083 return 0;
84}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040085
86static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070087smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Aurelien Aptel352d96f2020-05-31 12:38:22 -050088 const struct cifs_tcon *tcon,
89 struct TCP_Server_Info *server)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040090{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070091 shdr->ProtocolId = SMB2_PROTO_NUMBER;
92 shdr->StructureSize = cpu_to_le16(64);
93 shdr->Command = smb2_cmd;
Aurelien Aptel352d96f2020-05-31 12:38:22 -050094 if (server) {
Ross Lagerwall7d414f32016-09-20 13:37:13 +010095 spin_lock(&server->req_lock);
Steve French69dc4b12019-03-05 21:04:56 -060096 /* Request up to 10 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -050097 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070098 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +010099 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700100 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500101 min_t(int, server->max_credits -
Steve French69dc4b12019-03-05 21:04:56 -0600102 server->credits, 10));
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100103 spin_unlock(&server->req_lock);
104 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700105 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100106 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700107 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400108
109 if (!tcon)
110 goto out;
111
Steve French2b80d042013-06-23 18:43:37 -0500112 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
113 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500114 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700115 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500116 /* else CreditCharge MBZ */
117
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700118 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400119 /* Uid is not converted */
120 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700121 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500122
123 /*
124 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
125 * to pass the path on the Open SMB prefixed by \\server\share.
126 * Not sure when we would need to do the augmented path (if ever) and
127 * setting this flag breaks the SMB2 open operation since it is
128 * illegal to send an empty path name (without \\server\share prefix)
129 * when the DFS flag is set in the SMB open header. We could
130 * consider setting the flag on all operations other than open
131 * but it is safer to net set it for now.
132 */
133/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700134 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500135
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500136 if (server && server->sign && !smb3_encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700137 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400138out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400139 return;
140}
141
142static int
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500143smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
144 struct TCP_Server_Info *server)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400145{
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300146 int rc;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400147 struct nls_table *nls_codepage;
148 struct cifs_ses *ses;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200149 int retries;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400150
151 /*
152 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
153 * check for tcp and smb session status done differently
154 * for those three - in the calling routine.
155 */
156 if (tcon == NULL)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300157 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400158
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300159 if (smb2_command == SMB2_TREE_CONNECT)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300160 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400161
162 if (tcon->tidStatus == CifsExiting) {
163 /*
164 * only tree disconnect, open, and write,
165 * (and ulogoff which does not have tcon)
166 * are allowed as we start force umount.
167 */
168 if ((smb2_command != SMB2_WRITE) &&
169 (smb2_command != SMB2_CREATE) &&
170 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500171 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
172 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400173 return -ENODEV;
174 }
175 }
176 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500177 (!tcon->ses->server) || !server)
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400178 return -EIO;
179
180 ses = tcon->ses;
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200181 retries = server->nr_targets;
182
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400183 /*
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200184 * Give demultiplex thread up to 10 seconds to each target available for
185 * reconnect -- should be greater than cifs socket timeout which is 7
186 * seconds.
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400187 */
188 while (server->tcpStatus == CifsNeedReconnect) {
189 /*
190 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
191 * here since they are implicitly done when session drops.
192 */
193 switch (smb2_command) {
194 /*
195 * BB Should we keep oplock break and add flush to exceptions?
196 */
197 case SMB2_TREE_DISCONNECT:
198 case SMB2_CANCEL:
199 case SMB2_CLOSE:
200 case SMB2_OPLOCK_BREAK:
201 return -EAGAIN;
202 }
203
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300204 rc = wait_event_interruptible_timeout(server->response_q,
205 (server->tcpStatus != CifsNeedReconnect),
206 10 * HZ);
207 if (rc < 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700208 cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n",
209 __func__);
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300210 return -ERESTARTSYS;
211 }
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400212
213 /* are we still trying to reconnect? */
214 if (server->tcpStatus != CifsNeedReconnect)
215 break;
216
Ronnie Sahlbergc54849d2020-01-31 05:52:51 +1000217 if (retries && --retries)
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200218 continue;
219
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400220 /*
221 * on "soft" mounts we wait once. Hard mounts keep
222 * retrying until process is killed or server comes
223 * back on-line
224 */
225 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500226 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400227 return -EHOSTDOWN;
228 }
Paulo Alcantaraa3a53b72018-11-14 17:20:31 -0200229 retries = server->nr_targets;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400230 }
231
232 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300233 return 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400234
235 nls_codepage = load_nls_default();
236
237 /*
238 * need to prevent multiple threads trying to simultaneously reconnect
239 * the same SMB session
240 */
241 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200242
243 /*
244 * Recheck after acquire mutex. If another thread is negotiating
245 * and the server never sends an answer the socket will be closed
246 * and tcpStatus set to reconnect.
247 */
248 if (server->tcpStatus == CifsNeedReconnect) {
249 rc = -EHOSTDOWN;
250 mutex_unlock(&tcon->ses->session_mutex);
251 goto out;
252 }
253
Aurelien Aptel2f589672020-04-24 16:55:31 +0200254 /*
255 * If we are reconnecting an extra channel, bind
256 */
257 if (server->is_channel) {
258 ses->binding = true;
259 ses->binding_chan = cifs_ses_find_chan(ses, server);
260 }
261
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400262 rc = cifs_negotiate_protocol(0, tcon->ses);
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000263 if (!rc && tcon->ses->need_reconnect) {
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400264 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000265 if ((rc == -EACCES) && !tcon->retry) {
266 rc = -EHOSTDOWN;
Aurelien Aptel2f589672020-04-24 16:55:31 +0200267 ses->binding = false;
268 ses->binding_chan = NULL;
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000269 mutex_unlock(&tcon->ses->session_mutex);
270 goto failed;
271 }
272 }
Aurelien Aptel2f589672020-04-24 16:55:31 +0200273 /*
274 * End of channel binding
275 */
276 ses->binding = false;
277 ses->binding_chan = NULL;
278
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400279 if (rc || !tcon->need_reconnect) {
280 mutex_unlock(&tcon->ses->session_mutex);
281 goto out;
282 }
283
284 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800285 if (tcon->use_persistent)
286 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500287
Stefan Metzmacher565674d2020-07-21 09:36:38 -0300288 rc = cifs_tree_connect(0, tcon, nls_codepage);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400289 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500290
Joe Perchesf96637b2013-05-04 22:12:25 -0500291 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Steve Frenchc318e6c2018-04-04 14:08:52 -0500292 if (rc) {
293 /* If sess reconnected but tcon didn't, something strange ... */
Joe Perchesa0a30362020-04-14 22:42:53 -0700294 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400295 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500296 }
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800297
298 if (smb2_command != SMB2_INTERNAL_CMD)
Stefan Metzmacherb08484d2020-02-24 14:14:59 +0100299 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800300
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400301 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400302out:
303 /*
304 * Check if handle based operation so we know whether we can continue
305 * or not without returning to caller to reset file handle.
306 */
307 /*
308 * BB Is flush done by server on drop of tcp session? Should we special
309 * case it and skip above?
310 */
311 switch (smb2_command) {
312 case SMB2_FLUSH:
313 case SMB2_READ:
314 case SMB2_WRITE:
315 case SMB2_LOCK:
316 case SMB2_IOCTL:
317 case SMB2_QUERY_DIRECTORY:
318 case SMB2_CHANGE_NOTIFY:
319 case SMB2_QUERY_INFO:
320 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800321 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400322 }
Ronnie Sahlbergb0dd9402020-02-05 11:08:01 +1000323failed:
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400324 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400325 return rc;
326}
327
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700328static void
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500329fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
330 struct TCP_Server_Info *server,
331 void *buf,
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700332 unsigned int *total_len)
333{
334 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
335 /* lookup word count ie StructureSize from table */
336 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
337
338 /*
339 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
340 * largest operations (Create)
341 */
342 memset(buf, 0, 256);
343
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500344 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon, server);
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700345 spdu->StructureSize2 = cpu_to_le16(parmsize);
346
347 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
348}
349
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100350/*
351 * Allocate and return pointer to an SMB request hdr, and set basic
352 * SMB information in the SMB header. If the return code is zero, this
353 * function must have filled in request_buf pointer.
354 */
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300355static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500356 struct TCP_Server_Info *server,
357 void **request_buf, unsigned int *total_len)
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800358{
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800359 /* BB eventually switch this to SMB2 specific small buf size */
Stefano Briviof46ecbd2018-07-05 11:46:42 +0200360 if (smb2_command == SMB2_SET_INFO)
361 *request_buf = cifs_buf_get();
362 else
363 *request_buf = cifs_small_buf_get();
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800364 if (*request_buf == NULL) {
365 /* BB should we add a retry in here if not a writepage? */
366 return -ENOMEM;
367 }
368
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500369 fill_small_buf(smb2_command, tcon, server,
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100370 (struct smb2_sync_hdr *)(*request_buf),
371 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400372
373 if (tcon != NULL) {
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400374 uint16_t com_code = le16_to_cpu(smb2_command);
375 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400376 cifs_stats_inc(&tcon->num_smbs_sent);
377 }
378
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300379 return 0;
380}
381
382static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500383 struct TCP_Server_Info *server,
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300384 void **request_buf, unsigned int *total_len)
385{
386 int rc;
387
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500388 rc = smb2_reconnect(smb2_command, tcon, server);
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300389 if (rc)
390 return rc;
391
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500392 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300393 total_len);
394}
395
396static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500397 struct TCP_Server_Info *server,
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300398 void **request_buf, unsigned int *total_len)
399{
400 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
401 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500402 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
403 request_buf, total_len);
Paulo Alcantara (SUSE)84a1f5b2019-11-22 12:30:53 -0300404 }
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500405 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
406 request_buf, total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400407}
408
Steve Frenchd7bef4c2019-04-18 11:03:58 -0500409/* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500410
411static void
412build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
413{
414 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
415 pneg_ctxt->DataLength = cpu_to_le16(38);
416 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
Steve French7955f102020-12-09 22:19:00 -0600417 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_LINUX_CLIENT_SALT_SIZE);
418 get_random_bytes(pneg_ctxt->Salt, SMB311_LINUX_CLIENT_SALT_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500419 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
420}
421
422static void
Steve French26ea8882019-04-26 20:36:08 -0700423build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
424{
425 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
426 pneg_ctxt->DataLength =
427 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
428 - sizeof(struct smb2_neg_context));
429 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
430 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
431 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
432 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
433}
434
Steve French53d31a32021-07-05 15:05:39 -0500435static unsigned int
436build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
437{
438 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
439 unsigned short num_algs = 1; /* number of signing algorithms sent */
440
441 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
442 /*
443 * Context Data length must be rounded to multiple of 8 for some servers
444 */
445 pneg_ctxt->DataLength = cpu_to_le16(DIV_ROUND_UP(
446 sizeof(struct smb2_signing_capabilities) -
447 sizeof(struct smb2_neg_context) +
448 (num_algs * 2 /* sizeof u16 */), 8) * 8);
449 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
450 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
451
452 ctxt_len += 2 /* sizeof le16 */ * num_algs;
453 ctxt_len = DIV_ROUND_UP(ctxt_len, 8) * 8;
454 return ctxt_len;
455 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
456}
457
Steve French26ea8882019-04-26 20:36:08 -0700458static void
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500459build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
460{
461 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
Steve Frenchfbfd0b42020-09-11 16:19:28 -0500462 if (require_gcm_256) {
463 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
464 pneg_ctxt->CipherCount = cpu_to_le16(1);
465 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
Steve French29e27922020-10-14 20:24:09 -0500466 } else if (enable_gcm_256) {
467 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
468 pneg_ctxt->CipherCount = cpu_to_le16(3);
469 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
470 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
471 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
Steve Frenchfbfd0b42020-09-11 16:19:28 -0500472 } else {
473 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
474 pneg_ctxt->CipherCount = cpu_to_le16(2);
475 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
476 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
477 }
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500478}
479
Steve French96d3cca2019-06-25 04:39:51 -0500480static unsigned int
481build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
482{
483 struct nls_table *cp = load_nls_default();
484
485 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
486
487 /* copy up to max of first 100 bytes of server name to NetName field */
Steve Frenchdf58fae2019-08-05 17:07:26 -0500488 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
Steve French96d3cca2019-06-25 04:39:51 -0500489 /* context size is DataLength + minimal smb2_neg_context */
490 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
491 sizeof(struct smb2_neg_context), 8) * 8;
492}
493
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500494static void
Steve Frenchfcef0db2018-05-19 20:45:27 -0500495build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
496{
497 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
498 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
Steve French0d481322019-02-24 17:56:33 -0600499 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
500 pneg_ctxt->Name[0] = 0x93;
501 pneg_ctxt->Name[1] = 0xAD;
502 pneg_ctxt->Name[2] = 0x25;
503 pneg_ctxt->Name[3] = 0x50;
504 pneg_ctxt->Name[4] = 0x9C;
505 pneg_ctxt->Name[5] = 0xB4;
506 pneg_ctxt->Name[6] = 0x11;
507 pneg_ctxt->Name[7] = 0xE7;
508 pneg_ctxt->Name[8] = 0xB4;
509 pneg_ctxt->Name[9] = 0x23;
510 pneg_ctxt->Name[10] = 0x83;
511 pneg_ctxt->Name[11] = 0xDE;
512 pneg_ctxt->Name[12] = 0x96;
513 pneg_ctxt->Name[13] = 0x8B;
514 pneg_ctxt->Name[14] = 0xCD;
515 pneg_ctxt->Name[15] = 0x7C;
Steve Frenchfcef0db2018-05-19 20:45:27 -0500516}
517
518static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100519assemble_neg_contexts(struct smb2_negotiate_req *req,
Steve French9fe5ff12019-06-24 20:39:04 -0500520 struct TCP_Server_Info *server, unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500521{
Colin Ian Kinga9f76cf2019-12-02 18:59:42 +0000522 char *pneg_ctxt;
Steve French53d31a32021-07-05 15:05:39 -0500523 unsigned int ctxt_len, neg_context_count;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500524
Steve Frenchd5c70762019-01-03 02:37:21 -0600525 if (*total_len > 200) {
526 /* In case length corrupted don't want to overrun smb buffer */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000527 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
Steve Frenchd5c70762019-01-03 02:37:21 -0600528 return;
529 }
530
531 /*
532 * round up total_len of fixed part of SMB3 negotiate request to 8
533 * byte boundary before adding negotiate contexts
534 */
535 *total_len = roundup(*total_len, 8);
536
537 pneg_ctxt = (*total_len) + (char *)req;
538 req->NegotiateContextOffset = cpu_to_le32(*total_len);
539
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500540 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500541 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
542 *total_len += ctxt_len;
543 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100544
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500545 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500546 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
547 *total_len += ctxt_len;
548 pneg_ctxt += ctxt_len;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100549
Steve French53d31a32021-07-05 15:05:39 -0500550 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
551 server->hostname);
552 *total_len += ctxt_len;
553 pneg_ctxt += ctxt_len;
554
555 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
556 *total_len += sizeof(struct smb2_posix_neg_context);
557 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
558
559 neg_context_count = 4;
560
Steve French9fe5ff12019-06-24 20:39:04 -0500561 if (server->compress_algorithm) {
562 build_compression_ctxt((struct smb2_compression_capabilities_context *)
Steve French26ea8882019-04-26 20:36:08 -0700563 pneg_ctxt);
Steve French9fe5ff12019-06-24 20:39:04 -0500564 ctxt_len = DIV_ROUND_UP(
565 sizeof(struct smb2_compression_capabilities_context),
566 8) * 8;
567 *total_len += ctxt_len;
568 pneg_ctxt += ctxt_len;
Steve French53d31a32021-07-05 15:05:39 -0500569 neg_context_count++;
570 }
Steve French96d3cca2019-06-25 04:39:51 -0500571
Steve French53d31a32021-07-05 15:05:39 -0500572 if (enable_negotiate_signing) {
573 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
574 pneg_ctxt);
575 *total_len += ctxt_len;
576 pneg_ctxt += ctxt_len;
577 neg_context_count++;
578 }
Steve French96d3cca2019-06-25 04:39:51 -0500579
Steve French53d31a32021-07-05 15:05:39 -0500580 /* check for and add transport_capabilities and signing capabilities */
581 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
582
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500583}
Steve French5100d8a2018-04-09 10:47:14 -0500584
585static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
586{
587 unsigned int len = le16_to_cpu(ctxt->DataLength);
588
589 /* If invalid preauth context warn but use what we requested, SHA-512 */
590 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700591 pr_warn_once("server sent bad preauth context\n");
Steve French5100d8a2018-04-09 10:47:14 -0500592 return;
Steve French7955f102020-12-09 22:19:00 -0600593 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
594 pr_warn_once("server sent invalid SaltLength\n");
595 return;
Steve French5100d8a2018-04-09 10:47:14 -0500596 }
597 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
Joe Perchesa0a30362020-04-14 22:42:53 -0700598 pr_warn_once("Invalid SMB3 hash algorithm count\n");
Steve French5100d8a2018-04-09 10:47:14 -0500599 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
Joe Perchesa0a30362020-04-14 22:42:53 -0700600 pr_warn_once("unknown SMB3 hash algorithm\n");
Steve French5100d8a2018-04-09 10:47:14 -0500601}
602
Steve French26ea8882019-04-26 20:36:08 -0700603static void decode_compress_ctx(struct TCP_Server_Info *server,
604 struct smb2_compression_capabilities_context *ctxt)
605{
606 unsigned int len = le16_to_cpu(ctxt->DataLength);
607
608 /* sizeof compress context is a one element compression capbility struct */
609 if (len < 10) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700610 pr_warn_once("server sent bad compression cntxt\n");
Steve French26ea8882019-04-26 20:36:08 -0700611 return;
612 }
613 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700614 pr_warn_once("Invalid SMB3 compress algorithm count\n");
Steve French26ea8882019-04-26 20:36:08 -0700615 return;
616 }
617 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700618 pr_warn_once("unknown compression algorithm\n");
Steve French26ea8882019-04-26 20:36:08 -0700619 return;
620 }
621 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
622}
623
Steve French5100d8a2018-04-09 10:47:14 -0500624static int decode_encrypt_ctx(struct TCP_Server_Info *server,
625 struct smb2_encryption_neg_context *ctxt)
626{
627 unsigned int len = le16_to_cpu(ctxt->DataLength);
628
629 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
630 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700631 pr_warn_once("server sent bad crypto ctxt len\n");
Steve French5100d8a2018-04-09 10:47:14 -0500632 return -EINVAL;
633 }
634
635 if (le16_to_cpu(ctxt->CipherCount) != 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700636 pr_warn_once("Invalid SMB3.11 cipher count\n");
Steve French5100d8a2018-04-09 10:47:14 -0500637 return -EINVAL;
638 }
639 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
Steve French511ac892020-10-15 00:14:47 -0500640 if (require_gcm_256) {
641 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
642 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
643 return -EOPNOTSUPP;
644 }
645 } else if (ctxt->Ciphers[0] == 0) {
Steve Frenchacf96fe2020-10-17 03:54:27 -0500646 /*
647 * e.g. if server only supported AES256_CCM (very unlikely)
648 * or server supported no encryption types or had all disabled.
649 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
650 * in which mount requested encryption ("seal") checks later
651 * on during tree connection will return proper rc, but if
652 * seal not requested by client, since server is allowed to
653 * return 0 to indicate no supported cipher, we can't fail here
654 */
655 server->cipher_type = 0;
656 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
657 pr_warn_once("Server does not support requested encryption types\n");
658 return 0;
Steve French511ac892020-10-15 00:14:47 -0500659 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
660 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
661 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
662 /* server returned a cipher we didn't ask for */
Joe Perchesa0a30362020-04-14 22:42:53 -0700663 pr_warn_once("Invalid SMB3.11 cipher returned\n");
Steve French5100d8a2018-04-09 10:47:14 -0500664 return -EINVAL;
665 }
666 server->cipher_type = ctxt->Ciphers[0];
Steve French23657ad2018-04-22 15:14:58 -0500667 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
Steve French5100d8a2018-04-09 10:47:14 -0500668 return 0;
669}
670
Steve French53d31a32021-07-05 15:05:39 -0500671static void decode_signing_ctx(struct TCP_Server_Info *server,
672 struct smb2_signing_capabilities *pctxt)
673{
674 unsigned int len = le16_to_cpu(pctxt->DataLength);
675
676 if ((len < 4) || (len > 16)) {
677 pr_warn_once("server sent bad signing negcontext\n");
678 return;
679 }
680 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
681 pr_warn_once("Invalid signing algorithm count\n");
682 return;
683 }
684 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
685 pr_warn_once("unknown signing algorithm\n");
686 return;
687 }
688
689 server->signing_negotiated = true;
690 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
691 cifs_dbg(FYI, "signing algorithm %d chosen\n",
692 server->signing_algorithm);
693}
694
695
Steve French5100d8a2018-04-09 10:47:14 -0500696static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000697 struct TCP_Server_Info *server,
698 unsigned int len_of_smb)
Steve French5100d8a2018-04-09 10:47:14 -0500699{
700 struct smb2_neg_context *pctx;
701 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
702 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
Steve French5100d8a2018-04-09 10:47:14 -0500703 unsigned int len_of_ctxts, i;
704 int rc = 0;
705
706 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
707 if (len_of_smb <= offset) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000708 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
Steve French5100d8a2018-04-09 10:47:14 -0500709 return -EINVAL;
710 }
711
712 len_of_ctxts = len_of_smb - offset;
713
714 for (i = 0; i < ctxt_cnt; i++) {
715 int clen;
716 /* check that offset is not beyond end of SMB */
717 if (len_of_ctxts == 0)
718 break;
719
720 if (len_of_ctxts < sizeof(struct smb2_neg_context))
721 break;
722
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +1000723 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
Steve French5100d8a2018-04-09 10:47:14 -0500724 clen = le16_to_cpu(pctx->DataLength);
725 if (clen > len_of_ctxts)
726 break;
727
728 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
729 decode_preauth_context(
730 (struct smb2_preauth_neg_context *)pctx);
731 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
732 rc = decode_encrypt_ctx(server,
733 (struct smb2_encryption_neg_context *)pctx);
Steve French26ea8882019-04-26 20:36:08 -0700734 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
735 decode_compress_ctx(server,
736 (struct smb2_compression_capabilities_context *)pctx);
Steve Frenchfcef0db2018-05-19 20:45:27 -0500737 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
738 server->posix_ext_supported = true;
Steve French53d31a32021-07-05 15:05:39 -0500739 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
740 decode_signing_ctx(server,
741 (struct smb2_signing_capabilities *)pctx);
Steve French5100d8a2018-04-09 10:47:14 -0500742 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000743 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
Steve French5100d8a2018-04-09 10:47:14 -0500744 le16_to_cpu(pctx->ContextType));
745
746 if (rc)
747 break;
748 /* offsets must be 8 byte aligned */
749 clen = (clen + 7) & ~0x7;
750 offset += clen + sizeof(struct smb2_neg_context);
751 len_of_ctxts -= clen;
752 }
753 return rc;
754}
755
Steve Frenchce558b02018-05-31 19:16:54 -0500756static struct create_posix *
757create_posix_buf(umode_t mode)
758{
759 struct create_posix *buf;
760
761 buf = kzalloc(sizeof(struct create_posix),
762 GFP_KERNEL);
763 if (!buf)
764 return NULL;
765
766 buf->ccontext.DataOffset =
767 cpu_to_le16(offsetof(struct create_posix, Mode));
768 buf->ccontext.DataLength = cpu_to_le32(4);
769 buf->ccontext.NameOffset =
770 cpu_to_le16(offsetof(struct create_posix, Name));
771 buf->ccontext.NameLength = cpu_to_le16(16);
772
773 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
774 buf->Name[0] = 0x93;
775 buf->Name[1] = 0xAD;
776 buf->Name[2] = 0x25;
777 buf->Name[3] = 0x50;
778 buf->Name[4] = 0x9C;
779 buf->Name[5] = 0xB4;
780 buf->Name[6] = 0x11;
781 buf->Name[7] = 0xE7;
782 buf->Name[8] = 0xB4;
783 buf->Name[9] = 0x23;
784 buf->Name[10] = 0x83;
785 buf->Name[11] = 0xDE;
786 buf->Name[12] = 0x96;
787 buf->Name[13] = 0x8B;
788 buf->Name[14] = 0xCD;
789 buf->Name[15] = 0x7C;
790 buf->Mode = cpu_to_le32(mode);
Joe Perchesa0a30362020-04-14 22:42:53 -0700791 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
Steve Frenchce558b02018-05-31 19:16:54 -0500792 return buf;
793}
794
795static int
796add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
797{
798 struct smb2_create_req *req = iov[0].iov_base;
799 unsigned int num = *num_iovec;
800
801 iov[num].iov_base = create_posix_buf(mode);
Steve Frenchd0959b02019-10-05 10:53:58 -0500802 if (mode == ACL_NO_MODE)
Joe Perchesa0a30362020-04-14 22:42:53 -0700803 cifs_dbg(FYI, "Invalid mode\n");
Steve Frenchce558b02018-05-31 19:16:54 -0500804 if (iov[num].iov_base == NULL)
805 return -ENOMEM;
806 iov[num].iov_len = sizeof(struct create_posix);
807 if (!req->CreateContextsOffset)
808 req->CreateContextsOffset = cpu_to_le32(
809 sizeof(struct smb2_create_req) +
810 iov[num - 1].iov_len);
811 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
812 *num_iovec = num + 1;
813 return 0;
814}
815
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500816
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400817/*
818 *
819 * SMB2 Worker functions follow:
820 *
821 * The general structure of the worker functions is:
822 * 1) Call smb2_init (assembles SMB2 header)
823 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
824 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
825 * 4) Decode SMB2 command specific fields in the fixed length area
826 * 5) Decode variable length data area (if any for this SMB2 command type)
827 * 6) Call free smb buffer
828 * 7) return
829 *
830 */
831
832int
833SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
834{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000835 struct smb_rqst rqst;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400836 struct smb2_negotiate_req *req;
837 struct smb2_negotiate_rsp *rsp;
838 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700839 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400840 int rc = 0;
841 int resp_buftype;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200842 struct TCP_Server_Info *server = cifs_ses_server(ses);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400843 int blob_offset, blob_length;
844 char *security_blob;
845 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100846 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400847
Joe Perchesf96637b2013-05-04 22:12:25 -0500848 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400849
Jeff Layton3534b852013-05-24 07:41:01 -0400850 if (!server) {
851 WARN(1, "%s: server is NULL!\n", __func__);
852 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400853 }
854
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500855 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
856 (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400857 if (rc)
858 return rc;
859
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100860 req->sync_hdr.SessionId = 0;
Steve French0fdfef92018-06-28 19:30:23 -0500861
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100862 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
863 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400864
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200865 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500866 SMB3ANY_VERSION_STRING) == 0) {
867 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
868 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
Steve French6dffa4c2021-02-02 00:03:58 -0600869 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
870 req->DialectCount = cpu_to_le16(3);
871 total_len += 6;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000872 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500873 SMBDEFAULT_VERSION_STRING) == 0) {
874 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
875 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
876 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -0600877 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
878 req->DialectCount = cpu_to_le16(4);
879 total_len += 8;
Steve French9764c022017-09-17 10:41:35 -0500880 } else {
881 /* otherwise send specific dialect */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200882 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
Steve French9764c022017-09-17 10:41:35 -0500883 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100884 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500885 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400886
887 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400888 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500889 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400890 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500891 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400892 else
893 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400894
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000895 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
Steve French679971e2021-05-07 18:24:11 -0500896 if (ses->chan_max > 1)
897 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400898
Steve French3c5f9be12014-05-13 13:37:45 -0700899 /* ClientGUID must be zero for SMB2.02 dialect */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000900 if (server->vals->protocol_id == SMB20_PROT_ID)
Steve French3c5f9be12014-05-13 13:37:45 -0700901 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500902 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700903 memcpy(req->ClientGUID, server->client_guid,
904 SMB2_CLIENT_GUID_SIZE);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000905 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
906 (strcmp(server->vals->version_string,
Steve French6dffa4c2021-02-02 00:03:58 -0600907 SMB3ANY_VERSION_STRING) == 0) ||
908 (strcmp(server->vals->version_string,
Steve Frenchd5c70762019-01-03 02:37:21 -0600909 SMBDEFAULT_VERSION_STRING) == 0))
Steve French9fe5ff12019-06-24 20:39:04 -0500910 assemble_neg_contexts(req, server, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500911 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400912 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100913 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400914
Ronnie Sahlberg40eff452018-06-12 08:00:59 +1000915 memset(&rqst, 0, sizeof(struct smb_rqst));
916 rqst.rq_iov = iov;
917 rqst.rq_nvec = 1;
918
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500919 rc = cifs_send_recv(xid, ses, server,
920 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700921 cifs_small_buf_release(req);
922 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400923 /*
924 * No tcon so can't do
925 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
926 */
Steve French7e682f72017-08-31 21:34:24 -0500927 if (rc == -EOPNOTSUPP) {
Joe Perchesa0a30362020-04-14 22:42:53 -0700928 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 -0500929 goto neg_exit;
930 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400931 goto neg_exit;
932
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000933 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500934 SMB3ANY_VERSION_STRING) == 0) {
935 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000936 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500937 "SMB2 dialect returned but not requested\n");
938 return -EIO;
939 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000940 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500941 "SMB2.1 dialect returned but not requested\n");
942 return -EIO;
Steve French6dffa4c2021-02-02 00:03:58 -0600943 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
944 /* ops set to 3.0 by default for default so update */
945 server->ops = &smb311_operations;
946 server->vals = &smb311_values;
Steve French9764c022017-09-17 10:41:35 -0500947 }
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000948 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -0500949 SMBDEFAULT_VERSION_STRING) == 0) {
950 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000951 cifs_server_dbg(VFS,
Steve French9764c022017-09-17 10:41:35 -0500952 "SMB2 dialect returned but not requested\n");
953 return -EIO;
954 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
955 /* ops set to 3.0 by default for default so update */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000956 server->ops = &smb21_operations;
957 server->vals = &smb21_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800958 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000959 server->ops = &smb311_operations;
960 server->vals = &smb311_values;
ZhangXiaoxub57a55e2019-04-06 15:30:38 +0800961 }
Steve French590d08d2017-09-19 11:43:47 -0500962 } else if (le16_to_cpu(rsp->DialectRevision) !=
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000963 server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500964 /* if requested single dialect ensure returned dialect matched */
Joe Perchesa0a30362020-04-14 22:42:53 -0700965 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
966 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500967 return -EIO;
968 }
969
Joe Perchesf96637b2013-05-04 22:12:25 -0500970 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400971
Steve Frenche4aa25e2012-10-01 12:26:22 -0500972 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500973 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500974 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500975 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500976 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500977 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500978 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
979 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600980 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
981 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400982 else {
Joe Perchesa0a30362020-04-14 22:42:53 -0700983 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
984 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400985 rc = -EIO;
986 goto neg_exit;
987 }
988 server->dialect = le16_to_cpu(rsp->DialectRevision);
989
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100990 /*
991 * Keep a copy of the hash after negprot. This hash will be
992 * the starting hash value for all sessions made from this
993 * server.
994 */
995 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
996 SMB2_PREAUTH_HASH_SIZE);
Steve French0fdfef92018-06-28 19:30:23 -0500997
Jeff Laytone598d1d82013-05-26 07:00:59 -0400998 /* SMB2 only has an extended negflavor */
999 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +04001000 /* set it to the maximum buffer size value we can send with 1 credit */
1001 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1002 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001003 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1004 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001005 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Steve French07108d02018-04-01 20:15:55 -05001006 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1007 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1008 server->sec_mode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001009 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001010 /* Internal types */
1011 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001012
Aurelien Aptel6d2fcfe2021-05-21 17:19:27 +02001013 /*
1014 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1015 * Set the cipher type manually.
1016 */
1017 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1018 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1019
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001020 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001021 (struct smb2_sync_hdr *)rsp);
Steve French5d875cc2013-06-25 15:33:41 -05001022 /*
1023 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1024 * for us will be
1025 * ses->sectype = RawNTLMSSP;
1026 * but for time being this is our only auth choice so doesn't matter.
1027 * We just found a server which sets blob length to zero expecting raw.
1028 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -07001029 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -05001030 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -07001031 server->sec_ntlmssp = true;
1032 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001033
Jeff Layton38d77c52013-05-26 07:01:00 -04001034 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -04001035 if (rc)
1036 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -05001037 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -05001038 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -05001039 if (rc == 1)
1040 rc = 0;
1041 else if (rc == 0)
1042 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001043 }
Steve French5100d8a2018-04-09 10:47:14 -05001044
Steve French5100d8a2018-04-09 10:47:14 -05001045 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1046 if (rsp->NegotiateContextCount)
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10001047 rc = smb311_decode_neg_context(rsp, server,
1048 rsp_iov.iov_len);
Steve French5100d8a2018-04-09 10:47:14 -05001049 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001050 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
Steve French5100d8a2018-04-09 10:47:14 -05001051 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001052neg_exit:
1053 free_rsp_buf(resp_buftype, rsp);
1054 return rc;
1055}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001056
Steve Frenchff1c0382013-11-19 23:44:46 -06001057int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1058{
Long Li2796d302018-04-25 11:30:04 -07001059 int rc;
1060 struct validate_negotiate_info_req *pneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001061 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -06001062 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -05001063 u32 inbuflen; /* max of 4 dialects */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001064 struct TCP_Server_Info *server = tcon->ses->server;
Steve Frenchff1c0382013-11-19 23:44:46 -06001065
1066 cifs_dbg(FYI, "validate negotiate\n");
1067
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001068 /* In SMB3.11 preauth integrity supersedes validate negotiate */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001069 if (server->dialect == SMB311_PROT_ID)
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001070 return 0;
1071
Steve Frenchff1c0382013-11-19 23:44:46 -06001072 /*
1073 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -05001074 * can not sign it (ie are not known user). Even if signing is not
1075 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -06001076 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -05001077 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -06001078 */
Steve French0603c962017-09-20 19:57:18 -05001079 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -06001080 return 0; /* validation requires signing */
1081
Steve French0603c962017-09-20 19:57:18 -05001082 if (tcon->ses->user_name == NULL) {
1083 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1084 return 0; /* validation requires signing */
1085 }
1086
1087 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001088 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
Steve French0603c962017-09-20 19:57:18 -05001089
Long Li2796d302018-04-25 11:30:04 -07001090 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1091 if (!pneg_inbuf)
1092 return -ENOMEM;
1093
1094 pneg_inbuf->Capabilities =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001095 cpu_to_le32(server->vals->req_capabilities);
Steve French679971e2021-05-07 18:24:11 -05001096 if (tcon->ses->chan_max > 1)
1097 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1098
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001099 memcpy(pneg_inbuf->Guid, server->client_guid,
Sachin Prabhu39552ea2014-05-13 00:48:12 +01001100 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -06001101
1102 if (tcon->ses->sign)
Long Li2796d302018-04-25 11:30:04 -07001103 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001104 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1105 else if (global_secflags & CIFSSEC_MAY_SIGN)
Long Li2796d302018-04-25 11:30:04 -07001106 pneg_inbuf->SecurityMode =
Steve Frenchff1c0382013-11-19 23:44:46 -06001107 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1108 else
Long Li2796d302018-04-25 11:30:04 -07001109 pneg_inbuf->SecurityMode = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001110
Steve French9764c022017-09-17 10:41:35 -05001111
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001112 if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001113 SMB3ANY_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001114 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1115 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
Steve French6dffa4c2021-02-02 00:03:58 -06001116 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1117 pneg_inbuf->DialectCount = cpu_to_le16(3);
1118 /* SMB 2.1 not included so subtract one dialect from len */
Long Li2796d302018-04-25 11:30:04 -07001119 inbuflen = sizeof(*pneg_inbuf) -
Steve French6dffa4c2021-02-02 00:03:58 -06001120 (sizeof(pneg_inbuf->Dialects[0]));
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001121 } else if (strcmp(server->vals->version_string,
Steve French9764c022017-09-17 10:41:35 -05001122 SMBDEFAULT_VERSION_STRING) == 0) {
Long Li2796d302018-04-25 11:30:04 -07001123 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1124 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1125 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
Steve Frenchd5c70762019-01-03 02:37:21 -06001126 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1127 pneg_inbuf->DialectCount = cpu_to_le16(4);
Steve French6dffa4c2021-02-02 00:03:58 -06001128 /* structure is big enough for 4 dialects */
Long Li2796d302018-04-25 11:30:04 -07001129 inbuflen = sizeof(*pneg_inbuf);
Steve French9764c022017-09-17 10:41:35 -05001130 } else {
1131 /* otherwise specific dialect was requested */
Long Li2796d302018-04-25 11:30:04 -07001132 pneg_inbuf->Dialects[0] =
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001133 cpu_to_le16(server->vals->protocol_id);
Long Li2796d302018-04-25 11:30:04 -07001134 pneg_inbuf->DialectCount = cpu_to_le16(1);
Steve French9764c022017-09-17 10:41:35 -05001135 /* structure is big enough for 3 dialects, sending only 1 */
Long Li2796d302018-04-25 11:30:04 -07001136 inbuflen = sizeof(*pneg_inbuf) -
1137 sizeof(pneg_inbuf->Dialects[0]) * 2;
Steve French9764c022017-09-17 10:41:35 -05001138 }
Steve Frenchff1c0382013-11-19 23:44:46 -06001139
1140 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1141 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001142 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1143 (char **)&pneg_rsp, &rsplen);
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001144 if (rc == -EOPNOTSUPP) {
1145 /*
1146 * Old Windows versions or Netapp SMB server can return
1147 * not supported error. Client should accept it.
1148 */
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001149 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
Colin Ian King21078202019-05-17 09:12:33 +01001150 rc = 0;
1151 goto out_free_inbuf;
Namjae Jeon969ae8e2019-01-22 09:46:45 +09001152 } else if (rc != 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001153 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1154 rc);
Long Li2796d302018-04-25 11:30:04 -07001155 rc = -EIO;
1156 goto out_free_inbuf;
Steve Frenchff1c0382013-11-19 23:44:46 -06001157 }
1158
Long Li2796d302018-04-25 11:30:04 -07001159 rc = -EIO;
1160 if (rsplen != sizeof(*pneg_rsp)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001161 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1162 rsplen);
Steve French7db0a6e2017-05-03 21:12:20 -05001163
1164 /* relax check since Mac returns max bufsize allowed on ioctl */
Long Li2796d302018-04-25 11:30:04 -07001165 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1166 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001167 }
1168
1169 /* check validate negotiate info response matches what we got earlier */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001170 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -06001171 goto vneg_out;
1172
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001173 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
Steve Frenchff1c0382013-11-19 23:44:46 -06001174 goto vneg_out;
1175
1176 /* do not validate server guid because not saved at negprot time yet */
1177
1178 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001179 SMB2_LARGE_FILES) != server->capabilities)
Steve Frenchff1c0382013-11-19 23:44:46 -06001180 goto vneg_out;
1181
1182 /* validate negotiate successful */
Long Li2796d302018-04-25 11:30:04 -07001183 rc = 0;
Steve Frenchff1c0382013-11-19 23:44:46 -06001184 cifs_dbg(FYI, "validate negotiate info successful\n");
Long Li2796d302018-04-25 11:30:04 -07001185 goto out_free_rsp;
Steve Frenchff1c0382013-11-19 23:44:46 -06001186
1187vneg_out:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001188 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
Long Li2796d302018-04-25 11:30:04 -07001189out_free_rsp:
David Disseldorpfe83bebc2017-10-20 14:49:37 +02001190 kfree(pneg_rsp);
Long Li2796d302018-04-25 11:30:04 -07001191out_free_inbuf:
1192 kfree(pneg_inbuf);
1193 return rc;
Steve Frenchff1c0382013-11-19 23:44:46 -06001194}
1195
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301196enum securityEnum
1197smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1198{
1199 switch (requested) {
1200 case Kerberos:
1201 case RawNTLMSSP:
1202 return requested;
1203 case NTLMv2:
1204 return RawNTLMSSP;
1205 case Unspecified:
1206 if (server->sec_ntlmssp &&
1207 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1208 return RawNTLMSSP;
1209 if ((server->sec_kerberos || server->sec_mskerberos) &&
1210 (global_secflags & CIFSSEC_MAY_KRB5))
1211 return Kerberos;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001212 fallthrough;
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301213 default:
1214 return Unspecified;
1215 }
1216}
1217
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001218struct SMB2_sess_data {
1219 unsigned int xid;
1220 struct cifs_ses *ses;
1221 struct nls_table *nls_cp;
1222 void (*func)(struct SMB2_sess_data *);
1223 int result;
1224 u64 previous_session;
1225
1226 /* we will send the SMB in three pieces:
1227 * a fixed length beginning part, an optional
1228 * SPNEGO blob (which can be zero length), and a
1229 * last part which will include the strings
1230 * and rest of bcc area. This allows us to avoid
1231 * a large buffer 17K allocation
1232 */
1233 int buf0_type;
1234 struct kvec iov[2];
1235};
1236
1237static int
1238SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1239{
1240 int rc;
1241 struct cifs_ses *ses = sess_data->ses;
1242 struct smb2_sess_setup_req *req;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001243 struct TCP_Server_Info *server = cifs_ses_server(ses);
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001244 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001245
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001246 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1247 (void **) &req,
1248 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001249 if (rc)
1250 return rc;
1251
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001252 if (sess_data->ses->binding) {
1253 req->sync_hdr.SessionId = sess_data->ses->Suid;
1254 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1255 req->PreviousSessionId = 0;
1256 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1257 } else {
1258 /* First session, not a reauthenticate */
1259 req->sync_hdr.SessionId = 0;
1260 /*
1261 * if reconnect, we need to send previous sess id
1262 * otherwise it is 0
1263 */
1264 req->PreviousSessionId = sess_data->previous_session;
1265 req->Flags = 0; /* MBZ */
1266 }
Steve Frenchd4090142018-06-13 17:05:58 -05001267
1268 /* enough to enable echos and oplocks and one max size write */
1269 req->sync_hdr.CreditRequest = cpu_to_le16(130);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001270
1271 /* only one of SMB2 signing flags may be set in SMB2 request */
1272 if (server->sign)
1273 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1274 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1275 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1276 else
1277 req->SecurityMode = 0;
1278
Steve French8d330962019-07-25 18:13:10 -05001279#ifdef CONFIG_CIFS_DFS_UPCALL
1280 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1281#else
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001282 req->Capabilities = 0;
Steve French8d330962019-07-25 18:13:10 -05001283#endif /* DFS_UPCALL */
1284
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001285 req->Channel = 0; /* MBZ */
1286
1287 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001288 /* 1 for pad */
1289 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001290 /*
1291 * This variable will be used to clear the buffer
1292 * allocated above in case of any error in the calling function.
1293 */
1294 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1295
1296 return 0;
1297}
1298
1299static void
1300SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1301{
1302 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1303 sess_data->buf0_type = CIFS_NO_BUFFER;
1304}
1305
1306static int
1307SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1308{
1309 int rc;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001310 struct smb_rqst rqst;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001311 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001312 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001313
1314 /* Testing shows that buffer offset must be at location of Buffer[0] */
1315 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001316 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001317 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1318
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001319 memset(&rqst, 0, sizeof(struct smb_rqst));
1320 rqst.rq_iov = sess_data->iov;
1321 rqst.rq_nvec = 2;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001322
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001323 /* BB add code to build os and lm fields */
1324 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001325 cifs_ses_server(sess_data->ses),
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001326 &rqst,
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001327 &sess_data->buf0_type,
Shyam Prasad N0f56db82021-02-03 22:49:52 -08001328 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001329 cifs_small_buf_release(sess_data->iov[0].iov_base);
1330 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001331
1332 return rc;
1333}
1334
1335static int
1336SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1337{
1338 int rc = 0;
1339 struct cifs_ses *ses = sess_data->ses;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001340 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001341
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001342 mutex_lock(&server->srv_mutex);
1343 if (server->ops->generate_signingkey) {
1344 rc = server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001345 if (rc) {
1346 cifs_dbg(FYI,
1347 "SMB3 session key generation failed\n");
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001348 mutex_unlock(&server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -08001349 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001350 }
1351 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001352 if (!server->session_estab) {
1353 server->sequence_number = 0x2;
1354 server->session_estab = true;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001355 }
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001356 mutex_unlock(&server->srv_mutex);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001357
1358 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001359 /* keep existing ses state if binding */
1360 if (!ses->binding) {
1361 spin_lock(&GlobalMid_Lock);
1362 ses->status = CifsGood;
1363 ses->need_reconnect = false;
1364 spin_unlock(&GlobalMid_Lock);
1365 }
1366
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001367 return rc;
1368}
1369
1370#ifdef CONFIG_CIFS_UPCALL
1371static void
1372SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1373{
1374 int rc;
1375 struct cifs_ses *ses = sess_data->ses;
1376 struct cifs_spnego_msg *msg;
1377 struct key *spnego_key = NULL;
1378 struct smb2_sess_setup_rsp *rsp = NULL;
1379
1380 rc = SMB2_sess_alloc_buffer(sess_data);
1381 if (rc)
1382 goto out;
1383
1384 spnego_key = cifs_get_spnego_key(ses);
1385 if (IS_ERR(spnego_key)) {
1386 rc = PTR_ERR(spnego_key);
Steve French0a018942020-07-16 00:34:21 -05001387 if (rc == -ENOKEY)
1388 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001389 spnego_key = NULL;
1390 goto out;
1391 }
1392
1393 msg = spnego_key->payload.data[0];
1394 /*
1395 * check version field to make sure that cifs.upcall is
1396 * sending us a response in an expected form
1397 */
1398 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001399 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1400 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001401 rc = -EKEYREJECTED;
1402 goto out_put_spnego_key;
1403 }
1404
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001405 /* keep session key if binding */
1406 if (!ses->binding) {
1407 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1408 GFP_KERNEL);
1409 if (!ses->auth_key.response) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001410 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001411 msg->sesskey_len);
1412 rc = -ENOMEM;
1413 goto out_put_spnego_key;
1414 }
1415 ses->auth_key.len = msg->sesskey_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001416 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001417
1418 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1419 sess_data->iov[1].iov_len = msg->secblob_len;
1420
1421 rc = SMB2_sess_sendreceive(sess_data);
1422 if (rc)
1423 goto out_put_spnego_key;
1424
1425 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001426 /* keep session id and flags if binding */
1427 if (!ses->binding) {
1428 ses->Suid = rsp->sync_hdr.SessionId;
1429 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1430 }
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001431
1432 rc = SMB2_sess_establish_session(sess_data);
1433out_put_spnego_key:
1434 key_invalidate(spnego_key);
1435 key_put(spnego_key);
1436out:
1437 sess_data->result = rc;
1438 sess_data->func = NULL;
1439 SMB2_sess_free_buffer(sess_data);
1440}
1441#else
1442static void
1443SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1444{
1445 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1446 sess_data->result = -EOPNOTSUPP;
1447 sess_data->func = NULL;
1448}
1449#endif
1450
Sachin Prabhu166cea42016-10-07 19:11:22 +01001451static void
1452SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1453
1454static void
1455SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1456{
1457 int rc;
1458 struct cifs_ses *ses = sess_data->ses;
1459 struct smb2_sess_setup_rsp *rsp = NULL;
1460 char *ntlmssp_blob = NULL;
1461 bool use_spnego = false; /* else use raw ntlmssp */
1462 u16 blob_length = 0;
1463
1464 /*
1465 * If memory allocation is successful, caller of this function
1466 * frees it.
1467 */
1468 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1469 if (!ses->ntlmssp) {
1470 rc = -ENOMEM;
1471 goto out_err;
1472 }
1473 ses->ntlmssp->sesskey_per_smbsess = true;
1474
1475 rc = SMB2_sess_alloc_buffer(sess_data);
1476 if (rc)
1477 goto out_err;
1478
1479 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1480 GFP_KERNEL);
1481 if (ntlmssp_blob == NULL) {
1482 rc = -ENOMEM;
1483 goto out;
1484 }
1485
1486 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1487 if (use_spnego) {
1488 /* BB eventually need to add this */
1489 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1490 rc = -EOPNOTSUPP;
1491 goto out;
1492 } else {
1493 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1494 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1495 }
1496 sess_data->iov[1].iov_base = ntlmssp_blob;
1497 sess_data->iov[1].iov_len = blob_length;
1498
1499 rc = SMB2_sess_sendreceive(sess_data);
1500 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1501
1502 /* If true, rc here is expected and not an error */
1503 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001504 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001505 rc = 0;
1506
1507 if (rc)
1508 goto out;
1509
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10001510 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
Sachin Prabhu166cea42016-10-07 19:11:22 +01001511 le16_to_cpu(rsp->SecurityBufferOffset)) {
1512 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1513 le16_to_cpu(rsp->SecurityBufferOffset));
1514 rc = -EIO;
1515 goto out;
1516 }
1517 rc = decode_ntlmssp_challenge(rsp->Buffer,
1518 le16_to_cpu(rsp->SecurityBufferLength), ses);
1519 if (rc)
1520 goto out;
1521
1522 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1523
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001524 /* keep existing ses id and flags if binding */
1525 if (!ses->binding) {
1526 ses->Suid = rsp->sync_hdr.SessionId;
1527 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1528 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001529
1530out:
1531 kfree(ntlmssp_blob);
1532 SMB2_sess_free_buffer(sess_data);
1533 if (!rc) {
1534 sess_data->result = 0;
1535 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1536 return;
1537 }
1538out_err:
1539 kfree(ses->ntlmssp);
1540 ses->ntlmssp = NULL;
1541 sess_data->result = rc;
1542 sess_data->func = NULL;
1543}
1544
1545static void
1546SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1547{
1548 int rc;
1549 struct cifs_ses *ses = sess_data->ses;
1550 struct smb2_sess_setup_req *req;
1551 struct smb2_sess_setup_rsp *rsp = NULL;
1552 unsigned char *ntlmssp_blob = NULL;
1553 bool use_spnego = false; /* else use raw ntlmssp */
1554 u16 blob_length = 0;
1555
1556 rc = SMB2_sess_alloc_buffer(sess_data);
1557 if (rc)
1558 goto out;
1559
1560 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001561 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001562
1563 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1564 sess_data->nls_cp);
1565 if (rc) {
1566 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1567 goto out;
1568 }
1569
1570 if (use_spnego) {
1571 /* BB eventually need to add this */
1572 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1573 rc = -EOPNOTSUPP;
1574 goto out;
1575 }
1576 sess_data->iov[1].iov_base = ntlmssp_blob;
1577 sess_data->iov[1].iov_len = blob_length;
1578
1579 rc = SMB2_sess_sendreceive(sess_data);
1580 if (rc)
1581 goto out;
1582
1583 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1584
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001585 /* keep existing ses id and flags if binding */
1586 if (!ses->binding) {
1587 ses->Suid = rsp->sync_hdr.SessionId;
1588 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1589 }
Sachin Prabhu166cea42016-10-07 19:11:22 +01001590
1591 rc = SMB2_sess_establish_session(sess_data);
Ronnie Sahlbergf560cda2020-04-12 16:09:26 +10001592#ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1593 if (ses->server->dialect < SMB30_PROT_ID) {
1594 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1595 /*
1596 * The session id is opaque in terms of endianness, so we can't
1597 * print it as a long long. we dump it as we got it on the wire
1598 */
1599 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1600 &ses->Suid);
1601 cifs_dbg(VFS, "Session Key %*ph\n",
1602 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1603 cifs_dbg(VFS, "Signing Key %*ph\n",
1604 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1605 }
1606#endif
Sachin Prabhu166cea42016-10-07 19:11:22 +01001607out:
1608 kfree(ntlmssp_blob);
1609 SMB2_sess_free_buffer(sess_data);
1610 kfree(ses->ntlmssp);
1611 ses->ntlmssp = NULL;
1612 sess_data->result = rc;
1613 sess_data->func = NULL;
1614}
1615
1616static int
1617SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1618{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301619 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001620
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001621 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301622 cifs_dbg(FYI, "sess setup type %d\n", type);
1623 if (type == Unspecified) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001624 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301625 return -EINVAL;
1626 }
1627
1628 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001629 case Kerberos:
1630 sess_data->func = SMB2_auth_kerberos;
1631 break;
1632 case RawNTLMSSP:
1633 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1634 break;
1635 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301636 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001637 return -EOPNOTSUPP;
1638 }
1639
1640 return 0;
1641}
1642
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001643int
1644SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1645 const struct nls_table *nls_cp)
1646{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001647 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001648 struct TCP_Server_Info *server = cifs_ses_server(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001649 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001650
Joe Perchesf96637b2013-05-04 22:12:25 -05001651 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001652
Jeff Layton3534b852013-05-24 07:41:01 -04001653 if (!server) {
1654 WARN(1, "%s: server is NULL!\n", __func__);
1655 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001656 }
1657
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001658 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1659 if (!sess_data)
1660 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001661
1662 rc = SMB2_select_sec(ses, sess_data);
1663 if (rc)
1664 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001665 sess_data->xid = xid;
1666 sess_data->ses = ses;
1667 sess_data->buf0_type = CIFS_NO_BUFFER;
1668 sess_data->nls_cp = (struct nls_table *) nls_cp;
Steve Frenchb2adf22f2018-05-31 15:19:25 -05001669 sess_data->previous_session = ses->Suid;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001670
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001671 /*
1672 * Initialize the session hash with the server one.
1673 */
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02001674 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001675 SMB2_PREAUTH_HASH_SIZE);
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001676
Sachin Prabhu166cea42016-10-07 19:11:22 +01001677 while (sess_data->func)
1678 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001679
Steve Frenchc721c382017-09-19 18:40:03 -05001680 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001681 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001682 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001683out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001684 kfree(sess_data);
1685 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001686}
1687
1688int
1689SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1690{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001691 struct smb_rqst rqst;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001692 struct smb2_logoff_req *req; /* response is also trivial struct */
1693 int rc = 0;
1694 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001695 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001696 unsigned int total_len;
1697 struct kvec iov[1];
1698 struct kvec rsp_iov;
1699 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001700
Joe Perchesf96637b2013-05-04 22:12:25 -05001701 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001702
1703 if (ses && (ses->server))
1704 server = ses->server;
1705 else
1706 return -EIO;
1707
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001708 /* no need to send SMB logoff if uid already closed due to reconnect */
1709 if (ses->need_reconnect)
1710 goto smb2_session_already_dead;
1711
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001712 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1713 (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001714 if (rc)
1715 return rc;
1716
1717 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001718 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001719
1720 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1721 flags |= CIFS_TRANSFORM_REQ;
1722 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001723 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001724
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001725 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001726
1727 iov[0].iov_base = (char *)req;
1728 iov[0].iov_len = total_len;
1729
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001730 memset(&rqst, 0, sizeof(struct smb_rqst));
1731 rqst.rq_iov = iov;
1732 rqst.rq_nvec = 1;
1733
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001734 rc = cifs_send_recv(xid, ses, ses->server,
1735 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001736 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001737 /*
1738 * No tcon so can't do
1739 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1740 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001741
1742smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001743 return rc;
1744}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001745
1746static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1747{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001748 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001749}
1750
1751#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1752
Steve Frenchde9f68df2013-11-15 11:26:24 -06001753/* These are similar values to what Windows uses */
1754static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1755{
1756 tcon->max_chunks = 256;
1757 tcon->max_bytes_chunk = 1048576;
1758 tcon->max_bytes_copy = 16777216;
1759}
1760
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001761int
1762SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1763 struct cifs_tcon *tcon, const struct nls_table *cp)
1764{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001765 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001766 struct smb2_tree_connect_req *req;
1767 struct smb2_tree_connect_rsp *rsp = NULL;
1768 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001769 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001770 int rc = 0;
1771 int resp_buftype;
1772 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001773 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001774 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001775 unsigned int total_len;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001776 struct TCP_Server_Info *server;
1777
1778 /* always use master channel */
1779 server = ses->server;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001780
Joe Perchesf96637b2013-05-04 22:12:25 -05001781 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001782
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001783 if (!server || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001784 return -EIO;
1785
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001786 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1787 if (unc_path == NULL)
1788 return -ENOMEM;
1789
1790 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1791 unc_path_len *= 2;
1792 if (unc_path_len < 2) {
1793 kfree(unc_path);
1794 return -EINVAL;
1795 }
1796
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001797 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001798 tcon->tid = 0;
Steve Frenchfae80442018-10-19 17:14:32 -05001799 atomic_set(&tcon->num_remote_opens, 0);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001800 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1801 (void **) &req, &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001802 if (rc) {
1803 kfree(unc_path);
1804 return rc;
1805 }
1806
Steve French5a77e752018-05-09 17:43:08 -05001807 if (smb3_encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001808 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001809
1810 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001811 /* 1 for pad */
1812 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001813
1814 /* Testing shows that buffer offset must be at location of Buffer[0] */
1815 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001816 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001817 req->PathLength = cpu_to_le16(unc_path_len - 2);
1818 iov[1].iov_base = unc_path;
1819 iov[1].iov_len = unc_path_len;
1820
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001821 /*
1822 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1823 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
Steve French8c11a602019-03-22 22:31:17 -05001824 * (Samba servers don't always set the flag so also check if null user)
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001825 */
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001826 if ((server->dialect == SMB311_PROT_ID) &&
Ronnie Sahlberge71ab2a2019-03-21 14:59:02 +10001827 !smb3_encryption_required(tcon) &&
Steve French8c11a602019-03-22 22:31:17 -05001828 !(ses->session_flags &
1829 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1830 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
Steve French6188f282018-03-13 02:29:36 -05001831 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1832
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001833 memset(&rqst, 0, sizeof(struct smb_rqst));
1834 rqst.rq_iov = iov;
1835 rqst.rq_nvec = 2;
1836
Steve French4fe75c42019-02-14 01:19:02 -06001837 /* Need 64 for max size write so ask for more in case not there yet */
1838 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1839
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001840 rc = cifs_send_recv(xid, ses, server,
1841 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001842 cifs_small_buf_release(req);
1843 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Steve Frenchf8af49d2018-10-28 00:47:11 -05001844 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001845 if (rc != 0) {
Steve French35591342021-06-19 12:01:37 -05001846 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1847 tcon->need_reconnect = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001848 goto tcon_error_exit;
1849 }
1850
Christophe JAILLETcd123002017-05-12 17:59:32 +02001851 switch (rsp->ShareType) {
1852 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001853 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001854 break;
1855 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001856 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001857 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001858 break;
1859 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001860 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001861 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001862 break;
1863 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001864 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001865 rc = -EOPNOTSUPP;
1866 goto tcon_error_exit;
1867 }
1868
1869 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001870 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001871 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1872 tcon->tidStatus = CifsGood;
1873 tcon->need_reconnect = false;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001874 tcon->tid = rsp->sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001875 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001876
1877 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1878 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001879 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001880
1881 if (tcon->seal &&
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001882 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001883 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001884
Steve Frenchde9f68df2013-11-15 11:26:24 -06001885 init_copy_chunk_defaults(tcon);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001886 if (server->ops->validate_negotiate)
1887 rc = server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001888tcon_exit:
Steve Frenchf8af49d2018-10-28 00:47:11 -05001889
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001890 free_rsp_buf(resp_buftype, rsp);
1891 kfree(unc_path);
1892 return rc;
1893
1894tcon_error_exit:
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001895 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001896 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001897 }
1898 goto tcon_exit;
1899}
1900
1901int
1902SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1903{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001904 struct smb_rqst rqst;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001905 struct smb2_tree_disconnect_req *req; /* response is trivial */
1906 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001907 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001908 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001909 unsigned int total_len;
1910 struct kvec iov[1];
1911 struct kvec rsp_iov;
1912 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001913
Joe Perchesf96637b2013-05-04 22:12:25 -05001914 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001915
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001916 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001917 return -EIO;
1918
1919 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1920 return 0;
1921
Ronnie Sahlberg45c0f1a2021-03-09 09:07:29 +10001922 close_cached_dir_lease(&tcon->crfid);
Ronnie Sahlberg72e73c72019-11-07 17:00:38 +10001923
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001924 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
1925 (void **) &req,
1926 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001927 if (rc)
1928 return rc;
1929
Steve French5a77e752018-05-09 17:43:08 -05001930 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001931 flags |= CIFS_TRANSFORM_REQ;
1932
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10001933 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001934
1935 iov[0].iov_base = (char *)req;
1936 iov[0].iov_len = total_len;
1937
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10001938 memset(&rqst, 0, sizeof(struct smb_rqst));
1939 rqst.rq_iov = iov;
1940 rqst.rq_nvec = 1;
1941
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001942 rc = cifs_send_recv(xid, ses, ses->server,
1943 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001944 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001945 if (rc)
1946 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1947
1948 return rc;
1949}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001950
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001951
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001952static struct create_durable *
1953create_durable_buf(void)
1954{
1955 struct create_durable *buf;
1956
1957 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1958 if (!buf)
1959 return NULL;
1960
1961 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001962 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001963 buf->ccontext.DataLength = cpu_to_le32(16);
1964 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1965 (struct create_durable, Name));
1966 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001967 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001968 buf->Name[0] = 'D';
1969 buf->Name[1] = 'H';
1970 buf->Name[2] = 'n';
1971 buf->Name[3] = 'Q';
1972 return buf;
1973}
1974
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001975static struct create_durable *
1976create_reconnect_durable_buf(struct cifs_fid *fid)
1977{
1978 struct create_durable *buf;
1979
1980 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1981 if (!buf)
1982 return NULL;
1983
1984 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1985 (struct create_durable, Data));
1986 buf->ccontext.DataLength = cpu_to_le32(16);
1987 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1988 (struct create_durable, Name));
1989 buf->ccontext.NameLength = cpu_to_le16(4);
1990 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1991 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001992 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001993 buf->Name[0] = 'D';
1994 buf->Name[1] = 'H';
1995 buf->Name[2] = 'n';
1996 buf->Name[3] = 'C';
1997 return buf;
1998}
1999
Steve French89a5bfa2019-07-18 17:22:18 -05002000static void
2001parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2002{
2003 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
2004
2005 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2006 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2007 buf->IndexNumber = pdisk_id->DiskFileId;
2008}
2009
Steve Frenchab3459d2020-02-06 17:31:56 -06002010static void
Aurelien Aptel69dda302020-03-02 17:53:22 +01002011parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2012 struct create_posix_rsp *posix)
Steve Frenchab3459d2020-02-06 17:31:56 -06002013{
Aurelien Aptel69dda302020-03-02 17:53:22 +01002014 int sid_len;
2015 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2016 u8 *end = beg + le32_to_cpu(cc->DataLength);
2017 u8 *sid;
Steve Frenchab3459d2020-02-06 17:31:56 -06002018
Aurelien Aptel69dda302020-03-02 17:53:22 +01002019 memset(posix, 0, sizeof(*posix));
Aurelien Aptel2e8af972020-02-08 15:50:56 +01002020
Aurelien Aptel69dda302020-03-02 17:53:22 +01002021 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2022 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2023 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2024
2025 sid = beg + 12;
2026 sid_len = posix_info_sid_size(sid, end);
2027 if (sid_len < 0) {
2028 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2029 return;
2030 }
2031 memcpy(&posix->owner, sid, sid_len);
2032
2033 sid = sid + sid_len;
2034 sid_len = posix_info_sid_size(sid, end);
2035 if (sid_len < 0) {
2036 cifs_dbg(VFS, "bad group sid in posix create response\n");
2037 return;
2038 }
2039 memcpy(&posix->group, sid, sid_len);
2040
2041 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2042 posix->nlink, posix->mode, posix->reparse_tag);
Steve Frenchab3459d2020-02-06 17:31:56 -06002043}
2044
Steve French89a5bfa2019-07-18 17:22:18 -05002045void
2046smb2_parse_contexts(struct TCP_Server_Info *server,
Aurelien Aptel69dda302020-03-02 17:53:22 +01002047 struct smb2_create_rsp *rsp,
2048 unsigned int *epoch, char *lease_key, __u8 *oplock,
2049 struct smb2_file_all_info *buf,
2050 struct create_posix_rsp *posix)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002051{
2052 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002053 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08002054 unsigned int next;
2055 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04002056 char *name;
Colin Ian King3ece60e2020-10-20 15:19:36 +01002057 static const char smb3_create_tag_posix[] = {
2058 0x93, 0xAD, 0x25, 0x50, 0x9C,
2059 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2060 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2061 };
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002062
Steve French89a5bfa2019-07-18 17:22:18 -05002063 *oplock = 0;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002064 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08002065 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002066 cc = (struct create_context *)data_offset;
Steve French89a5bfa2019-07-18 17:22:18 -05002067
2068 /* Initialize inode number to 0 in case no valid data in qfid context */
2069 if (buf)
2070 buf->IndexNumber = 0;
2071
Justin Maggarddeb7def2016-02-09 15:52:08 -08002072 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002073 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08002074 if (le16_to_cpu(cc->NameLength) == 4 &&
Steve French89a5bfa2019-07-18 17:22:18 -05002075 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2076 *oplock = server->ops->parse_lease_buf(cc, epoch,
2077 lease_key);
2078 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2079 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2080 parse_query_id_ctxt(cc, buf);
Steve Frenchab3459d2020-02-06 17:31:56 -06002081 else if ((le16_to_cpu(cc->NameLength) == 16)) {
Aurelien Aptel69dda302020-03-02 17:53:22 +01002082 if (posix &&
2083 memcmp(name, smb3_create_tag_posix, 16) == 0)
2084 parse_posix_ctxt(cc, buf, posix);
Steve Frenchab3459d2020-02-06 17:31:56 -06002085 }
2086 /* else {
2087 cifs_dbg(FYI, "Context not matched with len %d\n",
2088 le16_to_cpu(cc->NameLength));
2089 cifs_dump_mem("Cctxt name: ", name, 4);
2090 } */
Justin Maggarddeb7def2016-02-09 15:52:08 -08002091
2092 next = le32_to_cpu(cc->Next);
2093 if (!next)
2094 break;
2095 remaining -= next;
2096 cc = (struct create_context *)((char *)cc + next);
2097 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002098
Steve French89a5bfa2019-07-18 17:22:18 -05002099 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2100 *oplock = rsp->OplockLevel;
2101
2102 return;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002103}
2104
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002105static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002106add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
Stefano Brivio729c0c92018-07-05 15:10:02 +02002107 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002108{
2109 struct smb2_create_req *req = iov[0].iov_base;
2110 unsigned int num = *num_iovec;
2111
Stefano Brivio729c0c92018-07-05 15:10:02 +02002112 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002113 if (iov[num].iov_base == NULL)
2114 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002115 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002116 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2117 if (!req->CreateContextsOffset)
2118 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002119 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002120 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002121 le32_add_cpu(&req->CreateContextsLength,
2122 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002123 *num_iovec = num + 1;
2124 return 0;
2125}
2126
Steve Frenchb56eae42015-11-03 09:26:27 -06002127static struct create_durable_v2 *
Steve Frenchca567eb2019-03-29 16:31:07 -05002128create_durable_v2_buf(struct cifs_open_parms *oparms)
Steve Frenchb56eae42015-11-03 09:26:27 -06002129{
Steve Frenchca567eb2019-03-29 16:31:07 -05002130 struct cifs_fid *pfid = oparms->fid;
Steve Frenchb56eae42015-11-03 09:26:27 -06002131 struct create_durable_v2 *buf;
2132
2133 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2134 if (!buf)
2135 return NULL;
2136
2137 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2138 (struct create_durable_v2, dcontext));
2139 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2140 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2141 (struct create_durable_v2, Name));
2142 buf->ccontext.NameLength = cpu_to_le16(4);
2143
Steve Frenchca567eb2019-03-29 16:31:07 -05002144 /*
2145 * NB: Handle timeout defaults to 0, which allows server to choose
2146 * (most servers default to 120 seconds) and most clients default to 0.
2147 * This can be overridden at mount ("handletimeout=") if the user wants
2148 * a different persistent (or resilient) handle timeout for all opens
2149 * opens on a particular SMB3 mount.
2150 */
2151 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
Steve Frenchb56eae42015-11-03 09:26:27 -06002152 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05002153 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06002154 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2155
2156 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2157 buf->Name[0] = 'D';
2158 buf->Name[1] = 'H';
2159 buf->Name[2] = '2';
2160 buf->Name[3] = 'Q';
2161 return buf;
2162}
2163
2164static struct create_durable_handle_reconnect_v2 *
2165create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2166{
2167 struct create_durable_handle_reconnect_v2 *buf;
2168
2169 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2170 GFP_KERNEL);
2171 if (!buf)
2172 return NULL;
2173
2174 buf->ccontext.DataOffset =
2175 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2176 dcontext));
2177 buf->ccontext.DataLength =
2178 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2179 buf->ccontext.NameOffset =
2180 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2181 Name));
2182 buf->ccontext.NameLength = cpu_to_le16(4);
2183
2184 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2185 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2186 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2187 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2188
2189 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2190 buf->Name[0] = 'D';
2191 buf->Name[1] = 'H';
2192 buf->Name[2] = '2';
2193 buf->Name[3] = 'C';
2194 return buf;
2195}
2196
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002197static int
Steve Frenchb56eae42015-11-03 09:26:27 -06002198add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002199 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002200{
2201 struct smb2_create_req *req = iov[0].iov_base;
2202 unsigned int num = *num_iovec;
2203
Steve Frenchca567eb2019-03-29 16:31:07 -05002204 iov[num].iov_base = create_durable_v2_buf(oparms);
Steve Frenchb56eae42015-11-03 09:26:27 -06002205 if (iov[num].iov_base == NULL)
2206 return -ENOMEM;
2207 iov[num].iov_len = sizeof(struct create_durable_v2);
2208 if (!req->CreateContextsOffset)
2209 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002210 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002211 iov[1].iov_len);
2212 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002213 *num_iovec = num + 1;
2214 return 0;
2215}
2216
2217static int
2218add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2219 struct cifs_open_parms *oparms)
2220{
2221 struct smb2_create_req *req = iov[0].iov_base;
2222 unsigned int num = *num_iovec;
2223
2224 /* indicate that we don't need to relock the file */
2225 oparms->reconnect = false;
2226
2227 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2228 if (iov[num].iov_base == NULL)
2229 return -ENOMEM;
2230 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2231 if (!req->CreateContextsOffset)
2232 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002233 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06002234 iov[1].iov_len);
2235 le32_add_cpu(&req->CreateContextsLength,
2236 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06002237 *num_iovec = num + 1;
2238 return 0;
2239}
2240
2241static int
2242add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2243 struct cifs_open_parms *oparms, bool use_persistent)
2244{
2245 struct smb2_create_req *req = iov[0].iov_base;
2246 unsigned int num = *num_iovec;
2247
2248 if (use_persistent) {
2249 if (oparms->reconnect)
2250 return add_durable_reconnect_v2_context(iov, num_iovec,
2251 oparms);
2252 else
2253 return add_durable_v2_context(iov, num_iovec, oparms);
2254 }
2255
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002256 if (oparms->reconnect) {
2257 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2258 /* indicate that we don't need to relock the file */
2259 oparms->reconnect = false;
2260 } else
2261 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002262 if (iov[num].iov_base == NULL)
2263 return -ENOMEM;
2264 iov[num].iov_len = sizeof(struct create_durable);
2265 if (!req->CreateContextsOffset)
2266 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002267 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002268 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08002269 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002270 *num_iovec = num + 1;
2271 return 0;
2272}
2273
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002274/* See MS-SMB2 2.2.13.2.7 */
2275static struct crt_twarp_ctxt *
2276create_twarp_buf(__u64 timewarp)
2277{
2278 struct crt_twarp_ctxt *buf;
2279
2280 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2281 if (!buf)
2282 return NULL;
2283
2284 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2285 (struct crt_twarp_ctxt, Timestamp));
2286 buf->ccontext.DataLength = cpu_to_le32(8);
2287 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2288 (struct crt_twarp_ctxt, Name));
2289 buf->ccontext.NameLength = cpu_to_le16(4);
2290 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2291 buf->Name[0] = 'T';
2292 buf->Name[1] = 'W';
2293 buf->Name[2] = 'r';
2294 buf->Name[3] = 'p';
2295 buf->Timestamp = cpu_to_le64(timewarp);
2296 return buf;
2297}
2298
2299/* See MS-SMB2 2.2.13.2.7 */
2300static int
2301add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2302{
2303 struct smb2_create_req *req = iov[0].iov_base;
2304 unsigned int num = *num_iovec;
2305
2306 iov[num].iov_base = create_twarp_buf(timewarp);
2307 if (iov[num].iov_base == NULL)
2308 return -ENOMEM;
2309 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2310 if (!req->CreateContextsOffset)
2311 req->CreateContextsOffset = cpu_to_le32(
2312 sizeof(struct smb2_create_req) +
2313 iov[num - 1].iov_len);
2314 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2315 *num_iovec = num + 1;
2316 return 0;
2317}
2318
Steve French975221e2020-06-12 09:25:21 -05002319/* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2320static void setup_owner_group_sids(char *buf)
2321{
2322 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2323
2324 /* Populate the user ownership fields S-1-5-88-1 */
2325 sids->owner.Revision = 1;
2326 sids->owner.NumAuth = 3;
2327 sids->owner.Authority[5] = 5;
2328 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2329 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2330 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2331
2332 /* Populate the group ownership fields S-1-5-88-2 */
2333 sids->group.Revision = 1;
2334 sids->group.NumAuth = 3;
2335 sids->group.Authority[5] = 5;
2336 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2337 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2338 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
Steve Frencha7a519a2020-06-12 14:49:47 -05002339
2340 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 -05002341}
2342
Steve Frenchfdef6652019-12-06 02:02:38 -06002343/* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2344static struct crt_sd_ctxt *
Steve French975221e2020-06-12 09:25:21 -05002345create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
Steve Frenchfdef6652019-12-06 02:02:38 -06002346{
2347 struct crt_sd_ctxt *buf;
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002348 __u8 *ptr, *aclptr;
2349 unsigned int acelen, acl_size, ace_count;
Steve French975221e2020-06-12 09:25:21 -05002350 unsigned int owner_offset = 0;
2351 unsigned int group_offset = 0;
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002352 struct smb3_acl acl;
Steve Frenchfdef6652019-12-06 02:02:38 -06002353
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002354 *len = roundup(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
Steve French975221e2020-06-12 09:25:21 -05002355
2356 if (set_owner) {
Steve French975221e2020-06-12 09:25:21 -05002357 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2358 *len += sizeof(struct owner_group_sids);
2359 }
2360
Steve Frenchfdef6652019-12-06 02:02:38 -06002361 buf = kzalloc(*len, GFP_KERNEL);
2362 if (buf == NULL)
2363 return buf;
2364
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002365 ptr = (__u8 *)&buf[1];
Steve French975221e2020-06-12 09:25:21 -05002366 if (set_owner) {
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002367 /* offset fields are from beginning of security descriptor not of create context */
2368 owner_offset = ptr - (__u8 *)&buf->sd;
Steve French975221e2020-06-12 09:25:21 -05002369 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002370 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
Steve French975221e2020-06-12 09:25:21 -05002371 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002372
2373 setup_owner_group_sids(ptr);
2374 ptr += sizeof(struct owner_group_sids);
Steve French975221e2020-06-12 09:25:21 -05002375 } else {
2376 buf->sd.OffsetOwner = 0;
2377 buf->sd.OffsetGroup = 0;
2378 }
2379
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002380 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
Steve French975221e2020-06-12 09:25:21 -05002381 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
Steve Frenchfdef6652019-12-06 02:02:38 -06002382 buf->ccontext.NameLength = cpu_to_le16(4);
2383 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2384 buf->Name[0] = 'S';
2385 buf->Name[1] = 'e';
2386 buf->Name[2] = 'c';
2387 buf->Name[3] = 'D';
2388 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002389
Steve Frenchfdef6652019-12-06 02:02:38 -06002390 /*
2391 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2392 * and "DP" ie the DACL is present
2393 */
2394 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2395
2396 /* offset owner, group and Sbz1 and SACL are all zero */
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002397 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2398 /* Ship the ACL for now. we will copy it into buf later. */
2399 aclptr = ptr;
Steve Frenchb06d8932021-09-23 16:00:31 -05002400 ptr += sizeof(struct smb3_acl);
Steve Frenchfdef6652019-12-06 02:02:38 -06002401
2402 /* create one ACE to hold the mode embedded in reserved special SID */
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002403 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2404 ptr += acelen;
2405 acl_size = acelen + sizeof(struct smb3_acl);
2406 ace_count = 1;
Steve French975221e2020-06-12 09:25:21 -05002407
2408 if (set_owner) {
2409 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002410 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2411 ptr += acelen;
2412 acl_size += acelen;
2413 ace_count += 1;
2414 }
Steve French975221e2020-06-12 09:25:21 -05002415
Steve French643fbce2020-01-16 19:55:33 -06002416 /* and one more ACE to allow access for authenticated users */
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002417 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2418 ptr += acelen;
2419 acl_size += acelen;
2420 ace_count += 1;
Steve French975221e2020-06-12 09:25:21 -05002421
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002422 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2423 acl.AclSize = cpu_to_le16(acl_size);
2424 acl.AceCount = cpu_to_le16(ace_count);
Steve Frenchb06d8932021-09-23 16:00:31 -05002425 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
Ronnie Sahlbergea643702020-11-30 11:29:20 +10002426
2427 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
Shyam Prasad N7d3fc012021-08-04 18:37:22 +00002428 *len = roundup(ptr - (__u8 *)buf, 8);
Steve French975221e2020-06-12 09:25:21 -05002429
Steve Frenchfdef6652019-12-06 02:02:38 -06002430 return buf;
2431}
2432
2433static int
Steve French975221e2020-06-12 09:25:21 -05002434add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
Steve Frenchfdef6652019-12-06 02:02:38 -06002435{
2436 struct smb2_create_req *req = iov[0].iov_base;
2437 unsigned int num = *num_iovec;
2438 unsigned int len = 0;
2439
Steve French975221e2020-06-12 09:25:21 -05002440 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
Steve Frenchfdef6652019-12-06 02:02:38 -06002441 if (iov[num].iov_base == NULL)
2442 return -ENOMEM;
2443 iov[num].iov_len = len;
2444 if (!req->CreateContextsOffset)
2445 req->CreateContextsOffset = cpu_to_le32(
2446 sizeof(struct smb2_create_req) +
2447 iov[num - 1].iov_len);
2448 le32_add_cpu(&req->CreateContextsLength, len);
2449 *num_iovec = num + 1;
2450 return 0;
2451}
2452
Steve Frenchff2a09e2019-07-06 14:41:38 -05002453static struct crt_query_id_ctxt *
2454create_query_id_buf(void)
2455{
2456 struct crt_query_id_ctxt *buf;
2457
2458 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2459 if (!buf)
2460 return NULL;
2461
2462 buf->ccontext.DataOffset = cpu_to_le16(0);
2463 buf->ccontext.DataLength = cpu_to_le32(0);
2464 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2465 (struct crt_query_id_ctxt, Name));
2466 buf->ccontext.NameLength = cpu_to_le16(4);
2467 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2468 buf->Name[0] = 'Q';
2469 buf->Name[1] = 'F';
2470 buf->Name[2] = 'i';
2471 buf->Name[3] = 'd';
2472 return buf;
2473}
2474
2475/* See MS-SMB2 2.2.13.2.9 */
2476static int
2477add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2478{
2479 struct smb2_create_req *req = iov[0].iov_base;
2480 unsigned int num = *num_iovec;
2481
2482 iov[num].iov_base = create_query_id_buf();
2483 if (iov[num].iov_base == NULL)
2484 return -ENOMEM;
2485 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2486 if (!req->CreateContextsOffset)
2487 req->CreateContextsOffset = cpu_to_le32(
2488 sizeof(struct smb2_create_req) +
2489 iov[num - 1].iov_len);
2490 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2491 *num_iovec = num + 1;
2492 return 0;
2493}
2494
Aurelien Aptelf0712922017-02-22 14:47:17 +01002495static int
2496alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2497 const char *treename, const __le16 *path)
2498{
2499 int treename_len, path_len;
2500 struct nls_table *cp;
2501 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2502
2503 /*
2504 * skip leading "\\"
2505 */
2506 treename_len = strlen(treename);
2507 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2508 return -EINVAL;
2509
2510 treename += 2;
2511 treename_len -= 2;
2512
2513 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2514
2515 /*
2516 * make room for one path separator between the treename and
2517 * path
2518 */
2519 *out_len = treename_len + 1 + path_len;
2520
2521 /*
2522 * final path needs to be null-terminated UTF16 with a
2523 * size aligned to 8
2524 */
2525
2526 *out_size = roundup((*out_len+1)*2, 8);
2527 *out_path = kzalloc(*out_size, GFP_KERNEL);
2528 if (!*out_path)
2529 return -ENOMEM;
2530
2531 cp = load_nls_default();
2532 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2533 UniStrcat(*out_path, sep);
2534 UniStrcat(*out_path, path);
2535 unload_nls(cp);
2536
2537 return 0;
2538}
2539
Steve Frenchbea851b2018-06-14 21:56:32 -05002540int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2541 umode_t mode, struct cifs_tcon *tcon,
2542 const char *full_path,
2543 struct cifs_sb_info *cifs_sb)
2544{
2545 struct smb_rqst rqst;
2546 struct smb2_create_req *req;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002547 struct smb2_create_rsp *rsp = NULL;
Steve Frenchbea851b2018-06-14 21:56:32 -05002548 struct cifs_ses *ses = tcon->ses;
2549 struct kvec iov[3]; /* make sure at least one for each open context */
2550 struct kvec rsp_iov = {NULL, 0};
2551 int resp_buftype;
2552 int uni_path_len;
2553 __le16 *copy_path = NULL;
2554 int copy_size;
2555 int rc = 0;
2556 unsigned int n_iov = 2;
2557 __u32 file_attributes = 0;
2558 char *pc_buf = NULL;
2559 int flags = 0;
2560 unsigned int total_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002561 __le16 *utf16_path = NULL;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002562 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve Frenchbea851b2018-06-14 21:56:32 -05002563
2564 cifs_dbg(FYI, "mkdir\n");
2565
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002566 /* resource #1: path allocation */
2567 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2568 if (!utf16_path)
2569 return -ENOMEM;
2570
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002571 if (!ses || !server) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002572 rc = -EIO;
2573 goto err_free_path;
2574 }
Steve Frenchbea851b2018-06-14 21:56:32 -05002575
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002576 /* resource #2: request */
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002577 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2578 (void **) &req, &total_len);
Steve Frenchbea851b2018-06-14 21:56:32 -05002579 if (rc)
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002580 goto err_free_path;
2581
Steve Frenchbea851b2018-06-14 21:56:32 -05002582
2583 if (smb3_encryption_required(tcon))
2584 flags |= CIFS_TRANSFORM_REQ;
2585
Steve Frenchbea851b2018-06-14 21:56:32 -05002586 req->ImpersonationLevel = IL_IMPERSONATION;
2587 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2588 /* File attributes ignored on open (used in create though) */
2589 req->FileAttributes = cpu_to_le32(file_attributes);
2590 req->ShareAccess = FILE_SHARE_ALL_LE;
2591 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2592 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2593
2594 iov[0].iov_base = (char *)req;
2595 /* -1 since last byte is buf[0] which is sent below (path) */
2596 iov[0].iov_len = total_len - 1;
2597
2598 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2599
2600 /* [MS-SMB2] 2.2.13 NameOffset:
2601 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2602 * the SMB2 header, the file name includes a prefix that will
2603 * be processed during DFS name normalization as specified in
2604 * section 3.3.5.9. Otherwise, the file name is relative to
2605 * the share that is identified by the TreeId in the SMB2
2606 * header.
2607 */
2608 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2609 int name_len;
2610
2611 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2612 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2613 &name_len,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002614 tcon->treeName, utf16_path);
2615 if (rc)
2616 goto err_free_req;
2617
Steve Frenchbea851b2018-06-14 21:56:32 -05002618 req->NameLength = cpu_to_le16(name_len * 2);
2619 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002620 /* free before overwriting resource */
2621 kfree(utf16_path);
2622 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002623 } else {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002624 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
Steve Frenchbea851b2018-06-14 21:56:32 -05002625 /* MUST set path len (NameLength) to 0 opening root of share */
2626 req->NameLength = cpu_to_le16(uni_path_len - 2);
2627 if (uni_path_len % 8 != 0) {
2628 copy_size = roundup(uni_path_len, 8);
2629 copy_path = kzalloc(copy_size, GFP_KERNEL);
2630 if (!copy_path) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002631 rc = -ENOMEM;
2632 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002633 }
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002634 memcpy((char *)copy_path, (const char *)utf16_path,
Steve Frenchbea851b2018-06-14 21:56:32 -05002635 uni_path_len);
2636 uni_path_len = copy_size;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002637 /* free before overwriting resource */
2638 kfree(utf16_path);
2639 utf16_path = copy_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002640 }
2641 }
2642
2643 iov[1].iov_len = uni_path_len;
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002644 iov[1].iov_base = utf16_path;
Steve Frenchbea851b2018-06-14 21:56:32 -05002645 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2646
2647 if (tcon->posix_extensions) {
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002648 /* resource #3: posix buf */
Steve Frenchbea851b2018-06-14 21:56:32 -05002649 rc = add_posix_context(iov, &n_iov, mode);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002650 if (rc)
2651 goto err_free_req;
Steve Frenchbea851b2018-06-14 21:56:32 -05002652 pc_buf = iov[n_iov-1].iov_base;
2653 }
2654
2655
2656 memset(&rqst, 0, sizeof(struct smb_rqst));
2657 rqst.rq_iov = iov;
2658 rqst.rq_nvec = n_iov;
2659
Steve Frenchd2f15422019-09-22 00:55:46 -05002660 /* no need to inc num_remote_opens because we close it just below */
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002661 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2662 FILE_WRITE_ATTRIBUTES);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002663 /* resource #4: response buffer */
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002664 rc = cifs_send_recv(xid, ses, server,
2665 &rqst, &resp_buftype, flags, &rsp_iov);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002666 if (rc) {
Steve Frenchbea851b2018-06-14 21:56:32 -05002667 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2668 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002669 CREATE_NOT_FILE,
2670 FILE_WRITE_ATTRIBUTES, rc);
2671 goto err_free_rsp_buf;
2672 }
2673
2674 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2675 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2676 ses->Suid, CREATE_NOT_FILE,
2677 FILE_WRITE_ATTRIBUTES);
Steve Frenchbea851b2018-06-14 21:56:32 -05002678
2679 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2680
2681 /* Eventually save off posix specific response info and timestaps */
2682
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002683err_free_rsp_buf:
Steve Frenchbea851b2018-06-14 21:56:32 -05002684 free_rsp_buf(resp_buftype, rsp);
Aurelien Aptel256b4c32018-06-19 15:18:48 -07002685 kfree(pc_buf);
2686err_free_req:
2687 cifs_small_buf_release(req);
2688err_free_path:
2689 kfree(utf16_path);
Steve Frenchbea851b2018-06-14 21:56:32 -05002690 return rc;
Steve Frenchbea851b2018-06-14 21:56:32 -05002691}
Steve Frenchbea851b2018-06-14 21:56:32 -05002692
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002693int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002694SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2695 struct smb_rqst *rqst, __u8 *oplock,
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002696 struct cifs_open_parms *oparms, __le16 *path)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002697{
2698 struct smb2_create_req *req;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002699 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002700 __u32 file_attributes = 0;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002701 int copy_size;
2702 int uni_path_len;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002703 unsigned int total_len;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002704 struct kvec *iov = rqst->rq_iov;
2705 __le16 *copy_path;
2706 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002707
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002708 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2709 (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002710 if (rc)
2711 return rc;
2712
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002713 iov[0].iov_base = (char *)req;
2714 /* -1 since last byte is buf[0] which is sent below (path) */
2715 iov[0].iov_len = total_len - 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002716
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002717 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04002718 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05002719 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2720 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04002721
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002722 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002723 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002724 /* File attributes ignored on open (used in create though) */
2725 req->FileAttributes = cpu_to_le32(file_attributes);
2726 req->ShareAccess = FILE_SHARE_ALL_LE;
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002727
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002728 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2729 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002730 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01002731
2732 /* [MS-SMB2] 2.2.13 NameOffset:
2733 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2734 * the SMB2 header, the file name includes a prefix that will
2735 * be processed during DFS name normalization as specified in
2736 * section 3.3.5.9. Otherwise, the file name is relative to
2737 * the share that is identified by the TreeId in the SMB2
2738 * header.
2739 */
2740 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2741 int name_len;
2742
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002743 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002744 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2745 &name_len,
2746 tcon->treeName, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002747 if (rc)
Aurelien Aptelf0712922017-02-22 14:47:17 +01002748 return rc;
2749 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002750 uni_path_len = copy_size;
2751 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01002752 } else {
2753 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2754 /* MUST set path len (NameLength) to 0 opening root of share */
2755 req->NameLength = cpu_to_le16(uni_path_len - 2);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002756 copy_size = uni_path_len;
2757 if (copy_size % 8 != 0)
2758 copy_size = roundup(copy_size, 8);
2759 copy_path = kzalloc(copy_size, GFP_KERNEL);
2760 if (!copy_path)
2761 return -ENOMEM;
2762 memcpy((char *)copy_path, (const char *)path,
2763 uni_path_len);
2764 uni_path_len = copy_size;
2765 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002766 }
2767
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002768 iov[1].iov_len = uni_path_len;
2769 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04002770
Steve French3e7a02d2019-09-11 21:46:20 -05002771 if ((!server->oplocks) || (tcon->no_lease))
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002772 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2773
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002774 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002775 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2776 req->RequestedOplockLevel = *oplock;
Steve Frenchf8015682018-08-31 15:12:10 -05002777 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2778 (oparms->create_options & CREATE_NOT_FILE))
2779 req->RequestedOplockLevel = *oplock; /* no srv lease support */
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002780 else {
Stefano Brivio729c0c92018-07-05 15:10:02 +02002781 rc = add_lease_context(server, iov, &n_iov,
2782 oparms->fid->lease_key, oplock);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002783 if (rc)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04002784 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002785 }
2786
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002787 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2788 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002789 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002790 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002791 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05002792 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002793 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002794 }
Steve Frenchb56eae42015-11-03 09:26:27 -06002795
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002796 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06002797 tcon->use_persistent);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002798 if (rc)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002799 return rc;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04002800 }
2801
Steve Frenchce558b02018-05-31 19:16:54 -05002802 if (tcon->posix_extensions) {
2803 if (n_iov > 2) {
2804 struct create_context *ccontext =
2805 (struct create_context *)iov[n_iov-1].iov_base;
2806 ccontext->Next =
2807 cpu_to_le32(iov[n_iov-1].iov_len);
2808 }
2809
2810 rc = add_posix_context(iov, &n_iov, oparms->mode);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002811 if (rc)
Steve Frenchce558b02018-05-31 19:16:54 -05002812 return rc;
Steve Frenchce558b02018-05-31 19:16:54 -05002813 }
Steve Frenchce558b02018-05-31 19:16:54 -05002814
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002815 if (tcon->snapshot_time) {
2816 cifs_dbg(FYI, "adding snapshot context\n");
2817 if (n_iov > 2) {
2818 struct create_context *ccontext =
2819 (struct create_context *)iov[n_iov-1].iov_base;
2820 ccontext->Next =
2821 cpu_to_le32(iov[n_iov-1].iov_len);
2822 }
2823
2824 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2825 if (rc)
2826 return rc;
2827 }
2828
Steve French975221e2020-06-12 09:25:21 -05002829 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2830 bool set_mode;
2831 bool set_owner;
2832
2833 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2834 (oparms->mode != ACL_NO_MODE))
2835 set_mode = true;
2836 else {
2837 set_mode = false;
2838 oparms->mode = ACL_NO_MODE;
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002839 }
2840
Steve French975221e2020-06-12 09:25:21 -05002841 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2842 set_owner = true;
2843 else
2844 set_owner = false;
2845
2846 if (set_owner | set_mode) {
2847 if (n_iov > 2) {
2848 struct create_context *ccontext =
2849 (struct create_context *)iov[n_iov-1].iov_base;
2850 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2851 }
2852
2853 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2854 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2855 if (rc)
2856 return rc;
2857 }
Steve Frenchc3ca78e2019-09-25 00:32:13 -05002858 }
2859
Steve Frenchff2a09e2019-07-06 14:41:38 -05002860 if (n_iov > 2) {
2861 struct create_context *ccontext =
2862 (struct create_context *)iov[n_iov-1].iov_base;
2863 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2864 }
2865 add_query_id_context(iov, &n_iov);
Steve Frenchcdeaf9d2018-08-10 02:25:06 -05002866
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002867 rqst->rq_nvec = n_iov;
2868 return 0;
2869}
2870
2871/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2872 * All other vectors are freed by kfree().
2873 */
2874void
2875SMB2_open_free(struct smb_rqst *rqst)
2876{
2877 int i;
2878
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10002879 if (rqst && rqst->rq_iov) {
2880 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2881 for (i = 1; i < rqst->rq_nvec; i++)
2882 if (rqst->rq_iov[i].iov_base != smb2_padding)
2883 kfree(rqst->rq_iov[i].iov_base);
2884 }
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002885}
2886
2887int
2888SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2889 __u8 *oplock, struct smb2_file_all_info *buf,
Aurelien Aptel69dda302020-03-02 17:53:22 +01002890 struct create_posix_rsp *posix,
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002891 struct kvec *err_iov, int *buftype)
2892{
2893 struct smb_rqst rqst;
2894 struct smb2_create_rsp *rsp = NULL;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002895 struct cifs_tcon *tcon = oparms->tcon;
2896 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002897 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002898 struct kvec iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002899 struct kvec rsp_iov = {NULL, 0};
Garry McNultyef2298a2018-10-03 20:51:21 +01002900 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002901 int rc = 0;
2902 int flags = 0;
2903
2904 cifs_dbg(FYI, "create/open\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002905 if (!ses || !server)
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002906 return -EIO;
2907
2908 if (smb3_encryption_required(tcon))
2909 flags |= CIFS_TRANSFORM_REQ;
2910
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002911 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002912 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002913 rqst.rq_iov = iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002914 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002915
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002916 rc = SMB2_open_init(tcon, server,
2917 &rqst, oplock, oparms, path);
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002918 if (rc)
2919 goto creat_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10002920
Steve Frenchefe2e9f2019-02-26 19:08:12 -06002921 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2922 oparms->create_options, oparms->desired_access);
2923
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002924 rc = cifs_send_recv(xid, ses, server,
2925 &rqst, &resp_buftype, flags,
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11002926 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002927 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002928
2929 if (rc != 0) {
2930 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002931 if (err_iov && rsp) {
2932 *err_iov = rsp_iov;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002933 *buftype = resp_buftype;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002934 resp_buftype = CIFS_NO_BUFFER;
2935 rsp = NULL;
2936 }
Steve French28d59362018-05-30 21:42:34 -05002937 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2938 oparms->create_options, oparms->desired_access, rc);
Steve French7dcc82c2019-09-11 00:07:36 -05002939 if (rc == -EREMCHG) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002940 pr_warn_once("server share %s deleted\n",
2941 tcon->treeName);
Steve French7dcc82c2019-09-11 00:07:36 -05002942 tcon->need_reconnect = true;
2943 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002944 goto creat_exit;
Steve French28d59362018-05-30 21:42:34 -05002945 } else
2946 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2947 ses->Suid, oparms->create_options,
2948 oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002949
Steve Frenchfae80442018-10-19 17:14:32 -05002950 atomic_inc(&tcon->num_remote_opens);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002951 oparms->fid->persistent_fid = rsp->PersistentFileId;
2952 oparms->fid->volatile_fid = rsp->VolatileFileId;
Aurelien Aptel86f740f2020-02-21 11:19:06 +01002953 oparms->fid->access = oparms->desired_access;
Steve Frenchdfe33f92018-10-30 19:50:31 -05002954#ifdef CONFIG_CIFS_DEBUG2
2955 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2956#endif /* CIFS_DEBUG2 */
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002957
2958 if (buf) {
Kees Cookfbcff332021-06-21 14:44:46 -07002959 buf->CreationTime = rsp->CreationTime;
2960 buf->LastAccessTime = rsp->LastAccessTime;
2961 buf->LastWriteTime = rsp->LastWriteTime;
2962 buf->ChangeTime = rsp->ChangeTime;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002963 buf->AllocationSize = rsp->AllocationSize;
2964 buf->EndOfFile = rsp->EndofFile;
2965 buf->Attributes = rsp->FileAttributes;
2966 buf->NumberOfLinks = cpu_to_le32(1);
2967 buf->DeletePending = 0;
2968 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002969
Steve French89a5bfa2019-07-18 17:22:18 -05002970
2971 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
Aurelien Aptel69dda302020-03-02 17:53:22 +01002972 oparms->fid->lease_key, oplock, buf, posix);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002973creat_exit:
Ronnie Sahlberg1eb9fb52018-08-08 15:07:46 +10002974 SMB2_open_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002975 free_rsp_buf(resp_buftype, rsp);
2976 return rc;
2977}
2978
Steve French4a72daf2013-06-25 00:20:49 -05002979int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002980SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2981 struct smb_rqst *rqst,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002982 u64 persistent_fid, u64 volatile_fid, u32 opcode,
Steve French153322f2019-03-28 22:32:49 -05002983 bool is_fsctl, char *in_data, u32 indatalen,
2984 __u32 max_response_size)
Steve French4a72daf2013-06-25 00:20:49 -05002985{
2986 struct smb2_ioctl_req *req;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002987 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002988 unsigned int total_len;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10002989 int rc;
Long Li2c87d6a2019-05-15 14:09:05 -07002990 char *in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05002991
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002992 rc = smb2_ioctl_req_init(opcode, tcon, server,
2993 (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05002994 if (rc)
2995 return rc;
2996
Long Li2c87d6a2019-05-15 14:09:05 -07002997 if (indatalen) {
2998 /*
2999 * indatalen is usually small at a couple of bytes max, so
3000 * just allocate through generic pool
3001 */
YueHaibingd81f0972019-06-01 03:31:10 +00003002 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
Long Li2c87d6a2019-05-15 14:09:05 -07003003 if (!in_data_buf) {
3004 cifs_small_buf_release(req);
3005 return -ENOMEM;
3006 }
Long Li2c87d6a2019-05-15 14:09:05 -07003007 }
3008
Steve French4a72daf2013-06-25 00:20:49 -05003009 req->CtlCode = cpu_to_le32(opcode);
3010 req->PersistentFileId = persistent_fid;
3011 req->VolatileFileId = volatile_fid;
3012
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003013 iov[0].iov_base = (char *)req;
3014 /*
3015 * If no input data, the size of ioctl struct in
3016 * protocol spec still includes a 1 byte data buffer,
3017 * but if input data passed to ioctl, we do not
3018 * want to double count this, so we do not send
3019 * the dummy one byte of data in iovec[0] if sending
3020 * input data (in iovec[1]).
3021 */
Steve French4a72daf2013-06-25 00:20:49 -05003022 if (indatalen) {
3023 req->InputCount = cpu_to_le32(indatalen);
3024 /* do not set InputOffset if no input data */
3025 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11003026 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003027 rqst->rq_nvec = 2;
3028 iov[0].iov_len = total_len - 1;
Long Li2c87d6a2019-05-15 14:09:05 -07003029 iov[1].iov_base = in_data_buf;
Steve French4a72daf2013-06-25 00:20:49 -05003030 iov[1].iov_len = indatalen;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003031 } else {
3032 rqst->rq_nvec = 1;
3033 iov[0].iov_len = total_len;
3034 }
Steve French4a72daf2013-06-25 00:20:49 -05003035
3036 req->OutputOffset = 0;
3037 req->OutputCount = 0; /* MBZ */
3038
3039 /*
Steve French153322f2019-03-28 22:32:49 -05003040 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3041 * We Could increase default MaxOutputResponse, but that could require
3042 * more credits. Windows typically sets this smaller, but for some
Steve French4a72daf2013-06-25 00:20:49 -05003043 * ioctls it may be useful to allow server to send more. No point
3044 * limiting what the server can send as long as fits in one credit
Steve French153322f2019-03-28 22:32:49 -05003045 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3046 * to increase this limit up in the future.
3047 * Note that for snapshot queries that servers like Azure expect that
3048 * the first query be minimal size (and just used to get the number/size
3049 * of previous versions) so response size must be specified as EXACTLY
3050 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3051 * of eight bytes. Currently that is the only case where we set max
3052 * response size smaller.
Steve French4a72daf2013-06-25 00:20:49 -05003053 */
Steve French153322f2019-03-28 22:32:49 -05003054 req->MaxOutputResponse = cpu_to_le32(max_response_size);
Namjae Jeonebf57442020-06-11 11:21:19 +09003055 req->sync_hdr.CreditCharge =
3056 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3057 SMB2_MAX_BUFFER_SIZE));
Steve French4a72daf2013-06-25 00:20:49 -05003058 if (is_fsctl)
3059 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3060 else
3061 req->Flags = 0;
3062
Steve French4587eee2017-10-25 15:58:31 -05003063 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3064 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11003065 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05003066
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003067 return 0;
3068}
3069
3070void
3071SMB2_ioctl_free(struct smb_rqst *rqst)
3072{
Murphy Zhou6457c202019-05-23 12:12:43 +08003073 int i;
Long Li2c87d6a2019-05-15 14:09:05 -07003074 if (rqst && rqst->rq_iov) {
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003075 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Murphy Zhou6457c202019-05-23 12:12:43 +08003076 for (i = 1; i < rqst->rq_nvec; i++)
3077 if (rqst->rq_iov[i].iov_base != smb2_padding)
3078 kfree(rqst->rq_iov[i].iov_base);
Long Li2c87d6a2019-05-15 14:09:05 -07003079 }
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003080}
3081
Steve French153322f2019-03-28 22:32:49 -05003082
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003083/*
3084 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3085 */
3086int
3087SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3088 u64 volatile_fid, u32 opcode, bool is_fsctl,
Steve French153322f2019-03-28 22:32:49 -05003089 char *in_data, u32 indatalen, u32 max_out_data_len,
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003090 char **out_data, u32 *plen /* returned data len */)
3091{
3092 struct smb_rqst rqst;
3093 struct smb2_ioctl_rsp *rsp = NULL;
3094 struct cifs_ses *ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003095 struct TCP_Server_Info *server;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003096 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003097 struct kvec rsp_iov = {NULL, 0};
3098 int resp_buftype = CIFS_NO_BUFFER;
3099 int rc = 0;
3100 int flags = 0;
3101
3102 cifs_dbg(FYI, "SMB2 IOCTL\n");
3103
3104 if (out_data != NULL)
3105 *out_data = NULL;
3106
3107 /* zero out returned data len, in case of error */
3108 if (plen)
3109 *plen = 0;
3110
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003111 if (!tcon)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003112 return -EIO;
3113
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003114 ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003115 if (!ses)
3116 return -EIO;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003117
3118 server = cifs_pick_channel(ses);
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003119 if (!server)
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003120 return -EIO;
3121
3122 if (smb3_encryption_required(tcon))
3123 flags |= CIFS_TRANSFORM_REQ;
3124
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003125 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003126 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003127 rqst.rq_iov = iov;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003128 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003129
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003130 rc = SMB2_ioctl_init(tcon, server,
3131 &rqst, persistent_fid, volatile_fid, opcode,
Steve French153322f2019-03-28 22:32:49 -05003132 is_fsctl, in_data, indatalen, max_out_data_len);
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003133 if (rc)
3134 goto ioctl_exit;
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003135
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003136 rc = cifs_send_recv(xid, ses, server,
3137 &rqst, &resp_buftype, flags,
Ronnie Sahlberg97754682017-11-09 12:14:20 +11003138 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003139 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05003140
Steve Frencheccb4422018-05-17 21:16:55 -05003141 if (rc != 0)
3142 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3143 ses->Suid, 0, opcode, rc);
3144
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003145 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
Steve French8e353102015-03-26 19:47:02 -05003146 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05003147 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06003148 } else if (rc == -EINVAL) {
3149 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3150 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05003151 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06003152 goto ioctl_exit;
3153 }
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003154 } else if (rc == -E2BIG) {
3155 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3156 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3157 goto ioctl_exit;
3158 }
Steve French4a72daf2013-06-25 00:20:49 -05003159 }
3160
3161 /* check if caller wants to look at return data or just return rc */
3162 if ((plen == NULL) || (out_data == NULL))
3163 goto ioctl_exit;
3164
3165 *plen = le32_to_cpu(rsp->OutputCount);
3166
3167 /* We check for obvious errors in the output buffer length and offset */
3168 if (*plen == 0)
3169 goto ioctl_exit; /* server returned no data */
Dan Carpenter2d204ee2018-09-10 14:12:07 +03003170 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003171 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
Steve French4a72daf2013-06-25 00:20:49 -05003172 *plen = 0;
3173 rc = -EIO;
3174 goto ioctl_exit;
3175 }
3176
Dan Carpenter2d204ee2018-09-10 14:12:07 +03003177 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003178 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
Steve French4a72daf2013-06-25 00:20:49 -05003179 le32_to_cpu(rsp->OutputOffset));
3180 *plen = 0;
3181 rc = -EIO;
3182 goto ioctl_exit;
3183 }
3184
YueHaibingd034fee2018-09-10 01:33:06 +00003185 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3186 *plen, GFP_KERNEL);
Steve French4a72daf2013-06-25 00:20:49 -05003187 if (*out_data == NULL) {
3188 rc = -ENOMEM;
3189 goto ioctl_exit;
3190 }
3191
Steve French4a72daf2013-06-25 00:20:49 -05003192ioctl_exit:
Ronnie Sahlbergccdc77a2019-03-13 14:37:48 +10003193 SMB2_ioctl_free(&rqst);
Steve French4a72daf2013-06-25 00:20:49 -05003194 free_rsp_buf(resp_buftype, rsp);
3195 return rc;
3196}
3197
Steve French64a5cfa2013-10-14 15:31:32 -05003198/*
3199 * Individual callers to ioctl worker function follow
3200 */
3201
3202int
3203SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3204 u64 persistent_fid, u64 volatile_fid)
3205{
3206 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05003207 struct compress_ioctl fsctl_input;
3208 char *ret_data = NULL;
3209
3210 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08003211 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05003212
3213 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3214 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3215 (char *)&fsctl_input /* data input */,
Steve French153322f2019-03-28 22:32:49 -05003216 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3217 &ret_data /* out data */, NULL);
Steve French64a5cfa2013-10-14 15:31:32 -05003218
3219 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05003220
3221 return rc;
3222}
3223
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003224int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003225SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3226 struct smb_rqst *rqst,
Steve French43f8a6a2019-12-02 21:46:54 -06003227 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003228{
3229 struct smb2_close_req *req;
3230 struct kvec *iov = rqst->rq_iov;
3231 unsigned int total_len;
3232 int rc;
3233
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003234 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3235 (void **) &req, &total_len);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003236 if (rc)
3237 return rc;
3238
3239 req->PersistentFileId = persistent_fid;
3240 req->VolatileFileId = volatile_fid;
Steve French43f8a6a2019-12-02 21:46:54 -06003241 if (query_attrs)
3242 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3243 else
3244 req->Flags = 0;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003245 iov[0].iov_base = (char *)req;
3246 iov[0].iov_len = total_len;
3247
3248 return 0;
3249}
3250
3251void
3252SMB2_close_free(struct smb_rqst *rqst)
3253{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003254 if (rqst && rqst->rq_iov)
3255 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003256}
3257
3258int
Steve French43f8a6a2019-12-02 21:46:54 -06003259__SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3260 u64 persistent_fid, u64 volatile_fid,
3261 struct smb2_file_network_open_info *pbuf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003262{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003263 struct smb_rqst rqst;
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003264 struct smb2_close_rsp *rsp = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003265 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003266 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003267 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003268 struct kvec rsp_iov;
Garry McNultyef2298a2018-10-03 20:51:21 +01003269 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003270 int rc = 0;
Steve French9e8fae22019-12-02 17:55:41 -06003271 int flags = 0;
Steve French43f8a6a2019-12-02 21:46:54 -06003272 bool query_attrs = false;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003273
Joe Perchesf96637b2013-05-04 22:12:25 -05003274 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003275
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003276 if (!ses || !server)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003277 return -EIO;
3278
Steve French5a77e752018-05-09 17:43:08 -05003279 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003280 flags |= CIFS_TRANSFORM_REQ;
3281
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003282 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003283 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003284 rqst.rq_iov = iov;
3285 rqst.rq_nvec = 1;
3286
Steve French43f8a6a2019-12-02 21:46:54 -06003287 /* check if need to ask server to return timestamps in close response */
3288 if (pbuf)
3289 query_attrs = true;
3290
Steve Frenchf90f9792019-09-03 18:35:42 -05003291 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003292 rc = SMB2_close_init(tcon, server,
3293 &rqst, persistent_fid, volatile_fid,
Steve French43f8a6a2019-12-02 21:46:54 -06003294 query_attrs);
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003295 if (rc)
3296 goto close_exit;
3297
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003298 rc = cifs_send_recv(xid, ses, server,
3299 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003300 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003301
3302 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09003303 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003304 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3305 rc);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003306 goto close_exit;
Steve French43f8a6a2019-12-02 21:46:54 -06003307 } else {
Steve Frenchf90f9792019-09-03 18:35:42 -05003308 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3309 ses->Suid);
Steve French43f8a6a2019-12-02 21:46:54 -06003310 /*
3311 * Note that have to subtract 4 since struct network_open_info
3312 * has a final 4 byte pad that close response does not have
3313 */
3314 if (pbuf)
3315 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3316 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003317
Steve Frenchfae80442018-10-19 17:14:32 -05003318 atomic_dec(&tcon->num_remote_opens);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003319close_exit:
Ronnie Sahlberg8eb4ecf2018-08-01 09:26:16 +10003320 SMB2_close_free(&rqst);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003321 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003322
3323 /* retry close in a worker thread if this one is interrupted */
Paulo Alcantara2659d3b2021-01-13 14:16:16 -03003324 if (is_interrupt_error(rc)) {
Steve French9e8fae22019-12-02 17:55:41 -06003325 int tmp_rc;
3326
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003327 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3328 volatile_fid);
3329 if (tmp_rc)
3330 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3331 persistent_fid, tmp_rc);
3332 }
Pavel Shilovsky9150c3a2019-11-21 11:35:12 -08003333 return rc;
Ronnie Sahlberg97ca1762018-04-26 08:50:49 -06003334}
3335
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003336int
Steve French43f8a6a2019-12-02 21:46:54 -06003337SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3338 u64 persistent_fid, u64 volatile_fid)
3339{
3340 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3341}
3342
3343int
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003344smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3345 struct kvec *iov, unsigned int min_buf_size)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003346{
Ronnie Sahlbergc1596ff2018-04-09 18:06:30 +10003347 unsigned int smb_len = iov->iov_len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003348 char *end_of_smb = smb_len + (char *)iov->iov_base;
3349 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003350 char *end_of_buf = begin_of_buf + buffer_length;
3351
3352
3353 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003354 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3355 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003356 return -EINVAL;
3357 }
3358
3359 /* check if beyond RFC1001 maximum length */
3360 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003361 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3362 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003363 return -EINVAL;
3364 }
3365
3366 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07003367 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003368 return -EINVAL;
3369 }
3370
3371 return 0;
3372}
3373
3374/*
3375 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3376 * Caller must free buffer.
3377 */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003378int
3379smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3380 struct kvec *iov, unsigned int minbufsize,
3381 char *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003382{
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003383 char *begin_of_buf = offset + (char *)iov->iov_base;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003384 int rc;
3385
3386 if (!data)
3387 return -EINVAL;
3388
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10003389 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003390 if (rc)
3391 return rc;
3392
3393 memcpy(data, begin_of_buf, buffer_length);
3394
3395 return 0;
3396}
3397
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003398int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003399SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3400 struct smb_rqst *rqst,
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003401 u64 persistent_fid, u64 volatile_fid,
3402 u8 info_class, u8 info_type, u32 additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003403 size_t output_len, size_t input_len, void *input)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003404{
3405 struct smb2_query_info_req *req;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003406 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003407 unsigned int total_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003408 int rc;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003409
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003410 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3411 (void **) &req, &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003412 if (rc)
3413 return rc;
3414
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003415 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003416 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003417 req->PersistentFileId = persistent_fid;
3418 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003419 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02003420
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003421 req->OutputBufferLength = cpu_to_le32(output_len);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003422 if (input_len) {
3423 req->InputBufferLength = cpu_to_le32(input_len);
3424 /* total_len for smb query request never close to le16 max */
3425 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3426 memcpy(req->Buffer, input, input_len);
3427 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003428
3429 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003430 /* 1 for Buffer */
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003431 iov[0].iov_len = total_len - 1 + input_len;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003432 return 0;
3433}
3434
3435void
3436SMB2_query_info_free(struct smb_rqst *rqst)
3437{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10003438 if (rqst && rqst->rq_iov)
3439 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003440}
3441
3442static int
3443query_info(const unsigned int xid, struct cifs_tcon *tcon,
3444 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3445 u32 additional_info, size_t output_len, size_t min_len, void **data,
3446 u32 *dlen)
3447{
3448 struct smb_rqst rqst;
3449 struct smb2_query_info_rsp *rsp = NULL;
3450 struct kvec iov[1];
3451 struct kvec rsp_iov;
3452 int rc = 0;
Garry McNultyef2298a2018-10-03 20:51:21 +01003453 int resp_buftype = CIFS_NO_BUFFER;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003454 struct cifs_ses *ses = tcon->ses;
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003455 struct TCP_Server_Info *server;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003456 int flags = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003457 bool allocated = false;
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003458
3459 cifs_dbg(FYI, "Query Info\n");
3460
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003461 if (!ses)
3462 return -EIO;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003463 server = cifs_pick_channel(ses);
Colin Ian Kingac6ad7a2019-09-02 16:10:59 +01003464 if (!server)
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003465 return -EIO;
3466
3467 if (smb3_encryption_required(tcon))
3468 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003469
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003470 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003471 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003472 rqst.rq_iov = iov;
3473 rqst.rq_nvec = 1;
3474
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003475 rc = SMB2_query_info_init(tcon, server,
3476 &rqst, persistent_fid, volatile_fid,
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003477 info_class, info_type, additional_info,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003478 output_len, 0, NULL);
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003479 if (rc)
3480 goto qinf_exit;
3481
Steve Frenchd42043a2019-02-26 21:58:30 -06003482 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3483 ses->Suid, info_class, (__u32)info_type);
3484
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003485 rc = cifs_send_recv(xid, ses, server,
3486 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003487 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003488
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003489 if (rc) {
3490 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003491 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3492 ses->Suid, info_class, (__u32)info_type, rc);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003493 goto qinf_exit;
3494 }
3495
Steve Frenchd42043a2019-02-26 21:58:30 -06003496 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3497 ses->Suid, info_class, (__u32)info_type);
3498
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003499 if (dlen) {
3500 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3501 if (!*data) {
3502 *data = kmalloc(*dlen, GFP_KERNEL);
3503 if (!*data) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003504 cifs_tcon_dbg(VFS,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003505 "Error %d allocating memory for acl\n",
3506 rc);
3507 *dlen = 0;
Colin Ian King73aaf922019-01-16 16:28:59 +00003508 rc = -ENOMEM;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003509 goto qinf_exit;
3510 }
Colin Ian King73aaf922019-01-16 16:28:59 +00003511 allocated = true;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003512 }
3513 }
3514
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10003515 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3516 le32_to_cpu(rsp->OutputBufferLength),
3517 &rsp_iov, min_len, *data);
Colin Ian King73aaf922019-01-16 16:28:59 +00003518 if (rc && allocated) {
3519 kfree(*data);
3520 *data = NULL;
3521 *dlen = 0;
3522 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003523
3524qinf_exit:
Ronnie Sahlberg296ecba2018-08-01 09:26:17 +10003525 SMB2_query_info_free(&rqst);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003526 free_rsp_buf(resp_buftype, rsp);
3527 return rc;
3528}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003529
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003530int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3531 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003532{
3533 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003534 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04003535 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003536 sizeof(struct smb2_file_all_info), (void **)&data,
3537 NULL);
3538}
3539
Steve Frenche0ae8a92021-06-19 16:19:09 -05003540#if 0
3541/* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003542int
Steve Frenchb1bc1872020-06-11 20:23:38 -05003543SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3544 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3545{
3546 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3547 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3548 *plen = 0;
3549
3550 return query_info(xid, tcon, persistent_fid, volatile_fid,
3551 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3552 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
Steve Frenche0ae8a92021-06-19 16:19:09 -05003553 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
Steve Frenchb1bc1872020-06-11 20:23:38 -05003554}
Steve Frenche0ae8a92021-06-19 16:19:09 -05003555#endif
Steve Frenchb1bc1872020-06-11 20:23:38 -05003556
3557int
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003558SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
Boris Protopopov3970acf2020-12-18 11:30:12 -06003559 u64 persistent_fid, u64 volatile_fid,
Boris Protopopov9541b812020-12-17 20:58:08 +00003560 void **data, u32 *plen, u32 extra_info)
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003561{
Boris Protopopov9541b812020-12-17 20:58:08 +00003562 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3563 extra_info;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003564 *plen = 0;
3565
3566 return query_info(xid, tcon, persistent_fid, volatile_fid,
3567 0, SMB2_O_INFO_SECURITY, additional_info,
Shirish Pargaonkaree25c6d2018-06-04 06:46:22 -05003568 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003569}
3570
3571int
3572SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3573 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3574{
3575 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003576 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003577 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05003578 sizeof(struct smb2_file_internal_info),
3579 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003580}
3581
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003582/*
Steve Frenchc3498182019-09-15 22:38:52 -05003583 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3584 * See MS-SMB2 2.2.35 and 2.2.36
3585 */
3586
zhengbin388962e2019-09-23 15:06:18 +08003587static int
Steve Frenchc3498182019-09-15 22:38:52 -05003588SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003589 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3590 u64 persistent_fid, u64 volatile_fid,
3591 u32 completion_filter, bool watch_tree)
Steve Frenchc3498182019-09-15 22:38:52 -05003592{
3593 struct smb2_change_notify_req *req;
3594 struct kvec *iov = rqst->rq_iov;
3595 unsigned int total_len;
3596 int rc;
3597
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003598 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3599 (void **) &req, &total_len);
Steve Frenchc3498182019-09-15 22:38:52 -05003600 if (rc)
3601 return rc;
3602
3603 req->PersistentFileId = persistent_fid;
3604 req->VolatileFileId = volatile_fid;
Steve Frenchd26c2dd2020-02-06 06:00:14 -06003605 /* See note 354 of MS-SMB2, 64K max */
Steve French52870d52019-10-01 21:25:46 -05003606 req->OutputBufferLength =
3607 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
Steve Frenchc3498182019-09-15 22:38:52 -05003608 req->CompletionFilter = cpu_to_le32(completion_filter);
3609 if (watch_tree)
3610 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3611 else
3612 req->Flags = 0;
3613
3614 iov[0].iov_base = (char *)req;
3615 iov[0].iov_len = total_len;
3616
3617 return 0;
3618}
3619
3620int
3621SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3622 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3623 u32 completion_filter)
3624{
3625 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003626 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve Frenchc3498182019-09-15 22:38:52 -05003627 struct smb_rqst rqst;
3628 struct kvec iov[1];
3629 struct kvec rsp_iov = {NULL, 0};
3630 int resp_buftype = CIFS_NO_BUFFER;
3631 int flags = 0;
3632 int rc = 0;
3633
3634 cifs_dbg(FYI, "change notify\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003635 if (!ses || !server)
Steve Frenchc3498182019-09-15 22:38:52 -05003636 return -EIO;
3637
3638 if (smb3_encryption_required(tcon))
3639 flags |= CIFS_TRANSFORM_REQ;
3640
3641 memset(&rqst, 0, sizeof(struct smb_rqst));
3642 memset(&iov, 0, sizeof(iov));
3643 rqst.rq_iov = iov;
3644 rqst.rq_nvec = 1;
3645
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003646 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3647 persistent_fid, volatile_fid,
Steve Frenchc3498182019-09-15 22:38:52 -05003648 completion_filter, watch_tree);
3649 if (rc)
3650 goto cnotify_exit;
3651
3652 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3653 (u8)watch_tree, completion_filter);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003654 rc = cifs_send_recv(xid, ses, server,
3655 &rqst, &resp_buftype, flags, &rsp_iov);
Steve Frenchc3498182019-09-15 22:38:52 -05003656
3657 if (rc != 0) {
3658 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3659 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3660 (u8)watch_tree, completion_filter, rc);
3661 } else
3662 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3663 ses->Suid, (u8)watch_tree, completion_filter);
3664
3665 cnotify_exit:
3666 if (rqst.rq_iov)
3667 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3668 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3669 return rc;
3670}
3671
3672
3673
3674/*
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003675 * This is a no-op for now. We're not really interested in the reply, but
3676 * rather in the fact that the server sent one and that server->lstrp
3677 * gets updated.
3678 *
3679 * FIXME: maybe we should consider checking that the reply matches request?
3680 */
3681static void
3682smb2_echo_callback(struct mid_q_entry *mid)
3683{
3684 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003685 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003686 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003687
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08003688 if (mid->mid_state == MID_RESPONSE_RECEIVED
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003689 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3690 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3691 credits.instance = server->reconnect_instance;
3692 }
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003693
3694 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003695 add_credits(server, &credits, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003696}
3697
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003698void smb2_reconnect_server(struct work_struct *work)
3699{
3700 struct TCP_Server_Info *server = container_of(work,
3701 struct TCP_Server_Info, reconnect.work);
3702 struct cifs_ses *ses;
3703 struct cifs_tcon *tcon, *tcon2;
3704 struct list_head tmp_list;
3705 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01003706 int rc;
3707 int resched = false;
3708
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003709
3710 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3711 mutex_lock(&server->reconnect_mutex);
3712
3713 INIT_LIST_HEAD(&tmp_list);
3714 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3715
3716 spin_lock(&cifs_tcp_ses_lock);
3717 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3718 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003719 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003720 tcon->tc_count++;
3721 list_add_tail(&tcon->rlist, &tmp_list);
3722 tcon_exist = true;
3723 }
3724 }
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003725 /*
3726 * IPC has the same lifetime as its session and uses its
3727 * refcount.
3728 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003729 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3730 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3731 tcon_exist = true;
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003732 ses->ses_count++;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003733 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003734 }
3735 /*
3736 * Get the reference to server struct to be sure that the last call of
3737 * cifs_put_tcon() in the loop below won't release the server pointer.
3738 */
3739 if (tcon_exist)
3740 server->srv_count++;
3741
3742 spin_unlock(&cifs_tcp_ses_lock);
3743
3744 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003745 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
Germano Percossi18ea4312017-04-07 12:29:36 +01003746 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08003747 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01003748 else
3749 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003750 list_del_init(&tcon->rlist);
Ronnie Sahlberg0ff2b012019-06-05 10:15:34 +10003751 if (tcon->ipc)
3752 cifs_put_smb_ses(tcon->ses);
3753 else
3754 cifs_put_tcon(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003755 }
3756
3757 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01003758 if (resched)
3759 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003760 mutex_unlock(&server->reconnect_mutex);
3761
3762 /* now we can safely release srv struct */
3763 if (tcon_exist)
3764 cifs_put_tcp_session(server, 1);
3765}
3766
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003767int
3768SMB2_echo(struct TCP_Server_Info *server)
3769{
3770 struct smb2_echo_req *req;
3771 int rc = 0;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003772 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003773 struct smb_rqst rqst = { .rq_iov = iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003774 .rq_nvec = 1 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003775 unsigned int total_len;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003776
Joe Perchesf96637b2013-05-04 22:12:25 -05003777 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003778
Steve French4fcd1812016-06-22 20:12:05 -05003779 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003780 /* No need to send echo on newly established connections */
Stefan Metzmacherb08484d2020-02-24 14:14:59 +01003781 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003782 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05003783 }
3784
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003785 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3786 (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003787 if (rc)
3788 return rc;
3789
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11003790 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003791
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003792 iov[0].iov_len = total_len;
3793 iov[0].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003794
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08003795 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08003796 server, CIFS_ECHO_OP, NULL);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003797 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003798 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003799
3800 cifs_small_buf_release(req);
3801 return rc;
3802}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003803
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003804void
3805SMB2_flush_free(struct smb_rqst *rqst)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003806{
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003807 if (rqst && rqst->rq_iov)
3808 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3809}
3810
3811int
3812SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003813 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3814 u64 persistent_fid, u64 volatile_fid)
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003815{
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003816 struct smb2_flush_req *req;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003817 struct kvec *iov = rqst->rq_iov;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003818 unsigned int total_len;
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003819 int rc;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003820
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003821 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3822 (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003823 if (rc)
3824 return rc;
3825
3826 req->PersistentFileId = persistent_fid;
3827 req->VolatileFileId = volatile_fid;
3828
3829 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11003830 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003831
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003832 return 0;
3833}
3834
3835int
3836SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3837 u64 volatile_fid)
3838{
3839 struct cifs_ses *ses = tcon->ses;
3840 struct smb_rqst rqst;
3841 struct kvec iov[1];
3842 struct kvec rsp_iov = {NULL, 0};
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003843 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003844 int resp_buftype = CIFS_NO_BUFFER;
3845 int flags = 0;
3846 int rc = 0;
3847
3848 cifs_dbg(FYI, "flush\n");
3849 if (!ses || !(ses->server))
3850 return -EIO;
3851
3852 if (smb3_encryption_required(tcon))
3853 flags |= CIFS_TRANSFORM_REQ;
3854
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003855 memset(&rqst, 0, sizeof(struct smb_rqst));
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003856 memset(&iov, 0, sizeof(iov));
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10003857 rqst.rq_iov = iov;
3858 rqst.rq_nvec = 1;
3859
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003860 rc = SMB2_flush_init(xid, &rqst, tcon, server,
3861 persistent_fid, volatile_fid);
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003862 if (rc)
3863 goto flush_exit;
3864
Steve Frenchf90f9792019-09-03 18:35:42 -05003865 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003866 rc = cifs_send_recv(xid, ses, server,
3867 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003868
Steve Frencheccb4422018-05-17 21:16:55 -05003869 if (rc != 0) {
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003870 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05003871 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3872 rc);
Steve Frenchf90f9792019-09-03 18:35:42 -05003873 } else
3874 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3875 ses->Suid);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003876
Ronnie Sahlberg86e14e12019-07-16 15:07:08 +10003877 flush_exit:
3878 SMB2_flush_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003879 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003880 return rc;
3881}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003882
3883/*
3884 * To form a chain of read requests, any read requests after the first should
3885 * have the end_of_chain boolean set to true.
3886 */
3887static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003888smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07003889 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3890 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003891{
3892 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003893 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003894 struct smb2_sync_hdr *shdr;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003895 struct TCP_Server_Info *server = io_parms->server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003896
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003897 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
3898 (void **) &req, total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003899 if (rc)
3900 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07003901
Long Li2dabfd52017-11-07 01:54:53 -07003902 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003903 return -ECONNABORTED;
3904
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003905 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003906 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003907
3908 req->PersistentFileId = io_parms->persistent_fid;
3909 req->VolatileFileId = io_parms->volatile_fid;
3910 req->ReadChannelInfoOffset = 0; /* reserved */
3911 req->ReadChannelInfoLength = 0; /* reserved */
3912 req->Channel = 0; /* reserved */
3913 req->MinimumCount = 0;
3914 req->Length = cpu_to_le32(io_parms->length);
3915 req->Offset = cpu_to_le64(io_parms->offset);
Steve Frenchd323c2462019-02-25 00:52:43 -06003916
3917 trace_smb3_read_enter(0 /* xid */,
3918 io_parms->persistent_fid,
3919 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3920 io_parms->offset, io_parms->length);
Long Libd3dcc62017-11-22 17:38:47 -07003921#ifdef CONFIG_CIFS_SMB_DIRECT
3922 /*
3923 * If we want to do a RDMA write, fill in and append
3924 * smbd_buffer_descriptor_v1 to the end of read request
3925 */
Long Libb4c0412018-04-17 12:17:08 -07003926 if (server->rdma && rdata && !server->sign &&
Long Libd3dcc62017-11-22 17:38:47 -07003927 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003928
Long Libd3dcc62017-11-22 17:38:47 -07003929 struct smbd_buffer_descriptor_v1 *v1;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003930 bool need_invalidate = server->dialect == SMB30_PROT_ID;
Long Libd3dcc62017-11-22 17:38:47 -07003931
3932 rdata->mr = smbd_register_mr(
3933 server->smbd_conn, rdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07003934 rdata->nr_pages, rdata->page_offset,
3935 rdata->tailsz, true, need_invalidate);
Long Libd3dcc62017-11-22 17:38:47 -07003936 if (!rdata->mr)
Long Lib7972092019-04-05 21:36:34 +00003937 return -EAGAIN;
Long Libd3dcc62017-11-22 17:38:47 -07003938
3939 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3940 if (need_invalidate)
3941 req->Channel = SMB2_CHANNEL_RDMA_V1;
3942 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06003943 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07003944 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06003945 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07003946 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06003947 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3948 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3949 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07003950
3951 *total_len += sizeof(*v1) - 1;
3952 }
3953#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003954 if (request_type & CHAINED_REQUEST) {
3955 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08003956 /* next 8-byte aligned request */
3957 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3958 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003959 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003960 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003961 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003962 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003963 /*
3964 * Related requests use info from previous read request
3965 * in chain.
3966 */
Steve Frenchc0d46712021-05-15 09:52:22 -05003967 shdr->SessionId = 0xFFFFFFFFFFFFFFFF;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003968 shdr->TreeId = 0xFFFFFFFF;
Steve Frenchc0d46712021-05-15 09:52:22 -05003969 req->PersistentFileId = 0xFFFFFFFFFFFFFFFF;
3970 req->VolatileFileId = 0xFFFFFFFFFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003971 }
3972 }
3973 if (remaining_bytes > io_parms->length)
3974 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3975 else
3976 req->RemainingBytes = 0;
3977
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003978 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003979 return rc;
3980}
3981
3982static void
3983smb2_readv_callback(struct mid_q_entry *mid)
3984{
3985 struct cifs_readdata *rdata = mid->callback_data;
3986 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003987 struct TCP_Server_Info *server = rdata->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08003988 struct smb2_sync_hdr *shdr =
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003989 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08003990 struct cifs_credits credits = { .value = 0, .instance = 0 };
Steve French46f17d12019-09-04 23:07:52 -05003991 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3992 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07003993 .rq_pages = rdata->pages,
Long Li1dbe3462018-05-30 12:47:55 -07003994 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07003995 .rq_npages = rdata->nr_pages,
3996 .rq_pagesz = rdata->pagesz,
3997 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003998
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003999 WARN_ONCE(rdata->server != mid->server,
4000 "rdata server %p != mid server %p",
4001 rdata->server, mid->server);
4002
Joe Perchesf96637b2013-05-04 22:12:25 -05004003 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4004 __func__, mid->mid, mid->mid_state, rdata->result,
4005 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004006
4007 switch (mid->mid_state) {
4008 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004009 credits.value = le16_to_cpu(shdr->CreditRequest);
4010 credits.instance = server->reconnect_instance;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004011 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004012 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07004013 int rc;
4014
Jeff Layton0b688cf2012-09-18 16:20:34 -07004015 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07004016 if (rc)
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004017 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05004018 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07004019 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004020 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04004021 task_io_account_read(rdata->got_bytes);
4022 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004023 break;
4024 case MID_REQUEST_SUBMITTED:
4025 case MID_RETRY_NEEDED:
4026 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04004027 if (server->sign && rdata->got_bytes)
4028 /* reset bytes number since we can not check a sign */
4029 rdata->got_bytes = 0;
4030 /* FIXME: should this be counted toward the initiating task? */
4031 task_io_account_read(rdata->got_bytes);
4032 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004033 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08004034 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004035 credits.value = le16_to_cpu(shdr->CreditRequest);
4036 credits.instance = server->reconnect_instance;
Miaohe Lin30b5ae22020-08-08 16:36:37 +08004037 fallthrough;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004038 default:
Pavel Shilovsky6b15eb12019-01-18 15:46:14 -08004039 rdata->result = -EIO;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004040 }
Long Libd3dcc62017-11-22 17:38:47 -07004041#ifdef CONFIG_CIFS_SMB_DIRECT
4042 /*
4043 * If this rdata has a memmory registered, the MR can be freed
4044 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4045 * because they have limited number and are used for future I/Os
4046 */
4047 if (rdata->mr) {
4048 smbd_deregister_mr(rdata->mr);
4049 rdata->mr = NULL;
4050 }
4051#endif
Pavel Shilovsky082aaa82019-01-18 15:54:34 -08004052 if (rdata->result && rdata->result != -ENODATA) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004053 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004054 trace_smb3_read_err(0 /* xid */,
4055 rdata->cfile->fid.persistent_fid,
4056 tcon->tid, tcon->ses->Suid, rdata->offset,
4057 rdata->bytes, rdata->result);
4058 } else
4059 trace_smb3_read_done(0 /* xid */,
4060 rdata->cfile->fid.persistent_fid,
4061 tcon->tid, tcon->ses->Suid,
4062 rdata->offset, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004063
4064 queue_work(cifsiod_wq, &rdata->work);
4065 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004066 add_credits(server, &credits, 0);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004067}
4068
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004069/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004070int
4071smb2_async_readv(struct cifs_readdata *rdata)
4072{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04004073 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004074 char *buf;
4075 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004076 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004077 struct smb_rqst rqst = { .rq_iov = rdata->iov,
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004078 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04004079 struct TCP_Server_Info *server;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004080 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004081 unsigned int total_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004082
Joe Perchesf96637b2013-05-04 22:12:25 -05004083 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4084 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004085
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004086 if (!rdata->server)
4087 rdata->server = cifs_pick_channel(tcon->ses);
4088
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004089 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004090 io_parms.server = server = rdata->server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004091 io_parms.offset = rdata->offset;
4092 io_parms.length = rdata->bytes;
4093 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4094 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4095 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04004096
Long Li2dabfd52017-11-07 01:54:53 -07004097 rc = smb2_new_read_req(
4098 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08004099 if (rc)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004100 return rc;
4101
Steve French5a77e752018-05-09 17:43:08 -05004102 if (smb3_encryption_required(io_parms.tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004103 flags |= CIFS_TRANSFORM_REQ;
4104
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004105 rdata->iov[0].iov_base = buf;
4106 rdata->iov[0].iov_len = total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08004107
4108 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004109
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004110 if (rdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004111 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04004112 SMB2_MAX_BUFFER_SIZE));
Aurelien Aptel88fd98a2021-03-04 17:51:48 +00004113 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004114
4115 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4116 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004117 goto async_readv_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004118
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004119 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04004120 }
4121
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004122 kref_get(&rdata->refcount);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004123 rc = cifs_call_async(server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004124 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08004125 smb3_handle_read_data, rdata, flags,
4126 &rdata->credits);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004127 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004128 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004129 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004130 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4131 io_parms.tcon->tid,
4132 io_parms.tcon->ses->Suid,
4133 io_parms.offset, io_parms.length, rc);
4134 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004135
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004136async_readv_out:
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004137 cifs_small_buf_release(buf);
4138 return rc;
4139}
Pavel Shilovsky33319142012-09-18 16:20:29 -07004140
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004141int
4142SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4143 unsigned int *nbytes, char **buf, int *buf_type)
4144{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004145 struct smb_rqst rqst;
Colin Ian King1efd4fc2019-07-31 10:05:26 +01004146 int resp_buftype, rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08004147 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004148 struct smb2_read_rsp *rsp = NULL;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004149 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004150 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004151 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004152 int flags = CIFS_LOG_ERROR;
4153 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004154
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004155 if (!io_parms->server)
4156 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4157
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004158 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07004159 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004160 if (rc)
4161 return rc;
4162
Steve French5a77e752018-05-09 17:43:08 -05004163 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004164 flags |= CIFS_TRANSFORM_REQ;
4165
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004166 iov[0].iov_base = (char *)req;
4167 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004168
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004169 memset(&rqst, 0, sizeof(struct smb_rqst));
4170 rqst.rq_iov = iov;
4171 rqst.rq_nvec = 1;
4172
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004173 rc = cifs_send_recv(xid, ses, io_parms->server,
4174 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004175 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004176
4177 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004178 if (rc != -ENODATA) {
4179 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4180 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004181 trace_smb3_read_err(xid, req->PersistentFileId,
4182 io_parms->tcon->tid, ses->Suid,
4183 io_parms->offset, io_parms->length,
4184 rc);
Steve Frenchb0a42f22019-02-25 15:02:58 -06004185 } else
4186 trace_smb3_read_done(xid, req->PersistentFileId,
4187 io_parms->tcon->tid, ses->Suid,
4188 io_parms->offset, 0);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004189 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Ronnie Sahlberg05fd5c22019-04-23 16:39:45 +10004190 cifs_small_buf_release(req);
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004191 return rc == -ENODATA ? 0 : rc;
Steve Frencheccb4422018-05-17 21:16:55 -05004192 } else
4193 trace_smb3_read_done(xid, req->PersistentFileId,
4194 io_parms->tcon->tid, ses->Suid,
4195 io_parms->offset, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004196
ZhangXiaoxu088aaf12019-04-06 15:47:39 +08004197 cifs_small_buf_release(req);
4198
Ronnie Sahlberga821df32017-11-21 09:36:33 +11004199 *nbytes = le32_to_cpu(rsp->DataLength);
4200 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4201 (*nbytes > io_parms->length)) {
4202 cifs_dbg(FYI, "bad length %d for count %d\n",
4203 *nbytes, io_parms->length);
4204 rc = -EIO;
4205 *nbytes = 0;
4206 }
4207
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004208 if (*buf) {
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004209 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004210 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004211 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004212 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004213 if (resp_buftype == CIFS_SMALL_BUFFER)
4214 *buf_type = CIFS_SMALL_BUFFER;
4215 else if (resp_buftype == CIFS_LARGE_BUFFER)
4216 *buf_type = CIFS_LARGE_BUFFER;
4217 }
4218 return rc;
4219}
4220
Pavel Shilovsky33319142012-09-18 16:20:29 -07004221/*
4222 * Check the mid_state and signature on received buffer (if any), and queue the
4223 * workqueue completion task.
4224 */
4225static void
4226smb2_writev_callback(struct mid_q_entry *mid)
4227{
4228 struct cifs_writedata *wdata = mid->callback_data;
4229 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004230 struct TCP_Server_Info *server = wdata->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004231 unsigned int written;
4232 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004233 struct cifs_credits credits = { .value = 0, .instance = 0 };
Pavel Shilovsky33319142012-09-18 16:20:29 -07004234
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004235 WARN_ONCE(wdata->server != mid->server,
4236 "wdata server %p != mid server %p",
4237 wdata->server, mid->server);
4238
Pavel Shilovsky33319142012-09-18 16:20:29 -07004239 switch (mid->mid_state) {
4240 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004241 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4242 credits.instance = server->reconnect_instance;
4243 wdata->result = smb2_check_receive(mid, server, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004244 if (wdata->result != 0)
4245 break;
4246
4247 written = le32_to_cpu(rsp->DataLength);
4248 /*
4249 * Mask off high 16 bits when bytes written as returned
4250 * by the server is greater than bytes requested by the
4251 * client. OS/2 servers are known to set incorrect
4252 * CountHigh values.
4253 */
4254 if (written > wdata->bytes)
4255 written &= 0xFFFF;
4256
4257 if (written < wdata->bytes)
4258 wdata->result = -ENOSPC;
4259 else
4260 wdata->bytes = written;
4261 break;
4262 case MID_REQUEST_SUBMITTED:
4263 case MID_RETRY_NEEDED:
4264 wdata->result = -EAGAIN;
4265 break;
Pavel Shilovsky0fd1d372019-01-15 15:08:48 -08004266 case MID_RESPONSE_MALFORMED:
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004267 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4268 credits.instance = server->reconnect_instance;
Miaohe Lin30b5ae22020-08-08 16:36:37 +08004269 fallthrough;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004270 default:
4271 wdata->result = -EIO;
4272 break;
4273 }
Long Lidb223a52017-11-22 17:38:45 -07004274#ifdef CONFIG_CIFS_SMB_DIRECT
4275 /*
4276 * If this wdata has a memory registered, the MR can be freed
4277 * The number of MRs available is limited, it's important to recover
4278 * used MR as soon as I/O is finished. Hold MR longer in the later
4279 * I/O process can possibly result in I/O deadlock due to lack of MR
4280 * to send request on I/O retry
4281 */
4282 if (wdata->mr) {
4283 smbd_deregister_mr(wdata->mr);
4284 wdata->mr = NULL;
4285 }
4286#endif
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004287 if (wdata->result) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004288 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004289 trace_smb3_write_err(0 /* no xid */,
4290 wdata->cfile->fid.persistent_fid,
4291 tcon->tid, tcon->ses->Suid, wdata->offset,
4292 wdata->bytes, wdata->result);
Steve Frenchd6fd4192020-02-05 16:52:11 -06004293 if (wdata->result == -ENOSPC)
Joe Perchesa0a30362020-04-14 22:42:53 -07004294 pr_warn_once("Out of space writing to %s\n",
4295 tcon->treeName);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004296 } else
4297 trace_smb3_write_done(0 /* no xid */,
4298 wdata->cfile->fid.persistent_fid,
4299 tcon->tid, tcon->ses->Suid,
4300 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004301
4302 queue_work(cifsiod_wq, &wdata->work);
4303 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08004304 add_credits(server, &credits, 0);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004305}
4306
4307/* smb2_async_writev - send an async write, and set up mid to handle result */
4308int
Steve French4a5c80d2014-02-07 20:45:12 -06004309smb2_async_writev(struct cifs_writedata *wdata,
4310 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07004311{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004312 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004313 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004314 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004315 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004316 struct TCP_Server_Info *server = wdata->server;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004317 struct kvec iov[1];
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004318 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004319 unsigned int total_len;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004320
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004321 if (!wdata->server)
4322 server = wdata->server = cifs_pick_channel(tcon->ses);
4323
4324 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4325 (void **) &req, &total_len);
Pavel Shilovskyf0b93cb2019-01-25 11:10:00 -08004326 if (rc)
4327 return rc;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004328
Steve French5a77e752018-05-09 17:43:08 -05004329 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004330 flags |= CIFS_TRANSFORM_REQ;
4331
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004332 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004333 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004334
4335 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4336 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4337 req->WriteChannelInfoOffset = 0;
4338 req->WriteChannelInfoLength = 0;
4339 req->Channel = 0;
4340 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004341 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004342 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07004343 req->RemainingBytes = 0;
Steve Frenchd323c2462019-02-25 00:52:43 -06004344
4345 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4346 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004347#ifdef CONFIG_CIFS_SMB_DIRECT
4348 /*
4349 * If we want to do a server RDMA read, fill in and append
4350 * smbd_buffer_descriptor_v1 to the end of write request
4351 */
Long Libb4c0412018-04-17 12:17:08 -07004352 if (server->rdma && !server->sign && wdata->bytes >=
Long Lidb223a52017-11-22 17:38:45 -07004353 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07004354
Long Lidb223a52017-11-22 17:38:45 -07004355 struct smbd_buffer_descriptor_v1 *v1;
4356 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4357
4358 wdata->mr = smbd_register_mr(
4359 server->smbd_conn, wdata->pages,
Long Li7cf20bc2018-05-30 12:48:02 -07004360 wdata->nr_pages, wdata->page_offset,
4361 wdata->tailsz, false, need_invalidate);
Long Lidb223a52017-11-22 17:38:45 -07004362 if (!wdata->mr) {
Long Lib7972092019-04-05 21:36:34 +00004363 rc = -EAGAIN;
Long Lidb223a52017-11-22 17:38:45 -07004364 goto async_writev_out;
4365 }
4366 req->Length = 0;
4367 req->DataOffset = 0;
Long Li7cf20bc2018-05-30 12:48:02 -07004368 if (wdata->nr_pages > 1)
4369 req->RemainingBytes =
4370 cpu_to_le32(
4371 (wdata->nr_pages - 1) * wdata->pagesz -
4372 wdata->page_offset + wdata->tailsz
4373 );
4374 else
4375 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07004376 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4377 if (need_invalidate)
4378 req->Channel = SMB2_CHANNEL_RDMA_V1;
4379 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06004380 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07004381 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06004382 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07004383 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06004384 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4385 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4386 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07004387 }
4388#endif
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004389 iov[0].iov_len = total_len - 1;
4390 iov[0].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07004391
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08004392 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004393 rqst.rq_nvec = 1;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004394 rqst.rq_pages = wdata->pages;
Long Li57a929a2018-05-30 12:47:53 -07004395 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07004396 rqst.rq_npages = wdata->nr_pages;
4397 rqst.rq_pagesz = wdata->pagesz;
4398 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07004399#ifdef CONFIG_CIFS_SMB_DIRECT
4400 if (wdata->mr) {
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004401 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
Long Lidb223a52017-11-22 17:38:45 -07004402 rqst.rq_npages = 0;
4403 }
4404#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05004405 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4406 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004407
Long Lidb223a52017-11-22 17:38:45 -07004408#ifdef CONFIG_CIFS_SMB_DIRECT
4409 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4410 if (!wdata->mr)
4411 req->Length = cpu_to_le32(wdata->bytes);
4412#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07004413 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07004414#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07004415
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004416 if (wdata->credits.value > 0) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004417 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004418 SMB2_MAX_BUFFER_SIZE));
Aurelien Aptel88fd98a2021-03-04 17:51:48 +00004419 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004420
4421 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4422 if (rc)
Pavel Shilovsky335b7b62019-01-16 11:12:41 -08004423 goto async_writev_out;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004424
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004425 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004426 }
4427
Pavel Shilovsky33319142012-09-18 16:20:29 -07004428 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08004429 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08004430 wdata, flags, &wdata->credits);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004431
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004432 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004433 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4434 tcon->tid, tcon->ses->Suid, wdata->offset,
4435 wdata->bytes, rc);
Steve French4a5c80d2014-02-07 20:45:12 -06004436 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004437 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
Pavel Shilovsky7d42e722019-01-25 11:38:53 -08004438 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07004439
Pavel Shilovsky33319142012-09-18 16:20:29 -07004440async_writev_out:
4441 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07004442 return rc;
4443}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004444
4445/*
4446 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4447 * The length field from io_parms must be at least 1 and indicates a number of
4448 * elements with data to write that begins with position 1 in iov array. All
4449 * data length is specified by count.
4450 */
4451int
4452SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4453 unsigned int *nbytes, struct kvec *iov, int n_vec)
4454{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004455 struct smb_rqst rqst;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004456 int rc = 0;
4457 struct smb2_write_req *req = NULL;
4458 struct smb2_write_rsp *rsp = NULL;
4459 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004460 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004461 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004462 unsigned int total_len;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004463 struct TCP_Server_Info *server;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004464
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004465 *nbytes = 0;
4466
4467 if (n_vec < 1)
4468 return rc;
4469
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004470 if (!io_parms->server)
4471 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4472 server = io_parms->server;
4473 if (server == NULL)
4474 return -ECONNABORTED;
4475
4476 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4477 (void **) &req, &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004478 if (rc)
4479 return rc;
4480
Steve French5a77e752018-05-09 17:43:08 -05004481 if (smb3_encryption_required(io_parms->tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004482 flags |= CIFS_TRANSFORM_REQ;
4483
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004484 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004485
4486 req->PersistentFileId = io_parms->persistent_fid;
4487 req->VolatileFileId = io_parms->volatile_fid;
4488 req->WriteChannelInfoOffset = 0;
4489 req->WriteChannelInfoLength = 0;
4490 req->Channel = 0;
4491 req->Length = cpu_to_le32(io_parms->length);
4492 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004493 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004494 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004495 req->RemainingBytes = 0;
4496
Steve Frenchd323c2462019-02-25 00:52:43 -06004497 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4498 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4499 io_parms->offset, io_parms->length);
4500
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004501 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004502 /* 1 for Buffer */
4503 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004504
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004505 memset(&rqst, 0, sizeof(struct smb_rqst));
4506 rqst.rq_iov = iov;
4507 rqst.rq_nvec = n_vec + 1;
4508
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004509 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4510 &rqst,
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11004511 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004512 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004513
4514 if (rc) {
Steve Frencheccb4422018-05-17 21:16:55 -05004515 trace_smb3_write_err(xid, req->PersistentFileId,
4516 io_parms->tcon->tid,
4517 io_parms->tcon->ses->Suid,
4518 io_parms->offset, io_parms->length, rc);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004519 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05004520 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Steve Frencheccb4422018-05-17 21:16:55 -05004521 } else {
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004522 *nbytes = le32_to_cpu(rsp->DataLength);
Steve Frencheccb4422018-05-17 21:16:55 -05004523 trace_smb3_write_done(xid, req->PersistentFileId,
4524 io_parms->tcon->tid,
4525 io_parms->tcon->ses->Suid,
4526 io_parms->offset, *nbytes);
4527 }
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004528
ZhangXiaoxu6a3eb332019-04-06 15:47:38 +08004529 cifs_small_buf_release(req);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004530 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004531 return rc;
4532}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004533
Aurelien Aptel69dda302020-03-02 17:53:22 +01004534int posix_info_sid_size(const void *beg, const void *end)
Aurelien Aptel349e13a2020-02-08 15:50:57 +01004535{
4536 size_t subauth;
4537 int total;
4538
4539 if (beg + 1 > end)
4540 return -1;
4541
4542 subauth = *(u8 *)(beg+1);
4543 if (subauth < 1 || subauth > 15)
4544 return -1;
4545
4546 total = 1 + 1 + 6 + 4*subauth;
4547 if (beg + total > end)
4548 return -1;
4549
4550 return total;
4551}
4552
4553int posix_info_parse(const void *beg, const void *end,
4554 struct smb2_posix_info_parsed *out)
4555
4556{
4557 int total_len = 0;
Ronnie Sahlbergca38fab2021-06-18 10:58:30 +10004558 int owner_len, group_len;
Aurelien Aptel349e13a2020-02-08 15:50:57 +01004559 int name_len;
4560 const void *owner_sid;
4561 const void *group_sid;
4562 const void *name;
4563
4564 /* if no end bound given, assume payload to be correct */
4565 if (!end) {
4566 const struct smb2_posix_info *p = beg;
4567
4568 end = beg + le32_to_cpu(p->NextEntryOffset);
4569 /* last element will have a 0 offset, pick a sensible bound */
4570 if (end == beg)
4571 end += 0xFFFF;
4572 }
4573
4574 /* check base buf */
4575 if (beg + sizeof(struct smb2_posix_info) > end)
4576 return -1;
4577 total_len = sizeof(struct smb2_posix_info);
4578
4579 /* check owner sid */
4580 owner_sid = beg + total_len;
Ronnie Sahlbergca38fab2021-06-18 10:58:30 +10004581 owner_len = posix_info_sid_size(owner_sid, end);
4582 if (owner_len < 0)
Aurelien Aptel349e13a2020-02-08 15:50:57 +01004583 return -1;
Ronnie Sahlbergca38fab2021-06-18 10:58:30 +10004584 total_len += owner_len;
Aurelien Aptel349e13a2020-02-08 15:50:57 +01004585
4586 /* check group sid */
4587 group_sid = beg + total_len;
Ronnie Sahlbergca38fab2021-06-18 10:58:30 +10004588 group_len = posix_info_sid_size(group_sid, end);
4589 if (group_len < 0)
Aurelien Aptel349e13a2020-02-08 15:50:57 +01004590 return -1;
Ronnie Sahlbergca38fab2021-06-18 10:58:30 +10004591 total_len += group_len;
Aurelien Aptel349e13a2020-02-08 15:50:57 +01004592
4593 /* check name len */
4594 if (beg + total_len + 4 > end)
4595 return -1;
4596 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4597 if (name_len < 1 || name_len > 0xFFFF)
4598 return -1;
4599 total_len += 4;
4600
4601 /* check name */
4602 name = beg + total_len;
4603 if (name + name_len > end)
4604 return -1;
4605 total_len += name_len;
4606
4607 if (out) {
4608 out->base = beg;
4609 out->size = total_len;
4610 out->name_len = name_len;
4611 out->name = name;
Ronnie Sahlbergca38fab2021-06-18 10:58:30 +10004612 memcpy(&out->owner, owner_sid, owner_len);
4613 memcpy(&out->group, group_sid, group_len);
Aurelien Aptel349e13a2020-02-08 15:50:57 +01004614 }
4615 return total_len;
4616}
4617
4618static int posix_info_extra_size(const void *beg, const void *end)
4619{
4620 int len = posix_info_parse(beg, end, NULL);
4621
4622 if (len < 0)
4623 return -1;
4624 return len - sizeof(struct smb2_posix_info);
4625}
4626
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004627static unsigned int
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004628num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4629 size_t size)
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004630{
4631 int len;
4632 unsigned int entrycount = 0;
4633 unsigned int next_offset = 0;
Dan Carpenter56446f22018-09-06 12:48:22 +03004634 char *entryptr;
4635 FILE_DIRECTORY_INFO *dir_info;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004636
4637 if (bufstart == NULL)
4638 return 0;
4639
Dan Carpenter56446f22018-09-06 12:48:22 +03004640 entryptr = bufstart;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004641
4642 while (1) {
Dan Carpenter56446f22018-09-06 12:48:22 +03004643 if (entryptr + next_offset < entryptr ||
4644 entryptr + next_offset > end_of_buf ||
4645 entryptr + next_offset + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004646 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004647 break;
4648 }
4649
Dan Carpenter56446f22018-09-06 12:48:22 +03004650 entryptr = entryptr + next_offset;
4651 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4652
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004653 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4654 len = posix_info_extra_size(entryptr, end_of_buf);
4655 else
4656 len = le32_to_cpu(dir_info->FileNameLength);
4657
4658 if (len < 0 ||
4659 entryptr + len < entryptr ||
Dan Carpenter56446f22018-09-06 12:48:22 +03004660 entryptr + len > end_of_buf ||
4661 entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004662 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4663 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004664 break;
4665 }
4666
Dan Carpenter56446f22018-09-06 12:48:22 +03004667 *lastentry = entryptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004668 entrycount++;
4669
Dan Carpenter56446f22018-09-06 12:48:22 +03004670 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004671 if (!next_offset)
4672 break;
4673 }
4674
4675 return entrycount;
4676}
4677
4678/*
4679 * Readdir/FindFirst
4680 */
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004681int SMB2_query_directory_init(const unsigned int xid,
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004682 struct cifs_tcon *tcon,
4683 struct TCP_Server_Info *server,
4684 struct smb_rqst *rqst,
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004685 u64 persistent_fid, u64 volatile_fid,
4686 int index, int info_level)
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004687{
4688 struct smb2_query_directory_req *req;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004689 unsigned char *bufptr;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004690 __le16 asteriks = cpu_to_le16('*');
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004691 unsigned int output_size = CIFSMaxBufSize -
4692 MAX_SMB2_CREATE_RESPONSE_SIZE -
4693 MAX_SMB2_CLOSE_RESPONSE_SIZE;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004694 unsigned int total_len;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004695 struct kvec *iov = rqst->rq_iov;
4696 int len, rc;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004697
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004698 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4699 (void **) &req, &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004700 if (rc)
4701 return rc;
4702
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004703 switch (info_level) {
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004704 case SMB_FIND_FILE_DIRECTORY_INFO:
4705 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004706 break;
4707 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4708 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004709 break;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004710 case SMB_FIND_FILE_POSIX_INFO:
4711 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4712 break;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004713 default:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004714 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004715 info_level);
4716 return -EINVAL;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004717 }
4718
4719 req->FileIndex = cpu_to_le32(index);
4720 req->PersistentFileId = persistent_fid;
4721 req->VolatileFileId = volatile_fid;
4722
4723 len = 0x2;
4724 bufptr = req->Buffer;
4725 memcpy(bufptr, &asteriks, len);
4726
4727 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004728 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004729 req->FileNameLength = cpu_to_le16(len);
4730 /*
4731 * BB could be 30 bytes or so longer if we used SMB2 specific
4732 * buffer lengths, but this is safe and close enough.
4733 */
4734 output_size = min_t(unsigned int, output_size, server->maxBuf);
4735 output_size = min_t(unsigned int, output_size, 2 << 15);
4736 req->OutputBufferLength = cpu_to_le32(output_size);
4737
4738 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11004739 /* 1 for Buffer */
4740 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004741
4742 iov[1].iov_base = (char *)(req->Buffer);
4743 iov[1].iov_len = len;
4744
Steve Frenchd323c2462019-02-25 00:52:43 -06004745 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4746 tcon->ses->Suid, index, output_size);
4747
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004748 return 0;
4749}
4750
4751void SMB2_query_directory_free(struct smb_rqst *rqst)
4752{
4753 if (rqst && rqst->rq_iov) {
4754 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4755 }
4756}
4757
4758int
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004759smb2_parse_query_directory(struct cifs_tcon *tcon,
4760 struct kvec *rsp_iov,
4761 int resp_buftype,
4762 struct cifs_search_info *srch_inf)
4763{
4764 struct smb2_query_directory_rsp *rsp;
4765 size_t info_buf_size;
4766 char *end_of_smb;
4767 int rc;
4768
4769 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4770
4771 switch (srch_inf->info_level) {
4772 case SMB_FIND_FILE_DIRECTORY_INFO:
4773 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4774 break;
4775 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4776 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4777 break;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004778 case SMB_FIND_FILE_POSIX_INFO:
4779 /* note that posix payload are variable size */
4780 info_buf_size = sizeof(struct smb2_posix_info);
4781 break;
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004782 default:
4783 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4784 srch_inf->info_level);
4785 return -EINVAL;
4786 }
4787
4788 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4789 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4790 info_buf_size);
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004791 if (rc) {
4792 cifs_tcon_dbg(VFS, "bad info payload");
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004793 return rc;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004794 }
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004795
4796 srch_inf->unicode = true;
4797
4798 if (srch_inf->ntwrk_buf_start) {
4799 if (srch_inf->smallBuf)
4800 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4801 else
4802 cifs_buf_release(srch_inf->ntwrk_buf_start);
4803 }
4804 srch_inf->ntwrk_buf_start = (char *)rsp;
4805 srch_inf->srch_entries_start = srch_inf->last_entry =
4806 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4807 end_of_smb = rsp_iov->iov_len + (char *)rsp;
Aurelien Aptel3d519bd2020-02-08 15:50:58 +01004808
4809 srch_inf->entries_in_buffer = num_entries(
4810 srch_inf->info_level,
4811 srch_inf->srch_entries_start,
4812 end_of_smb,
4813 &srch_inf->last_entry,
4814 info_buf_size);
4815
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004816 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4817 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4818 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4819 srch_inf->srch_entries_start, srch_inf->last_entry);
4820 if (resp_buftype == CIFS_LARGE_BUFFER)
4821 srch_inf->smallBuf = false;
4822 else if (resp_buftype == CIFS_SMALL_BUFFER)
4823 srch_inf->smallBuf = true;
4824 else
Joe Perchesa0a30362020-04-14 22:42:53 -07004825 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004826
4827 return 0;
4828}
4829
4830int
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004831SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4832 u64 persistent_fid, u64 volatile_fid, int index,
4833 struct cifs_search_info *srch_inf)
4834{
4835 struct smb_rqst rqst;
4836 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4837 struct smb2_query_directory_rsp *rsp = NULL;
4838 int resp_buftype = CIFS_NO_BUFFER;
4839 struct kvec rsp_iov;
4840 int rc = 0;
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004841 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004842 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004843 int flags = 0;
4844
YueHaibingc4985c32020-01-17 10:57:17 +08004845 if (!ses || !(ses->server))
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004846 return -EIO;
4847
4848 if (smb3_encryption_required(tcon))
4849 flags |= CIFS_TRANSFORM_REQ;
4850
4851 memset(&rqst, 0, sizeof(struct smb_rqst));
4852 memset(&iov, 0, sizeof(iov));
4853 rqst.rq_iov = iov;
4854 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4855
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004856 rc = SMB2_query_directory_init(xid, tcon, server,
4857 &rqst, persistent_fid,
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004858 volatile_fid, index,
4859 srch_inf->info_level);
4860 if (rc)
4861 goto qdir_exit;
4862
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004863 rc = cifs_send_recv(xid, ses, server,
4864 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004865 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04004866
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004867 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07004868 if (rc == -ENODATA &&
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004869 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004870 trace_smb3_query_dir_done(xid, persistent_fid,
4871 tcon->tid, tcon->ses->Suid, index, 0);
Pavel Shilovsky52755802014-08-18 20:49:57 +04004872 srch_inf->endOfSearch = true;
4873 rc = 0;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004874 } else {
4875 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4876 tcon->ses->Suid, index, 0, rc);
Pavel Shilovsky8e6e72a2019-01-26 12:21:32 -08004877 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004878 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004879 goto qdir_exit;
4880 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004881
Ronnie Sahlbergaf08f9e2020-01-08 13:08:05 +10004882 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4883 srch_inf);
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004884 if (rc) {
4885 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4886 tcon->ses->Suid, index, 0, rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004887 goto qdir_exit;
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004888 }
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004889 resp_buftype = CIFS_NO_BUFFER;
4890
Steve Frenchadb3b4e2019-02-25 13:51:11 -06004891 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4892 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004893
4894qdir_exit:
Ronnie Sahlberg0a177992020-01-08 13:08:04 +10004895 SMB2_query_directory_free(&rqst);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004896 free_rsp_buf(resp_buftype, rsp);
4897 return rc;
4898}
4899
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004900int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004901SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4902 struct smb_rqst *rqst,
4903 u64 persistent_fid, u64 volatile_fid, u32 pid,
4904 u8 info_class, u8 info_type, u32 additional_info,
4905 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004906{
4907 struct smb2_set_info_req *req;
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004908 struct kvec *iov = rqst->rq_iov;
4909 unsigned int i, total_len;
4910 int rc;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004911
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004912 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
4913 (void **) &req, &total_len);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004914 if (rc)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004915 return rc;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07004916
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004917 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004918 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004919 req->FileInfoClass = info_class;
4920 req->PersistentFileId = persistent_fid;
4921 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05004922 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004923
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004924 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004925 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004926 req->BufferLength = cpu_to_le32(*size);
4927
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004928 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004929 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004930
4931 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004932 /* 1 for Buffer */
4933 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004934
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004935 for (i = 1; i < rqst->rq_nvec; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004936 le32_add_cpu(&req->BufferLength, size[i]);
4937 iov[i].iov_base = (char *)data[i];
4938 iov[i].iov_len = size[i];
4939 }
4940
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004941 return 0;
4942}
4943
4944void
4945SMB2_set_info_free(struct smb_rqst *rqst)
4946{
Ronnie Sahlberg32a1fb32018-10-24 11:50:33 +10004947 if (rqst && rqst->rq_iov)
4948 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004949}
4950
4951static int
4952send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4953 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4954 u8 info_type, u32 additional_info, unsigned int num,
4955 void **data, unsigned int *size)
4956{
4957 struct smb_rqst rqst;
4958 struct smb2_set_info_rsp *rsp = NULL;
4959 struct kvec *iov;
4960 struct kvec rsp_iov;
4961 int rc = 0;
4962 int resp_buftype;
4963 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004964 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004965 int flags = 0;
4966
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004967 if (!ses || !server)
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004968 return -EIO;
4969
4970 if (!num)
4971 return -EINVAL;
4972
4973 if (smb3_encryption_required(tcon))
4974 flags |= CIFS_TRANSFORM_REQ;
4975
4976 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4977 if (!iov)
4978 return -ENOMEM;
4979
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10004980 memset(&rqst, 0, sizeof(struct smb_rqst));
4981 rqst.rq_iov = iov;
4982 rqst.rq_nvec = num;
4983
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004984 rc = SMB2_set_info_init(tcon, server,
4985 &rqst, persistent_fid, volatile_fid, pid,
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004986 info_class, info_type, additional_info,
4987 data, size);
4988 if (rc) {
4989 kfree(iov);
4990 return rc;
4991 }
4992
4993
Aurelien Aptel352d96f2020-05-31 12:38:22 -05004994 rc = cifs_send_recv(xid, ses, server,
4995 &rqst, &resp_buftype, flags,
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11004996 &rsp_iov);
Ronnie Sahlbergba8ca112018-09-03 13:33:44 +10004997 SMB2_set_info_free(&rqst);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004998 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004999
Steve Frencheccb4422018-05-17 21:16:55 -05005000 if (rc != 0) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07005001 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05005002 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5003 ses->Suid, info_class, (__u32)info_type, rc);
5004 }
Steve French7d3fb242013-11-18 09:56:28 -06005005
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07005006 free_rsp_buf(resp_buftype, rsp);
5007 kfree(iov);
5008 return rc;
5009}
5010
5011int
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07005012SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10005013 u64 volatile_fid, u32 pid, __le64 *eof)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07005014{
5015 struct smb2_file_eof_info info;
5016 void *data;
5017 unsigned int size;
5018
5019 info.EndOfFile = *eof;
5020
5021 data = &info;
5022 size = sizeof(struct smb2_file_eof_info);
5023
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10005024 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05005025 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5026 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07005027}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07005028
5029int
Shirish Pargaonkardac95342017-06-28 22:37:00 -05005030SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5031 u64 persistent_fid, u64 volatile_fid,
5032 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5033{
5034 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5035 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5036 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07005037}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005038
5039int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10005040SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5041 u64 persistent_fid, u64 volatile_fid,
5042 struct smb2_file_full_ea_info *buf, int len)
5043{
5044 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5045 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5046 0, 1, (void **)&buf, &len);
5047}
5048
5049int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005050SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5051 const u64 persistent_fid, const u64 volatile_fid,
5052 __u8 oplock_level)
5053{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005054 struct smb_rqst rqst;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005055 int rc;
Ronnie Sahlberg0d5a2882018-06-01 10:53:03 +10005056 struct smb2_oplock_break *req = NULL;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11005057 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005058 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005059 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11005060 unsigned int total_len;
5061 struct kvec iov[1];
5062 struct kvec rsp_iov;
5063 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005064
Joe Perchesf96637b2013-05-04 22:12:25 -05005065 cifs_dbg(FYI, "SMB2_oplock_break\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005066 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5067 (void **) &req, &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005068 if (rc)
5069 return rc;
5070
Steve French5a77e752018-05-09 17:43:08 -05005071 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005072 flags |= CIFS_TRANSFORM_REQ;
5073
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005074 req->VolatileFid = volatile_fid;
5075 req->PersistentFid = persistent_fid;
5076 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11005077 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005078
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005079 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11005080
5081 iov[0].iov_base = (char *)req;
5082 iov[0].iov_len = total_len;
5083
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005084 memset(&rqst, 0, sizeof(struct smb_rqst));
5085 rqst.rq_iov = iov;
5086 rqst.rq_nvec = 1;
5087
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005088 rc = cifs_send_recv(xid, ses, server,
5089 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005090 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005091
5092 if (rc) {
5093 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05005094 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005095 }
5096
5097 return rc;
5098}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005099
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005100void
5101smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5102 struct kstatfs *kst)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005103{
5104 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5105 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5106 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05305107 kst->f_bfree = kst->f_bavail =
5108 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005109 return;
5110}
5111
Steve French2d304212018-06-24 23:28:12 -05005112static void
5113copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5114 struct kstatfs *kst)
5115{
5116 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5117 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5118 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5119 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5120 kst->f_bavail = kst->f_bfree;
5121 else
5122 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5123 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5124 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5125 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5126 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5127
5128 return;
5129}
Steve French2d304212018-06-24 23:28:12 -05005130
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005131static int
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005132build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5133 struct TCP_Server_Info *server,
5134 int level, int outbuf_len, u64 persistent_fid,
5135 u64 volatile_fid)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005136{
5137 int rc;
5138 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005139 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005140
Joe Perchesf96637b2013-05-04 22:12:25 -05005141 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005142
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005143 if ((tcon->ses == NULL) || server == NULL)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005144 return -EIO;
5145
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005146 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5147 (void **) &req, &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005148 if (rc)
5149 return rc;
5150
5151 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5152 req->FileInfoClass = level;
5153 req->PersistentFileId = persistent_fid;
5154 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005155 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005156 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005157 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005158 req->OutputBufferLength = cpu_to_le32(
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005159 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005160
5161 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11005162 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005163 return 0;
5164}
5165
Steve French2d304212018-06-24 23:28:12 -05005166int
5167SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5168 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5169{
5170 struct smb_rqst rqst;
5171 struct smb2_query_info_rsp *rsp = NULL;
5172 struct kvec iov;
5173 struct kvec rsp_iov;
5174 int rc = 0;
5175 int resp_buftype;
5176 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005177 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve French2d304212018-06-24 23:28:12 -05005178 FILE_SYSTEM_POSIX_INFO *info = NULL;
5179 int flags = 0;
5180
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005181 rc = build_qfs_info_req(&iov, tcon, server,
5182 FS_POSIX_INFORMATION,
Steve French2d304212018-06-24 23:28:12 -05005183 sizeof(FILE_SYSTEM_POSIX_INFO),
5184 persistent_fid, volatile_fid);
5185 if (rc)
5186 return rc;
5187
5188 if (smb3_encryption_required(tcon))
5189 flags |= CIFS_TRANSFORM_REQ;
5190
5191 memset(&rqst, 0, sizeof(struct smb_rqst));
5192 rqst.rq_iov = &iov;
5193 rqst.rq_nvec = 1;
5194
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005195 rc = cifs_send_recv(xid, ses, server,
5196 &rqst, &resp_buftype, flags, &rsp_iov);
Steve French2d304212018-06-24 23:28:12 -05005197 cifs_small_buf_release(iov.iov_base);
5198 if (rc) {
5199 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5200 goto posix_qfsinf_exit;
5201 }
5202 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5203
5204 info = (FILE_SYSTEM_POSIX_INFO *)(
5205 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005206 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5207 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5208 sizeof(FILE_SYSTEM_POSIX_INFO));
Steve French2d304212018-06-24 23:28:12 -05005209 if (!rc)
5210 copy_posix_fs_info_to_kstatfs(info, fsdata);
5211
5212posix_qfsinf_exit:
5213 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5214 return rc;
5215}
Steve French2d304212018-06-24 23:28:12 -05005216
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005217int
5218SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5219 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5220{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005221 struct smb_rqst rqst;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005222 struct smb2_query_info_rsp *rsp = NULL;
5223 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005224 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005225 int rc = 0;
5226 int resp_buftype;
5227 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005228 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005229 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005230 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005231
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005232 rc = build_qfs_info_req(&iov, tcon, server,
5233 FS_FULL_SIZE_INFORMATION,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005234 sizeof(struct smb2_fs_full_size_info),
5235 persistent_fid, volatile_fid);
5236 if (rc)
5237 return rc;
5238
Steve French5a77e752018-05-09 17:43:08 -05005239 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005240 flags |= CIFS_TRANSFORM_REQ;
5241
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005242 memset(&rqst, 0, sizeof(struct smb_rqst));
5243 rqst.rq_iov = &iov;
5244 rqst.rq_nvec = 1;
5245
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005246 rc = cifs_send_recv(xid, ses, server,
5247 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005248 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005249 if (rc) {
5250 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05005251 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005252 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005253 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005254
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005255 info = (struct smb2_fs_full_size_info *)(
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005256 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005257 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5258 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5259 sizeof(struct smb2_fs_full_size_info));
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005260 if (!rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005261 smb2_copy_fs_info_to_kstatfs(info, fsdata);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005262
Steve French34f62642013-10-09 02:07:00 -05005263qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005264 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05005265 return rc;
5266}
5267
5268int
5269SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05005270 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05005271{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005272 struct smb_rqst rqst;
Steve French34f62642013-10-09 02:07:00 -05005273 struct smb2_query_info_rsp *rsp = NULL;
5274 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005275 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05005276 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05005277 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05005278 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005279 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Steve French34f62642013-10-09 02:07:00 -05005280 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005281 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05005282
Steven French21671142013-10-09 13:36:35 -05005283 if (level == FS_DEVICE_INFORMATION) {
5284 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5285 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5286 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5287 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5288 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005289 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5290 max_len = sizeof(struct smb3_fs_ss_info);
5291 min_len = sizeof(struct smb3_fs_ss_info);
Steve French21ba3842018-06-24 23:18:52 -05005292 } else if (level == FS_VOLUME_INFORMATION) {
5293 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5294 min_len = sizeof(struct smb3_fs_vol_info);
Steven French21671142013-10-09 13:36:35 -05005295 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005296 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05005297 return -EINVAL;
5298 }
5299
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005300 rc = build_qfs_info_req(&iov, tcon, server,
5301 level, max_len,
Steve French34f62642013-10-09 02:07:00 -05005302 persistent_fid, volatile_fid);
5303 if (rc)
5304 return rc;
5305
Steve French5a77e752018-05-09 17:43:08 -05005306 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005307 flags |= CIFS_TRANSFORM_REQ;
5308
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005309 memset(&rqst, 0, sizeof(struct smb_rqst));
5310 rqst.rq_iov = &iov;
5311 rqst.rq_nvec = 1;
5312
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005313 rc = cifs_send_recv(xid, ses, server,
5314 &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005315 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05005316 if (rc) {
5317 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5318 goto qfsattr_exit;
5319 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005320 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05005321
5322 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5323 offset = le16_to_cpu(rsp->OutputBufferOffset);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10005324 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
Steven French21671142013-10-09 13:36:35 -05005325 if (rc)
5326 goto qfsattr_exit;
5327
5328 if (level == FS_ATTRIBUTE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005329 memcpy(&tcon->fsAttrInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005330 + (char *)rsp, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05005331 rsp_len, max_len));
5332 else if (level == FS_DEVICE_INFORMATION)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005333 memcpy(&tcon->fsDevInfo, offset
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10005334 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005335 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5336 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10005337 (offset + (char *)rsp);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005338 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5339 tcon->perf_sector_size =
5340 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
Steve French21ba3842018-06-24 23:18:52 -05005341 } else if (level == FS_VOLUME_INFORMATION) {
5342 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5343 (offset + (char *)rsp);
5344 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5345 tcon->vol_create_time = vol_info->VolumeCreationTime;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005346 }
Steve French34f62642013-10-09 02:07:00 -05005347
5348qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005349 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005350 return rc;
5351}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005352
5353int
5354smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5355 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5356 const __u32 num_lock, struct smb2_lock_element *buf)
5357{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005358 struct smb_rqst rqst;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005359 int rc = 0;
5360 struct smb2_lock_req *req = NULL;
5361 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005362 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005363 int resp_buf_type;
5364 unsigned int count;
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005365 int flags = CIFS_NO_RSP_BUF;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005366 unsigned int total_len;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005367 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005368
Joe Perchesf96637b2013-05-04 22:12:25 -05005369 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005370
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005371 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5372 (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005373 if (rc)
5374 return rc;
5375
Steve French5a77e752018-05-09 17:43:08 -05005376 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005377 flags |= CIFS_TRANSFORM_REQ;
5378
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005379 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005380 req->LockCount = cpu_to_le16(num_lock);
5381
5382 req->PersistentFileId = persist_fid;
5383 req->VolatileFileId = volatile_fid;
5384
5385 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005386
5387 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005388 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005389 iov[1].iov_base = (char *)buf;
5390 iov[1].iov_len = count;
5391
5392 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005393
5394 memset(&rqst, 0, sizeof(struct smb_rqst));
5395 rqst.rq_iov = iov;
5396 rqst.rq_nvec = 2;
5397
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005398 rc = cifs_send_recv(xid, tcon->ses, server,
5399 &rqst, &resp_buf_type, flags,
Ronnie Sahlbergced93672017-11-21 10:07:27 +11005400 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005401 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005402 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005403 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005404 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
Steve Frencheccb4422018-05-17 21:16:55 -05005405 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5406 tcon->ses->Suid, rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005407 }
5408
5409 return rc;
5410}
5411
5412int
5413SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5414 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5415 const __u64 length, const __u64 offset, const __u32 lock_flags,
5416 const bool wait)
5417{
5418 struct smb2_lock_element lock;
5419
5420 lock.Offset = cpu_to_le64(offset);
5421 lock.Length = cpu_to_le64(length);
5422 lock.Flags = cpu_to_le32(lock_flags);
5423 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5424 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5425
5426 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5427}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005428
5429int
5430SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5431 __u8 *lease_key, const __le32 lease_state)
5432{
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005433 struct smb_rqst rqst;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005434 int rc;
5435 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005436 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005437 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005438 unsigned int total_len;
5439 struct kvec iov[1];
5440 struct kvec rsp_iov;
5441 int resp_buf_type;
Steve French179e44d2018-09-28 19:44:23 -05005442 __u64 *please_key_high;
5443 __u64 *please_key_low;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005444 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005445
Joe Perchesf96637b2013-05-04 22:12:25 -05005446 cifs_dbg(FYI, "SMB2_lease_break\n");
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005447 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5448 (void **) &req, &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005449 if (rc)
5450 return rc;
5451
Steve French5a77e752018-05-09 17:43:08 -05005452 if (smb3_encryption_required(tcon))
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07005453 flags |= CIFS_TRANSFORM_REQ;
5454
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005455 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005456 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005457 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005458
5459 memcpy(req->LeaseKey, lease_key, 16);
5460 req->LeaseState = lease_state;
5461
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10005462 flags |= CIFS_NO_RSP_BUF;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11005463
5464 iov[0].iov_base = (char *)req;
5465 iov[0].iov_len = total_len;
5466
Ronnie Sahlberg40eff452018-06-12 08:00:59 +10005467 memset(&rqst, 0, sizeof(struct smb_rqst));
5468 rqst.rq_iov = iov;
5469 rqst.rq_nvec = 1;
5470
Aurelien Aptel352d96f2020-05-31 12:38:22 -05005471 rc = cifs_send_recv(xid, ses, server,
5472 &rqst, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005473 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005474
Aurelien Apteld339adc2019-01-31 13:46:07 +01005475 please_key_low = (__u64 *)lease_key;
5476 please_key_high = (__u64 *)(lease_key+8);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005477 if (rc) {
5478 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Steve French179e44d2018-09-28 19:44:23 -05005479 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5480 ses->Suid, *please_key_low, *please_key_high, rc);
Joe Perchesf96637b2013-05-04 22:12:25 -05005481 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Steve French179e44d2018-09-28 19:44:23 -05005482 } else
5483 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5484 ses->Suid, *please_key_low, *please_key_high);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07005485
5486 return rc;
5487}