blob: bdd32925a15e950088105bb462f8dc159926ce9e [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);
369 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
370 + 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
534 * can not sign it. We could eventually change this to selectively
535 * sign just this, the first and only signed request on a connection.
536 * This is good enough for now since a user who wants better security
537 * would also enable signing on the mount. Having validation of
538 * negotiate info for signed connections helps reduce attack vectors
539 */
540 if (tcon->ses->server->sign == false)
541 return 0; /* validation requires signing */
542
543 vneg_inbuf.Capabilities =
544 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
Sachin Prabhu39552ea2014-05-13 00:48:12 +0100545 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
546 SMB2_CLIENT_GUID_SIZE);
Steve Frenchff1c0382013-11-19 23:44:46 -0600547
548 if (tcon->ses->sign)
549 vneg_inbuf.SecurityMode =
550 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
551 else if (global_secflags & CIFSSEC_MAY_SIGN)
552 vneg_inbuf.SecurityMode =
553 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
554 else
555 vneg_inbuf.SecurityMode = 0;
556
557 vneg_inbuf.DialectCount = cpu_to_le16(1);
558 vneg_inbuf.Dialects[0] =
559 cpu_to_le16(tcon->ses->server->vals->protocol_id);
560
561 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
562 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
563 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
564 (char **)&pneg_rsp, &rsplen);
565
566 if (rc != 0) {
567 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
568 return -EIO;
569 }
570
571 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
572 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
573 return -EIO;
574 }
575
576 /* check validate negotiate info response matches what we got earlier */
577 if (pneg_rsp->Dialect !=
578 cpu_to_le16(tcon->ses->server->vals->protocol_id))
579 goto vneg_out;
580
581 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
582 goto vneg_out;
583
584 /* do not validate server guid because not saved at negprot time yet */
585
586 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
587 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
588 goto vneg_out;
589
590 /* validate negotiate successful */
591 cifs_dbg(FYI, "validate negotiate info successful\n");
592 return 0;
593
594vneg_out:
595 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
596 return -EIO;
597}
598
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100599struct SMB2_sess_data {
600 unsigned int xid;
601 struct cifs_ses *ses;
602 struct nls_table *nls_cp;
603 void (*func)(struct SMB2_sess_data *);
604 int result;
605 u64 previous_session;
606
607 /* we will send the SMB in three pieces:
608 * a fixed length beginning part, an optional
609 * SPNEGO blob (which can be zero length), and a
610 * last part which will include the strings
611 * and rest of bcc area. This allows us to avoid
612 * a large buffer 17K allocation
613 */
614 int buf0_type;
615 struct kvec iov[2];
616};
617
618static int
619SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
620{
621 int rc;
622 struct cifs_ses *ses = sess_data->ses;
623 struct smb2_sess_setup_req *req;
624 struct TCP_Server_Info *server = ses->server;
625
626 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
627 if (rc)
628 return rc;
629
630 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
631
632 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
633 req->PreviousSessionId = sess_data->previous_session;
634
635 req->Flags = 0; /* MBZ */
636 /* to enable echos and oplocks */
637 req->hdr.CreditRequest = cpu_to_le16(3);
638
639 /* only one of SMB2 signing flags may be set in SMB2 request */
640 if (server->sign)
641 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
642 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
643 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
644 else
645 req->SecurityMode = 0;
646
647 req->Capabilities = 0;
648 req->Channel = 0; /* MBZ */
649
650 sess_data->iov[0].iov_base = (char *)req;
651 /* 4 for rfc1002 length field and 1 for pad */
652 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
653 /*
654 * This variable will be used to clear the buffer
655 * allocated above in case of any error in the calling function.
656 */
657 sess_data->buf0_type = CIFS_SMALL_BUFFER;
658
659 return 0;
660}
661
662static void
663SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
664{
665 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
666 sess_data->buf0_type = CIFS_NO_BUFFER;
667}
668
669static int
670SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
671{
672 int rc;
673 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
674
675 /* Testing shows that buffer offset must be at location of Buffer[0] */
676 req->SecurityBufferOffset =
677 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
678 1 /* pad */ - 4 /* rfc1001 len */);
679 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
680
681 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
682
683 /* BB add code to build os and lm fields */
684
685 rc = SendReceive2(sess_data->xid, sess_data->ses,
686 sess_data->iov, 2,
687 &sess_data->buf0_type,
688 CIFS_LOG_ERROR | CIFS_NEG_OP);
689
690 return rc;
691}
692
693static int
694SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
695{
696 int rc = 0;
697 struct cifs_ses *ses = sess_data->ses;
698
699 mutex_lock(&ses->server->srv_mutex);
700 if (ses->server->sign && ses->server->ops->generate_signingkey) {
701 rc = ses->server->ops->generate_signingkey(ses);
702 kfree(ses->auth_key.response);
703 ses->auth_key.response = NULL;
704 if (rc) {
705 cifs_dbg(FYI,
706 "SMB3 session key generation failed\n");
707 mutex_unlock(&ses->server->srv_mutex);
708 goto keygen_exit;
709 }
710 }
711 if (!ses->server->session_estab) {
712 ses->server->sequence_number = 0x2;
713 ses->server->session_estab = true;
714 }
715 mutex_unlock(&ses->server->srv_mutex);
716
717 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
718 spin_lock(&GlobalMid_Lock);
719 ses->status = CifsGood;
720 ses->need_reconnect = false;
721 spin_unlock(&GlobalMid_Lock);
722
723keygen_exit:
724 if (!ses->server->sign) {
725 kfree(ses->auth_key.response);
726 ses->auth_key.response = NULL;
727 }
728 return rc;
729}
730
731#ifdef CONFIG_CIFS_UPCALL
732static void
733SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
734{
735 int rc;
736 struct cifs_ses *ses = sess_data->ses;
737 struct cifs_spnego_msg *msg;
738 struct key *spnego_key = NULL;
739 struct smb2_sess_setup_rsp *rsp = NULL;
740
741 rc = SMB2_sess_alloc_buffer(sess_data);
742 if (rc)
743 goto out;
744
745 spnego_key = cifs_get_spnego_key(ses);
746 if (IS_ERR(spnego_key)) {
747 rc = PTR_ERR(spnego_key);
748 spnego_key = NULL;
749 goto out;
750 }
751
752 msg = spnego_key->payload.data[0];
753 /*
754 * check version field to make sure that cifs.upcall is
755 * sending us a response in an expected form
756 */
757 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
758 cifs_dbg(VFS,
759 "bad cifs.upcall version. Expected %d got %d",
760 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
761 rc = -EKEYREJECTED;
762 goto out_put_spnego_key;
763 }
764
765 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
766 GFP_KERNEL);
767 if (!ses->auth_key.response) {
768 cifs_dbg(VFS,
769 "Kerberos can't allocate (%u bytes) memory",
770 msg->sesskey_len);
771 rc = -ENOMEM;
772 goto out_put_spnego_key;
773 }
774 ses->auth_key.len = msg->sesskey_len;
775
776 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
777 sess_data->iov[1].iov_len = msg->secblob_len;
778
779 rc = SMB2_sess_sendreceive(sess_data);
780 if (rc)
781 goto out_put_spnego_key;
782
783 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
784 ses->Suid = rsp->hdr.SessionId;
785
786 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
787 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
788 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
789
790 rc = SMB2_sess_establish_session(sess_data);
791out_put_spnego_key:
792 key_invalidate(spnego_key);
793 key_put(spnego_key);
794out:
795 sess_data->result = rc;
796 sess_data->func = NULL;
797 SMB2_sess_free_buffer(sess_data);
798}
799#else
800static void
801SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
802{
803 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
804 sess_data->result = -EOPNOTSUPP;
805 sess_data->func = NULL;
806}
807#endif
808
Sachin Prabhu166cea42016-10-07 19:11:22 +0100809static void
810SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
811
812static void
813SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
814{
815 int rc;
816 struct cifs_ses *ses = sess_data->ses;
817 struct smb2_sess_setup_rsp *rsp = NULL;
818 char *ntlmssp_blob = NULL;
819 bool use_spnego = false; /* else use raw ntlmssp */
820 u16 blob_length = 0;
821
822 /*
823 * If memory allocation is successful, caller of this function
824 * frees it.
825 */
826 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
827 if (!ses->ntlmssp) {
828 rc = -ENOMEM;
829 goto out_err;
830 }
831 ses->ntlmssp->sesskey_per_smbsess = true;
832
833 rc = SMB2_sess_alloc_buffer(sess_data);
834 if (rc)
835 goto out_err;
836
837 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
838 GFP_KERNEL);
839 if (ntlmssp_blob == NULL) {
840 rc = -ENOMEM;
841 goto out;
842 }
843
844 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
845 if (use_spnego) {
846 /* BB eventually need to add this */
847 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
848 rc = -EOPNOTSUPP;
849 goto out;
850 } else {
851 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
852 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
853 }
854 sess_data->iov[1].iov_base = ntlmssp_blob;
855 sess_data->iov[1].iov_len = blob_length;
856
857 rc = SMB2_sess_sendreceive(sess_data);
858 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
859
860 /* If true, rc here is expected and not an error */
861 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
862 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
863 rc = 0;
864
865 if (rc)
866 goto out;
867
868 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
869 le16_to_cpu(rsp->SecurityBufferOffset)) {
870 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
871 le16_to_cpu(rsp->SecurityBufferOffset));
872 rc = -EIO;
873 goto out;
874 }
875 rc = decode_ntlmssp_challenge(rsp->Buffer,
876 le16_to_cpu(rsp->SecurityBufferLength), ses);
877 if (rc)
878 goto out;
879
880 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
881
882
883 ses->Suid = rsp->hdr.SessionId;
884 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
885 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
886 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
887
888out:
889 kfree(ntlmssp_blob);
890 SMB2_sess_free_buffer(sess_data);
891 if (!rc) {
892 sess_data->result = 0;
893 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
894 return;
895 }
896out_err:
897 kfree(ses->ntlmssp);
898 ses->ntlmssp = NULL;
899 sess_data->result = rc;
900 sess_data->func = NULL;
901}
902
903static void
904SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
905{
906 int rc;
907 struct cifs_ses *ses = sess_data->ses;
908 struct smb2_sess_setup_req *req;
909 struct smb2_sess_setup_rsp *rsp = NULL;
910 unsigned char *ntlmssp_blob = NULL;
911 bool use_spnego = false; /* else use raw ntlmssp */
912 u16 blob_length = 0;
913
914 rc = SMB2_sess_alloc_buffer(sess_data);
915 if (rc)
916 goto out;
917
918 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
919 req->hdr.SessionId = ses->Suid;
920
921 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
922 sess_data->nls_cp);
923 if (rc) {
924 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
925 goto out;
926 }
927
928 if (use_spnego) {
929 /* BB eventually need to add this */
930 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
931 rc = -EOPNOTSUPP;
932 goto out;
933 }
934 sess_data->iov[1].iov_base = ntlmssp_blob;
935 sess_data->iov[1].iov_len = blob_length;
936
937 rc = SMB2_sess_sendreceive(sess_data);
938 if (rc)
939 goto out;
940
941 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
942
943 ses->Suid = rsp->hdr.SessionId;
944 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
945 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
946 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
947
948 rc = SMB2_sess_establish_session(sess_data);
949out:
950 kfree(ntlmssp_blob);
951 SMB2_sess_free_buffer(sess_data);
952 kfree(ses->ntlmssp);
953 ses->ntlmssp = NULL;
954 sess_data->result = rc;
955 sess_data->func = NULL;
956}
957
958static int
959SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
960{
961 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
962 ses->sectype = RawNTLMSSP;
963
964 switch (ses->sectype) {
965 case Kerberos:
966 sess_data->func = SMB2_auth_kerberos;
967 break;
968 case RawNTLMSSP:
969 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
970 break;
971 default:
972 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
973 return -EOPNOTSUPP;
974 }
975
976 return 0;
977}
978
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400979int
980SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
981 const struct nls_table *nls_cp)
982{
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400983 int rc = 0;
Jeff Layton3534b852013-05-24 07:41:01 -0400984 struct TCP_Server_Info *server = ses->server;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100985 struct SMB2_sess_data *sess_data;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400986
Joe Perchesf96637b2013-05-04 22:12:25 -0500987 cifs_dbg(FYI, "Session Setup\n");
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400988
Jeff Layton3534b852013-05-24 07:41:01 -0400989 if (!server) {
990 WARN(1, "%s: server is NULL!\n", __func__);
991 return -EIO;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400992 }
993
Sachin Prabhu3baf1a72016-10-07 19:11:21 +0100994 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
995 if (!sess_data)
996 return -ENOMEM;
Sachin Prabhu166cea42016-10-07 19:11:22 +0100997
998 rc = SMB2_select_sec(ses, sess_data);
999 if (rc)
1000 goto out;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001001 sess_data->xid = xid;
1002 sess_data->ses = ses;
1003 sess_data->buf0_type = CIFS_NO_BUFFER;
1004 sess_data->nls_cp = (struct nls_table *) nls_cp;
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001005
Sachin Prabhu166cea42016-10-07 19:11:22 +01001006 while (sess_data->func)
1007 sess_data->func(sess_data);
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001008
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001009 rc = sess_data->result;
Sachin Prabhu166cea42016-10-07 19:11:22 +01001010out:
Sachin Prabhu3baf1a72016-10-07 19:11:21 +01001011 kfree(sess_data);
1012 return rc;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001013}
1014
1015int
1016SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1017{
1018 struct smb2_logoff_req *req; /* response is also trivial struct */
1019 int rc = 0;
1020 struct TCP_Server_Info *server;
1021
Joe Perchesf96637b2013-05-04 22:12:25 -05001022 cifs_dbg(FYI, "disconnect session %p\n", ses);
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001023
1024 if (ses && (ses->server))
1025 server = ses->server;
1026 else
1027 return -EIO;
1028
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001029 /* no need to send SMB logoff if uid already closed due to reconnect */
1030 if (ses->need_reconnect)
1031 goto smb2_session_already_dead;
1032
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001033 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1034 if (rc)
1035 return rc;
1036
1037 /* since no tcon, smb2_init can not do this, so do here */
1038 req->hdr.SessionId = ses->Suid;
Jeff Layton38d77c52013-05-26 07:01:00 -04001039 if (server->sign)
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07001040 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001041
1042 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1043 /*
1044 * No tcon so can't do
1045 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1046 */
Shirish Pargaonkareb4c7df2013-10-03 05:44:45 -05001047
1048smb2_session_already_dead:
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001049 return rc;
1050}
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001051
1052static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1053{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001054 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001055}
1056
1057#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1058
Steve Frenchde9f68d2013-11-15 11:26:24 -06001059/* These are similar values to what Windows uses */
1060static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1061{
1062 tcon->max_chunks = 256;
1063 tcon->max_bytes_chunk = 1048576;
1064 tcon->max_bytes_copy = 16777216;
1065}
1066
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001067int
1068SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1069 struct cifs_tcon *tcon, const struct nls_table *cp)
1070{
1071 struct smb2_tree_connect_req *req;
1072 struct smb2_tree_connect_rsp *rsp = NULL;
1073 struct kvec iov[2];
1074 int rc = 0;
1075 int resp_buftype;
1076 int unc_path_len;
1077 struct TCP_Server_Info *server;
1078 __le16 *unc_path = NULL;
1079
Joe Perchesf96637b2013-05-04 22:12:25 -05001080 cifs_dbg(FYI, "TCON\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001081
1082 if ((ses->server) && tree)
1083 server = ses->server;
1084 else
1085 return -EIO;
1086
1087 if (tcon && tcon->bad_network_name)
1088 return -ENOENT;
1089
Steve Frenchff9f84b2015-09-26 09:48:58 -05001090 if ((tcon && tcon->seal) &&
Steve French88627142015-09-22 03:16:27 -05001091 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1092 cifs_dbg(VFS, "encryption requested but no server support");
1093 return -EOPNOTSUPP;
1094 }
1095
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001096 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1097 if (unc_path == NULL)
1098 return -ENOMEM;
1099
1100 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1101 unc_path_len *= 2;
1102 if (unc_path_len < 2) {
1103 kfree(unc_path);
1104 return -EINVAL;
1105 }
1106
Jan-Marek Glogowski8446cb12017-02-20 12:25:58 +01001107 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1108 if (tcon)
1109 tcon->tid = 0;
1110
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001111 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1112 if (rc) {
1113 kfree(unc_path);
1114 return rc;
1115 }
1116
1117 if (tcon == NULL) {
1118 /* since no tcon, smb2_init can not do this, so do here */
1119 req->hdr.SessionId = ses->Suid;
1120 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1121 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1122 }
1123
1124 iov[0].iov_base = (char *)req;
1125 /* 4 for rfc1002 length field and 1 for pad */
1126 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1127
1128 /* Testing shows that buffer offset must be at location of Buffer[0] */
1129 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1130 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1131 req->PathLength = cpu_to_le16(unc_path_len - 2);
1132 iov[1].iov_base = unc_path;
1133 iov[1].iov_len = unc_path_len;
1134
1135 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1136
1137 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1138 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1139
1140 if (rc != 0) {
1141 if (tcon) {
1142 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1143 tcon->need_reconnect = true;
1144 }
1145 goto tcon_error_exit;
1146 }
1147
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001148 if (tcon == NULL) {
1149 ses->ipc_tid = rsp->hdr.TreeId;
1150 goto tcon_exit;
1151 }
1152
1153 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001154 cifs_dbg(FYI, "connection to disk share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001155 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1156 tcon->ipc = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001157 cifs_dbg(FYI, "connection to pipe share\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001158 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1159 tcon->print = true;
Joe Perchesf96637b2013-05-04 22:12:25 -05001160 cifs_dbg(FYI, "connection to printer\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001161 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001162 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001163 rc = -EOPNOTSUPP;
1164 goto tcon_error_exit;
1165 }
1166
1167 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
Steve French769ee6a2013-06-19 14:15:30 -05001168 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001169 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1170 tcon->tidStatus = CifsGood;
1171 tcon->need_reconnect = false;
1172 tcon->tid = rsp->hdr.TreeId;
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05001173 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001174
1175 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1176 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
Joe Perchesf96637b2013-05-04 22:12:25 -05001177 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
Steve Frenchde9f68d2013-11-15 11:26:24 -06001178 init_copy_chunk_defaults(tcon);
Steve French88627142015-09-22 03:16:27 -05001179 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1180 cifs_dbg(VFS, "Encrypted shares not supported");
Steve Frenchff1c0382013-11-19 23:44:46 -06001181 if (tcon->ses->server->ops->validate_negotiate)
1182 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001183tcon_exit:
1184 free_rsp_buf(resp_buftype, rsp);
1185 kfree(unc_path);
1186 return rc;
1187
1188tcon_error_exit:
1189 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001190 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
Steve French18f39e72014-08-17 00:22:24 -05001191 if (tcon)
1192 tcon->bad_network_name = true;
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001193 }
1194 goto tcon_exit;
1195}
1196
1197int
1198SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1199{
1200 struct smb2_tree_disconnect_req *req; /* response is trivial */
1201 int rc = 0;
1202 struct TCP_Server_Info *server;
1203 struct cifs_ses *ses = tcon->ses;
1204
Joe Perchesf96637b2013-05-04 22:12:25 -05001205 cifs_dbg(FYI, "Tree Disconnect\n");
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001206
1207 if (ses && (ses->server))
1208 server = ses->server;
1209 else
1210 return -EIO;
1211
1212 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1213 return 0;
1214
1215 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1216 if (rc)
1217 return rc;
1218
1219 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1220 if (rc)
1221 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1222
1223 return rc;
1224}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001225
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001226
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001227static struct create_durable *
1228create_durable_buf(void)
1229{
1230 struct create_durable *buf;
1231
1232 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1233 if (!buf)
1234 return NULL;
1235
1236 buf->ccontext.DataOffset = cpu_to_le16(offsetof
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001237 (struct create_durable, Data));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001238 buf->ccontext.DataLength = cpu_to_le32(16);
1239 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1240 (struct create_durable, Name));
1241 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001242 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001243 buf->Name[0] = 'D';
1244 buf->Name[1] = 'H';
1245 buf->Name[2] = 'n';
1246 buf->Name[3] = 'Q';
1247 return buf;
1248}
1249
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001250static struct create_durable *
1251create_reconnect_durable_buf(struct cifs_fid *fid)
1252{
1253 struct create_durable *buf;
1254
1255 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1256 if (!buf)
1257 return NULL;
1258
1259 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1260 (struct create_durable, Data));
1261 buf->ccontext.DataLength = cpu_to_le32(16);
1262 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1263 (struct create_durable, Name));
1264 buf->ccontext.NameLength = cpu_to_le16(4);
1265 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1266 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
Steve French12197a72014-05-14 05:29:40 -07001267 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001268 buf->Name[0] = 'D';
1269 buf->Name[1] = 'H';
1270 buf->Name[2] = 'n';
1271 buf->Name[3] = 'C';
1272 return buf;
1273}
1274
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001275static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001276parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1277 unsigned int *epoch)
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001278{
1279 char *data_offset;
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001280 struct create_context *cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001281 unsigned int next;
1282 unsigned int remaining;
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001283 char *name;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001284
Pavel Shilovskyfd554392013-07-09 19:44:56 +04001285 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
Justin Maggarddeb7def2016-02-09 15:52:08 -08001286 remaining = le32_to_cpu(rsp->CreateContextsLength);
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001287 cc = (struct create_context *)data_offset;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001288 while (remaining >= sizeof(struct create_context)) {
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001289 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
Justin Maggarddeb7def2016-02-09 15:52:08 -08001290 if (le16_to_cpu(cc->NameLength) == 4 &&
1291 strncmp(name, "RqLs", 4) == 0)
1292 return server->ops->parse_lease_buf(cc, epoch);
1293
1294 next = le32_to_cpu(cc->Next);
1295 if (!next)
1296 break;
1297 remaining -= next;
1298 cc = (struct create_context *)((char *)cc + next);
1299 }
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001300
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001301 return 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001302}
1303
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001304static int
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001305add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1306 unsigned int *num_iovec, __u8 *oplock)
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001307{
1308 struct smb2_create_req *req = iov[0].iov_base;
1309 unsigned int num = *num_iovec;
1310
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001311 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001312 if (iov[num].iov_base == NULL)
1313 return -ENOMEM;
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001314 iov[num].iov_len = server->vals->create_lease_size;
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001315 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1316 if (!req->CreateContextsOffset)
1317 req->CreateContextsOffset = cpu_to_le32(
1318 sizeof(struct smb2_create_req) - 4 +
1319 iov[num - 1].iov_len);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001320 le32_add_cpu(&req->CreateContextsLength,
1321 server->vals->create_lease_size);
1322 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001323 *num_iovec = num + 1;
1324 return 0;
1325}
1326
Steve Frenchb56eae42015-11-03 09:26:27 -06001327static struct create_durable_v2 *
1328create_durable_v2_buf(struct cifs_fid *pfid)
1329{
1330 struct create_durable_v2 *buf;
1331
1332 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1333 if (!buf)
1334 return NULL;
1335
1336 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1337 (struct create_durable_v2, dcontext));
1338 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1339 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1340 (struct create_durable_v2, Name));
1341 buf->ccontext.NameLength = cpu_to_le16(4);
1342
1343 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1344 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
Steve Frenchfa70b872016-09-22 00:39:34 -05001345 generate_random_uuid(buf->dcontext.CreateGuid);
Steve Frenchb56eae42015-11-03 09:26:27 -06001346 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1347
1348 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1349 buf->Name[0] = 'D';
1350 buf->Name[1] = 'H';
1351 buf->Name[2] = '2';
1352 buf->Name[3] = 'Q';
1353 return buf;
1354}
1355
1356static struct create_durable_handle_reconnect_v2 *
1357create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1358{
1359 struct create_durable_handle_reconnect_v2 *buf;
1360
1361 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1362 GFP_KERNEL);
1363 if (!buf)
1364 return NULL;
1365
1366 buf->ccontext.DataOffset =
1367 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1368 dcontext));
1369 buf->ccontext.DataLength =
1370 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1371 buf->ccontext.NameOffset =
1372 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1373 Name));
1374 buf->ccontext.NameLength = cpu_to_le16(4);
1375
1376 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1377 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1378 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1379 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1380
1381 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1382 buf->Name[0] = 'D';
1383 buf->Name[1] = 'H';
1384 buf->Name[2] = '2';
1385 buf->Name[3] = 'C';
1386 return buf;
1387}
1388
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001389static int
Steve Frenchb56eae42015-11-03 09:26:27 -06001390add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001391 struct cifs_open_parms *oparms)
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001392{
1393 struct smb2_create_req *req = iov[0].iov_base;
1394 unsigned int num = *num_iovec;
1395
Steve Frenchb56eae42015-11-03 09:26:27 -06001396 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1397 if (iov[num].iov_base == NULL)
1398 return -ENOMEM;
1399 iov[num].iov_len = sizeof(struct create_durable_v2);
1400 if (!req->CreateContextsOffset)
1401 req->CreateContextsOffset =
1402 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1403 iov[1].iov_len);
1404 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1405 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1406 *num_iovec = num + 1;
1407 return 0;
1408}
1409
1410static int
1411add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1412 struct cifs_open_parms *oparms)
1413{
1414 struct smb2_create_req *req = iov[0].iov_base;
1415 unsigned int num = *num_iovec;
1416
1417 /* indicate that we don't need to relock the file */
1418 oparms->reconnect = false;
1419
1420 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1421 if (iov[num].iov_base == NULL)
1422 return -ENOMEM;
1423 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1424 if (!req->CreateContextsOffset)
1425 req->CreateContextsOffset =
1426 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1427 iov[1].iov_len);
1428 le32_add_cpu(&req->CreateContextsLength,
1429 sizeof(struct create_durable_handle_reconnect_v2));
1430 inc_rfc1001_len(&req->hdr,
1431 sizeof(struct create_durable_handle_reconnect_v2));
1432 *num_iovec = num + 1;
1433 return 0;
1434}
1435
1436static int
1437add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1438 struct cifs_open_parms *oparms, bool use_persistent)
1439{
1440 struct smb2_create_req *req = iov[0].iov_base;
1441 unsigned int num = *num_iovec;
1442
1443 if (use_persistent) {
1444 if (oparms->reconnect)
1445 return add_durable_reconnect_v2_context(iov, num_iovec,
1446 oparms);
1447 else
1448 return add_durable_v2_context(iov, num_iovec, oparms);
1449 }
1450
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001451 if (oparms->reconnect) {
1452 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1453 /* indicate that we don't need to relock the file */
1454 oparms->reconnect = false;
1455 } else
1456 iov[num].iov_base = create_durable_buf();
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001457 if (iov[num].iov_base == NULL)
1458 return -ENOMEM;
1459 iov[num].iov_len = sizeof(struct create_durable);
1460 if (!req->CreateContextsOffset)
1461 req->CreateContextsOffset =
1462 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1463 iov[1].iov_len);
Wei Yongjun31f92e92013-08-26 14:34:46 +08001464 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001465 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1466 *num_iovec = num + 1;
1467 return 0;
1468}
1469
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001470int
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001471SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001472 __u8 *oplock, struct smb2_file_all_info *buf,
1473 struct smb2_err_rsp **err_buf)
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001474{
1475 struct smb2_create_req *req;
1476 struct smb2_create_rsp *rsp;
1477 struct TCP_Server_Info *server;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001478 struct cifs_tcon *tcon = oparms->tcon;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001479 struct cifs_ses *ses = tcon->ses;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001480 struct kvec iov[4];
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001481 int resp_buftype;
1482 int uni_path_len;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001483 __le16 *copy_path = NULL;
1484 int copy_size;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001485 int rc = 0;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001486 unsigned int num_iovecs = 2;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001487 __u32 file_attributes = 0;
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001488 char *dhc_buf = NULL, *lc_buf = NULL;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001489
Joe Perchesf96637b2013-05-04 22:12:25 -05001490 cifs_dbg(FYI, "create/open\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001491
1492 if (ses && (ses->server))
1493 server = ses->server;
1494 else
1495 return -EIO;
1496
1497 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1498 if (rc)
1499 return rc;
1500
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001501 if (oparms->create_options & CREATE_OPTION_READONLY)
Pavel Shilovskyca819832013-07-05 12:21:26 +04001502 file_attributes |= ATTR_READONLY;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001503 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1504 file_attributes |= ATTR_SYSTEM;
Pavel Shilovskyca819832013-07-05 12:21:26 +04001505
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001506 req->ImpersonationLevel = IL_IMPERSONATION;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001507 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001508 /* File attributes ignored on open (used in create though) */
1509 req->FileAttributes = cpu_to_le32(file_attributes);
1510 req->ShareAccess = FILE_SHARE_ALL_LE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001511 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1512 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001513 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001514 /* do not count rfc1001 len field */
1515 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001516
1517 iov[0].iov_base = (char *)req;
1518 /* 4 for rfc1002 length field */
1519 iov[0].iov_len = get_rfc1002_length(req) + 4;
1520
1521 /* MUST set path len (NameLength) to 0 opening root of share */
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001522 req->NameLength = cpu_to_le16(uni_path_len - 2);
1523 /* -1 since last byte is buf[0] which is sent below (path) */
1524 iov[0].iov_len--;
1525 if (uni_path_len % 8 != 0) {
1526 copy_size = uni_path_len / 8 * 8;
1527 if (copy_size < uni_path_len)
1528 copy_size += 8;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001529
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001530 copy_path = kzalloc(copy_size, GFP_KERNEL);
1531 if (!copy_path)
1532 return -ENOMEM;
1533 memcpy((char *)copy_path, (const char *)path,
1534 uni_path_len);
1535 uni_path_len = copy_size;
1536 path = copy_path;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001537 }
1538
Pavel Shilovsky59aa3712013-07-04 19:41:24 +04001539 iov[1].iov_len = uni_path_len;
1540 iov[1].iov_base = path;
1541 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1542 inc_rfc1001_len(req, uni_path_len - 1);
1543
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001544 if (!server->oplocks)
1545 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1546
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001547 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001548 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1549 req->RequestedOplockLevel = *oplock;
1550 else {
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001551 rc = add_lease_context(server, iov, &num_iovecs, oplock);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001552 if (rc) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001553 cifs_small_buf_release(req);
1554 kfree(copy_path);
Pavel Shilovskyd22cbfe2013-07-04 19:10:00 +04001555 return rc;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001556 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001557 lc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001558 }
1559
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001560 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1561 /* need to set Next field of lease context if we request it */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001562 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001563 struct create_context *ccontext =
1564 (struct create_context *)iov[num_iovecs-1].iov_base;
Steve French1c469432013-07-10 12:50:57 -05001565 ccontext->Next =
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001566 cpu_to_le32(server->vals->create_lease_size);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001567 }
Steve Frenchb56eae42015-11-03 09:26:27 -06001568
1569 rc = add_durable_context(iov, &num_iovecs, oparms,
1570 tcon->use_persistent);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001571 if (rc) {
1572 cifs_small_buf_release(req);
1573 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001574 kfree(lc_buf);
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001575 return rc;
1576 }
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001577 dhc_buf = iov[num_iovecs-1].iov_base;
Pavel Shilovsky63eb3de2013-07-04 18:41:09 +04001578 }
1579
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001580 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1581 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1582
1583 if (rc != 0) {
1584 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001585 if (err_buf)
1586 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1587 GFP_KERNEL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001588 goto creat_exit;
1589 }
1590
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001591 oparms->fid->persistent_fid = rsp->PersistentFileId;
1592 oparms->fid->volatile_fid = rsp->VolatileFileId;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001593
1594 if (buf) {
1595 memcpy(buf, &rsp->CreationTime, 32);
1596 buf->AllocationSize = rsp->AllocationSize;
1597 buf->EndOfFile = rsp->EndofFile;
1598 buf->Attributes = rsp->FileAttributes;
1599 buf->NumberOfLinks = cpu_to_le32(1);
1600 buf->DeletePending = 0;
1601 }
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001602
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001603 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001604 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001605 else
1606 *oplock = rsp->OplockLevel;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001607creat_exit:
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001608 kfree(copy_path);
Pavel Shilovsky663a96212014-05-24 16:42:02 +04001609 kfree(lc_buf);
1610 kfree(dhc_buf);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001611 free_rsp_buf(resp_buftype, rsp);
1612 return rc;
1613}
1614
Steve French4a72daf2013-06-25 00:20:49 -05001615/*
1616 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1617 */
1618int
1619SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1620 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1621 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1622{
1623 struct smb2_ioctl_req *req;
1624 struct smb2_ioctl_rsp *rsp;
1625 struct TCP_Server_Info *server;
Steve French8e353102015-03-26 19:47:02 -05001626 struct cifs_ses *ses;
Steve French4a72daf2013-06-25 00:20:49 -05001627 struct kvec iov[2];
1628 int resp_buftype;
1629 int num_iovecs;
1630 int rc = 0;
1631
1632 cifs_dbg(FYI, "SMB2 IOCTL\n");
1633
Steve French3d1a3742014-08-11 21:05:25 -05001634 if (out_data != NULL)
1635 *out_data = NULL;
1636
Steve French4a72daf2013-06-25 00:20:49 -05001637 /* zero out returned data len, in case of error */
1638 if (plen)
1639 *plen = 0;
1640
Steve French8e353102015-03-26 19:47:02 -05001641 if (tcon)
1642 ses = tcon->ses;
1643 else
1644 return -EIO;
1645
Steve French4a72daf2013-06-25 00:20:49 -05001646 if (ses && (ses->server))
1647 server = ses->server;
1648 else
1649 return -EIO;
1650
1651 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1652 if (rc)
1653 return rc;
1654
1655 req->CtlCode = cpu_to_le32(opcode);
1656 req->PersistentFileId = persistent_fid;
1657 req->VolatileFileId = volatile_fid;
1658
1659 if (indatalen) {
1660 req->InputCount = cpu_to_le32(indatalen);
1661 /* do not set InputOffset if no input data */
1662 req->InputOffset =
1663 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1664 iov[1].iov_base = in_data;
1665 iov[1].iov_len = indatalen;
1666 num_iovecs = 2;
1667 } else
1668 num_iovecs = 1;
1669
1670 req->OutputOffset = 0;
1671 req->OutputCount = 0; /* MBZ */
1672
1673 /*
1674 * Could increase MaxOutputResponse, but that would require more
1675 * than one credit. Windows typically sets this smaller, but for some
1676 * ioctls it may be useful to allow server to send more. No point
1677 * limiting what the server can send as long as fits in one credit
1678 */
1679 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1680
1681 if (is_fsctl)
1682 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1683 else
1684 req->Flags = 0;
1685
1686 iov[0].iov_base = (char *)req;
Steve French4a72daf2013-06-25 00:20:49 -05001687
Steve French7ff8d452013-10-14 00:44:19 -05001688 /*
1689 * If no input data, the size of ioctl struct in
1690 * protocol spec still includes a 1 byte data buffer,
1691 * but if input data passed to ioctl, we do not
1692 * want to double count this, so we do not send
1693 * the dummy one byte of data in iovec[0] if sending
1694 * input data (in iovec[1]). We also must add 4 bytes
1695 * in first iovec to allow for rfc1002 length field.
1696 */
1697
1698 if (indatalen) {
1699 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1700 inc_rfc1001_len(req, indatalen - 1);
1701 } else
1702 iov[0].iov_len = get_rfc1002_length(req) + 4;
1703
Steve French4a72daf2013-06-25 00:20:49 -05001704
1705 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1706 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1707
Steve French9bf0c9c2013-11-16 18:05:28 -06001708 if ((rc != 0) && (rc != -EINVAL)) {
Steve French8e353102015-03-26 19:47:02 -05001709 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French4a72daf2013-06-25 00:20:49 -05001710 goto ioctl_exit;
Steve French9bf0c9c2013-11-16 18:05:28 -06001711 } else if (rc == -EINVAL) {
1712 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1713 (opcode != FSCTL_SRV_COPYCHUNK)) {
Steve French8e353102015-03-26 19:47:02 -05001714 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
Steve French9bf0c9c2013-11-16 18:05:28 -06001715 goto ioctl_exit;
1716 }
Steve French4a72daf2013-06-25 00:20:49 -05001717 }
1718
1719 /* check if caller wants to look at return data or just return rc */
1720 if ((plen == NULL) || (out_data == NULL))
1721 goto ioctl_exit;
1722
1723 *plen = le32_to_cpu(rsp->OutputCount);
1724
1725 /* We check for obvious errors in the output buffer length and offset */
1726 if (*plen == 0)
1727 goto ioctl_exit; /* server returned no data */
1728 else if (*plen > 0xFF00) {
1729 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1730 *plen = 0;
1731 rc = -EIO;
1732 goto ioctl_exit;
1733 }
1734
1735 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1736 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1737 le32_to_cpu(rsp->OutputOffset));
1738 *plen = 0;
1739 rc = -EIO;
1740 goto ioctl_exit;
1741 }
1742
1743 *out_data = kmalloc(*plen, GFP_KERNEL);
1744 if (*out_data == NULL) {
1745 rc = -ENOMEM;
1746 goto ioctl_exit;
1747 }
1748
Steve French373512e2015-12-18 13:05:30 -06001749 memcpy(*out_data,
1750 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
Steve French4a72daf2013-06-25 00:20:49 -05001751 *plen);
1752ioctl_exit:
1753 free_rsp_buf(resp_buftype, rsp);
1754 return rc;
1755}
1756
Steve French64a5cfa2013-10-14 15:31:32 -05001757/*
1758 * Individual callers to ioctl worker function follow
1759 */
1760
1761int
1762SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1763 u64 persistent_fid, u64 volatile_fid)
1764{
1765 int rc;
Steve French64a5cfa2013-10-14 15:31:32 -05001766 struct compress_ioctl fsctl_input;
1767 char *ret_data = NULL;
1768
1769 fsctl_input.CompressionState =
Fabian Frederickbc09d142014-12-10 15:41:15 -08001770 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
Steve French64a5cfa2013-10-14 15:31:32 -05001771
1772 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1773 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1774 (char *)&fsctl_input /* data input */,
1775 2 /* in data len */, &ret_data /* out data */, NULL);
1776
1777 cifs_dbg(FYI, "set compression rc %d\n", rc);
Steve French64a5cfa2013-10-14 15:31:32 -05001778
1779 return rc;
1780}
1781
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001782int
1783SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1784 u64 persistent_fid, u64 volatile_fid)
1785{
1786 struct smb2_close_req *req;
1787 struct smb2_close_rsp *rsp;
1788 struct TCP_Server_Info *server;
1789 struct cifs_ses *ses = tcon->ses;
1790 struct kvec iov[1];
1791 int resp_buftype;
1792 int rc = 0;
1793
Joe Perchesf96637b2013-05-04 22:12:25 -05001794 cifs_dbg(FYI, "Close\n");
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001795
1796 if (ses && (ses->server))
1797 server = ses->server;
1798 else
1799 return -EIO;
1800
1801 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1802 if (rc)
1803 return rc;
1804
1805 req->PersistentFileId = persistent_fid;
1806 req->VolatileFileId = volatile_fid;
1807
1808 iov[0].iov_base = (char *)req;
1809 /* 4 for rfc1002 length field */
1810 iov[0].iov_len = get_rfc1002_length(req) + 4;
1811
1812 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1813 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1814
1815 if (rc != 0) {
Namjae Jeond4a029d2014-08-20 19:39:59 +09001816 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001817 goto close_exit;
1818 }
1819
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001820 /* BB FIXME - decode close response, update inode for caching */
1821
1822close_exit:
1823 free_rsp_buf(resp_buftype, rsp);
1824 return rc;
1825}
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001826
1827static int
1828validate_buf(unsigned int offset, unsigned int buffer_length,
1829 struct smb2_hdr *hdr, unsigned int min_buf_size)
1830
1831{
1832 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1833 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1834 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1835 char *end_of_buf = begin_of_buf + buffer_length;
1836
1837
1838 if (buffer_length < min_buf_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001839 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1840 buffer_length, min_buf_size);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001841 return -EINVAL;
1842 }
1843
1844 /* check if beyond RFC1001 maximum length */
1845 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001846 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1847 buffer_length, smb_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001848 return -EINVAL;
1849 }
1850
1851 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001852 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001853 return -EINVAL;
1854 }
1855
1856 return 0;
1857}
1858
1859/*
1860 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1861 * Caller must free buffer.
1862 */
1863static int
1864validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1865 struct smb2_hdr *hdr, unsigned int minbufsize,
1866 char *data)
1867
1868{
1869 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1870 int rc;
1871
1872 if (!data)
1873 return -EINVAL;
1874
1875 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1876 if (rc)
1877 return rc;
1878
1879 memcpy(data, begin_of_buf, buffer_length);
1880
1881 return 0;
1882}
1883
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001884static int
1885query_info(const unsigned int xid, struct cifs_tcon *tcon,
1886 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1887 size_t output_len, size_t min_len, void *data)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001888{
1889 struct smb2_query_info_req *req;
1890 struct smb2_query_info_rsp *rsp = NULL;
1891 struct kvec iov[2];
1892 int rc = 0;
1893 int resp_buftype;
1894 struct TCP_Server_Info *server;
1895 struct cifs_ses *ses = tcon->ses;
1896
Joe Perchesf96637b2013-05-04 22:12:25 -05001897 cifs_dbg(FYI, "Query Info\n");
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001898
1899 if (ses && (ses->server))
1900 server = ses->server;
1901 else
1902 return -EIO;
1903
1904 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1905 if (rc)
1906 return rc;
1907
1908 req->InfoType = SMB2_O_INFO_FILE;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001909 req->FileInfoClass = info_class;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001910 req->PersistentFileId = persistent_fid;
1911 req->VolatileFileId = volatile_fid;
1912 /* 4 for rfc1002 length field and 1 for Buffer */
1913 req->InputBufferOffset =
1914 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001915 req->OutputBufferLength = cpu_to_le32(output_len);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001916
1917 iov[0].iov_base = (char *)req;
1918 /* 4 for rfc1002 length field */
1919 iov[0].iov_len = get_rfc1002_length(req) + 4;
1920
1921 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04001922 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1923
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001924 if (rc) {
1925 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1926 goto qinf_exit;
1927 }
1928
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001929 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1930 le32_to_cpu(rsp->OutputBufferLength),
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001931 &rsp->hdr, min_len, data);
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001932
1933qinf_exit:
1934 free_rsp_buf(resp_buftype, rsp);
1935 return rc;
1936}
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001937
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001938int
1939SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1940 u64 persistent_fid, u64 volatile_fid,
1941 struct smb2_file_all_info *data)
1942{
1943 return query_info(xid, tcon, persistent_fid, volatile_fid,
1944 FILE_ALL_INFORMATION,
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001945 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001946 sizeof(struct smb2_file_all_info), data);
1947}
1948
1949int
1950SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1951 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1952{
1953 return query_info(xid, tcon, persistent_fid, volatile_fid,
1954 FILE_INTERNAL_INFORMATION,
1955 sizeof(struct smb2_file_internal_info),
1956 sizeof(struct smb2_file_internal_info), uniqueid);
1957}
1958
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001959/*
1960 * This is a no-op for now. We're not really interested in the reply, but
1961 * rather in the fact that the server sent one and that server->lstrp
1962 * gets updated.
1963 *
1964 * FIXME: maybe we should consider checking that the reply matches request?
1965 */
1966static void
1967smb2_echo_callback(struct mid_q_entry *mid)
1968{
1969 struct TCP_Server_Info *server = mid->callback_data;
1970 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1971 unsigned int credits_received = 1;
1972
1973 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1974 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1975
Christopher Oo5fb4e282015-06-25 16:10:48 -07001976 mutex_lock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001977 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001978 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001979 add_credits(server, credits_received, CIFS_ECHO_OP);
1980}
1981
Pavel Shilovsky48f95262016-11-04 11:50:31 -07001982void smb2_reconnect_server(struct work_struct *work)
1983{
1984 struct TCP_Server_Info *server = container_of(work,
1985 struct TCP_Server_Info, reconnect.work);
1986 struct cifs_ses *ses;
1987 struct cifs_tcon *tcon, *tcon2;
1988 struct list_head tmp_list;
1989 int tcon_exist = false;
1990
1991 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
1992 mutex_lock(&server->reconnect_mutex);
1993
1994 INIT_LIST_HEAD(&tmp_list);
1995 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
1996
1997 spin_lock(&cifs_tcp_ses_lock);
1998 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1999 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002000 if (tcon->need_reconnect || tcon->need_reopen_files) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002001 tcon->tc_count++;
2002 list_add_tail(&tcon->rlist, &tmp_list);
2003 tcon_exist = true;
2004 }
2005 }
2006 }
2007 /*
2008 * Get the reference to server struct to be sure that the last call of
2009 * cifs_put_tcon() in the loop below won't release the server pointer.
2010 */
2011 if (tcon_exist)
2012 server->srv_count++;
2013
2014 spin_unlock(&cifs_tcp_ses_lock);
2015
2016 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
Pavel Shilovsky46890ff2016-11-29 11:31:23 -08002017 if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon))
2018 cifs_reopen_persistent_handles(tcon);
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002019 list_del_init(&tcon->rlist);
2020 cifs_put_tcon(tcon);
2021 }
2022
2023 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2024 mutex_unlock(&server->reconnect_mutex);
2025
2026 /* now we can safely release srv struct */
2027 if (tcon_exist)
2028 cifs_put_tcp_session(server, 1);
2029}
2030
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002031int
2032SMB2_echo(struct TCP_Server_Info *server)
2033{
2034 struct smb2_echo_req *req;
2035 int rc = 0;
2036 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002037 struct smb_rqst rqst = { .rq_iov = &iov,
2038 .rq_nvec = 1 };
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002039
Joe Perchesf96637b2013-05-04 22:12:25 -05002040 cifs_dbg(FYI, "In echo request\n");
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002041
Steve French4fcd1812016-06-22 20:12:05 -05002042 if (server->tcpStatus == CifsNeedNegotiate) {
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002043 /* No need to send echo on newly established connections */
2044 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2045 return rc;
Steve French4fcd1812016-06-22 20:12:05 -05002046 }
2047
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002048 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2049 if (rc)
2050 return rc;
2051
2052 req->hdr.CreditRequest = cpu_to_le16(1);
2053
2054 iov.iov_base = (char *)req;
2055 /* 4 for rfc1002 length field */
2056 iov.iov_len = get_rfc1002_length(req) + 4;
2057
Jeff Laytonfec344e2012-09-18 16:20:35 -07002058 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002059 CIFS_ECHO_OP);
2060 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002061 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002062
2063 cifs_small_buf_release(req);
2064 return rc;
2065}
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002066
2067int
2068SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2069 u64 volatile_fid)
2070{
2071 struct smb2_flush_req *req;
2072 struct TCP_Server_Info *server;
2073 struct cifs_ses *ses = tcon->ses;
2074 struct kvec iov[1];
2075 int resp_buftype;
2076 int rc = 0;
2077
Joe Perchesf96637b2013-05-04 22:12:25 -05002078 cifs_dbg(FYI, "Flush\n");
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002079
2080 if (ses && (ses->server))
2081 server = ses->server;
2082 else
2083 return -EIO;
2084
2085 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2086 if (rc)
2087 return rc;
2088
2089 req->PersistentFileId = persistent_fid;
2090 req->VolatileFileId = volatile_fid;
2091
2092 iov[0].iov_base = (char *)req;
2093 /* 4 for rfc1002 length field */
2094 iov[0].iov_len = get_rfc1002_length(req) + 4;
2095
2096 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2097
Steve Frenchdfebe402015-03-27 01:00:06 -05002098 if (rc != 0)
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002099 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2100
2101 free_rsp_buf(resp_buftype, iov[0].iov_base);
2102 return rc;
2103}
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002104
2105/*
2106 * To form a chain of read requests, any read requests after the first should
2107 * have the end_of_chain boolean set to true.
2108 */
2109static int
2110smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2111 unsigned int remaining_bytes, int request_type)
2112{
2113 int rc = -EACCES;
2114 struct smb2_read_req *req = NULL;
2115
2116 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2117 if (rc)
2118 return rc;
2119 if (io_parms->tcon->ses->server == NULL)
2120 return -ECONNABORTED;
2121
2122 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2123
2124 req->PersistentFileId = io_parms->persistent_fid;
2125 req->VolatileFileId = io_parms->volatile_fid;
2126 req->ReadChannelInfoOffset = 0; /* reserved */
2127 req->ReadChannelInfoLength = 0; /* reserved */
2128 req->Channel = 0; /* reserved */
2129 req->MinimumCount = 0;
2130 req->Length = cpu_to_le32(io_parms->length);
2131 req->Offset = cpu_to_le64(io_parms->offset);
2132
2133 if (request_type & CHAINED_REQUEST) {
2134 if (!(request_type & END_OF_CHAIN)) {
2135 /* 4 for rfc1002 length field */
2136 req->hdr.NextCommand =
2137 cpu_to_le32(get_rfc1002_length(req) + 4);
2138 } else /* END_OF_CHAIN */
2139 req->hdr.NextCommand = 0;
2140 if (request_type & RELATED_REQUEST) {
2141 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2142 /*
2143 * Related requests use info from previous read request
2144 * in chain.
2145 */
2146 req->hdr.SessionId = 0xFFFFFFFF;
2147 req->hdr.TreeId = 0xFFFFFFFF;
2148 req->PersistentFileId = 0xFFFFFFFF;
2149 req->VolatileFileId = 0xFFFFFFFF;
2150 }
2151 }
2152 if (remaining_bytes > io_parms->length)
2153 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2154 else
2155 req->RemainingBytes = 0;
2156
2157 iov[0].iov_base = (char *)req;
2158 /* 4 for rfc1002 length field */
2159 iov[0].iov_len = get_rfc1002_length(req) + 4;
2160 return rc;
2161}
2162
2163static void
2164smb2_readv_callback(struct mid_q_entry *mid)
2165{
2166 struct cifs_readdata *rdata = mid->callback_data;
2167 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2168 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07002169 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002170 unsigned int credits_received = 1;
Jeff Layton58195752012-09-19 06:22:34 -07002171 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Layton8321fec2012-09-19 06:22:32 -07002172 .rq_nvec = 1,
2173 .rq_pages = rdata->pages,
2174 .rq_npages = rdata->nr_pages,
2175 .rq_pagesz = rdata->pagesz,
2176 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002177
Joe Perchesf96637b2013-05-04 22:12:25 -05002178 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2179 __func__, mid->mid, mid->mid_state, rdata->result,
2180 rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002181
2182 switch (mid->mid_state) {
2183 case MID_RESPONSE_RECEIVED:
2184 credits_received = le16_to_cpu(buf->CreditRequest);
2185 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04002186 if (server->sign) {
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002187 int rc;
2188
Jeff Layton0b688cf2012-09-18 16:20:34 -07002189 rc = smb2_verify_signature(&rqst, server);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002190 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002191 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2192 rc);
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -07002193 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002194 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04002195 task_io_account_read(rdata->got_bytes);
2196 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002197 break;
2198 case MID_REQUEST_SUBMITTED:
2199 case MID_RETRY_NEEDED:
2200 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04002201 if (server->sign && rdata->got_bytes)
2202 /* reset bytes number since we can not check a sign */
2203 rdata->got_bytes = 0;
2204 /* FIXME: should this be counted toward the initiating task? */
2205 task_io_account_read(rdata->got_bytes);
2206 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002207 break;
2208 default:
2209 if (rdata->result != -ENODATA)
2210 rdata->result = -EIO;
2211 }
2212
2213 if (rdata->result)
2214 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2215
2216 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002217 mutex_lock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002218 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002219 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002220 add_credits(server, credits_received, 0);
2221}
2222
2223/* smb2_async_readv - send an async write, and set up mid to handle result */
2224int
2225smb2_async_readv(struct cifs_readdata *rdata)
2226{
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002227 int rc, flags = 0;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002228 struct smb2_hdr *buf;
2229 struct cifs_io_parms io_parms;
Jeff Layton58195752012-09-19 06:22:34 -07002230 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07002231 .rq_nvec = 1 };
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002232 struct TCP_Server_Info *server;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002233
Joe Perchesf96637b2013-05-04 22:12:25 -05002234 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2235 __func__, rdata->offset, rdata->bytes);
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002236
2237 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2238 io_parms.offset = rdata->offset;
2239 io_parms.length = rdata->bytes;
2240 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2241 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2242 io_parms.pid = rdata->pid;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002243
2244 server = io_parms.tcon->ses->server;
2245
Jeff Layton58195752012-09-19 06:22:34 -07002246 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002247 if (rc) {
2248 if (rc == -EAGAIN && rdata->credits) {
2249 /* credits was reset by reconnect */
2250 rdata->credits = 0;
2251 /* reduce in_flight value since we won't send the req */
2252 spin_lock(&server->req_lock);
2253 server->in_flight--;
2254 spin_unlock(&server->req_lock);
2255 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002256 return rc;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002257 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002258
Jeff Layton58195752012-09-19 06:22:34 -07002259 buf = (struct smb2_hdr *)rdata->iov.iov_base;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002260 /* 4 for rfc1002 length field */
Jeff Layton58195752012-09-19 06:22:34 -07002261 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002262
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002263 if (rdata->credits) {
2264 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2265 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002266 buf->CreditRequest = buf->CreditCharge;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002267 spin_lock(&server->req_lock);
2268 server->credits += rdata->credits -
2269 le16_to_cpu(buf->CreditCharge);
2270 spin_unlock(&server->req_lock);
2271 wake_up(&server->request_q);
2272 flags = CIFS_HAS_CREDITS;
2273 }
2274
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002275 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002276 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002277 cifs_readv_receive, smb2_readv_callback,
Pavel Shilovskybed9da02014-06-25 11:28:57 +04002278 rdata, flags);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002279 if (rc) {
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002280 kref_put(&rdata->refcount, cifs_readdata_release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002281 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2282 }
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002283
2284 cifs_small_buf_release(buf);
2285 return rc;
2286}
Pavel Shilovsky33319142012-09-18 16:20:29 -07002287
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002288int
2289SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2290 unsigned int *nbytes, char **buf, int *buf_type)
2291{
2292 int resp_buftype, rc = -EACCES;
2293 struct smb2_read_rsp *rsp = NULL;
2294 struct kvec iov[1];
2295
2296 *nbytes = 0;
2297 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2298 if (rc)
2299 return rc;
2300
2301 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2302 &resp_buftype, CIFS_LOG_ERROR);
2303
2304 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2305
2306 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2307 free_rsp_buf(resp_buftype, iov[0].iov_base);
2308 return 0;
2309 }
2310
2311 if (rc) {
2312 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002313 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002314 } else {
2315 *nbytes = le32_to_cpu(rsp->DataLength);
2316 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2317 (*nbytes > io_parms->length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002318 cifs_dbg(FYI, "bad length %d for count %d\n",
2319 *nbytes, io_parms->length);
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002320 rc = -EIO;
2321 *nbytes = 0;
2322 }
2323 }
2324
2325 if (*buf) {
Steve French373512e2015-12-18 13:05:30 -06002326 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002327 *nbytes);
2328 free_rsp_buf(resp_buftype, iov[0].iov_base);
2329 } else if (resp_buftype != CIFS_NO_BUFFER) {
2330 *buf = iov[0].iov_base;
2331 if (resp_buftype == CIFS_SMALL_BUFFER)
2332 *buf_type = CIFS_SMALL_BUFFER;
2333 else if (resp_buftype == CIFS_LARGE_BUFFER)
2334 *buf_type = CIFS_LARGE_BUFFER;
2335 }
2336 return rc;
2337}
2338
Pavel Shilovsky33319142012-09-18 16:20:29 -07002339/*
2340 * Check the mid_state and signature on received buffer (if any), and queue the
2341 * workqueue completion task.
2342 */
2343static void
2344smb2_writev_callback(struct mid_q_entry *mid)
2345{
2346 struct cifs_writedata *wdata = mid->callback_data;
2347 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002348 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002349 unsigned int written;
2350 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2351 unsigned int credits_received = 1;
2352
2353 switch (mid->mid_state) {
2354 case MID_RESPONSE_RECEIVED:
2355 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2356 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2357 if (wdata->result != 0)
2358 break;
2359
2360 written = le32_to_cpu(rsp->DataLength);
2361 /*
2362 * Mask off high 16 bits when bytes written as returned
2363 * by the server is greater than bytes requested by the
2364 * client. OS/2 servers are known to set incorrect
2365 * CountHigh values.
2366 */
2367 if (written > wdata->bytes)
2368 written &= 0xFFFF;
2369
2370 if (written < wdata->bytes)
2371 wdata->result = -ENOSPC;
2372 else
2373 wdata->bytes = written;
2374 break;
2375 case MID_REQUEST_SUBMITTED:
2376 case MID_RETRY_NEEDED:
2377 wdata->result = -EAGAIN;
2378 break;
2379 default:
2380 wdata->result = -EIO;
2381 break;
2382 }
2383
2384 if (wdata->result)
2385 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2386
2387 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002388 mutex_lock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002389 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002390 mutex_unlock(&server->srv_mutex);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002391 add_credits(tcon->ses->server, credits_received, 0);
2392}
2393
2394/* smb2_async_writev - send an async write, and set up mid to handle result */
2395int
Steve French4a5c80d2014-02-07 20:45:12 -06002396smb2_async_writev(struct cifs_writedata *wdata,
2397 void (*release)(struct kref *kref))
Pavel Shilovsky33319142012-09-18 16:20:29 -07002398{
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002399 int rc = -EACCES, flags = 0;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002400 struct smb2_write_req *req = NULL;
2401 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002402 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002403 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002404 struct smb_rqst rqst;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002405
2406 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002407 if (rc) {
2408 if (rc == -EAGAIN && wdata->credits) {
2409 /* credits was reset by reconnect */
2410 wdata->credits = 0;
2411 /* reduce in_flight value since we won't send the req */
2412 spin_lock(&server->req_lock);
2413 server->in_flight--;
2414 spin_unlock(&server->req_lock);
2415 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002416 goto async_writev_out;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002417 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002418
Pavel Shilovsky33319142012-09-18 16:20:29 -07002419 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2420
2421 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2422 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2423 req->WriteChannelInfoOffset = 0;
2424 req->WriteChannelInfoLength = 0;
2425 req->Channel = 0;
2426 req->Offset = cpu_to_le64(wdata->offset);
2427 /* 4 for rfc1002 length field */
2428 req->DataOffset = cpu_to_le16(
2429 offsetof(struct smb2_write_req, Buffer) - 4);
2430 req->RemainingBytes = 0;
2431
2432 /* 4 for rfc1002 length field and 1 for Buffer */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002433 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2434 iov.iov_base = req;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002435
Jeff Laytoneddb0792012-09-18 16:20:35 -07002436 rqst.rq_iov = &iov;
2437 rqst.rq_nvec = 1;
2438 rqst.rq_pages = wdata->pages;
2439 rqst.rq_npages = wdata->nr_pages;
2440 rqst.rq_pagesz = wdata->pagesz;
2441 rqst.rq_tailsz = wdata->tailsz;
Pavel Shilovsky33319142012-09-18 16:20:29 -07002442
Joe Perchesf96637b2013-05-04 22:12:25 -05002443 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2444 wdata->offset, wdata->bytes);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002445
2446 req->Length = cpu_to_le32(wdata->bytes);
2447
2448 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2449
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002450 if (wdata->credits) {
2451 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2452 SMB2_MAX_BUFFER_SIZE));
Ross Lagerwall7d414f32016-09-20 13:37:13 +01002453 req->hdr.CreditRequest = req->hdr.CreditCharge;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002454 spin_lock(&server->req_lock);
2455 server->credits += wdata->credits -
2456 le16_to_cpu(req->hdr.CreditCharge);
2457 spin_unlock(&server->req_lock);
2458 wake_up(&server->request_q);
2459 flags = CIFS_HAS_CREDITS;
2460 }
2461
Pavel Shilovsky33319142012-09-18 16:20:29 -07002462 kref_get(&wdata->refcount);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002463 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2464 flags);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002465
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002466 if (rc) {
Steve French4a5c80d2014-02-07 20:45:12 -06002467 kref_put(&wdata->refcount, release);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002468 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2469 }
Pavel Shilovsky33319142012-09-18 16:20:29 -07002470
Pavel Shilovsky33319142012-09-18 16:20:29 -07002471async_writev_out:
2472 cifs_small_buf_release(req);
Pavel Shilovsky33319142012-09-18 16:20:29 -07002473 return rc;
2474}
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002475
2476/*
2477 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2478 * The length field from io_parms must be at least 1 and indicates a number of
2479 * elements with data to write that begins with position 1 in iov array. All
2480 * data length is specified by count.
2481 */
2482int
2483SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2484 unsigned int *nbytes, struct kvec *iov, int n_vec)
2485{
2486 int rc = 0;
2487 struct smb2_write_req *req = NULL;
2488 struct smb2_write_rsp *rsp = NULL;
2489 int resp_buftype;
2490 *nbytes = 0;
2491
2492 if (n_vec < 1)
2493 return rc;
2494
2495 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2496 if (rc)
2497 return rc;
2498
2499 if (io_parms->tcon->ses->server == NULL)
2500 return -ECONNABORTED;
2501
2502 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2503
2504 req->PersistentFileId = io_parms->persistent_fid;
2505 req->VolatileFileId = io_parms->volatile_fid;
2506 req->WriteChannelInfoOffset = 0;
2507 req->WriteChannelInfoLength = 0;
2508 req->Channel = 0;
2509 req->Length = cpu_to_le32(io_parms->length);
2510 req->Offset = cpu_to_le64(io_parms->offset);
2511 /* 4 for rfc1002 length field */
2512 req->DataOffset = cpu_to_le16(
2513 offsetof(struct smb2_write_req, Buffer) - 4);
2514 req->RemainingBytes = 0;
2515
2516 iov[0].iov_base = (char *)req;
2517 /* 4 for rfc1002 length field and 1 for Buffer */
2518 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2519
2520 /* length of entire message including data to be written */
2521 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2522
2523 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2524 &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002525 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002526
2527 if (rc) {
2528 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002529 cifs_dbg(VFS, "Send error in write = %d\n", rc);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002530 } else
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002531 *nbytes = le32_to_cpu(rsp->DataLength);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002532
2533 free_rsp_buf(resp_buftype, rsp);
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002534 return rc;
2535}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002536
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002537static unsigned int
2538num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2539{
2540 int len;
2541 unsigned int entrycount = 0;
2542 unsigned int next_offset = 0;
2543 FILE_DIRECTORY_INFO *entryptr;
2544
2545 if (bufstart == NULL)
2546 return 0;
2547
2548 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2549
2550 while (1) {
2551 entryptr = (FILE_DIRECTORY_INFO *)
2552 ((char *)entryptr + next_offset);
2553
2554 if ((char *)entryptr + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002555 cifs_dbg(VFS, "malformed search entry would overflow\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002556 break;
2557 }
2558
2559 len = le32_to_cpu(entryptr->FileNameLength);
2560 if ((char *)entryptr + len + size > end_of_buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002561 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2562 end_of_buf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002563 break;
2564 }
2565
2566 *lastentry = (char *)entryptr;
2567 entrycount++;
2568
2569 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2570 if (!next_offset)
2571 break;
2572 }
2573
2574 return entrycount;
2575}
2576
2577/*
2578 * Readdir/FindFirst
2579 */
2580int
2581SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2582 u64 persistent_fid, u64 volatile_fid, int index,
2583 struct cifs_search_info *srch_inf)
2584{
2585 struct smb2_query_directory_req *req;
2586 struct smb2_query_directory_rsp *rsp = NULL;
2587 struct kvec iov[2];
2588 int rc = 0;
2589 int len;
Steve French75fdfc82015-03-25 18:51:57 -05002590 int resp_buftype = CIFS_NO_BUFFER;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002591 unsigned char *bufptr;
2592 struct TCP_Server_Info *server;
2593 struct cifs_ses *ses = tcon->ses;
2594 __le16 asteriks = cpu_to_le16('*');
2595 char *end_of_smb;
2596 unsigned int output_size = CIFSMaxBufSize;
2597 size_t info_buf_size;
2598
2599 if (ses && (ses->server))
2600 server = ses->server;
2601 else
2602 return -EIO;
2603
2604 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2605 if (rc)
2606 return rc;
2607
2608 switch (srch_inf->info_level) {
2609 case SMB_FIND_FILE_DIRECTORY_INFO:
2610 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2611 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2612 break;
2613 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2614 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2615 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2616 break;
2617 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002618 cifs_dbg(VFS, "info level %u isn't supported\n",
2619 srch_inf->info_level);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002620 rc = -EINVAL;
2621 goto qdir_exit;
2622 }
2623
2624 req->FileIndex = cpu_to_le32(index);
2625 req->PersistentFileId = persistent_fid;
2626 req->VolatileFileId = volatile_fid;
2627
2628 len = 0x2;
2629 bufptr = req->Buffer;
2630 memcpy(bufptr, &asteriks, len);
2631
2632 req->FileNameOffset =
2633 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2634 req->FileNameLength = cpu_to_le16(len);
2635 /*
2636 * BB could be 30 bytes or so longer if we used SMB2 specific
2637 * buffer lengths, but this is safe and close enough.
2638 */
2639 output_size = min_t(unsigned int, output_size, server->maxBuf);
2640 output_size = min_t(unsigned int, output_size, 2 << 15);
2641 req->OutputBufferLength = cpu_to_le32(output_size);
2642
2643 iov[0].iov_base = (char *)req;
2644 /* 4 for RFC1001 length and 1 for Buffer */
2645 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2646
2647 iov[1].iov_base = (char *)(req->Buffer);
2648 iov[1].iov_len = len;
2649
2650 inc_rfc1001_len(req, len - 1 /* Buffer */);
2651
2652 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
Pavel Shilovskye5d04882012-09-19 16:03:26 +04002653 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2654
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002655 if (rc) {
Pavel Shilovsky52755802014-08-18 20:49:57 +04002656 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2657 srch_inf->endOfSearch = true;
2658 rc = 0;
2659 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002660 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2661 goto qdir_exit;
2662 }
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002663
2664 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2665 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2666 info_buf_size);
2667 if (rc)
2668 goto qdir_exit;
2669
2670 srch_inf->unicode = true;
2671
2672 if (srch_inf->ntwrk_buf_start) {
2673 if (srch_inf->smallBuf)
2674 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2675 else
2676 cifs_buf_release(srch_inf->ntwrk_buf_start);
2677 }
2678 srch_inf->ntwrk_buf_start = (char *)rsp;
2679 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2680 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2681 /* 4 for rfc1002 length field */
2682 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2683 srch_inf->entries_in_buffer =
2684 num_entries(srch_inf->srch_entries_start, end_of_smb,
2685 &srch_inf->last_entry, info_buf_size);
2686 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
Joe Perchesf96637b2013-05-04 22:12:25 -05002687 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2688 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2689 srch_inf->srch_entries_start, srch_inf->last_entry);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002690 if (resp_buftype == CIFS_LARGE_BUFFER)
2691 srch_inf->smallBuf = false;
2692 else if (resp_buftype == CIFS_SMALL_BUFFER)
2693 srch_inf->smallBuf = true;
2694 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002695 cifs_dbg(VFS, "illegal search buffer type\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002696
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002697 return rc;
2698
2699qdir_exit:
2700 free_rsp_buf(resp_buftype, rsp);
2701 return rc;
2702}
2703
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002704static int
2705send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002706 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002707 unsigned int num, void **data, unsigned int *size)
2708{
2709 struct smb2_set_info_req *req;
2710 struct smb2_set_info_rsp *rsp = NULL;
2711 struct kvec *iov;
2712 int rc = 0;
2713 int resp_buftype;
2714 unsigned int i;
2715 struct TCP_Server_Info *server;
2716 struct cifs_ses *ses = tcon->ses;
2717
2718 if (ses && (ses->server))
2719 server = ses->server;
2720 else
2721 return -EIO;
2722
2723 if (!num)
2724 return -EINVAL;
2725
2726 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2727 if (!iov)
2728 return -ENOMEM;
2729
2730 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2731 if (rc) {
2732 kfree(iov);
2733 return rc;
2734 }
2735
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002736 req->hdr.ProcessId = cpu_to_le32(pid);
2737
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002738 req->InfoType = SMB2_O_INFO_FILE;
2739 req->FileInfoClass = info_class;
2740 req->PersistentFileId = persistent_fid;
2741 req->VolatileFileId = volatile_fid;
2742
2743 /* 4 for RFC1001 length and 1 for Buffer */
2744 req->BufferOffset =
2745 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2746 req->BufferLength = cpu_to_le32(*size);
2747
2748 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2749
2750 memcpy(req->Buffer, *data, *size);
2751
2752 iov[0].iov_base = (char *)req;
2753 /* 4 for RFC1001 length */
2754 iov[0].iov_len = get_rfc1002_length(req) + 4;
2755
2756 for (i = 1; i < num; i++) {
2757 inc_rfc1001_len(req, size[i]);
2758 le32_add_cpu(&req->BufferLength, size[i]);
2759 iov[i].iov_base = (char *)data[i];
2760 iov[i].iov_len = size[i];
2761 }
2762
2763 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2764 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2765
Steve French7d3fb242013-11-18 09:56:28 -06002766 if (rc != 0)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002767 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
Steve French7d3fb242013-11-18 09:56:28 -06002768
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002769 free_rsp_buf(resp_buftype, rsp);
2770 kfree(iov);
2771 return rc;
2772}
2773
2774int
2775SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2776 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2777{
2778 struct smb2_file_rename_info info;
2779 void **data;
2780 unsigned int size[2];
2781 int rc;
2782 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2783
2784 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2785 if (!data)
2786 return -ENOMEM;
2787
2788 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2789 /* 0 = fail if target already exists */
2790 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2791 info.FileNameLength = cpu_to_le32(len);
2792
2793 data[0] = &info;
2794 size[0] = sizeof(struct smb2_file_rename_info);
2795
2796 data[1] = target_file;
2797 size[1] = len + 2 /* null */;
2798
2799 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002800 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2801 size);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002802 kfree(data);
2803 return rc;
2804}
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002805
2806int
Steve French897fba12016-05-12 21:20:36 -05002807SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2808 u64 persistent_fid, u64 volatile_fid)
2809{
2810 __u8 delete_pending = 1;
2811 void *data;
2812 unsigned int size;
2813
2814 data = &delete_pending;
2815 size = 1; /* sizeof __u8 */
2816
2817 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2818 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2819 &size);
2820}
2821
2822int
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002823SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2824 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2825{
2826 struct smb2_file_link_info info;
2827 void **data;
2828 unsigned int size[2];
2829 int rc;
2830 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2831
2832 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2833 if (!data)
2834 return -ENOMEM;
2835
2836 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2837 /* 0 = fail if link already exists */
2838 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2839 info.FileNameLength = cpu_to_le32(len);
2840
2841 data[0] = &info;
2842 size[0] = sizeof(struct smb2_file_link_info);
2843
2844 data[1] = target_file;
2845 size[1] = len + 2 /* null */;
2846
2847 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002848 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002849 kfree(data);
2850 return rc;
2851}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002852
2853int
2854SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -05002855 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002856{
2857 struct smb2_file_eof_info info;
2858 void *data;
2859 unsigned int size;
2860
2861 info.EndOfFile = *eof;
2862
2863 data = &info;
2864 size = sizeof(struct smb2_file_eof_info);
2865
Steve Frenchf29ebb42014-07-19 21:44:58 -05002866 if (is_falloc)
2867 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2868 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2869 else
2870 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2871 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002872}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002873
2874int
2875SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2876 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2877{
2878 unsigned int size;
2879 size = sizeof(FILE_BASIC_INFO);
2880 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2881 current->tgid, FILE_BASIC_INFORMATION, 1,
2882 (void **)&buf, &size);
2883}
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002884
2885int
2886SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2887 const u64 persistent_fid, const u64 volatile_fid,
2888 __u8 oplock_level)
2889{
2890 int rc;
2891 struct smb2_oplock_break *req = NULL;
2892
Joe Perchesf96637b2013-05-04 22:12:25 -05002893 cifs_dbg(FYI, "SMB2_oplock_break\n");
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002894 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2895
2896 if (rc)
2897 return rc;
2898
2899 req->VolatileFid = volatile_fid;
2900 req->PersistentFid = persistent_fid;
2901 req->OplockLevel = oplock_level;
2902 req->hdr.CreditRequest = cpu_to_le16(1);
2903
2904 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2905 /* SMB2 buffer freed by function above */
2906
2907 if (rc) {
2908 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05002909 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002910 }
2911
2912 return rc;
2913}
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002914
2915static void
2916copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2917 struct kstatfs *kst)
2918{
2919 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2920 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2921 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2922 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2923 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2924 return;
2925}
2926
2927static int
2928build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2929 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2930{
2931 int rc;
2932 struct smb2_query_info_req *req;
2933
Joe Perchesf96637b2013-05-04 22:12:25 -05002934 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002935
2936 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2937 return -EIO;
2938
2939 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2940 if (rc)
2941 return rc;
2942
2943 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2944 req->FileInfoClass = level;
2945 req->PersistentFileId = persistent_fid;
2946 req->VolatileFileId = volatile_fid;
2947 /* 4 for rfc1002 length field and 1 for pad */
2948 req->InputBufferOffset =
2949 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2950 req->OutputBufferLength = cpu_to_le32(
2951 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2952
2953 iov->iov_base = (char *)req;
2954 /* 4 for rfc1002 length field */
2955 iov->iov_len = get_rfc1002_length(req) + 4;
2956 return 0;
2957}
2958
2959int
2960SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2961 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2962{
2963 struct smb2_query_info_rsp *rsp = NULL;
2964 struct kvec iov;
2965 int rc = 0;
2966 int resp_buftype;
2967 struct cifs_ses *ses = tcon->ses;
2968 struct smb2_fs_full_size_info *info = NULL;
2969
2970 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2971 sizeof(struct smb2_fs_full_size_info),
2972 persistent_fid, volatile_fid);
2973 if (rc)
2974 return rc;
2975
2976 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2977 if (rc) {
2978 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
Steve French34f62642013-10-09 02:07:00 -05002979 goto qfsinf_exit;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002980 }
2981 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2982
2983 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2984 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2985 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2986 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2987 sizeof(struct smb2_fs_full_size_info));
2988 if (!rc)
2989 copy_fs_info_to_kstatfs(info, fsdata);
2990
Steve French34f62642013-10-09 02:07:00 -05002991qfsinf_exit:
2992 free_rsp_buf(resp_buftype, iov.iov_base);
2993 return rc;
2994}
2995
2996int
2997SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
Steven French21671142013-10-09 13:36:35 -05002998 u64 persistent_fid, u64 volatile_fid, int level)
Steve French34f62642013-10-09 02:07:00 -05002999{
3000 struct smb2_query_info_rsp *rsp = NULL;
3001 struct kvec iov;
3002 int rc = 0;
Steven French21671142013-10-09 13:36:35 -05003003 int resp_buftype, max_len, min_len;
Steve French34f62642013-10-09 02:07:00 -05003004 struct cifs_ses *ses = tcon->ses;
3005 unsigned int rsp_len, offset;
3006
Steven French21671142013-10-09 13:36:35 -05003007 if (level == FS_DEVICE_INFORMATION) {
3008 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3009 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3010 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3011 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3012 min_len = MIN_FS_ATTR_INFO_SIZE;
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003013 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3014 max_len = sizeof(struct smb3_fs_ss_info);
3015 min_len = sizeof(struct smb3_fs_ss_info);
Steven French21671142013-10-09 13:36:35 -05003016 } else {
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003017 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
Steven French21671142013-10-09 13:36:35 -05003018 return -EINVAL;
3019 }
3020
3021 rc = build_qfs_info_req(&iov, tcon, level, max_len,
Steve French34f62642013-10-09 02:07:00 -05003022 persistent_fid, volatile_fid);
3023 if (rc)
3024 return rc;
3025
3026 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
3027 if (rc) {
3028 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3029 goto qfsattr_exit;
3030 }
3031 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
3032
3033 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3034 offset = le16_to_cpu(rsp->OutputBufferOffset);
Steven French21671142013-10-09 13:36:35 -05003035 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3036 if (rc)
3037 goto qfsattr_exit;
3038
3039 if (level == FS_ATTRIBUTE_INFORMATION)
Steve French34f62642013-10-09 02:07:00 -05003040 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3041 + (char *)&rsp->hdr, min_t(unsigned int,
Steven French21671142013-10-09 13:36:35 -05003042 rsp_len, max_len));
3043 else if (level == FS_DEVICE_INFORMATION)
3044 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3045 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003046 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3047 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3048 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3049 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3050 tcon->perf_sector_size =
3051 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3052 }
Steve French34f62642013-10-09 02:07:00 -05003053
3054qfsattr_exit:
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003055 free_rsp_buf(resp_buftype, iov.iov_base);
3056 return rc;
3057}
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003058
3059int
3060smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3061 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3062 const __u32 num_lock, struct smb2_lock_element *buf)
3063{
3064 int rc = 0;
3065 struct smb2_lock_req *req = NULL;
3066 struct kvec iov[2];
3067 int resp_buf_type;
3068 unsigned int count;
3069
Joe Perchesf96637b2013-05-04 22:12:25 -05003070 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003071
3072 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3073 if (rc)
3074 return rc;
3075
3076 req->hdr.ProcessId = cpu_to_le32(pid);
3077 req->LockCount = cpu_to_le16(num_lock);
3078
3079 req->PersistentFileId = persist_fid;
3080 req->VolatileFileId = volatile_fid;
3081
3082 count = num_lock * sizeof(struct smb2_lock_element);
3083 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3084
3085 iov[0].iov_base = (char *)req;
3086 /* 4 for rfc1002 length field and count for all locks */
3087 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3088 iov[1].iov_base = (char *)buf;
3089 iov[1].iov_len = count;
3090
3091 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3092 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3093 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003094 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003095 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3096 }
3097
3098 return rc;
3099}
3100
3101int
3102SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3103 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3104 const __u64 length, const __u64 offset, const __u32 lock_flags,
3105 const bool wait)
3106{
3107 struct smb2_lock_element lock;
3108
3109 lock.Offset = cpu_to_le64(offset);
3110 lock.Length = cpu_to_le64(length);
3111 lock.Flags = cpu_to_le32(lock_flags);
3112 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3113 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3114
3115 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3116}
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003117
3118int
3119SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3120 __u8 *lease_key, const __le32 lease_state)
3121{
3122 int rc;
3123 struct smb2_lease_ack *req = NULL;
3124
Joe Perchesf96637b2013-05-04 22:12:25 -05003125 cifs_dbg(FYI, "SMB2_lease_break\n");
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003126 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3127
3128 if (rc)
3129 return rc;
3130
3131 req->hdr.CreditRequest = cpu_to_le16(1);
3132 req->StructureSize = cpu_to_le16(36);
3133 inc_rfc1001_len(req, 12);
3134
3135 memcpy(req->LeaseKey, lease_key, 16);
3136 req->LeaseState = lease_state;
3137
3138 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3139 /* SMB2 buffer freed by function above */
3140
3141 if (rc) {
3142 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
Joe Perchesf96637b2013-05-04 22:12:25 -05003143 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
Pavel Shilovsky0822f512012-09-19 06:22:45 -07003144 }
3145
3146 return rc;
3147}