blob: c0dc0491af93c18210cd65c1eb090bf51c83008c [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040051
52/*
53 * The following table defines the expected "StructureSize" of SMB2 requests
54 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
55 *
56 * Note that commands are defined in smb2pdu.h in le16 but the array below is
57 * indexed by command in host byte order.
58 */
59static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
60 /* SMB2_NEGOTIATE */ 36,
61 /* SMB2_SESSION_SETUP */ 25,
62 /* SMB2_LOGOFF */ 4,
63 /* SMB2_TREE_CONNECT */ 9,
64 /* SMB2_TREE_DISCONNECT */ 4,
65 /* SMB2_CREATE */ 57,
66 /* SMB2_CLOSE */ 24,
67 /* SMB2_FLUSH */ 24,
68 /* SMB2_READ */ 49,
69 /* SMB2_WRITE */ 49,
70 /* SMB2_LOCK */ 48,
71 /* SMB2_IOCTL */ 57,
72 /* SMB2_CANCEL */ 4,
73 /* SMB2_ECHO */ 4,
74 /* SMB2_QUERY_DIRECTORY */ 33,
75 /* SMB2_CHANGE_NOTIFY */ 32,
76 /* SMB2_QUERY_INFO */ 41,
77 /* SMB2_SET_INFO */ 33,
78 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
79};
80
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070081static int encryption_required(const struct cifs_tcon *tcon)
82{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080083 if (!tcon)
84 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070085 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
86 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
87 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080088 if (tcon->seal &&
89 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
90 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070091 return 0;
92}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040093
94static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070095smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040096 const struct cifs_tcon *tcon)
97{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070098 shdr->ProtocolId = SMB2_PROTO_NUMBER;
99 shdr->StructureSize = cpu_to_le16(64);
100 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100101 if (tcon && tcon->ses && tcon->ses->server) {
102 struct TCP_Server_Info *server = tcon->ses->server;
103
104 spin_lock(&server->req_lock);
105 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500106 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700107 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100108 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700109 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500110 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100111 server->credits, 2));
112 spin_unlock(&server->req_lock);
113 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700114 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100115 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700116 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400117
118 if (!tcon)
119 goto out;
120
Steve French2b80d042013-06-23 18:43:37 -0500121 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
122 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500123 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500124 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700125 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500126 /* else CreditCharge MBZ */
127
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700128 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400129 /* Uid is not converted */
130 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700131 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500132
133 /*
134 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
135 * to pass the path on the Open SMB prefixed by \\server\share.
136 * Not sure when we would need to do the augmented path (if ever) and
137 * setting this flag breaks the SMB2 open operation since it is
138 * illegal to send an empty path name (without \\server\share prefix)
139 * when the DFS flag is set in the SMB open header. We could
140 * consider setting the flag on all operations other than open
141 * but it is safer to net set it for now.
142 */
143/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700144 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500145
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700146 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
147 !encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700148 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400149out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400150 return;
151}
152
153static int
154smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
155{
156 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400157 struct nls_table *nls_codepage;
158 struct cifs_ses *ses;
159 struct TCP_Server_Info *server;
160
161 /*
162 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
163 * check for tcp and smb session status done differently
164 * for those three - in the calling routine.
165 */
166 if (tcon == NULL)
167 return rc;
168
169 if (smb2_command == SMB2_TREE_CONNECT)
170 return rc;
171
172 if (tcon->tidStatus == CifsExiting) {
173 /*
174 * only tree disconnect, open, and write,
175 * (and ulogoff which does not have tcon)
176 * are allowed as we start force umount.
177 */
178 if ((smb2_command != SMB2_WRITE) &&
179 (smb2_command != SMB2_CREATE) &&
180 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500181 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
182 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400183 return -ENODEV;
184 }
185 }
186 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
187 (!tcon->ses->server))
188 return -EIO;
189
190 ses = tcon->ses;
191 server = ses->server;
192
193 /*
194 * Give demultiplex thread up to 10 seconds to reconnect, should be
195 * greater than cifs socket timeout which is 7 seconds
196 */
197 while (server->tcpStatus == CifsNeedReconnect) {
198 /*
199 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
200 * here since they are implicitly done when session drops.
201 */
202 switch (smb2_command) {
203 /*
204 * BB Should we keep oplock break and add flush to exceptions?
205 */
206 case SMB2_TREE_DISCONNECT:
207 case SMB2_CANCEL:
208 case SMB2_CLOSE:
209 case SMB2_OPLOCK_BREAK:
210 return -EAGAIN;
211 }
212
213 wait_event_interruptible_timeout(server->response_q,
214 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
215
216 /* are we still trying to reconnect? */
217 if (server->tcpStatus != CifsNeedReconnect)
218 break;
219
220 /*
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 }
229 }
230
231 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
232 return rc;
233
234 nls_codepage = load_nls_default();
235
236 /*
237 * need to prevent multiple threads trying to simultaneously reconnect
238 * the same SMB session
239 */
240 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200241
242 /*
243 * Recheck after acquire mutex. If another thread is negotiating
244 * and the server never sends an answer the socket will be closed
245 * and tcpStatus set to reconnect.
246 */
247 if (server->tcpStatus == CifsNeedReconnect) {
248 rc = -EHOSTDOWN;
249 mutex_unlock(&tcon->ses->session_mutex);
250 goto out;
251 }
252
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400253 rc = cifs_negotiate_protocol(0, tcon->ses);
254 if (!rc && tcon->ses->need_reconnect)
255 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
256
257 if (rc || !tcon->need_reconnect) {
258 mutex_unlock(&tcon->ses->session_mutex);
259 goto out;
260 }
261
262 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800263 if (tcon->use_persistent)
264 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500265
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400266 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
267 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500268
Joe Perchesf96637b2013-05-04 22:12:25 -0500269 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400270 if (rc)
271 goto out;
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800272
273 if (smb2_command != SMB2_INTERNAL_CMD)
274 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
275
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400276 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400277out:
278 /*
279 * Check if handle based operation so we know whether we can continue
280 * or not without returning to caller to reset file handle.
281 */
282 /*
283 * BB Is flush done by server on drop of tcp session? Should we special
284 * case it and skip above?
285 */
286 switch (smb2_command) {
287 case SMB2_FLUSH:
288 case SMB2_READ:
289 case SMB2_WRITE:
290 case SMB2_LOCK:
291 case SMB2_IOCTL:
292 case SMB2_QUERY_DIRECTORY:
293 case SMB2_CHANGE_NOTIFY:
294 case SMB2_QUERY_INFO:
295 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800296 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400297 }
298 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400299 return rc;
300}
301
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700302static void
303fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
304 unsigned int *total_len)
305{
306 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
307 /* lookup word count ie StructureSize from table */
308 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
309
310 /*
311 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
312 * largest operations (Create)
313 */
314 memset(buf, 0, 256);
315
316 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
317 spdu->StructureSize2 = cpu_to_le16(parmsize);
318
319 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
320}
321
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100322/*
323 * Allocate and return pointer to an SMB request hdr, and set basic
324 * SMB information in the SMB header. If the return code is zero, this
325 * function must have filled in request_buf pointer.
326 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800327static int
328smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
329 void **request_buf, unsigned int *total_len)
330{
331 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800332
333 rc = smb2_reconnect(smb2_command, tcon);
334 if (rc)
335 return rc;
336
337 /* BB eventually switch this to SMB2 specific small buf size */
338 *request_buf = cifs_small_buf_get();
339 if (*request_buf == NULL) {
340 /* BB should we add a retry in here if not a writepage? */
341 return -ENOMEM;
342 }
343
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100344 fill_small_buf(smb2_command, tcon,
345 (struct smb2_sync_hdr *)(*request_buf),
346 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400347
348 if (tcon != NULL) {
349#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400350 uint16_t com_code = le16_to_cpu(smb2_command);
351 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400352#endif
353 cifs_stats_inc(&tcon->num_smbs_sent);
354 }
355
356 return rc;
357}
358
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500359#ifdef CONFIG_CIFS_SMB311
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100360/* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
361#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500362
363
364#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
365#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
366
367static void
368build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
369{
370 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
371 pneg_ctxt->DataLength = cpu_to_le16(38);
372 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
373 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
374 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
375 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
376}
377
378static void
379build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
380{
381 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
382 pneg_ctxt->DataLength = cpu_to_le16(6);
383 pneg_ctxt->CipherCount = cpu_to_le16(2);
384 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
385 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
386}
387
388static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100389assemble_neg_contexts(struct smb2_negotiate_req *req,
390 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500391{
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100392 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500393
394 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
395 /* Add 2 to size to round to 8 byte boundary */
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100396
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500397 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
398 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
399 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
400 req->NegotiateContextCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100401
402 *total_len += 4 + sizeof(struct smb2_preauth_neg_context)
403 + sizeof(struct smb2_encryption_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500404}
405#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100406static void assemble_neg_contexts(struct smb2_negotiate_req *req,
407 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500408{
409 return;
410}
411#endif /* SMB311 */
412
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400413/*
414 *
415 * SMB2 Worker functions follow:
416 *
417 * The general structure of the worker functions is:
418 * 1) Call smb2_init (assembles SMB2 header)
419 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
420 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
421 * 4) Decode SMB2 command specific fields in the fixed length area
422 * 5) Decode variable length data area (if any for this SMB2 command type)
423 * 6) Call free smb buffer
424 * 7) return
425 *
426 */
427
428int
429SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
430{
431 struct smb2_negotiate_req *req;
432 struct smb2_negotiate_rsp *rsp;
433 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700434 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400435 int rc = 0;
436 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400437 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400438 int blob_offset, blob_length;
439 char *security_blob;
440 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100441 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400442
Joe Perchesf96637b2013-05-04 22:12:25 -0500443 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400444
Jeff Layton3534b852013-05-24 07:41:01 -0400445 if (!server) {
446 WARN(1, "%s: server is NULL!\n", __func__);
447 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400448 }
449
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100450 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400451 if (rc)
452 return rc;
453
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100454 req->sync_hdr.SessionId = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400455
Steve French9764c022017-09-17 10:41:35 -0500456 if (strcmp(ses->server->vals->version_string,
457 SMB3ANY_VERSION_STRING) == 0) {
458 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
459 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
460 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100461 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500462 } else if (strcmp(ses->server->vals->version_string,
463 SMBDEFAULT_VERSION_STRING) == 0) {
464 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
465 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
466 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
467 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100468 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500469 } else {
470 /* otherwise send specific dialect */
471 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
472 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100473 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500474 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400475
476 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400477 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500478 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400479 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500480 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400481 else
482 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400483
Steve Frenche4aa25e2012-10-01 12:26:22 -0500484 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400485
Steve French3c5f9be12014-05-13 13:37:45 -0700486 /* ClientGUID must be zero for SMB2.02 dialect */
487 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
488 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500489 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700490 memcpy(req->ClientGUID, server->client_guid,
491 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500492 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100493 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500494 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400495 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100496 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400497
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100498 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700499 cifs_small_buf_release(req);
500 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400501 /*
502 * No tcon so can't do
503 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
504 */
Steve French7e682f72017-08-31 21:34:24 -0500505 if (rc == -EOPNOTSUPP) {
506 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500507 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500508 " older servers\n");
509 goto neg_exit;
510 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400511 goto neg_exit;
512
Steve French9764c022017-09-17 10:41:35 -0500513 if (strcmp(ses->server->vals->version_string,
514 SMB3ANY_VERSION_STRING) == 0) {
515 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
516 cifs_dbg(VFS,
517 "SMB2 dialect returned but not requested\n");
518 return -EIO;
519 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
520 cifs_dbg(VFS,
521 "SMB2.1 dialect returned but not requested\n");
522 return -EIO;
523 }
524 } else if (strcmp(ses->server->vals->version_string,
525 SMBDEFAULT_VERSION_STRING) == 0) {
526 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
527 cifs_dbg(VFS,
528 "SMB2 dialect returned but not requested\n");
529 return -EIO;
530 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
531 /* ops set to 3.0 by default for default so update */
532 ses->server->ops = &smb21_operations;
533 }
Steve French590d08d2017-09-19 11:43:47 -0500534 } else if (le16_to_cpu(rsp->DialectRevision) !=
535 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500536 /* if requested single dialect ensure returned dialect matched */
537 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500538 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500539 return -EIO;
540 }
541
Joe Perchesf96637b2013-05-04 22:12:25 -0500542 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400543
Steve Frenche4aa25e2012-10-01 12:26:22 -0500544 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500545 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500546 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500547 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500548 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500549 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500550 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
551 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600552#ifdef CONFIG_CIFS_SMB311
553 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
554 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
555#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400556 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500557 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500558 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400559 rc = -EIO;
560 goto neg_exit;
561 }
562 server->dialect = le16_to_cpu(rsp->DialectRevision);
563
Steve French9764c022017-09-17 10:41:35 -0500564 /* BB: add check that dialect was valid given dialect(s) we asked for */
565
Jeff Laytone598d1d82013-05-26 07:00:59 -0400566 /* SMB2 only has an extended negflavor */
567 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400568 /* set it to the maximum buffer size value we can send with 1 credit */
569 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
570 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400571 server->max_read = le32_to_cpu(rsp->MaxReadSize);
572 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
573 /* BB Do we need to validate the SecurityMode? */
574 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
575 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400576 /* Internal types */
577 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400578
579 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
580 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500581 /*
582 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
583 * for us will be
584 * ses->sectype = RawNTLMSSP;
585 * but for time being this is our only auth choice so doesn't matter.
586 * We just found a server which sets blob length to zero expecting raw.
587 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700588 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500589 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700590 server->sec_ntlmssp = true;
591 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700592
Jeff Layton38d77c52013-05-26 07:01:00 -0400593 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400594 if (rc)
595 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500596 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500597 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500598 if (rc == 1)
599 rc = 0;
600 else if (rc == 0)
601 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400602 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400603neg_exit:
604 free_rsp_buf(resp_buftype, rsp);
605 return rc;
606}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400607
Steve Frenchff1c0382013-11-19 23:44:46 -0600608int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
609{
610 int rc = 0;
611 struct validate_negotiate_info_req vneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200612 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600613 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500614 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600615
616 cifs_dbg(FYI, "validate negotiate\n");
617
618 /*
619 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500620 * can not sign it (ie are not known user). Even if signing is not
621 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600622 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500623 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600624 */
Steve French0603c962017-09-20 19:57:18 -0500625 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600626 return 0; /* validation requires signing */
627
Steve French0603c962017-09-20 19:57:18 -0500628 if (tcon->ses->user_name == NULL) {
629 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
630 return 0; /* validation requires signing */
631 }
632
633 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
634 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
635
Steve Frenchff1c0382013-11-19 23:44:46 -0600636 vneg_inbuf.Capabilities =
637 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100638 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
639 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600640
641 if (tcon->ses->sign)
642 vneg_inbuf.SecurityMode =
643 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
644 else if (global_secflags & CIFSSEC_MAY_SIGN)
645 vneg_inbuf.SecurityMode =
646 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
647 else
648 vneg_inbuf.SecurityMode = 0;
649
Steve French9764c022017-09-17 10:41:35 -0500650
651 if (strcmp(tcon->ses->server->vals->version_string,
652 SMB3ANY_VERSION_STRING) == 0) {
653 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
654 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
655 vneg_inbuf.DialectCount = cpu_to_le16(2);
656 /* structure is big enough for 3 dialects, sending only 2 */
657 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
658 } else if (strcmp(tcon->ses->server->vals->version_string,
659 SMBDEFAULT_VERSION_STRING) == 0) {
660 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
661 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
662 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
663 vneg_inbuf.DialectCount = cpu_to_le16(3);
664 /* structure is big enough for 3 dialects */
665 inbuflen = sizeof(struct validate_negotiate_info_req);
666 } else {
667 /* otherwise specific dialect was requested */
668 vneg_inbuf.Dialects[0] =
669 cpu_to_le16(tcon->ses->server->vals->protocol_id);
670 vneg_inbuf.DialectCount = cpu_to_le16(1);
671 /* structure is big enough for 3 dialects, sending only 1 */
672 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
673 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600674
675 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
676 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100677 false /* use_ipc */,
Steve Frenchff1c0382013-11-19 23:44:46 -0600678 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
679 (char **)&pneg_rsp, &rsplen);
680
681 if (rc != 0) {
682 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
683 return -EIO;
684 }
685
686 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500687 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
688 rsplen);
689
690 /* relax check since Mac returns max bufsize allowed on ioctl */
David Disseldorpa2d9daa2017-10-20 14:49:38 +0200691 if ((rsplen > CIFSMaxBufSize)
692 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200693 goto err_rsp_free;
Steve Frenchff1c0382013-11-19 23:44:46 -0600694 }
695
696 /* check validate negotiate info response matches what we got earlier */
697 if (pneg_rsp->Dialect !=
698 cpu_to_le16(tcon->ses->server->vals->protocol_id))
699 goto vneg_out;
700
701 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
702 goto vneg_out;
703
704 /* do not validate server guid because not saved at negprot time yet */
705
706 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
707 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
708 goto vneg_out;
709
710 /* validate negotiate successful */
711 cifs_dbg(FYI, "validate negotiate info successful\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200712 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600713 return 0;
714
715vneg_out:
716 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200717err_rsp_free:
718 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600719 return -EIO;
720}
721
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530722enum securityEnum
723smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
724{
725 switch (requested) {
726 case Kerberos:
727 case RawNTLMSSP:
728 return requested;
729 case NTLMv2:
730 return RawNTLMSSP;
731 case Unspecified:
732 if (server->sec_ntlmssp &&
733 (global_secflags & CIFSSEC_MAY_NTLMSSP))
734 return RawNTLMSSP;
735 if ((server->sec_kerberos || server->sec_mskerberos) &&
736 (global_secflags & CIFSSEC_MAY_KRB5))
737 return Kerberos;
738 /* Fallthrough */
739 default:
740 return Unspecified;
741 }
742}
743
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100744struct SMB2_sess_data {
745 unsigned int xid;
746 struct cifs_ses *ses;
747 struct nls_table *nls_cp;
748 void (*func)(struct SMB2_sess_data *);
749 int result;
750 u64 previous_session;
751
752 /* we will send the SMB in three pieces:
753 * a fixed length beginning part, an optional
754 * SPNEGO blob (which can be zero length), and a
755 * last part which will include the strings
756 * and rest of bcc area. This allows us to avoid
757 * a large buffer 17K allocation
758 */
759 int buf0_type;
760 struct kvec iov[2];
761};
762
763static int
764SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
765{
766 int rc;
767 struct cifs_ses *ses = sess_data->ses;
768 struct smb2_sess_setup_req *req;
769 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100770 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100771
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100772 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
773 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100774 if (rc)
775 return rc;
776
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700777 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100778 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100779
780 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
781 req->PreviousSessionId = sess_data->previous_session;
782
783 req->Flags = 0; /* MBZ */
784 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100785 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100786
787 /* only one of SMB2 signing flags may be set in SMB2 request */
788 if (server->sign)
789 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
790 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
791 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
792 else
793 req->SecurityMode = 0;
794
795 req->Capabilities = 0;
796 req->Channel = 0; /* MBZ */
797
798 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100799 /* 1 for pad */
800 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100801 /*
802 * This variable will be used to clear the buffer
803 * allocated above in case of any error in the calling function.
804 */
805 sess_data->buf0_type = CIFS_SMALL_BUFFER;
806
807 return 0;
808}
809
810static void
811SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
812{
813 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
814 sess_data->buf0_type = CIFS_NO_BUFFER;
815}
816
817static int
818SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
819{
820 int rc;
821 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700822 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100823
824 /* Testing shows that buffer offset must be at location of Buffer[0] */
825 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100826 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100827 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
828
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100829 /* BB add code to build os and lm fields */
830
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100831 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
832 sess_data->iov, 2,
833 &sess_data->buf0_type,
834 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700835 cifs_small_buf_release(sess_data->iov[0].iov_base);
836 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100837
838 return rc;
839}
840
841static int
842SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
843{
844 int rc = 0;
845 struct cifs_ses *ses = sess_data->ses;
846
847 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800848 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100849 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100850 if (rc) {
851 cifs_dbg(FYI,
852 "SMB3 session key generation failed\n");
853 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800854 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100855 }
856 }
857 if (!ses->server->session_estab) {
858 ses->server->sequence_number = 0x2;
859 ses->server->session_estab = true;
860 }
861 mutex_unlock(&ses->server->srv_mutex);
862
863 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
864 spin_lock(&GlobalMid_Lock);
865 ses->status = CifsGood;
866 ses->need_reconnect = false;
867 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100868 return rc;
869}
870
871#ifdef CONFIG_CIFS_UPCALL
872static void
873SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
874{
875 int rc;
876 struct cifs_ses *ses = sess_data->ses;
877 struct cifs_spnego_msg *msg;
878 struct key *spnego_key = NULL;
879 struct smb2_sess_setup_rsp *rsp = NULL;
880
881 rc = SMB2_sess_alloc_buffer(sess_data);
882 if (rc)
883 goto out;
884
885 spnego_key = cifs_get_spnego_key(ses);
886 if (IS_ERR(spnego_key)) {
887 rc = PTR_ERR(spnego_key);
888 spnego_key = NULL;
889 goto out;
890 }
891
892 msg = spnego_key->payload.data[0];
893 /*
894 * check version field to make sure that cifs.upcall is
895 * sending us a response in an expected form
896 */
897 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
898 cifs_dbg(VFS,
899 "bad cifs.upcall version. Expected %d got %d",
900 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
901 rc = -EKEYREJECTED;
902 goto out_put_spnego_key;
903 }
904
905 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
906 GFP_KERNEL);
907 if (!ses->auth_key.response) {
908 cifs_dbg(VFS,
909 "Kerberos can't allocate (%u bytes) memory",
910 msg->sesskey_len);
911 rc = -ENOMEM;
912 goto out_put_spnego_key;
913 }
914 ses->auth_key.len = msg->sesskey_len;
915
916 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
917 sess_data->iov[1].iov_len = msg->secblob_len;
918
919 rc = SMB2_sess_sendreceive(sess_data);
920 if (rc)
921 goto out_put_spnego_key;
922
923 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700924 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100925
926 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100927
928 rc = SMB2_sess_establish_session(sess_data);
929out_put_spnego_key:
930 key_invalidate(spnego_key);
931 key_put(spnego_key);
932out:
933 sess_data->result = rc;
934 sess_data->func = NULL;
935 SMB2_sess_free_buffer(sess_data);
936}
937#else
938static void
939SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
940{
941 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
942 sess_data->result = -EOPNOTSUPP;
943 sess_data->func = NULL;
944}
945#endif
946
Sachin Prabhu166cea42016-10-07 19:11:22 +0100947static void
948SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
949
950static void
951SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
952{
953 int rc;
954 struct cifs_ses *ses = sess_data->ses;
955 struct smb2_sess_setup_rsp *rsp = NULL;
956 char *ntlmssp_blob = NULL;
957 bool use_spnego = false; /* else use raw ntlmssp */
958 u16 blob_length = 0;
959
960 /*
961 * If memory allocation is successful, caller of this function
962 * frees it.
963 */
964 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
965 if (!ses->ntlmssp) {
966 rc = -ENOMEM;
967 goto out_err;
968 }
969 ses->ntlmssp->sesskey_per_smbsess = true;
970
971 rc = SMB2_sess_alloc_buffer(sess_data);
972 if (rc)
973 goto out_err;
974
975 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
976 GFP_KERNEL);
977 if (ntlmssp_blob == NULL) {
978 rc = -ENOMEM;
979 goto out;
980 }
981
982 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
983 if (use_spnego) {
984 /* BB eventually need to add this */
985 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
986 rc = -EOPNOTSUPP;
987 goto out;
988 } else {
989 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
990 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
991 }
992 sess_data->iov[1].iov_base = ntlmssp_blob;
993 sess_data->iov[1].iov_len = blob_length;
994
995 rc = SMB2_sess_sendreceive(sess_data);
996 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
997
998 /* If true, rc here is expected and not an error */
999 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001000 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001001 rc = 0;
1002
1003 if (rc)
1004 goto out;
1005
1006 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1007 le16_to_cpu(rsp->SecurityBufferOffset)) {
1008 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1009 le16_to_cpu(rsp->SecurityBufferOffset));
1010 rc = -EIO;
1011 goto out;
1012 }
1013 rc = decode_ntlmssp_challenge(rsp->Buffer,
1014 le16_to_cpu(rsp->SecurityBufferLength), ses);
1015 if (rc)
1016 goto out;
1017
1018 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1019
1020
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001021 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001022 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001023
1024out:
1025 kfree(ntlmssp_blob);
1026 SMB2_sess_free_buffer(sess_data);
1027 if (!rc) {
1028 sess_data->result = 0;
1029 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1030 return;
1031 }
1032out_err:
1033 kfree(ses->ntlmssp);
1034 ses->ntlmssp = NULL;
1035 sess_data->result = rc;
1036 sess_data->func = NULL;
1037}
1038
1039static void
1040SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1041{
1042 int rc;
1043 struct cifs_ses *ses = sess_data->ses;
1044 struct smb2_sess_setup_req *req;
1045 struct smb2_sess_setup_rsp *rsp = NULL;
1046 unsigned char *ntlmssp_blob = NULL;
1047 bool use_spnego = false; /* else use raw ntlmssp */
1048 u16 blob_length = 0;
1049
1050 rc = SMB2_sess_alloc_buffer(sess_data);
1051 if (rc)
1052 goto out;
1053
1054 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001055 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001056
1057 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1058 sess_data->nls_cp);
1059 if (rc) {
1060 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1061 goto out;
1062 }
1063
1064 if (use_spnego) {
1065 /* BB eventually need to add this */
1066 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1067 rc = -EOPNOTSUPP;
1068 goto out;
1069 }
1070 sess_data->iov[1].iov_base = ntlmssp_blob;
1071 sess_data->iov[1].iov_len = blob_length;
1072
1073 rc = SMB2_sess_sendreceive(sess_data);
1074 if (rc)
1075 goto out;
1076
1077 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1078
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001079 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001080 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001081
1082 rc = SMB2_sess_establish_session(sess_data);
1083out:
1084 kfree(ntlmssp_blob);
1085 SMB2_sess_free_buffer(sess_data);
1086 kfree(ses->ntlmssp);
1087 ses->ntlmssp = NULL;
1088 sess_data->result = rc;
1089 sess_data->func = NULL;
1090}
1091
1092static int
1093SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1094{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301095 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001096
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301097 type = smb2_select_sectype(ses->server, ses->sectype);
1098 cifs_dbg(FYI, "sess setup type %d\n", type);
1099 if (type == Unspecified) {
1100 cifs_dbg(VFS,
1101 "Unable to select appropriate authentication method!");
1102 return -EINVAL;
1103 }
1104
1105 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001106 case Kerberos:
1107 sess_data->func = SMB2_auth_kerberos;
1108 break;
1109 case RawNTLMSSP:
1110 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1111 break;
1112 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301113 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001114 return -EOPNOTSUPP;
1115 }
1116
1117 return 0;
1118}
1119
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001120int
1121SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1122 const struct nls_table *nls_cp)
1123{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001124 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001125 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001126 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001127
Joe Perchesf96637b2013-05-04 22:12:25 -05001128 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001129
Jeff Layton3534b852013-05-24 07:41:01 -04001130 if (!server) {
1131 WARN(1, "%s: server is NULL!\n", __func__);
1132 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001133 }
1134
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001135 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1136 if (!sess_data)
1137 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001138
1139 rc = SMB2_select_sec(ses, sess_data);
1140 if (rc)
1141 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001142 sess_data->xid = xid;
1143 sess_data->ses = ses;
1144 sess_data->buf0_type = CIFS_NO_BUFFER;
1145 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001146
Sachin Prabhu166cea42016-10-07 19:11:22 +01001147 while (sess_data->func)
1148 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001149
Steve Frenchc721c382017-09-19 18:40:03 -05001150 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1151 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001152 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001153out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001154 kfree(sess_data);
1155 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001156}
1157
1158int
1159SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1160{
1161 struct smb2_logoff_req *req; /* response is also trivial struct */
1162 int rc = 0;
1163 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001164 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001165 unsigned int total_len;
1166 struct kvec iov[1];
1167 struct kvec rsp_iov;
1168 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001169
Joe Perchesf96637b2013-05-04 22:12:25 -05001170 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001171
1172 if (ses && (ses->server))
1173 server = ses->server;
1174 else
1175 return -EIO;
1176
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001177 /* no need to send SMB logoff if uid already closed due to reconnect */
1178 if (ses->need_reconnect)
1179 goto smb2_session_already_dead;
1180
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001181 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001182 if (rc)
1183 return rc;
1184
1185 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001186 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001187
1188 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1189 flags |= CIFS_TRANSFORM_REQ;
1190 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001191 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001192
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001193 flags |= CIFS_NO_RESP;
1194
1195 iov[0].iov_base = (char *)req;
1196 iov[0].iov_len = total_len;
1197
1198 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001199 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001200 /*
1201 * No tcon so can't do
1202 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1203 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001204
1205smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001206 return rc;
1207}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001208
1209static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1210{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001211 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001212}
1213
1214#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1215
Steve Frenchde9f68df2013-11-15 11:26:24 -06001216/* These are similar values to what Windows uses */
1217static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1218{
1219 tcon->max_chunks = 256;
1220 tcon->max_bytes_chunk = 1048576;
1221 tcon->max_bytes_copy = 16777216;
1222}
1223
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001224int
1225SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1226 struct cifs_tcon *tcon, const struct nls_table *cp)
1227{
1228 struct smb2_tree_connect_req *req;
1229 struct smb2_tree_connect_rsp *rsp = NULL;
1230 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001231 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001232 int rc = 0;
1233 int resp_buftype;
1234 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001235 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001236 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001237 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001238
Joe Perchesf96637b2013-05-04 22:12:25 -05001239 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001240
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001241 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001242 return -EIO;
1243
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001244 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1245 if (unc_path == NULL)
1246 return -ENOMEM;
1247
1248 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1249 unc_path_len *= 2;
1250 if (unc_path_len < 2) {
1251 kfree(unc_path);
1252 return -EINVAL;
1253 }
1254
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001255 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1256 if (tcon)
1257 tcon->tid = 0;
1258
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001259 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1260 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001261 if (rc) {
1262 kfree(unc_path);
1263 return rc;
1264 }
1265
1266 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001267 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1268 flags |= CIFS_TRANSFORM_REQ;
1269
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001270 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001271 req->sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001272 if (ses->server->sign)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001273 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001274 } else if (encryption_required(tcon))
1275 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001276
1277 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001278 /* 1 for pad */
1279 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001280
1281 /* Testing shows that buffer offset must be at location of Buffer[0] */
1282 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001283 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001284 req->PathLength = cpu_to_le16(unc_path_len - 2);
1285 iov[1].iov_base = unc_path;
1286 iov[1].iov_len = unc_path_len;
1287
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001288 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001289 cifs_small_buf_release(req);
1290 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001291
1292 if (rc != 0) {
1293 if (tcon) {
1294 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1295 tcon->need_reconnect = true;
1296 }
1297 goto tcon_error_exit;
1298 }
1299
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001300 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001301 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001302 goto tcon_exit;
1303 }
1304
Christophe JAILLETcd123002017-05-12 17:59:32 +02001305 switch (rsp->ShareType) {
1306 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001307 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001308 break;
1309 case SMB2_SHARE_TYPE_PIPE:
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001310 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001311 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001312 break;
1313 case SMB2_SHARE_TYPE_PRINT:
1314 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001315 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001316 break;
1317 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001318 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001319 rc = -EOPNOTSUPP;
1320 goto tcon_error_exit;
1321 }
1322
1323 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001324 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001325 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1326 tcon->tidStatus = CifsGood;
1327 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001328 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001329 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001330
1331 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1332 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001333 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001334
1335 if (tcon->seal &&
1336 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1337 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1338
Steve Frenchde9f68df2013-11-15 11:26:24 -06001339 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001340 if (tcon->ses->server->ops->validate_negotiate)
1341 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001342tcon_exit:
1343 free_rsp_buf(resp_buftype, rsp);
1344 kfree(unc_path);
1345 return rc;
1346
1347tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001348 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001349 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001350 }
1351 goto tcon_exit;
1352}
1353
1354int
1355SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1356{
1357 struct smb2_tree_disconnect_req *req; /* response is trivial */
1358 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001359 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001360 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001361 unsigned int total_len;
1362 struct kvec iov[1];
1363 struct kvec rsp_iov;
1364 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001365
Joe Perchesf96637b2013-05-04 22:12:25 -05001366 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001367
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001368 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001369 return -EIO;
1370
1371 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1372 return 0;
1373
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001374 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1375 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001376 if (rc)
1377 return rc;
1378
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001379 if (encryption_required(tcon))
1380 flags |= CIFS_TRANSFORM_REQ;
1381
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001382 flags |= CIFS_NO_RESP;
1383
1384 iov[0].iov_base = (char *)req;
1385 iov[0].iov_len = total_len;
1386
1387 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001388 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001389 if (rc)
1390 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1391
1392 return rc;
1393}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001394
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001395
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001396static struct create_durable *
1397create_durable_buf(void)
1398{
1399 struct create_durable *buf;
1400
1401 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1402 if (!buf)
1403 return NULL;
1404
1405 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001406 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001407 buf->ccontext.DataLength = cpu_to_le32(16);
1408 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1409 (struct create_durable, Name));
1410 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001411 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001412 buf->Name[0] = 'D';
1413 buf->Name[1] = 'H';
1414 buf->Name[2] = 'n';
1415 buf->Name[3] = 'Q';
1416 return buf;
1417}
1418
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001419static struct create_durable *
1420create_reconnect_durable_buf(struct cifs_fid *fid)
1421{
1422 struct create_durable *buf;
1423
1424 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1425 if (!buf)
1426 return NULL;
1427
1428 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1429 (struct create_durable, Data));
1430 buf->ccontext.DataLength = cpu_to_le32(16);
1431 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1432 (struct create_durable, Name));
1433 buf->ccontext.NameLength = cpu_to_le16(4);
1434 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1435 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001436 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001437 buf->Name[0] = 'D';
1438 buf->Name[1] = 'H';
1439 buf->Name[2] = 'n';
1440 buf->Name[3] = 'C';
1441 return buf;
1442}
1443
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001444static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001445parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1446 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001447{
1448 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001449 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001450 unsigned int next;
1451 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001452 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001453
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001454 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001455 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001456 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001457 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001458 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001459 if (le16_to_cpu(cc->NameLength) == 4 &&
1460 strncmp(name, "RqLs", 4) == 0)
1461 return server->ops->parse_lease_buf(cc, epoch);
1462
1463 next = le32_to_cpu(cc->Next);
1464 if (!next)
1465 break;
1466 remaining -= next;
1467 cc = (struct create_context *)((char *)cc + next);
1468 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001469
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001470 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001471}
1472
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001473static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001474add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1475 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001476{
1477 struct smb2_create_req *req = iov[0].iov_base;
1478 unsigned int num = *num_iovec;
1479
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001480 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001481 if (iov[num].iov_base == NULL)
1482 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001483 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001484 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1485 if (!req->CreateContextsOffset)
1486 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001487 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001488 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001489 le32_add_cpu(&req->CreateContextsLength,
1490 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001491 *num_iovec = num + 1;
1492 return 0;
1493}
1494
Steve Frenchb56eae42015-11-03 09:26:27 -06001495static struct create_durable_v2 *
1496create_durable_v2_buf(struct cifs_fid *pfid)
1497{
1498 struct create_durable_v2 *buf;
1499
1500 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1501 if (!buf)
1502 return NULL;
1503
1504 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1505 (struct create_durable_v2, dcontext));
1506 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1507 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1508 (struct create_durable_v2, Name));
1509 buf->ccontext.NameLength = cpu_to_le16(4);
1510
1511 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1512 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001513 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001514 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1515
1516 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1517 buf->Name[0] = 'D';
1518 buf->Name[1] = 'H';
1519 buf->Name[2] = '2';
1520 buf->Name[3] = 'Q';
1521 return buf;
1522}
1523
1524static struct create_durable_handle_reconnect_v2 *
1525create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1526{
1527 struct create_durable_handle_reconnect_v2 *buf;
1528
1529 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1530 GFP_KERNEL);
1531 if (!buf)
1532 return NULL;
1533
1534 buf->ccontext.DataOffset =
1535 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1536 dcontext));
1537 buf->ccontext.DataLength =
1538 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1539 buf->ccontext.NameOffset =
1540 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1541 Name));
1542 buf->ccontext.NameLength = cpu_to_le16(4);
1543
1544 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1545 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1546 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1547 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1548
1549 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1550 buf->Name[0] = 'D';
1551 buf->Name[1] = 'H';
1552 buf->Name[2] = '2';
1553 buf->Name[3] = 'C';
1554 return buf;
1555}
1556
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001557static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001558add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001559 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001560{
1561 struct smb2_create_req *req = iov[0].iov_base;
1562 unsigned int num = *num_iovec;
1563
Steve Frenchb56eae42015-11-03 09:26:27 -06001564 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1565 if (iov[num].iov_base == NULL)
1566 return -ENOMEM;
1567 iov[num].iov_len = sizeof(struct create_durable_v2);
1568 if (!req->CreateContextsOffset)
1569 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001570 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001571 iov[1].iov_len);
1572 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001573 *num_iovec = num + 1;
1574 return 0;
1575}
1576
1577static int
1578add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1579 struct cifs_open_parms *oparms)
1580{
1581 struct smb2_create_req *req = iov[0].iov_base;
1582 unsigned int num = *num_iovec;
1583
1584 /* indicate that we don't need to relock the file */
1585 oparms->reconnect = false;
1586
1587 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1588 if (iov[num].iov_base == NULL)
1589 return -ENOMEM;
1590 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1591 if (!req->CreateContextsOffset)
1592 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001593 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001594 iov[1].iov_len);
1595 le32_add_cpu(&req->CreateContextsLength,
1596 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001597 *num_iovec = num + 1;
1598 return 0;
1599}
1600
1601static int
1602add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1603 struct cifs_open_parms *oparms, bool use_persistent)
1604{
1605 struct smb2_create_req *req = iov[0].iov_base;
1606 unsigned int num = *num_iovec;
1607
1608 if (use_persistent) {
1609 if (oparms->reconnect)
1610 return add_durable_reconnect_v2_context(iov, num_iovec,
1611 oparms);
1612 else
1613 return add_durable_v2_context(iov, num_iovec, oparms);
1614 }
1615
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001616 if (oparms->reconnect) {
1617 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1618 /* indicate that we don't need to relock the file */
1619 oparms->reconnect = false;
1620 } else
1621 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001622 if (iov[num].iov_base == NULL)
1623 return -ENOMEM;
1624 iov[num].iov_len = sizeof(struct create_durable);
1625 if (!req->CreateContextsOffset)
1626 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001627 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001628 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001629 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001630 *num_iovec = num + 1;
1631 return 0;
1632}
1633
Aurelien Aptelf0712922017-02-22 14:47:17 +01001634static int
1635alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1636 const char *treename, const __le16 *path)
1637{
1638 int treename_len, path_len;
1639 struct nls_table *cp;
1640 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1641
1642 /*
1643 * skip leading "\\"
1644 */
1645 treename_len = strlen(treename);
1646 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1647 return -EINVAL;
1648
1649 treename += 2;
1650 treename_len -= 2;
1651
1652 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1653
1654 /*
1655 * make room for one path separator between the treename and
1656 * path
1657 */
1658 *out_len = treename_len + 1 + path_len;
1659
1660 /*
1661 * final path needs to be null-terminated UTF16 with a
1662 * size aligned to 8
1663 */
1664
1665 *out_size = roundup((*out_len+1)*2, 8);
1666 *out_path = kzalloc(*out_size, GFP_KERNEL);
1667 if (!*out_path)
1668 return -ENOMEM;
1669
1670 cp = load_nls_default();
1671 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1672 UniStrcat(*out_path, sep);
1673 UniStrcat(*out_path, path);
1674 unload_nls(cp);
1675
1676 return 0;
1677}
1678
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001679int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001680SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001681 __u8 *oplock, struct smb2_file_all_info *buf,
1682 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001683{
1684 struct smb2_create_req *req;
1685 struct smb2_create_rsp *rsp;
1686 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001687 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001688 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001689 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001690 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001691 int resp_buftype;
1692 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001693 __le16 *copy_path = NULL;
1694 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001695 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001696 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001697 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001698 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001699 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001700 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001701
Joe Perchesf96637b2013-05-04 22:12:25 -05001702 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001703
1704 if (ses && (ses->server))
1705 server = ses->server;
1706 else
1707 return -EIO;
1708
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001709 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1710
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001711 if (rc)
1712 return rc;
1713
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001714 if (encryption_required(tcon))
1715 flags |= CIFS_TRANSFORM_REQ;
1716
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001717 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001718 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001719 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1720 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001721
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001722 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001723 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001724 /* File attributes ignored on open (used in create though) */
1725 req->FileAttributes = cpu_to_le32(file_attributes);
1726 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001727 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1728 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001729
1730 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001731 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001732 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001733
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001734 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001735
1736 /* [MS-SMB2] 2.2.13 NameOffset:
1737 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1738 * the SMB2 header, the file name includes a prefix that will
1739 * be processed during DFS name normalization as specified in
1740 * section 3.3.5.9. Otherwise, the file name is relative to
1741 * the share that is identified by the TreeId in the SMB2
1742 * header.
1743 */
1744 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1745 int name_len;
1746
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001747 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001748 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1749 &name_len,
1750 tcon->treeName, path);
1751 if (rc)
1752 return rc;
1753 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001754 uni_path_len = copy_size;
1755 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001756 } else {
1757 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1758 /* MUST set path len (NameLength) to 0 opening root of share */
1759 req->NameLength = cpu_to_le16(uni_path_len - 2);
1760 if (uni_path_len % 8 != 0) {
1761 copy_size = roundup(uni_path_len, 8);
1762 copy_path = kzalloc(copy_size, GFP_KERNEL);
1763 if (!copy_path)
1764 return -ENOMEM;
1765 memcpy((char *)copy_path, (const char *)path,
1766 uni_path_len);
1767 uni_path_len = copy_size;
1768 path = copy_path;
1769 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001770 }
1771
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001772 iov[1].iov_len = uni_path_len;
1773 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001774
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001775 if (!server->oplocks)
1776 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1777
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001778 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001779 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1780 req->RequestedOplockLevel = *oplock;
1781 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001782 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001783 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001784 cifs_small_buf_release(req);
1785 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001786 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001787 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001788 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001789 }
1790
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001791 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1792 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001793 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001794 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001795 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001796 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001797 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001798 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001799
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001800 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001801 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001802 if (rc) {
1803 cifs_small_buf_release(req);
1804 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001805 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001806 return rc;
1807 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001808 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001809 }
1810
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001811 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1812 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001813 cifs_small_buf_release(req);
1814 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001815
1816 if (rc != 0) {
1817 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001818 if (err_buf && rsp)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001819 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1820 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001821 goto creat_exit;
1822 }
1823
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001824 oparms->fid->persistent_fid = rsp->PersistentFileId;
1825 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001826
1827 if (buf) {
1828 memcpy(buf, &rsp->CreationTime, 32);
1829 buf->AllocationSize = rsp->AllocationSize;
1830 buf->EndOfFile = rsp->EndofFile;
1831 buf->Attributes = rsp->FileAttributes;
1832 buf->NumberOfLinks = cpu_to_le32(1);
1833 buf->DeletePending = 0;
1834 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001835
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001836 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001837 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001838 else
1839 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001840creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001841 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001842 kfree(lc_buf);
1843 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001844 free_rsp_buf(resp_buftype, rsp);
1845 return rc;
1846}
1847
Steve French4a72daf2013-06-25 00:20:49 -05001848/*
1849 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1850 */
1851int
1852SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001853 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1854 char *in_data, u32 indatalen,
1855 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001856{
1857 struct smb2_ioctl_req *req;
1858 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001859 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001860 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001861 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001862 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001863 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001864 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001865 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001866 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001867 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05001868
1869 cifs_dbg(FYI, "SMB2 IOCTL\n");
1870
Steve French3d1a3742014-08-11 21:05:25 -05001871 if (out_data != NULL)
1872 *out_data = NULL;
1873
Steve French4a72daf2013-06-25 00:20:49 -05001874 /* zero out returned data len, in case of error */
1875 if (plen)
1876 *plen = 0;
1877
Steve French8e353102015-03-26 19:47:02 -05001878 if (tcon)
1879 ses = tcon->ses;
1880 else
1881 return -EIO;
1882
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001883 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05001884 return -EIO;
1885
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001886 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05001887 if (rc)
1888 return rc;
1889
Aurelien Aptel51146622017-02-28 15:08:41 +01001890 if (use_ipc) {
1891 if (ses->ipc_tid == 0) {
1892 cifs_small_buf_release(req);
1893 return -ENOTCONN;
1894 }
1895
1896 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001897 req->sync_hdr.TreeId, ses->ipc_tid);
1898 req->sync_hdr.TreeId = ses->ipc_tid;
Aurelien Aptel51146622017-02-28 15:08:41 +01001899 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001900 if (encryption_required(tcon))
1901 flags |= CIFS_TRANSFORM_REQ;
1902
Steve French4a72daf2013-06-25 00:20:49 -05001903 req->CtlCode = cpu_to_le32(opcode);
1904 req->PersistentFileId = persistent_fid;
1905 req->VolatileFileId = volatile_fid;
1906
1907 if (indatalen) {
1908 req->InputCount = cpu_to_le32(indatalen);
1909 /* do not set InputOffset if no input data */
1910 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001911 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05001912 iov[1].iov_base = in_data;
1913 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001914 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001915 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001916 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001917
1918 req->OutputOffset = 0;
1919 req->OutputCount = 0; /* MBZ */
1920
1921 /*
1922 * Could increase MaxOutputResponse, but that would require more
1923 * than one credit. Windows typically sets this smaller, but for some
1924 * ioctls it may be useful to allow server to send more. No point
1925 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001926 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1927 * (by default, note that it can be overridden to make max larger)
1928 * in responses (except for read responses which can be bigger.
1929 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001930 */
Steve French7db0a6e2017-05-03 21:12:20 -05001931 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001932
1933 if (is_fsctl)
1934 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1935 else
1936 req->Flags = 0;
1937
1938 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001939
Steve French7ff8d452013-10-14 00:44:19 -05001940 /*
1941 * If no input data, the size of ioctl struct in
1942 * protocol spec still includes a 1 byte data buffer,
1943 * but if input data passed to ioctl, we do not
1944 * want to double count this, so we do not send
1945 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001946 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05001947 */
1948
1949 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001950 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05001951 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001952 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05001953
Steve French4587eee2017-10-25 15:58:31 -05001954 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1955 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001956 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05001957
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001958 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1959 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001960 cifs_small_buf_release(req);
1961 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001962
Steve French9bf0c9c2013-11-16 18:05:28 -06001963 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001964 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001965 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001966 } else if (rc == -EINVAL) {
1967 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1968 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001969 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001970 goto ioctl_exit;
1971 }
Steve French4a72daf2013-06-25 00:20:49 -05001972 }
1973
1974 /* check if caller wants to look at return data or just return rc */
1975 if ((plen == NULL) || (out_data == NULL))
1976 goto ioctl_exit;
1977
1978 *plen = le32_to_cpu(rsp->OutputCount);
1979
1980 /* We check for obvious errors in the output buffer length and offset */
1981 if (*plen == 0)
1982 goto ioctl_exit; /* server returned no data */
1983 else if (*plen > 0xFF00) {
1984 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1985 *plen = 0;
1986 rc = -EIO;
1987 goto ioctl_exit;
1988 }
1989
1990 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1991 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1992 le32_to_cpu(rsp->OutputOffset));
1993 *plen = 0;
1994 rc = -EIO;
1995 goto ioctl_exit;
1996 }
1997
1998 *out_data = kmalloc(*plen, GFP_KERNEL);
1999 if (*out_data == NULL) {
2000 rc = -ENOMEM;
2001 goto ioctl_exit;
2002 }
2003
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002004 shdr = get_sync_hdr(rsp);
2005 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002006ioctl_exit:
2007 free_rsp_buf(resp_buftype, rsp);
2008 return rc;
2009}
2010
Steve French64a5cfa2013-10-14 15:31:32 -05002011/*
2012 * Individual callers to ioctl worker function follow
2013 */
2014
2015int
2016SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2017 u64 persistent_fid, u64 volatile_fid)
2018{
2019 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002020 struct compress_ioctl fsctl_input;
2021 char *ret_data = NULL;
2022
2023 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002024 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002025
2026 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2027 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002028 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05002029 (char *)&fsctl_input /* data input */,
2030 2 /* in data len */, &ret_data /* out data */, NULL);
2031
2032 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002033
2034 return rc;
2035}
2036
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002037int
2038SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2039 u64 persistent_fid, u64 volatile_fid)
2040{
2041 struct smb2_close_req *req;
2042 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002043 struct cifs_ses *ses = tcon->ses;
2044 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002045 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002046 int resp_buftype;
2047 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002048 int flags = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002049 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002050
Joe Perchesf96637b2013-05-04 22:12:25 -05002051 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002052
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002053 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002054 return -EIO;
2055
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002056 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002057 if (rc)
2058 return rc;
2059
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002060 if (encryption_required(tcon))
2061 flags |= CIFS_TRANSFORM_REQ;
2062
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002063 req->PersistentFileId = persistent_fid;
2064 req->VolatileFileId = volatile_fid;
2065
2066 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002067 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002068
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002069 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002070 cifs_small_buf_release(req);
2071 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002072
2073 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002074 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002075 goto close_exit;
2076 }
2077
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002078 /* BB FIXME - decode close response, update inode for caching */
2079
2080close_exit:
2081 free_rsp_buf(resp_buftype, rsp);
2082 return rc;
2083}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002084
2085static int
2086validate_buf(unsigned int offset, unsigned int buffer_length,
2087 struct smb2_hdr *hdr, unsigned int min_buf_size)
2088
2089{
2090 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2091 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2092 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2093 char *end_of_buf = begin_of_buf + buffer_length;
2094
2095
2096 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002097 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2098 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002099 return -EINVAL;
2100 }
2101
2102 /* check if beyond RFC1001 maximum length */
2103 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002104 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2105 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002106 return -EINVAL;
2107 }
2108
2109 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002110 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002111 return -EINVAL;
2112 }
2113
2114 return 0;
2115}
2116
2117/*
2118 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2119 * Caller must free buffer.
2120 */
2121static int
2122validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2123 struct smb2_hdr *hdr, unsigned int minbufsize,
2124 char *data)
2125
2126{
2127 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2128 int rc;
2129
2130 if (!data)
2131 return -EINVAL;
2132
2133 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2134 if (rc)
2135 return rc;
2136
2137 memcpy(data, begin_of_buf, buffer_length);
2138
2139 return 0;
2140}
2141
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002142static int
2143query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002144 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2145 u32 additional_info, size_t output_len, size_t min_len, void **data,
2146 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002147{
2148 struct smb2_query_info_req *req;
2149 struct smb2_query_info_rsp *rsp = NULL;
2150 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002151 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002152 int rc = 0;
2153 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002154 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002155 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002156 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002157
Joe Perchesf96637b2013-05-04 22:12:25 -05002158 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002159
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002160 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002161 return -EIO;
2162
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002163 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2164 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002165 if (rc)
2166 return rc;
2167
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002168 if (encryption_required(tcon))
2169 flags |= CIFS_TRANSFORM_REQ;
2170
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002171 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002172 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002173 req->PersistentFileId = persistent_fid;
2174 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002175 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002176
2177 /*
2178 * We do not use the input buffer (do not send extra byte)
2179 */
2180 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002181
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002182 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002183
2184 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002185 /* 1 for Buffer */
2186 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002187
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002188 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002189 cifs_small_buf_release(req);
2190 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002191
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002192 if (rc) {
2193 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2194 goto qinf_exit;
2195 }
2196
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002197 if (dlen) {
2198 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2199 if (!*data) {
2200 *data = kmalloc(*dlen, GFP_KERNEL);
2201 if (!*data) {
2202 cifs_dbg(VFS,
2203 "Error %d allocating memory for acl\n",
2204 rc);
2205 *dlen = 0;
2206 goto qinf_exit;
2207 }
2208 }
2209 }
2210
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002211 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2212 le32_to_cpu(rsp->OutputBufferLength),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002213 &rsp->hdr, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002214
2215qinf_exit:
2216 free_rsp_buf(resp_buftype, rsp);
2217 return rc;
2218}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002219
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002220int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002221 u64 persistent_fid, u64 volatile_fid,
2222 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002223{
2224 return query_info(xid, tcon, persistent_fid, volatile_fid,
2225 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002226 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002227 sizeof(struct smb2_file_full_ea_info),
2228 (void **)&data,
2229 NULL);
2230}
2231
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002232int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2233 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002234{
2235 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002236 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002237 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002238 sizeof(struct smb2_file_all_info), (void **)&data,
2239 NULL);
2240}
2241
2242int
2243SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2244 u64 persistent_fid, u64 volatile_fid,
2245 void **data, u32 *plen)
2246{
2247 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2248 *plen = 0;
2249
2250 return query_info(xid, tcon, persistent_fid, volatile_fid,
2251 0, SMB2_O_INFO_SECURITY, additional_info,
2252 SMB2_MAX_BUFFER_SIZE,
2253 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002254}
2255
2256int
2257SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2258 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2259{
2260 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002261 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002262 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002263 sizeof(struct smb2_file_internal_info),
2264 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002265}
2266
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002267/*
2268 * This is a no-op for now. We're not really interested in the reply, but
2269 * rather in the fact that the server sent one and that server->lstrp
2270 * gets updated.
2271 *
2272 * FIXME: maybe we should consider checking that the reply matches request?
2273 */
2274static void
2275smb2_echo_callback(struct mid_q_entry *mid)
2276{
2277 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002278 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002279 unsigned int credits_received = 1;
2280
2281 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002282 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002283
2284 DeleteMidQEntry(mid);
2285 add_credits(server, credits_received, CIFS_ECHO_OP);
2286}
2287
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002288void smb2_reconnect_server(struct work_struct *work)
2289{
2290 struct TCP_Server_Info *server = container_of(work,
2291 struct TCP_Server_Info, reconnect.work);
2292 struct cifs_ses *ses;
2293 struct cifs_tcon *tcon, *tcon2;
2294 struct list_head tmp_list;
2295 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002296 int rc;
2297 int resched = false;
2298
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002299
2300 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2301 mutex_lock(&server->reconnect_mutex);
2302
2303 INIT_LIST_HEAD(&tmp_list);
2304 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2305
2306 spin_lock(&cifs_tcp_ses_lock);
2307 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2308 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002309 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002310 tcon->tc_count++;
2311 list_add_tail(&tcon->rlist, &tmp_list);
2312 tcon_exist = true;
2313 }
2314 }
2315 }
2316 /*
2317 * Get the reference to server struct to be sure that the last call of
2318 * cifs_put_tcon() in the loop below won't release the server pointer.
2319 */
2320 if (tcon_exist)
2321 server->srv_count++;
2322
2323 spin_unlock(&cifs_tcp_ses_lock);
2324
2325 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002326 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2327 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002328 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002329 else
2330 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002331 list_del_init(&tcon->rlist);
2332 cifs_put_tcon(tcon);
2333 }
2334
2335 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002336 if (resched)
2337 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002338 mutex_unlock(&server->reconnect_mutex);
2339
2340 /* now we can safely release srv struct */
2341 if (tcon_exist)
2342 cifs_put_tcp_session(server, 1);
2343}
2344
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002345int
2346SMB2_echo(struct TCP_Server_Info *server)
2347{
2348 struct smb2_echo_req *req;
2349 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002350 struct kvec iov[2];
2351 struct smb_rqst rqst = { .rq_iov = iov,
2352 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002353 unsigned int total_len;
2354 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002355
Joe Perchesf96637b2013-05-04 22:12:25 -05002356 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002357
Steve French4fcd1812016-06-22 20:12:05 -05002358 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002359 /* No need to send echo on newly established connections */
2360 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2361 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002362 }
2363
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002364 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002365 if (rc)
2366 return rc;
2367
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002368 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002369
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002370 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002371 rfc1002_marker = cpu_to_be32(total_len);
2372 iov[0].iov_base = &rfc1002_marker;
2373 iov[1].iov_len = total_len;
2374 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002375
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002376 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2377 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002378 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002379 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002380
2381 cifs_small_buf_release(req);
2382 return rc;
2383}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002384
2385int
2386SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2387 u64 volatile_fid)
2388{
2389 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002390 struct cifs_ses *ses = tcon->ses;
2391 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002392 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002393 int resp_buftype;
2394 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002395 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002396 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002397
Joe Perchesf96637b2013-05-04 22:12:25 -05002398 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002399
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002400 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002401 return -EIO;
2402
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002403 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002404 if (rc)
2405 return rc;
2406
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002407 if (encryption_required(tcon))
2408 flags |= CIFS_TRANSFORM_REQ;
2409
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002410 req->PersistentFileId = persistent_fid;
2411 req->VolatileFileId = volatile_fid;
2412
2413 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002414 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002415
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002416 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002417 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002418
Steve Frenchdfebe402015-03-27 01:00:06 -05002419 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002420 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2421
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002422 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002423 return rc;
2424}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002425
2426/*
2427 * To form a chain of read requests, any read requests after the first should
2428 * have the end_of_chain boolean set to true.
2429 */
2430static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002431smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002432 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2433 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002434{
2435 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002436 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002437 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002438 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002439
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002440 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2441 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002442 if (rc)
2443 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002444
2445 server = io_parms->tcon->ses->server;
2446 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002447 return -ECONNABORTED;
2448
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002449 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002450 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002451
2452 req->PersistentFileId = io_parms->persistent_fid;
2453 req->VolatileFileId = io_parms->volatile_fid;
2454 req->ReadChannelInfoOffset = 0; /* reserved */
2455 req->ReadChannelInfoLength = 0; /* reserved */
2456 req->Channel = 0; /* reserved */
2457 req->MinimumCount = 0;
2458 req->Length = cpu_to_le32(io_parms->length);
2459 req->Offset = cpu_to_le64(io_parms->offset);
2460
2461 if (request_type & CHAINED_REQUEST) {
2462 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002463 /* next 8-byte aligned request */
2464 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2465 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002466 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002467 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002468 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002469 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002470 /*
2471 * Related requests use info from previous read request
2472 * in chain.
2473 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002474 shdr->SessionId = 0xFFFFFFFF;
2475 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002476 req->PersistentFileId = 0xFFFFFFFF;
2477 req->VolatileFileId = 0xFFFFFFFF;
2478 }
2479 }
2480 if (remaining_bytes > io_parms->length)
2481 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2482 else
2483 req->RemainingBytes = 0;
2484
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002485 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002486 return rc;
2487}
2488
2489static void
2490smb2_readv_callback(struct mid_q_entry *mid)
2491{
2492 struct cifs_readdata *rdata = mid->callback_data;
2493 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2494 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002495 struct smb2_sync_hdr *shdr =
2496 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002497 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002498 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2499 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002500 .rq_pages = rdata->pages,
2501 .rq_npages = rdata->nr_pages,
2502 .rq_pagesz = rdata->pagesz,
2503 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002504
Joe Perchesf96637b2013-05-04 22:12:25 -05002505 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2506 __func__, mid->mid, mid->mid_state, rdata->result,
2507 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002508
2509 switch (mid->mid_state) {
2510 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002511 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002512 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002513 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002514 int rc;
2515
Jeff Layton0b688cf2012-09-18 16:20:34 -07002516 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002517 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002518 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2519 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002520 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002521 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002522 task_io_account_read(rdata->got_bytes);
2523 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002524 break;
2525 case MID_REQUEST_SUBMITTED:
2526 case MID_RETRY_NEEDED:
2527 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002528 if (server->sign && rdata->got_bytes)
2529 /* reset bytes number since we can not check a sign */
2530 rdata->got_bytes = 0;
2531 /* FIXME: should this be counted toward the initiating task? */
2532 task_io_account_read(rdata->got_bytes);
2533 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002534 break;
2535 default:
2536 if (rdata->result != -ENODATA)
2537 rdata->result = -EIO;
2538 }
2539
2540 if (rdata->result)
2541 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2542
2543 queue_work(cifsiod_wq, &rdata->work);
2544 DeleteMidQEntry(mid);
2545 add_credits(server, credits_received, 0);
2546}
2547
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002548/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002549int
2550smb2_async_readv(struct cifs_readdata *rdata)
2551{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002552 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002553 char *buf;
2554 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002555 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002556 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2557 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002558 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002559 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002560 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002561
Joe Perchesf96637b2013-05-04 22:12:25 -05002562 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2563 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002564
2565 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2566 io_parms.offset = rdata->offset;
2567 io_parms.length = rdata->bytes;
2568 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2569 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2570 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002571
2572 server = io_parms.tcon->ses->server;
2573
Long Li2dabfd52017-11-07 01:54:53 -07002574 rc = smb2_new_read_req(
2575 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002576 if (rc) {
2577 if (rc == -EAGAIN && rdata->credits) {
2578 /* credits was reset by reconnect */
2579 rdata->credits = 0;
2580 /* reduce in_flight value since we won't send the req */
2581 spin_lock(&server->req_lock);
2582 server->in_flight--;
2583 spin_unlock(&server->req_lock);
2584 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002585 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002586 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002587
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002588 if (encryption_required(io_parms.tcon))
2589 flags |= CIFS_TRANSFORM_REQ;
2590
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002591 req_len = cpu_to_be32(total_len);
2592
2593 rdata->iov[0].iov_base = &req_len;
2594 rdata->iov[0].iov_len = sizeof(__be32);
2595 rdata->iov[1].iov_base = buf;
2596 rdata->iov[1].iov_len = total_len;
2597
2598 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002599
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002600 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002601 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002602 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002603 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002604 spin_lock(&server->req_lock);
2605 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002606 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002607 spin_unlock(&server->req_lock);
2608 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002609 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002610 }
2611
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002612 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002613 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002614 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002615 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002616 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002617 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002618 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2619 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002620
2621 cifs_small_buf_release(buf);
2622 return rc;
2623}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002624
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002625int
2626SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2627 unsigned int *nbytes, char **buf, int *buf_type)
2628{
2629 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002630 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002631 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002632 struct smb2_sync_hdr *shdr;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002633 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002634 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002635 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002636 int flags = CIFS_LOG_ERROR;
2637 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002638
2639 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002640 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002641 if (rc)
2642 return rc;
2643
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002644 if (encryption_required(io_parms->tcon))
2645 flags |= CIFS_TRANSFORM_REQ;
2646
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002647 iov[0].iov_base = (char *)req;
2648 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002649
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002650 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002651 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002652
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002653 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002654
2655 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002656 if (rc != -ENODATA) {
2657 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2658 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002659 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002660 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2661 return rc == -ENODATA ? 0 : rc;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002662 }
2663
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002664 *nbytes = le32_to_cpu(rsp->DataLength);
2665 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2666 (*nbytes > io_parms->length)) {
2667 cifs_dbg(FYI, "bad length %d for count %d\n",
2668 *nbytes, io_parms->length);
2669 rc = -EIO;
2670 *nbytes = 0;
2671 }
2672
2673 shdr = get_sync_hdr(rsp);
2674
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002675 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002676 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002677 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002678 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002679 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002680 if (resp_buftype == CIFS_SMALL_BUFFER)
2681 *buf_type = CIFS_SMALL_BUFFER;
2682 else if (resp_buftype == CIFS_LARGE_BUFFER)
2683 *buf_type = CIFS_LARGE_BUFFER;
2684 }
2685 return rc;
2686}
2687
Pavel Shilovsky33319142012-09-18 16:20:29 -07002688/*
2689 * Check the mid_state and signature on received buffer (if any), and queue the
2690 * workqueue completion task.
2691 */
2692static void
2693smb2_writev_callback(struct mid_q_entry *mid)
2694{
2695 struct cifs_writedata *wdata = mid->callback_data;
2696 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2697 unsigned int written;
2698 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2699 unsigned int credits_received = 1;
2700
2701 switch (mid->mid_state) {
2702 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002703 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002704 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2705 if (wdata->result != 0)
2706 break;
2707
2708 written = le32_to_cpu(rsp->DataLength);
2709 /*
2710 * Mask off high 16 bits when bytes written as returned
2711 * by the server is greater than bytes requested by the
2712 * client. OS/2 servers are known to set incorrect
2713 * CountHigh values.
2714 */
2715 if (written > wdata->bytes)
2716 written &= 0xFFFF;
2717
2718 if (written < wdata->bytes)
2719 wdata->result = -ENOSPC;
2720 else
2721 wdata->bytes = written;
2722 break;
2723 case MID_REQUEST_SUBMITTED:
2724 case MID_RETRY_NEEDED:
2725 wdata->result = -EAGAIN;
2726 break;
2727 default:
2728 wdata->result = -EIO;
2729 break;
2730 }
2731
2732 if (wdata->result)
2733 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2734
2735 queue_work(cifsiod_wq, &wdata->work);
2736 DeleteMidQEntry(mid);
2737 add_credits(tcon->ses->server, credits_received, 0);
2738}
2739
2740/* smb2_async_writev - send an async write, and set up mid to handle result */
2741int
Steve French4a5c80d2014-02-07 20:45:12 -06002742smb2_async_writev(struct cifs_writedata *wdata,
2743 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002744{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002745 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002746 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002747 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002748 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002749 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002750 struct kvec iov[2];
2751 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002752 unsigned int total_len;
2753 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002754
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002755 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002756 if (rc) {
2757 if (rc == -EAGAIN && wdata->credits) {
2758 /* credits was reset by reconnect */
2759 wdata->credits = 0;
2760 /* reduce in_flight value since we won't send the req */
2761 spin_lock(&server->req_lock);
2762 server->in_flight--;
2763 spin_unlock(&server->req_lock);
2764 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002765 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002766 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002767
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002768 if (encryption_required(tcon))
2769 flags |= CIFS_TRANSFORM_REQ;
2770
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002771 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002772 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002773
2774 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2775 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2776 req->WriteChannelInfoOffset = 0;
2777 req->WriteChannelInfoLength = 0;
2778 req->Channel = 0;
2779 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002780 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002781 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07002782 req->RemainingBytes = 0;
2783
2784 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002785 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002786 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
2787 iov[0].iov_base = &rfc1002_marker;
2788 iov[1].iov_len = total_len - 1;
2789 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002790
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002791 rqst.rq_iov = iov;
2792 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002793 rqst.rq_pages = wdata->pages;
2794 rqst.rq_npages = wdata->nr_pages;
2795 rqst.rq_pagesz = wdata->pagesz;
2796 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002797
Joe Perchesf96637b2013-05-04 22:12:25 -05002798 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2799 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002800
2801 req->Length = cpu_to_le32(wdata->bytes);
2802
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002803 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002804 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002805 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002806 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002807 spin_lock(&server->req_lock);
2808 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002809 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002810 spin_unlock(&server->req_lock);
2811 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002812 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002813 }
2814
Pavel Shilovsky33319142012-09-18 16:20:29 -07002815 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002816 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2817 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002818
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002819 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002820 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002821 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2822 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002823
Pavel Shilovsky33319142012-09-18 16:20:29 -07002824async_writev_out:
2825 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002826 return rc;
2827}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002828
2829/*
2830 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2831 * The length field from io_parms must be at least 1 and indicates a number of
2832 * elements with data to write that begins with position 1 in iov array. All
2833 * data length is specified by count.
2834 */
2835int
2836SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2837 unsigned int *nbytes, struct kvec *iov, int n_vec)
2838{
2839 int rc = 0;
2840 struct smb2_write_req *req = NULL;
2841 struct smb2_write_rsp *rsp = NULL;
2842 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002843 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002844 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002845 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002846
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002847 *nbytes = 0;
2848
2849 if (n_vec < 1)
2850 return rc;
2851
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002852 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
2853 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002854 if (rc)
2855 return rc;
2856
2857 if (io_parms->tcon->ses->server == NULL)
2858 return -ECONNABORTED;
2859
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002860 if (encryption_required(io_parms->tcon))
2861 flags |= CIFS_TRANSFORM_REQ;
2862
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002863 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002864
2865 req->PersistentFileId = io_parms->persistent_fid;
2866 req->VolatileFileId = io_parms->volatile_fid;
2867 req->WriteChannelInfoOffset = 0;
2868 req->WriteChannelInfoLength = 0;
2869 req->Channel = 0;
2870 req->Length = cpu_to_le32(io_parms->length);
2871 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002872 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002873 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002874 req->RemainingBytes = 0;
2875
2876 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002877 /* 1 for Buffer */
2878 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002879
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002880 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
2881 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002882 cifs_small_buf_release(req);
2883 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002884
2885 if (rc) {
2886 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002887 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002888 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002889 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002890
2891 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002892 return rc;
2893}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002894
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002895static unsigned int
2896num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2897{
2898 int len;
2899 unsigned int entrycount = 0;
2900 unsigned int next_offset = 0;
2901 FILE_DIRECTORY_INFO *entryptr;
2902
2903 if (bufstart == NULL)
2904 return 0;
2905
2906 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2907
2908 while (1) {
2909 entryptr = (FILE_DIRECTORY_INFO *)
2910 ((char *)entryptr + next_offset);
2911
2912 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002913 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002914 break;
2915 }
2916
2917 len = le32_to_cpu(entryptr->FileNameLength);
2918 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002919 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2920 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002921 break;
2922 }
2923
2924 *lastentry = (char *)entryptr;
2925 entrycount++;
2926
2927 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2928 if (!next_offset)
2929 break;
2930 }
2931
2932 return entrycount;
2933}
2934
2935/*
2936 * Readdir/FindFirst
2937 */
2938int
2939SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2940 u64 persistent_fid, u64 volatile_fid, int index,
2941 struct cifs_search_info *srch_inf)
2942{
2943 struct smb2_query_directory_req *req;
2944 struct smb2_query_directory_rsp *rsp = NULL;
2945 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002946 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002947 int rc = 0;
2948 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002949 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002950 unsigned char *bufptr;
2951 struct TCP_Server_Info *server;
2952 struct cifs_ses *ses = tcon->ses;
2953 __le16 asteriks = cpu_to_le16('*');
2954 char *end_of_smb;
2955 unsigned int output_size = CIFSMaxBufSize;
2956 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002957 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11002958 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002959
2960 if (ses && (ses->server))
2961 server = ses->server;
2962 else
2963 return -EIO;
2964
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11002965 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
2966 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002967 if (rc)
2968 return rc;
2969
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002970 if (encryption_required(tcon))
2971 flags |= CIFS_TRANSFORM_REQ;
2972
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002973 switch (srch_inf->info_level) {
2974 case SMB_FIND_FILE_DIRECTORY_INFO:
2975 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2976 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2977 break;
2978 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2979 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2980 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2981 break;
2982 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002983 cifs_dbg(VFS, "info level %u isn't supported\n",
2984 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002985 rc = -EINVAL;
2986 goto qdir_exit;
2987 }
2988
2989 req->FileIndex = cpu_to_le32(index);
2990 req->PersistentFileId = persistent_fid;
2991 req->VolatileFileId = volatile_fid;
2992
2993 len = 0x2;
2994 bufptr = req->Buffer;
2995 memcpy(bufptr, &asteriks, len);
2996
2997 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11002998 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002999 req->FileNameLength = cpu_to_le16(len);
3000 /*
3001 * BB could be 30 bytes or so longer if we used SMB2 specific
3002 * buffer lengths, but this is safe and close enough.
3003 */
3004 output_size = min_t(unsigned int, output_size, server->maxBuf);
3005 output_size = min_t(unsigned int, output_size, 2 << 15);
3006 req->OutputBufferLength = cpu_to_le32(output_size);
3007
3008 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003009 /* 1 for Buffer */
3010 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003011
3012 iov[1].iov_base = (char *)(req->Buffer);
3013 iov[1].iov_len = len;
3014
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003015 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003016 cifs_small_buf_release(req);
3017 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003018
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003019 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003020 if (rc == -ENODATA &&
3021 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003022 srch_inf->endOfSearch = true;
3023 rc = 0;
3024 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003025 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3026 goto qdir_exit;
3027 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003028
3029 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3030 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3031 info_buf_size);
3032 if (rc)
3033 goto qdir_exit;
3034
3035 srch_inf->unicode = true;
3036
3037 if (srch_inf->ntwrk_buf_start) {
3038 if (srch_inf->smallBuf)
3039 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3040 else
3041 cifs_buf_release(srch_inf->ntwrk_buf_start);
3042 }
3043 srch_inf->ntwrk_buf_start = (char *)rsp;
3044 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3045 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3046 /* 4 for rfc1002 length field */
3047 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3048 srch_inf->entries_in_buffer =
3049 num_entries(srch_inf->srch_entries_start, end_of_smb,
3050 &srch_inf->last_entry, info_buf_size);
3051 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003052 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3053 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3054 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003055 if (resp_buftype == CIFS_LARGE_BUFFER)
3056 srch_inf->smallBuf = false;
3057 else if (resp_buftype == CIFS_SMALL_BUFFER)
3058 srch_inf->smallBuf = true;
3059 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003060 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003061
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003062 return rc;
3063
3064qdir_exit:
3065 free_rsp_buf(resp_buftype, rsp);
3066 return rc;
3067}
3068
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003069static int
3070send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003071 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3072 u8 info_type, u32 additional_info, unsigned int num,
3073 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003074{
3075 struct smb2_set_info_req *req;
3076 struct smb2_set_info_rsp *rsp = NULL;
3077 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003078 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003079 int rc = 0;
3080 int resp_buftype;
3081 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003082 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003083 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003084 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003085
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003086 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003087 return -EIO;
3088
3089 if (!num)
3090 return -EINVAL;
3091
3092 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3093 if (!iov)
3094 return -ENOMEM;
3095
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003096 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003097 if (rc) {
3098 kfree(iov);
3099 return rc;
3100 }
3101
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003102 if (encryption_required(tcon))
3103 flags |= CIFS_TRANSFORM_REQ;
3104
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003105 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003106
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003107 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003108 req->FileInfoClass = info_class;
3109 req->PersistentFileId = persistent_fid;
3110 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003111 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003112
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003113 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003114 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003115 req->BufferLength = cpu_to_le32(*size);
3116
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003117 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003118 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003119
3120 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003121 /* 1 for Buffer */
3122 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003123
3124 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003125 le32_add_cpu(&req->BufferLength, size[i]);
3126 iov[i].iov_base = (char *)data[i];
3127 iov[i].iov_len = size[i];
3128 }
3129
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003130 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3131 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003132 cifs_small_buf_release(req);
3133 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003134
Steve French7d3fb242013-11-18 09:56:28 -06003135 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003136 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003137
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003138 free_rsp_buf(resp_buftype, rsp);
3139 kfree(iov);
3140 return rc;
3141}
3142
3143int
3144SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3145 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3146{
3147 struct smb2_file_rename_info info;
3148 void **data;
3149 unsigned int size[2];
3150 int rc;
3151 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3152
3153 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3154 if (!data)
3155 return -ENOMEM;
3156
3157 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3158 /* 0 = fail if target already exists */
3159 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3160 info.FileNameLength = cpu_to_le32(len);
3161
3162 data[0] = &info;
3163 size[0] = sizeof(struct smb2_file_rename_info);
3164
3165 data[1] = target_file;
3166 size[1] = len + 2 /* null */;
3167
3168 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003169 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3170 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003171 kfree(data);
3172 return rc;
3173}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003174
3175int
Steve French897fba12016-05-12 21:20:36 -05003176SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3177 u64 persistent_fid, u64 volatile_fid)
3178{
3179 __u8 delete_pending = 1;
3180 void *data;
3181 unsigned int size;
3182
3183 data = &delete_pending;
3184 size = 1; /* sizeof __u8 */
3185
3186 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003187 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3188 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003189}
3190
3191int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003192SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3193 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3194{
3195 struct smb2_file_link_info info;
3196 void **data;
3197 unsigned int size[2];
3198 int rc;
3199 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3200
3201 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3202 if (!data)
3203 return -ENOMEM;
3204
3205 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3206 /* 0 = fail if link already exists */
3207 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3208 info.FileNameLength = cpu_to_le32(len);
3209
3210 data[0] = &info;
3211 size[0] = sizeof(struct smb2_file_link_info);
3212
3213 data[1] = target_file;
3214 size[1] = len + 2 /* null */;
3215
3216 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003217 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3218 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003219 kfree(data);
3220 return rc;
3221}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003222
3223int
3224SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003225 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003226{
3227 struct smb2_file_eof_info info;
3228 void *data;
3229 unsigned int size;
3230
3231 info.EndOfFile = *eof;
3232
3233 data = &info;
3234 size = sizeof(struct smb2_file_eof_info);
3235
Steve Frenchf29ebb42014-07-19 21:44:58 -05003236 if (is_falloc)
3237 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003238 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3239 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003240 else
3241 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003242 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3243 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003244}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003245
3246int
3247SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3248 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3249{
3250 unsigned int size;
3251 size = sizeof(FILE_BASIC_INFO);
3252 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003253 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3254 0, 1, (void **)&buf, &size);
3255}
3256
3257int
3258SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3259 u64 persistent_fid, u64 volatile_fid,
3260 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3261{
3262 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3263 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3264 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003265}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003266
3267int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003268SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3269 u64 persistent_fid, u64 volatile_fid,
3270 struct smb2_file_full_ea_info *buf, int len)
3271{
3272 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3273 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3274 0, 1, (void **)&buf, &len);
3275}
3276
3277int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003278SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3279 const u64 persistent_fid, const u64 volatile_fid,
3280 __u8 oplock_level)
3281{
3282 int rc;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003283 struct smb2_oplock_break_req *req = NULL;
3284 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003285 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003286 unsigned int total_len;
3287 struct kvec iov[1];
3288 struct kvec rsp_iov;
3289 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003290
Joe Perchesf96637b2013-05-04 22:12:25 -05003291 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003292 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3293 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003294 if (rc)
3295 return rc;
3296
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003297 if (encryption_required(tcon))
3298 flags |= CIFS_TRANSFORM_REQ;
3299
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003300 req->VolatileFid = volatile_fid;
3301 req->PersistentFid = persistent_fid;
3302 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003303 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003304
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003305 flags |= CIFS_NO_RESP;
3306
3307 iov[0].iov_base = (char *)req;
3308 iov[0].iov_len = total_len;
3309
3310 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003311 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003312
3313 if (rc) {
3314 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003315 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003316 }
3317
3318 return rc;
3319}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003320
3321static void
3322copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3323 struct kstatfs *kst)
3324{
3325 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3326 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3327 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303328 kst->f_bfree = kst->f_bavail =
3329 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003330 return;
3331}
3332
3333static int
3334build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3335 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3336{
3337 int rc;
3338 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003339 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003340
Joe Perchesf96637b2013-05-04 22:12:25 -05003341 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003342
3343 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3344 return -EIO;
3345
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003346 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3347 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003348 if (rc)
3349 return rc;
3350
3351 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3352 req->FileInfoClass = level;
3353 req->PersistentFileId = persistent_fid;
3354 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003355 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003356 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003357 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003358 req->OutputBufferLength = cpu_to_le32(
3359 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3360
3361 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003362 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003363 return 0;
3364}
3365
3366int
3367SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3368 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3369{
3370 struct smb2_query_info_rsp *rsp = NULL;
3371 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003372 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003373 int rc = 0;
3374 int resp_buftype;
3375 struct cifs_ses *ses = tcon->ses;
3376 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003377 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003378
3379 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3380 sizeof(struct smb2_fs_full_size_info),
3381 persistent_fid, volatile_fid);
3382 if (rc)
3383 return rc;
3384
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003385 if (encryption_required(tcon))
3386 flags |= CIFS_TRANSFORM_REQ;
3387
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003388 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003389 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003390 if (rc) {
3391 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003392 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003393 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003394 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003395
3396 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3397 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3398 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3399 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3400 sizeof(struct smb2_fs_full_size_info));
3401 if (!rc)
3402 copy_fs_info_to_kstatfs(info, fsdata);
3403
Steve French34f62642013-10-09 02:07:00 -05003404qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003405 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003406 return rc;
3407}
3408
3409int
3410SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003411 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003412{
3413 struct smb2_query_info_rsp *rsp = NULL;
3414 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003415 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003416 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003417 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003418 struct cifs_ses *ses = tcon->ses;
3419 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003420 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003421
Steven French21671142013-10-09 13:36:35 -05003422 if (level == FS_DEVICE_INFORMATION) {
3423 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3424 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3425 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3426 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3427 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003428 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3429 max_len = sizeof(struct smb3_fs_ss_info);
3430 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003431 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003432 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003433 return -EINVAL;
3434 }
3435
3436 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003437 persistent_fid, volatile_fid);
3438 if (rc)
3439 return rc;
3440
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003441 if (encryption_required(tcon))
3442 flags |= CIFS_TRANSFORM_REQ;
3443
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003444 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003445 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003446 if (rc) {
3447 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3448 goto qfsattr_exit;
3449 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003450 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003451
3452 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3453 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003454 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3455 if (rc)
3456 goto qfsattr_exit;
3457
3458 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003459 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3460 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003461 rsp_len, max_len));
3462 else if (level == FS_DEVICE_INFORMATION)
3463 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3464 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003465 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3466 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3467 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3468 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3469 tcon->perf_sector_size =
3470 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3471 }
Steve French34f62642013-10-09 02:07:00 -05003472
3473qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003474 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003475 return rc;
3476}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003477
3478int
3479smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3480 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3481 const __u32 num_lock, struct smb2_lock_element *buf)
3482{
3483 int rc = 0;
3484 struct smb2_lock_req *req = NULL;
3485 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003486 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003487 int resp_buf_type;
3488 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003489 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003490 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003491
Joe Perchesf96637b2013-05-04 22:12:25 -05003492 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003493
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003494 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003495 if (rc)
3496 return rc;
3497
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003498 if (encryption_required(tcon))
3499 flags |= CIFS_TRANSFORM_REQ;
3500
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003501 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003502 req->LockCount = cpu_to_le16(num_lock);
3503
3504 req->PersistentFileId = persist_fid;
3505 req->VolatileFileId = volatile_fid;
3506
3507 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003508
3509 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003510 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003511 iov[1].iov_base = (char *)buf;
3512 iov[1].iov_len = count;
3513
3514 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003515 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3516 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003517 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003518 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003519 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003520 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3521 }
3522
3523 return rc;
3524}
3525
3526int
3527SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3528 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3529 const __u64 length, const __u64 offset, const __u32 lock_flags,
3530 const bool wait)
3531{
3532 struct smb2_lock_element lock;
3533
3534 lock.Offset = cpu_to_le64(offset);
3535 lock.Length = cpu_to_le64(length);
3536 lock.Flags = cpu_to_le32(lock_flags);
3537 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3538 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3539
3540 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3541}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003542
3543int
3544SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3545 __u8 *lease_key, const __le32 lease_state)
3546{
3547 int rc;
3548 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003549 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003550 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003551 unsigned int total_len;
3552 struct kvec iov[1];
3553 struct kvec rsp_iov;
3554 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003555
Joe Perchesf96637b2013-05-04 22:12:25 -05003556 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003557 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3558 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003559 if (rc)
3560 return rc;
3561
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003562 if (encryption_required(tcon))
3563 flags |= CIFS_TRANSFORM_REQ;
3564
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003565 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003566 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003567 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003568
3569 memcpy(req->LeaseKey, lease_key, 16);
3570 req->LeaseState = lease_state;
3571
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003572 flags |= CIFS_NO_RESP;
3573
3574 iov[0].iov_base = (char *)req;
3575 iov[0].iov_len = total_len;
3576
3577 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003578 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003579
3580 if (rc) {
3581 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003582 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003583 }
3584
3585 return rc;
3586}