blob: 2943adc754e4aa69f79f11ef68d4fd986e97897c [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;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400456
Steve French9764c022017-09-17 10:41:35 -0500457 if (strcmp(ses->server->vals->version_string,
458 SMB3ANY_VERSION_STRING) == 0) {
459 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
460 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
461 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100462 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500463 } else if (strcmp(ses->server->vals->version_string,
464 SMBDEFAULT_VERSION_STRING) == 0) {
465 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
466 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
467 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
468 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100469 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500470 } else {
471 /* otherwise send specific dialect */
472 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
473 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100474 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500475 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400476
477 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400478 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500479 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400480 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500481 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400482 else
483 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400484
Steve Frenche4aa25e2012-10-01 12:26:22 -0500485 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400486
Steve French3c5f9be12014-05-13 13:37:45 -0700487 /* ClientGUID must be zero for SMB2.02 dialect */
488 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
489 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500490 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700491 memcpy(req->ClientGUID, server->client_guid,
492 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500493 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100494 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500495 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400496 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100497 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400498
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100499 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700500 cifs_small_buf_release(req);
501 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400502 /*
503 * No tcon so can't do
504 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
505 */
Steve French7e682f72017-08-31 21:34:24 -0500506 if (rc == -EOPNOTSUPP) {
507 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500508 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500509 " older servers\n");
510 goto neg_exit;
511 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400512 goto neg_exit;
513
Steve French9764c022017-09-17 10:41:35 -0500514 if (strcmp(ses->server->vals->version_string,
515 SMB3ANY_VERSION_STRING) == 0) {
516 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
517 cifs_dbg(VFS,
518 "SMB2 dialect returned but not requested\n");
519 return -EIO;
520 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
521 cifs_dbg(VFS,
522 "SMB2.1 dialect returned but not requested\n");
523 return -EIO;
524 }
525 } else if (strcmp(ses->server->vals->version_string,
526 SMBDEFAULT_VERSION_STRING) == 0) {
527 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
528 cifs_dbg(VFS,
529 "SMB2 dialect returned but not requested\n");
530 return -EIO;
531 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
532 /* ops set to 3.0 by default for default so update */
533 ses->server->ops = &smb21_operations;
534 }
Steve French590d08d2017-09-19 11:43:47 -0500535 } else if (le16_to_cpu(rsp->DialectRevision) !=
536 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500537 /* if requested single dialect ensure returned dialect matched */
538 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500539 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500540 return -EIO;
541 }
542
Joe Perchesf96637b2013-05-04 22:12:25 -0500543 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400544
Steve Frenche4aa25e2012-10-01 12:26:22 -0500545 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500546 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500547 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500548 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500549 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500550 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500551 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
552 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600553#ifdef CONFIG_CIFS_SMB311
554 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
555 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
556#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400557 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500558 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500559 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400560 rc = -EIO;
561 goto neg_exit;
562 }
563 server->dialect = le16_to_cpu(rsp->DialectRevision);
564
Steve French9764c022017-09-17 10:41:35 -0500565 /* BB: add check that dialect was valid given dialect(s) we asked for */
566
Jeff Laytone598d1d82013-05-26 07:00:59 -0400567 /* SMB2 only has an extended negflavor */
568 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400569 /* set it to the maximum buffer size value we can send with 1 credit */
570 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
571 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400572 server->max_read = le32_to_cpu(rsp->MaxReadSize);
573 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
574 /* BB Do we need to validate the SecurityMode? */
575 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
576 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400577 /* Internal types */
578 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400579
580 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
581 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500582 /*
583 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
584 * for us will be
585 * ses->sectype = RawNTLMSSP;
586 * but for time being this is our only auth choice so doesn't matter.
587 * We just found a server which sets blob length to zero expecting raw.
588 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700589 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500590 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700591 server->sec_ntlmssp = true;
592 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700593
Jeff Layton38d77c52013-05-26 07:01:00 -0400594 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400595 if (rc)
596 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500597 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500598 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500599 if (rc == 1)
600 rc = 0;
601 else if (rc == 0)
602 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400603 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400604neg_exit:
605 free_rsp_buf(resp_buftype, rsp);
606 return rc;
607}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400608
Steve Frenchff1c0382013-11-19 23:44:46 -0600609int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
610{
611 int rc = 0;
612 struct validate_negotiate_info_req vneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200613 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600614 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500615 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600616
617 cifs_dbg(FYI, "validate negotiate\n");
618
Long Li8801e902017-11-22 17:38:49 -0700619#ifdef CONFIG_CIFS_SMB_DIRECT
620 if (tcon->ses->server->rdma)
621 return 0;
622#endif
623
Steve Frenchff1c0382013-11-19 23:44:46 -0600624 /*
625 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500626 * can not sign it (ie are not known user). Even if signing is not
627 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600628 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500629 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600630 */
Steve French0603c962017-09-20 19:57:18 -0500631 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600632 return 0; /* validation requires signing */
633
Steve French0603c962017-09-20 19:57:18 -0500634 if (tcon->ses->user_name == NULL) {
635 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
636 return 0; /* validation requires signing */
637 }
638
639 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
640 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
641
Steve Frenchff1c0382013-11-19 23:44:46 -0600642 vneg_inbuf.Capabilities =
643 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100644 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
645 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600646
647 if (tcon->ses->sign)
648 vneg_inbuf.SecurityMode =
649 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
650 else if (global_secflags & CIFSSEC_MAY_SIGN)
651 vneg_inbuf.SecurityMode =
652 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
653 else
654 vneg_inbuf.SecurityMode = 0;
655
Steve French9764c022017-09-17 10:41:35 -0500656
657 if (strcmp(tcon->ses->server->vals->version_string,
658 SMB3ANY_VERSION_STRING) == 0) {
659 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
660 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
661 vneg_inbuf.DialectCount = cpu_to_le16(2);
662 /* structure is big enough for 3 dialects, sending only 2 */
663 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
664 } else if (strcmp(tcon->ses->server->vals->version_string,
665 SMBDEFAULT_VERSION_STRING) == 0) {
666 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
667 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
668 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
669 vneg_inbuf.DialectCount = cpu_to_le16(3);
670 /* structure is big enough for 3 dialects */
671 inbuflen = sizeof(struct validate_negotiate_info_req);
672 } else {
673 /* otherwise specific dialect was requested */
674 vneg_inbuf.Dialects[0] =
675 cpu_to_le16(tcon->ses->server->vals->protocol_id);
676 vneg_inbuf.DialectCount = cpu_to_le16(1);
677 /* structure is big enough for 3 dialects, sending only 1 */
678 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
679 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600680
681 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
682 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100683 false /* use_ipc */,
Steve Frenchff1c0382013-11-19 23:44:46 -0600684 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
685 (char **)&pneg_rsp, &rsplen);
686
687 if (rc != 0) {
688 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
689 return -EIO;
690 }
691
692 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500693 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
694 rsplen);
695
696 /* relax check since Mac returns max bufsize allowed on ioctl */
David Disseldorpa2d9daa2017-10-20 14:49:38 +0200697 if ((rsplen > CIFSMaxBufSize)
698 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200699 goto err_rsp_free;
Steve Frenchff1c0382013-11-19 23:44:46 -0600700 }
701
702 /* check validate negotiate info response matches what we got earlier */
Daniel N Pettersson9aca7e42018-01-11 16:00:12 +0100703 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600704 goto vneg_out;
705
706 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
707 goto vneg_out;
708
709 /* do not validate server guid because not saved at negprot time yet */
710
711 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
712 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
713 goto vneg_out;
714
715 /* validate negotiate successful */
716 cifs_dbg(FYI, "validate negotiate info successful\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200717 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600718 return 0;
719
720vneg_out:
721 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200722err_rsp_free:
723 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600724 return -EIO;
725}
726
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530727enum securityEnum
728smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
729{
730 switch (requested) {
731 case Kerberos:
732 case RawNTLMSSP:
733 return requested;
734 case NTLMv2:
735 return RawNTLMSSP;
736 case Unspecified:
737 if (server->sec_ntlmssp &&
738 (global_secflags & CIFSSEC_MAY_NTLMSSP))
739 return RawNTLMSSP;
740 if ((server->sec_kerberos || server->sec_mskerberos) &&
741 (global_secflags & CIFSSEC_MAY_KRB5))
742 return Kerberos;
743 /* Fallthrough */
744 default:
745 return Unspecified;
746 }
747}
748
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100749struct SMB2_sess_data {
750 unsigned int xid;
751 struct cifs_ses *ses;
752 struct nls_table *nls_cp;
753 void (*func)(struct SMB2_sess_data *);
754 int result;
755 u64 previous_session;
756
757 /* we will send the SMB in three pieces:
758 * a fixed length beginning part, an optional
759 * SPNEGO blob (which can be zero length), and a
760 * last part which will include the strings
761 * and rest of bcc area. This allows us to avoid
762 * a large buffer 17K allocation
763 */
764 int buf0_type;
765 struct kvec iov[2];
766};
767
768static int
769SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
770{
771 int rc;
772 struct cifs_ses *ses = sess_data->ses;
773 struct smb2_sess_setup_req *req;
774 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100775 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100776
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100777 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
778 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100779 if (rc)
780 return rc;
781
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700782 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100783 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100784
785 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
786 req->PreviousSessionId = sess_data->previous_session;
787
788 req->Flags = 0; /* MBZ */
789 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100790 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100791
792 /* only one of SMB2 signing flags may be set in SMB2 request */
793 if (server->sign)
794 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
795 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
796 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
797 else
798 req->SecurityMode = 0;
799
800 req->Capabilities = 0;
801 req->Channel = 0; /* MBZ */
802
803 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100804 /* 1 for pad */
805 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100806 /*
807 * This variable will be used to clear the buffer
808 * allocated above in case of any error in the calling function.
809 */
810 sess_data->buf0_type = CIFS_SMALL_BUFFER;
811
812 return 0;
813}
814
815static void
816SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
817{
818 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
819 sess_data->buf0_type = CIFS_NO_BUFFER;
820}
821
822static int
823SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
824{
825 int rc;
826 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700827 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100828
829 /* Testing shows that buffer offset must be at location of Buffer[0] */
830 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100831 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100832 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
833
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100834 /* BB add code to build os and lm fields */
835
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100836 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
837 sess_data->iov, 2,
838 &sess_data->buf0_type,
839 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700840 cifs_small_buf_release(sess_data->iov[0].iov_base);
841 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100842
843 return rc;
844}
845
846static int
847SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
848{
849 int rc = 0;
850 struct cifs_ses *ses = sess_data->ses;
851
852 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800853 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100854 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100855 if (rc) {
856 cifs_dbg(FYI,
857 "SMB3 session key generation failed\n");
858 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800859 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100860 }
861 }
862 if (!ses->server->session_estab) {
863 ses->server->sequence_number = 0x2;
864 ses->server->session_estab = true;
865 }
866 mutex_unlock(&ses->server->srv_mutex);
867
868 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
869 spin_lock(&GlobalMid_Lock);
870 ses->status = CifsGood;
871 ses->need_reconnect = false;
872 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100873 return rc;
874}
875
876#ifdef CONFIG_CIFS_UPCALL
877static void
878SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
879{
880 int rc;
881 struct cifs_ses *ses = sess_data->ses;
882 struct cifs_spnego_msg *msg;
883 struct key *spnego_key = NULL;
884 struct smb2_sess_setup_rsp *rsp = NULL;
885
886 rc = SMB2_sess_alloc_buffer(sess_data);
887 if (rc)
888 goto out;
889
890 spnego_key = cifs_get_spnego_key(ses);
891 if (IS_ERR(spnego_key)) {
892 rc = PTR_ERR(spnego_key);
893 spnego_key = NULL;
894 goto out;
895 }
896
897 msg = spnego_key->payload.data[0];
898 /*
899 * check version field to make sure that cifs.upcall is
900 * sending us a response in an expected form
901 */
902 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
903 cifs_dbg(VFS,
904 "bad cifs.upcall version. Expected %d got %d",
905 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
906 rc = -EKEYREJECTED;
907 goto out_put_spnego_key;
908 }
909
910 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
911 GFP_KERNEL);
912 if (!ses->auth_key.response) {
913 cifs_dbg(VFS,
914 "Kerberos can't allocate (%u bytes) memory",
915 msg->sesskey_len);
916 rc = -ENOMEM;
917 goto out_put_spnego_key;
918 }
919 ses->auth_key.len = msg->sesskey_len;
920
921 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
922 sess_data->iov[1].iov_len = msg->secblob_len;
923
924 rc = SMB2_sess_sendreceive(sess_data);
925 if (rc)
926 goto out_put_spnego_key;
927
928 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700929 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100930
931 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100932
933 rc = SMB2_sess_establish_session(sess_data);
934out_put_spnego_key:
935 key_invalidate(spnego_key);
936 key_put(spnego_key);
937out:
938 sess_data->result = rc;
939 sess_data->func = NULL;
940 SMB2_sess_free_buffer(sess_data);
941}
942#else
943static void
944SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
945{
946 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
947 sess_data->result = -EOPNOTSUPP;
948 sess_data->func = NULL;
949}
950#endif
951
Sachin Prabhu166cea42016-10-07 19:11:22 +0100952static void
953SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
954
955static void
956SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
957{
958 int rc;
959 struct cifs_ses *ses = sess_data->ses;
960 struct smb2_sess_setup_rsp *rsp = NULL;
961 char *ntlmssp_blob = NULL;
962 bool use_spnego = false; /* else use raw ntlmssp */
963 u16 blob_length = 0;
964
965 /*
966 * If memory allocation is successful, caller of this function
967 * frees it.
968 */
969 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
970 if (!ses->ntlmssp) {
971 rc = -ENOMEM;
972 goto out_err;
973 }
974 ses->ntlmssp->sesskey_per_smbsess = true;
975
976 rc = SMB2_sess_alloc_buffer(sess_data);
977 if (rc)
978 goto out_err;
979
980 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
981 GFP_KERNEL);
982 if (ntlmssp_blob == NULL) {
983 rc = -ENOMEM;
984 goto out;
985 }
986
987 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
988 if (use_spnego) {
989 /* BB eventually need to add this */
990 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
991 rc = -EOPNOTSUPP;
992 goto out;
993 } else {
994 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
995 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
996 }
997 sess_data->iov[1].iov_base = ntlmssp_blob;
998 sess_data->iov[1].iov_len = blob_length;
999
1000 rc = SMB2_sess_sendreceive(sess_data);
1001 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1002
1003 /* If true, rc here is expected and not an error */
1004 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001005 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001006 rc = 0;
1007
1008 if (rc)
1009 goto out;
1010
1011 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1012 le16_to_cpu(rsp->SecurityBufferOffset)) {
1013 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1014 le16_to_cpu(rsp->SecurityBufferOffset));
1015 rc = -EIO;
1016 goto out;
1017 }
1018 rc = decode_ntlmssp_challenge(rsp->Buffer,
1019 le16_to_cpu(rsp->SecurityBufferLength), ses);
1020 if (rc)
1021 goto out;
1022
1023 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1024
1025
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001026 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001027 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001028
1029out:
1030 kfree(ntlmssp_blob);
1031 SMB2_sess_free_buffer(sess_data);
1032 if (!rc) {
1033 sess_data->result = 0;
1034 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1035 return;
1036 }
1037out_err:
1038 kfree(ses->ntlmssp);
1039 ses->ntlmssp = NULL;
1040 sess_data->result = rc;
1041 sess_data->func = NULL;
1042}
1043
1044static void
1045SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1046{
1047 int rc;
1048 struct cifs_ses *ses = sess_data->ses;
1049 struct smb2_sess_setup_req *req;
1050 struct smb2_sess_setup_rsp *rsp = NULL;
1051 unsigned char *ntlmssp_blob = NULL;
1052 bool use_spnego = false; /* else use raw ntlmssp */
1053 u16 blob_length = 0;
1054
1055 rc = SMB2_sess_alloc_buffer(sess_data);
1056 if (rc)
1057 goto out;
1058
1059 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001060 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001061
1062 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1063 sess_data->nls_cp);
1064 if (rc) {
1065 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1066 goto out;
1067 }
1068
1069 if (use_spnego) {
1070 /* BB eventually need to add this */
1071 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1072 rc = -EOPNOTSUPP;
1073 goto out;
1074 }
1075 sess_data->iov[1].iov_base = ntlmssp_blob;
1076 sess_data->iov[1].iov_len = blob_length;
1077
1078 rc = SMB2_sess_sendreceive(sess_data);
1079 if (rc)
1080 goto out;
1081
1082 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1083
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001084 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001085 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001086
1087 rc = SMB2_sess_establish_session(sess_data);
1088out:
1089 kfree(ntlmssp_blob);
1090 SMB2_sess_free_buffer(sess_data);
1091 kfree(ses->ntlmssp);
1092 ses->ntlmssp = NULL;
1093 sess_data->result = rc;
1094 sess_data->func = NULL;
1095}
1096
1097static int
1098SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1099{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301100 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001101
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301102 type = smb2_select_sectype(ses->server, ses->sectype);
1103 cifs_dbg(FYI, "sess setup type %d\n", type);
1104 if (type == Unspecified) {
1105 cifs_dbg(VFS,
1106 "Unable to select appropriate authentication method!");
1107 return -EINVAL;
1108 }
1109
1110 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001111 case Kerberos:
1112 sess_data->func = SMB2_auth_kerberos;
1113 break;
1114 case RawNTLMSSP:
1115 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1116 break;
1117 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301118 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001119 return -EOPNOTSUPP;
1120 }
1121
1122 return 0;
1123}
1124
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001125int
1126SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1127 const struct nls_table *nls_cp)
1128{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001129 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001130 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001131 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001132
Joe Perchesf96637b2013-05-04 22:12:25 -05001133 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001134
Jeff Layton3534b852013-05-24 07:41:01 -04001135 if (!server) {
1136 WARN(1, "%s: server is NULL!\n", __func__);
1137 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001138 }
1139
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001140 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1141 if (!sess_data)
1142 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001143
1144 rc = SMB2_select_sec(ses, sess_data);
1145 if (rc)
1146 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001147 sess_data->xid = xid;
1148 sess_data->ses = ses;
1149 sess_data->buf0_type = CIFS_NO_BUFFER;
1150 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001151
Sachin Prabhu166cea42016-10-07 19:11:22 +01001152 while (sess_data->func)
1153 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001154
Steve Frenchc721c382017-09-19 18:40:03 -05001155 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1156 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001157 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001158out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001159 kfree(sess_data);
1160 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001161}
1162
1163int
1164SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1165{
1166 struct smb2_logoff_req *req; /* response is also trivial struct */
1167 int rc = 0;
1168 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001169 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001170 unsigned int total_len;
1171 struct kvec iov[1];
1172 struct kvec rsp_iov;
1173 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001174
Joe Perchesf96637b2013-05-04 22:12:25 -05001175 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001176
1177 if (ses && (ses->server))
1178 server = ses->server;
1179 else
1180 return -EIO;
1181
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001182 /* no need to send SMB logoff if uid already closed due to reconnect */
1183 if (ses->need_reconnect)
1184 goto smb2_session_already_dead;
1185
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001186 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001187 if (rc)
1188 return rc;
1189
1190 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001191 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001192
1193 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1194 flags |= CIFS_TRANSFORM_REQ;
1195 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001196 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001197
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001198 flags |= CIFS_NO_RESP;
1199
1200 iov[0].iov_base = (char *)req;
1201 iov[0].iov_len = total_len;
1202
1203 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001204 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001205 /*
1206 * No tcon so can't do
1207 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1208 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001209
1210smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001211 return rc;
1212}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001213
1214static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1215{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001216 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001217}
1218
1219#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1220
Steve Frenchde9f68d2013-11-15 11:26:24 -06001221/* These are similar values to what Windows uses */
1222static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1223{
1224 tcon->max_chunks = 256;
1225 tcon->max_bytes_chunk = 1048576;
1226 tcon->max_bytes_copy = 16777216;
1227}
1228
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001229int
1230SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1231 struct cifs_tcon *tcon, const struct nls_table *cp)
1232{
1233 struct smb2_tree_connect_req *req;
1234 struct smb2_tree_connect_rsp *rsp = NULL;
1235 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001236 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001237 int rc = 0;
1238 int resp_buftype;
1239 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001240 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001241 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001242 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001243
Joe Perchesf96637b2013-05-04 22:12:25 -05001244 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001245
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001246 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001247 return -EIO;
1248
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001249 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1250 if (unc_path == NULL)
1251 return -ENOMEM;
1252
1253 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1254 unc_path_len *= 2;
1255 if (unc_path_len < 2) {
1256 kfree(unc_path);
1257 return -EINVAL;
1258 }
1259
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001260 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
Aurelien Aptelb327a712018-01-24 13:46:10 +01001261 tcon->tid = 0;
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001262
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001263 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1264 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001265 if (rc) {
1266 kfree(unc_path);
1267 return rc;
1268 }
1269
Aurelien Aptelb327a712018-01-24 13:46:10 +01001270 if (encryption_required(tcon))
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001271 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001272
1273 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001274 /* 1 for pad */
1275 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001276
1277 /* Testing shows that buffer offset must be at location of Buffer[0] */
1278 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001279 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001280 req->PathLength = cpu_to_le16(unc_path_len - 2);
1281 iov[1].iov_base = unc_path;
1282 iov[1].iov_len = unc_path_len;
1283
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001284 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001285 cifs_small_buf_release(req);
1286 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001287
1288 if (rc != 0) {
1289 if (tcon) {
1290 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1291 tcon->need_reconnect = true;
1292 }
1293 goto tcon_error_exit;
1294 }
1295
Christophe JAILLETcd123002017-05-12 17:59:32 +02001296 switch (rsp->ShareType) {
1297 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001298 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001299 break;
1300 case SMB2_SHARE_TYPE_PIPE:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001301 tcon->pipe = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001302 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001303 break;
1304 case SMB2_SHARE_TYPE_PRINT:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001305 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001306 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001307 break;
1308 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001309 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001310 rc = -EOPNOTSUPP;
1311 goto tcon_error_exit;
1312 }
1313
1314 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001315 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001316 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1317 tcon->tidStatus = CifsGood;
1318 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001319 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001320 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001321
1322 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1323 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001324 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001325
1326 if (tcon->seal &&
1327 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1328 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1329
Steve Frenchde9f68d2013-11-15 11:26:24 -06001330 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001331 if (tcon->ses->server->ops->validate_negotiate)
1332 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001333tcon_exit:
1334 free_rsp_buf(resp_buftype, rsp);
1335 kfree(unc_path);
1336 return rc;
1337
1338tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001339 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001340 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001341 }
1342 goto tcon_exit;
1343}
1344
1345int
1346SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1347{
1348 struct smb2_tree_disconnect_req *req; /* response is trivial */
1349 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001350 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001351 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001352 unsigned int total_len;
1353 struct kvec iov[1];
1354 struct kvec rsp_iov;
1355 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001356
Joe Perchesf96637b2013-05-04 22:12:25 -05001357 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001358
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001359 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001360 return -EIO;
1361
1362 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1363 return 0;
1364
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001365 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1366 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001367 if (rc)
1368 return rc;
1369
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001370 if (encryption_required(tcon))
1371 flags |= CIFS_TRANSFORM_REQ;
1372
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001373 flags |= CIFS_NO_RESP;
1374
1375 iov[0].iov_base = (char *)req;
1376 iov[0].iov_len = total_len;
1377
1378 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001379 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001380 if (rc)
1381 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1382
1383 return rc;
1384}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001385
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001386
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001387static struct create_durable *
1388create_durable_buf(void)
1389{
1390 struct create_durable *buf;
1391
1392 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1393 if (!buf)
1394 return NULL;
1395
1396 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001397 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001398 buf->ccontext.DataLength = cpu_to_le32(16);
1399 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1400 (struct create_durable, Name));
1401 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001402 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001403 buf->Name[0] = 'D';
1404 buf->Name[1] = 'H';
1405 buf->Name[2] = 'n';
1406 buf->Name[3] = 'Q';
1407 return buf;
1408}
1409
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001410static struct create_durable *
1411create_reconnect_durable_buf(struct cifs_fid *fid)
1412{
1413 struct create_durable *buf;
1414
1415 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1416 if (!buf)
1417 return NULL;
1418
1419 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1420 (struct create_durable, Data));
1421 buf->ccontext.DataLength = cpu_to_le32(16);
1422 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1423 (struct create_durable, Name));
1424 buf->ccontext.NameLength = cpu_to_le16(4);
1425 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1426 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001427 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001428 buf->Name[0] = 'D';
1429 buf->Name[1] = 'H';
1430 buf->Name[2] = 'n';
1431 buf->Name[3] = 'C';
1432 return buf;
1433}
1434
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001435static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001436parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1437 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001438{
1439 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001440 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001441 unsigned int next;
1442 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001443 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001444
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001445 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001446 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001447 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001448 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001449 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001450 if (le16_to_cpu(cc->NameLength) == 4 &&
1451 strncmp(name, "RqLs", 4) == 0)
1452 return server->ops->parse_lease_buf(cc, epoch);
1453
1454 next = le32_to_cpu(cc->Next);
1455 if (!next)
1456 break;
1457 remaining -= next;
1458 cc = (struct create_context *)((char *)cc + next);
1459 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001460
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001461 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001462}
1463
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001464static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001465add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1466 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001467{
1468 struct smb2_create_req *req = iov[0].iov_base;
1469 unsigned int num = *num_iovec;
1470
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001471 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001472 if (iov[num].iov_base == NULL)
1473 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001474 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001475 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1476 if (!req->CreateContextsOffset)
1477 req->CreateContextsOffset = cpu_to_le32(
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001478 sizeof(struct smb2_create_req) +
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001479 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001480 le32_add_cpu(&req->CreateContextsLength,
1481 server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001482 *num_iovec = num + 1;
1483 return 0;
1484}
1485
Steve Frenchb56eae42015-11-03 09:26:27 -06001486static struct create_durable_v2 *
1487create_durable_v2_buf(struct cifs_fid *pfid)
1488{
1489 struct create_durable_v2 *buf;
1490
1491 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1492 if (!buf)
1493 return NULL;
1494
1495 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1496 (struct create_durable_v2, dcontext));
1497 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1498 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1499 (struct create_durable_v2, Name));
1500 buf->ccontext.NameLength = cpu_to_le16(4);
1501
1502 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1503 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001504 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001505 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1506
1507 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1508 buf->Name[0] = 'D';
1509 buf->Name[1] = 'H';
1510 buf->Name[2] = '2';
1511 buf->Name[3] = 'Q';
1512 return buf;
1513}
1514
1515static struct create_durable_handle_reconnect_v2 *
1516create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1517{
1518 struct create_durable_handle_reconnect_v2 *buf;
1519
1520 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1521 GFP_KERNEL);
1522 if (!buf)
1523 return NULL;
1524
1525 buf->ccontext.DataOffset =
1526 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1527 dcontext));
1528 buf->ccontext.DataLength =
1529 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1530 buf->ccontext.NameOffset =
1531 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1532 Name));
1533 buf->ccontext.NameLength = cpu_to_le16(4);
1534
1535 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1536 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1537 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1538 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1539
1540 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1541 buf->Name[0] = 'D';
1542 buf->Name[1] = 'H';
1543 buf->Name[2] = '2';
1544 buf->Name[3] = 'C';
1545 return buf;
1546}
1547
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001548static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001549add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001550 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001551{
1552 struct smb2_create_req *req = iov[0].iov_base;
1553 unsigned int num = *num_iovec;
1554
Steve Frenchb56eae42015-11-03 09:26:27 -06001555 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1556 if (iov[num].iov_base == NULL)
1557 return -ENOMEM;
1558 iov[num].iov_len = sizeof(struct create_durable_v2);
1559 if (!req->CreateContextsOffset)
1560 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001561 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001562 iov[1].iov_len);
1563 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001564 *num_iovec = num + 1;
1565 return 0;
1566}
1567
1568static int
1569add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1570 struct cifs_open_parms *oparms)
1571{
1572 struct smb2_create_req *req = iov[0].iov_base;
1573 unsigned int num = *num_iovec;
1574
1575 /* indicate that we don't need to relock the file */
1576 oparms->reconnect = false;
1577
1578 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1579 if (iov[num].iov_base == NULL)
1580 return -ENOMEM;
1581 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1582 if (!req->CreateContextsOffset)
1583 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001584 cpu_to_le32(sizeof(struct smb2_create_req) +
Steve Frenchb56eae42015-11-03 09:26:27 -06001585 iov[1].iov_len);
1586 le32_add_cpu(&req->CreateContextsLength,
1587 sizeof(struct create_durable_handle_reconnect_v2));
Steve Frenchb56eae42015-11-03 09:26:27 -06001588 *num_iovec = num + 1;
1589 return 0;
1590}
1591
1592static int
1593add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1594 struct cifs_open_parms *oparms, bool use_persistent)
1595{
1596 struct smb2_create_req *req = iov[0].iov_base;
1597 unsigned int num = *num_iovec;
1598
1599 if (use_persistent) {
1600 if (oparms->reconnect)
1601 return add_durable_reconnect_v2_context(iov, num_iovec,
1602 oparms);
1603 else
1604 return add_durable_v2_context(iov, num_iovec, oparms);
1605 }
1606
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001607 if (oparms->reconnect) {
1608 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1609 /* indicate that we don't need to relock the file */
1610 oparms->reconnect = false;
1611 } else
1612 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001613 if (iov[num].iov_base == NULL)
1614 return -ENOMEM;
1615 iov[num].iov_len = sizeof(struct create_durable);
1616 if (!req->CreateContextsOffset)
1617 req->CreateContextsOffset =
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001618 cpu_to_le32(sizeof(struct smb2_create_req) +
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001619 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001620 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001621 *num_iovec = num + 1;
1622 return 0;
1623}
1624
Aurelien Aptelf0712922017-02-22 14:47:17 +01001625static int
1626alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1627 const char *treename, const __le16 *path)
1628{
1629 int treename_len, path_len;
1630 struct nls_table *cp;
1631 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1632
1633 /*
1634 * skip leading "\\"
1635 */
1636 treename_len = strlen(treename);
1637 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1638 return -EINVAL;
1639
1640 treename += 2;
1641 treename_len -= 2;
1642
1643 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1644
1645 /*
1646 * make room for one path separator between the treename and
1647 * path
1648 */
1649 *out_len = treename_len + 1 + path_len;
1650
1651 /*
1652 * final path needs to be null-terminated UTF16 with a
1653 * size aligned to 8
1654 */
1655
1656 *out_size = roundup((*out_len+1)*2, 8);
1657 *out_path = kzalloc(*out_size, GFP_KERNEL);
1658 if (!*out_path)
1659 return -ENOMEM;
1660
1661 cp = load_nls_default();
1662 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1663 UniStrcat(*out_path, sep);
1664 UniStrcat(*out_path, path);
1665 unload_nls(cp);
1666
1667 return 0;
1668}
1669
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001670int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001671SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001672 __u8 *oplock, struct smb2_file_all_info *buf,
1673 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001674{
1675 struct smb2_create_req *req;
1676 struct smb2_create_rsp *rsp;
1677 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001678 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001679 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001680 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001681 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001682 int resp_buftype;
1683 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001684 __le16 *copy_path = NULL;
1685 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001686 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001687 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001688 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001689 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001690 int flags = 0;
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001691 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001692
Joe Perchesf96637b2013-05-04 22:12:25 -05001693 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001694
1695 if (ses && (ses->server))
1696 server = ses->server;
1697 else
1698 return -EIO;
1699
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001700 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1701
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001702 if (rc)
1703 return rc;
1704
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001705 if (encryption_required(tcon))
1706 flags |= CIFS_TRANSFORM_REQ;
1707
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001708 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001709 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001710 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1711 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001712
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001713 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001714 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001715 /* File attributes ignored on open (used in create though) */
1716 req->FileAttributes = cpu_to_le32(file_attributes);
1717 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001718 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1719 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001720
1721 iov[0].iov_base = (char *)req;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001722 /* -1 since last byte is buf[0] which is sent below (path) */
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001723 iov[0].iov_len = total_len - 1;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001724
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001725 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
Aurelien Aptelf0712922017-02-22 14:47:17 +01001726
1727 /* [MS-SMB2] 2.2.13 NameOffset:
1728 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1729 * the SMB2 header, the file name includes a prefix that will
1730 * be processed during DFS name normalization as specified in
1731 * section 3.3.5.9. Otherwise, the file name is relative to
1732 * the share that is identified by the TreeId in the SMB2
1733 * header.
1734 */
1735 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1736 int name_len;
1737
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001738 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001739 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1740 &name_len,
1741 tcon->treeName, path);
1742 if (rc)
1743 return rc;
1744 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001745 uni_path_len = copy_size;
1746 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001747 } else {
1748 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1749 /* MUST set path len (NameLength) to 0 opening root of share */
1750 req->NameLength = cpu_to_le16(uni_path_len - 2);
1751 if (uni_path_len % 8 != 0) {
1752 copy_size = roundup(uni_path_len, 8);
1753 copy_path = kzalloc(copy_size, GFP_KERNEL);
1754 if (!copy_path)
1755 return -ENOMEM;
1756 memcpy((char *)copy_path, (const char *)path,
1757 uni_path_len);
1758 uni_path_len = copy_size;
1759 path = copy_path;
1760 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001761 }
1762
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001763 iov[1].iov_len = uni_path_len;
1764 iov[1].iov_base = path;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001765
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001766 if (!server->oplocks)
1767 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1768
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001769 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001770 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1771 req->RequestedOplockLevel = *oplock;
1772 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001773 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001774 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001775 cifs_small_buf_release(req);
1776 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001777 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001778 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001779 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001780 }
1781
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001782 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1783 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001784 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001785 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001786 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001787 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001788 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001789 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001790
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001791 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001792 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001793 if (rc) {
1794 cifs_small_buf_release(req);
1795 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001796 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001797 return rc;
1798 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001799 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001800 }
1801
Ronnie Sahlberg4f33bc32017-11-20 11:24:38 +11001802 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1803 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001804 cifs_small_buf_release(req);
1805 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001806
1807 if (rc != 0) {
1808 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001809 if (err_buf && rsp)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001810 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1811 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001812 goto creat_exit;
1813 }
1814
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001815 oparms->fid->persistent_fid = rsp->PersistentFileId;
1816 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001817
1818 if (buf) {
1819 memcpy(buf, &rsp->CreationTime, 32);
1820 buf->AllocationSize = rsp->AllocationSize;
1821 buf->EndOfFile = rsp->EndofFile;
1822 buf->Attributes = rsp->FileAttributes;
1823 buf->NumberOfLinks = cpu_to_le32(1);
1824 buf->DeletePending = 0;
1825 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001826
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001827 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001828 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001829 else
1830 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001831creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001832 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001833 kfree(lc_buf);
1834 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001835 free_rsp_buf(resp_buftype, rsp);
1836 return rc;
1837}
1838
Steve French4a72daf2013-06-25 00:20:49 -05001839/*
1840 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1841 */
1842int
1843SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001844 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1845 char *in_data, u32 indatalen,
1846 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001847{
1848 struct smb2_ioctl_req *req;
1849 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001850 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001851 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001852 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001853 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001854 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001855 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001856 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001857 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001858 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05001859
1860 cifs_dbg(FYI, "SMB2 IOCTL\n");
1861
Steve French3d1a3742014-08-11 21:05:25 -05001862 if (out_data != NULL)
1863 *out_data = NULL;
1864
Steve French4a72daf2013-06-25 00:20:49 -05001865 /* zero out returned data len, in case of error */
1866 if (plen)
1867 *plen = 0;
1868
Steve French8e353102015-03-26 19:47:02 -05001869 if (tcon)
1870 ses = tcon->ses;
1871 else
1872 return -EIO;
1873
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001874 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05001875 return -EIO;
1876
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001877 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05001878 if (rc)
1879 return rc;
1880
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001881 if (encryption_required(tcon))
1882 flags |= CIFS_TRANSFORM_REQ;
1883
Steve French4a72daf2013-06-25 00:20:49 -05001884 req->CtlCode = cpu_to_le32(opcode);
1885 req->PersistentFileId = persistent_fid;
1886 req->VolatileFileId = volatile_fid;
1887
1888 if (indatalen) {
1889 req->InputCount = cpu_to_le32(indatalen);
1890 /* do not set InputOffset if no input data */
1891 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001892 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05001893 iov[1].iov_base = in_data;
1894 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001895 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001896 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001897 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001898
1899 req->OutputOffset = 0;
1900 req->OutputCount = 0; /* MBZ */
1901
1902 /*
1903 * Could increase MaxOutputResponse, but that would require more
1904 * than one credit. Windows typically sets this smaller, but for some
1905 * ioctls it may be useful to allow server to send more. No point
1906 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001907 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1908 * (by default, note that it can be overridden to make max larger)
1909 * in responses (except for read responses which can be bigger.
1910 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001911 */
Steve French7db0a6e2017-05-03 21:12:20 -05001912 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001913
1914 if (is_fsctl)
1915 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1916 else
1917 req->Flags = 0;
1918
1919 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001920
Steve French7ff8d452013-10-14 00:44:19 -05001921 /*
1922 * If no input data, the size of ioctl struct in
1923 * protocol spec still includes a 1 byte data buffer,
1924 * but if input data passed to ioctl, we do not
1925 * want to double count this, so we do not send
1926 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001927 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05001928 */
1929
1930 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001931 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05001932 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001933 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05001934
Steve French4587eee2017-10-25 15:58:31 -05001935 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1936 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001937 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05001938
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001939 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1940 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001941 cifs_small_buf_release(req);
1942 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001943
Steve French9bf0c9c2013-11-16 18:05:28 -06001944 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001945 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001946 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001947 } else if (rc == -EINVAL) {
1948 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1949 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001950 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001951 goto ioctl_exit;
1952 }
Steve French4a72daf2013-06-25 00:20:49 -05001953 }
1954
1955 /* check if caller wants to look at return data or just return rc */
1956 if ((plen == NULL) || (out_data == NULL))
1957 goto ioctl_exit;
1958
1959 *plen = le32_to_cpu(rsp->OutputCount);
1960
1961 /* We check for obvious errors in the output buffer length and offset */
1962 if (*plen == 0)
1963 goto ioctl_exit; /* server returned no data */
1964 else if (*plen > 0xFF00) {
1965 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1966 *plen = 0;
1967 rc = -EIO;
1968 goto ioctl_exit;
1969 }
1970
1971 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1972 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1973 le32_to_cpu(rsp->OutputOffset));
1974 *plen = 0;
1975 rc = -EIO;
1976 goto ioctl_exit;
1977 }
1978
1979 *out_data = kmalloc(*plen, GFP_KERNEL);
1980 if (*out_data == NULL) {
1981 rc = -ENOMEM;
1982 goto ioctl_exit;
1983 }
1984
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001985 shdr = get_sync_hdr(rsp);
1986 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05001987ioctl_exit:
1988 free_rsp_buf(resp_buftype, rsp);
1989 return rc;
1990}
1991
Steve French64a5cfa2013-10-14 15:31:32 -05001992/*
1993 * Individual callers to ioctl worker function follow
1994 */
1995
1996int
1997SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1998 u64 persistent_fid, u64 volatile_fid)
1999{
2000 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002001 struct compress_ioctl fsctl_input;
2002 char *ret_data = NULL;
2003
2004 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002005 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002006
2007 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2008 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002009 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05002010 (char *)&fsctl_input /* data input */,
2011 2 /* in data len */, &ret_data /* out data */, NULL);
2012
2013 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002014
2015 return rc;
2016}
2017
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002018int
2019SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2020 u64 persistent_fid, u64 volatile_fid)
2021{
2022 struct smb2_close_req *req;
2023 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002024 struct cifs_ses *ses = tcon->ses;
2025 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002026 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002027 int resp_buftype;
2028 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002029 int flags = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002030 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002031
Joe Perchesf96637b2013-05-04 22:12:25 -05002032 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002033
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002034 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002035 return -EIO;
2036
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002037 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002038 if (rc)
2039 return rc;
2040
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002041 if (encryption_required(tcon))
2042 flags |= CIFS_TRANSFORM_REQ;
2043
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002044 req->PersistentFileId = persistent_fid;
2045 req->VolatileFileId = volatile_fid;
2046
2047 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002048 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002049
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002050 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002051 cifs_small_buf_release(req);
2052 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002053
2054 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002055 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002056 goto close_exit;
2057 }
2058
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002059 /* BB FIXME - decode close response, update inode for caching */
2060
2061close_exit:
2062 free_rsp_buf(resp_buftype, rsp);
2063 return rc;
2064}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002065
2066static int
2067validate_buf(unsigned int offset, unsigned int buffer_length,
2068 struct smb2_hdr *hdr, unsigned int min_buf_size)
2069
2070{
2071 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2072 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2073 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2074 char *end_of_buf = begin_of_buf + buffer_length;
2075
2076
2077 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002078 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2079 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002080 return -EINVAL;
2081 }
2082
2083 /* check if beyond RFC1001 maximum length */
2084 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002085 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2086 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002087 return -EINVAL;
2088 }
2089
2090 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002091 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002092 return -EINVAL;
2093 }
2094
2095 return 0;
2096}
2097
2098/*
2099 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2100 * Caller must free buffer.
2101 */
2102static int
2103validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2104 struct smb2_hdr *hdr, unsigned int minbufsize,
2105 char *data)
2106
2107{
2108 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2109 int rc;
2110
2111 if (!data)
2112 return -EINVAL;
2113
2114 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2115 if (rc)
2116 return rc;
2117
2118 memcpy(data, begin_of_buf, buffer_length);
2119
2120 return 0;
2121}
2122
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002123static int
2124query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002125 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2126 u32 additional_info, size_t output_len, size_t min_len, void **data,
2127 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002128{
2129 struct smb2_query_info_req *req;
2130 struct smb2_query_info_rsp *rsp = NULL;
2131 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002132 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002133 int rc = 0;
2134 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002135 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002136 int flags = 0;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002137 unsigned int total_len;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002138
Joe Perchesf96637b2013-05-04 22:12:25 -05002139 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002140
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002141 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002142 return -EIO;
2143
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002144 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2145 &total_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002146 if (rc)
2147 return rc;
2148
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002149 if (encryption_required(tcon))
2150 flags |= CIFS_TRANSFORM_REQ;
2151
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002152 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002153 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002154 req->PersistentFileId = persistent_fid;
2155 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002156 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002157
2158 /*
2159 * We do not use the input buffer (do not send extra byte)
2160 */
2161 req->InputBufferOffset = 0;
Aurelien Aptel48923d22017-10-17 14:47:17 +02002162
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002163 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002164
2165 iov[0].iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002166 /* 1 for Buffer */
2167 iov[0].iov_len = total_len - 1;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002168
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11002169 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002170 cifs_small_buf_release(req);
2171 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002172
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002173 if (rc) {
2174 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2175 goto qinf_exit;
2176 }
2177
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002178 if (dlen) {
2179 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2180 if (!*data) {
2181 *data = kmalloc(*dlen, GFP_KERNEL);
2182 if (!*data) {
2183 cifs_dbg(VFS,
2184 "Error %d allocating memory for acl\n",
2185 rc);
2186 *dlen = 0;
2187 goto qinf_exit;
2188 }
2189 }
2190 }
2191
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002192 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2193 le32_to_cpu(rsp->OutputBufferLength),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002194 &rsp->hdr, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002195
2196qinf_exit:
2197 free_rsp_buf(resp_buftype, rsp);
2198 return rc;
2199}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002200
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002201int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002202 u64 persistent_fid, u64 volatile_fid,
2203 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002204{
2205 return query_info(xid, tcon, persistent_fid, volatile_fid,
2206 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002207 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002208 sizeof(struct smb2_file_full_ea_info),
2209 (void **)&data,
2210 NULL);
2211}
2212
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002213int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2214 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002215{
2216 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002217 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002218 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002219 sizeof(struct smb2_file_all_info), (void **)&data,
2220 NULL);
2221}
2222
2223int
2224SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2225 u64 persistent_fid, u64 volatile_fid,
2226 void **data, u32 *plen)
2227{
2228 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2229 *plen = 0;
2230
2231 return query_info(xid, tcon, persistent_fid, volatile_fid,
2232 0, SMB2_O_INFO_SECURITY, additional_info,
2233 SMB2_MAX_BUFFER_SIZE,
2234 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002235}
2236
2237int
2238SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2239 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2240{
2241 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002242 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002243 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002244 sizeof(struct smb2_file_internal_info),
2245 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002246}
2247
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002248/*
2249 * This is a no-op for now. We're not really interested in the reply, but
2250 * rather in the fact that the server sent one and that server->lstrp
2251 * gets updated.
2252 *
2253 * FIXME: maybe we should consider checking that the reply matches request?
2254 */
2255static void
2256smb2_echo_callback(struct mid_q_entry *mid)
2257{
2258 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002259 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002260 unsigned int credits_received = 1;
2261
2262 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002263 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002264
2265 DeleteMidQEntry(mid);
2266 add_credits(server, credits_received, CIFS_ECHO_OP);
2267}
2268
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002269void smb2_reconnect_server(struct work_struct *work)
2270{
2271 struct TCP_Server_Info *server = container_of(work,
2272 struct TCP_Server_Info, reconnect.work);
2273 struct cifs_ses *ses;
2274 struct cifs_tcon *tcon, *tcon2;
2275 struct list_head tmp_list;
2276 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002277 int rc;
2278 int resched = false;
2279
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002280
2281 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2282 mutex_lock(&server->reconnect_mutex);
2283
2284 INIT_LIST_HEAD(&tmp_list);
2285 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2286
2287 spin_lock(&cifs_tcp_ses_lock);
2288 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2289 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002290 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002291 tcon->tc_count++;
2292 list_add_tail(&tcon->rlist, &tmp_list);
2293 tcon_exist = true;
2294 }
2295 }
Aurelien Aptelb327a712018-01-24 13:46:10 +01002296 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2297 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2298 tcon_exist = true;
2299 }
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002300 }
2301 /*
2302 * Get the reference to server struct to be sure that the last call of
2303 * cifs_put_tcon() in the loop below won't release the server pointer.
2304 */
2305 if (tcon_exist)
2306 server->srv_count++;
2307
2308 spin_unlock(&cifs_tcp_ses_lock);
2309
2310 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002311 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2312 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002313 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002314 else
2315 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002316 list_del_init(&tcon->rlist);
2317 cifs_put_tcon(tcon);
2318 }
2319
2320 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002321 if (resched)
2322 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002323 mutex_unlock(&server->reconnect_mutex);
2324
2325 /* now we can safely release srv struct */
2326 if (tcon_exist)
2327 cifs_put_tcp_session(server, 1);
2328}
2329
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002330int
2331SMB2_echo(struct TCP_Server_Info *server)
2332{
2333 struct smb2_echo_req *req;
2334 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002335 struct kvec iov[2];
2336 struct smb_rqst rqst = { .rq_iov = iov,
2337 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002338 unsigned int total_len;
2339 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002340
Joe Perchesf96637b2013-05-04 22:12:25 -05002341 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002342
Steve French4fcd1812016-06-22 20:12:05 -05002343 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002344 /* No need to send echo on newly established connections */
2345 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2346 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002347 }
2348
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002349 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002350 if (rc)
2351 return rc;
2352
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002353 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002354
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002355 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002356 rfc1002_marker = cpu_to_be32(total_len);
2357 iov[0].iov_base = &rfc1002_marker;
2358 iov[1].iov_len = total_len;
2359 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002360
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002361 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2362 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002363 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002364 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002365
2366 cifs_small_buf_release(req);
2367 return rc;
2368}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002369
2370int
2371SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2372 u64 volatile_fid)
2373{
2374 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002375 struct cifs_ses *ses = tcon->ses;
2376 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002377 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002378 int resp_buftype;
2379 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002380 int flags = 0;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002381 unsigned int total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002382
Joe Perchesf96637b2013-05-04 22:12:25 -05002383 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002384
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002385 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002386 return -EIO;
2387
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002388 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002389 if (rc)
2390 return rc;
2391
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002392 if (encryption_required(tcon))
2393 flags |= CIFS_TRANSFORM_REQ;
2394
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002395 req->PersistentFileId = persistent_fid;
2396 req->VolatileFileId = volatile_fid;
2397
2398 iov[0].iov_base = (char *)req;
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002399 iov[0].iov_len = total_len;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002400
Ronnie Sahlberg1f444e42017-11-20 11:24:39 +11002401 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002402 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002403
Steve Frenchdfebe402015-03-27 01:00:06 -05002404 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002405 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2406
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002407 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002408 return rc;
2409}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002410
2411/*
2412 * To form a chain of read requests, any read requests after the first should
2413 * have the end_of_chain boolean set to true.
2414 */
2415static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002416smb2_new_read_req(void **buf, unsigned int *total_len,
Long Li2dabfd52017-11-07 01:54:53 -07002417 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2418 unsigned int remaining_bytes, int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002419{
2420 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002421 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002422 struct smb2_sync_hdr *shdr;
Long Li2dabfd52017-11-07 01:54:53 -07002423 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002424
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002425 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2426 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002427 if (rc)
2428 return rc;
Long Li2dabfd52017-11-07 01:54:53 -07002429
2430 server = io_parms->tcon->ses->server;
2431 if (server == NULL)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002432 return -ECONNABORTED;
2433
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002434 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002435 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002436
2437 req->PersistentFileId = io_parms->persistent_fid;
2438 req->VolatileFileId = io_parms->volatile_fid;
2439 req->ReadChannelInfoOffset = 0; /* reserved */
2440 req->ReadChannelInfoLength = 0; /* reserved */
2441 req->Channel = 0; /* reserved */
2442 req->MinimumCount = 0;
2443 req->Length = cpu_to_le32(io_parms->length);
2444 req->Offset = cpu_to_le64(io_parms->offset);
Long Libd3dcc62017-11-22 17:38:47 -07002445#ifdef CONFIG_CIFS_SMB_DIRECT
2446 /*
2447 * If we want to do a RDMA write, fill in and append
2448 * smbd_buffer_descriptor_v1 to the end of read request
2449 */
2450 if (server->rdma && rdata &&
2451 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002452
Long Libd3dcc62017-11-22 17:38:47 -07002453 struct smbd_buffer_descriptor_v1 *v1;
2454 bool need_invalidate =
2455 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2456
2457 rdata->mr = smbd_register_mr(
2458 server->smbd_conn, rdata->pages,
2459 rdata->nr_pages, rdata->tailsz,
2460 true, need_invalidate);
2461 if (!rdata->mr)
2462 return -ENOBUFS;
2463
2464 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2465 if (need_invalidate)
2466 req->Channel = SMB2_CHANNEL_RDMA_V1;
2467 req->ReadChannelInfoOffset =
2468 offsetof(struct smb2_read_plain_req, Buffer);
2469 req->ReadChannelInfoLength =
2470 sizeof(struct smbd_buffer_descriptor_v1);
2471 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2472 v1->offset = rdata->mr->mr->iova;
2473 v1->token = rdata->mr->mr->rkey;
2474 v1->length = rdata->mr->mr->length;
2475
2476 *total_len += sizeof(*v1) - 1;
2477 }
2478#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002479 if (request_type & CHAINED_REQUEST) {
2480 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002481 /* next 8-byte aligned request */
2482 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2483 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002484 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002485 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002486 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002487 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002488 /*
2489 * Related requests use info from previous read request
2490 * in chain.
2491 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002492 shdr->SessionId = 0xFFFFFFFF;
2493 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002494 req->PersistentFileId = 0xFFFFFFFF;
2495 req->VolatileFileId = 0xFFFFFFFF;
2496 }
2497 }
2498 if (remaining_bytes > io_parms->length)
2499 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2500 else
2501 req->RemainingBytes = 0;
2502
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002503 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002504 return rc;
2505}
2506
2507static void
2508smb2_readv_callback(struct mid_q_entry *mid)
2509{
2510 struct cifs_readdata *rdata = mid->callback_data;
2511 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2512 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002513 struct smb2_sync_hdr *shdr =
2514 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002515 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002516 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2517 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002518 .rq_pages = rdata->pages,
2519 .rq_npages = rdata->nr_pages,
2520 .rq_pagesz = rdata->pagesz,
2521 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002522
Joe Perchesf96637b2013-05-04 22:12:25 -05002523 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2524 __func__, mid->mid, mid->mid_state, rdata->result,
2525 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002526
2527 switch (mid->mid_state) {
2528 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002529 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002530 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002531 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002532 int rc;
2533
Jeff Layton0b688cf2012-09-18 16:20:34 -07002534 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002535 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002536 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2537 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002538 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002539 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002540 task_io_account_read(rdata->got_bytes);
2541 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002542 break;
2543 case MID_REQUEST_SUBMITTED:
2544 case MID_RETRY_NEEDED:
2545 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002546 if (server->sign && rdata->got_bytes)
2547 /* reset bytes number since we can not check a sign */
2548 rdata->got_bytes = 0;
2549 /* FIXME: should this be counted toward the initiating task? */
2550 task_io_account_read(rdata->got_bytes);
2551 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002552 break;
2553 default:
2554 if (rdata->result != -ENODATA)
2555 rdata->result = -EIO;
2556 }
Long Libd3dcc62017-11-22 17:38:47 -07002557#ifdef CONFIG_CIFS_SMB_DIRECT
2558 /*
2559 * If this rdata has a memmory registered, the MR can be freed
2560 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2561 * because they have limited number and are used for future I/Os
2562 */
2563 if (rdata->mr) {
2564 smbd_deregister_mr(rdata->mr);
2565 rdata->mr = NULL;
2566 }
2567#endif
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002568 if (rdata->result)
2569 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2570
2571 queue_work(cifsiod_wq, &rdata->work);
2572 DeleteMidQEntry(mid);
2573 add_credits(server, credits_received, 0);
2574}
2575
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002576/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002577int
2578smb2_async_readv(struct cifs_readdata *rdata)
2579{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002580 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002581 char *buf;
2582 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002583 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002584 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2585 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002586 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002587 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002588 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002589
Joe Perchesf96637b2013-05-04 22:12:25 -05002590 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2591 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002592
2593 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2594 io_parms.offset = rdata->offset;
2595 io_parms.length = rdata->bytes;
2596 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2597 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2598 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002599
2600 server = io_parms.tcon->ses->server;
2601
Long Li2dabfd52017-11-07 01:54:53 -07002602 rc = smb2_new_read_req(
2603 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002604 if (rc) {
2605 if (rc == -EAGAIN && rdata->credits) {
2606 /* credits was reset by reconnect */
2607 rdata->credits = 0;
2608 /* reduce in_flight value since we won't send the req */
2609 spin_lock(&server->req_lock);
2610 server->in_flight--;
2611 spin_unlock(&server->req_lock);
2612 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002613 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002614 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002615
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002616 if (encryption_required(io_parms.tcon))
2617 flags |= CIFS_TRANSFORM_REQ;
2618
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002619 req_len = cpu_to_be32(total_len);
2620
2621 rdata->iov[0].iov_base = &req_len;
2622 rdata->iov[0].iov_len = sizeof(__be32);
2623 rdata->iov[1].iov_base = buf;
2624 rdata->iov[1].iov_len = total_len;
2625
2626 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002627
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002628 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002629 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002630 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002631 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002632 spin_lock(&server->req_lock);
2633 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002634 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002635 spin_unlock(&server->req_lock);
2636 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002637 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002638 }
2639
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002640 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002641 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002642 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002643 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002644 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002645 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002646 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2647 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002648
2649 cifs_small_buf_release(buf);
2650 return rc;
2651}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002652
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002653int
2654SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2655 unsigned int *nbytes, char **buf, int *buf_type)
2656{
2657 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002658 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002659 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002660 struct smb2_sync_hdr *shdr;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002661 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002662 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002663 unsigned int total_len;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002664 int flags = CIFS_LOG_ERROR;
2665 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002666
2667 *nbytes = 0;
Long Li2dabfd52017-11-07 01:54:53 -07002668 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002669 if (rc)
2670 return rc;
2671
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002672 if (encryption_required(io_parms->tcon))
2673 flags |= CIFS_TRANSFORM_REQ;
2674
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002675 iov[0].iov_base = (char *)req;
2676 iov[0].iov_len = total_len;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002677
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002678 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002679 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002680
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002681 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002682
2683 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002684 if (rc != -ENODATA) {
2685 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2686 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002687 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002688 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2689 return rc == -ENODATA ? 0 : rc;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002690 }
2691
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002692 *nbytes = le32_to_cpu(rsp->DataLength);
2693 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2694 (*nbytes > io_parms->length)) {
2695 cifs_dbg(FYI, "bad length %d for count %d\n",
2696 *nbytes, io_parms->length);
2697 rc = -EIO;
2698 *nbytes = 0;
2699 }
2700
2701 shdr = get_sync_hdr(rsp);
2702
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002703 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002704 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002705 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002706 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002707 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002708 if (resp_buftype == CIFS_SMALL_BUFFER)
2709 *buf_type = CIFS_SMALL_BUFFER;
2710 else if (resp_buftype == CIFS_LARGE_BUFFER)
2711 *buf_type = CIFS_LARGE_BUFFER;
2712 }
2713 return rc;
2714}
2715
Pavel Shilovsky33319142012-09-18 16:20:29 -07002716/*
2717 * Check the mid_state and signature on received buffer (if any), and queue the
2718 * workqueue completion task.
2719 */
2720static void
2721smb2_writev_callback(struct mid_q_entry *mid)
2722{
2723 struct cifs_writedata *wdata = mid->callback_data;
2724 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2725 unsigned int written;
2726 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2727 unsigned int credits_received = 1;
2728
2729 switch (mid->mid_state) {
2730 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002731 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002732 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2733 if (wdata->result != 0)
2734 break;
2735
2736 written = le32_to_cpu(rsp->DataLength);
2737 /*
2738 * Mask off high 16 bits when bytes written as returned
2739 * by the server is greater than bytes requested by the
2740 * client. OS/2 servers are known to set incorrect
2741 * CountHigh values.
2742 */
2743 if (written > wdata->bytes)
2744 written &= 0xFFFF;
2745
2746 if (written < wdata->bytes)
2747 wdata->result = -ENOSPC;
2748 else
2749 wdata->bytes = written;
2750 break;
2751 case MID_REQUEST_SUBMITTED:
2752 case MID_RETRY_NEEDED:
2753 wdata->result = -EAGAIN;
2754 break;
2755 default:
2756 wdata->result = -EIO;
2757 break;
2758 }
Long Lidb223a52017-11-22 17:38:45 -07002759#ifdef CONFIG_CIFS_SMB_DIRECT
2760 /*
2761 * If this wdata has a memory registered, the MR can be freed
2762 * The number of MRs available is limited, it's important to recover
2763 * used MR as soon as I/O is finished. Hold MR longer in the later
2764 * I/O process can possibly result in I/O deadlock due to lack of MR
2765 * to send request on I/O retry
2766 */
2767 if (wdata->mr) {
2768 smbd_deregister_mr(wdata->mr);
2769 wdata->mr = NULL;
2770 }
2771#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002772 if (wdata->result)
2773 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2774
2775 queue_work(cifsiod_wq, &wdata->work);
2776 DeleteMidQEntry(mid);
2777 add_credits(tcon->ses->server, credits_received, 0);
2778}
2779
2780/* smb2_async_writev - send an async write, and set up mid to handle result */
2781int
Steve French4a5c80d2014-02-07 20:45:12 -06002782smb2_async_writev(struct cifs_writedata *wdata,
2783 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002784{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002785 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002786 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002787 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002788 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002789 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002790 struct kvec iov[2];
2791 struct smb_rqst rqst = { };
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002792 unsigned int total_len;
2793 __be32 rfc1002_marker;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002794
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002795 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002796 if (rc) {
2797 if (rc == -EAGAIN && wdata->credits) {
2798 /* credits was reset by reconnect */
2799 wdata->credits = 0;
2800 /* reduce in_flight value since we won't send the req */
2801 spin_lock(&server->req_lock);
2802 server->in_flight--;
2803 spin_unlock(&server->req_lock);
2804 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002805 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002806 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002807
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002808 if (encryption_required(tcon))
2809 flags |= CIFS_TRANSFORM_REQ;
2810
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002811 shdr = (struct smb2_sync_hdr *)req;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002812 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002813
2814 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2815 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2816 req->WriteChannelInfoOffset = 0;
2817 req->WriteChannelInfoLength = 0;
2818 req->Channel = 0;
2819 req->Offset = cpu_to_le64(wdata->offset);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002820 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002821 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky33319142012-09-18 16:20:29 -07002822 req->RemainingBytes = 0;
Long Lidb223a52017-11-22 17:38:45 -07002823#ifdef CONFIG_CIFS_SMB_DIRECT
2824 /*
2825 * If we want to do a server RDMA read, fill in and append
2826 * smbd_buffer_descriptor_v1 to the end of write request
2827 */
2828 if (server->rdma && wdata->bytes >=
2829 server->smbd_conn->rdma_readwrite_threshold) {
Pavel Shilovsky33319142012-09-18 16:20:29 -07002830
Long Lidb223a52017-11-22 17:38:45 -07002831 struct smbd_buffer_descriptor_v1 *v1;
2832 bool need_invalidate = server->dialect == SMB30_PROT_ID;
2833
2834 wdata->mr = smbd_register_mr(
2835 server->smbd_conn, wdata->pages,
2836 wdata->nr_pages, wdata->tailsz,
2837 false, need_invalidate);
2838 if (!wdata->mr) {
2839 rc = -ENOBUFS;
2840 goto async_writev_out;
2841 }
2842 req->Length = 0;
2843 req->DataOffset = 0;
2844 req->RemainingBytes =
2845 (wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz;
2846 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2847 if (need_invalidate)
2848 req->Channel = SMB2_CHANNEL_RDMA_V1;
2849 req->WriteChannelInfoOffset =
2850 offsetof(struct smb2_write_req, Buffer);
2851 req->WriteChannelInfoLength =
2852 sizeof(struct smbd_buffer_descriptor_v1);
2853 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2854 v1->offset = wdata->mr->mr->iova;
2855 v1->token = wdata->mr->mr->rkey;
2856 v1->length = wdata->mr->mr->length;
2857 }
2858#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002859 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002860 iov[0].iov_len = 4;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002861 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
2862 iov[0].iov_base = &rfc1002_marker;
2863 iov[1].iov_len = total_len - 1;
2864 iov[1].iov_base = (char *)req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002865
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002866 rqst.rq_iov = iov;
2867 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002868 rqst.rq_pages = wdata->pages;
2869 rqst.rq_npages = wdata->nr_pages;
2870 rqst.rq_pagesz = wdata->pagesz;
2871 rqst.rq_tailsz = wdata->tailsz;
Long Lidb223a52017-11-22 17:38:45 -07002872#ifdef CONFIG_CIFS_SMB_DIRECT
2873 if (wdata->mr) {
2874 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
2875 rqst.rq_npages = 0;
2876 }
2877#endif
Joe Perchesf96637b2013-05-04 22:12:25 -05002878 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2879 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002880
Long Lidb223a52017-11-22 17:38:45 -07002881#ifdef CONFIG_CIFS_SMB_DIRECT
2882 /* For RDMA read, I/O size is in RemainingBytes not in Length */
2883 if (!wdata->mr)
2884 req->Length = cpu_to_le32(wdata->bytes);
2885#else
Pavel Shilovsky33319142012-09-18 16:20:29 -07002886 req->Length = cpu_to_le32(wdata->bytes);
Long Lidb223a52017-11-22 17:38:45 -07002887#endif
Pavel Shilovsky33319142012-09-18 16:20:29 -07002888
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002889 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002890 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002891 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002892 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002893 spin_lock(&server->req_lock);
2894 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002895 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002896 spin_unlock(&server->req_lock);
2897 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002898 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002899 }
2900
Pavel Shilovsky33319142012-09-18 16:20:29 -07002901 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002902 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2903 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002904
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002905 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002906 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002907 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2908 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002909
Pavel Shilovsky33319142012-09-18 16:20:29 -07002910async_writev_out:
2911 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002912 return rc;
2913}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002914
2915/*
2916 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2917 * The length field from io_parms must be at least 1 and indicates a number of
2918 * elements with data to write that begins with position 1 in iov array. All
2919 * data length is specified by count.
2920 */
2921int
2922SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2923 unsigned int *nbytes, struct kvec *iov, int n_vec)
2924{
2925 int rc = 0;
2926 struct smb2_write_req *req = NULL;
2927 struct smb2_write_rsp *rsp = NULL;
2928 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002929 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002930 int flags = 0;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002931 unsigned int total_len;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002932
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002933 *nbytes = 0;
2934
2935 if (n_vec < 1)
2936 return rc;
2937
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002938 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
2939 &total_len);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002940 if (rc)
2941 return rc;
2942
2943 if (io_parms->tcon->ses->server == NULL)
2944 return -ECONNABORTED;
2945
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002946 if (encryption_required(io_parms->tcon))
2947 flags |= CIFS_TRANSFORM_REQ;
2948
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002949 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002950
2951 req->PersistentFileId = io_parms->persistent_fid;
2952 req->VolatileFileId = io_parms->volatile_fid;
2953 req->WriteChannelInfoOffset = 0;
2954 req->WriteChannelInfoLength = 0;
2955 req->Channel = 0;
2956 req->Length = cpu_to_le32(io_parms->length);
2957 req->Offset = cpu_to_le64(io_parms->offset);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002958 req->DataOffset = cpu_to_le16(
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002959 offsetof(struct smb2_write_req, Buffer));
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002960 req->RemainingBytes = 0;
2961
2962 iov[0].iov_base = (char *)req;
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002963 /* 1 for Buffer */
2964 iov[0].iov_len = total_len - 1;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002965
Ronnie Sahlbergf5688a62017-11-20 11:24:41 +11002966 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
2967 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002968 cifs_small_buf_release(req);
2969 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002970
2971 if (rc) {
2972 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002973 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002974 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002975 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002976
2977 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002978 return rc;
2979}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002980
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002981static unsigned int
2982num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2983{
2984 int len;
2985 unsigned int entrycount = 0;
2986 unsigned int next_offset = 0;
2987 FILE_DIRECTORY_INFO *entryptr;
2988
2989 if (bufstart == NULL)
2990 return 0;
2991
2992 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2993
2994 while (1) {
2995 entryptr = (FILE_DIRECTORY_INFO *)
2996 ((char *)entryptr + next_offset);
2997
2998 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002999 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003000 break;
3001 }
3002
3003 len = le32_to_cpu(entryptr->FileNameLength);
3004 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003005 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3006 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003007 break;
3008 }
3009
3010 *lastentry = (char *)entryptr;
3011 entrycount++;
3012
3013 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3014 if (!next_offset)
3015 break;
3016 }
3017
3018 return entrycount;
3019}
3020
3021/*
3022 * Readdir/FindFirst
3023 */
3024int
3025SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3026 u64 persistent_fid, u64 volatile_fid, int index,
3027 struct cifs_search_info *srch_inf)
3028{
3029 struct smb2_query_directory_req *req;
3030 struct smb2_query_directory_rsp *rsp = NULL;
3031 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003032 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003033 int rc = 0;
3034 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003035 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003036 unsigned char *bufptr;
3037 struct TCP_Server_Info *server;
3038 struct cifs_ses *ses = tcon->ses;
3039 __le16 asteriks = cpu_to_le16('*');
3040 char *end_of_smb;
3041 unsigned int output_size = CIFSMaxBufSize;
3042 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003043 int flags = 0;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003044 unsigned int total_len;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003045
3046 if (ses && (ses->server))
3047 server = ses->server;
3048 else
3049 return -EIO;
3050
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003051 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3052 &total_len);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003053 if (rc)
3054 return rc;
3055
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003056 if (encryption_required(tcon))
3057 flags |= CIFS_TRANSFORM_REQ;
3058
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003059 switch (srch_inf->info_level) {
3060 case SMB_FIND_FILE_DIRECTORY_INFO:
3061 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3062 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3063 break;
3064 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3065 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3066 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3067 break;
3068 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003069 cifs_dbg(VFS, "info level %u isn't supported\n",
3070 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003071 rc = -EINVAL;
3072 goto qdir_exit;
3073 }
3074
3075 req->FileIndex = cpu_to_le32(index);
3076 req->PersistentFileId = persistent_fid;
3077 req->VolatileFileId = volatile_fid;
3078
3079 len = 0x2;
3080 bufptr = req->Buffer;
3081 memcpy(bufptr, &asteriks, len);
3082
3083 req->FileNameOffset =
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003084 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003085 req->FileNameLength = cpu_to_le16(len);
3086 /*
3087 * BB could be 30 bytes or so longer if we used SMB2 specific
3088 * buffer lengths, but this is safe and close enough.
3089 */
3090 output_size = min_t(unsigned int, output_size, server->maxBuf);
3091 output_size = min_t(unsigned int, output_size, 2 << 15);
3092 req->OutputBufferLength = cpu_to_le32(output_size);
3093
3094 iov[0].iov_base = (char *)req;
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003095 /* 1 for Buffer */
3096 iov[0].iov_len = total_len - 1;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003097
3098 iov[1].iov_base = (char *)(req->Buffer);
3099 iov[1].iov_len = len;
3100
Ronnie Sahlberg7c00c3a2017-11-20 11:24:45 +11003101 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003102 cifs_small_buf_release(req);
3103 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003104
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003105 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003106 if (rc == -ENODATA &&
3107 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003108 srch_inf->endOfSearch = true;
3109 rc = 0;
3110 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003111 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3112 goto qdir_exit;
3113 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003114
3115 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3116 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3117 info_buf_size);
3118 if (rc)
3119 goto qdir_exit;
3120
3121 srch_inf->unicode = true;
3122
3123 if (srch_inf->ntwrk_buf_start) {
3124 if (srch_inf->smallBuf)
3125 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3126 else
3127 cifs_buf_release(srch_inf->ntwrk_buf_start);
3128 }
3129 srch_inf->ntwrk_buf_start = (char *)rsp;
3130 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3131 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3132 /* 4 for rfc1002 length field */
3133 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3134 srch_inf->entries_in_buffer =
3135 num_entries(srch_inf->srch_entries_start, end_of_smb,
3136 &srch_inf->last_entry, info_buf_size);
3137 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003138 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3139 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3140 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003141 if (resp_buftype == CIFS_LARGE_BUFFER)
3142 srch_inf->smallBuf = false;
3143 else if (resp_buftype == CIFS_SMALL_BUFFER)
3144 srch_inf->smallBuf = true;
3145 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003146 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003147
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003148 return rc;
3149
3150qdir_exit:
3151 free_rsp_buf(resp_buftype, rsp);
3152 return rc;
3153}
3154
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003155static int
3156send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003157 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3158 u8 info_type, u32 additional_info, unsigned int num,
3159 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003160{
3161 struct smb2_set_info_req *req;
3162 struct smb2_set_info_rsp *rsp = NULL;
3163 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003164 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003165 int rc = 0;
3166 int resp_buftype;
3167 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003168 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003169 int flags = 0;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003170 unsigned int total_len;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003171
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003172 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003173 return -EIO;
3174
3175 if (!num)
3176 return -EINVAL;
3177
3178 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3179 if (!iov)
3180 return -ENOMEM;
3181
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003182 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003183 if (rc) {
3184 kfree(iov);
3185 return rc;
3186 }
3187
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003188 if (encryption_required(tcon))
3189 flags |= CIFS_TRANSFORM_REQ;
3190
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003191 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003192
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003193 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003194 req->FileInfoClass = info_class;
3195 req->PersistentFileId = persistent_fid;
3196 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003197 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003198
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003199 req->BufferOffset =
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003200 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003201 req->BufferLength = cpu_to_le32(*size);
3202
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003203 memcpy(req->Buffer, *data, *size);
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003204 total_len += *size;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003205
3206 iov[0].iov_base = (char *)req;
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003207 /* 1 for Buffer */
3208 iov[0].iov_len = total_len - 1;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003209
3210 for (i = 1; i < num; i++) {
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003211 le32_add_cpu(&req->BufferLength, size[i]);
3212 iov[i].iov_base = (char *)data[i];
3213 iov[i].iov_len = size[i];
3214 }
3215
Ronnie Sahlberg2fc803e2017-11-20 11:24:44 +11003216 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3217 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003218 cifs_small_buf_release(req);
3219 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003220
Steve French7d3fb242013-11-18 09:56:28 -06003221 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003222 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003223
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003224 free_rsp_buf(resp_buftype, rsp);
3225 kfree(iov);
3226 return rc;
3227}
3228
3229int
3230SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3231 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3232{
3233 struct smb2_file_rename_info info;
3234 void **data;
3235 unsigned int size[2];
3236 int rc;
3237 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3238
3239 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3240 if (!data)
3241 return -ENOMEM;
3242
3243 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3244 /* 0 = fail if target already exists */
3245 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3246 info.FileNameLength = cpu_to_le32(len);
3247
3248 data[0] = &info;
3249 size[0] = sizeof(struct smb2_file_rename_info);
3250
3251 data[1] = target_file;
3252 size[1] = len + 2 /* null */;
3253
3254 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003255 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3256 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003257 kfree(data);
3258 return rc;
3259}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003260
3261int
Steve French897fba12016-05-12 21:20:36 -05003262SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3263 u64 persistent_fid, u64 volatile_fid)
3264{
3265 __u8 delete_pending = 1;
3266 void *data;
3267 unsigned int size;
3268
3269 data = &delete_pending;
3270 size = 1; /* sizeof __u8 */
3271
3272 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003273 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3274 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003275}
3276
3277int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003278SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3279 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3280{
3281 struct smb2_file_link_info info;
3282 void **data;
3283 unsigned int size[2];
3284 int rc;
3285 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3286
3287 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3288 if (!data)
3289 return -ENOMEM;
3290
3291 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3292 /* 0 = fail if link already exists */
3293 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3294 info.FileNameLength = cpu_to_le32(len);
3295
3296 data[0] = &info;
3297 size[0] = sizeof(struct smb2_file_link_info);
3298
3299 data[1] = target_file;
3300 size[1] = len + 2 /* null */;
3301
3302 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003303 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3304 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003305 kfree(data);
3306 return rc;
3307}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003308
3309int
3310SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003311 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003312{
3313 struct smb2_file_eof_info info;
3314 void *data;
3315 unsigned int size;
3316
3317 info.EndOfFile = *eof;
3318
3319 data = &info;
3320 size = sizeof(struct smb2_file_eof_info);
3321
Steve Frenchf29ebb42014-07-19 21:44:58 -05003322 if (is_falloc)
3323 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003324 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3325 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003326 else
3327 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003328 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3329 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003330}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003331
3332int
3333SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3334 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3335{
3336 unsigned int size;
3337 size = sizeof(FILE_BASIC_INFO);
3338 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003339 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3340 0, 1, (void **)&buf, &size);
3341}
3342
3343int
3344SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3345 u64 persistent_fid, u64 volatile_fid,
3346 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3347{
3348 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3349 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3350 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003351}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003352
3353int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003354SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3355 u64 persistent_fid, u64 volatile_fid,
3356 struct smb2_file_full_ea_info *buf, int len)
3357{
3358 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3359 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3360 0, 1, (void **)&buf, &len);
3361}
3362
3363int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003364SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3365 const u64 persistent_fid, const u64 volatile_fid,
3366 __u8 oplock_level)
3367{
3368 int rc;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003369 struct smb2_oplock_break_req *req = NULL;
3370 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003371 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003372 unsigned int total_len;
3373 struct kvec iov[1];
3374 struct kvec rsp_iov;
3375 int resp_buf_type;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003376
Joe Perchesf96637b2013-05-04 22:12:25 -05003377 cifs_dbg(FYI, "SMB2_oplock_break\n");
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003378 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3379 &total_len);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003380 if (rc)
3381 return rc;
3382
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003383 if (encryption_required(tcon))
3384 flags |= CIFS_TRANSFORM_REQ;
3385
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003386 req->VolatileFid = volatile_fid;
3387 req->PersistentFid = persistent_fid;
3388 req->OplockLevel = oplock_level;
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003389 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003390
Ronnie Sahlberg21ad9482017-11-20 11:24:43 +11003391 flags |= CIFS_NO_RESP;
3392
3393 iov[0].iov_base = (char *)req;
3394 iov[0].iov_len = total_len;
3395
3396 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003397 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003398
3399 if (rc) {
3400 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003401 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003402 }
3403
3404 return rc;
3405}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003406
3407static void
3408copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3409 struct kstatfs *kst)
3410{
3411 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3412 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3413 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303414 kst->f_bfree = kst->f_bavail =
3415 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003416 return;
3417}
3418
3419static int
3420build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3421 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3422{
3423 int rc;
3424 struct smb2_query_info_req *req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003425 unsigned int total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003426
Joe Perchesf96637b2013-05-04 22:12:25 -05003427 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003428
3429 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3430 return -EIO;
3431
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003432 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3433 &total_len);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003434 if (rc)
3435 return rc;
3436
3437 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3438 req->FileInfoClass = level;
3439 req->PersistentFileId = persistent_fid;
3440 req->VolatileFileId = volatile_fid;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003441 /* 1 for pad */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003442 req->InputBufferOffset =
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003443 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003444 req->OutputBufferLength = cpu_to_le32(
3445 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3446
3447 iov->iov_base = (char *)req;
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003448 iov->iov_len = total_len;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003449 return 0;
3450}
3451
3452int
3453SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3454 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3455{
3456 struct smb2_query_info_rsp *rsp = NULL;
3457 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003458 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003459 int rc = 0;
3460 int resp_buftype;
3461 struct cifs_ses *ses = tcon->ses;
3462 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003463 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003464
3465 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3466 sizeof(struct smb2_fs_full_size_info),
3467 persistent_fid, volatile_fid);
3468 if (rc)
3469 return rc;
3470
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003471 if (encryption_required(tcon))
3472 flags |= CIFS_TRANSFORM_REQ;
3473
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003474 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003475 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003476 if (rc) {
3477 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003478 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003479 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003480 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003481
3482 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3483 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3484 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3485 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3486 sizeof(struct smb2_fs_full_size_info));
3487 if (!rc)
3488 copy_fs_info_to_kstatfs(info, fsdata);
3489
Steve French34f62642013-10-09 02:07:00 -05003490qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003491 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003492 return rc;
3493}
3494
3495int
3496SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003497 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003498{
3499 struct smb2_query_info_rsp *rsp = NULL;
3500 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003501 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003502 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003503 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003504 struct cifs_ses *ses = tcon->ses;
3505 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003506 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003507
Steven French21671142013-10-09 13:36:35 -05003508 if (level == FS_DEVICE_INFORMATION) {
3509 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3510 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3511 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3512 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3513 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003514 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3515 max_len = sizeof(struct smb3_fs_ss_info);
3516 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003517 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003518 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003519 return -EINVAL;
3520 }
3521
3522 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003523 persistent_fid, volatile_fid);
3524 if (rc)
3525 return rc;
3526
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003527 if (encryption_required(tcon))
3528 flags |= CIFS_TRANSFORM_REQ;
3529
Ronnie Sahlbergb2fb7fe2017-11-20 11:24:46 +11003530 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003531 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003532 if (rc) {
3533 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3534 goto qfsattr_exit;
3535 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003536 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003537
3538 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3539 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003540 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3541 if (rc)
3542 goto qfsattr_exit;
3543
3544 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003545 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3546 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003547 rsp_len, max_len));
3548 else if (level == FS_DEVICE_INFORMATION)
3549 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3550 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003551 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3552 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3553 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3554 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3555 tcon->perf_sector_size =
3556 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3557 }
Steve French34f62642013-10-09 02:07:00 -05003558
3559qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003560 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003561 return rc;
3562}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003563
3564int
3565smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3566 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3567 const __u32 num_lock, struct smb2_lock_element *buf)
3568{
3569 int rc = 0;
3570 struct smb2_lock_req *req = NULL;
3571 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003572 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003573 int resp_buf_type;
3574 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003575 int flags = CIFS_NO_RESP;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003576 unsigned int total_len;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003577
Joe Perchesf96637b2013-05-04 22:12:25 -05003578 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003579
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003580 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003581 if (rc)
3582 return rc;
3583
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003584 if (encryption_required(tcon))
3585 flags |= CIFS_TRANSFORM_REQ;
3586
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003587 req->sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003588 req->LockCount = cpu_to_le16(num_lock);
3589
3590 req->PersistentFileId = persist_fid;
3591 req->VolatileFileId = volatile_fid;
3592
3593 count = num_lock * sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003594
3595 iov[0].iov_base = (char *)req;
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003596 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003597 iov[1].iov_base = (char *)buf;
3598 iov[1].iov_len = count;
3599
3600 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlbergced93672017-11-21 10:07:27 +11003601 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3602 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003603 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003604 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003605 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003606 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3607 }
3608
3609 return rc;
3610}
3611
3612int
3613SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3614 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3615 const __u64 length, const __u64 offset, const __u32 lock_flags,
3616 const bool wait)
3617{
3618 struct smb2_lock_element lock;
3619
3620 lock.Offset = cpu_to_le64(offset);
3621 lock.Length = cpu_to_le64(length);
3622 lock.Flags = cpu_to_le32(lock_flags);
3623 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3624 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3625
3626 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3627}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003628
3629int
3630SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3631 __u8 *lease_key, const __le32 lease_state)
3632{
3633 int rc;
3634 struct smb2_lease_ack *req = NULL;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003635 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003636 int flags = CIFS_OBREAK_OP;
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003637 unsigned int total_len;
3638 struct kvec iov[1];
3639 struct kvec rsp_iov;
3640 int resp_buf_type;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003641
Joe Perchesf96637b2013-05-04 22:12:25 -05003642 cifs_dbg(FYI, "SMB2_lease_break\n");
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003643 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3644 &total_len);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003645 if (rc)
3646 return rc;
3647
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003648 if (encryption_required(tcon))
3649 flags |= CIFS_TRANSFORM_REQ;
3650
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003651 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003652 req->StructureSize = cpu_to_le16(36);
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003653 total_len += 12;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003654
3655 memcpy(req->LeaseKey, lease_key, 16);
3656 req->LeaseState = lease_state;
3657
Ronnie Sahlberg8eb79982017-11-21 11:04:37 +11003658 flags |= CIFS_NO_RESP;
3659
3660 iov[0].iov_base = (char *)req;
3661 iov[0].iov_len = total_len;
3662
3663 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003664 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003665
3666 if (rc) {
3667 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003668 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003669 }
3670
3671 return rc;
3672}