blob: 4b6920de254171077f6ea3a7a6a6ac305664bd92 [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Long Lidb223a52017-11-22 17:38:45 -070051#include "smbdirect.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040052
53/*
54 * The following table defines the expected "StructureSize" of SMB2 requests
55 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
56 *
57 * Note that commands are defined in smb2pdu.h in le16 but the array below is
58 * indexed by command in host byte order.
59 */
60static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
61 /* SMB2_NEGOTIATE */ 36,
62 /* SMB2_SESSION_SETUP */ 25,
63 /* SMB2_LOGOFF */ 4,
64 /* SMB2_TREE_CONNECT */ 9,
65 /* SMB2_TREE_DISCONNECT */ 4,
66 /* SMB2_CREATE */ 57,
67 /* SMB2_CLOSE */ 24,
68 /* SMB2_FLUSH */ 24,
69 /* SMB2_READ */ 49,
70 /* SMB2_WRITE */ 49,
71 /* SMB2_LOCK */ 48,
72 /* SMB2_IOCTL */ 57,
73 /* SMB2_CANCEL */ 4,
74 /* SMB2_ECHO */ 4,
75 /* SMB2_QUERY_DIRECTORY */ 33,
76 /* SMB2_CHANGE_NOTIFY */ 32,
77 /* SMB2_QUERY_INFO */ 41,
78 /* SMB2_SET_INFO */ 33,
79 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
80};
81
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070082static int encryption_required(const struct cifs_tcon *tcon)
83{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080084 if (!tcon)
85 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070086 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
87 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
88 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080089 if (tcon->seal &&
90 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
91 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070092 return 0;
93}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040094
95static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070096smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040097 const struct cifs_tcon *tcon)
98{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070099 shdr->ProtocolId = SMB2_PROTO_NUMBER;
100 shdr->StructureSize = cpu_to_le16(64);
101 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100102 if (tcon && tcon->ses && tcon->ses->server) {
103 struct TCP_Server_Info *server = tcon->ses->server;
104
105 spin_lock(&server->req_lock);
106 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500107 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700108 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100109 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700110 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500111 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100112 server->credits, 2));
113 spin_unlock(&server->req_lock);
114 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700115 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100116 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700117 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400118
119 if (!tcon)
120 goto out;
121
Steve French2b80d042013-06-23 18:43:37 -0500122 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
123 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500124 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500125 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700126 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500127 /* else CreditCharge MBZ */
128
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700129 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400130 /* Uid is not converted */
131 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700132 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500133
134 /*
135 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
136 * to pass the path on the Open SMB prefixed by \\server\share.
137 * Not sure when we would need to do the augmented path (if ever) and
138 * setting this flag breaks the SMB2 open operation since it is
139 * illegal to send an empty path name (without \\server\share prefix)
140 * when the DFS flag is set in the SMB open header. We could
141 * consider setting the flag on all operations other than open
142 * but it is safer to net set it for now.
143 */
144/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700145 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500146
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700147 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
148 !encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700149 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400150out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400151 return;
152}
153
154static int
155smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
156{
157 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400158 struct nls_table *nls_codepage;
159 struct cifs_ses *ses;
160 struct TCP_Server_Info *server;
161
162 /*
163 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
164 * check for tcp and smb session status done differently
165 * for those three - in the calling routine.
166 */
167 if (tcon == NULL)
168 return rc;
169
170 if (smb2_command == SMB2_TREE_CONNECT)
171 return rc;
172
173 if (tcon->tidStatus == CifsExiting) {
174 /*
175 * only tree disconnect, open, and write,
176 * (and ulogoff which does not have tcon)
177 * are allowed as we start force umount.
178 */
179 if ((smb2_command != SMB2_WRITE) &&
180 (smb2_command != SMB2_CREATE) &&
181 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500182 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
183 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400184 return -ENODEV;
185 }
186 }
187 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
188 (!tcon->ses->server))
189 return -EIO;
190
191 ses = tcon->ses;
192 server = ses->server;
193
194 /*
195 * Give demultiplex thread up to 10 seconds to reconnect, should be
196 * greater than cifs socket timeout which is 7 seconds
197 */
198 while (server->tcpStatus == CifsNeedReconnect) {
199 /*
200 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
201 * here since they are implicitly done when session drops.
202 */
203 switch (smb2_command) {
204 /*
205 * BB Should we keep oplock break and add flush to exceptions?
206 */
207 case SMB2_TREE_DISCONNECT:
208 case SMB2_CANCEL:
209 case SMB2_CLOSE:
210 case SMB2_OPLOCK_BREAK:
211 return -EAGAIN;
212 }
213
214 wait_event_interruptible_timeout(server->response_q,
215 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
216
217 /* are we still trying to reconnect? */
218 if (server->tcpStatus != CifsNeedReconnect)
219 break;
220
221 /*
222 * on "soft" mounts we wait once. Hard mounts keep
223 * retrying until process is killed or server comes
224 * back on-line
225 */
226 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500227 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400228 return -EHOSTDOWN;
229 }
230 }
231
232 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
233 return rc;
234
235 nls_codepage = load_nls_default();
236
237 /*
238 * need to prevent multiple threads trying to simultaneously reconnect
239 * the same SMB session
240 */
241 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200242
243 /*
244 * Recheck after acquire mutex. If another thread is negotiating
245 * and the server never sends an answer the socket will be closed
246 * and tcpStatus set to reconnect.
247 */
248 if (server->tcpStatus == CifsNeedReconnect) {
249 rc = -EHOSTDOWN;
250 mutex_unlock(&tcon->ses->session_mutex);
251 goto out;
252 }
253
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254 rc = cifs_negotiate_protocol(0, tcon->ses);
255 if (!rc && tcon->ses->need_reconnect)
256 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
257
258 if (rc || !tcon->need_reconnect) {
259 mutex_unlock(&tcon->ses->session_mutex);
260 goto out;
261 }
262
263 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800264 if (tcon->use_persistent)
265 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500266
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400267 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
268 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500269
Joe Perchesf96637b2013-05-04 22:12:25 -0500270 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400271 if (rc)
272 goto out;
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800273
274 if (smb2_command != SMB2_INTERNAL_CMD)
275 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
276
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400277 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400278out:
279 /*
280 * Check if handle based operation so we know whether we can continue
281 * or not without returning to caller to reset file handle.
282 */
283 /*
284 * BB Is flush done by server on drop of tcp session? Should we special
285 * case it and skip above?
286 */
287 switch (smb2_command) {
288 case SMB2_FLUSH:
289 case SMB2_READ:
290 case SMB2_WRITE:
291 case SMB2_LOCK:
292 case SMB2_IOCTL:
293 case SMB2_QUERY_DIRECTORY:
294 case SMB2_CHANGE_NOTIFY:
295 case SMB2_QUERY_INFO:
296 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800297 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400298 }
299 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400300 return rc;
301}
302
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700303static void
304fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
305 unsigned int *total_len)
306{
307 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
308 /* lookup word count ie StructureSize from table */
309 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
310
311 /*
312 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
313 * largest operations (Create)
314 */
315 memset(buf, 0, 256);
316
317 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
318 spdu->StructureSize2 = cpu_to_le16(parmsize);
319
320 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
321}
322
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100323/*
324 * Allocate and return pointer to an SMB request hdr, and set basic
325 * SMB information in the SMB header. If the return code is zero, this
326 * function must have filled in request_buf pointer.
327 */
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800328static int
329smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
330 void **request_buf, unsigned int *total_len)
331{
332 int rc;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800333
334 rc = smb2_reconnect(smb2_command, tcon);
335 if (rc)
336 return rc;
337
338 /* BB eventually switch this to SMB2 specific small buf size */
339 *request_buf = cifs_small_buf_get();
340 if (*request_buf == NULL) {
341 /* BB should we add a retry in here if not a writepage? */
342 return -ENOMEM;
343 }
344
Ronnie Sahlberg305428a2017-11-21 11:04:42 +1100345 fill_small_buf(smb2_command, tcon,
346 (struct smb2_sync_hdr *)(*request_buf),
347 total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400348
349 if (tcon != NULL) {
350#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400351 uint16_t com_code = le16_to_cpu(smb2_command);
352 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400353#endif
354 cifs_stats_inc(&tcon->num_smbs_sent);
355 }
356
357 return rc;
358}
359
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500360#ifdef CONFIG_CIFS_SMB311
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100361/* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
362#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500363
364
365#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
366#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
367
368static void
369build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
370{
371 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
372 pneg_ctxt->DataLength = cpu_to_le16(38);
373 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
374 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
375 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
376 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
377}
378
379static void
380build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
381{
382 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
383 pneg_ctxt->DataLength = cpu_to_le16(6);
384 pneg_ctxt->CipherCount = cpu_to_le16(2);
385 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
386 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
387}
388
389static void
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100390assemble_neg_contexts(struct smb2_negotiate_req *req,
391 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500392{
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100393 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500394
395 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
396 /* Add 2 to size to round to 8 byte boundary */
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100397
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500398 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
399 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
400 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
401 req->NegotiateContextCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100402
403 *total_len += 4 + sizeof(struct smb2_preauth_neg_context)
404 + sizeof(struct smb2_encryption_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500405}
406#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100407static void assemble_neg_contexts(struct smb2_negotiate_req *req,
408 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500409{
410 return;
411}
412#endif /* SMB311 */
413
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400414/*
415 *
416 * SMB2 Worker functions follow:
417 *
418 * The general structure of the worker functions is:
419 * 1) Call smb2_init (assembles SMB2 header)
420 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
421 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
422 * 4) Decode SMB2 command specific fields in the fixed length area
423 * 5) Decode variable length data area (if any for this SMB2 command type)
424 * 6) Call free smb buffer
425 * 7) return
426 *
427 */
428
429int
430SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
431{
432 struct smb2_negotiate_req *req;
433 struct smb2_negotiate_rsp *rsp;
434 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700435 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400436 int rc = 0;
437 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400438 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400439 int blob_offset, blob_length;
440 char *security_blob;
441 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100442 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400443
Joe Perchesf96637b2013-05-04 22:12:25 -0500444 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400445
Jeff Layton3534b852013-05-24 07:41:01 -0400446 if (!server) {
447 WARN(1, "%s: server is NULL!\n", __func__);
448 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400449 }
450
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100451 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400452 if (rc)
453 return rc;
454
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100455 req->sync_hdr.SessionId = 0;
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100456#ifdef CONFIG_CIFS_SMB311
457 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
458 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
459#endif
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400460
Steve French9764c022017-09-17 10:41:35 -0500461 if (strcmp(ses->server->vals->version_string,
462 SMB3ANY_VERSION_STRING) == 0) {
463 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
464 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
465 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100466 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500467 } else if (strcmp(ses->server->vals->version_string,
468 SMBDEFAULT_VERSION_STRING) == 0) {
469 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
470 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
471 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
472 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100473 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500474 } else {
475 /* otherwise send specific dialect */
476 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
477 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100478 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500479 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400480
481 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400482 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500483 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400484 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500485 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400486 else
487 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400488
Steve Frenche4aa25e2012-10-01 12:26:22 -0500489 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400490
Steve French3c5f9be12014-05-13 13:37:45 -0700491 /* ClientGUID must be zero for SMB2.02 dialect */
492 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
493 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500494 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700495 memcpy(req->ClientGUID, server->client_guid,
496 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500497 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100498 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500499 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400500 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100501 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400502
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100503 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700504 cifs_small_buf_release(req);
505 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400506 /*
507 * No tcon so can't do
508 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
509 */
Steve French7e682f72017-08-31 21:34:24 -0500510 if (rc == -EOPNOTSUPP) {
511 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500512 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500513 " older servers\n");
514 goto neg_exit;
515 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400516 goto neg_exit;
517
Steve French9764c022017-09-17 10:41:35 -0500518 if (strcmp(ses->server->vals->version_string,
519 SMB3ANY_VERSION_STRING) == 0) {
520 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
521 cifs_dbg(VFS,
522 "SMB2 dialect returned but not requested\n");
523 return -EIO;
524 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
525 cifs_dbg(VFS,
526 "SMB2.1 dialect returned but not requested\n");
527 return -EIO;
528 }
529 } else if (strcmp(ses->server->vals->version_string,
530 SMBDEFAULT_VERSION_STRING) == 0) {
531 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
532 cifs_dbg(VFS,
533 "SMB2 dialect returned but not requested\n");
534 return -EIO;
535 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
536 /* ops set to 3.0 by default for default so update */
537 ses->server->ops = &smb21_operations;
538 }
Steve French590d08d2017-09-19 11:43:47 -0500539 } else if (le16_to_cpu(rsp->DialectRevision) !=
540 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500541 /* if requested single dialect ensure returned dialect matched */
542 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500543 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500544 return -EIO;
545 }
546
Joe Perchesf96637b2013-05-04 22:12:25 -0500547 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400548
Steve Frenche4aa25e2012-10-01 12:26:22 -0500549 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500550 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500551 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500552 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500553 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500554 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500555 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
556 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600557#ifdef CONFIG_CIFS_SMB311
558 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
559 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
560#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400561 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500562 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500563 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400564 rc = -EIO;
565 goto neg_exit;
566 }
567 server->dialect = le16_to_cpu(rsp->DialectRevision);
568
Steve French9764c022017-09-17 10:41:35 -0500569 /* BB: add check that dialect was valid given dialect(s) we asked for */
570
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100571#ifdef CONFIG_CIFS_SMB311
572 /*
573 * Keep a copy of the hash after negprot. This hash will be
574 * the starting hash value for all sessions made from this
575 * server.
576 */
577 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
578 SMB2_PREAUTH_HASH_SIZE);
579#endif
Jeff Laytone598d1d82013-05-26 07:00:59 -0400580 /* SMB2 only has an extended negflavor */
581 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400582 /* set it to the maximum buffer size value we can send with 1 credit */
583 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
584 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400585 server->max_read = le32_to_cpu(rsp->MaxReadSize);
586 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
587 /* BB Do we need to validate the SecurityMode? */
588 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
589 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400590 /* Internal types */
591 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400592
593 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
594 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500595 /*
596 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
597 * for us will be
598 * ses->sectype = RawNTLMSSP;
599 * but for time being this is our only auth choice so doesn't matter.
600 * We just found a server which sets blob length to zero expecting raw.
601 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700602 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500603 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700604 server->sec_ntlmssp = true;
605 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700606
Jeff Layton38d77c52013-05-26 07:01:00 -0400607 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400608 if (rc)
609 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500610 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500611 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500612 if (rc == 1)
613 rc = 0;
614 else if (rc == 0)
615 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400616 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400617neg_exit:
618 free_rsp_buf(resp_buftype, rsp);
619 return rc;
620}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400621
Steve Frenchff1c0382013-11-19 23:44:46 -0600622int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
623{
624 int rc = 0;
625 struct validate_negotiate_info_req vneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200626 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600627 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500628 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600629
630 cifs_dbg(FYI, "validate negotiate\n");
631
Long Li8801e902017-11-22 17:38:49 -0700632#ifdef CONFIG_CIFS_SMB_DIRECT
633 if (tcon->ses->server->rdma)
634 return 0;
635#endif
636
Aurelien Aptel8bd68c62018-02-16 19:19:29 +0100637 /* In SMB3.11 preauth integrity supersedes validate negotiate */
638 if (tcon->ses->server->dialect == SMB311_PROT_ID)
639 return 0;
640
Steve Frenchff1c0382013-11-19 23:44:46 -0600641 /*
642 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500643 * can not sign it (ie are not known user). Even if signing is not
644 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600645 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500646 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600647 */
Steve French0603c962017-09-20 19:57:18 -0500648 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600649 return 0; /* validation requires signing */
650
Steve French0603c962017-09-20 19:57:18 -0500651 if (tcon->ses->user_name == NULL) {
652 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
653 return 0; /* validation requires signing */
654 }
655
656 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
657 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
658
Steve Frenchff1c0382013-11-19 23:44:46 -0600659 vneg_inbuf.Capabilities =
660 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100661 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
662 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600663
664 if (tcon->ses->sign)
665 vneg_inbuf.SecurityMode =
666 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
667 else if (global_secflags & CIFSSEC_MAY_SIGN)
668 vneg_inbuf.SecurityMode =
669 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
670 else
671 vneg_inbuf.SecurityMode = 0;
672
Steve French9764c022017-09-17 10:41:35 -0500673
674 if (strcmp(tcon->ses->server->vals->version_string,
675 SMB3ANY_VERSION_STRING) == 0) {
676 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
677 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
678 vneg_inbuf.DialectCount = cpu_to_le16(2);
679 /* structure is big enough for 3 dialects, sending only 2 */
680 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
681 } else if (strcmp(tcon->ses->server->vals->version_string,
682 SMBDEFAULT_VERSION_STRING) == 0) {
683 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
684 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
685 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
686 vneg_inbuf.DialectCount = cpu_to_le16(3);
687 /* structure is big enough for 3 dialects */
688 inbuflen = sizeof(struct validate_negotiate_info_req);
689 } else {
690 /* otherwise specific dialect was requested */
691 vneg_inbuf.Dialects[0] =
692 cpu_to_le16(tcon->ses->server->vals->protocol_id);
693 vneg_inbuf.DialectCount = cpu_to_le16(1);
694 /* structure is big enough for 3 dialects, sending only 1 */
695 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
696 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600697
698 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
699 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
700 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
701 (char **)&pneg_rsp, &rsplen);
702
703 if (rc != 0) {
704 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
705 return -EIO;
706 }
707
708 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500709 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
710 rsplen);
711
712 /* relax check since Mac returns max bufsize allowed on ioctl */
David Disseldorpa2d9daa2017-10-20 14:49:38 +0200713 if ((rsplen > CIFSMaxBufSize)
714 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200715 goto err_rsp_free;
Steve Frenchff1c0382013-11-19 23:44:46 -0600716 }
717
718 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100719 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600720 goto vneg_out;
721
722 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
723 goto vneg_out;
724
725 /* do not validate server guid because not saved at negprot time yet */
726
727 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
728 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
729 goto vneg_out;
730
731 /* validate negotiate successful */
732 cifs_dbg(FYI, "validate negotiate info successful\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200733 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600734 return 0;
735
736vneg_out:
737 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200738err_rsp_free:
739 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600740 return -EIO;
741}
742
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530743enum securityEnum
744smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
745{
746 switch (requested) {
747 case Kerberos:
748 case RawNTLMSSP:
749 return requested;
750 case NTLMv2:
751 return RawNTLMSSP;
752 case Unspecified:
753 if (server->sec_ntlmssp &&
754 (global_secflags & CIFSSEC_MAY_NTLMSSP))
755 return RawNTLMSSP;
756 if ((server->sec_kerberos || server->sec_mskerberos) &&
757 (global_secflags & CIFSSEC_MAY_KRB5))
758 return Kerberos;
759 /* Fallthrough */
760 default:
761 return Unspecified;
762 }
763}
764
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100765struct SMB2_sess_data {
766 unsigned int xid;
767 struct cifs_ses *ses;
768 struct nls_table *nls_cp;
769 void (*func)(struct SMB2_sess_data *);
770 int result;
771 u64 previous_session;
772
773 /* we will send the SMB in three pieces:
774 * a fixed length beginning part, an optional
775 * SPNEGO blob (which can be zero length), and a
776 * last part which will include the strings
777 * and rest of bcc area. This allows us to avoid
778 * a large buffer 17K allocation
779 */
780 int buf0_type;
781 struct kvec iov[2];
782};
783
784static int
785SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
786{
787 int rc;
788 struct cifs_ses *ses = sess_data->ses;
789 struct smb2_sess_setup_req *req;
790 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100791 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100792
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100793 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
794 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100795 if (rc)
796 return rc;
797
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700798 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100799 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100800
801 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
802 req->PreviousSessionId = sess_data->previous_session;
803
804 req->Flags = 0; /* MBZ */
805 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100806 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100807
808 /* only one of SMB2 signing flags may be set in SMB2 request */
809 if (server->sign)
810 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
811 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
812 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
813 else
814 req->SecurityMode = 0;
815
816 req->Capabilities = 0;
817 req->Channel = 0; /* MBZ */
818
819 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100820 /* 1 for pad */
821 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100822 /*
823 * This variable will be used to clear the buffer
824 * allocated above in case of any error in the calling function.
825 */
826 sess_data->buf0_type = CIFS_SMALL_BUFFER;
827
828 return 0;
829}
830
831static void
832SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
833{
834 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
835 sess_data->buf0_type = CIFS_NO_BUFFER;
836}
837
838static int
839SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
840{
841 int rc;
842 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700843 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100844
845 /* Testing shows that buffer offset must be at location of Buffer[0] */
846 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100847 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100848 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
849
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100850 /* BB add code to build os and lm fields */
851
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100852 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
853 sess_data->iov, 2,
854 &sess_data->buf0_type,
855 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700856 cifs_small_buf_release(sess_data->iov[0].iov_base);
857 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100858
859 return rc;
860}
861
862static int
863SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
864{
865 int rc = 0;
866 struct cifs_ses *ses = sess_data->ses;
867
868 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800869 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100870 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100871 if (rc) {
872 cifs_dbg(FYI,
873 "SMB3 session key generation failed\n");
874 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800875 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100876 }
877 }
878 if (!ses->server->session_estab) {
879 ses->server->sequence_number = 0x2;
880 ses->server->session_estab = true;
881 }
882 mutex_unlock(&ses->server->srv_mutex);
883
884 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
885 spin_lock(&GlobalMid_Lock);
886 ses->status = CifsGood;
887 ses->need_reconnect = false;
888 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100889 return rc;
890}
891
892#ifdef CONFIG_CIFS_UPCALL
893static void
894SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
895{
896 int rc;
897 struct cifs_ses *ses = sess_data->ses;
898 struct cifs_spnego_msg *msg;
899 struct key *spnego_key = NULL;
900 struct smb2_sess_setup_rsp *rsp = NULL;
901
902 rc = SMB2_sess_alloc_buffer(sess_data);
903 if (rc)
904 goto out;
905
906 spnego_key = cifs_get_spnego_key(ses);
907 if (IS_ERR(spnego_key)) {
908 rc = PTR_ERR(spnego_key);
909 spnego_key = NULL;
910 goto out;
911 }
912
913 msg = spnego_key->payload.data[0];
914 /*
915 * check version field to make sure that cifs.upcall is
916 * sending us a response in an expected form
917 */
918 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
919 cifs_dbg(VFS,
920 "bad cifs.upcall version. Expected %d got %d",
921 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
922 rc = -EKEYREJECTED;
923 goto out_put_spnego_key;
924 }
925
926 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
927 GFP_KERNEL);
928 if (!ses->auth_key.response) {
929 cifs_dbg(VFS,
930 "Kerberos can't allocate (%u bytes) memory",
931 msg->sesskey_len);
932 rc = -ENOMEM;
933 goto out_put_spnego_key;
934 }
935 ses->auth_key.len = msg->sesskey_len;
936
937 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
938 sess_data->iov[1].iov_len = msg->secblob_len;
939
940 rc = SMB2_sess_sendreceive(sess_data);
941 if (rc)
942 goto out_put_spnego_key;
943
944 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700945 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100946
947 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100948
949 rc = SMB2_sess_establish_session(sess_data);
950out_put_spnego_key:
951 key_invalidate(spnego_key);
952 key_put(spnego_key);
953out:
954 sess_data->result = rc;
955 sess_data->func = NULL;
956 SMB2_sess_free_buffer(sess_data);
957}
958#else
959static void
960SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
961{
962 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
963 sess_data->result = -EOPNOTSUPP;
964 sess_data->func = NULL;
965}
966#endif
967
Sachin Prabhu166cea42016-10-07 19:11:22 +0100968static void
969SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
970
971static void
972SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
973{
974 int rc;
975 struct cifs_ses *ses = sess_data->ses;
976 struct smb2_sess_setup_rsp *rsp = NULL;
977 char *ntlmssp_blob = NULL;
978 bool use_spnego = false; /* else use raw ntlmssp */
979 u16 blob_length = 0;
980
981 /*
982 * If memory allocation is successful, caller of this function
983 * frees it.
984 */
985 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
986 if (!ses->ntlmssp) {
987 rc = -ENOMEM;
988 goto out_err;
989 }
990 ses->ntlmssp->sesskey_per_smbsess = true;
991
992 rc = SMB2_sess_alloc_buffer(sess_data);
993 if (rc)
994 goto out_err;
995
996 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
997 GFP_KERNEL);
998 if (ntlmssp_blob == NULL) {
999 rc = -ENOMEM;
1000 goto out;
1001 }
1002
1003 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1004 if (use_spnego) {
1005 /* BB eventually need to add this */
1006 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1007 rc = -EOPNOTSUPP;
1008 goto out;
1009 } else {
1010 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1011 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1012 }
1013 sess_data->iov[1].iov_base = ntlmssp_blob;
1014 sess_data->iov[1].iov_len = blob_length;
1015
1016 rc = SMB2_sess_sendreceive(sess_data);
1017 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1018
1019 /* If true, rc here is expected and not an error */
1020 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001021 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001022 rc = 0;
1023
1024 if (rc)
1025 goto out;
1026
1027 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1028 le16_to_cpu(rsp->SecurityBufferOffset)) {
1029 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1030 le16_to_cpu(rsp->SecurityBufferOffset));
1031 rc = -EIO;
1032 goto out;
1033 }
1034 rc = decode_ntlmssp_challenge(rsp->Buffer,
1035 le16_to_cpu(rsp->SecurityBufferLength), ses);
1036 if (rc)
1037 goto out;
1038
1039 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1040
1041
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001042 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001043 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001044
1045out:
1046 kfree(ntlmssp_blob);
1047 SMB2_sess_free_buffer(sess_data);
1048 if (!rc) {
1049 sess_data->result = 0;
1050 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1051 return;
1052 }
1053out_err:
1054 kfree(ses->ntlmssp);
1055 ses->ntlmssp = NULL;
1056 sess_data->result = rc;
1057 sess_data->func = NULL;
1058}
1059
1060static void
1061SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1062{
1063 int rc;
1064 struct cifs_ses *ses = sess_data->ses;
1065 struct smb2_sess_setup_req *req;
1066 struct smb2_sess_setup_rsp *rsp = NULL;
1067 unsigned char *ntlmssp_blob = NULL;
1068 bool use_spnego = false; /* else use raw ntlmssp */
1069 u16 blob_length = 0;
1070
1071 rc = SMB2_sess_alloc_buffer(sess_data);
1072 if (rc)
1073 goto out;
1074
1075 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001076 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001077
1078 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1079 sess_data->nls_cp);
1080 if (rc) {
1081 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1082 goto out;
1083 }
1084
1085 if (use_spnego) {
1086 /* BB eventually need to add this */
1087 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1088 rc = -EOPNOTSUPP;
1089 goto out;
1090 }
1091 sess_data->iov[1].iov_base = ntlmssp_blob;
1092 sess_data->iov[1].iov_len = blob_length;
1093
1094 rc = SMB2_sess_sendreceive(sess_data);
1095 if (rc)
1096 goto out;
1097
1098 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1099
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001100 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001101 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001102
1103 rc = SMB2_sess_establish_session(sess_data);
1104out:
1105 kfree(ntlmssp_blob);
1106 SMB2_sess_free_buffer(sess_data);
1107 kfree(ses->ntlmssp);
1108 ses->ntlmssp = NULL;
1109 sess_data->result = rc;
1110 sess_data->func = NULL;
1111}
1112
1113static int
1114SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1115{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301116 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001117
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301118 type = smb2_select_sectype(ses->server, ses->sectype);
1119 cifs_dbg(FYI, "sess setup type %d\n", type);
1120 if (type == Unspecified) {
1121 cifs_dbg(VFS,
1122 "Unable to select appropriate authentication method!");
1123 return -EINVAL;
1124 }
1125
1126 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001127 case Kerberos:
1128 sess_data->func = SMB2_auth_kerberos;
1129 break;
1130 case RawNTLMSSP:
1131 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1132 break;
1133 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301134 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001135 return -EOPNOTSUPP;
1136 }
1137
1138 return 0;
1139}
1140
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001141int
1142SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1143 const struct nls_table *nls_cp)
1144{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001145 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001146 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001147 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001148
Joe Perchesf96637b2013-05-04 22:12:25 -05001149 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001150
Jeff Layton3534b852013-05-24 07:41:01 -04001151 if (!server) {
1152 WARN(1, "%s: server is NULL!\n", __func__);
1153 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001154 }
1155
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001156 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1157 if (!sess_data)
1158 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001159
1160 rc = SMB2_select_sec(ses, sess_data);
1161 if (rc)
1162 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001163 sess_data->xid = xid;
1164 sess_data->ses = ses;
1165 sess_data->buf0_type = CIFS_NO_BUFFER;
1166 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001167
Aurelien Aptel8bd68c62018-02-16 19:19:29 +01001168#ifdef CONFIG_CIFS_SMB311
1169 /*
1170 * Initialize the session hash with the server one.
1171 */
1172 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1173 SMB2_PREAUTH_HASH_SIZE);
1174#endif
1175
Sachin Prabhu166cea42016-10-07 19:11:22 +01001176 while (sess_data->func)
1177 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001178
Steve Frenchc721c382017-09-19 18:40:03 -05001179 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1180 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001181 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001182out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001183 kfree(sess_data);
1184 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001185}
1186
1187int
1188SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1189{
1190 struct smb2_logoff_req *req; /* response is also trivial struct */
1191 int rc = 0;
1192 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001193 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001194 unsigned int total_len;
1195 struct kvec iov[1];
1196 struct kvec rsp_iov;
1197 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001198
Joe Perchesf96637b2013-05-04 22:12:25 -05001199 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001200
1201 if (ses && (ses->server))
1202 server = ses->server;
1203 else
1204 return -EIO;
1205
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001206 /* no need to send SMB logoff if uid already closed due to reconnect */
1207 if (ses->need_reconnect)
1208 goto smb2_session_already_dead;
1209
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001210 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001211 if (rc)
1212 return rc;
1213
1214 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001215 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001216
1217 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1218 flags |= CIFS_TRANSFORM_REQ;
1219 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001220 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001221
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001222 flags |= CIFS_NO_RESP;
1223
1224 iov[0].iov_base = (char *)req;
1225 iov[0].iov_len = total_len;
1226
1227 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001228 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001229 /*
1230 * No tcon so can't do
1231 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1232 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001233
1234smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001235 return rc;
1236}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001237
1238static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1239{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001240 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001241}
1242
1243#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1244
Steve Frenchde9f68df2013-11-15 11:26:24 -06001245/* These are similar values to what Windows uses */
1246static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1247{
1248 tcon->max_chunks = 256;
1249 tcon->max_bytes_chunk = 1048576;
1250 tcon->max_bytes_copy = 16777216;
1251}
1252
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001253int
1254SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1255 struct cifs_tcon *tcon, const struct nls_table *cp)
1256{
1257 struct smb2_tree_connect_req *req;
1258 struct smb2_tree_connect_rsp *rsp = NULL;
1259 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001260 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001261 int rc = 0;
1262 int resp_buftype;
1263 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001264 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001265 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001266 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001267
Joe Perchesf96637b2013-05-04 22:12:25 -05001268 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001269
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001270 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001271 return -EIO;
1272
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001273 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1274 if (unc_path == NULL)
1275 return -ENOMEM;
1276
1277 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1278 unc_path_len *= 2;
1279 if (unc_path_len < 2) {
1280 kfree(unc_path);
1281 return -EINVAL;
1282 }
1283
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001284 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001285 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001286
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001287 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1288 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001289 if (rc) {
1290 kfree(unc_path);
1291 return rc;
1292 }
1293
Aurelien Aptelb327a712018-01-24 13:46:10 +01001294 if (encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001295 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001296
1297 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001298 /* 1 for pad */
1299 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001300
1301 /* Testing shows that buffer offset must be at location of Buffer[0] */
1302 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001303 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001304 req->PathLength = cpu_to_le16(unc_path_len - 2);
1305 iov[1].iov_base = unc_path;
1306 iov[1].iov_len = unc_path_len;
1307
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001308 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001309 cifs_small_buf_release(req);
1310 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001311
1312 if (rc != 0) {
1313 if (tcon) {
1314 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1315 tcon->need_reconnect = true;
1316 }
1317 goto tcon_error_exit;
1318 }
1319
Christophe JAILLETcd123002017-05-12 17:59:32 +02001320 switch (rsp->ShareType) {
1321 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001322 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001323 break;
1324 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001325 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001326 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001327 break;
1328 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001329 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001330 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001331 break;
1332 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001333 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001334 rc = -EOPNOTSUPP;
1335 goto tcon_error_exit;
1336 }
1337
1338 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001339 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001340 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1341 tcon->tidStatus = CifsGood;
1342 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001343 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001344 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001345
1346 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1347 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001348 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001349
1350 if (tcon->seal &&
1351 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1352 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1353
Steve Frenchde9f68df2013-11-15 11:26:24 -06001354 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001355 if (tcon->ses->server->ops->validate_negotiate)
1356 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001357tcon_exit:
1358 free_rsp_buf(resp_buftype, rsp);
1359 kfree(unc_path);
1360 return rc;
1361
1362tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001363 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001364 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001365 }
1366 goto tcon_exit;
1367}
1368
1369int
1370SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1371{
1372 struct smb2_tree_disconnect_req *req; /* response is trivial */
1373 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001374 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001375 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001376 unsigned int total_len;
1377 struct kvec iov[1];
1378 struct kvec rsp_iov;
1379 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001380
Joe Perchesf96637b2013-05-04 22:12:25 -05001381 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001382
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001383 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001384 return -EIO;
1385
1386 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1387 return 0;
1388
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001389 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1390 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001391 if (rc)
1392 return rc;
1393
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001394 if (encryption_required(tcon))
1395 flags |= CIFS_TRANSFORM_REQ;
1396
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001397 flags |= CIFS_NO_RESP;
1398
1399 iov[0].iov_base = (char *)req;
1400 iov[0].iov_len = total_len;
1401
1402 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001403 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001404 if (rc)
1405 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1406
1407 return rc;
1408}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001409
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001410
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001411static struct create_durable *
1412create_durable_buf(void)
1413{
1414 struct create_durable *buf;
1415
1416 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1417 if (!buf)
1418 return NULL;
1419
1420 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001421 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001422 buf->ccontext.DataLength = cpu_to_le32(16);
1423 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1424 (struct create_durable, Name));
1425 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001426 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001427 buf->Name[0] = 'D';
1428 buf->Name[1] = 'H';
1429 buf->Name[2] = 'n';
1430 buf->Name[3] = 'Q';
1431 return buf;
1432}
1433
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001434static struct create_durable *
1435create_reconnect_durable_buf(struct cifs_fid *fid)
1436{
1437 struct create_durable *buf;
1438
1439 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1440 if (!buf)
1441 return NULL;
1442
1443 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1444 (struct create_durable, Data));
1445 buf->ccontext.DataLength = cpu_to_le32(16);
1446 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1447 (struct create_durable, Name));
1448 buf->ccontext.NameLength = cpu_to_le16(4);
1449 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1450 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001451 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001452 buf->Name[0] = 'D';
1453 buf->Name[1] = 'H';
1454 buf->Name[2] = 'n';
1455 buf->Name[3] = 'C';
1456 return buf;
1457}
1458
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001459static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001460parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1461 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001462{
1463 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001464 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001465 unsigned int next;
1466 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001467 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001468
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001469 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001470 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001471 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001472 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001473 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001474 if (le16_to_cpu(cc->NameLength) == 4 &&
1475 strncmp(name, "RqLs", 4) == 0)
1476 return server->ops->parse_lease_buf(cc, epoch);
1477
1478 next = le32_to_cpu(cc->Next);
1479 if (!next)
1480 break;
1481 remaining -= next;
1482 cc = (struct create_context *)((char *)cc + next);
1483 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001484
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001485 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001486}
1487
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001488static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001489add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1490 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001491{
1492 struct smb2_create_req *req = iov[0].iov_base;
1493 unsigned int num = *num_iovec;
1494
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001495 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001496 if (iov[num].iov_base == NULL)
1497 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001498 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001499 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1500 if (!req->CreateContextsOffset)
1501 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001502 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001503 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001504 le32_add_cpu(&req->CreateContextsLength,
1505 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001506 *num_iovec = num + 1;
1507 return 0;
1508}
1509
Steve Frenchb56eae42015-11-03 09:26:27 -06001510static struct create_durable_v2 *
1511create_durable_v2_buf(struct cifs_fid *pfid)
1512{
1513 struct create_durable_v2 *buf;
1514
1515 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1516 if (!buf)
1517 return NULL;
1518
1519 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1520 (struct create_durable_v2, dcontext));
1521 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1522 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1523 (struct create_durable_v2, Name));
1524 buf->ccontext.NameLength = cpu_to_le16(4);
1525
1526 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1527 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001528 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001529 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1530
1531 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1532 buf->Name[0] = 'D';
1533 buf->Name[1] = 'H';
1534 buf->Name[2] = '2';
1535 buf->Name[3] = 'Q';
1536 return buf;
1537}
1538
1539static struct create_durable_handle_reconnect_v2 *
1540create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1541{
1542 struct create_durable_handle_reconnect_v2 *buf;
1543
1544 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1545 GFP_KERNEL);
1546 if (!buf)
1547 return NULL;
1548
1549 buf->ccontext.DataOffset =
1550 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1551 dcontext));
1552 buf->ccontext.DataLength =
1553 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1554 buf->ccontext.NameOffset =
1555 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1556 Name));
1557 buf->ccontext.NameLength = cpu_to_le16(4);
1558
1559 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1560 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1561 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1562 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1563
1564 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1565 buf->Name[0] = 'D';
1566 buf->Name[1] = 'H';
1567 buf->Name[2] = '2';
1568 buf->Name[3] = 'C';
1569 return buf;
1570}
1571
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001572static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001573add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001574 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001575{
1576 struct smb2_create_req *req = iov[0].iov_base;
1577 unsigned int num = *num_iovec;
1578
Steve Frenchb56eae42015-11-03 09:26:27 -06001579 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1580 if (iov[num].iov_base == NULL)
1581 return -ENOMEM;
1582 iov[num].iov_len = sizeof(struct create_durable_v2);
1583 if (!req->CreateContextsOffset)
1584 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001585 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001586 iov[1].iov_len);
1587 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001588 *num_iovec = num + 1;
1589 return 0;
1590}
1591
1592static int
1593add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1594 struct cifs_open_parms *oparms)
1595{
1596 struct smb2_create_req *req = iov[0].iov_base;
1597 unsigned int num = *num_iovec;
1598
1599 /* indicate that we don't need to relock the file */
1600 oparms->reconnect = false;
1601
1602 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1603 if (iov[num].iov_base == NULL)
1604 return -ENOMEM;
1605 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1606 if (!req->CreateContextsOffset)
1607 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001608 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001609 iov[1].iov_len);
1610 le32_add_cpu(&req->CreateContextsLength,
1611 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001612 *num_iovec = num + 1;
1613 return 0;
1614}
1615
1616static int
1617add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1618 struct cifs_open_parms *oparms, bool use_persistent)
1619{
1620 struct smb2_create_req *req = iov[0].iov_base;
1621 unsigned int num = *num_iovec;
1622
1623 if (use_persistent) {
1624 if (oparms->reconnect)
1625 return add_durable_reconnect_v2_context(iov, num_iovec,
1626 oparms);
1627 else
1628 return add_durable_v2_context(iov, num_iovec, oparms);
1629 }
1630
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001631 if (oparms->reconnect) {
1632 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1633 /* indicate that we don't need to relock the file */
1634 oparms->reconnect = false;
1635 } else
1636 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001637 if (iov[num].iov_base == NULL)
1638 return -ENOMEM;
1639 iov[num].iov_len = sizeof(struct create_durable);
1640 if (!req->CreateContextsOffset)
1641 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001642 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001643 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001644 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001645 *num_iovec = num + 1;
1646 return 0;
1647}
1648
Aurelien Aptelf0712922017-02-22 14:47:17 +01001649static int
1650alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1651 const char *treename, const __le16 *path)
1652{
1653 int treename_len, path_len;
1654 struct nls_table *cp;
1655 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1656
1657 /*
1658 * skip leading "\\"
1659 */
1660 treename_len = strlen(treename);
1661 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1662 return -EINVAL;
1663
1664 treename += 2;
1665 treename_len -= 2;
1666
1667 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1668
1669 /*
1670 * make room for one path separator between the treename and
1671 * path
1672 */
1673 *out_len = treename_len + 1 + path_len;
1674
1675 /*
1676 * final path needs to be null-terminated UTF16 with a
1677 * size aligned to 8
1678 */
1679
1680 *out_size = roundup((*out_len+1)*2, 8);
1681 *out_path = kzalloc(*out_size, GFP_KERNEL);
1682 if (!*out_path)
1683 return -ENOMEM;
1684
1685 cp = load_nls_default();
1686 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1687 UniStrcat(*out_path, sep);
1688 UniStrcat(*out_path, path);
1689 unload_nls(cp);
1690
1691 return 0;
1692}
1693
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001694int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001695SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001696 __u8 *oplock, struct smb2_file_all_info *buf,
1697 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001698{
1699 struct smb2_create_req *req;
1700 struct smb2_create_rsp *rsp;
1701 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001702 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001703 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001704 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001705 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001706 int resp_buftype;
1707 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001708 __le16 *copy_path = NULL;
1709 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001710 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001711 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001712 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001713 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001714 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001715 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001716
Joe Perchesf96637b2013-05-04 22:12:25 -05001717 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001718
1719 if (ses && (ses->server))
1720 server = ses->server;
1721 else
1722 return -EIO;
1723
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001724 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1725
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001726 if (rc)
1727 return rc;
1728
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001729 if (encryption_required(tcon))
1730 flags |= CIFS_TRANSFORM_REQ;
1731
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001732 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001733 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001734 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1735 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001736
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001737 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001738 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001739 /* File attributes ignored on open (used in create though) */
1740 req->FileAttributes = cpu_to_le32(file_attributes);
1741 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001742 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1743 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001744
1745 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001746 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001747 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001748
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001749 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001750
1751 /* [MS-SMB2] 2.2.13 NameOffset:
1752 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1753 * the SMB2 header, the file name includes a prefix that will
1754 * be processed during DFS name normalization as specified in
1755 * section 3.3.5.9. Otherwise, the file name is relative to
1756 * the share that is identified by the TreeId in the SMB2
1757 * header.
1758 */
1759 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1760 int name_len;
1761
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001762 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001763 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1764 &name_len,
1765 tcon->treeName, path);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001766 if (rc) {
1767 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001768 return rc;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001769 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001770 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001771 uni_path_len = copy_size;
1772 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001773 } else {
1774 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1775 /* MUST set path len (NameLength) to 0 opening root of share */
1776 req->NameLength = cpu_to_le16(uni_path_len - 2);
1777 if (uni_path_len % 8 != 0) {
1778 copy_size = roundup(uni_path_len, 8);
1779 copy_path = kzalloc(copy_size, GFP_KERNEL);
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001780 if (!copy_path) {
1781 cifs_small_buf_release(req);
Aurelien Aptelf0712922017-02-22 14:47:17 +01001782 return -ENOMEM;
Ronnie Sahlbergb7a73c82018-02-13 15:42:30 +11001783 }
Aurelien Aptelf0712922017-02-22 14:47:17 +01001784 memcpy((char *)copy_path, (const char *)path,
1785 uni_path_len);
1786 uni_path_len = copy_size;
1787 path = copy_path;
1788 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001789 }
1790
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001791 iov[1].iov_len = uni_path_len;
1792 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001793
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001794 if (!server->oplocks)
1795 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1796
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001797 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001798 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1799 req->RequestedOplockLevel = *oplock;
1800 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001801 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001802 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001803 cifs_small_buf_release(req);
1804 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001805 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001806 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001807 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001808 }
1809
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001810 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1811 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001812 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001813 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001814 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001815 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001816 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001817 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001818
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001819 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001820 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001821 if (rc) {
1822 cifs_small_buf_release(req);
1823 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001824 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001825 return rc;
1826 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001827 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001828 }
1829
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001830 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1831 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001832 cifs_small_buf_release(req);
1833 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001834
1835 if (rc != 0) {
1836 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001837 if (err_buf && rsp)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001838 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1839 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001840 goto creat_exit;
1841 }
1842
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001843 oparms->fid->persistent_fid = rsp->PersistentFileId;
1844 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001845
1846 if (buf) {
1847 memcpy(buf, &rsp->CreationTime, 32);
1848 buf->AllocationSize = rsp->AllocationSize;
1849 buf->EndOfFile = rsp->EndofFile;
1850 buf->Attributes = rsp->FileAttributes;
1851 buf->NumberOfLinks = cpu_to_le32(1);
1852 buf->DeletePending = 0;
1853 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001854
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001855 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001856 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001857 else
1858 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001859creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001860 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001861 kfree(lc_buf);
1862 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001863 free_rsp_buf(resp_buftype, rsp);
1864 return rc;
1865}
1866
Steve French4a72daf2013-06-25 00:20:49 -05001867/*
1868 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1869 */
1870int
1871SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001872 u64 volatile_fid, u32 opcode, bool is_fsctl,
Aurelien Aptel51146622017-02-28 15:08:41 +01001873 char *in_data, u32 indatalen,
1874 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001875{
1876 struct smb2_ioctl_req *req;
1877 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001878 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001879 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001880 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001881 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001882 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001883 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001884 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001885 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001886 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05001887
1888 cifs_dbg(FYI, "SMB2 IOCTL\n");
1889
Steve French3d1a3742014-08-11 21:05:25 -05001890 if (out_data != NULL)
1891 *out_data = NULL;
1892
Steve French4a72daf2013-06-25 00:20:49 -05001893 /* zero out returned data len, in case of error */
1894 if (plen)
1895 *plen = 0;
1896
Steve French8e353102015-03-26 19:47:02 -05001897 if (tcon)
1898 ses = tcon->ses;
1899 else
1900 return -EIO;
1901
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001902 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05001903 return -EIO;
1904
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001905 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05001906 if (rc)
1907 return rc;
1908
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001909 if (encryption_required(tcon))
1910 flags |= CIFS_TRANSFORM_REQ;
1911
Steve French4a72daf2013-06-25 00:20:49 -05001912 req->CtlCode = cpu_to_le32(opcode);
1913 req->PersistentFileId = persistent_fid;
1914 req->VolatileFileId = volatile_fid;
1915
1916 if (indatalen) {
1917 req->InputCount = cpu_to_le32(indatalen);
1918 /* do not set InputOffset if no input data */
1919 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001920 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05001921 iov[1].iov_base = in_data;
1922 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001923 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001924 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001925 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001926
1927 req->OutputOffset = 0;
1928 req->OutputCount = 0; /* MBZ */
1929
1930 /*
1931 * Could increase MaxOutputResponse, but that would require more
1932 * than one credit. Windows typically sets this smaller, but for some
1933 * ioctls it may be useful to allow server to send more. No point
1934 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001935 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1936 * (by default, note that it can be overridden to make max larger)
1937 * in responses (except for read responses which can be bigger.
1938 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001939 */
Steve French7db0a6e2017-05-03 21:12:20 -05001940 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001941
1942 if (is_fsctl)
1943 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1944 else
1945 req->Flags = 0;
1946
1947 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001948
Steve French7ff8d452013-10-14 00:44:19 -05001949 /*
1950 * If no input data, the size of ioctl struct in
1951 * protocol spec still includes a 1 byte data buffer,
1952 * but if input data passed to ioctl, we do not
1953 * want to double count this, so we do not send
1954 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001955 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05001956 */
1957
1958 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001959 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05001960 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001961 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05001962
Steve French4587eee2017-10-25 15:58:31 -05001963 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1964 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001965 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05001966
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001967 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1968 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001969 cifs_small_buf_release(req);
1970 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001971
Steve French9bf0c9c2013-11-16 18:05:28 -06001972 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001973 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001974 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001975 } else if (rc == -EINVAL) {
1976 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1977 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001978 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001979 goto ioctl_exit;
1980 }
Steve French4a72daf2013-06-25 00:20:49 -05001981 }
1982
1983 /* check if caller wants to look at return data or just return rc */
1984 if ((plen == NULL) || (out_data == NULL))
1985 goto ioctl_exit;
1986
1987 *plen = le32_to_cpu(rsp->OutputCount);
1988
1989 /* We check for obvious errors in the output buffer length and offset */
1990 if (*plen == 0)
1991 goto ioctl_exit; /* server returned no data */
1992 else if (*plen > 0xFF00) {
1993 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1994 *plen = 0;
1995 rc = -EIO;
1996 goto ioctl_exit;
1997 }
1998
1999 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2000 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2001 le32_to_cpu(rsp->OutputOffset));
2002 *plen = 0;
2003 rc = -EIO;
2004 goto ioctl_exit;
2005 }
2006
2007 *out_data = kmalloc(*plen, GFP_KERNEL);
2008 if (*out_data == NULL) {
2009 rc = -ENOMEM;
2010 goto ioctl_exit;
2011 }
2012
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002013 shdr = get_sync_hdr(rsp);
2014 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002015ioctl_exit:
2016 free_rsp_buf(resp_buftype, rsp);
2017 return rc;
2018}
2019
Steve French64a5cfa2013-10-14 15:31:32 -05002020/*
2021 * Individual callers to ioctl worker function follow
2022 */
2023
2024int
2025SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2026 u64 persistent_fid, u64 volatile_fid)
2027{
2028 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002029 struct compress_ioctl fsctl_input;
2030 char *ret_data = NULL;
2031
2032 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002033 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002034
2035 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2036 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2037 (char *)&fsctl_input /* data input */,
2038 2 /* in data len */, &ret_data /* out data */, NULL);
2039
2040 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002041
2042 return rc;
2043}
2044
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002045int
2046SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2047 u64 persistent_fid, u64 volatile_fid)
2048{
2049 struct smb2_close_req *req;
2050 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002051 struct cifs_ses *ses = tcon->ses;
2052 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002053 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002054 int resp_buftype;
2055 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002056 int flags = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002057 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002058
Joe Perchesf96637b2013-05-04 22:12:25 -05002059 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002060
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002061 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002062 return -EIO;
2063
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002064 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002065 if (rc)
2066 return rc;
2067
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002068 if (encryption_required(tcon))
2069 flags |= CIFS_TRANSFORM_REQ;
2070
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002071 req->PersistentFileId = persistent_fid;
2072 req->VolatileFileId = volatile_fid;
2073
2074 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002075 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002076
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002077 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002078 cifs_small_buf_release(req);
2079 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002080
2081 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002082 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002083 goto close_exit;
2084 }
2085
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002086 /* BB FIXME - decode close response, update inode for caching */
2087
2088close_exit:
2089 free_rsp_buf(resp_buftype, rsp);
2090 return rc;
2091}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002092
2093static int
2094validate_buf(unsigned int offset, unsigned int buffer_length,
2095 struct smb2_hdr *hdr, unsigned int min_buf_size)
2096
2097{
2098 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2099 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2100 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2101 char *end_of_buf = begin_of_buf + buffer_length;
2102
2103
2104 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002105 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2106 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002107 return -EINVAL;
2108 }
2109
2110 /* check if beyond RFC1001 maximum length */
2111 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002112 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2113 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002114 return -EINVAL;
2115 }
2116
2117 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002118 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002119 return -EINVAL;
2120 }
2121
2122 return 0;
2123}
2124
2125/*
2126 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2127 * Caller must free buffer.
2128 */
2129static int
2130validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2131 struct smb2_hdr *hdr, unsigned int minbufsize,
2132 char *data)
2133
2134{
2135 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2136 int rc;
2137
2138 if (!data)
2139 return -EINVAL;
2140
2141 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2142 if (rc)
2143 return rc;
2144
2145 memcpy(data, begin_of_buf, buffer_length);
2146
2147 return 0;
2148}
2149
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002150static int
2151query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002152 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2153 u32 additional_info, size_t output_len, size_t min_len, void **data,
2154 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002155{
2156 struct smb2_query_info_req *req;
2157 struct smb2_query_info_rsp *rsp = NULL;
2158 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002159 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002160 int rc = 0;
2161 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002162 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002163 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002164 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002165
Joe Perchesf96637b2013-05-04 22:12:25 -05002166 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002167
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002168 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002169 return -EIO;
2170
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002171 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2172 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002173 if (rc)
2174 return rc;
2175
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002176 if (encryption_required(tcon))
2177 flags |= CIFS_TRANSFORM_REQ;
2178
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002179 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002180 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002181 req->PersistentFileId = persistent_fid;
2182 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002183 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002184
2185 /*
2186 * We do not use the input buffer (do not send extra byte)
2187 */
2188 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002189
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002190 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002191
2192 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002193 /* 1 for Buffer */
2194 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002195
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002196 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002197 cifs_small_buf_release(req);
2198 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002199
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002200 if (rc) {
2201 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2202 goto qinf_exit;
2203 }
2204
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002205 if (dlen) {
2206 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2207 if (!*data) {
2208 *data = kmalloc(*dlen, GFP_KERNEL);
2209 if (!*data) {
2210 cifs_dbg(VFS,
2211 "Error %d allocating memory for acl\n",
2212 rc);
2213 *dlen = 0;
2214 goto qinf_exit;
2215 }
2216 }
2217 }
2218
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002219 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2220 le32_to_cpu(rsp->OutputBufferLength),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002221 &rsp->hdr, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002222
2223qinf_exit:
2224 free_rsp_buf(resp_buftype, rsp);
2225 return rc;
2226}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002227
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002228int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002229 u64 persistent_fid, u64 volatile_fid,
2230 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002231{
2232 return query_info(xid, tcon, persistent_fid, volatile_fid,
2233 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002234 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002235 sizeof(struct smb2_file_full_ea_info),
2236 (void **)&data,
2237 NULL);
2238}
2239
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002240int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2241 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002242{
2243 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002244 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002245 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002246 sizeof(struct smb2_file_all_info), (void **)&data,
2247 NULL);
2248}
2249
2250int
2251SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2252 u64 persistent_fid, u64 volatile_fid,
2253 void **data, u32 *plen)
2254{
2255 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2256 *plen = 0;
2257
2258 return query_info(xid, tcon, persistent_fid, volatile_fid,
2259 0, SMB2_O_INFO_SECURITY, additional_info,
2260 SMB2_MAX_BUFFER_SIZE,
2261 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002262}
2263
2264int
2265SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2266 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2267{
2268 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002269 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002270 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002271 sizeof(struct smb2_file_internal_info),
2272 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002273}
2274
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002275/*
2276 * This is a no-op for now. We're not really interested in the reply, but
2277 * rather in the fact that the server sent one and that server->lstrp
2278 * gets updated.
2279 *
2280 * FIXME: maybe we should consider checking that the reply matches request?
2281 */
2282static void
2283smb2_echo_callback(struct mid_q_entry *mid)
2284{
2285 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002286 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002287 unsigned int credits_received = 1;
2288
2289 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002290 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002291
2292 DeleteMidQEntry(mid);
2293 add_credits(server, credits_received, CIFS_ECHO_OP);
2294}
2295
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002296void smb2_reconnect_server(struct work_struct *work)
2297{
2298 struct TCP_Server_Info *server = container_of(work,
2299 struct TCP_Server_Info, reconnect.work);
2300 struct cifs_ses *ses;
2301 struct cifs_tcon *tcon, *tcon2;
2302 struct list_head tmp_list;
2303 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002304 int rc;
2305 int resched = false;
2306
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002307
2308 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2309 mutex_lock(&server->reconnect_mutex);
2310
2311 INIT_LIST_HEAD(&tmp_list);
2312 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2313
2314 spin_lock(&cifs_tcp_ses_lock);
2315 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2316 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002317 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002318 tcon->tc_count++;
2319 list_add_tail(&tcon->rlist, &tmp_list);
2320 tcon_exist = true;
2321 }
2322 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002323 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2324 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2325 tcon_exist = true;
2326 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002327 }
2328 /*
2329 * Get the reference to server struct to be sure that the last call of
2330 * cifs_put_tcon() in the loop below won't release the server pointer.
2331 */
2332 if (tcon_exist)
2333 server->srv_count++;
2334
2335 spin_unlock(&cifs_tcp_ses_lock);
2336
2337 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002338 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2339 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002340 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002341 else
2342 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002343 list_del_init(&tcon->rlist);
2344 cifs_put_tcon(tcon);
2345 }
2346
2347 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002348 if (resched)
2349 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002350 mutex_unlock(&server->reconnect_mutex);
2351
2352 /* now we can safely release srv struct */
2353 if (tcon_exist)
2354 cifs_put_tcp_session(server, 1);
2355}
2356
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002357int
2358SMB2_echo(struct TCP_Server_Info *server)
2359{
2360 struct smb2_echo_req *req;
2361 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002362 struct kvec iov[2];
2363 struct smb_rqst rqst = { .rq_iov = iov,
2364 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002365 unsigned int total_len;
2366 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002367
Joe Perchesf96637b2013-05-04 22:12:25 -05002368 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002369
Steve French4fcd1812016-06-22 20:12:05 -05002370 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002371 /* No need to send echo on newly established connections */
2372 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2373 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002374 }
2375
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002376 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002377 if (rc)
2378 return rc;
2379
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002380 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002381
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002382 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002383 rfc1002_marker = cpu_to_be32(total_len);
2384 iov[0].iov_base = &rfc1002_marker;
2385 iov[1].iov_len = total_len;
2386 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002387
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002388 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2389 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002390 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002391 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002392
2393 cifs_small_buf_release(req);
2394 return rc;
2395}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002396
2397int
2398SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2399 u64 volatile_fid)
2400{
2401 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002402 struct cifs_ses *ses = tcon->ses;
2403 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002404 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002405 int resp_buftype;
2406 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002407 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002408 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002409
Joe Perchesf96637b2013-05-04 22:12:25 -05002410 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002411
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002412 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002413 return -EIO;
2414
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002415 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002416 if (rc)
2417 return rc;
2418
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002419 if (encryption_required(tcon))
2420 flags |= CIFS_TRANSFORM_REQ;
2421
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002422 req->PersistentFileId = persistent_fid;
2423 req->VolatileFileId = volatile_fid;
2424
2425 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002426 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002427
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002428 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002429 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002430
Steve Frenchdfebe402015-03-27 01:00:06 -05002431 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002432 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2433
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002434 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002435 return rc;
2436}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002437
2438/*
2439 * To form a chain of read requests, any read requests after the first should
2440 * have the end_of_chain boolean set to true.
2441 */
2442static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002443smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002444 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2445 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002446{
2447 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002448 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002449 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002450 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002451
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002452 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2453 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002454 if (rc)
2455 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002456
2457 server = io_parms->tcon->ses->server;
2458 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002459 return -ECONNABORTED;
2460
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002461 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002462 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002463
2464 req->PersistentFileId = io_parms->persistent_fid;
2465 req->VolatileFileId = io_parms->volatile_fid;
2466 req->ReadChannelInfoOffset = 0; /* reserved */
2467 req->ReadChannelInfoLength = 0; /* reserved */
2468 req->Channel = 0; /* reserved */
2469 req->MinimumCount = 0;
2470 req->Length = cpu_to_le32(io_parms->length);
2471 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002472#ifdef CONFIG_CIFS_SMB_DIRECT
2473 /*
2474 * If we want to do a RDMA write, fill in and append
2475 * smbd_buffer_descriptor_v1 to the end of read request
2476 */
2477 if (server->rdma && rdata &&
2478 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002479
Long Libd3dcc62017-11-22 17:38:47 -07002480 struct smbd_buffer_descriptor_v1 *v1;
2481 bool need_invalidate =
2482 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2483
2484 rdata->mr = smbd_register_mr(
2485 server->smbd_conn, rdata->pages,
2486 rdata->nr_pages, rdata->tailsz,
2487 true, need_invalidate);
2488 if (!rdata->mr)
2489 return -ENOBUFS;
2490
2491 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2492 if (need_invalidate)
2493 req->Channel = SMB2_CHANNEL_RDMA_V1;
2494 req->ReadChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002495 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
Long Libd3dcc62017-11-22 17:38:47 -07002496 req->ReadChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002497 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Libd3dcc62017-11-22 17:38:47 -07002498 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002499 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2500 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2501 v1->length = cpu_to_le32(rdata->mr->mr->length);
Long Libd3dcc62017-11-22 17:38:47 -07002502
2503 *total_len += sizeof(*v1) - 1;
2504 }
2505#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002506 if (request_type & CHAINED_REQUEST) {
2507 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002508 /* next 8-byte aligned request */
2509 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2510 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002511 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002512 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002513 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002514 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002515 /*
2516 * Related requests use info from previous read request
2517 * in chain.
2518 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002519 shdr->SessionId = 0xFFFFFFFF;
2520 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002521 req->PersistentFileId = 0xFFFFFFFF;
2522 req->VolatileFileId = 0xFFFFFFFF;
2523 }
2524 }
2525 if (remaining_bytes > io_parms->length)
2526 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2527 else
2528 req->RemainingBytes = 0;
2529
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002530 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002531 return rc;
2532}
2533
2534static void
2535smb2_readv_callback(struct mid_q_entry *mid)
2536{
2537 struct cifs_readdata *rdata = mid->callback_data;
2538 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2539 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002540 struct smb2_sync_hdr *shdr =
2541 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002542 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002543 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2544 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002545 .rq_pages = rdata->pages,
2546 .rq_npages = rdata->nr_pages,
2547 .rq_pagesz = rdata->pagesz,
2548 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002549
Joe Perchesf96637b2013-05-04 22:12:25 -05002550 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2551 __func__, mid->mid, mid->mid_state, rdata->result,
2552 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002553
2554 switch (mid->mid_state) {
2555 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002556 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002557 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002558 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002559 int rc;
2560
Jeff Layton0b688cf2012-09-18 16:20:34 -07002561 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002562 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002563 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2564 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002565 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002566 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002567 task_io_account_read(rdata->got_bytes);
2568 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002569 break;
2570 case MID_REQUEST_SUBMITTED:
2571 case MID_RETRY_NEEDED:
2572 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002573 if (server->sign && rdata->got_bytes)
2574 /* reset bytes number since we can not check a sign */
2575 rdata->got_bytes = 0;
2576 /* FIXME: should this be counted toward the initiating task? */
2577 task_io_account_read(rdata->got_bytes);
2578 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002579 break;
2580 default:
2581 if (rdata->result != -ENODATA)
2582 rdata->result = -EIO;
2583 }
Long Libd3dcc62017-11-22 17:38:47 -07002584#ifdef CONFIG_CIFS_SMB_DIRECT
2585 /*
2586 * If this rdata has a memmory registered, the MR can be freed
2587 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2588 * because they have limited number and are used for future I/Os
2589 */
2590 if (rdata->mr) {
2591 smbd_deregister_mr(rdata->mr);
2592 rdata->mr = NULL;
2593 }
2594#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002595 if (rdata->result)
2596 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2597
2598 queue_work(cifsiod_wq, &rdata->work);
2599 DeleteMidQEntry(mid);
2600 add_credits(server, credits_received, 0);
2601}
2602
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002603/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002604int
2605smb2_async_readv(struct cifs_readdata *rdata)
2606{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002607 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002608 char *buf;
2609 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002610 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002611 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2612 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002613 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002614 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002615 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002616
Joe Perchesf96637b2013-05-04 22:12:25 -05002617 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2618 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002619
2620 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2621 io_parms.offset = rdata->offset;
2622 io_parms.length = rdata->bytes;
2623 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2624 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2625 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002626
2627 server = io_parms.tcon->ses->server;
2628
Long Li2dabfd52017-11-07 01:54:53 -07002629 rc = smb2_new_read_req(
2630 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002631 if (rc) {
2632 if (rc == -EAGAIN && rdata->credits) {
2633 /* credits was reset by reconnect */
2634 rdata->credits = 0;
2635 /* reduce in_flight value since we won't send the req */
2636 spin_lock(&server->req_lock);
2637 server->in_flight--;
2638 spin_unlock(&server->req_lock);
2639 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002640 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002641 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002642
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002643 if (encryption_required(io_parms.tcon))
2644 flags |= CIFS_TRANSFORM_REQ;
2645
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002646 req_len = cpu_to_be32(total_len);
2647
2648 rdata->iov[0].iov_base = &req_len;
2649 rdata->iov[0].iov_len = sizeof(__be32);
2650 rdata->iov[1].iov_base = buf;
2651 rdata->iov[1].iov_len = total_len;
2652
2653 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002654
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002655 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002656 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002657 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002658 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002659 spin_lock(&server->req_lock);
2660 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002661 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002662 spin_unlock(&server->req_lock);
2663 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002664 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002665 }
2666
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002667 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002668 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002669 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002670 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002671 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002672 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002673 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2674 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002675
2676 cifs_small_buf_release(buf);
2677 return rc;
2678}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002679
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002680int
2681SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2682 unsigned int *nbytes, char **buf, int *buf_type)
2683{
2684 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002685 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002686 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002687 struct smb2_sync_hdr *shdr;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002688 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002689 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002690 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002691 int flags = CIFS_LOG_ERROR;
2692 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002693
2694 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002695 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002696 if (rc)
2697 return rc;
2698
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002699 if (encryption_required(io_parms->tcon))
2700 flags |= CIFS_TRANSFORM_REQ;
2701
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002702 iov[0].iov_base = (char *)req;
2703 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002704
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002705 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002706 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002707
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002708 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002709
2710 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002711 if (rc != -ENODATA) {
2712 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2713 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002714 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002715 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2716 return rc == -ENODATA ? 0 : rc;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002717 }
2718
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002719 *nbytes = le32_to_cpu(rsp->DataLength);
2720 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2721 (*nbytes > io_parms->length)) {
2722 cifs_dbg(FYI, "bad length %d for count %d\n",
2723 *nbytes, io_parms->length);
2724 rc = -EIO;
2725 *nbytes = 0;
2726 }
2727
2728 shdr = get_sync_hdr(rsp);
2729
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002730 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002731 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002732 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002733 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002734 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002735 if (resp_buftype == CIFS_SMALL_BUFFER)
2736 *buf_type = CIFS_SMALL_BUFFER;
2737 else if (resp_buftype == CIFS_LARGE_BUFFER)
2738 *buf_type = CIFS_LARGE_BUFFER;
2739 }
2740 return rc;
2741}
2742
Pavel Shilovsky33319142012-09-18 16:20:29 -07002743/*
2744 * Check the mid_state and signature on received buffer (if any), and queue the
2745 * workqueue completion task.
2746 */
2747static void
2748smb2_writev_callback(struct mid_q_entry *mid)
2749{
2750 struct cifs_writedata *wdata = mid->callback_data;
2751 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2752 unsigned int written;
2753 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2754 unsigned int credits_received = 1;
2755
2756 switch (mid->mid_state) {
2757 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002758 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002759 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2760 if (wdata->result != 0)
2761 break;
2762
2763 written = le32_to_cpu(rsp->DataLength);
2764 /*
2765 * Mask off high 16 bits when bytes written as returned
2766 * by the server is greater than bytes requested by the
2767 * client. OS/2 servers are known to set incorrect
2768 * CountHigh values.
2769 */
2770 if (written > wdata->bytes)
2771 written &= 0xFFFF;
2772
2773 if (written < wdata->bytes)
2774 wdata->result = -ENOSPC;
2775 else
2776 wdata->bytes = written;
2777 break;
2778 case MID_REQUEST_SUBMITTED:
2779 case MID_RETRY_NEEDED:
2780 wdata->result = -EAGAIN;
2781 break;
2782 default:
2783 wdata->result = -EIO;
2784 break;
2785 }
Long Lidb223a52017-11-22 17:38:45 -07002786#ifdef CONFIG_CIFS_SMB_DIRECT
2787 /*
2788 * If this wdata has a memory registered, the MR can be freed
2789 * The number of MRs available is limited, it's important to recover
2790 * used MR as soon as I/O is finished. Hold MR longer in the later
2791 * I/O process can possibly result in I/O deadlock due to lack of MR
2792 * to send request on I/O retry
2793 */
2794 if (wdata->mr) {
2795 smbd_deregister_mr(wdata->mr);
2796 wdata->mr = NULL;
2797 }
2798#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002799 if (wdata->result)
2800 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2801
2802 queue_work(cifsiod_wq, &wdata->work);
2803 DeleteMidQEntry(mid);
2804 add_credits(tcon->ses->server, credits_received, 0);
2805}
2806
2807/* smb2_async_writev - send an async write, and set up mid to handle result */
2808int
Steve French4a5c80d2014-02-07 20:45:12 -06002809smb2_async_writev(struct cifs_writedata *wdata,
2810 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002811{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002812 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002813 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002814 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002815 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002816 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002817 struct kvec iov[2];
2818 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002819 unsigned int total_len;
2820 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002821
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002822 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002823 if (rc) {
2824 if (rc == -EAGAIN && wdata->credits) {
2825 /* credits was reset by reconnect */
2826 wdata->credits = 0;
2827 /* reduce in_flight value since we won't send the req */
2828 spin_lock(&server->req_lock);
2829 server->in_flight--;
2830 spin_unlock(&server->req_lock);
2831 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002832 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002833 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002834
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002835 if (encryption_required(tcon))
2836 flags |= CIFS_TRANSFORM_REQ;
2837
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002838 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002839 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002840
2841 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2842 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2843 req->WriteChannelInfoOffset = 0;
2844 req->WriteChannelInfoLength = 0;
2845 req->Channel = 0;
2846 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002847 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002848 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07002849 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07002850#ifdef CONFIG_CIFS_SMB_DIRECT
2851 /*
2852 * If we want to do a server RDMA read, fill in and append
2853 * smbd_buffer_descriptor_v1 to the end of write request
2854 */
2855 if (server->rdma && wdata->bytes >=
2856 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07002857
Long Lidb223a52017-11-22 17:38:45 -07002858 struct smbd_buffer_descriptor_v1 *v1;
2859 bool need_invalidate = server->dialect == SMB30_PROT_ID;
2860
2861 wdata->mr = smbd_register_mr(
2862 server->smbd_conn, wdata->pages,
2863 wdata->nr_pages, wdata->tailsz,
2864 false, need_invalidate);
2865 if (!wdata->mr) {
2866 rc = -ENOBUFS;
2867 goto async_writev_out;
2868 }
2869 req->Length = 0;
2870 req->DataOffset = 0;
2871 req->RemainingBytes =
Steve French2026b062018-01-24 23:07:41 -06002872 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
Long Lidb223a52017-11-22 17:38:45 -07002873 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2874 if (need_invalidate)
2875 req->Channel = SMB2_CHANNEL_RDMA_V1;
2876 req->WriteChannelInfoOffset =
Steve French2026b062018-01-24 23:07:41 -06002877 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
Long Lidb223a52017-11-22 17:38:45 -07002878 req->WriteChannelInfoLength =
Steve French2026b062018-01-24 23:07:41 -06002879 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
Long Lidb223a52017-11-22 17:38:45 -07002880 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
Steve French2026b062018-01-24 23:07:41 -06002881 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
2882 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
2883 v1->length = cpu_to_le32(wdata->mr->mr->length);
Long Lidb223a52017-11-22 17:38:45 -07002884 }
2885#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002886 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002887 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002888 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
2889 iov[0].iov_base = &rfc1002_marker;
2890 iov[1].iov_len = total_len - 1;
2891 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002892
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002893 rqst.rq_iov = iov;
2894 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002895 rqst.rq_pages = wdata->pages;
2896 rqst.rq_npages = wdata->nr_pages;
2897 rqst.rq_pagesz = wdata->pagesz;
2898 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07002899#ifdef CONFIG_CIFS_SMB_DIRECT
2900 if (wdata->mr) {
2901 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
2902 rqst.rq_npages = 0;
2903 }
2904#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05002905 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2906 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002907
Long Lidb223a52017-11-22 17:38:45 -07002908#ifdef CONFIG_CIFS_SMB_DIRECT
2909 /* For RDMA read, I/O size is in RemainingBytes not in Length */
2910 if (!wdata->mr)
2911 req->Length = cpu_to_le32(wdata->bytes);
2912#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07002913 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07002914#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002915
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002916 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002917 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002918 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002919 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002920 spin_lock(&server->req_lock);
2921 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002922 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002923 spin_unlock(&server->req_lock);
2924 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002925 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002926 }
2927
Pavel Shilovsky33319142012-09-18 16:20:29 -07002928 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002929 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2930 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002931
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002932 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002933 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002934 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2935 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002936
Pavel Shilovsky33319142012-09-18 16:20:29 -07002937async_writev_out:
2938 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002939 return rc;
2940}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002941
2942/*
2943 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2944 * The length field from io_parms must be at least 1 and indicates a number of
2945 * elements with data to write that begins with position 1 in iov array. All
2946 * data length is specified by count.
2947 */
2948int
2949SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2950 unsigned int *nbytes, struct kvec *iov, int n_vec)
2951{
2952 int rc = 0;
2953 struct smb2_write_req *req = NULL;
2954 struct smb2_write_rsp *rsp = NULL;
2955 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002956 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002957 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002958 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002959
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002960 *nbytes = 0;
2961
2962 if (n_vec < 1)
2963 return rc;
2964
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002965 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
2966 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002967 if (rc)
2968 return rc;
2969
2970 if (io_parms->tcon->ses->server == NULL)
2971 return -ECONNABORTED;
2972
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002973 if (encryption_required(io_parms->tcon))
2974 flags |= CIFS_TRANSFORM_REQ;
2975
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002976 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002977
2978 req->PersistentFileId = io_parms->persistent_fid;
2979 req->VolatileFileId = io_parms->volatile_fid;
2980 req->WriteChannelInfoOffset = 0;
2981 req->WriteChannelInfoLength = 0;
2982 req->Channel = 0;
2983 req->Length = cpu_to_le32(io_parms->length);
2984 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002985 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002986 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002987 req->RemainingBytes = 0;
2988
2989 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002990 /* 1 for Buffer */
2991 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002992
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002993 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
2994 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002995 cifs_small_buf_release(req);
2996 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002997
2998 if (rc) {
2999 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003000 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003001 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003002 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003003
3004 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003005 return rc;
3006}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003007
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003008static unsigned int
3009num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3010{
3011 int len;
3012 unsigned int entrycount = 0;
3013 unsigned int next_offset = 0;
3014 FILE_DIRECTORY_INFO *entryptr;
3015
3016 if (bufstart == NULL)
3017 return 0;
3018
3019 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3020
3021 while (1) {
3022 entryptr = (FILE_DIRECTORY_INFO *)
3023 ((char *)entryptr + next_offset);
3024
3025 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003026 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003027 break;
3028 }
3029
3030 len = le32_to_cpu(entryptr->FileNameLength);
3031 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003032 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3033 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003034 break;
3035 }
3036
3037 *lastentry = (char *)entryptr;
3038 entrycount++;
3039
3040 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3041 if (!next_offset)
3042 break;
3043 }
3044
3045 return entrycount;
3046}
3047
3048/*
3049 * Readdir/FindFirst
3050 */
3051int
3052SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3053 u64 persistent_fid, u64 volatile_fid, int index,
3054 struct cifs_search_info *srch_inf)
3055{
3056 struct smb2_query_directory_req *req;
3057 struct smb2_query_directory_rsp *rsp = NULL;
3058 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003059 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003060 int rc = 0;
3061 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003062 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003063 unsigned char *bufptr;
3064 struct TCP_Server_Info *server;
3065 struct cifs_ses *ses = tcon->ses;
3066 __le16 asteriks = cpu_to_le16('*');
3067 char *end_of_smb;
3068 unsigned int output_size = CIFSMaxBufSize;
3069 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003070 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003071 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003072
3073 if (ses && (ses->server))
3074 server = ses->server;
3075 else
3076 return -EIO;
3077
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003078 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3079 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003080 if (rc)
3081 return rc;
3082
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003083 if (encryption_required(tcon))
3084 flags |= CIFS_TRANSFORM_REQ;
3085
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003086 switch (srch_inf->info_level) {
3087 case SMB_FIND_FILE_DIRECTORY_INFO:
3088 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3089 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3090 break;
3091 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3092 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3093 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3094 break;
3095 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003096 cifs_dbg(VFS, "info level %u isn't supported\n",
3097 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003098 rc = -EINVAL;
3099 goto qdir_exit;
3100 }
3101
3102 req->FileIndex = cpu_to_le32(index);
3103 req->PersistentFileId = persistent_fid;
3104 req->VolatileFileId = volatile_fid;
3105
3106 len = 0x2;
3107 bufptr = req->Buffer;
3108 memcpy(bufptr, &asteriks, len);
3109
3110 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003111 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003112 req->FileNameLength = cpu_to_le16(len);
3113 /*
3114 * BB could be 30 bytes or so longer if we used SMB2 specific
3115 * buffer lengths, but this is safe and close enough.
3116 */
3117 output_size = min_t(unsigned int, output_size, server->maxBuf);
3118 output_size = min_t(unsigned int, output_size, 2 << 15);
3119 req->OutputBufferLength = cpu_to_le32(output_size);
3120
3121 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003122 /* 1 for Buffer */
3123 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003124
3125 iov[1].iov_base = (char *)(req->Buffer);
3126 iov[1].iov_len = len;
3127
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003128 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003129 cifs_small_buf_release(req);
3130 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003131
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003132 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003133 if (rc == -ENODATA &&
3134 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003135 srch_inf->endOfSearch = true;
3136 rc = 0;
3137 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003138 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3139 goto qdir_exit;
3140 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003141
3142 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3143 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3144 info_buf_size);
3145 if (rc)
3146 goto qdir_exit;
3147
3148 srch_inf->unicode = true;
3149
3150 if (srch_inf->ntwrk_buf_start) {
3151 if (srch_inf->smallBuf)
3152 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3153 else
3154 cifs_buf_release(srch_inf->ntwrk_buf_start);
3155 }
3156 srch_inf->ntwrk_buf_start = (char *)rsp;
3157 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3158 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3159 /* 4 for rfc1002 length field */
3160 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3161 srch_inf->entries_in_buffer =
3162 num_entries(srch_inf->srch_entries_start, end_of_smb,
3163 &srch_inf->last_entry, info_buf_size);
3164 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003165 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3166 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3167 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003168 if (resp_buftype == CIFS_LARGE_BUFFER)
3169 srch_inf->smallBuf = false;
3170 else if (resp_buftype == CIFS_SMALL_BUFFER)
3171 srch_inf->smallBuf = true;
3172 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003173 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003174
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003175 return rc;
3176
3177qdir_exit:
3178 free_rsp_buf(resp_buftype, rsp);
3179 return rc;
3180}
3181
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003182static int
3183send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003184 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3185 u8 info_type, u32 additional_info, unsigned int num,
3186 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003187{
3188 struct smb2_set_info_req *req;
3189 struct smb2_set_info_rsp *rsp = NULL;
3190 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003191 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003192 int rc = 0;
3193 int resp_buftype;
3194 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003195 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003196 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003197 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003198
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003199 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003200 return -EIO;
3201
3202 if (!num)
3203 return -EINVAL;
3204
3205 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3206 if (!iov)
3207 return -ENOMEM;
3208
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003209 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003210 if (rc) {
3211 kfree(iov);
3212 return rc;
3213 }
3214
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003215 if (encryption_required(tcon))
3216 flags |= CIFS_TRANSFORM_REQ;
3217
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003218 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003219
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003220 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003221 req->FileInfoClass = info_class;
3222 req->PersistentFileId = persistent_fid;
3223 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003224 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003225
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003226 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003227 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003228 req->BufferLength = cpu_to_le32(*size);
3229
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003230 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003231 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003232
3233 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003234 /* 1 for Buffer */
3235 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003236
3237 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003238 le32_add_cpu(&req->BufferLength, size[i]);
3239 iov[i].iov_base = (char *)data[i];
3240 iov[i].iov_len = size[i];
3241 }
3242
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003243 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3244 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003245 cifs_small_buf_release(req);
3246 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003247
Steve French7d3fb242013-11-18 09:56:28 -06003248 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003249 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003250
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003251 free_rsp_buf(resp_buftype, rsp);
3252 kfree(iov);
3253 return rc;
3254}
3255
3256int
3257SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3258 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3259{
3260 struct smb2_file_rename_info info;
3261 void **data;
3262 unsigned int size[2];
3263 int rc;
3264 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3265
3266 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3267 if (!data)
3268 return -ENOMEM;
3269
3270 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3271 /* 0 = fail if target already exists */
3272 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3273 info.FileNameLength = cpu_to_le32(len);
3274
3275 data[0] = &info;
3276 size[0] = sizeof(struct smb2_file_rename_info);
3277
3278 data[1] = target_file;
3279 size[1] = len + 2 /* null */;
3280
3281 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003282 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3283 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003284 kfree(data);
3285 return rc;
3286}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003287
3288int
Steve French897fba12016-05-12 21:20:36 -05003289SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3290 u64 persistent_fid, u64 volatile_fid)
3291{
3292 __u8 delete_pending = 1;
3293 void *data;
3294 unsigned int size;
3295
3296 data = &delete_pending;
3297 size = 1; /* sizeof __u8 */
3298
3299 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003300 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3301 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003302}
3303
3304int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003305SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3306 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3307{
3308 struct smb2_file_link_info info;
3309 void **data;
3310 unsigned int size[2];
3311 int rc;
3312 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3313
3314 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3315 if (!data)
3316 return -ENOMEM;
3317
3318 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3319 /* 0 = fail if link already exists */
3320 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3321 info.FileNameLength = cpu_to_le32(len);
3322
3323 data[0] = &info;
3324 size[0] = sizeof(struct smb2_file_link_info);
3325
3326 data[1] = target_file;
3327 size[1] = len + 2 /* null */;
3328
3329 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003330 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3331 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003332 kfree(data);
3333 return rc;
3334}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003335
3336int
3337SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003338 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003339{
3340 struct smb2_file_eof_info info;
3341 void *data;
3342 unsigned int size;
3343
3344 info.EndOfFile = *eof;
3345
3346 data = &info;
3347 size = sizeof(struct smb2_file_eof_info);
3348
Steve Frenchf29ebb42014-07-19 21:44:58 -05003349 if (is_falloc)
3350 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003351 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3352 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003353 else
3354 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003355 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3356 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003357}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003358
3359int
3360SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3361 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3362{
3363 unsigned int size;
3364 size = sizeof(FILE_BASIC_INFO);
3365 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003366 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3367 0, 1, (void **)&buf, &size);
3368}
3369
3370int
3371SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3372 u64 persistent_fid, u64 volatile_fid,
3373 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3374{
3375 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3376 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3377 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003378}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003379
3380int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003381SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3382 u64 persistent_fid, u64 volatile_fid,
3383 struct smb2_file_full_ea_info *buf, int len)
3384{
3385 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3386 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3387 0, 1, (void **)&buf, &len);
3388}
3389
3390int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003391SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3392 const u64 persistent_fid, const u64 volatile_fid,
3393 __u8 oplock_level)
3394{
3395 int rc;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003396 struct smb2_oplock_break_req *req = NULL;
3397 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003398 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003399 unsigned int total_len;
3400 struct kvec iov[1];
3401 struct kvec rsp_iov;
3402 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003403
Joe Perchesf96637b2013-05-04 22:12:25 -05003404 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003405 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3406 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003407 if (rc)
3408 return rc;
3409
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003410 if (encryption_required(tcon))
3411 flags |= CIFS_TRANSFORM_REQ;
3412
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003413 req->VolatileFid = volatile_fid;
3414 req->PersistentFid = persistent_fid;
3415 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003416 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003417
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003418 flags |= CIFS_NO_RESP;
3419
3420 iov[0].iov_base = (char *)req;
3421 iov[0].iov_len = total_len;
3422
3423 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003424 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003425
3426 if (rc) {
3427 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003428 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003429 }
3430
3431 return rc;
3432}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003433
3434static void
3435copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3436 struct kstatfs *kst)
3437{
3438 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3439 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3440 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303441 kst->f_bfree = kst->f_bavail =
3442 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003443 return;
3444}
3445
3446static int
3447build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3448 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3449{
3450 int rc;
3451 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003452 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003453
Joe Perchesf96637b2013-05-04 22:12:25 -05003454 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003455
3456 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3457 return -EIO;
3458
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003459 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3460 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003461 if (rc)
3462 return rc;
3463
3464 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3465 req->FileInfoClass = level;
3466 req->PersistentFileId = persistent_fid;
3467 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003468 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003469 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003470 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003471 req->OutputBufferLength = cpu_to_le32(
3472 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3473
3474 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003475 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003476 return 0;
3477}
3478
3479int
3480SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3481 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3482{
3483 struct smb2_query_info_rsp *rsp = NULL;
3484 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003485 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003486 int rc = 0;
3487 int resp_buftype;
3488 struct cifs_ses *ses = tcon->ses;
3489 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003490 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003491
3492 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3493 sizeof(struct smb2_fs_full_size_info),
3494 persistent_fid, volatile_fid);
3495 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 Sahlbergb2fb7fe2017-11-20 11:24:46 +11003501 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003502 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003503 if (rc) {
3504 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003505 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003506 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003507 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003508
3509 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3510 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3511 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3512 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3513 sizeof(struct smb2_fs_full_size_info));
3514 if (!rc)
3515 copy_fs_info_to_kstatfs(info, fsdata);
3516
Steve French34f62642013-10-09 02:07:00 -05003517qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003518 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003519 return rc;
3520}
3521
3522int
3523SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003524 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003525{
3526 struct smb2_query_info_rsp *rsp = NULL;
3527 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003528 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003529 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003530 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003531 struct cifs_ses *ses = tcon->ses;
3532 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003533 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003534
Steven French21671142013-10-09 13:36:35 -05003535 if (level == FS_DEVICE_INFORMATION) {
3536 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3537 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3538 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3539 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3540 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003541 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3542 max_len = sizeof(struct smb3_fs_ss_info);
3543 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003544 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003545 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003546 return -EINVAL;
3547 }
3548
3549 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003550 persistent_fid, volatile_fid);
3551 if (rc)
3552 return rc;
3553
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003554 if (encryption_required(tcon))
3555 flags |= CIFS_TRANSFORM_REQ;
3556
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003557 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003558 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003559 if (rc) {
3560 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3561 goto qfsattr_exit;
3562 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003563 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003564
3565 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3566 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003567 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3568 if (rc)
3569 goto qfsattr_exit;
3570
3571 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003572 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3573 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003574 rsp_len, max_len));
3575 else if (level == FS_DEVICE_INFORMATION)
3576 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3577 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003578 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3579 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3580 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3581 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3582 tcon->perf_sector_size =
3583 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3584 }
Steve French34f62642013-10-09 02:07:00 -05003585
3586qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003587 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003588 return rc;
3589}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003590
3591int
3592smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3593 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3594 const __u32 num_lock, struct smb2_lock_element *buf)
3595{
3596 int rc = 0;
3597 struct smb2_lock_req *req = NULL;
3598 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003599 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003600 int resp_buf_type;
3601 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003602 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003603 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003604
Joe Perchesf96637b2013-05-04 22:12:25 -05003605 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003606
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003607 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003608 if (rc)
3609 return rc;
3610
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003611 if (encryption_required(tcon))
3612 flags |= CIFS_TRANSFORM_REQ;
3613
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003614 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003615 req->LockCount = cpu_to_le16(num_lock);
3616
3617 req->PersistentFileId = persist_fid;
3618 req->VolatileFileId = volatile_fid;
3619
3620 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003621
3622 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003623 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003624 iov[1].iov_base = (char *)buf;
3625 iov[1].iov_len = count;
3626
3627 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003628 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3629 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003630 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003631 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003632 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003633 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3634 }
3635
3636 return rc;
3637}
3638
3639int
3640SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3641 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3642 const __u64 length, const __u64 offset, const __u32 lock_flags,
3643 const bool wait)
3644{
3645 struct smb2_lock_element lock;
3646
3647 lock.Offset = cpu_to_le64(offset);
3648 lock.Length = cpu_to_le64(length);
3649 lock.Flags = cpu_to_le32(lock_flags);
3650 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3651 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3652
3653 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3654}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003655
3656int
3657SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3658 __u8 *lease_key, const __le32 lease_state)
3659{
3660 int rc;
3661 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003662 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003663 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003664 unsigned int total_len;
3665 struct kvec iov[1];
3666 struct kvec rsp_iov;
3667 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003668
Joe Perchesf96637b2013-05-04 22:12:25 -05003669 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003670 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3671 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003672 if (rc)
3673 return rc;
3674
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003675 if (encryption_required(tcon))
3676 flags |= CIFS_TRANSFORM_REQ;
3677
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003678 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003679 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003680 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003681
3682 memcpy(req->LeaseKey, lease_key, 16);
3683 req->LeaseState = lease_state;
3684
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003685 flags |= CIFS_NO_RESP;
3686
3687 iov[0].iov_base = (char *)req;
3688 iov[0].iov_len = total_len;
3689
3690 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003691 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003692
3693 if (rc) {
3694 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003695 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003696 }
3697
3698 return rc;
3699}