blob: 0fd63f0bc4408e77b8b81fa9bd46c3fa8dcc752e [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>
Pavel Shilovsky33319142012-09-18 16:20:29 -070036#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040037#include <linux/xattr.h>
38#include "smb2pdu.h"
39#include "cifsglob.h"
40#include "cifsacl.h"
41#include "cifsproto.h"
42#include "smb2proto.h"
43#include "cifs_unicode.h"
44#include "cifs_debug.h"
45#include "ntlmssp.h"
46#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070047#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070048#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050049#include "cifs_spnego.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040050
51/*
52 * The following table defines the expected "StructureSize" of SMB2 requests
53 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
54 *
55 * Note that commands are defined in smb2pdu.h in le16 but the array below is
56 * indexed by command in host byte order.
57 */
58static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
59 /* SMB2_NEGOTIATE */ 36,
60 /* SMB2_SESSION_SETUP */ 25,
61 /* SMB2_LOGOFF */ 4,
62 /* SMB2_TREE_CONNECT */ 9,
63 /* SMB2_TREE_DISCONNECT */ 4,
64 /* SMB2_CREATE */ 57,
65 /* SMB2_CLOSE */ 24,
66 /* SMB2_FLUSH */ 24,
67 /* SMB2_READ */ 49,
68 /* SMB2_WRITE */ 49,
69 /* SMB2_LOCK */ 48,
70 /* SMB2_IOCTL */ 57,
71 /* SMB2_CANCEL */ 4,
72 /* SMB2_ECHO */ 4,
73 /* SMB2_QUERY_DIRECTORY */ 33,
74 /* SMB2_CHANGE_NOTIFY */ 32,
75 /* SMB2_QUERY_INFO */ 41,
76 /* SMB2_SET_INFO */ 33,
77 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
78};
79
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070080static int encryption_required(const struct cifs_tcon *tcon)
81{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080082 if (!tcon)
83 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070084 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
85 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
86 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080087 if (tcon->seal &&
88 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
89 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070090 return 0;
91}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040092
93static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070094smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040095 const struct cifs_tcon *tcon)
96{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070097 shdr->ProtocolId = SMB2_PROTO_NUMBER;
98 shdr->StructureSize = cpu_to_le16(64);
99 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100100 if (tcon && tcon->ses && tcon->ses->server) {
101 struct TCP_Server_Info *server = tcon->ses->server;
102
103 spin_lock(&server->req_lock);
104 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500105 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700106 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100107 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700108 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500109 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100110 server->credits, 2));
111 spin_unlock(&server->req_lock);
112 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700113 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100114 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700115 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400116
117 if (!tcon)
118 goto out;
119
Steve French2b80d042013-06-23 18:43:37 -0500120 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
121 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500122 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500123 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700124 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500125 /* else CreditCharge MBZ */
126
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700127 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400128 /* Uid is not converted */
129 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700130 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500131
132 /*
133 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
134 * to pass the path on the Open SMB prefixed by \\server\share.
135 * Not sure when we would need to do the augmented path (if ever) and
136 * setting this flag breaks the SMB2 open operation since it is
137 * illegal to send an empty path name (without \\server\share prefix)
138 * when the DFS flag is set in the SMB open header. We could
139 * consider setting the flag on all operations other than open
140 * but it is safer to net set it for now.
141 */
142/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700143 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500144
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700145 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
146 !encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700147 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400148out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400149 return;
150}
151
152static int
153smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
154{
155 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400156 struct nls_table *nls_codepage;
157 struct cifs_ses *ses;
158 struct TCP_Server_Info *server;
159
160 /*
161 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
162 * check for tcp and smb session status done differently
163 * for those three - in the calling routine.
164 */
165 if (tcon == NULL)
166 return rc;
167
168 if (smb2_command == SMB2_TREE_CONNECT)
169 return rc;
170
171 if (tcon->tidStatus == CifsExiting) {
172 /*
173 * only tree disconnect, open, and write,
174 * (and ulogoff which does not have tcon)
175 * are allowed as we start force umount.
176 */
177 if ((smb2_command != SMB2_WRITE) &&
178 (smb2_command != SMB2_CREATE) &&
179 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500180 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
181 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400182 return -ENODEV;
183 }
184 }
185 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
186 (!tcon->ses->server))
187 return -EIO;
188
189 ses = tcon->ses;
190 server = ses->server;
191
192 /*
193 * Give demultiplex thread up to 10 seconds to reconnect, should be
194 * greater than cifs socket timeout which is 7 seconds
195 */
196 while (server->tcpStatus == CifsNeedReconnect) {
197 /*
198 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
199 * here since they are implicitly done when session drops.
200 */
201 switch (smb2_command) {
202 /*
203 * BB Should we keep oplock break and add flush to exceptions?
204 */
205 case SMB2_TREE_DISCONNECT:
206 case SMB2_CANCEL:
207 case SMB2_CLOSE:
208 case SMB2_OPLOCK_BREAK:
209 return -EAGAIN;
210 }
211
212 wait_event_interruptible_timeout(server->response_q,
213 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
214
215 /* are we still trying to reconnect? */
216 if (server->tcpStatus != CifsNeedReconnect)
217 break;
218
219 /*
220 * on "soft" mounts we wait once. Hard mounts keep
221 * retrying until process is killed or server comes
222 * back on-line
223 */
224 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500225 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400226 return -EHOSTDOWN;
227 }
228 }
229
230 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
231 return rc;
232
233 nls_codepage = load_nls_default();
234
235 /*
236 * need to prevent multiple threads trying to simultaneously reconnect
237 * the same SMB session
238 */
239 mutex_lock(&tcon->ses->session_mutex);
240 rc = cifs_negotiate_protocol(0, tcon->ses);
241 if (!rc && tcon->ses->need_reconnect)
242 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
243
244 if (rc || !tcon->need_reconnect) {
245 mutex_unlock(&tcon->ses->session_mutex);
246 goto out;
247 }
248
249 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800250 if (tcon->use_persistent)
251 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500252
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400253 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
254 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500255
Joe Perchesf96637b2013-05-04 22:12:25 -0500256 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400257 if (rc)
258 goto out;
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800259
260 if (smb2_command != SMB2_INTERNAL_CMD)
261 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
262
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400263 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400264out:
265 /*
266 * Check if handle based operation so we know whether we can continue
267 * or not without returning to caller to reset file handle.
268 */
269 /*
270 * BB Is flush done by server on drop of tcp session? Should we special
271 * case it and skip above?
272 */
273 switch (smb2_command) {
274 case SMB2_FLUSH:
275 case SMB2_READ:
276 case SMB2_WRITE:
277 case SMB2_LOCK:
278 case SMB2_IOCTL:
279 case SMB2_QUERY_DIRECTORY:
280 case SMB2_CHANGE_NOTIFY:
281 case SMB2_QUERY_INFO:
282 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800283 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400284 }
285 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400286 return rc;
287}
288
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700289static void
290fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
291 unsigned int *total_len)
292{
293 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
294 /* lookup word count ie StructureSize from table */
295 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
296
297 /*
298 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
299 * largest operations (Create)
300 */
301 memset(buf, 0, 256);
302
303 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
304 spdu->StructureSize2 = cpu_to_le16(parmsize);
305
306 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
307}
308
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800309/* init request without RFC1001 length at the beginning */
310static int
311smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
312 void **request_buf, unsigned int *total_len)
313{
314 int rc;
315 struct smb2_sync_hdr *shdr;
316
317 rc = smb2_reconnect(smb2_command, tcon);
318 if (rc)
319 return rc;
320
321 /* BB eventually switch this to SMB2 specific small buf size */
322 *request_buf = cifs_small_buf_get();
323 if (*request_buf == NULL) {
324 /* BB should we add a retry in here if not a writepage? */
325 return -ENOMEM;
326 }
327
328 shdr = (struct smb2_sync_hdr *)(*request_buf);
329
330 fill_small_buf(smb2_command, tcon, shdr, total_len);
331
332 if (tcon != NULL) {
333#ifdef CONFIG_CIFS_STATS2
334 uint16_t com_code = le16_to_cpu(smb2_command);
335
336 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
337#endif
338 cifs_stats_inc(&tcon->num_smbs_sent);
339 }
340
341 return rc;
342}
343
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400344/*
345 * Allocate and return pointer to an SMB request hdr, and set basic
346 * SMB information in the SMB header. If the return code is zero, this
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800347 * function must have filled in request_buf pointer. The returned buffer
348 * has RFC1001 length at the beginning.
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400349 */
350static int
351small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
352 void **request_buf)
353{
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700354 int rc;
355 unsigned int total_len;
356 struct smb2_pdu *pdu;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400357
358 rc = smb2_reconnect(smb2_command, tcon);
359 if (rc)
360 return rc;
361
362 /* BB eventually switch this to SMB2 specific small buf size */
363 *request_buf = cifs_small_buf_get();
364 if (*request_buf == NULL) {
365 /* BB should we add a retry in here if not a writepage? */
366 return -ENOMEM;
367 }
368
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700369 pdu = (struct smb2_pdu *)(*request_buf);
370
371 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
372
373 /* Note this is only network field converted to big endian */
374 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400375
376 if (tcon != NULL) {
377#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400378 uint16_t com_code = le16_to_cpu(smb2_command);
379 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400380#endif
381 cifs_stats_inc(&tcon->num_smbs_sent);
382 }
383
384 return rc;
385}
386
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500387#ifdef CONFIG_CIFS_SMB311
388/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
389#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
390
391
392#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
393#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
394
395static void
396build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
397{
398 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
399 pneg_ctxt->DataLength = cpu_to_le16(38);
400 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
401 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
402 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
403 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
404}
405
406static void
407build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
408{
409 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
410 pneg_ctxt->DataLength = cpu_to_le16(6);
411 pneg_ctxt->CipherCount = cpu_to_le16(2);
412 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
413 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
414}
415
416static void
417assemble_neg_contexts(struct smb2_negotiate_req *req)
418{
419
420 /* +4 is to account for the RFC1001 len field */
421 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
422
423 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
424 /* Add 2 to size to round to 8 byte boundary */
425 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
426 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
427 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
428 req->NegotiateContextCount = cpu_to_le16(2);
429 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
430 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
431}
432#else
433static void assemble_neg_contexts(struct smb2_negotiate_req *req)
434{
435 return;
436}
437#endif /* SMB311 */
438
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400439/*
440 *
441 * SMB2 Worker functions follow:
442 *
443 * The general structure of the worker functions is:
444 * 1) Call smb2_init (assembles SMB2 header)
445 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
446 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
447 * 4) Decode SMB2 command specific fields in the fixed length area
448 * 5) Decode variable length data area (if any for this SMB2 command type)
449 * 6) Call free smb buffer
450 * 7) return
451 *
452 */
453
454int
455SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
456{
457 struct smb2_negotiate_req *req;
458 struct smb2_negotiate_rsp *rsp;
459 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700460 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400461 int rc = 0;
462 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400463 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400464 int blob_offset, blob_length;
465 char *security_blob;
466 int flags = CIFS_NEG_OP;
467
Joe Perchesf96637b2013-05-04 22:12:25 -0500468 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400469
Jeff Layton3534b852013-05-24 07:41:01 -0400470 if (!server) {
471 WARN(1, "%s: server is NULL!\n", __func__);
472 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400473 }
474
475 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
476 if (rc)
477 return rc;
478
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700479 req->hdr.sync_hdr.SessionId = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400480
Steve Frenche4aa25e2012-10-01 12:26:22 -0500481 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400482
Steve Frenche4aa25e2012-10-01 12:26:22 -0500483 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
484 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400485
486 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400487 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500488 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400489 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500490 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400491 else
492 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400493
Steve Frenche4aa25e2012-10-01 12:26:22 -0500494 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400495
Steve French3c5f9be12014-05-13 13:37:45 -0700496 /* ClientGUID must be zero for SMB2.02 dialect */
497 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
498 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500499 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700500 memcpy(req->ClientGUID, server->client_guid,
501 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500502 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
503 assemble_neg_contexts(req);
504 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400505 iov[0].iov_base = (char *)req;
506 /* 4 for rfc1002 length field */
507 iov[0].iov_len = get_rfc1002_length(req) + 4;
508
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700509 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
510 cifs_small_buf_release(req);
511 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400512 /*
513 * No tcon so can't do
514 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
515 */
516 if (rc != 0)
517 goto neg_exit;
518
Joe Perchesf96637b2013-05-04 22:12:25 -0500519 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400520
Steve Frenche4aa25e2012-10-01 12:26:22 -0500521 /* BB we may eventually want to match the negotiated vs. requested
522 dialect, even though we are only requesting one at a time */
523 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500524 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500525 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500526 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500527 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500528 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500529 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
530 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600531#ifdef CONFIG_CIFS_SMB311
532 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
533 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
534#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400535 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500536 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500537 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400538 rc = -EIO;
539 goto neg_exit;
540 }
541 server->dialect = le16_to_cpu(rsp->DialectRevision);
542
Jeff Laytone598d1d82013-05-26 07:00:59 -0400543 /* SMB2 only has an extended negflavor */
544 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400545 /* set it to the maximum buffer size value we can send with 1 credit */
546 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
547 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400548 server->max_read = le32_to_cpu(rsp->MaxReadSize);
549 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
550 /* BB Do we need to validate the SecurityMode? */
551 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
552 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400553 /* Internal types */
554 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400555
556 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
557 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500558 /*
559 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
560 * for us will be
561 * ses->sectype = RawNTLMSSP;
562 * but for time being this is our only auth choice so doesn't matter.
563 * We just found a server which sets blob length to zero expecting raw.
564 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700565 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500566 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700567 server->sec_ntlmssp = true;
568 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700569
Jeff Layton38d77c52013-05-26 07:01:00 -0400570 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400571 if (rc)
572 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500573 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500574 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500575 if (rc == 1)
576 rc = 0;
577 else if (rc == 0)
578 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400579 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400580neg_exit:
581 free_rsp_buf(resp_buftype, rsp);
582 return rc;
583}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400584
Steve Frenchff1c0382013-11-19 23:44:46 -0600585int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
586{
587 int rc = 0;
588 struct validate_negotiate_info_req vneg_inbuf;
589 struct validate_negotiate_info_rsp *pneg_rsp;
590 u32 rsplen;
591
592 cifs_dbg(FYI, "validate negotiate\n");
593
594 /*
595 * validation ioctl must be signed, so no point sending this if we
596 * can not sign it. We could eventually change this to selectively
597 * sign just this, the first and only signed request on a connection.
598 * This is good enough for now since a user who wants better security
599 * would also enable signing on the mount. Having validation of
600 * negotiate info for signed connections helps reduce attack vectors
601 */
602 if (tcon->ses->server->sign == false)
603 return 0; /* validation requires signing */
604
605 vneg_inbuf.Capabilities =
606 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100607 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
608 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600609
610 if (tcon->ses->sign)
611 vneg_inbuf.SecurityMode =
612 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
613 else if (global_secflags & CIFSSEC_MAY_SIGN)
614 vneg_inbuf.SecurityMode =
615 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
616 else
617 vneg_inbuf.SecurityMode = 0;
618
619 vneg_inbuf.DialectCount = cpu_to_le16(1);
620 vneg_inbuf.Dialects[0] =
621 cpu_to_le16(tcon->ses->server->vals->protocol_id);
622
623 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
624 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100625 false /* use_ipc */,
Steve Frenchff1c0382013-11-19 23:44:46 -0600626 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
627 (char **)&pneg_rsp, &rsplen);
628
629 if (rc != 0) {
630 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
631 return -EIO;
632 }
633
634 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500635 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
636 rsplen);
637
638 /* relax check since Mac returns max bufsize allowed on ioctl */
639 if (rsplen > CIFSMaxBufSize)
640 return -EIO;
Steve Frenchff1c0382013-11-19 23:44:46 -0600641 }
642
643 /* check validate negotiate info response matches what we got earlier */
644 if (pneg_rsp->Dialect !=
645 cpu_to_le16(tcon->ses->server->vals->protocol_id))
646 goto vneg_out;
647
648 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
649 goto vneg_out;
650
651 /* do not validate server guid because not saved at negprot time yet */
652
653 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
654 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
655 goto vneg_out;
656
657 /* validate negotiate successful */
658 cifs_dbg(FYI, "validate negotiate info successful\n");
659 return 0;
660
661vneg_out:
662 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
663 return -EIO;
664}
665
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530666enum securityEnum
667smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
668{
669 switch (requested) {
670 case Kerberos:
671 case RawNTLMSSP:
672 return requested;
673 case NTLMv2:
674 return RawNTLMSSP;
675 case Unspecified:
676 if (server->sec_ntlmssp &&
677 (global_secflags & CIFSSEC_MAY_NTLMSSP))
678 return RawNTLMSSP;
679 if ((server->sec_kerberos || server->sec_mskerberos) &&
680 (global_secflags & CIFSSEC_MAY_KRB5))
681 return Kerberos;
682 /* Fallthrough */
683 default:
684 return Unspecified;
685 }
686}
687
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100688struct SMB2_sess_data {
689 unsigned int xid;
690 struct cifs_ses *ses;
691 struct nls_table *nls_cp;
692 void (*func)(struct SMB2_sess_data *);
693 int result;
694 u64 previous_session;
695
696 /* we will send the SMB in three pieces:
697 * a fixed length beginning part, an optional
698 * SPNEGO blob (which can be zero length), and a
699 * last part which will include the strings
700 * and rest of bcc area. This allows us to avoid
701 * a large buffer 17K allocation
702 */
703 int buf0_type;
704 struct kvec iov[2];
705};
706
707static int
708SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
709{
710 int rc;
711 struct cifs_ses *ses = sess_data->ses;
712 struct smb2_sess_setup_req *req;
713 struct TCP_Server_Info *server = ses->server;
714
715 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
716 if (rc)
717 return rc;
718
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700719 /* First session, not a reauthenticate */
720 req->hdr.sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100721
722 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
723 req->PreviousSessionId = sess_data->previous_session;
724
725 req->Flags = 0; /* MBZ */
726 /* to enable echos and oplocks */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700727 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100728
729 /* only one of SMB2 signing flags may be set in SMB2 request */
730 if (server->sign)
731 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
732 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
733 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
734 else
735 req->SecurityMode = 0;
736
737 req->Capabilities = 0;
738 req->Channel = 0; /* MBZ */
739
740 sess_data->iov[0].iov_base = (char *)req;
741 /* 4 for rfc1002 length field and 1 for pad */
742 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
743 /*
744 * This variable will be used to clear the buffer
745 * allocated above in case of any error in the calling function.
746 */
747 sess_data->buf0_type = CIFS_SMALL_BUFFER;
748
749 return 0;
750}
751
752static void
753SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
754{
755 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
756 sess_data->buf0_type = CIFS_NO_BUFFER;
757}
758
759static int
760SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
761{
762 int rc;
763 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700764 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100765
766 /* Testing shows that buffer offset must be at location of Buffer[0] */
767 req->SecurityBufferOffset =
768 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
769 1 /* pad */ - 4 /* rfc1001 len */);
770 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
771
772 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
773
774 /* BB add code to build os and lm fields */
775
776 rc = SendReceive2(sess_data->xid, sess_data->ses,
777 sess_data->iov, 2,
778 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700779 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
780 cifs_small_buf_release(sess_data->iov[0].iov_base);
781 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100782
783 return rc;
784}
785
786static int
787SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
788{
789 int rc = 0;
790 struct cifs_ses *ses = sess_data->ses;
791
792 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800793 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100794 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100795 if (rc) {
796 cifs_dbg(FYI,
797 "SMB3 session key generation failed\n");
798 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800799 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100800 }
801 }
802 if (!ses->server->session_estab) {
803 ses->server->sequence_number = 0x2;
804 ses->server->session_estab = true;
805 }
806 mutex_unlock(&ses->server->srv_mutex);
807
808 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
809 spin_lock(&GlobalMid_Lock);
810 ses->status = CifsGood;
811 ses->need_reconnect = false;
812 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100813 return rc;
814}
815
816#ifdef CONFIG_CIFS_UPCALL
817static void
818SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
819{
820 int rc;
821 struct cifs_ses *ses = sess_data->ses;
822 struct cifs_spnego_msg *msg;
823 struct key *spnego_key = NULL;
824 struct smb2_sess_setup_rsp *rsp = NULL;
825
826 rc = SMB2_sess_alloc_buffer(sess_data);
827 if (rc)
828 goto out;
829
830 spnego_key = cifs_get_spnego_key(ses);
831 if (IS_ERR(spnego_key)) {
832 rc = PTR_ERR(spnego_key);
833 spnego_key = NULL;
834 goto out;
835 }
836
837 msg = spnego_key->payload.data[0];
838 /*
839 * check version field to make sure that cifs.upcall is
840 * sending us a response in an expected form
841 */
842 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
843 cifs_dbg(VFS,
844 "bad cifs.upcall version. Expected %d got %d",
845 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
846 rc = -EKEYREJECTED;
847 goto out_put_spnego_key;
848 }
849
850 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
851 GFP_KERNEL);
852 if (!ses->auth_key.response) {
853 cifs_dbg(VFS,
854 "Kerberos can't allocate (%u bytes) memory",
855 msg->sesskey_len);
856 rc = -ENOMEM;
857 goto out_put_spnego_key;
858 }
859 ses->auth_key.len = msg->sesskey_len;
860
861 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
862 sess_data->iov[1].iov_len = msg->secblob_len;
863
864 rc = SMB2_sess_sendreceive(sess_data);
865 if (rc)
866 goto out_put_spnego_key;
867
868 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700869 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100870
871 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100872
873 rc = SMB2_sess_establish_session(sess_data);
874out_put_spnego_key:
875 key_invalidate(spnego_key);
876 key_put(spnego_key);
877out:
878 sess_data->result = rc;
879 sess_data->func = NULL;
880 SMB2_sess_free_buffer(sess_data);
881}
882#else
883static void
884SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
885{
886 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
887 sess_data->result = -EOPNOTSUPP;
888 sess_data->func = NULL;
889}
890#endif
891
Sachin Prabhu166cea42016-10-07 19:11:22 +0100892static void
893SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
894
895static void
896SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
897{
898 int rc;
899 struct cifs_ses *ses = sess_data->ses;
900 struct smb2_sess_setup_rsp *rsp = NULL;
901 char *ntlmssp_blob = NULL;
902 bool use_spnego = false; /* else use raw ntlmssp */
903 u16 blob_length = 0;
904
905 /*
906 * If memory allocation is successful, caller of this function
907 * frees it.
908 */
909 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
910 if (!ses->ntlmssp) {
911 rc = -ENOMEM;
912 goto out_err;
913 }
914 ses->ntlmssp->sesskey_per_smbsess = true;
915
916 rc = SMB2_sess_alloc_buffer(sess_data);
917 if (rc)
918 goto out_err;
919
920 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
921 GFP_KERNEL);
922 if (ntlmssp_blob == NULL) {
923 rc = -ENOMEM;
924 goto out;
925 }
926
927 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
928 if (use_spnego) {
929 /* BB eventually need to add this */
930 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
931 rc = -EOPNOTSUPP;
932 goto out;
933 } else {
934 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
935 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
936 }
937 sess_data->iov[1].iov_base = ntlmssp_blob;
938 sess_data->iov[1].iov_len = blob_length;
939
940 rc = SMB2_sess_sendreceive(sess_data);
941 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
942
943 /* If true, rc here is expected and not an error */
944 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700945 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +0100946 rc = 0;
947
948 if (rc)
949 goto out;
950
951 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
952 le16_to_cpu(rsp->SecurityBufferOffset)) {
953 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
954 le16_to_cpu(rsp->SecurityBufferOffset));
955 rc = -EIO;
956 goto out;
957 }
958 rc = decode_ntlmssp_challenge(rsp->Buffer,
959 le16_to_cpu(rsp->SecurityBufferLength), ses);
960 if (rc)
961 goto out;
962
963 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
964
965
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700966 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100967 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +0100968
969out:
970 kfree(ntlmssp_blob);
971 SMB2_sess_free_buffer(sess_data);
972 if (!rc) {
973 sess_data->result = 0;
974 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
975 return;
976 }
977out_err:
978 kfree(ses->ntlmssp);
979 ses->ntlmssp = NULL;
980 sess_data->result = rc;
981 sess_data->func = NULL;
982}
983
984static void
985SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
986{
987 int rc;
988 struct cifs_ses *ses = sess_data->ses;
989 struct smb2_sess_setup_req *req;
990 struct smb2_sess_setup_rsp *rsp = NULL;
991 unsigned char *ntlmssp_blob = NULL;
992 bool use_spnego = false; /* else use raw ntlmssp */
993 u16 blob_length = 0;
994
995 rc = SMB2_sess_alloc_buffer(sess_data);
996 if (rc)
997 goto out;
998
999 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001000 req->hdr.sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001001
1002 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1003 sess_data->nls_cp);
1004 if (rc) {
1005 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1006 goto out;
1007 }
1008
1009 if (use_spnego) {
1010 /* BB eventually need to add this */
1011 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1012 rc = -EOPNOTSUPP;
1013 goto out;
1014 }
1015 sess_data->iov[1].iov_base = ntlmssp_blob;
1016 sess_data->iov[1].iov_len = blob_length;
1017
1018 rc = SMB2_sess_sendreceive(sess_data);
1019 if (rc)
1020 goto out;
1021
1022 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1023
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001024 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001025 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001026
1027 rc = SMB2_sess_establish_session(sess_data);
1028out:
1029 kfree(ntlmssp_blob);
1030 SMB2_sess_free_buffer(sess_data);
1031 kfree(ses->ntlmssp);
1032 ses->ntlmssp = NULL;
1033 sess_data->result = rc;
1034 sess_data->func = NULL;
1035}
1036
1037static int
1038SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1039{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301040 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001041
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301042 type = smb2_select_sectype(ses->server, ses->sectype);
1043 cifs_dbg(FYI, "sess setup type %d\n", type);
1044 if (type == Unspecified) {
1045 cifs_dbg(VFS,
1046 "Unable to select appropriate authentication method!");
1047 return -EINVAL;
1048 }
1049
1050 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001051 case Kerberos:
1052 sess_data->func = SMB2_auth_kerberos;
1053 break;
1054 case RawNTLMSSP:
1055 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1056 break;
1057 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301058 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001059 return -EOPNOTSUPP;
1060 }
1061
1062 return 0;
1063}
1064
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001065int
1066SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1067 const struct nls_table *nls_cp)
1068{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001069 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001070 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001071 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001072
Joe Perchesf96637b2013-05-04 22:12:25 -05001073 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001074
Jeff Layton3534b852013-05-24 07:41:01 -04001075 if (!server) {
1076 WARN(1, "%s: server is NULL!\n", __func__);
1077 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001078 }
1079
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001080 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1081 if (!sess_data)
1082 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001083
1084 rc = SMB2_select_sec(ses, sess_data);
1085 if (rc)
1086 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001087 sess_data->xid = xid;
1088 sess_data->ses = ses;
1089 sess_data->buf0_type = CIFS_NO_BUFFER;
1090 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001091
Sachin Prabhu166cea42016-10-07 19:11:22 +01001092 while (sess_data->func)
1093 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001094
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001095 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001096out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001097 kfree(sess_data);
1098 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001099}
1100
1101int
1102SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1103{
1104 struct smb2_logoff_req *req; /* response is also trivial struct */
1105 int rc = 0;
1106 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001107 int flags = 0;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001108
Joe Perchesf96637b2013-05-04 22:12:25 -05001109 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001110
1111 if (ses && (ses->server))
1112 server = ses->server;
1113 else
1114 return -EIO;
1115
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001116 /* no need to send SMB logoff if uid already closed due to reconnect */
1117 if (ses->need_reconnect)
1118 goto smb2_session_already_dead;
1119
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001120 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1121 if (rc)
1122 return rc;
1123
1124 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001125 req->hdr.sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001126
1127 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1128 flags |= CIFS_TRANSFORM_REQ;
1129 else if (server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001130 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001131
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001132 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001133 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001134 /*
1135 * No tcon so can't do
1136 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1137 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001138
1139smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001140 return rc;
1141}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001142
1143static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1144{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001145 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001146}
1147
1148#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1149
Steve Frenchde9f68df2013-11-15 11:26:24 -06001150/* These are similar values to what Windows uses */
1151static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1152{
1153 tcon->max_chunks = 256;
1154 tcon->max_bytes_chunk = 1048576;
1155 tcon->max_bytes_copy = 16777216;
1156}
1157
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001158int
1159SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1160 struct cifs_tcon *tcon, const struct nls_table *cp)
1161{
1162 struct smb2_tree_connect_req *req;
1163 struct smb2_tree_connect_rsp *rsp = NULL;
1164 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001165 struct kvec rsp_iov;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001166 int rc = 0;
1167 int resp_buftype;
1168 int unc_path_len;
1169 struct TCP_Server_Info *server;
1170 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001171 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001172
Joe Perchesf96637b2013-05-04 22:12:25 -05001173 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001174
1175 if ((ses->server) && tree)
1176 server = ses->server;
1177 else
1178 return -EIO;
1179
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001180 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1181 if (unc_path == NULL)
1182 return -ENOMEM;
1183
1184 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1185 unc_path_len *= 2;
1186 if (unc_path_len < 2) {
1187 kfree(unc_path);
1188 return -EINVAL;
1189 }
1190
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001191 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1192 if (tcon)
1193 tcon->tid = 0;
1194
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001195 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1196 if (rc) {
1197 kfree(unc_path);
1198 return rc;
1199 }
1200
1201 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001202 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1203 flags |= CIFS_TRANSFORM_REQ;
1204
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001205 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001206 req->hdr.sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001207 if (ses->server->sign)
1208 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001209 } else if (encryption_required(tcon))
1210 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001211
1212 iov[0].iov_base = (char *)req;
1213 /* 4 for rfc1002 length field and 1 for pad */
1214 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1215
1216 /* Testing shows that buffer offset must be at location of Buffer[0] */
1217 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1218 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1219 req->PathLength = cpu_to_le16(unc_path_len - 2);
1220 iov[1].iov_base = unc_path;
1221 iov[1].iov_len = unc_path_len;
1222
1223 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1224
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001225 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001226 cifs_small_buf_release(req);
1227 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001228
1229 if (rc != 0) {
1230 if (tcon) {
1231 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1232 tcon->need_reconnect = true;
1233 }
1234 goto tcon_error_exit;
1235 }
1236
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001237 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001238 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001239 goto tcon_exit;
1240 }
1241
1242 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001243 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001244 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1245 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001246 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001247 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1248 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001249 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001250 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001251 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001252 rc = -EOPNOTSUPP;
1253 goto tcon_error_exit;
1254 }
1255
1256 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001257 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001258 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1259 tcon->tidStatus = CifsGood;
1260 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001261 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001262 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001263
1264 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1265 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001266 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001267
1268 if (tcon->seal &&
1269 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1270 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1271
Steve Frenchde9f68df2013-11-15 11:26:24 -06001272 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001273 if (tcon->ses->server->ops->validate_negotiate)
1274 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001275tcon_exit:
1276 free_rsp_buf(resp_buftype, rsp);
1277 kfree(unc_path);
1278 return rc;
1279
1280tcon_error_exit:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001281 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001282 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001283 }
1284 goto tcon_exit;
1285}
1286
1287int
1288SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1289{
1290 struct smb2_tree_disconnect_req *req; /* response is trivial */
1291 int rc = 0;
1292 struct TCP_Server_Info *server;
1293 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001294 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001295
Joe Perchesf96637b2013-05-04 22:12:25 -05001296 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001297
1298 if (ses && (ses->server))
1299 server = ses->server;
1300 else
1301 return -EIO;
1302
1303 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1304 return 0;
1305
1306 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1307 if (rc)
1308 return rc;
1309
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001310 if (encryption_required(tcon))
1311 flags |= CIFS_TRANSFORM_REQ;
1312
1313 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001314 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001315 if (rc)
1316 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1317
1318 return rc;
1319}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001320
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001321
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001322static struct create_durable *
1323create_durable_buf(void)
1324{
1325 struct create_durable *buf;
1326
1327 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1328 if (!buf)
1329 return NULL;
1330
1331 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001332 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001333 buf->ccontext.DataLength = cpu_to_le32(16);
1334 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1335 (struct create_durable, Name));
1336 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001337 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001338 buf->Name[0] = 'D';
1339 buf->Name[1] = 'H';
1340 buf->Name[2] = 'n';
1341 buf->Name[3] = 'Q';
1342 return buf;
1343}
1344
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001345static struct create_durable *
1346create_reconnect_durable_buf(struct cifs_fid *fid)
1347{
1348 struct create_durable *buf;
1349
1350 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1351 if (!buf)
1352 return NULL;
1353
1354 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1355 (struct create_durable, Data));
1356 buf->ccontext.DataLength = cpu_to_le32(16);
1357 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1358 (struct create_durable, Name));
1359 buf->ccontext.NameLength = cpu_to_le16(4);
1360 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1361 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001362 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001363 buf->Name[0] = 'D';
1364 buf->Name[1] = 'H';
1365 buf->Name[2] = 'n';
1366 buf->Name[3] = 'C';
1367 return buf;
1368}
1369
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001370static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001371parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1372 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001373{
1374 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001375 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001376 unsigned int next;
1377 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001378 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001379
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001380 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001381 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001382 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001383 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001384 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001385 if (le16_to_cpu(cc->NameLength) == 4 &&
1386 strncmp(name, "RqLs", 4) == 0)
1387 return server->ops->parse_lease_buf(cc, epoch);
1388
1389 next = le32_to_cpu(cc->Next);
1390 if (!next)
1391 break;
1392 remaining -= next;
1393 cc = (struct create_context *)((char *)cc + next);
1394 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001395
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001396 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001397}
1398
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001399static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001400add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1401 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001402{
1403 struct smb2_create_req *req = iov[0].iov_base;
1404 unsigned int num = *num_iovec;
1405
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001406 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001407 if (iov[num].iov_base == NULL)
1408 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001409 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001410 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1411 if (!req->CreateContextsOffset)
1412 req->CreateContextsOffset = cpu_to_le32(
1413 sizeof(struct smb2_create_req) - 4 +
1414 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001415 le32_add_cpu(&req->CreateContextsLength,
1416 server->vals->create_lease_size);
1417 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001418 *num_iovec = num + 1;
1419 return 0;
1420}
1421
Steve Frenchb56eae42015-11-03 09:26:27 -06001422static struct create_durable_v2 *
1423create_durable_v2_buf(struct cifs_fid *pfid)
1424{
1425 struct create_durable_v2 *buf;
1426
1427 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1428 if (!buf)
1429 return NULL;
1430
1431 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1432 (struct create_durable_v2, dcontext));
1433 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1434 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1435 (struct create_durable_v2, Name));
1436 buf->ccontext.NameLength = cpu_to_le16(4);
1437
1438 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1439 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001440 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001441 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1442
1443 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1444 buf->Name[0] = 'D';
1445 buf->Name[1] = 'H';
1446 buf->Name[2] = '2';
1447 buf->Name[3] = 'Q';
1448 return buf;
1449}
1450
1451static struct create_durable_handle_reconnect_v2 *
1452create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1453{
1454 struct create_durable_handle_reconnect_v2 *buf;
1455
1456 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1457 GFP_KERNEL);
1458 if (!buf)
1459 return NULL;
1460
1461 buf->ccontext.DataOffset =
1462 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1463 dcontext));
1464 buf->ccontext.DataLength =
1465 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1466 buf->ccontext.NameOffset =
1467 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1468 Name));
1469 buf->ccontext.NameLength = cpu_to_le16(4);
1470
1471 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1472 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1473 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1474 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1475
1476 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1477 buf->Name[0] = 'D';
1478 buf->Name[1] = 'H';
1479 buf->Name[2] = '2';
1480 buf->Name[3] = 'C';
1481 return buf;
1482}
1483
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001484static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001485add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001486 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001487{
1488 struct smb2_create_req *req = iov[0].iov_base;
1489 unsigned int num = *num_iovec;
1490
Steve Frenchb56eae42015-11-03 09:26:27 -06001491 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1492 if (iov[num].iov_base == NULL)
1493 return -ENOMEM;
1494 iov[num].iov_len = sizeof(struct create_durable_v2);
1495 if (!req->CreateContextsOffset)
1496 req->CreateContextsOffset =
1497 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1498 iov[1].iov_len);
1499 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1500 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1501 *num_iovec = num + 1;
1502 return 0;
1503}
1504
1505static int
1506add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1507 struct cifs_open_parms *oparms)
1508{
1509 struct smb2_create_req *req = iov[0].iov_base;
1510 unsigned int num = *num_iovec;
1511
1512 /* indicate that we don't need to relock the file */
1513 oparms->reconnect = false;
1514
1515 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1516 if (iov[num].iov_base == NULL)
1517 return -ENOMEM;
1518 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1519 if (!req->CreateContextsOffset)
1520 req->CreateContextsOffset =
1521 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1522 iov[1].iov_len);
1523 le32_add_cpu(&req->CreateContextsLength,
1524 sizeof(struct create_durable_handle_reconnect_v2));
1525 inc_rfc1001_len(&req->hdr,
1526 sizeof(struct create_durable_handle_reconnect_v2));
1527 *num_iovec = num + 1;
1528 return 0;
1529}
1530
1531static int
1532add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1533 struct cifs_open_parms *oparms, bool use_persistent)
1534{
1535 struct smb2_create_req *req = iov[0].iov_base;
1536 unsigned int num = *num_iovec;
1537
1538 if (use_persistent) {
1539 if (oparms->reconnect)
1540 return add_durable_reconnect_v2_context(iov, num_iovec,
1541 oparms);
1542 else
1543 return add_durable_v2_context(iov, num_iovec, oparms);
1544 }
1545
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001546 if (oparms->reconnect) {
1547 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1548 /* indicate that we don't need to relock the file */
1549 oparms->reconnect = false;
1550 } else
1551 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001552 if (iov[num].iov_base == NULL)
1553 return -ENOMEM;
1554 iov[num].iov_len = sizeof(struct create_durable);
1555 if (!req->CreateContextsOffset)
1556 req->CreateContextsOffset =
1557 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1558 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001559 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001560 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1561 *num_iovec = num + 1;
1562 return 0;
1563}
1564
Aurelien Aptelf0712922017-02-22 14:47:17 +01001565static int
1566alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1567 const char *treename, const __le16 *path)
1568{
1569 int treename_len, path_len;
1570 struct nls_table *cp;
1571 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1572
1573 /*
1574 * skip leading "\\"
1575 */
1576 treename_len = strlen(treename);
1577 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1578 return -EINVAL;
1579
1580 treename += 2;
1581 treename_len -= 2;
1582
1583 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1584
1585 /*
1586 * make room for one path separator between the treename and
1587 * path
1588 */
1589 *out_len = treename_len + 1 + path_len;
1590
1591 /*
1592 * final path needs to be null-terminated UTF16 with a
1593 * size aligned to 8
1594 */
1595
1596 *out_size = roundup((*out_len+1)*2, 8);
1597 *out_path = kzalloc(*out_size, GFP_KERNEL);
1598 if (!*out_path)
1599 return -ENOMEM;
1600
1601 cp = load_nls_default();
1602 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1603 UniStrcat(*out_path, sep);
1604 UniStrcat(*out_path, path);
1605 unload_nls(cp);
1606
1607 return 0;
1608}
1609
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001610int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001611SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001612 __u8 *oplock, struct smb2_file_all_info *buf,
1613 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001614{
1615 struct smb2_create_req *req;
1616 struct smb2_create_rsp *rsp;
1617 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001618 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001619 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001620 struct kvec iov[4];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001621 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001622 int resp_buftype;
1623 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001624 __le16 *copy_path = NULL;
1625 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001626 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001627 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001628 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001629 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001630 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001631
Joe Perchesf96637b2013-05-04 22:12:25 -05001632 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001633
1634 if (ses && (ses->server))
1635 server = ses->server;
1636 else
1637 return -EIO;
1638
1639 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1640 if (rc)
1641 return rc;
1642
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001643 if (encryption_required(tcon))
1644 flags |= CIFS_TRANSFORM_REQ;
1645
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001646 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001647 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001648 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1649 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001650
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001651 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001652 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001653 /* File attributes ignored on open (used in create though) */
1654 req->FileAttributes = cpu_to_le32(file_attributes);
1655 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001656 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1657 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001658
1659 iov[0].iov_base = (char *)req;
1660 /* 4 for rfc1002 length field */
1661 iov[0].iov_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001662 /* -1 since last byte is buf[0] which is sent below (path) */
1663 iov[0].iov_len--;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001664
Aurelien Aptelf0712922017-02-22 14:47:17 +01001665 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1666
1667 /* [MS-SMB2] 2.2.13 NameOffset:
1668 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1669 * the SMB2 header, the file name includes a prefix that will
1670 * be processed during DFS name normalization as specified in
1671 * section 3.3.5.9. Otherwise, the file name is relative to
1672 * the share that is identified by the TreeId in the SMB2
1673 * header.
1674 */
1675 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1676 int name_len;
1677
1678 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1679 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1680 &name_len,
1681 tcon->treeName, path);
1682 if (rc)
1683 return rc;
1684 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001685 uni_path_len = copy_size;
1686 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001687 } else {
1688 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1689 /* MUST set path len (NameLength) to 0 opening root of share */
1690 req->NameLength = cpu_to_le16(uni_path_len - 2);
1691 if (uni_path_len % 8 != 0) {
1692 copy_size = roundup(uni_path_len, 8);
1693 copy_path = kzalloc(copy_size, GFP_KERNEL);
1694 if (!copy_path)
1695 return -ENOMEM;
1696 memcpy((char *)copy_path, (const char *)path,
1697 uni_path_len);
1698 uni_path_len = copy_size;
1699 path = copy_path;
1700 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001701 }
1702
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001703 iov[1].iov_len = uni_path_len;
1704 iov[1].iov_base = path;
1705 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1706 inc_rfc1001_len(req, uni_path_len - 1);
1707
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001708 if (!server->oplocks)
1709 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1710
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001711 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001712 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1713 req->RequestedOplockLevel = *oplock;
1714 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001715 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001716 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001717 cifs_small_buf_release(req);
1718 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001719 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001720 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001721 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001722 }
1723
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001724 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1725 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001726 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001727 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001728 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001729 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001730 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001731 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001732
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001733 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001734 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001735 if (rc) {
1736 cifs_small_buf_release(req);
1737 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001738 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001739 return rc;
1740 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001741 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001742 }
1743
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001744 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001745 cifs_small_buf_release(req);
1746 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001747
1748 if (rc != 0) {
1749 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001750 if (err_buf)
1751 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1752 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001753 goto creat_exit;
1754 }
1755
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001756 oparms->fid->persistent_fid = rsp->PersistentFileId;
1757 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001758
1759 if (buf) {
1760 memcpy(buf, &rsp->CreationTime, 32);
1761 buf->AllocationSize = rsp->AllocationSize;
1762 buf->EndOfFile = rsp->EndofFile;
1763 buf->Attributes = rsp->FileAttributes;
1764 buf->NumberOfLinks = cpu_to_le32(1);
1765 buf->DeletePending = 0;
1766 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001767
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001768 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001769 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001770 else
1771 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001772creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001773 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001774 kfree(lc_buf);
1775 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001776 free_rsp_buf(resp_buftype, rsp);
1777 return rc;
1778}
1779
Steve French4a72daf2013-06-25 00:20:49 -05001780/*
1781 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1782 */
1783int
1784SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001785 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1786 char *in_data, u32 indatalen,
1787 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001788{
1789 struct smb2_ioctl_req *req;
1790 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001791 struct smb2_sync_hdr *shdr;
Steve French4a72daf2013-06-25 00:20:49 -05001792 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001793 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001794 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001795 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001796 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001797 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001798 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001799 int flags = 0;
Steve French4a72daf2013-06-25 00:20:49 -05001800
1801 cifs_dbg(FYI, "SMB2 IOCTL\n");
1802
Steve French3d1a3742014-08-11 21:05:25 -05001803 if (out_data != NULL)
1804 *out_data = NULL;
1805
Steve French4a72daf2013-06-25 00:20:49 -05001806 /* zero out returned data len, in case of error */
1807 if (plen)
1808 *plen = 0;
1809
Steve French8e353102015-03-26 19:47:02 -05001810 if (tcon)
1811 ses = tcon->ses;
1812 else
1813 return -EIO;
1814
Steve French4a72daf2013-06-25 00:20:49 -05001815 if (ses && (ses->server))
1816 server = ses->server;
1817 else
1818 return -EIO;
1819
1820 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1821 if (rc)
1822 return rc;
1823
Aurelien Aptel51146622017-02-28 15:08:41 +01001824 if (use_ipc) {
1825 if (ses->ipc_tid == 0) {
1826 cifs_small_buf_release(req);
1827 return -ENOTCONN;
1828 }
1829
1830 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1831 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1832 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1833 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001834 if (encryption_required(tcon))
1835 flags |= CIFS_TRANSFORM_REQ;
1836
Steve French4a72daf2013-06-25 00:20:49 -05001837 req->CtlCode = cpu_to_le32(opcode);
1838 req->PersistentFileId = persistent_fid;
1839 req->VolatileFileId = volatile_fid;
1840
1841 if (indatalen) {
1842 req->InputCount = cpu_to_le32(indatalen);
1843 /* do not set InputOffset if no input data */
1844 req->InputOffset =
1845 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1846 iov[1].iov_base = in_data;
1847 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001848 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001849 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001850 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001851
1852 req->OutputOffset = 0;
1853 req->OutputCount = 0; /* MBZ */
1854
1855 /*
1856 * Could increase MaxOutputResponse, but that would require more
1857 * than one credit. Windows typically sets this smaller, but for some
1858 * ioctls it may be useful to allow server to send more. No point
1859 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001860 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1861 * (by default, note that it can be overridden to make max larger)
1862 * in responses (except for read responses which can be bigger.
1863 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001864 */
Steve French7db0a6e2017-05-03 21:12:20 -05001865 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001866
1867 if (is_fsctl)
1868 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1869 else
1870 req->Flags = 0;
1871
1872 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001873
Steve French7ff8d452013-10-14 00:44:19 -05001874 /*
1875 * If no input data, the size of ioctl struct in
1876 * protocol spec still includes a 1 byte data buffer,
1877 * but if input data passed to ioctl, we do not
1878 * want to double count this, so we do not send
1879 * the dummy one byte of data in iovec[0] if sending
1880 * input data (in iovec[1]). We also must add 4 bytes
1881 * in first iovec to allow for rfc1002 length field.
1882 */
1883
1884 if (indatalen) {
1885 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1886 inc_rfc1001_len(req, indatalen - 1);
1887 } else
1888 iov[0].iov_len = get_rfc1002_length(req) + 4;
1889
Steve French4a72daf2013-06-25 00:20:49 -05001890
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001891 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001892 cifs_small_buf_release(req);
1893 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001894
Steve French9bf0c9c2013-11-16 18:05:28 -06001895 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001896 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001897 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001898 } else if (rc == -EINVAL) {
1899 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1900 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001901 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001902 goto ioctl_exit;
1903 }
Steve French4a72daf2013-06-25 00:20:49 -05001904 }
1905
1906 /* check if caller wants to look at return data or just return rc */
1907 if ((plen == NULL) || (out_data == NULL))
1908 goto ioctl_exit;
1909
1910 *plen = le32_to_cpu(rsp->OutputCount);
1911
1912 /* We check for obvious errors in the output buffer length and offset */
1913 if (*plen == 0)
1914 goto ioctl_exit; /* server returned no data */
1915 else if (*plen > 0xFF00) {
1916 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1917 *plen = 0;
1918 rc = -EIO;
1919 goto ioctl_exit;
1920 }
1921
1922 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1923 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1924 le32_to_cpu(rsp->OutputOffset));
1925 *plen = 0;
1926 rc = -EIO;
1927 goto ioctl_exit;
1928 }
1929
1930 *out_data = kmalloc(*plen, GFP_KERNEL);
1931 if (*out_data == NULL) {
1932 rc = -ENOMEM;
1933 goto ioctl_exit;
1934 }
1935
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001936 shdr = get_sync_hdr(rsp);
1937 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05001938ioctl_exit:
1939 free_rsp_buf(resp_buftype, rsp);
1940 return rc;
1941}
1942
Steve French64a5cfa2013-10-14 15:31:32 -05001943/*
1944 * Individual callers to ioctl worker function follow
1945 */
1946
1947int
1948SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1949 u64 persistent_fid, u64 volatile_fid)
1950{
1951 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001952 struct compress_ioctl fsctl_input;
1953 char *ret_data = NULL;
1954
1955 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001956 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001957
1958 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1959 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001960 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05001961 (char *)&fsctl_input /* data input */,
1962 2 /* in data len */, &ret_data /* out data */, NULL);
1963
1964 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001965
1966 return rc;
1967}
1968
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001969int
1970SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1971 u64 persistent_fid, u64 volatile_fid)
1972{
1973 struct smb2_close_req *req;
1974 struct smb2_close_rsp *rsp;
1975 struct TCP_Server_Info *server;
1976 struct cifs_ses *ses = tcon->ses;
1977 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001978 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001979 int resp_buftype;
1980 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001981 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001982
Joe Perchesf96637b2013-05-04 22:12:25 -05001983 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001984
1985 if (ses && (ses->server))
1986 server = ses->server;
1987 else
1988 return -EIO;
1989
1990 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1991 if (rc)
1992 return rc;
1993
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001994 if (encryption_required(tcon))
1995 flags |= CIFS_TRANSFORM_REQ;
1996
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001997 req->PersistentFileId = persistent_fid;
1998 req->VolatileFileId = volatile_fid;
1999
2000 iov[0].iov_base = (char *)req;
2001 /* 4 for rfc1002 length field */
2002 iov[0].iov_len = get_rfc1002_length(req) + 4;
2003
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002004 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002005 cifs_small_buf_release(req);
2006 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002007
2008 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002009 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002010 goto close_exit;
2011 }
2012
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002013 /* BB FIXME - decode close response, update inode for caching */
2014
2015close_exit:
2016 free_rsp_buf(resp_buftype, rsp);
2017 return rc;
2018}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002019
2020static int
2021validate_buf(unsigned int offset, unsigned int buffer_length,
2022 struct smb2_hdr *hdr, unsigned int min_buf_size)
2023
2024{
2025 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2026 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2027 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2028 char *end_of_buf = begin_of_buf + buffer_length;
2029
2030
2031 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002032 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2033 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002034 return -EINVAL;
2035 }
2036
2037 /* check if beyond RFC1001 maximum length */
2038 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002039 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2040 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002041 return -EINVAL;
2042 }
2043
2044 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002045 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002046 return -EINVAL;
2047 }
2048
2049 return 0;
2050}
2051
2052/*
2053 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2054 * Caller must free buffer.
2055 */
2056static int
2057validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2058 struct smb2_hdr *hdr, unsigned int minbufsize,
2059 char *data)
2060
2061{
2062 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2063 int rc;
2064
2065 if (!data)
2066 return -EINVAL;
2067
2068 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2069 if (rc)
2070 return rc;
2071
2072 memcpy(data, begin_of_buf, buffer_length);
2073
2074 return 0;
2075}
2076
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002077static int
2078query_info(const unsigned int xid, struct cifs_tcon *tcon,
2079 u64 persistent_fid, u64 volatile_fid, u8 info_class,
2080 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002081{
2082 struct smb2_query_info_req *req;
2083 struct smb2_query_info_rsp *rsp = NULL;
2084 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002085 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002086 int rc = 0;
2087 int resp_buftype;
2088 struct TCP_Server_Info *server;
2089 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002090 int flags = 0;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002091
Joe Perchesf96637b2013-05-04 22:12:25 -05002092 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002093
2094 if (ses && (ses->server))
2095 server = ses->server;
2096 else
2097 return -EIO;
2098
2099 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2100 if (rc)
2101 return rc;
2102
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002103 if (encryption_required(tcon))
2104 flags |= CIFS_TRANSFORM_REQ;
2105
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002106 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002107 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002108 req->PersistentFileId = persistent_fid;
2109 req->VolatileFileId = volatile_fid;
2110 /* 4 for rfc1002 length field and 1 for Buffer */
2111 req->InputBufferOffset =
2112 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002113 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002114
2115 iov[0].iov_base = (char *)req;
2116 /* 4 for rfc1002 length field */
2117 iov[0].iov_len = get_rfc1002_length(req) + 4;
2118
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002119 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002120 cifs_small_buf_release(req);
2121 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002122
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002123 if (rc) {
2124 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2125 goto qinf_exit;
2126 }
2127
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002128 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2129 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002130 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002131
2132qinf_exit:
2133 free_rsp_buf(resp_buftype, rsp);
2134 return rc;
2135}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002136
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002137int
2138SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2139 u64 persistent_fid, u64 volatile_fid,
2140 struct smb2_file_all_info *data)
2141{
2142 return query_info(xid, tcon, persistent_fid, volatile_fid,
2143 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002144 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002145 sizeof(struct smb2_file_all_info), data);
2146}
2147
2148int
2149SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2150 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2151{
2152 return query_info(xid, tcon, persistent_fid, volatile_fid,
2153 FILE_INTERNAL_INFORMATION,
2154 sizeof(struct smb2_file_internal_info),
2155 sizeof(struct smb2_file_internal_info), uniqueid);
2156}
2157
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002158/*
2159 * This is a no-op for now. We're not really interested in the reply, but
2160 * rather in the fact that the server sent one and that server->lstrp
2161 * gets updated.
2162 *
2163 * FIXME: maybe we should consider checking that the reply matches request?
2164 */
2165static void
2166smb2_echo_callback(struct mid_q_entry *mid)
2167{
2168 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002169 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002170 unsigned int credits_received = 1;
2171
2172 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002173 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002174
Christopher Oo5fb4e282015-06-25 16:10:48 -07002175 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002176 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002177 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002178 add_credits(server, credits_received, CIFS_ECHO_OP);
2179}
2180
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002181void smb2_reconnect_server(struct work_struct *work)
2182{
2183 struct TCP_Server_Info *server = container_of(work,
2184 struct TCP_Server_Info, reconnect.work);
2185 struct cifs_ses *ses;
2186 struct cifs_tcon *tcon, *tcon2;
2187 struct list_head tmp_list;
2188 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002189 int rc;
2190 int resched = false;
2191
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002192
2193 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2194 mutex_lock(&server->reconnect_mutex);
2195
2196 INIT_LIST_HEAD(&tmp_list);
2197 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2198
2199 spin_lock(&cifs_tcp_ses_lock);
2200 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2201 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002202 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002203 tcon->tc_count++;
2204 list_add_tail(&tcon->rlist, &tmp_list);
2205 tcon_exist = true;
2206 }
2207 }
2208 }
2209 /*
2210 * Get the reference to server struct to be sure that the last call of
2211 * cifs_put_tcon() in the loop below won't release the server pointer.
2212 */
2213 if (tcon_exist)
2214 server->srv_count++;
2215
2216 spin_unlock(&cifs_tcp_ses_lock);
2217
2218 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002219 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2220 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002221 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002222 else
2223 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002224 list_del_init(&tcon->rlist);
2225 cifs_put_tcon(tcon);
2226 }
2227
2228 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002229 if (resched)
2230 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002231 mutex_unlock(&server->reconnect_mutex);
2232
2233 /* now we can safely release srv struct */
2234 if (tcon_exist)
2235 cifs_put_tcp_session(server, 1);
2236}
2237
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002238int
2239SMB2_echo(struct TCP_Server_Info *server)
2240{
2241 struct smb2_echo_req *req;
2242 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002243 struct kvec iov[2];
2244 struct smb_rqst rqst = { .rq_iov = iov,
2245 .rq_nvec = 2 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002246
Joe Perchesf96637b2013-05-04 22:12:25 -05002247 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002248
Steve French4fcd1812016-06-22 20:12:05 -05002249 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002250 /* No need to send echo on newly established connections */
2251 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2252 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002253 }
2254
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002255 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2256 if (rc)
2257 return rc;
2258
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002259 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002260
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002261 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002262 iov[0].iov_len = 4;
2263 iov[0].iov_base = (char *)req;
2264 iov[1].iov_len = get_rfc1002_length(req);
2265 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002266
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002267 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2268 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002269 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002270 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002271
2272 cifs_small_buf_release(req);
2273 return rc;
2274}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002275
2276int
2277SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2278 u64 volatile_fid)
2279{
2280 struct smb2_flush_req *req;
2281 struct TCP_Server_Info *server;
2282 struct cifs_ses *ses = tcon->ses;
2283 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002284 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002285 int resp_buftype;
2286 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002287 int flags = 0;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002288
Joe Perchesf96637b2013-05-04 22:12:25 -05002289 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002290
2291 if (ses && (ses->server))
2292 server = ses->server;
2293 else
2294 return -EIO;
2295
2296 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2297 if (rc)
2298 return rc;
2299
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002300 if (encryption_required(tcon))
2301 flags |= CIFS_TRANSFORM_REQ;
2302
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002303 req->PersistentFileId = persistent_fid;
2304 req->VolatileFileId = volatile_fid;
2305
2306 iov[0].iov_base = (char *)req;
2307 /* 4 for rfc1002 length field */
2308 iov[0].iov_len = get_rfc1002_length(req) + 4;
2309
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002310 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002311 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002312
Steve Frenchdfebe402015-03-27 01:00:06 -05002313 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002314 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2315
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002316 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002317 return rc;
2318}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002319
2320/*
2321 * To form a chain of read requests, any read requests after the first should
2322 * have the end_of_chain boolean set to true.
2323 */
2324static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002325smb2_new_read_req(void **buf, unsigned int *total_len,
2326 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2327 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002328{
2329 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002330 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002331 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002332
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002333 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2334 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002335 if (rc)
2336 return rc;
2337 if (io_parms->tcon->ses->server == NULL)
2338 return -ECONNABORTED;
2339
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002340 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002341 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002342
2343 req->PersistentFileId = io_parms->persistent_fid;
2344 req->VolatileFileId = io_parms->volatile_fid;
2345 req->ReadChannelInfoOffset = 0; /* reserved */
2346 req->ReadChannelInfoLength = 0; /* reserved */
2347 req->Channel = 0; /* reserved */
2348 req->MinimumCount = 0;
2349 req->Length = cpu_to_le32(io_parms->length);
2350 req->Offset = cpu_to_le64(io_parms->offset);
2351
2352 if (request_type & CHAINED_REQUEST) {
2353 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002354 /* next 8-byte aligned request */
2355 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2356 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002357 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002358 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002359 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002360 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002361 /*
2362 * Related requests use info from previous read request
2363 * in chain.
2364 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002365 shdr->SessionId = 0xFFFFFFFF;
2366 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002367 req->PersistentFileId = 0xFFFFFFFF;
2368 req->VolatileFileId = 0xFFFFFFFF;
2369 }
2370 }
2371 if (remaining_bytes > io_parms->length)
2372 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2373 else
2374 req->RemainingBytes = 0;
2375
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002376 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002377 return rc;
2378}
2379
2380static void
2381smb2_readv_callback(struct mid_q_entry *mid)
2382{
2383 struct cifs_readdata *rdata = mid->callback_data;
2384 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2385 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002386 struct smb2_sync_hdr *shdr =
2387 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002388 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002389 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2390 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002391 .rq_pages = rdata->pages,
2392 .rq_npages = rdata->nr_pages,
2393 .rq_pagesz = rdata->pagesz,
2394 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002395
Joe Perchesf96637b2013-05-04 22:12:25 -05002396 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2397 __func__, mid->mid, mid->mid_state, rdata->result,
2398 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002399
2400 switch (mid->mid_state) {
2401 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002402 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002403 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002404 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002405 int rc;
2406
Jeff Layton0b688cf2012-09-18 16:20:34 -07002407 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002408 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002409 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2410 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002411 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002412 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002413 task_io_account_read(rdata->got_bytes);
2414 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002415 break;
2416 case MID_REQUEST_SUBMITTED:
2417 case MID_RETRY_NEEDED:
2418 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002419 if (server->sign && rdata->got_bytes)
2420 /* reset bytes number since we can not check a sign */
2421 rdata->got_bytes = 0;
2422 /* FIXME: should this be counted toward the initiating task? */
2423 task_io_account_read(rdata->got_bytes);
2424 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002425 break;
2426 default:
2427 if (rdata->result != -ENODATA)
2428 rdata->result = -EIO;
2429 }
2430
2431 if (rdata->result)
2432 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2433
2434 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002435 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002436 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002437 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002438 add_credits(server, credits_received, 0);
2439}
2440
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002441/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002442int
2443smb2_async_readv(struct cifs_readdata *rdata)
2444{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002445 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002446 char *buf;
2447 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002448 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002449 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2450 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002451 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002452 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002453 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002454
Joe Perchesf96637b2013-05-04 22:12:25 -05002455 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2456 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002457
2458 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2459 io_parms.offset = rdata->offset;
2460 io_parms.length = rdata->bytes;
2461 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2462 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2463 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002464
2465 server = io_parms.tcon->ses->server;
2466
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002467 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002468 if (rc) {
2469 if (rc == -EAGAIN && rdata->credits) {
2470 /* credits was reset by reconnect */
2471 rdata->credits = 0;
2472 /* reduce in_flight value since we won't send the req */
2473 spin_lock(&server->req_lock);
2474 server->in_flight--;
2475 spin_unlock(&server->req_lock);
2476 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002477 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002478 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002479
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002480 if (encryption_required(io_parms.tcon))
2481 flags |= CIFS_TRANSFORM_REQ;
2482
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002483 req_len = cpu_to_be32(total_len);
2484
2485 rdata->iov[0].iov_base = &req_len;
2486 rdata->iov[0].iov_len = sizeof(__be32);
2487 rdata->iov[1].iov_base = buf;
2488 rdata->iov[1].iov_len = total_len;
2489
2490 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002491
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002492 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002493 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002494 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002495 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002496 spin_lock(&server->req_lock);
2497 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002498 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002499 spin_unlock(&server->req_lock);
2500 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002501 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002502 }
2503
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002504 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002505 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002506 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002507 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002508 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002509 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002510 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2511 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002512
2513 cifs_small_buf_release(buf);
2514 return rc;
2515}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002516
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002517int
2518SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2519 unsigned int *nbytes, char **buf, int *buf_type)
2520{
2521 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002522 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002523 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002524 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002525 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002526 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002527 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002528 __be32 req_len;
2529 struct smb_rqst rqst = { .rq_iov = iov,
2530 .rq_nvec = 2 };
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002531 int flags = CIFS_LOG_ERROR;
2532 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002533
2534 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002535 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002536 if (rc)
2537 return rc;
2538
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002539 if (encryption_required(io_parms->tcon))
2540 flags |= CIFS_TRANSFORM_REQ;
2541
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002542 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002543
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002544 iov[0].iov_base = &req_len;
2545 iov[0].iov_len = sizeof(__be32);
2546 iov[1].iov_base = req;
2547 iov[1].iov_len = total_len;
2548
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002549 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002550 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002551
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002552 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002553 shdr = get_sync_hdr(rsp);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002554
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002555 if (shdr->Status == STATUS_END_OF_FILE) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002556 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002557 return 0;
2558 }
2559
2560 if (rc) {
2561 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002562 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002563 } else {
2564 *nbytes = le32_to_cpu(rsp->DataLength);
2565 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2566 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002567 cifs_dbg(FYI, "bad length %d for count %d\n",
2568 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002569 rc = -EIO;
2570 *nbytes = 0;
2571 }
2572 }
2573
2574 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002575 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002576 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002577 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002578 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002579 if (resp_buftype == CIFS_SMALL_BUFFER)
2580 *buf_type = CIFS_SMALL_BUFFER;
2581 else if (resp_buftype == CIFS_LARGE_BUFFER)
2582 *buf_type = CIFS_LARGE_BUFFER;
2583 }
2584 return rc;
2585}
2586
Pavel Shilovsky33319142012-09-18 16:20:29 -07002587/*
2588 * Check the mid_state and signature on received buffer (if any), and queue the
2589 * workqueue completion task.
2590 */
2591static void
2592smb2_writev_callback(struct mid_q_entry *mid)
2593{
2594 struct cifs_writedata *wdata = mid->callback_data;
2595 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002596 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002597 unsigned int written;
2598 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2599 unsigned int credits_received = 1;
2600
2601 switch (mid->mid_state) {
2602 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002603 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002604 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2605 if (wdata->result != 0)
2606 break;
2607
2608 written = le32_to_cpu(rsp->DataLength);
2609 /*
2610 * Mask off high 16 bits when bytes written as returned
2611 * by the server is greater than bytes requested by the
2612 * client. OS/2 servers are known to set incorrect
2613 * CountHigh values.
2614 */
2615 if (written > wdata->bytes)
2616 written &= 0xFFFF;
2617
2618 if (written < wdata->bytes)
2619 wdata->result = -ENOSPC;
2620 else
2621 wdata->bytes = written;
2622 break;
2623 case MID_REQUEST_SUBMITTED:
2624 case MID_RETRY_NEEDED:
2625 wdata->result = -EAGAIN;
2626 break;
2627 default:
2628 wdata->result = -EIO;
2629 break;
2630 }
2631
2632 if (wdata->result)
2633 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2634
2635 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002636 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002637 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002638 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002639 add_credits(tcon->ses->server, credits_received, 0);
2640}
2641
2642/* smb2_async_writev - send an async write, and set up mid to handle result */
2643int
Steve French4a5c80d2014-02-07 20:45:12 -06002644smb2_async_writev(struct cifs_writedata *wdata,
2645 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002646{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002647 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002648 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002649 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002650 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002651 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002652 struct kvec iov[2];
2653 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002654
2655 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002656 if (rc) {
2657 if (rc == -EAGAIN && wdata->credits) {
2658 /* credits was reset by reconnect */
2659 wdata->credits = 0;
2660 /* reduce in_flight value since we won't send the req */
2661 spin_lock(&server->req_lock);
2662 server->in_flight--;
2663 spin_unlock(&server->req_lock);
2664 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002665 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002666 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002667
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002668 if (encryption_required(tcon))
2669 flags |= CIFS_TRANSFORM_REQ;
2670
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002671 shdr = get_sync_hdr(req);
2672 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002673
2674 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2675 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2676 req->WriteChannelInfoOffset = 0;
2677 req->WriteChannelInfoLength = 0;
2678 req->Channel = 0;
2679 req->Offset = cpu_to_le64(wdata->offset);
2680 /* 4 for rfc1002 length field */
2681 req->DataOffset = cpu_to_le16(
2682 offsetof(struct smb2_write_req, Buffer) - 4);
2683 req->RemainingBytes = 0;
2684
2685 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002686 iov[0].iov_len = 4;
2687 iov[0].iov_base = req;
2688 iov[1].iov_len = get_rfc1002_length(req) - 1;
2689 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002690
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002691 rqst.rq_iov = iov;
2692 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002693 rqst.rq_pages = wdata->pages;
2694 rqst.rq_npages = wdata->nr_pages;
2695 rqst.rq_pagesz = wdata->pagesz;
2696 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002697
Joe Perchesf96637b2013-05-04 22:12:25 -05002698 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2699 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002700
2701 req->Length = cpu_to_le32(wdata->bytes);
2702
2703 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2704
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002705 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002706 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002707 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002708 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002709 spin_lock(&server->req_lock);
2710 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002711 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002712 spin_unlock(&server->req_lock);
2713 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002714 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002715 }
2716
Pavel Shilovsky33319142012-09-18 16:20:29 -07002717 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002718 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2719 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002720
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002721 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002722 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002723 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2724 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002725
Pavel Shilovsky33319142012-09-18 16:20:29 -07002726async_writev_out:
2727 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002728 return rc;
2729}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002730
2731/*
2732 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2733 * The length field from io_parms must be at least 1 and indicates a number of
2734 * elements with data to write that begins with position 1 in iov array. All
2735 * data length is specified by count.
2736 */
2737int
2738SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2739 unsigned int *nbytes, struct kvec *iov, int n_vec)
2740{
2741 int rc = 0;
2742 struct smb2_write_req *req = NULL;
2743 struct smb2_write_rsp *rsp = NULL;
2744 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002745 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002746 int flags = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002747
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002748 *nbytes = 0;
2749
2750 if (n_vec < 1)
2751 return rc;
2752
2753 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2754 if (rc)
2755 return rc;
2756
2757 if (io_parms->tcon->ses->server == NULL)
2758 return -ECONNABORTED;
2759
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002760 if (encryption_required(io_parms->tcon))
2761 flags |= CIFS_TRANSFORM_REQ;
2762
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002763 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002764
2765 req->PersistentFileId = io_parms->persistent_fid;
2766 req->VolatileFileId = io_parms->volatile_fid;
2767 req->WriteChannelInfoOffset = 0;
2768 req->WriteChannelInfoLength = 0;
2769 req->Channel = 0;
2770 req->Length = cpu_to_le32(io_parms->length);
2771 req->Offset = cpu_to_le64(io_parms->offset);
2772 /* 4 for rfc1002 length field */
2773 req->DataOffset = cpu_to_le16(
2774 offsetof(struct smb2_write_req, Buffer) - 4);
2775 req->RemainingBytes = 0;
2776
2777 iov[0].iov_base = (char *)req;
2778 /* 4 for rfc1002 length field and 1 for Buffer */
2779 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2780
2781 /* length of entire message including data to be written */
2782 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2783
2784 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002785 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002786 cifs_small_buf_release(req);
2787 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002788
2789 if (rc) {
2790 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002791 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002792 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002793 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002794
2795 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002796 return rc;
2797}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002798
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002799static unsigned int
2800num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2801{
2802 int len;
2803 unsigned int entrycount = 0;
2804 unsigned int next_offset = 0;
2805 FILE_DIRECTORY_INFO *entryptr;
2806
2807 if (bufstart == NULL)
2808 return 0;
2809
2810 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2811
2812 while (1) {
2813 entryptr = (FILE_DIRECTORY_INFO *)
2814 ((char *)entryptr + next_offset);
2815
2816 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002817 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002818 break;
2819 }
2820
2821 len = le32_to_cpu(entryptr->FileNameLength);
2822 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002823 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2824 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002825 break;
2826 }
2827
2828 *lastentry = (char *)entryptr;
2829 entrycount++;
2830
2831 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2832 if (!next_offset)
2833 break;
2834 }
2835
2836 return entrycount;
2837}
2838
2839/*
2840 * Readdir/FindFirst
2841 */
2842int
2843SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2844 u64 persistent_fid, u64 volatile_fid, int index,
2845 struct cifs_search_info *srch_inf)
2846{
2847 struct smb2_query_directory_req *req;
2848 struct smb2_query_directory_rsp *rsp = NULL;
2849 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002850 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002851 int rc = 0;
2852 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002853 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002854 unsigned char *bufptr;
2855 struct TCP_Server_Info *server;
2856 struct cifs_ses *ses = tcon->ses;
2857 __le16 asteriks = cpu_to_le16('*');
2858 char *end_of_smb;
2859 unsigned int output_size = CIFSMaxBufSize;
2860 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002861 int flags = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002862
2863 if (ses && (ses->server))
2864 server = ses->server;
2865 else
2866 return -EIO;
2867
2868 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2869 if (rc)
2870 return rc;
2871
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002872 if (encryption_required(tcon))
2873 flags |= CIFS_TRANSFORM_REQ;
2874
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002875 switch (srch_inf->info_level) {
2876 case SMB_FIND_FILE_DIRECTORY_INFO:
2877 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2878 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2879 break;
2880 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2881 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2882 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2883 break;
2884 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002885 cifs_dbg(VFS, "info level %u isn't supported\n",
2886 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002887 rc = -EINVAL;
2888 goto qdir_exit;
2889 }
2890
2891 req->FileIndex = cpu_to_le32(index);
2892 req->PersistentFileId = persistent_fid;
2893 req->VolatileFileId = volatile_fid;
2894
2895 len = 0x2;
2896 bufptr = req->Buffer;
2897 memcpy(bufptr, &asteriks, len);
2898
2899 req->FileNameOffset =
2900 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2901 req->FileNameLength = cpu_to_le16(len);
2902 /*
2903 * BB could be 30 bytes or so longer if we used SMB2 specific
2904 * buffer lengths, but this is safe and close enough.
2905 */
2906 output_size = min_t(unsigned int, output_size, server->maxBuf);
2907 output_size = min_t(unsigned int, output_size, 2 << 15);
2908 req->OutputBufferLength = cpu_to_le32(output_size);
2909
2910 iov[0].iov_base = (char *)req;
2911 /* 4 for RFC1001 length and 1 for Buffer */
2912 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2913
2914 iov[1].iov_base = (char *)(req->Buffer);
2915 iov[1].iov_len = len;
2916
2917 inc_rfc1001_len(req, len - 1 /* Buffer */);
2918
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002919 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002920 cifs_small_buf_release(req);
2921 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002922
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002923 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002924 if (rc == -ENODATA &&
2925 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002926 srch_inf->endOfSearch = true;
2927 rc = 0;
2928 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002929 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2930 goto qdir_exit;
2931 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002932
2933 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2934 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2935 info_buf_size);
2936 if (rc)
2937 goto qdir_exit;
2938
2939 srch_inf->unicode = true;
2940
2941 if (srch_inf->ntwrk_buf_start) {
2942 if (srch_inf->smallBuf)
2943 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2944 else
2945 cifs_buf_release(srch_inf->ntwrk_buf_start);
2946 }
2947 srch_inf->ntwrk_buf_start = (char *)rsp;
2948 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2949 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2950 /* 4 for rfc1002 length field */
2951 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2952 srch_inf->entries_in_buffer =
2953 num_entries(srch_inf->srch_entries_start, end_of_smb,
2954 &srch_inf->last_entry, info_buf_size);
2955 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002956 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2957 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2958 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002959 if (resp_buftype == CIFS_LARGE_BUFFER)
2960 srch_inf->smallBuf = false;
2961 else if (resp_buftype == CIFS_SMALL_BUFFER)
2962 srch_inf->smallBuf = true;
2963 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002964 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002965
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002966 return rc;
2967
2968qdir_exit:
2969 free_rsp_buf(resp_buftype, rsp);
2970 return rc;
2971}
2972
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002973static int
2974send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002975 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002976 unsigned int num, void **data, unsigned int *size)
2977{
2978 struct smb2_set_info_req *req;
2979 struct smb2_set_info_rsp *rsp = NULL;
2980 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002981 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002982 int rc = 0;
2983 int resp_buftype;
2984 unsigned int i;
2985 struct TCP_Server_Info *server;
2986 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002987 int flags = 0;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002988
2989 if (ses && (ses->server))
2990 server = ses->server;
2991 else
2992 return -EIO;
2993
2994 if (!num)
2995 return -EINVAL;
2996
2997 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2998 if (!iov)
2999 return -ENOMEM;
3000
3001 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3002 if (rc) {
3003 kfree(iov);
3004 return rc;
3005 }
3006
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003007 if (encryption_required(tcon))
3008 flags |= CIFS_TRANSFORM_REQ;
3009
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003010 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003011
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003012 req->InfoType = SMB2_O_INFO_FILE;
3013 req->FileInfoClass = info_class;
3014 req->PersistentFileId = persistent_fid;
3015 req->VolatileFileId = volatile_fid;
3016
3017 /* 4 for RFC1001 length and 1 for Buffer */
3018 req->BufferOffset =
3019 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3020 req->BufferLength = cpu_to_le32(*size);
3021
3022 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3023
3024 memcpy(req->Buffer, *data, *size);
3025
3026 iov[0].iov_base = (char *)req;
3027 /* 4 for RFC1001 length */
3028 iov[0].iov_len = get_rfc1002_length(req) + 4;
3029
3030 for (i = 1; i < num; i++) {
3031 inc_rfc1001_len(req, size[i]);
3032 le32_add_cpu(&req->BufferLength, size[i]);
3033 iov[i].iov_base = (char *)data[i];
3034 iov[i].iov_len = size[i];
3035 }
3036
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003037 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003038 cifs_small_buf_release(req);
3039 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003040
Steve French7d3fb242013-11-18 09:56:28 -06003041 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003042 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003043
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003044 free_rsp_buf(resp_buftype, rsp);
3045 kfree(iov);
3046 return rc;
3047}
3048
3049int
3050SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3051 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3052{
3053 struct smb2_file_rename_info info;
3054 void **data;
3055 unsigned int size[2];
3056 int rc;
3057 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3058
3059 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3060 if (!data)
3061 return -ENOMEM;
3062
3063 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3064 /* 0 = fail if target already exists */
3065 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3066 info.FileNameLength = cpu_to_le32(len);
3067
3068 data[0] = &info;
3069 size[0] = sizeof(struct smb2_file_rename_info);
3070
3071 data[1] = target_file;
3072 size[1] = len + 2 /* null */;
3073
3074 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003075 current->tgid, FILE_RENAME_INFORMATION, 2, data,
3076 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003077 kfree(data);
3078 return rc;
3079}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003080
3081int
Steve French897fba12016-05-12 21:20:36 -05003082SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3083 u64 persistent_fid, u64 volatile_fid)
3084{
3085 __u8 delete_pending = 1;
3086 void *data;
3087 unsigned int size;
3088
3089 data = &delete_pending;
3090 size = 1; /* sizeof __u8 */
3091
3092 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3093 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
3094 &size);
3095}
3096
3097int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003098SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3099 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3100{
3101 struct smb2_file_link_info info;
3102 void **data;
3103 unsigned int size[2];
3104 int rc;
3105 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3106
3107 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3108 if (!data)
3109 return -ENOMEM;
3110
3111 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3112 /* 0 = fail if link already exists */
3113 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3114 info.FileNameLength = cpu_to_le32(len);
3115
3116 data[0] = &info;
3117 size[0] = sizeof(struct smb2_file_link_info);
3118
3119 data[1] = target_file;
3120 size[1] = len + 2 /* null */;
3121
3122 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003123 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003124 kfree(data);
3125 return rc;
3126}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003127
3128int
3129SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003130 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003131{
3132 struct smb2_file_eof_info info;
3133 void *data;
3134 unsigned int size;
3135
3136 info.EndOfFile = *eof;
3137
3138 data = &info;
3139 size = sizeof(struct smb2_file_eof_info);
3140
Steve Frenchf29ebb42014-07-19 21:44:58 -05003141 if (is_falloc)
3142 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3143 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
3144 else
3145 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3146 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003147}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003148
3149int
3150SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3151 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3152{
3153 unsigned int size;
3154 size = sizeof(FILE_BASIC_INFO);
3155 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3156 current->tgid, FILE_BASIC_INFORMATION, 1,
3157 (void **)&buf, &size);
3158}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003159
3160int
3161SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3162 const u64 persistent_fid, const u64 volatile_fid,
3163 __u8 oplock_level)
3164{
3165 int rc;
3166 struct smb2_oplock_break *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003167 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003168
Joe Perchesf96637b2013-05-04 22:12:25 -05003169 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003170 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003171 if (rc)
3172 return rc;
3173
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003174 if (encryption_required(tcon))
3175 flags |= CIFS_TRANSFORM_REQ;
3176
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003177 req->VolatileFid = volatile_fid;
3178 req->PersistentFid = persistent_fid;
3179 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003180 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003181
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003182 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003183 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003184
3185 if (rc) {
3186 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003187 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003188 }
3189
3190 return rc;
3191}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003192
3193static void
3194copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3195 struct kstatfs *kst)
3196{
3197 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3198 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3199 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3200 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
3201 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3202 return;
3203}
3204
3205static int
3206build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3207 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3208{
3209 int rc;
3210 struct smb2_query_info_req *req;
3211
Joe Perchesf96637b2013-05-04 22:12:25 -05003212 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003213
3214 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3215 return -EIO;
3216
3217 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3218 if (rc)
3219 return rc;
3220
3221 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3222 req->FileInfoClass = level;
3223 req->PersistentFileId = persistent_fid;
3224 req->VolatileFileId = volatile_fid;
3225 /* 4 for rfc1002 length field and 1 for pad */
3226 req->InputBufferOffset =
3227 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3228 req->OutputBufferLength = cpu_to_le32(
3229 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3230
3231 iov->iov_base = (char *)req;
3232 /* 4 for rfc1002 length field */
3233 iov->iov_len = get_rfc1002_length(req) + 4;
3234 return 0;
3235}
3236
3237int
3238SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3239 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3240{
3241 struct smb2_query_info_rsp *rsp = NULL;
3242 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003243 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003244 int rc = 0;
3245 int resp_buftype;
3246 struct cifs_ses *ses = tcon->ses;
3247 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003248 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003249
3250 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3251 sizeof(struct smb2_fs_full_size_info),
3252 persistent_fid, volatile_fid);
3253 if (rc)
3254 return rc;
3255
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003256 if (encryption_required(tcon))
3257 flags |= CIFS_TRANSFORM_REQ;
3258
3259 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003260 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003261 if (rc) {
3262 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003263 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003264 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003265 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003266
3267 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3268 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3269 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3270 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3271 sizeof(struct smb2_fs_full_size_info));
3272 if (!rc)
3273 copy_fs_info_to_kstatfs(info, fsdata);
3274
Steve French34f62642013-10-09 02:07:00 -05003275qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003276 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003277 return rc;
3278}
3279
3280int
3281SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003282 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003283{
3284 struct smb2_query_info_rsp *rsp = NULL;
3285 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003286 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003287 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003288 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003289 struct cifs_ses *ses = tcon->ses;
3290 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003291 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003292
Steven French21671142013-10-09 13:36:35 -05003293 if (level == FS_DEVICE_INFORMATION) {
3294 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3295 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3296 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3297 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3298 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003299 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3300 max_len = sizeof(struct smb3_fs_ss_info);
3301 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003302 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003303 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003304 return -EINVAL;
3305 }
3306
3307 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003308 persistent_fid, volatile_fid);
3309 if (rc)
3310 return rc;
3311
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003312 if (encryption_required(tcon))
3313 flags |= CIFS_TRANSFORM_REQ;
3314
3315 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003316 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003317 if (rc) {
3318 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3319 goto qfsattr_exit;
3320 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003321 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003322
3323 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3324 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003325 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3326 if (rc)
3327 goto qfsattr_exit;
3328
3329 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003330 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3331 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003332 rsp_len, max_len));
3333 else if (level == FS_DEVICE_INFORMATION)
3334 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3335 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003336 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3337 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3338 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3339 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3340 tcon->perf_sector_size =
3341 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3342 }
Steve French34f62642013-10-09 02:07:00 -05003343
3344qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003345 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003346 return rc;
3347}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003348
3349int
3350smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3351 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3352 const __u32 num_lock, struct smb2_lock_element *buf)
3353{
3354 int rc = 0;
3355 struct smb2_lock_req *req = NULL;
3356 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003357 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003358 int resp_buf_type;
3359 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003360 int flags = CIFS_NO_RESP;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003361
Joe Perchesf96637b2013-05-04 22:12:25 -05003362 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003363
3364 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3365 if (rc)
3366 return rc;
3367
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003368 if (encryption_required(tcon))
3369 flags |= CIFS_TRANSFORM_REQ;
3370
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003371 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003372 req->LockCount = cpu_to_le16(num_lock);
3373
3374 req->PersistentFileId = persist_fid;
3375 req->VolatileFileId = volatile_fid;
3376
3377 count = num_lock * sizeof(struct smb2_lock_element);
3378 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3379
3380 iov[0].iov_base = (char *)req;
3381 /* 4 for rfc1002 length field and count for all locks */
3382 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3383 iov[1].iov_base = (char *)buf;
3384 iov[1].iov_len = count;
3385
3386 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003387 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003388 &rsp_iov);
3389 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003390 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003391 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003392 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3393 }
3394
3395 return rc;
3396}
3397
3398int
3399SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3400 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3401 const __u64 length, const __u64 offset, const __u32 lock_flags,
3402 const bool wait)
3403{
3404 struct smb2_lock_element lock;
3405
3406 lock.Offset = cpu_to_le64(offset);
3407 lock.Length = cpu_to_le64(length);
3408 lock.Flags = cpu_to_le32(lock_flags);
3409 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3410 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3411
3412 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3413}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003414
3415int
3416SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3417 __u8 *lease_key, const __le32 lease_state)
3418{
3419 int rc;
3420 struct smb2_lease_ack *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003421 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003422
Joe Perchesf96637b2013-05-04 22:12:25 -05003423 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003424 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003425 if (rc)
3426 return rc;
3427
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003428 if (encryption_required(tcon))
3429 flags |= CIFS_TRANSFORM_REQ;
3430
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003431 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003432 req->StructureSize = cpu_to_le16(36);
3433 inc_rfc1001_len(req, 12);
3434
3435 memcpy(req->LeaseKey, lease_key, 16);
3436 req->LeaseState = lease_state;
3437
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003438 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003439 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003440
3441 if (rc) {
3442 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003443 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003444 }
3445
3446 return rc;
3447}