blob: 544daec74de032fd70bbd401f7259a5ef9ba9557 [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;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100811 unsigned int total_len;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100812
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100813 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
814 &total_len);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100815 if (rc)
816 return rc;
817
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700818 /* First session, not a reauthenticate */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100819 req->sync_hdr.SessionId = 0;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100820
821 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
822 req->PreviousSessionId = sess_data->previous_session;
823
824 req->Flags = 0; /* MBZ */
825 /* to enable echos and oplocks */
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100826 req->sync_hdr.CreditRequest = cpu_to_le16(3);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100827
828 /* only one of SMB2 signing flags may be set in SMB2 request */
829 if (server->sign)
830 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
831 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
832 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
833 else
834 req->SecurityMode = 0;
835
836 req->Capabilities = 0;
837 req->Channel = 0; /* MBZ */
838
839 sess_data->iov[0].iov_base = (char *)req;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100840 /* 1 for pad */
841 sess_data->iov[0].iov_len = total_len - 1;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100842 /*
843 * This variable will be used to clear the buffer
844 * allocated above in case of any error in the calling function.
845 */
846 sess_data->buf0_type = CIFS_SMALL_BUFFER;
847
848 return 0;
849}
850
851static void
852SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
853{
854 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
855 sess_data->buf0_type = CIFS_NO_BUFFER;
856}
857
858static int
859SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
860{
861 int rc;
862 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700863 struct kvec rsp_iov = { NULL, 0 };
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100864
865 /* Testing shows that buffer offset must be at location of Buffer[0] */
866 req->SecurityBufferOffset =
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100867 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100868 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
869
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100870 /* BB add code to build os and lm fields */
871
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +1100872 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
873 sess_data->iov, 2,
874 &sess_data->buf0_type,
875 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700876 cifs_small_buf_release(sess_data->iov[0].iov_base);
877 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100878
879 return rc;
880}
881
882static int
883SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
884{
885 int rc = 0;
886 struct cifs_ses *ses = sess_data->ses;
887
888 mutex_lock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800889 if (ses->server->ops->generate_signingkey) {
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100890 rc = ses->server->ops->generate_signingkey(ses);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100891 if (rc) {
892 cifs_dbg(FYI,
893 "SMB3 session key generation failed\n");
894 mutex_unlock(&ses->server->srv_mutex);
Pavel Shilovskycabfb362016-11-07 18:20:50 -0800895 return rc;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100896 }
897 }
898 if (!ses->server->session_estab) {
899 ses->server->sequence_number = 0x2;
900 ses->server->session_estab = true;
901 }
902 mutex_unlock(&ses->server->srv_mutex);
903
904 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
905 spin_lock(&GlobalMid_Lock);
906 ses->status = CifsGood;
907 ses->need_reconnect = false;
908 spin_unlock(&GlobalMid_Lock);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100909 return rc;
910}
911
912#ifdef CONFIG_CIFS_UPCALL
913static void
914SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
915{
916 int rc;
917 struct cifs_ses *ses = sess_data->ses;
918 struct cifs_spnego_msg *msg;
919 struct key *spnego_key = NULL;
920 struct smb2_sess_setup_rsp *rsp = NULL;
921
922 rc = SMB2_sess_alloc_buffer(sess_data);
923 if (rc)
924 goto out;
925
926 spnego_key = cifs_get_spnego_key(ses);
927 if (IS_ERR(spnego_key)) {
928 rc = PTR_ERR(spnego_key);
929 spnego_key = NULL;
930 goto out;
931 }
932
933 msg = spnego_key->payload.data[0];
934 /*
935 * check version field to make sure that cifs.upcall is
936 * sending us a response in an expected form
937 */
938 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
939 cifs_dbg(VFS,
940 "bad cifs.upcall version. Expected %d got %d",
941 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
942 rc = -EKEYREJECTED;
943 goto out_put_spnego_key;
944 }
945
946 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
947 GFP_KERNEL);
948 if (!ses->auth_key.response) {
949 cifs_dbg(VFS,
950 "Kerberos can't allocate (%u bytes) memory",
951 msg->sesskey_len);
952 rc = -ENOMEM;
953 goto out_put_spnego_key;
954 }
955 ses->auth_key.len = msg->sesskey_len;
956
957 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
958 sess_data->iov[1].iov_len = msg->secblob_len;
959
960 rc = SMB2_sess_sendreceive(sess_data);
961 if (rc)
962 goto out_put_spnego_key;
963
964 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700965 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100966
967 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100968
969 rc = SMB2_sess_establish_session(sess_data);
970out_put_spnego_key:
971 key_invalidate(spnego_key);
972 key_put(spnego_key);
973out:
974 sess_data->result = rc;
975 sess_data->func = NULL;
976 SMB2_sess_free_buffer(sess_data);
977}
978#else
979static void
980SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
981{
982 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
983 sess_data->result = -EOPNOTSUPP;
984 sess_data->func = NULL;
985}
986#endif
987
Sachin Prabhu166cea42016-10-07 19:11:22 +0100988static void
989SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
990
991static void
992SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
993{
994 int rc;
995 struct cifs_ses *ses = sess_data->ses;
996 struct smb2_sess_setup_rsp *rsp = NULL;
997 char *ntlmssp_blob = NULL;
998 bool use_spnego = false; /* else use raw ntlmssp */
999 u16 blob_length = 0;
1000
1001 /*
1002 * If memory allocation is successful, caller of this function
1003 * frees it.
1004 */
1005 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1006 if (!ses->ntlmssp) {
1007 rc = -ENOMEM;
1008 goto out_err;
1009 }
1010 ses->ntlmssp->sesskey_per_smbsess = true;
1011
1012 rc = SMB2_sess_alloc_buffer(sess_data);
1013 if (rc)
1014 goto out_err;
1015
1016 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1017 GFP_KERNEL);
1018 if (ntlmssp_blob == NULL) {
1019 rc = -ENOMEM;
1020 goto out;
1021 }
1022
1023 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1024 if (use_spnego) {
1025 /* BB eventually need to add this */
1026 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1027 rc = -EOPNOTSUPP;
1028 goto out;
1029 } else {
1030 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1031 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1032 }
1033 sess_data->iov[1].iov_base = ntlmssp_blob;
1034 sess_data->iov[1].iov_len = blob_length;
1035
1036 rc = SMB2_sess_sendreceive(sess_data);
1037 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1038
1039 /* If true, rc here is expected and not an error */
1040 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001041 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
Sachin Prabhu166cea42016-10-07 19:11:22 +01001042 rc = 0;
1043
1044 if (rc)
1045 goto out;
1046
1047 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1048 le16_to_cpu(rsp->SecurityBufferOffset)) {
1049 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1050 le16_to_cpu(rsp->SecurityBufferOffset));
1051 rc = -EIO;
1052 goto out;
1053 }
1054 rc = decode_ntlmssp_challenge(rsp->Buffer,
1055 le16_to_cpu(rsp->SecurityBufferLength), ses);
1056 if (rc)
1057 goto out;
1058
1059 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1060
1061
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001062 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001063 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001064
1065out:
1066 kfree(ntlmssp_blob);
1067 SMB2_sess_free_buffer(sess_data);
1068 if (!rc) {
1069 sess_data->result = 0;
1070 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1071 return;
1072 }
1073out_err:
1074 kfree(ses->ntlmssp);
1075 ses->ntlmssp = NULL;
1076 sess_data->result = rc;
1077 sess_data->func = NULL;
1078}
1079
1080static void
1081SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1082{
1083 int rc;
1084 struct cifs_ses *ses = sess_data->ses;
1085 struct smb2_sess_setup_req *req;
1086 struct smb2_sess_setup_rsp *rsp = NULL;
1087 unsigned char *ntlmssp_blob = NULL;
1088 bool use_spnego = false; /* else use raw ntlmssp */
1089 u16 blob_length = 0;
1090
1091 rc = SMB2_sess_alloc_buffer(sess_data);
1092 if (rc)
1093 goto out;
1094
1095 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
Ronnie Sahlberg88ea5cb2017-11-20 11:24:36 +11001096 req->sync_hdr.SessionId = ses->Suid;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001097
1098 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1099 sess_data->nls_cp);
1100 if (rc) {
1101 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1102 goto out;
1103 }
1104
1105 if (use_spnego) {
1106 /* BB eventually need to add this */
1107 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1108 rc = -EOPNOTSUPP;
1109 goto out;
1110 }
1111 sess_data->iov[1].iov_base = ntlmssp_blob;
1112 sess_data->iov[1].iov_len = blob_length;
1113
1114 rc = SMB2_sess_sendreceive(sess_data);
1115 if (rc)
1116 goto out;
1117
1118 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1119
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001120 ses->Suid = rsp->hdr.sync_hdr.SessionId;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001121 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001122
1123 rc = SMB2_sess_establish_session(sess_data);
1124out:
1125 kfree(ntlmssp_blob);
1126 SMB2_sess_free_buffer(sess_data);
1127 kfree(ses->ntlmssp);
1128 ses->ntlmssp = NULL;
1129 sess_data->result = rc;
1130 sess_data->func = NULL;
1131}
1132
1133static int
1134SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1135{
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301136 int type;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001137
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301138 type = smb2_select_sectype(ses->server, ses->sectype);
1139 cifs_dbg(FYI, "sess setup type %d\n", type);
1140 if (type == Unspecified) {
1141 cifs_dbg(VFS,
1142 "Unable to select appropriate authentication method!");
1143 return -EINVAL;
1144 }
1145
1146 switch (type) {
Sachin Prabhu166cea42016-10-07 19:11:22 +01001147 case Kerberos:
1148 sess_data->func = SMB2_auth_kerberos;
1149 break;
1150 case RawNTLMSSP:
1151 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1152 break;
1153 default:
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301154 cifs_dbg(VFS, "secType %d not supported!\n", type);
Sachin Prabhu166cea42016-10-07 19:11:22 +01001155 return -EOPNOTSUPP;
1156 }
1157
1158 return 0;
1159}
1160
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001161int
1162SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1163 const struct nls_table *nls_cp)
1164{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001165 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -04001166 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001167 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001168
Joe Perchesf96637b2013-05-04 22:12:25 -05001169 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001170
Jeff Layton3534b852013-05-24 07:41:01 -04001171 if (!server) {
1172 WARN(1, "%s: server is NULL!\n", __func__);
1173 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001174 }
1175
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001176 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1177 if (!sess_data)
1178 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001179
1180 rc = SMB2_select_sec(ses, sess_data);
1181 if (rc)
1182 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001183 sess_data->xid = xid;
1184 sess_data->ses = ses;
1185 sess_data->buf0_type = CIFS_NO_BUFFER;
1186 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001187
Sachin Prabhu166cea42016-10-07 19:11:22 +01001188 while (sess_data->func)
1189 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001190
Steve Frenchc721c382017-09-19 18:40:03 -05001191 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1192 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001193 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001194out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001195 kfree(sess_data);
1196 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001197}
1198
1199int
1200SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1201{
1202 struct smb2_logoff_req *req; /* response is also trivial struct */
1203 int rc = 0;
1204 struct TCP_Server_Info *server;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001205 int flags = 0;
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001206 unsigned int total_len;
1207 struct kvec iov[1];
1208 struct kvec rsp_iov;
1209 int resp_buf_type;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001210
Joe Perchesf96637b2013-05-04 22:12:25 -05001211 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001212
1213 if (ses && (ses->server))
1214 server = ses->server;
1215 else
1216 return -EIO;
1217
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001218 /* no need to send SMB logoff if uid already closed due to reconnect */
1219 if (ses->need_reconnect)
1220 goto smb2_session_already_dead;
1221
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001222 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001223 if (rc)
1224 return rc;
1225
1226 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001227 req->sync_hdr.SessionId = ses->Suid;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001228
1229 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1230 flags |= CIFS_TRANSFORM_REQ;
1231 else if (server->sign)
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001232 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001233
Ronnie Sahlberg45305ed2017-11-09 12:14:17 +11001234 flags |= CIFS_NO_RESP;
1235
1236 iov[0].iov_base = (char *)req;
1237 iov[0].iov_len = total_len;
1238
1239 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001240 cifs_small_buf_release(req);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001241 /*
1242 * No tcon so can't do
1243 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1244 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001245
1246smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001247 return rc;
1248}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001249
1250static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1251{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001252 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001253}
1254
1255#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1256
Steve Frenchde9f68d2013-11-15 11:26:24 -06001257/* These are similar values to what Windows uses */
1258static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1259{
1260 tcon->max_chunks = 256;
1261 tcon->max_bytes_chunk = 1048576;
1262 tcon->max_bytes_copy = 16777216;
1263}
1264
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001265int
1266SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1267 struct cifs_tcon *tcon, const struct nls_table *cp)
1268{
1269 struct smb2_tree_connect_req *req;
1270 struct smb2_tree_connect_rsp *rsp = NULL;
1271 struct kvec iov[2];
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001272 struct kvec rsp_iov = { NULL, 0 };
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001273 int rc = 0;
1274 int resp_buftype;
1275 int unc_path_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001276 __le16 *unc_path = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001277 int flags = 0;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001278 unsigned int total_len;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001279
Joe Perchesf96637b2013-05-04 22:12:25 -05001280 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001281
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001282 if (!(ses->server) || !tree)
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001283 return -EIO;
1284
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001285 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1286 if (unc_path == NULL)
1287 return -ENOMEM;
1288
1289 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1290 unc_path_len *= 2;
1291 if (unc_path_len < 2) {
1292 kfree(unc_path);
1293 return -EINVAL;
1294 }
1295
Jan-Marek Glogowski806a28e2017-02-20 12:25:58 +01001296 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1297 if (tcon)
1298 tcon->tid = 0;
1299
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001300 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1301 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001302 if (rc) {
1303 kfree(unc_path);
1304 return rc;
1305 }
1306
1307 if (tcon == NULL) {
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001308 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1309 flags |= CIFS_TRANSFORM_REQ;
1310
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001311 /* since no tcon, smb2_init can not do this, so do here */
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001312 req->sync_hdr.SessionId = ses->Suid;
Aurelien Aptelb9043cc2017-02-13 16:19:04 +01001313 if (ses->server->sign)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001314 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001315 } else if (encryption_required(tcon))
1316 flags |= CIFS_TRANSFORM_REQ;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001317
1318 iov[0].iov_base = (char *)req;
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001319 /* 1 for pad */
1320 iov[0].iov_len = total_len - 1;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001321
1322 /* Testing shows that buffer offset must be at location of Buffer[0] */
1323 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001324 - 1 /* pad */);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001325 req->PathLength = cpu_to_le16(unc_path_len - 2);
1326 iov[1].iov_base = unc_path;
1327 iov[1].iov_len = unc_path_len;
1328
Ronnie Sahlberg661bb9432017-11-09 12:14:23 +11001329 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001330 cifs_small_buf_release(req);
1331 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001332
1333 if (rc != 0) {
1334 if (tcon) {
1335 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1336 tcon->need_reconnect = true;
1337 }
1338 goto tcon_error_exit;
1339 }
1340
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001341 if (tcon == NULL) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001342 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001343 goto tcon_exit;
1344 }
1345
Christophe JAILLETcd123002017-05-12 17:59:32 +02001346 switch (rsp->ShareType) {
1347 case SMB2_SHARE_TYPE_DISK:
Joe Perchesf96637b2013-05-04 22:12:25 -05001348 cifs_dbg(FYI, "connection to disk share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001349 break;
1350 case SMB2_SHARE_TYPE_PIPE:
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001351 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001352 cifs_dbg(FYI, "connection to pipe share\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001353 break;
1354 case SMB2_SHARE_TYPE_PRINT:
1355 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001356 cifs_dbg(FYI, "connection to printer\n");
Christophe JAILLETcd123002017-05-12 17:59:32 +02001357 break;
1358 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001359 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001360 rc = -EOPNOTSUPP;
1361 goto tcon_error_exit;
1362 }
1363
1364 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001365 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001366 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1367 tcon->tidStatus = CifsGood;
1368 tcon->need_reconnect = false;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001369 tcon->tid = rsp->hdr.sync_hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001370 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001371
1372 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1373 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001374 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001375
1376 if (tcon->seal &&
1377 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1378 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1379
Steve Frenchde9f68d2013-11-15 11:26:24 -06001380 init_copy_chunk_defaults(tcon);
Steve Frenchff1c0382013-11-19 23:44:46 -06001381 if (tcon->ses->server->ops->validate_negotiate)
1382 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001383tcon_exit:
1384 free_rsp_buf(resp_buftype, rsp);
1385 kfree(unc_path);
1386 return rc;
1387
1388tcon_error_exit:
Aurélien Apteldb3b5472017-10-11 13:23:36 +02001389 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001390 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001391 }
1392 goto tcon_exit;
1393}
1394
1395int
1396SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1397{
1398 struct smb2_tree_disconnect_req *req; /* response is trivial */
1399 int rc = 0;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001400 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001401 int flags = 0;
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001402 unsigned int total_len;
1403 struct kvec iov[1];
1404 struct kvec rsp_iov;
1405 int resp_buf_type;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001406
Joe Perchesf96637b2013-05-04 22:12:25 -05001407 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001408
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001409 if (!ses || !(ses->server))
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001410 return -EIO;
1411
1412 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1413 return 0;
1414
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001415 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1416 &total_len);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001417 if (rc)
1418 return rc;
1419
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001420 if (encryption_required(tcon))
1421 flags |= CIFS_TRANSFORM_REQ;
1422
Ronnie Sahlberg4eecf4c2017-11-09 12:14:18 +11001423 flags |= CIFS_NO_RESP;
1424
1425 iov[0].iov_base = (char *)req;
1426 iov[0].iov_len = total_len;
1427
1428 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001429 cifs_small_buf_release(req);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001430 if (rc)
1431 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1432
1433 return rc;
1434}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001435
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001436
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001437static struct create_durable *
1438create_durable_buf(void)
1439{
1440 struct create_durable *buf;
1441
1442 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1443 if (!buf)
1444 return NULL;
1445
1446 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001447 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001448 buf->ccontext.DataLength = cpu_to_le32(16);
1449 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1450 (struct create_durable, Name));
1451 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001452 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001453 buf->Name[0] = 'D';
1454 buf->Name[1] = 'H';
1455 buf->Name[2] = 'n';
1456 buf->Name[3] = 'Q';
1457 return buf;
1458}
1459
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001460static struct create_durable *
1461create_reconnect_durable_buf(struct cifs_fid *fid)
1462{
1463 struct create_durable *buf;
1464
1465 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1466 if (!buf)
1467 return NULL;
1468
1469 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1470 (struct create_durable, Data));
1471 buf->ccontext.DataLength = cpu_to_le32(16);
1472 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1473 (struct create_durable, Name));
1474 buf->ccontext.NameLength = cpu_to_le16(4);
1475 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1476 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001477 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001478 buf->Name[0] = 'D';
1479 buf->Name[1] = 'H';
1480 buf->Name[2] = 'n';
1481 buf->Name[3] = 'C';
1482 return buf;
1483}
1484
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001485static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001486parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1487 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001488{
1489 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001490 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001491 unsigned int next;
1492 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001493 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001494
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001495 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001496 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001497 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001498 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001499 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001500 if (le16_to_cpu(cc->NameLength) == 4 &&
1501 strncmp(name, "RqLs", 4) == 0)
1502 return server->ops->parse_lease_buf(cc, epoch);
1503
1504 next = le32_to_cpu(cc->Next);
1505 if (!next)
1506 break;
1507 remaining -= next;
1508 cc = (struct create_context *)((char *)cc + next);
1509 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001510
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001511 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001512}
1513
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001514static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001515add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1516 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001517{
1518 struct smb2_create_req *req = iov[0].iov_base;
1519 unsigned int num = *num_iovec;
1520
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001521 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001522 if (iov[num].iov_base == NULL)
1523 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001524 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001525 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1526 if (!req->CreateContextsOffset)
1527 req->CreateContextsOffset = cpu_to_le32(
1528 sizeof(struct smb2_create_req) - 4 +
1529 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001530 le32_add_cpu(&req->CreateContextsLength,
1531 server->vals->create_lease_size);
1532 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001533 *num_iovec = num + 1;
1534 return 0;
1535}
1536
Steve Frenchb56eae42015-11-03 09:26:27 -06001537static struct create_durable_v2 *
1538create_durable_v2_buf(struct cifs_fid *pfid)
1539{
1540 struct create_durable_v2 *buf;
1541
1542 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1543 if (!buf)
1544 return NULL;
1545
1546 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1547 (struct create_durable_v2, dcontext));
1548 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1549 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1550 (struct create_durable_v2, Name));
1551 buf->ccontext.NameLength = cpu_to_le16(4);
1552
1553 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1554 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001555 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001556 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1557
1558 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1559 buf->Name[0] = 'D';
1560 buf->Name[1] = 'H';
1561 buf->Name[2] = '2';
1562 buf->Name[3] = 'Q';
1563 return buf;
1564}
1565
1566static struct create_durable_handle_reconnect_v2 *
1567create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1568{
1569 struct create_durable_handle_reconnect_v2 *buf;
1570
1571 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1572 GFP_KERNEL);
1573 if (!buf)
1574 return NULL;
1575
1576 buf->ccontext.DataOffset =
1577 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1578 dcontext));
1579 buf->ccontext.DataLength =
1580 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1581 buf->ccontext.NameOffset =
1582 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1583 Name));
1584 buf->ccontext.NameLength = cpu_to_le16(4);
1585
1586 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1587 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1588 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1589 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1590
1591 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1592 buf->Name[0] = 'D';
1593 buf->Name[1] = 'H';
1594 buf->Name[2] = '2';
1595 buf->Name[3] = 'C';
1596 return buf;
1597}
1598
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001599static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001600add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001601 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001602{
1603 struct smb2_create_req *req = iov[0].iov_base;
1604 unsigned int num = *num_iovec;
1605
Steve Frenchb56eae42015-11-03 09:26:27 -06001606 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1607 if (iov[num].iov_base == NULL)
1608 return -ENOMEM;
1609 iov[num].iov_len = sizeof(struct create_durable_v2);
1610 if (!req->CreateContextsOffset)
1611 req->CreateContextsOffset =
1612 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1613 iov[1].iov_len);
1614 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1615 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1616 *num_iovec = num + 1;
1617 return 0;
1618}
1619
1620static int
1621add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1622 struct cifs_open_parms *oparms)
1623{
1624 struct smb2_create_req *req = iov[0].iov_base;
1625 unsigned int num = *num_iovec;
1626
1627 /* indicate that we don't need to relock the file */
1628 oparms->reconnect = false;
1629
1630 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1631 if (iov[num].iov_base == NULL)
1632 return -ENOMEM;
1633 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1634 if (!req->CreateContextsOffset)
1635 req->CreateContextsOffset =
1636 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1637 iov[1].iov_len);
1638 le32_add_cpu(&req->CreateContextsLength,
1639 sizeof(struct create_durable_handle_reconnect_v2));
1640 inc_rfc1001_len(&req->hdr,
1641 sizeof(struct create_durable_handle_reconnect_v2));
1642 *num_iovec = num + 1;
1643 return 0;
1644}
1645
1646static int
1647add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1648 struct cifs_open_parms *oparms, bool use_persistent)
1649{
1650 struct smb2_create_req *req = iov[0].iov_base;
1651 unsigned int num = *num_iovec;
1652
1653 if (use_persistent) {
1654 if (oparms->reconnect)
1655 return add_durable_reconnect_v2_context(iov, num_iovec,
1656 oparms);
1657 else
1658 return add_durable_v2_context(iov, num_iovec, oparms);
1659 }
1660
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001661 if (oparms->reconnect) {
1662 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1663 /* indicate that we don't need to relock the file */
1664 oparms->reconnect = false;
1665 } else
1666 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001667 if (iov[num].iov_base == NULL)
1668 return -ENOMEM;
1669 iov[num].iov_len = sizeof(struct create_durable);
1670 if (!req->CreateContextsOffset)
1671 req->CreateContextsOffset =
1672 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1673 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001674 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001675 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1676 *num_iovec = num + 1;
1677 return 0;
1678}
1679
Aurelien Aptelf0712922017-02-22 14:47:17 +01001680static int
1681alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1682 const char *treename, const __le16 *path)
1683{
1684 int treename_len, path_len;
1685 struct nls_table *cp;
1686 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1687
1688 /*
1689 * skip leading "\\"
1690 */
1691 treename_len = strlen(treename);
1692 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1693 return -EINVAL;
1694
1695 treename += 2;
1696 treename_len -= 2;
1697
1698 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1699
1700 /*
1701 * make room for one path separator between the treename and
1702 * path
1703 */
1704 *out_len = treename_len + 1 + path_len;
1705
1706 /*
1707 * final path needs to be null-terminated UTF16 with a
1708 * size aligned to 8
1709 */
1710
1711 *out_size = roundup((*out_len+1)*2, 8);
1712 *out_path = kzalloc(*out_size, GFP_KERNEL);
1713 if (!*out_path)
1714 return -ENOMEM;
1715
1716 cp = load_nls_default();
1717 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1718 UniStrcat(*out_path, sep);
1719 UniStrcat(*out_path, path);
1720 unload_nls(cp);
1721
1722 return 0;
1723}
1724
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001725int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001726SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001727 __u8 *oplock, struct smb2_file_all_info *buf,
1728 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001729{
1730 struct smb2_create_req *req;
1731 struct smb2_create_rsp *rsp;
1732 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001733 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001734 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001735 struct kvec iov[4];
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001736 struct kvec rsp_iov = {NULL, 0};
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001737 int resp_buftype;
1738 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001739 __le16 *copy_path = NULL;
1740 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001741 int rc = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001742 unsigned int n_iov = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001743 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001744 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001745 int flags = 0;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001746
Joe Perchesf96637b2013-05-04 22:12:25 -05001747 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001748
1749 if (ses && (ses->server))
1750 server = ses->server;
1751 else
1752 return -EIO;
1753
1754 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1755 if (rc)
1756 return rc;
1757
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001758 if (encryption_required(tcon))
1759 flags |= CIFS_TRANSFORM_REQ;
1760
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001761 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001762 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001763 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1764 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001765
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001766 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001767 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001768 /* File attributes ignored on open (used in create though) */
1769 req->FileAttributes = cpu_to_le32(file_attributes);
1770 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001771 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1772 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001773
1774 iov[0].iov_base = (char *)req;
1775 /* 4 for rfc1002 length field */
1776 iov[0].iov_len = get_rfc1002_length(req) + 4;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001777 /* -1 since last byte is buf[0] which is sent below (path) */
1778 iov[0].iov_len--;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001779
Aurelien Aptelf0712922017-02-22 14:47:17 +01001780 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1781
1782 /* [MS-SMB2] 2.2.13 NameOffset:
1783 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1784 * the SMB2 header, the file name includes a prefix that will
1785 * be processed during DFS name normalization as specified in
1786 * section 3.3.5.9. Otherwise, the file name is relative to
1787 * the share that is identified by the TreeId in the SMB2
1788 * header.
1789 */
1790 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1791 int name_len;
1792
1793 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1794 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1795 &name_len,
1796 tcon->treeName, path);
1797 if (rc)
1798 return rc;
1799 req->NameLength = cpu_to_le16(name_len * 2);
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001800 uni_path_len = copy_size;
1801 path = copy_path;
Aurelien Aptelf0712922017-02-22 14:47:17 +01001802 } else {
1803 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1804 /* MUST set path len (NameLength) to 0 opening root of share */
1805 req->NameLength = cpu_to_le16(uni_path_len - 2);
1806 if (uni_path_len % 8 != 0) {
1807 copy_size = roundup(uni_path_len, 8);
1808 copy_path = kzalloc(copy_size, GFP_KERNEL);
1809 if (!copy_path)
1810 return -ENOMEM;
1811 memcpy((char *)copy_path, (const char *)path,
1812 uni_path_len);
1813 uni_path_len = copy_size;
1814 path = copy_path;
1815 }
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001816 }
1817
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001818 iov[1].iov_len = uni_path_len;
1819 iov[1].iov_base = path;
1820 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1821 inc_rfc1001_len(req, uni_path_len - 1);
1822
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001823 if (!server->oplocks)
1824 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1825
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001826 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001827 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1828 req->RequestedOplockLevel = *oplock;
1829 else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001830 rc = add_lease_context(server, iov, &n_iov, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001831 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001832 cifs_small_buf_release(req);
1833 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001834 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001835 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001836 lc_buf = iov[n_iov-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001837 }
1838
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001839 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1840 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001841 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001842 struct create_context *ccontext =
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001843 (struct create_context *)iov[n_iov-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001844 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001845 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001846 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001847
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001848 rc = add_durable_context(iov, &n_iov, oparms,
Steve Frenchb56eae42015-11-03 09:26:27 -06001849 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001850 if (rc) {
1851 cifs_small_buf_release(req);
1852 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001853 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001854 return rc;
1855 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001856 dhc_buf = iov[n_iov-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001857 }
1858
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001859 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001860 cifs_small_buf_release(req);
1861 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001862
1863 if (rc != 0) {
1864 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Ronnie Sahlbergbf2afee2017-09-08 10:37:35 +10001865 if (err_buf && rsp)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001866 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1867 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001868 goto creat_exit;
1869 }
1870
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001871 oparms->fid->persistent_fid = rsp->PersistentFileId;
1872 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001873
1874 if (buf) {
1875 memcpy(buf, &rsp->CreationTime, 32);
1876 buf->AllocationSize = rsp->AllocationSize;
1877 buf->EndOfFile = rsp->EndofFile;
1878 buf->Attributes = rsp->FileAttributes;
1879 buf->NumberOfLinks = cpu_to_le32(1);
1880 buf->DeletePending = 0;
1881 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001882
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001883 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001884 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001885 else
1886 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001887creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001888 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001889 kfree(lc_buf);
1890 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001891 free_rsp_buf(resp_buftype, rsp);
1892 return rc;
1893}
1894
Steve French4a72daf2013-06-25 00:20:49 -05001895/*
1896 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1897 */
1898int
1899SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Aurelien Aptel51146622017-02-28 15:08:41 +01001900 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1901 char *in_data, u32 indatalen,
1902 char **out_data, u32 *plen /* returned data len */)
Steve French4a72daf2013-06-25 00:20:49 -05001903{
1904 struct smb2_ioctl_req *req;
1905 struct smb2_ioctl_rsp *rsp;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001906 struct smb2_sync_hdr *shdr;
Steve French8e353102015-03-26 19:47:02 -05001907 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001908 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001909 struct kvec rsp_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001910 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001911 int n_iov;
Steve French4a72daf2013-06-25 00:20:49 -05001912 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001913 int flags = 0;
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001914 unsigned int total_len;
Steve French4a72daf2013-06-25 00:20:49 -05001915
1916 cifs_dbg(FYI, "SMB2 IOCTL\n");
1917
Steve French3d1a3742014-08-11 21:05:25 -05001918 if (out_data != NULL)
1919 *out_data = NULL;
1920
Steve French4a72daf2013-06-25 00:20:49 -05001921 /* zero out returned data len, in case of error */
1922 if (plen)
1923 *plen = 0;
1924
Steve French8e353102015-03-26 19:47:02 -05001925 if (tcon)
1926 ses = tcon->ses;
1927 else
1928 return -EIO;
1929
Christos Gkekas68a6afa2017-07-09 11:45:04 +01001930 if (!ses || !(ses->server))
Steve French4a72daf2013-06-25 00:20:49 -05001931 return -EIO;
1932
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001933 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
Steve French4a72daf2013-06-25 00:20:49 -05001934 if (rc)
1935 return rc;
1936
Aurelien Aptel51146622017-02-28 15:08:41 +01001937 if (use_ipc) {
1938 if (ses->ipc_tid == 0) {
1939 cifs_small_buf_release(req);
1940 return -ENOTCONN;
1941 }
1942
1943 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001944 req->sync_hdr.TreeId, ses->ipc_tid);
1945 req->sync_hdr.TreeId = ses->ipc_tid;
Aurelien Aptel51146622017-02-28 15:08:41 +01001946 }
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07001947 if (encryption_required(tcon))
1948 flags |= CIFS_TRANSFORM_REQ;
1949
Steve French4a72daf2013-06-25 00:20:49 -05001950 req->CtlCode = cpu_to_le32(opcode);
1951 req->PersistentFileId = persistent_fid;
1952 req->VolatileFileId = volatile_fid;
1953
1954 if (indatalen) {
1955 req->InputCount = cpu_to_le32(indatalen);
1956 /* do not set InputOffset if no input data */
1957 req->InputOffset =
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001958 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
Steve French4a72daf2013-06-25 00:20:49 -05001959 iov[1].iov_base = in_data;
1960 iov[1].iov_len = indatalen;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001961 n_iov = 2;
Steve French4a72daf2013-06-25 00:20:49 -05001962 } else
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001963 n_iov = 1;
Steve French4a72daf2013-06-25 00:20:49 -05001964
1965 req->OutputOffset = 0;
1966 req->OutputCount = 0; /* MBZ */
1967
1968 /*
1969 * Could increase MaxOutputResponse, but that would require more
1970 * than one credit. Windows typically sets this smaller, but for some
1971 * ioctls it may be useful to allow server to send more. No point
1972 * limiting what the server can send as long as fits in one credit
Steve French7db0a6e2017-05-03 21:12:20 -05001973 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1974 * (by default, note that it can be overridden to make max larger)
1975 * in responses (except for read responses which can be bigger.
1976 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001977 */
Steve French7db0a6e2017-05-03 21:12:20 -05001978 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001979
1980 if (is_fsctl)
1981 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1982 else
1983 req->Flags = 0;
1984
1985 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001986
Steve French7ff8d452013-10-14 00:44:19 -05001987 /*
1988 * If no input data, the size of ioctl struct in
1989 * protocol spec still includes a 1 byte data buffer,
1990 * but if input data passed to ioctl, we do not
1991 * want to double count this, so we do not send
1992 * the dummy one byte of data in iovec[0] if sending
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001993 * input data (in iovec[1]).
Steve French7ff8d452013-10-14 00:44:19 -05001994 */
1995
1996 if (indatalen) {
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001997 iov[0].iov_len = total_len - 1;
Steve French7ff8d452013-10-14 00:44:19 -05001998 } else
Ronnie Sahlberg97754682017-11-09 12:14:20 +11001999 iov[0].iov_len = total_len;
Steve French7ff8d452013-10-14 00:44:19 -05002000
Steve French4587eee2017-10-25 15:58:31 -05002001 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2002 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002003 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05002004
Ronnie Sahlberg97754682017-11-09 12:14:20 +11002005 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2006 &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002007 cifs_small_buf_release(req);
2008 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
Steve French4a72daf2013-06-25 00:20:49 -05002009
Steve French9bf0c9c2013-11-16 18:05:28 -06002010 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05002011 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05002012 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06002013 } else if (rc == -EINVAL) {
2014 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2015 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05002016 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06002017 goto ioctl_exit;
2018 }
Steve French4a72daf2013-06-25 00:20:49 -05002019 }
2020
2021 /* check if caller wants to look at return data or just return rc */
2022 if ((plen == NULL) || (out_data == NULL))
2023 goto ioctl_exit;
2024
2025 *plen = le32_to_cpu(rsp->OutputCount);
2026
2027 /* We check for obvious errors in the output buffer length and offset */
2028 if (*plen == 0)
2029 goto ioctl_exit; /* server returned no data */
2030 else if (*plen > 0xFF00) {
2031 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2032 *plen = 0;
2033 rc = -EIO;
2034 goto ioctl_exit;
2035 }
2036
2037 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2038 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2039 le32_to_cpu(rsp->OutputOffset));
2040 *plen = 0;
2041 rc = -EIO;
2042 goto ioctl_exit;
2043 }
2044
2045 *out_data = kmalloc(*plen, GFP_KERNEL);
2046 if (*out_data == NULL) {
2047 rc = -ENOMEM;
2048 goto ioctl_exit;
2049 }
2050
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002051 shdr = get_sync_hdr(rsp);
2052 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
Steve French4a72daf2013-06-25 00:20:49 -05002053ioctl_exit:
2054 free_rsp_buf(resp_buftype, rsp);
2055 return rc;
2056}
2057
Steve French64a5cfa2013-10-14 15:31:32 -05002058/*
2059 * Individual callers to ioctl worker function follow
2060 */
2061
2062int
2063SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2064 u64 persistent_fid, u64 volatile_fid)
2065{
2066 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05002067 struct compress_ioctl fsctl_input;
2068 char *ret_data = NULL;
2069
2070 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08002071 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05002072
2073 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2074 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002075 false /* use_ipc */,
Steve French64a5cfa2013-10-14 15:31:32 -05002076 (char *)&fsctl_input /* data input */,
2077 2 /* in data len */, &ret_data /* out data */, NULL);
2078
2079 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05002080
2081 return rc;
2082}
2083
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002084int
2085SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2086 u64 persistent_fid, u64 volatile_fid)
2087{
2088 struct smb2_close_req *req;
2089 struct smb2_close_rsp *rsp;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002090 struct cifs_ses *ses = tcon->ses;
2091 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002092 struct kvec rsp_iov;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002093 int resp_buftype;
2094 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002095 int flags = 0;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002096 unsigned int total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002097
Joe Perchesf96637b2013-05-04 22:12:25 -05002098 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002099
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002100 if (!ses || !(ses->server))
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002101 return -EIO;
2102
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002103 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002104 if (rc)
2105 return rc;
2106
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002107 if (encryption_required(tcon))
2108 flags |= CIFS_TRANSFORM_REQ;
2109
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002110 req->PersistentFileId = persistent_fid;
2111 req->VolatileFileId = volatile_fid;
2112
2113 iov[0].iov_base = (char *)req;
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002114 iov[0].iov_len = total_len;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002115
Ronnie Sahlbergafcccef2017-11-09 12:14:19 +11002116 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002117 cifs_small_buf_release(req);
2118 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002119
2120 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09002121 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002122 goto close_exit;
2123 }
2124
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002125 /* BB FIXME - decode close response, update inode for caching */
2126
2127close_exit:
2128 free_rsp_buf(resp_buftype, rsp);
2129 return rc;
2130}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002131
2132static int
2133validate_buf(unsigned int offset, unsigned int buffer_length,
2134 struct smb2_hdr *hdr, unsigned int min_buf_size)
2135
2136{
2137 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2138 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2139 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2140 char *end_of_buf = begin_of_buf + buffer_length;
2141
2142
2143 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002144 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2145 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002146 return -EINVAL;
2147 }
2148
2149 /* check if beyond RFC1001 maximum length */
2150 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002151 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2152 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002153 return -EINVAL;
2154 }
2155
2156 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002157 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002158 return -EINVAL;
2159 }
2160
2161 return 0;
2162}
2163
2164/*
2165 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2166 * Caller must free buffer.
2167 */
2168static int
2169validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2170 struct smb2_hdr *hdr, unsigned int minbufsize,
2171 char *data)
2172
2173{
2174 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2175 int rc;
2176
2177 if (!data)
2178 return -EINVAL;
2179
2180 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2181 if (rc)
2182 return rc;
2183
2184 memcpy(data, begin_of_buf, buffer_length);
2185
2186 return 0;
2187}
2188
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002189static int
2190query_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002191 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2192 u32 additional_info, size_t output_len, size_t min_len, void **data,
2193 u32 *dlen)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002194{
2195 struct smb2_query_info_req *req;
2196 struct smb2_query_info_rsp *rsp = NULL;
2197 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002198 struct kvec rsp_iov;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002199 int rc = 0;
2200 int resp_buftype;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002201 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002202 int flags = 0;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002203
Joe Perchesf96637b2013-05-04 22:12:25 -05002204 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002205
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002206 if (!ses || !(ses->server))
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002207 return -EIO;
2208
2209 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2210 if (rc)
2211 return rc;
2212
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002213 if (encryption_required(tcon))
2214 flags |= CIFS_TRANSFORM_REQ;
2215
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002216 req->InfoType = info_type;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002217 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002218 req->PersistentFileId = persistent_fid;
2219 req->VolatileFileId = volatile_fid;
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002220 req->AdditionalInformation = cpu_to_le32(additional_info);
Aurelien Aptel48923d22017-10-17 14:47:17 +02002221
2222 /*
2223 * We do not use the input buffer (do not send extra byte)
2224 */
2225 req->InputBufferOffset = 0;
2226 inc_rfc1001_len(req, -1);
2227
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002228 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002229
2230 iov[0].iov_base = (char *)req;
2231 /* 4 for rfc1002 length field */
2232 iov[0].iov_len = get_rfc1002_length(req) + 4;
2233
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002234 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002235 cifs_small_buf_release(req);
2236 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002237
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002238 if (rc) {
2239 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2240 goto qinf_exit;
2241 }
2242
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002243 if (dlen) {
2244 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2245 if (!*data) {
2246 *data = kmalloc(*dlen, GFP_KERNEL);
2247 if (!*data) {
2248 cifs_dbg(VFS,
2249 "Error %d allocating memory for acl\n",
2250 rc);
2251 *dlen = 0;
2252 goto qinf_exit;
2253 }
2254 }
2255 }
2256
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002257 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2258 le32_to_cpu(rsp->OutputBufferLength),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002259 &rsp->hdr, min_len, *data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002260
2261qinf_exit:
2262 free_rsp_buf(resp_buftype, rsp);
2263 return rc;
2264}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002265
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002266int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002267 u64 persistent_fid, u64 volatile_fid,
2268 int ea_buf_size, struct smb2_file_full_ea_info *data)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002269{
2270 return query_info(xid, tcon, persistent_fid, volatile_fid,
2271 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10002272 ea_buf_size,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10002273 sizeof(struct smb2_file_full_ea_info),
2274 (void **)&data,
2275 NULL);
2276}
2277
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002278int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2279 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002280{
2281 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002282 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04002283 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002284 sizeof(struct smb2_file_all_info), (void **)&data,
2285 NULL);
2286}
2287
2288int
2289SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2290 u64 persistent_fid, u64 volatile_fid,
2291 void **data, u32 *plen)
2292{
2293 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2294 *plen = 0;
2295
2296 return query_info(xid, tcon, persistent_fid, volatile_fid,
2297 0, SMB2_O_INFO_SECURITY, additional_info,
2298 SMB2_MAX_BUFFER_SIZE,
2299 sizeof(struct smb2_file_all_info), data, plen);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002300}
2301
2302int
2303SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2304 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2305{
2306 return query_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002307 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002308 sizeof(struct smb2_file_internal_info),
Shirish Pargaonkar42c493c2017-06-22 22:51:31 -05002309 sizeof(struct smb2_file_internal_info),
2310 (void **)&uniqueid, NULL);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002311}
2312
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002313/*
2314 * This is a no-op for now. We're not really interested in the reply, but
2315 * rather in the fact that the server sent one and that server->lstrp
2316 * gets updated.
2317 *
2318 * FIXME: maybe we should consider checking that the reply matches request?
2319 */
2320static void
2321smb2_echo_callback(struct mid_q_entry *mid)
2322{
2323 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002324 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002325 unsigned int credits_received = 1;
2326
2327 if (mid->mid_state == MID_RESPONSE_RECEIVED)
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002328 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002329
2330 DeleteMidQEntry(mid);
2331 add_credits(server, credits_received, CIFS_ECHO_OP);
2332}
2333
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002334void smb2_reconnect_server(struct work_struct *work)
2335{
2336 struct TCP_Server_Info *server = container_of(work,
2337 struct TCP_Server_Info, reconnect.work);
2338 struct cifs_ses *ses;
2339 struct cifs_tcon *tcon, *tcon2;
2340 struct list_head tmp_list;
2341 int tcon_exist = false;
Germano Percossi18ea4312017-04-07 12:29:36 +01002342 int rc;
2343 int resched = false;
2344
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002345
2346 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2347 mutex_lock(&server->reconnect_mutex);
2348
2349 INIT_LIST_HEAD(&tmp_list);
2350 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2351
2352 spin_lock(&cifs_tcp_ses_lock);
2353 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2354 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002355 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002356 tcon->tc_count++;
2357 list_add_tail(&tcon->rlist, &tmp_list);
2358 tcon_exist = true;
2359 }
2360 }
2361 }
2362 /*
2363 * Get the reference to server struct to be sure that the last call of
2364 * cifs_put_tcon() in the loop below won't release the server pointer.
2365 */
2366 if (tcon_exist)
2367 server->srv_count++;
2368
2369 spin_unlock(&cifs_tcp_ses_lock);
2370
2371 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi18ea4312017-04-07 12:29:36 +01002372 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2373 if (!rc)
Pavel Shilovsky96a988f2016-11-29 11:31:23 -08002374 cifs_reopen_persistent_handles(tcon);
Germano Percossi18ea4312017-04-07 12:29:36 +01002375 else
2376 resched = true;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002377 list_del_init(&tcon->rlist);
2378 cifs_put_tcon(tcon);
2379 }
2380
2381 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi18ea4312017-04-07 12:29:36 +01002382 if (resched)
2383 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002384 mutex_unlock(&server->reconnect_mutex);
2385
2386 /* now we can safely release srv struct */
2387 if (tcon_exist)
2388 cifs_put_tcp_session(server, 1);
2389}
2390
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002391int
2392SMB2_echo(struct TCP_Server_Info *server)
2393{
2394 struct smb2_echo_req *req;
2395 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002396 struct kvec iov[2];
2397 struct smb_rqst rqst = { .rq_iov = iov,
2398 .rq_nvec = 2 };
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002399 unsigned int total_len;
2400 __be32 rfc1002_marker;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002401
Joe Perchesf96637b2013-05-04 22:12:25 -05002402 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002403
Steve French4fcd1812016-06-22 20:12:05 -05002404 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002405 /* No need to send echo on newly established connections */
2406 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2407 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002408 }
2409
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002410 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002411 if (rc)
2412 return rc;
2413
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002414 req->sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002415
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002416 iov[0].iov_len = 4;
Ronnie Sahlberg7f7ae752017-11-09 12:14:21 +11002417 rfc1002_marker = cpu_to_be32(total_len);
2418 iov[0].iov_base = &rfc1002_marker;
2419 iov[1].iov_len = total_len;
2420 iov[1].iov_base = (char *)req;
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002421
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002422 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2423 server, CIFS_ECHO_OP);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002424 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002425 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002426
2427 cifs_small_buf_release(req);
2428 return rc;
2429}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002430
2431int
2432SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2433 u64 volatile_fid)
2434{
2435 struct smb2_flush_req *req;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002436 struct cifs_ses *ses = tcon->ses;
2437 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002438 struct kvec rsp_iov;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002439 int resp_buftype;
2440 int rc = 0;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002441 int flags = 0;
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002442
Joe Perchesf96637b2013-05-04 22:12:25 -05002443 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002444
Christos Gkekas68a6afa2017-07-09 11:45:04 +01002445 if (!ses || !(ses->server))
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002446 return -EIO;
2447
2448 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2449 if (rc)
2450 return rc;
2451
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002452 if (encryption_required(tcon))
2453 flags |= CIFS_TRANSFORM_REQ;
2454
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002455 req->PersistentFileId = persistent_fid;
2456 req->VolatileFileId = volatile_fid;
2457
2458 iov[0].iov_base = (char *)req;
2459 /* 4 for rfc1002 length field */
2460 iov[0].iov_len = get_rfc1002_length(req) + 4;
2461
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002462 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002463 cifs_small_buf_release(req);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002464
Steve Frenchdfebe402015-03-27 01:00:06 -05002465 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002466 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2467
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002468 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002469 return rc;
2470}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002471
2472/*
2473 * To form a chain of read requests, any read requests after the first should
2474 * have the end_of_chain boolean set to true.
2475 */
2476static int
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002477smb2_new_read_req(void **buf, unsigned int *total_len,
2478 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2479 int request_type)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002480{
2481 int rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002482 struct smb2_read_plain_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002483 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002484
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002485 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2486 total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002487 if (rc)
2488 return rc;
2489 if (io_parms->tcon->ses->server == NULL)
2490 return -ECONNABORTED;
2491
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002492 shdr = &req->sync_hdr;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002493 shdr->ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002494
2495 req->PersistentFileId = io_parms->persistent_fid;
2496 req->VolatileFileId = io_parms->volatile_fid;
2497 req->ReadChannelInfoOffset = 0; /* reserved */
2498 req->ReadChannelInfoLength = 0; /* reserved */
2499 req->Channel = 0; /* reserved */
2500 req->MinimumCount = 0;
2501 req->Length = cpu_to_le32(io_parms->length);
2502 req->Offset = cpu_to_le64(io_parms->offset);
2503
2504 if (request_type & CHAINED_REQUEST) {
2505 if (!(request_type & END_OF_CHAIN)) {
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002506 /* next 8-byte aligned request */
2507 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2508 shdr->NextCommand = cpu_to_le32(*total_len);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002509 } else /* END_OF_CHAIN */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002510 shdr->NextCommand = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002511 if (request_type & RELATED_REQUEST) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002512 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002513 /*
2514 * Related requests use info from previous read request
2515 * in chain.
2516 */
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002517 shdr->SessionId = 0xFFFFFFFF;
2518 shdr->TreeId = 0xFFFFFFFF;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002519 req->PersistentFileId = 0xFFFFFFFF;
2520 req->VolatileFileId = 0xFFFFFFFF;
2521 }
2522 }
2523 if (remaining_bytes > io_parms->length)
2524 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2525 else
2526 req->RemainingBytes = 0;
2527
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002528 *buf = req;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002529 return rc;
2530}
2531
2532static void
2533smb2_readv_callback(struct mid_q_entry *mid)
2534{
2535 struct cifs_readdata *rdata = mid->callback_data;
2536 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2537 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002538 struct smb2_sync_hdr *shdr =
2539 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002540 unsigned int credits_received = 1;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002541 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2542 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07002543 .rq_pages = rdata->pages,
2544 .rq_npages = rdata->nr_pages,
2545 .rq_pagesz = rdata->pagesz,
2546 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002547
Joe Perchesf96637b2013-05-04 22:12:25 -05002548 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2549 __func__, mid->mid, mid->mid_state, rdata->result,
2550 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002551
2552 switch (mid->mid_state) {
2553 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002554 credits_received = le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002555 /* result already set, check signature */
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002556 if (server->sign && !mid->decrypted) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002557 int rc;
2558
Jeff Layton0b688cf2012-09-18 16:20:34 -07002559 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002560 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002561 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2562 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002563 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002564 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002565 task_io_account_read(rdata->got_bytes);
2566 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002567 break;
2568 case MID_REQUEST_SUBMITTED:
2569 case MID_RETRY_NEEDED:
2570 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002571 if (server->sign && rdata->got_bytes)
2572 /* reset bytes number since we can not check a sign */
2573 rdata->got_bytes = 0;
2574 /* FIXME: should this be counted toward the initiating task? */
2575 task_io_account_read(rdata->got_bytes);
2576 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002577 break;
2578 default:
2579 if (rdata->result != -ENODATA)
2580 rdata->result = -EIO;
2581 }
2582
2583 if (rdata->result)
2584 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2585
2586 queue_work(cifsiod_wq, &rdata->work);
2587 DeleteMidQEntry(mid);
2588 add_credits(server, credits_received, 0);
2589}
2590
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002591/* smb2_async_readv - send an async read, and set up mid to handle result */
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002592int
2593smb2_async_readv(struct cifs_readdata *rdata)
2594{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002595 int rc, flags = 0;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002596 char *buf;
2597 struct smb2_sync_hdr *shdr;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002598 struct cifs_io_parms io_parms;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002599 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2600 .rq_nvec = 2 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002601 struct TCP_Server_Info *server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002602 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002603 __be32 req_len;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002604
Joe Perchesf96637b2013-05-04 22:12:25 -05002605 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2606 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002607
2608 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2609 io_parms.offset = rdata->offset;
2610 io_parms.length = rdata->bytes;
2611 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2612 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2613 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002614
2615 server = io_parms.tcon->ses->server;
2616
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002617 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002618 if (rc) {
2619 if (rc == -EAGAIN && rdata->credits) {
2620 /* credits was reset by reconnect */
2621 rdata->credits = 0;
2622 /* reduce in_flight value since we won't send the req */
2623 spin_lock(&server->req_lock);
2624 server->in_flight--;
2625 spin_unlock(&server->req_lock);
2626 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002627 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002628 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002629
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002630 if (encryption_required(io_parms.tcon))
2631 flags |= CIFS_TRANSFORM_REQ;
2632
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002633 req_len = cpu_to_be32(total_len);
2634
2635 rdata->iov[0].iov_base = &req_len;
2636 rdata->iov[0].iov_len = sizeof(__be32);
2637 rdata->iov[1].iov_base = buf;
2638 rdata->iov[1].iov_len = total_len;
2639
2640 shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002641
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002642 if (rdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002643 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002644 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002645 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002646 spin_lock(&server->req_lock);
2647 server->credits += rdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002648 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002649 spin_unlock(&server->req_lock);
2650 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002651 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002652 }
2653
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002654 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002655 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002656 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002657 smb3_handle_read_data, rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002658 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002659 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002660 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2661 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002662
2663 cifs_small_buf_release(buf);
2664 return rc;
2665}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002666
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002667int
2668SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2669 unsigned int *nbytes, char **buf, int *buf_type)
2670{
2671 int resp_buftype, rc = -EACCES;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002672 struct smb2_read_plain_req *req = NULL;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002673 struct smb2_read_rsp *rsp = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002674 struct smb2_sync_hdr *shdr;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002675 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002676 struct kvec rsp_iov;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002677 unsigned int total_len;
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002678 __be32 req_len;
2679 struct smb_rqst rqst = { .rq_iov = iov,
2680 .rq_nvec = 2 };
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002681 int flags = CIFS_LOG_ERROR;
2682 struct cifs_ses *ses = io_parms->tcon->ses;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002683
2684 *nbytes = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002685 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002686 if (rc)
2687 return rc;
2688
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002689 if (encryption_required(io_parms->tcon))
2690 flags |= CIFS_TRANSFORM_REQ;
2691
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002692 req_len = cpu_to_be32(total_len);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002693
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002694 iov[0].iov_base = &req_len;
2695 iov[0].iov_len = sizeof(__be32);
2696 iov[1].iov_base = req;
2697 iov[1].iov_len = total_len;
2698
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002699 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyb8f57ee2016-11-23 15:31:54 -08002700 cifs_small_buf_release(req);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002701
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002702 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002703
2704 if (rc) {
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002705 if (rc != -ENODATA) {
2706 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2707 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002708 }
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002709 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2710 return rc == -ENODATA ? 0 : rc;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002711 }
2712
Ronnie Sahlberga821df32017-11-21 09:36:33 +11002713 *nbytes = le32_to_cpu(rsp->DataLength);
2714 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2715 (*nbytes > io_parms->length)) {
2716 cifs_dbg(FYI, "bad length %d for count %d\n",
2717 *nbytes, io_parms->length);
2718 rc = -EIO;
2719 *nbytes = 0;
2720 }
2721
2722 shdr = get_sync_hdr(rsp);
2723
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002724 if (*buf) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002725 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002726 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002727 } else if (resp_buftype != CIFS_NO_BUFFER) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002728 *buf = rsp_iov.iov_base;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002729 if (resp_buftype == CIFS_SMALL_BUFFER)
2730 *buf_type = CIFS_SMALL_BUFFER;
2731 else if (resp_buftype == CIFS_LARGE_BUFFER)
2732 *buf_type = CIFS_LARGE_BUFFER;
2733 }
2734 return rc;
2735}
2736
Pavel Shilovsky33319142012-09-18 16:20:29 -07002737/*
2738 * Check the mid_state and signature on received buffer (if any), and queue the
2739 * workqueue completion task.
2740 */
2741static void
2742smb2_writev_callback(struct mid_q_entry *mid)
2743{
2744 struct cifs_writedata *wdata = mid->callback_data;
2745 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2746 unsigned int written;
2747 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2748 unsigned int credits_received = 1;
2749
2750 switch (mid->mid_state) {
2751 case MID_RESPONSE_RECEIVED:
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002752 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002753 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2754 if (wdata->result != 0)
2755 break;
2756
2757 written = le32_to_cpu(rsp->DataLength);
2758 /*
2759 * Mask off high 16 bits when bytes written as returned
2760 * by the server is greater than bytes requested by the
2761 * client. OS/2 servers are known to set incorrect
2762 * CountHigh values.
2763 */
2764 if (written > wdata->bytes)
2765 written &= 0xFFFF;
2766
2767 if (written < wdata->bytes)
2768 wdata->result = -ENOSPC;
2769 else
2770 wdata->bytes = written;
2771 break;
2772 case MID_REQUEST_SUBMITTED:
2773 case MID_RETRY_NEEDED:
2774 wdata->result = -EAGAIN;
2775 break;
2776 default:
2777 wdata->result = -EIO;
2778 break;
2779 }
2780
2781 if (wdata->result)
2782 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2783
2784 queue_work(cifsiod_wq, &wdata->work);
2785 DeleteMidQEntry(mid);
2786 add_credits(tcon->ses->server, credits_received, 0);
2787}
2788
2789/* smb2_async_writev - send an async write, and set up mid to handle result */
2790int
Steve French4a5c80d2014-02-07 20:45:12 -06002791smb2_async_writev(struct cifs_writedata *wdata,
2792 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002793{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002794 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002795 struct smb2_write_req *req = NULL;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002796 struct smb2_sync_hdr *shdr;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002797 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002798 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002799 struct kvec iov[2];
2800 struct smb_rqst rqst = { };
Pavel Shilovsky33319142012-09-18 16:20:29 -07002801
2802 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002803 if (rc) {
2804 if (rc == -EAGAIN && wdata->credits) {
2805 /* credits was reset by reconnect */
2806 wdata->credits = 0;
2807 /* reduce in_flight value since we won't send the req */
2808 spin_lock(&server->req_lock);
2809 server->in_flight--;
2810 spin_unlock(&server->req_lock);
2811 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002812 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002813 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002814
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002815 if (encryption_required(tcon))
2816 flags |= CIFS_TRANSFORM_REQ;
2817
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002818 shdr = get_sync_hdr(req);
2819 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002820
2821 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2822 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2823 req->WriteChannelInfoOffset = 0;
2824 req->WriteChannelInfoLength = 0;
2825 req->Channel = 0;
2826 req->Offset = cpu_to_le64(wdata->offset);
2827 /* 4 for rfc1002 length field */
2828 req->DataOffset = cpu_to_le16(
2829 offsetof(struct smb2_write_req, Buffer) - 4);
2830 req->RemainingBytes = 0;
2831
2832 /* 4 for rfc1002 length field and 1 for Buffer */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002833 iov[0].iov_len = 4;
2834 iov[0].iov_base = req;
2835 iov[1].iov_len = get_rfc1002_length(req) - 1;
2836 iov[1].iov_base = (char *)req + 4;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002837
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002838 rqst.rq_iov = iov;
2839 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002840 rqst.rq_pages = wdata->pages;
2841 rqst.rq_npages = wdata->nr_pages;
2842 rqst.rq_pagesz = wdata->pagesz;
2843 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002844
Joe Perchesf96637b2013-05-04 22:12:25 -05002845 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2846 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002847
2848 req->Length = cpu_to_le32(wdata->bytes);
2849
2850 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2851
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002852 if (wdata->credits) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002853 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002854 SMB2_MAX_BUFFER_SIZE));
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002855 shdr->CreditRequest = shdr->CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002856 spin_lock(&server->req_lock);
2857 server->credits += wdata->credits -
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002858 le16_to_cpu(shdr->CreditCharge);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002859 spin_unlock(&server->req_lock);
2860 wake_up(&server->request_q);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002861 flags |= CIFS_HAS_CREDITS;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002862 }
2863
Pavel Shilovsky33319142012-09-18 16:20:29 -07002864 kref_get(&wdata->refcount);
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002865 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2866 wdata, flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002867
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002868 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002869 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002870 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2871 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002872
Pavel Shilovsky33319142012-09-18 16:20:29 -07002873async_writev_out:
2874 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002875 return rc;
2876}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002877
2878/*
2879 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2880 * The length field from io_parms must be at least 1 and indicates a number of
2881 * elements with data to write that begins with position 1 in iov array. All
2882 * data length is specified by count.
2883 */
2884int
2885SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2886 unsigned int *nbytes, struct kvec *iov, int n_vec)
2887{
2888 int rc = 0;
2889 struct smb2_write_req *req = NULL;
2890 struct smb2_write_rsp *rsp = NULL;
2891 int resp_buftype;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002892 struct kvec rsp_iov;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002893 int flags = 0;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002894
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002895 *nbytes = 0;
2896
2897 if (n_vec < 1)
2898 return rc;
2899
2900 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2901 if (rc)
2902 return rc;
2903
2904 if (io_parms->tcon->ses->server == NULL)
2905 return -ECONNABORTED;
2906
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002907 if (encryption_required(io_parms->tcon))
2908 flags |= CIFS_TRANSFORM_REQ;
2909
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002910 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002911
2912 req->PersistentFileId = io_parms->persistent_fid;
2913 req->VolatileFileId = io_parms->volatile_fid;
2914 req->WriteChannelInfoOffset = 0;
2915 req->WriteChannelInfoLength = 0;
2916 req->Channel = 0;
2917 req->Length = cpu_to_le32(io_parms->length);
2918 req->Offset = cpu_to_le64(io_parms->offset);
2919 /* 4 for rfc1002 length field */
2920 req->DataOffset = cpu_to_le16(
2921 offsetof(struct smb2_write_req, Buffer) - 4);
2922 req->RemainingBytes = 0;
2923
2924 iov[0].iov_base = (char *)req;
2925 /* 4 for rfc1002 length field and 1 for Buffer */
2926 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2927
2928 /* length of entire message including data to be written */
2929 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2930
2931 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07002932 &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002933 cifs_small_buf_release(req);
2934 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002935
2936 if (rc) {
2937 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002938 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002939 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002940 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002941
2942 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002943 return rc;
2944}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002945
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002946static unsigned int
2947num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2948{
2949 int len;
2950 unsigned int entrycount = 0;
2951 unsigned int next_offset = 0;
2952 FILE_DIRECTORY_INFO *entryptr;
2953
2954 if (bufstart == NULL)
2955 return 0;
2956
2957 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2958
2959 while (1) {
2960 entryptr = (FILE_DIRECTORY_INFO *)
2961 ((char *)entryptr + next_offset);
2962
2963 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002964 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002965 break;
2966 }
2967
2968 len = le32_to_cpu(entryptr->FileNameLength);
2969 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002970 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2971 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002972 break;
2973 }
2974
2975 *lastentry = (char *)entryptr;
2976 entrycount++;
2977
2978 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2979 if (!next_offset)
2980 break;
2981 }
2982
2983 return entrycount;
2984}
2985
2986/*
2987 * Readdir/FindFirst
2988 */
2989int
2990SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2991 u64 persistent_fid, u64 volatile_fid, int index,
2992 struct cifs_search_info *srch_inf)
2993{
2994 struct smb2_query_directory_req *req;
2995 struct smb2_query_directory_rsp *rsp = NULL;
2996 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002997 struct kvec rsp_iov;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002998 int rc = 0;
2999 int len;
Steve French75fdfc82015-03-25 18:51:57 -05003000 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003001 unsigned char *bufptr;
3002 struct TCP_Server_Info *server;
3003 struct cifs_ses *ses = tcon->ses;
3004 __le16 asteriks = cpu_to_le16('*');
3005 char *end_of_smb;
3006 unsigned int output_size = CIFSMaxBufSize;
3007 size_t info_buf_size;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003008 int flags = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003009
3010 if (ses && (ses->server))
3011 server = ses->server;
3012 else
3013 return -EIO;
3014
3015 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
3016 if (rc)
3017 return rc;
3018
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003019 if (encryption_required(tcon))
3020 flags |= CIFS_TRANSFORM_REQ;
3021
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003022 switch (srch_inf->info_level) {
3023 case SMB_FIND_FILE_DIRECTORY_INFO:
3024 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3025 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3026 break;
3027 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3028 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3029 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3030 break;
3031 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003032 cifs_dbg(VFS, "info level %u isn't supported\n",
3033 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003034 rc = -EINVAL;
3035 goto qdir_exit;
3036 }
3037
3038 req->FileIndex = cpu_to_le32(index);
3039 req->PersistentFileId = persistent_fid;
3040 req->VolatileFileId = volatile_fid;
3041
3042 len = 0x2;
3043 bufptr = req->Buffer;
3044 memcpy(bufptr, &asteriks, len);
3045
3046 req->FileNameOffset =
3047 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
3048 req->FileNameLength = cpu_to_le16(len);
3049 /*
3050 * BB could be 30 bytes or so longer if we used SMB2 specific
3051 * buffer lengths, but this is safe and close enough.
3052 */
3053 output_size = min_t(unsigned int, output_size, server->maxBuf);
3054 output_size = min_t(unsigned int, output_size, 2 << 15);
3055 req->OutputBufferLength = cpu_to_le32(output_size);
3056
3057 iov[0].iov_base = (char *)req;
3058 /* 4 for RFC1001 length and 1 for Buffer */
3059 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
3060
3061 iov[1].iov_base = (char *)(req->Buffer);
3062 iov[1].iov_len = len;
3063
3064 inc_rfc1001_len(req, len - 1 /* Buffer */);
3065
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003066 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003067 cifs_small_buf_release(req);
3068 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
Pavel Shilovskye5d04882012-09-19 16:03:26 +04003069
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003070 if (rc) {
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003071 if (rc == -ENODATA &&
3072 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04003073 srch_inf->endOfSearch = true;
3074 rc = 0;
3075 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003076 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3077 goto qdir_exit;
3078 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003079
3080 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3081 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3082 info_buf_size);
3083 if (rc)
3084 goto qdir_exit;
3085
3086 srch_inf->unicode = true;
3087
3088 if (srch_inf->ntwrk_buf_start) {
3089 if (srch_inf->smallBuf)
3090 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3091 else
3092 cifs_buf_release(srch_inf->ntwrk_buf_start);
3093 }
3094 srch_inf->ntwrk_buf_start = (char *)rsp;
3095 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3096 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3097 /* 4 for rfc1002 length field */
3098 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3099 srch_inf->entries_in_buffer =
3100 num_entries(srch_inf->srch_entries_start, end_of_smb,
3101 &srch_inf->last_entry, info_buf_size);
3102 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05003103 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3104 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3105 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003106 if (resp_buftype == CIFS_LARGE_BUFFER)
3107 srch_inf->smallBuf = false;
3108 else if (resp_buftype == CIFS_SMALL_BUFFER)
3109 srch_inf->smallBuf = true;
3110 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003111 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003112
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003113 return rc;
3114
3115qdir_exit:
3116 free_rsp_buf(resp_buftype, rsp);
3117 return rc;
3118}
3119
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003120static int
3121send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003122 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3123 u8 info_type, u32 additional_info, unsigned int num,
3124 void **data, unsigned int *size)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003125{
3126 struct smb2_set_info_req *req;
3127 struct smb2_set_info_rsp *rsp = NULL;
3128 struct kvec *iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003129 struct kvec rsp_iov;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003130 int rc = 0;
3131 int resp_buftype;
3132 unsigned int i;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003133 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003134 int flags = 0;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003135
Christos Gkekas68a6afa2017-07-09 11:45:04 +01003136 if (!ses || !(ses->server))
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003137 return -EIO;
3138
3139 if (!num)
3140 return -EINVAL;
3141
3142 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3143 if (!iov)
3144 return -ENOMEM;
3145
3146 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3147 if (rc) {
3148 kfree(iov);
3149 return rc;
3150 }
3151
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003152 if (encryption_required(tcon))
3153 flags |= CIFS_TRANSFORM_REQ;
3154
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003155 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003156
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003157 req->InfoType = info_type;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003158 req->FileInfoClass = info_class;
3159 req->PersistentFileId = persistent_fid;
3160 req->VolatileFileId = volatile_fid;
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003161 req->AdditionalInformation = cpu_to_le32(additional_info);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003162
3163 /* 4 for RFC1001 length and 1 for Buffer */
3164 req->BufferOffset =
3165 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3166 req->BufferLength = cpu_to_le32(*size);
3167
3168 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3169
3170 memcpy(req->Buffer, *data, *size);
3171
3172 iov[0].iov_base = (char *)req;
3173 /* 4 for RFC1001 length */
3174 iov[0].iov_len = get_rfc1002_length(req) + 4;
3175
3176 for (i = 1; i < num; i++) {
3177 inc_rfc1001_len(req, size[i]);
3178 le32_add_cpu(&req->BufferLength, size[i]);
3179 iov[i].iov_base = (char *)data[i];
3180 iov[i].iov_len = size[i];
3181 }
3182
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003183 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003184 cifs_small_buf_release(req);
3185 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003186
Steve French7d3fb242013-11-18 09:56:28 -06003187 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003188 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06003189
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003190 free_rsp_buf(resp_buftype, rsp);
3191 kfree(iov);
3192 return rc;
3193}
3194
3195int
3196SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3197 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3198{
3199 struct smb2_file_rename_info info;
3200 void **data;
3201 unsigned int size[2];
3202 int rc;
3203 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3204
3205 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3206 if (!data)
3207 return -ENOMEM;
3208
3209 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3210 /* 0 = fail if target already exists */
3211 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3212 info.FileNameLength = cpu_to_le32(len);
3213
3214 data[0] = &info;
3215 size[0] = sizeof(struct smb2_file_rename_info);
3216
3217 data[1] = target_file;
3218 size[1] = len + 2 /* null */;
3219
3220 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003221 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3222 0, 2, data, size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003223 kfree(data);
3224 return rc;
3225}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003226
3227int
Steve French897fba12016-05-12 21:20:36 -05003228SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3229 u64 persistent_fid, u64 volatile_fid)
3230{
3231 __u8 delete_pending = 1;
3232 void *data;
3233 unsigned int size;
3234
3235 data = &delete_pending;
3236 size = 1; /* sizeof __u8 */
3237
3238 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003239 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3240 0, 1, &data, &size);
Steve French897fba12016-05-12 21:20:36 -05003241}
3242
3243int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003244SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3245 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3246{
3247 struct smb2_file_link_info info;
3248 void **data;
3249 unsigned int size[2];
3250 int rc;
3251 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3252
3253 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3254 if (!data)
3255 return -ENOMEM;
3256
3257 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3258 /* 0 = fail if link already exists */
3259 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3260 info.FileNameLength = cpu_to_le32(len);
3261
3262 data[0] = &info;
3263 size[0] = sizeof(struct smb2_file_link_info);
3264
3265 data[1] = target_file;
3266 size[1] = len + 2 /* null */;
3267
3268 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003269 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3270 0, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003271 kfree(data);
3272 return rc;
3273}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003274
3275int
3276SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05003277 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003278{
3279 struct smb2_file_eof_info info;
3280 void *data;
3281 unsigned int size;
3282
3283 info.EndOfFile = *eof;
3284
3285 data = &info;
3286 size = sizeof(struct smb2_file_eof_info);
3287
Steve Frenchf29ebb42014-07-19 21:44:58 -05003288 if (is_falloc)
3289 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003290 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3291 0, 1, &data, &size);
Steve Frenchf29ebb42014-07-19 21:44:58 -05003292 else
3293 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003294 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3295 0, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003296}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003297
3298int
3299SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3300 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3301{
3302 unsigned int size;
3303 size = sizeof(FILE_BASIC_INFO);
3304 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
Shirish Pargaonkardac95342017-06-28 22:37:00 -05003305 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3306 0, 1, (void **)&buf, &size);
3307}
3308
3309int
3310SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3311 u64 persistent_fid, u64 volatile_fid,
3312 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3313{
3314 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3315 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3316 1, (void **)&pnntsd, &pacllen);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003317}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003318
3319int
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003320SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3321 u64 persistent_fid, u64 volatile_fid,
3322 struct smb2_file_full_ea_info *buf, int len)
3323{
3324 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3325 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3326 0, 1, (void **)&buf, &len);
3327}
3328
3329int
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003330SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3331 const u64 persistent_fid, const u64 volatile_fid,
3332 __u8 oplock_level)
3333{
3334 int rc;
3335 struct smb2_oplock_break *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003336 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003337
Joe Perchesf96637b2013-05-04 22:12:25 -05003338 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003339 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003340 if (rc)
3341 return rc;
3342
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003343 if (encryption_required(tcon))
3344 flags |= CIFS_TRANSFORM_REQ;
3345
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003346 req->VolatileFid = volatile_fid;
3347 req->PersistentFid = persistent_fid;
3348 req->OplockLevel = oplock_level;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003349 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003350
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003351 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003352 cifs_small_buf_release(req);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003353
3354 if (rc) {
3355 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003356 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003357 }
3358
3359 return rc;
3360}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003361
3362static void
3363copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3364 struct kstatfs *kst)
3365{
3366 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3367 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3368 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu42bec212017-08-03 13:09:03 +05303369 kst->f_bfree = kst->f_bavail =
3370 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003371 return;
3372}
3373
3374static int
3375build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3376 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3377{
3378 int rc;
3379 struct smb2_query_info_req *req;
3380
Joe Perchesf96637b2013-05-04 22:12:25 -05003381 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003382
3383 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3384 return -EIO;
3385
3386 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3387 if (rc)
3388 return rc;
3389
3390 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3391 req->FileInfoClass = level;
3392 req->PersistentFileId = persistent_fid;
3393 req->VolatileFileId = volatile_fid;
3394 /* 4 for rfc1002 length field and 1 for pad */
3395 req->InputBufferOffset =
3396 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3397 req->OutputBufferLength = cpu_to_le32(
3398 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3399
3400 iov->iov_base = (char *)req;
3401 /* 4 for rfc1002 length field */
3402 iov->iov_len = get_rfc1002_length(req) + 4;
3403 return 0;
3404}
3405
3406int
3407SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3408 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3409{
3410 struct smb2_query_info_rsp *rsp = NULL;
3411 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003412 struct kvec rsp_iov;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003413 int rc = 0;
3414 int resp_buftype;
3415 struct cifs_ses *ses = tcon->ses;
3416 struct smb2_fs_full_size_info *info = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003417 int flags = 0;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003418
3419 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3420 sizeof(struct smb2_fs_full_size_info),
3421 persistent_fid, volatile_fid);
3422 if (rc)
3423 return rc;
3424
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003425 if (encryption_required(tcon))
3426 flags |= CIFS_TRANSFORM_REQ;
3427
3428 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003429 cifs_small_buf_release(iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003430 if (rc) {
3431 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003432 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003433 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003434 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003435
3436 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3437 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3438 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3439 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3440 sizeof(struct smb2_fs_full_size_info));
3441 if (!rc)
3442 copy_fs_info_to_kstatfs(info, fsdata);
3443
Steve French34f62642013-10-09 02:07:00 -05003444qfsinf_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003445 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003446 return rc;
3447}
3448
3449int
3450SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003451 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003452{
3453 struct smb2_query_info_rsp *rsp = NULL;
3454 struct kvec iov;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003455 struct kvec rsp_iov;
Steve French34f62642013-10-09 02:07:00 -05003456 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003457 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003458 struct cifs_ses *ses = tcon->ses;
3459 unsigned int rsp_len, offset;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003460 int flags = 0;
Steve French34f62642013-10-09 02:07:00 -05003461
Steven French21671142013-10-09 13:36:35 -05003462 if (level == FS_DEVICE_INFORMATION) {
3463 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3464 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3465 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3466 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3467 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003468 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3469 max_len = sizeof(struct smb3_fs_ss_info);
3470 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003471 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003472 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003473 return -EINVAL;
3474 }
3475
3476 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003477 persistent_fid, volatile_fid);
3478 if (rc)
3479 return rc;
3480
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003481 if (encryption_required(tcon))
3482 flags |= CIFS_TRANSFORM_REQ;
3483
3484 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003485 cifs_small_buf_release(iov.iov_base);
Steve French34f62642013-10-09 02:07:00 -05003486 if (rc) {
3487 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3488 goto qfsattr_exit;
3489 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003490 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Steve French34f62642013-10-09 02:07:00 -05003491
3492 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3493 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003494 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3495 if (rc)
3496 goto qfsattr_exit;
3497
3498 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003499 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3500 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003501 rsp_len, max_len));
3502 else if (level == FS_DEVICE_INFORMATION)
3503 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3504 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003505 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3506 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3507 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3508 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3509 tcon->perf_sector_size =
3510 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3511 }
Steve French34f62642013-10-09 02:07:00 -05003512
3513qfsattr_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003514 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003515 return rc;
3516}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003517
3518int
3519smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3520 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3521 const __u32 num_lock, struct smb2_lock_element *buf)
3522{
3523 int rc = 0;
3524 struct smb2_lock_req *req = NULL;
3525 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003526 struct kvec rsp_iov;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003527 int resp_buf_type;
3528 unsigned int count;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003529 int flags = CIFS_NO_RESP;
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003530
Joe Perchesf96637b2013-05-04 22:12:25 -05003531 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003532
3533 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3534 if (rc)
3535 return rc;
3536
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003537 if (encryption_required(tcon))
3538 flags |= CIFS_TRANSFORM_REQ;
3539
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003540 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003541 req->LockCount = cpu_to_le16(num_lock);
3542
3543 req->PersistentFileId = persist_fid;
3544 req->VolatileFileId = volatile_fid;
3545
3546 count = num_lock * sizeof(struct smb2_lock_element);
3547 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3548
3549 iov[0].iov_base = (char *)req;
3550 /* 4 for rfc1002 length field and count for all locks */
3551 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3552 iov[1].iov_base = (char *)buf;
3553 iov[1].iov_len = count;
3554
3555 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003556 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003557 &rsp_iov);
3558 cifs_small_buf_release(req);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003559 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003560 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003561 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3562 }
3563
3564 return rc;
3565}
3566
3567int
3568SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3569 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3570 const __u64 length, const __u64 offset, const __u32 lock_flags,
3571 const bool wait)
3572{
3573 struct smb2_lock_element lock;
3574
3575 lock.Offset = cpu_to_le64(offset);
3576 lock.Length = cpu_to_le64(length);
3577 lock.Flags = cpu_to_le32(lock_flags);
3578 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3579 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3580
3581 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3582}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003583
3584int
3585SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3586 __u8 *lease_key, const __le32 lease_state)
3587{
3588 int rc;
3589 struct smb2_lease_ack *req = NULL;
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003590 int flags = CIFS_OBREAK_OP;
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003591
Joe Perchesf96637b2013-05-04 22:12:25 -05003592 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003593 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003594 if (rc)
3595 return rc;
3596
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003597 if (encryption_required(tcon))
3598 flags |= CIFS_TRANSFORM_REQ;
3599
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07003600 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003601 req->StructureSize = cpu_to_le16(36);
3602 inc_rfc1001_len(req, 12);
3603
3604 memcpy(req->LeaseKey, lease_key, 16);
3605 req->LeaseState = lease_state;
3606
Pavel Shilovsky7fb89862016-10-31 13:49:30 -07003607 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003608 cifs_small_buf_release(req);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003609
3610 if (rc) {
3611 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003612 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003613 }
3614
3615 return rc;
3616}