blob: 5531e7ee1210eba48915a6a0d4d01b348b386cde [file] [log] [blame]
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001/*
2 * fs/cifs/smb2pdu.c
3 *
Steve French2b80d042013-06-23 18:43:37 -05004 * Copyright (C) International Business Machines Corp., 2009, 2013
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
Pavel Shilovsky09a47072012-09-18 16:20:29 -070034#include <linux/task_io_accounting_ops.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040035#include <linux/uaccess.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020036#include <linux/uuid.h>
Pavel Shilovsky33319142012-09-18 16:20:29 -070037#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040038#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070048#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070049#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050050#include "cifs_spnego.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040051
52/*
53 * The following table defines the expected "StructureSize" of SMB2 requests
54 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
55 *
56 * Note that commands are defined in smb2pdu.h in le16 but the array below is
57 * indexed by command in host byte order.
58 */
59static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
60 /* SMB2_NEGOTIATE */ 36,
61 /* SMB2_SESSION_SETUP */ 25,
62 /* SMB2_LOGOFF */ 4,
63 /* SMB2_TREE_CONNECT */ 9,
64 /* SMB2_TREE_DISCONNECT */ 4,
65 /* SMB2_CREATE */ 57,
66 /* SMB2_CLOSE */ 24,
67 /* SMB2_FLUSH */ 24,
68 /* SMB2_READ */ 49,
69 /* SMB2_WRITE */ 49,
70 /* SMB2_LOCK */ 48,
71 /* SMB2_IOCTL */ 57,
72 /* SMB2_CANCEL */ 4,
73 /* SMB2_ECHO */ 4,
74 /* SMB2_QUERY_DIRECTORY */ 33,
75 /* SMB2_CHANGE_NOTIFY */ 32,
76 /* SMB2_QUERY_INFO */ 41,
77 /* SMB2_SET_INFO */ 33,
78 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
79};
80
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070081static int encryption_required(const struct cifs_tcon *tcon)
82{
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080083 if (!tcon)
84 return 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070085 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
86 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
87 return 1;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -080088 if (tcon->seal &&
89 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
90 return 1;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -070091 return 0;
92}
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040093
94static void
Pavel Shilovskycb200bd2016-10-24 16:59:57 -070095smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040096 const struct cifs_tcon *tcon)
97{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -070098 shdr->ProtocolId = SMB2_PROTO_NUMBER;
99 shdr->StructureSize = cpu_to_le16(64);
100 shdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100101 if (tcon && tcon->ses && tcon->ses->server) {
102 struct TCP_Server_Info *server = tcon->ses->server;
103
104 spin_lock(&server->req_lock);
105 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500106 if (server->credits >= server->max_credits)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700107 shdr->CreditRequest = cpu_to_le16(0);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100108 else
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700109 shdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500110 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100111 server->credits, 2));
112 spin_unlock(&server->req_lock);
113 } else {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700114 shdr->CreditRequest = cpu_to_le16(2);
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100115 }
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700116 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400117
118 if (!tcon)
119 goto out;
120
Steve French2b80d042013-06-23 18:43:37 -0500121 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
122 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500123 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500124 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700125 shdr->CreditCharge = cpu_to_le16(1);
Steve French2b80d042013-06-23 18:43:37 -0500126 /* else CreditCharge MBZ */
127
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700128 shdr->TreeId = tcon->tid;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400129 /* Uid is not converted */
130 if (tcon->ses)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700131 shdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500132
133 /*
134 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
135 * to pass the path on the Open SMB prefixed by \\server\share.
136 * Not sure when we would need to do the augmented path (if ever) and
137 * setting this flag breaks the SMB2 open operation since it is
138 * illegal to send an empty path name (without \\server\share prefix)
139 * when the DFS flag is set in the SMB open header. We could
140 * consider setting the flag on all operations other than open
141 * but it is safer to net set it for now.
142 */
143/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700144 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
Steve Frenchf87ab882013-06-26 19:14:55 -0500145
Pavel Shilovsky7fb89862016-10-31 13:49:30 -0700146 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
147 !encryption_required(tcon))
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700148 shdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400149out:
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400150 return;
151}
152
153static int
154smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
155{
156 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400157 struct nls_table *nls_codepage;
158 struct cifs_ses *ses;
159 struct TCP_Server_Info *server;
160
161 /*
162 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
163 * check for tcp and smb session status done differently
164 * for those three - in the calling routine.
165 */
166 if (tcon == NULL)
167 return rc;
168
169 if (smb2_command == SMB2_TREE_CONNECT)
170 return rc;
171
172 if (tcon->tidStatus == CifsExiting) {
173 /*
174 * only tree disconnect, open, and write,
175 * (and ulogoff which does not have tcon)
176 * are allowed as we start force umount.
177 */
178 if ((smb2_command != SMB2_WRITE) &&
179 (smb2_command != SMB2_CREATE) &&
180 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500181 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
182 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400183 return -ENODEV;
184 }
185 }
186 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
187 (!tcon->ses->server))
188 return -EIO;
189
190 ses = tcon->ses;
191 server = ses->server;
192
193 /*
194 * Give demultiplex thread up to 10 seconds to reconnect, should be
195 * greater than cifs socket timeout which is 7 seconds
196 */
197 while (server->tcpStatus == CifsNeedReconnect) {
198 /*
199 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
200 * here since they are implicitly done when session drops.
201 */
202 switch (smb2_command) {
203 /*
204 * BB Should we keep oplock break and add flush to exceptions?
205 */
206 case SMB2_TREE_DISCONNECT:
207 case SMB2_CANCEL:
208 case SMB2_CLOSE:
209 case SMB2_OPLOCK_BREAK:
210 return -EAGAIN;
211 }
212
213 wait_event_interruptible_timeout(server->response_q,
214 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
215
216 /* are we still trying to reconnect? */
217 if (server->tcpStatus != CifsNeedReconnect)
218 break;
219
220 /*
221 * on "soft" mounts we wait once. Hard mounts keep
222 * retrying until process is killed or server comes
223 * back on-line
224 */
225 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500226 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400227 return -EHOSTDOWN;
228 }
229 }
230
231 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
232 return rc;
233
234 nls_codepage = load_nls_default();
235
236 /*
237 * need to prevent multiple threads trying to simultaneously reconnect
238 * the same SMB session
239 */
240 mutex_lock(&tcon->ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200241
242 /*
243 * Recheck after acquire mutex. If another thread is negotiating
244 * and the server never sends an answer the socket will be closed
245 * and tcpStatus set to reconnect.
246 */
247 if (server->tcpStatus == CifsNeedReconnect) {
248 rc = -EHOSTDOWN;
249 mutex_unlock(&tcon->ses->session_mutex);
250 goto out;
251 }
252
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400253 rc = cifs_negotiate_protocol(0, tcon->ses);
254 if (!rc && tcon->ses->need_reconnect)
255 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
256
257 if (rc || !tcon->need_reconnect) {
258 mutex_unlock(&tcon->ses->session_mutex);
259 goto out;
260 }
261
262 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800263 if (tcon->use_persistent)
264 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500265
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400266 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
267 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500268
Joe Perchesf96637b2013-05-04 22:12:25 -0500269 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400270 if (rc)
271 goto out;
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800272
273 if (smb2_command != SMB2_INTERNAL_CMD)
274 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
275
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400276 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400277out:
278 /*
279 * Check if handle based operation so we know whether we can continue
280 * or not without returning to caller to reset file handle.
281 */
282 /*
283 * BB Is flush done by server on drop of tcp session? Should we special
284 * case it and skip above?
285 */
286 switch (smb2_command) {
287 case SMB2_FLUSH:
288 case SMB2_READ:
289 case SMB2_WRITE:
290 case SMB2_LOCK:
291 case SMB2_IOCTL:
292 case SMB2_QUERY_DIRECTORY:
293 case SMB2_CHANGE_NOTIFY:
294 case SMB2_QUERY_INFO:
295 case SMB2_SET_INFO:
Pavel Shilovsky4772c792016-11-29 11:30:58 -0800296 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400297 }
298 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400299 return rc;
300}
301
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700302static void
303fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
304 unsigned int *total_len)
305{
306 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
307 /* lookup word count ie StructureSize from table */
308 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
309
310 /*
311 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
312 * largest operations (Create)
313 */
314 memset(buf, 0, 256);
315
316 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
317 spdu->StructureSize2 = cpu_to_le16(parmsize);
318
319 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
320}
321
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800322/* init request without RFC1001 length at the beginning */
323static int
324smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
325 void **request_buf, unsigned int *total_len)
326{
327 int rc;
328 struct smb2_sync_hdr *shdr;
329
330 rc = smb2_reconnect(smb2_command, tcon);
331 if (rc)
332 return rc;
333
334 /* BB eventually switch this to SMB2 specific small buf size */
335 *request_buf = cifs_small_buf_get();
336 if (*request_buf == NULL) {
337 /* BB should we add a retry in here if not a writepage? */
338 return -ENOMEM;
339 }
340
341 shdr = (struct smb2_sync_hdr *)(*request_buf);
342
343 fill_small_buf(smb2_command, tcon, shdr, total_len);
344
345 if (tcon != NULL) {
346#ifdef CONFIG_CIFS_STATS2
347 uint16_t com_code = le16_to_cpu(smb2_command);
348
349 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
350#endif
351 cifs_stats_inc(&tcon->num_smbs_sent);
352 }
353
354 return rc;
355}
356
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400357/*
358 * Allocate and return pointer to an SMB request hdr, and set basic
359 * SMB information in the SMB header. If the return code is zero, this
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -0800360 * function must have filled in request_buf pointer. The returned buffer
361 * has RFC1001 length at the beginning.
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400362 */
363static int
364small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
365 void **request_buf)
366{
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700367 int rc;
368 unsigned int total_len;
369 struct smb2_pdu *pdu;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400370
371 rc = smb2_reconnect(smb2_command, tcon);
372 if (rc)
373 return rc;
374
375 /* BB eventually switch this to SMB2 specific small buf size */
376 *request_buf = cifs_small_buf_get();
377 if (*request_buf == NULL) {
378 /* BB should we add a retry in here if not a writepage? */
379 return -ENOMEM;
380 }
381
Pavel Shilovskycb200bd2016-10-24 16:59:57 -0700382 pdu = (struct smb2_pdu *)(*request_buf);
383
384 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
385
386 /* Note this is only network field converted to big endian */
387 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400388
389 if (tcon != NULL) {
390#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400391 uint16_t com_code = le16_to_cpu(smb2_command);
392 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400393#endif
394 cifs_stats_inc(&tcon->num_smbs_sent);
395 }
396
397 return rc;
398}
399
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500400#ifdef CONFIG_CIFS_SMB311
401/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
402#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
403
404
405#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
406#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
407
408static void
409build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
410{
411 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
412 pneg_ctxt->DataLength = cpu_to_le16(38);
413 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
414 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
415 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
416 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
417}
418
419static void
420build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
421{
422 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
423 pneg_ctxt->DataLength = cpu_to_le16(6);
424 pneg_ctxt->CipherCount = cpu_to_le16(2);
425 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
426 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
427}
428
429static void
430assemble_neg_contexts(struct smb2_negotiate_req *req)
431{
432
433 /* +4 is to account for the RFC1001 len field */
434 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
435
436 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
437 /* Add 2 to size to round to 8 byte boundary */
438 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
439 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
440 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
441 req->NegotiateContextCount = cpu_to_le16(2);
442 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
443 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
444}
445#else
446static void assemble_neg_contexts(struct smb2_negotiate_req *req)
447{
448 return;
449}
450#endif /* SMB311 */
451
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400452/*
453 *
454 * SMB2 Worker functions follow:
455 *
456 * The general structure of the worker functions is:
457 * 1) Call smb2_init (assembles SMB2 header)
458 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
459 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
460 * 4) Decode SMB2 command specific fields in the fixed length area
461 * 5) Decode variable length data area (if any for this SMB2 command type)
462 * 6) Call free smb buffer
463 * 7) return
464 *
465 */
466
467int
468SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
469{
470 struct smb2_negotiate_req *req;
471 struct smb2_negotiate_rsp *rsp;
472 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700473 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400474 int rc = 0;
475 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400476 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400477 int blob_offset, blob_length;
478 char *security_blob;
479 int flags = CIFS_NEG_OP;
480
Joe Perchesf96637b2013-05-04 22:12:25 -0500481 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400482
Jeff Layton3534b852013-05-24 07:41:01 -0400483 if (!server) {
484 WARN(1, "%s: server is NULL!\n", __func__);
485 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400486 }
487
488 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
489 if (rc)
490 return rc;
491
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700492 req->hdr.sync_hdr.SessionId = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400493
Steve Frenche4aa25e2012-10-01 12:26:22 -0500494 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400495
Steve Frenche4aa25e2012-10-01 12:26:22 -0500496 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
497 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400498
499 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400500 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500501 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400502 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500503 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400504 else
505 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400506
Steve Frenche4aa25e2012-10-01 12:26:22 -0500507 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400508
Steve French3c5f9be12014-05-13 13:37:45 -0700509 /* ClientGUID must be zero for SMB2.02 dialect */
510 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
511 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500512 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700513 memcpy(req->ClientGUID, server->client_guid,
514 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500515 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
516 assemble_neg_contexts(req);
517 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400518 iov[0].iov_base = (char *)req;
519 /* 4 for rfc1002 length field */
520 iov[0].iov_len = get_rfc1002_length(req) + 4;
521
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700522 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
523 cifs_small_buf_release(req);
524 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400525 /*
526 * No tcon so can't do
527 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
528 */
Steve French7e682f72017-08-31 21:34:24 -0500529 if (rc == -EOPNOTSUPP) {
530 cifs_dbg(VFS, "Dialect not supported by server. Consider "
531 "specifying vers=1.0 or vers=2.1 on mount for accessing"
532 " older servers\n");
533 goto neg_exit;
534 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400535 goto neg_exit;
536
Joe Perchesf96637b2013-05-04 22:12:25 -0500537 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400538
Steve Frenche4aa25e2012-10-01 12:26:22 -0500539 /* BB we may eventually want to match the negotiated vs. requested
540 dialect, even though we are only requesting one at a time */
541 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500542 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500543 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500544 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500545 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500546 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500547 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
548 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600549#ifdef CONFIG_CIFS_SMB311
550 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
551 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
552#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400553 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500554 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500555 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400556 rc = -EIO;
557 goto neg_exit;
558 }
559 server->dialect = le16_to_cpu(rsp->DialectRevision);
560
Jeff Laytone598d1d82013-05-26 07:00:59 -0400561 /* SMB2 only has an extended negflavor */
562 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400563 /* set it to the maximum buffer size value we can send with 1 credit */
564 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
565 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400566 server->max_read = le32_to_cpu(rsp->MaxReadSize);
567 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
568 /* BB Do we need to validate the SecurityMode? */
569 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
570 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400571 /* Internal types */
572 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400573
574 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
575 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500576 /*
577 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
578 * for us will be
579 * ses->sectype = RawNTLMSSP;
580 * but for time being this is our only auth choice so doesn't matter.
581 * We just found a server which sets blob length to zero expecting raw.
582 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700583 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500584 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700585 server->sec_ntlmssp = true;
586 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700587
Jeff Layton38d77c52013-05-26 07:01:00 -0400588 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400589 if (rc)
590 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500591 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500592 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500593 if (rc == 1)
594 rc = 0;
595 else if (rc == 0)
596 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400597 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400598neg_exit:
599 free_rsp_buf(resp_buftype, rsp);
600 return rc;
601}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400602
Steve Frenchff1c0382013-11-19 23:44:46 -0600603int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
604{
605 int rc = 0;
606 struct validate_negotiate_info_req vneg_inbuf;
607 struct validate_negotiate_info_rsp *pneg_rsp;
608 u32 rsplen;
609
610 cifs_dbg(FYI, "validate negotiate\n");
611
612 /*
613 * validation ioctl must be signed, so no point sending this if we
614 * can not sign it. We could eventually change this to selectively
615 * sign just this, the first and only signed request on a connection.
616 * This is good enough for now since a user who wants better security
617 * would also enable signing on the mount. Having validation of
618 * negotiate info for signed connections helps reduce attack vectors
619 */
620 if (tcon->ses->server->sign == false)
621 return 0; /* validation requires signing */
622
623 vneg_inbuf.Capabilities =
624 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100625 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
626 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600627
628 if (tcon->ses->sign)
629 vneg_inbuf.SecurityMode =
630 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
631 else if (global_secflags & CIFSSEC_MAY_SIGN)
632 vneg_inbuf.SecurityMode =
633 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
634 else
635 vneg_inbuf.SecurityMode = 0;
636
637 vneg_inbuf.DialectCount = cpu_to_le16(1);
638 vneg_inbuf.Dialects[0] =
639 cpu_to_le16(tcon->ses->server->vals->protocol_id);
640
641 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
642 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100643 false /* use_ipc */,
Steve Frenchff1c0382013-11-19 23:44:46 -0600644 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
645 (char **)&pneg_rsp, &rsplen);
646
647 if (rc != 0) {
648 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
649 return -EIO;
650 }
651
652 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500653 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
654 rsplen);
655
656 /* relax check since Mac returns max bufsize allowed on ioctl */
657 if (rsplen > CIFSMaxBufSize)
658 return -EIO;
Steve Frenchff1c0382013-11-19 23:44:46 -0600659 }
660
661 /* check validate negotiate info response matches what we got earlier */
662 if (pneg_rsp->Dialect !=
663 cpu_to_le16(tcon->ses->server->vals->protocol_id))
664 goto vneg_out;
665
666 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
667 goto vneg_out;
668
669 /* do not validate server guid because not saved at negprot time yet */
670
671 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
672 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
673 goto vneg_out;
674
675 /* validate negotiate successful */
676 cifs_dbg(FYI, "validate negotiate info successful\n");
677 return 0;
678
679vneg_out:
680 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
681 return -EIO;
682}
683
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530684enum securityEnum
685smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
686{
687 switch (requested) {
688 case Kerberos:
689 case RawNTLMSSP:
690 return requested;
691 case NTLMv2:
692 return RawNTLMSSP;
693 case Unspecified:
694 if (server->sec_ntlmssp &&
695 (global_secflags & CIFSSEC_MAY_NTLMSSP))
696 return RawNTLMSSP;
697 if ((server->sec_kerberos || server->sec_mskerberos) &&
698 (global_secflags & CIFSSEC_MAY_KRB5))
699 return Kerberos;
700 /* Fallthrough */
701 default:
702 return Unspecified;
703 }
704}
705
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100706struct SMB2_sess_data {
707 unsigned int xid;
708 struct cifs_ses *ses;
709 struct nls_table *nls_cp;
710 void (*func)(struct SMB2_sess_data *);
711 int result;
712 u64 previous_session;
713
714 /* we will send the SMB in three pieces:
715 * a fixed length beginning part, an optional
716 * SPNEGO blob (which can be zero length), and a
717 * last part which will include the strings
718 * and rest of bcc area. This allows us to avoid
719 * a large buffer 17K allocation
720 */
721 int buf0_type;
722 struct kvec iov[2];
723};
724
725static int
726SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
727{
728 int rc;
729 struct cifs_ses *ses = sess_data->ses;
730 struct smb2_sess_setup_req *req;
731 struct TCP_Server_Info *server = ses->server;
732
733 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
734 if (rc)
735 return rc;
736
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700737 /* First session, not a reauthenticate */
738 req->hdr.sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100739
740 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
741 req->PreviousSessionId = sess_data->previous_session;
742
743 req->Flags = 0; /* MBZ */
744 /* to enable echos and oplocks */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700745 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100746
747 /* only one of SMB2 signing flags may be set in SMB2 request */
748 if (server->sign)
749 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
750 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
751 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
752 else
753 req->SecurityMode = 0;
754
755 req->Capabilities = 0;
756 req->Channel = 0; /* MBZ */
757
758 sess_data->iov[0].iov_base = (char *)req;
759 /* 4 for rfc1002 length field and 1 for pad */
760 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
761 /*
762 * This variable will be used to clear the buffer
763 * allocated above in case of any error in the calling function.
764 */
765 sess_data->buf0_type = CIFS_SMALL_BUFFER;
766
767 return 0;
768}
769
770static void
771SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
772{
773 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
774 sess_data->buf0_type = CIFS_NO_BUFFER;
775}
776
777static int
778SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
779{
780 int rc;
781 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700782 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100783
784 /* Testing shows that buffer offset must be at location of Buffer[0] */
785 req->SecurityBufferOffset =
786 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
787 1 /* pad */ - 4 /* rfc1001 len */);
788 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
789
790 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
791
792 /* BB add code to build os and lm fields */
793
794 rc = SendReceive2(sess_data->xid, sess_data->ses,
795 sess_data->iov, 2,
796 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700797 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
798 cifs_small_buf_release(sess_data->iov[0].iov_base);
799 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100800
801 return rc;
802}
803
804static int
805SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
806{
807 int rc = 0;
808 struct cifs_ses *ses = sess_data->ses;
809
810 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800811 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100812 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100813 if (rc) {
814 cifs_dbg(FYI,
815 "SMB3 session key generation failed\n");
816 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800817 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100818 }
819 }
820 if (!ses->server->session_estab) {
821 ses->server->sequence_number = 0x2;
822 ses->server->session_estab = true;
823 }
824 mutex_unlock(&ses->server->srv_mutex);
825
826 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
827 spin_lock(&GlobalMid_Lock);
828 ses->status = CifsGood;
829 ses->need_reconnect = false;
830 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100831 return rc;
832}
833
834#ifdef CONFIG_CIFS_UPCALL
835static void
836SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
837{
838 int rc;
839 struct cifs_ses *ses = sess_data->ses;
840 struct cifs_spnego_msg *msg;
841 struct key *spnego_key = NULL;
842 struct smb2_sess_setup_rsp *rsp = NULL;
843
844 rc = SMB2_sess_alloc_buffer(sess_data);
845 if (rc)
846 goto out;
847
848 spnego_key = cifs_get_spnego_key(ses);
849 if (IS_ERR(spnego_key)) {
850 rc = PTR_ERR(spnego_key);
851 spnego_key = NULL;
852 goto out;
853 }
854
855 msg = spnego_key->payload.data[0];
856 /*
857 * check version field to make sure that cifs.upcall is
858 * sending us a response in an expected form
859 */
860 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
861 cifs_dbg(VFS,
862 "bad cifs.upcall version. Expected %d got %d",
863 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
864 rc = -EKEYREJECTED;
865 goto out_put_spnego_key;
866 }
867
868 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
869 GFP_KERNEL);
870 if (!ses->auth_key.response) {
871 cifs_dbg(VFS,
872 "Kerberos can't allocate (%u bytes) memory",
873 msg->sesskey_len);
874 rc = -ENOMEM;
875 goto out_put_spnego_key;
876 }
877 ses->auth_key.len = msg->sesskey_len;
878
879 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
880 sess_data->iov[1].iov_len = msg->secblob_len;
881
882 rc = SMB2_sess_sendreceive(sess_data);
883 if (rc)
884 goto out_put_spnego_key;
885
886 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700887 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100888
889 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100890
891 rc = SMB2_sess_establish_session(sess_data);
892out_put_spnego_key:
893 key_invalidate(spnego_key);
894 key_put(spnego_key);
895out:
896 sess_data->result = rc;
897 sess_data->func = NULL;
898 SMB2_sess_free_buffer(sess_data);
899}
900#else
901static void
902SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
903{
904 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
905 sess_data->result = -EOPNOTSUPP;
906 sess_data->func = NULL;
907}
908#endif
909
Sachin Prabhu166cea42016-10-07 19:11:22 +0100910static void
911SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
912
913static void
914SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
915{
916 int rc;
917 struct cifs_ses *ses = sess_data->ses;
918 struct smb2_sess_setup_rsp *rsp = NULL;
919 char *ntlmssp_blob = NULL;
920 bool use_spnego = false; /* else use raw ntlmssp */
921 u16 blob_length = 0;
922
923 /*
924 * If memory allocation is successful, caller of this function
925 * frees it.
926 */
927 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
928 if (!ses->ntlmssp) {
929 rc = -ENOMEM;
930 goto out_err;
931 }
932 ses->ntlmssp->sesskey_per_smbsess = true;
933
934 rc = SMB2_sess_alloc_buffer(sess_data);
935 if (rc)
936 goto out_err;
937
938 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
939 GFP_KERNEL);
940 if (ntlmssp_blob == NULL) {
941 rc = -ENOMEM;
942 goto out;
943 }
944
945 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
946 if (use_spnego) {
947 /* BB eventually need to add this */
948 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
949 rc = -EOPNOTSUPP;
950 goto out;
951 } else {
952 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
953 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
954 }
955 sess_data->iov[1].iov_base = ntlmssp_blob;
956 sess_data->iov[1].iov_len = blob_length;
957
958 rc = SMB2_sess_sendreceive(sess_data);
959 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
960
961 /* If true, rc here is expected and not an error */
962 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700963 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +0100964 rc = 0;
965
966 if (rc)
967 goto out;
968
969 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
970 le16_to_cpu(rsp->SecurityBufferOffset)) {
971 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
972 le16_to_cpu(rsp->SecurityBufferOffset));
973 rc = -EIO;
974 goto out;
975 }
976 rc = decode_ntlmssp_challenge(rsp->Buffer,
977 le16_to_cpu(rsp->SecurityBufferLength), ses);
978 if (rc)
979 goto out;
980
981 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
982
983
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700984 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100985 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +0100986
987out:
988 kfree(ntlmssp_blob);
989 SMB2_sess_free_buffer(sess_data);
990 if (!rc) {
991 sess_data->result = 0;
992 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
993 return;
994 }
995out_err:
996 kfree(ses->ntlmssp);
997 ses->ntlmssp = NULL;
998 sess_data->result = rc;
999 sess_data->func = NULL;
1000}
1001
1002static void
1003SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1004{
1005 int rc;
1006 struct cifs_ses *ses = sess_data->ses;
1007 struct smb2_sess_setup_req *req;
1008 struct smb2_sess_setup_rsp *rsp = NULL;
1009 unsigned char *ntlmssp_blob = NULL;
1010 bool use_spnego = false; /* else use raw ntlmssp */
1011 u16 blob_length = 0;
1012
1013 rc = SMB2_sess_alloc_buffer(sess_data);
1014 if (rc)
1015 goto out;
1016
1017 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001018 req->hdr.sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001019
1020 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1021 sess_data->nls_cp);
1022 if (rc) {
1023 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1024 goto out;
1025 }
1026
1027 if (use_spnego) {
1028 /* BB eventually need to add this */
1029 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1030 rc = -EOPNOTSUPP;
1031 goto out;
1032 }
1033 sess_data->iov[1].iov_base = ntlmssp_blob;
1034 sess_data->iov[1].iov_len = blob_length;
1035
1036 rc = SMB2_sess_sendreceive(sess_data);
1037 if (rc)
1038 goto out;
1039
1040 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1041
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001042 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001043 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001044
1045 rc = SMB2_sess_establish_session(sess_data);
1046out:
1047 kfree(ntlmssp_blob);
1048 SMB2_sess_free_buffer(sess_data);
1049 kfree(ses->ntlmssp);
1050 ses->ntlmssp = NULL;
1051 sess_data->result = rc;
1052 sess_data->func = NULL;
1053}
1054
1055static int
1056SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1057{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301058 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001059
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301060 type = smb2_select_sectype(ses->server, ses->sectype);
1061 cifs_dbg(FYI, "sess setup type %d\n", type);
1062 if (type == Unspecified) {
1063 cifs_dbg(VFS,
1064 "Unable to select appropriate authentication method!");
1065 return -EINVAL;
1066 }
1067
1068 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001069 case Kerberos:
1070 sess_data->func = SMB2_auth_kerberos;
1071 break;
1072 case RawNTLMSSP:
1073 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1074 break;
1075 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301076 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001077 return -EOPNOTSUPP;
1078 }
1079
1080 return 0;
1081}
1082
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001083int
1084SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1085 const struct nls_table *nls_cp)
1086{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001087 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001088 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001089 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001090
Joe Perchesf96637b2013-05-04 22:12:25 -05001091 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001092
Jeff Layton3534b852013-05-24 07:41:01 -04001093 if (!server) {
1094 WARN(1, "%s: server is NULL!\n", __func__);
1095 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001096 }
1097
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001098 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1099 if (!sess_data)
1100 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001101
1102 rc = SMB2_select_sec(ses, sess_data);
1103 if (rc)
1104 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001105 sess_data->xid = xid;
1106 sess_data->ses = ses;
1107 sess_data->buf0_type = CIFS_NO_BUFFER;
1108 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001109
Sachin Prabhu166cea42016-10-07 19:11:22 +01001110 while (sess_data->func)
1111 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001112
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001113 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001114out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001115 kfree(sess_data);
1116 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001117}
1118
1119int
1120SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1121{
1122 struct smb2_logoff_req *req; /* response is also trivial struct */
1123 int rc = 0;
1124 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001125 int flags = 0;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001126
Joe Perchesf96637b2013-05-04 22:12:25 -05001127 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001128
1129 if (ses && (ses->server))
1130 server = ses->server;
1131 else
1132 return -EIO;
1133
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001134 /* no need to send SMB logoff if uid already closed due to reconnect */
1135 if (ses->need_reconnect)
1136 goto smb2_session_already_dead;
1137
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001138 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1139 if (rc)
1140 return rc;
1141
1142 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001143 req->hdr.sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001144
1145 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1146 flags |= CIFS_TRANSFORM_REQ;
1147 else if (server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001148 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001149
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001150 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001151 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001152 /*
1153 * No tcon so can't do
1154 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1155 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001156
1157smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001158 return rc;
1159}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001160
1161static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1162{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001163 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001164}
1165
1166#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1167
Steve Frenchde9f68df2013-11-15 11:26:24 -06001168/* These are similar values to what Windows uses */
1169static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1170{
1171 tcon->max_chunks = 256;
1172 tcon->max_bytes_chunk = 1048576;
1173 tcon->max_bytes_copy = 16777216;
1174}
1175
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001176int
1177SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1178 struct cifs_tcon *tcon, const struct nls_table *cp)
1179{
1180 struct smb2_tree_connect_req *req;
1181 struct smb2_tree_connect_rsp *rsp = NULL;
1182 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001183 struct kvec rsp_iov;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001184 int rc = 0;
1185 int resp_buftype;
1186 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001187 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001188 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001189
Joe Perchesf96637b2013-05-04 22:12:25 -05001190 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001191
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001192 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001193 return -EIO;
1194
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001195 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1196 if (unc_path == NULL)
1197 return -ENOMEM;
1198
1199 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1200 unc_path_len *= 2;
1201 if (unc_path_len < 2) {
1202 kfree(unc_path);
1203 return -EINVAL;
1204 }
1205
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001206 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1207 if (tcon)
1208 tcon->tid = 0;
1209
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001210 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1211 if (rc) {
1212 kfree(unc_path);
1213 return rc;
1214 }
1215
1216 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001217 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1218 flags |= CIFS_TRANSFORM_REQ;
1219
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001220 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001221 req->hdr.sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001222 if (ses->server->sign)
1223 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001224 } else if (encryption_required(tcon))
1225 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001226
1227 iov[0].iov_base = (char *)req;
1228 /* 4 for rfc1002 length field and 1 for pad */
1229 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1230
1231 /* Testing shows that buffer offset must be at location of Buffer[0] */
1232 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1233 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1234 req->PathLength = cpu_to_le16(unc_path_len - 2);
1235 iov[1].iov_base = unc_path;
1236 iov[1].iov_len = unc_path_len;
1237
1238 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1239
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001240 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001241 cifs_small_buf_release(req);
1242 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001243
1244 if (rc != 0) {
1245 if (tcon) {
1246 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1247 tcon->need_reconnect = true;
1248 }
1249 goto tcon_error_exit;
1250 }
1251
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001252 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001253 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001254 goto tcon_exit;
1255 }
1256
Christophe JAILLETcd123002017-05-12 17:59:32 +02001257 switch (rsp->ShareType) {
1258 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001259 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001260 break;
1261 case SMB2_SHARE_TYPE_PIPE:
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001262 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001263 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001264 break;
1265 case SMB2_SHARE_TYPE_PRINT:
1266 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001267 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001268 break;
1269 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001270 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001271 rc = -EOPNOTSUPP;
1272 goto tcon_error_exit;
1273 }
1274
1275 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001276 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001277 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1278 tcon->tidStatus = CifsGood;
1279 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001280 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001281 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001282
1283 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1284 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001285 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001286
1287 if (tcon->seal &&
1288 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1289 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1290
Steve Frenchde9f68df2013-11-15 11:26:24 -06001291 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001292 if (tcon->ses->server->ops->validate_negotiate)
1293 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001294tcon_exit:
1295 free_rsp_buf(resp_buftype, rsp);
1296 kfree(unc_path);
1297 return rc;
1298
1299tcon_error_exit:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001300 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001301 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001302 }
1303 goto tcon_exit;
1304}
1305
1306int
1307SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1308{
1309 struct smb2_tree_disconnect_req *req; /* response is trivial */
1310 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001311 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001312 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001313
Joe Perchesf96637b2013-05-04 22:12:25 -05001314 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001315
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001316 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001317 return -EIO;
1318
1319 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1320 return 0;
1321
1322 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1323 if (rc)
1324 return rc;
1325
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001326 if (encryption_required(tcon))
1327 flags |= CIFS_TRANSFORM_REQ;
1328
1329 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001330 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001331 if (rc)
1332 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1333
1334 return rc;
1335}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001336
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001337
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001338static struct create_durable *
1339create_durable_buf(void)
1340{
1341 struct create_durable *buf;
1342
1343 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1344 if (!buf)
1345 return NULL;
1346
1347 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001348 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001349 buf->ccontext.DataLength = cpu_to_le32(16);
1350 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1351 (struct create_durable, Name));
1352 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001353 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001354 buf->Name[0] = 'D';
1355 buf->Name[1] = 'H';
1356 buf->Name[2] = 'n';
1357 buf->Name[3] = 'Q';
1358 return buf;
1359}
1360
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001361static struct create_durable *
1362create_reconnect_durable_buf(struct cifs_fid *fid)
1363{
1364 struct create_durable *buf;
1365
1366 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1367 if (!buf)
1368 return NULL;
1369
1370 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1371 (struct create_durable, Data));
1372 buf->ccontext.DataLength = cpu_to_le32(16);
1373 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1374 (struct create_durable, Name));
1375 buf->ccontext.NameLength = cpu_to_le16(4);
1376 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1377 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001378 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001379 buf->Name[0] = 'D';
1380 buf->Name[1] = 'H';
1381 buf->Name[2] = 'n';
1382 buf->Name[3] = 'C';
1383 return buf;
1384}
1385
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001386static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001387parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1388 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001389{
1390 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001391 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001392 unsigned int next;
1393 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001394 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001395
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001396 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001397 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001398 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001399 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001400 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001401 if (le16_to_cpu(cc->NameLength) == 4 &&
1402 strncmp(name, "RqLs", 4) == 0)
1403 return server->ops->parse_lease_buf(cc, epoch);
1404
1405 next = le32_to_cpu(cc->Next);
1406 if (!next)
1407 break;
1408 remaining -= next;
1409 cc = (struct create_context *)((char *)cc + next);
1410 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001411
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001412 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001413}
1414
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001415static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001416add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1417 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001418{
1419 struct smb2_create_req *req = iov[0].iov_base;
1420 unsigned int num = *num_iovec;
1421
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001422 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001423 if (iov[num].iov_base == NULL)
1424 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001425 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001426 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1427 if (!req->CreateContextsOffset)
1428 req->CreateContextsOffset = cpu_to_le32(
1429 sizeof(struct smb2_create_req) - 4 +
1430 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001431 le32_add_cpu(&req->CreateContextsLength,
1432 server->vals->create_lease_size);
1433 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001434 *num_iovec = num + 1;
1435 return 0;
1436}
1437
Steve Frenchb56eae42015-11-03 09:26:27 -06001438static struct create_durable_v2 *
1439create_durable_v2_buf(struct cifs_fid *pfid)
1440{
1441 struct create_durable_v2 *buf;
1442
1443 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1444 if (!buf)
1445 return NULL;
1446
1447 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1448 (struct create_durable_v2, dcontext));
1449 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1450 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1451 (struct create_durable_v2, Name));
1452 buf->ccontext.NameLength = cpu_to_le16(4);
1453
1454 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1455 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001456 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001457 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1458
1459 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1460 buf->Name[0] = 'D';
1461 buf->Name[1] = 'H';
1462 buf->Name[2] = '2';
1463 buf->Name[3] = 'Q';
1464 return buf;
1465}
1466
1467static struct create_durable_handle_reconnect_v2 *
1468create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1469{
1470 struct create_durable_handle_reconnect_v2 *buf;
1471
1472 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1473 GFP_KERNEL);
1474 if (!buf)
1475 return NULL;
1476
1477 buf->ccontext.DataOffset =
1478 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1479 dcontext));
1480 buf->ccontext.DataLength =
1481 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1482 buf->ccontext.NameOffset =
1483 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1484 Name));
1485 buf->ccontext.NameLength = cpu_to_le16(4);
1486
1487 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1488 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1489 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1490 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1491
1492 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1493 buf->Name[0] = 'D';
1494 buf->Name[1] = 'H';
1495 buf->Name[2] = '2';
1496 buf->Name[3] = 'C';
1497 return buf;
1498}
1499
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001500static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001501add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001502 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001503{
1504 struct smb2_create_req *req = iov[0].iov_base;
1505 unsigned int num = *num_iovec;
1506
Steve Frenchb56eae42015-11-03 09:26:27 -06001507 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1508 if (iov[num].iov_base == NULL)
1509 return -ENOMEM;
1510 iov[num].iov_len = sizeof(struct create_durable_v2);
1511 if (!req->CreateContextsOffset)
1512 req->CreateContextsOffset =
1513 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1514 iov[1].iov_len);
1515 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1516 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1517 *num_iovec = num + 1;
1518 return 0;
1519}
1520
1521static int
1522add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1523 struct cifs_open_parms *oparms)
1524{
1525 struct smb2_create_req *req = iov[0].iov_base;
1526 unsigned int num = *num_iovec;
1527
1528 /* indicate that we don't need to relock the file */
1529 oparms->reconnect = false;
1530
1531 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1532 if (iov[num].iov_base == NULL)
1533 return -ENOMEM;
1534 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1535 if (!req->CreateContextsOffset)
1536 req->CreateContextsOffset =
1537 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1538 iov[1].iov_len);
1539 le32_add_cpu(&req->CreateContextsLength,
1540 sizeof(struct create_durable_handle_reconnect_v2));
1541 inc_rfc1001_len(&req->hdr,
1542 sizeof(struct create_durable_handle_reconnect_v2));
1543 *num_iovec = num + 1;
1544 return 0;
1545}
1546
1547static int
1548add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1549 struct cifs_open_parms *oparms, bool use_persistent)
1550{
1551 struct smb2_create_req *req = iov[0].iov_base;
1552 unsigned int num = *num_iovec;
1553
1554 if (use_persistent) {
1555 if (oparms->reconnect)
1556 return add_durable_reconnect_v2_context(iov, num_iovec,
1557 oparms);
1558 else
1559 return add_durable_v2_context(iov, num_iovec, oparms);
1560 }
1561
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001562 if (oparms->reconnect) {
1563 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1564 /* indicate that we don't need to relock the file */
1565 oparms->reconnect = false;
1566 } else
1567 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001568 if (iov[num].iov_base == NULL)
1569 return -ENOMEM;
1570 iov[num].iov_len = sizeof(struct create_durable);
1571 if (!req->CreateContextsOffset)
1572 req->CreateContextsOffset =
1573 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1574 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001575 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001576 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1577 *num_iovec = num + 1;
1578 return 0;
1579}
1580
Aurelien Aptelf0712922017-02-22 14:47:17 +01001581static int
1582alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1583 const char *treename, const __le16 *path)
1584{
1585 int treename_len, path_len;
1586 struct nls_table *cp;
1587 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1588
1589 /*
1590 * skip leading "\\"
1591 */
1592 treename_len = strlen(treename);
1593 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1594 return -EINVAL;
1595
1596 treename += 2;
1597 treename_len -= 2;
1598
1599 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1600
1601 /*
1602 * make room for one path separator between the treename and
1603 * path
1604 */
1605 *out_len = treename_len + 1 + path_len;
1606
1607 /*
1608 * final path needs to be null-terminated UTF16 with a
1609 * size aligned to 8
1610 */
1611
1612 *out_size = roundup((*out_len+1)*2, 8);
1613 *out_path = kzalloc(*out_size, GFP_KERNEL);
1614 if (!*out_path)
1615 return -ENOMEM;
1616
1617 cp = load_nls_default();
1618 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1619 UniStrcat(*out_path, sep);
1620 UniStrcat(*out_path, path);
1621 unload_nls(cp);
1622
1623 return 0;
1624}
1625
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001626int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001627SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001628 __u8 *oplock, struct smb2_file_all_info *buf,
1629 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001630{
1631 struct smb2_create_req *req;
1632 struct smb2_create_rsp *rsp;
1633 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001634 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001635 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001636 struct kvec iov[4];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001637 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001638 int resp_buftype;
1639 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001640 __le16 *copy_path = NULL;
1641 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001642 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001643 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001644 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001645 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001646 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001647
Joe Perchesf96637b2013-05-04 22:12:25 -05001648 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001649
1650 if (ses && (ses->server))
1651 server = ses->server;
1652 else
1653 return -EIO;
1654
1655 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1656 if (rc)
1657 return rc;
1658
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001659 if (encryption_required(tcon))
1660 flags |= CIFS_TRANSFORM_REQ;
1661
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001662 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001663 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001664 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1665 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001666
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001667 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001668 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001669 /* File attributes ignored on open (used in create though) */
1670 req->FileAttributes = cpu_to_le32(file_attributes);
1671 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001672 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1673 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001674
1675 iov[0].iov_base = (char *)req;
1676 /* 4 for rfc1002 length field */
1677 iov[0].iov_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001678 /* -1 since last byte is buf[0] which is sent below (path) */
1679 iov[0].iov_len--;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001680
Aurelien Aptelf0712922017-02-22 14:47:17 +01001681 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1682
1683 /* [MS-SMB2] 2.2.13 NameOffset:
1684 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1685 * the SMB2 header, the file name includes a prefix that will
1686 * be processed during DFS name normalization as specified in
1687 * section 3.3.5.9. Otherwise, the file name is relative to
1688 * the share that is identified by the TreeId in the SMB2
1689 * header.
1690 */
1691 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1692 int name_len;
1693
1694 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1695 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1696 &name_len,
1697 tcon->treeName, path);
1698 if (rc)
1699 return rc;
1700 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001701 uni_path_len = copy_size;
1702 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001703 } else {
1704 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1705 /* MUST set path len (NameLength) to 0 opening root of share */
1706 req->NameLength = cpu_to_le16(uni_path_len - 2);
1707 if (uni_path_len % 8 != 0) {
1708 copy_size = roundup(uni_path_len, 8);
1709 copy_path = kzalloc(copy_size, GFP_KERNEL);
1710 if (!copy_path)
1711 return -ENOMEM;
1712 memcpy((char *)copy_path, (const char *)path,
1713 uni_path_len);
1714 uni_path_len = copy_size;
1715 path = copy_path;
1716 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001717 }
1718
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001719 iov[1].iov_len = uni_path_len;
1720 iov[1].iov_base = path;
1721 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1722 inc_rfc1001_len(req, uni_path_len - 1);
1723
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001724 if (!server->oplocks)
1725 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1726
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001727 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001728 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1729 req->RequestedOplockLevel = *oplock;
1730 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001731 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001732 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001733 cifs_small_buf_release(req);
1734 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001735 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001736 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001737 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001738 }
1739
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001740 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1741 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001742 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001743 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001744 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001745 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001746 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001747 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001748
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001749 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001750 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001751 if (rc) {
1752 cifs_small_buf_release(req);
1753 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001754 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001755 return rc;
1756 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001757 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001758 }
1759
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001760 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001761 cifs_small_buf_release(req);
1762 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001763
1764 if (rc != 0) {
1765 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001766 if (err_buf)
1767 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1768 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001769 goto creat_exit;
1770 }
1771
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001772 oparms->fid->persistent_fid = rsp->PersistentFileId;
1773 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001774
1775 if (buf) {
1776 memcpy(buf, &rsp->CreationTime, 32);
1777 buf->AllocationSize = rsp->AllocationSize;
1778 buf->EndOfFile = rsp->EndofFile;
1779 buf->Attributes = rsp->FileAttributes;
1780 buf->NumberOfLinks = cpu_to_le32(1);
1781 buf->DeletePending = 0;
1782 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001783
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001784 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001785 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001786 else
1787 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001788creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001789 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001790 kfree(lc_buf);
1791 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001792 free_rsp_buf(resp_buftype, rsp);
1793 return rc;
1794}
1795
Steve French4a72daf2013-06-25 00:20:49 -05001796/*
1797 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1798 */
1799int
1800SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001801 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1802 char *in_data, u32 indatalen,
1803 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001804{
1805 struct smb2_ioctl_req *req;
1806 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001807 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001808 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001809 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001810 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001811 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001812 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001813 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001814 int flags = 0;
Steve French4a72daf2013-06-25 00:20:49 -05001815
1816 cifs_dbg(FYI, "SMB2 IOCTL\n");
1817
Steve French3d1a3742014-08-11 21:05:25 -05001818 if (out_data != NULL)
1819 *out_data = NULL;
1820
Steve French4a72daf2013-06-25 00:20:49 -05001821 /* zero out returned data len, in case of error */
1822 if (plen)
1823 *plen = 0;
1824
Steve French8e353102015-03-26 19:47:02 -05001825 if (tcon)
1826 ses = tcon->ses;
1827 else
1828 return -EIO;
1829
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001830 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05001831 return -EIO;
1832
1833 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1834 if (rc)
1835 return rc;
1836
Aurelien Aptel51146622017-02-28 15:08:41 +01001837 if (use_ipc) {
1838 if (ses->ipc_tid == 0) {
1839 cifs_small_buf_release(req);
1840 return -ENOTCONN;
1841 }
1842
1843 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1844 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1845 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1846 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001847 if (encryption_required(tcon))
1848 flags |= CIFS_TRANSFORM_REQ;
1849
Steve French4a72daf2013-06-25 00:20:49 -05001850 req->CtlCode = cpu_to_le32(opcode);
1851 req->PersistentFileId = persistent_fid;
1852 req->VolatileFileId = volatile_fid;
1853
1854 if (indatalen) {
1855 req->InputCount = cpu_to_le32(indatalen);
1856 /* do not set InputOffset if no input data */
1857 req->InputOffset =
1858 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1859 iov[1].iov_base = in_data;
1860 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001861 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001862 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001863 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001864
1865 req->OutputOffset = 0;
1866 req->OutputCount = 0; /* MBZ */
1867
1868 /*
1869 * Could increase MaxOutputResponse, but that would require more
1870 * than one credit. Windows typically sets this smaller, but for some
1871 * ioctls it may be useful to allow server to send more. No point
1872 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001873 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1874 * (by default, note that it can be overridden to make max larger)
1875 * in responses (except for read responses which can be bigger.
1876 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001877 */
Steve French7db0a6e2017-05-03 21:12:20 -05001878 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001879
1880 if (is_fsctl)
1881 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1882 else
1883 req->Flags = 0;
1884
1885 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001886
Steve French7ff8d452013-10-14 00:44:19 -05001887 /*
1888 * If no input data, the size of ioctl struct in
1889 * protocol spec still includes a 1 byte data buffer,
1890 * but if input data passed to ioctl, we do not
1891 * want to double count this, so we do not send
1892 * the dummy one byte of data in iovec[0] if sending
1893 * input data (in iovec[1]). We also must add 4 bytes
1894 * in first iovec to allow for rfc1002 length field.
1895 */
1896
1897 if (indatalen) {
1898 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1899 inc_rfc1001_len(req, indatalen - 1);
1900 } else
1901 iov[0].iov_len = get_rfc1002_length(req) + 4;
1902
Steve French4a72daf2013-06-25 00:20:49 -05001903
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001904 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001905 cifs_small_buf_release(req);
1906 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001907
Steve French9bf0c9c2013-11-16 18:05:28 -06001908 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001909 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001910 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001911 } else if (rc == -EINVAL) {
1912 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1913 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001914 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001915 goto ioctl_exit;
1916 }
Steve French4a72daf2013-06-25 00:20:49 -05001917 }
1918
1919 /* check if caller wants to look at return data or just return rc */
1920 if ((plen == NULL) || (out_data == NULL))
1921 goto ioctl_exit;
1922
1923 *plen = le32_to_cpu(rsp->OutputCount);
1924
1925 /* We check for obvious errors in the output buffer length and offset */
1926 if (*plen == 0)
1927 goto ioctl_exit; /* server returned no data */
1928 else if (*plen > 0xFF00) {
1929 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1930 *plen = 0;
1931 rc = -EIO;
1932 goto ioctl_exit;
1933 }
1934
1935 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1936 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1937 le32_to_cpu(rsp->OutputOffset));
1938 *plen = 0;
1939 rc = -EIO;
1940 goto ioctl_exit;
1941 }
1942
1943 *out_data = kmalloc(*plen, GFP_KERNEL);
1944 if (*out_data == NULL) {
1945 rc = -ENOMEM;
1946 goto ioctl_exit;
1947 }
1948
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001949 shdr = get_sync_hdr(rsp);
1950 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05001951ioctl_exit:
1952 free_rsp_buf(resp_buftype, rsp);
1953 return rc;
1954}
1955
Steve French64a5cfa2013-10-14 15:31:32 -05001956/*
1957 * Individual callers to ioctl worker function follow
1958 */
1959
1960int
1961SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1962 u64 persistent_fid, u64 volatile_fid)
1963{
1964 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001965 struct compress_ioctl fsctl_input;
1966 char *ret_data = NULL;
1967
1968 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001969 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001970
1971 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1972 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001973 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05001974 (char *)&fsctl_input /* data input */,
1975 2 /* in data len */, &ret_data /* out data */, NULL);
1976
1977 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001978
1979 return rc;
1980}
1981
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001982int
1983SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1984 u64 persistent_fid, u64 volatile_fid)
1985{
1986 struct smb2_close_req *req;
1987 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001988 struct cifs_ses *ses = tcon->ses;
1989 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001990 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001991 int resp_buftype;
1992 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001993 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001994
Joe Perchesf96637b2013-05-04 22:12:25 -05001995 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001996
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001997 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001998 return -EIO;
1999
2000 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
2001 if (rc)
2002 return rc;
2003
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002004 if (encryption_required(tcon))
2005 flags |= CIFS_TRANSFORM_REQ;
2006
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002007 req->PersistentFileId = persistent_fid;
2008 req->VolatileFileId = volatile_fid;
2009
2010 iov[0].iov_base = (char *)req;
2011 /* 4 for rfc1002 length field */
2012 iov[0].iov_len = get_rfc1002_length(req) + 4;
2013
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002014 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002015 cifs_small_buf_release(req);
2016 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002017
2018 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002019 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002020 goto close_exit;
2021 }
2022
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002023 /* BB FIXME - decode close response, update inode for caching */
2024
2025close_exit:
2026 free_rsp_buf(resp_buftype, rsp);
2027 return rc;
2028}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002029
2030static int
2031validate_buf(unsigned int offset, unsigned int buffer_length,
2032 struct smb2_hdr *hdr, unsigned int min_buf_size)
2033
2034{
2035 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2036 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2037 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2038 char *end_of_buf = begin_of_buf + buffer_length;
2039
2040
2041 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002042 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2043 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002044 return -EINVAL;
2045 }
2046
2047 /* check if beyond RFC1001 maximum length */
2048 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002049 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2050 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002051 return -EINVAL;
2052 }
2053
2054 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002055 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002056 return -EINVAL;
2057 }
2058
2059 return 0;
2060}
2061
2062/*
2063 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2064 * Caller must free buffer.
2065 */
2066static int
2067validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2068 struct smb2_hdr *hdr, unsigned int minbufsize,
2069 char *data)
2070
2071{
2072 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2073 int rc;
2074
2075 if (!data)
2076 return -EINVAL;
2077
2078 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2079 if (rc)
2080 return rc;
2081
2082 memcpy(data, begin_of_buf, buffer_length);
2083
2084 return 0;
2085}
2086
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002087static int
2088query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002089 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2090 u32 additional_info, size_t output_len, size_t min_len, void **data,
2091 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002092{
2093 struct smb2_query_info_req *req;
2094 struct smb2_query_info_rsp *rsp = NULL;
2095 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002096 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002097 int rc = 0;
2098 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002099 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002100 int flags = 0;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002101
Joe Perchesf96637b2013-05-04 22:12:25 -05002102 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002103
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002104 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002105 return -EIO;
2106
2107 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2108 if (rc)
2109 return rc;
2110
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002111 if (encryption_required(tcon))
2112 flags |= CIFS_TRANSFORM_REQ;
2113
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002114 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002115 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002116 req->PersistentFileId = persistent_fid;
2117 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002118 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002119 /* 4 for rfc1002 length field and 1 for Buffer */
2120 req->InputBufferOffset =
2121 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002122 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002123
2124 iov[0].iov_base = (char *)req;
2125 /* 4 for rfc1002 length field */
2126 iov[0].iov_len = get_rfc1002_length(req) + 4;
2127
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002128 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002129 cifs_small_buf_release(req);
2130 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002131
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002132 if (rc) {
2133 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2134 goto qinf_exit;
2135 }
2136
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002137 if (dlen) {
2138 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2139 if (!*data) {
2140 *data = kmalloc(*dlen, GFP_KERNEL);
2141 if (!*data) {
2142 cifs_dbg(VFS,
2143 "Error %d allocating memory for acl\n",
2144 rc);
2145 *dlen = 0;
2146 goto qinf_exit;
2147 }
2148 }
2149 }
2150
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002151 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2152 le32_to_cpu(rsp->OutputBufferLength),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002153 &rsp->hdr, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002154
2155qinf_exit:
2156 free_rsp_buf(resp_buftype, rsp);
2157 return rc;
2158}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002159
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002160int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
2161 u64 persistent_fid, u64 volatile_fid,
2162 struct smb2_file_full_ea_info *data)
2163{
2164 return query_info(xid, tcon, persistent_fid, volatile_fid,
2165 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
2166 SMB2_MAX_EA_BUF,
2167 sizeof(struct smb2_file_full_ea_info),
2168 (void **)&data,
2169 NULL);
2170}
2171
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002172int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2173 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002174{
2175 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002176 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002177 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002178 sizeof(struct smb2_file_all_info), (void **)&data,
2179 NULL);
2180}
2181
2182int
2183SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2184 u64 persistent_fid, u64 volatile_fid,
2185 void **data, u32 *plen)
2186{
2187 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2188 *plen = 0;
2189
2190 return query_info(xid, tcon, persistent_fid, volatile_fid,
2191 0, SMB2_O_INFO_SECURITY, additional_info,
2192 SMB2_MAX_BUFFER_SIZE,
2193 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002194}
2195
2196int
2197SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2198 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2199{
2200 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002201 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002202 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002203 sizeof(struct smb2_file_internal_info),
2204 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002205}
2206
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002207/*
2208 * This is a no-op for now. We're not really interested in the reply, but
2209 * rather in the fact that the server sent one and that server->lstrp
2210 * gets updated.
2211 *
2212 * FIXME: maybe we should consider checking that the reply matches request?
2213 */
2214static void
2215smb2_echo_callback(struct mid_q_entry *mid)
2216{
2217 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002218 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002219 unsigned int credits_received = 1;
2220
2221 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002222 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002223
2224 DeleteMidQEntry(mid);
2225 add_credits(server, credits_received, CIFS_ECHO_OP);
2226}
2227
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002228void smb2_reconnect_server(struct work_struct *work)
2229{
2230 struct TCP_Server_Info *server = container_of(work,
2231 struct TCP_Server_Info, reconnect.work);
2232 struct cifs_ses *ses;
2233 struct cifs_tcon *tcon, *tcon2;
2234 struct list_head tmp_list;
2235 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002236 int rc;
2237 int resched = false;
2238
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002239
2240 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2241 mutex_lock(&server->reconnect_mutex);
2242
2243 INIT_LIST_HEAD(&tmp_list);
2244 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2245
2246 spin_lock(&cifs_tcp_ses_lock);
2247 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2248 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002249 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002250 tcon->tc_count++;
2251 list_add_tail(&tcon->rlist, &tmp_list);
2252 tcon_exist = true;
2253 }
2254 }
2255 }
2256 /*
2257 * Get the reference to server struct to be sure that the last call of
2258 * cifs_put_tcon() in the loop below won't release the server pointer.
2259 */
2260 if (tcon_exist)
2261 server->srv_count++;
2262
2263 spin_unlock(&cifs_tcp_ses_lock);
2264
2265 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002266 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2267 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002268 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002269 else
2270 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002271 list_del_init(&tcon->rlist);
2272 cifs_put_tcon(tcon);
2273 }
2274
2275 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002276 if (resched)
2277 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002278 mutex_unlock(&server->reconnect_mutex);
2279
2280 /* now we can safely release srv struct */
2281 if (tcon_exist)
2282 cifs_put_tcp_session(server, 1);
2283}
2284
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002285int
2286SMB2_echo(struct TCP_Server_Info *server)
2287{
2288 struct smb2_echo_req *req;
2289 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002290 struct kvec iov[2];
2291 struct smb_rqst rqst = { .rq_iov = iov,
2292 .rq_nvec = 2 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002293
Joe Perchesf96637b2013-05-04 22:12:25 -05002294 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002295
Steve French4fcd1812016-06-22 20:12:05 -05002296 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002297 /* No need to send echo on newly established connections */
2298 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2299 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002300 }
2301
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002302 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2303 if (rc)
2304 return rc;
2305
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002306 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002307
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002308 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002309 iov[0].iov_len = 4;
2310 iov[0].iov_base = (char *)req;
2311 iov[1].iov_len = get_rfc1002_length(req);
2312 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002313
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002314 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2315 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002316 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002317 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002318
2319 cifs_small_buf_release(req);
2320 return rc;
2321}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002322
2323int
2324SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2325 u64 volatile_fid)
2326{
2327 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002328 struct cifs_ses *ses = tcon->ses;
2329 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002330 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002331 int resp_buftype;
2332 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002333 int flags = 0;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002334
Joe Perchesf96637b2013-05-04 22:12:25 -05002335 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002336
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002337 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002338 return -EIO;
2339
2340 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2341 if (rc)
2342 return rc;
2343
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002344 if (encryption_required(tcon))
2345 flags |= CIFS_TRANSFORM_REQ;
2346
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002347 req->PersistentFileId = persistent_fid;
2348 req->VolatileFileId = volatile_fid;
2349
2350 iov[0].iov_base = (char *)req;
2351 /* 4 for rfc1002 length field */
2352 iov[0].iov_len = get_rfc1002_length(req) + 4;
2353
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002354 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002355 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002356
Steve Frenchdfebe402015-03-27 01:00:06 -05002357 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002358 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2359
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002360 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002361 return rc;
2362}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002363
2364/*
2365 * To form a chain of read requests, any read requests after the first should
2366 * have the end_of_chain boolean set to true.
2367 */
2368static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002369smb2_new_read_req(void **buf, unsigned int *total_len,
2370 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2371 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002372{
2373 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002374 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002375 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002376
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002377 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2378 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002379 if (rc)
2380 return rc;
2381 if (io_parms->tcon->ses->server == NULL)
2382 return -ECONNABORTED;
2383
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002384 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002385 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002386
2387 req->PersistentFileId = io_parms->persistent_fid;
2388 req->VolatileFileId = io_parms->volatile_fid;
2389 req->ReadChannelInfoOffset = 0; /* reserved */
2390 req->ReadChannelInfoLength = 0; /* reserved */
2391 req->Channel = 0; /* reserved */
2392 req->MinimumCount = 0;
2393 req->Length = cpu_to_le32(io_parms->length);
2394 req->Offset = cpu_to_le64(io_parms->offset);
2395
2396 if (request_type & CHAINED_REQUEST) {
2397 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002398 /* next 8-byte aligned request */
2399 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2400 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002401 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002402 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002403 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002404 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002405 /*
2406 * Related requests use info from previous read request
2407 * in chain.
2408 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002409 shdr->SessionId = 0xFFFFFFFF;
2410 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002411 req->PersistentFileId = 0xFFFFFFFF;
2412 req->VolatileFileId = 0xFFFFFFFF;
2413 }
2414 }
2415 if (remaining_bytes > io_parms->length)
2416 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2417 else
2418 req->RemainingBytes = 0;
2419
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002420 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002421 return rc;
2422}
2423
2424static void
2425smb2_readv_callback(struct mid_q_entry *mid)
2426{
2427 struct cifs_readdata *rdata = mid->callback_data;
2428 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2429 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002430 struct smb2_sync_hdr *shdr =
2431 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002432 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002433 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2434 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002435 .rq_pages = rdata->pages,
2436 .rq_npages = rdata->nr_pages,
2437 .rq_pagesz = rdata->pagesz,
2438 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002439
Joe Perchesf96637b2013-05-04 22:12:25 -05002440 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2441 __func__, mid->mid, mid->mid_state, rdata->result,
2442 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002443
2444 switch (mid->mid_state) {
2445 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002446 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002447 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002448 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002449 int rc;
2450
Jeff Layton0b688cf2012-09-18 16:20:34 -07002451 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002452 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002453 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2454 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002455 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002456 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002457 task_io_account_read(rdata->got_bytes);
2458 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002459 break;
2460 case MID_REQUEST_SUBMITTED:
2461 case MID_RETRY_NEEDED:
2462 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002463 if (server->sign && rdata->got_bytes)
2464 /* reset bytes number since we can not check a sign */
2465 rdata->got_bytes = 0;
2466 /* FIXME: should this be counted toward the initiating task? */
2467 task_io_account_read(rdata->got_bytes);
2468 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002469 break;
2470 default:
2471 if (rdata->result != -ENODATA)
2472 rdata->result = -EIO;
2473 }
2474
2475 if (rdata->result)
2476 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2477
2478 queue_work(cifsiod_wq, &rdata->work);
2479 DeleteMidQEntry(mid);
2480 add_credits(server, credits_received, 0);
2481}
2482
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002483/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002484int
2485smb2_async_readv(struct cifs_readdata *rdata)
2486{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002487 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002488 char *buf;
2489 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002490 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002491 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2492 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002493 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002494 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002495 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002496
Joe Perchesf96637b2013-05-04 22:12:25 -05002497 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2498 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002499
2500 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2501 io_parms.offset = rdata->offset;
2502 io_parms.length = rdata->bytes;
2503 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2504 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2505 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002506
2507 server = io_parms.tcon->ses->server;
2508
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002509 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002510 if (rc) {
2511 if (rc == -EAGAIN && rdata->credits) {
2512 /* credits was reset by reconnect */
2513 rdata->credits = 0;
2514 /* reduce in_flight value since we won't send the req */
2515 spin_lock(&server->req_lock);
2516 server->in_flight--;
2517 spin_unlock(&server->req_lock);
2518 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002519 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002520 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002521
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002522 if (encryption_required(io_parms.tcon))
2523 flags |= CIFS_TRANSFORM_REQ;
2524
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002525 req_len = cpu_to_be32(total_len);
2526
2527 rdata->iov[0].iov_base = &req_len;
2528 rdata->iov[0].iov_len = sizeof(__be32);
2529 rdata->iov[1].iov_base = buf;
2530 rdata->iov[1].iov_len = total_len;
2531
2532 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002533
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002534 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002535 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002536 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002537 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002538 spin_lock(&server->req_lock);
2539 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002540 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002541 spin_unlock(&server->req_lock);
2542 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002543 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002544 }
2545
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002546 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002547 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002548 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002549 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002550 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002551 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002552 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2553 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002554
2555 cifs_small_buf_release(buf);
2556 return rc;
2557}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002558
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002559int
2560SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2561 unsigned int *nbytes, char **buf, int *buf_type)
2562{
2563 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002564 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002565 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002566 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002567 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002568 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002569 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002570 __be32 req_len;
2571 struct smb_rqst rqst = { .rq_iov = iov,
2572 .rq_nvec = 2 };
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002573 int flags = CIFS_LOG_ERROR;
2574 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002575
2576 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002577 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002578 if (rc)
2579 return rc;
2580
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002581 if (encryption_required(io_parms->tcon))
2582 flags |= CIFS_TRANSFORM_REQ;
2583
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002584 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002585
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002586 iov[0].iov_base = &req_len;
2587 iov[0].iov_len = sizeof(__be32);
2588 iov[1].iov_base = req;
2589 iov[1].iov_len = total_len;
2590
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002591 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002592 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002593
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002594 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002595 shdr = get_sync_hdr(rsp);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002596
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002597 if (shdr->Status == STATUS_END_OF_FILE) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002598 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002599 return 0;
2600 }
2601
2602 if (rc) {
2603 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002604 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002605 } else {
2606 *nbytes = le32_to_cpu(rsp->DataLength);
2607 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2608 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002609 cifs_dbg(FYI, "bad length %d for count %d\n",
2610 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002611 rc = -EIO;
2612 *nbytes = 0;
2613 }
2614 }
2615
2616 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002617 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002618 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002619 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002620 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002621 if (resp_buftype == CIFS_SMALL_BUFFER)
2622 *buf_type = CIFS_SMALL_BUFFER;
2623 else if (resp_buftype == CIFS_LARGE_BUFFER)
2624 *buf_type = CIFS_LARGE_BUFFER;
2625 }
2626 return rc;
2627}
2628
Pavel Shilovsky33319142012-09-18 16:20:29 -07002629/*
2630 * Check the mid_state and signature on received buffer (if any), and queue the
2631 * workqueue completion task.
2632 */
2633static void
2634smb2_writev_callback(struct mid_q_entry *mid)
2635{
2636 struct cifs_writedata *wdata = mid->callback_data;
2637 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2638 unsigned int written;
2639 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2640 unsigned int credits_received = 1;
2641
2642 switch (mid->mid_state) {
2643 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002644 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002645 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2646 if (wdata->result != 0)
2647 break;
2648
2649 written = le32_to_cpu(rsp->DataLength);
2650 /*
2651 * Mask off high 16 bits when bytes written as returned
2652 * by the server is greater than bytes requested by the
2653 * client. OS/2 servers are known to set incorrect
2654 * CountHigh values.
2655 */
2656 if (written > wdata->bytes)
2657 written &= 0xFFFF;
2658
2659 if (written < wdata->bytes)
2660 wdata->result = -ENOSPC;
2661 else
2662 wdata->bytes = written;
2663 break;
2664 case MID_REQUEST_SUBMITTED:
2665 case MID_RETRY_NEEDED:
2666 wdata->result = -EAGAIN;
2667 break;
2668 default:
2669 wdata->result = -EIO;
2670 break;
2671 }
2672
2673 if (wdata->result)
2674 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2675
2676 queue_work(cifsiod_wq, &wdata->work);
2677 DeleteMidQEntry(mid);
2678 add_credits(tcon->ses->server, credits_received, 0);
2679}
2680
2681/* smb2_async_writev - send an async write, and set up mid to handle result */
2682int
Steve French4a5c80d2014-02-07 20:45:12 -06002683smb2_async_writev(struct cifs_writedata *wdata,
2684 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002685{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002686 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002687 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002688 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002689 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002690 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002691 struct kvec iov[2];
2692 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002693
2694 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002695 if (rc) {
2696 if (rc == -EAGAIN && wdata->credits) {
2697 /* credits was reset by reconnect */
2698 wdata->credits = 0;
2699 /* reduce in_flight value since we won't send the req */
2700 spin_lock(&server->req_lock);
2701 server->in_flight--;
2702 spin_unlock(&server->req_lock);
2703 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002704 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002705 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002706
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002707 if (encryption_required(tcon))
2708 flags |= CIFS_TRANSFORM_REQ;
2709
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002710 shdr = get_sync_hdr(req);
2711 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002712
2713 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2714 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2715 req->WriteChannelInfoOffset = 0;
2716 req->WriteChannelInfoLength = 0;
2717 req->Channel = 0;
2718 req->Offset = cpu_to_le64(wdata->offset);
2719 /* 4 for rfc1002 length field */
2720 req->DataOffset = cpu_to_le16(
2721 offsetof(struct smb2_write_req, Buffer) - 4);
2722 req->RemainingBytes = 0;
2723
2724 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002725 iov[0].iov_len = 4;
2726 iov[0].iov_base = req;
2727 iov[1].iov_len = get_rfc1002_length(req) - 1;
2728 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002729
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002730 rqst.rq_iov = iov;
2731 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002732 rqst.rq_pages = wdata->pages;
2733 rqst.rq_npages = wdata->nr_pages;
2734 rqst.rq_pagesz = wdata->pagesz;
2735 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002736
Joe Perchesf96637b2013-05-04 22:12:25 -05002737 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2738 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002739
2740 req->Length = cpu_to_le32(wdata->bytes);
2741
2742 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2743
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002744 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002745 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002746 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002747 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002748 spin_lock(&server->req_lock);
2749 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002750 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002751 spin_unlock(&server->req_lock);
2752 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002753 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002754 }
2755
Pavel Shilovsky33319142012-09-18 16:20:29 -07002756 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002757 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2758 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002759
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002760 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002761 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002762 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2763 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002764
Pavel Shilovsky33319142012-09-18 16:20:29 -07002765async_writev_out:
2766 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002767 return rc;
2768}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002769
2770/*
2771 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2772 * The length field from io_parms must be at least 1 and indicates a number of
2773 * elements with data to write that begins with position 1 in iov array. All
2774 * data length is specified by count.
2775 */
2776int
2777SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2778 unsigned int *nbytes, struct kvec *iov, int n_vec)
2779{
2780 int rc = 0;
2781 struct smb2_write_req *req = NULL;
2782 struct smb2_write_rsp *rsp = NULL;
2783 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002784 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002785 int flags = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002786
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002787 *nbytes = 0;
2788
2789 if (n_vec < 1)
2790 return rc;
2791
2792 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2793 if (rc)
2794 return rc;
2795
2796 if (io_parms->tcon->ses->server == NULL)
2797 return -ECONNABORTED;
2798
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002799 if (encryption_required(io_parms->tcon))
2800 flags |= CIFS_TRANSFORM_REQ;
2801
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002802 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002803
2804 req->PersistentFileId = io_parms->persistent_fid;
2805 req->VolatileFileId = io_parms->volatile_fid;
2806 req->WriteChannelInfoOffset = 0;
2807 req->WriteChannelInfoLength = 0;
2808 req->Channel = 0;
2809 req->Length = cpu_to_le32(io_parms->length);
2810 req->Offset = cpu_to_le64(io_parms->offset);
2811 /* 4 for rfc1002 length field */
2812 req->DataOffset = cpu_to_le16(
2813 offsetof(struct smb2_write_req, Buffer) - 4);
2814 req->RemainingBytes = 0;
2815
2816 iov[0].iov_base = (char *)req;
2817 /* 4 for rfc1002 length field and 1 for Buffer */
2818 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2819
2820 /* length of entire message including data to be written */
2821 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2822
2823 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002824 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002825 cifs_small_buf_release(req);
2826 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002827
2828 if (rc) {
2829 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002830 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002831 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002832 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002833
2834 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002835 return rc;
2836}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002837
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002838static unsigned int
2839num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2840{
2841 int len;
2842 unsigned int entrycount = 0;
2843 unsigned int next_offset = 0;
2844 FILE_DIRECTORY_INFO *entryptr;
2845
2846 if (bufstart == NULL)
2847 return 0;
2848
2849 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2850
2851 while (1) {
2852 entryptr = (FILE_DIRECTORY_INFO *)
2853 ((char *)entryptr + next_offset);
2854
2855 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002856 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002857 break;
2858 }
2859
2860 len = le32_to_cpu(entryptr->FileNameLength);
2861 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002862 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2863 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002864 break;
2865 }
2866
2867 *lastentry = (char *)entryptr;
2868 entrycount++;
2869
2870 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2871 if (!next_offset)
2872 break;
2873 }
2874
2875 return entrycount;
2876}
2877
2878/*
2879 * Readdir/FindFirst
2880 */
2881int
2882SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2883 u64 persistent_fid, u64 volatile_fid, int index,
2884 struct cifs_search_info *srch_inf)
2885{
2886 struct smb2_query_directory_req *req;
2887 struct smb2_query_directory_rsp *rsp = NULL;
2888 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002889 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002890 int rc = 0;
2891 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002892 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002893 unsigned char *bufptr;
2894 struct TCP_Server_Info *server;
2895 struct cifs_ses *ses = tcon->ses;
2896 __le16 asteriks = cpu_to_le16('*');
2897 char *end_of_smb;
2898 unsigned int output_size = CIFSMaxBufSize;
2899 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002900 int flags = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002901
2902 if (ses && (ses->server))
2903 server = ses->server;
2904 else
2905 return -EIO;
2906
2907 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2908 if (rc)
2909 return rc;
2910
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002911 if (encryption_required(tcon))
2912 flags |= CIFS_TRANSFORM_REQ;
2913
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002914 switch (srch_inf->info_level) {
2915 case SMB_FIND_FILE_DIRECTORY_INFO:
2916 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2917 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2918 break;
2919 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2920 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2921 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2922 break;
2923 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002924 cifs_dbg(VFS, "info level %u isn't supported\n",
2925 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002926 rc = -EINVAL;
2927 goto qdir_exit;
2928 }
2929
2930 req->FileIndex = cpu_to_le32(index);
2931 req->PersistentFileId = persistent_fid;
2932 req->VolatileFileId = volatile_fid;
2933
2934 len = 0x2;
2935 bufptr = req->Buffer;
2936 memcpy(bufptr, &asteriks, len);
2937
2938 req->FileNameOffset =
2939 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2940 req->FileNameLength = cpu_to_le16(len);
2941 /*
2942 * BB could be 30 bytes or so longer if we used SMB2 specific
2943 * buffer lengths, but this is safe and close enough.
2944 */
2945 output_size = min_t(unsigned int, output_size, server->maxBuf);
2946 output_size = min_t(unsigned int, output_size, 2 << 15);
2947 req->OutputBufferLength = cpu_to_le32(output_size);
2948
2949 iov[0].iov_base = (char *)req;
2950 /* 4 for RFC1001 length and 1 for Buffer */
2951 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2952
2953 iov[1].iov_base = (char *)(req->Buffer);
2954 iov[1].iov_len = len;
2955
2956 inc_rfc1001_len(req, len - 1 /* Buffer */);
2957
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002958 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002959 cifs_small_buf_release(req);
2960 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002961
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002962 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002963 if (rc == -ENODATA &&
2964 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002965 srch_inf->endOfSearch = true;
2966 rc = 0;
2967 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002968 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2969 goto qdir_exit;
2970 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002971
2972 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2973 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2974 info_buf_size);
2975 if (rc)
2976 goto qdir_exit;
2977
2978 srch_inf->unicode = true;
2979
2980 if (srch_inf->ntwrk_buf_start) {
2981 if (srch_inf->smallBuf)
2982 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2983 else
2984 cifs_buf_release(srch_inf->ntwrk_buf_start);
2985 }
2986 srch_inf->ntwrk_buf_start = (char *)rsp;
2987 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2988 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2989 /* 4 for rfc1002 length field */
2990 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2991 srch_inf->entries_in_buffer =
2992 num_entries(srch_inf->srch_entries_start, end_of_smb,
2993 &srch_inf->last_entry, info_buf_size);
2994 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002995 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2996 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2997 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002998 if (resp_buftype == CIFS_LARGE_BUFFER)
2999 srch_inf->smallBuf = false;
3000 else if (resp_buftype == CIFS_SMALL_BUFFER)
3001 srch_inf->smallBuf = true;
3002 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003003 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003004
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003005 return rc;
3006
3007qdir_exit:
3008 free_rsp_buf(resp_buftype, rsp);
3009 return rc;
3010}
3011
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003012static int
3013send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003014 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3015 u8 info_type, u32 additional_info, unsigned int num,
3016 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003017{
3018 struct smb2_set_info_req *req;
3019 struct smb2_set_info_rsp *rsp = NULL;
3020 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003021 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003022 int rc = 0;
3023 int resp_buftype;
3024 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003025 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003026 int flags = 0;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003027
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003028 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003029 return -EIO;
3030
3031 if (!num)
3032 return -EINVAL;
3033
3034 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3035 if (!iov)
3036 return -ENOMEM;
3037
3038 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3039 if (rc) {
3040 kfree(iov);
3041 return rc;
3042 }
3043
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003044 if (encryption_required(tcon))
3045 flags |= CIFS_TRANSFORM_REQ;
3046
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003047 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003048
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003049 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003050 req->FileInfoClass = info_class;
3051 req->PersistentFileId = persistent_fid;
3052 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003053 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003054
3055 /* 4 for RFC1001 length and 1 for Buffer */
3056 req->BufferOffset =
3057 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3058 req->BufferLength = cpu_to_le32(*size);
3059
3060 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3061
3062 memcpy(req->Buffer, *data, *size);
3063
3064 iov[0].iov_base = (char *)req;
3065 /* 4 for RFC1001 length */
3066 iov[0].iov_len = get_rfc1002_length(req) + 4;
3067
3068 for (i = 1; i < num; i++) {
3069 inc_rfc1001_len(req, size[i]);
3070 le32_add_cpu(&req->BufferLength, size[i]);
3071 iov[i].iov_base = (char *)data[i];
3072 iov[i].iov_len = size[i];
3073 }
3074
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003075 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003076 cifs_small_buf_release(req);
3077 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003078
Steve French7d3fb242013-11-18 09:56:28 -06003079 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003080 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003081
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003082 free_rsp_buf(resp_buftype, rsp);
3083 kfree(iov);
3084 return rc;
3085}
3086
3087int
3088SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3089 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3090{
3091 struct smb2_file_rename_info info;
3092 void **data;
3093 unsigned int size[2];
3094 int rc;
3095 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3096
3097 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3098 if (!data)
3099 return -ENOMEM;
3100
3101 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3102 /* 0 = fail if target already exists */
3103 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3104 info.FileNameLength = cpu_to_le32(len);
3105
3106 data[0] = &info;
3107 size[0] = sizeof(struct smb2_file_rename_info);
3108
3109 data[1] = target_file;
3110 size[1] = len + 2 /* null */;
3111
3112 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003113 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3114 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003115 kfree(data);
3116 return rc;
3117}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003118
3119int
Steve French897fba12016-05-12 21:20:36 -05003120SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3121 u64 persistent_fid, u64 volatile_fid)
3122{
3123 __u8 delete_pending = 1;
3124 void *data;
3125 unsigned int size;
3126
3127 data = &delete_pending;
3128 size = 1; /* sizeof __u8 */
3129
3130 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003131 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3132 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003133}
3134
3135int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003136SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3137 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3138{
3139 struct smb2_file_link_info info;
3140 void **data;
3141 unsigned int size[2];
3142 int rc;
3143 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3144
3145 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3146 if (!data)
3147 return -ENOMEM;
3148
3149 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3150 /* 0 = fail if link already exists */
3151 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3152 info.FileNameLength = cpu_to_le32(len);
3153
3154 data[0] = &info;
3155 size[0] = sizeof(struct smb2_file_link_info);
3156
3157 data[1] = target_file;
3158 size[1] = len + 2 /* null */;
3159
3160 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003161 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3162 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003163 kfree(data);
3164 return rc;
3165}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003166
3167int
3168SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003169 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003170{
3171 struct smb2_file_eof_info info;
3172 void *data;
3173 unsigned int size;
3174
3175 info.EndOfFile = *eof;
3176
3177 data = &info;
3178 size = sizeof(struct smb2_file_eof_info);
3179
Steve Frenchf29ebb42014-07-19 21:44:58 -05003180 if (is_falloc)
3181 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003182 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3183 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003184 else
3185 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003186 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3187 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003188}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003189
3190int
3191SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3192 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3193{
3194 unsigned int size;
3195 size = sizeof(FILE_BASIC_INFO);
3196 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003197 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3198 0, 1, (void **)&buf, &size);
3199}
3200
3201int
3202SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3203 u64 persistent_fid, u64 volatile_fid,
3204 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3205{
3206 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3207 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3208 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003209}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003210
3211int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003212SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3213 u64 persistent_fid, u64 volatile_fid,
3214 struct smb2_file_full_ea_info *buf, int len)
3215{
3216 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3217 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3218 0, 1, (void **)&buf, &len);
3219}
3220
3221int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003222SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3223 const u64 persistent_fid, const u64 volatile_fid,
3224 __u8 oplock_level)
3225{
3226 int rc;
3227 struct smb2_oplock_break *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003228 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003229
Joe Perchesf96637b2013-05-04 22:12:25 -05003230 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003231 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003232 if (rc)
3233 return rc;
3234
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003235 if (encryption_required(tcon))
3236 flags |= CIFS_TRANSFORM_REQ;
3237
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003238 req->VolatileFid = volatile_fid;
3239 req->PersistentFid = persistent_fid;
3240 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003241 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003242
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003243 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003244 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003245
3246 if (rc) {
3247 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003248 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003249 }
3250
3251 return rc;
3252}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003253
3254static void
3255copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3256 struct kstatfs *kst)
3257{
3258 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3259 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3260 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303261 kst->f_bfree = kst->f_bavail =
3262 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003263 return;
3264}
3265
3266static int
3267build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3268 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3269{
3270 int rc;
3271 struct smb2_query_info_req *req;
3272
Joe Perchesf96637b2013-05-04 22:12:25 -05003273 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003274
3275 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3276 return -EIO;
3277
3278 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3279 if (rc)
3280 return rc;
3281
3282 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3283 req->FileInfoClass = level;
3284 req->PersistentFileId = persistent_fid;
3285 req->VolatileFileId = volatile_fid;
3286 /* 4 for rfc1002 length field and 1 for pad */
3287 req->InputBufferOffset =
3288 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3289 req->OutputBufferLength = cpu_to_le32(
3290 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3291
3292 iov->iov_base = (char *)req;
3293 /* 4 for rfc1002 length field */
3294 iov->iov_len = get_rfc1002_length(req) + 4;
3295 return 0;
3296}
3297
3298int
3299SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3300 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3301{
3302 struct smb2_query_info_rsp *rsp = NULL;
3303 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003304 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003305 int rc = 0;
3306 int resp_buftype;
3307 struct cifs_ses *ses = tcon->ses;
3308 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003309 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003310
3311 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3312 sizeof(struct smb2_fs_full_size_info),
3313 persistent_fid, volatile_fid);
3314 if (rc)
3315 return rc;
3316
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003317 if (encryption_required(tcon))
3318 flags |= CIFS_TRANSFORM_REQ;
3319
3320 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003321 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003322 if (rc) {
3323 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003324 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003325 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003326 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003327
3328 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3329 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3330 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3331 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3332 sizeof(struct smb2_fs_full_size_info));
3333 if (!rc)
3334 copy_fs_info_to_kstatfs(info, fsdata);
3335
Steve French34f62642013-10-09 02:07:00 -05003336qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003337 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003338 return rc;
3339}
3340
3341int
3342SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003343 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003344{
3345 struct smb2_query_info_rsp *rsp = NULL;
3346 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003347 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003348 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003349 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003350 struct cifs_ses *ses = tcon->ses;
3351 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003352 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003353
Steven French21671142013-10-09 13:36:35 -05003354 if (level == FS_DEVICE_INFORMATION) {
3355 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3356 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3357 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3358 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3359 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003360 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3361 max_len = sizeof(struct smb3_fs_ss_info);
3362 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003363 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003364 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003365 return -EINVAL;
3366 }
3367
3368 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003369 persistent_fid, volatile_fid);
3370 if (rc)
3371 return rc;
3372
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003373 if (encryption_required(tcon))
3374 flags |= CIFS_TRANSFORM_REQ;
3375
3376 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003377 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003378 if (rc) {
3379 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3380 goto qfsattr_exit;
3381 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003382 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003383
3384 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3385 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003386 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3387 if (rc)
3388 goto qfsattr_exit;
3389
3390 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003391 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3392 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003393 rsp_len, max_len));
3394 else if (level == FS_DEVICE_INFORMATION)
3395 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3396 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003397 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3398 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3399 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3400 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3401 tcon->perf_sector_size =
3402 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3403 }
Steve French34f62642013-10-09 02:07:00 -05003404
3405qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003406 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003407 return rc;
3408}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003409
3410int
3411smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3412 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3413 const __u32 num_lock, struct smb2_lock_element *buf)
3414{
3415 int rc = 0;
3416 struct smb2_lock_req *req = NULL;
3417 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003418 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003419 int resp_buf_type;
3420 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003421 int flags = CIFS_NO_RESP;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003422
Joe Perchesf96637b2013-05-04 22:12:25 -05003423 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003424
3425 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3426 if (rc)
3427 return rc;
3428
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003429 if (encryption_required(tcon))
3430 flags |= CIFS_TRANSFORM_REQ;
3431
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003432 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003433 req->LockCount = cpu_to_le16(num_lock);
3434
3435 req->PersistentFileId = persist_fid;
3436 req->VolatileFileId = volatile_fid;
3437
3438 count = num_lock * sizeof(struct smb2_lock_element);
3439 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3440
3441 iov[0].iov_base = (char *)req;
3442 /* 4 for rfc1002 length field and count for all locks */
3443 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3444 iov[1].iov_base = (char *)buf;
3445 iov[1].iov_len = count;
3446
3447 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003448 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003449 &rsp_iov);
3450 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003451 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003452 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003453 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3454 }
3455
3456 return rc;
3457}
3458
3459int
3460SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3461 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3462 const __u64 length, const __u64 offset, const __u32 lock_flags,
3463 const bool wait)
3464{
3465 struct smb2_lock_element lock;
3466
3467 lock.Offset = cpu_to_le64(offset);
3468 lock.Length = cpu_to_le64(length);
3469 lock.Flags = cpu_to_le32(lock_flags);
3470 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3471 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3472
3473 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3474}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003475
3476int
3477SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3478 __u8 *lease_key, const __le32 lease_state)
3479{
3480 int rc;
3481 struct smb2_lease_ack *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003482 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003483
Joe Perchesf96637b2013-05-04 22:12:25 -05003484 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003485 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003486 if (rc)
3487 return rc;
3488
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003489 if (encryption_required(tcon))
3490 flags |= CIFS_TRANSFORM_REQ;
3491
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003492 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003493 req->StructureSize = cpu_to_le16(36);
3494 inc_rfc1001_len(req, 12);
3495
3496 memcpy(req->LeaseKey, lease_key, 16);
3497 req->LeaseState = lease_state;
3498
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003499 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003500 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003501
3502 if (rc) {
3503 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003504 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003505 }
3506
3507 return rc;
3508}