blob: 4295cb535c85daa325847dff38b52a0ff9384c52 [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
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100401/* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
402#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500403
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
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100430assemble_neg_contexts(struct smb2_negotiate_req *req,
431 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500432{
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100433 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500434
435 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
436 /* Add 2 to size to round to 8 byte boundary */
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100437
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500438 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);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100442
443 *total_len += 4 + sizeof(struct smb2_preauth_neg_context)
444 + sizeof(struct smb2_encryption_neg_context);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500445}
446#else
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100447static void assemble_neg_contexts(struct smb2_negotiate_req *req,
448 unsigned int *total_len)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500449{
450 return;
451}
452#endif /* SMB311 */
453
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400454/*
455 *
456 * SMB2 Worker functions follow:
457 *
458 * The general structure of the worker functions is:
459 * 1) Call smb2_init (assembles SMB2 header)
460 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
461 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
462 * 4) Decode SMB2 command specific fields in the fixed length area
463 * 5) Decode variable length data area (if any for this SMB2 command type)
464 * 6) Call free smb buffer
465 * 7) return
466 *
467 */
468
469int
470SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
471{
472 struct smb2_negotiate_req *req;
473 struct smb2_negotiate_rsp *rsp;
474 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700475 struct kvec rsp_iov;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400476 int rc = 0;
477 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400478 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400479 int blob_offset, blob_length;
480 char *security_blob;
481 int flags = CIFS_NEG_OP;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100482 unsigned int total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400483
Joe Perchesf96637b2013-05-04 22:12:25 -0500484 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400485
Jeff Layton3534b852013-05-24 07:41:01 -0400486 if (!server) {
487 WARN(1, "%s: server is NULL!\n", __func__);
488 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400489 }
490
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100491 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400492 if (rc)
493 return rc;
494
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100495 req->sync_hdr.SessionId = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400496
Steve French9764c022017-09-17 10:41:35 -0500497 if (strcmp(ses->server->vals->version_string,
498 SMB3ANY_VERSION_STRING) == 0) {
499 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
500 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
501 req->DialectCount = cpu_to_le16(2);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100502 total_len += 4;
Steve French9764c022017-09-17 10:41:35 -0500503 } else if (strcmp(ses->server->vals->version_string,
504 SMBDEFAULT_VERSION_STRING) == 0) {
505 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
506 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
507 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
508 req->DialectCount = cpu_to_le16(3);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100509 total_len += 6;
Steve French9764c022017-09-17 10:41:35 -0500510 } else {
511 /* otherwise send specific dialect */
512 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
513 req->DialectCount = cpu_to_le16(1);
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100514 total_len += 2;
Steve French9764c022017-09-17 10:41:35 -0500515 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400516
517 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400518 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500519 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400520 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500521 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400522 else
523 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400524
Steve Frenche4aa25e2012-10-01 12:26:22 -0500525 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400526
Steve French3c5f9be12014-05-13 13:37:45 -0700527 /* ClientGUID must be zero for SMB2.02 dialect */
528 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
529 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500530 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700531 memcpy(req->ClientGUID, server->client_guid,
532 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500533 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100534 assemble_neg_contexts(req, &total_len);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500535 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400536 iov[0].iov_base = (char *)req;
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100537 iov[0].iov_len = total_len;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400538
Ronnie Sahlberg13cacea2017-11-20 11:24:30 +1100539 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700540 cifs_small_buf_release(req);
541 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400542 /*
543 * No tcon so can't do
544 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
545 */
Steve French7e682f72017-08-31 21:34:24 -0500546 if (rc == -EOPNOTSUPP) {
547 cifs_dbg(VFS, "Dialect not supported by server. Consider "
Steve French9764c022017-09-17 10:41:35 -0500548 "specifying vers=1.0 or vers=2.0 on mount for accessing"
Steve French7e682f72017-08-31 21:34:24 -0500549 " older servers\n");
550 goto neg_exit;
551 } else if (rc != 0)
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400552 goto neg_exit;
553
Steve French9764c022017-09-17 10:41:35 -0500554 if (strcmp(ses->server->vals->version_string,
555 SMB3ANY_VERSION_STRING) == 0) {
556 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
557 cifs_dbg(VFS,
558 "SMB2 dialect returned but not requested\n");
559 return -EIO;
560 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
561 cifs_dbg(VFS,
562 "SMB2.1 dialect returned but not requested\n");
563 return -EIO;
564 }
565 } else if (strcmp(ses->server->vals->version_string,
566 SMBDEFAULT_VERSION_STRING) == 0) {
567 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
568 cifs_dbg(VFS,
569 "SMB2 dialect returned but not requested\n");
570 return -EIO;
571 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
572 /* ops set to 3.0 by default for default so update */
573 ses->server->ops = &smb21_operations;
574 }
Steve French590d08d2017-09-19 11:43:47 -0500575 } else if (le16_to_cpu(rsp->DialectRevision) !=
576 ses->server->vals->protocol_id) {
Steve French9764c022017-09-17 10:41:35 -0500577 /* if requested single dialect ensure returned dialect matched */
578 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
Steve French590d08d2017-09-19 11:43:47 -0500579 le16_to_cpu(rsp->DialectRevision));
Steve French9764c022017-09-17 10:41:35 -0500580 return -EIO;
581 }
582
Joe Perchesf96637b2013-05-04 22:12:25 -0500583 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400584
Steve Frenche4aa25e2012-10-01 12:26:22 -0500585 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500586 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500587 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500588 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500589 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500590 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500591 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
592 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600593#ifdef CONFIG_CIFS_SMB311
594 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
595 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
596#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400597 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500598 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500599 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400600 rc = -EIO;
601 goto neg_exit;
602 }
603 server->dialect = le16_to_cpu(rsp->DialectRevision);
604
Steve French9764c022017-09-17 10:41:35 -0500605 /* BB: add check that dialect was valid given dialect(s) we asked for */
606
Jeff Laytone598d1d82013-05-26 07:00:59 -0400607 /* SMB2 only has an extended negflavor */
608 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400609 /* set it to the maximum buffer size value we can send with 1 credit */
610 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
611 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400612 server->max_read = le32_to_cpu(rsp->MaxReadSize);
613 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
614 /* BB Do we need to validate the SecurityMode? */
615 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
616 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400617 /* Internal types */
618 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400619
620 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
621 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500622 /*
623 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
624 * for us will be
625 * ses->sectype = RawNTLMSSP;
626 * but for time being this is our only auth choice so doesn't matter.
627 * We just found a server which sets blob length to zero expecting raw.
628 */
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700629 if (blob_length == 0) {
Steve French5d875cc2013-06-25 15:33:41 -0500630 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky67dbea22017-04-12 13:32:07 -0700631 server->sec_ntlmssp = true;
632 }
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700633
Jeff Layton38d77c52013-05-26 07:01:00 -0400634 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400635 if (rc)
636 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500637 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500638 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500639 if (rc == 1)
640 rc = 0;
641 else if (rc == 0)
642 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400643 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400644neg_exit:
645 free_rsp_buf(resp_buftype, rsp);
646 return rc;
647}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400648
Steve Frenchff1c0382013-11-19 23:44:46 -0600649int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
650{
651 int rc = 0;
652 struct validate_negotiate_info_req vneg_inbuf;
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200653 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
Steve Frenchff1c0382013-11-19 23:44:46 -0600654 u32 rsplen;
Steve French9764c022017-09-17 10:41:35 -0500655 u32 inbuflen; /* max of 4 dialects */
Steve Frenchff1c0382013-11-19 23:44:46 -0600656
657 cifs_dbg(FYI, "validate negotiate\n");
658
659 /*
660 * validation ioctl must be signed, so no point sending this if we
Steve French0603c962017-09-20 19:57:18 -0500661 * can not sign it (ie are not known user). Even if signing is not
662 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600663 * sign just this, the first and only signed request on a connection.
Steve French0603c962017-09-20 19:57:18 -0500664 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600665 */
Steve French0603c962017-09-20 19:57:18 -0500666 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600667 return 0; /* validation requires signing */
668
Steve French0603c962017-09-20 19:57:18 -0500669 if (tcon->ses->user_name == NULL) {
670 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
671 return 0; /* validation requires signing */
672 }
673
674 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
675 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
676
Steve Frenchff1c0382013-11-19 23:44:46 -0600677 vneg_inbuf.Capabilities =
678 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100679 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
680 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600681
682 if (tcon->ses->sign)
683 vneg_inbuf.SecurityMode =
684 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
685 else if (global_secflags & CIFSSEC_MAY_SIGN)
686 vneg_inbuf.SecurityMode =
687 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
688 else
689 vneg_inbuf.SecurityMode = 0;
690
Steve French9764c022017-09-17 10:41:35 -0500691
692 if (strcmp(tcon->ses->server->vals->version_string,
693 SMB3ANY_VERSION_STRING) == 0) {
694 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
695 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
696 vneg_inbuf.DialectCount = cpu_to_le16(2);
697 /* structure is big enough for 3 dialects, sending only 2 */
698 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
699 } else if (strcmp(tcon->ses->server->vals->version_string,
700 SMBDEFAULT_VERSION_STRING) == 0) {
701 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
702 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
703 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
704 vneg_inbuf.DialectCount = cpu_to_le16(3);
705 /* structure is big enough for 3 dialects */
706 inbuflen = sizeof(struct validate_negotiate_info_req);
707 } else {
708 /* otherwise specific dialect was requested */
709 vneg_inbuf.Dialects[0] =
710 cpu_to_le16(tcon->ses->server->vals->protocol_id);
711 vneg_inbuf.DialectCount = cpu_to_le16(1);
712 /* structure is big enough for 3 dialects, sending only 1 */
713 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
714 }
Steve Frenchff1c0382013-11-19 23:44:46 -0600715
716 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
717 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +0100718 false /* use_ipc */,
Steve Frenchff1c0382013-11-19 23:44:46 -0600719 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
720 (char **)&pneg_rsp, &rsplen);
721
722 if (rc != 0) {
723 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
724 return -EIO;
725 }
726
727 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French7db0a6e2017-05-03 21:12:20 -0500728 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
729 rsplen);
730
731 /* relax check since Mac returns max bufsize allowed on ioctl */
David Disseldorpa2d9daa2017-10-20 14:49:38 +0200732 if ((rsplen > CIFSMaxBufSize)
733 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200734 goto err_rsp_free;
Steve Frenchff1c0382013-11-19 23:44:46 -0600735 }
736
737 /* check validate negotiate info response matches what we got earlier */
738 if (pneg_rsp->Dialect !=
739 cpu_to_le16(tcon->ses->server->vals->protocol_id))
740 goto vneg_out;
741
742 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
743 goto vneg_out;
744
745 /* do not validate server guid because not saved at negprot time yet */
746
747 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
748 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
749 goto vneg_out;
750
751 /* validate negotiate successful */
752 cifs_dbg(FYI, "validate negotiate info successful\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200753 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600754 return 0;
755
756vneg_out:
757 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
David Disseldorpfe83bebc2017-10-20 14:49:37 +0200758err_rsp_free:
759 kfree(pneg_rsp);
Steve Frenchff1c0382013-11-19 23:44:46 -0600760 return -EIO;
761}
762
Sachin Prabhuef65aae2017-01-18 15:35:57 +0530763enum securityEnum
764smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
765{
766 switch (requested) {
767 case Kerberos:
768 case RawNTLMSSP:
769 return requested;
770 case NTLMv2:
771 return RawNTLMSSP;
772 case Unspecified:
773 if (server->sec_ntlmssp &&
774 (global_secflags & CIFSSEC_MAY_NTLMSSP))
775 return RawNTLMSSP;
776 if ((server->sec_kerberos || server->sec_mskerberos) &&
777 (global_secflags & CIFSSEC_MAY_KRB5))
778 return Kerberos;
779 /* Fallthrough */
780 default:
781 return Unspecified;
782 }
783}
784
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100785struct SMB2_sess_data {
786 unsigned int xid;
787 struct cifs_ses *ses;
788 struct nls_table *nls_cp;
789 void (*func)(struct SMB2_sess_data *);
790 int result;
791 u64 previous_session;
792
793 /* we will send the SMB in three pieces:
794 * a fixed length beginning part, an optional
795 * SPNEGO blob (which can be zero length), and a
796 * last part which will include the strings
797 * and rest of bcc area. This allows us to avoid
798 * a large buffer 17K allocation
799 */
800 int buf0_type;
801 struct kvec iov[2];
802};
803
804static int
805SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
806{
807 int rc;
808 struct cifs_ses *ses = sess_data->ses;
809 struct smb2_sess_setup_req *req;
810 struct TCP_Server_Info *server = ses->server;
811
812 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
813 if (rc)
814 return rc;
815
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700816 /* First session, not a reauthenticate */
817 req->hdr.sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100818
819 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
820 req->PreviousSessionId = sess_data->previous_session;
821
822 req->Flags = 0; /* MBZ */
823 /* to enable echos and oplocks */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700824 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100825
826 /* only one of SMB2 signing flags may be set in SMB2 request */
827 if (server->sign)
828 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
829 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
830 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
831 else
832 req->SecurityMode = 0;
833
834 req->Capabilities = 0;
835 req->Channel = 0; /* MBZ */
836
837 sess_data->iov[0].iov_base = (char *)req;
838 /* 4 for rfc1002 length field and 1 for pad */
839 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
840 /*
841 * This variable will be used to clear the buffer
842 * allocated above in case of any error in the calling function.
843 */
844 sess_data->buf0_type = CIFS_SMALL_BUFFER;
845
846 return 0;
847}
848
849static void
850SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
851{
852 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
853 sess_data->buf0_type = CIFS_NO_BUFFER;
854}
855
856static int
857SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
858{
859 int rc;
860 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700861 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100862
863 /* Testing shows that buffer offset must be at location of Buffer[0] */
864 req->SecurityBufferOffset =
865 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
866 1 /* pad */ - 4 /* rfc1001 len */);
867 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
868
869 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
870
871 /* BB add code to build os and lm fields */
872
873 rc = SendReceive2(sess_data->xid, sess_data->ses,
874 sess_data->iov, 2,
875 &sess_data->buf0_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700876 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
877 cifs_small_buf_release(sess_data->iov[0].iov_base);
878 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100879
880 return rc;
881}
882
883static int
884SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
885{
886 int rc = 0;
887 struct cifs_ses *ses = sess_data->ses;
888
889 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800890 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100891 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100892 if (rc) {
893 cifs_dbg(FYI,
894 "SMB3 session key generation failed\n");
895 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800896 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100897 }
898 }
899 if (!ses->server->session_estab) {
900 ses->server->sequence_number = 0x2;
901 ses->server->session_estab = true;
902 }
903 mutex_unlock(&ses->server->srv_mutex);
904
905 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
906 spin_lock(&GlobalMid_Lock);
907 ses->status = CifsGood;
908 ses->need_reconnect = false;
909 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100910 return rc;
911}
912
913#ifdef CONFIG_CIFS_UPCALL
914static void
915SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
916{
917 int rc;
918 struct cifs_ses *ses = sess_data->ses;
919 struct cifs_spnego_msg *msg;
920 struct key *spnego_key = NULL;
921 struct smb2_sess_setup_rsp *rsp = NULL;
922
923 rc = SMB2_sess_alloc_buffer(sess_data);
924 if (rc)
925 goto out;
926
927 spnego_key = cifs_get_spnego_key(ses);
928 if (IS_ERR(spnego_key)) {
929 rc = PTR_ERR(spnego_key);
930 spnego_key = NULL;
931 goto out;
932 }
933
934 msg = spnego_key->payload.data[0];
935 /*
936 * check version field to make sure that cifs.upcall is
937 * sending us a response in an expected form
938 */
939 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
940 cifs_dbg(VFS,
941 "bad cifs.upcall version. Expected %d got %d",
942 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
943 rc = -EKEYREJECTED;
944 goto out_put_spnego_key;
945 }
946
947 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
948 GFP_KERNEL);
949 if (!ses->auth_key.response) {
950 cifs_dbg(VFS,
951 "Kerberos can't allocate (%u bytes) memory",
952 msg->sesskey_len);
953 rc = -ENOMEM;
954 goto out_put_spnego_key;
955 }
956 ses->auth_key.len = msg->sesskey_len;
957
958 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
959 sess_data->iov[1].iov_len = msg->secblob_len;
960
961 rc = SMB2_sess_sendreceive(sess_data);
962 if (rc)
963 goto out_put_spnego_key;
964
965 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700966 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100967
968 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100969
970 rc = SMB2_sess_establish_session(sess_data);
971out_put_spnego_key:
972 key_invalidate(spnego_key);
973 key_put(spnego_key);
974out:
975 sess_data->result = rc;
976 sess_data->func = NULL;
977 SMB2_sess_free_buffer(sess_data);
978}
979#else
980static void
981SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
982{
983 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
984 sess_data->result = -EOPNOTSUPP;
985 sess_data->func = NULL;
986}
987#endif
988
Sachin Prabhu166cea42016-10-07 19:11:22 +0100989static void
990SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
991
992static void
993SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
994{
995 int rc;
996 struct cifs_ses *ses = sess_data->ses;
997 struct smb2_sess_setup_rsp *rsp = NULL;
998 char *ntlmssp_blob = NULL;
999 bool use_spnego = false; /* else use raw ntlmssp */
1000 u16 blob_length = 0;
1001
1002 /*
1003 * If memory allocation is successful, caller of this function
1004 * frees it.
1005 */
1006 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1007 if (!ses->ntlmssp) {
1008 rc = -ENOMEM;
1009 goto out_err;
1010 }
1011 ses->ntlmssp->sesskey_per_smbsess = true;
1012
1013 rc = SMB2_sess_alloc_buffer(sess_data);
1014 if (rc)
1015 goto out_err;
1016
1017 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1018 GFP_KERNEL);
1019 if (ntlmssp_blob == NULL) {
1020 rc = -ENOMEM;
1021 goto out;
1022 }
1023
1024 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1025 if (use_spnego) {
1026 /* BB eventually need to add this */
1027 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1028 rc = -EOPNOTSUPP;
1029 goto out;
1030 } else {
1031 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1032 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1033 }
1034 sess_data->iov[1].iov_base = ntlmssp_blob;
1035 sess_data->iov[1].iov_len = blob_length;
1036
1037 rc = SMB2_sess_sendreceive(sess_data);
1038 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1039
1040 /* If true, rc here is expected and not an error */
1041 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001042 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001043 rc = 0;
1044
1045 if (rc)
1046 goto out;
1047
1048 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1049 le16_to_cpu(rsp->SecurityBufferOffset)) {
1050 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1051 le16_to_cpu(rsp->SecurityBufferOffset));
1052 rc = -EIO;
1053 goto out;
1054 }
1055 rc = decode_ntlmssp_challenge(rsp->Buffer,
1056 le16_to_cpu(rsp->SecurityBufferLength), ses);
1057 if (rc)
1058 goto out;
1059
1060 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1061
1062
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001063 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001064 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001065
1066out:
1067 kfree(ntlmssp_blob);
1068 SMB2_sess_free_buffer(sess_data);
1069 if (!rc) {
1070 sess_data->result = 0;
1071 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1072 return;
1073 }
1074out_err:
1075 kfree(ses->ntlmssp);
1076 ses->ntlmssp = NULL;
1077 sess_data->result = rc;
1078 sess_data->func = NULL;
1079}
1080
1081static void
1082SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1083{
1084 int rc;
1085 struct cifs_ses *ses = sess_data->ses;
1086 struct smb2_sess_setup_req *req;
1087 struct smb2_sess_setup_rsp *rsp = NULL;
1088 unsigned char *ntlmssp_blob = NULL;
1089 bool use_spnego = false; /* else use raw ntlmssp */
1090 u16 blob_length = 0;
1091
1092 rc = SMB2_sess_alloc_buffer(sess_data);
1093 if (rc)
1094 goto out;
1095
1096 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001097 req->hdr.sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001098
1099 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1100 sess_data->nls_cp);
1101 if (rc) {
1102 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1103 goto out;
1104 }
1105
1106 if (use_spnego) {
1107 /* BB eventually need to add this */
1108 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1109 rc = -EOPNOTSUPP;
1110 goto out;
1111 }
1112 sess_data->iov[1].iov_base = ntlmssp_blob;
1113 sess_data->iov[1].iov_len = blob_length;
1114
1115 rc = SMB2_sess_sendreceive(sess_data);
1116 if (rc)
1117 goto out;
1118
1119 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1120
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001121 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001122 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001123
1124 rc = SMB2_sess_establish_session(sess_data);
1125out:
1126 kfree(ntlmssp_blob);
1127 SMB2_sess_free_buffer(sess_data);
1128 kfree(ses->ntlmssp);
1129 ses->ntlmssp = NULL;
1130 sess_data->result = rc;
1131 sess_data->func = NULL;
1132}
1133
1134static int
1135SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1136{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301137 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001138
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301139 type = smb2_select_sectype(ses->server, ses->sectype);
1140 cifs_dbg(FYI, "sess setup type %d\n", type);
1141 if (type == Unspecified) {
1142 cifs_dbg(VFS,
1143 "Unable to select appropriate authentication method!");
1144 return -EINVAL;
1145 }
1146
1147 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001148 case Kerberos:
1149 sess_data->func = SMB2_auth_kerberos;
1150 break;
1151 case RawNTLMSSP:
1152 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1153 break;
1154 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301155 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001156 return -EOPNOTSUPP;
1157 }
1158
1159 return 0;
1160}
1161
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001162int
1163SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1164 const struct nls_table *nls_cp)
1165{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001166 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001167 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001168 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001169
Joe Perchesf96637b2013-05-04 22:12:25 -05001170 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001171
Jeff Layton3534b852013-05-24 07:41:01 -04001172 if (!server) {
1173 WARN(1, "%s: server is NULL!\n", __func__);
1174 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001175 }
1176
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001177 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1178 if (!sess_data)
1179 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001180
1181 rc = SMB2_select_sec(ses, sess_data);
1182 if (rc)
1183 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001184 sess_data->xid = xid;
1185 sess_data->ses = ses;
1186 sess_data->buf0_type = CIFS_NO_BUFFER;
1187 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001188
Sachin Prabhu166cea42016-10-07 19:11:22 +01001189 while (sess_data->func)
1190 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001191
Steve Frenchc721c382017-09-19 18:40:03 -05001192 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1193 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001194 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001195out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001196 kfree(sess_data);
1197 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001198}
1199
1200int
1201SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1202{
1203 struct smb2_logoff_req *req; /* response is also trivial struct */
1204 int rc = 0;
1205 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001206 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001207 unsigned int total_len;
1208 struct kvec iov[1];
1209 struct kvec rsp_iov;
1210 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001211
Joe Perchesf96637b2013-05-04 22:12:25 -05001212 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001213
1214 if (ses && (ses->server))
1215 server = ses->server;
1216 else
1217 return -EIO;
1218
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001219 /* no need to send SMB logoff if uid already closed due to reconnect */
1220 if (ses->need_reconnect)
1221 goto smb2_session_already_dead;
1222
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001223 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001224 if (rc)
1225 return rc;
1226
1227 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001228 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001229
1230 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1231 flags |= CIFS_TRANSFORM_REQ;
1232 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001233 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001234
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001235 flags |= CIFS_NO_RESP;
1236
1237 iov[0].iov_base = (char *)req;
1238 iov[0].iov_len = total_len;
1239
1240 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001241 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001242 /*
1243 * No tcon so can't do
1244 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1245 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001246
1247smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001248 return rc;
1249}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001250
1251static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1252{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001253 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001254}
1255
1256#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1257
Steve Frenchde9f68df2013-11-15 11:26:24 -06001258/* These are similar values to what Windows uses */
1259static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1260{
1261 tcon->max_chunks = 256;
1262 tcon->max_bytes_chunk = 1048576;
1263 tcon->max_bytes_copy = 16777216;
1264}
1265
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001266int
1267SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1268 struct cifs_tcon *tcon, const struct nls_table *cp)
1269{
1270 struct smb2_tree_connect_req *req;
1271 struct smb2_tree_connect_rsp *rsp = NULL;
1272 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001273 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001274 int rc = 0;
1275 int resp_buftype;
1276 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001277 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001278 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001279
Joe Perchesf96637b2013-05-04 22:12:25 -05001280 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001281
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001282 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001283 return -EIO;
1284
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001285 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1286 if (unc_path == NULL)
1287 return -ENOMEM;
1288
1289 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1290 unc_path_len *= 2;
1291 if (unc_path_len < 2) {
1292 kfree(unc_path);
1293 return -EINVAL;
1294 }
1295
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001296 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1297 if (tcon)
1298 tcon->tid = 0;
1299
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001300 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1301 if (rc) {
1302 kfree(unc_path);
1303 return rc;
1304 }
1305
1306 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001307 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1308 flags |= CIFS_TRANSFORM_REQ;
1309
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001310 /* since no tcon, smb2_init can not do this, so do here */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001311 req->hdr.sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001312 if (ses->server->sign)
1313 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001314 } else if (encryption_required(tcon))
1315 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001316
1317 iov[0].iov_base = (char *)req;
1318 /* 4 for rfc1002 length field and 1 for pad */
1319 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1320
1321 /* Testing shows that buffer offset must be at location of Buffer[0] */
1322 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1323 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1324 req->PathLength = cpu_to_le16(unc_path_len - 2);
1325 iov[1].iov_base = unc_path;
1326 iov[1].iov_len = unc_path_len;
1327
1328 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1329
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001330 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001331 cifs_small_buf_release(req);
1332 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001333
1334 if (rc != 0) {
1335 if (tcon) {
1336 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1337 tcon->need_reconnect = true;
1338 }
1339 goto tcon_error_exit;
1340 }
1341
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001342 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001343 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001344 goto tcon_exit;
1345 }
1346
Christophe JAILLETcd123002017-05-12 17:59:32 +02001347 switch (rsp->ShareType) {
1348 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001349 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001350 break;
1351 case SMB2_SHARE_TYPE_PIPE:
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001352 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001353 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001354 break;
1355 case SMB2_SHARE_TYPE_PRINT:
1356 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001357 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001358 break;
1359 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001360 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001361 rc = -EOPNOTSUPP;
1362 goto tcon_error_exit;
1363 }
1364
1365 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001366 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001367 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1368 tcon->tidStatus = CifsGood;
1369 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001370 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001371 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001372
1373 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1374 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001375 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001376
1377 if (tcon->seal &&
1378 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1379 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1380
Steve Frenchde9f68df2013-11-15 11:26:24 -06001381 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001382 if (tcon->ses->server->ops->validate_negotiate)
1383 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001384tcon_exit:
1385 free_rsp_buf(resp_buftype, rsp);
1386 kfree(unc_path);
1387 return rc;
1388
1389tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001390 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001391 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001392 }
1393 goto tcon_exit;
1394}
1395
1396int
1397SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1398{
1399 struct smb2_tree_disconnect_req *req; /* response is trivial */
1400 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001401 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001402 int flags = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001403
Joe Perchesf96637b2013-05-04 22:12:25 -05001404 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001405
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001406 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001407 return -EIO;
1408
1409 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1410 return 0;
1411
1412 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1413 if (rc)
1414 return rc;
1415
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001416 if (encryption_required(tcon))
1417 flags |= CIFS_TRANSFORM_REQ;
1418
1419 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001420 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001421 if (rc)
1422 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1423
1424 return rc;
1425}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001426
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001427
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001428static struct create_durable *
1429create_durable_buf(void)
1430{
1431 struct create_durable *buf;
1432
1433 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1434 if (!buf)
1435 return NULL;
1436
1437 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001438 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001439 buf->ccontext.DataLength = cpu_to_le32(16);
1440 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1441 (struct create_durable, Name));
1442 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001443 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001444 buf->Name[0] = 'D';
1445 buf->Name[1] = 'H';
1446 buf->Name[2] = 'n';
1447 buf->Name[3] = 'Q';
1448 return buf;
1449}
1450
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001451static struct create_durable *
1452create_reconnect_durable_buf(struct cifs_fid *fid)
1453{
1454 struct create_durable *buf;
1455
1456 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1457 if (!buf)
1458 return NULL;
1459
1460 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1461 (struct create_durable, Data));
1462 buf->ccontext.DataLength = cpu_to_le32(16);
1463 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1464 (struct create_durable, Name));
1465 buf->ccontext.NameLength = cpu_to_le16(4);
1466 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1467 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001468 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001469 buf->Name[0] = 'D';
1470 buf->Name[1] = 'H';
1471 buf->Name[2] = 'n';
1472 buf->Name[3] = 'C';
1473 return buf;
1474}
1475
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001476static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001477parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1478 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001479{
1480 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001481 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001482 unsigned int next;
1483 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001484 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001485
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001486 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001487 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001488 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001489 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001490 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001491 if (le16_to_cpu(cc->NameLength) == 4 &&
1492 strncmp(name, "RqLs", 4) == 0)
1493 return server->ops->parse_lease_buf(cc, epoch);
1494
1495 next = le32_to_cpu(cc->Next);
1496 if (!next)
1497 break;
1498 remaining -= next;
1499 cc = (struct create_context *)((char *)cc + next);
1500 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001501
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001502 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001503}
1504
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001505static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001506add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1507 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001508{
1509 struct smb2_create_req *req = iov[0].iov_base;
1510 unsigned int num = *num_iovec;
1511
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001512 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001513 if (iov[num].iov_base == NULL)
1514 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001515 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001516 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1517 if (!req->CreateContextsOffset)
1518 req->CreateContextsOffset = cpu_to_le32(
1519 sizeof(struct smb2_create_req) - 4 +
1520 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001521 le32_add_cpu(&req->CreateContextsLength,
1522 server->vals->create_lease_size);
1523 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001524 *num_iovec = num + 1;
1525 return 0;
1526}
1527
Steve Frenchb56eae42015-11-03 09:26:27 -06001528static struct create_durable_v2 *
1529create_durable_v2_buf(struct cifs_fid *pfid)
1530{
1531 struct create_durable_v2 *buf;
1532
1533 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1534 if (!buf)
1535 return NULL;
1536
1537 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1538 (struct create_durable_v2, dcontext));
1539 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1540 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1541 (struct create_durable_v2, Name));
1542 buf->ccontext.NameLength = cpu_to_le16(4);
1543
1544 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1545 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001546 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001547 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1548
1549 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1550 buf->Name[0] = 'D';
1551 buf->Name[1] = 'H';
1552 buf->Name[2] = '2';
1553 buf->Name[3] = 'Q';
1554 return buf;
1555}
1556
1557static struct create_durable_handle_reconnect_v2 *
1558create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1559{
1560 struct create_durable_handle_reconnect_v2 *buf;
1561
1562 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1563 GFP_KERNEL);
1564 if (!buf)
1565 return NULL;
1566
1567 buf->ccontext.DataOffset =
1568 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1569 dcontext));
1570 buf->ccontext.DataLength =
1571 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1572 buf->ccontext.NameOffset =
1573 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1574 Name));
1575 buf->ccontext.NameLength = cpu_to_le16(4);
1576
1577 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1578 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1579 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1580 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1581
1582 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1583 buf->Name[0] = 'D';
1584 buf->Name[1] = 'H';
1585 buf->Name[2] = '2';
1586 buf->Name[3] = 'C';
1587 return buf;
1588}
1589
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001590static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001591add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001592 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001593{
1594 struct smb2_create_req *req = iov[0].iov_base;
1595 unsigned int num = *num_iovec;
1596
Steve Frenchb56eae42015-11-03 09:26:27 -06001597 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1598 if (iov[num].iov_base == NULL)
1599 return -ENOMEM;
1600 iov[num].iov_len = sizeof(struct create_durable_v2);
1601 if (!req->CreateContextsOffset)
1602 req->CreateContextsOffset =
1603 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1604 iov[1].iov_len);
1605 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1606 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1607 *num_iovec = num + 1;
1608 return 0;
1609}
1610
1611static int
1612add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1613 struct cifs_open_parms *oparms)
1614{
1615 struct smb2_create_req *req = iov[0].iov_base;
1616 unsigned int num = *num_iovec;
1617
1618 /* indicate that we don't need to relock the file */
1619 oparms->reconnect = false;
1620
1621 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1622 if (iov[num].iov_base == NULL)
1623 return -ENOMEM;
1624 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1625 if (!req->CreateContextsOffset)
1626 req->CreateContextsOffset =
1627 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1628 iov[1].iov_len);
1629 le32_add_cpu(&req->CreateContextsLength,
1630 sizeof(struct create_durable_handle_reconnect_v2));
1631 inc_rfc1001_len(&req->hdr,
1632 sizeof(struct create_durable_handle_reconnect_v2));
1633 *num_iovec = num + 1;
1634 return 0;
1635}
1636
1637static int
1638add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1639 struct cifs_open_parms *oparms, bool use_persistent)
1640{
1641 struct smb2_create_req *req = iov[0].iov_base;
1642 unsigned int num = *num_iovec;
1643
1644 if (use_persistent) {
1645 if (oparms->reconnect)
1646 return add_durable_reconnect_v2_context(iov, num_iovec,
1647 oparms);
1648 else
1649 return add_durable_v2_context(iov, num_iovec, oparms);
1650 }
1651
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001652 if (oparms->reconnect) {
1653 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1654 /* indicate that we don't need to relock the file */
1655 oparms->reconnect = false;
1656 } else
1657 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001658 if (iov[num].iov_base == NULL)
1659 return -ENOMEM;
1660 iov[num].iov_len = sizeof(struct create_durable);
1661 if (!req->CreateContextsOffset)
1662 req->CreateContextsOffset =
1663 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1664 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001665 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001666 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1667 *num_iovec = num + 1;
1668 return 0;
1669}
1670
Aurelien Aptelf0712922017-02-22 14:47:17 +01001671static int
1672alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1673 const char *treename, const __le16 *path)
1674{
1675 int treename_len, path_len;
1676 struct nls_table *cp;
1677 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1678
1679 /*
1680 * skip leading "\\"
1681 */
1682 treename_len = strlen(treename);
1683 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1684 return -EINVAL;
1685
1686 treename += 2;
1687 treename_len -= 2;
1688
1689 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1690
1691 /*
1692 * make room for one path separator between the treename and
1693 * path
1694 */
1695 *out_len = treename_len + 1 + path_len;
1696
1697 /*
1698 * final path needs to be null-terminated UTF16 with a
1699 * size aligned to 8
1700 */
1701
1702 *out_size = roundup((*out_len+1)*2, 8);
1703 *out_path = kzalloc(*out_size, GFP_KERNEL);
1704 if (!*out_path)
1705 return -ENOMEM;
1706
1707 cp = load_nls_default();
1708 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1709 UniStrcat(*out_path, sep);
1710 UniStrcat(*out_path, path);
1711 unload_nls(cp);
1712
1713 return 0;
1714}
1715
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001716int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001717SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001718 __u8 *oplock, struct smb2_file_all_info *buf,
1719 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001720{
1721 struct smb2_create_req *req;
1722 struct smb2_create_rsp *rsp;
1723 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001724 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001725 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001726 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001727 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001728 int resp_buftype;
1729 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001730 __le16 *copy_path = NULL;
1731 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001732 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001733 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001734 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001735 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001736 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001737
Joe Perchesf96637b2013-05-04 22:12:25 -05001738 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001739
1740 if (ses && (ses->server))
1741 server = ses->server;
1742 else
1743 return -EIO;
1744
1745 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1746 if (rc)
1747 return rc;
1748
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001749 if (encryption_required(tcon))
1750 flags |= CIFS_TRANSFORM_REQ;
1751
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001752 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001753 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001754 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1755 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001756
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001757 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001758 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001759 /* File attributes ignored on open (used in create though) */
1760 req->FileAttributes = cpu_to_le32(file_attributes);
1761 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001762 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1763 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001764
1765 iov[0].iov_base = (char *)req;
1766 /* 4 for rfc1002 length field */
1767 iov[0].iov_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001768 /* -1 since last byte is buf[0] which is sent below (path) */
1769 iov[0].iov_len--;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001770
Aurelien Aptelf0712922017-02-22 14:47:17 +01001771 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1772
1773 /* [MS-SMB2] 2.2.13 NameOffset:
1774 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1775 * the SMB2 header, the file name includes a prefix that will
1776 * be processed during DFS name normalization as specified in
1777 * section 3.3.5.9. Otherwise, the file name is relative to
1778 * the share that is identified by the TreeId in the SMB2
1779 * header.
1780 */
1781 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1782 int name_len;
1783
1784 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1785 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1786 &name_len,
1787 tcon->treeName, path);
1788 if (rc)
1789 return rc;
1790 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001791 uni_path_len = copy_size;
1792 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001793 } else {
1794 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1795 /* MUST set path len (NameLength) to 0 opening root of share */
1796 req->NameLength = cpu_to_le16(uni_path_len - 2);
1797 if (uni_path_len % 8 != 0) {
1798 copy_size = roundup(uni_path_len, 8);
1799 copy_path = kzalloc(copy_size, GFP_KERNEL);
1800 if (!copy_path)
1801 return -ENOMEM;
1802 memcpy((char *)copy_path, (const char *)path,
1803 uni_path_len);
1804 uni_path_len = copy_size;
1805 path = copy_path;
1806 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001807 }
1808
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001809 iov[1].iov_len = uni_path_len;
1810 iov[1].iov_base = path;
1811 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1812 inc_rfc1001_len(req, uni_path_len - 1);
1813
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001814 if (!server->oplocks)
1815 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1816
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001817 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001818 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1819 req->RequestedOplockLevel = *oplock;
1820 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001821 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001822 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001823 cifs_small_buf_release(req);
1824 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001825 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001826 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001827 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001828 }
1829
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001830 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1831 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001832 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001833 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001834 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001835 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001836 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001837 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001838
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001839 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001840 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001841 if (rc) {
1842 cifs_small_buf_release(req);
1843 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001844 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001845 return rc;
1846 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001847 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001848 }
1849
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001850 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001851 cifs_small_buf_release(req);
1852 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001853
1854 if (rc != 0) {
1855 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001856 if (err_buf && rsp)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001857 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1858 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001859 goto creat_exit;
1860 }
1861
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001862 oparms->fid->persistent_fid = rsp->PersistentFileId;
1863 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001864
1865 if (buf) {
1866 memcpy(buf, &rsp->CreationTime, 32);
1867 buf->AllocationSize = rsp->AllocationSize;
1868 buf->EndOfFile = rsp->EndofFile;
1869 buf->Attributes = rsp->FileAttributes;
1870 buf->NumberOfLinks = cpu_to_le32(1);
1871 buf->DeletePending = 0;
1872 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001873
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001874 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001875 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001876 else
1877 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001878creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001879 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001880 kfree(lc_buf);
1881 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001882 free_rsp_buf(resp_buftype, rsp);
1883 return rc;
1884}
1885
Steve French4a72daf2013-06-25 00:20:49 -05001886/*
1887 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1888 */
1889int
1890SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001891 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1892 char *in_data, u32 indatalen,
1893 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001894{
1895 struct smb2_ioctl_req *req;
1896 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001897 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001898 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001899 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001900 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001901 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001902 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001903 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001904 int flags = 0;
Steve French4a72daf2013-06-25 00:20:49 -05001905
1906 cifs_dbg(FYI, "SMB2 IOCTL\n");
1907
Steve French3d1a3742014-08-11 21:05:25 -05001908 if (out_data != NULL)
1909 *out_data = NULL;
1910
Steve French4a72daf2013-06-25 00:20:49 -05001911 /* zero out returned data len, in case of error */
1912 if (plen)
1913 *plen = 0;
1914
Steve French8e353102015-03-26 19:47:02 -05001915 if (tcon)
1916 ses = tcon->ses;
1917 else
1918 return -EIO;
1919
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001920 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05001921 return -EIO;
1922
1923 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1924 if (rc)
1925 return rc;
1926
Aurelien Aptel51146622017-02-28 15:08:41 +01001927 if (use_ipc) {
1928 if (ses->ipc_tid == 0) {
1929 cifs_small_buf_release(req);
1930 return -ENOTCONN;
1931 }
1932
1933 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1934 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1935 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1936 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001937 if (encryption_required(tcon))
1938 flags |= CIFS_TRANSFORM_REQ;
1939
Steve French4a72daf2013-06-25 00:20:49 -05001940 req->CtlCode = cpu_to_le32(opcode);
1941 req->PersistentFileId = persistent_fid;
1942 req->VolatileFileId = volatile_fid;
1943
1944 if (indatalen) {
1945 req->InputCount = cpu_to_le32(indatalen);
1946 /* do not set InputOffset if no input data */
1947 req->InputOffset =
1948 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1949 iov[1].iov_base = in_data;
1950 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001951 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001952 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001953 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001954
1955 req->OutputOffset = 0;
1956 req->OutputCount = 0; /* MBZ */
1957
1958 /*
1959 * Could increase MaxOutputResponse, but that would require more
1960 * than one credit. Windows typically sets this smaller, but for some
1961 * ioctls it may be useful to allow server to send more. No point
1962 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001963 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1964 * (by default, note that it can be overridden to make max larger)
1965 * in responses (except for read responses which can be bigger.
1966 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001967 */
Steve French7db0a6e2017-05-03 21:12:20 -05001968 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001969
1970 if (is_fsctl)
1971 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1972 else
1973 req->Flags = 0;
1974
1975 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001976
Steve French7ff8d452013-10-14 00:44:19 -05001977 /*
1978 * If no input data, the size of ioctl struct in
1979 * protocol spec still includes a 1 byte data buffer,
1980 * but if input data passed to ioctl, we do not
1981 * want to double count this, so we do not send
1982 * the dummy one byte of data in iovec[0] if sending
1983 * input data (in iovec[1]). We also must add 4 bytes
1984 * in first iovec to allow for rfc1002 length field.
1985 */
1986
1987 if (indatalen) {
1988 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1989 inc_rfc1001_len(req, indatalen - 1);
1990 } else
1991 iov[0].iov_len = get_rfc1002_length(req) + 4;
1992
Steve French4587eee2017-10-25 15:58:31 -05001993 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1994 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1995 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05001996
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001997 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001998 cifs_small_buf_release(req);
1999 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002000
Steve French9bf0c9c2013-11-16 18:05:28 -06002001 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002002 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002003 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002004 } else if (rc == -EINVAL) {
2005 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2006 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002007 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002008 goto ioctl_exit;
2009 }
Steve French4a72daf2013-06-25 00:20:49 -05002010 }
2011
2012 /* check if caller wants to look at return data or just return rc */
2013 if ((plen == NULL) || (out_data == NULL))
2014 goto ioctl_exit;
2015
2016 *plen = le32_to_cpu(rsp->OutputCount);
2017
2018 /* We check for obvious errors in the output buffer length and offset */
2019 if (*plen == 0)
2020 goto ioctl_exit; /* server returned no data */
2021 else if (*plen > 0xFF00) {
2022 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2023 *plen = 0;
2024 rc = -EIO;
2025 goto ioctl_exit;
2026 }
2027
2028 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2029 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2030 le32_to_cpu(rsp->OutputOffset));
2031 *plen = 0;
2032 rc = -EIO;
2033 goto ioctl_exit;
2034 }
2035
2036 *out_data = kmalloc(*plen, GFP_KERNEL);
2037 if (*out_data == NULL) {
2038 rc = -ENOMEM;
2039 goto ioctl_exit;
2040 }
2041
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002042 shdr = get_sync_hdr(rsp);
2043 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002044ioctl_exit:
2045 free_rsp_buf(resp_buftype, rsp);
2046 return rc;
2047}
2048
Steve French64a5cfa2013-10-14 15:31:32 -05002049/*
2050 * Individual callers to ioctl worker function follow
2051 */
2052
2053int
2054SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2055 u64 persistent_fid, u64 volatile_fid)
2056{
2057 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002058 struct compress_ioctl fsctl_input;
2059 char *ret_data = NULL;
2060
2061 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002062 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002063
2064 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2065 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002066 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05002067 (char *)&fsctl_input /* data input */,
2068 2 /* in data len */, &ret_data /* out data */, NULL);
2069
2070 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002071
2072 return rc;
2073}
2074
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002075int
2076SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2077 u64 persistent_fid, u64 volatile_fid)
2078{
2079 struct smb2_close_req *req;
2080 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002081 struct cifs_ses *ses = tcon->ses;
2082 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002083 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002084 int resp_buftype;
2085 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002086 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002087
Joe Perchesf96637b2013-05-04 22:12:25 -05002088 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002089
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002090 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002091 return -EIO;
2092
2093 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
2094 if (rc)
2095 return rc;
2096
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002097 if (encryption_required(tcon))
2098 flags |= CIFS_TRANSFORM_REQ;
2099
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002100 req->PersistentFileId = persistent_fid;
2101 req->VolatileFileId = volatile_fid;
2102
2103 iov[0].iov_base = (char *)req;
2104 /* 4 for rfc1002 length field */
2105 iov[0].iov_len = get_rfc1002_length(req) + 4;
2106
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002107 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002108 cifs_small_buf_release(req);
2109 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002110
2111 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002112 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002113 goto close_exit;
2114 }
2115
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002116 /* BB FIXME - decode close response, update inode for caching */
2117
2118close_exit:
2119 free_rsp_buf(resp_buftype, rsp);
2120 return rc;
2121}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002122
2123static int
2124validate_buf(unsigned int offset, unsigned int buffer_length,
2125 struct smb2_hdr *hdr, unsigned int min_buf_size)
2126
2127{
2128 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2129 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2130 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2131 char *end_of_buf = begin_of_buf + buffer_length;
2132
2133
2134 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002135 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2136 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002137 return -EINVAL;
2138 }
2139
2140 /* check if beyond RFC1001 maximum length */
2141 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002142 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2143 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002144 return -EINVAL;
2145 }
2146
2147 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002148 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002149 return -EINVAL;
2150 }
2151
2152 return 0;
2153}
2154
2155/*
2156 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2157 * Caller must free buffer.
2158 */
2159static int
2160validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2161 struct smb2_hdr *hdr, unsigned int minbufsize,
2162 char *data)
2163
2164{
2165 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2166 int rc;
2167
2168 if (!data)
2169 return -EINVAL;
2170
2171 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2172 if (rc)
2173 return rc;
2174
2175 memcpy(data, begin_of_buf, buffer_length);
2176
2177 return 0;
2178}
2179
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002180static int
2181query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002182 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2183 u32 additional_info, size_t output_len, size_t min_len, void **data,
2184 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002185{
2186 struct smb2_query_info_req *req;
2187 struct smb2_query_info_rsp *rsp = NULL;
2188 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002189 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002190 int rc = 0;
2191 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002192 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002193 int flags = 0;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002194
Joe Perchesf96637b2013-05-04 22:12:25 -05002195 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002196
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002197 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002198 return -EIO;
2199
2200 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2201 if (rc)
2202 return rc;
2203
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002204 if (encryption_required(tcon))
2205 flags |= CIFS_TRANSFORM_REQ;
2206
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002207 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002208 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002209 req->PersistentFileId = persistent_fid;
2210 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002211 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002212
2213 /*
2214 * We do not use the input buffer (do not send extra byte)
2215 */
2216 req->InputBufferOffset = 0;
2217 inc_rfc1001_len(req, -1);
2218
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002219 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002220
2221 iov[0].iov_base = (char *)req;
2222 /* 4 for rfc1002 length field */
2223 iov[0].iov_len = get_rfc1002_length(req) + 4;
2224
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002225 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002226 cifs_small_buf_release(req);
2227 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002228
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002229 if (rc) {
2230 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2231 goto qinf_exit;
2232 }
2233
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002234 if (dlen) {
2235 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2236 if (!*data) {
2237 *data = kmalloc(*dlen, GFP_KERNEL);
2238 if (!*data) {
2239 cifs_dbg(VFS,
2240 "Error %d allocating memory for acl\n",
2241 rc);
2242 *dlen = 0;
2243 goto qinf_exit;
2244 }
2245 }
2246 }
2247
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002248 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2249 le32_to_cpu(rsp->OutputBufferLength),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002250 &rsp->hdr, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002251
2252qinf_exit:
2253 free_rsp_buf(resp_buftype, rsp);
2254 return rc;
2255}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002256
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002257int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002258 u64 persistent_fid, u64 volatile_fid,
2259 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002260{
2261 return query_info(xid, tcon, persistent_fid, volatile_fid,
2262 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002263 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002264 sizeof(struct smb2_file_full_ea_info),
2265 (void **)&data,
2266 NULL);
2267}
2268
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002269int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2270 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002271{
2272 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002273 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002274 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002275 sizeof(struct smb2_file_all_info), (void **)&data,
2276 NULL);
2277}
2278
2279int
2280SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2281 u64 persistent_fid, u64 volatile_fid,
2282 void **data, u32 *plen)
2283{
2284 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2285 *plen = 0;
2286
2287 return query_info(xid, tcon, persistent_fid, volatile_fid,
2288 0, SMB2_O_INFO_SECURITY, additional_info,
2289 SMB2_MAX_BUFFER_SIZE,
2290 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002291}
2292
2293int
2294SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2295 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2296{
2297 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002298 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002299 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002300 sizeof(struct smb2_file_internal_info),
2301 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002302}
2303
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002304/*
2305 * This is a no-op for now. We're not really interested in the reply, but
2306 * rather in the fact that the server sent one and that server->lstrp
2307 * gets updated.
2308 *
2309 * FIXME: maybe we should consider checking that the reply matches request?
2310 */
2311static void
2312smb2_echo_callback(struct mid_q_entry *mid)
2313{
2314 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002315 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002316 unsigned int credits_received = 1;
2317
2318 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002319 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002320
2321 DeleteMidQEntry(mid);
2322 add_credits(server, credits_received, CIFS_ECHO_OP);
2323}
2324
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002325void smb2_reconnect_server(struct work_struct *work)
2326{
2327 struct TCP_Server_Info *server = container_of(work,
2328 struct TCP_Server_Info, reconnect.work);
2329 struct cifs_ses *ses;
2330 struct cifs_tcon *tcon, *tcon2;
2331 struct list_head tmp_list;
2332 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002333 int rc;
2334 int resched = false;
2335
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002336
2337 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2338 mutex_lock(&server->reconnect_mutex);
2339
2340 INIT_LIST_HEAD(&tmp_list);
2341 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2342
2343 spin_lock(&cifs_tcp_ses_lock);
2344 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2345 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002346 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002347 tcon->tc_count++;
2348 list_add_tail(&tcon->rlist, &tmp_list);
2349 tcon_exist = true;
2350 }
2351 }
2352 }
2353 /*
2354 * Get the reference to server struct to be sure that the last call of
2355 * cifs_put_tcon() in the loop below won't release the server pointer.
2356 */
2357 if (tcon_exist)
2358 server->srv_count++;
2359
2360 spin_unlock(&cifs_tcp_ses_lock);
2361
2362 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002363 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2364 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002365 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002366 else
2367 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002368 list_del_init(&tcon->rlist);
2369 cifs_put_tcon(tcon);
2370 }
2371
2372 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002373 if (resched)
2374 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002375 mutex_unlock(&server->reconnect_mutex);
2376
2377 /* now we can safely release srv struct */
2378 if (tcon_exist)
2379 cifs_put_tcp_session(server, 1);
2380}
2381
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002382int
2383SMB2_echo(struct TCP_Server_Info *server)
2384{
2385 struct smb2_echo_req *req;
2386 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002387 struct kvec iov[2];
2388 struct smb_rqst rqst = { .rq_iov = iov,
2389 .rq_nvec = 2 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002390
Joe Perchesf96637b2013-05-04 22:12:25 -05002391 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002392
Steve French4fcd1812016-06-22 20:12:05 -05002393 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002394 /* No need to send echo on newly established connections */
2395 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2396 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002397 }
2398
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002399 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2400 if (rc)
2401 return rc;
2402
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002403 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002404
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002405 /* 4 for rfc1002 length field */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002406 iov[0].iov_len = 4;
2407 iov[0].iov_base = (char *)req;
2408 iov[1].iov_len = get_rfc1002_length(req);
2409 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002410
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002411 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2412 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002413 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002414 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002415
2416 cifs_small_buf_release(req);
2417 return rc;
2418}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002419
2420int
2421SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2422 u64 volatile_fid)
2423{
2424 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002425 struct cifs_ses *ses = tcon->ses;
2426 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002427 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002428 int resp_buftype;
2429 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002430 int flags = 0;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002431
Joe Perchesf96637b2013-05-04 22:12:25 -05002432 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002433
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002434 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002435 return -EIO;
2436
2437 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2438 if (rc)
2439 return rc;
2440
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002441 if (encryption_required(tcon))
2442 flags |= CIFS_TRANSFORM_REQ;
2443
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002444 req->PersistentFileId = persistent_fid;
2445 req->VolatileFileId = volatile_fid;
2446
2447 iov[0].iov_base = (char *)req;
2448 /* 4 for rfc1002 length field */
2449 iov[0].iov_len = get_rfc1002_length(req) + 4;
2450
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002451 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002452 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002453
Steve Frenchdfebe402015-03-27 01:00:06 -05002454 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002455 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2456
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002457 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002458 return rc;
2459}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002460
2461/*
2462 * To form a chain of read requests, any read requests after the first should
2463 * have the end_of_chain boolean set to true.
2464 */
2465static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002466smb2_new_read_req(void **buf, unsigned int *total_len,
2467 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2468 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002469{
2470 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002471 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002472 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002473
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002474 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2475 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002476 if (rc)
2477 return rc;
2478 if (io_parms->tcon->ses->server == NULL)
2479 return -ECONNABORTED;
2480
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002481 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002482 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002483
2484 req->PersistentFileId = io_parms->persistent_fid;
2485 req->VolatileFileId = io_parms->volatile_fid;
2486 req->ReadChannelInfoOffset = 0; /* reserved */
2487 req->ReadChannelInfoLength = 0; /* reserved */
2488 req->Channel = 0; /* reserved */
2489 req->MinimumCount = 0;
2490 req->Length = cpu_to_le32(io_parms->length);
2491 req->Offset = cpu_to_le64(io_parms->offset);
2492
2493 if (request_type & CHAINED_REQUEST) {
2494 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002495 /* next 8-byte aligned request */
2496 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2497 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002498 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002499 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002500 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002501 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002502 /*
2503 * Related requests use info from previous read request
2504 * in chain.
2505 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002506 shdr->SessionId = 0xFFFFFFFF;
2507 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002508 req->PersistentFileId = 0xFFFFFFFF;
2509 req->VolatileFileId = 0xFFFFFFFF;
2510 }
2511 }
2512 if (remaining_bytes > io_parms->length)
2513 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2514 else
2515 req->RemainingBytes = 0;
2516
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002517 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002518 return rc;
2519}
2520
2521static void
2522smb2_readv_callback(struct mid_q_entry *mid)
2523{
2524 struct cifs_readdata *rdata = mid->callback_data;
2525 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2526 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002527 struct smb2_sync_hdr *shdr =
2528 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002529 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002530 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2531 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002532 .rq_pages = rdata->pages,
2533 .rq_npages = rdata->nr_pages,
2534 .rq_pagesz = rdata->pagesz,
2535 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002536
Joe Perchesf96637b2013-05-04 22:12:25 -05002537 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2538 __func__, mid->mid, mid->mid_state, rdata->result,
2539 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002540
2541 switch (mid->mid_state) {
2542 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002543 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002544 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002545 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002546 int rc;
2547
Jeff Layton0b688cf2012-09-18 16:20:34 -07002548 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002549 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002550 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2551 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002552 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002553 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002554 task_io_account_read(rdata->got_bytes);
2555 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002556 break;
2557 case MID_REQUEST_SUBMITTED:
2558 case MID_RETRY_NEEDED:
2559 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002560 if (server->sign && rdata->got_bytes)
2561 /* reset bytes number since we can not check a sign */
2562 rdata->got_bytes = 0;
2563 /* FIXME: should this be counted toward the initiating task? */
2564 task_io_account_read(rdata->got_bytes);
2565 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002566 break;
2567 default:
2568 if (rdata->result != -ENODATA)
2569 rdata->result = -EIO;
2570 }
2571
2572 if (rdata->result)
2573 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2574
2575 queue_work(cifsiod_wq, &rdata->work);
2576 DeleteMidQEntry(mid);
2577 add_credits(server, credits_received, 0);
2578}
2579
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002580/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002581int
2582smb2_async_readv(struct cifs_readdata *rdata)
2583{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002584 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002585 char *buf;
2586 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002587 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002588 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2589 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002590 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002591 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002592 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002593
Joe Perchesf96637b2013-05-04 22:12:25 -05002594 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2595 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002596
2597 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2598 io_parms.offset = rdata->offset;
2599 io_parms.length = rdata->bytes;
2600 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2601 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2602 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002603
2604 server = io_parms.tcon->ses->server;
2605
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002606 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002607 if (rc) {
2608 if (rc == -EAGAIN && rdata->credits) {
2609 /* credits was reset by reconnect */
2610 rdata->credits = 0;
2611 /* reduce in_flight value since we won't send the req */
2612 spin_lock(&server->req_lock);
2613 server->in_flight--;
2614 spin_unlock(&server->req_lock);
2615 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002616 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002617 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002618
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002619 if (encryption_required(io_parms.tcon))
2620 flags |= CIFS_TRANSFORM_REQ;
2621
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002622 req_len = cpu_to_be32(total_len);
2623
2624 rdata->iov[0].iov_base = &req_len;
2625 rdata->iov[0].iov_len = sizeof(__be32);
2626 rdata->iov[1].iov_base = buf;
2627 rdata->iov[1].iov_len = total_len;
2628
2629 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002630
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002631 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002632 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002633 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002634 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002635 spin_lock(&server->req_lock);
2636 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002637 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002638 spin_unlock(&server->req_lock);
2639 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002640 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002641 }
2642
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002643 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002644 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002645 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002646 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002647 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002648 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002649 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2650 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002651
2652 cifs_small_buf_release(buf);
2653 return rc;
2654}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002655
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002656int
2657SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2658 unsigned int *nbytes, char **buf, int *buf_type)
2659{
2660 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002661 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002662 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002663 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002664 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002665 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002666 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002667 __be32 req_len;
2668 struct smb_rqst rqst = { .rq_iov = iov,
2669 .rq_nvec = 2 };
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002670 int flags = CIFS_LOG_ERROR;
2671 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002672
2673 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002674 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002675 if (rc)
2676 return rc;
2677
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002678 if (encryption_required(io_parms->tcon))
2679 flags |= CIFS_TRANSFORM_REQ;
2680
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002681 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002682
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002683 iov[0].iov_base = &req_len;
2684 iov[0].iov_len = sizeof(__be32);
2685 iov[1].iov_base = req;
2686 iov[1].iov_len = total_len;
2687
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002688 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002689 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002690
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002691 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002692
2693 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002694 if (rc != -ENODATA) {
2695 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2696 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002697 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002698 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2699 return rc == -ENODATA ? 0 : rc;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002700 }
2701
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002702 *nbytes = le32_to_cpu(rsp->DataLength);
2703 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2704 (*nbytes > io_parms->length)) {
2705 cifs_dbg(FYI, "bad length %d for count %d\n",
2706 *nbytes, io_parms->length);
2707 rc = -EIO;
2708 *nbytes = 0;
2709 }
2710
2711 shdr = get_sync_hdr(rsp);
2712
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002713 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002714 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002715 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002716 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002717 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002718 if (resp_buftype == CIFS_SMALL_BUFFER)
2719 *buf_type = CIFS_SMALL_BUFFER;
2720 else if (resp_buftype == CIFS_LARGE_BUFFER)
2721 *buf_type = CIFS_LARGE_BUFFER;
2722 }
2723 return rc;
2724}
2725
Pavel Shilovsky33319142012-09-18 16:20:29 -07002726/*
2727 * Check the mid_state and signature on received buffer (if any), and queue the
2728 * workqueue completion task.
2729 */
2730static void
2731smb2_writev_callback(struct mid_q_entry *mid)
2732{
2733 struct cifs_writedata *wdata = mid->callback_data;
2734 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2735 unsigned int written;
2736 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2737 unsigned int credits_received = 1;
2738
2739 switch (mid->mid_state) {
2740 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002741 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002742 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2743 if (wdata->result != 0)
2744 break;
2745
2746 written = le32_to_cpu(rsp->DataLength);
2747 /*
2748 * Mask off high 16 bits when bytes written as returned
2749 * by the server is greater than bytes requested by the
2750 * client. OS/2 servers are known to set incorrect
2751 * CountHigh values.
2752 */
2753 if (written > wdata->bytes)
2754 written &= 0xFFFF;
2755
2756 if (written < wdata->bytes)
2757 wdata->result = -ENOSPC;
2758 else
2759 wdata->bytes = written;
2760 break;
2761 case MID_REQUEST_SUBMITTED:
2762 case MID_RETRY_NEEDED:
2763 wdata->result = -EAGAIN;
2764 break;
2765 default:
2766 wdata->result = -EIO;
2767 break;
2768 }
2769
2770 if (wdata->result)
2771 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2772
2773 queue_work(cifsiod_wq, &wdata->work);
2774 DeleteMidQEntry(mid);
2775 add_credits(tcon->ses->server, credits_received, 0);
2776}
2777
2778/* smb2_async_writev - send an async write, and set up mid to handle result */
2779int
Steve French4a5c80d2014-02-07 20:45:12 -06002780smb2_async_writev(struct cifs_writedata *wdata,
2781 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002782{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002783 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002784 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002785 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002786 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002787 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002788 struct kvec iov[2];
2789 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002790
2791 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002792 if (rc) {
2793 if (rc == -EAGAIN && wdata->credits) {
2794 /* credits was reset by reconnect */
2795 wdata->credits = 0;
2796 /* reduce in_flight value since we won't send the req */
2797 spin_lock(&server->req_lock);
2798 server->in_flight--;
2799 spin_unlock(&server->req_lock);
2800 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002801 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002802 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002803
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002804 if (encryption_required(tcon))
2805 flags |= CIFS_TRANSFORM_REQ;
2806
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002807 shdr = get_sync_hdr(req);
2808 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002809
2810 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2811 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2812 req->WriteChannelInfoOffset = 0;
2813 req->WriteChannelInfoLength = 0;
2814 req->Channel = 0;
2815 req->Offset = cpu_to_le64(wdata->offset);
2816 /* 4 for rfc1002 length field */
2817 req->DataOffset = cpu_to_le16(
2818 offsetof(struct smb2_write_req, Buffer) - 4);
2819 req->RemainingBytes = 0;
2820
2821 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002822 iov[0].iov_len = 4;
2823 iov[0].iov_base = req;
2824 iov[1].iov_len = get_rfc1002_length(req) - 1;
2825 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002826
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002827 rqst.rq_iov = iov;
2828 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002829 rqst.rq_pages = wdata->pages;
2830 rqst.rq_npages = wdata->nr_pages;
2831 rqst.rq_pagesz = wdata->pagesz;
2832 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002833
Joe Perchesf96637b2013-05-04 22:12:25 -05002834 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2835 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002836
2837 req->Length = cpu_to_le32(wdata->bytes);
2838
2839 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2840
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002841 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002842 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002843 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002844 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002845 spin_lock(&server->req_lock);
2846 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002847 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002848 spin_unlock(&server->req_lock);
2849 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002850 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002851 }
2852
Pavel Shilovsky33319142012-09-18 16:20:29 -07002853 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002854 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2855 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002856
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002857 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002858 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002859 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2860 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002861
Pavel Shilovsky33319142012-09-18 16:20:29 -07002862async_writev_out:
2863 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002864 return rc;
2865}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002866
2867/*
2868 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2869 * The length field from io_parms must be at least 1 and indicates a number of
2870 * elements with data to write that begins with position 1 in iov array. All
2871 * data length is specified by count.
2872 */
2873int
2874SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2875 unsigned int *nbytes, struct kvec *iov, int n_vec)
2876{
2877 int rc = 0;
2878 struct smb2_write_req *req = NULL;
2879 struct smb2_write_rsp *rsp = NULL;
2880 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002881 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002882 int flags = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002883
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002884 *nbytes = 0;
2885
2886 if (n_vec < 1)
2887 return rc;
2888
2889 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2890 if (rc)
2891 return rc;
2892
2893 if (io_parms->tcon->ses->server == NULL)
2894 return -ECONNABORTED;
2895
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002896 if (encryption_required(io_parms->tcon))
2897 flags |= CIFS_TRANSFORM_REQ;
2898
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002899 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002900
2901 req->PersistentFileId = io_parms->persistent_fid;
2902 req->VolatileFileId = io_parms->volatile_fid;
2903 req->WriteChannelInfoOffset = 0;
2904 req->WriteChannelInfoLength = 0;
2905 req->Channel = 0;
2906 req->Length = cpu_to_le32(io_parms->length);
2907 req->Offset = cpu_to_le64(io_parms->offset);
2908 /* 4 for rfc1002 length field */
2909 req->DataOffset = cpu_to_le16(
2910 offsetof(struct smb2_write_req, Buffer) - 4);
2911 req->RemainingBytes = 0;
2912
2913 iov[0].iov_base = (char *)req;
2914 /* 4 for rfc1002 length field and 1 for Buffer */
2915 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2916
2917 /* length of entire message including data to be written */
2918 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2919
2920 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002921 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002922 cifs_small_buf_release(req);
2923 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002924
2925 if (rc) {
2926 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002927 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002928 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002929 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002930
2931 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002932 return rc;
2933}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002934
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002935static unsigned int
2936num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2937{
2938 int len;
2939 unsigned int entrycount = 0;
2940 unsigned int next_offset = 0;
2941 FILE_DIRECTORY_INFO *entryptr;
2942
2943 if (bufstart == NULL)
2944 return 0;
2945
2946 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2947
2948 while (1) {
2949 entryptr = (FILE_DIRECTORY_INFO *)
2950 ((char *)entryptr + next_offset);
2951
2952 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002953 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002954 break;
2955 }
2956
2957 len = le32_to_cpu(entryptr->FileNameLength);
2958 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002959 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2960 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002961 break;
2962 }
2963
2964 *lastentry = (char *)entryptr;
2965 entrycount++;
2966
2967 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2968 if (!next_offset)
2969 break;
2970 }
2971
2972 return entrycount;
2973}
2974
2975/*
2976 * Readdir/FindFirst
2977 */
2978int
2979SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2980 u64 persistent_fid, u64 volatile_fid, int index,
2981 struct cifs_search_info *srch_inf)
2982{
2983 struct smb2_query_directory_req *req;
2984 struct smb2_query_directory_rsp *rsp = NULL;
2985 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002986 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002987 int rc = 0;
2988 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002989 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002990 unsigned char *bufptr;
2991 struct TCP_Server_Info *server;
2992 struct cifs_ses *ses = tcon->ses;
2993 __le16 asteriks = cpu_to_le16('*');
2994 char *end_of_smb;
2995 unsigned int output_size = CIFSMaxBufSize;
2996 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002997 int flags = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002998
2999 if (ses && (ses->server))
3000 server = ses->server;
3001 else
3002 return -EIO;
3003
3004 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
3005 if (rc)
3006 return rc;
3007
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003008 if (encryption_required(tcon))
3009 flags |= CIFS_TRANSFORM_REQ;
3010
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003011 switch (srch_inf->info_level) {
3012 case SMB_FIND_FILE_DIRECTORY_INFO:
3013 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3014 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3015 break;
3016 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3017 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3018 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3019 break;
3020 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003021 cifs_dbg(VFS, "info level %u isn't supported\n",
3022 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003023 rc = -EINVAL;
3024 goto qdir_exit;
3025 }
3026
3027 req->FileIndex = cpu_to_le32(index);
3028 req->PersistentFileId = persistent_fid;
3029 req->VolatileFileId = volatile_fid;
3030
3031 len = 0x2;
3032 bufptr = req->Buffer;
3033 memcpy(bufptr, &asteriks, len);
3034
3035 req->FileNameOffset =
3036 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
3037 req->FileNameLength = cpu_to_le16(len);
3038 /*
3039 * BB could be 30 bytes or so longer if we used SMB2 specific
3040 * buffer lengths, but this is safe and close enough.
3041 */
3042 output_size = min_t(unsigned int, output_size, server->maxBuf);
3043 output_size = min_t(unsigned int, output_size, 2 << 15);
3044 req->OutputBufferLength = cpu_to_le32(output_size);
3045
3046 iov[0].iov_base = (char *)req;
3047 /* 4 for RFC1001 length and 1 for Buffer */
3048 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
3049
3050 iov[1].iov_base = (char *)(req->Buffer);
3051 iov[1].iov_len = len;
3052
3053 inc_rfc1001_len(req, len - 1 /* Buffer */);
3054
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003055 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003056 cifs_small_buf_release(req);
3057 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003058
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003059 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003060 if (rc == -ENODATA &&
3061 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003062 srch_inf->endOfSearch = true;
3063 rc = 0;
3064 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003065 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3066 goto qdir_exit;
3067 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003068
3069 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3070 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3071 info_buf_size);
3072 if (rc)
3073 goto qdir_exit;
3074
3075 srch_inf->unicode = true;
3076
3077 if (srch_inf->ntwrk_buf_start) {
3078 if (srch_inf->smallBuf)
3079 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3080 else
3081 cifs_buf_release(srch_inf->ntwrk_buf_start);
3082 }
3083 srch_inf->ntwrk_buf_start = (char *)rsp;
3084 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3085 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3086 /* 4 for rfc1002 length field */
3087 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3088 srch_inf->entries_in_buffer =
3089 num_entries(srch_inf->srch_entries_start, end_of_smb,
3090 &srch_inf->last_entry, info_buf_size);
3091 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003092 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3093 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3094 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003095 if (resp_buftype == CIFS_LARGE_BUFFER)
3096 srch_inf->smallBuf = false;
3097 else if (resp_buftype == CIFS_SMALL_BUFFER)
3098 srch_inf->smallBuf = true;
3099 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003100 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003101
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003102 return rc;
3103
3104qdir_exit:
3105 free_rsp_buf(resp_buftype, rsp);
3106 return rc;
3107}
3108
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003109static int
3110send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003111 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3112 u8 info_type, u32 additional_info, unsigned int num,
3113 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003114{
3115 struct smb2_set_info_req *req;
3116 struct smb2_set_info_rsp *rsp = NULL;
3117 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003118 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003119 int rc = 0;
3120 int resp_buftype;
3121 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003122 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003123 int flags = 0;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003124
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003125 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003126 return -EIO;
3127
3128 if (!num)
3129 return -EINVAL;
3130
3131 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3132 if (!iov)
3133 return -ENOMEM;
3134
3135 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3136 if (rc) {
3137 kfree(iov);
3138 return rc;
3139 }
3140
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003141 if (encryption_required(tcon))
3142 flags |= CIFS_TRANSFORM_REQ;
3143
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003144 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003145
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003146 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003147 req->FileInfoClass = info_class;
3148 req->PersistentFileId = persistent_fid;
3149 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003150 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003151
3152 /* 4 for RFC1001 length and 1 for Buffer */
3153 req->BufferOffset =
3154 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3155 req->BufferLength = cpu_to_le32(*size);
3156
3157 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3158
3159 memcpy(req->Buffer, *data, *size);
3160
3161 iov[0].iov_base = (char *)req;
3162 /* 4 for RFC1001 length */
3163 iov[0].iov_len = get_rfc1002_length(req) + 4;
3164
3165 for (i = 1; i < num; i++) {
3166 inc_rfc1001_len(req, size[i]);
3167 le32_add_cpu(&req->BufferLength, size[i]);
3168 iov[i].iov_base = (char *)data[i];
3169 iov[i].iov_len = size[i];
3170 }
3171
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003172 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003173 cifs_small_buf_release(req);
3174 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003175
Steve French7d3fb242013-11-18 09:56:28 -06003176 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003177 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003178
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003179 free_rsp_buf(resp_buftype, rsp);
3180 kfree(iov);
3181 return rc;
3182}
3183
3184int
3185SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3186 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3187{
3188 struct smb2_file_rename_info info;
3189 void **data;
3190 unsigned int size[2];
3191 int rc;
3192 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3193
3194 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3195 if (!data)
3196 return -ENOMEM;
3197
3198 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3199 /* 0 = fail if target already exists */
3200 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3201 info.FileNameLength = cpu_to_le32(len);
3202
3203 data[0] = &info;
3204 size[0] = sizeof(struct smb2_file_rename_info);
3205
3206 data[1] = target_file;
3207 size[1] = len + 2 /* null */;
3208
3209 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003210 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3211 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003212 kfree(data);
3213 return rc;
3214}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003215
3216int
Steve French897fba12016-05-12 21:20:36 -05003217SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3218 u64 persistent_fid, u64 volatile_fid)
3219{
3220 __u8 delete_pending = 1;
3221 void *data;
3222 unsigned int size;
3223
3224 data = &delete_pending;
3225 size = 1; /* sizeof __u8 */
3226
3227 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003228 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3229 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003230}
3231
3232int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003233SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3234 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3235{
3236 struct smb2_file_link_info info;
3237 void **data;
3238 unsigned int size[2];
3239 int rc;
3240 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3241
3242 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3243 if (!data)
3244 return -ENOMEM;
3245
3246 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3247 /* 0 = fail if link already exists */
3248 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3249 info.FileNameLength = cpu_to_le32(len);
3250
3251 data[0] = &info;
3252 size[0] = sizeof(struct smb2_file_link_info);
3253
3254 data[1] = target_file;
3255 size[1] = len + 2 /* null */;
3256
3257 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003258 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3259 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003260 kfree(data);
3261 return rc;
3262}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003263
3264int
3265SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003266 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003267{
3268 struct smb2_file_eof_info info;
3269 void *data;
3270 unsigned int size;
3271
3272 info.EndOfFile = *eof;
3273
3274 data = &info;
3275 size = sizeof(struct smb2_file_eof_info);
3276
Steve Frenchf29ebb42014-07-19 21:44:58 -05003277 if (is_falloc)
3278 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003279 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3280 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003281 else
3282 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003283 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3284 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003285}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003286
3287int
3288SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3289 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3290{
3291 unsigned int size;
3292 size = sizeof(FILE_BASIC_INFO);
3293 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003294 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3295 0, 1, (void **)&buf, &size);
3296}
3297
3298int
3299SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3300 u64 persistent_fid, u64 volatile_fid,
3301 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3302{
3303 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3304 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3305 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003306}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003307
3308int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003309SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3310 u64 persistent_fid, u64 volatile_fid,
3311 struct smb2_file_full_ea_info *buf, int len)
3312{
3313 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3314 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3315 0, 1, (void **)&buf, &len);
3316}
3317
3318int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003319SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3320 const u64 persistent_fid, const u64 volatile_fid,
3321 __u8 oplock_level)
3322{
3323 int rc;
3324 struct smb2_oplock_break *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003325 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003326
Joe Perchesf96637b2013-05-04 22:12:25 -05003327 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003328 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003329 if (rc)
3330 return rc;
3331
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003332 if (encryption_required(tcon))
3333 flags |= CIFS_TRANSFORM_REQ;
3334
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003335 req->VolatileFid = volatile_fid;
3336 req->PersistentFid = persistent_fid;
3337 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003338 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003339
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003340 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003341 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003342
3343 if (rc) {
3344 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003345 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003346 }
3347
3348 return rc;
3349}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003350
3351static void
3352copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3353 struct kstatfs *kst)
3354{
3355 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3356 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3357 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303358 kst->f_bfree = kst->f_bavail =
3359 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003360 return;
3361}
3362
3363static int
3364build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3365 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3366{
3367 int rc;
3368 struct smb2_query_info_req *req;
3369
Joe Perchesf96637b2013-05-04 22:12:25 -05003370 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003371
3372 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3373 return -EIO;
3374
3375 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3376 if (rc)
3377 return rc;
3378
3379 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3380 req->FileInfoClass = level;
3381 req->PersistentFileId = persistent_fid;
3382 req->VolatileFileId = volatile_fid;
3383 /* 4 for rfc1002 length field and 1 for pad */
3384 req->InputBufferOffset =
3385 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3386 req->OutputBufferLength = cpu_to_le32(
3387 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3388
3389 iov->iov_base = (char *)req;
3390 /* 4 for rfc1002 length field */
3391 iov->iov_len = get_rfc1002_length(req) + 4;
3392 return 0;
3393}
3394
3395int
3396SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3397 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3398{
3399 struct smb2_query_info_rsp *rsp = NULL;
3400 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003401 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003402 int rc = 0;
3403 int resp_buftype;
3404 struct cifs_ses *ses = tcon->ses;
3405 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003406 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003407
3408 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3409 sizeof(struct smb2_fs_full_size_info),
3410 persistent_fid, volatile_fid);
3411 if (rc)
3412 return rc;
3413
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003414 if (encryption_required(tcon))
3415 flags |= CIFS_TRANSFORM_REQ;
3416
3417 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003418 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003419 if (rc) {
3420 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003421 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003422 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003423 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003424
3425 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3426 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3427 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3428 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3429 sizeof(struct smb2_fs_full_size_info));
3430 if (!rc)
3431 copy_fs_info_to_kstatfs(info, fsdata);
3432
Steve French34f62642013-10-09 02:07:00 -05003433qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003434 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003435 return rc;
3436}
3437
3438int
3439SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003440 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003441{
3442 struct smb2_query_info_rsp *rsp = NULL;
3443 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003444 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003445 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003446 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003447 struct cifs_ses *ses = tcon->ses;
3448 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003449 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003450
Steven French21671142013-10-09 13:36:35 -05003451 if (level == FS_DEVICE_INFORMATION) {
3452 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3453 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3454 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3455 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3456 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003457 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3458 max_len = sizeof(struct smb3_fs_ss_info);
3459 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003460 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003461 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003462 return -EINVAL;
3463 }
3464
3465 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003466 persistent_fid, volatile_fid);
3467 if (rc)
3468 return rc;
3469
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003470 if (encryption_required(tcon))
3471 flags |= CIFS_TRANSFORM_REQ;
3472
3473 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003474 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003475 if (rc) {
3476 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3477 goto qfsattr_exit;
3478 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003479 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003480
3481 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3482 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003483 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3484 if (rc)
3485 goto qfsattr_exit;
3486
3487 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003488 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3489 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003490 rsp_len, max_len));
3491 else if (level == FS_DEVICE_INFORMATION)
3492 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3493 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003494 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3495 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3496 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3497 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3498 tcon->perf_sector_size =
3499 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3500 }
Steve French34f62642013-10-09 02:07:00 -05003501
3502qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003503 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003504 return rc;
3505}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003506
3507int
3508smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3509 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3510 const __u32 num_lock, struct smb2_lock_element *buf)
3511{
3512 int rc = 0;
3513 struct smb2_lock_req *req = NULL;
3514 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003515 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003516 int resp_buf_type;
3517 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003518 int flags = CIFS_NO_RESP;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003519
Joe Perchesf96637b2013-05-04 22:12:25 -05003520 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003521
3522 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3523 if (rc)
3524 return rc;
3525
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003526 if (encryption_required(tcon))
3527 flags |= CIFS_TRANSFORM_REQ;
3528
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003529 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003530 req->LockCount = cpu_to_le16(num_lock);
3531
3532 req->PersistentFileId = persist_fid;
3533 req->VolatileFileId = volatile_fid;
3534
3535 count = num_lock * sizeof(struct smb2_lock_element);
3536 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3537
3538 iov[0].iov_base = (char *)req;
3539 /* 4 for rfc1002 length field and count for all locks */
3540 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3541 iov[1].iov_base = (char *)buf;
3542 iov[1].iov_len = count;
3543
3544 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003545 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003546 &rsp_iov);
3547 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003548 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003549 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003550 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3551 }
3552
3553 return rc;
3554}
3555
3556int
3557SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3558 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3559 const __u64 length, const __u64 offset, const __u32 lock_flags,
3560 const bool wait)
3561{
3562 struct smb2_lock_element lock;
3563
3564 lock.Offset = cpu_to_le64(offset);
3565 lock.Length = cpu_to_le64(length);
3566 lock.Flags = cpu_to_le32(lock_flags);
3567 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3568 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3569
3570 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3571}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003572
3573int
3574SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3575 __u8 *lease_key, const __le32 lease_state)
3576{
3577 int rc;
3578 struct smb2_lease_ack *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003579 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003580
Joe Perchesf96637b2013-05-04 22:12:25 -05003581 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003582 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003583 if (rc)
3584 return rc;
3585
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003586 if (encryption_required(tcon))
3587 flags |= CIFS_TRANSFORM_REQ;
3588
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003589 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003590 req->StructureSize = cpu_to_le16(36);
3591 inc_rfc1001_len(req, 12);
3592
3593 memcpy(req->LeaseKey, lease_key, 16);
3594 req->LeaseState = lease_state;
3595
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003596 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003597 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003598
3599 if (rc) {
3600 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003601 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003602 }
3603
3604 return rc;
3605}