]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/cifs/smb2pdu.c
CIFS: Fix possible freed pointer dereference in SMB2_sess_setup
[mirror_ubuntu-zesty-kernel.git] / fs / cifs / smb2pdu.c
CommitLineData
ec2e4523
PS
1/*
2 * fs/cifs/smb2pdu.c
3 *
4 * Copyright (C) International Business Machines Corp., 2009, 2011
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
111 hdr->TreeId = tcon->tid;
112 /* Uid is not converted */
113 if (tcon->ses)
114 hdr->SessionId = tcon->ses->Suid;
115 /* BB check following DFS flags BB */
116 /* BB do we have to add check for SHI1005_FLAGS_DFS_ROOT too? */
faaf946a
PS
117 if (tcon->share_flags & SHI1005_FLAGS_DFS)
118 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS;
ec2e4523
PS
119 /* BB how does SMB2 do case sensitive? */
120 /* if (tcon->nocase)
121 hdr->Flags |= SMBFLG_CASELESS; */
3c1bf7e4 122 if (tcon->ses && tcon->ses->server &&
ec2e4523 123 (tcon->ses->server->sec_mode & SECMODE_SIGN_REQUIRED))
3c1bf7e4 124 hdr->Flags |= SMB2_FLAGS_SIGNED;
ec2e4523
PS
125out:
126 pdu->StructureSize2 = cpu_to_le16(parmsize);
127 return;
128}
129
130static int
131smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
132{
133 int rc = 0;
aa24d1e9
PS
134 struct nls_table *nls_codepage;
135 struct cifs_ses *ses;
136 struct TCP_Server_Info *server;
137
138 /*
139 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
140 * check for tcp and smb session status done differently
141 * for those three - in the calling routine.
142 */
143 if (tcon == NULL)
144 return rc;
145
146 if (smb2_command == SMB2_TREE_CONNECT)
147 return rc;
148
149 if (tcon->tidStatus == CifsExiting) {
150 /*
151 * only tree disconnect, open, and write,
152 * (and ulogoff which does not have tcon)
153 * are allowed as we start force umount.
154 */
155 if ((smb2_command != SMB2_WRITE) &&
156 (smb2_command != SMB2_CREATE) &&
157 (smb2_command != SMB2_TREE_DISCONNECT)) {
158 cFYI(1, "can not send cmd %d while umounting",
159 smb2_command);
160 return -ENODEV;
161 }
162 }
163 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
164 (!tcon->ses->server))
165 return -EIO;
166
167 ses = tcon->ses;
168 server = ses->server;
169
170 /*
171 * Give demultiplex thread up to 10 seconds to reconnect, should be
172 * greater than cifs socket timeout which is 7 seconds
173 */
174 while (server->tcpStatus == CifsNeedReconnect) {
175 /*
176 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
177 * here since they are implicitly done when session drops.
178 */
179 switch (smb2_command) {
180 /*
181 * BB Should we keep oplock break and add flush to exceptions?
182 */
183 case SMB2_TREE_DISCONNECT:
184 case SMB2_CANCEL:
185 case SMB2_CLOSE:
186 case SMB2_OPLOCK_BREAK:
187 return -EAGAIN;
188 }
189
190 wait_event_interruptible_timeout(server->response_q,
191 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
192
193 /* are we still trying to reconnect? */
194 if (server->tcpStatus != CifsNeedReconnect)
195 break;
196
197 /*
198 * on "soft" mounts we wait once. Hard mounts keep
199 * retrying until process is killed or server comes
200 * back on-line
201 */
202 if (!tcon->retry) {
203 cFYI(1, "gave up waiting on reconnect in smb_init");
204 return -EHOSTDOWN;
205 }
206 }
207
208 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
209 return rc;
210
211 nls_codepage = load_nls_default();
212
213 /*
214 * need to prevent multiple threads trying to simultaneously reconnect
215 * the same SMB session
216 */
217 mutex_lock(&tcon->ses->session_mutex);
218 rc = cifs_negotiate_protocol(0, tcon->ses);
219 if (!rc && tcon->ses->need_reconnect)
220 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
221
222 if (rc || !tcon->need_reconnect) {
223 mutex_unlock(&tcon->ses->session_mutex);
224 goto out;
225 }
226
227 cifs_mark_open_files_invalid(tcon);
228 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
229 mutex_unlock(&tcon->ses->session_mutex);
230 cFYI(1, "reconnect tcon rc = %d", rc);
231 if (rc)
232 goto out;
233 atomic_inc(&tconInfoReconnectCount);
234 /*
235 * BB FIXME add code to check if wsize needs update due to negotiated
236 * smb buffer size shrinking.
237 */
238out:
239 /*
240 * Check if handle based operation so we know whether we can continue
241 * or not without returning to caller to reset file handle.
242 */
243 /*
244 * BB Is flush done by server on drop of tcp session? Should we special
245 * case it and skip above?
246 */
247 switch (smb2_command) {
248 case SMB2_FLUSH:
249 case SMB2_READ:
250 case SMB2_WRITE:
251 case SMB2_LOCK:
252 case SMB2_IOCTL:
253 case SMB2_QUERY_DIRECTORY:
254 case SMB2_CHANGE_NOTIFY:
255 case SMB2_QUERY_INFO:
256 case SMB2_SET_INFO:
257 return -EAGAIN;
258 }
259 unload_nls(nls_codepage);
ec2e4523
PS
260 return rc;
261}
262
263/*
264 * Allocate and return pointer to an SMB request hdr, and set basic
265 * SMB information in the SMB header. If the return code is zero, this
266 * function must have filled in request_buf pointer.
267 */
268static int
269small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
270 void **request_buf)
271{
272 int rc = 0;
273
274 rc = smb2_reconnect(smb2_command, tcon);
275 if (rc)
276 return rc;
277
278 /* BB eventually switch this to SMB2 specific small buf size */
279 *request_buf = cifs_small_buf_get();
280 if (*request_buf == NULL) {
281 /* BB should we add a retry in here if not a writepage? */
282 return -ENOMEM;
283 }
284
285 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
286
287 if (tcon != NULL) {
288#ifdef CONFIG_CIFS_STATS2
ec2e4523
PS
289 uint16_t com_code = le16_to_cpu(smb2_command);
290 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
ec2e4523
PS
291#endif
292 cifs_stats_inc(&tcon->num_smbs_sent);
293 }
294
295 return rc;
296}
297
298static void
299free_rsp_buf(int resp_buftype, void *rsp)
300{
301 if (resp_buftype == CIFS_SMALL_BUFFER)
302 cifs_small_buf_release(rsp);
303 else if (resp_buftype == CIFS_LARGE_BUFFER)
304 cifs_buf_release(rsp);
305}
306
b8c32dbb 307#define SMB2_NUM_PROT 2
ec2e4523
PS
308
309#define SMB2_PROT 0
310#define SMB21_PROT 1
311#define BAD_PROT 0xFFFF
312
313#define SMB2_PROT_ID 0x0202
314#define SMB21_PROT_ID 0x0210
315#define BAD_PROT_ID 0xFFFF
316
317static struct {
318 int index;
319 __le16 name;
320} smb2protocols[] = {
321 {SMB2_PROT, cpu_to_le16(SMB2_PROT_ID)},
322 {SMB21_PROT, cpu_to_le16(SMB21_PROT_ID)},
323 {BAD_PROT, cpu_to_le16(BAD_PROT_ID)}
324};
325
326/*
327 *
328 * SMB2 Worker functions follow:
329 *
330 * The general structure of the worker functions is:
331 * 1) Call smb2_init (assembles SMB2 header)
332 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
333 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
334 * 4) Decode SMB2 command specific fields in the fixed length area
335 * 5) Decode variable length data area (if any for this SMB2 command type)
336 * 6) Call free smb buffer
337 * 7) return
338 *
339 */
340
341int
342SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
343{
344 struct smb2_negotiate_req *req;
345 struct smb2_negotiate_rsp *rsp;
346 struct kvec iov[1];
347 int rc = 0;
348 int resp_buftype;
349 struct TCP_Server_Info *server;
350 unsigned int sec_flags;
351 u16 i;
352 u16 temp = 0;
353 int blob_offset, blob_length;
354 char *security_blob;
355 int flags = CIFS_NEG_OP;
356
357 cFYI(1, "Negotiate protocol");
358
359 if (ses->server)
360 server = ses->server;
361 else {
362 rc = -EIO;
363 return rc;
364 }
365
366 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
367 if (rc)
368 return rc;
369
370 /* if any of auth flags (ie not sign or seal) are overriden use them */
371 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
372 sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
373 else /* if override flags set only sign/seal OR them with global auth */
374 sec_flags = global_secflags | ses->overrideSecFlg;
375
376 cFYI(1, "sec_flags 0x%x", sec_flags);
377
378 req->hdr.SessionId = 0;
379
380 for (i = 0; i < SMB2_NUM_PROT; i++)
381 req->Dialects[i] = smb2protocols[i].name;
382
383 req->DialectCount = cpu_to_le16(i);
384 inc_rfc1001_len(req, i * 2);
385
386 /* only one of SMB2 signing flags may be set in SMB2 request */
387 if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
388 temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
389 else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
390 temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
391
392 req->SecurityMode = cpu_to_le16(temp);
393
394 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
395
b8c32dbb
PS
396 memcpy(req->ClientGUID, cifs_client_guid, SMB2_CLIENT_GUID_SIZE);
397
ec2e4523
PS
398 iov[0].iov_base = (char *)req;
399 /* 4 for rfc1002 length field */
400 iov[0].iov_len = get_rfc1002_length(req) + 4;
401
402 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
403
404 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
405 /*
406 * No tcon so can't do
407 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
408 */
409 if (rc != 0)
410 goto neg_exit;
411
ec2e4523
PS
412 cFYI(1, "mode 0x%x", rsp->SecurityMode);
413
414 if (rsp->DialectRevision == smb2protocols[SMB21_PROT].name)
415 cFYI(1, "negotiated smb2.1 dialect");
416 else if (rsp->DialectRevision == smb2protocols[SMB2_PROT].name)
417 cFYI(1, "negotiated smb2 dialect");
418 else {
419 cERROR(1, "Illegal dialect returned by server %d",
420 le16_to_cpu(rsp->DialectRevision));
421 rc = -EIO;
422 goto neg_exit;
423 }
424 server->dialect = le16_to_cpu(rsp->DialectRevision);
425
426 server->maxBuf = le32_to_cpu(rsp->MaxTransactSize);
427 server->max_read = le32_to_cpu(rsp->MaxReadSize);
428 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
429 /* BB Do we need to validate the SecurityMode? */
430 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
431 server->capabilities = le32_to_cpu(rsp->Capabilities);
29e20f9c
PS
432 /* Internal types */
433 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
ec2e4523
PS
434
435 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
436 &rsp->hdr);
437 if (blob_length == 0) {
438 cERROR(1, "missing security blob on negprot");
439 rc = -EIO;
440 goto neg_exit;
441 }
3c1bf7e4
PS
442
443 cFYI(1, "sec_flags 0x%x", sec_flags);
444 if (sec_flags & CIFSSEC_MUST_SIGN) {
445 cFYI(1, "Signing required");
446 if (!(server->sec_mode & (SMB2_NEGOTIATE_SIGNING_REQUIRED |
447 SMB2_NEGOTIATE_SIGNING_ENABLED))) {
448 cERROR(1, "signing required but server lacks support");
449 rc = -EOPNOTSUPP;
450 goto neg_exit;
451 }
452 server->sec_mode |= SECMODE_SIGN_REQUIRED;
453 } else if (sec_flags & CIFSSEC_MAY_SIGN) {
454 cFYI(1, "Signing optional");
455 if (server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
456 cFYI(1, "Server requires signing");
457 server->sec_mode |= SECMODE_SIGN_REQUIRED;
458 } else {
459 server->sec_mode &=
460 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
461 }
462 } else {
463 cFYI(1, "Signing disabled");
464 if (server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
465 cERROR(1, "Server requires packet signing to be enabled"
466 " in /proc/fs/cifs/SecurityFlags.");
467 rc = -EOPNOTSUPP;
468 goto neg_exit;
469 }
470 server->sec_mode &=
471 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
472 }
473
ec2e4523
PS
474#ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
475 rc = decode_neg_token_init(security_blob, blob_length,
476 &server->sec_type);
477 if (rc == 1)
478 rc = 0;
479 else if (rc == 0) {
480 rc = -EIO;
481 goto neg_exit;
482 }
483#endif
484
485neg_exit:
486 free_rsp_buf(resp_buftype, rsp);
487 return rc;
488}
5478f9ba
PS
489
490int
491SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
492 const struct nls_table *nls_cp)
493{
494 struct smb2_sess_setup_req *req;
495 struct smb2_sess_setup_rsp *rsp = NULL;
496 struct kvec iov[2];
497 int rc = 0;
498 int resp_buftype;
499 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
500 struct TCP_Server_Info *server;
501 unsigned int sec_flags;
502 u8 temp = 0;
503 u16 blob_length = 0;
504 char *security_blob;
505 char *ntlmssp_blob = NULL;
506 bool use_spnego = false; /* else use raw ntlmssp */
507
508 cFYI(1, "Session Setup");
509
510 if (ses->server)
511 server = ses->server;
512 else {
513 rc = -EIO;
514 return rc;
515 }
516
517 /*
518 * If memory allocation is successful, caller of this function
519 * frees it.
520 */
521 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
522 if (!ses->ntlmssp)
523 return -ENOMEM;
524
525 ses->server->secType = RawNTLMSSP;
526
527ssetup_ntlmssp_authenticate:
528 if (phase == NtLmChallenge)
529 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
530
531 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
532 if (rc)
533 return rc;
534
535 /* if any of auth flags (ie not sign or seal) are overriden use them */
536 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
537 sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
538 else /* if override flags set only sign/seal OR them with global auth */
539 sec_flags = global_secflags | ses->overrideSecFlg;
540
541 cFYI(1, "sec_flags 0x%x", sec_flags);
542
543 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
544 req->VcNumber = 0; /* MBZ */
545 /* to enable echos and oplocks */
546 req->hdr.CreditRequest = cpu_to_le16(3);
547
548 /* only one of SMB2 signing flags may be set in SMB2 request */
549 if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
550 temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
551 else if (ses->server->sec_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED)
552 temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
553 else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
554 temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
555
556 req->SecurityMode = temp;
557 req->Capabilities = 0;
558 req->Channel = 0; /* MBZ */
559
560 iov[0].iov_base = (char *)req;
561 /* 4 for rfc1002 length field and 1 for pad */
562 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
563 if (phase == NtLmNegotiate) {
564 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
565 GFP_KERNEL);
566 if (ntlmssp_blob == NULL) {
567 rc = -ENOMEM;
568 goto ssetup_exit;
569 }
570 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
571 if (use_spnego) {
572 /* blob_length = build_spnego_ntlmssp_blob(
573 &security_blob,
574 sizeof(struct _NEGOTIATE_MESSAGE),
575 ntlmssp_blob); */
576 /* BB eventually need to add this */
577 cERROR(1, "spnego not supported for SMB2 yet");
578 rc = -EOPNOTSUPP;
579 kfree(ntlmssp_blob);
580 goto ssetup_exit;
581 } else {
582 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
583 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
584 security_blob = ntlmssp_blob;
585 }
586 } else if (phase == NtLmAuthenticate) {
587 req->hdr.SessionId = ses->Suid;
588 ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
589 GFP_KERNEL);
590 if (ntlmssp_blob == NULL) {
591 cERROR(1, "failed to malloc ntlmssp blob");
592 rc = -ENOMEM;
593 goto ssetup_exit;
594 }
595 rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
596 nls_cp);
597 if (rc) {
598 cFYI(1, "build_ntlmssp_auth_blob failed %d", rc);
599 goto ssetup_exit; /* BB double check error handling */
600 }
601 if (use_spnego) {
602 /* blob_length = build_spnego_ntlmssp_blob(
603 &security_blob,
604 blob_length,
605 ntlmssp_blob); */
606 cERROR(1, "spnego not supported for SMB2 yet");
607 rc = -EOPNOTSUPP;
608 kfree(ntlmssp_blob);
609 goto ssetup_exit;
610 } else {
611 security_blob = ntlmssp_blob;
612 }
613 } else {
614 cERROR(1, "illegal ntlmssp phase");
615 rc = -EIO;
616 goto ssetup_exit;
617 }
618
619 /* Testing shows that buffer offset must be at location of Buffer[0] */
620 req->SecurityBufferOffset =
621 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
622 1 /* pad */ - 4 /* rfc1001 len */);
623 req->SecurityBufferLength = cpu_to_le16(blob_length);
624 iov[1].iov_base = security_blob;
625 iov[1].iov_len = blob_length;
626
627 inc_rfc1001_len(req, blob_length - 1 /* pad */);
628
629 /* BB add code to build os and lm fields */
630
631 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, CIFS_LOG_ERROR);
632
633 kfree(security_blob);
634 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
4ca3a99c
PS
635 if (resp_buftype != CIFS_NO_BUFFER &&
636 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
5478f9ba
PS
637 if (phase != NtLmNegotiate) {
638 cERROR(1, "Unexpected more processing error");
639 goto ssetup_exit;
640 }
641 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
4ca3a99c 642 le16_to_cpu(rsp->SecurityBufferOffset)) {
5478f9ba
PS
643 cERROR(1, "Invalid security buffer offset %d",
644 le16_to_cpu(rsp->SecurityBufferOffset));
645 rc = -EIO;
646 goto ssetup_exit;
647 }
648
649 /* NTLMSSP Negotiate sent now processing challenge (response) */
650 phase = NtLmChallenge; /* process ntlmssp challenge */
651 rc = 0; /* MORE_PROCESSING is not an error here but expected */
652 ses->Suid = rsp->hdr.SessionId;
653 rc = decode_ntlmssp_challenge(rsp->Buffer,
654 le16_to_cpu(rsp->SecurityBufferLength), ses);
655 }
656
657 /*
658 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
659 * but at least the raw NTLMSSP case works.
660 */
661 /*
662 * No tcon so can't do
663 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
664 */
665 if (rc != 0)
666 goto ssetup_exit;
667
5478f9ba
PS
668 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
669ssetup_exit:
670 free_rsp_buf(resp_buftype, rsp);
671
672 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
673 if ((phase == NtLmChallenge) && (rc == 0))
674 goto ssetup_ntlmssp_authenticate;
675 return rc;
676}
677
678int
679SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
680{
681 struct smb2_logoff_req *req; /* response is also trivial struct */
682 int rc = 0;
683 struct TCP_Server_Info *server;
684
685 cFYI(1, "disconnect session %p", ses);
686
687 if (ses && (ses->server))
688 server = ses->server;
689 else
690 return -EIO;
691
692 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
693 if (rc)
694 return rc;
695
696 /* since no tcon, smb2_init can not do this, so do here */
697 req->hdr.SessionId = ses->Suid;
3c1bf7e4
PS
698 if (server->sec_mode & SECMODE_SIGN_REQUIRED)
699 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
5478f9ba
PS
700
701 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
702 /*
703 * No tcon so can't do
704 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
705 */
706 return rc;
707}
faaf946a
PS
708
709static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
710{
d60622eb 711 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
faaf946a
PS
712}
713
714#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
715
716int
717SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
718 struct cifs_tcon *tcon, const struct nls_table *cp)
719{
720 struct smb2_tree_connect_req *req;
721 struct smb2_tree_connect_rsp *rsp = NULL;
722 struct kvec iov[2];
723 int rc = 0;
724 int resp_buftype;
725 int unc_path_len;
726 struct TCP_Server_Info *server;
727 __le16 *unc_path = NULL;
728
729 cFYI(1, "TCON");
730
731 if ((ses->server) && tree)
732 server = ses->server;
733 else
734 return -EIO;
735
736 if (tcon && tcon->bad_network_name)
737 return -ENOENT;
738
739 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
740 if (unc_path == NULL)
741 return -ENOMEM;
742
743 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
744 unc_path_len *= 2;
745 if (unc_path_len < 2) {
746 kfree(unc_path);
747 return -EINVAL;
748 }
749
750 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
751 if (rc) {
752 kfree(unc_path);
753 return rc;
754 }
755
756 if (tcon == NULL) {
757 /* since no tcon, smb2_init can not do this, so do here */
758 req->hdr.SessionId = ses->Suid;
759 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
760 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
761 }
762
763 iov[0].iov_base = (char *)req;
764 /* 4 for rfc1002 length field and 1 for pad */
765 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
766
767 /* Testing shows that buffer offset must be at location of Buffer[0] */
768 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
769 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
770 req->PathLength = cpu_to_le16(unc_path_len - 2);
771 iov[1].iov_base = unc_path;
772 iov[1].iov_len = unc_path_len;
773
774 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
775
776 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
777 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
778
779 if (rc != 0) {
780 if (tcon) {
781 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
782 tcon->need_reconnect = true;
783 }
784 goto tcon_error_exit;
785 }
786
faaf946a
PS
787 if (tcon == NULL) {
788 ses->ipc_tid = rsp->hdr.TreeId;
789 goto tcon_exit;
790 }
791
792 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
793 cFYI(1, "connection to disk share");
794 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
795 tcon->ipc = true;
796 cFYI(1, "connection to pipe share");
797 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
798 tcon->print = true;
799 cFYI(1, "connection to printer");
800 } else {
801 cERROR(1, "unknown share type %d", rsp->ShareType);
802 rc = -EOPNOTSUPP;
803 goto tcon_error_exit;
804 }
805
806 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
807 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
808 tcon->tidStatus = CifsGood;
809 tcon->need_reconnect = false;
810 tcon->tid = rsp->hdr.TreeId;
811 strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
812
813 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
814 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
815 cERROR(1, "DFS capability contradicts DFS flag");
816
817tcon_exit:
818 free_rsp_buf(resp_buftype, rsp);
819 kfree(unc_path);
820 return rc;
821
822tcon_error_exit:
823 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
824 cERROR(1, "BAD_NETWORK_NAME: %s", tree);
825 tcon->bad_network_name = true;
826 }
827 goto tcon_exit;
828}
829
830int
831SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
832{
833 struct smb2_tree_disconnect_req *req; /* response is trivial */
834 int rc = 0;
835 struct TCP_Server_Info *server;
836 struct cifs_ses *ses = tcon->ses;
837
838 cFYI(1, "Tree Disconnect");
839
840 if (ses && (ses->server))
841 server = ses->server;
842 else
843 return -EIO;
844
845 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
846 return 0;
847
848 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
849 if (rc)
850 return rc;
851
852 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
853 if (rc)
854 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
855
856 return rc;
857}
2503a0db 858
b8c32dbb
PS
859static struct create_lease *
860create_lease_buf(u8 *lease_key, u8 oplock)
861{
862 struct create_lease *buf;
863
864 buf = kmalloc(sizeof(struct create_lease), GFP_KERNEL);
865 if (!buf)
866 return NULL;
867
868 memset(buf, 0, sizeof(struct create_lease));
869
870 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
871 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
872 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
873 buf->lcontext.LeaseState = SMB2_LEASE_WRITE_CACHING |
874 SMB2_LEASE_READ_CACHING;
875 else if (oplock == SMB2_OPLOCK_LEVEL_II)
876 buf->lcontext.LeaseState = SMB2_LEASE_READ_CACHING;
877 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
878 buf->lcontext.LeaseState = SMB2_LEASE_HANDLE_CACHING |
879 SMB2_LEASE_READ_CACHING |
880 SMB2_LEASE_WRITE_CACHING;
881
882 buf->ccontext.DataOffset = cpu_to_le16(offsetof
883 (struct create_lease, lcontext));
884 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
885 buf->ccontext.NameOffset = cpu_to_le16(offsetof
886 (struct create_lease, Name));
887 buf->ccontext.NameLength = cpu_to_le16(4);
888 buf->Name[0] = 'R';
889 buf->Name[1] = 'q';
890 buf->Name[2] = 'L';
891 buf->Name[3] = 's';
892 return buf;
893}
894
895static __u8
896parse_lease_state(struct smb2_create_rsp *rsp)
897{
898 char *data_offset;
899 struct create_lease *lc;
b8c32dbb
PS
900 bool found = false;
901
902 data_offset = (char *)rsp;
903 data_offset += 4 + le32_to_cpu(rsp->CreateContextsOffset);
904 lc = (struct create_lease *)data_offset;
905 do {
906 char *name = le16_to_cpu(lc->ccontext.NameOffset) + (char *)lc;
907 if (le16_to_cpu(lc->ccontext.NameLength) != 4 ||
908 strncmp(name, "RqLs", 4)) {
909 lc = (struct create_lease *)((char *)lc
910 + le32_to_cpu(lc->ccontext.Next));
911 continue;
912 }
913 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
914 return SMB2_OPLOCK_LEVEL_NOCHANGE;
915 found = true;
916 break;
917 } while (le32_to_cpu(lc->ccontext.Next) != 0);
918
919 if (!found)
0822f514 920 return 0;
b8c32dbb 921
0822f514 922 return smb2_map_lease_to_oplock(lc->lcontext.LeaseState);
b8c32dbb
PS
923}
924
2503a0db
PS
925int
926SMB2_open(const unsigned int xid, struct cifs_tcon *tcon, __le16 *path,
927 u64 *persistent_fid, u64 *volatile_fid, __u32 desired_access,
f0df737e 928 __u32 create_disposition, __u32 file_attributes, __u32 create_options,
2e44b288 929 __u8 *oplock, struct smb2_file_all_info *buf)
2503a0db
PS
930{
931 struct smb2_create_req *req;
932 struct smb2_create_rsp *rsp;
933 struct TCP_Server_Info *server;
934 struct cifs_ses *ses = tcon->ses;
b8c32dbb 935 struct kvec iov[3];
2503a0db
PS
936 int resp_buftype;
937 int uni_path_len;
b8c32dbb
PS
938 __le16 *copy_path = NULL;
939 int copy_size;
2503a0db
PS
940 int rc = 0;
941 int num_iovecs = 2;
942
943 cFYI(1, "create/open");
944
945 if (ses && (ses->server))
946 server = ses->server;
947 else
948 return -EIO;
949
950 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
951 if (rc)
952 return rc;
953
2503a0db
PS
954 req->ImpersonationLevel = IL_IMPERSONATION;
955 req->DesiredAccess = cpu_to_le32(desired_access);
956 /* File attributes ignored on open (used in create though) */
957 req->FileAttributes = cpu_to_le32(file_attributes);
958 req->ShareAccess = FILE_SHARE_ALL_LE;
959 req->CreateDisposition = cpu_to_le32(create_disposition);
960 req->CreateOptions = cpu_to_le32(create_options);
961 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
962 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req)
b8c32dbb 963 - 8 /* pad */ - 4 /* do not count rfc1001 len field */);
2503a0db
PS
964
965 iov[0].iov_base = (char *)req;
966 /* 4 for rfc1002 length field */
967 iov[0].iov_len = get_rfc1002_length(req) + 4;
968
969 /* MUST set path len (NameLength) to 0 opening root of share */
970 if (uni_path_len >= 4) {
971 req->NameLength = cpu_to_le16(uni_path_len - 2);
972 /* -1 since last byte is buf[0] which is sent below (path) */
973 iov[0].iov_len--;
b8c32dbb
PS
974 if (uni_path_len % 8 != 0) {
975 copy_size = uni_path_len / 8 * 8;
976 if (copy_size < uni_path_len)
977 copy_size += 8;
978
979 copy_path = kzalloc(copy_size, GFP_KERNEL);
980 if (!copy_path)
981 return -ENOMEM;
982 memcpy((char *)copy_path, (const char *)path,
983 uni_path_len);
984 uni_path_len = copy_size;
985 path = copy_path;
986 }
987
2503a0db
PS
988 iov[1].iov_len = uni_path_len;
989 iov[1].iov_base = path;
990 /*
991 * -1 since last byte is buf[0] which was counted in
992 * smb2_buf_len.
993 */
994 inc_rfc1001_len(req, uni_path_len - 1);
995 } else {
b8c32dbb
PS
996 iov[0].iov_len += 7;
997 req->hdr.smb2_buf_length = cpu_to_be32(be32_to_cpu(
998 req->hdr.smb2_buf_length) + 8 - 1);
2503a0db
PS
999 num_iovecs = 1;
1000 req->NameLength = 0;
1001 }
1002
b8c32dbb
PS
1003 if (!server->oplocks)
1004 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1005
1006 if (!(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1007 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1008 req->RequestedOplockLevel = *oplock;
1009 else {
1010 iov[num_iovecs].iov_base = create_lease_buf(oplock+1, *oplock);
1011 if (iov[num_iovecs].iov_base == NULL) {
1012 cifs_small_buf_release(req);
1013 kfree(copy_path);
1014 return -ENOMEM;
1015 }
1016 iov[num_iovecs].iov_len = sizeof(struct create_lease);
1017 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1018 req->CreateContextsOffset = cpu_to_le32(
1019 sizeof(struct smb2_create_req) - 4 - 8 +
1020 iov[num_iovecs-1].iov_len);
1021 req->CreateContextsLength = cpu_to_le32(
1022 sizeof(struct create_lease));
1023 inc_rfc1001_len(&req->hdr, sizeof(struct create_lease));
1024 num_iovecs++;
1025 }
1026
2503a0db
PS
1027 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1028 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1029
1030 if (rc != 0) {
1031 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1032 goto creat_exit;
1033 }
1034
2503a0db
PS
1035 *persistent_fid = rsp->PersistentFileId;
1036 *volatile_fid = rsp->VolatileFileId;
f0df737e
PS
1037
1038 if (buf) {
1039 memcpy(buf, &rsp->CreationTime, 32);
1040 buf->AllocationSize = rsp->AllocationSize;
1041 buf->EndOfFile = rsp->EndofFile;
1042 buf->Attributes = rsp->FileAttributes;
1043 buf->NumberOfLinks = cpu_to_le32(1);
1044 buf->DeletePending = 0;
1045 }
2e44b288 1046
b8c32dbb
PS
1047 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1048 *oplock = parse_lease_state(rsp);
1049 else
1050 *oplock = rsp->OplockLevel;
2503a0db 1051creat_exit:
b8c32dbb 1052 kfree(copy_path);
2503a0db
PS
1053 free_rsp_buf(resp_buftype, rsp);
1054 return rc;
1055}
1056
1057int
1058SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1059 u64 persistent_fid, u64 volatile_fid)
1060{
1061 struct smb2_close_req *req;
1062 struct smb2_close_rsp *rsp;
1063 struct TCP_Server_Info *server;
1064 struct cifs_ses *ses = tcon->ses;
1065 struct kvec iov[1];
1066 int resp_buftype;
1067 int rc = 0;
1068
1069 cFYI(1, "Close");
1070
1071 if (ses && (ses->server))
1072 server = ses->server;
1073 else
1074 return -EIO;
1075
1076 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1077 if (rc)
1078 return rc;
1079
1080 req->PersistentFileId = persistent_fid;
1081 req->VolatileFileId = volatile_fid;
1082
1083 iov[0].iov_base = (char *)req;
1084 /* 4 for rfc1002 length field */
1085 iov[0].iov_len = get_rfc1002_length(req) + 4;
1086
1087 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1088 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1089
1090 if (rc != 0) {
1091 if (tcon)
1092 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1093 goto close_exit;
1094 }
1095
2503a0db
PS
1096 /* BB FIXME - decode close response, update inode for caching */
1097
1098close_exit:
1099 free_rsp_buf(resp_buftype, rsp);
1100 return rc;
1101}
be4cb9e3
PS
1102
1103static int
1104validate_buf(unsigned int offset, unsigned int buffer_length,
1105 struct smb2_hdr *hdr, unsigned int min_buf_size)
1106
1107{
1108 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1109 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1110 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1111 char *end_of_buf = begin_of_buf + buffer_length;
1112
1113
1114 if (buffer_length < min_buf_size) {
1115 cERROR(1, "buffer length %d smaller than minimum size %d",
1116 buffer_length, min_buf_size);
1117 return -EINVAL;
1118 }
1119
1120 /* check if beyond RFC1001 maximum length */
1121 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1122 cERROR(1, "buffer length %d or smb length %d too large",
1123 buffer_length, smb_len);
1124 return -EINVAL;
1125 }
1126
1127 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1128 cERROR(1, "illegal server response, bad offset to data");
1129 return -EINVAL;
1130 }
1131
1132 return 0;
1133}
1134
1135/*
1136 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1137 * Caller must free buffer.
1138 */
1139static int
1140validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1141 struct smb2_hdr *hdr, unsigned int minbufsize,
1142 char *data)
1143
1144{
1145 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1146 int rc;
1147
1148 if (!data)
1149 return -EINVAL;
1150
1151 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1152 if (rc)
1153 return rc;
1154
1155 memcpy(data, begin_of_buf, buffer_length);
1156
1157 return 0;
1158}
1159
f0df737e
PS
1160static int
1161query_info(const unsigned int xid, struct cifs_tcon *tcon,
1162 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1163 size_t output_len, size_t min_len, void *data)
be4cb9e3
PS
1164{
1165 struct smb2_query_info_req *req;
1166 struct smb2_query_info_rsp *rsp = NULL;
1167 struct kvec iov[2];
1168 int rc = 0;
1169 int resp_buftype;
1170 struct TCP_Server_Info *server;
1171 struct cifs_ses *ses = tcon->ses;
1172
1173 cFYI(1, "Query Info");
1174
1175 if (ses && (ses->server))
1176 server = ses->server;
1177 else
1178 return -EIO;
1179
1180 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1181 if (rc)
1182 return rc;
1183
1184 req->InfoType = SMB2_O_INFO_FILE;
f0df737e 1185 req->FileInfoClass = info_class;
be4cb9e3
PS
1186 req->PersistentFileId = persistent_fid;
1187 req->VolatileFileId = volatile_fid;
1188 /* 4 for rfc1002 length field and 1 for Buffer */
1189 req->InputBufferOffset =
1190 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
f0df737e 1191 req->OutputBufferLength = cpu_to_le32(output_len);
be4cb9e3
PS
1192
1193 iov[0].iov_base = (char *)req;
1194 /* 4 for rfc1002 length field */
1195 iov[0].iov_len = get_rfc1002_length(req) + 4;
1196
1197 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
e5d04887
PS
1198 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1199
be4cb9e3
PS
1200 if (rc) {
1201 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1202 goto qinf_exit;
1203 }
1204
be4cb9e3
PS
1205 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1206 le32_to_cpu(rsp->OutputBufferLength),
f0df737e 1207 &rsp->hdr, min_len, data);
be4cb9e3
PS
1208
1209qinf_exit:
1210 free_rsp_buf(resp_buftype, rsp);
1211 return rc;
1212}
9094fad1 1213
f0df737e
PS
1214int
1215SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1216 u64 persistent_fid, u64 volatile_fid,
1217 struct smb2_file_all_info *data)
1218{
1219 return query_info(xid, tcon, persistent_fid, volatile_fid,
1220 FILE_ALL_INFORMATION,
1221 sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
1222 sizeof(struct smb2_file_all_info), data);
1223}
1224
1225int
1226SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1227 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1228{
1229 return query_info(xid, tcon, persistent_fid, volatile_fid,
1230 FILE_INTERNAL_INFORMATION,
1231 sizeof(struct smb2_file_internal_info),
1232 sizeof(struct smb2_file_internal_info), uniqueid);
1233}
1234
9094fad1
PS
1235/*
1236 * This is a no-op for now. We're not really interested in the reply, but
1237 * rather in the fact that the server sent one and that server->lstrp
1238 * gets updated.
1239 *
1240 * FIXME: maybe we should consider checking that the reply matches request?
1241 */
1242static void
1243smb2_echo_callback(struct mid_q_entry *mid)
1244{
1245 struct TCP_Server_Info *server = mid->callback_data;
1246 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1247 unsigned int credits_received = 1;
1248
1249 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1250 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1251
1252 DeleteMidQEntry(mid);
1253 add_credits(server, credits_received, CIFS_ECHO_OP);
1254}
1255
1256int
1257SMB2_echo(struct TCP_Server_Info *server)
1258{
1259 struct smb2_echo_req *req;
1260 int rc = 0;
1261 struct kvec iov;
fec344e3
JL
1262 struct smb_rqst rqst = { .rq_iov = &iov,
1263 .rq_nvec = 1 };
9094fad1
PS
1264
1265 cFYI(1, "In echo request");
1266
1267 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1268 if (rc)
1269 return rc;
1270
1271 req->hdr.CreditRequest = cpu_to_le16(1);
1272
1273 iov.iov_base = (char *)req;
1274 /* 4 for rfc1002 length field */
1275 iov.iov_len = get_rfc1002_length(req) + 4;
1276
fec344e3 1277 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
9094fad1
PS
1278 CIFS_ECHO_OP);
1279 if (rc)
1280 cFYI(1, "Echo request failed: %d", rc);
1281
1282 cifs_small_buf_release(req);
1283 return rc;
1284}
7a5cfb19
PS
1285
1286int
1287SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1288 u64 volatile_fid)
1289{
1290 struct smb2_flush_req *req;
1291 struct TCP_Server_Info *server;
1292 struct cifs_ses *ses = tcon->ses;
1293 struct kvec iov[1];
1294 int resp_buftype;
1295 int rc = 0;
1296
1297 cFYI(1, "Flush");
1298
1299 if (ses && (ses->server))
1300 server = ses->server;
1301 else
1302 return -EIO;
1303
1304 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1305 if (rc)
1306 return rc;
1307
1308 req->PersistentFileId = persistent_fid;
1309 req->VolatileFileId = volatile_fid;
1310
1311 iov[0].iov_base = (char *)req;
1312 /* 4 for rfc1002 length field */
1313 iov[0].iov_len = get_rfc1002_length(req) + 4;
1314
1315 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1316
1317 if ((rc != 0) && tcon)
1318 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1319
1320 free_rsp_buf(resp_buftype, iov[0].iov_base);
1321 return rc;
1322}
09a4707e
PS
1323
1324/*
1325 * To form a chain of read requests, any read requests after the first should
1326 * have the end_of_chain boolean set to true.
1327 */
1328static int
1329smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1330 unsigned int remaining_bytes, int request_type)
1331{
1332 int rc = -EACCES;
1333 struct smb2_read_req *req = NULL;
1334
1335 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1336 if (rc)
1337 return rc;
1338 if (io_parms->tcon->ses->server == NULL)
1339 return -ECONNABORTED;
1340
1341 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1342
1343 req->PersistentFileId = io_parms->persistent_fid;
1344 req->VolatileFileId = io_parms->volatile_fid;
1345 req->ReadChannelInfoOffset = 0; /* reserved */
1346 req->ReadChannelInfoLength = 0; /* reserved */
1347 req->Channel = 0; /* reserved */
1348 req->MinimumCount = 0;
1349 req->Length = cpu_to_le32(io_parms->length);
1350 req->Offset = cpu_to_le64(io_parms->offset);
1351
1352 if (request_type & CHAINED_REQUEST) {
1353 if (!(request_type & END_OF_CHAIN)) {
1354 /* 4 for rfc1002 length field */
1355 req->hdr.NextCommand =
1356 cpu_to_le32(get_rfc1002_length(req) + 4);
1357 } else /* END_OF_CHAIN */
1358 req->hdr.NextCommand = 0;
1359 if (request_type & RELATED_REQUEST) {
1360 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1361 /*
1362 * Related requests use info from previous read request
1363 * in chain.
1364 */
1365 req->hdr.SessionId = 0xFFFFFFFF;
1366 req->hdr.TreeId = 0xFFFFFFFF;
1367 req->PersistentFileId = 0xFFFFFFFF;
1368 req->VolatileFileId = 0xFFFFFFFF;
1369 }
1370 }
1371 if (remaining_bytes > io_parms->length)
1372 req->RemainingBytes = cpu_to_le32(remaining_bytes);
1373 else
1374 req->RemainingBytes = 0;
1375
1376 iov[0].iov_base = (char *)req;
1377 /* 4 for rfc1002 length field */
1378 iov[0].iov_len = get_rfc1002_length(req) + 4;
1379 return rc;
1380}
1381
1382static void
1383smb2_readv_callback(struct mid_q_entry *mid)
1384{
1385 struct cifs_readdata *rdata = mid->callback_data;
1386 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1387 struct TCP_Server_Info *server = tcon->ses->server;
5819575e 1388 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
09a4707e 1389 unsigned int credits_received = 1;
5819575e 1390 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
8321fec4
JL
1391 .rq_nvec = 1,
1392 .rq_pages = rdata->pages,
1393 .rq_npages = rdata->nr_pages,
1394 .rq_pagesz = rdata->pagesz,
1395 .rq_tailsz = rdata->tailsz };
09a4707e
PS
1396
1397 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
1398 mid->mid, mid->mid_state, rdata->result, rdata->bytes);
1399
1400 switch (mid->mid_state) {
1401 case MID_RESPONSE_RECEIVED:
1402 credits_received = le16_to_cpu(buf->CreditRequest);
1403 /* result already set, check signature */
3c1bf7e4
PS
1404 if (server->sec_mode &
1405 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1406 int rc;
1407
0b688cfc 1408 rc = smb2_verify_signature(&rqst, server);
3c1bf7e4
PS
1409 if (rc)
1410 cERROR(1, "SMB signature verification returned "
1411 "error = %d", rc);
1412 }
09a4707e
PS
1413 /* FIXME: should this be counted toward the initiating task? */
1414 task_io_account_read(rdata->bytes);
1415 cifs_stats_bytes_read(tcon, rdata->bytes);
1416 break;
1417 case MID_REQUEST_SUBMITTED:
1418 case MID_RETRY_NEEDED:
1419 rdata->result = -EAGAIN;
1420 break;
1421 default:
1422 if (rdata->result != -ENODATA)
1423 rdata->result = -EIO;
1424 }
1425
1426 if (rdata->result)
1427 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
1428
1429 queue_work(cifsiod_wq, &rdata->work);
1430 DeleteMidQEntry(mid);
1431 add_credits(server, credits_received, 0);
1432}
1433
1434/* smb2_async_readv - send an async write, and set up mid to handle result */
1435int
1436smb2_async_readv(struct cifs_readdata *rdata)
1437{
1438 int rc;
1439 struct smb2_hdr *buf;
1440 struct cifs_io_parms io_parms;
5819575e 1441 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
fec344e3 1442 .rq_nvec = 1 };
09a4707e
PS
1443
1444 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1445 rdata->offset, rdata->bytes);
1446
1447 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
1448 io_parms.offset = rdata->offset;
1449 io_parms.length = rdata->bytes;
1450 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
1451 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
1452 io_parms.pid = rdata->pid;
5819575e 1453 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
09a4707e
PS
1454 if (rc)
1455 return rc;
1456
5819575e 1457 buf = (struct smb2_hdr *)rdata->iov.iov_base;
09a4707e 1458 /* 4 for rfc1002 length field */
5819575e 1459 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
09a4707e
PS
1460
1461 kref_get(&rdata->refcount);
fec344e3 1462 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
09a4707e
PS
1463 cifs_readv_receive, smb2_readv_callback,
1464 rdata, 0);
e5d04887 1465 if (rc) {
09a4707e 1466 kref_put(&rdata->refcount, cifs_readdata_release);
e5d04887
PS
1467 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
1468 }
09a4707e
PS
1469
1470 cifs_small_buf_release(buf);
1471 return rc;
1472}
33319141 1473
d8e05039
PS
1474int
1475SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
1476 unsigned int *nbytes, char **buf, int *buf_type)
1477{
1478 int resp_buftype, rc = -EACCES;
1479 struct smb2_read_rsp *rsp = NULL;
1480 struct kvec iov[1];
1481
1482 *nbytes = 0;
1483 rc = smb2_new_read_req(iov, io_parms, 0, 0);
1484 if (rc)
1485 return rc;
1486
1487 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
1488 &resp_buftype, CIFS_LOG_ERROR);
1489
1490 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
1491
1492 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
1493 free_rsp_buf(resp_buftype, iov[0].iov_base);
1494 return 0;
1495 }
1496
1497 if (rc) {
1498 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
1499 cERROR(1, "Send error in read = %d", rc);
1500 } else {
1501 *nbytes = le32_to_cpu(rsp->DataLength);
1502 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
1503 (*nbytes > io_parms->length)) {
1504 cFYI(1, "bad length %d for count %d", *nbytes,
1505 io_parms->length);
1506 rc = -EIO;
1507 *nbytes = 0;
1508 }
1509 }
1510
1511 if (*buf) {
1512 memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
1513 *nbytes);
1514 free_rsp_buf(resp_buftype, iov[0].iov_base);
1515 } else if (resp_buftype != CIFS_NO_BUFFER) {
1516 *buf = iov[0].iov_base;
1517 if (resp_buftype == CIFS_SMALL_BUFFER)
1518 *buf_type = CIFS_SMALL_BUFFER;
1519 else if (resp_buftype == CIFS_LARGE_BUFFER)
1520 *buf_type = CIFS_LARGE_BUFFER;
1521 }
1522 return rc;
1523}
1524
33319141
PS
1525/*
1526 * Check the mid_state and signature on received buffer (if any), and queue the
1527 * workqueue completion task.
1528 */
1529static void
1530smb2_writev_callback(struct mid_q_entry *mid)
1531{
1532 struct cifs_writedata *wdata = mid->callback_data;
1533 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1534 unsigned int written;
1535 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
1536 unsigned int credits_received = 1;
1537
1538 switch (mid->mid_state) {
1539 case MID_RESPONSE_RECEIVED:
1540 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
1541 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
1542 if (wdata->result != 0)
1543 break;
1544
1545 written = le32_to_cpu(rsp->DataLength);
1546 /*
1547 * Mask off high 16 bits when bytes written as returned
1548 * by the server is greater than bytes requested by the
1549 * client. OS/2 servers are known to set incorrect
1550 * CountHigh values.
1551 */
1552 if (written > wdata->bytes)
1553 written &= 0xFFFF;
1554
1555 if (written < wdata->bytes)
1556 wdata->result = -ENOSPC;
1557 else
1558 wdata->bytes = written;
1559 break;
1560 case MID_REQUEST_SUBMITTED:
1561 case MID_RETRY_NEEDED:
1562 wdata->result = -EAGAIN;
1563 break;
1564 default:
1565 wdata->result = -EIO;
1566 break;
1567 }
1568
1569 if (wdata->result)
1570 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
1571
1572 queue_work(cifsiod_wq, &wdata->work);
1573 DeleteMidQEntry(mid);
1574 add_credits(tcon->ses->server, credits_received, 0);
1575}
1576
1577/* smb2_async_writev - send an async write, and set up mid to handle result */
1578int
1579smb2_async_writev(struct cifs_writedata *wdata)
1580{
eddb079d 1581 int rc = -EACCES;
33319141
PS
1582 struct smb2_write_req *req = NULL;
1583 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
eddb079d 1584 struct kvec iov;
fec344e3 1585 struct smb_rqst rqst;
33319141
PS
1586
1587 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
1588 if (rc)
1589 goto async_writev_out;
1590
33319141
PS
1591 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
1592
1593 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
1594 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
1595 req->WriteChannelInfoOffset = 0;
1596 req->WriteChannelInfoLength = 0;
1597 req->Channel = 0;
1598 req->Offset = cpu_to_le64(wdata->offset);
1599 /* 4 for rfc1002 length field */
1600 req->DataOffset = cpu_to_le16(
1601 offsetof(struct smb2_write_req, Buffer) - 4);
1602 req->RemainingBytes = 0;
1603
1604 /* 4 for rfc1002 length field and 1 for Buffer */
eddb079d
JL
1605 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
1606 iov.iov_base = req;
33319141 1607
eddb079d
JL
1608 rqst.rq_iov = &iov;
1609 rqst.rq_nvec = 1;
1610 rqst.rq_pages = wdata->pages;
1611 rqst.rq_npages = wdata->nr_pages;
1612 rqst.rq_pagesz = wdata->pagesz;
1613 rqst.rq_tailsz = wdata->tailsz;
33319141
PS
1614
1615 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
1616
1617 req->Length = cpu_to_le32(wdata->bytes);
1618
1619 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
1620
1621 kref_get(&wdata->refcount);
fec344e3
JL
1622 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
1623 smb2_writev_callback, wdata, 0);
33319141 1624
e5d04887 1625 if (rc) {
33319141 1626 kref_put(&wdata->refcount, cifs_writedata_release);
e5d04887
PS
1627 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
1628 }
33319141 1629
33319141
PS
1630async_writev_out:
1631 cifs_small_buf_release(req);
33319141
PS
1632 return rc;
1633}
009d3443
PS
1634
1635/*
1636 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
1637 * The length field from io_parms must be at least 1 and indicates a number of
1638 * elements with data to write that begins with position 1 in iov array. All
1639 * data length is specified by count.
1640 */
1641int
1642SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
1643 unsigned int *nbytes, struct kvec *iov, int n_vec)
1644{
1645 int rc = 0;
1646 struct smb2_write_req *req = NULL;
1647 struct smb2_write_rsp *rsp = NULL;
1648 int resp_buftype;
1649 *nbytes = 0;
1650
1651 if (n_vec < 1)
1652 return rc;
1653
1654 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
1655 if (rc)
1656 return rc;
1657
1658 if (io_parms->tcon->ses->server == NULL)
1659 return -ECONNABORTED;
1660
1661 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1662
1663 req->PersistentFileId = io_parms->persistent_fid;
1664 req->VolatileFileId = io_parms->volatile_fid;
1665 req->WriteChannelInfoOffset = 0;
1666 req->WriteChannelInfoLength = 0;
1667 req->Channel = 0;
1668 req->Length = cpu_to_le32(io_parms->length);
1669 req->Offset = cpu_to_le64(io_parms->offset);
1670 /* 4 for rfc1002 length field */
1671 req->DataOffset = cpu_to_le16(
1672 offsetof(struct smb2_write_req, Buffer) - 4);
1673 req->RemainingBytes = 0;
1674
1675 iov[0].iov_base = (char *)req;
1676 /* 4 for rfc1002 length field and 1 for Buffer */
1677 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1678
1679 /* length of entire message including data to be written */
1680 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
1681
1682 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
1683 &resp_buftype, 0);
e5d04887 1684 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
009d3443
PS
1685
1686 if (rc) {
1687 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
1688 cERROR(1, "Send error in write = %d", rc);
e5d04887 1689 } else
009d3443 1690 *nbytes = le32_to_cpu(rsp->DataLength);
e5d04887
PS
1691
1692 free_rsp_buf(resp_buftype, rsp);
009d3443
PS
1693 return rc;
1694}
35143eb5 1695
d324f08d
PS
1696static unsigned int
1697num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
1698{
1699 int len;
1700 unsigned int entrycount = 0;
1701 unsigned int next_offset = 0;
1702 FILE_DIRECTORY_INFO *entryptr;
1703
1704 if (bufstart == NULL)
1705 return 0;
1706
1707 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
1708
1709 while (1) {
1710 entryptr = (FILE_DIRECTORY_INFO *)
1711 ((char *)entryptr + next_offset);
1712
1713 if ((char *)entryptr + size > end_of_buf) {
1714 cERROR(1, "malformed search entry would overflow");
1715 break;
1716 }
1717
1718 len = le32_to_cpu(entryptr->FileNameLength);
1719 if ((char *)entryptr + len + size > end_of_buf) {
1720 cERROR(1, "directory entry name would overflow frame "
1721 "end of buf %p", end_of_buf);
1722 break;
1723 }
1724
1725 *lastentry = (char *)entryptr;
1726 entrycount++;
1727
1728 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
1729 if (!next_offset)
1730 break;
1731 }
1732
1733 return entrycount;
1734}
1735
1736/*
1737 * Readdir/FindFirst
1738 */
1739int
1740SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
1741 u64 persistent_fid, u64 volatile_fid, int index,
1742 struct cifs_search_info *srch_inf)
1743{
1744 struct smb2_query_directory_req *req;
1745 struct smb2_query_directory_rsp *rsp = NULL;
1746 struct kvec iov[2];
1747 int rc = 0;
1748 int len;
1749 int resp_buftype;
1750 unsigned char *bufptr;
1751 struct TCP_Server_Info *server;
1752 struct cifs_ses *ses = tcon->ses;
1753 __le16 asteriks = cpu_to_le16('*');
1754 char *end_of_smb;
1755 unsigned int output_size = CIFSMaxBufSize;
1756 size_t info_buf_size;
1757
1758 if (ses && (ses->server))
1759 server = ses->server;
1760 else
1761 return -EIO;
1762
1763 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
1764 if (rc)
1765 return rc;
1766
1767 switch (srch_inf->info_level) {
1768 case SMB_FIND_FILE_DIRECTORY_INFO:
1769 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
1770 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
1771 break;
1772 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
1773 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
1774 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
1775 break;
1776 default:
1777 cERROR(1, "info level %u isn't supported",
1778 srch_inf->info_level);
1779 rc = -EINVAL;
1780 goto qdir_exit;
1781 }
1782
1783 req->FileIndex = cpu_to_le32(index);
1784 req->PersistentFileId = persistent_fid;
1785 req->VolatileFileId = volatile_fid;
1786
1787 len = 0x2;
1788 bufptr = req->Buffer;
1789 memcpy(bufptr, &asteriks, len);
1790
1791 req->FileNameOffset =
1792 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
1793 req->FileNameLength = cpu_to_le16(len);
1794 /*
1795 * BB could be 30 bytes or so longer if we used SMB2 specific
1796 * buffer lengths, but this is safe and close enough.
1797 */
1798 output_size = min_t(unsigned int, output_size, server->maxBuf);
1799 output_size = min_t(unsigned int, output_size, 2 << 15);
1800 req->OutputBufferLength = cpu_to_le32(output_size);
1801
1802 iov[0].iov_base = (char *)req;
1803 /* 4 for RFC1001 length and 1 for Buffer */
1804 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1805
1806 iov[1].iov_base = (char *)(req->Buffer);
1807 iov[1].iov_len = len;
1808
1809 inc_rfc1001_len(req, len - 1 /* Buffer */);
1810
1811 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
e5d04887
PS
1812 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
1813
d324f08d
PS
1814 if (rc) {
1815 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
1816 goto qdir_exit;
1817 }
d324f08d
PS
1818
1819 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
1820 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
1821 info_buf_size);
1822 if (rc)
1823 goto qdir_exit;
1824
1825 srch_inf->unicode = true;
1826
1827 if (srch_inf->ntwrk_buf_start) {
1828 if (srch_inf->smallBuf)
1829 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
1830 else
1831 cifs_buf_release(srch_inf->ntwrk_buf_start);
1832 }
1833 srch_inf->ntwrk_buf_start = (char *)rsp;
1834 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
1835 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
1836 /* 4 for rfc1002 length field */
1837 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
1838 srch_inf->entries_in_buffer =
1839 num_entries(srch_inf->srch_entries_start, end_of_smb,
1840 &srch_inf->last_entry, info_buf_size);
1841 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
1842 cFYI(1, "num entries %d last_index %lld srch start %p srch end %p",
1843 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
1844 srch_inf->srch_entries_start, srch_inf->last_entry);
1845 if (resp_buftype == CIFS_LARGE_BUFFER)
1846 srch_inf->smallBuf = false;
1847 else if (resp_buftype == CIFS_SMALL_BUFFER)
1848 srch_inf->smallBuf = true;
1849 else
1850 cERROR(1, "illegal search buffer type");
1851
1852 if (rsp->hdr.Status == STATUS_NO_MORE_FILES)
1853 srch_inf->endOfSearch = 1;
1854 else
1855 srch_inf->endOfSearch = 0;
1856
1857 return rc;
1858
1859qdir_exit:
1860 free_rsp_buf(resp_buftype, rsp);
1861 return rc;
1862}
1863
35143eb5
PS
1864static int
1865send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
c839ff24 1866 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
35143eb5
PS
1867 unsigned int num, void **data, unsigned int *size)
1868{
1869 struct smb2_set_info_req *req;
1870 struct smb2_set_info_rsp *rsp = NULL;
1871 struct kvec *iov;
1872 int rc = 0;
1873 int resp_buftype;
1874 unsigned int i;
1875 struct TCP_Server_Info *server;
1876 struct cifs_ses *ses = tcon->ses;
1877
1878 if (ses && (ses->server))
1879 server = ses->server;
1880 else
1881 return -EIO;
1882
1883 if (!num)
1884 return -EINVAL;
1885
1886 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
1887 if (!iov)
1888 return -ENOMEM;
1889
1890 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
1891 if (rc) {
1892 kfree(iov);
1893 return rc;
1894 }
1895
c839ff24
PS
1896 req->hdr.ProcessId = cpu_to_le32(pid);
1897
35143eb5
PS
1898 req->InfoType = SMB2_O_INFO_FILE;
1899 req->FileInfoClass = info_class;
1900 req->PersistentFileId = persistent_fid;
1901 req->VolatileFileId = volatile_fid;
1902
1903 /* 4 for RFC1001 length and 1 for Buffer */
1904 req->BufferOffset =
1905 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
1906 req->BufferLength = cpu_to_le32(*size);
1907
1908 inc_rfc1001_len(req, *size - 1 /* Buffer */);
1909
1910 memcpy(req->Buffer, *data, *size);
1911
1912 iov[0].iov_base = (char *)req;
1913 /* 4 for RFC1001 length */
1914 iov[0].iov_len = get_rfc1002_length(req) + 4;
1915
1916 for (i = 1; i < num; i++) {
1917 inc_rfc1001_len(req, size[i]);
1918 le32_add_cpu(&req->BufferLength, size[i]);
1919 iov[i].iov_base = (char *)data[i];
1920 iov[i].iov_len = size[i];
1921 }
1922
1923 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
1924 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
1925
1926 if (rc != 0) {
1927 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
1928 goto out;
1929 }
35143eb5
PS
1930out:
1931 free_rsp_buf(resp_buftype, rsp);
1932 kfree(iov);
1933 return rc;
1934}
1935
1936int
1937SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
1938 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
1939{
1940 struct smb2_file_rename_info info;
1941 void **data;
1942 unsigned int size[2];
1943 int rc;
1944 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
1945
1946 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
1947 if (!data)
1948 return -ENOMEM;
1949
1950 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
1951 /* 0 = fail if target already exists */
1952 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
1953 info.FileNameLength = cpu_to_le32(len);
1954
1955 data[0] = &info;
1956 size[0] = sizeof(struct smb2_file_rename_info);
1957
1958 data[1] = target_file;
1959 size[1] = len + 2 /* null */;
1960
1961 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
c839ff24
PS
1962 current->tgid, FILE_RENAME_INFORMATION, 2, data,
1963 size);
35143eb5
PS
1964 kfree(data);
1965 return rc;
1966}
568798cc
PS
1967
1968int
1969SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
1970 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
1971{
1972 struct smb2_file_link_info info;
1973 void **data;
1974 unsigned int size[2];
1975 int rc;
1976 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
1977
1978 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
1979 if (!data)
1980 return -ENOMEM;
1981
1982 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
1983 /* 0 = fail if link already exists */
1984 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
1985 info.FileNameLength = cpu_to_le32(len);
1986
1987 data[0] = &info;
1988 size[0] = sizeof(struct smb2_file_link_info);
1989
1990 data[1] = target_file;
1991 size[1] = len + 2 /* null */;
1992
1993 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
c839ff24 1994 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
568798cc
PS
1995 kfree(data);
1996 return rc;
1997}
c839ff24
PS
1998
1999int
2000SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2001 u64 volatile_fid, u32 pid, __le64 *eof)
2002{
2003 struct smb2_file_eof_info info;
2004 void *data;
2005 unsigned int size;
2006
2007 info.EndOfFile = *eof;
2008
2009 data = &info;
2010 size = sizeof(struct smb2_file_eof_info);
2011
2012 return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
2013 FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2014}
1feeaac7
PS
2015
2016int
2017SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2018 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2019{
2020 unsigned int size;
2021 size = sizeof(FILE_BASIC_INFO);
2022 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2023 current->tgid, FILE_BASIC_INFORMATION, 1,
2024 (void **)&buf, &size);
2025}
983c88a4
PS
2026
2027int
2028SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2029 const u64 persistent_fid, const u64 volatile_fid,
2030 __u8 oplock_level)
2031{
2032 int rc;
2033 struct smb2_oplock_break *req = NULL;
2034
2035 cFYI(1, "SMB2_oplock_break");
2036 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2037
2038 if (rc)
2039 return rc;
2040
2041 req->VolatileFid = volatile_fid;
2042 req->PersistentFid = persistent_fid;
2043 req->OplockLevel = oplock_level;
2044 req->hdr.CreditRequest = cpu_to_le16(1);
2045
2046 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2047 /* SMB2 buffer freed by function above */
2048
2049 if (rc) {
2050 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2051 cFYI(1, "Send error in Oplock Break = %d", rc);
2052 }
2053
2054 return rc;
2055}
6fc05c25
PS
2056
2057static void
2058copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2059 struct kstatfs *kst)
2060{
2061 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2062 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2063 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2064 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2065 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2066 return;
2067}
2068
2069static int
2070build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2071 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2072{
2073 int rc;
2074 struct smb2_query_info_req *req;
2075
2076 cFYI(1, "Query FSInfo level %d", level);
2077
2078 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2079 return -EIO;
2080
2081 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2082 if (rc)
2083 return rc;
2084
2085 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2086 req->FileInfoClass = level;
2087 req->PersistentFileId = persistent_fid;
2088 req->VolatileFileId = volatile_fid;
2089 /* 4 for rfc1002 length field and 1 for pad */
2090 req->InputBufferOffset =
2091 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2092 req->OutputBufferLength = cpu_to_le32(
2093 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2094
2095 iov->iov_base = (char *)req;
2096 /* 4 for rfc1002 length field */
2097 iov->iov_len = get_rfc1002_length(req) + 4;
2098 return 0;
2099}
2100
2101int
2102SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2103 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2104{
2105 struct smb2_query_info_rsp *rsp = NULL;
2106 struct kvec iov;
2107 int rc = 0;
2108 int resp_buftype;
2109 struct cifs_ses *ses = tcon->ses;
2110 struct smb2_fs_full_size_info *info = NULL;
2111
2112 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2113 sizeof(struct smb2_fs_full_size_info),
2114 persistent_fid, volatile_fid);
2115 if (rc)
2116 return rc;
2117
2118 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2119 if (rc) {
2120 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2121 goto qinf_exit;
2122 }
2123 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2124
2125 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2126 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2127 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2128 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2129 sizeof(struct smb2_fs_full_size_info));
2130 if (!rc)
2131 copy_fs_info_to_kstatfs(info, fsdata);
2132
2133qinf_exit:
2134 free_rsp_buf(resp_buftype, iov.iov_base);
2135 return rc;
2136}
f7ba7fe6
PS
2137
2138int
2139smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2140 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2141 const __u32 num_lock, struct smb2_lock_element *buf)
2142{
2143 int rc = 0;
2144 struct smb2_lock_req *req = NULL;
2145 struct kvec iov[2];
2146 int resp_buf_type;
2147 unsigned int count;
2148
2149 cFYI(1, "smb2_lockv num lock %d", num_lock);
2150
2151 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2152 if (rc)
2153 return rc;
2154
2155 req->hdr.ProcessId = cpu_to_le32(pid);
2156 req->LockCount = cpu_to_le16(num_lock);
2157
2158 req->PersistentFileId = persist_fid;
2159 req->VolatileFileId = volatile_fid;
2160
2161 count = num_lock * sizeof(struct smb2_lock_element);
2162 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2163
2164 iov[0].iov_base = (char *)req;
2165 /* 4 for rfc1002 length field and count for all locks */
2166 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2167 iov[1].iov_base = (char *)buf;
2168 iov[1].iov_len = count;
2169
2170 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2171 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2172 if (rc) {
2173 cFYI(1, "Send error in smb2_lockv = %d", rc);
2174 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2175 }
2176
2177 return rc;
2178}
2179
2180int
2181SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2182 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2183 const __u64 length, const __u64 offset, const __u32 lock_flags,
2184 const bool wait)
2185{
2186 struct smb2_lock_element lock;
2187
2188 lock.Offset = cpu_to_le64(offset);
2189 lock.Length = cpu_to_le64(length);
2190 lock.Flags = cpu_to_le32(lock_flags);
2191 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2192 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2193
2194 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2195}
0822f514
PS
2196
2197int
2198SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2199 __u8 *lease_key, const __le32 lease_state)
2200{
2201 int rc;
2202 struct smb2_lease_ack *req = NULL;
2203
2204 cFYI(1, "SMB2_lease_break");
2205 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2206
2207 if (rc)
2208 return rc;
2209
2210 req->hdr.CreditRequest = cpu_to_le16(1);
2211 req->StructureSize = cpu_to_le16(36);
2212 inc_rfc1001_len(req, 12);
2213
2214 memcpy(req->LeaseKey, lease_key, 16);
2215 req->LeaseState = lease_state;
2216
2217 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2218 /* SMB2 buffer freed by function above */
2219
2220 if (rc) {
2221 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2222 cFYI(1, "Send error in Lease Break = %d", rc);
2223 }
2224
2225 return rc;
2226}