blob: b4c58a1db1ae6d6873ea0871d4e116295eadcf1a [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);
Steve French23586b62017-09-18 18:18:45 -0500442 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500443 + 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 French9764c022017-09-17 10:41:35 -0500494 if (strcmp(ses->server->vals->version_string,
495 SMB3ANY_VERSION_STRING) == 0) {
496 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
497 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
498 req->DialectCount = cpu_to_le16(2);
499 inc_rfc1001_len(req, 4);
500 } else if (strcmp(ses->server->vals->version_string,
501 SMBDEFAULT_VERSION_STRING) == 0) {
502 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
503 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
504 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
505 req->DialectCount = cpu_to_le16(3);
506 inc_rfc1001_len(req, 6);
507 } else {
508 /* otherwise send specific dialect */
509 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
510 req->DialectCount = cpu_to_le16(1);
511 inc_rfc1001_len(req, 2);
512 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400513
514 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400515 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500516 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400517 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500518 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400519 else
520 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400521
Steve Frenche4aa25e2012-10-01 12:26:22 -0500522 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400523
Steve French3c5f9be12014-05-13 13:37:45 -0700524 /* ClientGUID must be zero for SMB2.02 dialect */
525 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
526 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500527 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700528 memcpy(req->ClientGUID, server->client_guid,
529 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500530 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
531 assemble_neg_contexts(req);
532 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400533 iov[0].iov_base = (char *)req;
534 /* 4 for rfc1002 length field */
535 iov[0].iov_len = get_rfc1002_length(req) + 4;
536
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700537 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
538 cifs_small_buf_release(req);
539 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400540 /*
541 * No tcon so can't do
542 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
543 */
Steve French7e682f72017-08-31 21:34:24 -0500544 if (rc == -EOPNOTSUPP) {
545 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500546 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500547 " older servers\n");
548 goto neg_exit;
549 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400550 goto neg_exit;
551
Steve French9764c022017-09-17 10:41:35 -0500552 if (strcmp(ses->server->vals->version_string,
553 SMB3ANY_VERSION_STRING) == 0) {
554 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
555 cifs_dbg(VFS,
556 "SMB2 dialect returned but not requested\n");
557 return -EIO;
558 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
559 cifs_dbg(VFS,
560 "SMB2.1 dialect returned but not requested\n");
561 return -EIO;
562 }
563 } else if (strcmp(ses->server->vals->version_string,
564 SMBDEFAULT_VERSION_STRING) == 0) {
565 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
566 cifs_dbg(VFS,
567 "SMB2 dialect returned but not requested\n");
568 return -EIO;
569 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
570 /* ops set to 3.0 by default for default so update */
571 ses->server->ops = &smb21_operations;
572 }
Steve French590d08d2017-09-19 11:43:47 -0500573 } else if (le16_to_cpu(rsp->DialectRevision) !=
574 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500575 /* if requested single dialect ensure returned dialect matched */
576 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500577 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500578 return -EIO;
579 }
580
Joe Perchesf96637b2013-05-04 22:12:25 -0500581 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400582
Steve Frenche4aa25e2012-10-01 12:26:22 -0500583 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500584 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500585 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500586 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500587 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500588 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500589 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
590 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600591#ifdef CONFIG_CIFS_SMB311
592 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
593 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
594#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400595 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500596 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500597 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400598 rc = -EIO;
599 goto neg_exit;
600 }
601 server->dialect = le16_to_cpu(rsp->DialectRevision);
602
Steve French9764c022017-09-17 10:41:35 -0500603 /* BB: add check that dialect was valid given dialect(s) we asked for */
604
Jeff Laytone598d1d82013-05-26 07:00:59 -0400605 /* SMB2 only has an extended negflavor */
606 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400607 /* set it to the maximum buffer size value we can send with 1 credit */
608 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
609 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400610 server->max_read = le32_to_cpu(rsp->MaxReadSize);
611 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
612 /* BB Do we need to validate the SecurityMode? */
613 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
614 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400615 /* Internal types */
616 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400617
618 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
619 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500620 /*
621 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
622 * for us will be
623 * ses->sectype = RawNTLMSSP;
624 * but for time being this is our only auth choice so doesn't matter.
625 * We just found a server which sets blob length to zero expecting raw.
626 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700627 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500628 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700629 server->sec_ntlmssp = true;
630 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700631
Jeff Layton38d77c52013-05-26 07:01:00 -0400632 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400633 if (rc)
634 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500635 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500636 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500637 if (rc == 1)
638 rc = 0;
639 else if (rc == 0)
640 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400641 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400642neg_exit:
643 free_rsp_buf(resp_buftype, rsp);
644 return rc;
645}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400646
Steve Frenchff1c0382013-11-19 23:44:46 -0600647int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
648{
649 int rc = 0;
650 struct validate_negotiate_info_req vneg_inbuf;
651 struct validate_negotiate_info_rsp *pneg_rsp;
652 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500653 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600654
655 cifs_dbg(FYI, "validate negotiate\n");
656
657 /*
658 * validation ioctl must be signed, so no point sending this if we
659 * can not sign it. We could eventually change this to selectively
660 * sign just this, the first and only signed request on a connection.
661 * This is good enough for now since a user who wants better security
662 * would also enable signing on the mount. Having validation of
663 * negotiate info for signed connections helps reduce attack vectors
664 */
665 if (tcon->ses->server->sign == false)
666 return 0; /* validation requires signing */
667
668 vneg_inbuf.Capabilities =
669 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100670 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
671 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600672
673 if (tcon->ses->sign)
674 vneg_inbuf.SecurityMode =
675 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
676 else if (global_secflags & CIFSSEC_MAY_SIGN)
677 vneg_inbuf.SecurityMode =
678 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
679 else
680 vneg_inbuf.SecurityMode = 0;
681
Steve French9764c022017-09-17 10:41:35 -0500682
683 if (strcmp(tcon->ses->server->vals->version_string,
684 SMB3ANY_VERSION_STRING) == 0) {
685 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
686 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
687 vneg_inbuf.DialectCount = cpu_to_le16(2);
688 /* structure is big enough for 3 dialects, sending only 2 */
689 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
690 } else if (strcmp(tcon->ses->server->vals->version_string,
691 SMBDEFAULT_VERSION_STRING) == 0) {
692 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
693 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
694 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
695 vneg_inbuf.DialectCount = cpu_to_le16(3);
696 /* structure is big enough for 3 dialects */
697 inbuflen = sizeof(struct validate_negotiate_info_req);
698 } else {
699 /* otherwise specific dialect was requested */
700 vneg_inbuf.Dialects[0] =
701 cpu_to_le16(tcon->ses->server->vals->protocol_id);
702 vneg_inbuf.DialectCount = cpu_to_le16(1);
703 /* structure is big enough for 3 dialects, sending only 1 */
704 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
705 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600706
707 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
708 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100709 false /* use_ipc */,
Steve Frenchff1c0382013-11-19 23:44:46 -0600710 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
711 (char **)&pneg_rsp, &rsplen);
712
713 if (rc != 0) {
714 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
715 return -EIO;
716 }
717
718 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500719 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
720 rsplen);
721
722 /* relax check since Mac returns max bufsize allowed on ioctl */
723 if (rsplen > CIFSMaxBufSize)
724 return -EIO;
Steve Frenchff1c0382013-11-19 23:44:46 -0600725 }
726
727 /* check validate negotiate info response matches what we got earlier */
728 if (pneg_rsp->Dialect !=
729 cpu_to_le16(tcon->ses->server->vals->protocol_id))
730 goto vneg_out;
731
732 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
733 goto vneg_out;
734
735 /* do not validate server guid because not saved at negprot time yet */
736
737 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
738 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
739 goto vneg_out;
740
741 /* validate negotiate successful */
742 cifs_dbg(FYI, "validate negotiate info successful\n");
743 return 0;
744
745vneg_out:
746 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
747 return -EIO;
748}
749
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530750enum securityEnum
751smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
752{
753 switch (requested) {
754 case Kerberos:
755 case RawNTLMSSP:
756 return requested;
757 case NTLMv2:
758 return RawNTLMSSP;
759 case Unspecified:
760 if (server->sec_ntlmssp &&
761 (global_secflags & CIFSSEC_MAY_NTLMSSP))
762 return RawNTLMSSP;
763 if ((server->sec_kerberos || server->sec_mskerberos) &&
764 (global_secflags & CIFSSEC_MAY_KRB5))
765 return Kerberos;
766 /* Fallthrough */
767 default:
768 return Unspecified;
769 }
770}
771
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100772struct SMB2_sess_data {
773 unsigned int xid;
774 struct cifs_ses *ses;
775 struct nls_table *nls_cp;
776 void (*func)(struct SMB2_sess_data *);
777 int result;
778 u64 previous_session;
779
780 /* we will send the SMB in three pieces:
781 * a fixed length beginning part, an optional
782 * SPNEGO blob (which can be zero length), and a
783 * last part which will include the strings
784 * and rest of bcc area. This allows us to avoid
785 * a large buffer 17K allocation
786 */
787 int buf0_type;
788 struct kvec iov[2];
789};
790
791static int
792SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
793{
794 int rc;
795 struct cifs_ses *ses = sess_data->ses;
796 struct smb2_sess_setup_req *req;
797 struct TCP_Server_Info *server = ses->server;
798
799 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
800 if (rc)
801 return rc;
802
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700803 /* First session, not a reauthenticate */
804 req->hdr.sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100805
806 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
807 req->PreviousSessionId = sess_data->previous_session;
808
809 req->Flags = 0; /* MBZ */
810 /* to enable echos and oplocks */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700811 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100812
813 /* only one of SMB2 signing flags may be set in SMB2 request */
814 if (server->sign)
815 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
816 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
817 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
818 else
819 req->SecurityMode = 0;
820
821 req->Capabilities = 0;
822 req->Channel = 0; /* MBZ */
823
824 sess_data->iov[0].iov_base = (char *)req;
825 /* 4 for rfc1002 length field and 1 for pad */
826 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
827 /*
828 * This variable will be used to clear the buffer
829 * allocated above in case of any error in the calling function.
830 */
831 sess_data->buf0_type = CIFS_SMALL_BUFFER;
832
833 return 0;
834}
835
836static void
837SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
838{
839 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
840 sess_data->buf0_type = CIFS_NO_BUFFER;
841}
842
843static int
844SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
845{
846 int rc;
847 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700848 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100849
850 /* Testing shows that buffer offset must be at location of Buffer[0] */
851 req->SecurityBufferOffset =
852 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
853 1 /* pad */ - 4 /* rfc1001 len */);
854 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
855
856 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
857
858 /* BB add code to build os and lm fields */
859
860 rc = SendReceive2(sess_data->xid, sess_data->ses,
861 sess_data->iov, 2,
862 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700863 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
864 cifs_small_buf_release(sess_data->iov[0].iov_base);
865 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100866
867 return rc;
868}
869
870static int
871SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
872{
873 int rc = 0;
874 struct cifs_ses *ses = sess_data->ses;
875
876 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800877 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100878 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100879 if (rc) {
880 cifs_dbg(FYI,
881 "SMB3 session key generation failed\n");
882 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800883 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100884 }
885 }
886 if (!ses->server->session_estab) {
887 ses->server->sequence_number = 0x2;
888 ses->server->session_estab = true;
889 }
890 mutex_unlock(&ses->server->srv_mutex);
891
892 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
893 spin_lock(&GlobalMid_Lock);
894 ses->status = CifsGood;
895 ses->need_reconnect = false;
896 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100897 return rc;
898}
899
900#ifdef CONFIG_CIFS_UPCALL
901static void
902SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
903{
904 int rc;
905 struct cifs_ses *ses = sess_data->ses;
906 struct cifs_spnego_msg *msg;
907 struct key *spnego_key = NULL;
908 struct smb2_sess_setup_rsp *rsp = NULL;
909
910 rc = SMB2_sess_alloc_buffer(sess_data);
911 if (rc)
912 goto out;
913
914 spnego_key = cifs_get_spnego_key(ses);
915 if (IS_ERR(spnego_key)) {
916 rc = PTR_ERR(spnego_key);
917 spnego_key = NULL;
918 goto out;
919 }
920
921 msg = spnego_key->payload.data[0];
922 /*
923 * check version field to make sure that cifs.upcall is
924 * sending us a response in an expected form
925 */
926 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
927 cifs_dbg(VFS,
928 "bad cifs.upcall version. Expected %d got %d",
929 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
930 rc = -EKEYREJECTED;
931 goto out_put_spnego_key;
932 }
933
934 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
935 GFP_KERNEL);
936 if (!ses->auth_key.response) {
937 cifs_dbg(VFS,
938 "Kerberos can't allocate (%u bytes) memory",
939 msg->sesskey_len);
940 rc = -ENOMEM;
941 goto out_put_spnego_key;
942 }
943 ses->auth_key.len = msg->sesskey_len;
944
945 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
946 sess_data->iov[1].iov_len = msg->secblob_len;
947
948 rc = SMB2_sess_sendreceive(sess_data);
949 if (rc)
950 goto out_put_spnego_key;
951
952 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700953 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100954
955 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100956
957 rc = SMB2_sess_establish_session(sess_data);
958out_put_spnego_key:
959 key_invalidate(spnego_key);
960 key_put(spnego_key);
961out:
962 sess_data->result = rc;
963 sess_data->func = NULL;
964 SMB2_sess_free_buffer(sess_data);
965}
966#else
967static void
968SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
969{
970 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
971 sess_data->result = -EOPNOTSUPP;
972 sess_data->func = NULL;
973}
974#endif
975
Sachin Prabhu166cea42016-10-07 19:11:22 +0100976static void
977SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
978
979static void
980SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
981{
982 int rc;
983 struct cifs_ses *ses = sess_data->ses;
984 struct smb2_sess_setup_rsp *rsp = NULL;
985 char *ntlmssp_blob = NULL;
986 bool use_spnego = false; /* else use raw ntlmssp */
987 u16 blob_length = 0;
988
989 /*
990 * If memory allocation is successful, caller of this function
991 * frees it.
992 */
993 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
994 if (!ses->ntlmssp) {
995 rc = -ENOMEM;
996 goto out_err;
997 }
998 ses->ntlmssp->sesskey_per_smbsess = true;
999
1000 rc = SMB2_sess_alloc_buffer(sess_data);
1001 if (rc)
1002 goto out_err;
1003
1004 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1005 GFP_KERNEL);
1006 if (ntlmssp_blob == NULL) {
1007 rc = -ENOMEM;
1008 goto out;
1009 }
1010
1011 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1012 if (use_spnego) {
1013 /* BB eventually need to add this */
1014 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1015 rc = -EOPNOTSUPP;
1016 goto out;
1017 } else {
1018 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1019 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1020 }
1021 sess_data->iov[1].iov_base = ntlmssp_blob;
1022 sess_data->iov[1].iov_len = blob_length;
1023
1024 rc = SMB2_sess_sendreceive(sess_data);
1025 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1026
1027 /* If true, rc here is expected and not an error */
1028 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001029 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001030 rc = 0;
1031
1032 if (rc)
1033 goto out;
1034
1035 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1036 le16_to_cpu(rsp->SecurityBufferOffset)) {
1037 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1038 le16_to_cpu(rsp->SecurityBufferOffset));
1039 rc = -EIO;
1040 goto out;
1041 }
1042 rc = decode_ntlmssp_challenge(rsp->Buffer,
1043 le16_to_cpu(rsp->SecurityBufferLength), ses);
1044 if (rc)
1045 goto out;
1046
1047 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1048
1049
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001050 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001051 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001052
1053out:
1054 kfree(ntlmssp_blob);
1055 SMB2_sess_free_buffer(sess_data);
1056 if (!rc) {
1057 sess_data->result = 0;
1058 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1059 return;
1060 }
1061out_err:
1062 kfree(ses->ntlmssp);
1063 ses->ntlmssp = NULL;
1064 sess_data->result = rc;
1065 sess_data->func = NULL;
1066}
1067
1068static void
1069SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1070{
1071 int rc;
1072 struct cifs_ses *ses = sess_data->ses;
1073 struct smb2_sess_setup_req *req;
1074 struct smb2_sess_setup_rsp *rsp = NULL;
1075 unsigned char *ntlmssp_blob = NULL;
1076 bool use_spnego = false; /* else use raw ntlmssp */
1077 u16 blob_length = 0;
1078
1079 rc = SMB2_sess_alloc_buffer(sess_data);
1080 if (rc)
1081 goto out;
1082
1083 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001084 req->hdr.sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001085
1086 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1087 sess_data->nls_cp);
1088 if (rc) {
1089 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1090 goto out;
1091 }
1092
1093 if (use_spnego) {
1094 /* BB eventually need to add this */
1095 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1096 rc = -EOPNOTSUPP;
1097 goto out;
1098 }
1099 sess_data->iov[1].iov_base = ntlmssp_blob;
1100 sess_data->iov[1].iov_len = blob_length;
1101
1102 rc = SMB2_sess_sendreceive(sess_data);
1103 if (rc)
1104 goto out;
1105
1106 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1107
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001108 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001109 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001110
1111 rc = SMB2_sess_establish_session(sess_data);
1112out:
1113 kfree(ntlmssp_blob);
1114 SMB2_sess_free_buffer(sess_data);
1115 kfree(ses->ntlmssp);
1116 ses->ntlmssp = NULL;
1117 sess_data->result = rc;
1118 sess_data->func = NULL;
1119}
1120
1121static int
1122SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1123{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301124 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001125
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301126 type = smb2_select_sectype(ses->server, ses->sectype);
1127 cifs_dbg(FYI, "sess setup type %d\n", type);
1128 if (type == Unspecified) {
1129 cifs_dbg(VFS,
1130 "Unable to select appropriate authentication method!");
1131 return -EINVAL;
1132 }
1133
1134 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001135 case Kerberos:
1136 sess_data->func = SMB2_auth_kerberos;
1137 break;
1138 case RawNTLMSSP:
1139 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1140 break;
1141 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301142 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001143 return -EOPNOTSUPP;
1144 }
1145
1146 return 0;
1147}
1148
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001149int
1150SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1151 const struct nls_table *nls_cp)
1152{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001153 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001154 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001155 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001156
Joe Perchesf96637b2013-05-04 22:12:25 -05001157 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001158
Jeff Layton3534b852013-05-24 07:41:01 -04001159 if (!server) {
1160 WARN(1, "%s: server is NULL!\n", __func__);
1161 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001162 }
1163
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001164 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1165 if (!sess_data)
1166 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001167
1168 rc = SMB2_select_sec(ses, sess_data);
1169 if (rc)
1170 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001171 sess_data->xid = xid;
1172 sess_data->ses = ses;
1173 sess_data->buf0_type = CIFS_NO_BUFFER;
1174 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001175
Sachin Prabhu166cea42016-10-07 19:11:22 +01001176 while (sess_data->func)
1177 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001178
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001179 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001180out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001181 kfree(sess_data);
1182 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001183}
1184
1185int
1186SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1187{
1188 struct smb2_logoff_req *req; /* response is also trivial struct */
1189 int rc = 0;
1190 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001191 int flags = 0;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001192
Joe Perchesf96637b2013-05-04 22:12:25 -05001193 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001194
1195 if (ses && (ses->server))
1196 server = ses->server;
1197 else
1198 return -EIO;
1199
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001200 /* no need to send SMB logoff if uid already closed due to reconnect */
1201 if (ses->need_reconnect)
1202 goto smb2_session_already_dead;
1203
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001204 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1205 if (rc)
1206 return rc;
1207
1208 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001209 req->hdr.sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001210
1211 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1212 flags |= CIFS_TRANSFORM_REQ;
1213 else if (server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001214 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001215
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001216 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001217 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001218 /*
1219 * No tcon so can't do
1220 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1221 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001222
1223smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001224 return rc;
1225}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001226
1227static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1228{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001229 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001230}
1231
1232#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1233
Steve Frenchde9f68df2013-11-15 11:26:24 -06001234/* These are similar values to what Windows uses */
1235static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1236{
1237 tcon->max_chunks = 256;
1238 tcon->max_bytes_chunk = 1048576;
1239 tcon->max_bytes_copy = 16777216;
1240}
1241
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001242int
1243SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1244 struct cifs_tcon *tcon, const struct nls_table *cp)
1245{
1246 struct smb2_tree_connect_req *req;
1247 struct smb2_tree_connect_rsp *rsp = NULL;
1248 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001249 struct kvec rsp_iov;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001250 int rc = 0;
1251 int resp_buftype;
1252 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001253 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001254 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001255
Joe Perchesf96637b2013-05-04 22:12:25 -05001256 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001257
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001258 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001259 return -EIO;
1260
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001261 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1262 if (unc_path == NULL)
1263 return -ENOMEM;
1264
1265 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1266 unc_path_len *= 2;
1267 if (unc_path_len < 2) {
1268 kfree(unc_path);
1269 return -EINVAL;
1270 }
1271
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001272 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1273 if (tcon)
1274 tcon->tid = 0;
1275
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001276 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1277 if (rc) {
1278 kfree(unc_path);
1279 return rc;
1280 }
1281
1282 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001283 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1284 flags |= CIFS_TRANSFORM_REQ;
1285
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001286 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001287 req->hdr.sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001288 if (ses->server->sign)
1289 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001290 } else if (encryption_required(tcon))
1291 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001292
1293 iov[0].iov_base = (char *)req;
1294 /* 4 for rfc1002 length field and 1 for pad */
1295 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1296
1297 /* Testing shows that buffer offset must be at location of Buffer[0] */
1298 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1299 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1300 req->PathLength = cpu_to_le16(unc_path_len - 2);
1301 iov[1].iov_base = unc_path;
1302 iov[1].iov_len = unc_path_len;
1303
1304 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1305
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001306 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001307 cifs_small_buf_release(req);
1308 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001309
1310 if (rc != 0) {
1311 if (tcon) {
1312 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1313 tcon->need_reconnect = true;
1314 }
1315 goto tcon_error_exit;
1316 }
1317
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001318 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001319 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001320 goto tcon_exit;
1321 }
1322
Christophe JAILLETcd123002017-05-12 17:59:32 +02001323 switch (rsp->ShareType) {
1324 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001325 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001326 break;
1327 case SMB2_SHARE_TYPE_PIPE:
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001328 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001329 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001330 break;
1331 case SMB2_SHARE_TYPE_PRINT:
1332 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001333 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001334 break;
1335 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001336 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001337 rc = -EOPNOTSUPP;
1338 goto tcon_error_exit;
1339 }
1340
1341 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001342 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001343 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1344 tcon->tidStatus = CifsGood;
1345 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001346 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001347 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001348
1349 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1350 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001351 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001352
1353 if (tcon->seal &&
1354 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1355 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1356
Steve Frenchde9f68df2013-11-15 11:26:24 -06001357 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001358 if (tcon->ses->server->ops->validate_negotiate)
1359 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001360tcon_exit:
1361 free_rsp_buf(resp_buftype, rsp);
1362 kfree(unc_path);
1363 return rc;
1364
1365tcon_error_exit:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001366 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001367 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001368 }
1369 goto tcon_exit;
1370}
1371
1372int
1373SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1374{
1375 struct smb2_tree_disconnect_req *req; /* response is trivial */
1376 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001377 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001378 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001379
Joe Perchesf96637b2013-05-04 22:12:25 -05001380 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001381
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001382 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001383 return -EIO;
1384
1385 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1386 return 0;
1387
1388 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1389 if (rc)
1390 return rc;
1391
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001392 if (encryption_required(tcon))
1393 flags |= CIFS_TRANSFORM_REQ;
1394
1395 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001396 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001397 if (rc)
1398 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1399
1400 return rc;
1401}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001402
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001403
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001404static struct create_durable *
1405create_durable_buf(void)
1406{
1407 struct create_durable *buf;
1408
1409 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1410 if (!buf)
1411 return NULL;
1412
1413 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001414 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001415 buf->ccontext.DataLength = cpu_to_le32(16);
1416 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1417 (struct create_durable, Name));
1418 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001419 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001420 buf->Name[0] = 'D';
1421 buf->Name[1] = 'H';
1422 buf->Name[2] = 'n';
1423 buf->Name[3] = 'Q';
1424 return buf;
1425}
1426
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001427static struct create_durable *
1428create_reconnect_durable_buf(struct cifs_fid *fid)
1429{
1430 struct create_durable *buf;
1431
1432 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1433 if (!buf)
1434 return NULL;
1435
1436 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1437 (struct create_durable, Data));
1438 buf->ccontext.DataLength = cpu_to_le32(16);
1439 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1440 (struct create_durable, Name));
1441 buf->ccontext.NameLength = cpu_to_le16(4);
1442 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1443 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001444 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001445 buf->Name[0] = 'D';
1446 buf->Name[1] = 'H';
1447 buf->Name[2] = 'n';
1448 buf->Name[3] = 'C';
1449 return buf;
1450}
1451
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001452static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001453parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1454 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001455{
1456 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001457 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001458 unsigned int next;
1459 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001460 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001461
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001462 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001463 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001464 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001465 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001466 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001467 if (le16_to_cpu(cc->NameLength) == 4 &&
1468 strncmp(name, "RqLs", 4) == 0)
1469 return server->ops->parse_lease_buf(cc, epoch);
1470
1471 next = le32_to_cpu(cc->Next);
1472 if (!next)
1473 break;
1474 remaining -= next;
1475 cc = (struct create_context *)((char *)cc + next);
1476 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001477
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001478 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001479}
1480
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001481static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001482add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1483 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001484{
1485 struct smb2_create_req *req = iov[0].iov_base;
1486 unsigned int num = *num_iovec;
1487
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001488 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001489 if (iov[num].iov_base == NULL)
1490 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001491 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001492 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1493 if (!req->CreateContextsOffset)
1494 req->CreateContextsOffset = cpu_to_le32(
1495 sizeof(struct smb2_create_req) - 4 +
1496 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001497 le32_add_cpu(&req->CreateContextsLength,
1498 server->vals->create_lease_size);
1499 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001500 *num_iovec = num + 1;
1501 return 0;
1502}
1503
Steve Frenchb56eae42015-11-03 09:26:27 -06001504static struct create_durable_v2 *
1505create_durable_v2_buf(struct cifs_fid *pfid)
1506{
1507 struct create_durable_v2 *buf;
1508
1509 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1510 if (!buf)
1511 return NULL;
1512
1513 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1514 (struct create_durable_v2, dcontext));
1515 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1516 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1517 (struct create_durable_v2, Name));
1518 buf->ccontext.NameLength = cpu_to_le16(4);
1519
1520 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1521 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001522 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001523 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1524
1525 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1526 buf->Name[0] = 'D';
1527 buf->Name[1] = 'H';
1528 buf->Name[2] = '2';
1529 buf->Name[3] = 'Q';
1530 return buf;
1531}
1532
1533static struct create_durable_handle_reconnect_v2 *
1534create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1535{
1536 struct create_durable_handle_reconnect_v2 *buf;
1537
1538 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1539 GFP_KERNEL);
1540 if (!buf)
1541 return NULL;
1542
1543 buf->ccontext.DataOffset =
1544 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1545 dcontext));
1546 buf->ccontext.DataLength =
1547 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1548 buf->ccontext.NameOffset =
1549 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1550 Name));
1551 buf->ccontext.NameLength = cpu_to_le16(4);
1552
1553 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1554 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1555 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1556 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1557
1558 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1559 buf->Name[0] = 'D';
1560 buf->Name[1] = 'H';
1561 buf->Name[2] = '2';
1562 buf->Name[3] = 'C';
1563 return buf;
1564}
1565
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001566static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001567add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001568 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001569{
1570 struct smb2_create_req *req = iov[0].iov_base;
1571 unsigned int num = *num_iovec;
1572
Steve Frenchb56eae42015-11-03 09:26:27 -06001573 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1574 if (iov[num].iov_base == NULL)
1575 return -ENOMEM;
1576 iov[num].iov_len = sizeof(struct create_durable_v2);
1577 if (!req->CreateContextsOffset)
1578 req->CreateContextsOffset =
1579 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1580 iov[1].iov_len);
1581 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1582 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1583 *num_iovec = num + 1;
1584 return 0;
1585}
1586
1587static int
1588add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1589 struct cifs_open_parms *oparms)
1590{
1591 struct smb2_create_req *req = iov[0].iov_base;
1592 unsigned int num = *num_iovec;
1593
1594 /* indicate that we don't need to relock the file */
1595 oparms->reconnect = false;
1596
1597 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1598 if (iov[num].iov_base == NULL)
1599 return -ENOMEM;
1600 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1601 if (!req->CreateContextsOffset)
1602 req->CreateContextsOffset =
1603 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1604 iov[1].iov_len);
1605 le32_add_cpu(&req->CreateContextsLength,
1606 sizeof(struct create_durable_handle_reconnect_v2));
1607 inc_rfc1001_len(&req->hdr,
1608 sizeof(struct create_durable_handle_reconnect_v2));
1609 *num_iovec = num + 1;
1610 return 0;
1611}
1612
1613static int
1614add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1615 struct cifs_open_parms *oparms, bool use_persistent)
1616{
1617 struct smb2_create_req *req = iov[0].iov_base;
1618 unsigned int num = *num_iovec;
1619
1620 if (use_persistent) {
1621 if (oparms->reconnect)
1622 return add_durable_reconnect_v2_context(iov, num_iovec,
1623 oparms);
1624 else
1625 return add_durable_v2_context(iov, num_iovec, oparms);
1626 }
1627
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001628 if (oparms->reconnect) {
1629 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1630 /* indicate that we don't need to relock the file */
1631 oparms->reconnect = false;
1632 } else
1633 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001634 if (iov[num].iov_base == NULL)
1635 return -ENOMEM;
1636 iov[num].iov_len = sizeof(struct create_durable);
1637 if (!req->CreateContextsOffset)
1638 req->CreateContextsOffset =
1639 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1640 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001641 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001642 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1643 *num_iovec = num + 1;
1644 return 0;
1645}
1646
Aurelien Aptelf0712922017-02-22 14:47:17 +01001647static int
1648alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1649 const char *treename, const __le16 *path)
1650{
1651 int treename_len, path_len;
1652 struct nls_table *cp;
1653 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1654
1655 /*
1656 * skip leading "\\"
1657 */
1658 treename_len = strlen(treename);
1659 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1660 return -EINVAL;
1661
1662 treename += 2;
1663 treename_len -= 2;
1664
1665 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1666
1667 /*
1668 * make room for one path separator between the treename and
1669 * path
1670 */
1671 *out_len = treename_len + 1 + path_len;
1672
1673 /*
1674 * final path needs to be null-terminated UTF16 with a
1675 * size aligned to 8
1676 */
1677
1678 *out_size = roundup((*out_len+1)*2, 8);
1679 *out_path = kzalloc(*out_size, GFP_KERNEL);
1680 if (!*out_path)
1681 return -ENOMEM;
1682
1683 cp = load_nls_default();
1684 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1685 UniStrcat(*out_path, sep);
1686 UniStrcat(*out_path, path);
1687 unload_nls(cp);
1688
1689 return 0;
1690}
1691
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001692int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001693SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001694 __u8 *oplock, struct smb2_file_all_info *buf,
1695 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001696{
1697 struct smb2_create_req *req;
1698 struct smb2_create_rsp *rsp;
1699 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001700 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001701 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001702 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001703 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001704 int resp_buftype;
1705 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001706 __le16 *copy_path = NULL;
1707 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001708 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001709 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001710 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001711 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001712 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001713
Joe Perchesf96637b2013-05-04 22:12:25 -05001714 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001715
1716 if (ses && (ses->server))
1717 server = ses->server;
1718 else
1719 return -EIO;
1720
1721 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1722 if (rc)
1723 return rc;
1724
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001725 if (encryption_required(tcon))
1726 flags |= CIFS_TRANSFORM_REQ;
1727
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001728 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001729 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001730 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1731 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001732
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001733 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001734 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001735 /* File attributes ignored on open (used in create though) */
1736 req->FileAttributes = cpu_to_le32(file_attributes);
1737 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001738 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1739 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001740
1741 iov[0].iov_base = (char *)req;
1742 /* 4 for rfc1002 length field */
1743 iov[0].iov_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001744 /* -1 since last byte is buf[0] which is sent below (path) */
1745 iov[0].iov_len--;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001746
Aurelien Aptelf0712922017-02-22 14:47:17 +01001747 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1748
1749 /* [MS-SMB2] 2.2.13 NameOffset:
1750 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1751 * the SMB2 header, the file name includes a prefix that will
1752 * be processed during DFS name normalization as specified in
1753 * section 3.3.5.9. Otherwise, the file name is relative to
1754 * the share that is identified by the TreeId in the SMB2
1755 * header.
1756 */
1757 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1758 int name_len;
1759
1760 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1761 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1762 &name_len,
1763 tcon->treeName, path);
1764 if (rc)
1765 return rc;
1766 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001767 uni_path_len = copy_size;
1768 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001769 } else {
1770 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1771 /* MUST set path len (NameLength) to 0 opening root of share */
1772 req->NameLength = cpu_to_le16(uni_path_len - 2);
1773 if (uni_path_len % 8 != 0) {
1774 copy_size = roundup(uni_path_len, 8);
1775 copy_path = kzalloc(copy_size, GFP_KERNEL);
1776 if (!copy_path)
1777 return -ENOMEM;
1778 memcpy((char *)copy_path, (const char *)path,
1779 uni_path_len);
1780 uni_path_len = copy_size;
1781 path = copy_path;
1782 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001783 }
1784
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001785 iov[1].iov_len = uni_path_len;
1786 iov[1].iov_base = path;
1787 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1788 inc_rfc1001_len(req, uni_path_len - 1);
1789
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001790 if (!server->oplocks)
1791 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1792
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001793 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001794 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1795 req->RequestedOplockLevel = *oplock;
1796 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001797 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001798 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001799 cifs_small_buf_release(req);
1800 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001801 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001802 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001803 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001804 }
1805
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001806 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1807 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001808 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001809 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001810 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001811 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001812 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001813 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001814
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001815 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001816 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001817 if (rc) {
1818 cifs_small_buf_release(req);
1819 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001820 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001821 return rc;
1822 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001823 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001824 }
1825
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001826 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001827 cifs_small_buf_release(req);
1828 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001829
1830 if (rc != 0) {
1831 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001832 if (err_buf && rsp)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001833 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1834 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001835 goto creat_exit;
1836 }
1837
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001838 oparms->fid->persistent_fid = rsp->PersistentFileId;
1839 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001840
1841 if (buf) {
1842 memcpy(buf, &rsp->CreationTime, 32);
1843 buf->AllocationSize = rsp->AllocationSize;
1844 buf->EndOfFile = rsp->EndofFile;
1845 buf->Attributes = rsp->FileAttributes;
1846 buf->NumberOfLinks = cpu_to_le32(1);
1847 buf->DeletePending = 0;
1848 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001849
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001850 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001851 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001852 else
1853 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001854creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001855 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001856 kfree(lc_buf);
1857 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001858 free_rsp_buf(resp_buftype, rsp);
1859 return rc;
1860}
1861
Steve French4a72daf2013-06-25 00:20:49 -05001862/*
1863 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1864 */
1865int
1866SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001867 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1868 char *in_data, u32 indatalen,
1869 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001870{
1871 struct smb2_ioctl_req *req;
1872 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001873 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001874 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001875 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001876 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001877 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001878 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001879 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001880 int flags = 0;
Steve French4a72daf2013-06-25 00:20:49 -05001881
1882 cifs_dbg(FYI, "SMB2 IOCTL\n");
1883
Steve French3d1a3742014-08-11 21:05:25 -05001884 if (out_data != NULL)
1885 *out_data = NULL;
1886
Steve French4a72daf2013-06-25 00:20:49 -05001887 /* zero out returned data len, in case of error */
1888 if (plen)
1889 *plen = 0;
1890
Steve French8e353102015-03-26 19:47:02 -05001891 if (tcon)
1892 ses = tcon->ses;
1893 else
1894 return -EIO;
1895
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001896 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05001897 return -EIO;
1898
1899 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1900 if (rc)
1901 return rc;
1902
Aurelien Aptel51146622017-02-28 15:08:41 +01001903 if (use_ipc) {
1904 if (ses->ipc_tid == 0) {
1905 cifs_small_buf_release(req);
1906 return -ENOTCONN;
1907 }
1908
1909 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1910 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1911 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1912 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001913 if (encryption_required(tcon))
1914 flags |= CIFS_TRANSFORM_REQ;
1915
Steve French4a72daf2013-06-25 00:20:49 -05001916 req->CtlCode = cpu_to_le32(opcode);
1917 req->PersistentFileId = persistent_fid;
1918 req->VolatileFileId = volatile_fid;
1919
1920 if (indatalen) {
1921 req->InputCount = cpu_to_le32(indatalen);
1922 /* do not set InputOffset if no input data */
1923 req->InputOffset =
1924 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1925 iov[1].iov_base = in_data;
1926 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001927 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001928 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001929 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001930
1931 req->OutputOffset = 0;
1932 req->OutputCount = 0; /* MBZ */
1933
1934 /*
1935 * Could increase MaxOutputResponse, but that would require more
1936 * than one credit. Windows typically sets this smaller, but for some
1937 * ioctls it may be useful to allow server to send more. No point
1938 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001939 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1940 * (by default, note that it can be overridden to make max larger)
1941 * in responses (except for read responses which can be bigger.
1942 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001943 */
Steve French7db0a6e2017-05-03 21:12:20 -05001944 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001945
1946 if (is_fsctl)
1947 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1948 else
1949 req->Flags = 0;
1950
1951 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001952
Steve French7ff8d452013-10-14 00:44:19 -05001953 /*
1954 * If no input data, the size of ioctl struct in
1955 * protocol spec still includes a 1 byte data buffer,
1956 * but if input data passed to ioctl, we do not
1957 * want to double count this, so we do not send
1958 * the dummy one byte of data in iovec[0] if sending
1959 * input data (in iovec[1]). We also must add 4 bytes
1960 * in first iovec to allow for rfc1002 length field.
1961 */
1962
1963 if (indatalen) {
1964 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1965 inc_rfc1001_len(req, indatalen - 1);
1966 } else
1967 iov[0].iov_len = get_rfc1002_length(req) + 4;
1968
Steve French4a72daf2013-06-25 00:20:49 -05001969
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001970 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001971 cifs_small_buf_release(req);
1972 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001973
Steve French9bf0c9c2013-11-16 18:05:28 -06001974 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001975 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001976 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001977 } else if (rc == -EINVAL) {
1978 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1979 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001980 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001981 goto ioctl_exit;
1982 }
Steve French4a72daf2013-06-25 00:20:49 -05001983 }
1984
1985 /* check if caller wants to look at return data or just return rc */
1986 if ((plen == NULL) || (out_data == NULL))
1987 goto ioctl_exit;
1988
1989 *plen = le32_to_cpu(rsp->OutputCount);
1990
1991 /* We check for obvious errors in the output buffer length and offset */
1992 if (*plen == 0)
1993 goto ioctl_exit; /* server returned no data */
1994 else if (*plen > 0xFF00) {
1995 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1996 *plen = 0;
1997 rc = -EIO;
1998 goto ioctl_exit;
1999 }
2000
2001 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2002 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2003 le32_to_cpu(rsp->OutputOffset));
2004 *plen = 0;
2005 rc = -EIO;
2006 goto ioctl_exit;
2007 }
2008
2009 *out_data = kmalloc(*plen, GFP_KERNEL);
2010 if (*out_data == NULL) {
2011 rc = -ENOMEM;
2012 goto ioctl_exit;
2013 }
2014
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002015 shdr = get_sync_hdr(rsp);
2016 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002017ioctl_exit:
2018 free_rsp_buf(resp_buftype, rsp);
2019 return rc;
2020}
2021
Steve French64a5cfa2013-10-14 15:31:32 -05002022/*
2023 * Individual callers to ioctl worker function follow
2024 */
2025
2026int
2027SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2028 u64 persistent_fid, u64 volatile_fid)
2029{
2030 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002031 struct compress_ioctl fsctl_input;
2032 char *ret_data = NULL;
2033
2034 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002035 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002036
2037 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2038 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002039 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05002040 (char *)&fsctl_input /* data input */,
2041 2 /* in data len */, &ret_data /* out data */, NULL);
2042
2043 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002044
2045 return rc;
2046}
2047
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002048int
2049SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2050 u64 persistent_fid, u64 volatile_fid)
2051{
2052 struct smb2_close_req *req;
2053 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002054 struct cifs_ses *ses = tcon->ses;
2055 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002056 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002057 int resp_buftype;
2058 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002059 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002060
Joe Perchesf96637b2013-05-04 22:12:25 -05002061 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002062
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002063 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002064 return -EIO;
2065
2066 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
2067 if (rc)
2068 return rc;
2069
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002070 if (encryption_required(tcon))
2071 flags |= CIFS_TRANSFORM_REQ;
2072
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002073 req->PersistentFileId = persistent_fid;
2074 req->VolatileFileId = volatile_fid;
2075
2076 iov[0].iov_base = (char *)req;
2077 /* 4 for rfc1002 length field */
2078 iov[0].iov_len = get_rfc1002_length(req) + 4;
2079
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002080 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002081 cifs_small_buf_release(req);
2082 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002083
2084 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002085 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002086 goto close_exit;
2087 }
2088
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002089 /* BB FIXME - decode close response, update inode for caching */
2090
2091close_exit:
2092 free_rsp_buf(resp_buftype, rsp);
2093 return rc;
2094}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002095
2096static int
2097validate_buf(unsigned int offset, unsigned int buffer_length,
2098 struct smb2_hdr *hdr, unsigned int min_buf_size)
2099
2100{
2101 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2102 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2103 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2104 char *end_of_buf = begin_of_buf + buffer_length;
2105
2106
2107 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002108 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2109 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002110 return -EINVAL;
2111 }
2112
2113 /* check if beyond RFC1001 maximum length */
2114 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002115 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2116 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002117 return -EINVAL;
2118 }
2119
2120 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002121 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002122 return -EINVAL;
2123 }
2124
2125 return 0;
2126}
2127
2128/*
2129 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2130 * Caller must free buffer.
2131 */
2132static int
2133validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2134 struct smb2_hdr *hdr, unsigned int minbufsize,
2135 char *data)
2136
2137{
2138 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2139 int rc;
2140
2141 if (!data)
2142 return -EINVAL;
2143
2144 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2145 if (rc)
2146 return rc;
2147
2148 memcpy(data, begin_of_buf, buffer_length);
2149
2150 return 0;
2151}
2152
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002153static int
2154query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002155 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2156 u32 additional_info, size_t output_len, size_t min_len, void **data,
2157 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002158{
2159 struct smb2_query_info_req *req;
2160 struct smb2_query_info_rsp *rsp = NULL;
2161 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002162 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002163 int rc = 0;
2164 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002165 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002166 int flags = 0;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002167
Joe Perchesf96637b2013-05-04 22:12:25 -05002168 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002169
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002170 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002171 return -EIO;
2172
2173 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2174 if (rc)
2175 return rc;
2176
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002177 if (encryption_required(tcon))
2178 flags |= CIFS_TRANSFORM_REQ;
2179
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002180 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002181 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002182 req->PersistentFileId = persistent_fid;
2183 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002184 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002185 /* 4 for rfc1002 length field and 1 for Buffer */
2186 req->InputBufferOffset =
2187 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002188 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002189
2190 iov[0].iov_base = (char *)req;
2191 /* 4 for rfc1002 length field */
2192 iov[0].iov_len = get_rfc1002_length(req) + 4;
2193
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002194 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002195 cifs_small_buf_release(req);
2196 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002197
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002198 if (rc) {
2199 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2200 goto qinf_exit;
2201 }
2202
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002203 if (dlen) {
2204 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2205 if (!*data) {
2206 *data = kmalloc(*dlen, GFP_KERNEL);
2207 if (!*data) {
2208 cifs_dbg(VFS,
2209 "Error %d allocating memory for acl\n",
2210 rc);
2211 *dlen = 0;
2212 goto qinf_exit;
2213 }
2214 }
2215 }
2216
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002217 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2218 le32_to_cpu(rsp->OutputBufferLength),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002219 &rsp->hdr, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002220
2221qinf_exit:
2222 free_rsp_buf(resp_buftype, rsp);
2223 return rc;
2224}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002225
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002226int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
2227 u64 persistent_fid, u64 volatile_fid,
2228 struct smb2_file_full_ea_info *data)
2229{
2230 return query_info(xid, tcon, persistent_fid, volatile_fid,
2231 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
2232 SMB2_MAX_EA_BUF,
2233 sizeof(struct smb2_file_full_ea_info),
2234 (void **)&data,
2235 NULL);
2236}
2237
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002238int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2239 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002240{
2241 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002242 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002243 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002244 sizeof(struct smb2_file_all_info), (void **)&data,
2245 NULL);
2246}
2247
2248int
2249SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2250 u64 persistent_fid, u64 volatile_fid,
2251 void **data, u32 *plen)
2252{
2253 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2254 *plen = 0;
2255
2256 return query_info(xid, tcon, persistent_fid, volatile_fid,
2257 0, SMB2_O_INFO_SECURITY, additional_info,
2258 SMB2_MAX_BUFFER_SIZE,
2259 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002260}
2261
2262int
2263SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2264 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2265{
2266 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002267 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002268 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002269 sizeof(struct smb2_file_internal_info),
2270 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002271}
2272
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002273/*
2274 * This is a no-op for now. We're not really interested in the reply, but
2275 * rather in the fact that the server sent one and that server->lstrp
2276 * gets updated.
2277 *
2278 * FIXME: maybe we should consider checking that the reply matches request?
2279 */
2280static void
2281smb2_echo_callback(struct mid_q_entry *mid)
2282{
2283 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002284 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002285 unsigned int credits_received = 1;
2286
2287 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002288 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002289
2290 DeleteMidQEntry(mid);
2291 add_credits(server, credits_received, CIFS_ECHO_OP);
2292}
2293
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002294void smb2_reconnect_server(struct work_struct *work)
2295{
2296 struct TCP_Server_Info *server = container_of(work,
2297 struct TCP_Server_Info, reconnect.work);
2298 struct cifs_ses *ses;
2299 struct cifs_tcon *tcon, *tcon2;
2300 struct list_head tmp_list;
2301 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002302 int rc;
2303 int resched = false;
2304
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002305
2306 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2307 mutex_lock(&server->reconnect_mutex);
2308
2309 INIT_LIST_HEAD(&tmp_list);
2310 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2311
2312 spin_lock(&cifs_tcp_ses_lock);
2313 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2314 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002315 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002316 tcon->tc_count++;
2317 list_add_tail(&tcon->rlist, &tmp_list);
2318 tcon_exist = true;
2319 }
2320 }
2321 }
2322 /*
2323 * Get the reference to server struct to be sure that the last call of
2324 * cifs_put_tcon() in the loop below won't release the server pointer.
2325 */
2326 if (tcon_exist)
2327 server->srv_count++;
2328
2329 spin_unlock(&cifs_tcp_ses_lock);
2330
2331 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002332 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2333 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002334 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002335 else
2336 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002337 list_del_init(&tcon->rlist);
2338 cifs_put_tcon(tcon);
2339 }
2340
2341 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002342 if (resched)
2343 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002344 mutex_unlock(&server->reconnect_mutex);
2345
2346 /* now we can safely release srv struct */
2347 if (tcon_exist)
2348 cifs_put_tcp_session(server, 1);
2349}
2350
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002351int
2352SMB2_echo(struct TCP_Server_Info *server)
2353{
2354 struct smb2_echo_req *req;
2355 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002356 struct kvec iov[2];
2357 struct smb_rqst rqst = { .rq_iov = iov,
2358 .rq_nvec = 2 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002359
Joe Perchesf96637b2013-05-04 22:12:25 -05002360 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002361
Steve French4fcd1812016-06-22 20:12:05 -05002362 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002363 /* No need to send echo on newly established connections */
2364 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2365 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002366 }
2367
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002368 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2369 if (rc)
2370 return rc;
2371
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002372 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002373
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002374 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002375 iov[0].iov_len = 4;
2376 iov[0].iov_base = (char *)req;
2377 iov[1].iov_len = get_rfc1002_length(req);
2378 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002379
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002380 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2381 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002382 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002383 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002384
2385 cifs_small_buf_release(req);
2386 return rc;
2387}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002388
2389int
2390SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2391 u64 volatile_fid)
2392{
2393 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002394 struct cifs_ses *ses = tcon->ses;
2395 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002396 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002397 int resp_buftype;
2398 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002399 int flags = 0;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002400
Joe Perchesf96637b2013-05-04 22:12:25 -05002401 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002402
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002403 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002404 return -EIO;
2405
2406 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2407 if (rc)
2408 return rc;
2409
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002410 if (encryption_required(tcon))
2411 flags |= CIFS_TRANSFORM_REQ;
2412
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002413 req->PersistentFileId = persistent_fid;
2414 req->VolatileFileId = volatile_fid;
2415
2416 iov[0].iov_base = (char *)req;
2417 /* 4 for rfc1002 length field */
2418 iov[0].iov_len = get_rfc1002_length(req) + 4;
2419
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002420 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002421 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002422
Steve Frenchdfebe402015-03-27 01:00:06 -05002423 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002424 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2425
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002426 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002427 return rc;
2428}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002429
2430/*
2431 * To form a chain of read requests, any read requests after the first should
2432 * have the end_of_chain boolean set to true.
2433 */
2434static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002435smb2_new_read_req(void **buf, unsigned int *total_len,
2436 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2437 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002438{
2439 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002440 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002441 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002442
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002443 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2444 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002445 if (rc)
2446 return rc;
2447 if (io_parms->tcon->ses->server == NULL)
2448 return -ECONNABORTED;
2449
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002450 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002451 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002452
2453 req->PersistentFileId = io_parms->persistent_fid;
2454 req->VolatileFileId = io_parms->volatile_fid;
2455 req->ReadChannelInfoOffset = 0; /* reserved */
2456 req->ReadChannelInfoLength = 0; /* reserved */
2457 req->Channel = 0; /* reserved */
2458 req->MinimumCount = 0;
2459 req->Length = cpu_to_le32(io_parms->length);
2460 req->Offset = cpu_to_le64(io_parms->offset);
2461
2462 if (request_type & CHAINED_REQUEST) {
2463 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002464 /* next 8-byte aligned request */
2465 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2466 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002467 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002468 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002469 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002470 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002471 /*
2472 * Related requests use info from previous read request
2473 * in chain.
2474 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002475 shdr->SessionId = 0xFFFFFFFF;
2476 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002477 req->PersistentFileId = 0xFFFFFFFF;
2478 req->VolatileFileId = 0xFFFFFFFF;
2479 }
2480 }
2481 if (remaining_bytes > io_parms->length)
2482 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2483 else
2484 req->RemainingBytes = 0;
2485
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002486 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002487 return rc;
2488}
2489
2490static void
2491smb2_readv_callback(struct mid_q_entry *mid)
2492{
2493 struct cifs_readdata *rdata = mid->callback_data;
2494 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2495 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002496 struct smb2_sync_hdr *shdr =
2497 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002498 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002499 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2500 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002501 .rq_pages = rdata->pages,
2502 .rq_npages = rdata->nr_pages,
2503 .rq_pagesz = rdata->pagesz,
2504 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002505
Joe Perchesf96637b2013-05-04 22:12:25 -05002506 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2507 __func__, mid->mid, mid->mid_state, rdata->result,
2508 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002509
2510 switch (mid->mid_state) {
2511 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002512 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002513 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002514 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002515 int rc;
2516
Jeff Layton0b688cf2012-09-18 16:20:34 -07002517 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002518 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002519 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2520 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002521 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002522 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002523 task_io_account_read(rdata->got_bytes);
2524 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002525 break;
2526 case MID_REQUEST_SUBMITTED:
2527 case MID_RETRY_NEEDED:
2528 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002529 if (server->sign && rdata->got_bytes)
2530 /* reset bytes number since we can not check a sign */
2531 rdata->got_bytes = 0;
2532 /* FIXME: should this be counted toward the initiating task? */
2533 task_io_account_read(rdata->got_bytes);
2534 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002535 break;
2536 default:
2537 if (rdata->result != -ENODATA)
2538 rdata->result = -EIO;
2539 }
2540
2541 if (rdata->result)
2542 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2543
2544 queue_work(cifsiod_wq, &rdata->work);
2545 DeleteMidQEntry(mid);
2546 add_credits(server, credits_received, 0);
2547}
2548
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002549/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002550int
2551smb2_async_readv(struct cifs_readdata *rdata)
2552{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002553 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002554 char *buf;
2555 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002556 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002557 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2558 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002559 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002560 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002561 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002562
Joe Perchesf96637b2013-05-04 22:12:25 -05002563 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2564 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002565
2566 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2567 io_parms.offset = rdata->offset;
2568 io_parms.length = rdata->bytes;
2569 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2570 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2571 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002572
2573 server = io_parms.tcon->ses->server;
2574
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002575 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002576 if (rc) {
2577 if (rc == -EAGAIN && rdata->credits) {
2578 /* credits was reset by reconnect */
2579 rdata->credits = 0;
2580 /* reduce in_flight value since we won't send the req */
2581 spin_lock(&server->req_lock);
2582 server->in_flight--;
2583 spin_unlock(&server->req_lock);
2584 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002585 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002586 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002587
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002588 if (encryption_required(io_parms.tcon))
2589 flags |= CIFS_TRANSFORM_REQ;
2590
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002591 req_len = cpu_to_be32(total_len);
2592
2593 rdata->iov[0].iov_base = &req_len;
2594 rdata->iov[0].iov_len = sizeof(__be32);
2595 rdata->iov[1].iov_base = buf;
2596 rdata->iov[1].iov_len = total_len;
2597
2598 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002599
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002600 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002601 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002602 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002603 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002604 spin_lock(&server->req_lock);
2605 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002606 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002607 spin_unlock(&server->req_lock);
2608 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002609 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002610 }
2611
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002612 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002613 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002614 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002615 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002616 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002617 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002618 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2619 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002620
2621 cifs_small_buf_release(buf);
2622 return rc;
2623}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002624
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002625int
2626SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2627 unsigned int *nbytes, char **buf, int *buf_type)
2628{
2629 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002630 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002631 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002632 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002633 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002634 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002635 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002636 __be32 req_len;
2637 struct smb_rqst rqst = { .rq_iov = iov,
2638 .rq_nvec = 2 };
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002639 int flags = CIFS_LOG_ERROR;
2640 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002641
2642 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002643 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002644 if (rc)
2645 return rc;
2646
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002647 if (encryption_required(io_parms->tcon))
2648 flags |= CIFS_TRANSFORM_REQ;
2649
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002650 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002651
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002652 iov[0].iov_base = &req_len;
2653 iov[0].iov_len = sizeof(__be32);
2654 iov[1].iov_base = req;
2655 iov[1].iov_len = total_len;
2656
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002657 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002658 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002659
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002660 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002661 shdr = get_sync_hdr(rsp);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002662
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002663 if (shdr->Status == STATUS_END_OF_FILE) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002664 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002665 return 0;
2666 }
2667
2668 if (rc) {
2669 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002670 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002671 } else {
2672 *nbytes = le32_to_cpu(rsp->DataLength);
2673 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2674 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002675 cifs_dbg(FYI, "bad length %d for count %d\n",
2676 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002677 rc = -EIO;
2678 *nbytes = 0;
2679 }
2680 }
2681
2682 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002683 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002684 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002685 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002686 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002687 if (resp_buftype == CIFS_SMALL_BUFFER)
2688 *buf_type = CIFS_SMALL_BUFFER;
2689 else if (resp_buftype == CIFS_LARGE_BUFFER)
2690 *buf_type = CIFS_LARGE_BUFFER;
2691 }
2692 return rc;
2693}
2694
Pavel Shilovsky33319142012-09-18 16:20:29 -07002695/*
2696 * Check the mid_state and signature on received buffer (if any), and queue the
2697 * workqueue completion task.
2698 */
2699static void
2700smb2_writev_callback(struct mid_q_entry *mid)
2701{
2702 struct cifs_writedata *wdata = mid->callback_data;
2703 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2704 unsigned int written;
2705 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2706 unsigned int credits_received = 1;
2707
2708 switch (mid->mid_state) {
2709 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002710 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002711 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2712 if (wdata->result != 0)
2713 break;
2714
2715 written = le32_to_cpu(rsp->DataLength);
2716 /*
2717 * Mask off high 16 bits when bytes written as returned
2718 * by the server is greater than bytes requested by the
2719 * client. OS/2 servers are known to set incorrect
2720 * CountHigh values.
2721 */
2722 if (written > wdata->bytes)
2723 written &= 0xFFFF;
2724
2725 if (written < wdata->bytes)
2726 wdata->result = -ENOSPC;
2727 else
2728 wdata->bytes = written;
2729 break;
2730 case MID_REQUEST_SUBMITTED:
2731 case MID_RETRY_NEEDED:
2732 wdata->result = -EAGAIN;
2733 break;
2734 default:
2735 wdata->result = -EIO;
2736 break;
2737 }
2738
2739 if (wdata->result)
2740 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2741
2742 queue_work(cifsiod_wq, &wdata->work);
2743 DeleteMidQEntry(mid);
2744 add_credits(tcon->ses->server, credits_received, 0);
2745}
2746
2747/* smb2_async_writev - send an async write, and set up mid to handle result */
2748int
Steve French4a5c80d2014-02-07 20:45:12 -06002749smb2_async_writev(struct cifs_writedata *wdata,
2750 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002751{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002752 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002753 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002754 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002755 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002756 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002757 struct kvec iov[2];
2758 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002759
2760 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002761 if (rc) {
2762 if (rc == -EAGAIN && wdata->credits) {
2763 /* credits was reset by reconnect */
2764 wdata->credits = 0;
2765 /* reduce in_flight value since we won't send the req */
2766 spin_lock(&server->req_lock);
2767 server->in_flight--;
2768 spin_unlock(&server->req_lock);
2769 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002770 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002771 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002772
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002773 if (encryption_required(tcon))
2774 flags |= CIFS_TRANSFORM_REQ;
2775
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002776 shdr = get_sync_hdr(req);
2777 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002778
2779 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2780 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2781 req->WriteChannelInfoOffset = 0;
2782 req->WriteChannelInfoLength = 0;
2783 req->Channel = 0;
2784 req->Offset = cpu_to_le64(wdata->offset);
2785 /* 4 for rfc1002 length field */
2786 req->DataOffset = cpu_to_le16(
2787 offsetof(struct smb2_write_req, Buffer) - 4);
2788 req->RemainingBytes = 0;
2789
2790 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002791 iov[0].iov_len = 4;
2792 iov[0].iov_base = req;
2793 iov[1].iov_len = get_rfc1002_length(req) - 1;
2794 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002795
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002796 rqst.rq_iov = iov;
2797 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002798 rqst.rq_pages = wdata->pages;
2799 rqst.rq_npages = wdata->nr_pages;
2800 rqst.rq_pagesz = wdata->pagesz;
2801 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002802
Joe Perchesf96637b2013-05-04 22:12:25 -05002803 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2804 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002805
2806 req->Length = cpu_to_le32(wdata->bytes);
2807
2808 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2809
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002810 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002811 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002812 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002813 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002814 spin_lock(&server->req_lock);
2815 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002816 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002817 spin_unlock(&server->req_lock);
2818 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002819 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002820 }
2821
Pavel Shilovsky33319142012-09-18 16:20:29 -07002822 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002823 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2824 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002825
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002826 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002827 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002828 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2829 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002830
Pavel Shilovsky33319142012-09-18 16:20:29 -07002831async_writev_out:
2832 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002833 return rc;
2834}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002835
2836/*
2837 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2838 * The length field from io_parms must be at least 1 and indicates a number of
2839 * elements with data to write that begins with position 1 in iov array. All
2840 * data length is specified by count.
2841 */
2842int
2843SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2844 unsigned int *nbytes, struct kvec *iov, int n_vec)
2845{
2846 int rc = 0;
2847 struct smb2_write_req *req = NULL;
2848 struct smb2_write_rsp *rsp = NULL;
2849 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002850 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002851 int flags = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002852
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002853 *nbytes = 0;
2854
2855 if (n_vec < 1)
2856 return rc;
2857
2858 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2859 if (rc)
2860 return rc;
2861
2862 if (io_parms->tcon->ses->server == NULL)
2863 return -ECONNABORTED;
2864
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002865 if (encryption_required(io_parms->tcon))
2866 flags |= CIFS_TRANSFORM_REQ;
2867
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002868 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002869
2870 req->PersistentFileId = io_parms->persistent_fid;
2871 req->VolatileFileId = io_parms->volatile_fid;
2872 req->WriteChannelInfoOffset = 0;
2873 req->WriteChannelInfoLength = 0;
2874 req->Channel = 0;
2875 req->Length = cpu_to_le32(io_parms->length);
2876 req->Offset = cpu_to_le64(io_parms->offset);
2877 /* 4 for rfc1002 length field */
2878 req->DataOffset = cpu_to_le16(
2879 offsetof(struct smb2_write_req, Buffer) - 4);
2880 req->RemainingBytes = 0;
2881
2882 iov[0].iov_base = (char *)req;
2883 /* 4 for rfc1002 length field and 1 for Buffer */
2884 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2885
2886 /* length of entire message including data to be written */
2887 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2888
2889 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002890 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002891 cifs_small_buf_release(req);
2892 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002893
2894 if (rc) {
2895 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002896 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002897 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002898 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002899
2900 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002901 return rc;
2902}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002903
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002904static unsigned int
2905num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2906{
2907 int len;
2908 unsigned int entrycount = 0;
2909 unsigned int next_offset = 0;
2910 FILE_DIRECTORY_INFO *entryptr;
2911
2912 if (bufstart == NULL)
2913 return 0;
2914
2915 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2916
2917 while (1) {
2918 entryptr = (FILE_DIRECTORY_INFO *)
2919 ((char *)entryptr + next_offset);
2920
2921 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002922 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002923 break;
2924 }
2925
2926 len = le32_to_cpu(entryptr->FileNameLength);
2927 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002928 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2929 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002930 break;
2931 }
2932
2933 *lastentry = (char *)entryptr;
2934 entrycount++;
2935
2936 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2937 if (!next_offset)
2938 break;
2939 }
2940
2941 return entrycount;
2942}
2943
2944/*
2945 * Readdir/FindFirst
2946 */
2947int
2948SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2949 u64 persistent_fid, u64 volatile_fid, int index,
2950 struct cifs_search_info *srch_inf)
2951{
2952 struct smb2_query_directory_req *req;
2953 struct smb2_query_directory_rsp *rsp = NULL;
2954 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002955 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002956 int rc = 0;
2957 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002958 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002959 unsigned char *bufptr;
2960 struct TCP_Server_Info *server;
2961 struct cifs_ses *ses = tcon->ses;
2962 __le16 asteriks = cpu_to_le16('*');
2963 char *end_of_smb;
2964 unsigned int output_size = CIFSMaxBufSize;
2965 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002966 int flags = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002967
2968 if (ses && (ses->server))
2969 server = ses->server;
2970 else
2971 return -EIO;
2972
2973 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2974 if (rc)
2975 return rc;
2976
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002977 if (encryption_required(tcon))
2978 flags |= CIFS_TRANSFORM_REQ;
2979
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002980 switch (srch_inf->info_level) {
2981 case SMB_FIND_FILE_DIRECTORY_INFO:
2982 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2983 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2984 break;
2985 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2986 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2987 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2988 break;
2989 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002990 cifs_dbg(VFS, "info level %u isn't supported\n",
2991 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002992 rc = -EINVAL;
2993 goto qdir_exit;
2994 }
2995
2996 req->FileIndex = cpu_to_le32(index);
2997 req->PersistentFileId = persistent_fid;
2998 req->VolatileFileId = volatile_fid;
2999
3000 len = 0x2;
3001 bufptr = req->Buffer;
3002 memcpy(bufptr, &asteriks, len);
3003
3004 req->FileNameOffset =
3005 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
3006 req->FileNameLength = cpu_to_le16(len);
3007 /*
3008 * BB could be 30 bytes or so longer if we used SMB2 specific
3009 * buffer lengths, but this is safe and close enough.
3010 */
3011 output_size = min_t(unsigned int, output_size, server->maxBuf);
3012 output_size = min_t(unsigned int, output_size, 2 << 15);
3013 req->OutputBufferLength = cpu_to_le32(output_size);
3014
3015 iov[0].iov_base = (char *)req;
3016 /* 4 for RFC1001 length and 1 for Buffer */
3017 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
3018
3019 iov[1].iov_base = (char *)(req->Buffer);
3020 iov[1].iov_len = len;
3021
3022 inc_rfc1001_len(req, len - 1 /* Buffer */);
3023
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003024 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003025 cifs_small_buf_release(req);
3026 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003027
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003028 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003029 if (rc == -ENODATA &&
3030 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003031 srch_inf->endOfSearch = true;
3032 rc = 0;
3033 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003034 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3035 goto qdir_exit;
3036 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003037
3038 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3039 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3040 info_buf_size);
3041 if (rc)
3042 goto qdir_exit;
3043
3044 srch_inf->unicode = true;
3045
3046 if (srch_inf->ntwrk_buf_start) {
3047 if (srch_inf->smallBuf)
3048 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3049 else
3050 cifs_buf_release(srch_inf->ntwrk_buf_start);
3051 }
3052 srch_inf->ntwrk_buf_start = (char *)rsp;
3053 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3054 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3055 /* 4 for rfc1002 length field */
3056 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3057 srch_inf->entries_in_buffer =
3058 num_entries(srch_inf->srch_entries_start, end_of_smb,
3059 &srch_inf->last_entry, info_buf_size);
3060 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003061 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3062 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3063 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003064 if (resp_buftype == CIFS_LARGE_BUFFER)
3065 srch_inf->smallBuf = false;
3066 else if (resp_buftype == CIFS_SMALL_BUFFER)
3067 srch_inf->smallBuf = true;
3068 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003069 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003070
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003071 return rc;
3072
3073qdir_exit:
3074 free_rsp_buf(resp_buftype, rsp);
3075 return rc;
3076}
3077
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003078static int
3079send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003080 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3081 u8 info_type, u32 additional_info, unsigned int num,
3082 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003083{
3084 struct smb2_set_info_req *req;
3085 struct smb2_set_info_rsp *rsp = NULL;
3086 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003087 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003088 int rc = 0;
3089 int resp_buftype;
3090 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003091 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003092 int flags = 0;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003093
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003094 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003095 return -EIO;
3096
3097 if (!num)
3098 return -EINVAL;
3099
3100 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3101 if (!iov)
3102 return -ENOMEM;
3103
3104 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3105 if (rc) {
3106 kfree(iov);
3107 return rc;
3108 }
3109
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003110 if (encryption_required(tcon))
3111 flags |= CIFS_TRANSFORM_REQ;
3112
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003113 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003114
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003115 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003116 req->FileInfoClass = info_class;
3117 req->PersistentFileId = persistent_fid;
3118 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003119 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003120
3121 /* 4 for RFC1001 length and 1 for Buffer */
3122 req->BufferOffset =
3123 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3124 req->BufferLength = cpu_to_le32(*size);
3125
3126 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3127
3128 memcpy(req->Buffer, *data, *size);
3129
3130 iov[0].iov_base = (char *)req;
3131 /* 4 for RFC1001 length */
3132 iov[0].iov_len = get_rfc1002_length(req) + 4;
3133
3134 for (i = 1; i < num; i++) {
3135 inc_rfc1001_len(req, size[i]);
3136 le32_add_cpu(&req->BufferLength, size[i]);
3137 iov[i].iov_base = (char *)data[i];
3138 iov[i].iov_len = size[i];
3139 }
3140
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003141 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003142 cifs_small_buf_release(req);
3143 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003144
Steve French7d3fb242013-11-18 09:56:28 -06003145 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003146 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003147
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003148 free_rsp_buf(resp_buftype, rsp);
3149 kfree(iov);
3150 return rc;
3151}
3152
3153int
3154SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3155 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3156{
3157 struct smb2_file_rename_info info;
3158 void **data;
3159 unsigned int size[2];
3160 int rc;
3161 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3162
3163 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3164 if (!data)
3165 return -ENOMEM;
3166
3167 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3168 /* 0 = fail if target already exists */
3169 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3170 info.FileNameLength = cpu_to_le32(len);
3171
3172 data[0] = &info;
3173 size[0] = sizeof(struct smb2_file_rename_info);
3174
3175 data[1] = target_file;
3176 size[1] = len + 2 /* null */;
3177
3178 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003179 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3180 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003181 kfree(data);
3182 return rc;
3183}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003184
3185int
Steve French897fba12016-05-12 21:20:36 -05003186SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3187 u64 persistent_fid, u64 volatile_fid)
3188{
3189 __u8 delete_pending = 1;
3190 void *data;
3191 unsigned int size;
3192
3193 data = &delete_pending;
3194 size = 1; /* sizeof __u8 */
3195
3196 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003197 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3198 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003199}
3200
3201int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003202SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3203 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3204{
3205 struct smb2_file_link_info info;
3206 void **data;
3207 unsigned int size[2];
3208 int rc;
3209 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3210
3211 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3212 if (!data)
3213 return -ENOMEM;
3214
3215 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3216 /* 0 = fail if link already exists */
3217 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3218 info.FileNameLength = cpu_to_le32(len);
3219
3220 data[0] = &info;
3221 size[0] = sizeof(struct smb2_file_link_info);
3222
3223 data[1] = target_file;
3224 size[1] = len + 2 /* null */;
3225
3226 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003227 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3228 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003229 kfree(data);
3230 return rc;
3231}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003232
3233int
3234SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003235 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003236{
3237 struct smb2_file_eof_info info;
3238 void *data;
3239 unsigned int size;
3240
3241 info.EndOfFile = *eof;
3242
3243 data = &info;
3244 size = sizeof(struct smb2_file_eof_info);
3245
Steve Frenchf29ebb42014-07-19 21:44:58 -05003246 if (is_falloc)
3247 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003248 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3249 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003250 else
3251 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003252 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3253 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003254}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003255
3256int
3257SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3258 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3259{
3260 unsigned int size;
3261 size = sizeof(FILE_BASIC_INFO);
3262 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003263 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3264 0, 1, (void **)&buf, &size);
3265}
3266
3267int
3268SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3269 u64 persistent_fid, u64 volatile_fid,
3270 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3271{
3272 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3273 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3274 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003275}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003276
3277int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003278SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3279 u64 persistent_fid, u64 volatile_fid,
3280 struct smb2_file_full_ea_info *buf, int len)
3281{
3282 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3283 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3284 0, 1, (void **)&buf, &len);
3285}
3286
3287int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003288SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3289 const u64 persistent_fid, const u64 volatile_fid,
3290 __u8 oplock_level)
3291{
3292 int rc;
3293 struct smb2_oplock_break *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003294 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003295
Joe Perchesf96637b2013-05-04 22:12:25 -05003296 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003297 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003298 if (rc)
3299 return rc;
3300
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003301 if (encryption_required(tcon))
3302 flags |= CIFS_TRANSFORM_REQ;
3303
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003304 req->VolatileFid = volatile_fid;
3305 req->PersistentFid = persistent_fid;
3306 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003307 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003308
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003309 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003310 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003311
3312 if (rc) {
3313 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003314 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003315 }
3316
3317 return rc;
3318}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003319
3320static void
3321copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3322 struct kstatfs *kst)
3323{
3324 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3325 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3326 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303327 kst->f_bfree = kst->f_bavail =
3328 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003329 return;
3330}
3331
3332static int
3333build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3334 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3335{
3336 int rc;
3337 struct smb2_query_info_req *req;
3338
Joe Perchesf96637b2013-05-04 22:12:25 -05003339 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003340
3341 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3342 return -EIO;
3343
3344 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3345 if (rc)
3346 return rc;
3347
3348 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3349 req->FileInfoClass = level;
3350 req->PersistentFileId = persistent_fid;
3351 req->VolatileFileId = volatile_fid;
3352 /* 4 for rfc1002 length field and 1 for pad */
3353 req->InputBufferOffset =
3354 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3355 req->OutputBufferLength = cpu_to_le32(
3356 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3357
3358 iov->iov_base = (char *)req;
3359 /* 4 for rfc1002 length field */
3360 iov->iov_len = get_rfc1002_length(req) + 4;
3361 return 0;
3362}
3363
3364int
3365SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3366 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3367{
3368 struct smb2_query_info_rsp *rsp = NULL;
3369 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003370 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003371 int rc = 0;
3372 int resp_buftype;
3373 struct cifs_ses *ses = tcon->ses;
3374 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003375 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003376
3377 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3378 sizeof(struct smb2_fs_full_size_info),
3379 persistent_fid, volatile_fid);
3380 if (rc)
3381 return rc;
3382
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003383 if (encryption_required(tcon))
3384 flags |= CIFS_TRANSFORM_REQ;
3385
3386 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003387 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003388 if (rc) {
3389 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003390 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003391 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003392 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003393
3394 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3395 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3396 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3397 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3398 sizeof(struct smb2_fs_full_size_info));
3399 if (!rc)
3400 copy_fs_info_to_kstatfs(info, fsdata);
3401
Steve French34f62642013-10-09 02:07:00 -05003402qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003403 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003404 return rc;
3405}
3406
3407int
3408SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003409 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003410{
3411 struct smb2_query_info_rsp *rsp = NULL;
3412 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003413 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003414 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003415 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003416 struct cifs_ses *ses = tcon->ses;
3417 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003418 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003419
Steven French21671142013-10-09 13:36:35 -05003420 if (level == FS_DEVICE_INFORMATION) {
3421 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3422 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3423 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3424 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3425 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003426 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3427 max_len = sizeof(struct smb3_fs_ss_info);
3428 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003429 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003430 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003431 return -EINVAL;
3432 }
3433
3434 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003435 persistent_fid, volatile_fid);
3436 if (rc)
3437 return rc;
3438
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003439 if (encryption_required(tcon))
3440 flags |= CIFS_TRANSFORM_REQ;
3441
3442 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003443 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003444 if (rc) {
3445 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3446 goto qfsattr_exit;
3447 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003448 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003449
3450 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3451 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003452 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3453 if (rc)
3454 goto qfsattr_exit;
3455
3456 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003457 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3458 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003459 rsp_len, max_len));
3460 else if (level == FS_DEVICE_INFORMATION)
3461 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3462 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003463 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3464 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3465 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3466 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3467 tcon->perf_sector_size =
3468 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3469 }
Steve French34f62642013-10-09 02:07:00 -05003470
3471qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003472 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003473 return rc;
3474}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003475
3476int
3477smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3478 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3479 const __u32 num_lock, struct smb2_lock_element *buf)
3480{
3481 int rc = 0;
3482 struct smb2_lock_req *req = NULL;
3483 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003484 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003485 int resp_buf_type;
3486 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003487 int flags = CIFS_NO_RESP;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003488
Joe Perchesf96637b2013-05-04 22:12:25 -05003489 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003490
3491 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3492 if (rc)
3493 return rc;
3494
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003495 if (encryption_required(tcon))
3496 flags |= CIFS_TRANSFORM_REQ;
3497
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003498 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003499 req->LockCount = cpu_to_le16(num_lock);
3500
3501 req->PersistentFileId = persist_fid;
3502 req->VolatileFileId = volatile_fid;
3503
3504 count = num_lock * sizeof(struct smb2_lock_element);
3505 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3506
3507 iov[0].iov_base = (char *)req;
3508 /* 4 for rfc1002 length field and count for all locks */
3509 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3510 iov[1].iov_base = (char *)buf;
3511 iov[1].iov_len = count;
3512
3513 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003514 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003515 &rsp_iov);
3516 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003517 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003518 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003519 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3520 }
3521
3522 return rc;
3523}
3524
3525int
3526SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3527 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3528 const __u64 length, const __u64 offset, const __u32 lock_flags,
3529 const bool wait)
3530{
3531 struct smb2_lock_element lock;
3532
3533 lock.Offset = cpu_to_le64(offset);
3534 lock.Length = cpu_to_le64(length);
3535 lock.Flags = cpu_to_le32(lock_flags);
3536 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3537 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3538
3539 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3540}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003541
3542int
3543SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3544 __u8 *lease_key, const __le32 lease_state)
3545{
3546 int rc;
3547 struct smb2_lease_ack *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003548 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003549
Joe Perchesf96637b2013-05-04 22:12:25 -05003550 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003551 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003552 if (rc)
3553 return rc;
3554
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003555 if (encryption_required(tcon))
3556 flags |= CIFS_TRANSFORM_REQ;
3557
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003558 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003559 req->StructureSize = cpu_to_le16(36);
3560 inc_rfc1001_len(req, 12);
3561
3562 memcpy(req->LeaseKey, lease_key, 16);
3563 req->LeaseState = lease_state;
3564
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003565 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003566 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003567
3568 if (rc) {
3569 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003570 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003571 }
3572
3573 return rc;
3574}