]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/cifs/smb2pdu.c
CIFS: Fix wsize usage in iovec write
[thirdparty/linux.git] / fs / cifs / smb2pdu.c
CommitLineData
ec2e4523
PS
1/*
2 * fs/cifs/smb2pdu.c
3 *
2b80d049 4 * Copyright (C) International Business Machines Corp., 2009, 2013
ec2e4523
PS
5 * 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>
09a4707e 34#include <linux/task_io_accounting_ops.h>
ec2e4523 35#include <linux/uaccess.h>
33319141 36#include <linux/pagemap.h>
ec2e4523
PS
37#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"
09a4707e 47#include "smb2glob.h"
d324f08d 48#include "cifspdu.h"
ec2e4523
PS
49
50/*
51 * The following table defines the expected "StructureSize" of SMB2 requests
52 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
53 *
54 * Note that commands are defined in smb2pdu.h in le16 but the array below is
55 * indexed by command in host byte order.
56 */
57static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
58 /* SMB2_NEGOTIATE */ 36,
59 /* SMB2_SESSION_SETUP */ 25,
60 /* SMB2_LOGOFF */ 4,
61 /* SMB2_TREE_CONNECT */ 9,
62 /* SMB2_TREE_DISCONNECT */ 4,
63 /* SMB2_CREATE */ 57,
64 /* SMB2_CLOSE */ 24,
65 /* SMB2_FLUSH */ 24,
66 /* SMB2_READ */ 49,
67 /* SMB2_WRITE */ 49,
68 /* SMB2_LOCK */ 48,
69 /* SMB2_IOCTL */ 57,
70 /* SMB2_CANCEL */ 4,
71 /* SMB2_ECHO */ 4,
72 /* SMB2_QUERY_DIRECTORY */ 33,
73 /* SMB2_CHANGE_NOTIFY */ 32,
74 /* SMB2_QUERY_INFO */ 41,
75 /* SMB2_SET_INFO */ 33,
76 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
77};
78
79
80static void
81smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
82 const struct cifs_tcon *tcon)
83{
84 struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
85 char *temp = (char *)hdr;
86 /* lookup word count ie StructureSize from table */
87 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
88
89 /*
90 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
91 * largest operations (Create)
92 */
93 memset(temp, 0, 256);
94
95 /* Note this is only network field converted to big endian */
96 hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
97 - 4 /* RFC 1001 length field itself not counted */);
98
99 hdr->ProtocolId[0] = 0xFE;
100 hdr->ProtocolId[1] = 'S';
101 hdr->ProtocolId[2] = 'M';
102 hdr->ProtocolId[3] = 'B';
103 hdr->StructureSize = cpu_to_le16(64);
104 hdr->Command = smb2_cmd;
105 hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */
106 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
107
108 if (!tcon)
109 goto out;
110
2b80d049
SF
111 /* BB FIXME when we do write > 64K add +1 for every 64K in req or rsp */
112 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
113 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
84ceeb96
SF
114 if ((tcon->ses) &&
115 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
2b80d049
SF
116 hdr->CreditCharge = cpu_to_le16(1);
117 /* else CreditCharge MBZ */
118
ec2e4523
PS
119 hdr->TreeId = tcon->tid;
120 /* Uid is not converted */
121 if (tcon->ses)
122 hdr->SessionId = tcon->ses->Suid;
f87ab88b
SF
123
124 /*
125 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
126 * to pass the path on the Open SMB prefixed by \\server\share.
127 * Not sure when we would need to do the augmented path (if ever) and
128 * setting this flag breaks the SMB2 open operation since it is
129 * illegal to send an empty path name (without \\server\share prefix)
130 * when the DFS flag is set in the SMB open header. We could
131 * consider setting the flag on all operations other than open
132 * but it is safer to net set it for now.
133 */
134/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
135 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
136
38d77c50 137 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
3c1bf7e4 138 hdr->Flags |= SMB2_FLAGS_SIGNED;
ec2e4523
PS
139out:
140 pdu->StructureSize2 = cpu_to_le16(parmsize);
141 return;
142}
143
144static int
145smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
146{
147 int rc = 0;
aa24d1e9
PS
148 struct nls_table *nls_codepage;
149 struct cifs_ses *ses;
150 struct TCP_Server_Info *server;
151
152 /*
153 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
154 * check for tcp and smb session status done differently
155 * for those three - in the calling routine.
156 */
157 if (tcon == NULL)
158 return rc;
159
160 if (smb2_command == SMB2_TREE_CONNECT)
161 return rc;
162
163 if (tcon->tidStatus == CifsExiting) {
164 /*
165 * only tree disconnect, open, and write,
166 * (and ulogoff which does not have tcon)
167 * are allowed as we start force umount.
168 */
169 if ((smb2_command != SMB2_WRITE) &&
170 (smb2_command != SMB2_CREATE) &&
171 (smb2_command != SMB2_TREE_DISCONNECT)) {
f96637be
JP
172 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
173 smb2_command);
aa24d1e9
PS
174 return -ENODEV;
175 }
176 }
177 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
178 (!tcon->ses->server))
179 return -EIO;
180
181 ses = tcon->ses;
182 server = ses->server;
183
184 /*
185 * Give demultiplex thread up to 10 seconds to reconnect, should be
186 * greater than cifs socket timeout which is 7 seconds
187 */
188 while (server->tcpStatus == CifsNeedReconnect) {
189 /*
190 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
191 * here since they are implicitly done when session drops.
192 */
193 switch (smb2_command) {
194 /*
195 * BB Should we keep oplock break and add flush to exceptions?
196 */
197 case SMB2_TREE_DISCONNECT:
198 case SMB2_CANCEL:
199 case SMB2_CLOSE:
200 case SMB2_OPLOCK_BREAK:
201 return -EAGAIN;
202 }
203
204 wait_event_interruptible_timeout(server->response_q,
205 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
206
207 /* are we still trying to reconnect? */
208 if (server->tcpStatus != CifsNeedReconnect)
209 break;
210
211 /*
212 * on "soft" mounts we wait once. Hard mounts keep
213 * retrying until process is killed or server comes
214 * back on-line
215 */
216 if (!tcon->retry) {
f96637be 217 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
aa24d1e9
PS
218 return -EHOSTDOWN;
219 }
220 }
221
222 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
223 return rc;
224
225 nls_codepage = load_nls_default();
226
227 /*
228 * need to prevent multiple threads trying to simultaneously reconnect
229 * the same SMB session
230 */
231 mutex_lock(&tcon->ses->session_mutex);
232 rc = cifs_negotiate_protocol(0, tcon->ses);
233 if (!rc && tcon->ses->need_reconnect)
234 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
235
236 if (rc || !tcon->need_reconnect) {
237 mutex_unlock(&tcon->ses->session_mutex);
238 goto out;
239 }
240
241 cifs_mark_open_files_invalid(tcon);
242 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
243 mutex_unlock(&tcon->ses->session_mutex);
f96637be 244 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
aa24d1e9
PS
245 if (rc)
246 goto out;
247 atomic_inc(&tconInfoReconnectCount);
aa24d1e9
PS
248out:
249 /*
250 * Check if handle based operation so we know whether we can continue
251 * or not without returning to caller to reset file handle.
252 */
253 /*
254 * BB Is flush done by server on drop of tcp session? Should we special
255 * case it and skip above?
256 */
257 switch (smb2_command) {
258 case SMB2_FLUSH:
259 case SMB2_READ:
260 case SMB2_WRITE:
261 case SMB2_LOCK:
262 case SMB2_IOCTL:
263 case SMB2_QUERY_DIRECTORY:
264 case SMB2_CHANGE_NOTIFY:
265 case SMB2_QUERY_INFO:
266 case SMB2_SET_INFO:
267 return -EAGAIN;
268 }
269 unload_nls(nls_codepage);
ec2e4523
PS
270 return rc;
271}
272
273/*
274 * Allocate and return pointer to an SMB request hdr, and set basic
275 * SMB information in the SMB header. If the return code is zero, this
276 * function must have filled in request_buf pointer.
277 */
278static int
279small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
280 void **request_buf)
281{
282 int rc = 0;
283
284 rc = smb2_reconnect(smb2_command, tcon);
285 if (rc)
286 return rc;
287
288 /* BB eventually switch this to SMB2 specific small buf size */
289 *request_buf = cifs_small_buf_get();
290 if (*request_buf == NULL) {
291 /* BB should we add a retry in here if not a writepage? */
292 return -ENOMEM;
293 }
294
295 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
296
297 if (tcon != NULL) {
298#ifdef CONFIG_CIFS_STATS2
ec2e4523
PS
299 uint16_t com_code = le16_to_cpu(smb2_command);
300 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
ec2e4523
PS
301#endif
302 cifs_stats_inc(&tcon->num_smbs_sent);
303 }
304
305 return rc;
306}
307
ec2e4523
PS
308/*
309 *
310 * SMB2 Worker functions follow:
311 *
312 * The general structure of the worker functions is:
313 * 1) Call smb2_init (assembles SMB2 header)
314 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
315 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
316 * 4) Decode SMB2 command specific fields in the fixed length area
317 * 5) Decode variable length data area (if any for this SMB2 command type)
318 * 6) Call free smb buffer
319 * 7) return
320 *
321 */
322
323int
324SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
325{
326 struct smb2_negotiate_req *req;
327 struct smb2_negotiate_rsp *rsp;
328 struct kvec iov[1];
329 int rc = 0;
330 int resp_buftype;
3534b850 331 struct TCP_Server_Info *server = ses->server;
ec2e4523
PS
332 int blob_offset, blob_length;
333 char *security_blob;
334 int flags = CIFS_NEG_OP;
335
f96637be 336 cifs_dbg(FYI, "Negotiate protocol\n");
ec2e4523 337
3534b850
JL
338 if (!server) {
339 WARN(1, "%s: server is NULL!\n", __func__);
340 return -EIO;
ec2e4523
PS
341 }
342
343 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
344 if (rc)
345 return rc;
346
ec2e4523
PS
347 req->hdr.SessionId = 0;
348
e4aa25e7 349 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
ec2e4523 350
e4aa25e7
SF
351 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
352 inc_rfc1001_len(req, 2);
ec2e4523
PS
353
354 /* only one of SMB2 signing flags may be set in SMB2 request */
38d77c50 355 if (ses->sign)
9cd2e62c 356 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
38d77c50 357 else if (global_secflags & CIFSSEC_MAY_SIGN)
9cd2e62c 358 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
38d77c50
JL
359 else
360 req->SecurityMode = 0;
ec2e4523 361
e4aa25e7 362 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
ec2e4523 363
3c5f9be1
SF
364 /* ClientGUID must be zero for SMB2.02 dialect */
365 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
366 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
367 else
368 memcpy(req->ClientGUID, server->client_guid,
369 SMB2_CLIENT_GUID_SIZE);
b8c32dbb 370
ec2e4523
PS
371 iov[0].iov_base = (char *)req;
372 /* 4 for rfc1002 length field */
373 iov[0].iov_len = get_rfc1002_length(req) + 4;
374
375 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
376
377 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
378 /*
379 * No tcon so can't do
380 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
381 */
382 if (rc != 0)
383 goto neg_exit;
384
f96637be 385 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
ec2e4523 386
e4aa25e7
SF
387 /* BB we may eventually want to match the negotiated vs. requested
388 dialect, even though we are only requesting one at a time */
389 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
f96637be 390 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
e4aa25e7 391 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
f96637be 392 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
e4aa25e7 393 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
f96637be 394 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
20b6d8b4
SF
395 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
396 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
ec2e4523 397 else {
f96637be
JP
398 cifs_dbg(VFS, "Illegal dialect returned by server %d\n",
399 le16_to_cpu(rsp->DialectRevision));
ec2e4523
PS
400 rc = -EIO;
401 goto neg_exit;
402 }
403 server->dialect = le16_to_cpu(rsp->DialectRevision);
404
e598d1d8
JL
405 /* SMB2 only has an extended negflavor */
406 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
2365c4ea
PS
407 /* set it to the maximum buffer size value we can send with 1 credit */
408 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
409 SMB2_MAX_BUFFER_SIZE);
ec2e4523
PS
410 server->max_read = le32_to_cpu(rsp->MaxReadSize);
411 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
412 /* BB Do we need to validate the SecurityMode? */
413 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
414 server->capabilities = le32_to_cpu(rsp->Capabilities);
29e20f9c
PS
415 /* Internal types */
416 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
ec2e4523
PS
417
418 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
419 &rsp->hdr);
5d875cc9
SF
420 /*
421 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
422 * for us will be
423 * ses->sectype = RawNTLMSSP;
424 * but for time being this is our only auth choice so doesn't matter.
425 * We just found a server which sets blob length to zero expecting raw.
426 */
427 if (blob_length == 0)
428 cifs_dbg(FYI, "missing security blob on negprot\n");
3c1bf7e4 429
38d77c50 430 rc = cifs_enable_signing(server, ses->sign);
ec2e4523 431#ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
9ddec561
JL
432 if (rc)
433 goto neg_exit;
5d875cc9
SF
434 if (blob_length)
435 rc = decode_neg_token_init(security_blob, blob_length,
ec2e4523
PS
436 &server->sec_type);
437 if (rc == 1)
438 rc = 0;
439 else if (rc == 0) {
440 rc = -EIO;
441 goto neg_exit;
442 }
443#endif
444
445neg_exit:
446 free_rsp_buf(resp_buftype, rsp);
447 return rc;
448}
5478f9ba 449
ff1c038a
SF
450int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
451{
452 int rc = 0;
453 struct validate_negotiate_info_req vneg_inbuf;
454 struct validate_negotiate_info_rsp *pneg_rsp;
455 u32 rsplen;
456
457 cifs_dbg(FYI, "validate negotiate\n");
458
459 /*
460 * validation ioctl must be signed, so no point sending this if we
461 * can not sign it. We could eventually change this to selectively
462 * sign just this, the first and only signed request on a connection.
463 * This is good enough for now since a user who wants better security
464 * would also enable signing on the mount. Having validation of
465 * negotiate info for signed connections helps reduce attack vectors
466 */
467 if (tcon->ses->server->sign == false)
468 return 0; /* validation requires signing */
469
470 vneg_inbuf.Capabilities =
471 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
39552ea8
SP
472 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
473 SMB2_CLIENT_GUID_SIZE);
ff1c038a
SF
474
475 if (tcon->ses->sign)
476 vneg_inbuf.SecurityMode =
477 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
478 else if (global_secflags & CIFSSEC_MAY_SIGN)
479 vneg_inbuf.SecurityMode =
480 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
481 else
482 vneg_inbuf.SecurityMode = 0;
483
484 vneg_inbuf.DialectCount = cpu_to_le16(1);
485 vneg_inbuf.Dialects[0] =
486 cpu_to_le16(tcon->ses->server->vals->protocol_id);
487
488 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
489 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
490 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
491 (char **)&pneg_rsp, &rsplen);
492
493 if (rc != 0) {
494 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
495 return -EIO;
496 }
497
498 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
499 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
500 return -EIO;
501 }
502
503 /* check validate negotiate info response matches what we got earlier */
504 if (pneg_rsp->Dialect !=
505 cpu_to_le16(tcon->ses->server->vals->protocol_id))
506 goto vneg_out;
507
508 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
509 goto vneg_out;
510
511 /* do not validate server guid because not saved at negprot time yet */
512
513 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
514 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
515 goto vneg_out;
516
517 /* validate negotiate successful */
518 cifs_dbg(FYI, "validate negotiate info successful\n");
519 return 0;
520
521vneg_out:
522 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
523 return -EIO;
524}
525
5478f9ba
PS
526int
527SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
528 const struct nls_table *nls_cp)
529{
530 struct smb2_sess_setup_req *req;
531 struct smb2_sess_setup_rsp *rsp = NULL;
532 struct kvec iov[2];
533 int rc = 0;
534 int resp_buftype;
535 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
3534b850 536 struct TCP_Server_Info *server = ses->server;
5478f9ba
PS
537 u16 blob_length = 0;
538 char *security_blob;
539 char *ntlmssp_blob = NULL;
540 bool use_spnego = false; /* else use raw ntlmssp */
541
f96637be 542 cifs_dbg(FYI, "Session Setup\n");
5478f9ba 543
3534b850
JL
544 if (!server) {
545 WARN(1, "%s: server is NULL!\n", __func__);
546 return -EIO;
5478f9ba
PS
547 }
548
d4e63bd6
SP
549 /*
550 * If we are here due to reconnect, free per-smb session key
551 * in case signing was required.
552 */
553 kfree(ses->auth_key.response);
554 ses->auth_key.response = NULL;
555
5478f9ba
PS
556 /*
557 * If memory allocation is successful, caller of this function
558 * frees it.
559 */
560 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
561 if (!ses->ntlmssp)
562 return -ENOMEM;
5c234aa5 563 ses->ntlmssp->sesskey_per_smbsess = true;
5478f9ba 564
3f618223
JL
565 /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
566 ses->sectype = RawNTLMSSP;
5478f9ba
PS
567
568ssetup_ntlmssp_authenticate:
569 if (phase == NtLmChallenge)
570 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
571
572 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
573 if (rc)
574 return rc;
575
5478f9ba
PS
576 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
577 req->VcNumber = 0; /* MBZ */
578 /* to enable echos and oplocks */
579 req->hdr.CreditRequest = cpu_to_le16(3);
580
581 /* only one of SMB2 signing flags may be set in SMB2 request */
38d77c50
JL
582 if (server->sign)
583 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
584 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
585 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
586 else
587 req->SecurityMode = 0;
588
5478f9ba
PS
589 req->Capabilities = 0;
590 req->Channel = 0; /* MBZ */
591
592 iov[0].iov_base = (char *)req;
593 /* 4 for rfc1002 length field and 1 for pad */
594 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
595 if (phase == NtLmNegotiate) {
596 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
597 GFP_KERNEL);
598 if (ntlmssp_blob == NULL) {
599 rc = -ENOMEM;
600 goto ssetup_exit;
601 }
602 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
603 if (use_spnego) {
604 /* blob_length = build_spnego_ntlmssp_blob(
605 &security_blob,
606 sizeof(struct _NEGOTIATE_MESSAGE),
607 ntlmssp_blob); */
608 /* BB eventually need to add this */
f96637be 609 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
5478f9ba
PS
610 rc = -EOPNOTSUPP;
611 kfree(ntlmssp_blob);
612 goto ssetup_exit;
613 } else {
614 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
615 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
616 security_blob = ntlmssp_blob;
617 }
618 } else if (phase == NtLmAuthenticate) {
619 req->hdr.SessionId = ses->Suid;
620 ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
621 GFP_KERNEL);
622 if (ntlmssp_blob == NULL) {
5478f9ba
PS
623 rc = -ENOMEM;
624 goto ssetup_exit;
625 }
626 rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
627 nls_cp);
628 if (rc) {
f96637be
JP
629 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
630 rc);
5478f9ba
PS
631 goto ssetup_exit; /* BB double check error handling */
632 }
633 if (use_spnego) {
634 /* blob_length = build_spnego_ntlmssp_blob(
635 &security_blob,
636 blob_length,
637 ntlmssp_blob); */
f96637be 638 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
5478f9ba
PS
639 rc = -EOPNOTSUPP;
640 kfree(ntlmssp_blob);
641 goto ssetup_exit;
642 } else {
643 security_blob = ntlmssp_blob;
644 }
645 } else {
f96637be 646 cifs_dbg(VFS, "illegal ntlmssp phase\n");
5478f9ba
PS
647 rc = -EIO;
648 goto ssetup_exit;
649 }
650
651 /* Testing shows that buffer offset must be at location of Buffer[0] */
652 req->SecurityBufferOffset =
653 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
654 1 /* pad */ - 4 /* rfc1001 len */);
655 req->SecurityBufferLength = cpu_to_le16(blob_length);
656 iov[1].iov_base = security_blob;
657 iov[1].iov_len = blob_length;
658
659 inc_rfc1001_len(req, blob_length - 1 /* pad */);
660
661 /* BB add code to build os and lm fields */
662
6d8b59d7
SF
663 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
664 CIFS_LOG_ERROR | CIFS_NEG_OP);
5478f9ba
PS
665
666 kfree(security_blob);
667 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
4ca3a99c
PS
668 if (resp_buftype != CIFS_NO_BUFFER &&
669 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
5478f9ba 670 if (phase != NtLmNegotiate) {
f96637be 671 cifs_dbg(VFS, "Unexpected more processing error\n");
5478f9ba
PS
672 goto ssetup_exit;
673 }
674 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
4ca3a99c 675 le16_to_cpu(rsp->SecurityBufferOffset)) {
f96637be
JP
676 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
677 le16_to_cpu(rsp->SecurityBufferOffset));
5478f9ba
PS
678 rc = -EIO;
679 goto ssetup_exit;
680 }
681
682 /* NTLMSSP Negotiate sent now processing challenge (response) */
683 phase = NtLmChallenge; /* process ntlmssp challenge */
684 rc = 0; /* MORE_PROCESSING is not an error here but expected */
685 ses->Suid = rsp->hdr.SessionId;
686 rc = decode_ntlmssp_challenge(rsp->Buffer,
687 le16_to_cpu(rsp->SecurityBufferLength), ses);
688 }
689
690 /*
691 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
692 * but at least the raw NTLMSSP case works.
693 */
694 /*
695 * No tcon so can't do
696 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
697 */
698 if (rc != 0)
699 goto ssetup_exit;
700
5478f9ba 701 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
0cbaa53c
SF
702 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
703 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
5478f9ba
PS
704ssetup_exit:
705 free_rsp_buf(resp_buftype, rsp);
706
707 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
708 if ((phase == NtLmChallenge) && (rc == 0))
709 goto ssetup_ntlmssp_authenticate;
d4e63bd6
SP
710
711 if (!rc) {
712 mutex_lock(&server->srv_mutex);
32811d24
SP
713 if (server->sign && server->ops->generate_signingkey) {
714 rc = server->ops->generate_signingkey(ses);
715 kfree(ses->auth_key.response);
716 ses->auth_key.response = NULL;
717 if (rc) {
718 cifs_dbg(FYI,
719 "SMB3 session key generation failed\n");
720 mutex_unlock(&server->srv_mutex);
721 goto keygen_exit;
722 }
723 }
d4e63bd6
SP
724 if (!server->session_estab) {
725 server->sequence_number = 0x2;
726 server->session_estab = true;
d4e63bd6
SP
727 }
728 mutex_unlock(&server->srv_mutex);
729
730 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
731 spin_lock(&GlobalMid_Lock);
732 ses->status = CifsGood;
733 ses->need_reconnect = false;
734 spin_unlock(&GlobalMid_Lock);
735 }
736
32811d24 737keygen_exit:
d4e63bd6
SP
738 if (!server->sign) {
739 kfree(ses->auth_key.response);
740 ses->auth_key.response = NULL;
741 }
742 kfree(ses->ntlmssp);
743
5478f9ba
PS
744 return rc;
745}
746
747int
748SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
749{
750 struct smb2_logoff_req *req; /* response is also trivial struct */
751 int rc = 0;
752 struct TCP_Server_Info *server;
753
f96637be 754 cifs_dbg(FYI, "disconnect session %p\n", ses);
5478f9ba
PS
755
756 if (ses && (ses->server))
757 server = ses->server;
758 else
759 return -EIO;
760
eb4c7df6
SP
761 /* no need to send SMB logoff if uid already closed due to reconnect */
762 if (ses->need_reconnect)
763 goto smb2_session_already_dead;
764
5478f9ba
PS
765 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
766 if (rc)
767 return rc;
768
769 /* since no tcon, smb2_init can not do this, so do here */
770 req->hdr.SessionId = ses->Suid;
38d77c50 771 if (server->sign)
3c1bf7e4 772 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
5478f9ba
PS
773
774 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
775 /*
776 * No tcon so can't do
777 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
778 */
eb4c7df6
SP
779
780smb2_session_already_dead:
5478f9ba
PS
781 return rc;
782}
faaf946a
PS
783
784static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
785{
d60622eb 786 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
faaf946a
PS
787}
788
789#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
790
de9f68df
SF
791/* These are similar values to what Windows uses */
792static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
793{
794 tcon->max_chunks = 256;
795 tcon->max_bytes_chunk = 1048576;
796 tcon->max_bytes_copy = 16777216;
797}
798
faaf946a
PS
799int
800SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
801 struct cifs_tcon *tcon, const struct nls_table *cp)
802{
803 struct smb2_tree_connect_req *req;
804 struct smb2_tree_connect_rsp *rsp = NULL;
805 struct kvec iov[2];
806 int rc = 0;
807 int resp_buftype;
808 int unc_path_len;
809 struct TCP_Server_Info *server;
810 __le16 *unc_path = NULL;
811
f96637be 812 cifs_dbg(FYI, "TCON\n");
faaf946a
PS
813
814 if ((ses->server) && tree)
815 server = ses->server;
816 else
817 return -EIO;
818
819 if (tcon && tcon->bad_network_name)
820 return -ENOENT;
821
822 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
823 if (unc_path == NULL)
824 return -ENOMEM;
825
826 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
827 unc_path_len *= 2;
828 if (unc_path_len < 2) {
829 kfree(unc_path);
830 return -EINVAL;
831 }
832
833 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
834 if (rc) {
835 kfree(unc_path);
836 return rc;
837 }
838
839 if (tcon == NULL) {
840 /* since no tcon, smb2_init can not do this, so do here */
841 req->hdr.SessionId = ses->Suid;
842 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
843 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
844 }
845
846 iov[0].iov_base = (char *)req;
847 /* 4 for rfc1002 length field and 1 for pad */
848 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
849
850 /* Testing shows that buffer offset must be at location of Buffer[0] */
851 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
852 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
853 req->PathLength = cpu_to_le16(unc_path_len - 2);
854 iov[1].iov_base = unc_path;
855 iov[1].iov_len = unc_path_len;
856
857 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
858
859 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
860 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
861
862 if (rc != 0) {
863 if (tcon) {
864 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
865 tcon->need_reconnect = true;
866 }
867 goto tcon_error_exit;
868 }
869
faaf946a
PS
870 if (tcon == NULL) {
871 ses->ipc_tid = rsp->hdr.TreeId;
872 goto tcon_exit;
873 }
874
875 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
f96637be 876 cifs_dbg(FYI, "connection to disk share\n");
faaf946a
PS
877 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
878 tcon->ipc = true;
f96637be 879 cifs_dbg(FYI, "connection to pipe share\n");
faaf946a
PS
880 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
881 tcon->print = true;
f96637be 882 cifs_dbg(FYI, "connection to printer\n");
faaf946a 883 } else {
f96637be 884 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
faaf946a
PS
885 rc = -EOPNOTSUPP;
886 goto tcon_error_exit;
887 }
888
889 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
769ee6a4 890 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
faaf946a
PS
891 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
892 tcon->tidStatus = CifsGood;
893 tcon->need_reconnect = false;
894 tcon->tid = rsp->hdr.TreeId;
46b51d08 895 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
faaf946a
PS
896
897 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
898 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
f96637be 899 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
de9f68df 900 init_copy_chunk_defaults(tcon);
ff1c038a
SF
901 if (tcon->ses->server->ops->validate_negotiate)
902 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
faaf946a
PS
903tcon_exit:
904 free_rsp_buf(resp_buftype, rsp);
905 kfree(unc_path);
906 return rc;
907
908tcon_error_exit:
909 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
f96637be 910 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
faaf946a
PS
911 tcon->bad_network_name = true;
912 }
913 goto tcon_exit;
914}
915
916int
917SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
918{
919 struct smb2_tree_disconnect_req *req; /* response is trivial */
920 int rc = 0;
921 struct TCP_Server_Info *server;
922 struct cifs_ses *ses = tcon->ses;
923
f96637be 924 cifs_dbg(FYI, "Tree Disconnect\n");
faaf946a
PS
925
926 if (ses && (ses->server))
927 server = ses->server;
928 else
929 return -EIO;
930
931 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
932 return 0;
933
934 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
935 if (rc)
936 return rc;
937
938 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
939 if (rc)
940 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
941
942 return rc;
943}
2503a0db 944
b8c32dbb 945
63eb3def
PS
946static struct create_durable *
947create_durable_buf(void)
948{
949 struct create_durable *buf;
950
951 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
952 if (!buf)
953 return NULL;
954
955 buf->ccontext.DataOffset = cpu_to_le16(offsetof
9cbc0b73 956 (struct create_durable, Data));
63eb3def
PS
957 buf->ccontext.DataLength = cpu_to_le32(16);
958 buf->ccontext.NameOffset = cpu_to_le16(offsetof
959 (struct create_durable, Name));
960 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 961 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
63eb3def
PS
962 buf->Name[0] = 'D';
963 buf->Name[1] = 'H';
964 buf->Name[2] = 'n';
965 buf->Name[3] = 'Q';
966 return buf;
967}
968
9cbc0b73
PS
969static struct create_durable *
970create_reconnect_durable_buf(struct cifs_fid *fid)
971{
972 struct create_durable *buf;
973
974 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
975 if (!buf)
976 return NULL;
977
978 buf->ccontext.DataOffset = cpu_to_le16(offsetof
979 (struct create_durable, Data));
980 buf->ccontext.DataLength = cpu_to_le32(16);
981 buf->ccontext.NameOffset = cpu_to_le16(offsetof
982 (struct create_durable, Name));
983 buf->ccontext.NameLength = cpu_to_le16(4);
984 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
985 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
12197a7f 986 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
9cbc0b73
PS
987 buf->Name[0] = 'D';
988 buf->Name[1] = 'H';
989 buf->Name[2] = 'n';
990 buf->Name[3] = 'C';
991 return buf;
992}
993
b8c32dbb 994static __u8
42873b0a
PS
995parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
996 unsigned int *epoch)
b8c32dbb
PS
997{
998 char *data_offset;
b5c7cde3 999 struct create_context *cc;
fd554396
PS
1000 unsigned int next = 0;
1001 char *name;
b8c32dbb 1002
fd554396 1003 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
b5c7cde3 1004 cc = (struct create_context *)data_offset;
b8c32dbb 1005 do {
b5c7cde3
PS
1006 cc = (struct create_context *)((char *)cc + next);
1007 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1008 if (le16_to_cpu(cc->NameLength) != 4 ||
b8c32dbb 1009 strncmp(name, "RqLs", 4)) {
b5c7cde3 1010 next = le32_to_cpu(cc->Next);
b8c32dbb
PS
1011 continue;
1012 }
42873b0a 1013 return server->ops->parse_lease_buf(cc, epoch);
fd554396 1014 } while (next != 0);
b8c32dbb 1015
b5c7cde3 1016 return 0;
b8c32dbb
PS
1017}
1018
d22cbfec 1019static int
a41a28bd
PS
1020add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1021 unsigned int *num_iovec, __u8 *oplock)
d22cbfec
PS
1022{
1023 struct smb2_create_req *req = iov[0].iov_base;
1024 unsigned int num = *num_iovec;
1025
a41a28bd 1026 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
d22cbfec
PS
1027 if (iov[num].iov_base == NULL)
1028 return -ENOMEM;
a41a28bd 1029 iov[num].iov_len = server->vals->create_lease_size;
d22cbfec
PS
1030 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1031 if (!req->CreateContextsOffset)
1032 req->CreateContextsOffset = cpu_to_le32(
1033 sizeof(struct smb2_create_req) - 4 +
1034 iov[num - 1].iov_len);
a41a28bd
PS
1035 le32_add_cpu(&req->CreateContextsLength,
1036 server->vals->create_lease_size);
1037 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
d22cbfec
PS
1038 *num_iovec = num + 1;
1039 return 0;
1040}
1041
63eb3def 1042static int
9cbc0b73
PS
1043add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1044 struct cifs_open_parms *oparms)
63eb3def
PS
1045{
1046 struct smb2_create_req *req = iov[0].iov_base;
1047 unsigned int num = *num_iovec;
1048
9cbc0b73
PS
1049 if (oparms->reconnect) {
1050 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1051 /* indicate that we don't need to relock the file */
1052 oparms->reconnect = false;
1053 } else
1054 iov[num].iov_base = create_durable_buf();
63eb3def
PS
1055 if (iov[num].iov_base == NULL)
1056 return -ENOMEM;
1057 iov[num].iov_len = sizeof(struct create_durable);
1058 if (!req->CreateContextsOffset)
1059 req->CreateContextsOffset =
1060 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1061 iov[1].iov_len);
31f92e9a 1062 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
63eb3def
PS
1063 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1064 *num_iovec = num + 1;
1065 return 0;
1066}
1067
2503a0db 1068int
064f6047 1069SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
b42bf888
PS
1070 __u8 *oplock, struct smb2_file_all_info *buf,
1071 struct smb2_err_rsp **err_buf)
2503a0db
PS
1072{
1073 struct smb2_create_req *req;
1074 struct smb2_create_rsp *rsp;
1075 struct TCP_Server_Info *server;
064f6047 1076 struct cifs_tcon *tcon = oparms->tcon;
2503a0db 1077 struct cifs_ses *ses = tcon->ses;
63eb3def 1078 struct kvec iov[4];
2503a0db
PS
1079 int resp_buftype;
1080 int uni_path_len;
b8c32dbb
PS
1081 __le16 *copy_path = NULL;
1082 int copy_size;
2503a0db 1083 int rc = 0;
59aa3718 1084 unsigned int num_iovecs = 2;
ca81983f 1085 __u32 file_attributes = 0;
663a9621 1086 char *dhc_buf = NULL, *lc_buf = NULL;
2503a0db 1087
f96637be 1088 cifs_dbg(FYI, "create/open\n");
2503a0db
PS
1089
1090 if (ses && (ses->server))
1091 server = ses->server;
1092 else
1093 return -EIO;
1094
1095 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1096 if (rc)
1097 return rc;
1098
064f6047 1099 if (oparms->create_options & CREATE_OPTION_READONLY)
ca81983f
PS
1100 file_attributes |= ATTR_READONLY;
1101
2503a0db 1102 req->ImpersonationLevel = IL_IMPERSONATION;
064f6047 1103 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2503a0db
PS
1104 /* File attributes ignored on open (used in create though) */
1105 req->FileAttributes = cpu_to_le32(file_attributes);
1106 req->ShareAccess = FILE_SHARE_ALL_LE;
064f6047
PS
1107 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1108 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2503a0db 1109 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
59aa3718
PS
1110 /* do not count rfc1001 len field */
1111 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
2503a0db
PS
1112
1113 iov[0].iov_base = (char *)req;
1114 /* 4 for rfc1002 length field */
1115 iov[0].iov_len = get_rfc1002_length(req) + 4;
1116
1117 /* MUST set path len (NameLength) to 0 opening root of share */
59aa3718
PS
1118 req->NameLength = cpu_to_le16(uni_path_len - 2);
1119 /* -1 since last byte is buf[0] which is sent below (path) */
1120 iov[0].iov_len--;
1121 if (uni_path_len % 8 != 0) {
1122 copy_size = uni_path_len / 8 * 8;
1123 if (copy_size < uni_path_len)
1124 copy_size += 8;
1125
1126 copy_path = kzalloc(copy_size, GFP_KERNEL);
1127 if (!copy_path)
1128 return -ENOMEM;
1129 memcpy((char *)copy_path, (const char *)path,
1130 uni_path_len);
1131 uni_path_len = copy_size;
1132 path = copy_path;
2503a0db
PS
1133 }
1134
59aa3718
PS
1135 iov[1].iov_len = uni_path_len;
1136 iov[1].iov_base = path;
1137 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1138 inc_rfc1001_len(req, uni_path_len - 1);
1139
b8c32dbb
PS
1140 if (!server->oplocks)
1141 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1142
a41a28bd 1143 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
b8c32dbb
PS
1144 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1145 req->RequestedOplockLevel = *oplock;
1146 else {
a41a28bd 1147 rc = add_lease_context(server, iov, &num_iovecs, oplock);
d22cbfec 1148 if (rc) {
b8c32dbb
PS
1149 cifs_small_buf_release(req);
1150 kfree(copy_path);
d22cbfec 1151 return rc;
b8c32dbb 1152 }
663a9621 1153 lc_buf = iov[num_iovecs-1].iov_base;
b8c32dbb
PS
1154 }
1155
63eb3def
PS
1156 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1157 /* need to set Next field of lease context if we request it */
a41a28bd 1158 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
63eb3def
PS
1159 struct create_context *ccontext =
1160 (struct create_context *)iov[num_iovecs-1].iov_base;
1c46943f 1161 ccontext->Next =
a41a28bd 1162 cpu_to_le32(server->vals->create_lease_size);
63eb3def 1163 }
9cbc0b73 1164 rc = add_durable_context(iov, &num_iovecs, oparms);
63eb3def
PS
1165 if (rc) {
1166 cifs_small_buf_release(req);
1167 kfree(copy_path);
663a9621 1168 kfree(lc_buf);
63eb3def
PS
1169 return rc;
1170 }
663a9621 1171 dhc_buf = iov[num_iovecs-1].iov_base;
63eb3def
PS
1172 }
1173
2503a0db
PS
1174 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1175 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1176
1177 if (rc != 0) {
1178 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
b42bf888
PS
1179 if (err_buf)
1180 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1181 GFP_KERNEL);
2503a0db
PS
1182 goto creat_exit;
1183 }
1184
064f6047
PS
1185 oparms->fid->persistent_fid = rsp->PersistentFileId;
1186 oparms->fid->volatile_fid = rsp->VolatileFileId;
f0df737e
PS
1187
1188 if (buf) {
1189 memcpy(buf, &rsp->CreationTime, 32);
1190 buf->AllocationSize = rsp->AllocationSize;
1191 buf->EndOfFile = rsp->EndofFile;
1192 buf->Attributes = rsp->FileAttributes;
1193 buf->NumberOfLinks = cpu_to_le32(1);
1194 buf->DeletePending = 0;
1195 }
2e44b288 1196
b8c32dbb 1197 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
42873b0a 1198 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
b8c32dbb
PS
1199 else
1200 *oplock = rsp->OplockLevel;
2503a0db 1201creat_exit:
b8c32dbb 1202 kfree(copy_path);
663a9621
PS
1203 kfree(lc_buf);
1204 kfree(dhc_buf);
2503a0db
PS
1205 free_rsp_buf(resp_buftype, rsp);
1206 return rc;
1207}
1208
4a72dafa
SF
1209/*
1210 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1211 */
1212int
1213SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1214 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1215 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1216{
1217 struct smb2_ioctl_req *req;
1218 struct smb2_ioctl_rsp *rsp;
1219 struct TCP_Server_Info *server;
1220 struct cifs_ses *ses = tcon->ses;
1221 struct kvec iov[2];
1222 int resp_buftype;
1223 int num_iovecs;
1224 int rc = 0;
1225
1226 cifs_dbg(FYI, "SMB2 IOCTL\n");
1227
7ff8d45c 1228 *out_data = NULL;
4a72dafa
SF
1229 /* zero out returned data len, in case of error */
1230 if (plen)
1231 *plen = 0;
1232
1233 if (ses && (ses->server))
1234 server = ses->server;
1235 else
1236 return -EIO;
1237
1238 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1239 if (rc)
1240 return rc;
1241
1242 req->CtlCode = cpu_to_le32(opcode);
1243 req->PersistentFileId = persistent_fid;
1244 req->VolatileFileId = volatile_fid;
1245
1246 if (indatalen) {
1247 req->InputCount = cpu_to_le32(indatalen);
1248 /* do not set InputOffset if no input data */
1249 req->InputOffset =
1250 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1251 iov[1].iov_base = in_data;
1252 iov[1].iov_len = indatalen;
1253 num_iovecs = 2;
1254 } else
1255 num_iovecs = 1;
1256
1257 req->OutputOffset = 0;
1258 req->OutputCount = 0; /* MBZ */
1259
1260 /*
1261 * Could increase MaxOutputResponse, but that would require more
1262 * than one credit. Windows typically sets this smaller, but for some
1263 * ioctls it may be useful to allow server to send more. No point
1264 * limiting what the server can send as long as fits in one credit
1265 */
1266 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1267
1268 if (is_fsctl)
1269 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1270 else
1271 req->Flags = 0;
1272
1273 iov[0].iov_base = (char *)req;
4a72dafa 1274
7ff8d45c
SF
1275 /*
1276 * If no input data, the size of ioctl struct in
1277 * protocol spec still includes a 1 byte data buffer,
1278 * but if input data passed to ioctl, we do not
1279 * want to double count this, so we do not send
1280 * the dummy one byte of data in iovec[0] if sending
1281 * input data (in iovec[1]). We also must add 4 bytes
1282 * in first iovec to allow for rfc1002 length field.
1283 */
1284
1285 if (indatalen) {
1286 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1287 inc_rfc1001_len(req, indatalen - 1);
1288 } else
1289 iov[0].iov_len = get_rfc1002_length(req) + 4;
1290
4a72dafa
SF
1291
1292 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1293 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1294
9bf0c9cd 1295 if ((rc != 0) && (rc != -EINVAL)) {
4a72dafa
SF
1296 if (tcon)
1297 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1298 goto ioctl_exit;
9bf0c9cd
SF
1299 } else if (rc == -EINVAL) {
1300 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1301 (opcode != FSCTL_SRV_COPYCHUNK)) {
1302 if (tcon)
1303 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1304 goto ioctl_exit;
1305 }
4a72dafa
SF
1306 }
1307
1308 /* check if caller wants to look at return data or just return rc */
1309 if ((plen == NULL) || (out_data == NULL))
1310 goto ioctl_exit;
1311
1312 *plen = le32_to_cpu(rsp->OutputCount);
1313
1314 /* We check for obvious errors in the output buffer length and offset */
1315 if (*plen == 0)
1316 goto ioctl_exit; /* server returned no data */
1317 else if (*plen > 0xFF00) {
1318 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1319 *plen = 0;
1320 rc = -EIO;
1321 goto ioctl_exit;
1322 }
1323
1324 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1325 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1326 le32_to_cpu(rsp->OutputOffset));
1327 *plen = 0;
1328 rc = -EIO;
1329 goto ioctl_exit;
1330 }
1331
1332 *out_data = kmalloc(*plen, GFP_KERNEL);
1333 if (*out_data == NULL) {
1334 rc = -ENOMEM;
1335 goto ioctl_exit;
1336 }
1337
1338 memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
1339 *plen);
1340ioctl_exit:
1341 free_rsp_buf(resp_buftype, rsp);
1342 return rc;
1343}
1344
64a5cfa6
SF
1345/*
1346 * Individual callers to ioctl worker function follow
1347 */
1348
1349int
1350SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1351 u64 persistent_fid, u64 volatile_fid)
1352{
1353 int rc;
64a5cfa6
SF
1354 struct compress_ioctl fsctl_input;
1355 char *ret_data = NULL;
1356
1357 fsctl_input.CompressionState =
1358 __constant_cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1359
1360 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1361 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1362 (char *)&fsctl_input /* data input */,
1363 2 /* in data len */, &ret_data /* out data */, NULL);
1364
1365 cifs_dbg(FYI, "set compression rc %d\n", rc);
64a5cfa6
SF
1366
1367 return rc;
1368}
1369
2503a0db
PS
1370int
1371SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1372 u64 persistent_fid, u64 volatile_fid)
1373{
1374 struct smb2_close_req *req;
1375 struct smb2_close_rsp *rsp;
1376 struct TCP_Server_Info *server;
1377 struct cifs_ses *ses = tcon->ses;
1378 struct kvec iov[1];
1379 int resp_buftype;
1380 int rc = 0;
1381
f96637be 1382 cifs_dbg(FYI, "Close\n");
2503a0db
PS
1383
1384 if (ses && (ses->server))
1385 server = ses->server;
1386 else
1387 return -EIO;
1388
1389 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1390 if (rc)
1391 return rc;
1392
1393 req->PersistentFileId = persistent_fid;
1394 req->VolatileFileId = volatile_fid;
1395
1396 iov[0].iov_base = (char *)req;
1397 /* 4 for rfc1002 length field */
1398 iov[0].iov_len = get_rfc1002_length(req) + 4;
1399
1400 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1401 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1402
1403 if (rc != 0) {
1404 if (tcon)
1405 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1406 goto close_exit;
1407 }
1408
2503a0db
PS
1409 /* BB FIXME - decode close response, update inode for caching */
1410
1411close_exit:
1412 free_rsp_buf(resp_buftype, rsp);
1413 return rc;
1414}
be4cb9e3
PS
1415
1416static int
1417validate_buf(unsigned int offset, unsigned int buffer_length,
1418 struct smb2_hdr *hdr, unsigned int min_buf_size)
1419
1420{
1421 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1422 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1423 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1424 char *end_of_buf = begin_of_buf + buffer_length;
1425
1426
1427 if (buffer_length < min_buf_size) {
f96637be
JP
1428 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1429 buffer_length, min_buf_size);
be4cb9e3
PS
1430 return -EINVAL;
1431 }
1432
1433 /* check if beyond RFC1001 maximum length */
1434 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
f96637be
JP
1435 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1436 buffer_length, smb_len);
be4cb9e3
PS
1437 return -EINVAL;
1438 }
1439
1440 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
f96637be 1441 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
be4cb9e3
PS
1442 return -EINVAL;
1443 }
1444
1445 return 0;
1446}
1447
1448/*
1449 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1450 * Caller must free buffer.
1451 */
1452static int
1453validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1454 struct smb2_hdr *hdr, unsigned int minbufsize,
1455 char *data)
1456
1457{
1458 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1459 int rc;
1460
1461 if (!data)
1462 return -EINVAL;
1463
1464 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1465 if (rc)
1466 return rc;
1467
1468 memcpy(data, begin_of_buf, buffer_length);
1469
1470 return 0;
1471}
1472
f0df737e
PS
1473static int
1474query_info(const unsigned int xid, struct cifs_tcon *tcon,
1475 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1476 size_t output_len, size_t min_len, void *data)
be4cb9e3
PS
1477{
1478 struct smb2_query_info_req *req;
1479 struct smb2_query_info_rsp *rsp = NULL;
1480 struct kvec iov[2];
1481 int rc = 0;
1482 int resp_buftype;
1483 struct TCP_Server_Info *server;
1484 struct cifs_ses *ses = tcon->ses;
1485
f96637be 1486 cifs_dbg(FYI, "Query Info\n");
be4cb9e3
PS
1487
1488 if (ses && (ses->server))
1489 server = ses->server;
1490 else
1491 return -EIO;
1492
1493 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1494 if (rc)
1495 return rc;
1496
1497 req->InfoType = SMB2_O_INFO_FILE;
f0df737e 1498 req->FileInfoClass = info_class;
be4cb9e3
PS
1499 req->PersistentFileId = persistent_fid;
1500 req->VolatileFileId = volatile_fid;
1501 /* 4 for rfc1002 length field and 1 for Buffer */
1502 req->InputBufferOffset =
1503 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
f0df737e 1504 req->OutputBufferLength = cpu_to_le32(output_len);
be4cb9e3
PS
1505
1506 iov[0].iov_base = (char *)req;
1507 /* 4 for rfc1002 length field */
1508 iov[0].iov_len = get_rfc1002_length(req) + 4;
1509
1510 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
e5d04887
PS
1511 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1512
be4cb9e3
PS
1513 if (rc) {
1514 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1515 goto qinf_exit;
1516 }
1517
be4cb9e3
PS
1518 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1519 le32_to_cpu(rsp->OutputBufferLength),
f0df737e 1520 &rsp->hdr, min_len, data);
be4cb9e3
PS
1521
1522qinf_exit:
1523 free_rsp_buf(resp_buftype, rsp);
1524 return rc;
1525}
9094fad1 1526
f0df737e
PS
1527int
1528SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1529 u64 persistent_fid, u64 volatile_fid,
1530 struct smb2_file_all_info *data)
1531{
1532 return query_info(xid, tcon, persistent_fid, volatile_fid,
1533 FILE_ALL_INFORMATION,
1534 sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
1535 sizeof(struct smb2_file_all_info), data);
1536}
1537
1538int
1539SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1540 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1541{
1542 return query_info(xid, tcon, persistent_fid, volatile_fid,
1543 FILE_INTERNAL_INFORMATION,
1544 sizeof(struct smb2_file_internal_info),
1545 sizeof(struct smb2_file_internal_info), uniqueid);
1546}
1547
9094fad1
PS
1548/*
1549 * This is a no-op for now. We're not really interested in the reply, but
1550 * rather in the fact that the server sent one and that server->lstrp
1551 * gets updated.
1552 *
1553 * FIXME: maybe we should consider checking that the reply matches request?
1554 */
1555static void
1556smb2_echo_callback(struct mid_q_entry *mid)
1557{
1558 struct TCP_Server_Info *server = mid->callback_data;
1559 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1560 unsigned int credits_received = 1;
1561
1562 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1563 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1564
1565 DeleteMidQEntry(mid);
1566 add_credits(server, credits_received, CIFS_ECHO_OP);
1567}
1568
1569int
1570SMB2_echo(struct TCP_Server_Info *server)
1571{
1572 struct smb2_echo_req *req;
1573 int rc = 0;
1574 struct kvec iov;
fec344e3
JL
1575 struct smb_rqst rqst = { .rq_iov = &iov,
1576 .rq_nvec = 1 };
9094fad1 1577
f96637be 1578 cifs_dbg(FYI, "In echo request\n");
9094fad1
PS
1579
1580 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1581 if (rc)
1582 return rc;
1583
1584 req->hdr.CreditRequest = cpu_to_le16(1);
1585
1586 iov.iov_base = (char *)req;
1587 /* 4 for rfc1002 length field */
1588 iov.iov_len = get_rfc1002_length(req) + 4;
1589
fec344e3 1590 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
9094fad1
PS
1591 CIFS_ECHO_OP);
1592 if (rc)
f96637be 1593 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
9094fad1
PS
1594
1595 cifs_small_buf_release(req);
1596 return rc;
1597}
7a5cfb19
PS
1598
1599int
1600SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1601 u64 volatile_fid)
1602{
1603 struct smb2_flush_req *req;
1604 struct TCP_Server_Info *server;
1605 struct cifs_ses *ses = tcon->ses;
1606 struct kvec iov[1];
1607 int resp_buftype;
1608 int rc = 0;
1609
f96637be 1610 cifs_dbg(FYI, "Flush\n");
7a5cfb19
PS
1611
1612 if (ses && (ses->server))
1613 server = ses->server;
1614 else
1615 return -EIO;
1616
1617 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1618 if (rc)
1619 return rc;
1620
1621 req->PersistentFileId = persistent_fid;
1622 req->VolatileFileId = volatile_fid;
1623
1624 iov[0].iov_base = (char *)req;
1625 /* 4 for rfc1002 length field */
1626 iov[0].iov_len = get_rfc1002_length(req) + 4;
1627
1628 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1629
1630 if ((rc != 0) && tcon)
1631 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1632
1633 free_rsp_buf(resp_buftype, iov[0].iov_base);
1634 return rc;
1635}
09a4707e
PS
1636
1637/*
1638 * To form a chain of read requests, any read requests after the first should
1639 * have the end_of_chain boolean set to true.
1640 */
1641static int
1642smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1643 unsigned int remaining_bytes, int request_type)
1644{
1645 int rc = -EACCES;
1646 struct smb2_read_req *req = NULL;
1647
1648 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1649 if (rc)
1650 return rc;
1651 if (io_parms->tcon->ses->server == NULL)
1652 return -ECONNABORTED;
1653
1654 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1655
1656 req->PersistentFileId = io_parms->persistent_fid;
1657 req->VolatileFileId = io_parms->volatile_fid;
1658 req->ReadChannelInfoOffset = 0; /* reserved */
1659 req->ReadChannelInfoLength = 0; /* reserved */
1660 req->Channel = 0; /* reserved */
1661 req->MinimumCount = 0;
1662 req->Length = cpu_to_le32(io_parms->length);
1663 req->Offset = cpu_to_le64(io_parms->offset);
1664
1665 if (request_type & CHAINED_REQUEST) {
1666 if (!(request_type & END_OF_CHAIN)) {
1667 /* 4 for rfc1002 length field */
1668 req->hdr.NextCommand =
1669 cpu_to_le32(get_rfc1002_length(req) + 4);
1670 } else /* END_OF_CHAIN */
1671 req->hdr.NextCommand = 0;
1672 if (request_type & RELATED_REQUEST) {
1673 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1674 /*
1675 * Related requests use info from previous read request
1676 * in chain.
1677 */
1678 req->hdr.SessionId = 0xFFFFFFFF;
1679 req->hdr.TreeId = 0xFFFFFFFF;
1680 req->PersistentFileId = 0xFFFFFFFF;
1681 req->VolatileFileId = 0xFFFFFFFF;
1682 }
1683 }
1684 if (remaining_bytes > io_parms->length)
1685 req->RemainingBytes = cpu_to_le32(remaining_bytes);
1686 else
1687 req->RemainingBytes = 0;
1688
1689 iov[0].iov_base = (char *)req;
1690 /* 4 for rfc1002 length field */
1691 iov[0].iov_len = get_rfc1002_length(req) + 4;
1692 return rc;
1693}
1694
1695static void
1696smb2_readv_callback(struct mid_q_entry *mid)
1697{
1698 struct cifs_readdata *rdata = mid->callback_data;
1699 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1700 struct TCP_Server_Info *server = tcon->ses->server;
5819575e 1701 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
09a4707e 1702 unsigned int credits_received = 1;
5819575e 1703 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
8321fec4
JL
1704 .rq_nvec = 1,
1705 .rq_pages = rdata->pages,
1706 .rq_npages = rdata->nr_pages,
1707 .rq_pagesz = rdata->pagesz,
1708 .rq_tailsz = rdata->tailsz };
09a4707e 1709
f96637be
JP
1710 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1711 __func__, mid->mid, mid->mid_state, rdata->result,
1712 rdata->bytes);
09a4707e
PS
1713
1714 switch (mid->mid_state) {
1715 case MID_RESPONSE_RECEIVED:
1716 credits_received = le16_to_cpu(buf->CreditRequest);
1717 /* result already set, check signature */
38d77c50 1718 if (server->sign) {
3c1bf7e4
PS
1719 int rc;
1720
0b688cfc 1721 rc = smb2_verify_signature(&rqst, server);
3c1bf7e4 1722 if (rc)
f96637be
JP
1723 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1724 rc);
3c1bf7e4 1725 }
09a4707e
PS
1726 /* FIXME: should this be counted toward the initiating task? */
1727 task_io_account_read(rdata->bytes);
1728 cifs_stats_bytes_read(tcon, rdata->bytes);
1729 break;
1730 case MID_REQUEST_SUBMITTED:
1731 case MID_RETRY_NEEDED:
1732 rdata->result = -EAGAIN;
1733 break;
1734 default:
1735 if (rdata->result != -ENODATA)
1736 rdata->result = -EIO;
1737 }
1738
1739 if (rdata->result)
1740 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
1741
1742 queue_work(cifsiod_wq, &rdata->work);
1743 DeleteMidQEntry(mid);
1744 add_credits(server, credits_received, 0);
1745}
1746
1747/* smb2_async_readv - send an async write, and set up mid to handle result */
1748int
1749smb2_async_readv(struct cifs_readdata *rdata)
1750{
1751 int rc;
1752 struct smb2_hdr *buf;
1753 struct cifs_io_parms io_parms;
5819575e 1754 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
fec344e3 1755 .rq_nvec = 1 };
09a4707e 1756
f96637be
JP
1757 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1758 __func__, rdata->offset, rdata->bytes);
09a4707e
PS
1759
1760 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
1761 io_parms.offset = rdata->offset;
1762 io_parms.length = rdata->bytes;
1763 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
1764 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
1765 io_parms.pid = rdata->pid;
5819575e 1766 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
09a4707e
PS
1767 if (rc)
1768 return rc;
1769
5819575e 1770 buf = (struct smb2_hdr *)rdata->iov.iov_base;
09a4707e 1771 /* 4 for rfc1002 length field */
5819575e 1772 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
09a4707e
PS
1773
1774 kref_get(&rdata->refcount);
fec344e3 1775 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
09a4707e
PS
1776 cifs_readv_receive, smb2_readv_callback,
1777 rdata, 0);
e5d04887 1778 if (rc) {
09a4707e 1779 kref_put(&rdata->refcount, cifs_readdata_release);
e5d04887
PS
1780 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
1781 }
09a4707e
PS
1782
1783 cifs_small_buf_release(buf);
1784 return rc;
1785}
33319141 1786
d8e05039
PS
1787int
1788SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
1789 unsigned int *nbytes, char **buf, int *buf_type)
1790{
1791 int resp_buftype, rc = -EACCES;
1792 struct smb2_read_rsp *rsp = NULL;
1793 struct kvec iov[1];
1794
1795 *nbytes = 0;
1796 rc = smb2_new_read_req(iov, io_parms, 0, 0);
1797 if (rc)
1798 return rc;
1799
1800 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
1801 &resp_buftype, CIFS_LOG_ERROR);
1802
1803 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
1804
1805 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
1806 free_rsp_buf(resp_buftype, iov[0].iov_base);
1807 return 0;
1808 }
1809
1810 if (rc) {
1811 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
f96637be 1812 cifs_dbg(VFS, "Send error in read = %d\n", rc);
d8e05039
PS
1813 } else {
1814 *nbytes = le32_to_cpu(rsp->DataLength);
1815 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
1816 (*nbytes > io_parms->length)) {
f96637be
JP
1817 cifs_dbg(FYI, "bad length %d for count %d\n",
1818 *nbytes, io_parms->length);
d8e05039
PS
1819 rc = -EIO;
1820 *nbytes = 0;
1821 }
1822 }
1823
1824 if (*buf) {
1825 memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
1826 *nbytes);
1827 free_rsp_buf(resp_buftype, iov[0].iov_base);
1828 } else if (resp_buftype != CIFS_NO_BUFFER) {
1829 *buf = iov[0].iov_base;
1830 if (resp_buftype == CIFS_SMALL_BUFFER)
1831 *buf_type = CIFS_SMALL_BUFFER;
1832 else if (resp_buftype == CIFS_LARGE_BUFFER)
1833 *buf_type = CIFS_LARGE_BUFFER;
1834 }
1835 return rc;
1836}
1837
33319141
PS
1838/*
1839 * Check the mid_state and signature on received buffer (if any), and queue the
1840 * workqueue completion task.
1841 */
1842static void
1843smb2_writev_callback(struct mid_q_entry *mid)
1844{
1845 struct cifs_writedata *wdata = mid->callback_data;
1846 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1847 unsigned int written;
1848 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
1849 unsigned int credits_received = 1;
1850
1851 switch (mid->mid_state) {
1852 case MID_RESPONSE_RECEIVED:
1853 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
1854 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
1855 if (wdata->result != 0)
1856 break;
1857
1858 written = le32_to_cpu(rsp->DataLength);
1859 /*
1860 * Mask off high 16 bits when bytes written as returned
1861 * by the server is greater than bytes requested by the
1862 * client. OS/2 servers are known to set incorrect
1863 * CountHigh values.
1864 */
1865 if (written > wdata->bytes)
1866 written &= 0xFFFF;
1867
1868 if (written < wdata->bytes)
1869 wdata->result = -ENOSPC;
1870 else
1871 wdata->bytes = written;
1872 break;
1873 case MID_REQUEST_SUBMITTED:
1874 case MID_RETRY_NEEDED:
1875 wdata->result = -EAGAIN;
1876 break;
1877 default:
1878 wdata->result = -EIO;
1879 break;
1880 }
1881
1882 if (wdata->result)
1883 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
1884
1885 queue_work(cifsiod_wq, &wdata->work);
1886 DeleteMidQEntry(mid);
1887 add_credits(tcon->ses->server, credits_received, 0);
1888}
1889
1890/* smb2_async_writev - send an async write, and set up mid to handle result */
1891int
4a5c80d7
SF
1892smb2_async_writev(struct cifs_writedata *wdata,
1893 void (*release)(struct kref *kref))
33319141 1894{
eddb079d 1895 int rc = -EACCES;
33319141
PS
1896 struct smb2_write_req *req = NULL;
1897 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
eddb079d 1898 struct kvec iov;
fec344e3 1899 struct smb_rqst rqst;
33319141
PS
1900
1901 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
1902 if (rc)
1903 goto async_writev_out;
1904
33319141
PS
1905 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
1906
1907 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
1908 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
1909 req->WriteChannelInfoOffset = 0;
1910 req->WriteChannelInfoLength = 0;
1911 req->Channel = 0;
1912 req->Offset = cpu_to_le64(wdata->offset);
1913 /* 4 for rfc1002 length field */
1914 req->DataOffset = cpu_to_le16(
1915 offsetof(struct smb2_write_req, Buffer) - 4);
1916 req->RemainingBytes = 0;
1917
1918 /* 4 for rfc1002 length field and 1 for Buffer */
eddb079d
JL
1919 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
1920 iov.iov_base = req;
33319141 1921
eddb079d
JL
1922 rqst.rq_iov = &iov;
1923 rqst.rq_nvec = 1;
1924 rqst.rq_pages = wdata->pages;
1925 rqst.rq_npages = wdata->nr_pages;
1926 rqst.rq_pagesz = wdata->pagesz;
1927 rqst.rq_tailsz = wdata->tailsz;
33319141 1928
f96637be
JP
1929 cifs_dbg(FYI, "async write at %llu %u bytes\n",
1930 wdata->offset, wdata->bytes);
33319141
PS
1931
1932 req->Length = cpu_to_le32(wdata->bytes);
1933
1934 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
1935
1936 kref_get(&wdata->refcount);
fec344e3
JL
1937 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
1938 smb2_writev_callback, wdata, 0);
33319141 1939
e5d04887 1940 if (rc) {
4a5c80d7 1941 kref_put(&wdata->refcount, release);
e5d04887
PS
1942 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
1943 }
33319141 1944
33319141
PS
1945async_writev_out:
1946 cifs_small_buf_release(req);
33319141
PS
1947 return rc;
1948}
009d3443
PS
1949
1950/*
1951 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
1952 * The length field from io_parms must be at least 1 and indicates a number of
1953 * elements with data to write that begins with position 1 in iov array. All
1954 * data length is specified by count.
1955 */
1956int
1957SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
1958 unsigned int *nbytes, struct kvec *iov, int n_vec)
1959{
1960 int rc = 0;
1961 struct smb2_write_req *req = NULL;
1962 struct smb2_write_rsp *rsp = NULL;
1963 int resp_buftype;
1964 *nbytes = 0;
1965
1966 if (n_vec < 1)
1967 return rc;
1968
1969 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
1970 if (rc)
1971 return rc;
1972
1973 if (io_parms->tcon->ses->server == NULL)
1974 return -ECONNABORTED;
1975
1976 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1977
1978 req->PersistentFileId = io_parms->persistent_fid;
1979 req->VolatileFileId = io_parms->volatile_fid;
1980 req->WriteChannelInfoOffset = 0;
1981 req->WriteChannelInfoLength = 0;
1982 req->Channel = 0;
1983 req->Length = cpu_to_le32(io_parms->length);
1984 req->Offset = cpu_to_le64(io_parms->offset);
1985 /* 4 for rfc1002 length field */
1986 req->DataOffset = cpu_to_le16(
1987 offsetof(struct smb2_write_req, Buffer) - 4);
1988 req->RemainingBytes = 0;
1989
1990 iov[0].iov_base = (char *)req;
1991 /* 4 for rfc1002 length field and 1 for Buffer */
1992 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1993
1994 /* length of entire message including data to be written */
1995 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
1996
1997 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
1998 &resp_buftype, 0);
e5d04887 1999 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
009d3443
PS
2000
2001 if (rc) {
2002 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
f96637be 2003 cifs_dbg(VFS, "Send error in write = %d\n", rc);
e5d04887 2004 } else
009d3443 2005 *nbytes = le32_to_cpu(rsp->DataLength);
e5d04887
PS
2006
2007 free_rsp_buf(resp_buftype, rsp);
009d3443
PS
2008 return rc;
2009}
35143eb5 2010
d324f08d
PS
2011static unsigned int
2012num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2013{
2014 int len;
2015 unsigned int entrycount = 0;
2016 unsigned int next_offset = 0;
2017 FILE_DIRECTORY_INFO *entryptr;
2018
2019 if (bufstart == NULL)
2020 return 0;
2021
2022 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2023
2024 while (1) {
2025 entryptr = (FILE_DIRECTORY_INFO *)
2026 ((char *)entryptr + next_offset);
2027
2028 if ((char *)entryptr + size > end_of_buf) {
f96637be 2029 cifs_dbg(VFS, "malformed search entry would overflow\n");
d324f08d
PS
2030 break;
2031 }
2032
2033 len = le32_to_cpu(entryptr->FileNameLength);
2034 if ((char *)entryptr + len + size > end_of_buf) {
f96637be
JP
2035 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2036 end_of_buf);
d324f08d
PS
2037 break;
2038 }
2039
2040 *lastentry = (char *)entryptr;
2041 entrycount++;
2042
2043 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2044 if (!next_offset)
2045 break;
2046 }
2047
2048 return entrycount;
2049}
2050
2051/*
2052 * Readdir/FindFirst
2053 */
2054int
2055SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2056 u64 persistent_fid, u64 volatile_fid, int index,
2057 struct cifs_search_info *srch_inf)
2058{
2059 struct smb2_query_directory_req *req;
2060 struct smb2_query_directory_rsp *rsp = NULL;
2061 struct kvec iov[2];
2062 int rc = 0;
2063 int len;
2064 int resp_buftype;
2065 unsigned char *bufptr;
2066 struct TCP_Server_Info *server;
2067 struct cifs_ses *ses = tcon->ses;
2068 __le16 asteriks = cpu_to_le16('*');
2069 char *end_of_smb;
2070 unsigned int output_size = CIFSMaxBufSize;
2071 size_t info_buf_size;
2072
2073 if (ses && (ses->server))
2074 server = ses->server;
2075 else
2076 return -EIO;
2077
2078 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2079 if (rc)
2080 return rc;
2081
2082 switch (srch_inf->info_level) {
2083 case SMB_FIND_FILE_DIRECTORY_INFO:
2084 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2085 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2086 break;
2087 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2088 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2089 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2090 break;
2091 default:
f96637be
JP
2092 cifs_dbg(VFS, "info level %u isn't supported\n",
2093 srch_inf->info_level);
d324f08d
PS
2094 rc = -EINVAL;
2095 goto qdir_exit;
2096 }
2097
2098 req->FileIndex = cpu_to_le32(index);
2099 req->PersistentFileId = persistent_fid;
2100 req->VolatileFileId = volatile_fid;
2101
2102 len = 0x2;
2103 bufptr = req->Buffer;
2104 memcpy(bufptr, &asteriks, len);
2105
2106 req->FileNameOffset =
2107 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2108 req->FileNameLength = cpu_to_le16(len);
2109 /*
2110 * BB could be 30 bytes or so longer if we used SMB2 specific
2111 * buffer lengths, but this is safe and close enough.
2112 */
2113 output_size = min_t(unsigned int, output_size, server->maxBuf);
2114 output_size = min_t(unsigned int, output_size, 2 << 15);
2115 req->OutputBufferLength = cpu_to_le32(output_size);
2116
2117 iov[0].iov_base = (char *)req;
2118 /* 4 for RFC1001 length and 1 for Buffer */
2119 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2120
2121 iov[1].iov_base = (char *)(req->Buffer);
2122 iov[1].iov_len = len;
2123
2124 inc_rfc1001_len(req, len - 1 /* Buffer */);
2125
2126 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
e5d04887
PS
2127 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2128
d324f08d
PS
2129 if (rc) {
2130 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2131 goto qdir_exit;
2132 }
d324f08d
PS
2133
2134 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2135 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2136 info_buf_size);
2137 if (rc)
2138 goto qdir_exit;
2139
2140 srch_inf->unicode = true;
2141
2142 if (srch_inf->ntwrk_buf_start) {
2143 if (srch_inf->smallBuf)
2144 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2145 else
2146 cifs_buf_release(srch_inf->ntwrk_buf_start);
2147 }
2148 srch_inf->ntwrk_buf_start = (char *)rsp;
2149 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2150 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2151 /* 4 for rfc1002 length field */
2152 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2153 srch_inf->entries_in_buffer =
2154 num_entries(srch_inf->srch_entries_start, end_of_smb,
2155 &srch_inf->last_entry, info_buf_size);
2156 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
f96637be
JP
2157 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2158 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2159 srch_inf->srch_entries_start, srch_inf->last_entry);
d324f08d
PS
2160 if (resp_buftype == CIFS_LARGE_BUFFER)
2161 srch_inf->smallBuf = false;
2162 else if (resp_buftype == CIFS_SMALL_BUFFER)
2163 srch_inf->smallBuf = true;
2164 else
f96637be 2165 cifs_dbg(VFS, "illegal search buffer type\n");
d324f08d
PS
2166
2167 if (rsp->hdr.Status == STATUS_NO_MORE_FILES)
2168 srch_inf->endOfSearch = 1;
2169 else
2170 srch_inf->endOfSearch = 0;
2171
2172 return rc;
2173
2174qdir_exit:
2175 free_rsp_buf(resp_buftype, rsp);
2176 return rc;
2177}
2178
35143eb5
PS
2179static int
2180send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
c839ff24 2181 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
35143eb5
PS
2182 unsigned int num, void **data, unsigned int *size)
2183{
2184 struct smb2_set_info_req *req;
2185 struct smb2_set_info_rsp *rsp = NULL;
2186 struct kvec *iov;
2187 int rc = 0;
2188 int resp_buftype;
2189 unsigned int i;
2190 struct TCP_Server_Info *server;
2191 struct cifs_ses *ses = tcon->ses;
2192
2193 if (ses && (ses->server))
2194 server = ses->server;
2195 else
2196 return -EIO;
2197
2198 if (!num)
2199 return -EINVAL;
2200
2201 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2202 if (!iov)
2203 return -ENOMEM;
2204
2205 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2206 if (rc) {
2207 kfree(iov);
2208 return rc;
2209 }
2210
c839ff24
PS
2211 req->hdr.ProcessId = cpu_to_le32(pid);
2212
35143eb5
PS
2213 req->InfoType = SMB2_O_INFO_FILE;
2214 req->FileInfoClass = info_class;
2215 req->PersistentFileId = persistent_fid;
2216 req->VolatileFileId = volatile_fid;
2217
2218 /* 4 for RFC1001 length and 1 for Buffer */
2219 req->BufferOffset =
2220 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2221 req->BufferLength = cpu_to_le32(*size);
2222
2223 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2224
2225 memcpy(req->Buffer, *data, *size);
2226
2227 iov[0].iov_base = (char *)req;
2228 /* 4 for RFC1001 length */
2229 iov[0].iov_len = get_rfc1002_length(req) + 4;
2230
2231 for (i = 1; i < num; i++) {
2232 inc_rfc1001_len(req, size[i]);
2233 le32_add_cpu(&req->BufferLength, size[i]);
2234 iov[i].iov_base = (char *)data[i];
2235 iov[i].iov_len = size[i];
2236 }
2237
2238 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2239 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2240
7d3fb24b 2241 if (rc != 0)
35143eb5 2242 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
7d3fb24b 2243
35143eb5
PS
2244 free_rsp_buf(resp_buftype, rsp);
2245 kfree(iov);
2246 return rc;
2247}
2248
2249int
2250SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2251 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2252{
2253 struct smb2_file_rename_info info;
2254 void **data;
2255 unsigned int size[2];
2256 int rc;
2257 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2258
2259 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2260 if (!data)
2261 return -ENOMEM;
2262
2263 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2264 /* 0 = fail if target already exists */
2265 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2266 info.FileNameLength = cpu_to_le32(len);
2267
2268 data[0] = &info;
2269 size[0] = sizeof(struct smb2_file_rename_info);
2270
2271 data[1] = target_file;
2272 size[1] = len + 2 /* null */;
2273
2274 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
c839ff24
PS
2275 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2276 size);
35143eb5
PS
2277 kfree(data);
2278 return rc;
2279}
568798cc
PS
2280
2281int
2282SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2283 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2284{
2285 struct smb2_file_link_info info;
2286 void **data;
2287 unsigned int size[2];
2288 int rc;
2289 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2290
2291 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2292 if (!data)
2293 return -ENOMEM;
2294
2295 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2296 /* 0 = fail if link already exists */
2297 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2298 info.FileNameLength = cpu_to_le32(len);
2299
2300 data[0] = &info;
2301 size[0] = sizeof(struct smb2_file_link_info);
2302
2303 data[1] = target_file;
2304 size[1] = len + 2 /* null */;
2305
2306 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
c839ff24 2307 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
568798cc
PS
2308 kfree(data);
2309 return rc;
2310}
c839ff24
PS
2311
2312int
2313SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2314 u64 volatile_fid, u32 pid, __le64 *eof)
2315{
2316 struct smb2_file_eof_info info;
2317 void *data;
2318 unsigned int size;
2319
2320 info.EndOfFile = *eof;
2321
2322 data = &info;
2323 size = sizeof(struct smb2_file_eof_info);
2324
2325 return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
2326 FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2327}
1feeaac7
PS
2328
2329int
2330SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2331 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2332{
2333 unsigned int size;
2334 size = sizeof(FILE_BASIC_INFO);
2335 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2336 current->tgid, FILE_BASIC_INFORMATION, 1,
2337 (void **)&buf, &size);
2338}
983c88a4
PS
2339
2340int
2341SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2342 const u64 persistent_fid, const u64 volatile_fid,
2343 __u8 oplock_level)
2344{
2345 int rc;
2346 struct smb2_oplock_break *req = NULL;
2347
f96637be 2348 cifs_dbg(FYI, "SMB2_oplock_break\n");
983c88a4
PS
2349 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2350
2351 if (rc)
2352 return rc;
2353
2354 req->VolatileFid = volatile_fid;
2355 req->PersistentFid = persistent_fid;
2356 req->OplockLevel = oplock_level;
2357 req->hdr.CreditRequest = cpu_to_le16(1);
2358
2359 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2360 /* SMB2 buffer freed by function above */
2361
2362 if (rc) {
2363 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
f96637be 2364 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
983c88a4
PS
2365 }
2366
2367 return rc;
2368}
6fc05c25
PS
2369
2370static void
2371copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2372 struct kstatfs *kst)
2373{
2374 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2375 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2376 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2377 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2378 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2379 return;
2380}
2381
2382static int
2383build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2384 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2385{
2386 int rc;
2387 struct smb2_query_info_req *req;
2388
f96637be 2389 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
6fc05c25
PS
2390
2391 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2392 return -EIO;
2393
2394 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2395 if (rc)
2396 return rc;
2397
2398 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2399 req->FileInfoClass = level;
2400 req->PersistentFileId = persistent_fid;
2401 req->VolatileFileId = volatile_fid;
2402 /* 4 for rfc1002 length field and 1 for pad */
2403 req->InputBufferOffset =
2404 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2405 req->OutputBufferLength = cpu_to_le32(
2406 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2407
2408 iov->iov_base = (char *)req;
2409 /* 4 for rfc1002 length field */
2410 iov->iov_len = get_rfc1002_length(req) + 4;
2411 return 0;
2412}
2413
2414int
2415SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2416 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2417{
2418 struct smb2_query_info_rsp *rsp = NULL;
2419 struct kvec iov;
2420 int rc = 0;
2421 int resp_buftype;
2422 struct cifs_ses *ses = tcon->ses;
2423 struct smb2_fs_full_size_info *info = NULL;
2424
2425 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2426 sizeof(struct smb2_fs_full_size_info),
2427 persistent_fid, volatile_fid);
2428 if (rc)
2429 return rc;
2430
2431 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2432 if (rc) {
2433 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
34f62640 2434 goto qfsinf_exit;
6fc05c25
PS
2435 }
2436 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2437
2438 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2439 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2440 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2441 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2442 sizeof(struct smb2_fs_full_size_info));
2443 if (!rc)
2444 copy_fs_info_to_kstatfs(info, fsdata);
2445
34f62640
SF
2446qfsinf_exit:
2447 free_rsp_buf(resp_buftype, iov.iov_base);
2448 return rc;
2449}
2450
2451int
2452SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
2167114c 2453 u64 persistent_fid, u64 volatile_fid, int level)
34f62640
SF
2454{
2455 struct smb2_query_info_rsp *rsp = NULL;
2456 struct kvec iov;
2457 int rc = 0;
2167114c 2458 int resp_buftype, max_len, min_len;
34f62640
SF
2459 struct cifs_ses *ses = tcon->ses;
2460 unsigned int rsp_len, offset;
2461
2167114c
SF
2462 if (level == FS_DEVICE_INFORMATION) {
2463 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2464 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2465 } else if (level == FS_ATTRIBUTE_INFORMATION) {
2466 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
2467 min_len = MIN_FS_ATTR_INFO_SIZE;
af6a12ea
SF
2468 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
2469 max_len = sizeof(struct smb3_fs_ss_info);
2470 min_len = sizeof(struct smb3_fs_ss_info);
2167114c 2471 } else {
af6a12ea 2472 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
2167114c
SF
2473 return -EINVAL;
2474 }
2475
2476 rc = build_qfs_info_req(&iov, tcon, level, max_len,
34f62640
SF
2477 persistent_fid, volatile_fid);
2478 if (rc)
2479 return rc;
2480
2481 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2482 if (rc) {
2483 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2484 goto qfsattr_exit;
2485 }
2486 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2487
2488 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
2489 offset = le16_to_cpu(rsp->OutputBufferOffset);
2167114c
SF
2490 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
2491 if (rc)
2492 goto qfsattr_exit;
2493
2494 if (level == FS_ATTRIBUTE_INFORMATION)
34f62640
SF
2495 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
2496 + (char *)&rsp->hdr, min_t(unsigned int,
2167114c
SF
2497 rsp_len, max_len));
2498 else if (level == FS_DEVICE_INFORMATION)
2499 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
2500 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
af6a12ea
SF
2501 else if (level == FS_SECTOR_SIZE_INFORMATION) {
2502 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
2503 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
2504 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
2505 tcon->perf_sector_size =
2506 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
2507 }
34f62640
SF
2508
2509qfsattr_exit:
6fc05c25
PS
2510 free_rsp_buf(resp_buftype, iov.iov_base);
2511 return rc;
2512}
f7ba7fe6
PS
2513
2514int
2515smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2516 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2517 const __u32 num_lock, struct smb2_lock_element *buf)
2518{
2519 int rc = 0;
2520 struct smb2_lock_req *req = NULL;
2521 struct kvec iov[2];
2522 int resp_buf_type;
2523 unsigned int count;
2524
f96637be 2525 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
f7ba7fe6
PS
2526
2527 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2528 if (rc)
2529 return rc;
2530
2531 req->hdr.ProcessId = cpu_to_le32(pid);
2532 req->LockCount = cpu_to_le16(num_lock);
2533
2534 req->PersistentFileId = persist_fid;
2535 req->VolatileFileId = volatile_fid;
2536
2537 count = num_lock * sizeof(struct smb2_lock_element);
2538 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2539
2540 iov[0].iov_base = (char *)req;
2541 /* 4 for rfc1002 length field and count for all locks */
2542 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2543 iov[1].iov_base = (char *)buf;
2544 iov[1].iov_len = count;
2545
2546 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2547 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2548 if (rc) {
f96637be 2549 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
f7ba7fe6
PS
2550 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2551 }
2552
2553 return rc;
2554}
2555
2556int
2557SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2558 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2559 const __u64 length, const __u64 offset, const __u32 lock_flags,
2560 const bool wait)
2561{
2562 struct smb2_lock_element lock;
2563
2564 lock.Offset = cpu_to_le64(offset);
2565 lock.Length = cpu_to_le64(length);
2566 lock.Flags = cpu_to_le32(lock_flags);
2567 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2568 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2569
2570 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2571}
0822f514
PS
2572
2573int
2574SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2575 __u8 *lease_key, const __le32 lease_state)
2576{
2577 int rc;
2578 struct smb2_lease_ack *req = NULL;
2579
f96637be 2580 cifs_dbg(FYI, "SMB2_lease_break\n");
0822f514
PS
2581 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2582
2583 if (rc)
2584 return rc;
2585
2586 req->hdr.CreditRequest = cpu_to_le16(1);
2587 req->StructureSize = cpu_to_le16(36);
2588 inc_rfc1001_len(req, 12);
2589
2590 memcpy(req->LeaseKey, lease_key, 16);
2591 req->LeaseState = lease_state;
2592
2593 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2594 /* SMB2 buffer freed by function above */
2595
2596 if (rc) {
2597 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
f96637be 2598 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
0822f514
PS
2599 }
2600
2601 return rc;
2602}