/
iscsi.c
2259 lines (2020 loc) · 63.3 KB
/
iscsi.c
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
/*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <byteswap.h>
#include <ipxe/vsprintf.h>
#include <ipxe/socket.h>
#include <ipxe/iobuf.h>
#include <ipxe/uri.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/scsi.h>
#include <ipxe/process.h>
#include <ipxe/uaccess.h>
#include <ipxe/tcpip.h>
#include <ipxe/settings.h>
#include <ipxe/features.h>
#include <ipxe/base16.h>
#include <ipxe/base64.h>
#include <ipxe/ibft.h>
#include <ipxe/blockdev.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/iscsi.h>
/** @file
*
* iSCSI protocol
*
*/
FEATURE ( FEATURE_PROTOCOL, "iSCSI", DHCP_EB_FEATURE_ISCSI, 1 );
/* Disambiguate the various error causes */
#define EACCES_INCORRECT_TARGET_USERNAME \
__einfo_error ( EINFO_EACCES_INCORRECT_TARGET_USERNAME )
#define EINFO_EACCES_INCORRECT_TARGET_USERNAME \
__einfo_uniqify ( EINFO_EACCES, 0x01, "Incorrect target username" )
#define EACCES_INCORRECT_TARGET_PASSWORD \
__einfo_error ( EINFO_EACCES_INCORRECT_TARGET_PASSWORD )
#define EINFO_EACCES_INCORRECT_TARGET_PASSWORD \
__einfo_uniqify ( EINFO_EACCES, 0x02, "Incorrect target password" )
#define EINVAL_ROOT_PATH_TOO_SHORT \
__einfo_error ( EINFO_EINVAL_ROOT_PATH_TOO_SHORT )
#define EINFO_EINVAL_ROOT_PATH_TOO_SHORT \
__einfo_uniqify ( EINFO_EINVAL, 0x01, "Root path too short" )
#define EINVAL_BAD_CREDENTIAL_MIX \
__einfo_error ( EINFO_EINVAL_BAD_CREDENTIAL_MIX )
#define EINFO_EINVAL_BAD_CREDENTIAL_MIX \
__einfo_uniqify ( EINFO_EINVAL, 0x02, "Bad credential mix" )
#define EINVAL_NO_ROOT_PATH \
__einfo_error ( EINFO_EINVAL_NO_ROOT_PATH )
#define EINFO_EINVAL_NO_ROOT_PATH \
__einfo_uniqify ( EINFO_EINVAL, 0x03, "No root path" )
#define EINVAL_NO_TARGET_IQN \
__einfo_error ( EINFO_EINVAL_NO_TARGET_IQN )
#define EINFO_EINVAL_NO_TARGET_IQN \
__einfo_uniqify ( EINFO_EINVAL, 0x04, "No target IQN" )
#define EINVAL_NO_INITIATOR_IQN \
__einfo_error ( EINFO_EINVAL_NO_INITIATOR_IQN )
#define EINFO_EINVAL_NO_INITIATOR_IQN \
__einfo_uniqify ( EINFO_EINVAL, 0x05, "No initiator IQN" )
#define EINVAL_MAXBURSTLENGTH \
__einfo_error ( EINFO_EINVAL_MAXBURSTLENGTH )
#define EINFO_EINVAL_MAXBURSTLENGTH \
__einfo_uniqify ( EINFO_EINVAL, 0x06, "Invalid MaxBurstLength" )
#define EIO_TARGET_UNAVAILABLE \
__einfo_error ( EINFO_EIO_TARGET_UNAVAILABLE )
#define EINFO_EIO_TARGET_UNAVAILABLE \
__einfo_uniqify ( EINFO_EIO, 0x01, "Target not currently operational" )
#define EIO_TARGET_NO_RESOURCES \
__einfo_error ( EINFO_EIO_TARGET_NO_RESOURCES )
#define EINFO_EIO_TARGET_NO_RESOURCES \
__einfo_uniqify ( EINFO_EIO, 0x02, "Target out of resources" )
#define ENOTSUP_INITIATOR_STATUS \
__einfo_error ( EINFO_ENOTSUP_INITIATOR_STATUS )
#define EINFO_ENOTSUP_INITIATOR_STATUS \
__einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported initiator status" )
#define ENOTSUP_OPCODE \
__einfo_error ( EINFO_ENOTSUP_OPCODE )
#define EINFO_ENOTSUP_OPCODE \
__einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported opcode" )
#define ENOTSUP_DISCOVERY \
__einfo_error ( EINFO_ENOTSUP_DISCOVERY )
#define EINFO_ENOTSUP_DISCOVERY \
__einfo_uniqify ( EINFO_ENOTSUP, 0x03, "Discovery not supported" )
#define ENOTSUP_TARGET_STATUS \
__einfo_error ( EINFO_ENOTSUP_TARGET_STATUS )
#define EINFO_ENOTSUP_TARGET_STATUS \
__einfo_uniqify ( EINFO_ENOTSUP, 0x04, "Unsupported target status" )
#define EPERM_INITIATOR_AUTHENTICATION \
__einfo_error ( EINFO_EPERM_INITIATOR_AUTHENTICATION )
#define EINFO_EPERM_INITIATOR_AUTHENTICATION \
__einfo_uniqify ( EINFO_EPERM, 0x01, "Initiator authentication failed" )
#define EPERM_INITIATOR_AUTHORISATION \
__einfo_error ( EINFO_EPERM_INITIATOR_AUTHORISATION )
#define EINFO_EPERM_INITIATOR_AUTHORISATION \
__einfo_uniqify ( EINFO_EPERM, 0x02, "Initiator not authorised" )
#define EPROTO_INVALID_CHAP_ALGORITHM \
__einfo_error ( EINFO_EPROTO_INVALID_CHAP_ALGORITHM )
#define EINFO_EPROTO_INVALID_CHAP_ALGORITHM \
__einfo_uniqify ( EINFO_EPROTO, 0x01, "Invalid CHAP algorithm" )
#define EPROTO_INVALID_CHAP_IDENTIFIER \
__einfo_error ( EINFO_EPROTO_INVALID_CHAP_IDENTIFIER )
#define EINFO_EPROTO_INVALID_CHAP_IDENTIFIER \
__einfo_uniqify ( EINFO_EPROTO, 0x02, "Invalid CHAP identifier" )
#define EPROTO_INVALID_LARGE_BINARY \
__einfo_error ( EINFO_EPROTO_INVALID_LARGE_BINARY )
#define EINFO_EPROTO_INVALID_LARGE_BINARY \
__einfo_uniqify ( EINFO_EPROTO, 0x03, "Invalid large binary value" )
#define EPROTO_INVALID_CHAP_RESPONSE \
__einfo_error ( EINFO_EPROTO_INVALID_CHAP_RESPONSE )
#define EINFO_EPROTO_INVALID_CHAP_RESPONSE \
__einfo_uniqify ( EINFO_EPROTO, 0x04, "Invalid CHAP response" )
#define EPROTO_INVALID_KEY_VALUE_PAIR \
__einfo_error ( EINFO_EPROTO_INVALID_KEY_VALUE_PAIR )
#define EINFO_EPROTO_INVALID_KEY_VALUE_PAIR \
__einfo_uniqify ( EINFO_EPROTO, 0x05, "Invalid key/value pair" )
#define EPROTO_VALUE_REJECTED \
__einfo_error ( EINFO_EPROTO_VALUE_REJECTED )
#define EINFO_EPROTO_VALUE_REJECTED \
__einfo_uniqify ( EINFO_EPROTO, 0x06, "Parameter rejected" )
static void iscsi_start_tx ( struct iscsi_session *iscsi );
static void iscsi_start_login ( struct iscsi_session *iscsi );
static void iscsi_start_data_out ( struct iscsi_session *iscsi,
unsigned int datasn );
/**
* Finish receiving PDU data into buffer
*
* @v iscsi iSCSI session
*/
static void iscsi_rx_buffered_data_done ( struct iscsi_session *iscsi ) {
free ( iscsi->rx_buffer );
iscsi->rx_buffer = NULL;
}
/**
* Receive PDU data into buffer
*
* @v iscsi iSCSI session
* @v data Data to receive
* @v len Length of data
* @ret rc Return status code
*
* This can be used when the RX PDU type handler wishes to buffer up
* all received data and process the PDU as a single unit. The caller
* is repsonsible for calling iscsi_rx_buffered_data_done() after
* processing the data.
*/
static int iscsi_rx_buffered_data ( struct iscsi_session *iscsi,
const void *data, size_t len ) {
/* Allocate buffer on first call */
if ( ! iscsi->rx_buffer ) {
iscsi->rx_buffer = malloc ( iscsi->rx_len );
if ( ! iscsi->rx_buffer )
return -ENOMEM;
}
/* Copy data to buffer */
assert ( ( iscsi->rx_offset + len ) <= iscsi->rx_len );
memcpy ( ( iscsi->rx_buffer + iscsi->rx_offset ), data, len );
return 0;
}
/**
* Free iSCSI session
*
* @v refcnt Reference counter
*/
static void iscsi_free ( struct refcnt *refcnt ) {
struct iscsi_session *iscsi =
container_of ( refcnt, struct iscsi_session, refcnt );
free ( iscsi->initiator_iqn );
free ( iscsi->target_address );
free ( iscsi->target_iqn );
free ( iscsi->initiator_username );
free ( iscsi->initiator_password );
free ( iscsi->target_username );
free ( iscsi->target_password );
chap_finish ( &iscsi->chap );
iscsi_rx_buffered_data_done ( iscsi );
free ( iscsi->command );
free ( iscsi );
}
/**
* Shut down iSCSI interface
*
* @v iscsi iSCSI session
* @v rc Reason for close
*/
static void iscsi_close ( struct iscsi_session *iscsi, int rc ) {
/* A TCP graceful close is still an error from our point of view */
if ( rc == 0 )
rc = -ECONNRESET;
DBGC ( iscsi, "iSCSI %p closed: %s\n", iscsi, strerror ( rc ) );
/* Stop transmission process */
process_del ( &iscsi->process );
/* Shut down interfaces */
intfs_shutdown ( rc, &iscsi->socket, &iscsi->control, &iscsi->data,
NULL );
}
/**
* Assign new iSCSI initiator task tag
*
* @v iscsi iSCSI session
*/
static void iscsi_new_itt ( struct iscsi_session *iscsi ) {
static uint16_t itt_idx;
iscsi->itt = ( ISCSI_TAG_MAGIC | (++itt_idx) );
}
/**
* Open iSCSI transport-layer connection
*
* @v iscsi iSCSI session
* @ret rc Return status code
*/
static int iscsi_open_connection ( struct iscsi_session *iscsi ) {
struct sockaddr_tcpip target;
int rc;
assert ( iscsi->tx_state == ISCSI_TX_IDLE );
assert ( iscsi->rx_state == ISCSI_RX_BHS );
assert ( iscsi->rx_offset == 0 );
/* Open socket */
memset ( &target, 0, sizeof ( target ) );
target.st_port = htons ( iscsi->target_port );
if ( ( rc = xfer_open_named_socket ( &iscsi->socket, SOCK_STREAM,
( struct sockaddr * ) &target,
iscsi->target_address,
NULL ) ) != 0 ) {
DBGC ( iscsi, "iSCSI %p could not open socket: %s\n",
iscsi, strerror ( rc ) );
return rc;
}
/* Enter security negotiation phase */
iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
ISCSI_STATUS_STRINGS_SECURITY );
if ( iscsi->target_username )
iscsi->status |= ISCSI_STATUS_AUTH_REVERSE_REQUIRED;
/* Assign new ISID */
iscsi->isid_iana_qual = ( random() & 0xffff );
/* Assign fresh initiator task tag */
iscsi_new_itt ( iscsi );
/* Set default operational parameters */
iscsi->max_burst_len = ISCSI_MAX_BURST_LEN;
/* Initiate login */
iscsi_start_login ( iscsi );
return 0;
}
/**
* Close iSCSI transport-layer connection
*
* @v iscsi iSCSI session
* @v rc Reason for close
*
* Closes the transport-layer connection and resets the session state
* ready to attempt a fresh login.
*/
static void iscsi_close_connection ( struct iscsi_session *iscsi, int rc ) {
/* Close all data transfer interfaces */
intf_restart ( &iscsi->socket, rc );
/* Clear connection status */
iscsi->status = 0;
/* Reset TX and RX state machines */
iscsi->tx_state = ISCSI_TX_IDLE;
iscsi->rx_state = ISCSI_RX_BHS;
iscsi->rx_offset = 0;
/* Free any temporary dynamically allocated memory */
chap_finish ( &iscsi->chap );
iscsi_rx_buffered_data_done ( iscsi );
}
/**
* Mark iSCSI SCSI operation as complete
*
* @v iscsi iSCSI session
* @v rc Return status code
* @v rsp SCSI response, if any
*
* Note that iscsi_scsi_done() will not close the connection, and must
* therefore be called only when the internal state machines are in an
* appropriate state, otherwise bad things may happen on the next call
* to iscsi_scsi_command(). The general rule is to call
* iscsi_scsi_done() only at the end of receiving a PDU; at this point
* the TX and RX engines should both be idle.
*/
static void iscsi_scsi_done ( struct iscsi_session *iscsi, int rc,
struct scsi_rsp *rsp ) {
uint32_t itt = iscsi->itt;
assert ( iscsi->tx_state == ISCSI_TX_IDLE );
/* Clear command */
free ( iscsi->command );
iscsi->command = NULL;
/* Send SCSI response, if any */
if ( rsp )
scsi_response ( &iscsi->data, rsp );
/* Close SCSI command, if this is still the same command. (It
* is possible that the command interface has already been
* closed as a result of the SCSI response we sent.)
*/
if ( iscsi->itt == itt )
intf_restart ( &iscsi->data, rc );
}
/****************************************************************************
*
* iSCSI SCSI command issuing
*
*/
/**
* Build iSCSI SCSI command BHS
*
* @v iscsi iSCSI session
*
* We don't currently support bidirectional commands (i.e. with both
* Data-In and Data-Out segments); these would require providing code
* to generate an AHS, and there doesn't seem to be any need for it at
* the moment.
*/
static void iscsi_start_command ( struct iscsi_session *iscsi ) {
struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
/* Construct BHS and initiate transmission */
iscsi_start_tx ( iscsi );
command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
command->flags = ( ISCSI_FLAG_FINAL |
ISCSI_COMMAND_ATTR_SIMPLE );
if ( iscsi->command->data_in )
command->flags |= ISCSI_COMMAND_FLAG_READ;
if ( iscsi->command->data_out )
command->flags |= ISCSI_COMMAND_FLAG_WRITE;
/* lengths left as zero */
memcpy ( &command->lun, &iscsi->command->lun,
sizeof ( command->lun ) );
command->itt = htonl ( iscsi->itt );
command->exp_len = htonl ( iscsi->command->data_in_len |
iscsi->command->data_out_len );
command->cmdsn = htonl ( iscsi->cmdsn );
command->expstatsn = htonl ( iscsi->statsn + 1 );
memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
DBGC2 ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#zx\n",
iscsi, SCSI_CDB_DATA ( command->cdb ),
( iscsi->command->data_in ? "in" : "out" ),
( iscsi->command->data_in ?
iscsi->command->data_in_len :
iscsi->command->data_out_len ) );
}
/**
* Receive data segment of an iSCSI SCSI response PDU
*
* @v iscsi iSCSI session
* @v data Received data
* @v len Length of received data
* @v remaining Data remaining after this data
* @ret rc Return status code
*/
static int iscsi_rx_scsi_response ( struct iscsi_session *iscsi,
const void *data, size_t len,
size_t remaining ) {
struct iscsi_bhs_scsi_response *response
= &iscsi->rx_bhs.scsi_response;
struct scsi_rsp rsp;
uint32_t residual_count;
size_t data_len;
int rc;
/* Buffer up the PDU data */
if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
DBGC ( iscsi, "iSCSI %p could not buffer SCSI response: %s\n",
iscsi, strerror ( rc ) );
return rc;
}
if ( remaining )
return 0;
/* Parse SCSI response and discard buffer */
memset ( &rsp, 0, sizeof ( rsp ) );
rsp.status = response->status;
residual_count = ntohl ( response->residual_count );
if ( response->flags & ISCSI_DATA_FLAG_OVERFLOW ) {
rsp.overrun = residual_count;
} else if ( response->flags & ISCSI_DATA_FLAG_UNDERFLOW ) {
rsp.overrun = -(residual_count);
}
data_len = ISCSI_DATA_LEN ( response->lengths );
if ( data_len ) {
scsi_parse_sense ( ( iscsi->rx_buffer + 2 ), ( data_len - 2 ),
&rsp.sense );
}
iscsi_rx_buffered_data_done ( iscsi );
/* Check for errors */
if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
return -EIO;
/* Mark as completed */
iscsi_scsi_done ( iscsi, 0, &rsp );
return 0;
}
/**
* Receive data segment of an iSCSI data-in PDU
*
* @v iscsi iSCSI session
* @v data Received data
* @v len Length of received data
* @v remaining Data remaining after this data
* @ret rc Return status code
*/
static int iscsi_rx_data_in ( struct iscsi_session *iscsi,
const void *data, size_t len,
size_t remaining ) {
struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
unsigned long offset;
/* Copy data to data-in buffer */
offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
assert ( iscsi->command != NULL );
assert ( iscsi->command->data_in );
assert ( ( offset + len ) <= iscsi->command->data_in_len );
copy_to_user ( iscsi->command->data_in, offset, data, len );
/* Wait for whole SCSI response to arrive */
if ( remaining )
return 0;
/* Mark as completed if status is present */
if ( data_in->flags & ISCSI_DATA_FLAG_STATUS ) {
assert ( ( offset + len ) == iscsi->command->data_in_len );
assert ( data_in->flags & ISCSI_FLAG_FINAL );
/* iSCSI cannot return an error status via a data-in */
iscsi_scsi_done ( iscsi, 0, NULL );
}
return 0;
}
/**
* Receive data segment of an iSCSI R2T PDU
*
* @v iscsi iSCSI session
* @v data Received data
* @v len Length of received data
* @v remaining Data remaining after this data
* @ret rc Return status code
*/
static int iscsi_rx_r2t ( struct iscsi_session *iscsi,
const void *data __unused, size_t len __unused,
size_t remaining __unused ) {
struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
/* Record transfer parameters and trigger first data-out */
iscsi->ttt = ntohl ( r2t->ttt );
iscsi->transfer_offset = ntohl ( r2t->offset );
iscsi->transfer_len = ntohl ( r2t->len );
iscsi_start_data_out ( iscsi, 0 );
return 0;
}
/**
* Build iSCSI data-out BHS
*
* @v iscsi iSCSI session
* @v datasn Data sequence number within the transfer
*
*/
static void iscsi_start_data_out ( struct iscsi_session *iscsi,
unsigned int datasn ) {
struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
unsigned long offset;
unsigned long remaining;
unsigned long len;
/* We always send 512-byte Data-Out PDUs; this removes the
* need to worry about the target's MaxRecvDataSegmentLength.
*/
offset = datasn * 512;
remaining = iscsi->transfer_len - offset;
len = remaining;
if ( len > 512 )
len = 512;
/* Construct BHS and initiate transmission */
iscsi_start_tx ( iscsi );
data_out->opcode = ISCSI_OPCODE_DATA_OUT;
if ( len == remaining )
data_out->flags = ( ISCSI_FLAG_FINAL );
ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
data_out->lun = iscsi->command->lun;
data_out->itt = htonl ( iscsi->itt );
data_out->ttt = htonl ( iscsi->ttt );
data_out->expstatsn = htonl ( iscsi->statsn + 1 );
data_out->datasn = htonl ( datasn );
data_out->offset = htonl ( iscsi->transfer_offset + offset );
DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
iscsi, datasn, len );
}
/**
* Complete iSCSI data-out PDU transmission
*
* @v iscsi iSCSI session
*
*/
static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
/* If we haven't reached the end of the sequence, start
* sending the next data-out PDU.
*/
if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
}
/**
* Send iSCSI data-out data segment
*
* @v iscsi iSCSI session
* @ret rc Return status code
*/
static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
struct io_buffer *iobuf;
unsigned long offset;
size_t len;
size_t pad_len;
offset = ntohl ( data_out->offset );
len = ISCSI_DATA_LEN ( data_out->lengths );
pad_len = ISCSI_DATA_PAD_LEN ( data_out->lengths );
assert ( iscsi->command != NULL );
assert ( iscsi->command->data_out );
assert ( ( offset + len ) <= iscsi->command->data_out_len );
iobuf = xfer_alloc_iob ( &iscsi->socket, ( len + pad_len ) );
if ( ! iobuf )
return -ENOMEM;
copy_from_user ( iob_put ( iobuf, len ),
iscsi->command->data_out, offset, len );
memset ( iob_put ( iobuf, pad_len ), 0, pad_len );
return xfer_deliver_iob ( &iscsi->socket, iobuf );
}
/**
* Receive data segment of an iSCSI NOP-In
*
* @v iscsi iSCSI session
* @v data Received data
* @v len Length of received data
* @v remaining Data remaining after this data
* @ret rc Return status code
*/
static int iscsi_rx_nop_in ( struct iscsi_session *iscsi,
const void *data __unused, size_t len __unused,
size_t remaining __unused ) {
struct iscsi_nop_in *nop_in = &iscsi->rx_bhs.nop_in;
DBGC2 ( iscsi, "iSCSI %p received NOP-In\n", iscsi );
/* We don't currently have the ability to respond to NOP-Ins
* sent as ping requests, but we can happily accept NOP-Ins
* sent merely to update CmdSN.
*/
if ( nop_in->ttt == htonl ( ISCSI_TAG_RESERVED ) )
return 0;
/* Ignore any other NOP-Ins. The target may eventually
* disconnect us for failing to respond, but this minimises
* unnecessary connection closures.
*/
DBGC ( iscsi, "iSCSI %p received unsupported NOP-In with TTT %08x\n",
iscsi, ntohl ( nop_in->ttt ) );
return 0;
}
/****************************************************************************
*
* iSCSI login
*
*/
/**
* Build iSCSI login request strings
*
* @v iscsi iSCSI session
*
* These are the initial set of strings sent in the first login
* request PDU. We want the following settings:
*
* HeaderDigest=None
* DataDigest=None
* MaxConnections=1 (irrelevant; we make only one connection anyway) [4]
* InitialR2T=Yes [1]
* ImmediateData=No (irrelevant; we never send immediate data) [4]
* MaxRecvDataSegmentLength=8192 (default; we don't care) [3]
* MaxBurstLength=262144 (default; we don't care) [3]
* FirstBurstLength=65536 (irrelevant due to other settings) [5]
* DefaultTime2Wait=0 [2]
* DefaultTime2Retain=0 [2]
* MaxOutstandingR2T=1
* DataPDUInOrder=Yes
* DataSequenceInOrder=Yes
* ErrorRecoveryLevel=0
*
* [1] InitialR2T has an OR resolution function, so the target may
* force us to use it. We therefore simplify our logic by always
* using it.
*
* [2] These ensure that we can safely start a new task once we have
* reconnected after a failure, without having to manually tidy up
* after the old one.
*
* [3] We are quite happy to use the RFC-defined default values for
* these parameters, but some targets (notably OpenSolaris)
* incorrectly assume a default value of zero, so we explicitly
* specify the default values.
*
* [4] We are quite happy to use the RFC-defined default values for
* these parameters, but some targets (notably a QNAP TS-639Pro) fail
* unless they are supplied, so we explicitly specify the default
* values.
*
* [5] FirstBurstLength is defined to be irrelevant since we already
* force InitialR2T=Yes and ImmediateData=No, but some targets
* (notably LIO as of kernel 4.11) fail unless it is specified, so we
* explicitly specify the default value.
*/
static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
void *data, size_t len ) {
unsigned int used = 0;
const char *auth_method;
if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
/* Default to allowing no authentication */
auth_method = "None";
/* If we have a credential to supply, permit CHAP */
if ( iscsi->initiator_username )
auth_method = "CHAP,None";
/* If we have a credential to check, force CHAP */
if ( iscsi->target_username )
auth_method = "CHAP";
used += ssnprintf ( data + used, len - used,
"InitiatorName=%s%c"
"TargetName=%s%c"
"SessionType=Normal%c"
"AuthMethod=%s%c",
iscsi->initiator_iqn, 0,
iscsi->target_iqn, 0, 0,
auth_method, 0 );
}
if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
}
if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
char buf[ base16_encoded_len ( iscsi->chap.response_len ) + 1 ];
assert ( iscsi->initiator_username != NULL );
base16_encode ( iscsi->chap.response, iscsi->chap.response_len,
buf, sizeof ( buf ) );
used += ssnprintf ( data + used, len - used,
"CHAP_N=%s%cCHAP_R=0x%s%c",
iscsi->initiator_username, 0, buf, 0 );
}
if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_CHALLENGE ) ) {
size_t challenge_len = ( sizeof ( iscsi->chap_challenge ) - 1 );
char buf[ base16_encoded_len ( challenge_len ) + 1 ];
base16_encode ( ( iscsi->chap_challenge + 1 ), challenge_len,
buf, sizeof ( buf ) );
used += ssnprintf ( data + used, len - used,
"CHAP_I=%d%cCHAP_C=0x%s%c",
iscsi->chap_challenge[0], 0, buf, 0 );
}
if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
used += ssnprintf ( data + used, len - used,
"HeaderDigest=None%c"
"DataDigest=None%c"
"MaxConnections=1%c"
"InitialR2T=Yes%c"
"ImmediateData=No%c"
"MaxRecvDataSegmentLength=%d%c"
"MaxBurstLength=%d%c"
"FirstBurstLength=%d%c"
"DefaultTime2Wait=0%c"
"DefaultTime2Retain=0%c"
"MaxOutstandingR2T=1%c"
"DataPDUInOrder=Yes%c"
"DataSequenceInOrder=Yes%c"
"ErrorRecoveryLevel=0%c",
0, 0, 0, 0, 0,
ISCSI_MAX_RECV_DATA_SEG_LEN, 0,
ISCSI_MAX_BURST_LEN, 0,
ISCSI_FIRST_BURST_LEN, 0,
0, 0, 0, 0, 0, 0 );
}
return used;
}
/**
* Build iSCSI login request BHS
*
* @v iscsi iSCSI session
*/
static void iscsi_start_login ( struct iscsi_session *iscsi ) {
struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
int len;
switch ( iscsi->status & ISCSI_LOGIN_CSG_MASK ) {
case ISCSI_LOGIN_CSG_SECURITY_NEGOTIATION:
DBGC ( iscsi, "iSCSI %p entering security negotiation\n",
iscsi );
break;
case ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION:
DBGC ( iscsi, "iSCSI %p entering operational negotiation\n",
iscsi );
break;
default:
assert ( 0 );
}
/* Construct BHS and initiate transmission */
iscsi_start_tx ( iscsi );
request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
ISCSI_FLAG_IMMEDIATE );
request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
ISCSI_LOGIN_FLAG_TRANSITION );
/* version_max and version_min left as zero */
len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
ISCSI_SET_LENGTHS ( request->lengths, 0, len );
request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
IANA_EN_FEN_SYSTEMS );
request->isid_iana_qual = htons ( iscsi->isid_iana_qual );
/* tsih left as zero */
request->itt = htonl ( iscsi->itt );
/* cid left as zero */
request->cmdsn = htonl ( iscsi->cmdsn );
request->expstatsn = htonl ( iscsi->statsn + 1 );
}
/**
* Complete iSCSI login request PDU transmission
*
* @v iscsi iSCSI session
*
*/
static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
/* Clear any "strings to send" flags */
iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
/* Free any dynamically allocated storage used for login */
chap_finish ( &iscsi->chap );
}
/**
* Transmit data segment of an iSCSI login request PDU
*
* @v iscsi iSCSI session
* @ret rc Return status code
*
* For login requests, the data segment consists of the login strings.
*/
static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
struct io_buffer *iobuf;
size_t len;
size_t pad_len;
len = ISCSI_DATA_LEN ( request->lengths );
pad_len = ISCSI_DATA_PAD_LEN ( request->lengths );
iobuf = xfer_alloc_iob ( &iscsi->socket, ( len + pad_len ) );
if ( ! iobuf )
return -ENOMEM;
iob_put ( iobuf, len );
iscsi_build_login_request_strings ( iscsi, iobuf->data, len );
memset ( iob_put ( iobuf, pad_len ), 0, pad_len );
return xfer_deliver_iob ( &iscsi->socket, iobuf );
}
/**
* Decode large binary value
*
* @v encoded Encoded large binary value
* @v raw Raw data
* @v len Length of data buffer
* @ret len Length of raw data, or negative error
*/
static int iscsi_large_binary_decode ( const char *encoded, uint8_t *raw,
size_t len ) {
/* Check for initial '0x' or '0b' and decode as appropriate */
if ( *(encoded++) == '0' ) {
switch ( tolower ( *(encoded++) ) ) {
case 'x' :
return base16_decode ( encoded, raw, len );
case 'b' :
return base64_decode ( encoded, raw, len );
}
}
return -EPROTO_INVALID_LARGE_BINARY;
}
/**
* Handle iSCSI TargetAddress text value
*
* @v iscsi iSCSI session
* @v value TargetAddress value
* @ret rc Return status code
*/
static int iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
const char *value ) {
char *separator;
DBGC ( iscsi, "iSCSI %p will redirect to %s\n", iscsi, value );
/* Replace target address */
free ( iscsi->target_address );
iscsi->target_address = strdup ( value );
if ( ! iscsi->target_address )
return -ENOMEM;
/* Replace target port */
iscsi->target_port = htons ( ISCSI_PORT );
separator = strchr ( iscsi->target_address, ':' );
if ( separator ) {
*separator = '\0';
iscsi->target_port = strtoul ( ( separator + 1 ), NULL, 0 );
}
return 0;
}
/**
* Handle iSCSI AuthMethod text value
*
* @v iscsi iSCSI session
* @v value AuthMethod value
* @ret rc Return status code
*/
static int iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
const char *value ) {
/* If server requests CHAP, send the CHAP_A string */
if ( strcmp ( value, "CHAP" ) == 0 ) {
DBGC ( iscsi, "iSCSI %p initiating CHAP authentication\n",
iscsi );
iscsi->status |= ( ISCSI_STATUS_STRINGS_CHAP_ALGORITHM |
ISCSI_STATUS_AUTH_FORWARD_REQUIRED );
}
return 0;
}
/**
* Handle iSCSI MaxBurstLength text value
*
* @v iscsi iSCSI session
* @v value MaxBurstLength value
* @ret rc Return status code
*/
static int iscsi_handle_maxburstlength_value ( struct iscsi_session *iscsi,
const char *value ) {
unsigned long max_burst_len;
char *end;
/* Update maximum burst length */
max_burst_len = strtoul ( value, &end, 0 );
if ( *end ) {
DBGC ( iscsi, "iSCSI %p invalid MaxBurstLength \"%s\"\n",
iscsi, value );
return -EINVAL_MAXBURSTLENGTH;
}
if ( max_burst_len < iscsi->max_burst_len )
iscsi->max_burst_len = max_burst_len;
return 0;
}
/**
* Handle iSCSI CHAP_A text value
*
* @v iscsi iSCSI session
* @v value CHAP_A value
* @ret rc Return status code
*/
static int iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
const char *value ) {
/* We only ever offer "5" (i.e. MD5) as an algorithm, so if
* the server responds with anything else it is a protocol
* violation.
*/
if ( strcmp ( value, "5" ) != 0 ) {
DBGC ( iscsi, "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
iscsi, value );
return -EPROTO_INVALID_CHAP_ALGORITHM;
}
return 0;
}
/**
* Handle iSCSI CHAP_I text value
*
* @v iscsi iSCSI session
* @v value CHAP_I value
* @ret rc Return status code
*/
static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
const char *value ) {
unsigned int identifier;
char *endp;
int rc;
/* The CHAP identifier is an integer value */
identifier = strtoul ( value, &endp, 0 );
if ( *endp != '\0' ) {
DBGC ( iscsi, "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
iscsi, value );
return -EPROTO_INVALID_CHAP_IDENTIFIER;
}
/* Prepare for CHAP with MD5 */
chap_finish ( &iscsi->chap );
if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
iscsi, strerror ( rc ) );
return rc;
}
/* Identifier and secret are the first two components of the