blob: 6b03e2d45d8ecdcc890dacbd5dda84d58ff32ba5 [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;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200651 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600652 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
Steve French0603c962017-09-20 19:57:18 -0500659 * can not sign it (ie are not known user). Even if signing is not
660 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600661 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500662 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600663 */
Steve French0603c962017-09-20 19:57:18 -0500664 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600665 return 0; /* validation requires signing */
666
Steve French0603c962017-09-20 19:57:18 -0500667 if (tcon->ses->user_name == NULL) {
668 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
669 return 0; /* validation requires signing */
670 }
671
672 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
673 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
674
Steve Frenchff1c0382013-11-19 23:44:46 -0600675 vneg_inbuf.Capabilities =
676 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100677 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
678 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600679
680 if (tcon->ses->sign)
681 vneg_inbuf.SecurityMode =
682 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
683 else if (global_secflags & CIFSSEC_MAY_SIGN)
684 vneg_inbuf.SecurityMode =
685 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
686 else
687 vneg_inbuf.SecurityMode = 0;
688
Steve French9764c022017-09-17 10:41:35 -0500689
690 if (strcmp(tcon->ses->server->vals->version_string,
691 SMB3ANY_VERSION_STRING) == 0) {
692 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
693 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
694 vneg_inbuf.DialectCount = cpu_to_le16(2);
695 /* structure is big enough for 3 dialects, sending only 2 */
696 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
697 } else if (strcmp(tcon->ses->server->vals->version_string,
698 SMBDEFAULT_VERSION_STRING) == 0) {
699 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
700 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
701 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
702 vneg_inbuf.DialectCount = cpu_to_le16(3);
703 /* structure is big enough for 3 dialects */
704 inbuflen = sizeof(struct validate_negotiate_info_req);
705 } else {
706 /* otherwise specific dialect was requested */
707 vneg_inbuf.Dialects[0] =
708 cpu_to_le16(tcon->ses->server->vals->protocol_id);
709 vneg_inbuf.DialectCount = cpu_to_le16(1);
710 /* structure is big enough for 3 dialects, sending only 1 */
711 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
712 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600713
714 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
715 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100716 false /* use_ipc */,
Steve Frenchff1c0382013-11-19 23:44:46 -0600717 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
718 (char **)&pneg_rsp, &rsplen);
719
720 if (rc != 0) {
721 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
722 return -EIO;
723 }
724
725 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500726 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
727 rsplen);
728
729 /* relax check since Mac returns max bufsize allowed on ioctl */
730 if (rsplen > CIFSMaxBufSize)
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200731 goto err_rsp_free;
Steve Frenchff1c0382013-11-19 23:44:46 -0600732 }
733
734 /* check validate negotiate info response matches what we got earlier */
735 if (pneg_rsp->Dialect !=
736 cpu_to_le16(tcon->ses->server->vals->protocol_id))
737 goto vneg_out;
738
739 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
740 goto vneg_out;
741
742 /* do not validate server guid because not saved at negprot time yet */
743
744 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
745 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
746 goto vneg_out;
747
748 /* validate negotiate successful */
749 cifs_dbg(FYI, "validate negotiate info successful\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200750 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600751 return 0;
752
753vneg_out:
754 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200755err_rsp_free:
756 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600757 return -EIO;
758}
759
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530760enum securityEnum
761smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
762{
763 switch (requested) {
764 case Kerberos:
765 case RawNTLMSSP:
766 return requested;
767 case NTLMv2:
768 return RawNTLMSSP;
769 case Unspecified:
770 if (server->sec_ntlmssp &&
771 (global_secflags & CIFSSEC_MAY_NTLMSSP))
772 return RawNTLMSSP;
773 if ((server->sec_kerberos || server->sec_mskerberos) &&
774 (global_secflags & CIFSSEC_MAY_KRB5))
775 return Kerberos;
776 /* Fallthrough */
777 default:
778 return Unspecified;
779 }
780}
781
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100782struct SMB2_sess_data {
783 unsigned int xid;
784 struct cifs_ses *ses;
785 struct nls_table *nls_cp;
786 void (*func)(struct SMB2_sess_data *);
787 int result;
788 u64 previous_session;
789
790 /* we will send the SMB in three pieces:
791 * a fixed length beginning part, an optional
792 * SPNEGO blob (which can be zero length), and a
793 * last part which will include the strings
794 * and rest of bcc area. This allows us to avoid
795 * a large buffer 17K allocation
796 */
797 int buf0_type;
798 struct kvec iov[2];
799};
800
801static int
802SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
803{
804 int rc;
805 struct cifs_ses *ses = sess_data->ses;
806 struct smb2_sess_setup_req *req;
807 struct TCP_Server_Info *server = ses->server;
808
809 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
810 if (rc)
811 return rc;
812
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700813 /* First session, not a reauthenticate */
814 req->hdr.sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100815
816 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
817 req->PreviousSessionId = sess_data->previous_session;
818
819 req->Flags = 0; /* MBZ */
820 /* to enable echos and oplocks */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700821 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100822
823 /* only one of SMB2 signing flags may be set in SMB2 request */
824 if (server->sign)
825 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
826 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
827 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
828 else
829 req->SecurityMode = 0;
830
831 req->Capabilities = 0;
832 req->Channel = 0; /* MBZ */
833
834 sess_data->iov[0].iov_base = (char *)req;
835 /* 4 for rfc1002 length field and 1 for pad */
836 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
837 /*
838 * This variable will be used to clear the buffer
839 * allocated above in case of any error in the calling function.
840 */
841 sess_data->buf0_type = CIFS_SMALL_BUFFER;
842
843 return 0;
844}
845
846static void
847SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
848{
849 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
850 sess_data->buf0_type = CIFS_NO_BUFFER;
851}
852
853static int
854SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
855{
856 int rc;
857 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700858 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100859
860 /* Testing shows that buffer offset must be at location of Buffer[0] */
861 req->SecurityBufferOffset =
862 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
863 1 /* pad */ - 4 /* rfc1001 len */);
864 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
865
866 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
867
868 /* BB add code to build os and lm fields */
869
870 rc = SendReceive2(sess_data->xid, sess_data->ses,
871 sess_data->iov, 2,
872 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700873 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
874 cifs_small_buf_release(sess_data->iov[0].iov_base);
875 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100876
877 return rc;
878}
879
880static int
881SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
882{
883 int rc = 0;
884 struct cifs_ses *ses = sess_data->ses;
885
886 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800887 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100888 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100889 if (rc) {
890 cifs_dbg(FYI,
891 "SMB3 session key generation failed\n");
892 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800893 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100894 }
895 }
896 if (!ses->server->session_estab) {
897 ses->server->sequence_number = 0x2;
898 ses->server->session_estab = true;
899 }
900 mutex_unlock(&ses->server->srv_mutex);
901
902 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
903 spin_lock(&GlobalMid_Lock);
904 ses->status = CifsGood;
905 ses->need_reconnect = false;
906 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100907 return rc;
908}
909
910#ifdef CONFIG_CIFS_UPCALL
911static void
912SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
913{
914 int rc;
915 struct cifs_ses *ses = sess_data->ses;
916 struct cifs_spnego_msg *msg;
917 struct key *spnego_key = NULL;
918 struct smb2_sess_setup_rsp *rsp = NULL;
919
920 rc = SMB2_sess_alloc_buffer(sess_data);
921 if (rc)
922 goto out;
923
924 spnego_key = cifs_get_spnego_key(ses);
925 if (IS_ERR(spnego_key)) {
926 rc = PTR_ERR(spnego_key);
927 spnego_key = NULL;
928 goto out;
929 }
930
931 msg = spnego_key->payload.data[0];
932 /*
933 * check version field to make sure that cifs.upcall is
934 * sending us a response in an expected form
935 */
936 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
937 cifs_dbg(VFS,
938 "bad cifs.upcall version. Expected %d got %d",
939 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
940 rc = -EKEYREJECTED;
941 goto out_put_spnego_key;
942 }
943
944 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
945 GFP_KERNEL);
946 if (!ses->auth_key.response) {
947 cifs_dbg(VFS,
948 "Kerberos can't allocate (%u bytes) memory",
949 msg->sesskey_len);
950 rc = -ENOMEM;
951 goto out_put_spnego_key;
952 }
953 ses->auth_key.len = msg->sesskey_len;
954
955 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
956 sess_data->iov[1].iov_len = msg->secblob_len;
957
958 rc = SMB2_sess_sendreceive(sess_data);
959 if (rc)
960 goto out_put_spnego_key;
961
962 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700963 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100964
965 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100966
967 rc = SMB2_sess_establish_session(sess_data);
968out_put_spnego_key:
969 key_invalidate(spnego_key);
970 key_put(spnego_key);
971out:
972 sess_data->result = rc;
973 sess_data->func = NULL;
974 SMB2_sess_free_buffer(sess_data);
975}
976#else
977static void
978SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
979{
980 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
981 sess_data->result = -EOPNOTSUPP;
982 sess_data->func = NULL;
983}
984#endif
985
Sachin Prabhu166cea42016-10-07 19:11:22 +0100986static void
987SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
988
989static void
990SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
991{
992 int rc;
993 struct cifs_ses *ses = sess_data->ses;
994 struct smb2_sess_setup_rsp *rsp = NULL;
995 char *ntlmssp_blob = NULL;
996 bool use_spnego = false; /* else use raw ntlmssp */
997 u16 blob_length = 0;
998
999 /*
1000 * If memory allocation is successful, caller of this function
1001 * frees it.
1002 */
1003 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1004 if (!ses->ntlmssp) {
1005 rc = -ENOMEM;
1006 goto out_err;
1007 }
1008 ses->ntlmssp->sesskey_per_smbsess = true;
1009
1010 rc = SMB2_sess_alloc_buffer(sess_data);
1011 if (rc)
1012 goto out_err;
1013
1014 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1015 GFP_KERNEL);
1016 if (ntlmssp_blob == NULL) {
1017 rc = -ENOMEM;
1018 goto out;
1019 }
1020
1021 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1022 if (use_spnego) {
1023 /* BB eventually need to add this */
1024 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1025 rc = -EOPNOTSUPP;
1026 goto out;
1027 } else {
1028 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1029 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1030 }
1031 sess_data->iov[1].iov_base = ntlmssp_blob;
1032 sess_data->iov[1].iov_len = blob_length;
1033
1034 rc = SMB2_sess_sendreceive(sess_data);
1035 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1036
1037 /* If true, rc here is expected and not an error */
1038 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001039 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001040 rc = 0;
1041
1042 if (rc)
1043 goto out;
1044
1045 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1046 le16_to_cpu(rsp->SecurityBufferOffset)) {
1047 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1048 le16_to_cpu(rsp->SecurityBufferOffset));
1049 rc = -EIO;
1050 goto out;
1051 }
1052 rc = decode_ntlmssp_challenge(rsp->Buffer,
1053 le16_to_cpu(rsp->SecurityBufferLength), ses);
1054 if (rc)
1055 goto out;
1056
1057 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1058
1059
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001060 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001061 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001062
1063out:
1064 kfree(ntlmssp_blob);
1065 SMB2_sess_free_buffer(sess_data);
1066 if (!rc) {
1067 sess_data->result = 0;
1068 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1069 return;
1070 }
1071out_err:
1072 kfree(ses->ntlmssp);
1073 ses->ntlmssp = NULL;
1074 sess_data->result = rc;
1075 sess_data->func = NULL;
1076}
1077
1078static void
1079SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1080{
1081 int rc;
1082 struct cifs_ses *ses = sess_data->ses;
1083 struct smb2_sess_setup_req *req;
1084 struct smb2_sess_setup_rsp *rsp = NULL;
1085 unsigned char *ntlmssp_blob = NULL;
1086 bool use_spnego = false; /* else use raw ntlmssp */
1087 u16 blob_length = 0;
1088
1089 rc = SMB2_sess_alloc_buffer(sess_data);
1090 if (rc)
1091 goto out;
1092
1093 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001094 req->hdr.sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001095
1096 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1097 sess_data->nls_cp);
1098 if (rc) {
1099 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1100 goto out;
1101 }
1102
1103 if (use_spnego) {
1104 /* BB eventually need to add this */
1105 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1106 rc = -EOPNOTSUPP;
1107 goto out;
1108 }
1109 sess_data->iov[1].iov_base = ntlmssp_blob;
1110 sess_data->iov[1].iov_len = blob_length;
1111
1112 rc = SMB2_sess_sendreceive(sess_data);
1113 if (rc)
1114 goto out;
1115
1116 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1117
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001118 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001119 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001120
1121 rc = SMB2_sess_establish_session(sess_data);
1122out:
1123 kfree(ntlmssp_blob);
1124 SMB2_sess_free_buffer(sess_data);
1125 kfree(ses->ntlmssp);
1126 ses->ntlmssp = NULL;
1127 sess_data->result = rc;
1128 sess_data->func = NULL;
1129}
1130
1131static int
1132SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1133{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301134 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001135
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301136 type = smb2_select_sectype(ses->server, ses->sectype);
1137 cifs_dbg(FYI, "sess setup type %d\n", type);
1138 if (type == Unspecified) {
1139 cifs_dbg(VFS,
1140 "Unable to select appropriate authentication method!");
1141 return -EINVAL;
1142 }
1143
1144 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001145 case Kerberos:
1146 sess_data->func = SMB2_auth_kerberos;
1147 break;
1148 case RawNTLMSSP:
1149 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1150 break;
1151 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301152 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001153 return -EOPNOTSUPP;
1154 }
1155
1156 return 0;
1157}
1158
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001159int
1160SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1161 const struct nls_table *nls_cp)
1162{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001163 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001164 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001165 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001166
Joe Perchesf96637b2013-05-04 22:12:25 -05001167 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001168
Jeff Layton3534b852013-05-24 07:41:01 -04001169 if (!server) {
1170 WARN(1, "%s: server is NULL!\n", __func__);
1171 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001172 }
1173
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001174 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1175 if (!sess_data)
1176 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001177
1178 rc = SMB2_select_sec(ses, sess_data);
1179 if (rc)
1180 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001181 sess_data->xid = xid;
1182 sess_data->ses = ses;
1183 sess_data->buf0_type = CIFS_NO_BUFFER;
1184 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001185
Sachin Prabhu166cea42016-10-07 19:11:22 +01001186 while (sess_data->func)
1187 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001188
Steve Frenchc721c382017-09-19 18:40:03 -05001189 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1190 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001191 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001192out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001193 kfree(sess_data);
1194 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001195}
1196
1197int
1198SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1199{
1200 struct smb2_logoff_req *req; /* response is also trivial struct */
1201 int rc = 0;
1202 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001203 int flags = 0;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001204
Joe Perchesf96637b2013-05-04 22:12:25 -05001205 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001206
1207 if (ses && (ses->server))
1208 server = ses->server;
1209 else
1210 return -EIO;
1211
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001212 /* no need to send SMB logoff if uid already closed due to reconnect */
1213 if (ses->need_reconnect)
1214 goto smb2_session_already_dead;
1215
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001216 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1217 if (rc)
1218 return rc;
1219
1220 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001221 req->hdr.sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001222
1223 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1224 flags |= CIFS_TRANSFORM_REQ;
1225 else if (server->sign)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001226 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001227
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001228 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001229 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001230 /*
1231 * No tcon so can't do
1232 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1233 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001234
1235smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001236 return rc;
1237}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001238
1239static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1240{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001241 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001242}
1243
1244#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1245
Steve Frenchde9f68d2013-11-15 11:26:24 -06001246/* These are similar values to what Windows uses */
1247static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1248{
1249 tcon->max_chunks = 256;
1250 tcon->max_bytes_chunk = 1048576;
1251 tcon->max_bytes_copy = 16777216;
1252}
1253
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001254int
1255SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1256 struct cifs_tcon *tcon, const struct nls_table *cp)
1257{
1258 struct smb2_tree_connect_req *req;
1259 struct smb2_tree_connect_rsp *rsp = NULL;
1260 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001261 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001262 int rc = 0;
1263 int resp_buftype;
1264 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001265 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001266 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001267
Joe Perchesf96637b2013-05-04 22:12:25 -05001268 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001269
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001270 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001271 return -EIO;
1272
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001273 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1274 if (unc_path == NULL)
1275 return -ENOMEM;
1276
1277 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1278 unc_path_len *= 2;
1279 if (unc_path_len < 2) {
1280 kfree(unc_path);
1281 return -EINVAL;
1282 }
1283
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001284 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1285 if (tcon)
1286 tcon->tid = 0;
1287
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001288 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1289 if (rc) {
1290 kfree(unc_path);
1291 return rc;
1292 }
1293
1294 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001295 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1296 flags |= CIFS_TRANSFORM_REQ;
1297
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001298 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001299 req->hdr.sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001300 if (ses->server->sign)
1301 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001302 } else if (encryption_required(tcon))
1303 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001304
1305 iov[0].iov_base = (char *)req;
1306 /* 4 for rfc1002 length field and 1 for pad */
1307 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1308
1309 /* Testing shows that buffer offset must be at location of Buffer[0] */
1310 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1311 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1312 req->PathLength = cpu_to_le16(unc_path_len - 2);
1313 iov[1].iov_base = unc_path;
1314 iov[1].iov_len = unc_path_len;
1315
1316 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1317
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001318 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001319 cifs_small_buf_release(req);
1320 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001321
1322 if (rc != 0) {
1323 if (tcon) {
1324 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1325 tcon->need_reconnect = true;
1326 }
1327 goto tcon_error_exit;
1328 }
1329
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001330 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001331 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001332 goto tcon_exit;
1333 }
1334
Christophe JAILLETcd123002017-05-12 17:59:32 +02001335 switch (rsp->ShareType) {
1336 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001337 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001338 break;
1339 case SMB2_SHARE_TYPE_PIPE:
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001340 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001341 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001342 break;
1343 case SMB2_SHARE_TYPE_PRINT:
1344 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001345 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001346 break;
1347 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001348 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001349 rc = -EOPNOTSUPP;
1350 goto tcon_error_exit;
1351 }
1352
1353 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001354 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001355 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1356 tcon->tidStatus = CifsGood;
1357 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001358 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001359 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001360
1361 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1362 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001363 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001364
1365 if (tcon->seal &&
1366 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1367 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1368
Steve Frenchde9f68d2013-11-15 11:26:24 -06001369 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001370 if (tcon->ses->server->ops->validate_negotiate)
1371 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001372tcon_exit:
1373 free_rsp_buf(resp_buftype, rsp);
1374 kfree(unc_path);
1375 return rc;
1376
1377tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001378 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001379 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001380 }
1381 goto tcon_exit;
1382}
1383
1384int
1385SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1386{
1387 struct smb2_tree_disconnect_req *req; /* response is trivial */
1388 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001389 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001390 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001391
Joe Perchesf96637b2013-05-04 22:12:25 -05001392 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001393
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001394 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001395 return -EIO;
1396
1397 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1398 return 0;
1399
1400 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1401 if (rc)
1402 return rc;
1403
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001404 if (encryption_required(tcon))
1405 flags |= CIFS_TRANSFORM_REQ;
1406
1407 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001408 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001409 if (rc)
1410 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1411
1412 return rc;
1413}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001414
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001415
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001416static struct create_durable *
1417create_durable_buf(void)
1418{
1419 struct create_durable *buf;
1420
1421 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1422 if (!buf)
1423 return NULL;
1424
1425 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001426 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001427 buf->ccontext.DataLength = cpu_to_le32(16);
1428 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1429 (struct create_durable, Name));
1430 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001431 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001432 buf->Name[0] = 'D';
1433 buf->Name[1] = 'H';
1434 buf->Name[2] = 'n';
1435 buf->Name[3] = 'Q';
1436 return buf;
1437}
1438
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001439static struct create_durable *
1440create_reconnect_durable_buf(struct cifs_fid *fid)
1441{
1442 struct create_durable *buf;
1443
1444 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1445 if (!buf)
1446 return NULL;
1447
1448 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1449 (struct create_durable, Data));
1450 buf->ccontext.DataLength = cpu_to_le32(16);
1451 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1452 (struct create_durable, Name));
1453 buf->ccontext.NameLength = cpu_to_le16(4);
1454 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1455 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001456 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001457 buf->Name[0] = 'D';
1458 buf->Name[1] = 'H';
1459 buf->Name[2] = 'n';
1460 buf->Name[3] = 'C';
1461 return buf;
1462}
1463
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001464static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001465parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1466 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001467{
1468 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001469 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001470 unsigned int next;
1471 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001472 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001473
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001474 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001475 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001476 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001477 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001478 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001479 if (le16_to_cpu(cc->NameLength) == 4 &&
1480 strncmp(name, "RqLs", 4) == 0)
1481 return server->ops->parse_lease_buf(cc, epoch);
1482
1483 next = le32_to_cpu(cc->Next);
1484 if (!next)
1485 break;
1486 remaining -= next;
1487 cc = (struct create_context *)((char *)cc + next);
1488 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001489
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001490 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001491}
1492
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001493static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001494add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1495 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001496{
1497 struct smb2_create_req *req = iov[0].iov_base;
1498 unsigned int num = *num_iovec;
1499
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001500 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001501 if (iov[num].iov_base == NULL)
1502 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001503 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001504 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1505 if (!req->CreateContextsOffset)
1506 req->CreateContextsOffset = cpu_to_le32(
1507 sizeof(struct smb2_create_req) - 4 +
1508 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001509 le32_add_cpu(&req->CreateContextsLength,
1510 server->vals->create_lease_size);
1511 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001512 *num_iovec = num + 1;
1513 return 0;
1514}
1515
Steve Frenchb56eae42015-11-03 09:26:27 -06001516static struct create_durable_v2 *
1517create_durable_v2_buf(struct cifs_fid *pfid)
1518{
1519 struct create_durable_v2 *buf;
1520
1521 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1522 if (!buf)
1523 return NULL;
1524
1525 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1526 (struct create_durable_v2, dcontext));
1527 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1528 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1529 (struct create_durable_v2, Name));
1530 buf->ccontext.NameLength = cpu_to_le16(4);
1531
1532 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1533 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001534 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001535 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1536
1537 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1538 buf->Name[0] = 'D';
1539 buf->Name[1] = 'H';
1540 buf->Name[2] = '2';
1541 buf->Name[3] = 'Q';
1542 return buf;
1543}
1544
1545static struct create_durable_handle_reconnect_v2 *
1546create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1547{
1548 struct create_durable_handle_reconnect_v2 *buf;
1549
1550 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1551 GFP_KERNEL);
1552 if (!buf)
1553 return NULL;
1554
1555 buf->ccontext.DataOffset =
1556 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1557 dcontext));
1558 buf->ccontext.DataLength =
1559 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1560 buf->ccontext.NameOffset =
1561 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1562 Name));
1563 buf->ccontext.NameLength = cpu_to_le16(4);
1564
1565 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1566 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1567 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1568 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1569
1570 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1571 buf->Name[0] = 'D';
1572 buf->Name[1] = 'H';
1573 buf->Name[2] = '2';
1574 buf->Name[3] = 'C';
1575 return buf;
1576}
1577
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001578static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001579add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001580 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001581{
1582 struct smb2_create_req *req = iov[0].iov_base;
1583 unsigned int num = *num_iovec;
1584
Steve Frenchb56eae42015-11-03 09:26:27 -06001585 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1586 if (iov[num].iov_base == NULL)
1587 return -ENOMEM;
1588 iov[num].iov_len = sizeof(struct create_durable_v2);
1589 if (!req->CreateContextsOffset)
1590 req->CreateContextsOffset =
1591 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1592 iov[1].iov_len);
1593 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1594 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1595 *num_iovec = num + 1;
1596 return 0;
1597}
1598
1599static int
1600add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1601 struct cifs_open_parms *oparms)
1602{
1603 struct smb2_create_req *req = iov[0].iov_base;
1604 unsigned int num = *num_iovec;
1605
1606 /* indicate that we don't need to relock the file */
1607 oparms->reconnect = false;
1608
1609 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1610 if (iov[num].iov_base == NULL)
1611 return -ENOMEM;
1612 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1613 if (!req->CreateContextsOffset)
1614 req->CreateContextsOffset =
1615 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1616 iov[1].iov_len);
1617 le32_add_cpu(&req->CreateContextsLength,
1618 sizeof(struct create_durable_handle_reconnect_v2));
1619 inc_rfc1001_len(&req->hdr,
1620 sizeof(struct create_durable_handle_reconnect_v2));
1621 *num_iovec = num + 1;
1622 return 0;
1623}
1624
1625static int
1626add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1627 struct cifs_open_parms *oparms, bool use_persistent)
1628{
1629 struct smb2_create_req *req = iov[0].iov_base;
1630 unsigned int num = *num_iovec;
1631
1632 if (use_persistent) {
1633 if (oparms->reconnect)
1634 return add_durable_reconnect_v2_context(iov, num_iovec,
1635 oparms);
1636 else
1637 return add_durable_v2_context(iov, num_iovec, oparms);
1638 }
1639
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001640 if (oparms->reconnect) {
1641 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1642 /* indicate that we don't need to relock the file */
1643 oparms->reconnect = false;
1644 } else
1645 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001646 if (iov[num].iov_base == NULL)
1647 return -ENOMEM;
1648 iov[num].iov_len = sizeof(struct create_durable);
1649 if (!req->CreateContextsOffset)
1650 req->CreateContextsOffset =
1651 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1652 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001653 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001654 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1655 *num_iovec = num + 1;
1656 return 0;
1657}
1658
Aurelien Aptelf0712922017-02-22 14:47:17 +01001659static int
1660alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1661 const char *treename, const __le16 *path)
1662{
1663 int treename_len, path_len;
1664 struct nls_table *cp;
1665 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1666
1667 /*
1668 * skip leading "\\"
1669 */
1670 treename_len = strlen(treename);
1671 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1672 return -EINVAL;
1673
1674 treename += 2;
1675 treename_len -= 2;
1676
1677 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1678
1679 /*
1680 * make room for one path separator between the treename and
1681 * path
1682 */
1683 *out_len = treename_len + 1 + path_len;
1684
1685 /*
1686 * final path needs to be null-terminated UTF16 with a
1687 * size aligned to 8
1688 */
1689
1690 *out_size = roundup((*out_len+1)*2, 8);
1691 *out_path = kzalloc(*out_size, GFP_KERNEL);
1692 if (!*out_path)
1693 return -ENOMEM;
1694
1695 cp = load_nls_default();
1696 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1697 UniStrcat(*out_path, sep);
1698 UniStrcat(*out_path, path);
1699 unload_nls(cp);
1700
1701 return 0;
1702}
1703
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001704int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001705SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001706 __u8 *oplock, struct smb2_file_all_info *buf,
1707 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001708{
1709 struct smb2_create_req *req;
1710 struct smb2_create_rsp *rsp;
1711 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001712 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001713 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001714 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001715 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001716 int resp_buftype;
1717 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001718 __le16 *copy_path = NULL;
1719 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001720 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001721 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001722 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001723 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001724 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001725
Joe Perchesf96637b2013-05-04 22:12:25 -05001726 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001727
1728 if (ses && (ses->server))
1729 server = ses->server;
1730 else
1731 return -EIO;
1732
1733 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1734 if (rc)
1735 return rc;
1736
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001737 if (encryption_required(tcon))
1738 flags |= CIFS_TRANSFORM_REQ;
1739
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001740 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001741 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001742 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1743 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001744
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001745 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001746 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001747 /* File attributes ignored on open (used in create though) */
1748 req->FileAttributes = cpu_to_le32(file_attributes);
1749 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001750 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1751 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001752
1753 iov[0].iov_base = (char *)req;
1754 /* 4 for rfc1002 length field */
1755 iov[0].iov_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001756 /* -1 since last byte is buf[0] which is sent below (path) */
1757 iov[0].iov_len--;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001758
Aurelien Aptelf0712922017-02-22 14:47:17 +01001759 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1760
1761 /* [MS-SMB2] 2.2.13 NameOffset:
1762 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1763 * the SMB2 header, the file name includes a prefix that will
1764 * be processed during DFS name normalization as specified in
1765 * section 3.3.5.9. Otherwise, the file name is relative to
1766 * the share that is identified by the TreeId in the SMB2
1767 * header.
1768 */
1769 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1770 int name_len;
1771
1772 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1773 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1774 &name_len,
1775 tcon->treeName, path);
1776 if (rc)
1777 return rc;
1778 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001779 uni_path_len = copy_size;
1780 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001781 } else {
1782 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1783 /* MUST set path len (NameLength) to 0 opening root of share */
1784 req->NameLength = cpu_to_le16(uni_path_len - 2);
1785 if (uni_path_len % 8 != 0) {
1786 copy_size = roundup(uni_path_len, 8);
1787 copy_path = kzalloc(copy_size, GFP_KERNEL);
1788 if (!copy_path)
1789 return -ENOMEM;
1790 memcpy((char *)copy_path, (const char *)path,
1791 uni_path_len);
1792 uni_path_len = copy_size;
1793 path = copy_path;
1794 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001795 }
1796
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001797 iov[1].iov_len = uni_path_len;
1798 iov[1].iov_base = path;
1799 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1800 inc_rfc1001_len(req, uni_path_len - 1);
1801
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001802 if (!server->oplocks)
1803 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1804
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001805 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001806 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1807 req->RequestedOplockLevel = *oplock;
1808 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001809 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001810 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001811 cifs_small_buf_release(req);
1812 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001813 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001814 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001815 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001816 }
1817
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001818 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1819 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001820 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001821 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001822 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001823 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001824 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001825 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001826
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001827 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001828 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001829 if (rc) {
1830 cifs_small_buf_release(req);
1831 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001832 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001833 return rc;
1834 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001835 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001836 }
1837
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001838 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001839 cifs_small_buf_release(req);
1840 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001841
1842 if (rc != 0) {
1843 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001844 if (err_buf && rsp)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001845 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1846 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001847 goto creat_exit;
1848 }
1849
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001850 oparms->fid->persistent_fid = rsp->PersistentFileId;
1851 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001852
1853 if (buf) {
1854 memcpy(buf, &rsp->CreationTime, 32);
1855 buf->AllocationSize = rsp->AllocationSize;
1856 buf->EndOfFile = rsp->EndofFile;
1857 buf->Attributes = rsp->FileAttributes;
1858 buf->NumberOfLinks = cpu_to_le32(1);
1859 buf->DeletePending = 0;
1860 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001861
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001862 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001863 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001864 else
1865 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001866creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001867 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001868 kfree(lc_buf);
1869 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001870 free_rsp_buf(resp_buftype, rsp);
1871 return rc;
1872}
1873
Steve French4a72daf2013-06-25 00:20:49 -05001874/*
1875 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1876 */
1877int
1878SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001879 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1880 char *in_data, u32 indatalen,
1881 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001882{
1883 struct smb2_ioctl_req *req;
1884 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001885 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001886 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001887 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001888 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001889 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001890 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001891 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001892 int flags = 0;
Steve French4a72daf2013-06-25 00:20:49 -05001893
1894 cifs_dbg(FYI, "SMB2 IOCTL\n");
1895
Steve French3d1a3742014-08-11 21:05:25 -05001896 if (out_data != NULL)
1897 *out_data = NULL;
1898
Steve French4a72daf2013-06-25 00:20:49 -05001899 /* zero out returned data len, in case of error */
1900 if (plen)
1901 *plen = 0;
1902
Steve French8e353102015-03-26 19:47:02 -05001903 if (tcon)
1904 ses = tcon->ses;
1905 else
1906 return -EIO;
1907
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001908 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05001909 return -EIO;
1910
1911 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1912 if (rc)
1913 return rc;
1914
Aurelien Aptel51146622017-02-28 15:08:41 +01001915 if (use_ipc) {
1916 if (ses->ipc_tid == 0) {
1917 cifs_small_buf_release(req);
1918 return -ENOTCONN;
1919 }
1920
1921 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1922 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1923 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1924 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001925 if (encryption_required(tcon))
1926 flags |= CIFS_TRANSFORM_REQ;
1927
Steve French4a72daf2013-06-25 00:20:49 -05001928 req->CtlCode = cpu_to_le32(opcode);
1929 req->PersistentFileId = persistent_fid;
1930 req->VolatileFileId = volatile_fid;
1931
1932 if (indatalen) {
1933 req->InputCount = cpu_to_le32(indatalen);
1934 /* do not set InputOffset if no input data */
1935 req->InputOffset =
1936 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1937 iov[1].iov_base = in_data;
1938 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001939 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001940 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001941 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001942
1943 req->OutputOffset = 0;
1944 req->OutputCount = 0; /* MBZ */
1945
1946 /*
1947 * Could increase MaxOutputResponse, but that would require more
1948 * than one credit. Windows typically sets this smaller, but for some
1949 * ioctls it may be useful to allow server to send more. No point
1950 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001951 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1952 * (by default, note that it can be overridden to make max larger)
1953 * in responses (except for read responses which can be bigger.
1954 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001955 */
Steve French7db0a6e2017-05-03 21:12:20 -05001956 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001957
1958 if (is_fsctl)
1959 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1960 else
1961 req->Flags = 0;
1962
1963 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001964
Steve French7ff8d452013-10-14 00:44:19 -05001965 /*
1966 * If no input data, the size of ioctl struct in
1967 * protocol spec still includes a 1 byte data buffer,
1968 * but if input data passed to ioctl, we do not
1969 * want to double count this, so we do not send
1970 * the dummy one byte of data in iovec[0] if sending
1971 * input data (in iovec[1]). We also must add 4 bytes
1972 * in first iovec to allow for rfc1002 length field.
1973 */
1974
1975 if (indatalen) {
1976 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1977 inc_rfc1001_len(req, indatalen - 1);
1978 } else
1979 iov[0].iov_len = get_rfc1002_length(req) + 4;
1980
Steve French4a72daf2013-06-25 00:20:49 -05001981
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001982 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001983 cifs_small_buf_release(req);
1984 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05001985
Steve French9bf0c9c2013-11-16 18:05:28 -06001986 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001987 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001988 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001989 } else if (rc == -EINVAL) {
1990 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1991 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001992 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001993 goto ioctl_exit;
1994 }
Steve French4a72daf2013-06-25 00:20:49 -05001995 }
1996
1997 /* check if caller wants to look at return data or just return rc */
1998 if ((plen == NULL) || (out_data == NULL))
1999 goto ioctl_exit;
2000
2001 *plen = le32_to_cpu(rsp->OutputCount);
2002
2003 /* We check for obvious errors in the output buffer length and offset */
2004 if (*plen == 0)
2005 goto ioctl_exit; /* server returned no data */
2006 else if (*plen > 0xFF00) {
2007 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2008 *plen = 0;
2009 rc = -EIO;
2010 goto ioctl_exit;
2011 }
2012
2013 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2014 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2015 le32_to_cpu(rsp->OutputOffset));
2016 *plen = 0;
2017 rc = -EIO;
2018 goto ioctl_exit;
2019 }
2020
2021 *out_data = kmalloc(*plen, GFP_KERNEL);
2022 if (*out_data == NULL) {
2023 rc = -ENOMEM;
2024 goto ioctl_exit;
2025 }
2026
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002027 shdr = get_sync_hdr(rsp);
2028 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002029ioctl_exit:
2030 free_rsp_buf(resp_buftype, rsp);
2031 return rc;
2032}
2033
Steve French64a5cfa2013-10-14 15:31:32 -05002034/*
2035 * Individual callers to ioctl worker function follow
2036 */
2037
2038int
2039SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2040 u64 persistent_fid, u64 volatile_fid)
2041{
2042 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002043 struct compress_ioctl fsctl_input;
2044 char *ret_data = NULL;
2045
2046 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002047 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002048
2049 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2050 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002051 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05002052 (char *)&fsctl_input /* data input */,
2053 2 /* in data len */, &ret_data /* out data */, NULL);
2054
2055 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002056
2057 return rc;
2058}
2059
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002060int
2061SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2062 u64 persistent_fid, u64 volatile_fid)
2063{
2064 struct smb2_close_req *req;
2065 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002066 struct cifs_ses *ses = tcon->ses;
2067 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002068 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002069 int resp_buftype;
2070 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002071 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002072
Joe Perchesf96637b2013-05-04 22:12:25 -05002073 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002074
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002075 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002076 return -EIO;
2077
2078 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
2079 if (rc)
2080 return rc;
2081
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002082 if (encryption_required(tcon))
2083 flags |= CIFS_TRANSFORM_REQ;
2084
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002085 req->PersistentFileId = persistent_fid;
2086 req->VolatileFileId = volatile_fid;
2087
2088 iov[0].iov_base = (char *)req;
2089 /* 4 for rfc1002 length field */
2090 iov[0].iov_len = get_rfc1002_length(req) + 4;
2091
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002092 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002093 cifs_small_buf_release(req);
2094 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002095
2096 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002097 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002098 goto close_exit;
2099 }
2100
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002101 /* BB FIXME - decode close response, update inode for caching */
2102
2103close_exit:
2104 free_rsp_buf(resp_buftype, rsp);
2105 return rc;
2106}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002107
2108static int
2109validate_buf(unsigned int offset, unsigned int buffer_length,
2110 struct smb2_hdr *hdr, unsigned int min_buf_size)
2111
2112{
2113 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2114 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2115 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2116 char *end_of_buf = begin_of_buf + buffer_length;
2117
2118
2119 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002120 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2121 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002122 return -EINVAL;
2123 }
2124
2125 /* check if beyond RFC1001 maximum length */
2126 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002127 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2128 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002129 return -EINVAL;
2130 }
2131
2132 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002133 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002134 return -EINVAL;
2135 }
2136
2137 return 0;
2138}
2139
2140/*
2141 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2142 * Caller must free buffer.
2143 */
2144static int
2145validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2146 struct smb2_hdr *hdr, unsigned int minbufsize,
2147 char *data)
2148
2149{
2150 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2151 int rc;
2152
2153 if (!data)
2154 return -EINVAL;
2155
2156 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2157 if (rc)
2158 return rc;
2159
2160 memcpy(data, begin_of_buf, buffer_length);
2161
2162 return 0;
2163}
2164
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002165static int
2166query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002167 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2168 u32 additional_info, size_t output_len, size_t min_len, void **data,
2169 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002170{
2171 struct smb2_query_info_req *req;
2172 struct smb2_query_info_rsp *rsp = NULL;
2173 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002174 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002175 int rc = 0;
2176 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002177 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002178 int flags = 0;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002179
Joe Perchesf96637b2013-05-04 22:12:25 -05002180 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002181
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002182 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002183 return -EIO;
2184
2185 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2186 if (rc)
2187 return rc;
2188
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002189 if (encryption_required(tcon))
2190 flags |= CIFS_TRANSFORM_REQ;
2191
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002192 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002193 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002194 req->PersistentFileId = persistent_fid;
2195 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002196 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002197
2198 /*
2199 * We do not use the input buffer (do not send extra byte)
2200 */
2201 req->InputBufferOffset = 0;
2202 inc_rfc1001_len(req, -1);
2203
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002204 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002205
2206 iov[0].iov_base = (char *)req;
2207 /* 4 for rfc1002 length field */
2208 iov[0].iov_len = get_rfc1002_length(req) + 4;
2209
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002210 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002211 cifs_small_buf_release(req);
2212 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002213
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002214 if (rc) {
2215 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2216 goto qinf_exit;
2217 }
2218
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002219 if (dlen) {
2220 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2221 if (!*data) {
2222 *data = kmalloc(*dlen, GFP_KERNEL);
2223 if (!*data) {
2224 cifs_dbg(VFS,
2225 "Error %d allocating memory for acl\n",
2226 rc);
2227 *dlen = 0;
2228 goto qinf_exit;
2229 }
2230 }
2231 }
2232
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002233 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2234 le32_to_cpu(rsp->OutputBufferLength),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002235 &rsp->hdr, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002236
2237qinf_exit:
2238 free_rsp_buf(resp_buftype, rsp);
2239 return rc;
2240}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002241
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002242int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002243 u64 persistent_fid, u64 volatile_fid,
2244 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002245{
2246 return query_info(xid, tcon, persistent_fid, volatile_fid,
2247 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002248 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002249 sizeof(struct smb2_file_full_ea_info),
2250 (void **)&data,
2251 NULL);
2252}
2253
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002254int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2255 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002256{
2257 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002258 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002259 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002260 sizeof(struct smb2_file_all_info), (void **)&data,
2261 NULL);
2262}
2263
2264int
2265SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2266 u64 persistent_fid, u64 volatile_fid,
2267 void **data, u32 *plen)
2268{
2269 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2270 *plen = 0;
2271
2272 return query_info(xid, tcon, persistent_fid, volatile_fid,
2273 0, SMB2_O_INFO_SECURITY, additional_info,
2274 SMB2_MAX_BUFFER_SIZE,
2275 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002276}
2277
2278int
2279SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2280 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2281{
2282 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002283 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002284 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002285 sizeof(struct smb2_file_internal_info),
2286 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002287}
2288
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002289/*
2290 * This is a no-op for now. We're not really interested in the reply, but
2291 * rather in the fact that the server sent one and that server->lstrp
2292 * gets updated.
2293 *
2294 * FIXME: maybe we should consider checking that the reply matches request?
2295 */
2296static void
2297smb2_echo_callback(struct mid_q_entry *mid)
2298{
2299 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002300 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002301 unsigned int credits_received = 1;
2302
2303 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002304 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002305
2306 DeleteMidQEntry(mid);
2307 add_credits(server, credits_received, CIFS_ECHO_OP);
2308}
2309
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002310void smb2_reconnect_server(struct work_struct *work)
2311{
2312 struct TCP_Server_Info *server = container_of(work,
2313 struct TCP_Server_Info, reconnect.work);
2314 struct cifs_ses *ses;
2315 struct cifs_tcon *tcon, *tcon2;
2316 struct list_head tmp_list;
2317 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002318 int rc;
2319 int resched = false;
2320
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002321
2322 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2323 mutex_lock(&server->reconnect_mutex);
2324
2325 INIT_LIST_HEAD(&tmp_list);
2326 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2327
2328 spin_lock(&cifs_tcp_ses_lock);
2329 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2330 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002331 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002332 tcon->tc_count++;
2333 list_add_tail(&tcon->rlist, &tmp_list);
2334 tcon_exist = true;
2335 }
2336 }
2337 }
2338 /*
2339 * Get the reference to server struct to be sure that the last call of
2340 * cifs_put_tcon() in the loop below won't release the server pointer.
2341 */
2342 if (tcon_exist)
2343 server->srv_count++;
2344
2345 spin_unlock(&cifs_tcp_ses_lock);
2346
2347 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002348 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2349 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002350 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002351 else
2352 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002353 list_del_init(&tcon->rlist);
2354 cifs_put_tcon(tcon);
2355 }
2356
2357 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002358 if (resched)
2359 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002360 mutex_unlock(&server->reconnect_mutex);
2361
2362 /* now we can safely release srv struct */
2363 if (tcon_exist)
2364 cifs_put_tcp_session(server, 1);
2365}
2366
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002367int
2368SMB2_echo(struct TCP_Server_Info *server)
2369{
2370 struct smb2_echo_req *req;
2371 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002372 struct kvec iov[2];
2373 struct smb_rqst rqst = { .rq_iov = iov,
2374 .rq_nvec = 2 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002375
Joe Perchesf96637b2013-05-04 22:12:25 -05002376 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002377
Steve French4fcd1812016-06-22 20:12:05 -05002378 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002379 /* No need to send echo on newly established connections */
2380 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2381 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002382 }
2383
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002384 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2385 if (rc)
2386 return rc;
2387
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002388 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002389
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002390 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002391 iov[0].iov_len = 4;
2392 iov[0].iov_base = (char *)req;
2393 iov[1].iov_len = get_rfc1002_length(req);
2394 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002395
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002396 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2397 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002398 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002399 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002400
2401 cifs_small_buf_release(req);
2402 return rc;
2403}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002404
2405int
2406SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2407 u64 volatile_fid)
2408{
2409 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002410 struct cifs_ses *ses = tcon->ses;
2411 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002412 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002413 int resp_buftype;
2414 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002415 int flags = 0;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002416
Joe Perchesf96637b2013-05-04 22:12:25 -05002417 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002418
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002419 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002420 return -EIO;
2421
2422 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2423 if (rc)
2424 return rc;
2425
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002426 if (encryption_required(tcon))
2427 flags |= CIFS_TRANSFORM_REQ;
2428
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002429 req->PersistentFileId = persistent_fid;
2430 req->VolatileFileId = volatile_fid;
2431
2432 iov[0].iov_base = (char *)req;
2433 /* 4 for rfc1002 length field */
2434 iov[0].iov_len = get_rfc1002_length(req) + 4;
2435
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002436 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002437 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002438
Steve Frenchdfebe402015-03-27 01:00:06 -05002439 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002440 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2441
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002442 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002443 return rc;
2444}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002445
2446/*
2447 * To form a chain of read requests, any read requests after the first should
2448 * have the end_of_chain boolean set to true.
2449 */
2450static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002451smb2_new_read_req(void **buf, unsigned int *total_len,
2452 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2453 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002454{
2455 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002456 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002457 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002458
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002459 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2460 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002461 if (rc)
2462 return rc;
2463 if (io_parms->tcon->ses->server == NULL)
2464 return -ECONNABORTED;
2465
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002466 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002467 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002468
2469 req->PersistentFileId = io_parms->persistent_fid;
2470 req->VolatileFileId = io_parms->volatile_fid;
2471 req->ReadChannelInfoOffset = 0; /* reserved */
2472 req->ReadChannelInfoLength = 0; /* reserved */
2473 req->Channel = 0; /* reserved */
2474 req->MinimumCount = 0;
2475 req->Length = cpu_to_le32(io_parms->length);
2476 req->Offset = cpu_to_le64(io_parms->offset);
2477
2478 if (request_type & CHAINED_REQUEST) {
2479 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002480 /* next 8-byte aligned request */
2481 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2482 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002483 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002484 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002485 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002486 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002487 /*
2488 * Related requests use info from previous read request
2489 * in chain.
2490 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002491 shdr->SessionId = 0xFFFFFFFF;
2492 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002493 req->PersistentFileId = 0xFFFFFFFF;
2494 req->VolatileFileId = 0xFFFFFFFF;
2495 }
2496 }
2497 if (remaining_bytes > io_parms->length)
2498 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2499 else
2500 req->RemainingBytes = 0;
2501
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002502 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002503 return rc;
2504}
2505
2506static void
2507smb2_readv_callback(struct mid_q_entry *mid)
2508{
2509 struct cifs_readdata *rdata = mid->callback_data;
2510 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2511 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002512 struct smb2_sync_hdr *shdr =
2513 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002514 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002515 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2516 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002517 .rq_pages = rdata->pages,
2518 .rq_npages = rdata->nr_pages,
2519 .rq_pagesz = rdata->pagesz,
2520 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002521
Joe Perchesf96637b2013-05-04 22:12:25 -05002522 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2523 __func__, mid->mid, mid->mid_state, rdata->result,
2524 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002525
2526 switch (mid->mid_state) {
2527 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002528 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002529 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002530 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002531 int rc;
2532
Jeff Layton0b688cf2012-09-18 16:20:34 -07002533 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002534 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002535 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2536 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002537 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002538 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002539 task_io_account_read(rdata->got_bytes);
2540 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002541 break;
2542 case MID_REQUEST_SUBMITTED:
2543 case MID_RETRY_NEEDED:
2544 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002545 if (server->sign && rdata->got_bytes)
2546 /* reset bytes number since we can not check a sign */
2547 rdata->got_bytes = 0;
2548 /* FIXME: should this be counted toward the initiating task? */
2549 task_io_account_read(rdata->got_bytes);
2550 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002551 break;
2552 default:
2553 if (rdata->result != -ENODATA)
2554 rdata->result = -EIO;
2555 }
2556
2557 if (rdata->result)
2558 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2559
2560 queue_work(cifsiod_wq, &rdata->work);
2561 DeleteMidQEntry(mid);
2562 add_credits(server, credits_received, 0);
2563}
2564
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002565/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002566int
2567smb2_async_readv(struct cifs_readdata *rdata)
2568{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002569 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002570 char *buf;
2571 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002572 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002573 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2574 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002575 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002576 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002577 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002578
Joe Perchesf96637b2013-05-04 22:12:25 -05002579 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2580 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002581
2582 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2583 io_parms.offset = rdata->offset;
2584 io_parms.length = rdata->bytes;
2585 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2586 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2587 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002588
2589 server = io_parms.tcon->ses->server;
2590
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002591 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002592 if (rc) {
2593 if (rc == -EAGAIN && rdata->credits) {
2594 /* credits was reset by reconnect */
2595 rdata->credits = 0;
2596 /* reduce in_flight value since we won't send the req */
2597 spin_lock(&server->req_lock);
2598 server->in_flight--;
2599 spin_unlock(&server->req_lock);
2600 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002601 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002602 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002603
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002604 if (encryption_required(io_parms.tcon))
2605 flags |= CIFS_TRANSFORM_REQ;
2606
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002607 req_len = cpu_to_be32(total_len);
2608
2609 rdata->iov[0].iov_base = &req_len;
2610 rdata->iov[0].iov_len = sizeof(__be32);
2611 rdata->iov[1].iov_base = buf;
2612 rdata->iov[1].iov_len = total_len;
2613
2614 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002615
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002616 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002617 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002618 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002619 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002620 spin_lock(&server->req_lock);
2621 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002622 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002623 spin_unlock(&server->req_lock);
2624 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002625 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002626 }
2627
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002628 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002629 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002630 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002631 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002632 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002633 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002634 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2635 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002636
2637 cifs_small_buf_release(buf);
2638 return rc;
2639}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002640
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002641int
2642SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2643 unsigned int *nbytes, char **buf, int *buf_type)
2644{
2645 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002646 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002647 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002648 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002649 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002650 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002651 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002652 __be32 req_len;
2653 struct smb_rqst rqst = { .rq_iov = iov,
2654 .rq_nvec = 2 };
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002655 int flags = CIFS_LOG_ERROR;
2656 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002657
2658 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002659 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002660 if (rc)
2661 return rc;
2662
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002663 if (encryption_required(io_parms->tcon))
2664 flags |= CIFS_TRANSFORM_REQ;
2665
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002666 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002667
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002668 iov[0].iov_base = &req_len;
2669 iov[0].iov_len = sizeof(__be32);
2670 iov[1].iov_base = req;
2671 iov[1].iov_len = total_len;
2672
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002673 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002674 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002675
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002676 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002677 shdr = get_sync_hdr(rsp);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002678
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002679 if (shdr->Status == STATUS_END_OF_FILE) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002680 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002681 return 0;
2682 }
2683
2684 if (rc) {
2685 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002686 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002687 } else {
2688 *nbytes = le32_to_cpu(rsp->DataLength);
2689 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2690 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002691 cifs_dbg(FYI, "bad length %d for count %d\n",
2692 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002693 rc = -EIO;
2694 *nbytes = 0;
2695 }
2696 }
2697
2698 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002699 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002700 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002701 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002702 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002703 if (resp_buftype == CIFS_SMALL_BUFFER)
2704 *buf_type = CIFS_SMALL_BUFFER;
2705 else if (resp_buftype == CIFS_LARGE_BUFFER)
2706 *buf_type = CIFS_LARGE_BUFFER;
2707 }
2708 return rc;
2709}
2710
Pavel Shilovsky33319142012-09-18 16:20:29 -07002711/*
2712 * Check the mid_state and signature on received buffer (if any), and queue the
2713 * workqueue completion task.
2714 */
2715static void
2716smb2_writev_callback(struct mid_q_entry *mid)
2717{
2718 struct cifs_writedata *wdata = mid->callback_data;
2719 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2720 unsigned int written;
2721 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2722 unsigned int credits_received = 1;
2723
2724 switch (mid->mid_state) {
2725 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002726 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002727 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2728 if (wdata->result != 0)
2729 break;
2730
2731 written = le32_to_cpu(rsp->DataLength);
2732 /*
2733 * Mask off high 16 bits when bytes written as returned
2734 * by the server is greater than bytes requested by the
2735 * client. OS/2 servers are known to set incorrect
2736 * CountHigh values.
2737 */
2738 if (written > wdata->bytes)
2739 written &= 0xFFFF;
2740
2741 if (written < wdata->bytes)
2742 wdata->result = -ENOSPC;
2743 else
2744 wdata->bytes = written;
2745 break;
2746 case MID_REQUEST_SUBMITTED:
2747 case MID_RETRY_NEEDED:
2748 wdata->result = -EAGAIN;
2749 break;
2750 default:
2751 wdata->result = -EIO;
2752 break;
2753 }
2754
2755 if (wdata->result)
2756 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2757
2758 queue_work(cifsiod_wq, &wdata->work);
2759 DeleteMidQEntry(mid);
2760 add_credits(tcon->ses->server, credits_received, 0);
2761}
2762
2763/* smb2_async_writev - send an async write, and set up mid to handle result */
2764int
Steve French4a5c80d2014-02-07 20:45:12 -06002765smb2_async_writev(struct cifs_writedata *wdata,
2766 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002767{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002768 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002769 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002770 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002771 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002772 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002773 struct kvec iov[2];
2774 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002775
2776 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002777 if (rc) {
2778 if (rc == -EAGAIN && wdata->credits) {
2779 /* credits was reset by reconnect */
2780 wdata->credits = 0;
2781 /* reduce in_flight value since we won't send the req */
2782 spin_lock(&server->req_lock);
2783 server->in_flight--;
2784 spin_unlock(&server->req_lock);
2785 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002786 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002787 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002788
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002789 if (encryption_required(tcon))
2790 flags |= CIFS_TRANSFORM_REQ;
2791
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002792 shdr = get_sync_hdr(req);
2793 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002794
2795 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2796 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2797 req->WriteChannelInfoOffset = 0;
2798 req->WriteChannelInfoLength = 0;
2799 req->Channel = 0;
2800 req->Offset = cpu_to_le64(wdata->offset);
2801 /* 4 for rfc1002 length field */
2802 req->DataOffset = cpu_to_le16(
2803 offsetof(struct smb2_write_req, Buffer) - 4);
2804 req->RemainingBytes = 0;
2805
2806 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002807 iov[0].iov_len = 4;
2808 iov[0].iov_base = req;
2809 iov[1].iov_len = get_rfc1002_length(req) - 1;
2810 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002811
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002812 rqst.rq_iov = iov;
2813 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002814 rqst.rq_pages = wdata->pages;
2815 rqst.rq_npages = wdata->nr_pages;
2816 rqst.rq_pagesz = wdata->pagesz;
2817 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002818
Joe Perchesf96637b2013-05-04 22:12:25 -05002819 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2820 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002821
2822 req->Length = cpu_to_le32(wdata->bytes);
2823
2824 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2825
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002826 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002827 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002828 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002829 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002830 spin_lock(&server->req_lock);
2831 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002832 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002833 spin_unlock(&server->req_lock);
2834 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002835 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002836 }
2837
Pavel Shilovsky33319142012-09-18 16:20:29 -07002838 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002839 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2840 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002841
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002842 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002843 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002844 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2845 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002846
Pavel Shilovsky33319142012-09-18 16:20:29 -07002847async_writev_out:
2848 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002849 return rc;
2850}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002851
2852/*
2853 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2854 * The length field from io_parms must be at least 1 and indicates a number of
2855 * elements with data to write that begins with position 1 in iov array. All
2856 * data length is specified by count.
2857 */
2858int
2859SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2860 unsigned int *nbytes, struct kvec *iov, int n_vec)
2861{
2862 int rc = 0;
2863 struct smb2_write_req *req = NULL;
2864 struct smb2_write_rsp *rsp = NULL;
2865 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002866 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002867 int flags = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002868
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002869 *nbytes = 0;
2870
2871 if (n_vec < 1)
2872 return rc;
2873
2874 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2875 if (rc)
2876 return rc;
2877
2878 if (io_parms->tcon->ses->server == NULL)
2879 return -ECONNABORTED;
2880
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002881 if (encryption_required(io_parms->tcon))
2882 flags |= CIFS_TRANSFORM_REQ;
2883
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002884 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002885
2886 req->PersistentFileId = io_parms->persistent_fid;
2887 req->VolatileFileId = io_parms->volatile_fid;
2888 req->WriteChannelInfoOffset = 0;
2889 req->WriteChannelInfoLength = 0;
2890 req->Channel = 0;
2891 req->Length = cpu_to_le32(io_parms->length);
2892 req->Offset = cpu_to_le64(io_parms->offset);
2893 /* 4 for rfc1002 length field */
2894 req->DataOffset = cpu_to_le16(
2895 offsetof(struct smb2_write_req, Buffer) - 4);
2896 req->RemainingBytes = 0;
2897
2898 iov[0].iov_base = (char *)req;
2899 /* 4 for rfc1002 length field and 1 for Buffer */
2900 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2901
2902 /* length of entire message including data to be written */
2903 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2904
2905 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002906 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002907 cifs_small_buf_release(req);
2908 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002909
2910 if (rc) {
2911 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002912 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002913 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002914 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002915
2916 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002917 return rc;
2918}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002919
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002920static unsigned int
2921num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2922{
2923 int len;
2924 unsigned int entrycount = 0;
2925 unsigned int next_offset = 0;
2926 FILE_DIRECTORY_INFO *entryptr;
2927
2928 if (bufstart == NULL)
2929 return 0;
2930
2931 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2932
2933 while (1) {
2934 entryptr = (FILE_DIRECTORY_INFO *)
2935 ((char *)entryptr + next_offset);
2936
2937 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002938 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002939 break;
2940 }
2941
2942 len = le32_to_cpu(entryptr->FileNameLength);
2943 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002944 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2945 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002946 break;
2947 }
2948
2949 *lastentry = (char *)entryptr;
2950 entrycount++;
2951
2952 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2953 if (!next_offset)
2954 break;
2955 }
2956
2957 return entrycount;
2958}
2959
2960/*
2961 * Readdir/FindFirst
2962 */
2963int
2964SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2965 u64 persistent_fid, u64 volatile_fid, int index,
2966 struct cifs_search_info *srch_inf)
2967{
2968 struct smb2_query_directory_req *req;
2969 struct smb2_query_directory_rsp *rsp = NULL;
2970 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002971 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002972 int rc = 0;
2973 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002974 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002975 unsigned char *bufptr;
2976 struct TCP_Server_Info *server;
2977 struct cifs_ses *ses = tcon->ses;
2978 __le16 asteriks = cpu_to_le16('*');
2979 char *end_of_smb;
2980 unsigned int output_size = CIFSMaxBufSize;
2981 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002982 int flags = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002983
2984 if (ses && (ses->server))
2985 server = ses->server;
2986 else
2987 return -EIO;
2988
2989 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2990 if (rc)
2991 return rc;
2992
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002993 if (encryption_required(tcon))
2994 flags |= CIFS_TRANSFORM_REQ;
2995
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002996 switch (srch_inf->info_level) {
2997 case SMB_FIND_FILE_DIRECTORY_INFO:
2998 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2999 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3000 break;
3001 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3002 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3003 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3004 break;
3005 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003006 cifs_dbg(VFS, "info level %u isn't supported\n",
3007 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003008 rc = -EINVAL;
3009 goto qdir_exit;
3010 }
3011
3012 req->FileIndex = cpu_to_le32(index);
3013 req->PersistentFileId = persistent_fid;
3014 req->VolatileFileId = volatile_fid;
3015
3016 len = 0x2;
3017 bufptr = req->Buffer;
3018 memcpy(bufptr, &asteriks, len);
3019
3020 req->FileNameOffset =
3021 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
3022 req->FileNameLength = cpu_to_le16(len);
3023 /*
3024 * BB could be 30 bytes or so longer if we used SMB2 specific
3025 * buffer lengths, but this is safe and close enough.
3026 */
3027 output_size = min_t(unsigned int, output_size, server->maxBuf);
3028 output_size = min_t(unsigned int, output_size, 2 << 15);
3029 req->OutputBufferLength = cpu_to_le32(output_size);
3030
3031 iov[0].iov_base = (char *)req;
3032 /* 4 for RFC1001 length and 1 for Buffer */
3033 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
3034
3035 iov[1].iov_base = (char *)(req->Buffer);
3036 iov[1].iov_len = len;
3037
3038 inc_rfc1001_len(req, len - 1 /* Buffer */);
3039
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003040 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003041 cifs_small_buf_release(req);
3042 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003043
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003044 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003045 if (rc == -ENODATA &&
3046 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003047 srch_inf->endOfSearch = true;
3048 rc = 0;
3049 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003050 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3051 goto qdir_exit;
3052 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003053
3054 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3055 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3056 info_buf_size);
3057 if (rc)
3058 goto qdir_exit;
3059
3060 srch_inf->unicode = true;
3061
3062 if (srch_inf->ntwrk_buf_start) {
3063 if (srch_inf->smallBuf)
3064 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3065 else
3066 cifs_buf_release(srch_inf->ntwrk_buf_start);
3067 }
3068 srch_inf->ntwrk_buf_start = (char *)rsp;
3069 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3070 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3071 /* 4 for rfc1002 length field */
3072 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3073 srch_inf->entries_in_buffer =
3074 num_entries(srch_inf->srch_entries_start, end_of_smb,
3075 &srch_inf->last_entry, info_buf_size);
3076 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003077 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3078 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3079 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003080 if (resp_buftype == CIFS_LARGE_BUFFER)
3081 srch_inf->smallBuf = false;
3082 else if (resp_buftype == CIFS_SMALL_BUFFER)
3083 srch_inf->smallBuf = true;
3084 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003085 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003086
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003087 return rc;
3088
3089qdir_exit:
3090 free_rsp_buf(resp_buftype, rsp);
3091 return rc;
3092}
3093
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003094static int
3095send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003096 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3097 u8 info_type, u32 additional_info, unsigned int num,
3098 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003099{
3100 struct smb2_set_info_req *req;
3101 struct smb2_set_info_rsp *rsp = NULL;
3102 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003103 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003104 int rc = 0;
3105 int resp_buftype;
3106 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003107 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003108 int flags = 0;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003109
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003110 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003111 return -EIO;
3112
3113 if (!num)
3114 return -EINVAL;
3115
3116 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3117 if (!iov)
3118 return -ENOMEM;
3119
3120 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3121 if (rc) {
3122 kfree(iov);
3123 return rc;
3124 }
3125
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003126 if (encryption_required(tcon))
3127 flags |= CIFS_TRANSFORM_REQ;
3128
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003129 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003130
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003131 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003132 req->FileInfoClass = info_class;
3133 req->PersistentFileId = persistent_fid;
3134 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003135 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003136
3137 /* 4 for RFC1001 length and 1 for Buffer */
3138 req->BufferOffset =
3139 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3140 req->BufferLength = cpu_to_le32(*size);
3141
3142 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3143
3144 memcpy(req->Buffer, *data, *size);
3145
3146 iov[0].iov_base = (char *)req;
3147 /* 4 for RFC1001 length */
3148 iov[0].iov_len = get_rfc1002_length(req) + 4;
3149
3150 for (i = 1; i < num; i++) {
3151 inc_rfc1001_len(req, size[i]);
3152 le32_add_cpu(&req->BufferLength, size[i]);
3153 iov[i].iov_base = (char *)data[i];
3154 iov[i].iov_len = size[i];
3155 }
3156
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003157 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003158 cifs_small_buf_release(req);
3159 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003160
Steve French7d3fb242013-11-18 09:56:28 -06003161 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003162 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003163
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003164 free_rsp_buf(resp_buftype, rsp);
3165 kfree(iov);
3166 return rc;
3167}
3168
3169int
3170SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3171 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3172{
3173 struct smb2_file_rename_info info;
3174 void **data;
3175 unsigned int size[2];
3176 int rc;
3177 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3178
3179 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3180 if (!data)
3181 return -ENOMEM;
3182
3183 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3184 /* 0 = fail if target already exists */
3185 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3186 info.FileNameLength = cpu_to_le32(len);
3187
3188 data[0] = &info;
3189 size[0] = sizeof(struct smb2_file_rename_info);
3190
3191 data[1] = target_file;
3192 size[1] = len + 2 /* null */;
3193
3194 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003195 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3196 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003197 kfree(data);
3198 return rc;
3199}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003200
3201int
Steve French897fba12016-05-12 21:20:36 -05003202SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3203 u64 persistent_fid, u64 volatile_fid)
3204{
3205 __u8 delete_pending = 1;
3206 void *data;
3207 unsigned int size;
3208
3209 data = &delete_pending;
3210 size = 1; /* sizeof __u8 */
3211
3212 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003213 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3214 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003215}
3216
3217int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003218SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3219 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3220{
3221 struct smb2_file_link_info info;
3222 void **data;
3223 unsigned int size[2];
3224 int rc;
3225 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3226
3227 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3228 if (!data)
3229 return -ENOMEM;
3230
3231 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3232 /* 0 = fail if link already exists */
3233 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3234 info.FileNameLength = cpu_to_le32(len);
3235
3236 data[0] = &info;
3237 size[0] = sizeof(struct smb2_file_link_info);
3238
3239 data[1] = target_file;
3240 size[1] = len + 2 /* null */;
3241
3242 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003243 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3244 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003245 kfree(data);
3246 return rc;
3247}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003248
3249int
3250SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003251 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003252{
3253 struct smb2_file_eof_info info;
3254 void *data;
3255 unsigned int size;
3256
3257 info.EndOfFile = *eof;
3258
3259 data = &info;
3260 size = sizeof(struct smb2_file_eof_info);
3261
Steve Frenchf29ebb42014-07-19 21:44:58 -05003262 if (is_falloc)
3263 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003264 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3265 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003266 else
3267 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003268 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3269 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003270}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003271
3272int
3273SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3274 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3275{
3276 unsigned int size;
3277 size = sizeof(FILE_BASIC_INFO);
3278 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003279 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3280 0, 1, (void **)&buf, &size);
3281}
3282
3283int
3284SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3285 u64 persistent_fid, u64 volatile_fid,
3286 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3287{
3288 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3289 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3290 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003291}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003292
3293int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003294SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3295 u64 persistent_fid, u64 volatile_fid,
3296 struct smb2_file_full_ea_info *buf, int len)
3297{
3298 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3299 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3300 0, 1, (void **)&buf, &len);
3301}
3302
3303int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003304SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3305 const u64 persistent_fid, const u64 volatile_fid,
3306 __u8 oplock_level)
3307{
3308 int rc;
3309 struct smb2_oplock_break *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003310 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003311
Joe Perchesf96637b2013-05-04 22:12:25 -05003312 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003313 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003314 if (rc)
3315 return rc;
3316
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003317 if (encryption_required(tcon))
3318 flags |= CIFS_TRANSFORM_REQ;
3319
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003320 req->VolatileFid = volatile_fid;
3321 req->PersistentFid = persistent_fid;
3322 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003323 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003324
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003325 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003326 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003327
3328 if (rc) {
3329 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003330 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003331 }
3332
3333 return rc;
3334}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003335
3336static void
3337copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3338 struct kstatfs *kst)
3339{
3340 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3341 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3342 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303343 kst->f_bfree = kst->f_bavail =
3344 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003345 return;
3346}
3347
3348static int
3349build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3350 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3351{
3352 int rc;
3353 struct smb2_query_info_req *req;
3354
Joe Perchesf96637b2013-05-04 22:12:25 -05003355 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003356
3357 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3358 return -EIO;
3359
3360 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3361 if (rc)
3362 return rc;
3363
3364 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3365 req->FileInfoClass = level;
3366 req->PersistentFileId = persistent_fid;
3367 req->VolatileFileId = volatile_fid;
3368 /* 4 for rfc1002 length field and 1 for pad */
3369 req->InputBufferOffset =
3370 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3371 req->OutputBufferLength = cpu_to_le32(
3372 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3373
3374 iov->iov_base = (char *)req;
3375 /* 4 for rfc1002 length field */
3376 iov->iov_len = get_rfc1002_length(req) + 4;
3377 return 0;
3378}
3379
3380int
3381SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3382 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3383{
3384 struct smb2_query_info_rsp *rsp = NULL;
3385 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003386 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003387 int rc = 0;
3388 int resp_buftype;
3389 struct cifs_ses *ses = tcon->ses;
3390 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003391 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003392
3393 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3394 sizeof(struct smb2_fs_full_size_info),
3395 persistent_fid, volatile_fid);
3396 if (rc)
3397 return rc;
3398
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003399 if (encryption_required(tcon))
3400 flags |= CIFS_TRANSFORM_REQ;
3401
3402 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003403 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003404 if (rc) {
3405 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003406 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003407 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003408 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003409
3410 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3411 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3412 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3413 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3414 sizeof(struct smb2_fs_full_size_info));
3415 if (!rc)
3416 copy_fs_info_to_kstatfs(info, fsdata);
3417
Steve French34f62642013-10-09 02:07:00 -05003418qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003419 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003420 return rc;
3421}
3422
3423int
3424SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003425 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003426{
3427 struct smb2_query_info_rsp *rsp = NULL;
3428 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003429 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003430 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003431 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003432 struct cifs_ses *ses = tcon->ses;
3433 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003434 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003435
Steven French21671142013-10-09 13:36:35 -05003436 if (level == FS_DEVICE_INFORMATION) {
3437 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3438 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3439 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3440 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3441 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003442 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3443 max_len = sizeof(struct smb3_fs_ss_info);
3444 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003445 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003446 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003447 return -EINVAL;
3448 }
3449
3450 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003451 persistent_fid, volatile_fid);
3452 if (rc)
3453 return rc;
3454
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003455 if (encryption_required(tcon))
3456 flags |= CIFS_TRANSFORM_REQ;
3457
3458 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003459 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003460 if (rc) {
3461 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3462 goto qfsattr_exit;
3463 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003464 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003465
3466 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3467 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003468 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3469 if (rc)
3470 goto qfsattr_exit;
3471
3472 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003473 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3474 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003475 rsp_len, max_len));
3476 else if (level == FS_DEVICE_INFORMATION)
3477 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3478 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003479 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3480 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3481 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3482 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3483 tcon->perf_sector_size =
3484 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3485 }
Steve French34f62642013-10-09 02:07:00 -05003486
3487qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003488 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003489 return rc;
3490}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003491
3492int
3493smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3494 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3495 const __u32 num_lock, struct smb2_lock_element *buf)
3496{
3497 int rc = 0;
3498 struct smb2_lock_req *req = NULL;
3499 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003500 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003501 int resp_buf_type;
3502 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003503 int flags = CIFS_NO_RESP;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003504
Joe Perchesf96637b2013-05-04 22:12:25 -05003505 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003506
3507 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3508 if (rc)
3509 return rc;
3510
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003511 if (encryption_required(tcon))
3512 flags |= CIFS_TRANSFORM_REQ;
3513
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003514 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003515 req->LockCount = cpu_to_le16(num_lock);
3516
3517 req->PersistentFileId = persist_fid;
3518 req->VolatileFileId = volatile_fid;
3519
3520 count = num_lock * sizeof(struct smb2_lock_element);
3521 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3522
3523 iov[0].iov_base = (char *)req;
3524 /* 4 for rfc1002 length field and count for all locks */
3525 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3526 iov[1].iov_base = (char *)buf;
3527 iov[1].iov_len = count;
3528
3529 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003530 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003531 &rsp_iov);
3532 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003533 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003534 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003535 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3536 }
3537
3538 return rc;
3539}
3540
3541int
3542SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3543 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3544 const __u64 length, const __u64 offset, const __u32 lock_flags,
3545 const bool wait)
3546{
3547 struct smb2_lock_element lock;
3548
3549 lock.Offset = cpu_to_le64(offset);
3550 lock.Length = cpu_to_le64(length);
3551 lock.Flags = cpu_to_le32(lock_flags);
3552 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3553 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3554
3555 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3556}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003557
3558int
3559SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3560 __u8 *lease_key, const __le32 lease_state)
3561{
3562 int rc;
3563 struct smb2_lease_ack *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003564 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003565
Joe Perchesf96637b2013-05-04 22:12:25 -05003566 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003567 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003568 if (rc)
3569 return rc;
3570
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003571 if (encryption_required(tcon))
3572 flags |= CIFS_TRANSFORM_REQ;
3573
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003574 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003575 req->StructureSize = cpu_to_le16(36);
3576 inc_rfc1001_len(req, 12);
3577
3578 memcpy(req->LeaseKey, lease_key, 16);
3579 req->LeaseState = lease_state;
3580
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003581 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003582 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003583
3584 if (rc) {
3585 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003586 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003587 }
3588
3589 return rc;
3590}