-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iscsi-pdu.cpp
1190 lines (957 loc) · 38.4 KB
/
iscsi-pdu.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cassert>
#include <cstring>
#include <vector>
#include "iscsi.h"
#include "iscsi-pdu.h"
#include "log.h"
#include "random.h"
#include "scsi.h"
#include "utils.h"
#if defined(ARDUINO)
constexpr int max_msg_depth = 1;
#else
constexpr int max_msg_depth = 128;
#endif
std::optional<std::string> pdu_opcode_to_string(const iscsi_pdu_bhs::iscsi_bhs_opcode opcode)
{
switch(opcode) {
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_nop_out: return "I: NOP-Out";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_scsi_cmd: return "I: SCSI Command (encapsulates a SCSI Command Descriptor Block)";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_scsi_taskman: return "I: SCSI Task Management function request";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_login_req: return "I: Login Request";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_text_req: return "I: Text Request";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_scsi_data_out: return "I: SCSI Data-Out (for WRITE operations)";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_logout_req: return "I: Logout Request";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_snack_req: return "I: SNACK Request";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_nop_in: return "T: NOP-In";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_scsi_resp: return "T: SCSI Response - contains SCSI status and possibly sense information or other response information.";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_scsi_taskmanr: return "T: SCSI Task Management function response";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_login_resp: return "T: Login Response";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_text_resp: return "T: Text Response";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_scsi_data_in: return "T: SCSI Data-In - for READ operations";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_logout_resp: return "T: Logout Response";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_r2t: return "T: Ready To Transfer (R2T) - sent by target when it is ready to receive data";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_async_msg: return "T: Asynchronous Message - sent by target to indicate certain special conditions";
case iscsi_pdu_bhs::iscsi_bhs_opcode::o_reject: return "T: Reject";
}
return { };
}
/*--------------------------------------------------------------------------*/
// BHS
iscsi_pdu_bhs::iscsi_pdu_bhs(session *const ses): ses(ses)
{
assert(sizeof(*bhs) == 48);
*bhs = { };
}
iscsi_pdu_bhs::~iscsi_pdu_bhs()
{
for(auto & ahs : ahs_list)
delete ahs;
delete [] data.first;
}
std::vector<blob_t> iscsi_pdu_bhs::get_helper(const void *const header, const uint8_t *const data, const size_t data_len, const bool allow_digest) const
{
size_t header_size = sizeof(__bhs__);
assert(header_size == 48);
size_t out_size = header_size;
std::optional<uint32_t> header_digest;
size_t header_digest_offset = header_size;
assert(header_digest_offset == 48);
size_t data_offset = 0;
size_t data_digest_offset = 0;
size_t data_padded_length = 0;
size_t digest_length = sizeof(uint32_t);
if (ses->get_header_digest() && allow_digest) {
uint32_t crc32 = crc32_0x11EDC6F41(reinterpret_cast<const uint8_t *>(header), header_size, { }).first;
out_size += digest_length;
header_digest = crc32;
}
data_offset = out_size;
data_padded_length = (data_len + 3) & ~3;
out_size += data_padded_length;
data_digest_offset = out_size;
if (ses->get_data_digest() && allow_digest && data_len > 0)
out_size += digest_length;
uint8_t *out = new uint8_t[out_size];
memcpy(&out[0], header, header_size);
if (header_digest.has_value())
memcpy(&out[header_digest_offset], &header_digest.value(), digest_length);
if (data_len > 0) {
if (ses->get_data_digest() && allow_digest) {
memset(&out[out_size] - (4 /* data padding */ + digest_length), 0x00, 4); // make sure padding is 0x00
memcpy(&out[data_offset], data, data_len);
uint32_t data_digest = crc32_0x11EDC6F41(&out[data_offset], data_padded_length, { }).first;
memcpy(&out[data_digest_offset], &data_digest, digest_length);
}
else {
memset(&out[out_size - 4 /* data padding */], 0x00, 4);
memcpy(&out[data_offset], data, data_len);
}
}
std::vector<blob_t> out_vector;
out_vector.push_back({ out, out_size });
return out_vector;
}
bool iscsi_pdu_bhs::set(const uint8_t *const in, const size_t n)
{
if (n != 48)
return false;
memcpy(pdu_bytes, in, n);
// TODO validate against session
return true;
}
bool iscsi_pdu_bhs::set_ahs_segment(std::pair<const uint8_t *, size_t> ahs_in)
{
size_t offset = 0;
while(offset + 3 < ahs_in.second) {
size_t ahs_length = (ahs_in.first[offset + 0] << 8) | ahs_in.first[offset + 1];
if (offset + 3 + ahs_length > ahs_in.second)
break;
if (ahs_length > 0) {
iscsi_pdu_ahs *instance = new iscsi_pdu_ahs();
if (instance->set(&ahs_in.first[offset], ahs_length) == false) {
delete instance;
break;
}
ahs_list.push_back(instance);
}
}
return offset == ahs_in.second;
}
bool iscsi_pdu_bhs::set_data(const std::pair<const uint8_t *, size_t> & data_in)
{
if (data_in.second == 0 || data_in.second > 16777215)
return false;
delete [] data.first;
data.second = data_in.second;
data.first = duplicate_new(data_in.first, data_in.second);
return true;
}
std::optional<std::pair<const uint8_t *, size_t> > iscsi_pdu_bhs::get_data() const
{
if (data.second == 0)
return { };
return { { data.first, data.second } };
}
std::vector<blob_t> iscsi_pdu_bhs::get() const
{
return get_helper(bhs, nullptr, 0);
}
std::optional<iscsi_response_set> iscsi_pdu_bhs::get_response(scsi *const sd)
{
DOLOG(logging::ll_debug, "iscsi_pdu_bhs::get_response", ses->get_endpoint_name(), "invoked!");
assert(0);
return { };
}
blob_t iscsi_pdu_bhs::get_raw() const
{
size_t copy_len = sizeof pdu_bytes;
assert(copy_len == 48);
uint8_t *copy_data = duplicate_new(pdu_bytes, copy_len);
return { copy_data, copy_len };
}
/*--------------------------------------------------------------------------*/
// AHS
iscsi_pdu_ahs::iscsi_pdu_ahs()
{
assert(sizeof(__ahs_header__) == 3);
}
iscsi_pdu_ahs::~iscsi_pdu_ahs()
{
delete ahs;
}
bool iscsi_pdu_ahs::set(const uint8_t *const in, const size_t n)
{
if (n < 3)
return false;
size_t expected_size = my_NTOHS(reinterpret_cast<const __ahs_header__ *>(in)->length);
if (expected_size + 3 != n)
return false;
ahs = reinterpret_cast<__ahs_header__ *>(new uint8_t[expected_size + 3]());
memcpy(ahs, in, n);
return true;
}
blob_t iscsi_pdu_ahs::get()
{
uint16_t expected_size = my_NTOHS(reinterpret_cast<const __ahs_header__ *>(ahs)->length);
uint32_t out_size = sizeof(__ahs_header__) + expected_size;
uint8_t *out = duplicate_new(ahs, out_size);
return { out, out_size };
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_login_request::iscsi_pdu_login_request(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*login_req) == 48);
*login_req = { };
}
bool has_CRC32C(const std::string & value)
{
auto parts = split(value, ",");
for(const auto & part: parts) {
if (part == "CRC32C")
return true;
}
return false;
}
bool iscsi_pdu_login_request::set_data(const std::pair<const uint8_t *, size_t> & data_in)
{
if (iscsi_pdu_bhs::set_data(data_in) == false) {
DOLOG(logging::ll_warning, "iscsi_pdu_login_request::set_data", ses->get_endpoint_name(), "iscsi_pdu_bhs::set_data returned false");
return false;
}
auto kvs_in = data_to_text_array(data.first, data.second);
uint32_t max_burst = ~0;
uint32_t max_seg_len = ses->get_max_seg_len();
for(const auto & kv: kvs_in) {
DOLOG(logging::ll_debug, "iscsi_pdu_login_request::set_data", ses->get_endpoint_name(), "kv %s", kv.c_str());
auto parts = split(kv, "=");
if (parts.size() < 2)
continue;
if (parts[0] == "MaxBurstLength")
max_burst = std::min(max_burst, uint32_t(std::stoi(parts[1])));
else if (parts[0] == "FirstBurstLength")
max_burst = std::min(max_burst, uint32_t(std::stoi(parts[1])));
else if (parts[0] == "MaxRecvDataSegmentLength")
max_seg_len = std::min(max_seg_len, uint32_t(std::stoi(parts[1])));
else if (parts[0] == "InitiatorName")
initiator = parts[1];
else if (parts[0] == "HeaderDigest")
ses->set_header_digest(has_CRC32C(parts[1]));
else if (parts[0] == "DataDigest")
ses->set_data_digest(has_CRC32C(parts[1]));
}
ses->set_max_seg_len(max_seg_len);
if (max_burst < uint32_t(~0)) {
DOLOG(logging::ll_debug, "iscsi_pdu_login_request::set_data", ses->get_endpoint_name(), "set max-burst to %u", max_burst);
ses->set_ack_interval(max_burst);
}
return true;
}
std::vector<blob_t> iscsi_pdu_login_request::get() const
{
return get_helper(login_req, nullptr, 0, false);
}
std::optional<iscsi_response_set> iscsi_pdu_login_request::get_response(scsi *const sd)
{
iscsi_response_set response;
auto reply_pdu = new iscsi_pdu_login_reply(ses);
if (reply_pdu->set(*this) == false) {
delete reply_pdu;
return { };
}
response.responses.push_back(reply_pdu);
return response;
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_login_reply::iscsi_pdu_login_reply(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*login_reply) == 48);
}
iscsi_pdu_login_reply::~iscsi_pdu_login_reply()
{
delete [] login_reply_reply_data.first;
}
bool iscsi_pdu_login_reply::set(const iscsi_pdu_login_request & reply_to)
{
bool discovery = reply_to.get_NSG() == 1;
if (discovery) {
DOLOG(logging::ll_debug, "iscsi_pdu_login_reply::set", ses->get_endpoint_name(), "discovery mode, CSG %d, NSG %d", reply_to.get_CSG(), reply_to.get_NSG());
const std::vector<std::string> kvs {
"TargetPortalGroupTag=1",
"AuthMethod=None",
"ErrorRecoveryLevel=0",
};
for(const auto & kv : kvs)
DOLOG(logging::ll_debug, "iscsi_pdu_login_reply::set", ses->get_endpoint_name(), "send KV \"%s\"", kv.c_str());
auto temp = text_array_to_data(kvs);
login_reply_reply_data.first = temp.first;
login_reply_reply_data.second = temp.second;
}
else {
DOLOG(logging::ll_debug, "iscsi_pdu_login_reply::set", ses->get_endpoint_name(), "login mode, CSG %d, NSG %d", reply_to.get_CSG(), reply_to.get_NSG());
const std::vector<std::string> kvs {
ses->get_header_digest() ? "HeaderDigest=CRC32C" : "HeaderDigest=None",
ses->get_data_digest () ? "DataDigest=CRC32C" : "DataDigest=None",
"DefaultTime2Wait=2",
"DefaultTime2Retain=20",
"InitialR2T=Yes",
myformat("FirstBurstLength=%d", MAX_DATA_SEGMENT_SIZE),
myformat("MaxBurstLength=%d", MAX_DATA_SEGMENT_SIZE),
myformat("MaxRecvDataSegmentLength=%d", MAX_DATA_SEGMENT_SIZE),
};
for(const auto & kv : kvs)
DOLOG(logging::ll_debug, "iscsi_pdu_login_reply::set", ses->get_endpoint_name(), "send KV \"%s\"", kv.c_str());
auto temp = text_array_to_data(kvs);
login_reply_reply_data.first = temp.first;
login_reply_reply_data.second = temp.second;
}
*login_reply = { };
set_bits(&login_reply->b1, 7, 1, false); // filler 1
set_bits(&login_reply->b1, 6, 1, false); // filler 0
set_bits(&login_reply->b1, 0, 6, o_login_resp); // opcode
set_bits(&login_reply->b2, 7, 1, true); // T
set_bits(&login_reply->b2, 6, 1, false); // C
set_bits(&login_reply->b2, 2, 2, reply_to.get_CSG()); // CSG
set_bits(&login_reply->b2, 0, 2, reply_to.get_NSG()); // NSG
login_reply->versionmax = reply_to.get_versionmin();
login_reply->versionact = reply_to.get_versionmin();
login_reply->datalenH = login_reply_reply_data.second >> 16;
login_reply->datalenM = login_reply_reply_data.second >> 8;
login_reply->datalenL = login_reply_reply_data.second ;
memcpy(login_reply->ISID, reply_to.get_ISID(), 6);
if (reply_to.get_NSG() == 3) {
while(login_reply->TSIH == 0) {
if (my_getrandom(&login_reply->TSIH, sizeof login_reply->TSIH) == false) {
DOLOG(logging::ll_error, "iscsi_pdu_login_reply::set", ses->get_endpoint_name(), "random generator returned an error");
return false;
}
}
}
login_reply->Itasktag = reply_to.get_Itasktag();
login_reply->StatSN = my_HTONL(reply_to.get_CSG() == 0 ? 0 : 1);
login_reply->ExpCmdSN = my_HTONL(reply_to.get_CmdSN());
login_reply->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + 1);
return true;
}
std::vector<blob_t> iscsi_pdu_login_reply::get() const
{
return get_helper(login_reply, login_reply_reply_data.first, login_reply_reply_data.second, false);
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_scsi_cmd::iscsi_pdu_scsi_cmd(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*cdb_pdu_req) == 48);
assert(offsetof(__cdb_pdu_req__, Itasktag ) == 16);
assert(offsetof(__cdb_pdu_req__, expdatlen) == 20);
assert(offsetof(__cdb_pdu_req__, CmdSN ) == 24);
}
bool iscsi_pdu_scsi_cmd::set(const uint8_t *const in, const size_t n)
{
if (iscsi_pdu_bhs::set(in, n) == false) {
DOLOG(logging::ll_info, "iscsi_pdu_scsi_cmd::set", ses->get_endpoint_name(), "iscsi_pdu_bhs::set returned error state");
return false;
}
// TODO further validation
return true;
}
std::vector<blob_t> iscsi_pdu_scsi_cmd::get() const
{
return get_helper(cdb_pdu_req, nullptr, 0);
}
std::optional<iscsi_response_set> iscsi_pdu_scsi_cmd::get_response(scsi *const sd, const uint8_t status)
{
// TODO: handle errors (see 'status')
iscsi_response_set response;
auto *pdu_scsi_response = new iscsi_pdu_scsi_response(ses) /* 0x21 */;
if (pdu_scsi_response->set(*this, { }, { }, { }) == false) {
DOLOG(logging::ll_info, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "iscsi_pdu_scsi_response::set returned error");
return { };
}
response.responses.push_back(pdu_scsi_response);
return response;
}
std::optional<iscsi_response_set> iscsi_pdu_scsi_cmd::get_response(scsi *const sd)
{
const uint64_t lun = get_LUN_nr();
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "working on ITT %08x for LUN %" PRIu64, get_Itasktag(), lun);
uint64_t iscsi_expected = get_ExpDatLen();
auto scsi_reply = sd->send(ses->get_io_stats(), lun, get_CDB(), 16, data);
if (scsi_reply.has_value() == false) {
DOLOG(logging::ll_warning, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "scsi::send returned nothing");
return { };
}
iscsi_response_set response;
bool ok = true;
if (scsi_reply.value().io.is_inline) {
auto pdu_data_in = new iscsi_pdu_scsi_data_in(ses); // 0x25
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "sending SCSI DATA-IN with %zu payload bytes, is meta: %d", scsi_reply.value().io.what.data.second, scsi_reply.value().data_is_meta);
if (pdu_data_in->set(*this, scsi_reply.value().io.what.data, scsi_reply.value().data_is_meta) == false) {
ok = false;
DOLOG(logging::ll_error, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "iscsi_pdu_scsi_data_in::set returned error state");
}
response.responses.push_back(pdu_data_in);
delete [] scsi_reply.value().io.what.data.first;
}
else {
assert(scsi_reply.value().io.what.data.first == nullptr);
}
iscsi_pdu_bhs *pdu_scsi_response = nullptr;
if (scsi_reply.value().type == ir_as_is || scsi_reply.value().type == ir_empty_sense) {
if (scsi_reply.value().sense_data.empty() == false || scsi_reply.value().type == ir_empty_sense) {
auto *temp = new iscsi_pdu_scsi_response(ses) /* 0x21 */;
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "sending SCSI response with %zu sense bytes", scsi_reply.value().sense_data.size());
std::optional<uint8_t> iscsi_status;
auto & sense_data = scsi_reply.value().sense_data;
if (sense_data == sd->error_reservation_conflict_1())
iscsi_status = 0x18; // RESERVATION CONFLICT
if (temp->set(*this, sense_data, { }, iscsi_status) == false) {
ok = false;
DOLOG(logging::ll_info, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "iscsi_pdu_scsi_response::set returned error");
}
pdu_scsi_response = temp;
}
}
else if (scsi_reply.value().type == ir_r2t) {
assert(scsi_reply.value().amount_of_data_expected.has_value());
uint64_t scsi_expected = scsi_reply.value().amount_of_data_expected.value();
bool r2t_would_under_or_overflow = scsi_expected != iscsi_expected;
if (r2t_would_under_or_overflow) {
auto *temp = new iscsi_pdu_scsi_response(ses) /* 0x21 */;
std::pair<residual, uint32_t> residual_state { };
if (scsi_expected < iscsi_expected)
residual_state = { iSR_UNDERFLOW, iscsi_expected - scsi_expected };
else if (scsi_expected > iscsi_expected)
residual_state = { iSR_OVERFLOW, scsi_expected - iscsi_expected };
else
assert(0);
if (temp->set(*this, { }, residual_state, { }) == false) {
ok = false;
DOLOG(logging::ll_info, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "iscsi_pdu_scsi_response::set returned error");
}
pdu_scsi_response = temp;
}
else {
auto *temp = new iscsi_pdu_scsi_r2t(ses) /* 0x31 */;
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "sending R2T with %zu sense bytes", scsi_reply.value().sense_data.size());
uint32_t TTT = 0;
uint32_t ITT = get_Itasktag();
if (ITT != 0xffffffff) {
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "has an ITT: %08x", ITT);
TTT = ITT;
}
else if (my_getrandom(&TTT) == false) {
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "my_getrandom failed");
ok = false;
}
else {
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "TTT is %08x", TTT);
}
ses->init_r2t_session(scsi_reply.value().r2t, scsi_reply.value().r2t.fua, this, TTT);
if (temp->set(*this, TTT, scsi_reply.value().r2t.bytes_done, scsi_reply.value().r2t.bytes_left) == false) {
ok = false;
DOLOG(logging::ll_info, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "iscsi_pdu_scsi_response::set returned error");
}
pdu_scsi_response = temp;
}
}
if (pdu_scsi_response)
response.responses.push_back(pdu_scsi_response);
if (!ok) {
for(auto & r: response.responses)
delete r;
DOLOG(logging::ll_info, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "failed");
return { };
}
if (scsi_reply.value().io.is_inline == false && scsi_reply.value().io.what.location.n_sectors > 0) {
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_cmd::get_response", ses->get_endpoint_name(), "queing stream");
response.to_stream = scsi_reply.value().io.what.location;
}
return response;
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_scsi_response::iscsi_pdu_scsi_response(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*pdu_response) == 48);
}
iscsi_pdu_scsi_response::~iscsi_pdu_scsi_response()
{
delete [] pdu_response_data.first;
}
bool iscsi_pdu_scsi_response::set(const iscsi_pdu_scsi_cmd & reply_to, const std::vector<uint8_t> & scsi_sense_data, std::optional<std::pair<residual, uint32_t> > has_residual, const std::optional<uint8_t> iscsi_status)
{
size_t sense_data_size = scsi_sense_data.size();
size_t reply_data_plus_sense_header = sense_data_size > 0 ? 2 + sense_data_size : 0;
*pdu_response = { };
set_bits(&pdu_response->b1, 0, 6, o_scsi_resp); // 0x21
set_bits(&pdu_response->b1, 6, 1, false);
set_bits(&pdu_response->b1, 7, 1, false);
set_bits(&pdu_response->b2, 7, 1, true);
pdu_response->datalenH = reply_data_plus_sense_header >> 16;
pdu_response->datalenM = reply_data_plus_sense_header >> 8;
pdu_response->datalenL = reply_data_plus_sense_header ;
pdu_response->Itasktag = reply_to.get_Itasktag();
pdu_response->StatSN = my_HTONL(reply_to.get_ExpStatSN());
pdu_response->ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1);
pdu_response->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth);
pdu_response->ExpDataSN = my_HTONL(0);
if (has_residual.has_value()) {
if (has_residual.value().first == iSR_UNDERFLOW)
set_bits(&pdu_response->b2, 1, 1, true); // U (residual underflow)
else if (has_residual.value().first == iSR_OVERFLOW)
set_bits(&pdu_response->b2, 2, 1, true); // O (residual overflow)
else
return false;
pdu_response->ResidualCt = my_HTONL(has_residual.value().second);
}
pdu_response_data.second = reply_data_plus_sense_header;
if (pdu_response_data.second) {
DOLOG(logging::ll_info, "iscsi_pdu_scsi_response::set", ses->get_endpoint_name(), "CHECK CONDITION");
if (iscsi_status.has_value())
pdu_response->status = iscsi_status.value();
else
pdu_response->status = 0x02; // check condition
pdu_response->response = 0x01; // target failure
pdu_response->ExpDataSN = 0;
pdu_response_data.first = new uint8_t[pdu_response_data.second]();
pdu_response_data.first[0] = sense_data_size >> 8;
pdu_response_data.first[1] = sense_data_size;
memcpy(pdu_response_data.first + 2, scsi_sense_data.data(), sense_data_size);
}
return true;
}
std::vector<blob_t> iscsi_pdu_scsi_response::get() const
{
return get_helper(pdu_response, pdu_response_data.first, pdu_response_data.second);
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_scsi_data_in::iscsi_pdu_scsi_data_in(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*pdu_data_in) == 48);
}
iscsi_pdu_scsi_data_in::~iscsi_pdu_scsi_data_in()
{
delete [] pdu_data_in_data.first;
delete reply_to_copy;
}
bool iscsi_pdu_scsi_data_in::set(const iscsi_pdu_scsi_cmd & reply_to, const std::pair<uint8_t *, size_t> scsi_reply_data, const bool has_sense)
{
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_data_in::set", ses->get_endpoint_name(), "%zu payload bytes, has_sense: %d", scsi_reply_data.second, has_sense);
auto temp = reply_to.get_raw();
reply_to_copy = new iscsi_pdu_scsi_cmd(ses);
reply_to_copy->set(temp.data, temp.n);
delete [] temp.data;
pdu_data_in_data.second = scsi_reply_data.second;
if (pdu_data_in_data.second) {
pdu_data_in_data.first = duplicate_new(scsi_reply_data.first, pdu_data_in_data.second);
}
return true;
}
std::vector<blob_t> iscsi_pdu_scsi_data_in::get() const
{
std::vector<blob_t> v_out;
// resize to limit
auto use_pdu_data_size = pdu_data_in_data.second;
size_t expdatlen = size_t(reply_to_copy->get_ExpDatLen());
if (use_pdu_data_size > expdatlen)
DOLOG(logging::ll_warning, "iscsi_pdu_scsi_data_in", ses->get_endpoint_name(), "requested less (%zu) than wat is available (%zu)", expdatlen, use_pdu_data_size);
else if (use_pdu_data_size == 0)
DOLOG(logging::ll_warning, "iscsi_pdu_scsi_data_in", ses->get_endpoint_name(), "trying to send DATA-IN without data");
use_pdu_data_size = std::min(use_pdu_data_size, expdatlen);
auto block_size = ses->get_block_size();
size_t n_to_do = (use_pdu_data_size + block_size - 1) / block_size;
for(size_t i=0, count=0; i<use_pdu_data_size; i += block_size, count++) { // 4kB blocks
*pdu_data_in = { };
set_bits(&pdu_data_in->b1, 0, 6, o_scsi_data_in); // 0x25
bool last_block = count == n_to_do - 1;
if (last_block) {
set_bits(&pdu_data_in->b2, 7, 1, true); // F
if (pdu_data_in_data.second < reply_to_copy->get_ExpDatLen()) {
set_bits(&pdu_data_in->b2, 1, 1, true); // U
pdu_data_in->ResidualCt = my_HTONL(reply_to_copy->get_ExpDatLen() - pdu_data_in_data.second);
}
else if (pdu_data_in_data.second > reply_to_copy->get_ExpDatLen()) {
set_bits(&pdu_data_in->b2, 2, 1, true); // O
pdu_data_in->ResidualCt = my_HTONL(pdu_data_in_data.second - reply_to_copy->get_ExpDatLen());
}
set_bits(&pdu_data_in->b2, 0, 1, true); // S
}
size_t cur_len = std::min(use_pdu_data_size - i, size_t(block_size));
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_data_in::get", ses->get_endpoint_name(), "block %zu, last_block: %d, cur_len: %zu", count, last_block, cur_len);
pdu_data_in->datalenH = cur_len >> 16;
pdu_data_in->datalenM = cur_len >> 8;
pdu_data_in->datalenL = cur_len ;
memcpy(pdu_data_in->LUN, reply_to_copy->get_LUN(), sizeof pdu_data_in->LUN);
pdu_data_in->Itasktag = reply_to_copy->get_Itasktag();
pdu_data_in->StatSN = my_HTONL(reply_to_copy->get_ExpStatSN());
pdu_data_in->ExpCmdSN = my_HTONL(reply_to_copy->get_CmdSN() + 1);
pdu_data_in->MaxCmdSN = my_HTONL(reply_to_copy->get_CmdSN() + max_msg_depth);
pdu_data_in->DataSN = my_HTONL(ses->get_inc_datasn(reply_to_copy->get_Itasktag()));
pdu_data_in->bufferoff = my_HTONL(i);
pdu_data_in->ResidualCt = my_HTONL(use_pdu_data_size - i);
auto temp_out = get_helper(pdu_data_in, pdu_data_in_data.first + i, cur_len);
v_out.push_back(temp_out.at(0));
}
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_data_in::get", ses->get_endpoint_name(), "returning %zu PDUs", v_out.size());
return v_out;
}
std::pair<blob_t, uint8_t *> iscsi_pdu_scsi_data_in::gen_data_in_pdu(session *const ses, const iscsi_pdu_scsi_cmd & reply_to, const std::optional<std::pair<residual, uint32_t> > & has_residual, const uint32_t offset_in_data, const uint32_t data_is_n_bytes, const bool is_last_block)
{
__pdu_data_in__ pdu_data_in { };
set_bits(&pdu_data_in.b1, 0, 6, o_scsi_data_in); // 0x25
if (is_last_block) {
set_bits(&pdu_data_in.b2, 7, 1, true); // F
set_bits(&pdu_data_in.b2, 0, 1, true); // S
}
pdu_data_in.datalenH = data_is_n_bytes >> 16;
pdu_data_in.datalenM = data_is_n_bytes >> 8;
pdu_data_in.datalenL = data_is_n_bytes ;
memcpy(pdu_data_in.LUN, reply_to.get_LUN(), sizeof pdu_data_in.LUN);
pdu_data_in.Itasktag = reply_to.get_Itasktag();
pdu_data_in.StatSN = my_HTONL(reply_to.get_ExpStatSN());
pdu_data_in.ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1);
pdu_data_in.MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth);
pdu_data_in.DataSN = my_HTONL(ses->get_inc_datasn(reply_to.get_Itasktag()));
pdu_data_in.bufferoff = my_HTONL(offset_in_data);
if (has_residual.has_value()) {
pdu_data_in.ResidualCt = my_HTONL(has_residual.value().second);
if (has_residual.value().first == iSR_UNDERFLOW)
set_bits(&pdu_data_in.b2, 1, 1, true); // U
else if (has_residual.value().first == iSR_OVERFLOW)
set_bits(&pdu_data_in.b2, 2, 1, true); // O
else
assert(0);
}
size_t out_size = sizeof(pdu_data_in) + data_is_n_bytes;
out_size = (out_size + 3) & ~3;
if (ses->get_header_digest())
out_size += sizeof(uint32_t);
if (ses->get_data_digest())
out_size += sizeof(uint32_t);
uint8_t *out = new (std::nothrow) uint8_t[out_size]();
uint8_t *out_data = nullptr;
if (out) {
const size_t pdu_size = sizeof pdu_data_in;
memcpy(out, &pdu_data_in, pdu_size); // data is set by caller! (to reduce memcpy's)
if (ses->get_header_digest()) {
uint32_t crc32 = crc32_0x11EDC6F41(reinterpret_cast<const uint8_t *>(&pdu_data_in), pdu_size, { }).first;
memcpy(&out[pdu_size], &crc32, sizeof crc32);
out_data = &out[pdu_size + sizeof crc32];
}
else {
out_data = &out[pdu_size];
}
}
else {
out_size = 0;
}
return { { out, out_size }, out_data };
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_scsi_data_out::iscsi_pdu_scsi_data_out(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*pdu_data_out) == 48);
}
iscsi_pdu_scsi_data_out::~iscsi_pdu_scsi_data_out()
{
delete [] pdu_data_out_data.first;
}
bool iscsi_pdu_scsi_data_out::set(const iscsi_pdu_scsi_cmd & reply_to, const std::pair<uint8_t *, size_t> scsi_reply_data)
{
DOLOG(logging::ll_debug, "iscsi_pdu_scsi_data_out::set", ses->get_endpoint_name(), "with %zu payload bytes", scsi_reply_data.second);
pdu_data_out_data.second = scsi_reply_data.second;
if (pdu_data_out_data.second) {
pdu_data_out_data.first = duplicate_new(scsi_reply_data.first, pdu_data_out_data.second);
}
return true;
}
std::vector<blob_t> iscsi_pdu_scsi_data_out::get() const
{
return { };
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_nop_out::iscsi_pdu_nop_out(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*nop_out) == 48);
}
std::optional<iscsi_response_set> iscsi_pdu_nop_out::get_response(scsi *const sd)
{
DOLOG(logging::ll_debug, "iscsi_pdu_nop_out::get_response", ses->get_endpoint_name(), "invoked");
iscsi_response_set response;
auto reply_pdu = new iscsi_pdu_nop_in(ses);
if (reply_pdu->set(*this) == false) {
delete reply_pdu;
return { };
}
response.responses.push_back(reply_pdu);
return response;
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_nop_in::iscsi_pdu_nop_in(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*nop_in) == 48);
}
bool iscsi_pdu_nop_in::set(const iscsi_pdu_nop_out & reply_to)
{
auto ping_data = reply_to.get_data();
*nop_in = { };
set_bits(&nop_in->b1, 0, 6, o_nop_in); // opcode
set_bits(&nop_in->b2, 7, 1, true); // reserved
if (ping_data.has_value()) {
nop_in->datalenH = ping_data.value().second >> 16;
nop_in->datalenM = ping_data.value().second >> 8;
nop_in->datalenL = ping_data.value().second;
set_data({ ping_data.value().first, ping_data.value().second });
}
memcpy(nop_in->LUN, reply_to.get_LUN(), sizeof nop_in->LUN);
nop_in->Itasktag = reply_to.get_Itasktag();
nop_in->TTT = reply_to.get_TTT();
nop_in->StatSN = my_HTONL(reply_to.get_ExpStatSN());
nop_in->ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1);
nop_in->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth);
return true;
}
std::vector<blob_t> iscsi_pdu_nop_in::get() const
{
return get_helper(nop_in, data.first, data.second);
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_scsi_r2t::iscsi_pdu_scsi_r2t(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*pdu_scsi_r2t) == 48);
}
iscsi_pdu_scsi_r2t::~iscsi_pdu_scsi_r2t()
{
delete [] pdu_scsi_r2t_data.first;
}
bool iscsi_pdu_scsi_r2t::set(const iscsi_pdu_scsi_cmd & reply_to, const uint32_t TTT, const uint32_t buffer_offset, const uint32_t data_length)
{
*pdu_scsi_r2t = { };
set_bits(&pdu_scsi_r2t->b1, 0, 6, o_r2t);
set_bits(&pdu_scsi_r2t->b2, 7, 1, true);
memcpy(pdu_scsi_r2t->LUN, reply_to.get_LUN(), sizeof pdu_scsi_r2t->LUN);
pdu_scsi_r2t->Itasktag = reply_to.get_Itasktag();
pdu_scsi_r2t->TTT = TTT;
pdu_scsi_r2t->StatSN = my_HTONL(reply_to.get_ExpStatSN());
pdu_scsi_r2t->ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1);
pdu_scsi_r2t->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth);
pdu_scsi_r2t->bufferoff = my_HTONL(buffer_offset);
pdu_scsi_r2t->DDTF = my_HTONL(data_length);
return true;
}
std::vector<blob_t> iscsi_pdu_scsi_r2t::get() const
{
return get_helper(pdu_scsi_r2t, nullptr, 0);
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_text_request::iscsi_pdu_text_request(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*text_req) == 48);
*text_req = { };
}
bool iscsi_pdu_text_request::set(const uint8_t *const in, const size_t n)
{
if (iscsi_pdu_bhs::set(in, n) == false)
return false;
// TODO further validation
return true;
}
std::vector<blob_t> iscsi_pdu_text_request::get() const
{
return get_helper(text_req, nullptr, 0);
}
std::optional<iscsi_response_set> iscsi_pdu_text_request::get_response(scsi *const sd)
{
iscsi_response_set response;
auto reply_pdu = new iscsi_pdu_text_reply(ses);
if (reply_pdu->set(*this, sd) == false) {
delete reply_pdu;
return { };
}
response.responses.push_back(reply_pdu);
return response;
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_text_reply::iscsi_pdu_text_reply(session *const ses): iscsi_pdu_bhs(ses)
{
assert(sizeof(*text_reply) == 48);
}
iscsi_pdu_text_reply::~iscsi_pdu_text_reply()
{
delete [] text_reply_reply_data.first;
}
bool iscsi_pdu_text_reply::set(const iscsi_pdu_text_request & reply_to, scsi *const sd)
{
const auto data = reply_to.get_data();
if (data.has_value() == false)
return false;
auto kvs_in = data_to_text_array(data.value().first, data.value().second);
bool send_targets = false;
for(const auto & kv: kvs_in) {
auto parts = split(kv, "=");
if (parts.size() < 2)
return false;
if (parts[0] == "SendTargets" && parts[1] == "All")
send_targets = true;
DOLOG(logging::ll_debug, "iscsi_pdu_text_reply::set", ses->get_endpoint_name(), "text request, responding to: %s", kv.c_str());
}
if (send_targets) {
const std::vector<std::string> kvs {
"TargetName=" + ses->get_target_name(),
"TargetAddress=" + ses->get_local_address() + ",1",
};
auto temp = text_array_to_data(kvs);
text_reply_reply_data.first = temp.first;
text_reply_reply_data.second = temp.second;
}
*text_reply = { };
set_bits(&text_reply->b1, 0, 6, o_text_resp); // opcode, 0x24
set_bits(&text_reply->b2, 7, 1, true); // F
text_reply->ahslen = 0;
text_reply->datalenH = text_reply_reply_data.second >> 16;
text_reply->datalenM = text_reply_reply_data.second >> 8;
text_reply->datalenL = text_reply_reply_data.second ;
memcpy(text_reply->LUN, reply_to.get_LUN(), sizeof text_reply->LUN);
text_reply->TTT = reply_to.get_TTT();
text_reply->Itasktag = reply_to.get_Itasktag();
text_reply->StatSN = my_HTONL(reply_to.get_ExpStatSN());
text_reply->ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1);
text_reply->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth);
return true;
}
std::vector<blob_t> iscsi_pdu_text_reply::get() const
{
return get_helper(text_reply, text_reply_reply_data.first, text_reply_reply_data.second);
}
/*--------------------------------------------------------------------------*/
iscsi_pdu_logout_request::iscsi_pdu_logout_request(session *const ses): iscsi_pdu_bhs(ses)
{