blob: fb75fe908225d77572eaa0a962ab5cd2afa227dd [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 */
566 if (blob_length == 0)
567 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700568
Jeff Layton38d77c52013-05-26 07:01:00 -0400569 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400570 if (rc)
571 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500572 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500573 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500574 if (rc == 1)
575 rc = 0;
576 else if (rc == 0)
577 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400578 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400579neg_exit:
580 free_rsp_buf(resp_buftype, rsp);
581 return rc;
582}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400583
Steve Frenchff1c0382013-11-19 23:44:46 -0600584int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
585{
586 int rc = 0;
587 struct validate_negotiate_info_req vneg_inbuf;
588 struct validate_negotiate_info_rsp *pneg_rsp;
589 u32 rsplen;
590
591 cifs_dbg(FYI, "validate negotiate\n");
592
593 /*
594 * validation ioctl must be signed, so no point sending this if we
595 * can not sign it. We could eventually change this to selectively
596 * sign just this, the first and only signed request on a connection.
597 * This is good enough for now since a user who wants better security
598 * would also enable signing on the mount. Having validation of
599 * negotiate info for signed connections helps reduce attack vectors
600 */
601 if (tcon->ses->server->sign == false)
602 return 0; /* validation requires signing */
603
604 vneg_inbuf.Capabilities =
605 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100606 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
607 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600608
609 if (tcon->ses->sign)
610 vneg_inbuf.SecurityMode =
611 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
612 else if (global_secflags & CIFSSEC_MAY_SIGN)
613 vneg_inbuf.SecurityMode =
614 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
615 else
616 vneg_inbuf.SecurityMode = 0;
617
618 vneg_inbuf.DialectCount = cpu_to_le16(1);
619 vneg_inbuf.Dialects[0] =
620 cpu_to_le16(tcon->ses->server->vals->protocol_id);
621
622 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
623 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100624 false /* use_ipc */,
Steve Frenchff1c0382013-11-19 23:44:46 -0600625 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
626 (char **)&pneg_rsp, &rsplen);
627
628 if (rc != 0) {
629 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
630 return -EIO;
631 }
632
633 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
634 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
635 return -EIO;
636 }
637
638 /* check validate negotiate info response matches what we got earlier */
639 if (pneg_rsp->Dialect !=
640 cpu_to_le16(tcon->ses->server->vals->protocol_id))
641 goto vneg_out;
642
643 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
644 goto vneg_out;
645
646 /* do not validate server guid because not saved at negprot time yet */
647
648 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
649 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
650 goto vneg_out;
651
652 /* validate negotiate successful */
653 cifs_dbg(FYI, "validate negotiate info successful\n");
654 return 0;
655
656vneg_out:
657 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
658 return -EIO;
659}
660
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530661enum securityEnum
662smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
663{
664 switch (requested) {
665 case Kerberos:
666 case RawNTLMSSP:
667 return requested;
668 case NTLMv2:
669 return RawNTLMSSP;
670 case Unspecified:
671 if (server->sec_ntlmssp &&
672 (global_secflags & CIFSSEC_MAY_NTLMSSP))
673 return RawNTLMSSP;
674 if ((server->sec_kerberos || server->sec_mskerberos) &&
675 (global_secflags & CIFSSEC_MAY_KRB5))
676 return Kerberos;
677 /* Fallthrough */
678 default:
679 return Unspecified;
680 }
681}
682
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100683struct SMB2_sess_data {
684 unsigned int xid;
685 struct cifs_ses *ses;
686 struct nls_table *nls_cp;
687 void (*func)(struct SMB2_sess_data *);
688 int result;
689 u64 previous_session;
690
691 /* we will send the SMB in three pieces:
692 * a fixed length beginning part, an optional
693 * SPNEGO blob (which can be zero length), and a
694 * last part which will include the strings
695 * and rest of bcc area. This allows us to avoid
696 * a large buffer 17K allocation
697 */
698 int buf0_type;
699 struct kvec iov[2];
700};
701
702static int
703SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
704{
705 int rc;
706 struct cifs_ses *ses = sess_data->ses;
707 struct smb2_sess_setup_req *req;
708 struct TCP_Server_Info *server = ses->server;
709
710 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
711 if (rc)
712 return rc;
713
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700714 /* First session, not a reauthenticate */
715 req->hdr.sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100716
717 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
718 req->PreviousSessionId = sess_data->previous_session;
719
720 req->Flags = 0; /* MBZ */
721 /* to enable echos and oplocks */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700722 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100723
724 /* only one of SMB2 signing flags may be set in SMB2 request */
725 if (server->sign)
726 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
727 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
728 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
729 else
730 req->SecurityMode = 0;
731
732 req->Capabilities = 0;
733 req->Channel = 0; /* MBZ */
734
735 sess_data->iov[0].iov_base = (char *)req;
736 /* 4 for rfc1002 length field and 1 for pad */
737 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
738 /*
739 * This variable will be used to clear the buffer
740 * allocated above in case of any error in the calling function.
741 */
742 sess_data->buf0_type = CIFS_SMALL_BUFFER;
743
744 return 0;
745}
746
747static void
748SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
749{
750 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
751 sess_data->buf0_type = CIFS_NO_BUFFER;
752}
753
754static int
755SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
756{
757 int rc;
758 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700759 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100760
761 /* Testing shows that buffer offset must be at location of Buffer[0] */
762 req->SecurityBufferOffset =
763 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
764 1 /* pad */ - 4 /* rfc1001 len */);
765 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
766
767 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
768
769 /* BB add code to build os and lm fields */
770
771 rc = SendReceive2(sess_data->xid, sess_data->ses,
772 sess_data->iov, 2,
773 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700774 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
775 cifs_small_buf_release(sess_data->iov[0].iov_base);
776 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100777
778 return rc;
779}
780
781static int
782SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
783{
784 int rc = 0;
785 struct cifs_ses *ses = sess_data->ses;
786
787 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800788 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100789 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100790 if (rc) {
791 cifs_dbg(FYI,
792 "SMB3 session key generation failed\n");
793 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800794 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100795 }
796 }
797 if (!ses->server->session_estab) {
798 ses->server->sequence_number = 0x2;
799 ses->server->session_estab = true;
800 }
801 mutex_unlock(&ses->server->srv_mutex);
802
803 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
804 spin_lock(&GlobalMid_Lock);
805 ses->status = CifsGood;
806 ses->need_reconnect = false;
807 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100808 return rc;
809}
810
811#ifdef CONFIG_CIFS_UPCALL
812static void
813SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
814{
815 int rc;
816 struct cifs_ses *ses = sess_data->ses;
817 struct cifs_spnego_msg *msg;
818 struct key *spnego_key = NULL;
819 struct smb2_sess_setup_rsp *rsp = NULL;
820
821 rc = SMB2_sess_alloc_buffer(sess_data);
822 if (rc)
823 goto out;
824
825 spnego_key = cifs_get_spnego_key(ses);
826 if (IS_ERR(spnego_key)) {
827 rc = PTR_ERR(spnego_key);
828 spnego_key = NULL;
829 goto out;
830 }
831
832 msg = spnego_key->payload.data[0];
833 /*
834 * check version field to make sure that cifs.upcall is
835 * sending us a response in an expected form
836 */
837 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
838 cifs_dbg(VFS,
839 "bad cifs.upcall version. Expected %d got %d",
840 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
841 rc = -EKEYREJECTED;
842 goto out_put_spnego_key;
843 }
844
845 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
846 GFP_KERNEL);
847 if (!ses->auth_key.response) {
848 cifs_dbg(VFS,
849 "Kerberos can't allocate (%u bytes) memory",
850 msg->sesskey_len);
851 rc = -ENOMEM;
852 goto out_put_spnego_key;
853 }
854 ses->auth_key.len = msg->sesskey_len;
855
856 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
857 sess_data->iov[1].iov_len = msg->secblob_len;
858
859 rc = SMB2_sess_sendreceive(sess_data);
860 if (rc)
861 goto out_put_spnego_key;
862
863 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700864 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100865
866 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100867
868 rc = SMB2_sess_establish_session(sess_data);
869out_put_spnego_key:
870 key_invalidate(spnego_key);
871 key_put(spnego_key);
872out:
873 sess_data->result = rc;
874 sess_data->func = NULL;
875 SMB2_sess_free_buffer(sess_data);
876}
877#else
878static void
879SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
880{
881 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
882 sess_data->result = -EOPNOTSUPP;
883 sess_data->func = NULL;
884}
885#endif
886
Sachin Prabhu166cea42016-10-07 19:11:22 +0100887static void
888SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
889
890static void
891SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
892{
893 int rc;
894 struct cifs_ses *ses = sess_data->ses;
895 struct smb2_sess_setup_rsp *rsp = NULL;
896 char *ntlmssp_blob = NULL;
897 bool use_spnego = false; /* else use raw ntlmssp */
898 u16 blob_length = 0;
899
900 /*
901 * If memory allocation is successful, caller of this function
902 * frees it.
903 */
904 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
905 if (!ses->ntlmssp) {
906 rc = -ENOMEM;
907 goto out_err;
908 }
909 ses->ntlmssp->sesskey_per_smbsess = true;
910
911 rc = SMB2_sess_alloc_buffer(sess_data);
912 if (rc)
913 goto out_err;
914
915 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
916 GFP_KERNEL);
917 if (ntlmssp_blob == NULL) {
918 rc = -ENOMEM;
919 goto out;
920 }
921
922 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
923 if (use_spnego) {
924 /* BB eventually need to add this */
925 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
926 rc = -EOPNOTSUPP;
927 goto out;
928 } else {
929 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
930 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
931 }
932 sess_data->iov[1].iov_base = ntlmssp_blob;
933 sess_data->iov[1].iov_len = blob_length;
934
935 rc = SMB2_sess_sendreceive(sess_data);
936 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
937
938 /* If true, rc here is expected and not an error */
939 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700940 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +0100941 rc = 0;
942
943 if (rc)
944 goto out;
945
946 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
947 le16_to_cpu(rsp->SecurityBufferOffset)) {
948 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
949 le16_to_cpu(rsp->SecurityBufferOffset));
950 rc = -EIO;
951 goto out;
952 }
953 rc = decode_ntlmssp_challenge(rsp->Buffer,
954 le16_to_cpu(rsp->SecurityBufferLength), ses);
955 if (rc)
956 goto out;
957
958 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
959
960
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700961 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100962 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +0100963
964out:
965 kfree(ntlmssp_blob);
966 SMB2_sess_free_buffer(sess_data);
967 if (!rc) {
968 sess_data->result = 0;
969 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
970 return;
971 }
972out_err:
973 kfree(ses->ntlmssp);
974 ses->ntlmssp = NULL;
975 sess_data->result = rc;
976 sess_data->func = NULL;
977}
978
979static void
980SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
981{
982 int rc;
983 struct cifs_ses *ses = sess_data->ses;
984 struct smb2_sess_setup_req *req;
985 struct smb2_sess_setup_rsp *rsp = NULL;
986 unsigned char *ntlmssp_blob = NULL;
987 bool use_spnego = false; /* else use raw ntlmssp */
988 u16 blob_length = 0;
989
990 rc = SMB2_sess_alloc_buffer(sess_data);
991 if (rc)
992 goto out;
993
994 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700995 req->hdr.sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100996
997 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
998 sess_data->nls_cp);
999 if (rc) {
1000 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1001 goto out;
1002 }
1003
1004 if (use_spnego) {
1005 /* BB eventually need to add this */
1006 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1007 rc = -EOPNOTSUPP;
1008 goto out;
1009 }
1010 sess_data->iov[1].iov_base = ntlmssp_blob;
1011 sess_data->iov[1].iov_len = blob_length;
1012
1013 rc = SMB2_sess_sendreceive(sess_data);
1014 if (rc)
1015 goto out;
1016
1017 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1018
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001019 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001020 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001021
1022 rc = SMB2_sess_establish_session(sess_data);
1023out:
1024 kfree(ntlmssp_blob);
1025 SMB2_sess_free_buffer(sess_data);
1026 kfree(ses->ntlmssp);
1027 ses->ntlmssp = NULL;
1028 sess_data->result = rc;
1029 sess_data->func = NULL;
1030}
1031
1032static int
1033SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1034{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301035 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001036
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301037 type = smb2_select_sectype(ses->server, ses->sectype);
1038 cifs_dbg(FYI, "sess setup type %d\n", type);
1039 if (type == Unspecified) {
1040 cifs_dbg(VFS,
1041 "Unable to select appropriate authentication method!");
1042 return -EINVAL;
1043 }
1044
1045 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001046 case Kerberos:
1047 sess_data->func = SMB2_auth_kerberos;
1048 break;
1049 case RawNTLMSSP:
1050 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1051 break;
1052 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301053 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001054 return -EOPNOTSUPP;
1055 }
1056
1057 return 0;
1058}
1059
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001060int
1061SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1062 const struct nls_table *nls_cp)
1063{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001064 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001065 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001066 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001067
Joe Perchesf96637b2013-05-04 22:12:25 -05001068 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001069
Jeff Layton3534b852013-05-24 07:41:01 -04001070 if (!server) {
1071 WARN(1, "%s: server is NULL!\n", __func__);
1072 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001073 }
1074
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001075 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1076 if (!sess_data)
1077 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001078
1079 rc = SMB2_select_sec(ses, sess_data);
1080 if (rc)
1081 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001082 sess_data->xid = xid;
1083 sess_data->ses = ses;
1084 sess_data->buf0_type = CIFS_NO_BUFFER;
1085 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001086
Sachin Prabhu166cea42016-10-07 19:11:22 +01001087 while (sess_data->func)
1088 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001089
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001090 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001091out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001092 kfree(sess_data);
1093 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001094}
1095
1096int
1097SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1098{
1099 struct smb2_logoff_req *req; /* response is also trivial struct */
1100 int rc = 0;
1101 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001102 int flags = 0;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001103
Joe Perchesf96637b2013-05-04 22:12:25 -05001104 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001105
1106 if (ses && (ses->server))
1107 server = ses->server;
1108 else
1109 return -EIO;
1110
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001111 /* no need to send SMB logoff if uid already closed due to reconnect */
1112 if (ses->need_reconnect)
1113 goto smb2_session_already_dead;
1114
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001115 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1116 if (rc)
1117 return rc;
1118
1119 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001120 req->hdr.sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001121
1122 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1123 flags |= CIFS_TRANSFORM_REQ;
1124 else if (server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001125 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001126
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001127 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001128 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001129 /*
1130 * No tcon so can't do
1131 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1132 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001133
1134smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001135 return rc;
1136}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001137
1138static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1139{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001140 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001141}
1142
1143#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1144
Steve Frenchde9f68d2013-11-15 11:26:24 -06001145/* These are similar values to what Windows uses */
1146static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1147{
1148 tcon->max_chunks = 256;
1149 tcon->max_bytes_chunk = 1048576;
1150 tcon->max_bytes_copy = 16777216;
1151}
1152
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001153int
1154SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1155 struct cifs_tcon *tcon, const struct nls_table *cp)
1156{
1157 struct smb2_tree_connect_req *req;
1158 struct smb2_tree_connect_rsp *rsp = NULL;
1159 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001160 struct kvec rsp_iov;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001161 int rc = 0;
1162 int resp_buftype;
1163 int unc_path_len;
1164 struct TCP_Server_Info *server;
1165 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001166 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001167
Joe Perchesf96637b2013-05-04 22:12:25 -05001168 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001169
1170 if ((ses->server) && tree)
1171 server = ses->server;
1172 else
1173 return -EIO;
1174
1175 if (tcon && tcon->bad_network_name)
1176 return -ENOENT;
1177
1178 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1179 if (unc_path == NULL)
1180 return -ENOMEM;
1181
1182 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1183 unc_path_len *= 2;
1184 if (unc_path_len < 2) {
1185 kfree(unc_path);
1186 return -EINVAL;
1187 }
1188
1189 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1190 if (rc) {
1191 kfree(unc_path);
1192 return rc;
1193 }
1194
1195 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001196 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1197 flags |= CIFS_TRANSFORM_REQ;
1198
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001199 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001200 req->hdr.sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001201 if (ses->server->sign)
1202 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001203 } else if (encryption_required(tcon))
1204 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001205
1206 iov[0].iov_base = (char *)req;
1207 /* 4 for rfc1002 length field and 1 for pad */
1208 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1209
1210 /* Testing shows that buffer offset must be at location of Buffer[0] */
1211 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1212 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1213 req->PathLength = cpu_to_le16(unc_path_len - 2);
1214 iov[1].iov_base = unc_path;
1215 iov[1].iov_len = unc_path_len;
1216
1217 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1218
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001219 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001220 cifs_small_buf_release(req);
1221 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001222
1223 if (rc != 0) {
1224 if (tcon) {
1225 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1226 tcon->need_reconnect = true;
1227 }
1228 goto tcon_error_exit;
1229 }
1230
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001231 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001232 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001233 goto tcon_exit;
1234 }
1235
1236 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001237 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001238 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1239 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001240 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001241 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1242 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001243 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001244 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001245 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001246 rc = -EOPNOTSUPP;
1247 goto tcon_error_exit;
1248 }
1249
1250 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001251 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001252 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1253 tcon->tidStatus = CifsGood;
1254 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001255 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001256 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001257
1258 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1259 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001260 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001261
1262 if (tcon->seal &&
1263 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1264 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1265
Steve Frenchde9f68d2013-11-15 11:26:24 -06001266 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001267 if (tcon->ses->server->ops->validate_negotiate)
1268 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001269tcon_exit:
1270 free_rsp_buf(resp_buftype, rsp);
1271 kfree(unc_path);
1272 return rc;
1273
1274tcon_error_exit:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001275 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001276 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Steve French18f39e72014-08-17 00:22:24 -05001277 if (tcon)
1278 tcon->bad_network_name = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001279 }
1280 goto tcon_exit;
1281}
1282
1283int
1284SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1285{
1286 struct smb2_tree_disconnect_req *req; /* response is trivial */
1287 int rc = 0;
1288 struct TCP_Server_Info *server;
1289 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001290 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001291
Joe Perchesf96637b2013-05-04 22:12:25 -05001292 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001293
1294 if (ses && (ses->server))
1295 server = ses->server;
1296 else
1297 return -EIO;
1298
1299 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1300 return 0;
1301
1302 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1303 if (rc)
1304 return rc;
1305
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001306 if (encryption_required(tcon))
1307 flags |= CIFS_TRANSFORM_REQ;
1308
1309 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001310 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001311 if (rc)
1312 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1313
1314 return rc;
1315}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001316
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001317
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001318static struct create_durable *
1319create_durable_buf(void)
1320{
1321 struct create_durable *buf;
1322
1323 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1324 if (!buf)
1325 return NULL;
1326
1327 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001328 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001329 buf->ccontext.DataLength = cpu_to_le32(16);
1330 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1331 (struct create_durable, Name));
1332 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001333 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001334 buf->Name[0] = 'D';
1335 buf->Name[1] = 'H';
1336 buf->Name[2] = 'n';
1337 buf->Name[3] = 'Q';
1338 return buf;
1339}
1340
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001341static struct create_durable *
1342create_reconnect_durable_buf(struct cifs_fid *fid)
1343{
1344 struct create_durable *buf;
1345
1346 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1347 if (!buf)
1348 return NULL;
1349
1350 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1351 (struct create_durable, Data));
1352 buf->ccontext.DataLength = cpu_to_le32(16);
1353 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1354 (struct create_durable, Name));
1355 buf->ccontext.NameLength = cpu_to_le16(4);
1356 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1357 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001358 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001359 buf->Name[0] = 'D';
1360 buf->Name[1] = 'H';
1361 buf->Name[2] = 'n';
1362 buf->Name[3] = 'C';
1363 return buf;
1364}
1365
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001366static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001367parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1368 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001369{
1370 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001371 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001372 unsigned int next;
1373 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001374 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001375
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001376 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001377 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001378 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001379 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001380 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001381 if (le16_to_cpu(cc->NameLength) == 4 &&
1382 strncmp(name, "RqLs", 4) == 0)
1383 return server->ops->parse_lease_buf(cc, epoch);
1384
1385 next = le32_to_cpu(cc->Next);
1386 if (!next)
1387 break;
1388 remaining -= next;
1389 cc = (struct create_context *)((char *)cc + next);
1390 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001391
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001392 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001393}
1394
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001395static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001396add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1397 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001398{
1399 struct smb2_create_req *req = iov[0].iov_base;
1400 unsigned int num = *num_iovec;
1401
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001402 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001403 if (iov[num].iov_base == NULL)
1404 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001405 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001406 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1407 if (!req->CreateContextsOffset)
1408 req->CreateContextsOffset = cpu_to_le32(
1409 sizeof(struct smb2_create_req) - 4 +
1410 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001411 le32_add_cpu(&req->CreateContextsLength,
1412 server->vals->create_lease_size);
1413 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001414 *num_iovec = num + 1;
1415 return 0;
1416}
1417
Steve Frenchb56eae42015-11-03 09:26:27 -06001418static struct create_durable_v2 *
1419create_durable_v2_buf(struct cifs_fid *pfid)
1420{
1421 struct create_durable_v2 *buf;
1422
1423 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1424 if (!buf)
1425 return NULL;
1426
1427 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1428 (struct create_durable_v2, dcontext));
1429 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1430 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1431 (struct create_durable_v2, Name));
1432 buf->ccontext.NameLength = cpu_to_le16(4);
1433
1434 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1435 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001436 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001437 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1438
1439 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1440 buf->Name[0] = 'D';
1441 buf->Name[1] = 'H';
1442 buf->Name[2] = '2';
1443 buf->Name[3] = 'Q';
1444 return buf;
1445}
1446
1447static struct create_durable_handle_reconnect_v2 *
1448create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1449{
1450 struct create_durable_handle_reconnect_v2 *buf;
1451
1452 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1453 GFP_KERNEL);
1454 if (!buf)
1455 return NULL;
1456
1457 buf->ccontext.DataOffset =
1458 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1459 dcontext));
1460 buf->ccontext.DataLength =
1461 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1462 buf->ccontext.NameOffset =
1463 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1464 Name));
1465 buf->ccontext.NameLength = cpu_to_le16(4);
1466
1467 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1468 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1469 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1470 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1471
1472 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1473 buf->Name[0] = 'D';
1474 buf->Name[1] = 'H';
1475 buf->Name[2] = '2';
1476 buf->Name[3] = 'C';
1477 return buf;
1478}
1479
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001480static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001481add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001482 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001483{
1484 struct smb2_create_req *req = iov[0].iov_base;
1485 unsigned int num = *num_iovec;
1486
Steve Frenchb56eae42015-11-03 09:26:27 -06001487 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1488 if (iov[num].iov_base == NULL)
1489 return -ENOMEM;
1490 iov[num].iov_len = sizeof(struct create_durable_v2);
1491 if (!req->CreateContextsOffset)
1492 req->CreateContextsOffset =
1493 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1494 iov[1].iov_len);
1495 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1496 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1497 *num_iovec = num + 1;
1498 return 0;
1499}
1500
1501static int
1502add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1503 struct cifs_open_parms *oparms)
1504{
1505 struct smb2_create_req *req = iov[0].iov_base;
1506 unsigned int num = *num_iovec;
1507
1508 /* indicate that we don't need to relock the file */
1509 oparms->reconnect = false;
1510
1511 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1512 if (iov[num].iov_base == NULL)
1513 return -ENOMEM;
1514 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1515 if (!req->CreateContextsOffset)
1516 req->CreateContextsOffset =
1517 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1518 iov[1].iov_len);
1519 le32_add_cpu(&req->CreateContextsLength,
1520 sizeof(struct create_durable_handle_reconnect_v2));
1521 inc_rfc1001_len(&req->hdr,
1522 sizeof(struct create_durable_handle_reconnect_v2));
1523 *num_iovec = num + 1;
1524 return 0;
1525}
1526
1527static int
1528add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1529 struct cifs_open_parms *oparms, bool use_persistent)
1530{
1531 struct smb2_create_req *req = iov[0].iov_base;
1532 unsigned int num = *num_iovec;
1533
1534 if (use_persistent) {
1535 if (oparms->reconnect)
1536 return add_durable_reconnect_v2_context(iov, num_iovec,
1537 oparms);
1538 else
1539 return add_durable_v2_context(iov, num_iovec, oparms);
1540 }
1541
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001542 if (oparms->reconnect) {
1543 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1544 /* indicate that we don't need to relock the file */
1545 oparms->reconnect = false;
1546 } else
1547 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001548 if (iov[num].iov_base == NULL)
1549 return -ENOMEM;
1550 iov[num].iov_len = sizeof(struct create_durable);
1551 if (!req->CreateContextsOffset)
1552 req->CreateContextsOffset =
1553 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1554 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001555 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001556 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1557 *num_iovec = num + 1;
1558 return 0;
1559}
1560
Aurelien Aptelf0712922017-02-22 14:47:17 +01001561static int
1562alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1563 const char *treename, const __le16 *path)
1564{
1565 int treename_len, path_len;
1566 struct nls_table *cp;
1567 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1568
1569 /*
1570 * skip leading "\\"
1571 */
1572 treename_len = strlen(treename);
1573 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1574 return -EINVAL;
1575
1576 treename += 2;
1577 treename_len -= 2;
1578
1579 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1580
1581 /*
1582 * make room for one path separator between the treename and
1583 * path
1584 */
1585 *out_len = treename_len + 1 + path_len;
1586
1587 /*
1588 * final path needs to be null-terminated UTF16 with a
1589 * size aligned to 8
1590 */
1591
1592 *out_size = roundup((*out_len+1)*2, 8);
1593 *out_path = kzalloc(*out_size, GFP_KERNEL);
1594 if (!*out_path)
1595 return -ENOMEM;
1596
1597 cp = load_nls_default();
1598 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1599 UniStrcat(*out_path, sep);
1600 UniStrcat(*out_path, path);
1601 unload_nls(cp);
1602
1603 return 0;
1604}
1605
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001606int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001607SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001608 __u8 *oplock, struct smb2_file_all_info *buf,
1609 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001610{
1611 struct smb2_create_req *req;
1612 struct smb2_create_rsp *rsp;
1613 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001614 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001615 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001616 struct kvec iov[4];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001617 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001618 int resp_buftype;
1619 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001620 __le16 *copy_path = NULL;
1621 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001622 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001623 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001624 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001625 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001626 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001627
Joe Perchesf96637b2013-05-04 22:12:25 -05001628 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001629
1630 if (ses && (ses->server))
1631 server = ses->server;
1632 else
1633 return -EIO;
1634
1635 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1636 if (rc)
1637 return rc;
1638
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001639 if (encryption_required(tcon))
1640 flags |= CIFS_TRANSFORM_REQ;
1641
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001642 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001643 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001644 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1645 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001646
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001647 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001648 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001649 /* File attributes ignored on open (used in create though) */
1650 req->FileAttributes = cpu_to_le32(file_attributes);
1651 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001652 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1653 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001654
1655 iov[0].iov_base = (char *)req;
1656 /* 4 for rfc1002 length field */
1657 iov[0].iov_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001658 /* -1 since last byte is buf[0] which is sent below (path) */
1659 iov[0].iov_len--;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001660
Aurelien Aptelf0712922017-02-22 14:47:17 +01001661 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1662
1663 /* [MS-SMB2] 2.2.13 NameOffset:
1664 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1665 * the SMB2 header, the file name includes a prefix that will
1666 * be processed during DFS name normalization as specified in
1667 * section 3.3.5.9. Otherwise, the file name is relative to
1668 * the share that is identified by the TreeId in the SMB2
1669 * header.
1670 */
1671 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1672 int name_len;
1673
1674 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1675 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1676 &name_len,
1677 tcon->treeName, path);
1678 if (rc)
1679 return rc;
1680 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001681 uni_path_len = copy_size;
1682 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001683 } else {
1684 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1685 /* MUST set path len (NameLength) to 0 opening root of share */
1686 req->NameLength = cpu_to_le16(uni_path_len - 2);
1687 if (uni_path_len % 8 != 0) {
1688 copy_size = roundup(uni_path_len, 8);
1689 copy_path = kzalloc(copy_size, GFP_KERNEL);
1690 if (!copy_path)
1691 return -ENOMEM;
1692 memcpy((char *)copy_path, (const char *)path,
1693 uni_path_len);
1694 uni_path_len = copy_size;
1695 path = copy_path;
1696 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001697 }
1698
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001699 iov[1].iov_len = uni_path_len;
1700 iov[1].iov_base = path;
1701 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1702 inc_rfc1001_len(req, uni_path_len - 1);
1703
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001704 if (!server->oplocks)
1705 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1706
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001707 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001708 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1709 req->RequestedOplockLevel = *oplock;
1710 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001711 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001712 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001713 cifs_small_buf_release(req);
1714 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001715 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001716 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001717 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001718 }
1719
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001720 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1721 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001722 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001723 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001724 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001725 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001726 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001727 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001728
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001729 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001730 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001731 if (rc) {
1732 cifs_small_buf_release(req);
1733 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001734 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001735 return rc;
1736 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001737 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001738 }
1739
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001740 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001741 cifs_small_buf_release(req);
1742 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001743
1744 if (rc != 0) {
1745 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001746 if (err_buf)
1747 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1748 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001749 goto creat_exit;
1750 }
1751
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001752 oparms->fid->persistent_fid = rsp->PersistentFileId;
1753 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001754
1755 if (buf) {
1756 memcpy(buf, &rsp->CreationTime, 32);
1757 buf->AllocationSize = rsp->AllocationSize;
1758 buf->EndOfFile = rsp->EndofFile;
1759 buf->Attributes = rsp->FileAttributes;
1760 buf->NumberOfLinks = cpu_to_le32(1);
1761 buf->DeletePending = 0;
1762 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001763
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001764 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001765 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001766 else
1767 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001768creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001769 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001770 kfree(lc_buf);
1771 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001772 free_rsp_buf(resp_buftype, rsp);
1773 return rc;
1774}
1775
Steve French4a72daf2013-06-25 00:20:49 -05001776/*
1777 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1778 */
1779int
1780SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001781 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1782 char *in_data, u32 indatalen,
1783 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001784{
1785 struct smb2_ioctl_req *req;
1786 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001787 struct smb2_sync_hdr *shdr;
Steve French4a72daf2013-06-25 00:20:49 -05001788 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001789 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001790 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001791 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001792 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001793 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001794 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001795 int flags = 0;
Steve French4a72daf2013-06-25 00:20:49 -05001796
1797 cifs_dbg(FYI, "SMB2 IOCTL\n");
1798
Steve French3d1a3742014-08-11 21:05:25 -05001799 if (out_data != NULL)
1800 *out_data = NULL;
1801
Steve French4a72daf2013-06-25 00:20:49 -05001802 /* zero out returned data len, in case of error */
1803 if (plen)
1804 *plen = 0;
1805
Steve French8e353102015-03-26 19:47:02 -05001806 if (tcon)
1807 ses = tcon->ses;
1808 else
1809 return -EIO;
1810
Steve French4a72daf2013-06-25 00:20:49 -05001811 if (ses && (ses->server))
1812 server = ses->server;
1813 else
1814 return -EIO;
1815
1816 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1817 if (rc)
1818 return rc;
1819
Aurelien Aptel51146622017-02-28 15:08:41 +01001820 if (use_ipc) {
1821 if (ses->ipc_tid == 0) {
1822 cifs_small_buf_release(req);
1823 return -ENOTCONN;
1824 }
1825
1826 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1827 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1828 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1829 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001830 if (encryption_required(tcon))
1831 flags |= CIFS_TRANSFORM_REQ;
1832
Steve French4a72daf2013-06-25 00:20:49 -05001833 req->CtlCode = cpu_to_le32(opcode);
1834 req->PersistentFileId = persistent_fid;
1835 req->VolatileFileId = volatile_fid;
1836
1837 if (indatalen) {
1838 req->InputCount = cpu_to_le32(indatalen);
1839 /* do not set InputOffset if no input data */
1840 req->InputOffset =
1841 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1842 iov[1].iov_base = in_data;
1843 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001844 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001845 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001846 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001847
1848 req->OutputOffset = 0;
1849 req->OutputCount = 0; /* MBZ */
1850
1851 /*
1852 * Could increase MaxOutputResponse, but that would require more
1853 * than one credit. Windows typically sets this smaller, but for some
1854 * ioctls it may be useful to allow server to send more. No point
1855 * limiting what the server can send as long as fits in one credit
1856 */
1857 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1858
1859 if (is_fsctl)
1860 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1861 else
1862 req->Flags = 0;
1863
1864 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001865
Steve French7ff8d452013-10-14 00:44:19 -05001866 /*
1867 * If no input data, the size of ioctl struct in
1868 * protocol spec still includes a 1 byte data buffer,
1869 * but if input data passed to ioctl, we do not
1870 * want to double count this, so we do not send
1871 * the dummy one byte of data in iovec[0] if sending
1872 * input data (in iovec[1]). We also must add 4 bytes
1873 * in first iovec to allow for rfc1002 length field.
1874 */
1875
1876 if (indatalen) {
1877 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1878 inc_rfc1001_len(req, indatalen - 1);
1879 } else
1880 iov[0].iov_len = get_rfc1002_length(req) + 4;
1881
Steve French4a72daf2013-06-25 00:20:49 -05001882
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001883 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001884 cifs_small_buf_release(req);
1885 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001886
Steve French9bf0c9c2013-11-16 18:05:28 -06001887 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001888 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001889 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001890 } else if (rc == -EINVAL) {
1891 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1892 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001893 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001894 goto ioctl_exit;
1895 }
Steve French4a72daf2013-06-25 00:20:49 -05001896 }
1897
1898 /* check if caller wants to look at return data or just return rc */
1899 if ((plen == NULL) || (out_data == NULL))
1900 goto ioctl_exit;
1901
1902 *plen = le32_to_cpu(rsp->OutputCount);
1903
1904 /* We check for obvious errors in the output buffer length and offset */
1905 if (*plen == 0)
1906 goto ioctl_exit; /* server returned no data */
1907 else if (*plen > 0xFF00) {
1908 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1909 *plen = 0;
1910 rc = -EIO;
1911 goto ioctl_exit;
1912 }
1913
1914 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1915 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1916 le32_to_cpu(rsp->OutputOffset));
1917 *plen = 0;
1918 rc = -EIO;
1919 goto ioctl_exit;
1920 }
1921
1922 *out_data = kmalloc(*plen, GFP_KERNEL);
1923 if (*out_data == NULL) {
1924 rc = -ENOMEM;
1925 goto ioctl_exit;
1926 }
1927
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001928 shdr = get_sync_hdr(rsp);
1929 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05001930ioctl_exit:
1931 free_rsp_buf(resp_buftype, rsp);
1932 return rc;
1933}
1934
Steve French64a5cfa2013-10-14 15:31:32 -05001935/*
1936 * Individual callers to ioctl worker function follow
1937 */
1938
1939int
1940SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1941 u64 persistent_fid, u64 volatile_fid)
1942{
1943 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001944 struct compress_ioctl fsctl_input;
1945 char *ret_data = NULL;
1946
1947 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001948 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001949
1950 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1951 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001952 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05001953 (char *)&fsctl_input /* data input */,
1954 2 /* in data len */, &ret_data /* out data */, NULL);
1955
1956 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001957
1958 return rc;
1959}
1960
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001961int
1962SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1963 u64 persistent_fid, u64 volatile_fid)
1964{
1965 struct smb2_close_req *req;
1966 struct smb2_close_rsp *rsp;
1967 struct TCP_Server_Info *server;
1968 struct cifs_ses *ses = tcon->ses;
1969 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001970 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001971 int resp_buftype;
1972 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001973 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001974
Joe Perchesf96637b2013-05-04 22:12:25 -05001975 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001976
1977 if (ses && (ses->server))
1978 server = ses->server;
1979 else
1980 return -EIO;
1981
1982 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1983 if (rc)
1984 return rc;
1985
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001986 if (encryption_required(tcon))
1987 flags |= CIFS_TRANSFORM_REQ;
1988
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001989 req->PersistentFileId = persistent_fid;
1990 req->VolatileFileId = volatile_fid;
1991
1992 iov[0].iov_base = (char *)req;
1993 /* 4 for rfc1002 length field */
1994 iov[0].iov_len = get_rfc1002_length(req) + 4;
1995
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001996 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001997 cifs_small_buf_release(req);
1998 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001999
2000 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002001 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002002 goto close_exit;
2003 }
2004
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002005 /* BB FIXME - decode close response, update inode for caching */
2006
2007close_exit:
2008 free_rsp_buf(resp_buftype, rsp);
2009 return rc;
2010}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002011
2012static int
2013validate_buf(unsigned int offset, unsigned int buffer_length,
2014 struct smb2_hdr *hdr, unsigned int min_buf_size)
2015
2016{
2017 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2018 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2019 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2020 char *end_of_buf = begin_of_buf + buffer_length;
2021
2022
2023 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002024 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2025 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002026 return -EINVAL;
2027 }
2028
2029 /* check if beyond RFC1001 maximum length */
2030 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002031 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2032 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002033 return -EINVAL;
2034 }
2035
2036 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002037 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002038 return -EINVAL;
2039 }
2040
2041 return 0;
2042}
2043
2044/*
2045 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2046 * Caller must free buffer.
2047 */
2048static int
2049validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2050 struct smb2_hdr *hdr, unsigned int minbufsize,
2051 char *data)
2052
2053{
2054 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2055 int rc;
2056
2057 if (!data)
2058 return -EINVAL;
2059
2060 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2061 if (rc)
2062 return rc;
2063
2064 memcpy(data, begin_of_buf, buffer_length);
2065
2066 return 0;
2067}
2068
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002069static int
2070query_info(const unsigned int xid, struct cifs_tcon *tcon,
2071 u64 persistent_fid, u64 volatile_fid, u8 info_class,
2072 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002073{
2074 struct smb2_query_info_req *req;
2075 struct smb2_query_info_rsp *rsp = NULL;
2076 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002077 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002078 int rc = 0;
2079 int resp_buftype;
2080 struct TCP_Server_Info *server;
2081 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002082 int flags = 0;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002083
Joe Perchesf96637b2013-05-04 22:12:25 -05002084 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002085
2086 if (ses && (ses->server))
2087 server = ses->server;
2088 else
2089 return -EIO;
2090
2091 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2092 if (rc)
2093 return rc;
2094
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002095 if (encryption_required(tcon))
2096 flags |= CIFS_TRANSFORM_REQ;
2097
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002098 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002099 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002100 req->PersistentFileId = persistent_fid;
2101 req->VolatileFileId = volatile_fid;
2102 /* 4 for rfc1002 length field and 1 for Buffer */
2103 req->InputBufferOffset =
2104 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002105 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002106
2107 iov[0].iov_base = (char *)req;
2108 /* 4 for rfc1002 length field */
2109 iov[0].iov_len = get_rfc1002_length(req) + 4;
2110
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002111 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002112 cifs_small_buf_release(req);
2113 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002114
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002115 if (rc) {
2116 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2117 goto qinf_exit;
2118 }
2119
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002120 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2121 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002122 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002123
2124qinf_exit:
2125 free_rsp_buf(resp_buftype, rsp);
2126 return rc;
2127}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002128
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002129int
2130SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2131 u64 persistent_fid, u64 volatile_fid,
2132 struct smb2_file_all_info *data)
2133{
2134 return query_info(xid, tcon, persistent_fid, volatile_fid,
2135 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002136 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002137 sizeof(struct smb2_file_all_info), data);
2138}
2139
2140int
2141SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2142 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2143{
2144 return query_info(xid, tcon, persistent_fid, volatile_fid,
2145 FILE_INTERNAL_INFORMATION,
2146 sizeof(struct smb2_file_internal_info),
2147 sizeof(struct smb2_file_internal_info), uniqueid);
2148}
2149
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002150/*
2151 * This is a no-op for now. We're not really interested in the reply, but
2152 * rather in the fact that the server sent one and that server->lstrp
2153 * gets updated.
2154 *
2155 * FIXME: maybe we should consider checking that the reply matches request?
2156 */
2157static void
2158smb2_echo_callback(struct mid_q_entry *mid)
2159{
2160 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002161 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002162 unsigned int credits_received = 1;
2163
2164 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002165 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002166
Christopher Oo5fb4e282015-06-25 16:10:48 -07002167 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002168 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002169 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002170 add_credits(server, credits_received, CIFS_ECHO_OP);
2171}
2172
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002173void smb2_reconnect_server(struct work_struct *work)
2174{
2175 struct TCP_Server_Info *server = container_of(work,
2176 struct TCP_Server_Info, reconnect.work);
2177 struct cifs_ses *ses;
2178 struct cifs_tcon *tcon, *tcon2;
2179 struct list_head tmp_list;
2180 int tcon_exist = false;
2181
2182 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2183 mutex_lock(&server->reconnect_mutex);
2184
2185 INIT_LIST_HEAD(&tmp_list);
2186 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2187
2188 spin_lock(&cifs_tcp_ses_lock);
2189 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2190 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002191 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002192 tcon->tc_count++;
2193 list_add_tail(&tcon->rlist, &tmp_list);
2194 tcon_exist = true;
2195 }
2196 }
2197 }
2198 /*
2199 * Get the reference to server struct to be sure that the last call of
2200 * cifs_put_tcon() in the loop below won't release the server pointer.
2201 */
2202 if (tcon_exist)
2203 server->srv_count++;
2204
2205 spin_unlock(&cifs_tcp_ses_lock);
2206
2207 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002208 if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon))
2209 cifs_reopen_persistent_handles(tcon);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002210 list_del_init(&tcon->rlist);
2211 cifs_put_tcon(tcon);
2212 }
2213
2214 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2215 mutex_unlock(&server->reconnect_mutex);
2216
2217 /* now we can safely release srv struct */
2218 if (tcon_exist)
2219 cifs_put_tcp_session(server, 1);
2220}
2221
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002222int
2223SMB2_echo(struct TCP_Server_Info *server)
2224{
2225 struct smb2_echo_req *req;
2226 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002227 struct kvec iov[2];
2228 struct smb_rqst rqst = { .rq_iov = iov,
2229 .rq_nvec = 2 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002230
Joe Perchesf96637b2013-05-04 22:12:25 -05002231 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002232
Steve French4fcd1812016-06-22 20:12:05 -05002233 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002234 /* No need to send echo on newly established connections */
2235 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2236 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002237 }
2238
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002239 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2240 if (rc)
2241 return rc;
2242
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002243 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002244
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002245 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002246 iov[0].iov_len = 4;
2247 iov[0].iov_base = (char *)req;
2248 iov[1].iov_len = get_rfc1002_length(req);
2249 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002250
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002251 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2252 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002253 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002254 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002255
2256 cifs_small_buf_release(req);
2257 return rc;
2258}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002259
2260int
2261SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2262 u64 volatile_fid)
2263{
2264 struct smb2_flush_req *req;
2265 struct TCP_Server_Info *server;
2266 struct cifs_ses *ses = tcon->ses;
2267 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002268 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002269 int resp_buftype;
2270 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002271 int flags = 0;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002272
Joe Perchesf96637b2013-05-04 22:12:25 -05002273 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002274
2275 if (ses && (ses->server))
2276 server = ses->server;
2277 else
2278 return -EIO;
2279
2280 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2281 if (rc)
2282 return rc;
2283
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002284 if (encryption_required(tcon))
2285 flags |= CIFS_TRANSFORM_REQ;
2286
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002287 req->PersistentFileId = persistent_fid;
2288 req->VolatileFileId = volatile_fid;
2289
2290 iov[0].iov_base = (char *)req;
2291 /* 4 for rfc1002 length field */
2292 iov[0].iov_len = get_rfc1002_length(req) + 4;
2293
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002294 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002295 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002296
Steve Frenchdfebe402015-03-27 01:00:06 -05002297 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002298 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2299
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002300 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002301 return rc;
2302}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002303
2304/*
2305 * To form a chain of read requests, any read requests after the first should
2306 * have the end_of_chain boolean set to true.
2307 */
2308static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002309smb2_new_read_req(void **buf, unsigned int *total_len,
2310 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2311 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002312{
2313 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002314 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002315 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002316
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002317 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2318 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002319 if (rc)
2320 return rc;
2321 if (io_parms->tcon->ses->server == NULL)
2322 return -ECONNABORTED;
2323
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002324 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002325 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002326
2327 req->PersistentFileId = io_parms->persistent_fid;
2328 req->VolatileFileId = io_parms->volatile_fid;
2329 req->ReadChannelInfoOffset = 0; /* reserved */
2330 req->ReadChannelInfoLength = 0; /* reserved */
2331 req->Channel = 0; /* reserved */
2332 req->MinimumCount = 0;
2333 req->Length = cpu_to_le32(io_parms->length);
2334 req->Offset = cpu_to_le64(io_parms->offset);
2335
2336 if (request_type & CHAINED_REQUEST) {
2337 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002338 /* next 8-byte aligned request */
2339 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2340 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002341 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002342 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002343 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002344 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002345 /*
2346 * Related requests use info from previous read request
2347 * in chain.
2348 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002349 shdr->SessionId = 0xFFFFFFFF;
2350 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002351 req->PersistentFileId = 0xFFFFFFFF;
2352 req->VolatileFileId = 0xFFFFFFFF;
2353 }
2354 }
2355 if (remaining_bytes > io_parms->length)
2356 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2357 else
2358 req->RemainingBytes = 0;
2359
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002360 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002361 return rc;
2362}
2363
2364static void
2365smb2_readv_callback(struct mid_q_entry *mid)
2366{
2367 struct cifs_readdata *rdata = mid->callback_data;
2368 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2369 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002370 struct smb2_sync_hdr *shdr =
2371 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002372 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002373 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2374 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002375 .rq_pages = rdata->pages,
2376 .rq_npages = rdata->nr_pages,
2377 .rq_pagesz = rdata->pagesz,
2378 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002379
Joe Perchesf96637b2013-05-04 22:12:25 -05002380 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2381 __func__, mid->mid, mid->mid_state, rdata->result,
2382 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002383
2384 switch (mid->mid_state) {
2385 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002386 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002387 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002388 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002389 int rc;
2390
Jeff Layton0b688cf2012-09-18 16:20:34 -07002391 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002392 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002393 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2394 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002395 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002396 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002397 task_io_account_read(rdata->got_bytes);
2398 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002399 break;
2400 case MID_REQUEST_SUBMITTED:
2401 case MID_RETRY_NEEDED:
2402 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002403 if (server->sign && rdata->got_bytes)
2404 /* reset bytes number since we can not check a sign */
2405 rdata->got_bytes = 0;
2406 /* FIXME: should this be counted toward the initiating task? */
2407 task_io_account_read(rdata->got_bytes);
2408 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002409 break;
2410 default:
2411 if (rdata->result != -ENODATA)
2412 rdata->result = -EIO;
2413 }
2414
2415 if (rdata->result)
2416 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2417
2418 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002419 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002420 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002421 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002422 add_credits(server, credits_received, 0);
2423}
2424
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002425/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002426int
2427smb2_async_readv(struct cifs_readdata *rdata)
2428{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002429 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002430 char *buf;
2431 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002432 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002433 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2434 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002435 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002436 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002437 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002438
Joe Perchesf96637b2013-05-04 22:12:25 -05002439 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2440 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002441
2442 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2443 io_parms.offset = rdata->offset;
2444 io_parms.length = rdata->bytes;
2445 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2446 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2447 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002448
2449 server = io_parms.tcon->ses->server;
2450
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002451 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002452 if (rc) {
2453 if (rc == -EAGAIN && rdata->credits) {
2454 /* credits was reset by reconnect */
2455 rdata->credits = 0;
2456 /* reduce in_flight value since we won't send the req */
2457 spin_lock(&server->req_lock);
2458 server->in_flight--;
2459 spin_unlock(&server->req_lock);
2460 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002461 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002462 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002463
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002464 if (encryption_required(io_parms.tcon))
2465 flags |= CIFS_TRANSFORM_REQ;
2466
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002467 req_len = cpu_to_be32(total_len);
2468
2469 rdata->iov[0].iov_base = &req_len;
2470 rdata->iov[0].iov_len = sizeof(__be32);
2471 rdata->iov[1].iov_base = buf;
2472 rdata->iov[1].iov_len = total_len;
2473
2474 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002475
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002476 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002477 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002478 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002479 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002480 spin_lock(&server->req_lock);
2481 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002482 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002483 spin_unlock(&server->req_lock);
2484 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002485 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002486 }
2487
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002488 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002489 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002490 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002491 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002492 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002493 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002494 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2495 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002496
2497 cifs_small_buf_release(buf);
2498 return rc;
2499}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002500
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002501int
2502SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2503 unsigned int *nbytes, char **buf, int *buf_type)
2504{
2505 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002506 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002507 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002508 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002509 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002510 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002511 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002512 __be32 req_len;
2513 struct smb_rqst rqst = { .rq_iov = iov,
2514 .rq_nvec = 2 };
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002515 int flags = CIFS_LOG_ERROR;
2516 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002517
2518 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002519 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002520 if (rc)
2521 return rc;
2522
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002523 if (encryption_required(io_parms->tcon))
2524 flags |= CIFS_TRANSFORM_REQ;
2525
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002526 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002527
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002528 iov[0].iov_base = &req_len;
2529 iov[0].iov_len = sizeof(__be32);
2530 iov[1].iov_base = req;
2531 iov[1].iov_len = total_len;
2532
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002533 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002534 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002535
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002536 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002537 shdr = get_sync_hdr(rsp);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002538
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002539 if (shdr->Status == STATUS_END_OF_FILE) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002540 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002541 return 0;
2542 }
2543
2544 if (rc) {
2545 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002546 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002547 } else {
2548 *nbytes = le32_to_cpu(rsp->DataLength);
2549 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2550 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002551 cifs_dbg(FYI, "bad length %d for count %d\n",
2552 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002553 rc = -EIO;
2554 *nbytes = 0;
2555 }
2556 }
2557
2558 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002559 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002560 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002561 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002562 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002563 if (resp_buftype == CIFS_SMALL_BUFFER)
2564 *buf_type = CIFS_SMALL_BUFFER;
2565 else if (resp_buftype == CIFS_LARGE_BUFFER)
2566 *buf_type = CIFS_LARGE_BUFFER;
2567 }
2568 return rc;
2569}
2570
Pavel Shilovsky33319142012-09-18 16:20:29 -07002571/*
2572 * Check the mid_state and signature on received buffer (if any), and queue the
2573 * workqueue completion task.
2574 */
2575static void
2576smb2_writev_callback(struct mid_q_entry *mid)
2577{
2578 struct cifs_writedata *wdata = mid->callback_data;
2579 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002580 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002581 unsigned int written;
2582 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2583 unsigned int credits_received = 1;
2584
2585 switch (mid->mid_state) {
2586 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002587 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002588 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2589 if (wdata->result != 0)
2590 break;
2591
2592 written = le32_to_cpu(rsp->DataLength);
2593 /*
2594 * Mask off high 16 bits when bytes written as returned
2595 * by the server is greater than bytes requested by the
2596 * client. OS/2 servers are known to set incorrect
2597 * CountHigh values.
2598 */
2599 if (written > wdata->bytes)
2600 written &= 0xFFFF;
2601
2602 if (written < wdata->bytes)
2603 wdata->result = -ENOSPC;
2604 else
2605 wdata->bytes = written;
2606 break;
2607 case MID_REQUEST_SUBMITTED:
2608 case MID_RETRY_NEEDED:
2609 wdata->result = -EAGAIN;
2610 break;
2611 default:
2612 wdata->result = -EIO;
2613 break;
2614 }
2615
2616 if (wdata->result)
2617 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2618
2619 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002620 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002621 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002622 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002623 add_credits(tcon->ses->server, credits_received, 0);
2624}
2625
2626/* smb2_async_writev - send an async write, and set up mid to handle result */
2627int
Steve French4a5c80d2014-02-07 20:45:12 -06002628smb2_async_writev(struct cifs_writedata *wdata,
2629 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002630{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002631 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002632 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002633 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002634 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002635 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002636 struct kvec iov[2];
2637 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002638
2639 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002640 if (rc) {
2641 if (rc == -EAGAIN && wdata->credits) {
2642 /* credits was reset by reconnect */
2643 wdata->credits = 0;
2644 /* reduce in_flight value since we won't send the req */
2645 spin_lock(&server->req_lock);
2646 server->in_flight--;
2647 spin_unlock(&server->req_lock);
2648 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002649 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002650 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002651
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002652 if (encryption_required(tcon))
2653 flags |= CIFS_TRANSFORM_REQ;
2654
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002655 shdr = get_sync_hdr(req);
2656 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002657
2658 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2659 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2660 req->WriteChannelInfoOffset = 0;
2661 req->WriteChannelInfoLength = 0;
2662 req->Channel = 0;
2663 req->Offset = cpu_to_le64(wdata->offset);
2664 /* 4 for rfc1002 length field */
2665 req->DataOffset = cpu_to_le16(
2666 offsetof(struct smb2_write_req, Buffer) - 4);
2667 req->RemainingBytes = 0;
2668
2669 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002670 iov[0].iov_len = 4;
2671 iov[0].iov_base = req;
2672 iov[1].iov_len = get_rfc1002_length(req) - 1;
2673 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002674
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002675 rqst.rq_iov = iov;
2676 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002677 rqst.rq_pages = wdata->pages;
2678 rqst.rq_npages = wdata->nr_pages;
2679 rqst.rq_pagesz = wdata->pagesz;
2680 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002681
Joe Perchesf96637b2013-05-04 22:12:25 -05002682 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2683 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002684
2685 req->Length = cpu_to_le32(wdata->bytes);
2686
2687 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2688
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002689 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002690 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002691 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002692 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002693 spin_lock(&server->req_lock);
2694 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002695 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002696 spin_unlock(&server->req_lock);
2697 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002698 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002699 }
2700
Pavel Shilovsky33319142012-09-18 16:20:29 -07002701 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002702 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2703 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002704
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002705 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002706 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002707 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2708 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002709
Pavel Shilovsky33319142012-09-18 16:20:29 -07002710async_writev_out:
2711 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002712 return rc;
2713}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002714
2715/*
2716 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2717 * The length field from io_parms must be at least 1 and indicates a number of
2718 * elements with data to write that begins with position 1 in iov array. All
2719 * data length is specified by count.
2720 */
2721int
2722SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2723 unsigned int *nbytes, struct kvec *iov, int n_vec)
2724{
2725 int rc = 0;
2726 struct smb2_write_req *req = NULL;
2727 struct smb2_write_rsp *rsp = NULL;
2728 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002729 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002730 int flags = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002731
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002732 *nbytes = 0;
2733
2734 if (n_vec < 1)
2735 return rc;
2736
2737 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2738 if (rc)
2739 return rc;
2740
2741 if (io_parms->tcon->ses->server == NULL)
2742 return -ECONNABORTED;
2743
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002744 if (encryption_required(io_parms->tcon))
2745 flags |= CIFS_TRANSFORM_REQ;
2746
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002747 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002748
2749 req->PersistentFileId = io_parms->persistent_fid;
2750 req->VolatileFileId = io_parms->volatile_fid;
2751 req->WriteChannelInfoOffset = 0;
2752 req->WriteChannelInfoLength = 0;
2753 req->Channel = 0;
2754 req->Length = cpu_to_le32(io_parms->length);
2755 req->Offset = cpu_to_le64(io_parms->offset);
2756 /* 4 for rfc1002 length field */
2757 req->DataOffset = cpu_to_le16(
2758 offsetof(struct smb2_write_req, Buffer) - 4);
2759 req->RemainingBytes = 0;
2760
2761 iov[0].iov_base = (char *)req;
2762 /* 4 for rfc1002 length field and 1 for Buffer */
2763 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2764
2765 /* length of entire message including data to be written */
2766 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2767
2768 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002769 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002770 cifs_small_buf_release(req);
2771 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002772
2773 if (rc) {
2774 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002775 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002776 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002777 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002778
2779 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002780 return rc;
2781}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002782
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002783static unsigned int
2784num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2785{
2786 int len;
2787 unsigned int entrycount = 0;
2788 unsigned int next_offset = 0;
2789 FILE_DIRECTORY_INFO *entryptr;
2790
2791 if (bufstart == NULL)
2792 return 0;
2793
2794 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2795
2796 while (1) {
2797 entryptr = (FILE_DIRECTORY_INFO *)
2798 ((char *)entryptr + next_offset);
2799
2800 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002801 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002802 break;
2803 }
2804
2805 len = le32_to_cpu(entryptr->FileNameLength);
2806 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002807 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2808 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002809 break;
2810 }
2811
2812 *lastentry = (char *)entryptr;
2813 entrycount++;
2814
2815 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2816 if (!next_offset)
2817 break;
2818 }
2819
2820 return entrycount;
2821}
2822
2823/*
2824 * Readdir/FindFirst
2825 */
2826int
2827SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2828 u64 persistent_fid, u64 volatile_fid, int index,
2829 struct cifs_search_info *srch_inf)
2830{
2831 struct smb2_query_directory_req *req;
2832 struct smb2_query_directory_rsp *rsp = NULL;
2833 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002834 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002835 int rc = 0;
2836 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002837 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002838 unsigned char *bufptr;
2839 struct TCP_Server_Info *server;
2840 struct cifs_ses *ses = tcon->ses;
2841 __le16 asteriks = cpu_to_le16('*');
2842 char *end_of_smb;
2843 unsigned int output_size = CIFSMaxBufSize;
2844 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002845 int flags = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002846
2847 if (ses && (ses->server))
2848 server = ses->server;
2849 else
2850 return -EIO;
2851
2852 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2853 if (rc)
2854 return rc;
2855
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002856 if (encryption_required(tcon))
2857 flags |= CIFS_TRANSFORM_REQ;
2858
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002859 switch (srch_inf->info_level) {
2860 case SMB_FIND_FILE_DIRECTORY_INFO:
2861 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2862 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2863 break;
2864 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2865 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2866 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2867 break;
2868 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002869 cifs_dbg(VFS, "info level %u isn't supported\n",
2870 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002871 rc = -EINVAL;
2872 goto qdir_exit;
2873 }
2874
2875 req->FileIndex = cpu_to_le32(index);
2876 req->PersistentFileId = persistent_fid;
2877 req->VolatileFileId = volatile_fid;
2878
2879 len = 0x2;
2880 bufptr = req->Buffer;
2881 memcpy(bufptr, &asteriks, len);
2882
2883 req->FileNameOffset =
2884 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2885 req->FileNameLength = cpu_to_le16(len);
2886 /*
2887 * BB could be 30 bytes or so longer if we used SMB2 specific
2888 * buffer lengths, but this is safe and close enough.
2889 */
2890 output_size = min_t(unsigned int, output_size, server->maxBuf);
2891 output_size = min_t(unsigned int, output_size, 2 << 15);
2892 req->OutputBufferLength = cpu_to_le32(output_size);
2893
2894 iov[0].iov_base = (char *)req;
2895 /* 4 for RFC1001 length and 1 for Buffer */
2896 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2897
2898 iov[1].iov_base = (char *)(req->Buffer);
2899 iov[1].iov_len = len;
2900
2901 inc_rfc1001_len(req, len - 1 /* Buffer */);
2902
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002903 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002904 cifs_small_buf_release(req);
2905 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002906
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002907 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002908 if (rc == -ENODATA &&
2909 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002910 srch_inf->endOfSearch = true;
2911 rc = 0;
2912 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002913 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2914 goto qdir_exit;
2915 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002916
2917 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2918 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2919 info_buf_size);
2920 if (rc)
2921 goto qdir_exit;
2922
2923 srch_inf->unicode = true;
2924
2925 if (srch_inf->ntwrk_buf_start) {
2926 if (srch_inf->smallBuf)
2927 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2928 else
2929 cifs_buf_release(srch_inf->ntwrk_buf_start);
2930 }
2931 srch_inf->ntwrk_buf_start = (char *)rsp;
2932 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2933 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2934 /* 4 for rfc1002 length field */
2935 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2936 srch_inf->entries_in_buffer =
2937 num_entries(srch_inf->srch_entries_start, end_of_smb,
2938 &srch_inf->last_entry, info_buf_size);
2939 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002940 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2941 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2942 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002943 if (resp_buftype == CIFS_LARGE_BUFFER)
2944 srch_inf->smallBuf = false;
2945 else if (resp_buftype == CIFS_SMALL_BUFFER)
2946 srch_inf->smallBuf = true;
2947 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002948 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002949
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002950 return rc;
2951
2952qdir_exit:
2953 free_rsp_buf(resp_buftype, rsp);
2954 return rc;
2955}
2956
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002957static int
2958send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002959 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002960 unsigned int num, void **data, unsigned int *size)
2961{
2962 struct smb2_set_info_req *req;
2963 struct smb2_set_info_rsp *rsp = NULL;
2964 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002965 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002966 int rc = 0;
2967 int resp_buftype;
2968 unsigned int i;
2969 struct TCP_Server_Info *server;
2970 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002971 int flags = 0;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002972
2973 if (ses && (ses->server))
2974 server = ses->server;
2975 else
2976 return -EIO;
2977
2978 if (!num)
2979 return -EINVAL;
2980
2981 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2982 if (!iov)
2983 return -ENOMEM;
2984
2985 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2986 if (rc) {
2987 kfree(iov);
2988 return rc;
2989 }
2990
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002991 if (encryption_required(tcon))
2992 flags |= CIFS_TRANSFORM_REQ;
2993
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002994 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002995
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002996 req->InfoType = SMB2_O_INFO_FILE;
2997 req->FileInfoClass = info_class;
2998 req->PersistentFileId = persistent_fid;
2999 req->VolatileFileId = volatile_fid;
3000
3001 /* 4 for RFC1001 length and 1 for Buffer */
3002 req->BufferOffset =
3003 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3004 req->BufferLength = cpu_to_le32(*size);
3005
3006 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3007
3008 memcpy(req->Buffer, *data, *size);
3009
3010 iov[0].iov_base = (char *)req;
3011 /* 4 for RFC1001 length */
3012 iov[0].iov_len = get_rfc1002_length(req) + 4;
3013
3014 for (i = 1; i < num; i++) {
3015 inc_rfc1001_len(req, size[i]);
3016 le32_add_cpu(&req->BufferLength, size[i]);
3017 iov[i].iov_base = (char *)data[i];
3018 iov[i].iov_len = size[i];
3019 }
3020
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003021 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003022 cifs_small_buf_release(req);
3023 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003024
Steve French7d3fb242013-11-18 09:56:28 -06003025 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003026 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003027
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003028 free_rsp_buf(resp_buftype, rsp);
3029 kfree(iov);
3030 return rc;
3031}
3032
3033int
3034SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3035 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3036{
3037 struct smb2_file_rename_info info;
3038 void **data;
3039 unsigned int size[2];
3040 int rc;
3041 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3042
3043 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3044 if (!data)
3045 return -ENOMEM;
3046
3047 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3048 /* 0 = fail if target already exists */
3049 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3050 info.FileNameLength = cpu_to_le32(len);
3051
3052 data[0] = &info;
3053 size[0] = sizeof(struct smb2_file_rename_info);
3054
3055 data[1] = target_file;
3056 size[1] = len + 2 /* null */;
3057
3058 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003059 current->tgid, FILE_RENAME_INFORMATION, 2, data,
3060 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003061 kfree(data);
3062 return rc;
3063}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003064
3065int
Steve French897fba12016-05-12 21:20:36 -05003066SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3067 u64 persistent_fid, u64 volatile_fid)
3068{
3069 __u8 delete_pending = 1;
3070 void *data;
3071 unsigned int size;
3072
3073 data = &delete_pending;
3074 size = 1; /* sizeof __u8 */
3075
3076 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3077 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
3078 &size);
3079}
3080
3081int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003082SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3083 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3084{
3085 struct smb2_file_link_info info;
3086 void **data;
3087 unsigned int size[2];
3088 int rc;
3089 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3090
3091 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3092 if (!data)
3093 return -ENOMEM;
3094
3095 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3096 /* 0 = fail if link already exists */
3097 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3098 info.FileNameLength = cpu_to_le32(len);
3099
3100 data[0] = &info;
3101 size[0] = sizeof(struct smb2_file_link_info);
3102
3103 data[1] = target_file;
3104 size[1] = len + 2 /* null */;
3105
3106 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003107 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003108 kfree(data);
3109 return rc;
3110}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003111
3112int
3113SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003114 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003115{
3116 struct smb2_file_eof_info info;
3117 void *data;
3118 unsigned int size;
3119
3120 info.EndOfFile = *eof;
3121
3122 data = &info;
3123 size = sizeof(struct smb2_file_eof_info);
3124
Steve Frenchf29ebb42014-07-19 21:44:58 -05003125 if (is_falloc)
3126 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3127 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
3128 else
3129 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3130 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003131}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003132
3133int
3134SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3135 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3136{
3137 unsigned int size;
3138 size = sizeof(FILE_BASIC_INFO);
3139 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3140 current->tgid, FILE_BASIC_INFORMATION, 1,
3141 (void **)&buf, &size);
3142}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003143
3144int
3145SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3146 const u64 persistent_fid, const u64 volatile_fid,
3147 __u8 oplock_level)
3148{
3149 int rc;
3150 struct smb2_oplock_break *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003151 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003152
Joe Perchesf96637b2013-05-04 22:12:25 -05003153 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003154 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003155 if (rc)
3156 return rc;
3157
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003158 if (encryption_required(tcon))
3159 flags |= CIFS_TRANSFORM_REQ;
3160
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003161 req->VolatileFid = volatile_fid;
3162 req->PersistentFid = persistent_fid;
3163 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003164 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003165
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003166 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003167 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003168
3169 if (rc) {
3170 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003171 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003172 }
3173
3174 return rc;
3175}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003176
3177static void
3178copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3179 struct kstatfs *kst)
3180{
3181 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3182 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3183 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3184 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
3185 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3186 return;
3187}
3188
3189static int
3190build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3191 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3192{
3193 int rc;
3194 struct smb2_query_info_req *req;
3195
Joe Perchesf96637b2013-05-04 22:12:25 -05003196 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003197
3198 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3199 return -EIO;
3200
3201 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3202 if (rc)
3203 return rc;
3204
3205 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3206 req->FileInfoClass = level;
3207 req->PersistentFileId = persistent_fid;
3208 req->VolatileFileId = volatile_fid;
3209 /* 4 for rfc1002 length field and 1 for pad */
3210 req->InputBufferOffset =
3211 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3212 req->OutputBufferLength = cpu_to_le32(
3213 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3214
3215 iov->iov_base = (char *)req;
3216 /* 4 for rfc1002 length field */
3217 iov->iov_len = get_rfc1002_length(req) + 4;
3218 return 0;
3219}
3220
3221int
3222SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3223 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3224{
3225 struct smb2_query_info_rsp *rsp = NULL;
3226 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003227 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003228 int rc = 0;
3229 int resp_buftype;
3230 struct cifs_ses *ses = tcon->ses;
3231 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003232 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003233
3234 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3235 sizeof(struct smb2_fs_full_size_info),
3236 persistent_fid, volatile_fid);
3237 if (rc)
3238 return rc;
3239
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003240 if (encryption_required(tcon))
3241 flags |= CIFS_TRANSFORM_REQ;
3242
3243 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003244 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003245 if (rc) {
3246 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003247 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003248 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003249 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003250
3251 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3252 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3253 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3254 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3255 sizeof(struct smb2_fs_full_size_info));
3256 if (!rc)
3257 copy_fs_info_to_kstatfs(info, fsdata);
3258
Steve French34f62642013-10-09 02:07:00 -05003259qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003260 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003261 return rc;
3262}
3263
3264int
3265SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003266 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003267{
3268 struct smb2_query_info_rsp *rsp = NULL;
3269 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003270 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003271 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003272 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003273 struct cifs_ses *ses = tcon->ses;
3274 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003275 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003276
Steven French21671142013-10-09 13:36:35 -05003277 if (level == FS_DEVICE_INFORMATION) {
3278 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3279 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3280 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3281 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3282 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003283 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3284 max_len = sizeof(struct smb3_fs_ss_info);
3285 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003286 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003287 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003288 return -EINVAL;
3289 }
3290
3291 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003292 persistent_fid, volatile_fid);
3293 if (rc)
3294 return rc;
3295
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003296 if (encryption_required(tcon))
3297 flags |= CIFS_TRANSFORM_REQ;
3298
3299 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003300 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003301 if (rc) {
3302 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3303 goto qfsattr_exit;
3304 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003305 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003306
3307 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3308 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003309 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3310 if (rc)
3311 goto qfsattr_exit;
3312
3313 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003314 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3315 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003316 rsp_len, max_len));
3317 else if (level == FS_DEVICE_INFORMATION)
3318 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3319 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003320 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3321 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3322 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3323 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3324 tcon->perf_sector_size =
3325 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3326 }
Steve French34f62642013-10-09 02:07:00 -05003327
3328qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003329 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003330 return rc;
3331}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003332
3333int
3334smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3335 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3336 const __u32 num_lock, struct smb2_lock_element *buf)
3337{
3338 int rc = 0;
3339 struct smb2_lock_req *req = NULL;
3340 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003341 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003342 int resp_buf_type;
3343 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003344 int flags = CIFS_NO_RESP;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003345
Joe Perchesf96637b2013-05-04 22:12:25 -05003346 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003347
3348 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3349 if (rc)
3350 return rc;
3351
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003352 if (encryption_required(tcon))
3353 flags |= CIFS_TRANSFORM_REQ;
3354
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003355 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003356 req->LockCount = cpu_to_le16(num_lock);
3357
3358 req->PersistentFileId = persist_fid;
3359 req->VolatileFileId = volatile_fid;
3360
3361 count = num_lock * sizeof(struct smb2_lock_element);
3362 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3363
3364 iov[0].iov_base = (char *)req;
3365 /* 4 for rfc1002 length field and count for all locks */
3366 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3367 iov[1].iov_base = (char *)buf;
3368 iov[1].iov_len = count;
3369
3370 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003371 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003372 &rsp_iov);
3373 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003374 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003375 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003376 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3377 }
3378
3379 return rc;
3380}
3381
3382int
3383SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3384 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3385 const __u64 length, const __u64 offset, const __u32 lock_flags,
3386 const bool wait)
3387{
3388 struct smb2_lock_element lock;
3389
3390 lock.Offset = cpu_to_le64(offset);
3391 lock.Length = cpu_to_le64(length);
3392 lock.Flags = cpu_to_le32(lock_flags);
3393 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3394 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3395
3396 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3397}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003398
3399int
3400SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3401 __u8 *lease_key, const __le32 lease_state)
3402{
3403 int rc;
3404 struct smb2_lease_ack *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003405 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003406
Joe Perchesf96637b2013-05-04 22:12:25 -05003407 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003408 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003409 if (rc)
3410 return rc;
3411
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003412 if (encryption_required(tcon))
3413 flags |= CIFS_TRANSFORM_REQ;
3414
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003415 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003416 req->StructureSize = cpu_to_le16(36);
3417 inc_rfc1001_len(req, 12);
3418
3419 memcpy(req->LeaseKey, lease_key, 16);
3420 req->LeaseState = lease_state;
3421
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003422 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003423 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003424
3425 if (rc) {
3426 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003427 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003428 }
3429
3430 return rc;
3431}