blob: 48ff7703b91912c79b8412539919c3ce6fe76ac6 [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040051
52/*
53 * The following table defines the expected "StructureSize" of SMB2 requests
54 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
55 *
56 * Note that commands are defined in smb2pdu.h in le16 but the array below is
57 * indexed by command in host byte order.
58 */
59static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
60 /* SMB2_NEGOTIATE */ 36,
61 /* SMB2_SESSION_SETUP */ 25,
62 /* SMB2_LOGOFF */ 4,
63 /* SMB2_TREE_CONNECT */ 9,
64 /* SMB2_TREE_DISCONNECT */ 4,
65 /* SMB2_CREATE */ 57,
66 /* SMB2_CLOSE */ 24,
67 /* SMB2_FLUSH */ 24,
68 /* SMB2_READ */ 49,
69 /* SMB2_WRITE */ 49,
70 /* SMB2_LOCK */ 48,
71 /* SMB2_IOCTL */ 57,
72 /* SMB2_CANCEL */ 4,
73 /* SMB2_ECHO */ 4,
74 /* SMB2_QUERY_DIRECTORY */ 33,
75 /* SMB2_CHANGE_NOTIFY */ 32,
76 /* SMB2_QUERY_INFO */ 41,
77 /* SMB2_SET_INFO */ 33,
78 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
79};
80
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070081static int encryption_required(const struct cifs_tcon *tcon)
82{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080083 if (!tcon)
84 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070085 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
86 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
87 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080088 if (tcon->seal &&
89 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
90 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070091 return 0;
92}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040093
94static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070095smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040096 const struct cifs_tcon *tcon)
97{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070098 shdr->ProtocolId = SMB2_PROTO_NUMBER;
99 shdr->StructureSize = cpu_to_le16(64);
100 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100101 if (tcon && tcon->ses && tcon->ses->server) {
102 struct TCP_Server_Info *server = tcon->ses->server;
103
104 spin_lock(&server->req_lock);
105 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500106 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700107 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100108 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700109 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500110 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100111 server->credits, 2));
112 spin_unlock(&server->req_lock);
113 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700114 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100115 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700116 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400117
118 if (!tcon)
119 goto out;
120
Steve French2b80d042013-06-23 18:43:37 -0500121 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
122 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500123 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500124 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700125 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500126 /* else CreditCharge MBZ */
127
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700128 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400129 /* Uid is not converted */
130 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700131 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500132
133 /*
134 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
135 * to pass the path on the Open SMB prefixed by \\server\share.
136 * Not sure when we would need to do the augmented path (if ever) and
137 * setting this flag breaks the SMB2 open operation since it is
138 * illegal to send an empty path name (without \\server\share prefix)
139 * when the DFS flag is set in the SMB open header. We could
140 * consider setting the flag on all operations other than open
141 * but it is safer to net set it for now.
142 */
143/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700144 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500145
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700146 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
147 !encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700148 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400149out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400150 return;
151}
152
153static int
154smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
155{
156 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400157 struct nls_table *nls_codepage;
158 struct cifs_ses *ses;
159 struct TCP_Server_Info *server;
160
161 /*
162 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
163 * check for tcp and smb session status done differently
164 * for those three - in the calling routine.
165 */
166 if (tcon == NULL)
167 return rc;
168
169 if (smb2_command == SMB2_TREE_CONNECT)
170 return rc;
171
172 if (tcon->tidStatus == CifsExiting) {
173 /*
174 * only tree disconnect, open, and write,
175 * (and ulogoff which does not have tcon)
176 * are allowed as we start force umount.
177 */
178 if ((smb2_command != SMB2_WRITE) &&
179 (smb2_command != SMB2_CREATE) &&
180 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500181 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
182 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400183 return -ENODEV;
184 }
185 }
186 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
187 (!tcon->ses->server))
188 return -EIO;
189
190 ses = tcon->ses;
191 server = ses->server;
192
193 /*
194 * Give demultiplex thread up to 10 seconds to reconnect, should be
195 * greater than cifs socket timeout which is 7 seconds
196 */
197 while (server->tcpStatus == CifsNeedReconnect) {
198 /*
199 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
200 * here since they are implicitly done when session drops.
201 */
202 switch (smb2_command) {
203 /*
204 * BB Should we keep oplock break and add flush to exceptions?
205 */
206 case SMB2_TREE_DISCONNECT:
207 case SMB2_CANCEL:
208 case SMB2_CLOSE:
209 case SMB2_OPLOCK_BREAK:
210 return -EAGAIN;
211 }
212
213 wait_event_interruptible_timeout(server->response_q,
214 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
215
216 /* are we still trying to reconnect? */
217 if (server->tcpStatus != CifsNeedReconnect)
218 break;
219
220 /*
221 * on "soft" mounts we wait once. Hard mounts keep
222 * retrying until process is killed or server comes
223 * back on-line
224 */
225 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500226 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400227 return -EHOSTDOWN;
228 }
229 }
230
231 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
232 return rc;
233
234 nls_codepage = load_nls_default();
235
236 /*
237 * need to prevent multiple threads trying to simultaneously reconnect
238 * the same SMB session
239 */
240 mutex_lock(&tcon->ses->session_mutex);
241 rc = cifs_negotiate_protocol(0, tcon->ses);
242 if (!rc && tcon->ses->need_reconnect)
243 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
244
245 if (rc || !tcon->need_reconnect) {
246 mutex_unlock(&tcon->ses->session_mutex);
247 goto out;
248 }
249
250 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800251 if (tcon->use_persistent)
252 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500253
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400254 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
255 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500256
Joe Perchesf96637b2013-05-04 22:12:25 -0500257 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400258 if (rc)
259 goto out;
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800260
261 if (smb2_command != SMB2_INTERNAL_CMD)
262 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
263
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400264 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400265out:
266 /*
267 * Check if handle based operation so we know whether we can continue
268 * or not without returning to caller to reset file handle.
269 */
270 /*
271 * BB Is flush done by server on drop of tcp session? Should we special
272 * case it and skip above?
273 */
274 switch (smb2_command) {
275 case SMB2_FLUSH:
276 case SMB2_READ:
277 case SMB2_WRITE:
278 case SMB2_LOCK:
279 case SMB2_IOCTL:
280 case SMB2_QUERY_DIRECTORY:
281 case SMB2_CHANGE_NOTIFY:
282 case SMB2_QUERY_INFO:
283 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800284 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400285 }
286 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400287 return rc;
288}
289
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700290static void
291fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
292 unsigned int *total_len)
293{
294 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
295 /* lookup word count ie StructureSize from table */
296 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
297
298 /*
299 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
300 * largest operations (Create)
301 */
302 memset(buf, 0, 256);
303
304 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
305 spdu->StructureSize2 = cpu_to_le16(parmsize);
306
307 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
308}
309
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800310/* init request without RFC1001 length at the beginning */
311static int
312smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
313 void **request_buf, unsigned int *total_len)
314{
315 int rc;
316 struct smb2_sync_hdr *shdr;
317
318 rc = smb2_reconnect(smb2_command, tcon);
319 if (rc)
320 return rc;
321
322 /* BB eventually switch this to SMB2 specific small buf size */
323 *request_buf = cifs_small_buf_get();
324 if (*request_buf == NULL) {
325 /* BB should we add a retry in here if not a writepage? */
326 return -ENOMEM;
327 }
328
329 shdr = (struct smb2_sync_hdr *)(*request_buf);
330
331 fill_small_buf(smb2_command, tcon, shdr, total_len);
332
333 if (tcon != NULL) {
334#ifdef CONFIG_CIFS_STATS2
335 uint16_t com_code = le16_to_cpu(smb2_command);
336
337 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
338#endif
339 cifs_stats_inc(&tcon->num_smbs_sent);
340 }
341
342 return rc;
343}
344
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400345/*
346 * Allocate and return pointer to an SMB request hdr, and set basic
347 * SMB information in the SMB header. If the return code is zero, this
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800348 * function must have filled in request_buf pointer. The returned buffer
349 * has RFC1001 length at the beginning.
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400350 */
351static int
352small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
353 void **request_buf)
354{
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700355 int rc;
356 unsigned int total_len;
357 struct smb2_pdu *pdu;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400358
359 rc = smb2_reconnect(smb2_command, tcon);
360 if (rc)
361 return rc;
362
363 /* BB eventually switch this to SMB2 specific small buf size */
364 *request_buf = cifs_small_buf_get();
365 if (*request_buf == NULL) {
366 /* BB should we add a retry in here if not a writepage? */
367 return -ENOMEM;
368 }
369
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700370 pdu = (struct smb2_pdu *)(*request_buf);
371
372 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
373
374 /* Note this is only network field converted to big endian */
375 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400376
377 if (tcon != NULL) {
378#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400379 uint16_t com_code = le16_to_cpu(smb2_command);
380 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400381#endif
382 cifs_stats_inc(&tcon->num_smbs_sent);
383 }
384
385 return rc;
386}
387
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500388#ifdef CONFIG_CIFS_SMB311
389/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
390#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
391
392
393#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
394#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
395
396static void
397build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
398{
399 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
400 pneg_ctxt->DataLength = cpu_to_le16(38);
401 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
402 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
403 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
404 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
405}
406
407static void
408build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
409{
410 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
411 pneg_ctxt->DataLength = cpu_to_le16(6);
412 pneg_ctxt->CipherCount = cpu_to_le16(2);
413 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
414 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
415}
416
417static void
418assemble_neg_contexts(struct smb2_negotiate_req *req)
419{
420
421 /* +4 is to account for the RFC1001 len field */
422 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
423
424 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
425 /* Add 2 to size to round to 8 byte boundary */
426 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
427 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
428 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
429 req->NegotiateContextCount = cpu_to_le16(2);
430 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
431 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
432}
433#else
434static void assemble_neg_contexts(struct smb2_negotiate_req *req)
435{
436 return;
437}
438#endif /* SMB311 */
439
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400440/*
441 *
442 * SMB2 Worker functions follow:
443 *
444 * The general structure of the worker functions is:
445 * 1) Call smb2_init (assembles SMB2 header)
446 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
447 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
448 * 4) Decode SMB2 command specific fields in the fixed length area
449 * 5) Decode variable length data area (if any for this SMB2 command type)
450 * 6) Call free smb buffer
451 * 7) return
452 *
453 */
454
455int
456SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
457{
458 struct smb2_negotiate_req *req;
459 struct smb2_negotiate_rsp *rsp;
460 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700461 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400462 int rc = 0;
463 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400464 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400465 int blob_offset, blob_length;
466 char *security_blob;
467 int flags = CIFS_NEG_OP;
468
Joe Perchesf96637b2013-05-04 22:12:25 -0500469 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400470
Jeff Layton3534b852013-05-24 07:41:01 -0400471 if (!server) {
472 WARN(1, "%s: server is NULL!\n", __func__);
473 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400474 }
475
476 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
477 if (rc)
478 return rc;
479
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700480 req->hdr.sync_hdr.SessionId = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400481
Steve Frenche4aa25e2012-10-01 12:26:22 -0500482 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400483
Steve Frenche4aa25e2012-10-01 12:26:22 -0500484 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
485 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400486
487 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400488 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500489 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400490 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500491 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400492 else
493 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400494
Steve Frenche4aa25e2012-10-01 12:26:22 -0500495 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400496
Steve French3c5f9be12014-05-13 13:37:45 -0700497 /* ClientGUID must be zero for SMB2.02 dialect */
498 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
499 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500500 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700501 memcpy(req->ClientGUID, server->client_guid,
502 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500503 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
504 assemble_neg_contexts(req);
505 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400506 iov[0].iov_base = (char *)req;
507 /* 4 for rfc1002 length field */
508 iov[0].iov_len = get_rfc1002_length(req) + 4;
509
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700510 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
511 cifs_small_buf_release(req);
512 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400513 /*
514 * No tcon so can't do
515 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
516 */
517 if (rc != 0)
518 goto neg_exit;
519
Joe Perchesf96637b2013-05-04 22:12:25 -0500520 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400521
Steve Frenche4aa25e2012-10-01 12:26:22 -0500522 /* BB we may eventually want to match the negotiated vs. requested
523 dialect, even though we are only requesting one at a time */
524 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500525 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500526 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500527 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500528 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500529 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500530 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
531 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600532#ifdef CONFIG_CIFS_SMB311
533 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
534 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
535#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400536 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500537 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500538 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400539 rc = -EIO;
540 goto neg_exit;
541 }
542 server->dialect = le16_to_cpu(rsp->DialectRevision);
543
Jeff Laytone598d1d82013-05-26 07:00:59 -0400544 /* SMB2 only has an extended negflavor */
545 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400546 /* set it to the maximum buffer size value we can send with 1 credit */
547 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
548 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400549 server->max_read = le32_to_cpu(rsp->MaxReadSize);
550 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
551 /* BB Do we need to validate the SecurityMode? */
552 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
553 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400554 /* Internal types */
555 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400556
557 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
558 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500559 /*
560 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
561 * for us will be
562 * ses->sectype = RawNTLMSSP;
563 * but for time being this is our only auth choice so doesn't matter.
564 * We just found a server which sets blob length to zero expecting raw.
565 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700566 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500567 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700568 server->sec_ntlmssp = true;
569 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700570
Jeff Layton38d77c52013-05-26 07:01:00 -0400571 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400572 if (rc)
573 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500574 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500575 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500576 if (rc == 1)
577 rc = 0;
578 else if (rc == 0)
579 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400580 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400581neg_exit:
582 free_rsp_buf(resp_buftype, rsp);
583 return rc;
584}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400585
Steve Frenchff1c0382013-11-19 23:44:46 -0600586int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
587{
588 int rc = 0;
589 struct validate_negotiate_info_req vneg_inbuf;
590 struct validate_negotiate_info_rsp *pneg_rsp;
591 u32 rsplen;
592
593 cifs_dbg(FYI, "validate negotiate\n");
594
595 /*
596 * validation ioctl must be signed, so no point sending this if we
597 * can not sign it. We could eventually change this to selectively
598 * sign just this, the first and only signed request on a connection.
599 * This is good enough for now since a user who wants better security
600 * would also enable signing on the mount. Having validation of
601 * negotiate info for signed connections helps reduce attack vectors
602 */
603 if (tcon->ses->server->sign == false)
604 return 0; /* validation requires signing */
605
606 vneg_inbuf.Capabilities =
607 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100608 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
609 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600610
611 if (tcon->ses->sign)
612 vneg_inbuf.SecurityMode =
613 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
614 else if (global_secflags & CIFSSEC_MAY_SIGN)
615 vneg_inbuf.SecurityMode =
616 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
617 else
618 vneg_inbuf.SecurityMode = 0;
619
620 vneg_inbuf.DialectCount = cpu_to_le16(1);
621 vneg_inbuf.Dialects[0] =
622 cpu_to_le16(tcon->ses->server->vals->protocol_id);
623
624 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
625 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100626 false /* use_ipc */,
Steve Frenchff1c0382013-11-19 23:44:46 -0600627 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
628 (char **)&pneg_rsp, &rsplen);
629
630 if (rc != 0) {
631 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
632 return -EIO;
633 }
634
635 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500636 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
637 rsplen);
638
639 /* relax check since Mac returns max bufsize allowed on ioctl */
640 if (rsplen > CIFSMaxBufSize)
641 return -EIO;
Steve Frenchff1c0382013-11-19 23:44:46 -0600642 }
643
644 /* check validate negotiate info response matches what we got earlier */
645 if (pneg_rsp->Dialect !=
646 cpu_to_le16(tcon->ses->server->vals->protocol_id))
647 goto vneg_out;
648
649 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
650 goto vneg_out;
651
652 /* do not validate server guid because not saved at negprot time yet */
653
654 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
655 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
656 goto vneg_out;
657
658 /* validate negotiate successful */
659 cifs_dbg(FYI, "validate negotiate info successful\n");
660 return 0;
661
662vneg_out:
663 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
664 return -EIO;
665}
666
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530667enum securityEnum
668smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
669{
670 switch (requested) {
671 case Kerberos:
672 case RawNTLMSSP:
673 return requested;
674 case NTLMv2:
675 return RawNTLMSSP;
676 case Unspecified:
677 if (server->sec_ntlmssp &&
678 (global_secflags & CIFSSEC_MAY_NTLMSSP))
679 return RawNTLMSSP;
680 if ((server->sec_kerberos || server->sec_mskerberos) &&
681 (global_secflags & CIFSSEC_MAY_KRB5))
682 return Kerberos;
683 /* Fallthrough */
684 default:
685 return Unspecified;
686 }
687}
688
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100689struct SMB2_sess_data {
690 unsigned int xid;
691 struct cifs_ses *ses;
692 struct nls_table *nls_cp;
693 void (*func)(struct SMB2_sess_data *);
694 int result;
695 u64 previous_session;
696
697 /* we will send the SMB in three pieces:
698 * a fixed length beginning part, an optional
699 * SPNEGO blob (which can be zero length), and a
700 * last part which will include the strings
701 * and rest of bcc area. This allows us to avoid
702 * a large buffer 17K allocation
703 */
704 int buf0_type;
705 struct kvec iov[2];
706};
707
708static int
709SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
710{
711 int rc;
712 struct cifs_ses *ses = sess_data->ses;
713 struct smb2_sess_setup_req *req;
714 struct TCP_Server_Info *server = ses->server;
715
716 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
717 if (rc)
718 return rc;
719
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700720 /* First session, not a reauthenticate */
721 req->hdr.sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100722
723 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
724 req->PreviousSessionId = sess_data->previous_session;
725
726 req->Flags = 0; /* MBZ */
727 /* to enable echos and oplocks */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700728 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100729
730 /* only one of SMB2 signing flags may be set in SMB2 request */
731 if (server->sign)
732 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
733 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
734 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
735 else
736 req->SecurityMode = 0;
737
738 req->Capabilities = 0;
739 req->Channel = 0; /* MBZ */
740
741 sess_data->iov[0].iov_base = (char *)req;
742 /* 4 for rfc1002 length field and 1 for pad */
743 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
744 /*
745 * This variable will be used to clear the buffer
746 * allocated above in case of any error in the calling function.
747 */
748 sess_data->buf0_type = CIFS_SMALL_BUFFER;
749
750 return 0;
751}
752
753static void
754SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
755{
756 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
757 sess_data->buf0_type = CIFS_NO_BUFFER;
758}
759
760static int
761SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
762{
763 int rc;
764 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700765 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100766
767 /* Testing shows that buffer offset must be at location of Buffer[0] */
768 req->SecurityBufferOffset =
769 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
770 1 /* pad */ - 4 /* rfc1001 len */);
771 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
772
773 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
774
775 /* BB add code to build os and lm fields */
776
777 rc = SendReceive2(sess_data->xid, sess_data->ses,
778 sess_data->iov, 2,
779 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700780 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
781 cifs_small_buf_release(sess_data->iov[0].iov_base);
782 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100783
784 return rc;
785}
786
787static int
788SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
789{
790 int rc = 0;
791 struct cifs_ses *ses = sess_data->ses;
792
793 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800794 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100795 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100796 if (rc) {
797 cifs_dbg(FYI,
798 "SMB3 session key generation failed\n");
799 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800800 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100801 }
802 }
803 if (!ses->server->session_estab) {
804 ses->server->sequence_number = 0x2;
805 ses->server->session_estab = true;
806 }
807 mutex_unlock(&ses->server->srv_mutex);
808
809 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
810 spin_lock(&GlobalMid_Lock);
811 ses->status = CifsGood;
812 ses->need_reconnect = false;
813 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100814 return rc;
815}
816
817#ifdef CONFIG_CIFS_UPCALL
818static void
819SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
820{
821 int rc;
822 struct cifs_ses *ses = sess_data->ses;
823 struct cifs_spnego_msg *msg;
824 struct key *spnego_key = NULL;
825 struct smb2_sess_setup_rsp *rsp = NULL;
826
827 rc = SMB2_sess_alloc_buffer(sess_data);
828 if (rc)
829 goto out;
830
831 spnego_key = cifs_get_spnego_key(ses);
832 if (IS_ERR(spnego_key)) {
833 rc = PTR_ERR(spnego_key);
834 spnego_key = NULL;
835 goto out;
836 }
837
838 msg = spnego_key->payload.data[0];
839 /*
840 * check version field to make sure that cifs.upcall is
841 * sending us a response in an expected form
842 */
843 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
844 cifs_dbg(VFS,
845 "bad cifs.upcall version. Expected %d got %d",
846 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
847 rc = -EKEYREJECTED;
848 goto out_put_spnego_key;
849 }
850
851 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
852 GFP_KERNEL);
853 if (!ses->auth_key.response) {
854 cifs_dbg(VFS,
855 "Kerberos can't allocate (%u bytes) memory",
856 msg->sesskey_len);
857 rc = -ENOMEM;
858 goto out_put_spnego_key;
859 }
860 ses->auth_key.len = msg->sesskey_len;
861
862 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
863 sess_data->iov[1].iov_len = msg->secblob_len;
864
865 rc = SMB2_sess_sendreceive(sess_data);
866 if (rc)
867 goto out_put_spnego_key;
868
869 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700870 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100871
872 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100873
874 rc = SMB2_sess_establish_session(sess_data);
875out_put_spnego_key:
876 key_invalidate(spnego_key);
877 key_put(spnego_key);
878out:
879 sess_data->result = rc;
880 sess_data->func = NULL;
881 SMB2_sess_free_buffer(sess_data);
882}
883#else
884static void
885SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
886{
887 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
888 sess_data->result = -EOPNOTSUPP;
889 sess_data->func = NULL;
890}
891#endif
892
Sachin Prabhu166cea42016-10-07 19:11:22 +0100893static void
894SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
895
896static void
897SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
898{
899 int rc;
900 struct cifs_ses *ses = sess_data->ses;
901 struct smb2_sess_setup_rsp *rsp = NULL;
902 char *ntlmssp_blob = NULL;
903 bool use_spnego = false; /* else use raw ntlmssp */
904 u16 blob_length = 0;
905
906 /*
907 * If memory allocation is successful, caller of this function
908 * frees it.
909 */
910 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
911 if (!ses->ntlmssp) {
912 rc = -ENOMEM;
913 goto out_err;
914 }
915 ses->ntlmssp->sesskey_per_smbsess = true;
916
917 rc = SMB2_sess_alloc_buffer(sess_data);
918 if (rc)
919 goto out_err;
920
921 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
922 GFP_KERNEL);
923 if (ntlmssp_blob == NULL) {
924 rc = -ENOMEM;
925 goto out;
926 }
927
928 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
929 if (use_spnego) {
930 /* BB eventually need to add this */
931 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
932 rc = -EOPNOTSUPP;
933 goto out;
934 } else {
935 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
936 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
937 }
938 sess_data->iov[1].iov_base = ntlmssp_blob;
939 sess_data->iov[1].iov_len = blob_length;
940
941 rc = SMB2_sess_sendreceive(sess_data);
942 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
943
944 /* If true, rc here is expected and not an error */
945 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700946 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +0100947 rc = 0;
948
949 if (rc)
950 goto out;
951
952 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
953 le16_to_cpu(rsp->SecurityBufferOffset)) {
954 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
955 le16_to_cpu(rsp->SecurityBufferOffset));
956 rc = -EIO;
957 goto out;
958 }
959 rc = decode_ntlmssp_challenge(rsp->Buffer,
960 le16_to_cpu(rsp->SecurityBufferLength), ses);
961 if (rc)
962 goto out;
963
964 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
965
966
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700967 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100968 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +0100969
970out:
971 kfree(ntlmssp_blob);
972 SMB2_sess_free_buffer(sess_data);
973 if (!rc) {
974 sess_data->result = 0;
975 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
976 return;
977 }
978out_err:
979 kfree(ses->ntlmssp);
980 ses->ntlmssp = NULL;
981 sess_data->result = rc;
982 sess_data->func = NULL;
983}
984
985static void
986SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
987{
988 int rc;
989 struct cifs_ses *ses = sess_data->ses;
990 struct smb2_sess_setup_req *req;
991 struct smb2_sess_setup_rsp *rsp = NULL;
992 unsigned char *ntlmssp_blob = NULL;
993 bool use_spnego = false; /* else use raw ntlmssp */
994 u16 blob_length = 0;
995
996 rc = SMB2_sess_alloc_buffer(sess_data);
997 if (rc)
998 goto out;
999
1000 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001001 req->hdr.sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001002
1003 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1004 sess_data->nls_cp);
1005 if (rc) {
1006 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1007 goto out;
1008 }
1009
1010 if (use_spnego) {
1011 /* BB eventually need to add this */
1012 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1013 rc = -EOPNOTSUPP;
1014 goto out;
1015 }
1016 sess_data->iov[1].iov_base = ntlmssp_blob;
1017 sess_data->iov[1].iov_len = blob_length;
1018
1019 rc = SMB2_sess_sendreceive(sess_data);
1020 if (rc)
1021 goto out;
1022
1023 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1024
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001025 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001026 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001027
1028 rc = SMB2_sess_establish_session(sess_data);
1029out:
1030 kfree(ntlmssp_blob);
1031 SMB2_sess_free_buffer(sess_data);
1032 kfree(ses->ntlmssp);
1033 ses->ntlmssp = NULL;
1034 sess_data->result = rc;
1035 sess_data->func = NULL;
1036}
1037
1038static int
1039SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1040{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301041 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001042
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301043 type = smb2_select_sectype(ses->server, ses->sectype);
1044 cifs_dbg(FYI, "sess setup type %d\n", type);
1045 if (type == Unspecified) {
1046 cifs_dbg(VFS,
1047 "Unable to select appropriate authentication method!");
1048 return -EINVAL;
1049 }
1050
1051 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001052 case Kerberos:
1053 sess_data->func = SMB2_auth_kerberos;
1054 break;
1055 case RawNTLMSSP:
1056 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1057 break;
1058 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301059 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001060 return -EOPNOTSUPP;
1061 }
1062
1063 return 0;
1064}
1065
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001066int
1067SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1068 const struct nls_table *nls_cp)
1069{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001070 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001071 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001072 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001073
Joe Perchesf96637b2013-05-04 22:12:25 -05001074 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001075
Jeff Layton3534b852013-05-24 07:41:01 -04001076 if (!server) {
1077 WARN(1, "%s: server is NULL!\n", __func__);
1078 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001079 }
1080
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001081 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1082 if (!sess_data)
1083 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001084
1085 rc = SMB2_select_sec(ses, sess_data);
1086 if (rc)
1087 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001088 sess_data->xid = xid;
1089 sess_data->ses = ses;
1090 sess_data->buf0_type = CIFS_NO_BUFFER;
1091 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001092
Sachin Prabhu166cea42016-10-07 19:11:22 +01001093 while (sess_data->func)
1094 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001095
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001096 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001097out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001098 kfree(sess_data);
1099 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001100}
1101
1102int
1103SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1104{
1105 struct smb2_logoff_req *req; /* response is also trivial struct */
1106 int rc = 0;
1107 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001108 int flags = 0;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001109
Joe Perchesf96637b2013-05-04 22:12:25 -05001110 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001111
1112 if (ses && (ses->server))
1113 server = ses->server;
1114 else
1115 return -EIO;
1116
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001117 /* no need to send SMB logoff if uid already closed due to reconnect */
1118 if (ses->need_reconnect)
1119 goto smb2_session_already_dead;
1120
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001121 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1122 if (rc)
1123 return rc;
1124
1125 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001126 req->hdr.sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001127
1128 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1129 flags |= CIFS_TRANSFORM_REQ;
1130 else if (server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001131 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001132
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001133 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001134 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001135 /*
1136 * No tcon so can't do
1137 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1138 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001139
1140smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001141 return rc;
1142}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001143
1144static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1145{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001146 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001147}
1148
1149#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1150
Steve Frenchde9f68d2013-11-15 11:26:24 -06001151/* These are similar values to what Windows uses */
1152static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1153{
1154 tcon->max_chunks = 256;
1155 tcon->max_bytes_chunk = 1048576;
1156 tcon->max_bytes_copy = 16777216;
1157}
1158
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001159int
1160SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1161 struct cifs_tcon *tcon, const struct nls_table *cp)
1162{
1163 struct smb2_tree_connect_req *req;
1164 struct smb2_tree_connect_rsp *rsp = NULL;
1165 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001166 struct kvec rsp_iov;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001167 int rc = 0;
1168 int resp_buftype;
1169 int unc_path_len;
1170 struct TCP_Server_Info *server;
1171 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001172 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001173
Joe Perchesf96637b2013-05-04 22:12:25 -05001174 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001175
1176 if ((ses->server) && tree)
1177 server = ses->server;
1178 else
1179 return -EIO;
1180
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001181 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1182 if (unc_path == NULL)
1183 return -ENOMEM;
1184
1185 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1186 unc_path_len *= 2;
1187 if (unc_path_len < 2) {
1188 kfree(unc_path);
1189 return -EINVAL;
1190 }
1191
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001192 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1193 if (tcon)
1194 tcon->tid = 0;
1195
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001196 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1197 if (rc) {
1198 kfree(unc_path);
1199 return rc;
1200 }
1201
1202 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001203 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1204 flags |= CIFS_TRANSFORM_REQ;
1205
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001206 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001207 req->hdr.sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001208 if (ses->server->sign)
1209 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001210 } else if (encryption_required(tcon))
1211 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001212
1213 iov[0].iov_base = (char *)req;
1214 /* 4 for rfc1002 length field and 1 for pad */
1215 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1216
1217 /* Testing shows that buffer offset must be at location of Buffer[0] */
1218 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1219 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1220 req->PathLength = cpu_to_le16(unc_path_len - 2);
1221 iov[1].iov_base = unc_path;
1222 iov[1].iov_len = unc_path_len;
1223
1224 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1225
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001226 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001227 cifs_small_buf_release(req);
1228 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001229
1230 if (rc != 0) {
1231 if (tcon) {
1232 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1233 tcon->need_reconnect = true;
1234 }
1235 goto tcon_error_exit;
1236 }
1237
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001238 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001239 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001240 goto tcon_exit;
1241 }
1242
1243 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001244 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001245 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1246 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001247 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001248 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1249 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001250 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001251 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001252 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001253 rc = -EOPNOTSUPP;
1254 goto tcon_error_exit;
1255 }
1256
1257 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001258 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001259 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1260 tcon->tidStatus = CifsGood;
1261 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001262 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001263 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001264
1265 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1266 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001267 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001268
1269 if (tcon->seal &&
1270 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1271 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1272
Steve Frenchde9f68d2013-11-15 11:26:24 -06001273 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001274 if (tcon->ses->server->ops->validate_negotiate)
1275 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001276tcon_exit:
1277 free_rsp_buf(resp_buftype, rsp);
1278 kfree(unc_path);
1279 return rc;
1280
1281tcon_error_exit:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001282 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001283 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001284 }
1285 goto tcon_exit;
1286}
1287
1288int
1289SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1290{
1291 struct smb2_tree_disconnect_req *req; /* response is trivial */
1292 int rc = 0;
1293 struct TCP_Server_Info *server;
1294 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001295 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001296
Joe Perchesf96637b2013-05-04 22:12:25 -05001297 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001298
1299 if (ses && (ses->server))
1300 server = ses->server;
1301 else
1302 return -EIO;
1303
1304 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1305 return 0;
1306
1307 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1308 if (rc)
1309 return rc;
1310
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001311 if (encryption_required(tcon))
1312 flags |= CIFS_TRANSFORM_REQ;
1313
1314 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001315 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001316 if (rc)
1317 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1318
1319 return rc;
1320}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001321
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001322
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001323static struct create_durable *
1324create_durable_buf(void)
1325{
1326 struct create_durable *buf;
1327
1328 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1329 if (!buf)
1330 return NULL;
1331
1332 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001333 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001334 buf->ccontext.DataLength = cpu_to_le32(16);
1335 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1336 (struct create_durable, Name));
1337 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001338 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001339 buf->Name[0] = 'D';
1340 buf->Name[1] = 'H';
1341 buf->Name[2] = 'n';
1342 buf->Name[3] = 'Q';
1343 return buf;
1344}
1345
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001346static struct create_durable *
1347create_reconnect_durable_buf(struct cifs_fid *fid)
1348{
1349 struct create_durable *buf;
1350
1351 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1352 if (!buf)
1353 return NULL;
1354
1355 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1356 (struct create_durable, Data));
1357 buf->ccontext.DataLength = cpu_to_le32(16);
1358 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1359 (struct create_durable, Name));
1360 buf->ccontext.NameLength = cpu_to_le16(4);
1361 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1362 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001363 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001364 buf->Name[0] = 'D';
1365 buf->Name[1] = 'H';
1366 buf->Name[2] = 'n';
1367 buf->Name[3] = 'C';
1368 return buf;
1369}
1370
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001371static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001372parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1373 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001374{
1375 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001376 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001377 unsigned int next;
1378 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001379 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001380
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001381 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001382 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001383 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001384 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001385 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001386 if (le16_to_cpu(cc->NameLength) == 4 &&
1387 strncmp(name, "RqLs", 4) == 0)
1388 return server->ops->parse_lease_buf(cc, epoch);
1389
1390 next = le32_to_cpu(cc->Next);
1391 if (!next)
1392 break;
1393 remaining -= next;
1394 cc = (struct create_context *)((char *)cc + next);
1395 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001396
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001397 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001398}
1399
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001400static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001401add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1402 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001403{
1404 struct smb2_create_req *req = iov[0].iov_base;
1405 unsigned int num = *num_iovec;
1406
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001407 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001408 if (iov[num].iov_base == NULL)
1409 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001410 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001411 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1412 if (!req->CreateContextsOffset)
1413 req->CreateContextsOffset = cpu_to_le32(
1414 sizeof(struct smb2_create_req) - 4 +
1415 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001416 le32_add_cpu(&req->CreateContextsLength,
1417 server->vals->create_lease_size);
1418 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001419 *num_iovec = num + 1;
1420 return 0;
1421}
1422
Steve Frenchb56eae42015-11-03 09:26:27 -06001423static struct create_durable_v2 *
1424create_durable_v2_buf(struct cifs_fid *pfid)
1425{
1426 struct create_durable_v2 *buf;
1427
1428 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1429 if (!buf)
1430 return NULL;
1431
1432 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1433 (struct create_durable_v2, dcontext));
1434 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1435 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1436 (struct create_durable_v2, Name));
1437 buf->ccontext.NameLength = cpu_to_le16(4);
1438
1439 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1440 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001441 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001442 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1443
1444 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1445 buf->Name[0] = 'D';
1446 buf->Name[1] = 'H';
1447 buf->Name[2] = '2';
1448 buf->Name[3] = 'Q';
1449 return buf;
1450}
1451
1452static struct create_durable_handle_reconnect_v2 *
1453create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1454{
1455 struct create_durable_handle_reconnect_v2 *buf;
1456
1457 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1458 GFP_KERNEL);
1459 if (!buf)
1460 return NULL;
1461
1462 buf->ccontext.DataOffset =
1463 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1464 dcontext));
1465 buf->ccontext.DataLength =
1466 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1467 buf->ccontext.NameOffset =
1468 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1469 Name));
1470 buf->ccontext.NameLength = cpu_to_le16(4);
1471
1472 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1473 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1474 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1475 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1476
1477 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1478 buf->Name[0] = 'D';
1479 buf->Name[1] = 'H';
1480 buf->Name[2] = '2';
1481 buf->Name[3] = 'C';
1482 return buf;
1483}
1484
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001485static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001486add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001487 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001488{
1489 struct smb2_create_req *req = iov[0].iov_base;
1490 unsigned int num = *num_iovec;
1491
Steve Frenchb56eae42015-11-03 09:26:27 -06001492 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1493 if (iov[num].iov_base == NULL)
1494 return -ENOMEM;
1495 iov[num].iov_len = sizeof(struct create_durable_v2);
1496 if (!req->CreateContextsOffset)
1497 req->CreateContextsOffset =
1498 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1499 iov[1].iov_len);
1500 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1501 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1502 *num_iovec = num + 1;
1503 return 0;
1504}
1505
1506static int
1507add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1508 struct cifs_open_parms *oparms)
1509{
1510 struct smb2_create_req *req = iov[0].iov_base;
1511 unsigned int num = *num_iovec;
1512
1513 /* indicate that we don't need to relock the file */
1514 oparms->reconnect = false;
1515
1516 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1517 if (iov[num].iov_base == NULL)
1518 return -ENOMEM;
1519 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1520 if (!req->CreateContextsOffset)
1521 req->CreateContextsOffset =
1522 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1523 iov[1].iov_len);
1524 le32_add_cpu(&req->CreateContextsLength,
1525 sizeof(struct create_durable_handle_reconnect_v2));
1526 inc_rfc1001_len(&req->hdr,
1527 sizeof(struct create_durable_handle_reconnect_v2));
1528 *num_iovec = num + 1;
1529 return 0;
1530}
1531
1532static int
1533add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1534 struct cifs_open_parms *oparms, bool use_persistent)
1535{
1536 struct smb2_create_req *req = iov[0].iov_base;
1537 unsigned int num = *num_iovec;
1538
1539 if (use_persistent) {
1540 if (oparms->reconnect)
1541 return add_durable_reconnect_v2_context(iov, num_iovec,
1542 oparms);
1543 else
1544 return add_durable_v2_context(iov, num_iovec, oparms);
1545 }
1546
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001547 if (oparms->reconnect) {
1548 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1549 /* indicate that we don't need to relock the file */
1550 oparms->reconnect = false;
1551 } else
1552 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001553 if (iov[num].iov_base == NULL)
1554 return -ENOMEM;
1555 iov[num].iov_len = sizeof(struct create_durable);
1556 if (!req->CreateContextsOffset)
1557 req->CreateContextsOffset =
1558 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1559 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001560 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001561 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1562 *num_iovec = num + 1;
1563 return 0;
1564}
1565
Aurelien Aptelf0712922017-02-22 14:47:17 +01001566static int
1567alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1568 const char *treename, const __le16 *path)
1569{
1570 int treename_len, path_len;
1571 struct nls_table *cp;
1572 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1573
1574 /*
1575 * skip leading "\\"
1576 */
1577 treename_len = strlen(treename);
1578 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1579 return -EINVAL;
1580
1581 treename += 2;
1582 treename_len -= 2;
1583
1584 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1585
1586 /*
1587 * make room for one path separator between the treename and
1588 * path
1589 */
1590 *out_len = treename_len + 1 + path_len;
1591
1592 /*
1593 * final path needs to be null-terminated UTF16 with a
1594 * size aligned to 8
1595 */
1596
1597 *out_size = roundup((*out_len+1)*2, 8);
1598 *out_path = kzalloc(*out_size, GFP_KERNEL);
1599 if (!*out_path)
1600 return -ENOMEM;
1601
1602 cp = load_nls_default();
1603 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1604 UniStrcat(*out_path, sep);
1605 UniStrcat(*out_path, path);
1606 unload_nls(cp);
1607
1608 return 0;
1609}
1610
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001611int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001612SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001613 __u8 *oplock, struct smb2_file_all_info *buf,
1614 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001615{
1616 struct smb2_create_req *req;
1617 struct smb2_create_rsp *rsp;
1618 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001619 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001620 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001621 struct kvec iov[4];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001622 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001623 int resp_buftype;
1624 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001625 __le16 *copy_path = NULL;
1626 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001627 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001628 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001629 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001630 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001631 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001632
Joe Perchesf96637b2013-05-04 22:12:25 -05001633 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001634
1635 if (ses && (ses->server))
1636 server = ses->server;
1637 else
1638 return -EIO;
1639
1640 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1641 if (rc)
1642 return rc;
1643
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001644 if (encryption_required(tcon))
1645 flags |= CIFS_TRANSFORM_REQ;
1646
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001647 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001648 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001649 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1650 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001651
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001652 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001653 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001654 /* File attributes ignored on open (used in create though) */
1655 req->FileAttributes = cpu_to_le32(file_attributes);
1656 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001657 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1658 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001659
1660 iov[0].iov_base = (char *)req;
1661 /* 4 for rfc1002 length field */
1662 iov[0].iov_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001663 /* -1 since last byte is buf[0] which is sent below (path) */
1664 iov[0].iov_len--;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001665
Aurelien Aptelf0712922017-02-22 14:47:17 +01001666 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1667
1668 /* [MS-SMB2] 2.2.13 NameOffset:
1669 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1670 * the SMB2 header, the file name includes a prefix that will
1671 * be processed during DFS name normalization as specified in
1672 * section 3.3.5.9. Otherwise, the file name is relative to
1673 * the share that is identified by the TreeId in the SMB2
1674 * header.
1675 */
1676 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1677 int name_len;
1678
1679 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1680 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1681 &name_len,
1682 tcon->treeName, path);
1683 if (rc)
1684 return rc;
1685 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001686 uni_path_len = copy_size;
1687 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001688 } else {
1689 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1690 /* MUST set path len (NameLength) to 0 opening root of share */
1691 req->NameLength = cpu_to_le16(uni_path_len - 2);
1692 if (uni_path_len % 8 != 0) {
1693 copy_size = roundup(uni_path_len, 8);
1694 copy_path = kzalloc(copy_size, GFP_KERNEL);
1695 if (!copy_path)
1696 return -ENOMEM;
1697 memcpy((char *)copy_path, (const char *)path,
1698 uni_path_len);
1699 uni_path_len = copy_size;
1700 path = copy_path;
1701 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001702 }
1703
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001704 iov[1].iov_len = uni_path_len;
1705 iov[1].iov_base = path;
1706 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1707 inc_rfc1001_len(req, uni_path_len - 1);
1708
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001709 if (!server->oplocks)
1710 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1711
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001712 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001713 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1714 req->RequestedOplockLevel = *oplock;
1715 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001716 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001717 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001718 cifs_small_buf_release(req);
1719 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001720 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001721 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001722 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001723 }
1724
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001725 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1726 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001727 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001728 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001729 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001730 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001731 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001732 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001733
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001734 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001735 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001736 if (rc) {
1737 cifs_small_buf_release(req);
1738 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001739 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001740 return rc;
1741 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001742 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001743 }
1744
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001745 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001746 cifs_small_buf_release(req);
1747 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001748
1749 if (rc != 0) {
1750 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001751 if (err_buf)
1752 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1753 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001754 goto creat_exit;
1755 }
1756
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001757 oparms->fid->persistent_fid = rsp->PersistentFileId;
1758 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001759
1760 if (buf) {
1761 memcpy(buf, &rsp->CreationTime, 32);
1762 buf->AllocationSize = rsp->AllocationSize;
1763 buf->EndOfFile = rsp->EndofFile;
1764 buf->Attributes = rsp->FileAttributes;
1765 buf->NumberOfLinks = cpu_to_le32(1);
1766 buf->DeletePending = 0;
1767 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001768
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001769 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001770 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001771 else
1772 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001773creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001774 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001775 kfree(lc_buf);
1776 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001777 free_rsp_buf(resp_buftype, rsp);
1778 return rc;
1779}
1780
Steve French4a72daf2013-06-25 00:20:49 -05001781/*
1782 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1783 */
1784int
1785SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001786 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1787 char *in_data, u32 indatalen,
1788 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001789{
1790 struct smb2_ioctl_req *req;
1791 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001792 struct smb2_sync_hdr *shdr;
Steve French4a72daf2013-06-25 00:20:49 -05001793 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001794 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001795 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001796 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001797 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001798 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001799 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001800 int flags = 0;
Steve French4a72daf2013-06-25 00:20:49 -05001801
1802 cifs_dbg(FYI, "SMB2 IOCTL\n");
1803
Steve French3d1a3742014-08-11 21:05:25 -05001804 if (out_data != NULL)
1805 *out_data = NULL;
1806
Steve French4a72daf2013-06-25 00:20:49 -05001807 /* zero out returned data len, in case of error */
1808 if (plen)
1809 *plen = 0;
1810
Steve French8e353102015-03-26 19:47:02 -05001811 if (tcon)
1812 ses = tcon->ses;
1813 else
1814 return -EIO;
1815
Steve French4a72daf2013-06-25 00:20:49 -05001816 if (ses && (ses->server))
1817 server = ses->server;
1818 else
1819 return -EIO;
1820
1821 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1822 if (rc)
1823 return rc;
1824
Aurelien Aptel51146622017-02-28 15:08:41 +01001825 if (use_ipc) {
1826 if (ses->ipc_tid == 0) {
1827 cifs_small_buf_release(req);
1828 return -ENOTCONN;
1829 }
1830
1831 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1832 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1833 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1834 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001835 if (encryption_required(tcon))
1836 flags |= CIFS_TRANSFORM_REQ;
1837
Steve French4a72daf2013-06-25 00:20:49 -05001838 req->CtlCode = cpu_to_le32(opcode);
1839 req->PersistentFileId = persistent_fid;
1840 req->VolatileFileId = volatile_fid;
1841
1842 if (indatalen) {
1843 req->InputCount = cpu_to_le32(indatalen);
1844 /* do not set InputOffset if no input data */
1845 req->InputOffset =
1846 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1847 iov[1].iov_base = in_data;
1848 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001849 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001850 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001851 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001852
1853 req->OutputOffset = 0;
1854 req->OutputCount = 0; /* MBZ */
1855
1856 /*
1857 * Could increase MaxOutputResponse, but that would require more
1858 * than one credit. Windows typically sets this smaller, but for some
1859 * ioctls it may be useful to allow server to send more. No point
1860 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001861 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1862 * (by default, note that it can be overridden to make max larger)
1863 * in responses (except for read responses which can be bigger.
1864 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001865 */
Steve French7db0a6e2017-05-03 21:12:20 -05001866 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001867
1868 if (is_fsctl)
1869 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1870 else
1871 req->Flags = 0;
1872
1873 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001874
Steve French7ff8d452013-10-14 00:44:19 -05001875 /*
1876 * If no input data, the size of ioctl struct in
1877 * protocol spec still includes a 1 byte data buffer,
1878 * but if input data passed to ioctl, we do not
1879 * want to double count this, so we do not send
1880 * the dummy one byte of data in iovec[0] if sending
1881 * input data (in iovec[1]). We also must add 4 bytes
1882 * in first iovec to allow for rfc1002 length field.
1883 */
1884
1885 if (indatalen) {
1886 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1887 inc_rfc1001_len(req, indatalen - 1);
1888 } else
1889 iov[0].iov_len = get_rfc1002_length(req) + 4;
1890
Steve French4a72daf2013-06-25 00:20:49 -05001891
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001892 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001893 cifs_small_buf_release(req);
1894 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001895
Steve French9bf0c9c2013-11-16 18:05:28 -06001896 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001897 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001898 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001899 } else if (rc == -EINVAL) {
1900 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1901 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001902 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001903 goto ioctl_exit;
1904 }
Steve French4a72daf2013-06-25 00:20:49 -05001905 }
1906
1907 /* check if caller wants to look at return data or just return rc */
1908 if ((plen == NULL) || (out_data == NULL))
1909 goto ioctl_exit;
1910
1911 *plen = le32_to_cpu(rsp->OutputCount);
1912
1913 /* We check for obvious errors in the output buffer length and offset */
1914 if (*plen == 0)
1915 goto ioctl_exit; /* server returned no data */
1916 else if (*plen > 0xFF00) {
1917 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1918 *plen = 0;
1919 rc = -EIO;
1920 goto ioctl_exit;
1921 }
1922
1923 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1924 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1925 le32_to_cpu(rsp->OutputOffset));
1926 *plen = 0;
1927 rc = -EIO;
1928 goto ioctl_exit;
1929 }
1930
1931 *out_data = kmalloc(*plen, GFP_KERNEL);
1932 if (*out_data == NULL) {
1933 rc = -ENOMEM;
1934 goto ioctl_exit;
1935 }
1936
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001937 shdr = get_sync_hdr(rsp);
1938 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05001939ioctl_exit:
1940 free_rsp_buf(resp_buftype, rsp);
1941 return rc;
1942}
1943
Steve French64a5cfa2013-10-14 15:31:32 -05001944/*
1945 * Individual callers to ioctl worker function follow
1946 */
1947
1948int
1949SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1950 u64 persistent_fid, u64 volatile_fid)
1951{
1952 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001953 struct compress_ioctl fsctl_input;
1954 char *ret_data = NULL;
1955
1956 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001957 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001958
1959 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1960 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001961 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05001962 (char *)&fsctl_input /* data input */,
1963 2 /* in data len */, &ret_data /* out data */, NULL);
1964
1965 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001966
1967 return rc;
1968}
1969
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001970int
1971SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1972 u64 persistent_fid, u64 volatile_fid)
1973{
1974 struct smb2_close_req *req;
1975 struct smb2_close_rsp *rsp;
1976 struct TCP_Server_Info *server;
1977 struct cifs_ses *ses = tcon->ses;
1978 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001979 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001980 int resp_buftype;
1981 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001982 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001983
Joe Perchesf96637b2013-05-04 22:12:25 -05001984 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001985
1986 if (ses && (ses->server))
1987 server = ses->server;
1988 else
1989 return -EIO;
1990
1991 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1992 if (rc)
1993 return rc;
1994
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001995 if (encryption_required(tcon))
1996 flags |= CIFS_TRANSFORM_REQ;
1997
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001998 req->PersistentFileId = persistent_fid;
1999 req->VolatileFileId = volatile_fid;
2000
2001 iov[0].iov_base = (char *)req;
2002 /* 4 for rfc1002 length field */
2003 iov[0].iov_len = get_rfc1002_length(req) + 4;
2004
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002005 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002006 cifs_small_buf_release(req);
2007 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002008
2009 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002010 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002011 goto close_exit;
2012 }
2013
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002014 /* BB FIXME - decode close response, update inode for caching */
2015
2016close_exit:
2017 free_rsp_buf(resp_buftype, rsp);
2018 return rc;
2019}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002020
2021static int
2022validate_buf(unsigned int offset, unsigned int buffer_length,
2023 struct smb2_hdr *hdr, unsigned int min_buf_size)
2024
2025{
2026 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2027 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2028 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2029 char *end_of_buf = begin_of_buf + buffer_length;
2030
2031
2032 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002033 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2034 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002035 return -EINVAL;
2036 }
2037
2038 /* check if beyond RFC1001 maximum length */
2039 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002040 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2041 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002042 return -EINVAL;
2043 }
2044
2045 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002046 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002047 return -EINVAL;
2048 }
2049
2050 return 0;
2051}
2052
2053/*
2054 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2055 * Caller must free buffer.
2056 */
2057static int
2058validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2059 struct smb2_hdr *hdr, unsigned int minbufsize,
2060 char *data)
2061
2062{
2063 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2064 int rc;
2065
2066 if (!data)
2067 return -EINVAL;
2068
2069 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2070 if (rc)
2071 return rc;
2072
2073 memcpy(data, begin_of_buf, buffer_length);
2074
2075 return 0;
2076}
2077
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002078static int
2079query_info(const unsigned int xid, struct cifs_tcon *tcon,
2080 u64 persistent_fid, u64 volatile_fid, u8 info_class,
2081 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002082{
2083 struct smb2_query_info_req *req;
2084 struct smb2_query_info_rsp *rsp = NULL;
2085 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002086 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002087 int rc = 0;
2088 int resp_buftype;
2089 struct TCP_Server_Info *server;
2090 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002091 int flags = 0;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002092
Joe Perchesf96637b2013-05-04 22:12:25 -05002093 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002094
2095 if (ses && (ses->server))
2096 server = ses->server;
2097 else
2098 return -EIO;
2099
2100 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2101 if (rc)
2102 return rc;
2103
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002104 if (encryption_required(tcon))
2105 flags |= CIFS_TRANSFORM_REQ;
2106
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002107 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002108 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002109 req->PersistentFileId = persistent_fid;
2110 req->VolatileFileId = volatile_fid;
2111 /* 4 for rfc1002 length field and 1 for Buffer */
2112 req->InputBufferOffset =
2113 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002114 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002115
2116 iov[0].iov_base = (char *)req;
2117 /* 4 for rfc1002 length field */
2118 iov[0].iov_len = get_rfc1002_length(req) + 4;
2119
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002120 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002121 cifs_small_buf_release(req);
2122 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002123
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002124 if (rc) {
2125 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2126 goto qinf_exit;
2127 }
2128
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002129 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2130 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002131 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002132
2133qinf_exit:
2134 free_rsp_buf(resp_buftype, rsp);
2135 return rc;
2136}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002137
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002138int
2139SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2140 u64 persistent_fid, u64 volatile_fid,
2141 struct smb2_file_all_info *data)
2142{
2143 return query_info(xid, tcon, persistent_fid, volatile_fid,
2144 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002145 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002146 sizeof(struct smb2_file_all_info), data);
2147}
2148
2149int
2150SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2151 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2152{
2153 return query_info(xid, tcon, persistent_fid, volatile_fid,
2154 FILE_INTERNAL_INFORMATION,
2155 sizeof(struct smb2_file_internal_info),
2156 sizeof(struct smb2_file_internal_info), uniqueid);
2157}
2158
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002159/*
2160 * This is a no-op for now. We're not really interested in the reply, but
2161 * rather in the fact that the server sent one and that server->lstrp
2162 * gets updated.
2163 *
2164 * FIXME: maybe we should consider checking that the reply matches request?
2165 */
2166static void
2167smb2_echo_callback(struct mid_q_entry *mid)
2168{
2169 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002170 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002171 unsigned int credits_received = 1;
2172
2173 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002174 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002175
Christopher Oo5fb4e282015-06-25 16:10:48 -07002176 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002177 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002178 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002179 add_credits(server, credits_received, CIFS_ECHO_OP);
2180}
2181
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002182void smb2_reconnect_server(struct work_struct *work)
2183{
2184 struct TCP_Server_Info *server = container_of(work,
2185 struct TCP_Server_Info, reconnect.work);
2186 struct cifs_ses *ses;
2187 struct cifs_tcon *tcon, *tcon2;
2188 struct list_head tmp_list;
2189 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002190 int rc;
2191 int resched = false;
2192
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002193
2194 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2195 mutex_lock(&server->reconnect_mutex);
2196
2197 INIT_LIST_HEAD(&tmp_list);
2198 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2199
2200 spin_lock(&cifs_tcp_ses_lock);
2201 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2202 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002203 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002204 tcon->tc_count++;
2205 list_add_tail(&tcon->rlist, &tmp_list);
2206 tcon_exist = true;
2207 }
2208 }
2209 }
2210 /*
2211 * Get the reference to server struct to be sure that the last call of
2212 * cifs_put_tcon() in the loop below won't release the server pointer.
2213 */
2214 if (tcon_exist)
2215 server->srv_count++;
2216
2217 spin_unlock(&cifs_tcp_ses_lock);
2218
2219 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002220 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2221 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002222 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002223 else
2224 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002225 list_del_init(&tcon->rlist);
2226 cifs_put_tcon(tcon);
2227 }
2228
2229 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002230 if (resched)
2231 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002232 mutex_unlock(&server->reconnect_mutex);
2233
2234 /* now we can safely release srv struct */
2235 if (tcon_exist)
2236 cifs_put_tcp_session(server, 1);
2237}
2238
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002239int
2240SMB2_echo(struct TCP_Server_Info *server)
2241{
2242 struct smb2_echo_req *req;
2243 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002244 struct kvec iov[2];
2245 struct smb_rqst rqst = { .rq_iov = iov,
2246 .rq_nvec = 2 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002247
Joe Perchesf96637b2013-05-04 22:12:25 -05002248 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002249
Steve French4fcd1812016-06-22 20:12:05 -05002250 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002251 /* No need to send echo on newly established connections */
2252 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2253 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002254 }
2255
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002256 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2257 if (rc)
2258 return rc;
2259
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002260 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002261
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002262 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002263 iov[0].iov_len = 4;
2264 iov[0].iov_base = (char *)req;
2265 iov[1].iov_len = get_rfc1002_length(req);
2266 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002267
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002268 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2269 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002270 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002271 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002272
2273 cifs_small_buf_release(req);
2274 return rc;
2275}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002276
2277int
2278SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2279 u64 volatile_fid)
2280{
2281 struct smb2_flush_req *req;
2282 struct TCP_Server_Info *server;
2283 struct cifs_ses *ses = tcon->ses;
2284 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002285 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002286 int resp_buftype;
2287 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002288 int flags = 0;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002289
Joe Perchesf96637b2013-05-04 22:12:25 -05002290 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002291
2292 if (ses && (ses->server))
2293 server = ses->server;
2294 else
2295 return -EIO;
2296
2297 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2298 if (rc)
2299 return rc;
2300
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002301 if (encryption_required(tcon))
2302 flags |= CIFS_TRANSFORM_REQ;
2303
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002304 req->PersistentFileId = persistent_fid;
2305 req->VolatileFileId = volatile_fid;
2306
2307 iov[0].iov_base = (char *)req;
2308 /* 4 for rfc1002 length field */
2309 iov[0].iov_len = get_rfc1002_length(req) + 4;
2310
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002311 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002312 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002313
Steve Frenchdfebe402015-03-27 01:00:06 -05002314 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002315 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2316
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002317 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002318 return rc;
2319}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002320
2321/*
2322 * To form a chain of read requests, any read requests after the first should
2323 * have the end_of_chain boolean set to true.
2324 */
2325static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002326smb2_new_read_req(void **buf, unsigned int *total_len,
2327 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2328 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002329{
2330 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002331 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002332 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002333
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002334 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2335 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002336 if (rc)
2337 return rc;
2338 if (io_parms->tcon->ses->server == NULL)
2339 return -ECONNABORTED;
2340
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002341 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002342 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002343
2344 req->PersistentFileId = io_parms->persistent_fid;
2345 req->VolatileFileId = io_parms->volatile_fid;
2346 req->ReadChannelInfoOffset = 0; /* reserved */
2347 req->ReadChannelInfoLength = 0; /* reserved */
2348 req->Channel = 0; /* reserved */
2349 req->MinimumCount = 0;
2350 req->Length = cpu_to_le32(io_parms->length);
2351 req->Offset = cpu_to_le64(io_parms->offset);
2352
2353 if (request_type & CHAINED_REQUEST) {
2354 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002355 /* next 8-byte aligned request */
2356 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2357 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002358 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002359 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002360 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002361 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002362 /*
2363 * Related requests use info from previous read request
2364 * in chain.
2365 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002366 shdr->SessionId = 0xFFFFFFFF;
2367 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002368 req->PersistentFileId = 0xFFFFFFFF;
2369 req->VolatileFileId = 0xFFFFFFFF;
2370 }
2371 }
2372 if (remaining_bytes > io_parms->length)
2373 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2374 else
2375 req->RemainingBytes = 0;
2376
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002377 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002378 return rc;
2379}
2380
2381static void
2382smb2_readv_callback(struct mid_q_entry *mid)
2383{
2384 struct cifs_readdata *rdata = mid->callback_data;
2385 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2386 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002387 struct smb2_sync_hdr *shdr =
2388 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002389 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002390 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2391 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002392 .rq_pages = rdata->pages,
2393 .rq_npages = rdata->nr_pages,
2394 .rq_pagesz = rdata->pagesz,
2395 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002396
Joe Perchesf96637b2013-05-04 22:12:25 -05002397 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2398 __func__, mid->mid, mid->mid_state, rdata->result,
2399 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002400
2401 switch (mid->mid_state) {
2402 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002403 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002404 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002405 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002406 int rc;
2407
Jeff Layton0b688cf2012-09-18 16:20:34 -07002408 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002409 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002410 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2411 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002412 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002413 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002414 task_io_account_read(rdata->got_bytes);
2415 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002416 break;
2417 case MID_REQUEST_SUBMITTED:
2418 case MID_RETRY_NEEDED:
2419 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002420 if (server->sign && rdata->got_bytes)
2421 /* reset bytes number since we can not check a sign */
2422 rdata->got_bytes = 0;
2423 /* FIXME: should this be counted toward the initiating task? */
2424 task_io_account_read(rdata->got_bytes);
2425 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002426 break;
2427 default:
2428 if (rdata->result != -ENODATA)
2429 rdata->result = -EIO;
2430 }
2431
2432 if (rdata->result)
2433 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2434
2435 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002436 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002437 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002438 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002439 add_credits(server, credits_received, 0);
2440}
2441
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002442/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002443int
2444smb2_async_readv(struct cifs_readdata *rdata)
2445{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002446 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002447 char *buf;
2448 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002449 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002450 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2451 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002452 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002453 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002454 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002455
Joe Perchesf96637b2013-05-04 22:12:25 -05002456 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2457 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002458
2459 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2460 io_parms.offset = rdata->offset;
2461 io_parms.length = rdata->bytes;
2462 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2463 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2464 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002465
2466 server = io_parms.tcon->ses->server;
2467
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002468 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002469 if (rc) {
2470 if (rc == -EAGAIN && rdata->credits) {
2471 /* credits was reset by reconnect */
2472 rdata->credits = 0;
2473 /* reduce in_flight value since we won't send the req */
2474 spin_lock(&server->req_lock);
2475 server->in_flight--;
2476 spin_unlock(&server->req_lock);
2477 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002478 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002479 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002480
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002481 if (encryption_required(io_parms.tcon))
2482 flags |= CIFS_TRANSFORM_REQ;
2483
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002484 req_len = cpu_to_be32(total_len);
2485
2486 rdata->iov[0].iov_base = &req_len;
2487 rdata->iov[0].iov_len = sizeof(__be32);
2488 rdata->iov[1].iov_base = buf;
2489 rdata->iov[1].iov_len = total_len;
2490
2491 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002492
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002493 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002494 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002495 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002496 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002497 spin_lock(&server->req_lock);
2498 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002499 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002500 spin_unlock(&server->req_lock);
2501 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002502 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002503 }
2504
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002505 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002506 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002507 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002508 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002509 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002510 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002511 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2512 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002513
2514 cifs_small_buf_release(buf);
2515 return rc;
2516}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002517
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002518int
2519SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2520 unsigned int *nbytes, char **buf, int *buf_type)
2521{
2522 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002523 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002524 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002525 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002526 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002527 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002528 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002529 __be32 req_len;
2530 struct smb_rqst rqst = { .rq_iov = iov,
2531 .rq_nvec = 2 };
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002532 int flags = CIFS_LOG_ERROR;
2533 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002534
2535 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002536 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002537 if (rc)
2538 return rc;
2539
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002540 if (encryption_required(io_parms->tcon))
2541 flags |= CIFS_TRANSFORM_REQ;
2542
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002543 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002544
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002545 iov[0].iov_base = &req_len;
2546 iov[0].iov_len = sizeof(__be32);
2547 iov[1].iov_base = req;
2548 iov[1].iov_len = total_len;
2549
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002550 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002551 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002552
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002553 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002554 shdr = get_sync_hdr(rsp);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002555
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002556 if (shdr->Status == STATUS_END_OF_FILE) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002557 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002558 return 0;
2559 }
2560
2561 if (rc) {
2562 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002563 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002564 } else {
2565 *nbytes = le32_to_cpu(rsp->DataLength);
2566 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2567 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002568 cifs_dbg(FYI, "bad length %d for count %d\n",
2569 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002570 rc = -EIO;
2571 *nbytes = 0;
2572 }
2573 }
2574
2575 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002576 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002577 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002578 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002579 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002580 if (resp_buftype == CIFS_SMALL_BUFFER)
2581 *buf_type = CIFS_SMALL_BUFFER;
2582 else if (resp_buftype == CIFS_LARGE_BUFFER)
2583 *buf_type = CIFS_LARGE_BUFFER;
2584 }
2585 return rc;
2586}
2587
Pavel Shilovsky33319142012-09-18 16:20:29 -07002588/*
2589 * Check the mid_state and signature on received buffer (if any), and queue the
2590 * workqueue completion task.
2591 */
2592static void
2593smb2_writev_callback(struct mid_q_entry *mid)
2594{
2595 struct cifs_writedata *wdata = mid->callback_data;
2596 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002597 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002598 unsigned int written;
2599 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2600 unsigned int credits_received = 1;
2601
2602 switch (mid->mid_state) {
2603 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002604 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002605 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2606 if (wdata->result != 0)
2607 break;
2608
2609 written = le32_to_cpu(rsp->DataLength);
2610 /*
2611 * Mask off high 16 bits when bytes written as returned
2612 * by the server is greater than bytes requested by the
2613 * client. OS/2 servers are known to set incorrect
2614 * CountHigh values.
2615 */
2616 if (written > wdata->bytes)
2617 written &= 0xFFFF;
2618
2619 if (written < wdata->bytes)
2620 wdata->result = -ENOSPC;
2621 else
2622 wdata->bytes = written;
2623 break;
2624 case MID_REQUEST_SUBMITTED:
2625 case MID_RETRY_NEEDED:
2626 wdata->result = -EAGAIN;
2627 break;
2628 default:
2629 wdata->result = -EIO;
2630 break;
2631 }
2632
2633 if (wdata->result)
2634 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2635
2636 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002637 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002638 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002639 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002640 add_credits(tcon->ses->server, credits_received, 0);
2641}
2642
2643/* smb2_async_writev - send an async write, and set up mid to handle result */
2644int
Steve French4a5c80d2014-02-07 20:45:12 -06002645smb2_async_writev(struct cifs_writedata *wdata,
2646 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002647{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002648 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002649 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002650 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002651 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002652 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002653 struct kvec iov[2];
2654 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002655
2656 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002657 if (rc) {
2658 if (rc == -EAGAIN && wdata->credits) {
2659 /* credits was reset by reconnect */
2660 wdata->credits = 0;
2661 /* reduce in_flight value since we won't send the req */
2662 spin_lock(&server->req_lock);
2663 server->in_flight--;
2664 spin_unlock(&server->req_lock);
2665 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002666 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002667 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002668
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002669 if (encryption_required(tcon))
2670 flags |= CIFS_TRANSFORM_REQ;
2671
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002672 shdr = get_sync_hdr(req);
2673 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002674
2675 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2676 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2677 req->WriteChannelInfoOffset = 0;
2678 req->WriteChannelInfoLength = 0;
2679 req->Channel = 0;
2680 req->Offset = cpu_to_le64(wdata->offset);
2681 /* 4 for rfc1002 length field */
2682 req->DataOffset = cpu_to_le16(
2683 offsetof(struct smb2_write_req, Buffer) - 4);
2684 req->RemainingBytes = 0;
2685
2686 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002687 iov[0].iov_len = 4;
2688 iov[0].iov_base = req;
2689 iov[1].iov_len = get_rfc1002_length(req) - 1;
2690 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002691
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002692 rqst.rq_iov = iov;
2693 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002694 rqst.rq_pages = wdata->pages;
2695 rqst.rq_npages = wdata->nr_pages;
2696 rqst.rq_pagesz = wdata->pagesz;
2697 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002698
Joe Perchesf96637b2013-05-04 22:12:25 -05002699 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2700 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002701
2702 req->Length = cpu_to_le32(wdata->bytes);
2703
2704 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2705
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002706 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002707 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002708 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002709 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002710 spin_lock(&server->req_lock);
2711 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002712 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002713 spin_unlock(&server->req_lock);
2714 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002715 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002716 }
2717
Pavel Shilovsky33319142012-09-18 16:20:29 -07002718 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002719 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2720 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002721
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002722 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002723 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002724 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2725 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002726
Pavel Shilovsky33319142012-09-18 16:20:29 -07002727async_writev_out:
2728 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002729 return rc;
2730}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002731
2732/*
2733 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2734 * The length field from io_parms must be at least 1 and indicates a number of
2735 * elements with data to write that begins with position 1 in iov array. All
2736 * data length is specified by count.
2737 */
2738int
2739SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2740 unsigned int *nbytes, struct kvec *iov, int n_vec)
2741{
2742 int rc = 0;
2743 struct smb2_write_req *req = NULL;
2744 struct smb2_write_rsp *rsp = NULL;
2745 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002746 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002747 int flags = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002748
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002749 *nbytes = 0;
2750
2751 if (n_vec < 1)
2752 return rc;
2753
2754 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2755 if (rc)
2756 return rc;
2757
2758 if (io_parms->tcon->ses->server == NULL)
2759 return -ECONNABORTED;
2760
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002761 if (encryption_required(io_parms->tcon))
2762 flags |= CIFS_TRANSFORM_REQ;
2763
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002764 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002765
2766 req->PersistentFileId = io_parms->persistent_fid;
2767 req->VolatileFileId = io_parms->volatile_fid;
2768 req->WriteChannelInfoOffset = 0;
2769 req->WriteChannelInfoLength = 0;
2770 req->Channel = 0;
2771 req->Length = cpu_to_le32(io_parms->length);
2772 req->Offset = cpu_to_le64(io_parms->offset);
2773 /* 4 for rfc1002 length field */
2774 req->DataOffset = cpu_to_le16(
2775 offsetof(struct smb2_write_req, Buffer) - 4);
2776 req->RemainingBytes = 0;
2777
2778 iov[0].iov_base = (char *)req;
2779 /* 4 for rfc1002 length field and 1 for Buffer */
2780 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2781
2782 /* length of entire message including data to be written */
2783 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2784
2785 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002786 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002787 cifs_small_buf_release(req);
2788 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002789
2790 if (rc) {
2791 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002792 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002793 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002794 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002795
2796 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002797 return rc;
2798}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002799
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002800static unsigned int
2801num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2802{
2803 int len;
2804 unsigned int entrycount = 0;
2805 unsigned int next_offset = 0;
2806 FILE_DIRECTORY_INFO *entryptr;
2807
2808 if (bufstart == NULL)
2809 return 0;
2810
2811 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2812
2813 while (1) {
2814 entryptr = (FILE_DIRECTORY_INFO *)
2815 ((char *)entryptr + next_offset);
2816
2817 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002818 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002819 break;
2820 }
2821
2822 len = le32_to_cpu(entryptr->FileNameLength);
2823 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002824 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2825 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002826 break;
2827 }
2828
2829 *lastentry = (char *)entryptr;
2830 entrycount++;
2831
2832 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2833 if (!next_offset)
2834 break;
2835 }
2836
2837 return entrycount;
2838}
2839
2840/*
2841 * Readdir/FindFirst
2842 */
2843int
2844SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2845 u64 persistent_fid, u64 volatile_fid, int index,
2846 struct cifs_search_info *srch_inf)
2847{
2848 struct smb2_query_directory_req *req;
2849 struct smb2_query_directory_rsp *rsp = NULL;
2850 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002851 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002852 int rc = 0;
2853 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002854 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002855 unsigned char *bufptr;
2856 struct TCP_Server_Info *server;
2857 struct cifs_ses *ses = tcon->ses;
2858 __le16 asteriks = cpu_to_le16('*');
2859 char *end_of_smb;
2860 unsigned int output_size = CIFSMaxBufSize;
2861 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002862 int flags = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002863
2864 if (ses && (ses->server))
2865 server = ses->server;
2866 else
2867 return -EIO;
2868
2869 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2870 if (rc)
2871 return rc;
2872
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002873 if (encryption_required(tcon))
2874 flags |= CIFS_TRANSFORM_REQ;
2875
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002876 switch (srch_inf->info_level) {
2877 case SMB_FIND_FILE_DIRECTORY_INFO:
2878 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2879 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2880 break;
2881 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2882 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2883 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2884 break;
2885 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002886 cifs_dbg(VFS, "info level %u isn't supported\n",
2887 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002888 rc = -EINVAL;
2889 goto qdir_exit;
2890 }
2891
2892 req->FileIndex = cpu_to_le32(index);
2893 req->PersistentFileId = persistent_fid;
2894 req->VolatileFileId = volatile_fid;
2895
2896 len = 0x2;
2897 bufptr = req->Buffer;
2898 memcpy(bufptr, &asteriks, len);
2899
2900 req->FileNameOffset =
2901 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2902 req->FileNameLength = cpu_to_le16(len);
2903 /*
2904 * BB could be 30 bytes or so longer if we used SMB2 specific
2905 * buffer lengths, but this is safe and close enough.
2906 */
2907 output_size = min_t(unsigned int, output_size, server->maxBuf);
2908 output_size = min_t(unsigned int, output_size, 2 << 15);
2909 req->OutputBufferLength = cpu_to_le32(output_size);
2910
2911 iov[0].iov_base = (char *)req;
2912 /* 4 for RFC1001 length and 1 for Buffer */
2913 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2914
2915 iov[1].iov_base = (char *)(req->Buffer);
2916 iov[1].iov_len = len;
2917
2918 inc_rfc1001_len(req, len - 1 /* Buffer */);
2919
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002920 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002921 cifs_small_buf_release(req);
2922 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002923
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002924 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002925 if (rc == -ENODATA &&
2926 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002927 srch_inf->endOfSearch = true;
2928 rc = 0;
2929 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002930 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2931 goto qdir_exit;
2932 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002933
2934 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2935 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2936 info_buf_size);
2937 if (rc)
2938 goto qdir_exit;
2939
2940 srch_inf->unicode = true;
2941
2942 if (srch_inf->ntwrk_buf_start) {
2943 if (srch_inf->smallBuf)
2944 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2945 else
2946 cifs_buf_release(srch_inf->ntwrk_buf_start);
2947 }
2948 srch_inf->ntwrk_buf_start = (char *)rsp;
2949 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2950 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2951 /* 4 for rfc1002 length field */
2952 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2953 srch_inf->entries_in_buffer =
2954 num_entries(srch_inf->srch_entries_start, end_of_smb,
2955 &srch_inf->last_entry, info_buf_size);
2956 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002957 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2958 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2959 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002960 if (resp_buftype == CIFS_LARGE_BUFFER)
2961 srch_inf->smallBuf = false;
2962 else if (resp_buftype == CIFS_SMALL_BUFFER)
2963 srch_inf->smallBuf = true;
2964 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002965 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002966
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002967 return rc;
2968
2969qdir_exit:
2970 free_rsp_buf(resp_buftype, rsp);
2971 return rc;
2972}
2973
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002974static int
2975send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002976 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002977 unsigned int num, void **data, unsigned int *size)
2978{
2979 struct smb2_set_info_req *req;
2980 struct smb2_set_info_rsp *rsp = NULL;
2981 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002982 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002983 int rc = 0;
2984 int resp_buftype;
2985 unsigned int i;
2986 struct TCP_Server_Info *server;
2987 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002988 int flags = 0;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002989
2990 if (ses && (ses->server))
2991 server = ses->server;
2992 else
2993 return -EIO;
2994
2995 if (!num)
2996 return -EINVAL;
2997
2998 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2999 if (!iov)
3000 return -ENOMEM;
3001
3002 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3003 if (rc) {
3004 kfree(iov);
3005 return rc;
3006 }
3007
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003008 if (encryption_required(tcon))
3009 flags |= CIFS_TRANSFORM_REQ;
3010
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003011 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003012
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003013 req->InfoType = SMB2_O_INFO_FILE;
3014 req->FileInfoClass = info_class;
3015 req->PersistentFileId = persistent_fid;
3016 req->VolatileFileId = volatile_fid;
3017
3018 /* 4 for RFC1001 length and 1 for Buffer */
3019 req->BufferOffset =
3020 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3021 req->BufferLength = cpu_to_le32(*size);
3022
3023 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3024
3025 memcpy(req->Buffer, *data, *size);
3026
3027 iov[0].iov_base = (char *)req;
3028 /* 4 for RFC1001 length */
3029 iov[0].iov_len = get_rfc1002_length(req) + 4;
3030
3031 for (i = 1; i < num; i++) {
3032 inc_rfc1001_len(req, size[i]);
3033 le32_add_cpu(&req->BufferLength, size[i]);
3034 iov[i].iov_base = (char *)data[i];
3035 iov[i].iov_len = size[i];
3036 }
3037
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003038 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003039 cifs_small_buf_release(req);
3040 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003041
Steve French7d3fb242013-11-18 09:56:28 -06003042 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003043 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003044
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003045 free_rsp_buf(resp_buftype, rsp);
3046 kfree(iov);
3047 return rc;
3048}
3049
3050int
3051SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3052 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3053{
3054 struct smb2_file_rename_info info;
3055 void **data;
3056 unsigned int size[2];
3057 int rc;
3058 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3059
3060 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3061 if (!data)
3062 return -ENOMEM;
3063
3064 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3065 /* 0 = fail if target already exists */
3066 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3067 info.FileNameLength = cpu_to_le32(len);
3068
3069 data[0] = &info;
3070 size[0] = sizeof(struct smb2_file_rename_info);
3071
3072 data[1] = target_file;
3073 size[1] = len + 2 /* null */;
3074
3075 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003076 current->tgid, FILE_RENAME_INFORMATION, 2, data,
3077 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003078 kfree(data);
3079 return rc;
3080}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003081
3082int
Steve French897fba12016-05-12 21:20:36 -05003083SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3084 u64 persistent_fid, u64 volatile_fid)
3085{
3086 __u8 delete_pending = 1;
3087 void *data;
3088 unsigned int size;
3089
3090 data = &delete_pending;
3091 size = 1; /* sizeof __u8 */
3092
3093 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3094 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
3095 &size);
3096}
3097
3098int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003099SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3100 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3101{
3102 struct smb2_file_link_info info;
3103 void **data;
3104 unsigned int size[2];
3105 int rc;
3106 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3107
3108 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3109 if (!data)
3110 return -ENOMEM;
3111
3112 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3113 /* 0 = fail if link already exists */
3114 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3115 info.FileNameLength = cpu_to_le32(len);
3116
3117 data[0] = &info;
3118 size[0] = sizeof(struct smb2_file_link_info);
3119
3120 data[1] = target_file;
3121 size[1] = len + 2 /* null */;
3122
3123 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003124 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003125 kfree(data);
3126 return rc;
3127}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003128
3129int
3130SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003131 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003132{
3133 struct smb2_file_eof_info info;
3134 void *data;
3135 unsigned int size;
3136
3137 info.EndOfFile = *eof;
3138
3139 data = &info;
3140 size = sizeof(struct smb2_file_eof_info);
3141
Steve Frenchf29ebb42014-07-19 21:44:58 -05003142 if (is_falloc)
3143 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3144 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
3145 else
3146 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3147 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003148}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003149
3150int
3151SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3152 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3153{
3154 unsigned int size;
3155 size = sizeof(FILE_BASIC_INFO);
3156 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3157 current->tgid, FILE_BASIC_INFORMATION, 1,
3158 (void **)&buf, &size);
3159}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003160
3161int
3162SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3163 const u64 persistent_fid, const u64 volatile_fid,
3164 __u8 oplock_level)
3165{
3166 int rc;
3167 struct smb2_oplock_break *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003168 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003169
Joe Perchesf96637b2013-05-04 22:12:25 -05003170 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003171 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003172 if (rc)
3173 return rc;
3174
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003175 if (encryption_required(tcon))
3176 flags |= CIFS_TRANSFORM_REQ;
3177
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003178 req->VolatileFid = volatile_fid;
3179 req->PersistentFid = persistent_fid;
3180 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003181 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003182
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003183 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003184 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003185
3186 if (rc) {
3187 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003188 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003189 }
3190
3191 return rc;
3192}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003193
3194static void
3195copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3196 struct kstatfs *kst)
3197{
3198 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3199 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3200 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3201 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
3202 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3203 return;
3204}
3205
3206static int
3207build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3208 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3209{
3210 int rc;
3211 struct smb2_query_info_req *req;
3212
Joe Perchesf96637b2013-05-04 22:12:25 -05003213 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003214
3215 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3216 return -EIO;
3217
3218 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3219 if (rc)
3220 return rc;
3221
3222 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3223 req->FileInfoClass = level;
3224 req->PersistentFileId = persistent_fid;
3225 req->VolatileFileId = volatile_fid;
3226 /* 4 for rfc1002 length field and 1 for pad */
3227 req->InputBufferOffset =
3228 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3229 req->OutputBufferLength = cpu_to_le32(
3230 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3231
3232 iov->iov_base = (char *)req;
3233 /* 4 for rfc1002 length field */
3234 iov->iov_len = get_rfc1002_length(req) + 4;
3235 return 0;
3236}
3237
3238int
3239SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3240 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3241{
3242 struct smb2_query_info_rsp *rsp = NULL;
3243 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003244 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003245 int rc = 0;
3246 int resp_buftype;
3247 struct cifs_ses *ses = tcon->ses;
3248 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003249 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003250
3251 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3252 sizeof(struct smb2_fs_full_size_info),
3253 persistent_fid, volatile_fid);
3254 if (rc)
3255 return rc;
3256
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003257 if (encryption_required(tcon))
3258 flags |= CIFS_TRANSFORM_REQ;
3259
3260 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003261 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003262 if (rc) {
3263 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003264 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003265 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003266 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003267
3268 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3269 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3270 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3271 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3272 sizeof(struct smb2_fs_full_size_info));
3273 if (!rc)
3274 copy_fs_info_to_kstatfs(info, fsdata);
3275
Steve French34f62642013-10-09 02:07:00 -05003276qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003277 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003278 return rc;
3279}
3280
3281int
3282SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003283 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003284{
3285 struct smb2_query_info_rsp *rsp = NULL;
3286 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003287 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003288 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003289 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003290 struct cifs_ses *ses = tcon->ses;
3291 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003292 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003293
Steven French21671142013-10-09 13:36:35 -05003294 if (level == FS_DEVICE_INFORMATION) {
3295 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3296 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3297 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3298 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3299 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003300 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3301 max_len = sizeof(struct smb3_fs_ss_info);
3302 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003303 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003304 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003305 return -EINVAL;
3306 }
3307
3308 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003309 persistent_fid, volatile_fid);
3310 if (rc)
3311 return rc;
3312
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003313 if (encryption_required(tcon))
3314 flags |= CIFS_TRANSFORM_REQ;
3315
3316 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003317 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003318 if (rc) {
3319 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3320 goto qfsattr_exit;
3321 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003322 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003323
3324 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3325 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003326 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3327 if (rc)
3328 goto qfsattr_exit;
3329
3330 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003331 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3332 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003333 rsp_len, max_len));
3334 else if (level == FS_DEVICE_INFORMATION)
3335 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3336 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003337 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3338 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3339 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3340 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3341 tcon->perf_sector_size =
3342 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3343 }
Steve French34f62642013-10-09 02:07:00 -05003344
3345qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003346 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003347 return rc;
3348}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003349
3350int
3351smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3352 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3353 const __u32 num_lock, struct smb2_lock_element *buf)
3354{
3355 int rc = 0;
3356 struct smb2_lock_req *req = NULL;
3357 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003358 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003359 int resp_buf_type;
3360 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003361 int flags = CIFS_NO_RESP;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003362
Joe Perchesf96637b2013-05-04 22:12:25 -05003363 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003364
3365 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3366 if (rc)
3367 return rc;
3368
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003369 if (encryption_required(tcon))
3370 flags |= CIFS_TRANSFORM_REQ;
3371
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003372 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003373 req->LockCount = cpu_to_le16(num_lock);
3374
3375 req->PersistentFileId = persist_fid;
3376 req->VolatileFileId = volatile_fid;
3377
3378 count = num_lock * sizeof(struct smb2_lock_element);
3379 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3380
3381 iov[0].iov_base = (char *)req;
3382 /* 4 for rfc1002 length field and count for all locks */
3383 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3384 iov[1].iov_base = (char *)buf;
3385 iov[1].iov_len = count;
3386
3387 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003388 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003389 &rsp_iov);
3390 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003391 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003392 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003393 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3394 }
3395
3396 return rc;
3397}
3398
3399int
3400SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3401 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3402 const __u64 length, const __u64 offset, const __u32 lock_flags,
3403 const bool wait)
3404{
3405 struct smb2_lock_element lock;
3406
3407 lock.Offset = cpu_to_le64(offset);
3408 lock.Length = cpu_to_le64(length);
3409 lock.Flags = cpu_to_le32(lock_flags);
3410 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3411 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3412
3413 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3414}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003415
3416int
3417SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3418 __u8 *lease_key, const __le32 lease_state)
3419{
3420 int rc;
3421 struct smb2_lease_ack *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003422 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003423
Joe Perchesf96637b2013-05-04 22:12:25 -05003424 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003425 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003426 if (rc)
3427 return rc;
3428
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003429 if (encryption_required(tcon))
3430 flags |= CIFS_TRANSFORM_REQ;
3431
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003432 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003433 req->StructureSize = cpu_to_le16(36);
3434 inc_rfc1001_len(req, 12);
3435
3436 memcpy(req->LeaseKey, lease_key, 16);
3437 req->LeaseState = lease_state;
3438
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003439 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003440 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003441
3442 if (rc) {
3443 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003444 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003445 }
3446
3447 return rc;
3448}