blob: 4c2eaf05a6a4591c75de03202688be20fc0db43f [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>
Pavel Shilovsky33319142012-09-18 16:20:29 -070036#include <linux/pagemap.h>
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040037#include <linux/xattr.h>
38#include "smb2pdu.h"
39#include "cifsglob.h"
40#include "cifsacl.h"
41#include "cifsproto.h"
42#include "smb2proto.h"
43#include "cifs_unicode.h"
44#include "cifs_debug.h"
45#include "ntlmssp.h"
46#include "smb2status.h"
Pavel Shilovsky09a47072012-09-18 16:20:29 -070047#include "smb2glob.h"
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -070048#include "cifspdu.h"
Steve Frenchceb1b0b2015-09-24 00:52:37 -050049#include "cifs_spnego.h"
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040050
51/*
52 * The following table defines the expected "StructureSize" of SMB2 requests
53 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
54 *
55 * Note that commands are defined in smb2pdu.h in le16 but the array below is
56 * indexed by command in host byte order.
57 */
58static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
59 /* SMB2_NEGOTIATE */ 36,
60 /* SMB2_SESSION_SETUP */ 25,
61 /* SMB2_LOGOFF */ 4,
62 /* SMB2_TREE_CONNECT */ 9,
63 /* SMB2_TREE_DISCONNECT */ 4,
64 /* SMB2_CREATE */ 57,
65 /* SMB2_CLOSE */ 24,
66 /* SMB2_FLUSH */ 24,
67 /* SMB2_READ */ 49,
68 /* SMB2_WRITE */ 49,
69 /* SMB2_LOCK */ 48,
70 /* SMB2_IOCTL */ 57,
71 /* SMB2_CANCEL */ 4,
72 /* SMB2_ECHO */ 4,
73 /* SMB2_QUERY_DIRECTORY */ 33,
74 /* SMB2_CHANGE_NOTIFY */ 32,
75 /* SMB2_QUERY_INFO */ 41,
76 /* SMB2_SET_INFO */ 33,
77 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
78};
79
80
81static void
82smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
83 const struct cifs_tcon *tcon)
84{
85 struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
86 char *temp = (char *)hdr;
87 /* lookup word count ie StructureSize from table */
88 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
89
90 /*
91 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
92 * largest operations (Create)
93 */
94 memset(temp, 0, 256);
95
96 /* Note this is only network field converted to big endian */
97 hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
98 - 4 /* RFC 1001 length field itself not counted */);
99
Steve French373512e2015-12-18 13:05:30 -0600100 hdr->ProtocolId = SMB2_PROTO_NUMBER;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400101 hdr->StructureSize = cpu_to_le16(64);
102 hdr->Command = smb2_cmd;
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100103 if (tcon && tcon->ses && tcon->ses->server) {
104 struct TCP_Server_Info *server = tcon->ses->server;
105
106 spin_lock(&server->req_lock);
107 /* Request up to 2 credits but don't go over the limit. */
Steve French141891f2016-09-23 00:44:16 -0500108 if (server->credits >= server->max_credits)
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100109 hdr->CreditRequest = cpu_to_le16(0);
110 else
111 hdr->CreditRequest = cpu_to_le16(
Steve French141891f2016-09-23 00:44:16 -0500112 min_t(int, server->max_credits -
Ross Lagerwall7d414f32016-09-20 13:37:13 +0100113 server->credits, 2));
114 spin_unlock(&server->req_lock);
115 } else {
116 hdr->CreditRequest = cpu_to_le16(2);
117 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400118 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
119
120 if (!tcon)
121 goto out;
122
Steve French2b80d042013-06-23 18:43:37 -0500123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
Steve French1dc92c42015-05-20 09:32:21 -0500125 if ((tcon->ses) && (tcon->ses->server) &&
Steve French84ceeb92013-06-26 17:52:17 -0500126 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
Steve French2b80d042013-06-23 18:43:37 -0500127 hdr->CreditCharge = cpu_to_le16(1);
128 /* else CreditCharge MBZ */
129
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400130 hdr->TreeId = tcon->tid;
131 /* Uid is not converted */
132 if (tcon->ses)
133 hdr->SessionId = tcon->ses->Suid;
Steve Frenchf87ab882013-06-26 19:14:55 -0500134
135 /*
136 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
137 * to pass the path on the Open SMB prefixed by \\server\share.
138 * Not sure when we would need to do the augmented path (if ever) and
139 * setting this flag breaks the SMB2 open operation since it is
140 * illegal to send an empty path name (without \\server\share prefix)
141 * when the DFS flag is set in the SMB open header. We could
142 * consider setting the flag on all operations other than open
143 * but it is safer to net set it for now.
144 */
145/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
146 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
147
Jeff Layton38d77c52013-05-26 07:01:00 -0400148 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700149 hdr->Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400150out:
151 pdu->StructureSize2 = cpu_to_le16(parmsize);
152 return;
153}
154
155static int
156smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
157{
158 int rc = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400159 struct nls_table *nls_codepage;
160 struct cifs_ses *ses;
161 struct TCP_Server_Info *server;
162
163 /*
164 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
165 * check for tcp and smb session status done differently
166 * for those three - in the calling routine.
167 */
168 if (tcon == NULL)
169 return rc;
170
171 if (smb2_command == SMB2_TREE_CONNECT)
172 return rc;
173
174 if (tcon->tidStatus == CifsExiting) {
175 /*
176 * only tree disconnect, open, and write,
177 * (and ulogoff which does not have tcon)
178 * are allowed as we start force umount.
179 */
180 if ((smb2_command != SMB2_WRITE) &&
181 (smb2_command != SMB2_CREATE) &&
182 (smb2_command != SMB2_TREE_DISCONNECT)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
184 smb2_command);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400185 return -ENODEV;
186 }
187 }
188 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
189 (!tcon->ses->server))
190 return -EIO;
191
192 ses = tcon->ses;
193 server = ses->server;
194
195 /*
196 * Give demultiplex thread up to 10 seconds to reconnect, should be
197 * greater than cifs socket timeout which is 7 seconds
198 */
199 while (server->tcpStatus == CifsNeedReconnect) {
200 /*
201 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
202 * here since they are implicitly done when session drops.
203 */
204 switch (smb2_command) {
205 /*
206 * BB Should we keep oplock break and add flush to exceptions?
207 */
208 case SMB2_TREE_DISCONNECT:
209 case SMB2_CANCEL:
210 case SMB2_CLOSE:
211 case SMB2_OPLOCK_BREAK:
212 return -EAGAIN;
213 }
214
215 wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
217
218 /* are we still trying to reconnect? */
219 if (server->tcpStatus != CifsNeedReconnect)
220 break;
221
222 /*
223 * on "soft" mounts we wait once. Hard mounts keep
224 * retrying until process is killed or server comes
225 * back on-line
226 */
227 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500228 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400229 return -EHOSTDOWN;
230 }
231 }
232
233 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
234 return rc;
235
236 nls_codepage = load_nls_default();
237
238 /*
239 * need to prevent multiple threads trying to simultaneously reconnect
240 * the same SMB session
241 */
242 mutex_lock(&tcon->ses->session_mutex);
243 rc = cifs_negotiate_protocol(0, tcon->ses);
244 if (!rc && tcon->ses->need_reconnect)
245 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
246
247 if (rc || !tcon->need_reconnect) {
248 mutex_unlock(&tcon->ses->session_mutex);
249 goto out;
250 }
251
252 cifs_mark_open_files_invalid(tcon);
Pavel Shilovsky46890ff2016-11-29 11:31:23 -0800253 if (tcon->use_persistent)
254 tcon->need_reopen_files = true;
Steve French52ace1e2016-09-22 19:23:56 -0500255
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400256 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
257 mutex_unlock(&tcon->ses->session_mutex);
Steve French52ace1e2016-09-22 19:23:56 -0500258
Joe Perchesf96637b2013-05-04 22:12:25 -0500259 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400260 if (rc)
261 goto out;
Pavel Shilovsky46890ff2016-11-29 11:31:23 -0800262
263 if (smb2_command != SMB2_INTERNAL_CMD)
264 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
265
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400266 atomic_inc(&tconInfoReconnectCount);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400267out:
268 /*
269 * Check if handle based operation so we know whether we can continue
270 * or not without returning to caller to reset file handle.
271 */
272 /*
273 * BB Is flush done by server on drop of tcp session? Should we special
274 * case it and skip above?
275 */
276 switch (smb2_command) {
277 case SMB2_FLUSH:
278 case SMB2_READ:
279 case SMB2_WRITE:
280 case SMB2_LOCK:
281 case SMB2_IOCTL:
282 case SMB2_QUERY_DIRECTORY:
283 case SMB2_CHANGE_NOTIFY:
284 case SMB2_QUERY_INFO:
285 case SMB2_SET_INFO:
Pavel Shilovsky69d13b62016-11-29 11:30:58 -0800286 rc = -EAGAIN;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400287 }
288 unload_nls(nls_codepage);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400289 return rc;
290}
291
292/*
293 * Allocate and return pointer to an SMB request hdr, and set basic
294 * SMB information in the SMB header. If the return code is zero, this
295 * function must have filled in request_buf pointer.
296 */
297static int
298small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
299 void **request_buf)
300{
301 int rc = 0;
302
303 rc = smb2_reconnect(smb2_command, tcon);
304 if (rc)
305 return rc;
306
307 /* BB eventually switch this to SMB2 specific small buf size */
308 *request_buf = cifs_small_buf_get();
309 if (*request_buf == NULL) {
310 /* BB should we add a retry in here if not a writepage? */
311 return -ENOMEM;
312 }
313
314 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
315
316 if (tcon != NULL) {
317#ifdef CONFIG_CIFS_STATS2
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400318 uint16_t com_code = le16_to_cpu(smb2_command);
319 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400320#endif
321 cifs_stats_inc(&tcon->num_smbs_sent);
322 }
323
324 return rc;
325}
326
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500327#ifdef CONFIG_CIFS_SMB311
328/* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
329#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
330
331
332#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
333#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
334
335static void
336build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
337{
338 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
339 pneg_ctxt->DataLength = cpu_to_le16(38);
340 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
341 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
342 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
343 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
344}
345
346static void
347build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
348{
349 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
350 pneg_ctxt->DataLength = cpu_to_le16(6);
351 pneg_ctxt->CipherCount = cpu_to_le16(2);
352 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
353 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
354}
355
356static void
357assemble_neg_contexts(struct smb2_negotiate_req *req)
358{
359
360 /* +4 is to account for the RFC1001 len field */
361 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
362
363 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
364 /* Add 2 to size to round to 8 byte boundary */
365 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
366 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
367 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
368 req->NegotiateContextCount = cpu_to_le16(2);
Steve Frenchf2d395b2017-09-18 18:18:45 -0500369 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context)
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500370 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
371}
372#else
373static void assemble_neg_contexts(struct smb2_negotiate_req *req)
374{
375 return;
376}
377#endif /* SMB311 */
378
379
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400380/*
381 *
382 * SMB2 Worker functions follow:
383 *
384 * The general structure of the worker functions is:
385 * 1) Call smb2_init (assembles SMB2 header)
386 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
387 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
388 * 4) Decode SMB2 command specific fields in the fixed length area
389 * 5) Decode variable length data area (if any for this SMB2 command type)
390 * 6) Call free smb buffer
391 * 7) return
392 *
393 */
394
395int
396SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
397{
398 struct smb2_negotiate_req *req;
399 struct smb2_negotiate_rsp *rsp;
400 struct kvec iov[1];
401 int rc = 0;
402 int resp_buftype;
Jeff Layton3534b852013-05-24 07:41:01 -0400403 struct TCP_Server_Info *server = ses->server;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400404 int blob_offset, blob_length;
405 char *security_blob;
406 int flags = CIFS_NEG_OP;
407
Joe Perchesf96637b2013-05-04 22:12:25 -0500408 cifs_dbg(FYI, "Negotiate protocol\n");
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400409
Jeff Layton3534b852013-05-24 07:41:01 -0400410 if (!server) {
411 WARN(1, "%s: server is NULL!\n", __func__);
412 return -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400413 }
414
415 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
416 if (rc)
417 return rc;
418
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400419 req->hdr.SessionId = 0;
420
Steve Frenche4aa25e2012-10-01 12:26:22 -0500421 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400422
Steve Frenche4aa25e2012-10-01 12:26:22 -0500423 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
424 inc_rfc1001_len(req, 2);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400425
426 /* only one of SMB2 signing flags may be set in SMB2 request */
Jeff Layton38d77c52013-05-26 07:01:00 -0400427 if (ses->sign)
Steve French9cd2e622013-06-12 19:59:03 -0500428 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400429 else if (global_secflags & CIFSSEC_MAY_SIGN)
Steve French9cd2e622013-06-12 19:59:03 -0500430 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
Jeff Layton38d77c52013-05-26 07:01:00 -0400431 else
432 req->SecurityMode = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400433
Steve Frenche4aa25e2012-10-01 12:26:22 -0500434 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400435
Steve French3c5f9be12014-05-13 13:37:45 -0700436 /* ClientGUID must be zero for SMB2.02 dialect */
437 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
438 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500439 else {
Steve French3c5f9be12014-05-13 13:37:45 -0700440 memcpy(req->ClientGUID, server->client_guid,
441 SMB2_CLIENT_GUID_SIZE);
Steve Frenchebb3a9d2015-06-18 04:49:47 -0500442 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
443 assemble_neg_contexts(req);
444 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400445 iov[0].iov_base = (char *)req;
446 /* 4 for rfc1002 length field */
447 iov[0].iov_len = get_rfc1002_length(req) + 4;
448
449 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
450
451 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
452 /*
453 * No tcon so can't do
454 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
455 */
456 if (rc != 0)
457 goto neg_exit;
458
Joe Perchesf96637b2013-05-04 22:12:25 -0500459 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400460
Steve Frenche4aa25e2012-10-01 12:26:22 -0500461 /* BB we may eventually want to match the negotiated vs. requested
462 dialect, even though we are only requesting one at a time */
463 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500464 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500465 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500466 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
Steve Frenche4aa25e2012-10-01 12:26:22 -0500467 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
Joe Perchesf96637b2013-05-04 22:12:25 -0500468 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
Steve French20b6d8b2013-06-12 22:48:41 -0500469 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
470 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
Steve French5f7fbf72014-12-17 22:52:58 -0600471#ifdef CONFIG_CIFS_SMB311
472 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
473 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
474#endif /* SMB311 */
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400475 else {
Steve Frenchf799d622015-06-18 05:07:52 -0500476 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
Joe Perchesf96637b2013-05-04 22:12:25 -0500477 le16_to_cpu(rsp->DialectRevision));
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400478 rc = -EIO;
479 goto neg_exit;
480 }
481 server->dialect = le16_to_cpu(rsp->DialectRevision);
482
Jeff Laytone598d1d82013-05-26 07:00:59 -0400483 /* SMB2 only has an extended negflavor */
484 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Pavel Shilovsky2365c4e2014-02-14 13:31:02 +0400485 /* set it to the maximum buffer size value we can send with 1 credit */
486 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
487 SMB2_MAX_BUFFER_SIZE);
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400488 server->max_read = le32_to_cpu(rsp->MaxReadSize);
489 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
490 /* BB Do we need to validate the SecurityMode? */
491 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
492 server->capabilities = le32_to_cpu(rsp->Capabilities);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400493 /* Internal types */
494 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400495
496 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
497 &rsp->hdr);
Steve French5d875cc2013-06-25 15:33:41 -0500498 /*
499 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
500 * for us will be
501 * ses->sectype = RawNTLMSSP;
502 * but for time being this is our only auth choice so doesn't matter.
503 * We just found a server which sets blob length to zero expecting raw.
504 */
505 if (blob_length == 0)
506 cifs_dbg(FYI, "missing security blob on negprot\n");
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700507
Jeff Layton38d77c52013-05-26 07:01:00 -0400508 rc = cifs_enable_signing(server, ses->sign);
Jeff Layton9ddec562013-05-26 07:00:58 -0400509 if (rc)
510 goto neg_exit;
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500511 if (blob_length) {
Steve Frenchebdd2072014-10-20 12:48:23 -0500512 rc = decode_negTokenInit(security_blob, blob_length, server);
Steve Frenchceb1b0b2015-09-24 00:52:37 -0500513 if (rc == 1)
514 rc = 0;
515 else if (rc == 0)
516 rc = -EIO;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400517 }
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400518neg_exit:
519 free_rsp_buf(resp_buftype, rsp);
520 return rc;
521}
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400522
Steve Frenchff1c0382013-11-19 23:44:46 -0600523int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
524{
525 int rc = 0;
526 struct validate_negotiate_info_req vneg_inbuf;
527 struct validate_negotiate_info_rsp *pneg_rsp;
528 u32 rsplen;
529
530 cifs_dbg(FYI, "validate negotiate\n");
531
532 /*
533 * validation ioctl must be signed, so no point sending this if we
Steve French0e1b85a2017-09-20 19:57:18 -0500534 * can not sign it (ie are not known user). Even if signing is not
535 * required (enabled but not negotiated), in those cases we selectively
Steve Frenchff1c0382013-11-19 23:44:46 -0600536 * sign just this, the first and only signed request on a connection.
Steve French0e1b85a2017-09-20 19:57:18 -0500537 * Having validation of negotiate info helps reduce attack vectors.
Steve Frenchff1c0382013-11-19 23:44:46 -0600538 */
Steve French0e1b85a2017-09-20 19:57:18 -0500539 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
Steve Frenchff1c0382013-11-19 23:44:46 -0600540 return 0; /* validation requires signing */
541
Steve French0e1b85a2017-09-20 19:57:18 -0500542 if (tcon->ses->user_name == NULL) {
543 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
544 return 0; /* validation requires signing */
545 }
546
547 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
548 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
549
Steve Frenchff1c0382013-11-19 23:44:46 -0600550 vneg_inbuf.Capabilities =
551 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100552 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
553 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600554
555 if (tcon->ses->sign)
556 vneg_inbuf.SecurityMode =
557 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
558 else if (global_secflags & CIFSSEC_MAY_SIGN)
559 vneg_inbuf.SecurityMode =
560 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
561 else
562 vneg_inbuf.SecurityMode = 0;
563
564 vneg_inbuf.DialectCount = cpu_to_le16(1);
565 vneg_inbuf.Dialects[0] =
566 cpu_to_le16(tcon->ses->server->vals->protocol_id);
567
568 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
569 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
570 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
571 (char **)&pneg_rsp, &rsplen);
572
573 if (rc != 0) {
574 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
575 return -EIO;
576 }
577
578 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
Steve French8dd4e3f2017-05-03 21:12:20 -0500579 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
580 rsplen);
581
582 /* relax check since Mac returns max bufsize allowed on ioctl */
583 if (rsplen > CIFSMaxBufSize)
584 return -EIO;
Steve Frenchff1c0382013-11-19 23:44:46 -0600585 }
586
587 /* check validate negotiate info response matches what we got earlier */
Daniel N Petterssonf59eda12018-01-11 16:00:12 +0100588 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
Steve Frenchff1c0382013-11-19 23:44:46 -0600589 goto vneg_out;
590
591 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
592 goto vneg_out;
593
594 /* do not validate server guid because not saved at negprot time yet */
595
596 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
597 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
598 goto vneg_out;
599
600 /* validate negotiate successful */
601 cifs_dbg(FYI, "validate negotiate info successful\n");
602 return 0;
603
604vneg_out:
605 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
606 return -EIO;
607}
608
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100609struct SMB2_sess_data {
610 unsigned int xid;
611 struct cifs_ses *ses;
612 struct nls_table *nls_cp;
613 void (*func)(struct SMB2_sess_data *);
614 int result;
615 u64 previous_session;
616
617 /* we will send the SMB in three pieces:
618 * a fixed length beginning part, an optional
619 * SPNEGO blob (which can be zero length), and a
620 * last part which will include the strings
621 * and rest of bcc area. This allows us to avoid
622 * a large buffer 17K allocation
623 */
624 int buf0_type;
625 struct kvec iov[2];
626};
627
628static int
629SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
630{
631 int rc;
632 struct cifs_ses *ses = sess_data->ses;
633 struct smb2_sess_setup_req *req;
634 struct TCP_Server_Info *server = ses->server;
635
636 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
637 if (rc)
638 return rc;
639
640 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
641
642 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
643 req->PreviousSessionId = sess_data->previous_session;
644
645 req->Flags = 0; /* MBZ */
646 /* to enable echos and oplocks */
647 req->hdr.CreditRequest = cpu_to_le16(3);
648
649 /* only one of SMB2 signing flags may be set in SMB2 request */
650 if (server->sign)
651 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
652 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
653 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
654 else
655 req->SecurityMode = 0;
656
657 req->Capabilities = 0;
658 req->Channel = 0; /* MBZ */
659
660 sess_data->iov[0].iov_base = (char *)req;
661 /* 4 for rfc1002 length field and 1 for pad */
662 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
663 /*
664 * This variable will be used to clear the buffer
665 * allocated above in case of any error in the calling function.
666 */
667 sess_data->buf0_type = CIFS_SMALL_BUFFER;
668
669 return 0;
670}
671
672static void
673SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
674{
675 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
676 sess_data->buf0_type = CIFS_NO_BUFFER;
677}
678
679static int
680SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
681{
682 int rc;
683 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
684
685 /* Testing shows that buffer offset must be at location of Buffer[0] */
686 req->SecurityBufferOffset =
687 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
688 1 /* pad */ - 4 /* rfc1001 len */);
689 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
690
691 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
692
693 /* BB add code to build os and lm fields */
694
695 rc = SendReceive2(sess_data->xid, sess_data->ses,
696 sess_data->iov, 2,
697 &sess_data->buf0_type,
698 CIFS_LOG_ERROR | CIFS_NEG_OP);
699
700 return rc;
701}
702
703static int
704SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
705{
706 int rc = 0;
707 struct cifs_ses *ses = sess_data->ses;
708
709 mutex_lock(&ses->server->srv_mutex);
710 if (ses->server->sign && ses->server->ops->generate_signingkey) {
711 rc = ses->server->ops->generate_signingkey(ses);
712 kfree(ses->auth_key.response);
713 ses->auth_key.response = NULL;
714 if (rc) {
715 cifs_dbg(FYI,
716 "SMB3 session key generation failed\n");
717 mutex_unlock(&ses->server->srv_mutex);
718 goto keygen_exit;
719 }
720 }
721 if (!ses->server->session_estab) {
722 ses->server->sequence_number = 0x2;
723 ses->server->session_estab = true;
724 }
725 mutex_unlock(&ses->server->srv_mutex);
726
727 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
728 spin_lock(&GlobalMid_Lock);
729 ses->status = CifsGood;
730 ses->need_reconnect = false;
731 spin_unlock(&GlobalMid_Lock);
732
733keygen_exit:
734 if (!ses->server->sign) {
735 kfree(ses->auth_key.response);
736 ses->auth_key.response = NULL;
737 }
738 return rc;
739}
740
741#ifdef CONFIG_CIFS_UPCALL
742static void
743SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
744{
745 int rc;
746 struct cifs_ses *ses = sess_data->ses;
747 struct cifs_spnego_msg *msg;
748 struct key *spnego_key = NULL;
749 struct smb2_sess_setup_rsp *rsp = NULL;
750
751 rc = SMB2_sess_alloc_buffer(sess_data);
752 if (rc)
753 goto out;
754
755 spnego_key = cifs_get_spnego_key(ses);
756 if (IS_ERR(spnego_key)) {
757 rc = PTR_ERR(spnego_key);
758 spnego_key = NULL;
759 goto out;
760 }
761
762 msg = spnego_key->payload.data[0];
763 /*
764 * check version field to make sure that cifs.upcall is
765 * sending us a response in an expected form
766 */
767 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
768 cifs_dbg(VFS,
769 "bad cifs.upcall version. Expected %d got %d",
770 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
771 rc = -EKEYREJECTED;
772 goto out_put_spnego_key;
773 }
774
775 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
776 GFP_KERNEL);
777 if (!ses->auth_key.response) {
778 cifs_dbg(VFS,
779 "Kerberos can't allocate (%u bytes) memory",
780 msg->sesskey_len);
781 rc = -ENOMEM;
782 goto out_put_spnego_key;
783 }
784 ses->auth_key.len = msg->sesskey_len;
785
786 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
787 sess_data->iov[1].iov_len = msg->secblob_len;
788
789 rc = SMB2_sess_sendreceive(sess_data);
790 if (rc)
791 goto out_put_spnego_key;
792
793 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
794 ses->Suid = rsp->hdr.SessionId;
795
796 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
797 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
798 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
799
800 rc = SMB2_sess_establish_session(sess_data);
801out_put_spnego_key:
802 key_invalidate(spnego_key);
803 key_put(spnego_key);
804out:
805 sess_data->result = rc;
806 sess_data->func = NULL;
807 SMB2_sess_free_buffer(sess_data);
808}
809#else
810static void
811SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
812{
813 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
814 sess_data->result = -EOPNOTSUPP;
815 sess_data->func = NULL;
816}
817#endif
818
Sachin Prabhu166cea42016-10-07 19:11:22 +0100819static void
820SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
821
822static void
823SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
824{
825 int rc;
826 struct cifs_ses *ses = sess_data->ses;
827 struct smb2_sess_setup_rsp *rsp = NULL;
828 char *ntlmssp_blob = NULL;
829 bool use_spnego = false; /* else use raw ntlmssp */
830 u16 blob_length = 0;
831
832 /*
833 * If memory allocation is successful, caller of this function
834 * frees it.
835 */
836 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
837 if (!ses->ntlmssp) {
838 rc = -ENOMEM;
839 goto out_err;
840 }
841 ses->ntlmssp->sesskey_per_smbsess = true;
842
843 rc = SMB2_sess_alloc_buffer(sess_data);
844 if (rc)
845 goto out_err;
846
847 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
848 GFP_KERNEL);
849 if (ntlmssp_blob == NULL) {
850 rc = -ENOMEM;
851 goto out;
852 }
853
854 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
855 if (use_spnego) {
856 /* BB eventually need to add this */
857 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
858 rc = -EOPNOTSUPP;
859 goto out;
860 } else {
861 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
862 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
863 }
864 sess_data->iov[1].iov_base = ntlmssp_blob;
865 sess_data->iov[1].iov_len = blob_length;
866
867 rc = SMB2_sess_sendreceive(sess_data);
868 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
869
870 /* If true, rc here is expected and not an error */
871 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
872 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
873 rc = 0;
874
875 if (rc)
876 goto out;
877
878 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
879 le16_to_cpu(rsp->SecurityBufferOffset)) {
880 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
881 le16_to_cpu(rsp->SecurityBufferOffset));
882 rc = -EIO;
883 goto out;
884 }
885 rc = decode_ntlmssp_challenge(rsp->Buffer,
886 le16_to_cpu(rsp->SecurityBufferLength), ses);
887 if (rc)
888 goto out;
889
890 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
891
892
893 ses->Suid = rsp->hdr.SessionId;
894 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
895 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
896 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
897
898out:
899 kfree(ntlmssp_blob);
900 SMB2_sess_free_buffer(sess_data);
901 if (!rc) {
902 sess_data->result = 0;
903 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
904 return;
905 }
906out_err:
907 kfree(ses->ntlmssp);
908 ses->ntlmssp = NULL;
909 sess_data->result = rc;
910 sess_data->func = NULL;
911}
912
913static void
914SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
915{
916 int rc;
917 struct cifs_ses *ses = sess_data->ses;
918 struct smb2_sess_setup_req *req;
919 struct smb2_sess_setup_rsp *rsp = NULL;
920 unsigned char *ntlmssp_blob = NULL;
921 bool use_spnego = false; /* else use raw ntlmssp */
922 u16 blob_length = 0;
923
924 rc = SMB2_sess_alloc_buffer(sess_data);
925 if (rc)
926 goto out;
927
928 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
929 req->hdr.SessionId = ses->Suid;
930
931 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
932 sess_data->nls_cp);
933 if (rc) {
934 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
935 goto out;
936 }
937
938 if (use_spnego) {
939 /* BB eventually need to add this */
940 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
941 rc = -EOPNOTSUPP;
942 goto out;
943 }
944 sess_data->iov[1].iov_base = ntlmssp_blob;
945 sess_data->iov[1].iov_len = blob_length;
946
947 rc = SMB2_sess_sendreceive(sess_data);
948 if (rc)
949 goto out;
950
951 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
952
953 ses->Suid = rsp->hdr.SessionId;
954 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
955 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
956 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
957
958 rc = SMB2_sess_establish_session(sess_data);
959out:
960 kfree(ntlmssp_blob);
961 SMB2_sess_free_buffer(sess_data);
962 kfree(ses->ntlmssp);
963 ses->ntlmssp = NULL;
964 sess_data->result = rc;
965 sess_data->func = NULL;
966}
967
968static int
969SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
970{
971 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
972 ses->sectype = RawNTLMSSP;
973
974 switch (ses->sectype) {
975 case Kerberos:
976 sess_data->func = SMB2_auth_kerberos;
977 break;
978 case RawNTLMSSP:
979 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
980 break;
981 default:
982 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
983 return -EOPNOTSUPP;
984 }
985
986 return 0;
987}
988
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400989int
990SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
991 const struct nls_table *nls_cp)
992{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400993 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -0400994 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100995 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400996
Joe Perchesf96637b2013-05-04 22:12:25 -0500997 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400998
Jeff Layton3534b852013-05-24 07:41:01 -0400999 if (!server) {
1000 WARN(1, "%s: server is NULL!\n", __func__);
1001 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001002 }
1003
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001004 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1005 if (!sess_data)
1006 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001007
1008 rc = SMB2_select_sec(ses, sess_data);
1009 if (rc)
1010 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001011 sess_data->xid = xid;
1012 sess_data->ses = ses;
1013 sess_data->buf0_type = CIFS_NO_BUFFER;
1014 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001015
Sachin Prabhu166cea42016-10-07 19:11:22 +01001016 while (sess_data->func)
1017 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001018
Steve Frenchdf1be202017-09-19 18:40:03 -05001019 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1020 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001021 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001022out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001023 kfree(sess_data);
1024 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001025}
1026
1027int
1028SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1029{
1030 struct smb2_logoff_req *req; /* response is also trivial struct */
1031 int rc = 0;
1032 struct TCP_Server_Info *server;
1033
Joe Perchesf96637b2013-05-04 22:12:25 -05001034 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001035
1036 if (ses && (ses->server))
1037 server = ses->server;
1038 else
1039 return -EIO;
1040
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001041 /* no need to send SMB logoff if uid already closed due to reconnect */
1042 if (ses->need_reconnect)
1043 goto smb2_session_already_dead;
1044
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001045 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1046 if (rc)
1047 return rc;
1048
1049 /* since no tcon, smb2_init can not do this, so do here */
1050 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001051 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001052 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001053
1054 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1055 /*
1056 * No tcon so can't do
1057 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1058 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001059
1060smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001061 return rc;
1062}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001063
1064static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1065{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001066 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001067}
1068
1069#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1070
Steve Frenchde9f68d2013-11-15 11:26:24 -06001071/* These are similar values to what Windows uses */
1072static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1073{
1074 tcon->max_chunks = 256;
1075 tcon->max_bytes_chunk = 1048576;
1076 tcon->max_bytes_copy = 16777216;
1077}
1078
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001079int
1080SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1081 struct cifs_tcon *tcon, const struct nls_table *cp)
1082{
1083 struct smb2_tree_connect_req *req;
1084 struct smb2_tree_connect_rsp *rsp = NULL;
1085 struct kvec iov[2];
1086 int rc = 0;
1087 int resp_buftype;
1088 int unc_path_len;
1089 struct TCP_Server_Info *server;
1090 __le16 *unc_path = NULL;
1091
Joe Perchesf96637b2013-05-04 22:12:25 -05001092 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001093
1094 if ((ses->server) && tree)
1095 server = ses->server;
1096 else
1097 return -EIO;
1098
Steve Frenchff9f84b2015-09-26 09:48:58 -05001099 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001100 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1101 cifs_dbg(VFS, "encryption requested but no server support");
1102 return -EOPNOTSUPP;
1103 }
1104
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001105 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1106 if (unc_path == NULL)
1107 return -ENOMEM;
1108
1109 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1110 unc_path_len *= 2;
1111 if (unc_path_len < 2) {
1112 kfree(unc_path);
1113 return -EINVAL;
1114 }
1115
Jan-Marek Glogowski8446cb12017-02-20 12:25:58 +01001116 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1117 if (tcon)
1118 tcon->tid = 0;
1119
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001120 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1121 if (rc) {
1122 kfree(unc_path);
1123 return rc;
1124 }
1125
1126 if (tcon == NULL) {
1127 /* since no tcon, smb2_init can not do this, so do here */
1128 req->hdr.SessionId = ses->Suid;
1129 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1130 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1131 }
1132
1133 iov[0].iov_base = (char *)req;
1134 /* 4 for rfc1002 length field and 1 for pad */
1135 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1136
1137 /* Testing shows that buffer offset must be at location of Buffer[0] */
1138 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1139 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1140 req->PathLength = cpu_to_le16(unc_path_len - 2);
1141 iov[1].iov_base = unc_path;
1142 iov[1].iov_len = unc_path_len;
1143
1144 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1145
1146 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1147 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1148
1149 if (rc != 0) {
1150 if (tcon) {
1151 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1152 tcon->need_reconnect = true;
1153 }
1154 goto tcon_error_exit;
1155 }
1156
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001157 if (tcon == NULL) {
1158 ses->ipc_tid = rsp->hdr.TreeId;
1159 goto tcon_exit;
1160 }
1161
1162 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001163 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001164 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1165 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001166 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001167 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1168 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001169 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001170 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001171 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001172 rc = -EOPNOTSUPP;
1173 goto tcon_error_exit;
1174 }
1175
1176 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001177 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001178 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1179 tcon->tidStatus = CifsGood;
1180 tcon->need_reconnect = false;
1181 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001182 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001183
1184 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1185 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001186 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001187 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001188 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1189 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001190 if (tcon->ses->server->ops->validate_negotiate)
1191 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001192tcon_exit:
1193 free_rsp_buf(resp_buftype, rsp);
1194 kfree(unc_path);
1195 return rc;
1196
1197tcon_error_exit:
1198 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001199 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001200 }
1201 goto tcon_exit;
1202}
1203
1204int
1205SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1206{
1207 struct smb2_tree_disconnect_req *req; /* response is trivial */
1208 int rc = 0;
1209 struct TCP_Server_Info *server;
1210 struct cifs_ses *ses = tcon->ses;
1211
Joe Perchesf96637b2013-05-04 22:12:25 -05001212 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001213
1214 if (ses && (ses->server))
1215 server = ses->server;
1216 else
1217 return -EIO;
1218
1219 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1220 return 0;
1221
1222 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1223 if (rc)
1224 return rc;
1225
1226 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1227 if (rc)
1228 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1229
1230 return rc;
1231}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001232
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001233
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001234static struct create_durable *
1235create_durable_buf(void)
1236{
1237 struct create_durable *buf;
1238
1239 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1240 if (!buf)
1241 return NULL;
1242
1243 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001244 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001245 buf->ccontext.DataLength = cpu_to_le32(16);
1246 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1247 (struct create_durable, Name));
1248 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001249 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001250 buf->Name[0] = 'D';
1251 buf->Name[1] = 'H';
1252 buf->Name[2] = 'n';
1253 buf->Name[3] = 'Q';
1254 return buf;
1255}
1256
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001257static struct create_durable *
1258create_reconnect_durable_buf(struct cifs_fid *fid)
1259{
1260 struct create_durable *buf;
1261
1262 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1263 if (!buf)
1264 return NULL;
1265
1266 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1267 (struct create_durable, Data));
1268 buf->ccontext.DataLength = cpu_to_le32(16);
1269 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1270 (struct create_durable, Name));
1271 buf->ccontext.NameLength = cpu_to_le16(4);
1272 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1273 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001274 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001275 buf->Name[0] = 'D';
1276 buf->Name[1] = 'H';
1277 buf->Name[2] = 'n';
1278 buf->Name[3] = 'C';
1279 return buf;
1280}
1281
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001282static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001283parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1284 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001285{
1286 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001287 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001288 unsigned int next;
1289 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001290 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001291
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001292 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001293 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001294 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001295 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001296 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001297 if (le16_to_cpu(cc->NameLength) == 4 &&
1298 strncmp(name, "RqLs", 4) == 0)
1299 return server->ops->parse_lease_buf(cc, epoch);
1300
1301 next = le32_to_cpu(cc->Next);
1302 if (!next)
1303 break;
1304 remaining -= next;
1305 cc = (struct create_context *)((char *)cc + next);
1306 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001307
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001308 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001309}
1310
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001311static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001312add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1313 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001314{
1315 struct smb2_create_req *req = iov[0].iov_base;
1316 unsigned int num = *num_iovec;
1317
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001318 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001319 if (iov[num].iov_base == NULL)
1320 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001321 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001322 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1323 if (!req->CreateContextsOffset)
1324 req->CreateContextsOffset = cpu_to_le32(
1325 sizeof(struct smb2_create_req) - 4 +
1326 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001327 le32_add_cpu(&req->CreateContextsLength,
1328 server->vals->create_lease_size);
1329 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001330 *num_iovec = num + 1;
1331 return 0;
1332}
1333
Steve Frenchb56eae42015-11-03 09:26:27 -06001334static struct create_durable_v2 *
1335create_durable_v2_buf(struct cifs_fid *pfid)
1336{
1337 struct create_durable_v2 *buf;
1338
1339 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1340 if (!buf)
1341 return NULL;
1342
1343 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1344 (struct create_durable_v2, dcontext));
1345 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1346 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1347 (struct create_durable_v2, Name));
1348 buf->ccontext.NameLength = cpu_to_le16(4);
1349
1350 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1351 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001352 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001353 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1354
1355 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1356 buf->Name[0] = 'D';
1357 buf->Name[1] = 'H';
1358 buf->Name[2] = '2';
1359 buf->Name[3] = 'Q';
1360 return buf;
1361}
1362
1363static struct create_durable_handle_reconnect_v2 *
1364create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1365{
1366 struct create_durable_handle_reconnect_v2 *buf;
1367
1368 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1369 GFP_KERNEL);
1370 if (!buf)
1371 return NULL;
1372
1373 buf->ccontext.DataOffset =
1374 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1375 dcontext));
1376 buf->ccontext.DataLength =
1377 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1378 buf->ccontext.NameOffset =
1379 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1380 Name));
1381 buf->ccontext.NameLength = cpu_to_le16(4);
1382
1383 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1384 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1385 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1386 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1387
1388 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1389 buf->Name[0] = 'D';
1390 buf->Name[1] = 'H';
1391 buf->Name[2] = '2';
1392 buf->Name[3] = 'C';
1393 return buf;
1394}
1395
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001396static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001397add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001398 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001399{
1400 struct smb2_create_req *req = iov[0].iov_base;
1401 unsigned int num = *num_iovec;
1402
Steve Frenchb56eae42015-11-03 09:26:27 -06001403 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1404 if (iov[num].iov_base == NULL)
1405 return -ENOMEM;
1406 iov[num].iov_len = sizeof(struct create_durable_v2);
1407 if (!req->CreateContextsOffset)
1408 req->CreateContextsOffset =
1409 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1410 iov[1].iov_len);
1411 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1412 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1413 *num_iovec = num + 1;
1414 return 0;
1415}
1416
1417static int
1418add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1419 struct cifs_open_parms *oparms)
1420{
1421 struct smb2_create_req *req = iov[0].iov_base;
1422 unsigned int num = *num_iovec;
1423
1424 /* indicate that we don't need to relock the file */
1425 oparms->reconnect = false;
1426
1427 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1428 if (iov[num].iov_base == NULL)
1429 return -ENOMEM;
1430 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1431 if (!req->CreateContextsOffset)
1432 req->CreateContextsOffset =
1433 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1434 iov[1].iov_len);
1435 le32_add_cpu(&req->CreateContextsLength,
1436 sizeof(struct create_durable_handle_reconnect_v2));
1437 inc_rfc1001_len(&req->hdr,
1438 sizeof(struct create_durable_handle_reconnect_v2));
1439 *num_iovec = num + 1;
1440 return 0;
1441}
1442
1443static int
1444add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1445 struct cifs_open_parms *oparms, bool use_persistent)
1446{
1447 struct smb2_create_req *req = iov[0].iov_base;
1448 unsigned int num = *num_iovec;
1449
1450 if (use_persistent) {
1451 if (oparms->reconnect)
1452 return add_durable_reconnect_v2_context(iov, num_iovec,
1453 oparms);
1454 else
1455 return add_durable_v2_context(iov, num_iovec, oparms);
1456 }
1457
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001458 if (oparms->reconnect) {
1459 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1460 /* indicate that we don't need to relock the file */
1461 oparms->reconnect = false;
1462 } else
1463 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001464 if (iov[num].iov_base == NULL)
1465 return -ENOMEM;
1466 iov[num].iov_len = sizeof(struct create_durable);
1467 if (!req->CreateContextsOffset)
1468 req->CreateContextsOffset =
1469 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1470 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001471 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001472 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1473 *num_iovec = num + 1;
1474 return 0;
1475}
1476
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001477int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001478SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001479 __u8 *oplock, struct smb2_file_all_info *buf,
1480 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001481{
1482 struct smb2_create_req *req;
1483 struct smb2_create_rsp *rsp;
1484 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001485 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001486 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001487 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001488 int resp_buftype;
1489 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001490 __le16 *copy_path = NULL;
1491 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001492 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001493 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001494 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001495 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001496
Joe Perchesf96637b2013-05-04 22:12:25 -05001497 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001498
1499 if (ses && (ses->server))
1500 server = ses->server;
1501 else
1502 return -EIO;
1503
1504 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1505 if (rc)
1506 return rc;
1507
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001508 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001509 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001510 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1511 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001512
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001513 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001514 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001515 /* File attributes ignored on open (used in create though) */
1516 req->FileAttributes = cpu_to_le32(file_attributes);
1517 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001518 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1519 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001520 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001521 /* do not count rfc1001 len field */
1522 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001523
1524 iov[0].iov_base = (char *)req;
1525 /* 4 for rfc1002 length field */
1526 iov[0].iov_len = get_rfc1002_length(req) + 4;
1527
1528 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001529 req->NameLength = cpu_to_le16(uni_path_len - 2);
1530 /* -1 since last byte is buf[0] which is sent below (path) */
1531 iov[0].iov_len--;
1532 if (uni_path_len % 8 != 0) {
1533 copy_size = uni_path_len / 8 * 8;
1534 if (copy_size < uni_path_len)
1535 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001536
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001537 copy_path = kzalloc(copy_size, GFP_KERNEL);
1538 if (!copy_path)
1539 return -ENOMEM;
1540 memcpy((char *)copy_path, (const char *)path,
1541 uni_path_len);
1542 uni_path_len = copy_size;
1543 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001544 }
1545
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001546 iov[1].iov_len = uni_path_len;
1547 iov[1].iov_base = path;
1548 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1549 inc_rfc1001_len(req, uni_path_len - 1);
1550
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001551 if (!server->oplocks)
1552 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1553
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001554 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001555 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1556 req->RequestedOplockLevel = *oplock;
1557 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001558 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001559 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001560 cifs_small_buf_release(req);
1561 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001562 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001563 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001564 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001565 }
1566
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001567 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1568 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001569 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001570 struct create_context *ccontext =
1571 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001572 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001573 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001574 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001575
1576 rc = add_durable_context(iov, &num_iovecs, oparms,
1577 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001578 if (rc) {
1579 cifs_small_buf_release(req);
1580 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001581 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001582 return rc;
1583 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001584 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001585 }
1586
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001587 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1588 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1589
1590 if (rc != 0) {
1591 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001592 if (err_buf)
1593 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1594 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001595 goto creat_exit;
1596 }
1597
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001598 oparms->fid->persistent_fid = rsp->PersistentFileId;
1599 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001600
1601 if (buf) {
1602 memcpy(buf, &rsp->CreationTime, 32);
1603 buf->AllocationSize = rsp->AllocationSize;
1604 buf->EndOfFile = rsp->EndofFile;
1605 buf->Attributes = rsp->FileAttributes;
1606 buf->NumberOfLinks = cpu_to_le32(1);
1607 buf->DeletePending = 0;
1608 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001609
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001610 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001611 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001612 else
1613 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001614creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001615 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001616 kfree(lc_buf);
1617 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001618 free_rsp_buf(resp_buftype, rsp);
1619 return rc;
1620}
1621
Steve French4a72daf2013-06-25 00:20:49 -05001622/*
1623 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1624 */
1625int
1626SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1627 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1628 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1629{
1630 struct smb2_ioctl_req *req;
1631 struct smb2_ioctl_rsp *rsp;
1632 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001633 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001634 struct kvec iov[2];
1635 int resp_buftype;
1636 int num_iovecs;
1637 int rc = 0;
1638
1639 cifs_dbg(FYI, "SMB2 IOCTL\n");
1640
Steve French3d1a3742014-08-11 21:05:25 -05001641 if (out_data != NULL)
1642 *out_data = NULL;
1643
Steve French4a72daf2013-06-25 00:20:49 -05001644 /* zero out returned data len, in case of error */
1645 if (plen)
1646 *plen = 0;
1647
Steve French8e353102015-03-26 19:47:02 -05001648 if (tcon)
1649 ses = tcon->ses;
1650 else
1651 return -EIO;
1652
Steve French4a72daf2013-06-25 00:20:49 -05001653 if (ses && (ses->server))
1654 server = ses->server;
1655 else
1656 return -EIO;
1657
1658 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1659 if (rc)
1660 return rc;
1661
1662 req->CtlCode = cpu_to_le32(opcode);
1663 req->PersistentFileId = persistent_fid;
1664 req->VolatileFileId = volatile_fid;
1665
1666 if (indatalen) {
1667 req->InputCount = cpu_to_le32(indatalen);
1668 /* do not set InputOffset if no input data */
1669 req->InputOffset =
1670 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1671 iov[1].iov_base = in_data;
1672 iov[1].iov_len = indatalen;
1673 num_iovecs = 2;
1674 } else
1675 num_iovecs = 1;
1676
1677 req->OutputOffset = 0;
1678 req->OutputCount = 0; /* MBZ */
1679
1680 /*
1681 * Could increase MaxOutputResponse, but that would require more
1682 * than one credit. Windows typically sets this smaller, but for some
1683 * ioctls it may be useful to allow server to send more. No point
1684 * limiting what the server can send as long as fits in one credit
Steve French8dd4e3f2017-05-03 21:12:20 -05001685 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1686 * (by default, note that it can be overridden to make max larger)
1687 * in responses (except for read responses which can be bigger.
1688 * We may want to bump this limit up
Steve French4a72daf2013-06-25 00:20:49 -05001689 */
Steve French8dd4e3f2017-05-03 21:12:20 -05001690 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
Steve French4a72daf2013-06-25 00:20:49 -05001691
1692 if (is_fsctl)
1693 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1694 else
1695 req->Flags = 0;
1696
1697 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001698
Steve French7ff8d452013-10-14 00:44:19 -05001699 /*
1700 * If no input data, the size of ioctl struct in
1701 * protocol spec still includes a 1 byte data buffer,
1702 * but if input data passed to ioctl, we do not
1703 * want to double count this, so we do not send
1704 * the dummy one byte of data in iovec[0] if sending
1705 * input data (in iovec[1]). We also must add 4 bytes
1706 * in first iovec to allow for rfc1002 length field.
1707 */
1708
1709 if (indatalen) {
1710 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1711 inc_rfc1001_len(req, indatalen - 1);
1712 } else
1713 iov[0].iov_len = get_rfc1002_length(req) + 4;
1714
Steve Frenchfca16f92017-10-25 15:58:31 -05001715 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1716 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1717 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Steve French4a72daf2013-06-25 00:20:49 -05001718
1719 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1720 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1721
Steve French9bf0c9c2013-11-16 18:05:28 -06001722 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001723 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001724 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001725 } else if (rc == -EINVAL) {
1726 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1727 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001728 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001729 goto ioctl_exit;
1730 }
Steve French4a72daf2013-06-25 00:20:49 -05001731 }
1732
1733 /* check if caller wants to look at return data or just return rc */
1734 if ((plen == NULL) || (out_data == NULL))
1735 goto ioctl_exit;
1736
1737 *plen = le32_to_cpu(rsp->OutputCount);
1738
1739 /* We check for obvious errors in the output buffer length and offset */
1740 if (*plen == 0)
1741 goto ioctl_exit; /* server returned no data */
1742 else if (*plen > 0xFF00) {
1743 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1744 *plen = 0;
1745 rc = -EIO;
1746 goto ioctl_exit;
1747 }
1748
1749 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1750 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1751 le32_to_cpu(rsp->OutputOffset));
1752 *plen = 0;
1753 rc = -EIO;
1754 goto ioctl_exit;
1755 }
1756
1757 *out_data = kmalloc(*plen, GFP_KERNEL);
1758 if (*out_data == NULL) {
1759 rc = -ENOMEM;
1760 goto ioctl_exit;
1761 }
1762
Steve French373512e2015-12-18 13:05:30 -06001763 memcpy(*out_data,
1764 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001765 *plen);
1766ioctl_exit:
1767 free_rsp_buf(resp_buftype, rsp);
1768 return rc;
1769}
1770
Steve French64a5cfa2013-10-14 15:31:32 -05001771/*
1772 * Individual callers to ioctl worker function follow
1773 */
1774
1775int
1776SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1777 u64 persistent_fid, u64 volatile_fid)
1778{
1779 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001780 struct compress_ioctl fsctl_input;
1781 char *ret_data = NULL;
1782
1783 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001784 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001785
1786 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1787 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1788 (char *)&fsctl_input /* data input */,
1789 2 /* in data len */, &ret_data /* out data */, NULL);
1790
1791 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001792
1793 return rc;
1794}
1795
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001796int
1797SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1798 u64 persistent_fid, u64 volatile_fid)
1799{
1800 struct smb2_close_req *req;
1801 struct smb2_close_rsp *rsp;
1802 struct TCP_Server_Info *server;
1803 struct cifs_ses *ses = tcon->ses;
1804 struct kvec iov[1];
1805 int resp_buftype;
1806 int rc = 0;
1807
Joe Perchesf96637b2013-05-04 22:12:25 -05001808 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001809
1810 if (ses && (ses->server))
1811 server = ses->server;
1812 else
1813 return -EIO;
1814
1815 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1816 if (rc)
1817 return rc;
1818
1819 req->PersistentFileId = persistent_fid;
1820 req->VolatileFileId = volatile_fid;
1821
1822 iov[0].iov_base = (char *)req;
1823 /* 4 for rfc1002 length field */
1824 iov[0].iov_len = get_rfc1002_length(req) + 4;
1825
1826 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1827 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1828
1829 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001830 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001831 goto close_exit;
1832 }
1833
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001834 /* BB FIXME - decode close response, update inode for caching */
1835
1836close_exit:
1837 free_rsp_buf(resp_buftype, rsp);
1838 return rc;
1839}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001840
1841static int
1842validate_buf(unsigned int offset, unsigned int buffer_length,
1843 struct smb2_hdr *hdr, unsigned int min_buf_size)
1844
1845{
1846 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1847 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1848 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1849 char *end_of_buf = begin_of_buf + buffer_length;
1850
1851
1852 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001853 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1854 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001855 return -EINVAL;
1856 }
1857
1858 /* check if beyond RFC1001 maximum length */
1859 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001860 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1861 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001862 return -EINVAL;
1863 }
1864
1865 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001866 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001867 return -EINVAL;
1868 }
1869
1870 return 0;
1871}
1872
1873/*
1874 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1875 * Caller must free buffer.
1876 */
1877static int
1878validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1879 struct smb2_hdr *hdr, unsigned int minbufsize,
1880 char *data)
1881
1882{
1883 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1884 int rc;
1885
1886 if (!data)
1887 return -EINVAL;
1888
1889 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1890 if (rc)
1891 return rc;
1892
1893 memcpy(data, begin_of_buf, buffer_length);
1894
1895 return 0;
1896}
1897
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001898static int
1899query_info(const unsigned int xid, struct cifs_tcon *tcon,
1900 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1901 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001902{
1903 struct smb2_query_info_req *req;
1904 struct smb2_query_info_rsp *rsp = NULL;
1905 struct kvec iov[2];
1906 int rc = 0;
1907 int resp_buftype;
1908 struct TCP_Server_Info *server;
1909 struct cifs_ses *ses = tcon->ses;
1910
Joe Perchesf96637b2013-05-04 22:12:25 -05001911 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001912
1913 if (ses && (ses->server))
1914 server = ses->server;
1915 else
1916 return -EIO;
1917
1918 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1919 if (rc)
1920 return rc;
1921
1922 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001923 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001924 req->PersistentFileId = persistent_fid;
1925 req->VolatileFileId = volatile_fid;
1926 /* 4 for rfc1002 length field and 1 for Buffer */
1927 req->InputBufferOffset =
1928 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001929 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001930
1931 iov[0].iov_base = (char *)req;
1932 /* 4 for rfc1002 length field */
1933 iov[0].iov_len = get_rfc1002_length(req) + 4;
1934
1935 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001936 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1937
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001938 if (rc) {
1939 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1940 goto qinf_exit;
1941 }
1942
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001943 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1944 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001945 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001946
1947qinf_exit:
1948 free_rsp_buf(resp_buftype, rsp);
1949 return rc;
1950}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001951
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001952int
1953SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1954 u64 persistent_fid, u64 volatile_fid,
1955 struct smb2_file_all_info *data)
1956{
1957 return query_info(xid, tcon, persistent_fid, volatile_fid,
1958 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001959 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001960 sizeof(struct smb2_file_all_info), data);
1961}
1962
1963int
1964SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1965 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1966{
1967 return query_info(xid, tcon, persistent_fid, volatile_fid,
1968 FILE_INTERNAL_INFORMATION,
1969 sizeof(struct smb2_file_internal_info),
1970 sizeof(struct smb2_file_internal_info), uniqueid);
1971}
1972
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001973/*
1974 * This is a no-op for now. We're not really interested in the reply, but
1975 * rather in the fact that the server sent one and that server->lstrp
1976 * gets updated.
1977 *
1978 * FIXME: maybe we should consider checking that the reply matches request?
1979 */
1980static void
1981smb2_echo_callback(struct mid_q_entry *mid)
1982{
1983 struct TCP_Server_Info *server = mid->callback_data;
1984 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1985 unsigned int credits_received = 1;
1986
1987 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1988 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1989
Christopher Oo5fb4e282015-06-25 16:10:48 -07001990 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001991 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001992 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001993 add_credits(server, credits_received, CIFS_ECHO_OP);
1994}
1995
Pavel Shilovsky48f95262016-11-04 11:50:31 -07001996void smb2_reconnect_server(struct work_struct *work)
1997{
1998 struct TCP_Server_Info *server = container_of(work,
1999 struct TCP_Server_Info, reconnect.work);
2000 struct cifs_ses *ses;
2001 struct cifs_tcon *tcon, *tcon2;
2002 struct list_head tmp_list;
2003 int tcon_exist = false;
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002004 int rc;
2005 int resched = false;
2006
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002007
2008 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2009 mutex_lock(&server->reconnect_mutex);
2010
2011 INIT_LIST_HEAD(&tmp_list);
2012 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2013
2014 spin_lock(&cifs_tcp_ses_lock);
2015 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2016 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002017 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002018 tcon->tc_count++;
2019 list_add_tail(&tcon->rlist, &tmp_list);
2020 tcon_exist = true;
2021 }
2022 }
2023 }
2024 /*
2025 * Get the reference to server struct to be sure that the last call of
2026 * cifs_put_tcon() in the loop below won't release the server pointer.
2027 */
2028 if (tcon_exist)
2029 server->srv_count++;
2030
2031 spin_unlock(&cifs_tcp_ses_lock);
2032
2033 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002034 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2035 if (!rc)
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002036 cifs_reopen_persistent_handles(tcon);
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002037 else
2038 resched = true;
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002039 list_del_init(&tcon->rlist);
2040 cifs_put_tcon(tcon);
2041 }
2042
2043 cifs_dbg(FYI, "Reconnecting tcons finished\n");
Germano Percossi3d8d2f22017-04-07 12:29:36 +01002044 if (resched)
2045 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002046 mutex_unlock(&server->reconnect_mutex);
2047
2048 /* now we can safely release srv struct */
2049 if (tcon_exist)
2050 cifs_put_tcp_session(server, 1);
2051}
2052
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002053int
2054SMB2_echo(struct TCP_Server_Info *server)
2055{
2056 struct smb2_echo_req *req;
2057 int rc = 0;
2058 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002059 struct smb_rqst rqst = { .rq_iov = &iov,
2060 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002061
Joe Perchesf96637b2013-05-04 22:12:25 -05002062 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002063
Steve French4fcd1812016-06-22 20:12:05 -05002064 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002065 /* No need to send echo on newly established connections */
2066 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2067 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002068 }
2069
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002070 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2071 if (rc)
2072 return rc;
2073
2074 req->hdr.CreditRequest = cpu_to_le16(1);
2075
2076 iov.iov_base = (char *)req;
2077 /* 4 for rfc1002 length field */
2078 iov.iov_len = get_rfc1002_length(req) + 4;
2079
Jeff Laytonfec344e2012-09-18 16:20:35 -07002080 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002081 CIFS_ECHO_OP);
2082 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002083 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002084
2085 cifs_small_buf_release(req);
2086 return rc;
2087}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002088
2089int
2090SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2091 u64 volatile_fid)
2092{
2093 struct smb2_flush_req *req;
2094 struct TCP_Server_Info *server;
2095 struct cifs_ses *ses = tcon->ses;
2096 struct kvec iov[1];
2097 int resp_buftype;
2098 int rc = 0;
2099
Joe Perchesf96637b2013-05-04 22:12:25 -05002100 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002101
2102 if (ses && (ses->server))
2103 server = ses->server;
2104 else
2105 return -EIO;
2106
2107 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2108 if (rc)
2109 return rc;
2110
2111 req->PersistentFileId = persistent_fid;
2112 req->VolatileFileId = volatile_fid;
2113
2114 iov[0].iov_base = (char *)req;
2115 /* 4 for rfc1002 length field */
2116 iov[0].iov_len = get_rfc1002_length(req) + 4;
2117
2118 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2119
Steve Frenchdfebe402015-03-27 01:00:06 -05002120 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002121 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2122
2123 free_rsp_buf(resp_buftype, iov[0].iov_base);
2124 return rc;
2125}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002126
2127/*
2128 * To form a chain of read requests, any read requests after the first should
2129 * have the end_of_chain boolean set to true.
2130 */
2131static int
2132smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2133 unsigned int remaining_bytes, int request_type)
2134{
2135 int rc = -EACCES;
2136 struct smb2_read_req *req = NULL;
2137
2138 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2139 if (rc)
2140 return rc;
2141 if (io_parms->tcon->ses->server == NULL)
2142 return -ECONNABORTED;
2143
2144 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2145
2146 req->PersistentFileId = io_parms->persistent_fid;
2147 req->VolatileFileId = io_parms->volatile_fid;
2148 req->ReadChannelInfoOffset = 0; /* reserved */
2149 req->ReadChannelInfoLength = 0; /* reserved */
2150 req->Channel = 0; /* reserved */
2151 req->MinimumCount = 0;
2152 req->Length = cpu_to_le32(io_parms->length);
2153 req->Offset = cpu_to_le64(io_parms->offset);
2154
2155 if (request_type & CHAINED_REQUEST) {
2156 if (!(request_type & END_OF_CHAIN)) {
2157 /* 4 for rfc1002 length field */
2158 req->hdr.NextCommand =
2159 cpu_to_le32(get_rfc1002_length(req) + 4);
2160 } else /* END_OF_CHAIN */
2161 req->hdr.NextCommand = 0;
2162 if (request_type & RELATED_REQUEST) {
2163 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2164 /*
2165 * Related requests use info from previous read request
2166 * in chain.
2167 */
2168 req->hdr.SessionId = 0xFFFFFFFF;
2169 req->hdr.TreeId = 0xFFFFFFFF;
2170 req->PersistentFileId = 0xFFFFFFFF;
2171 req->VolatileFileId = 0xFFFFFFFF;
2172 }
2173 }
2174 if (remaining_bytes > io_parms->length)
2175 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2176 else
2177 req->RemainingBytes = 0;
2178
2179 iov[0].iov_base = (char *)req;
2180 /* 4 for rfc1002 length field */
2181 iov[0].iov_len = get_rfc1002_length(req) + 4;
2182 return rc;
2183}
2184
2185static void
2186smb2_readv_callback(struct mid_q_entry *mid)
2187{
2188 struct cifs_readdata *rdata = mid->callback_data;
2189 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2190 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07002191 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002192 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07002193 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07002194 .rq_nvec = 1,
2195 .rq_pages = rdata->pages,
2196 .rq_npages = rdata->nr_pages,
2197 .rq_pagesz = rdata->pagesz,
2198 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002199
Joe Perchesf96637b2013-05-04 22:12:25 -05002200 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2201 __func__, mid->mid, mid->mid_state, rdata->result,
2202 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002203
2204 switch (mid->mid_state) {
2205 case MID_RESPONSE_RECEIVED:
2206 credits_received = le16_to_cpu(buf->CreditRequest);
2207 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002208 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002209 int rc;
2210
Jeff Layton0b688cf2012-09-18 16:20:34 -07002211 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002212 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002213 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2214 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002215 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002216 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002217 task_io_account_read(rdata->got_bytes);
2218 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002219 break;
2220 case MID_REQUEST_SUBMITTED:
2221 case MID_RETRY_NEEDED:
2222 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002223 if (server->sign && rdata->got_bytes)
2224 /* reset bytes number since we can not check a sign */
2225 rdata->got_bytes = 0;
2226 /* FIXME: should this be counted toward the initiating task? */
2227 task_io_account_read(rdata->got_bytes);
2228 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002229 break;
2230 default:
2231 if (rdata->result != -ENODATA)
2232 rdata->result = -EIO;
2233 }
2234
2235 if (rdata->result)
2236 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2237
2238 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002239 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002240 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002241 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002242 add_credits(server, credits_received, 0);
2243}
2244
2245/* smb2_async_readv - send an async write, and set up mid to handle result */
2246int
2247smb2_async_readv(struct cifs_readdata *rdata)
2248{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002249 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002250 struct smb2_hdr *buf;
2251 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002252 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002253 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002254 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002255
Joe Perchesf96637b2013-05-04 22:12:25 -05002256 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2257 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002258
2259 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2260 io_parms.offset = rdata->offset;
2261 io_parms.length = rdata->bytes;
2262 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2263 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2264 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002265
2266 server = io_parms.tcon->ses->server;
2267
Jeff Layton58195752012-09-19 06:22:34 -07002268 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002269 if (rc) {
2270 if (rc == -EAGAIN && rdata->credits) {
2271 /* credits was reset by reconnect */
2272 rdata->credits = 0;
2273 /* reduce in_flight value since we won't send the req */
2274 spin_lock(&server->req_lock);
2275 server->in_flight--;
2276 spin_unlock(&server->req_lock);
2277 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002278 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002279 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002280
Jeff Layton58195752012-09-19 06:22:34 -07002281 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002282 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002283 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002284
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002285 if (rdata->credits) {
2286 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2287 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002288 buf->CreditRequest = buf->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002289 spin_lock(&server->req_lock);
2290 server->credits += rdata->credits -
2291 le16_to_cpu(buf->CreditCharge);
2292 spin_unlock(&server->req_lock);
2293 wake_up(&server->request_q);
2294 flags = CIFS_HAS_CREDITS;
2295 }
2296
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002297 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002298 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002299 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002300 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002301 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002302 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002303 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2304 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002305
2306 cifs_small_buf_release(buf);
2307 return rc;
2308}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002309
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002310int
2311SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2312 unsigned int *nbytes, char **buf, int *buf_type)
2313{
2314 int resp_buftype, rc = -EACCES;
2315 struct smb2_read_rsp *rsp = NULL;
2316 struct kvec iov[1];
2317
2318 *nbytes = 0;
2319 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2320 if (rc)
2321 return rc;
2322
2323 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2324 &resp_buftype, CIFS_LOG_ERROR);
2325
2326 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2327
2328 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2329 free_rsp_buf(resp_buftype, iov[0].iov_base);
2330 return 0;
2331 }
2332
2333 if (rc) {
2334 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002335 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002336 } else {
2337 *nbytes = le32_to_cpu(rsp->DataLength);
2338 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2339 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002340 cifs_dbg(FYI, "bad length %d for count %d\n",
2341 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002342 rc = -EIO;
2343 *nbytes = 0;
2344 }
2345 }
2346
2347 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002348 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002349 *nbytes);
2350 free_rsp_buf(resp_buftype, iov[0].iov_base);
2351 } else if (resp_buftype != CIFS_NO_BUFFER) {
2352 *buf = iov[0].iov_base;
2353 if (resp_buftype == CIFS_SMALL_BUFFER)
2354 *buf_type = CIFS_SMALL_BUFFER;
2355 else if (resp_buftype == CIFS_LARGE_BUFFER)
2356 *buf_type = CIFS_LARGE_BUFFER;
2357 }
2358 return rc;
2359}
2360
Pavel Shilovsky33319142012-09-18 16:20:29 -07002361/*
2362 * Check the mid_state and signature on received buffer (if any), and queue the
2363 * workqueue completion task.
2364 */
2365static void
2366smb2_writev_callback(struct mid_q_entry *mid)
2367{
2368 struct cifs_writedata *wdata = mid->callback_data;
2369 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002370 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002371 unsigned int written;
2372 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2373 unsigned int credits_received = 1;
2374
2375 switch (mid->mid_state) {
2376 case MID_RESPONSE_RECEIVED:
2377 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2378 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2379 if (wdata->result != 0)
2380 break;
2381
2382 written = le32_to_cpu(rsp->DataLength);
2383 /*
2384 * Mask off high 16 bits when bytes written as returned
2385 * by the server is greater than bytes requested by the
2386 * client. OS/2 servers are known to set incorrect
2387 * CountHigh values.
2388 */
2389 if (written > wdata->bytes)
2390 written &= 0xFFFF;
2391
2392 if (written < wdata->bytes)
2393 wdata->result = -ENOSPC;
2394 else
2395 wdata->bytes = written;
2396 break;
2397 case MID_REQUEST_SUBMITTED:
2398 case MID_RETRY_NEEDED:
2399 wdata->result = -EAGAIN;
2400 break;
2401 default:
2402 wdata->result = -EIO;
2403 break;
2404 }
2405
2406 if (wdata->result)
2407 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2408
2409 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002410 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002411 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002412 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002413 add_credits(tcon->ses->server, credits_received, 0);
2414}
2415
2416/* smb2_async_writev - send an async write, and set up mid to handle result */
2417int
Steve French4a5c80d2014-02-07 20:45:12 -06002418smb2_async_writev(struct cifs_writedata *wdata,
2419 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002420{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002421 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002422 struct smb2_write_req *req = NULL;
2423 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002424 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002425 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002426 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002427
2428 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002429 if (rc) {
2430 if (rc == -EAGAIN && wdata->credits) {
2431 /* credits was reset by reconnect */
2432 wdata->credits = 0;
2433 /* reduce in_flight value since we won't send the req */
2434 spin_lock(&server->req_lock);
2435 server->in_flight--;
2436 spin_unlock(&server->req_lock);
2437 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002438 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002439 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002440
Pavel Shilovsky33319142012-09-18 16:20:29 -07002441 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2442
2443 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2444 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2445 req->WriteChannelInfoOffset = 0;
2446 req->WriteChannelInfoLength = 0;
2447 req->Channel = 0;
2448 req->Offset = cpu_to_le64(wdata->offset);
2449 /* 4 for rfc1002 length field */
2450 req->DataOffset = cpu_to_le16(
2451 offsetof(struct smb2_write_req, Buffer) - 4);
2452 req->RemainingBytes = 0;
2453
2454 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002455 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2456 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002457
Jeff Laytoneddb0792012-09-18 16:20:35 -07002458 rqst.rq_iov = &iov;
2459 rqst.rq_nvec = 1;
2460 rqst.rq_pages = wdata->pages;
2461 rqst.rq_npages = wdata->nr_pages;
2462 rqst.rq_pagesz = wdata->pagesz;
2463 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002464
Joe Perchesf96637b2013-05-04 22:12:25 -05002465 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2466 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002467
2468 req->Length = cpu_to_le32(wdata->bytes);
2469
2470 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2471
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002472 if (wdata->credits) {
2473 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2474 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002475 req->hdr.CreditRequest = req->hdr.CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002476 spin_lock(&server->req_lock);
2477 server->credits += wdata->credits -
2478 le16_to_cpu(req->hdr.CreditCharge);
2479 spin_unlock(&server->req_lock);
2480 wake_up(&server->request_q);
2481 flags = CIFS_HAS_CREDITS;
2482 }
2483
Pavel Shilovsky33319142012-09-18 16:20:29 -07002484 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002485 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2486 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002487
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002488 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002489 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002490 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2491 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002492
Pavel Shilovsky33319142012-09-18 16:20:29 -07002493async_writev_out:
2494 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002495 return rc;
2496}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002497
2498/*
2499 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2500 * The length field from io_parms must be at least 1 and indicates a number of
2501 * elements with data to write that begins with position 1 in iov array. All
2502 * data length is specified by count.
2503 */
2504int
2505SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2506 unsigned int *nbytes, struct kvec *iov, int n_vec)
2507{
2508 int rc = 0;
2509 struct smb2_write_req *req = NULL;
2510 struct smb2_write_rsp *rsp = NULL;
2511 int resp_buftype;
2512 *nbytes = 0;
2513
2514 if (n_vec < 1)
2515 return rc;
2516
2517 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2518 if (rc)
2519 return rc;
2520
2521 if (io_parms->tcon->ses->server == NULL)
2522 return -ECONNABORTED;
2523
2524 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2525
2526 req->PersistentFileId = io_parms->persistent_fid;
2527 req->VolatileFileId = io_parms->volatile_fid;
2528 req->WriteChannelInfoOffset = 0;
2529 req->WriteChannelInfoLength = 0;
2530 req->Channel = 0;
2531 req->Length = cpu_to_le32(io_parms->length);
2532 req->Offset = cpu_to_le64(io_parms->offset);
2533 /* 4 for rfc1002 length field */
2534 req->DataOffset = cpu_to_le16(
2535 offsetof(struct smb2_write_req, Buffer) - 4);
2536 req->RemainingBytes = 0;
2537
2538 iov[0].iov_base = (char *)req;
2539 /* 4 for rfc1002 length field and 1 for Buffer */
2540 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2541
2542 /* length of entire message including data to be written */
2543 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2544
2545 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2546 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002547 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002548
2549 if (rc) {
2550 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002551 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002552 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002553 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002554
2555 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002556 return rc;
2557}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002558
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002559static unsigned int
2560num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2561{
2562 int len;
2563 unsigned int entrycount = 0;
2564 unsigned int next_offset = 0;
2565 FILE_DIRECTORY_INFO *entryptr;
2566
2567 if (bufstart == NULL)
2568 return 0;
2569
2570 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2571
2572 while (1) {
2573 entryptr = (FILE_DIRECTORY_INFO *)
2574 ((char *)entryptr + next_offset);
2575
2576 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002577 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002578 break;
2579 }
2580
2581 len = le32_to_cpu(entryptr->FileNameLength);
2582 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002583 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2584 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002585 break;
2586 }
2587
2588 *lastentry = (char *)entryptr;
2589 entrycount++;
2590
2591 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2592 if (!next_offset)
2593 break;
2594 }
2595
2596 return entrycount;
2597}
2598
2599/*
2600 * Readdir/FindFirst
2601 */
2602int
2603SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2604 u64 persistent_fid, u64 volatile_fid, int index,
2605 struct cifs_search_info *srch_inf)
2606{
2607 struct smb2_query_directory_req *req;
2608 struct smb2_query_directory_rsp *rsp = NULL;
2609 struct kvec iov[2];
2610 int rc = 0;
2611 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002612 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002613 unsigned char *bufptr;
2614 struct TCP_Server_Info *server;
2615 struct cifs_ses *ses = tcon->ses;
2616 __le16 asteriks = cpu_to_le16('*');
2617 char *end_of_smb;
2618 unsigned int output_size = CIFSMaxBufSize;
2619 size_t info_buf_size;
2620
2621 if (ses && (ses->server))
2622 server = ses->server;
2623 else
2624 return -EIO;
2625
2626 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2627 if (rc)
2628 return rc;
2629
2630 switch (srch_inf->info_level) {
2631 case SMB_FIND_FILE_DIRECTORY_INFO:
2632 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2633 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2634 break;
2635 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2636 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2637 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2638 break;
2639 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002640 cifs_dbg(VFS, "info level %u isn't supported\n",
2641 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002642 rc = -EINVAL;
2643 goto qdir_exit;
2644 }
2645
2646 req->FileIndex = cpu_to_le32(index);
2647 req->PersistentFileId = persistent_fid;
2648 req->VolatileFileId = volatile_fid;
2649
2650 len = 0x2;
2651 bufptr = req->Buffer;
2652 memcpy(bufptr, &asteriks, len);
2653
2654 req->FileNameOffset =
2655 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2656 req->FileNameLength = cpu_to_le16(len);
2657 /*
2658 * BB could be 30 bytes or so longer if we used SMB2 specific
2659 * buffer lengths, but this is safe and close enough.
2660 */
2661 output_size = min_t(unsigned int, output_size, server->maxBuf);
2662 output_size = min_t(unsigned int, output_size, 2 << 15);
2663 req->OutputBufferLength = cpu_to_le32(output_size);
2664
2665 iov[0].iov_base = (char *)req;
2666 /* 4 for RFC1001 length and 1 for Buffer */
2667 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2668
2669 iov[1].iov_base = (char *)(req->Buffer);
2670 iov[1].iov_len = len;
2671
2672 inc_rfc1001_len(req, len - 1 /* Buffer */);
2673
2674 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002675 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2676
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002677 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002678 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2679 srch_inf->endOfSearch = true;
2680 rc = 0;
2681 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002682 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2683 goto qdir_exit;
2684 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002685
2686 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2687 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2688 info_buf_size);
2689 if (rc)
2690 goto qdir_exit;
2691
2692 srch_inf->unicode = true;
2693
2694 if (srch_inf->ntwrk_buf_start) {
2695 if (srch_inf->smallBuf)
2696 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2697 else
2698 cifs_buf_release(srch_inf->ntwrk_buf_start);
2699 }
2700 srch_inf->ntwrk_buf_start = (char *)rsp;
2701 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2702 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2703 /* 4 for rfc1002 length field */
2704 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2705 srch_inf->entries_in_buffer =
2706 num_entries(srch_inf->srch_entries_start, end_of_smb,
2707 &srch_inf->last_entry, info_buf_size);
2708 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002709 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2710 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2711 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002712 if (resp_buftype == CIFS_LARGE_BUFFER)
2713 srch_inf->smallBuf = false;
2714 else if (resp_buftype == CIFS_SMALL_BUFFER)
2715 srch_inf->smallBuf = true;
2716 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002717 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002718
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002719 return rc;
2720
2721qdir_exit:
2722 free_rsp_buf(resp_buftype, rsp);
2723 return rc;
2724}
2725
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002726static int
2727send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002728 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002729 unsigned int num, void **data, unsigned int *size)
2730{
2731 struct smb2_set_info_req *req;
2732 struct smb2_set_info_rsp *rsp = NULL;
2733 struct kvec *iov;
2734 int rc = 0;
2735 int resp_buftype;
2736 unsigned int i;
2737 struct TCP_Server_Info *server;
2738 struct cifs_ses *ses = tcon->ses;
2739
2740 if (ses && (ses->server))
2741 server = ses->server;
2742 else
2743 return -EIO;
2744
2745 if (!num)
2746 return -EINVAL;
2747
2748 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2749 if (!iov)
2750 return -ENOMEM;
2751
2752 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2753 if (rc) {
2754 kfree(iov);
2755 return rc;
2756 }
2757
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002758 req->hdr.ProcessId = cpu_to_le32(pid);
2759
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002760 req->InfoType = SMB2_O_INFO_FILE;
2761 req->FileInfoClass = info_class;
2762 req->PersistentFileId = persistent_fid;
2763 req->VolatileFileId = volatile_fid;
2764
2765 /* 4 for RFC1001 length and 1 for Buffer */
2766 req->BufferOffset =
2767 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2768 req->BufferLength = cpu_to_le32(*size);
2769
2770 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2771
2772 memcpy(req->Buffer, *data, *size);
2773
2774 iov[0].iov_base = (char *)req;
2775 /* 4 for RFC1001 length */
2776 iov[0].iov_len = get_rfc1002_length(req) + 4;
2777
2778 for (i = 1; i < num; i++) {
2779 inc_rfc1001_len(req, size[i]);
2780 le32_add_cpu(&req->BufferLength, size[i]);
2781 iov[i].iov_base = (char *)data[i];
2782 iov[i].iov_len = size[i];
2783 }
2784
2785 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2786 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2787
Steve French7d3fb242013-11-18 09:56:28 -06002788 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002789 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002790
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002791 free_rsp_buf(resp_buftype, rsp);
2792 kfree(iov);
2793 return rc;
2794}
2795
2796int
2797SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2798 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2799{
2800 struct smb2_file_rename_info info;
2801 void **data;
2802 unsigned int size[2];
2803 int rc;
2804 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2805
2806 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2807 if (!data)
2808 return -ENOMEM;
2809
2810 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2811 /* 0 = fail if target already exists */
2812 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2813 info.FileNameLength = cpu_to_le32(len);
2814
2815 data[0] = &info;
2816 size[0] = sizeof(struct smb2_file_rename_info);
2817
2818 data[1] = target_file;
2819 size[1] = len + 2 /* null */;
2820
2821 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002822 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2823 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002824 kfree(data);
2825 return rc;
2826}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002827
2828int
Steve French897fba12016-05-12 21:20:36 -05002829SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2830 u64 persistent_fid, u64 volatile_fid)
2831{
2832 __u8 delete_pending = 1;
2833 void *data;
2834 unsigned int size;
2835
2836 data = &delete_pending;
2837 size = 1; /* sizeof __u8 */
2838
2839 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2840 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2841 &size);
2842}
2843
2844int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002845SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2846 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2847{
2848 struct smb2_file_link_info info;
2849 void **data;
2850 unsigned int size[2];
2851 int rc;
2852 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2853
2854 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2855 if (!data)
2856 return -ENOMEM;
2857
2858 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2859 /* 0 = fail if link already exists */
2860 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2861 info.FileNameLength = cpu_to_le32(len);
2862
2863 data[0] = &info;
2864 size[0] = sizeof(struct smb2_file_link_info);
2865
2866 data[1] = target_file;
2867 size[1] = len + 2 /* null */;
2868
2869 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002870 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002871 kfree(data);
2872 return rc;
2873}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002874
2875int
2876SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002877 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002878{
2879 struct smb2_file_eof_info info;
2880 void *data;
2881 unsigned int size;
2882
2883 info.EndOfFile = *eof;
2884
2885 data = &info;
2886 size = sizeof(struct smb2_file_eof_info);
2887
Steve Frenchf29ebb42014-07-19 21:44:58 -05002888 if (is_falloc)
2889 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2890 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2891 else
2892 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2893 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002894}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002895
2896int
2897SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2898 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2899{
2900 unsigned int size;
2901 size = sizeof(FILE_BASIC_INFO);
2902 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2903 current->tgid, FILE_BASIC_INFORMATION, 1,
2904 (void **)&buf, &size);
2905}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002906
2907int
2908SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2909 const u64 persistent_fid, const u64 volatile_fid,
2910 __u8 oplock_level)
2911{
2912 int rc;
2913 struct smb2_oplock_break *req = NULL;
2914
Joe Perchesf96637b2013-05-04 22:12:25 -05002915 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002916 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2917
2918 if (rc)
2919 return rc;
2920
2921 req->VolatileFid = volatile_fid;
2922 req->PersistentFid = persistent_fid;
2923 req->OplockLevel = oplock_level;
2924 req->hdr.CreditRequest = cpu_to_le16(1);
2925
2926 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2927 /* SMB2 buffer freed by function above */
2928
2929 if (rc) {
2930 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002931 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002932 }
2933
2934 return rc;
2935}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002936
2937static void
2938copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2939 struct kstatfs *kst)
2940{
2941 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2942 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2943 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
Sachin Prabhu8b053292017-08-03 13:09:03 +05302944 kst->f_bfree = kst->f_bavail =
2945 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002946 return;
2947}
2948
2949static int
2950build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2951 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2952{
2953 int rc;
2954 struct smb2_query_info_req *req;
2955
Joe Perchesf96637b2013-05-04 22:12:25 -05002956 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002957
2958 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2959 return -EIO;
2960
2961 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2962 if (rc)
2963 return rc;
2964
2965 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2966 req->FileInfoClass = level;
2967 req->PersistentFileId = persistent_fid;
2968 req->VolatileFileId = volatile_fid;
2969 /* 4 for rfc1002 length field and 1 for pad */
2970 req->InputBufferOffset =
2971 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2972 req->OutputBufferLength = cpu_to_le32(
2973 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2974
2975 iov->iov_base = (char *)req;
2976 /* 4 for rfc1002 length field */
2977 iov->iov_len = get_rfc1002_length(req) + 4;
2978 return 0;
2979}
2980
2981int
2982SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2983 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2984{
2985 struct smb2_query_info_rsp *rsp = NULL;
2986 struct kvec iov;
2987 int rc = 0;
2988 int resp_buftype;
2989 struct cifs_ses *ses = tcon->ses;
2990 struct smb2_fs_full_size_info *info = NULL;
2991
2992 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2993 sizeof(struct smb2_fs_full_size_info),
2994 persistent_fid, volatile_fid);
2995 if (rc)
2996 return rc;
2997
2998 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2999 if (rc) {
3000 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05003001 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003002 }
3003 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3004
3005 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3006 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3007 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3008 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3009 sizeof(struct smb2_fs_full_size_info));
3010 if (!rc)
3011 copy_fs_info_to_kstatfs(info, fsdata);
3012
Steve French34f62642013-10-09 02:07:00 -05003013qfsinf_exit:
3014 free_rsp_buf(resp_buftype, iov.iov_base);
3015 return rc;
3016}
3017
3018int
3019SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05003020 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05003021{
3022 struct smb2_query_info_rsp *rsp = NULL;
3023 struct kvec iov;
3024 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003025 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003026 struct cifs_ses *ses = tcon->ses;
3027 unsigned int rsp_len, offset;
3028
Steven French21671142013-10-09 13:36:35 -05003029 if (level == FS_DEVICE_INFORMATION) {
3030 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3031 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3032 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3033 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3034 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003035 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3036 max_len = sizeof(struct smb3_fs_ss_info);
3037 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003038 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003039 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003040 return -EINVAL;
3041 }
3042
3043 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003044 persistent_fid, volatile_fid);
3045 if (rc)
3046 return rc;
3047
3048 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3049 if (rc) {
3050 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3051 goto qfsattr_exit;
3052 }
3053 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3054
3055 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3056 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003057 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3058 if (rc)
3059 goto qfsattr_exit;
3060
3061 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003062 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3063 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003064 rsp_len, max_len));
3065 else if (level == FS_DEVICE_INFORMATION)
3066 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3067 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003068 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3069 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3070 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3071 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3072 tcon->perf_sector_size =
3073 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3074 }
Steve French34f62642013-10-09 02:07:00 -05003075
3076qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003077 free_rsp_buf(resp_buftype, iov.iov_base);
3078 return rc;
3079}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003080
3081int
3082smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3083 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3084 const __u32 num_lock, struct smb2_lock_element *buf)
3085{
3086 int rc = 0;
3087 struct smb2_lock_req *req = NULL;
3088 struct kvec iov[2];
3089 int resp_buf_type;
3090 unsigned int count;
3091
Joe Perchesf96637b2013-05-04 22:12:25 -05003092 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003093
3094 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3095 if (rc)
3096 return rc;
3097
3098 req->hdr.ProcessId = cpu_to_le32(pid);
3099 req->LockCount = cpu_to_le16(num_lock);
3100
3101 req->PersistentFileId = persist_fid;
3102 req->VolatileFileId = volatile_fid;
3103
3104 count = num_lock * sizeof(struct smb2_lock_element);
3105 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3106
3107 iov[0].iov_base = (char *)req;
3108 /* 4 for rfc1002 length field and count for all locks */
3109 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3110 iov[1].iov_base = (char *)buf;
3111 iov[1].iov_len = count;
3112
3113 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3114 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3115 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003116 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003117 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3118 }
3119
3120 return rc;
3121}
3122
3123int
3124SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3125 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3126 const __u64 length, const __u64 offset, const __u32 lock_flags,
3127 const bool wait)
3128{
3129 struct smb2_lock_element lock;
3130
3131 lock.Offset = cpu_to_le64(offset);
3132 lock.Length = cpu_to_le64(length);
3133 lock.Flags = cpu_to_le32(lock_flags);
3134 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3135 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3136
3137 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3138}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003139
3140int
3141SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3142 __u8 *lease_key, const __le32 lease_state)
3143{
3144 int rc;
3145 struct smb2_lease_ack *req = NULL;
3146
Joe Perchesf96637b2013-05-04 22:12:25 -05003147 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003148 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3149
3150 if (rc)
3151 return rc;
3152
3153 req->hdr.CreditRequest = cpu_to_le16(1);
3154 req->StructureSize = cpu_to_le16(36);
3155 inc_rfc1001_len(req, 12);
3156
3157 memcpy(req->LeaseKey, lease_key, 16);
3158 req->LeaseState = lease_state;
3159
3160 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3161 /* SMB2 buffer freed by function above */
3162
3163 if (rc) {
3164 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003165 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003166 }
3167
3168 return rc;
3169}