1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/vfs.h>
34#include <linux/task_io_accounting_ops.h>
35#include <linux/uaccess.h>
36#include <linux/pagemap.h>
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"
47#include "smb2glob.h"
48#include "cifspdu.h"
49#include "cifs_spnego.h"
50
51
52
53
54
55
56
57
58static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
59 36,
60 25,
61 4,
62 9,
63 4,
64 57,
65 24,
66 24,
67 49,
68 49,
69 48,
70 57,
71 4,
72 4,
73 33,
74 32,
75 41,
76 33,
77 24
78};
79
80static int encryption_required(const struct cifs_tcon *tcon)
81{
82 if (!tcon)
83 return 0;
84 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
85 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
86 return 1;
87 if (tcon->seal &&
88 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
89 return 1;
90 return 0;
91}
92
93static void
94smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
95 const struct cifs_tcon *tcon)
96{
97 shdr->ProtocolId = SMB2_PROTO_NUMBER;
98 shdr->StructureSize = cpu_to_le16(64);
99 shdr->Command = smb2_cmd;
100 if (tcon && tcon->ses && tcon->ses->server) {
101 struct TCP_Server_Info *server = tcon->ses->server;
102
103 spin_lock(&server->req_lock);
104
105 if (server->credits >= server->max_credits)
106 shdr->CreditRequest = cpu_to_le16(0);
107 else
108 shdr->CreditRequest = cpu_to_le16(
109 min_t(int, server->max_credits -
110 server->credits, 2));
111 spin_unlock(&server->req_lock);
112 } else {
113 shdr->CreditRequest = cpu_to_le16(2);
114 }
115 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
116
117 if (!tcon)
118 goto out;
119
120
121
122 if ((tcon->ses) && (tcon->ses->server) &&
123 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
124 shdr->CreditCharge = cpu_to_le16(1);
125
126
127 shdr->TreeId = tcon->tid;
128
129 if (tcon->ses)
130 shdr->SessionId = tcon->ses->Suid;
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
146 !encryption_required(tcon))
147 shdr->Flags |= SMB2_FLAGS_SIGNED;
148out:
149 return;
150}
151
152static int
153smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
154{
155 int rc;
156 struct nls_table *nls_codepage;
157 struct cifs_ses *ses;
158 struct TCP_Server_Info *server;
159
160
161
162
163
164
165 if (tcon == NULL)
166 return 0;
167
168 if (smb2_command == SMB2_TREE_CONNECT)
169 return 0;
170
171 if (tcon->tidStatus == CifsExiting) {
172
173
174
175
176
177 if ((smb2_command != SMB2_WRITE) &&
178 (smb2_command != SMB2_CREATE) &&
179 (smb2_command != SMB2_TREE_DISCONNECT)) {
180 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
181 smb2_command);
182 return -ENODEV;
183 }
184 }
185 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
186 (!tcon->ses->server))
187 return -EIO;
188
189 ses = tcon->ses;
190 server = ses->server;
191
192
193
194
195
196 while (server->tcpStatus == CifsNeedReconnect) {
197
198
199
200
201 switch (smb2_command) {
202
203
204
205 case SMB2_TREE_DISCONNECT:
206 case SMB2_CANCEL:
207 case SMB2_CLOSE:
208 case SMB2_OPLOCK_BREAK:
209 return -EAGAIN;
210 }
211
212 rc = wait_event_interruptible_timeout(server->response_q,
213 (server->tcpStatus != CifsNeedReconnect),
214 10 * HZ);
215 if (rc < 0) {
216 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
217 " signal by the process\n", __func__);
218 return -ERESTARTSYS;
219 }
220
221
222 if (server->tcpStatus != CifsNeedReconnect)
223 break;
224
225
226
227
228
229
230 if (!tcon->retry) {
231 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
232 return -EHOSTDOWN;
233 }
234 }
235
236 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
237 return 0;
238
239 nls_codepage = load_nls_default();
240
241
242
243
244
245 mutex_lock(&tcon->ses->session_mutex);
246
247
248
249
250
251
252 if (server->tcpStatus == CifsNeedReconnect) {
253 rc = -EHOSTDOWN;
254 mutex_unlock(&tcon->ses->session_mutex);
255 goto out;
256 }
257
258 rc = cifs_negotiate_protocol(0, tcon->ses);
259 if (!rc && tcon->ses->need_reconnect)
260 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
261
262 if (rc || !tcon->need_reconnect) {
263 mutex_unlock(&tcon->ses->session_mutex);
264 goto out;
265 }
266
267 cifs_mark_open_files_invalid(tcon);
268 if (tcon->use_persistent)
269 tcon->need_reopen_files = true;
270
271 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
272 mutex_unlock(&tcon->ses->session_mutex);
273
274 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
275 if (rc)
276 goto out;
277
278 if (smb2_command != SMB2_INTERNAL_CMD)
279 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
280
281 atomic_inc(&tconInfoReconnectCount);
282out:
283
284
285
286
287
288
289
290
291 switch (smb2_command) {
292 case SMB2_FLUSH:
293 case SMB2_READ:
294 case SMB2_WRITE:
295 case SMB2_LOCK:
296 case SMB2_IOCTL:
297 case SMB2_QUERY_DIRECTORY:
298 case SMB2_CHANGE_NOTIFY:
299 case SMB2_QUERY_INFO:
300 case SMB2_SET_INFO:
301 rc = -EAGAIN;
302 }
303 unload_nls(nls_codepage);
304 return rc;
305}
306
307static void
308fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
309 unsigned int *total_len)
310{
311 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
312
313 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
314
315
316
317
318
319 memset(buf, 0, 256);
320
321 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
322 spdu->StructureSize2 = cpu_to_le16(parmsize);
323
324 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
325}
326
327
328
329
330
331
332static int
333smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
334 void **request_buf, unsigned int *total_len)
335{
336 int rc;
337
338 rc = smb2_reconnect(smb2_command, tcon);
339 if (rc)
340 return rc;
341
342
343 if (smb2_command == SMB2_SET_INFO)
344 *request_buf = cifs_buf_get();
345 else
346 *request_buf = cifs_small_buf_get();
347 if (*request_buf == NULL) {
348
349 return -ENOMEM;
350 }
351
352 fill_small_buf(smb2_command, tcon,
353 (struct smb2_sync_hdr *)(*request_buf),
354 total_len);
355
356 if (tcon != NULL) {
357#ifdef CONFIG_CIFS_STATS2
358 uint16_t com_code = le16_to_cpu(smb2_command);
359 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
360#endif
361 cifs_stats_inc(&tcon->num_smbs_sent);
362 }
363
364 return rc;
365}
366
367#ifdef CONFIG_CIFS_SMB311
368
369#define OFFSET_OF_NEG_CONTEXT 0x68
370
371
372#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
373#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
374
375static void
376build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
377{
378 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
379 pneg_ctxt->DataLength = cpu_to_le16(38);
380 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
381 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
382 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
383 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
384}
385
386static void
387build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
388{
389 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
390 pneg_ctxt->DataLength = cpu_to_le16(6);
391 pneg_ctxt->CipherCount = cpu_to_le16(2);
392 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
393 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
394}
395
396static void
397assemble_neg_contexts(struct smb2_negotiate_req *req,
398 unsigned int *total_len)
399{
400 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
401
402 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
403
404
405 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
406 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
407 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
408 req->NegotiateContextCount = cpu_to_le16(2);
409
410 *total_len += 4 + sizeof(struct smb2_preauth_neg_context)
411 + sizeof(struct smb2_encryption_neg_context);
412}
413#else
414static void assemble_neg_contexts(struct smb2_negotiate_req *req,
415 unsigned int *total_len)
416{
417 return;
418}
419#endif
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436int
437SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
438{
439 struct smb2_negotiate_req *req;
440 struct smb2_negotiate_rsp *rsp;
441 struct kvec iov[1];
442 struct kvec rsp_iov;
443 int rc = 0;
444 int resp_buftype;
445 struct TCP_Server_Info *server = ses->server;
446 int blob_offset, blob_length;
447 char *security_blob;
448 int flags = CIFS_NEG_OP;
449 unsigned int total_len;
450
451 cifs_dbg(FYI, "Negotiate protocol\n");
452
453 if (!server) {
454 WARN(1, "%s: server is NULL!\n", __func__);
455 return -EIO;
456 }
457
458 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
459 if (rc)
460 return rc;
461
462 req->sync_hdr.SessionId = 0;
463
464 if (strcmp(ses->server->vals->version_string,
465 SMB3ANY_VERSION_STRING) == 0) {
466 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
467 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
468 req->DialectCount = cpu_to_le16(2);
469 total_len += 4;
470 } else if (strcmp(ses->server->vals->version_string,
471 SMBDEFAULT_VERSION_STRING) == 0) {
472 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
473 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
474 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
475 req->DialectCount = cpu_to_le16(3);
476 total_len += 6;
477 } else {
478
479 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
480 req->DialectCount = cpu_to_le16(1);
481 total_len += 2;
482 }
483
484
485 if (ses->sign)
486 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
487 else if (global_secflags & CIFSSEC_MAY_SIGN)
488 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
489 else
490 req->SecurityMode = 0;
491
492 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
493
494
495 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
496 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
497 else {
498 memcpy(req->ClientGUID, server->client_guid,
499 SMB2_CLIENT_GUID_SIZE);
500 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
501 assemble_neg_contexts(req, &total_len);
502 }
503 iov[0].iov_base = (char *)req;
504 iov[0].iov_len = total_len;
505
506 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
507 cifs_small_buf_release(req);
508 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
509
510
511
512
513 if (rc == -EOPNOTSUPP) {
514 cifs_dbg(VFS, "Dialect not supported by server. Consider "
515 "specifying vers=1.0 or vers=2.0 on mount for accessing"
516 " older servers\n");
517 goto neg_exit;
518 } else if (rc != 0)
519 goto neg_exit;
520
521 if (strcmp(ses->server->vals->version_string,
522 SMB3ANY_VERSION_STRING) == 0) {
523 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
524 cifs_dbg(VFS,
525 "SMB2 dialect returned but not requested\n");
526 return -EIO;
527 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
528 cifs_dbg(VFS,
529 "SMB2.1 dialect returned but not requested\n");
530 return -EIO;
531 }
532 } else if (strcmp(ses->server->vals->version_string,
533 SMBDEFAULT_VERSION_STRING) == 0) {
534 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
535 cifs_dbg(VFS,
536 "SMB2 dialect returned but not requested\n");
537 return -EIO;
538 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
539
540 ses->server->ops = &smb21_operations;
541 }
542 } else if (le16_to_cpu(rsp->DialectRevision) !=
543 ses->server->vals->protocol_id) {
544
545 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
546 le16_to_cpu(rsp->DialectRevision));
547 return -EIO;
548 }
549
550 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
551
552 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
553 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
554 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
555 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
556 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
557 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
558 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
559 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
560#ifdef CONFIG_CIFS_SMB311
561 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
562 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
563#endif
564 else {
565 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
566 le16_to_cpu(rsp->DialectRevision));
567 rc = -EIO;
568 goto neg_exit;
569 }
570 server->dialect = le16_to_cpu(rsp->DialectRevision);
571
572
573
574
575 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
576
577 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
578 SMB2_MAX_BUFFER_SIZE);
579 server->max_read = le32_to_cpu(rsp->MaxReadSize);
580 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
581
582 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
583 server->capabilities = le32_to_cpu(rsp->Capabilities);
584
585 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
586
587 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
588 &rsp->hdr);
589
590
591
592
593
594
595
596 if (blob_length == 0) {
597 cifs_dbg(FYI, "missing security blob on negprot\n");
598 server->sec_ntlmssp = true;
599 }
600
601 rc = cifs_enable_signing(server, ses->sign);
602 if (rc)
603 goto neg_exit;
604 if (blob_length) {
605 rc = decode_negTokenInit(security_blob, blob_length, server);
606 if (rc == 1)
607 rc = 0;
608 else if (rc == 0)
609 rc = -EIO;
610 }
611neg_exit:
612 free_rsp_buf(resp_buftype, rsp);
613 return rc;
614}
615
616int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
617{
618 int rc = 0;
619 struct validate_negotiate_info_req vneg_inbuf;
620 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
621 u32 rsplen;
622 u32 inbuflen;
623
624 cifs_dbg(FYI, "validate negotiate\n");
625
626
627
628
629
630
631
632
633 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
634 return 0;
635
636 if (tcon->ses->user_name == NULL) {
637 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
638 return 0;
639 }
640
641 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
642 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
643
644 vneg_inbuf.Capabilities =
645 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
646 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
647 SMB2_CLIENT_GUID_SIZE);
648
649 if (tcon->ses->sign)
650 vneg_inbuf.SecurityMode =
651 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
652 else if (global_secflags & CIFSSEC_MAY_SIGN)
653 vneg_inbuf.SecurityMode =
654 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
655 else
656 vneg_inbuf.SecurityMode = 0;
657
658
659 if (strcmp(tcon->ses->server->vals->version_string,
660 SMB3ANY_VERSION_STRING) == 0) {
661 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
662 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
663 vneg_inbuf.DialectCount = cpu_to_le16(2);
664
665 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
666 } else if (strcmp(tcon->ses->server->vals->version_string,
667 SMBDEFAULT_VERSION_STRING) == 0) {
668 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
669 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
670 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
671 vneg_inbuf.DialectCount = cpu_to_le16(3);
672
673 inbuflen = sizeof(struct validate_negotiate_info_req);
674 } else {
675
676 vneg_inbuf.Dialects[0] =
677 cpu_to_le16(tcon->ses->server->vals->protocol_id);
678 vneg_inbuf.DialectCount = cpu_to_le16(1);
679
680 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
681 }
682
683 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
684 FSCTL_VALIDATE_NEGOTIATE_INFO, true ,
685 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
686 (char **)&pneg_rsp, &rsplen);
687
688 if (rc == -EOPNOTSUPP) {
689
690
691
692
693 cifs_dbg(VFS, "Server does not support validate negotiate\n");
694 return 0;
695 } else if (rc != 0) {
696 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
697 return -EIO;
698 }
699
700 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
701 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
702 rsplen);
703
704
705 if ((rsplen > CIFSMaxBufSize)
706 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
707 goto err_rsp_free;
708 }
709
710
711 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
712 goto vneg_out;
713
714 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
715 goto vneg_out;
716
717
718
719 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
720 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
721 goto vneg_out;
722
723
724 cifs_dbg(FYI, "validate negotiate info successful\n");
725 kfree(pneg_rsp);
726 return 0;
727
728vneg_out:
729 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
730err_rsp_free:
731 kfree(pneg_rsp);
732 return -EIO;
733}
734
735enum securityEnum
736smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
737{
738 switch (requested) {
739 case Kerberos:
740 case RawNTLMSSP:
741 return requested;
742 case NTLMv2:
743 return RawNTLMSSP;
744 case Unspecified:
745 if (server->sec_ntlmssp &&
746 (global_secflags & CIFSSEC_MAY_NTLMSSP))
747 return RawNTLMSSP;
748 if ((server->sec_kerberos || server->sec_mskerberos) &&
749 (global_secflags & CIFSSEC_MAY_KRB5))
750 return Kerberos;
751
752 default:
753 return Unspecified;
754 }
755}
756
757struct SMB2_sess_data {
758 unsigned int xid;
759 struct cifs_ses *ses;
760 struct nls_table *nls_cp;
761 void (*func)(struct SMB2_sess_data *);
762 int result;
763 u64 previous_session;
764
765
766
767
768
769
770
771
772 int buf0_type;
773 struct kvec iov[2];
774};
775
776static int
777SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
778{
779 int rc;
780 struct cifs_ses *ses = sess_data->ses;
781 struct smb2_sess_setup_req *req;
782 struct TCP_Server_Info *server = ses->server;
783 unsigned int total_len;
784
785 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
786 &total_len);
787 if (rc)
788 return rc;
789
790
791 req->sync_hdr.SessionId = 0;
792
793
794 req->PreviousSessionId = sess_data->previous_session;
795
796 req->Flags = 0;
797
798 req->sync_hdr.CreditRequest = cpu_to_le16(3);
799
800
801 if (server->sign)
802 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
803 else if (global_secflags & CIFSSEC_MAY_SIGN)
804 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
805 else
806 req->SecurityMode = 0;
807
808 req->Capabilities = 0;
809 req->Channel = 0;
810
811 sess_data->iov[0].iov_base = (char *)req;
812
813 sess_data->iov[0].iov_len = total_len - 1;
814
815
816
817
818 sess_data->buf0_type = CIFS_SMALL_BUFFER;
819
820 return 0;
821}
822
823static void
824SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
825{
826 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
827 sess_data->buf0_type = CIFS_NO_BUFFER;
828}
829
830static int
831SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
832{
833 int rc;
834 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
835 struct kvec rsp_iov = { NULL, 0 };
836
837
838 req->SecurityBufferOffset =
839 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 );
840 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
841
842
843
844 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
845 sess_data->iov, 2,
846 &sess_data->buf0_type,
847 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
848 cifs_small_buf_release(sess_data->iov[0].iov_base);
849 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
850
851 return rc;
852}
853
854static int
855SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
856{
857 int rc = 0;
858 struct cifs_ses *ses = sess_data->ses;
859
860 mutex_lock(&ses->server->srv_mutex);
861 if (ses->server->ops->generate_signingkey) {
862 rc = ses->server->ops->generate_signingkey(ses);
863 if (rc) {
864 cifs_dbg(FYI,
865 "SMB3 session key generation failed\n");
866 mutex_unlock(&ses->server->srv_mutex);
867 return rc;
868 }
869 }
870 if (!ses->server->session_estab) {
871 ses->server->sequence_number = 0x2;
872 ses->server->session_estab = true;
873 }
874 mutex_unlock(&ses->server->srv_mutex);
875
876 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
877 spin_lock(&GlobalMid_Lock);
878 ses->status = CifsGood;
879 ses->need_reconnect = false;
880 spin_unlock(&GlobalMid_Lock);
881 return rc;
882}
883
884#ifdef CONFIG_CIFS_UPCALL
885static void
886SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
887{
888 int rc;
889 struct cifs_ses *ses = sess_data->ses;
890 struct cifs_spnego_msg *msg;
891 struct key *spnego_key = NULL;
892 struct smb2_sess_setup_rsp *rsp = NULL;
893
894 rc = SMB2_sess_alloc_buffer(sess_data);
895 if (rc)
896 goto out;
897
898 spnego_key = cifs_get_spnego_key(ses);
899 if (IS_ERR(spnego_key)) {
900 rc = PTR_ERR(spnego_key);
901 spnego_key = NULL;
902 goto out;
903 }
904
905 msg = spnego_key->payload.data;
906
907
908
909
910 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
911 cifs_dbg(VFS,
912 "bad cifs.upcall version. Expected %d got %d",
913 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
914 rc = -EKEYREJECTED;
915 goto out_put_spnego_key;
916 }
917
918 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
919 GFP_KERNEL);
920 if (!ses->auth_key.response) {
921 cifs_dbg(VFS,
922 "Kerberos can't allocate (%u bytes) memory",
923 msg->sesskey_len);
924 rc = -ENOMEM;
925 goto out_put_spnego_key;
926 }
927 ses->auth_key.len = msg->sesskey_len;
928
929 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
930 sess_data->iov[1].iov_len = msg->secblob_len;
931
932 rc = SMB2_sess_sendreceive(sess_data);
933 if (rc)
934 goto out_put_spnego_key;
935
936 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
937 ses->Suid = rsp->hdr.sync_hdr.SessionId;
938
939 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
940
941 rc = SMB2_sess_establish_session(sess_data);
942out_put_spnego_key:
943 key_invalidate(spnego_key);
944 key_put(spnego_key);
945out:
946 sess_data->result = rc;
947 sess_data->func = NULL;
948 SMB2_sess_free_buffer(sess_data);
949}
950#else
951static void
952SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
953{
954 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
955 sess_data->result = -EOPNOTSUPP;
956 sess_data->func = NULL;
957}
958#endif
959
960static void
961SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
962
963static void
964SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
965{
966 int rc;
967 struct cifs_ses *ses = sess_data->ses;
968 struct smb2_sess_setup_rsp *rsp = NULL;
969 char *ntlmssp_blob = NULL;
970 bool use_spnego = false;
971 u16 blob_length = 0;
972
973
974
975
976
977 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
978 if (!ses->ntlmssp) {
979 rc = -ENOMEM;
980 goto out_err;
981 }
982 ses->ntlmssp->sesskey_per_smbsess = true;
983
984 rc = SMB2_sess_alloc_buffer(sess_data);
985 if (rc)
986 goto out_err;
987
988 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
989 GFP_KERNEL);
990 if (ntlmssp_blob == NULL) {
991 rc = -ENOMEM;
992 goto out;
993 }
994
995 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
996 if (use_spnego) {
997
998 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
999 rc = -EOPNOTSUPP;
1000 goto out;
1001 } else {
1002 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1003
1004 }
1005 sess_data->iov[1].iov_base = ntlmssp_blob;
1006 sess_data->iov[1].iov_len = blob_length;
1007
1008 rc = SMB2_sess_sendreceive(sess_data);
1009 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1010
1011
1012 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1013 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1014 rc = 0;
1015
1016 if (rc)
1017 goto out;
1018
1019 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1020 le16_to_cpu(rsp->SecurityBufferOffset)) {
1021 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1022 le16_to_cpu(rsp->SecurityBufferOffset));
1023 rc = -EIO;
1024 goto out;
1025 }
1026 rc = decode_ntlmssp_challenge(rsp->Buffer,
1027 le16_to_cpu(rsp->SecurityBufferLength), ses);
1028 if (rc)
1029 goto out;
1030
1031 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1032
1033
1034 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1035 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1036
1037out:
1038 kfree(ntlmssp_blob);
1039 SMB2_sess_free_buffer(sess_data);
1040 if (!rc) {
1041 sess_data->result = 0;
1042 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1043 return;
1044 }
1045out_err:
1046 kfree(ses->ntlmssp);
1047 ses->ntlmssp = NULL;
1048 sess_data->result = rc;
1049 sess_data->func = NULL;
1050}
1051
1052static void
1053SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1054{
1055 int rc;
1056 struct cifs_ses *ses = sess_data->ses;
1057 struct smb2_sess_setup_req *req;
1058 struct smb2_sess_setup_rsp *rsp = NULL;
1059 unsigned char *ntlmssp_blob = NULL;
1060 bool use_spnego = false;
1061 u16 blob_length = 0;
1062
1063 rc = SMB2_sess_alloc_buffer(sess_data);
1064 if (rc)
1065 goto out;
1066
1067 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1068 req->sync_hdr.SessionId = ses->Suid;
1069
1070 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1071 sess_data->nls_cp);
1072 if (rc) {
1073 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1074 goto out;
1075 }
1076
1077 if (use_spnego) {
1078
1079 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1080 rc = -EOPNOTSUPP;
1081 goto out;
1082 }
1083 sess_data->iov[1].iov_base = ntlmssp_blob;
1084 sess_data->iov[1].iov_len = blob_length;
1085
1086 rc = SMB2_sess_sendreceive(sess_data);
1087 if (rc)
1088 goto out;
1089
1090 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1091
1092 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1093 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1094
1095 rc = SMB2_sess_establish_session(sess_data);
1096out:
1097 kfree(ntlmssp_blob);
1098 SMB2_sess_free_buffer(sess_data);
1099 kfree(ses->ntlmssp);
1100 ses->ntlmssp = NULL;
1101 sess_data->result = rc;
1102 sess_data->func = NULL;
1103}
1104
1105static int
1106SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1107{
1108 int type;
1109
1110 type = smb2_select_sectype(ses->server, ses->sectype);
1111 cifs_dbg(FYI, "sess setup type %d\n", type);
1112 if (type == Unspecified) {
1113 cifs_dbg(VFS,
1114 "Unable to select appropriate authentication method!");
1115 return -EINVAL;
1116 }
1117
1118 switch (type) {
1119 case Kerberos:
1120 sess_data->func = SMB2_auth_kerberos;
1121 break;
1122 case RawNTLMSSP:
1123 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1124 break;
1125 default:
1126 cifs_dbg(VFS, "secType %d not supported!\n", type);
1127 return -EOPNOTSUPP;
1128 }
1129
1130 return 0;
1131}
1132
1133int
1134SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1135 const struct nls_table *nls_cp)
1136{
1137 int rc = 0;
1138 struct TCP_Server_Info *server = ses->server;
1139 struct SMB2_sess_data *sess_data;
1140
1141 cifs_dbg(FYI, "Session Setup\n");
1142
1143 if (!server) {
1144 WARN(1, "%s: server is NULL!\n", __func__);
1145 return -EIO;
1146 }
1147
1148 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1149 if (!sess_data)
1150 return -ENOMEM;
1151
1152 rc = SMB2_select_sec(ses, sess_data);
1153 if (rc)
1154 goto out;
1155 sess_data->xid = xid;
1156 sess_data->ses = ses;
1157 sess_data->buf0_type = CIFS_NO_BUFFER;
1158 sess_data->nls_cp = (struct nls_table *) nls_cp;
1159
1160 while (sess_data->func)
1161 sess_data->func(sess_data);
1162
1163 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1164 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1165 rc = sess_data->result;
1166out:
1167 kfree(sess_data);
1168 return rc;
1169}
1170
1171int
1172SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1173{
1174 struct smb2_logoff_req *req;
1175 int rc = 0;
1176 struct TCP_Server_Info *server;
1177 int flags = 0;
1178 unsigned int total_len;
1179 struct kvec iov[1];
1180 struct kvec rsp_iov;
1181 int resp_buf_type;
1182
1183 cifs_dbg(FYI, "disconnect session %p\n", ses);
1184
1185 if (ses && (ses->server))
1186 server = ses->server;
1187 else
1188 return -EIO;
1189
1190
1191 if (ses->need_reconnect)
1192 goto smb2_session_already_dead;
1193
1194 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1195 if (rc)
1196 return rc;
1197
1198
1199 req->sync_hdr.SessionId = ses->Suid;
1200
1201 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1202 flags |= CIFS_TRANSFORM_REQ;
1203 else if (server->sign)
1204 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1205
1206 flags |= CIFS_NO_RESP;
1207
1208 iov[0].iov_base = (char *)req;
1209 iov[0].iov_len = total_len;
1210
1211 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
1212 cifs_small_buf_release(req);
1213
1214
1215
1216
1217
1218smb2_session_already_dead:
1219 return rc;
1220}
1221
1222static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1223{
1224 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1225}
1226
1227#define MAX_SHARENAME_LENGTH (255 + 80 + 1 )
1228
1229
1230static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1231{
1232 tcon->max_chunks = 256;
1233 tcon->max_bytes_chunk = 1048576;
1234 tcon->max_bytes_copy = 16777216;
1235}
1236
1237int
1238SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1239 struct cifs_tcon *tcon, const struct nls_table *cp)
1240{
1241 struct smb2_tree_connect_req *req;
1242 struct smb2_tree_connect_rsp *rsp = NULL;
1243 struct kvec iov[2];
1244 struct kvec rsp_iov = { NULL, 0 };
1245 int rc = 0;
1246 int resp_buftype;
1247 int unc_path_len;
1248 __le16 *unc_path = NULL;
1249 int flags = 0;
1250 unsigned int total_len;
1251
1252 cifs_dbg(FYI, "TCON\n");
1253
1254 if (!(ses->server) || !tree)
1255 return -EIO;
1256
1257 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1258 if (unc_path == NULL)
1259 return -ENOMEM;
1260
1261 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1262 unc_path_len *= 2;
1263 if (unc_path_len < 2) {
1264 kfree(unc_path);
1265 return -EINVAL;
1266 }
1267
1268
1269 tcon->tid = 0;
1270
1271 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1272 &total_len);
1273 if (rc) {
1274 kfree(unc_path);
1275 return rc;
1276 }
1277
1278 if (encryption_required(tcon))
1279 flags |= CIFS_TRANSFORM_REQ;
1280
1281 iov[0].iov_base = (char *)req;
1282
1283 iov[0].iov_len = total_len - 1;
1284
1285
1286 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1287 - 1 );
1288 req->PathLength = cpu_to_le16(unc_path_len - 2);
1289 iov[1].iov_base = unc_path;
1290 iov[1].iov_len = unc_path_len;
1291
1292 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
1293 cifs_small_buf_release(req);
1294 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1295
1296 if (rc != 0) {
1297 if (tcon) {
1298 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1299 tcon->need_reconnect = true;
1300 }
1301 goto tcon_error_exit;
1302 }
1303
1304 switch (rsp->ShareType) {
1305 case SMB2_SHARE_TYPE_DISK:
1306 cifs_dbg(FYI, "connection to disk share\n");
1307 break;
1308 case SMB2_SHARE_TYPE_PIPE:
1309 tcon->pipe = true;
1310 cifs_dbg(FYI, "connection to pipe share\n");
1311 break;
1312 case SMB2_SHARE_TYPE_PRINT:
1313 tcon->print = true;
1314 cifs_dbg(FYI, "connection to printer\n");
1315 break;
1316 default:
1317 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1318 rc = -EOPNOTSUPP;
1319 goto tcon_error_exit;
1320 }
1321
1322 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1323 tcon->capabilities = rsp->Capabilities;
1324 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1325 tcon->tidStatus = CifsGood;
1326 tcon->need_reconnect = false;
1327 tcon->tid = rsp->hdr.sync_hdr.TreeId;
1328 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1329
1330 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1331 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1332 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1333
1334 if (tcon->seal &&
1335 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1336 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1337
1338 init_copy_chunk_defaults(tcon);
1339 if (tcon->ses->server->ops->validate_negotiate)
1340 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1341tcon_exit:
1342 free_rsp_buf(resp_buftype, rsp);
1343 kfree(unc_path);
1344 return rc;
1345
1346tcon_error_exit:
1347 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1348 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1349 }
1350 goto tcon_exit;
1351}
1352
1353int
1354SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1355{
1356 struct smb2_tree_disconnect_req *req;
1357 int rc = 0;
1358 struct cifs_ses *ses = tcon->ses;
1359 int flags = 0;
1360 unsigned int total_len;
1361 struct kvec iov[1];
1362 struct kvec rsp_iov;
1363 int resp_buf_type;
1364
1365 cifs_dbg(FYI, "Tree Disconnect\n");
1366
1367 if (!ses || !(ses->server))
1368 return -EIO;
1369
1370 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1371 return 0;
1372
1373 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1374 &total_len);
1375 if (rc)
1376 return rc;
1377
1378 if (encryption_required(tcon))
1379 flags |= CIFS_TRANSFORM_REQ;
1380
1381 flags |= CIFS_NO_RESP;
1382
1383 iov[0].iov_base = (char *)req;
1384 iov[0].iov_len = total_len;
1385
1386 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
1387 cifs_small_buf_release(req);
1388 if (rc)
1389 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1390
1391 return rc;
1392}
1393
1394
1395static struct create_durable *
1396create_durable_buf(void)
1397{
1398 struct create_durable *buf;
1399
1400 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1401 if (!buf)
1402 return NULL;
1403
1404 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1405 (struct create_durable, Data));
1406 buf->ccontext.DataLength = cpu_to_le32(16);
1407 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1408 (struct create_durable, Name));
1409 buf->ccontext.NameLength = cpu_to_le16(4);
1410
1411 buf->Name[0] = 'D';
1412 buf->Name[1] = 'H';
1413 buf->Name[2] = 'n';
1414 buf->Name[3] = 'Q';
1415 return buf;
1416}
1417
1418static struct create_durable *
1419create_reconnect_durable_buf(struct cifs_fid *fid)
1420{
1421 struct create_durable *buf;
1422
1423 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1424 if (!buf)
1425 return NULL;
1426
1427 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1428 (struct create_durable, Data));
1429 buf->ccontext.DataLength = cpu_to_le32(16);
1430 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1431 (struct create_durable, Name));
1432 buf->ccontext.NameLength = cpu_to_le16(4);
1433 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1434 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1435
1436 buf->Name[0] = 'D';
1437 buf->Name[1] = 'H';
1438 buf->Name[2] = 'n';
1439 buf->Name[3] = 'C';
1440 return buf;
1441}
1442
1443static __u8
1444parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1445 unsigned int *epoch, char *lease_key)
1446{
1447 char *data_offset;
1448 struct create_context *cc;
1449 unsigned int next;
1450 unsigned int remaining;
1451 char *name;
1452
1453 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1454 remaining = le32_to_cpu(rsp->CreateContextsLength);
1455 cc = (struct create_context *)data_offset;
1456 while (remaining >= sizeof(struct create_context)) {
1457 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1458 if (le16_to_cpu(cc->NameLength) == 4 &&
1459 strncmp(name, "RqLs", 4) == 0)
1460 return server->ops->parse_lease_buf(cc, epoch,
1461 lease_key);
1462
1463 next = le32_to_cpu(cc->Next);
1464 if (!next)
1465 break;
1466 remaining -= next;
1467 cc = (struct create_context *)((char *)cc + next);
1468 }
1469
1470 return 0;
1471}
1472
1473static int
1474add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1475 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1476{
1477 struct smb2_create_req *req = iov[0].iov_base;
1478 unsigned int num = *num_iovec;
1479
1480 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1481 if (iov[num].iov_base == NULL)
1482 return -ENOMEM;
1483 iov[num].iov_len = server->vals->create_lease_size;
1484 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1485 if (!req->CreateContextsOffset)
1486 req->CreateContextsOffset = cpu_to_le32(
1487 sizeof(struct smb2_create_req) +
1488 iov[num - 1].iov_len);
1489 le32_add_cpu(&req->CreateContextsLength,
1490 server->vals->create_lease_size);
1491 *num_iovec = num + 1;
1492 return 0;
1493}
1494
1495static struct create_durable_v2 *
1496create_durable_v2_buf(struct cifs_fid *pfid)
1497{
1498 struct create_durable_v2 *buf;
1499
1500 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1501 if (!buf)
1502 return NULL;
1503
1504 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1505 (struct create_durable_v2, dcontext));
1506 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1507 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1508 (struct create_durable_v2, Name));
1509 buf->ccontext.NameLength = cpu_to_le16(4);
1510
1511 buf->dcontext.Timeout = 0;
1512 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1513 generate_random_uuid(buf->dcontext.CreateGuid);
1514 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1515
1516
1517 buf->Name[0] = 'D';
1518 buf->Name[1] = 'H';
1519 buf->Name[2] = '2';
1520 buf->Name[3] = 'Q';
1521 return buf;
1522}
1523
1524static struct create_durable_handle_reconnect_v2 *
1525create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1526{
1527 struct create_durable_handle_reconnect_v2 *buf;
1528
1529 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1530 GFP_KERNEL);
1531 if (!buf)
1532 return NULL;
1533
1534 buf->ccontext.DataOffset =
1535 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1536 dcontext));
1537 buf->ccontext.DataLength =
1538 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1539 buf->ccontext.NameOffset =
1540 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1541 Name));
1542 buf->ccontext.NameLength = cpu_to_le16(4);
1543
1544 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1545 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1546 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1547 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1548
1549
1550 buf->Name[0] = 'D';
1551 buf->Name[1] = 'H';
1552 buf->Name[2] = '2';
1553 buf->Name[3] = 'C';
1554 return buf;
1555}
1556
1557static int
1558add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1559 struct cifs_open_parms *oparms)
1560{
1561 struct smb2_create_req *req = iov[0].iov_base;
1562 unsigned int num = *num_iovec;
1563
1564 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1565 if (iov[num].iov_base == NULL)
1566 return -ENOMEM;
1567 iov[num].iov_len = sizeof(struct create_durable_v2);
1568 if (!req->CreateContextsOffset)
1569 req->CreateContextsOffset =
1570 cpu_to_le32(sizeof(struct smb2_create_req) +
1571 iov[1].iov_len);
1572 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1573 *num_iovec = num + 1;
1574 return 0;
1575}
1576
1577static int
1578add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1579 struct cifs_open_parms *oparms)
1580{
1581 struct smb2_create_req *req = iov[0].iov_base;
1582 unsigned int num = *num_iovec;
1583
1584
1585 oparms->reconnect = false;
1586
1587 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1588 if (iov[num].iov_base == NULL)
1589 return -ENOMEM;
1590 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1591 if (!req->CreateContextsOffset)
1592 req->CreateContextsOffset =
1593 cpu_to_le32(sizeof(struct smb2_create_req) +
1594 iov[1].iov_len);
1595 le32_add_cpu(&req->CreateContextsLength,
1596 sizeof(struct create_durable_handle_reconnect_v2));
1597 *num_iovec = num + 1;
1598 return 0;
1599}
1600
1601static int
1602add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1603 struct cifs_open_parms *oparms, bool use_persistent)
1604{
1605 struct smb2_create_req *req = iov[0].iov_base;
1606 unsigned int num = *num_iovec;
1607
1608 if (use_persistent) {
1609 if (oparms->reconnect)
1610 return add_durable_reconnect_v2_context(iov, num_iovec,
1611 oparms);
1612 else
1613 return add_durable_v2_context(iov, num_iovec, oparms);
1614 }
1615
1616 if (oparms->reconnect) {
1617 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1618
1619 oparms->reconnect = false;
1620 } else
1621 iov[num].iov_base = create_durable_buf();
1622 if (iov[num].iov_base == NULL)
1623 return -ENOMEM;
1624 iov[num].iov_len = sizeof(struct create_durable);
1625 if (!req->CreateContextsOffset)
1626 req->CreateContextsOffset =
1627 cpu_to_le32(sizeof(struct smb2_create_req) +
1628 iov[1].iov_len);
1629 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1630 *num_iovec = num + 1;
1631 return 0;
1632}
1633
1634static int
1635alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1636 const char *treename, const __le16 *path)
1637{
1638 int treename_len, path_len;
1639 struct nls_table *cp;
1640 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1641
1642
1643
1644
1645 treename_len = strlen(treename);
1646 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1647 return -EINVAL;
1648
1649 treename += 2;
1650 treename_len -= 2;
1651
1652 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1653
1654
1655
1656
1657
1658 *out_len = treename_len + 1 + path_len;
1659
1660
1661
1662
1663
1664
1665 *out_size = roundup((*out_len+1)*2, 8);
1666 *out_path = kzalloc(*out_size, GFP_KERNEL);
1667 if (!*out_path)
1668 return -ENOMEM;
1669
1670 cp = load_nls_default();
1671 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1672 UniStrcat(*out_path, sep);
1673 UniStrcat(*out_path, path);
1674 unload_nls(cp);
1675
1676 return 0;
1677}
1678
1679int
1680SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1681 __u8 *oplock, struct smb2_file_all_info *buf,
1682 struct smb2_err_rsp **err_buf)
1683{
1684 struct smb2_create_req *req;
1685 struct smb2_create_rsp *rsp;
1686 struct TCP_Server_Info *server;
1687 struct cifs_tcon *tcon = oparms->tcon;
1688 struct cifs_ses *ses = tcon->ses;
1689 struct kvec iov[4];
1690 struct kvec rsp_iov = {NULL, 0};
1691 int resp_buftype;
1692 int uni_path_len;
1693 __le16 *copy_path = NULL;
1694 int copy_size;
1695 int rc = 0;
1696 unsigned int n_iov = 2;
1697 __u32 file_attributes = 0;
1698 char *dhc_buf = NULL, *lc_buf = NULL;
1699 int flags = 0;
1700 unsigned int total_len;
1701
1702 cifs_dbg(FYI, "create/open\n");
1703
1704 if (ses && (ses->server))
1705 server = ses->server;
1706 else
1707 return -EIO;
1708
1709 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1710
1711 if (rc)
1712 return rc;
1713
1714 if (encryption_required(tcon))
1715 flags |= CIFS_TRANSFORM_REQ;
1716
1717 if (oparms->create_options & CREATE_OPTION_READONLY)
1718 file_attributes |= ATTR_READONLY;
1719 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1720 file_attributes |= ATTR_SYSTEM;
1721
1722 req->ImpersonationLevel = IL_IMPERSONATION;
1723 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1724
1725 req->FileAttributes = cpu_to_le32(file_attributes);
1726 req->ShareAccess = FILE_SHARE_ALL_LE;
1727 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1728 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1729
1730 iov[0].iov_base = (char *)req;
1731
1732 iov[0].iov_len = total_len - 1;
1733
1734 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1745 int name_len;
1746
1747 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1748 rc = alloc_path_with_tree_prefix(©_path, ©_size,
1749 &name_len,
1750 tcon->treeName, path);
1751 if (rc)
1752 return rc;
1753 req->NameLength = cpu_to_le16(name_len * 2);
1754 uni_path_len = copy_size;
1755 path = copy_path;
1756 } else {
1757 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1758
1759 req->NameLength = cpu_to_le16(uni_path_len - 2);
1760 if (uni_path_len % 8 != 0) {
1761 copy_size = roundup(uni_path_len, 8);
1762 copy_path = kzalloc(copy_size, GFP_KERNEL);
1763 if (!copy_path)
1764 return -ENOMEM;
1765 memcpy((char *)copy_path, (const char *)path,
1766 uni_path_len);
1767 uni_path_len = copy_size;
1768 path = copy_path;
1769 }
1770 }
1771
1772 iov[1].iov_len = uni_path_len;
1773 iov[1].iov_base = path;
1774
1775 if (!server->oplocks)
1776 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1777
1778 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1779 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1780 req->RequestedOplockLevel = *oplock;
1781 else {
1782 rc = add_lease_context(server, iov, &n_iov,
1783 oparms->fid->lease_key, oplock);
1784 if (rc) {
1785 cifs_small_buf_release(req);
1786 kfree(copy_path);
1787 return rc;
1788 }
1789 lc_buf = iov[n_iov-1].iov_base;
1790 }
1791
1792 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1793
1794 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1795 struct create_context *ccontext =
1796 (struct create_context *)iov[n_iov-1].iov_base;
1797 ccontext->Next =
1798 cpu_to_le32(server->vals->create_lease_size);
1799 }
1800
1801 rc = add_durable_context(iov, &n_iov, oparms,
1802 tcon->use_persistent);
1803 if (rc) {
1804 cifs_small_buf_release(req);
1805 kfree(copy_path);
1806 kfree(lc_buf);
1807 return rc;
1808 }
1809 dhc_buf = iov[n_iov-1].iov_base;
1810 }
1811
1812 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1813 &rsp_iov);
1814 cifs_small_buf_release(req);
1815 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
1816
1817 if (rc != 0) {
1818 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1819 if (err_buf && rsp)
1820 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1821 GFP_KERNEL);
1822 goto creat_exit;
1823 }
1824
1825 oparms->fid->persistent_fid = rsp->PersistentFileId;
1826 oparms->fid->volatile_fid = rsp->VolatileFileId;
1827
1828 if (buf) {
1829 memcpy(buf, &rsp->CreationTime, 32);
1830 buf->AllocationSize = rsp->AllocationSize;
1831 buf->EndOfFile = rsp->EndofFile;
1832 buf->Attributes = rsp->FileAttributes;
1833 buf->NumberOfLinks = cpu_to_le32(1);
1834 buf->DeletePending = 0;
1835 }
1836
1837 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1838 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
1839 oparms->fid->lease_key);
1840 else
1841 *oplock = rsp->OplockLevel;
1842creat_exit:
1843 kfree(copy_path);
1844 kfree(lc_buf);
1845 kfree(dhc_buf);
1846 free_rsp_buf(resp_buftype, rsp);
1847 return rc;
1848}
1849
1850
1851
1852
1853int
1854SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1855 u64 volatile_fid, u32 opcode, bool is_fsctl,
1856 char *in_data, u32 indatalen,
1857 char **out_data, u32 *plen )
1858{
1859 struct smb2_ioctl_req *req;
1860 struct smb2_ioctl_rsp *rsp;
1861 struct smb2_sync_hdr *shdr;
1862 struct cifs_ses *ses;
1863 struct kvec iov[2];
1864 struct kvec rsp_iov;
1865 int resp_buftype;
1866 int n_iov;
1867 int rc = 0;
1868 int flags = 0;
1869 unsigned int total_len;
1870
1871 cifs_dbg(FYI, "SMB2 IOCTL\n");
1872
1873 if (out_data != NULL)
1874 *out_data = NULL;
1875
1876
1877 if (plen)
1878 *plen = 0;
1879
1880 if (tcon)
1881 ses = tcon->ses;
1882 else
1883 return -EIO;
1884
1885 if (!ses || !(ses->server))
1886 return -EIO;
1887
1888 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
1889 if (rc)
1890 return rc;
1891
1892 if (encryption_required(tcon))
1893 flags |= CIFS_TRANSFORM_REQ;
1894
1895 req->CtlCode = cpu_to_le32(opcode);
1896 req->PersistentFileId = persistent_fid;
1897 req->VolatileFileId = volatile_fid;
1898
1899 if (indatalen) {
1900 req->InputCount = cpu_to_le32(indatalen);
1901
1902 req->InputOffset =
1903 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
1904 iov[1].iov_base = in_data;
1905 iov[1].iov_len = indatalen;
1906 n_iov = 2;
1907 } else
1908 n_iov = 1;
1909
1910 req->OutputOffset = 0;
1911 req->OutputCount = 0;
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
1924
1925 if (is_fsctl)
1926 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1927 else
1928 req->Flags = 0;
1929
1930 iov[0].iov_base = (char *)req;
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941 if (indatalen) {
1942 iov[0].iov_len = total_len - 1;
1943 } else
1944 iov[0].iov_len = total_len;
1945
1946
1947 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1948 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1949
1950 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1951 &rsp_iov);
1952 cifs_small_buf_release(req);
1953 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
1954
1955 if ((rc != 0) && (rc != -EINVAL)) {
1956 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1957 goto ioctl_exit;
1958 } else if (rc == -EINVAL) {
1959 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1960 (opcode != FSCTL_SRV_COPYCHUNK)) {
1961 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1962 goto ioctl_exit;
1963 }
1964 }
1965
1966
1967 if ((plen == NULL) || (out_data == NULL))
1968 goto ioctl_exit;
1969
1970 *plen = le32_to_cpu(rsp->OutputCount);
1971
1972
1973 if (*plen == 0)
1974 goto ioctl_exit;
1975 else if (*plen > 0xFF00) {
1976 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1977 *plen = 0;
1978 rc = -EIO;
1979 goto ioctl_exit;
1980 }
1981
1982 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1983 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1984 le32_to_cpu(rsp->OutputOffset));
1985 *plen = 0;
1986 rc = -EIO;
1987 goto ioctl_exit;
1988 }
1989
1990 *out_data = kmalloc(*plen, GFP_KERNEL);
1991 if (*out_data == NULL) {
1992 rc = -ENOMEM;
1993 goto ioctl_exit;
1994 }
1995
1996 shdr = get_sync_hdr(rsp);
1997 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
1998ioctl_exit:
1999 free_rsp_buf(resp_buftype, rsp);
2000 return rc;
2001}
2002
2003
2004
2005
2006
2007int
2008SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2009 u64 persistent_fid, u64 volatile_fid)
2010{
2011 int rc;
2012 struct compress_ioctl fsctl_input;
2013 char *ret_data = NULL;
2014
2015 fsctl_input.CompressionState =
2016 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2017
2018 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2019 FSCTL_SET_COMPRESSION, true ,
2020 (char *)&fsctl_input ,
2021 2 , &ret_data , NULL);
2022
2023 cifs_dbg(FYI, "set compression rc %d\n", rc);
2024
2025 return rc;
2026}
2027
2028int
2029SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2030 u64 persistent_fid, u64 volatile_fid)
2031{
2032 struct smb2_close_req *req;
2033 struct smb2_close_rsp *rsp;
2034 struct cifs_ses *ses = tcon->ses;
2035 struct kvec iov[1];
2036 struct kvec rsp_iov;
2037 int resp_buftype;
2038 int rc = 0;
2039 int flags = 0;
2040 unsigned int total_len;
2041
2042 cifs_dbg(FYI, "Close\n");
2043
2044 if (!ses || !(ses->server))
2045 return -EIO;
2046
2047 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2048 if (rc)
2049 return rc;
2050
2051 if (encryption_required(tcon))
2052 flags |= CIFS_TRANSFORM_REQ;
2053
2054 req->PersistentFileId = persistent_fid;
2055 req->VolatileFileId = volatile_fid;
2056
2057 iov[0].iov_base = (char *)req;
2058 iov[0].iov_len = total_len;
2059
2060 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2061 cifs_small_buf_release(req);
2062 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2063
2064 if (rc != 0) {
2065 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2066 goto close_exit;
2067 }
2068
2069
2070
2071close_exit:
2072 free_rsp_buf(resp_buftype, rsp);
2073 return rc;
2074}
2075
2076static int
2077validate_buf(unsigned int offset, unsigned int buffer_length,
2078 struct smb2_hdr *hdr, unsigned int min_buf_size)
2079
2080{
2081 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2082 char *end_of_smb = smb_len + 4 + (char *)hdr;
2083 char *begin_of_buf = 4 + offset + (char *)hdr;
2084 char *end_of_buf = begin_of_buf + buffer_length;
2085
2086
2087 if (buffer_length < min_buf_size) {
2088 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2089 buffer_length, min_buf_size);
2090 return -EINVAL;
2091 }
2092
2093
2094 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2095 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2096 buffer_length, smb_len);
2097 return -EINVAL;
2098 }
2099
2100 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2101 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2102 return -EINVAL;
2103 }
2104
2105 return 0;
2106}
2107
2108
2109
2110
2111
2112static int
2113validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2114 struct smb2_hdr *hdr, unsigned int minbufsize,
2115 char *data)
2116
2117{
2118 char *begin_of_buf = 4 + offset + (char *)hdr;
2119 int rc;
2120
2121 if (!data)
2122 return -EINVAL;
2123
2124 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2125 if (rc)
2126 return rc;
2127
2128 memcpy(data, begin_of_buf, buffer_length);
2129
2130 return 0;
2131}
2132
2133static int
2134query_info(const unsigned int xid, struct cifs_tcon *tcon,
2135 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2136 u32 additional_info, size_t output_len, size_t min_len, void **data,
2137 u32 *dlen)
2138{
2139 struct smb2_query_info_req *req;
2140 struct smb2_query_info_rsp *rsp = NULL;
2141 struct kvec iov[2];
2142 struct kvec rsp_iov;
2143 int rc = 0;
2144 int resp_buftype;
2145 struct cifs_ses *ses = tcon->ses;
2146 int flags = 0;
2147 unsigned int total_len;
2148
2149 cifs_dbg(FYI, "Query Info\n");
2150
2151 if (!ses || !(ses->server))
2152 return -EIO;
2153
2154 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2155 &total_len);
2156 if (rc)
2157 return rc;
2158
2159 if (encryption_required(tcon))
2160 flags |= CIFS_TRANSFORM_REQ;
2161
2162 req->InfoType = info_type;
2163 req->FileInfoClass = info_class;
2164 req->PersistentFileId = persistent_fid;
2165 req->VolatileFileId = volatile_fid;
2166 req->AdditionalInformation = cpu_to_le32(additional_info);
2167
2168
2169
2170
2171 req->InputBufferOffset = 0;
2172
2173 req->OutputBufferLength = cpu_to_le32(output_len);
2174
2175 iov[0].iov_base = (char *)req;
2176
2177 iov[0].iov_len = total_len - 1;
2178
2179 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2180 cifs_small_buf_release(req);
2181 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2182
2183 if (rc) {
2184 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2185 goto qinf_exit;
2186 }
2187
2188 if (dlen) {
2189 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2190 if (!*data) {
2191 *data = kmalloc(*dlen, GFP_KERNEL);
2192 if (!*data) {
2193 cifs_dbg(VFS,
2194 "Error %d allocating memory for acl\n",
2195 rc);
2196 *dlen = 0;
2197 goto qinf_exit;
2198 }
2199 }
2200 }
2201
2202 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2203 le32_to_cpu(rsp->OutputBufferLength),
2204 &rsp->hdr, min_len, *data);
2205
2206qinf_exit:
2207 free_rsp_buf(resp_buftype, rsp);
2208 return rc;
2209}
2210
2211int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
2212 u64 persistent_fid, u64 volatile_fid,
2213 int ea_buf_size, struct smb2_file_full_ea_info *data)
2214{
2215 return query_info(xid, tcon, persistent_fid, volatile_fid,
2216 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
2217 ea_buf_size,
2218 sizeof(struct smb2_file_full_ea_info),
2219 (void **)&data,
2220 NULL);
2221}
2222
2223int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2224 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2225{
2226 return query_info(xid, tcon, persistent_fid, volatile_fid,
2227 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2228 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2229 sizeof(struct smb2_file_all_info), (void **)&data,
2230 NULL);
2231}
2232
2233int
2234SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2235 u64 persistent_fid, u64 volatile_fid,
2236 void **data, u32 *plen)
2237{
2238 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2239 *plen = 0;
2240
2241 return query_info(xid, tcon, persistent_fid, volatile_fid,
2242 0, SMB2_O_INFO_SECURITY, additional_info,
2243 SMB2_MAX_BUFFER_SIZE,
2244 sizeof(struct smb2_file_all_info), data, plen);
2245}
2246
2247int
2248SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2249 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2250{
2251 return query_info(xid, tcon, persistent_fid, volatile_fid,
2252 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2253 sizeof(struct smb2_file_internal_info),
2254 sizeof(struct smb2_file_internal_info),
2255 (void **)&uniqueid, NULL);
2256}
2257
2258
2259
2260
2261
2262
2263
2264
2265static void
2266smb2_echo_callback(struct mid_q_entry *mid)
2267{
2268 struct TCP_Server_Info *server = mid->callback_data;
2269 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2270 unsigned int credits_received = 1;
2271
2272 if (mid->mid_state == MID_RESPONSE_RECEIVED)
2273 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2274
2275 DeleteMidQEntry(mid);
2276 add_credits(server, credits_received, CIFS_ECHO_OP);
2277}
2278
2279void smb2_reconnect_server(struct work_struct *work)
2280{
2281 struct TCP_Server_Info *server = container_of(work,
2282 struct TCP_Server_Info, reconnect.work);
2283 struct cifs_ses *ses;
2284 struct cifs_tcon *tcon, *tcon2;
2285 struct list_head tmp_list;
2286 int tcon_exist = false;
2287 int rc;
2288 int resched = false;
2289
2290
2291
2292 mutex_lock(&server->reconnect_mutex);
2293
2294 INIT_LIST_HEAD(&tmp_list);
2295 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2296
2297 spin_lock(&cifs_tcp_ses_lock);
2298 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2299 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2300 if (tcon->need_reconnect || tcon->need_reopen_files) {
2301 tcon->tc_count++;
2302 list_add_tail(&tcon->rlist, &tmp_list);
2303 tcon_exist = true;
2304 }
2305 }
2306 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2307 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2308 tcon_exist = true;
2309 ses->ses_count++;
2310 }
2311 }
2312
2313
2314
2315
2316 if (tcon_exist)
2317 server->srv_count++;
2318
2319 spin_unlock(&cifs_tcp_ses_lock);
2320
2321 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2322 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2323 if (!rc)
2324 cifs_reopen_persistent_handles(tcon);
2325 else
2326 resched = true;
2327 list_del_init(&tcon->rlist);
2328 if (tcon->ipc)
2329 cifs_put_smb_ses(tcon->ses);
2330 cifs_put_tcon(tcon);
2331 }
2332
2333 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2334 if (resched)
2335 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2336 mutex_unlock(&server->reconnect_mutex);
2337
2338
2339 if (tcon_exist)
2340 cifs_put_tcp_session(server, 1);
2341}
2342
2343int
2344SMB2_echo(struct TCP_Server_Info *server)
2345{
2346 struct smb2_echo_req *req;
2347 int rc = 0;
2348 struct kvec iov[2];
2349 struct smb_rqst rqst = { .rq_iov = iov,
2350 .rq_nvec = 2 };
2351 unsigned int total_len;
2352 __be32 rfc1002_marker;
2353
2354 cifs_dbg(FYI, "In echo request\n");
2355
2356 if (server->tcpStatus == CifsNeedNegotiate) {
2357
2358 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2359 return rc;
2360 }
2361
2362 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
2363 if (rc)
2364 return rc;
2365
2366 req->sync_hdr.CreditRequest = cpu_to_le16(1);
2367
2368 iov[0].iov_len = 4;
2369 rfc1002_marker = cpu_to_be32(total_len);
2370 iov[0].iov_base = &rfc1002_marker;
2371 iov[1].iov_len = total_len;
2372 iov[1].iov_base = (char *)req;
2373
2374 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2375 server, CIFS_ECHO_OP);
2376 if (rc)
2377 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2378
2379 cifs_small_buf_release(req);
2380 return rc;
2381}
2382
2383int
2384SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2385 u64 volatile_fid)
2386{
2387 struct smb2_flush_req *req;
2388 struct cifs_ses *ses = tcon->ses;
2389 struct kvec iov[1];
2390 struct kvec rsp_iov;
2391 int resp_buftype;
2392 int rc = 0;
2393 int flags = 0;
2394 unsigned int total_len;
2395
2396 cifs_dbg(FYI, "Flush\n");
2397
2398 if (!ses || !(ses->server))
2399 return -EIO;
2400
2401 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
2402 if (rc)
2403 return rc;
2404
2405 if (encryption_required(tcon))
2406 flags |= CIFS_TRANSFORM_REQ;
2407
2408 req->PersistentFileId = persistent_fid;
2409 req->VolatileFileId = volatile_fid;
2410
2411 iov[0].iov_base = (char *)req;
2412 iov[0].iov_len = total_len;
2413
2414 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2415 cifs_small_buf_release(req);
2416
2417 if (rc != 0)
2418 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2419
2420 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2421 return rc;
2422}
2423
2424
2425
2426
2427
2428static int
2429smb2_new_read_req(void **buf, unsigned int *total_len,
2430 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2431 int request_type)
2432{
2433 int rc = -EACCES;
2434 struct smb2_read_plain_req *req = NULL;
2435 struct smb2_sync_hdr *shdr;
2436
2437 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2438 total_len);
2439 if (rc)
2440 return rc;
2441 if (io_parms->tcon->ses->server == NULL)
2442 return -ECONNABORTED;
2443
2444 shdr = &req->sync_hdr;
2445 shdr->ProcessId = cpu_to_le32(io_parms->pid);
2446
2447 req->PersistentFileId = io_parms->persistent_fid;
2448 req->VolatileFileId = io_parms->volatile_fid;
2449 req->ReadChannelInfoOffset = 0;
2450 req->ReadChannelInfoLength = 0;
2451 req->Channel = 0;
2452 req->MinimumCount = 0;
2453 req->Length = cpu_to_le32(io_parms->length);
2454 req->Offset = cpu_to_le64(io_parms->offset);
2455
2456 if (request_type & CHAINED_REQUEST) {
2457 if (!(request_type & END_OF_CHAIN)) {
2458
2459 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2460 shdr->NextCommand = cpu_to_le32(*total_len);
2461 } else
2462 shdr->NextCommand = 0;
2463 if (request_type & RELATED_REQUEST) {
2464 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2465
2466
2467
2468
2469 shdr->SessionId = 0xFFFFFFFF;
2470 shdr->TreeId = 0xFFFFFFFF;
2471 req->PersistentFileId = 0xFFFFFFFF;
2472 req->VolatileFileId = 0xFFFFFFFF;
2473 }
2474 }
2475 if (remaining_bytes > io_parms->length)
2476 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2477 else
2478 req->RemainingBytes = 0;
2479
2480 *buf = req;
2481 return rc;
2482}
2483
2484static void
2485smb2_readv_callback(struct mid_q_entry *mid)
2486{
2487 struct cifs_readdata *rdata = mid->callback_data;
2488 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2489 struct TCP_Server_Info *server = tcon->ses->server;
2490 struct smb2_sync_hdr *shdr =
2491 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
2492 unsigned int credits_received = 1;
2493 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2494 .rq_nvec = 2,
2495 .rq_pages = rdata->pages,
2496 .rq_npages = rdata->nr_pages,
2497 .rq_pagesz = rdata->pagesz,
2498 .rq_tailsz = rdata->tailsz };
2499
2500 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2501 __func__, mid->mid, mid->mid_state, rdata->result,
2502 rdata->bytes);
2503
2504 switch (mid->mid_state) {
2505 case MID_RESPONSE_RECEIVED:
2506 credits_received = le16_to_cpu(shdr->CreditRequest);
2507
2508 if (server->sign && !mid->decrypted) {
2509 int rc;
2510
2511 rc = smb2_verify_signature(&rqst, server);
2512 if (rc)
2513 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2514 rc);
2515 }
2516
2517 task_io_account_read(rdata->got_bytes);
2518 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2519 break;
2520 case MID_REQUEST_SUBMITTED:
2521 case MID_RETRY_NEEDED:
2522 rdata->result = -EAGAIN;
2523 if (server->sign && rdata->got_bytes)
2524
2525 rdata->got_bytes = 0;
2526
2527 task_io_account_read(rdata->got_bytes);
2528 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2529 break;
2530 default:
2531 if (rdata->result != -ENODATA)
2532 rdata->result = -EIO;
2533 }
2534
2535 if (rdata->result)
2536 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2537
2538 queue_work(cifsiod_wq, &rdata->work);
2539 DeleteMidQEntry(mid);
2540 add_credits(server, credits_received, 0);
2541}
2542
2543
2544int
2545smb2_async_readv(struct cifs_readdata *rdata)
2546{
2547 int rc, flags = 0;
2548 char *buf;
2549 struct smb2_sync_hdr *shdr;
2550 struct cifs_io_parms io_parms;
2551 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2552 .rq_nvec = 2 };
2553 struct TCP_Server_Info *server;
2554 unsigned int total_len;
2555 __be32 req_len;
2556
2557 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2558 __func__, rdata->offset, rdata->bytes);
2559
2560 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2561 io_parms.offset = rdata->offset;
2562 io_parms.length = rdata->bytes;
2563 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2564 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2565 io_parms.pid = rdata->pid;
2566
2567 server = io_parms.tcon->ses->server;
2568
2569 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
2570 if (rc) {
2571 if (rc == -EAGAIN && rdata->credits) {
2572
2573 rdata->credits = 0;
2574
2575 spin_lock(&server->req_lock);
2576 server->in_flight--;
2577 spin_unlock(&server->req_lock);
2578 }
2579 return rc;
2580 }
2581
2582 if (encryption_required(io_parms.tcon))
2583 flags |= CIFS_TRANSFORM_REQ;
2584
2585 req_len = cpu_to_be32(total_len);
2586
2587 rdata->iov[0].iov_base = &req_len;
2588 rdata->iov[0].iov_len = sizeof(__be32);
2589 rdata->iov[1].iov_base = buf;
2590 rdata->iov[1].iov_len = total_len;
2591
2592 shdr = (struct smb2_sync_hdr *)buf;
2593
2594 if (rdata->credits) {
2595 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2596 SMB2_MAX_BUFFER_SIZE));
2597 shdr->CreditRequest = shdr->CreditCharge;
2598 spin_lock(&server->req_lock);
2599 server->credits += rdata->credits -
2600 le16_to_cpu(shdr->CreditCharge);
2601 spin_unlock(&server->req_lock);
2602 wake_up(&server->request_q);
2603 flags |= CIFS_HAS_CREDITS;
2604 }
2605
2606 kref_get(&rdata->refcount);
2607 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2608 cifs_readv_receive, smb2_readv_callback,
2609 smb3_handle_read_data, rdata, flags);
2610 if (rc) {
2611 kref_put(&rdata->refcount, cifs_readdata_release);
2612 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2613 }
2614
2615 cifs_small_buf_release(buf);
2616 return rc;
2617}
2618
2619int
2620SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2621 unsigned int *nbytes, char **buf, int *buf_type)
2622{
2623 int resp_buftype, rc = -EACCES;
2624 struct smb2_read_plain_req *req = NULL;
2625 struct smb2_read_rsp *rsp = NULL;
2626 struct smb2_sync_hdr *shdr;
2627 struct kvec iov[1];
2628 struct kvec rsp_iov;
2629 unsigned int total_len;
2630 int flags = CIFS_LOG_ERROR;
2631 struct cifs_ses *ses = io_parms->tcon->ses;
2632
2633 *nbytes = 0;
2634 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
2635 if (rc)
2636 return rc;
2637
2638 if (encryption_required(io_parms->tcon))
2639 flags |= CIFS_TRANSFORM_REQ;
2640
2641 iov[0].iov_base = (char *)req;
2642 iov[0].iov_len = total_len;
2643
2644 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2645 cifs_small_buf_release(req);
2646
2647 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
2648
2649 if (rc) {
2650 if (rc != -ENODATA) {
2651 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2652 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2653 }
2654 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2655 return rc == -ENODATA ? 0 : rc;
2656 }
2657
2658 *nbytes = le32_to_cpu(rsp->DataLength);
2659 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2660 (*nbytes > io_parms->length)) {
2661 cifs_dbg(FYI, "bad length %d for count %d\n",
2662 *nbytes, io_parms->length);
2663 rc = -EIO;
2664 *nbytes = 0;
2665 }
2666
2667 shdr = get_sync_hdr(rsp);
2668
2669 if (*buf) {
2670 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
2671 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2672 } else if (resp_buftype != CIFS_NO_BUFFER) {
2673 *buf = rsp_iov.iov_base;
2674 if (resp_buftype == CIFS_SMALL_BUFFER)
2675 *buf_type = CIFS_SMALL_BUFFER;
2676 else if (resp_buftype == CIFS_LARGE_BUFFER)
2677 *buf_type = CIFS_LARGE_BUFFER;
2678 }
2679 return rc;
2680}
2681
2682
2683
2684
2685
2686static void
2687smb2_writev_callback(struct mid_q_entry *mid)
2688{
2689 struct cifs_writedata *wdata = mid->callback_data;
2690 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2691 unsigned int written;
2692 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2693 unsigned int credits_received = 1;
2694
2695 switch (mid->mid_state) {
2696 case MID_RESPONSE_RECEIVED:
2697 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2698 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2699 if (wdata->result != 0)
2700 break;
2701
2702 written = le32_to_cpu(rsp->DataLength);
2703
2704
2705
2706
2707
2708
2709 if (written > wdata->bytes)
2710 written &= 0xFFFF;
2711
2712 if (written < wdata->bytes)
2713 wdata->result = -ENOSPC;
2714 else
2715 wdata->bytes = written;
2716 break;
2717 case MID_REQUEST_SUBMITTED:
2718 case MID_RETRY_NEEDED:
2719 wdata->result = -EAGAIN;
2720 break;
2721 default:
2722 wdata->result = -EIO;
2723 break;
2724 }
2725
2726 if (wdata->result)
2727 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2728
2729 queue_work(cifsiod_wq, &wdata->work);
2730 DeleteMidQEntry(mid);
2731 add_credits(tcon->ses->server, credits_received, 0);
2732}
2733
2734
2735int
2736smb2_async_writev(struct cifs_writedata *wdata,
2737 void (*release)(struct kref *kref))
2738{
2739 int rc = -EACCES, flags = 0;
2740 struct smb2_write_req *req = NULL;
2741 struct smb2_sync_hdr *shdr;
2742 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2743 struct TCP_Server_Info *server = tcon->ses->server;
2744 struct kvec iov[2];
2745 struct smb_rqst rqst = { };
2746 unsigned int total_len;
2747 __be32 rfc1002_marker;
2748
2749 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
2750 if (rc) {
2751 if (rc == -EAGAIN && wdata->credits) {
2752
2753 wdata->credits = 0;
2754
2755 spin_lock(&server->req_lock);
2756 server->in_flight--;
2757 spin_unlock(&server->req_lock);
2758 }
2759 goto async_writev_out;
2760 }
2761
2762 if (encryption_required(tcon))
2763 flags |= CIFS_TRANSFORM_REQ;
2764
2765 shdr = (struct smb2_sync_hdr *)req;
2766 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
2767
2768 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2769 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2770 req->WriteChannelInfoOffset = 0;
2771 req->WriteChannelInfoLength = 0;
2772 req->Channel = 0;
2773 req->Offset = cpu_to_le64(wdata->offset);
2774 req->DataOffset = cpu_to_le16(
2775 offsetof(struct smb2_write_req, Buffer));
2776 req->RemainingBytes = 0;
2777
2778
2779 iov[0].iov_len = 4;
2780 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
2781 iov[0].iov_base = &rfc1002_marker;
2782 iov[1].iov_len = total_len - 1;
2783 iov[1].iov_base = (char *)req;
2784
2785 rqst.rq_iov = iov;
2786 rqst.rq_nvec = 2;
2787 rqst.rq_pages = wdata->pages;
2788 rqst.rq_npages = wdata->nr_pages;
2789 rqst.rq_pagesz = wdata->pagesz;
2790 rqst.rq_tailsz = wdata->tailsz;
2791
2792 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2793 wdata->offset, wdata->bytes);
2794
2795 req->Length = cpu_to_le32(wdata->bytes);
2796
2797 if (wdata->credits) {
2798 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2799 SMB2_MAX_BUFFER_SIZE));
2800 shdr->CreditRequest = shdr->CreditCharge;
2801 spin_lock(&server->req_lock);
2802 server->credits += wdata->credits -
2803 le16_to_cpu(shdr->CreditCharge);
2804 spin_unlock(&server->req_lock);
2805 wake_up(&server->request_q);
2806 flags |= CIFS_HAS_CREDITS;
2807 }
2808
2809 kref_get(&wdata->refcount);
2810 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2811 wdata, flags);
2812
2813 if (rc) {
2814 kref_put(&wdata->refcount, release);
2815 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2816 }
2817
2818async_writev_out:
2819 cifs_small_buf_release(req);
2820 return rc;
2821}
2822
2823
2824
2825
2826
2827
2828
2829int
2830SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2831 unsigned int *nbytes, struct kvec *iov, int n_vec)
2832{
2833 int rc = 0;
2834 struct smb2_write_req *req = NULL;
2835 struct smb2_write_rsp *rsp = NULL;
2836 int resp_buftype;
2837 struct kvec rsp_iov;
2838 int flags = 0;
2839 unsigned int total_len;
2840
2841 *nbytes = 0;
2842
2843 if (n_vec < 1)
2844 return rc;
2845
2846 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
2847 &total_len);
2848 if (rc)
2849 return rc;
2850
2851 if (io_parms->tcon->ses->server == NULL)
2852 return -ECONNABORTED;
2853
2854 if (encryption_required(io_parms->tcon))
2855 flags |= CIFS_TRANSFORM_REQ;
2856
2857 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
2858
2859 req->PersistentFileId = io_parms->persistent_fid;
2860 req->VolatileFileId = io_parms->volatile_fid;
2861 req->WriteChannelInfoOffset = 0;
2862 req->WriteChannelInfoLength = 0;
2863 req->Channel = 0;
2864 req->Length = cpu_to_le32(io_parms->length);
2865 req->Offset = cpu_to_le64(io_parms->offset);
2866 req->DataOffset = cpu_to_le16(
2867 offsetof(struct smb2_write_req, Buffer));
2868 req->RemainingBytes = 0;
2869
2870 iov[0].iov_base = (char *)req;
2871
2872 iov[0].iov_len = total_len - 1;
2873
2874 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
2875 &resp_buftype, flags, &rsp_iov);
2876 cifs_small_buf_release(req);
2877 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
2878
2879 if (rc) {
2880 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2881 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2882 } else
2883 *nbytes = le32_to_cpu(rsp->DataLength);
2884
2885 free_rsp_buf(resp_buftype, rsp);
2886 return rc;
2887}
2888
2889static unsigned int
2890num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2891{
2892 int len;
2893 unsigned int entrycount = 0;
2894 unsigned int next_offset = 0;
2895 FILE_DIRECTORY_INFO *entryptr;
2896
2897 if (bufstart == NULL)
2898 return 0;
2899
2900 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2901
2902 while (1) {
2903 entryptr = (FILE_DIRECTORY_INFO *)
2904 ((char *)entryptr + next_offset);
2905
2906 if ((char *)entryptr + size > end_of_buf) {
2907 cifs_dbg(VFS, "malformed search entry would overflow\n");
2908 break;
2909 }
2910
2911 len = le32_to_cpu(entryptr->FileNameLength);
2912 if ((char *)entryptr + len + size > end_of_buf) {
2913 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2914 end_of_buf);
2915 break;
2916 }
2917
2918 *lastentry = (char *)entryptr;
2919 entrycount++;
2920
2921 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2922 if (!next_offset)
2923 break;
2924 }
2925
2926 return entrycount;
2927}
2928
2929
2930
2931
2932int
2933SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2934 u64 persistent_fid, u64 volatile_fid, int index,
2935 struct cifs_search_info *srch_inf)
2936{
2937 struct smb2_query_directory_req *req;
2938 struct smb2_query_directory_rsp *rsp = NULL;
2939 struct kvec iov[2];
2940 struct kvec rsp_iov;
2941 int rc = 0;
2942 int len;
2943 int resp_buftype = CIFS_NO_BUFFER;
2944 unsigned char *bufptr;
2945 struct TCP_Server_Info *server;
2946 struct cifs_ses *ses = tcon->ses;
2947 __le16 asteriks = cpu_to_le16('*');
2948 char *end_of_smb;
2949 unsigned int output_size = CIFSMaxBufSize;
2950 size_t info_buf_size;
2951 int flags = 0;
2952 unsigned int total_len;
2953
2954 if (ses && (ses->server))
2955 server = ses->server;
2956 else
2957 return -EIO;
2958
2959 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
2960 &total_len);
2961 if (rc)
2962 return rc;
2963
2964 if (encryption_required(tcon))
2965 flags |= CIFS_TRANSFORM_REQ;
2966
2967 switch (srch_inf->info_level) {
2968 case SMB_FIND_FILE_DIRECTORY_INFO:
2969 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2970 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2971 break;
2972 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2973 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2974 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2975 break;
2976 default:
2977 cifs_dbg(VFS, "info level %u isn't supported\n",
2978 srch_inf->info_level);
2979 rc = -EINVAL;
2980 goto qdir_exit;
2981 }
2982
2983 req->FileIndex = cpu_to_le32(index);
2984 req->PersistentFileId = persistent_fid;
2985 req->VolatileFileId = volatile_fid;
2986
2987 len = 0x2;
2988 bufptr = req->Buffer;
2989 memcpy(bufptr, &asteriks, len);
2990
2991 req->FileNameOffset =
2992 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
2993 req->FileNameLength = cpu_to_le16(len);
2994
2995
2996
2997
2998 output_size = min_t(unsigned int, output_size, server->maxBuf);
2999 output_size = min_t(unsigned int, output_size, 2 << 15);
3000 req->OutputBufferLength = cpu_to_le32(output_size);
3001
3002 iov[0].iov_base = (char *)req;
3003
3004 iov[0].iov_len = total_len - 1;
3005
3006 iov[1].iov_base = (char *)(req->Buffer);
3007 iov[1].iov_len = len;
3008
3009 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
3010 cifs_small_buf_release(req);
3011 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3012
3013 if (rc) {
3014 if (rc == -ENODATA &&
3015 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
3016 srch_inf->endOfSearch = true;
3017 rc = 0;
3018 }
3019 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3020 goto qdir_exit;
3021 }
3022
3023 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3024 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3025 info_buf_size);
3026 if (rc)
3027 goto qdir_exit;
3028
3029 srch_inf->unicode = true;
3030
3031 if (srch_inf->ntwrk_buf_start) {
3032 if (srch_inf->smallBuf)
3033 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3034 else
3035 cifs_buf_release(srch_inf->ntwrk_buf_start);
3036 }
3037 srch_inf->ntwrk_buf_start = (char *)rsp;
3038 srch_inf->srch_entries_start = srch_inf->last_entry = 4 +
3039 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3040
3041 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3042 srch_inf->entries_in_buffer =
3043 num_entries(srch_inf->srch_entries_start, end_of_smb,
3044 &srch_inf->last_entry, info_buf_size);
3045 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
3046 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3047 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3048 srch_inf->srch_entries_start, srch_inf->last_entry);
3049 if (resp_buftype == CIFS_LARGE_BUFFER)
3050 srch_inf->smallBuf = false;
3051 else if (resp_buftype == CIFS_SMALL_BUFFER)
3052 srch_inf->smallBuf = true;
3053 else
3054 cifs_dbg(VFS, "illegal search buffer type\n");
3055
3056 return rc;
3057
3058qdir_exit:
3059 free_rsp_buf(resp_buftype, rsp);
3060 return rc;
3061}
3062
3063static int
3064send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3065 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3066 u8 info_type, u32 additional_info, unsigned int num,
3067 void **data, unsigned int *size)
3068{
3069 struct smb2_set_info_req *req;
3070 struct smb2_set_info_rsp *rsp = NULL;
3071 struct kvec *iov;
3072 struct kvec rsp_iov;
3073 int rc = 0;
3074 int resp_buftype;
3075 unsigned int i;
3076 struct cifs_ses *ses = tcon->ses;
3077 int flags = 0;
3078 unsigned int total_len;
3079
3080 if (!ses || !(ses->server))
3081 return -EIO;
3082
3083 if (!num)
3084 return -EINVAL;
3085
3086 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3087 if (!iov)
3088 return -ENOMEM;
3089
3090 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
3091 if (rc) {
3092 kfree(iov);
3093 return rc;
3094 }
3095
3096 if (encryption_required(tcon))
3097 flags |= CIFS_TRANSFORM_REQ;
3098
3099 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3100
3101 req->InfoType = info_type;
3102 req->FileInfoClass = info_class;
3103 req->PersistentFileId = persistent_fid;
3104 req->VolatileFileId = volatile_fid;
3105 req->AdditionalInformation = cpu_to_le32(additional_info);
3106
3107 req->BufferOffset =
3108 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
3109 req->BufferLength = cpu_to_le32(*size);
3110
3111 memcpy(req->Buffer, *data, *size);
3112 total_len += *size;
3113
3114 iov[0].iov_base = (char *)req;
3115
3116 iov[0].iov_len = total_len - 1;
3117
3118 for (i = 1; i < num; i++) {
3119 le32_add_cpu(&req->BufferLength, size[i]);
3120 iov[i].iov_base = (char *)data[i];
3121 iov[i].iov_len = size[i];
3122 }
3123
3124 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3125 &rsp_iov);
3126 cifs_buf_release(req);
3127 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
3128
3129 if (rc != 0)
3130 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
3131
3132 free_rsp_buf(resp_buftype, rsp);
3133 kfree(iov);
3134 return rc;
3135}
3136
3137int
3138SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3139 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3140{
3141 struct smb2_file_rename_info info;
3142 void **data;
3143 unsigned int size[2];
3144 int rc;
3145 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3146
3147 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3148 if (!data)
3149 return -ENOMEM;
3150
3151 info.ReplaceIfExists = 1;
3152
3153 info.RootDirectory = 0;
3154 info.FileNameLength = cpu_to_le32(len);
3155
3156 data[0] = &info;
3157 size[0] = sizeof(struct smb2_file_rename_info);
3158
3159 data[1] = target_file;
3160 size[1] = len + 2 ;
3161
3162 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3163 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3164 0, 2, data, size);
3165 kfree(data);
3166 return rc;
3167}
3168
3169int
3170SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3171 u64 persistent_fid, u64 volatile_fid)
3172{
3173 __u8 delete_pending = 1;
3174 void *data;
3175 unsigned int size;
3176
3177 data = &delete_pending;
3178 size = 1;
3179
3180 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3181 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3182 0, 1, &data, &size);
3183}
3184
3185int
3186SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3187 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3188{
3189 struct smb2_file_link_info info;
3190 void **data;
3191 unsigned int size[2];
3192 int rc;
3193 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3194
3195 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3196 if (!data)
3197 return -ENOMEM;
3198
3199 info.ReplaceIfExists = 0;
3200
3201 info.RootDirectory = 0;
3202 info.FileNameLength = cpu_to_le32(len);
3203
3204 data[0] = &info;
3205 size[0] = sizeof(struct smb2_file_link_info);
3206
3207 data[1] = target_file;
3208 size[1] = len + 2 ;
3209
3210 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3211 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3212 0, 2, data, size);
3213 kfree(data);
3214 return rc;
3215}
3216
3217int
3218SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3219 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
3220{
3221 struct smb2_file_eof_info info;
3222 void *data;
3223 unsigned int size;
3224
3225 info.EndOfFile = *eof;
3226
3227 data = &info;
3228 size = sizeof(struct smb2_file_eof_info);
3229
3230 if (is_falloc)
3231 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3232 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3233 0, 1, &data, &size);
3234 else
3235 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3236 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3237 0, 1, &data, &size);
3238}
3239
3240int
3241SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3242 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3243{
3244 unsigned int size;
3245 size = sizeof(FILE_BASIC_INFO);
3246 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3247 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3248 0, 1, (void **)&buf, &size);
3249}
3250
3251int
3252SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3253 u64 persistent_fid, u64 volatile_fid,
3254 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3255{
3256 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3257 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3258 1, (void **)&pnntsd, &pacllen);
3259}
3260
3261int
3262SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3263 u64 persistent_fid, u64 volatile_fid,
3264 struct smb2_file_full_ea_info *buf, int len)
3265{
3266 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3267 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3268 0, 1, (void **)&buf, &len);
3269}
3270
3271int
3272SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3273 const u64 persistent_fid, const u64 volatile_fid,
3274 __u8 oplock_level)
3275{
3276 int rc;
3277 struct smb2_oplock_break_req *req = NULL;
3278 struct cifs_ses *ses = tcon->ses;
3279 int flags = CIFS_OBREAK_OP;
3280 unsigned int total_len;
3281 struct kvec iov[1];
3282 struct kvec rsp_iov;
3283 int resp_buf_type;
3284
3285 cifs_dbg(FYI, "SMB2_oplock_break\n");
3286 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3287 &total_len);
3288 if (rc)
3289 return rc;
3290
3291 if (encryption_required(tcon))
3292 flags |= CIFS_TRANSFORM_REQ;
3293
3294 req->VolatileFid = volatile_fid;
3295 req->PersistentFid = persistent_fid;
3296 req->OplockLevel = oplock_level;
3297 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3298
3299 flags |= CIFS_NO_RESP;
3300
3301 iov[0].iov_base = (char *)req;
3302 iov[0].iov_len = total_len;
3303
3304 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
3305 cifs_small_buf_release(req);
3306
3307 if (rc) {
3308 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3309 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
3310 }
3311
3312 return rc;
3313}
3314
3315static void
3316copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3317 struct kstatfs *kst)
3318{
3319 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3320 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3321 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3322 kst->f_bfree = kst->f_bavail =
3323 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3324 return;
3325}
3326
3327static int
3328build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3329 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3330{
3331 int rc;
3332 struct smb2_query_info_req *req;
3333 unsigned int total_len;
3334
3335 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
3336
3337 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3338 return -EIO;
3339
3340 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3341 &total_len);
3342 if (rc)
3343 return rc;
3344
3345 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3346 req->FileInfoClass = level;
3347 req->PersistentFileId = persistent_fid;
3348 req->VolatileFileId = volatile_fid;
3349
3350 req->InputBufferOffset =
3351 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
3352 req->OutputBufferLength = cpu_to_le32(
3353 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3354
3355 iov->iov_base = (char *)req;
3356 iov->iov_len = total_len;
3357 return 0;
3358}
3359
3360int
3361SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3362 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3363{
3364 struct smb2_query_info_rsp *rsp = NULL;
3365 struct kvec iov;
3366 struct kvec rsp_iov;
3367 int rc = 0;
3368 int resp_buftype;
3369 struct cifs_ses *ses = tcon->ses;
3370 struct smb2_fs_full_size_info *info = NULL;
3371 int flags = 0;
3372
3373 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3374 sizeof(struct smb2_fs_full_size_info),
3375 persistent_fid, volatile_fid);
3376 if (rc)
3377 return rc;
3378
3379 if (encryption_required(tcon))
3380 flags |= CIFS_TRANSFORM_REQ;
3381
3382 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3383 cifs_small_buf_release(iov.iov_base);
3384 if (rc) {
3385 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3386 goto qfsinf_exit;
3387 }
3388 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3389
3390 info = (struct smb2_fs_full_size_info *)(4 +
3391 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3392 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3393 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3394 sizeof(struct smb2_fs_full_size_info));
3395 if (!rc)
3396 copy_fs_info_to_kstatfs(info, fsdata);
3397
3398qfsinf_exit:
3399 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3400 return rc;
3401}
3402
3403int
3404SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3405 u64 persistent_fid, u64 volatile_fid, int level)
3406{
3407 struct smb2_query_info_rsp *rsp = NULL;
3408 struct kvec iov;
3409 struct kvec rsp_iov;
3410 int rc = 0;
3411 int resp_buftype, max_len, min_len;
3412 struct cifs_ses *ses = tcon->ses;
3413 unsigned int rsp_len, offset;
3414 int flags = 0;
3415
3416 if (level == FS_DEVICE_INFORMATION) {
3417 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3418 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3419 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3420 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3421 min_len = MIN_FS_ATTR_INFO_SIZE;
3422 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3423 max_len = sizeof(struct smb3_fs_ss_info);
3424 min_len = sizeof(struct smb3_fs_ss_info);
3425 } else {
3426 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3427 return -EINVAL;
3428 }
3429
3430 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3431 persistent_fid, volatile_fid);
3432 if (rc)
3433 return rc;
3434
3435 if (encryption_required(tcon))
3436 flags |= CIFS_TRANSFORM_REQ;
3437
3438 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3439 cifs_small_buf_release(iov.iov_base);
3440 if (rc) {
3441 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3442 goto qfsattr_exit;
3443 }
3444 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3445
3446 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3447 offset = le16_to_cpu(rsp->OutputBufferOffset);
3448 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3449 if (rc)
3450 goto qfsattr_exit;
3451
3452 if (level == FS_ATTRIBUTE_INFORMATION)
3453 memcpy(&tcon->fsAttrInfo, 4 + offset
3454 + (char *)&rsp->hdr, min_t(unsigned int,
3455 rsp_len, max_len));
3456 else if (level == FS_DEVICE_INFORMATION)
3457 memcpy(&tcon->fsDevInfo, 4 + offset
3458 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3459 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3460 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3461 (4 + offset + (char *)&rsp->hdr);
3462 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3463 tcon->perf_sector_size =
3464 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3465 }
3466
3467qfsattr_exit:
3468 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3469 return rc;
3470}
3471
3472int
3473smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3474 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3475 const __u32 num_lock, struct smb2_lock_element *buf)
3476{
3477 int rc = 0;
3478 struct smb2_lock_req *req = NULL;
3479 struct kvec iov[2];
3480 struct kvec rsp_iov;
3481 int resp_buf_type;
3482 unsigned int count;
3483 int flags = CIFS_NO_RESP;
3484 unsigned int total_len;
3485
3486 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3487
3488 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
3489 if (rc)
3490 return rc;
3491
3492 if (encryption_required(tcon))
3493 flags |= CIFS_TRANSFORM_REQ;
3494
3495 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3496 req->LockCount = cpu_to_le16(num_lock);
3497
3498 req->PersistentFileId = persist_fid;
3499 req->VolatileFileId = volatile_fid;
3500
3501 count = num_lock * sizeof(struct smb2_lock_element);
3502
3503 iov[0].iov_base = (char *)req;
3504 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
3505 iov[1].iov_base = (char *)buf;
3506 iov[1].iov_len = count;
3507
3508 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3509 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3510 &rsp_iov);
3511 cifs_small_buf_release(req);
3512 if (rc) {
3513 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3514 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3515 }
3516
3517 return rc;
3518}
3519
3520int
3521SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3522 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3523 const __u64 length, const __u64 offset, const __u32 lock_flags,
3524 const bool wait)
3525{
3526 struct smb2_lock_element lock;
3527
3528 lock.Offset = cpu_to_le64(offset);
3529 lock.Length = cpu_to_le64(length);
3530 lock.Flags = cpu_to_le32(lock_flags);
3531 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3532 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3533
3534 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3535}
3536
3537int
3538SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3539 __u8 *lease_key, const __le32 lease_state)
3540{
3541 int rc;
3542 struct smb2_lease_ack *req = NULL;
3543 struct cifs_ses *ses = tcon->ses;
3544 int flags = CIFS_OBREAK_OP;
3545 unsigned int total_len;
3546 struct kvec iov[1];
3547 struct kvec rsp_iov;
3548 int resp_buf_type;
3549
3550 cifs_dbg(FYI, "SMB2_lease_break\n");
3551 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3552 &total_len);
3553 if (rc)
3554 return rc;
3555
3556 if (encryption_required(tcon))
3557 flags |= CIFS_TRANSFORM_REQ;
3558
3559 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3560 req->StructureSize = cpu_to_le16(36);
3561 total_len += 12;
3562
3563 memcpy(req->LeaseKey, lease_key, 16);
3564 req->LeaseState = lease_state;
3565
3566 flags |= CIFS_NO_RESP;
3567
3568 iov[0].iov_base = (char *)req;
3569 iov[0].iov_len = total_len;
3570
3571 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
3572 cifs_small_buf_release(req);
3573
3574 if (rc) {
3575 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3576 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
3577 }
3578
3579 return rc;
3580}
3581