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