blob: db7410462dc3944f6de216bb25f5f7833bc79eae [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;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001279 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001280
Joe Perchesf96637b2013-05-04 22:12:25 -05001281 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001282
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001283 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001284 return -EIO;
1285
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001286 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1287 if (unc_path == NULL)
1288 return -ENOMEM;
1289
1290 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1291 unc_path_len *= 2;
1292 if (unc_path_len < 2) {
1293 kfree(unc_path);
1294 return -EINVAL;
1295 }
1296
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001297 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1298 if (tcon)
1299 tcon->tid = 0;
1300
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001301 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1302 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001303 if (rc) {
1304 kfree(unc_path);
1305 return rc;
1306 }
1307
1308 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001309 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1310 flags |= CIFS_TRANSFORM_REQ;
1311
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001312 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001313 req->sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001314 if (ses->server->sign)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001315 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001316 } else if (encryption_required(tcon))
1317 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001318
1319 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001320 /* 1 for pad */
1321 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001322
1323 /* Testing shows that buffer offset must be at location of Buffer[0] */
1324 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001325 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001326 req->PathLength = cpu_to_le16(unc_path_len - 2);
1327 iov[1].iov_base = unc_path;
1328 iov[1].iov_len = unc_path_len;
1329
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001330 rc = smb2_send_recv(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;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001403 unsigned int total_len;
1404 struct kvec iov[1];
1405 struct kvec rsp_iov;
1406 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001407
Joe Perchesf96637b2013-05-04 22:12:25 -05001408 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001409
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001410 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001411 return -EIO;
1412
1413 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1414 return 0;
1415
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001416 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1417 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001418 if (rc)
1419 return rc;
1420
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001421 if (encryption_required(tcon))
1422 flags |= CIFS_TRANSFORM_REQ;
1423
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001424 flags |= CIFS_NO_RESP;
1425
1426 iov[0].iov_base = (char *)req;
1427 iov[0].iov_len = total_len;
1428
1429 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001430 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001431 if (rc)
1432 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1433
1434 return rc;
1435}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001436
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001437
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001438static struct create_durable *
1439create_durable_buf(void)
1440{
1441 struct create_durable *buf;
1442
1443 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1444 if (!buf)
1445 return NULL;
1446
1447 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001448 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001449 buf->ccontext.DataLength = cpu_to_le32(16);
1450 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1451 (struct create_durable, Name));
1452 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001453 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001454 buf->Name[0] = 'D';
1455 buf->Name[1] = 'H';
1456 buf->Name[2] = 'n';
1457 buf->Name[3] = 'Q';
1458 return buf;
1459}
1460
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001461static struct create_durable *
1462create_reconnect_durable_buf(struct cifs_fid *fid)
1463{
1464 struct create_durable *buf;
1465
1466 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1467 if (!buf)
1468 return NULL;
1469
1470 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1471 (struct create_durable, Data));
1472 buf->ccontext.DataLength = cpu_to_le32(16);
1473 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1474 (struct create_durable, Name));
1475 buf->ccontext.NameLength = cpu_to_le16(4);
1476 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1477 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001478 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001479 buf->Name[0] = 'D';
1480 buf->Name[1] = 'H';
1481 buf->Name[2] = 'n';
1482 buf->Name[3] = 'C';
1483 return buf;
1484}
1485
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001486static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001487parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1488 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001489{
1490 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001491 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001492 unsigned int next;
1493 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001494 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001495
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001496 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001497 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001498 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001499 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001500 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001501 if (le16_to_cpu(cc->NameLength) == 4 &&
1502 strncmp(name, "RqLs", 4) == 0)
1503 return server->ops->parse_lease_buf(cc, epoch);
1504
1505 next = le32_to_cpu(cc->Next);
1506 if (!next)
1507 break;
1508 remaining -= next;
1509 cc = (struct create_context *)((char *)cc + next);
1510 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001511
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001512 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001513}
1514
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001515static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001516add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1517 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001518{
1519 struct smb2_create_req *req = iov[0].iov_base;
1520 unsigned int num = *num_iovec;
1521
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001522 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001523 if (iov[num].iov_base == NULL)
1524 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001525 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001526 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1527 if (!req->CreateContextsOffset)
1528 req->CreateContextsOffset = cpu_to_le32(
1529 sizeof(struct smb2_create_req) - 4 +
1530 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001531 le32_add_cpu(&req->CreateContextsLength,
1532 server->vals->create_lease_size);
1533 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001534 *num_iovec = num + 1;
1535 return 0;
1536}
1537
Steve Frenchb56eae42015-11-03 09:26:27 -06001538static struct create_durable_v2 *
1539create_durable_v2_buf(struct cifs_fid *pfid)
1540{
1541 struct create_durable_v2 *buf;
1542
1543 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1544 if (!buf)
1545 return NULL;
1546
1547 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1548 (struct create_durable_v2, dcontext));
1549 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1550 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1551 (struct create_durable_v2, Name));
1552 buf->ccontext.NameLength = cpu_to_le16(4);
1553
1554 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1555 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001556 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001557 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1558
1559 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1560 buf->Name[0] = 'D';
1561 buf->Name[1] = 'H';
1562 buf->Name[2] = '2';
1563 buf->Name[3] = 'Q';
1564 return buf;
1565}
1566
1567static struct create_durable_handle_reconnect_v2 *
1568create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1569{
1570 struct create_durable_handle_reconnect_v2 *buf;
1571
1572 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1573 GFP_KERNEL);
1574 if (!buf)
1575 return NULL;
1576
1577 buf->ccontext.DataOffset =
1578 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1579 dcontext));
1580 buf->ccontext.DataLength =
1581 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1582 buf->ccontext.NameOffset =
1583 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1584 Name));
1585 buf->ccontext.NameLength = cpu_to_le16(4);
1586
1587 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1588 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1589 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1590 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1591
1592 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1593 buf->Name[0] = 'D';
1594 buf->Name[1] = 'H';
1595 buf->Name[2] = '2';
1596 buf->Name[3] = 'C';
1597 return buf;
1598}
1599
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001600static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001601add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001602 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001603{
1604 struct smb2_create_req *req = iov[0].iov_base;
1605 unsigned int num = *num_iovec;
1606
Steve Frenchb56eae42015-11-03 09:26:27 -06001607 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1608 if (iov[num].iov_base == NULL)
1609 return -ENOMEM;
1610 iov[num].iov_len = sizeof(struct create_durable_v2);
1611 if (!req->CreateContextsOffset)
1612 req->CreateContextsOffset =
1613 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1614 iov[1].iov_len);
1615 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1616 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1617 *num_iovec = num + 1;
1618 return 0;
1619}
1620
1621static int
1622add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1623 struct cifs_open_parms *oparms)
1624{
1625 struct smb2_create_req *req = iov[0].iov_base;
1626 unsigned int num = *num_iovec;
1627
1628 /* indicate that we don't need to relock the file */
1629 oparms->reconnect = false;
1630
1631 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1632 if (iov[num].iov_base == NULL)
1633 return -ENOMEM;
1634 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1635 if (!req->CreateContextsOffset)
1636 req->CreateContextsOffset =
1637 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1638 iov[1].iov_len);
1639 le32_add_cpu(&req->CreateContextsLength,
1640 sizeof(struct create_durable_handle_reconnect_v2));
1641 inc_rfc1001_len(&req->hdr,
1642 sizeof(struct create_durable_handle_reconnect_v2));
1643 *num_iovec = num + 1;
1644 return 0;
1645}
1646
1647static int
1648add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1649 struct cifs_open_parms *oparms, bool use_persistent)
1650{
1651 struct smb2_create_req *req = iov[0].iov_base;
1652 unsigned int num = *num_iovec;
1653
1654 if (use_persistent) {
1655 if (oparms->reconnect)
1656 return add_durable_reconnect_v2_context(iov, num_iovec,
1657 oparms);
1658 else
1659 return add_durable_v2_context(iov, num_iovec, oparms);
1660 }
1661
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001662 if (oparms->reconnect) {
1663 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1664 /* indicate that we don't need to relock the file */
1665 oparms->reconnect = false;
1666 } else
1667 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001668 if (iov[num].iov_base == NULL)
1669 return -ENOMEM;
1670 iov[num].iov_len = sizeof(struct create_durable);
1671 if (!req->CreateContextsOffset)
1672 req->CreateContextsOffset =
1673 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1674 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001675 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001676 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1677 *num_iovec = num + 1;
1678 return 0;
1679}
1680
Aurelien Aptelf0712922017-02-22 14:47:17 +01001681static int
1682alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1683 const char *treename, const __le16 *path)
1684{
1685 int treename_len, path_len;
1686 struct nls_table *cp;
1687 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1688
1689 /*
1690 * skip leading "\\"
1691 */
1692 treename_len = strlen(treename);
1693 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1694 return -EINVAL;
1695
1696 treename += 2;
1697 treename_len -= 2;
1698
1699 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1700
1701 /*
1702 * make room for one path separator between the treename and
1703 * path
1704 */
1705 *out_len = treename_len + 1 + path_len;
1706
1707 /*
1708 * final path needs to be null-terminated UTF16 with a
1709 * size aligned to 8
1710 */
1711
1712 *out_size = roundup((*out_len+1)*2, 8);
1713 *out_path = kzalloc(*out_size, GFP_KERNEL);
1714 if (!*out_path)
1715 return -ENOMEM;
1716
1717 cp = load_nls_default();
1718 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1719 UniStrcat(*out_path, sep);
1720 UniStrcat(*out_path, path);
1721 unload_nls(cp);
1722
1723 return 0;
1724}
1725
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001726int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001727SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001728 __u8 *oplock, struct smb2_file_all_info *buf,
1729 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001730{
1731 struct smb2_create_req *req;
1732 struct smb2_create_rsp *rsp;
1733 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001734 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001735 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001736 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001737 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001738 int resp_buftype;
1739 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001740 __le16 *copy_path = NULL;
1741 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001742 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001743 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001744 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001745 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001746 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001747
Joe Perchesf96637b2013-05-04 22:12:25 -05001748 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001749
1750 if (ses && (ses->server))
1751 server = ses->server;
1752 else
1753 return -EIO;
1754
1755 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1756 if (rc)
1757 return rc;
1758
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001759 if (encryption_required(tcon))
1760 flags |= CIFS_TRANSFORM_REQ;
1761
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001762 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001763 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001764 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1765 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001766
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001767 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001768 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001769 /* File attributes ignored on open (used in create though) */
1770 req->FileAttributes = cpu_to_le32(file_attributes);
1771 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001772 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1773 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001774
1775 iov[0].iov_base = (char *)req;
1776 /* 4 for rfc1002 length field */
1777 iov[0].iov_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001778 /* -1 since last byte is buf[0] which is sent below (path) */
1779 iov[0].iov_len--;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001780
Aurelien Aptelf0712922017-02-22 14:47:17 +01001781 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1782
1783 /* [MS-SMB2] 2.2.13 NameOffset:
1784 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1785 * the SMB2 header, the file name includes a prefix that will
1786 * be processed during DFS name normalization as specified in
1787 * section 3.3.5.9. Otherwise, the file name is relative to
1788 * the share that is identified by the TreeId in the SMB2
1789 * header.
1790 */
1791 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1792 int name_len;
1793
1794 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1795 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1796 &name_len,
1797 tcon->treeName, path);
1798 if (rc)
1799 return rc;
1800 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001801 uni_path_len = copy_size;
1802 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001803 } else {
1804 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1805 /* MUST set path len (NameLength) to 0 opening root of share */
1806 req->NameLength = cpu_to_le16(uni_path_len - 2);
1807 if (uni_path_len % 8 != 0) {
1808 copy_size = roundup(uni_path_len, 8);
1809 copy_path = kzalloc(copy_size, GFP_KERNEL);
1810 if (!copy_path)
1811 return -ENOMEM;
1812 memcpy((char *)copy_path, (const char *)path,
1813 uni_path_len);
1814 uni_path_len = copy_size;
1815 path = copy_path;
1816 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001817 }
1818
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001819 iov[1].iov_len = uni_path_len;
1820 iov[1].iov_base = path;
1821 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1822 inc_rfc1001_len(req, uni_path_len - 1);
1823
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001824 if (!server->oplocks)
1825 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1826
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001827 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001828 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1829 req->RequestedOplockLevel = *oplock;
1830 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001831 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001832 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001833 cifs_small_buf_release(req);
1834 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001835 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001836 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001837 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001838 }
1839
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001840 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1841 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001842 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001843 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001844 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001845 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001846 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001847 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001848
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001849 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001850 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001851 if (rc) {
1852 cifs_small_buf_release(req);
1853 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001854 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001855 return rc;
1856 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001857 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001858 }
1859
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001860 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001861 cifs_small_buf_release(req);
1862 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001863
1864 if (rc != 0) {
1865 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001866 if (err_buf && rsp)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001867 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1868 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001869 goto creat_exit;
1870 }
1871
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001872 oparms->fid->persistent_fid = rsp->PersistentFileId;
1873 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001874
1875 if (buf) {
1876 memcpy(buf, &rsp->CreationTime, 32);
1877 buf->AllocationSize = rsp->AllocationSize;
1878 buf->EndOfFile = rsp->EndofFile;
1879 buf->Attributes = rsp->FileAttributes;
1880 buf->NumberOfLinks = cpu_to_le32(1);
1881 buf->DeletePending = 0;
1882 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001883
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001884 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001885 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001886 else
1887 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001888creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001889 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001890 kfree(lc_buf);
1891 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001892 free_rsp_buf(resp_buftype, rsp);
1893 return rc;
1894}
1895
Steve French4a72daf2013-06-25 00:20:49 -05001896/*
1897 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1898 */
1899int
1900SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001901 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1902 char *in_data, u32 indatalen,
1903 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001904{
1905 struct smb2_ioctl_req *req;
1906 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001907 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001908 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001909 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001910 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001911 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001912 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001913 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001914 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001915 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05001916
1917 cifs_dbg(FYI, "SMB2 IOCTL\n");
1918
Steve French3d1a3742014-08-11 21:05:25 -05001919 if (out_data != NULL)
1920 *out_data = NULL;
1921
Steve French4a72daf2013-06-25 00:20:49 -05001922 /* zero out returned data len, in case of error */
1923 if (plen)
1924 *plen = 0;
1925
Steve French8e353102015-03-26 19:47:02 -05001926 if (tcon)
1927 ses = tcon->ses;
1928 else
1929 return -EIO;
1930
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001931 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05001932 return -EIO;
1933
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001934 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05001935 if (rc)
1936 return rc;
1937
Aurelien Aptel51146622017-02-28 15:08:41 +01001938 if (use_ipc) {
1939 if (ses->ipc_tid == 0) {
1940 cifs_small_buf_release(req);
1941 return -ENOTCONN;
1942 }
1943
1944 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001945 req->sync_hdr.TreeId, ses->ipc_tid);
1946 req->sync_hdr.TreeId = ses->ipc_tid;
Aurelien Aptel51146622017-02-28 15:08:41 +01001947 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001948 if (encryption_required(tcon))
1949 flags |= CIFS_TRANSFORM_REQ;
1950
Steve French4a72daf2013-06-25 00:20:49 -05001951 req->CtlCode = cpu_to_le32(opcode);
1952 req->PersistentFileId = persistent_fid;
1953 req->VolatileFileId = volatile_fid;
1954
1955 if (indatalen) {
1956 req->InputCount = cpu_to_le32(indatalen);
1957 /* do not set InputOffset if no input data */
1958 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001959 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05001960 iov[1].iov_base = in_data;
1961 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001962 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001963 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001964 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001965
1966 req->OutputOffset = 0;
1967 req->OutputCount = 0; /* MBZ */
1968
1969 /*
1970 * Could increase MaxOutputResponse, but that would require more
1971 * than one credit. Windows typically sets this smaller, but for some
1972 * ioctls it may be useful to allow server to send more. No point
1973 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001974 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1975 * (by default, note that it can be overridden to make max larger)
1976 * in responses (except for read responses which can be bigger.
1977 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001978 */
Steve French7db0a6e2017-05-03 21:12:20 -05001979 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001980
1981 if (is_fsctl)
1982 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1983 else
1984 req->Flags = 0;
1985
1986 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001987
Steve French7ff8d452013-10-14 00:44:19 -05001988 /*
1989 * If no input data, the size of ioctl struct in
1990 * protocol spec still includes a 1 byte data buffer,
1991 * but if input data passed to ioctl, we do not
1992 * want to double count this, so we do not send
1993 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001994 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05001995 */
1996
1997 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001998 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05001999 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002000 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002001
Steve French4587eee2017-10-25 15:58:31 -05002002 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2003 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002004 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002005
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002006 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2007 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002008 cifs_small_buf_release(req);
2009 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002010
Steve French9bf0c9c2013-11-16 18:05:28 -06002011 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002012 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002013 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002014 } else if (rc == -EINVAL) {
2015 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2016 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002017 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002018 goto ioctl_exit;
2019 }
Steve French4a72daf2013-06-25 00:20:49 -05002020 }
2021
2022 /* check if caller wants to look at return data or just return rc */
2023 if ((plen == NULL) || (out_data == NULL))
2024 goto ioctl_exit;
2025
2026 *plen = le32_to_cpu(rsp->OutputCount);
2027
2028 /* We check for obvious errors in the output buffer length and offset */
2029 if (*plen == 0)
2030 goto ioctl_exit; /* server returned no data */
2031 else if (*plen > 0xFF00) {
2032 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2033 *plen = 0;
2034 rc = -EIO;
2035 goto ioctl_exit;
2036 }
2037
2038 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2039 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2040 le32_to_cpu(rsp->OutputOffset));
2041 *plen = 0;
2042 rc = -EIO;
2043 goto ioctl_exit;
2044 }
2045
2046 *out_data = kmalloc(*plen, GFP_KERNEL);
2047 if (*out_data == NULL) {
2048 rc = -ENOMEM;
2049 goto ioctl_exit;
2050 }
2051
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002052 shdr = get_sync_hdr(rsp);
2053 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002054ioctl_exit:
2055 free_rsp_buf(resp_buftype, rsp);
2056 return rc;
2057}
2058
Steve French64a5cfa2013-10-14 15:31:32 -05002059/*
2060 * Individual callers to ioctl worker function follow
2061 */
2062
2063int
2064SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2065 u64 persistent_fid, u64 volatile_fid)
2066{
2067 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002068 struct compress_ioctl fsctl_input;
2069 char *ret_data = NULL;
2070
2071 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002072 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002073
2074 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2075 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002076 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05002077 (char *)&fsctl_input /* data input */,
2078 2 /* in data len */, &ret_data /* out data */, NULL);
2079
2080 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002081
2082 return rc;
2083}
2084
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002085int
2086SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2087 u64 persistent_fid, u64 volatile_fid)
2088{
2089 struct smb2_close_req *req;
2090 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002091 struct cifs_ses *ses = tcon->ses;
2092 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002093 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002094 int resp_buftype;
2095 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002096 int flags = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002097 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002098
Joe Perchesf96637b2013-05-04 22:12:25 -05002099 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002100
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002101 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002102 return -EIO;
2103
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002104 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002105 if (rc)
2106 return rc;
2107
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002108 if (encryption_required(tcon))
2109 flags |= CIFS_TRANSFORM_REQ;
2110
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002111 req->PersistentFileId = persistent_fid;
2112 req->VolatileFileId = volatile_fid;
2113
2114 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002115 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002116
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002117 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002118 cifs_small_buf_release(req);
2119 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002120
2121 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002122 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002123 goto close_exit;
2124 }
2125
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002126 /* BB FIXME - decode close response, update inode for caching */
2127
2128close_exit:
2129 free_rsp_buf(resp_buftype, rsp);
2130 return rc;
2131}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002132
2133static int
2134validate_buf(unsigned int offset, unsigned int buffer_length,
2135 struct smb2_hdr *hdr, unsigned int min_buf_size)
2136
2137{
2138 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2139 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2140 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2141 char *end_of_buf = begin_of_buf + buffer_length;
2142
2143
2144 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002145 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2146 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002147 return -EINVAL;
2148 }
2149
2150 /* check if beyond RFC1001 maximum length */
2151 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002152 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2153 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002154 return -EINVAL;
2155 }
2156
2157 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002158 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002159 return -EINVAL;
2160 }
2161
2162 return 0;
2163}
2164
2165/*
2166 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2167 * Caller must free buffer.
2168 */
2169static int
2170validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2171 struct smb2_hdr *hdr, unsigned int minbufsize,
2172 char *data)
2173
2174{
2175 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2176 int rc;
2177
2178 if (!data)
2179 return -EINVAL;
2180
2181 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2182 if (rc)
2183 return rc;
2184
2185 memcpy(data, begin_of_buf, buffer_length);
2186
2187 return 0;
2188}
2189
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002190static int
2191query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002192 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2193 u32 additional_info, size_t output_len, size_t min_len, void **data,
2194 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002195{
2196 struct smb2_query_info_req *req;
2197 struct smb2_query_info_rsp *rsp = NULL;
2198 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002199 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002200 int rc = 0;
2201 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002202 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002203 int flags = 0;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002204
Joe Perchesf96637b2013-05-04 22:12:25 -05002205 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002206
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002207 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002208 return -EIO;
2209
2210 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2211 if (rc)
2212 return rc;
2213
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002214 if (encryption_required(tcon))
2215 flags |= CIFS_TRANSFORM_REQ;
2216
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002217 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002218 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002219 req->PersistentFileId = persistent_fid;
2220 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002221 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002222
2223 /*
2224 * We do not use the input buffer (do not send extra byte)
2225 */
2226 req->InputBufferOffset = 0;
2227 inc_rfc1001_len(req, -1);
2228
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002229 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002230
2231 iov[0].iov_base = (char *)req;
2232 /* 4 for rfc1002 length field */
2233 iov[0].iov_len = get_rfc1002_length(req) + 4;
2234
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002235 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002236 cifs_small_buf_release(req);
2237 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002238
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002239 if (rc) {
2240 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2241 goto qinf_exit;
2242 }
2243
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002244 if (dlen) {
2245 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2246 if (!*data) {
2247 *data = kmalloc(*dlen, GFP_KERNEL);
2248 if (!*data) {
2249 cifs_dbg(VFS,
2250 "Error %d allocating memory for acl\n",
2251 rc);
2252 *dlen = 0;
2253 goto qinf_exit;
2254 }
2255 }
2256 }
2257
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002258 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2259 le32_to_cpu(rsp->OutputBufferLength),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002260 &rsp->hdr, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002261
2262qinf_exit:
2263 free_rsp_buf(resp_buftype, rsp);
2264 return rc;
2265}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002266
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002267int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002268 u64 persistent_fid, u64 volatile_fid,
2269 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002270{
2271 return query_info(xid, tcon, persistent_fid, volatile_fid,
2272 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002273 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002274 sizeof(struct smb2_file_full_ea_info),
2275 (void **)&data,
2276 NULL);
2277}
2278
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002279int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2280 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002281{
2282 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002283 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002284 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002285 sizeof(struct smb2_file_all_info), (void **)&data,
2286 NULL);
2287}
2288
2289int
2290SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2291 u64 persistent_fid, u64 volatile_fid,
2292 void **data, u32 *plen)
2293{
2294 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2295 *plen = 0;
2296
2297 return query_info(xid, tcon, persistent_fid, volatile_fid,
2298 0, SMB2_O_INFO_SECURITY, additional_info,
2299 SMB2_MAX_BUFFER_SIZE,
2300 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002301}
2302
2303int
2304SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2305 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2306{
2307 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002308 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002309 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002310 sizeof(struct smb2_file_internal_info),
2311 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002312}
2313
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002314/*
2315 * This is a no-op for now. We're not really interested in the reply, but
2316 * rather in the fact that the server sent one and that server->lstrp
2317 * gets updated.
2318 *
2319 * FIXME: maybe we should consider checking that the reply matches request?
2320 */
2321static void
2322smb2_echo_callback(struct mid_q_entry *mid)
2323{
2324 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002325 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002326 unsigned int credits_received = 1;
2327
2328 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002329 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002330
2331 DeleteMidQEntry(mid);
2332 add_credits(server, credits_received, CIFS_ECHO_OP);
2333}
2334
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002335void smb2_reconnect_server(struct work_struct *work)
2336{
2337 struct TCP_Server_Info *server = container_of(work,
2338 struct TCP_Server_Info, reconnect.work);
2339 struct cifs_ses *ses;
2340 struct cifs_tcon *tcon, *tcon2;
2341 struct list_head tmp_list;
2342 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002343 int rc;
2344 int resched = false;
2345
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002346
2347 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2348 mutex_lock(&server->reconnect_mutex);
2349
2350 INIT_LIST_HEAD(&tmp_list);
2351 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2352
2353 spin_lock(&cifs_tcp_ses_lock);
2354 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2355 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002356 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002357 tcon->tc_count++;
2358 list_add_tail(&tcon->rlist, &tmp_list);
2359 tcon_exist = true;
2360 }
2361 }
2362 }
2363 /*
2364 * Get the reference to server struct to be sure that the last call of
2365 * cifs_put_tcon() in the loop below won't release the server pointer.
2366 */
2367 if (tcon_exist)
2368 server->srv_count++;
2369
2370 spin_unlock(&cifs_tcp_ses_lock);
2371
2372 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002373 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2374 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002375 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002376 else
2377 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002378 list_del_init(&tcon->rlist);
2379 cifs_put_tcon(tcon);
2380 }
2381
2382 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002383 if (resched)
2384 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002385 mutex_unlock(&server->reconnect_mutex);
2386
2387 /* now we can safely release srv struct */
2388 if (tcon_exist)
2389 cifs_put_tcp_session(server, 1);
2390}
2391
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002392int
2393SMB2_echo(struct TCP_Server_Info *server)
2394{
2395 struct smb2_echo_req *req;
2396 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002397 struct kvec iov[2];
2398 struct smb_rqst rqst = { .rq_iov = iov,
2399 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002400 unsigned int total_len;
2401 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002402
Joe Perchesf96637b2013-05-04 22:12:25 -05002403 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002404
Steve French4fcd1812016-06-22 20:12:05 -05002405 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002406 /* No need to send echo on newly established connections */
2407 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2408 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002409 }
2410
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002411 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002412 if (rc)
2413 return rc;
2414
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002415 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002416
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002417 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002418 rfc1002_marker = cpu_to_be32(total_len);
2419 iov[0].iov_base = &rfc1002_marker;
2420 iov[1].iov_len = total_len;
2421 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002422
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002423 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2424 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002425 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002426 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002427
2428 cifs_small_buf_release(req);
2429 return rc;
2430}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002431
2432int
2433SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2434 u64 volatile_fid)
2435{
2436 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002437 struct cifs_ses *ses = tcon->ses;
2438 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002439 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002440 int resp_buftype;
2441 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002442 int flags = 0;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002443
Joe Perchesf96637b2013-05-04 22:12:25 -05002444 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002445
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002446 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002447 return -EIO;
2448
2449 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2450 if (rc)
2451 return rc;
2452
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002453 if (encryption_required(tcon))
2454 flags |= CIFS_TRANSFORM_REQ;
2455
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002456 req->PersistentFileId = persistent_fid;
2457 req->VolatileFileId = volatile_fid;
2458
2459 iov[0].iov_base = (char *)req;
2460 /* 4 for rfc1002 length field */
2461 iov[0].iov_len = get_rfc1002_length(req) + 4;
2462
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002463 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002464 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002465
Steve Frenchdfebe402015-03-27 01:00:06 -05002466 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002467 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2468
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002469 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002470 return rc;
2471}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002472
2473/*
2474 * To form a chain of read requests, any read requests after the first should
2475 * have the end_of_chain boolean set to true.
2476 */
2477static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002478smb2_new_read_req(void **buf, unsigned int *total_len,
2479 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2480 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002481{
2482 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002483 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002484 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002485
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002486 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2487 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002488 if (rc)
2489 return rc;
2490 if (io_parms->tcon->ses->server == NULL)
2491 return -ECONNABORTED;
2492
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002493 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002494 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002495
2496 req->PersistentFileId = io_parms->persistent_fid;
2497 req->VolatileFileId = io_parms->volatile_fid;
2498 req->ReadChannelInfoOffset = 0; /* reserved */
2499 req->ReadChannelInfoLength = 0; /* reserved */
2500 req->Channel = 0; /* reserved */
2501 req->MinimumCount = 0;
2502 req->Length = cpu_to_le32(io_parms->length);
2503 req->Offset = cpu_to_le64(io_parms->offset);
2504
2505 if (request_type & CHAINED_REQUEST) {
2506 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002507 /* next 8-byte aligned request */
2508 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2509 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002510 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002511 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002512 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002513 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002514 /*
2515 * Related requests use info from previous read request
2516 * in chain.
2517 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002518 shdr->SessionId = 0xFFFFFFFF;
2519 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002520 req->PersistentFileId = 0xFFFFFFFF;
2521 req->VolatileFileId = 0xFFFFFFFF;
2522 }
2523 }
2524 if (remaining_bytes > io_parms->length)
2525 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2526 else
2527 req->RemainingBytes = 0;
2528
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002529 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002530 return rc;
2531}
2532
2533static void
2534smb2_readv_callback(struct mid_q_entry *mid)
2535{
2536 struct cifs_readdata *rdata = mid->callback_data;
2537 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2538 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002539 struct smb2_sync_hdr *shdr =
2540 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002541 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002542 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2543 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002544 .rq_pages = rdata->pages,
2545 .rq_npages = rdata->nr_pages,
2546 .rq_pagesz = rdata->pagesz,
2547 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002548
Joe Perchesf96637b2013-05-04 22:12:25 -05002549 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2550 __func__, mid->mid, mid->mid_state, rdata->result,
2551 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002552
2553 switch (mid->mid_state) {
2554 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002555 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002556 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002557 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002558 int rc;
2559
Jeff Layton0b688cf2012-09-18 16:20:34 -07002560 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002561 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002562 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2563 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002564 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002565 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002566 task_io_account_read(rdata->got_bytes);
2567 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002568 break;
2569 case MID_REQUEST_SUBMITTED:
2570 case MID_RETRY_NEEDED:
2571 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002572 if (server->sign && rdata->got_bytes)
2573 /* reset bytes number since we can not check a sign */
2574 rdata->got_bytes = 0;
2575 /* FIXME: should this be counted toward the initiating task? */
2576 task_io_account_read(rdata->got_bytes);
2577 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002578 break;
2579 default:
2580 if (rdata->result != -ENODATA)
2581 rdata->result = -EIO;
2582 }
2583
2584 if (rdata->result)
2585 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2586
2587 queue_work(cifsiod_wq, &rdata->work);
2588 DeleteMidQEntry(mid);
2589 add_credits(server, credits_received, 0);
2590}
2591
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002592/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002593int
2594smb2_async_readv(struct cifs_readdata *rdata)
2595{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002596 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002597 char *buf;
2598 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002599 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002600 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2601 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002602 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002603 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002604 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002605
Joe Perchesf96637b2013-05-04 22:12:25 -05002606 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2607 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002608
2609 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2610 io_parms.offset = rdata->offset;
2611 io_parms.length = rdata->bytes;
2612 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2613 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2614 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002615
2616 server = io_parms.tcon->ses->server;
2617
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002618 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002619 if (rc) {
2620 if (rc == -EAGAIN && rdata->credits) {
2621 /* credits was reset by reconnect */
2622 rdata->credits = 0;
2623 /* reduce in_flight value since we won't send the req */
2624 spin_lock(&server->req_lock);
2625 server->in_flight--;
2626 spin_unlock(&server->req_lock);
2627 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002628 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002629 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002630
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002631 if (encryption_required(io_parms.tcon))
2632 flags |= CIFS_TRANSFORM_REQ;
2633
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002634 req_len = cpu_to_be32(total_len);
2635
2636 rdata->iov[0].iov_base = &req_len;
2637 rdata->iov[0].iov_len = sizeof(__be32);
2638 rdata->iov[1].iov_base = buf;
2639 rdata->iov[1].iov_len = total_len;
2640
2641 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002642
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002643 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002644 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002645 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002646 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002647 spin_lock(&server->req_lock);
2648 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002649 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002650 spin_unlock(&server->req_lock);
2651 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002652 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002653 }
2654
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002655 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002656 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002657 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002658 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002659 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002660 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002661 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2662 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002663
2664 cifs_small_buf_release(buf);
2665 return rc;
2666}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002667
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002668int
2669SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2670 unsigned int *nbytes, char **buf, int *buf_type)
2671{
2672 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002673 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002674 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002675 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002676 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002677 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002678 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002679 __be32 req_len;
2680 struct smb_rqst rqst = { .rq_iov = iov,
2681 .rq_nvec = 2 };
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002682 int flags = CIFS_LOG_ERROR;
2683 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002684
2685 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002686 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002687 if (rc)
2688 return rc;
2689
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002690 if (encryption_required(io_parms->tcon))
2691 flags |= CIFS_TRANSFORM_REQ;
2692
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002693 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002694
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002695 iov[0].iov_base = &req_len;
2696 iov[0].iov_len = sizeof(__be32);
2697 iov[1].iov_base = req;
2698 iov[1].iov_len = total_len;
2699
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002700 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002701 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002702
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002703 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002704
2705 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002706 if (rc != -ENODATA) {
2707 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2708 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002709 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002710 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2711 return rc == -ENODATA ? 0 : rc;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002712 }
2713
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002714 *nbytes = le32_to_cpu(rsp->DataLength);
2715 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2716 (*nbytes > io_parms->length)) {
2717 cifs_dbg(FYI, "bad length %d for count %d\n",
2718 *nbytes, io_parms->length);
2719 rc = -EIO;
2720 *nbytes = 0;
2721 }
2722
2723 shdr = get_sync_hdr(rsp);
2724
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002725 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002726 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002727 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002728 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002729 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002730 if (resp_buftype == CIFS_SMALL_BUFFER)
2731 *buf_type = CIFS_SMALL_BUFFER;
2732 else if (resp_buftype == CIFS_LARGE_BUFFER)
2733 *buf_type = CIFS_LARGE_BUFFER;
2734 }
2735 return rc;
2736}
2737
Pavel Shilovsky33319142012-09-18 16:20:29 -07002738/*
2739 * Check the mid_state and signature on received buffer (if any), and queue the
2740 * workqueue completion task.
2741 */
2742static void
2743smb2_writev_callback(struct mid_q_entry *mid)
2744{
2745 struct cifs_writedata *wdata = mid->callback_data;
2746 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2747 unsigned int written;
2748 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2749 unsigned int credits_received = 1;
2750
2751 switch (mid->mid_state) {
2752 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002753 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002754 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2755 if (wdata->result != 0)
2756 break;
2757
2758 written = le32_to_cpu(rsp->DataLength);
2759 /*
2760 * Mask off high 16 bits when bytes written as returned
2761 * by the server is greater than bytes requested by the
2762 * client. OS/2 servers are known to set incorrect
2763 * CountHigh values.
2764 */
2765 if (written > wdata->bytes)
2766 written &= 0xFFFF;
2767
2768 if (written < wdata->bytes)
2769 wdata->result = -ENOSPC;
2770 else
2771 wdata->bytes = written;
2772 break;
2773 case MID_REQUEST_SUBMITTED:
2774 case MID_RETRY_NEEDED:
2775 wdata->result = -EAGAIN;
2776 break;
2777 default:
2778 wdata->result = -EIO;
2779 break;
2780 }
2781
2782 if (wdata->result)
2783 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2784
2785 queue_work(cifsiod_wq, &wdata->work);
2786 DeleteMidQEntry(mid);
2787 add_credits(tcon->ses->server, credits_received, 0);
2788}
2789
2790/* smb2_async_writev - send an async write, and set up mid to handle result */
2791int
Steve French4a5c80d2014-02-07 20:45:12 -06002792smb2_async_writev(struct cifs_writedata *wdata,
2793 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002794{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002795 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002796 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002797 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002798 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002799 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002800 struct kvec iov[2];
2801 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002802
2803 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002804 if (rc) {
2805 if (rc == -EAGAIN && wdata->credits) {
2806 /* credits was reset by reconnect */
2807 wdata->credits = 0;
2808 /* reduce in_flight value since we won't send the req */
2809 spin_lock(&server->req_lock);
2810 server->in_flight--;
2811 spin_unlock(&server->req_lock);
2812 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002813 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002814 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002815
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002816 if (encryption_required(tcon))
2817 flags |= CIFS_TRANSFORM_REQ;
2818
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002819 shdr = get_sync_hdr(req);
2820 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002821
2822 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2823 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2824 req->WriteChannelInfoOffset = 0;
2825 req->WriteChannelInfoLength = 0;
2826 req->Channel = 0;
2827 req->Offset = cpu_to_le64(wdata->offset);
2828 /* 4 for rfc1002 length field */
2829 req->DataOffset = cpu_to_le16(
2830 offsetof(struct smb2_write_req, Buffer) - 4);
2831 req->RemainingBytes = 0;
2832
2833 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002834 iov[0].iov_len = 4;
2835 iov[0].iov_base = req;
2836 iov[1].iov_len = get_rfc1002_length(req) - 1;
2837 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002838
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002839 rqst.rq_iov = iov;
2840 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002841 rqst.rq_pages = wdata->pages;
2842 rqst.rq_npages = wdata->nr_pages;
2843 rqst.rq_pagesz = wdata->pagesz;
2844 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002845
Joe Perchesf96637b2013-05-04 22:12:25 -05002846 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2847 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002848
2849 req->Length = cpu_to_le32(wdata->bytes);
2850
2851 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2852
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002853 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002854 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002855 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002856 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002857 spin_lock(&server->req_lock);
2858 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002859 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002860 spin_unlock(&server->req_lock);
2861 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002862 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002863 }
2864
Pavel Shilovsky33319142012-09-18 16:20:29 -07002865 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002866 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2867 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002868
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002869 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002870 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002871 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2872 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002873
Pavel Shilovsky33319142012-09-18 16:20:29 -07002874async_writev_out:
2875 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002876 return rc;
2877}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002878
2879/*
2880 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2881 * The length field from io_parms must be at least 1 and indicates a number of
2882 * elements with data to write that begins with position 1 in iov array. All
2883 * data length is specified by count.
2884 */
2885int
2886SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2887 unsigned int *nbytes, struct kvec *iov, int n_vec)
2888{
2889 int rc = 0;
2890 struct smb2_write_req *req = NULL;
2891 struct smb2_write_rsp *rsp = NULL;
2892 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002893 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002894 int flags = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002895
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002896 *nbytes = 0;
2897
2898 if (n_vec < 1)
2899 return rc;
2900
2901 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2902 if (rc)
2903 return rc;
2904
2905 if (io_parms->tcon->ses->server == NULL)
2906 return -ECONNABORTED;
2907
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002908 if (encryption_required(io_parms->tcon))
2909 flags |= CIFS_TRANSFORM_REQ;
2910
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002911 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002912
2913 req->PersistentFileId = io_parms->persistent_fid;
2914 req->VolatileFileId = io_parms->volatile_fid;
2915 req->WriteChannelInfoOffset = 0;
2916 req->WriteChannelInfoLength = 0;
2917 req->Channel = 0;
2918 req->Length = cpu_to_le32(io_parms->length);
2919 req->Offset = cpu_to_le64(io_parms->offset);
2920 /* 4 for rfc1002 length field */
2921 req->DataOffset = cpu_to_le16(
2922 offsetof(struct smb2_write_req, Buffer) - 4);
2923 req->RemainingBytes = 0;
2924
2925 iov[0].iov_base = (char *)req;
2926 /* 4 for rfc1002 length field and 1 for Buffer */
2927 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2928
2929 /* length of entire message including data to be written */
2930 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2931
2932 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002933 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002934 cifs_small_buf_release(req);
2935 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002936
2937 if (rc) {
2938 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002939 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002940 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002941 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002942
2943 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002944 return rc;
2945}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002946
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002947static unsigned int
2948num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2949{
2950 int len;
2951 unsigned int entrycount = 0;
2952 unsigned int next_offset = 0;
2953 FILE_DIRECTORY_INFO *entryptr;
2954
2955 if (bufstart == NULL)
2956 return 0;
2957
2958 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2959
2960 while (1) {
2961 entryptr = (FILE_DIRECTORY_INFO *)
2962 ((char *)entryptr + next_offset);
2963
2964 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002965 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002966 break;
2967 }
2968
2969 len = le32_to_cpu(entryptr->FileNameLength);
2970 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002971 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2972 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002973 break;
2974 }
2975
2976 *lastentry = (char *)entryptr;
2977 entrycount++;
2978
2979 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2980 if (!next_offset)
2981 break;
2982 }
2983
2984 return entrycount;
2985}
2986
2987/*
2988 * Readdir/FindFirst
2989 */
2990int
2991SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2992 u64 persistent_fid, u64 volatile_fid, int index,
2993 struct cifs_search_info *srch_inf)
2994{
2995 struct smb2_query_directory_req *req;
2996 struct smb2_query_directory_rsp *rsp = NULL;
2997 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002998 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002999 int rc = 0;
3000 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003001 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003002 unsigned char *bufptr;
3003 struct TCP_Server_Info *server;
3004 struct cifs_ses *ses = tcon->ses;
3005 __le16 asteriks = cpu_to_le16('*');
3006 char *end_of_smb;
3007 unsigned int output_size = CIFSMaxBufSize;
3008 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003009 int flags = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003010
3011 if (ses && (ses->server))
3012 server = ses->server;
3013 else
3014 return -EIO;
3015
3016 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
3017 if (rc)
3018 return rc;
3019
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003020 if (encryption_required(tcon))
3021 flags |= CIFS_TRANSFORM_REQ;
3022
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003023 switch (srch_inf->info_level) {
3024 case SMB_FIND_FILE_DIRECTORY_INFO:
3025 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3026 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3027 break;
3028 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3029 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3030 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3031 break;
3032 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003033 cifs_dbg(VFS, "info level %u isn't supported\n",
3034 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003035 rc = -EINVAL;
3036 goto qdir_exit;
3037 }
3038
3039 req->FileIndex = cpu_to_le32(index);
3040 req->PersistentFileId = persistent_fid;
3041 req->VolatileFileId = volatile_fid;
3042
3043 len = 0x2;
3044 bufptr = req->Buffer;
3045 memcpy(bufptr, &asteriks, len);
3046
3047 req->FileNameOffset =
3048 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
3049 req->FileNameLength = cpu_to_le16(len);
3050 /*
3051 * BB could be 30 bytes or so longer if we used SMB2 specific
3052 * buffer lengths, but this is safe and close enough.
3053 */
3054 output_size = min_t(unsigned int, output_size, server->maxBuf);
3055 output_size = min_t(unsigned int, output_size, 2 << 15);
3056 req->OutputBufferLength = cpu_to_le32(output_size);
3057
3058 iov[0].iov_base = (char *)req;
3059 /* 4 for RFC1001 length and 1 for Buffer */
3060 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
3061
3062 iov[1].iov_base = (char *)(req->Buffer);
3063 iov[1].iov_len = len;
3064
3065 inc_rfc1001_len(req, len - 1 /* Buffer */);
3066
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003067 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003068 cifs_small_buf_release(req);
3069 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003070
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003071 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003072 if (rc == -ENODATA &&
3073 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003074 srch_inf->endOfSearch = true;
3075 rc = 0;
3076 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003077 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3078 goto qdir_exit;
3079 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003080
3081 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3082 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3083 info_buf_size);
3084 if (rc)
3085 goto qdir_exit;
3086
3087 srch_inf->unicode = true;
3088
3089 if (srch_inf->ntwrk_buf_start) {
3090 if (srch_inf->smallBuf)
3091 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3092 else
3093 cifs_buf_release(srch_inf->ntwrk_buf_start);
3094 }
3095 srch_inf->ntwrk_buf_start = (char *)rsp;
3096 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3097 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3098 /* 4 for rfc1002 length field */
3099 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3100 srch_inf->entries_in_buffer =
3101 num_entries(srch_inf->srch_entries_start, end_of_smb,
3102 &srch_inf->last_entry, info_buf_size);
3103 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003104 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3105 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3106 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003107 if (resp_buftype == CIFS_LARGE_BUFFER)
3108 srch_inf->smallBuf = false;
3109 else if (resp_buftype == CIFS_SMALL_BUFFER)
3110 srch_inf->smallBuf = true;
3111 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003112 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003113
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003114 return rc;
3115
3116qdir_exit:
3117 free_rsp_buf(resp_buftype, rsp);
3118 return rc;
3119}
3120
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003121static int
3122send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003123 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3124 u8 info_type, u32 additional_info, unsigned int num,
3125 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003126{
3127 struct smb2_set_info_req *req;
3128 struct smb2_set_info_rsp *rsp = NULL;
3129 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003130 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003131 int rc = 0;
3132 int resp_buftype;
3133 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003134 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003135 int flags = 0;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003136
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003137 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003138 return -EIO;
3139
3140 if (!num)
3141 return -EINVAL;
3142
3143 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3144 if (!iov)
3145 return -ENOMEM;
3146
3147 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3148 if (rc) {
3149 kfree(iov);
3150 return rc;
3151 }
3152
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003153 if (encryption_required(tcon))
3154 flags |= CIFS_TRANSFORM_REQ;
3155
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003156 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003157
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003158 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003159 req->FileInfoClass = info_class;
3160 req->PersistentFileId = persistent_fid;
3161 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003162 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003163
3164 /* 4 for RFC1001 length and 1 for Buffer */
3165 req->BufferOffset =
3166 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3167 req->BufferLength = cpu_to_le32(*size);
3168
3169 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3170
3171 memcpy(req->Buffer, *data, *size);
3172
3173 iov[0].iov_base = (char *)req;
3174 /* 4 for RFC1001 length */
3175 iov[0].iov_len = get_rfc1002_length(req) + 4;
3176
3177 for (i = 1; i < num; i++) {
3178 inc_rfc1001_len(req, size[i]);
3179 le32_add_cpu(&req->BufferLength, size[i]);
3180 iov[i].iov_base = (char *)data[i];
3181 iov[i].iov_len = size[i];
3182 }
3183
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003184 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003185 cifs_small_buf_release(req);
3186 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003187
Steve French7d3fb242013-11-18 09:56:28 -06003188 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003189 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003190
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003191 free_rsp_buf(resp_buftype, rsp);
3192 kfree(iov);
3193 return rc;
3194}
3195
3196int
3197SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3198 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3199{
3200 struct smb2_file_rename_info info;
3201 void **data;
3202 unsigned int size[2];
3203 int rc;
3204 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3205
3206 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3207 if (!data)
3208 return -ENOMEM;
3209
3210 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3211 /* 0 = fail if target already exists */
3212 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3213 info.FileNameLength = cpu_to_le32(len);
3214
3215 data[0] = &info;
3216 size[0] = sizeof(struct smb2_file_rename_info);
3217
3218 data[1] = target_file;
3219 size[1] = len + 2 /* null */;
3220
3221 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003222 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3223 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003224 kfree(data);
3225 return rc;
3226}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003227
3228int
Steve French897fba12016-05-12 21:20:36 -05003229SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3230 u64 persistent_fid, u64 volatile_fid)
3231{
3232 __u8 delete_pending = 1;
3233 void *data;
3234 unsigned int size;
3235
3236 data = &delete_pending;
3237 size = 1; /* sizeof __u8 */
3238
3239 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003240 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3241 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003242}
3243
3244int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003245SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3246 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3247{
3248 struct smb2_file_link_info info;
3249 void **data;
3250 unsigned int size[2];
3251 int rc;
3252 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3253
3254 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3255 if (!data)
3256 return -ENOMEM;
3257
3258 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3259 /* 0 = fail if link already exists */
3260 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3261 info.FileNameLength = cpu_to_le32(len);
3262
3263 data[0] = &info;
3264 size[0] = sizeof(struct smb2_file_link_info);
3265
3266 data[1] = target_file;
3267 size[1] = len + 2 /* null */;
3268
3269 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003270 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3271 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003272 kfree(data);
3273 return rc;
3274}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003275
3276int
3277SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003278 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003279{
3280 struct smb2_file_eof_info info;
3281 void *data;
3282 unsigned int size;
3283
3284 info.EndOfFile = *eof;
3285
3286 data = &info;
3287 size = sizeof(struct smb2_file_eof_info);
3288
Steve Frenchf29ebb42014-07-19 21:44:58 -05003289 if (is_falloc)
3290 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003291 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3292 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003293 else
3294 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003295 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3296 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003297}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003298
3299int
3300SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3301 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3302{
3303 unsigned int size;
3304 size = sizeof(FILE_BASIC_INFO);
3305 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003306 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3307 0, 1, (void **)&buf, &size);
3308}
3309
3310int
3311SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3312 u64 persistent_fid, u64 volatile_fid,
3313 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3314{
3315 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3316 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3317 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003318}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003319
3320int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003321SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3322 u64 persistent_fid, u64 volatile_fid,
3323 struct smb2_file_full_ea_info *buf, int len)
3324{
3325 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3326 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3327 0, 1, (void **)&buf, &len);
3328}
3329
3330int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003331SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3332 const u64 persistent_fid, const u64 volatile_fid,
3333 __u8 oplock_level)
3334{
3335 int rc;
3336 struct smb2_oplock_break *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003337 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003338
Joe Perchesf96637b2013-05-04 22:12:25 -05003339 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003340 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003341 if (rc)
3342 return rc;
3343
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003344 if (encryption_required(tcon))
3345 flags |= CIFS_TRANSFORM_REQ;
3346
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003347 req->VolatileFid = volatile_fid;
3348 req->PersistentFid = persistent_fid;
3349 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003350 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003351
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003352 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003353 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003354
3355 if (rc) {
3356 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003357 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003358 }
3359
3360 return rc;
3361}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003362
3363static void
3364copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3365 struct kstatfs *kst)
3366{
3367 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3368 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3369 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303370 kst->f_bfree = kst->f_bavail =
3371 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003372 return;
3373}
3374
3375static int
3376build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3377 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3378{
3379 int rc;
3380 struct smb2_query_info_req *req;
3381
Joe Perchesf96637b2013-05-04 22:12:25 -05003382 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003383
3384 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3385 return -EIO;
3386
3387 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3388 if (rc)
3389 return rc;
3390
3391 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3392 req->FileInfoClass = level;
3393 req->PersistentFileId = persistent_fid;
3394 req->VolatileFileId = volatile_fid;
3395 /* 4 for rfc1002 length field and 1 for pad */
3396 req->InputBufferOffset =
3397 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3398 req->OutputBufferLength = cpu_to_le32(
3399 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3400
3401 iov->iov_base = (char *)req;
3402 /* 4 for rfc1002 length field */
3403 iov->iov_len = get_rfc1002_length(req) + 4;
3404 return 0;
3405}
3406
3407int
3408SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3409 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3410{
3411 struct smb2_query_info_rsp *rsp = NULL;
3412 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003413 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003414 int rc = 0;
3415 int resp_buftype;
3416 struct cifs_ses *ses = tcon->ses;
3417 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003418 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003419
3420 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3421 sizeof(struct smb2_fs_full_size_info),
3422 persistent_fid, volatile_fid);
3423 if (rc)
3424 return rc;
3425
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003426 if (encryption_required(tcon))
3427 flags |= CIFS_TRANSFORM_REQ;
3428
3429 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003430 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003431 if (rc) {
3432 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003433 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003434 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003435 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003436
3437 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3438 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3439 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3440 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3441 sizeof(struct smb2_fs_full_size_info));
3442 if (!rc)
3443 copy_fs_info_to_kstatfs(info, fsdata);
3444
Steve French34f62642013-10-09 02:07:00 -05003445qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003446 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003447 return rc;
3448}
3449
3450int
3451SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003452 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003453{
3454 struct smb2_query_info_rsp *rsp = NULL;
3455 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003456 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003457 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003458 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003459 struct cifs_ses *ses = tcon->ses;
3460 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003461 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003462
Steven French21671142013-10-09 13:36:35 -05003463 if (level == FS_DEVICE_INFORMATION) {
3464 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3465 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3466 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3467 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3468 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003469 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3470 max_len = sizeof(struct smb3_fs_ss_info);
3471 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003472 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003473 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003474 return -EINVAL;
3475 }
3476
3477 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003478 persistent_fid, volatile_fid);
3479 if (rc)
3480 return rc;
3481
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003482 if (encryption_required(tcon))
3483 flags |= CIFS_TRANSFORM_REQ;
3484
3485 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003486 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003487 if (rc) {
3488 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3489 goto qfsattr_exit;
3490 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003491 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003492
3493 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3494 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003495 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3496 if (rc)
3497 goto qfsattr_exit;
3498
3499 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003500 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3501 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003502 rsp_len, max_len));
3503 else if (level == FS_DEVICE_INFORMATION)
3504 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3505 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003506 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3507 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3508 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3509 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3510 tcon->perf_sector_size =
3511 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3512 }
Steve French34f62642013-10-09 02:07:00 -05003513
3514qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003515 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003516 return rc;
3517}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003518
3519int
3520smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3521 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3522 const __u32 num_lock, struct smb2_lock_element *buf)
3523{
3524 int rc = 0;
3525 struct smb2_lock_req *req = NULL;
3526 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003527 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003528 int resp_buf_type;
3529 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003530 int flags = CIFS_NO_RESP;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003531
Joe Perchesf96637b2013-05-04 22:12:25 -05003532 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003533
3534 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3535 if (rc)
3536 return rc;
3537
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003538 if (encryption_required(tcon))
3539 flags |= CIFS_TRANSFORM_REQ;
3540
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003541 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003542 req->LockCount = cpu_to_le16(num_lock);
3543
3544 req->PersistentFileId = persist_fid;
3545 req->VolatileFileId = volatile_fid;
3546
3547 count = num_lock * sizeof(struct smb2_lock_element);
3548 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3549
3550 iov[0].iov_base = (char *)req;
3551 /* 4 for rfc1002 length field and count for all locks */
3552 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3553 iov[1].iov_base = (char *)buf;
3554 iov[1].iov_len = count;
3555
3556 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003557 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003558 &rsp_iov);
3559 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003560 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003561 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003562 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3563 }
3564
3565 return rc;
3566}
3567
3568int
3569SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3570 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3571 const __u64 length, const __u64 offset, const __u32 lock_flags,
3572 const bool wait)
3573{
3574 struct smb2_lock_element lock;
3575
3576 lock.Offset = cpu_to_le64(offset);
3577 lock.Length = cpu_to_le64(length);
3578 lock.Flags = cpu_to_le32(lock_flags);
3579 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3580 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3581
3582 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3583}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003584
3585int
3586SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3587 __u8 *lease_key, const __le32 lease_state)
3588{
3589 int rc;
3590 struct smb2_lease_ack *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003591 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003592
Joe Perchesf96637b2013-05-04 22:12:25 -05003593 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003594 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003595 if (rc)
3596 return rc;
3597
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003598 if (encryption_required(tcon))
3599 flags |= CIFS_TRANSFORM_REQ;
3600
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003601 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003602 req->StructureSize = cpu_to_le16(36);
3603 inc_rfc1001_len(req, 12);
3604
3605 memcpy(req->LeaseKey, lease_key, 16);
3606 req->LeaseState = lease_state;
3607
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003608 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003609 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003610
3611 if (rc) {
3612 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003613 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003614 }
3615
3616 return rc;
3617}