2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <gpxe/vsprintf.h>
27 #include <gpxe/socket.h>
28 #include <gpxe/xfer.h>
29 #include <gpxe/open.h>
30 #include <gpxe/scsi.h>
31 #include <gpxe/process.h>
32 #include <gpxe/uaccess.h>
33 #include <gpxe/tcpip.h>
34 #include <gpxe/dhcp.h>
35 #include <gpxe/iscsi.h>
43 /** iSCSI initiator name (explicitly specified) */
44 char *iscsi_initiator_iqn;
46 /** Default iSCSI initiator name (constructed from hostname) */
47 char *iscsi_default_initiator_iqn;
55 static void iscsi_start_tx ( struct iscsi_session *iscsi );
56 static void iscsi_start_login ( struct iscsi_session *iscsi );
57 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
58 unsigned int datasn );
61 * Finish receiving PDU data into buffer
63 * @v iscsi iSCSI session
65 static void iscsi_rx_buffered_data_done ( struct iscsi_session *iscsi ) {
66 free ( iscsi->rx_buffer );
67 iscsi->rx_buffer = NULL;
73 * @v refcnt Reference counter
75 static void iscsi_free ( struct refcnt *refcnt ) {
76 struct iscsi_session *iscsi =
77 container_of ( refcnt, struct iscsi_session, refcnt );
79 free ( iscsi->target_address );
80 free ( iscsi->target_iqn );
81 chap_finish ( &iscsi->chap );
82 iscsi_rx_buffered_data_done ( iscsi );
87 * Open iSCSI transport-layer connection
89 * @v iscsi iSCSI session
90 * @ret rc Return status code
92 static int iscsi_open_connection ( struct iscsi_session *iscsi ) {
93 struct sockaddr_tcpip target;
96 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
97 assert ( iscsi->rx_state == ISCSI_RX_BHS );
98 assert ( iscsi->rx_offset == 0 );
101 memset ( &target, 0, sizeof ( target ) );
102 target.st_port = htons ( iscsi->target_port );
103 if ( ( rc = xfer_open_named_socket ( &iscsi->socket, SOCK_STREAM,
104 ( struct sockaddr * ) &target,
105 iscsi->target_address,
107 DBGC ( iscsi, "iSCSI %p could not open socket: %s\n",
108 iscsi, strerror ( rc ) );
112 /* Enter security negotiation phase */
113 iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
114 ISCSI_STATUS_STRINGS_SECURITY );
116 /* Assign fresh initiator task tag */
120 iscsi_start_login ( iscsi );
126 * Close iSCSI transport-layer connection
128 * @v iscsi iSCSI session
129 * @v rc Reason for close
131 * Closes the transport-layer connection and resets the session state
132 * ready to attempt a fresh login.
134 static void iscsi_close_connection ( struct iscsi_session *iscsi, int rc ) {
136 /* Close all data transfer interfaces */
137 xfer_close ( &iscsi->socket, rc );
139 /* Clear connection status */
142 /* Reset TX and RX state machines */
143 iscsi->tx_state = ISCSI_TX_IDLE;
144 iscsi->rx_state = ISCSI_RX_BHS;
146 /* Free any temporary dynamically allocated memory */
147 chap_finish ( &iscsi->chap );
148 iscsi_rx_buffered_data_done ( iscsi );
152 * Mark iSCSI SCSI operation as complete
154 * @v iscsi iSCSI session
155 * @v rc Return status code
157 * Note that iscsi_scsi_done() will not close the connection, and must
158 * therefore be called only when the internal state machines are in an
159 * appropriate state, otherwise bad things may happen on the next call
160 * to iscsi_issue(). The general rule is to call iscsi_scsi_done()
161 * only at the end of receiving a PDU; at this point the TX and RX
162 * engines should both be idle.
164 static void iscsi_scsi_done ( struct iscsi_session *iscsi, int rc ) {
166 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
168 iscsi->command = NULL;
172 /****************************************************************************
174 * iSCSI SCSI command issuing
179 * Build iSCSI SCSI command BHS
181 * @v iscsi iSCSI session
183 * We don't currently support bidirectional commands (i.e. with both
184 * Data-In and Data-Out segments); these would require providing code
185 * to generate an AHS, and there doesn't seem to be any need for it at
188 static void iscsi_start_command ( struct iscsi_session *iscsi ) {
189 struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
191 assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
193 /* Construct BHS and initiate transmission */
194 iscsi_start_tx ( iscsi );
195 command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
196 command->flags = ( ISCSI_FLAG_FINAL |
197 ISCSI_COMMAND_ATTR_SIMPLE );
198 if ( iscsi->command->data_in )
199 command->flags |= ISCSI_COMMAND_FLAG_READ;
200 if ( iscsi->command->data_out )
201 command->flags |= ISCSI_COMMAND_FLAG_WRITE;
202 /* lengths left as zero */
203 command->lun = iscsi->lun;
204 command->itt = htonl ( ++iscsi->itt );
205 command->exp_len = htonl ( iscsi->command->data_in_len |
206 iscsi->command->data_out_len );
207 command->cmdsn = htonl ( iscsi->cmdsn );
208 command->expstatsn = htonl ( iscsi->statsn + 1 );
209 memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
210 DBGC ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#x\n",
211 iscsi, SCSI_CDB_DATA ( command->cdb ),
212 ( iscsi->command->data_in ? "in" : "out" ),
213 ( iscsi->command->data_in ?
214 iscsi->command->data_in_len : iscsi->command->data_out_len ));
218 * Receive data segment of an iSCSI SCSI response PDU
220 * @v iscsi iSCSI session
221 * @v data Received data
222 * @v len Length of received data
223 * @v remaining Data remaining after this data
224 * @ret rc Return status code
226 static int iscsi_rx_scsi_response ( struct iscsi_session *iscsi,
227 const void *data, size_t len,
229 struct iscsi_bhs_scsi_response *response
230 = &iscsi->rx_bhs.scsi_response;
233 /* Capture the sense response code as it floats past, if present */
234 sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
235 if ( ( sense_offset >= 0 ) && len ) {
236 iscsi->command->sense_response =
237 * ( ( char * ) data + sense_offset );
240 /* Wait for whole SCSI response to arrive */
244 /* Record SCSI status code */
245 iscsi->command->status = response->status;
247 /* Check for errors */
248 if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
251 /* Mark as completed */
252 iscsi_scsi_done ( iscsi, 0 );
257 * Receive data segment of an iSCSI data-in PDU
259 * @v iscsi iSCSI session
260 * @v data Received data
261 * @v len Length of received data
262 * @v remaining Data remaining after this data
263 * @ret rc Return status code
265 static int iscsi_rx_data_in ( struct iscsi_session *iscsi,
266 const void *data, size_t len,
267 size_t remaining __unused ) {
268 struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
269 unsigned long offset;
271 /* Copy data to data-in buffer */
272 offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
273 assert ( iscsi->command != NULL );
274 assert ( iscsi->command->data_in );
275 assert ( ( offset + len ) <= iscsi->command->data_in_len );
276 copy_to_user ( iscsi->command->data_in, offset, data, len );
278 /* Record SCSI status, if present */
279 if ( data_in->flags & ISCSI_DATA_FLAG_STATUS )
280 iscsi->command->status = data_in->status;
282 /* If this is the end, flag as complete */
283 if ( ( offset + len ) == iscsi->command->data_in_len ) {
284 assert ( data_in->flags & ISCSI_FLAG_FINAL );
285 assert ( remaining == 0 );
286 iscsi_scsi_done ( iscsi, 0 );
293 * Receive data segment of an iSCSI R2T PDU
295 * @v iscsi iSCSI session
296 * @v data Received data
297 * @v len Length of received data
298 * @v remaining Data remaining after this data
299 * @ret rc Return status code
301 static int iscsi_rx_r2t ( struct iscsi_session *iscsi,
302 const void *data __unused, size_t len __unused,
303 size_t remaining __unused ) {
304 struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
306 /* Record transfer parameters and trigger first data-out */
307 iscsi->ttt = ntohl ( r2t->ttt );
308 iscsi->transfer_offset = ntohl ( r2t->offset );
309 iscsi->transfer_len = ntohl ( r2t->len );
310 iscsi_start_data_out ( iscsi, 0 );
316 * Build iSCSI data-out BHS
318 * @v iscsi iSCSI session
319 * @v datasn Data sequence number within the transfer
322 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
323 unsigned int datasn ) {
324 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
325 unsigned long offset;
326 unsigned long remaining;
329 /* We always send 512-byte Data-Out PDUs; this removes the
330 * need to worry about the target's MaxRecvDataSegmentLength.
332 offset = datasn * 512;
333 remaining = iscsi->transfer_len - offset;
338 /* Construct BHS and initiate transmission */
339 iscsi_start_tx ( iscsi );
340 data_out->opcode = ISCSI_OPCODE_DATA_OUT;
341 if ( len == remaining )
342 data_out->flags = ( ISCSI_FLAG_FINAL );
343 ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
344 data_out->lun = iscsi->lun;
345 data_out->itt = htonl ( iscsi->itt );
346 data_out->ttt = htonl ( iscsi->ttt );
347 data_out->expstatsn = htonl ( iscsi->statsn + 1 );
348 data_out->datasn = htonl ( datasn );
349 data_out->offset = htonl ( iscsi->transfer_offset + offset );
350 DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
351 iscsi, datasn, len );
355 * Complete iSCSI data-out PDU transmission
357 * @v iscsi iSCSI session
360 static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
361 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
363 /* If we haven't reached the end of the sequence, start
364 * sending the next data-out PDU.
366 if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
367 iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
371 * Send iSCSI data-out data segment
373 * @v iscsi iSCSI session
374 * @ret rc Return status code
376 static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
377 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
378 struct io_buffer *iobuf;
379 unsigned long offset;
382 offset = ntohl ( data_out->offset );
383 len = ISCSI_DATA_LEN ( data_out->lengths );
385 assert ( iscsi->command != NULL );
386 assert ( iscsi->command->data_out );
387 assert ( ( offset + len ) <= iscsi->command->data_out_len );
389 iobuf = xfer_alloc_iob ( &iscsi->socket, len );
393 copy_from_user ( iob_put ( iobuf, len ),
394 iscsi->command->data_out, offset, len );
396 return xfer_deliver_iob ( &iscsi->socket, iobuf );
399 /****************************************************************************
406 * Build iSCSI login request strings
408 * @v iscsi iSCSI session
410 * These are the initial set of strings sent in the first login
411 * request PDU. We want the following settings:
415 * MaxConnections is irrelevant; we make only one connection anyway
417 * ImmediateData is irrelevant; we never send immediate data
418 * MaxRecvDataSegmentLength=8192 (default; we don't care)
419 * MaxBurstLength=262144 (default; we don't care)
420 * FirstBurstLength=262144 (default; we don't care)
421 * DefaultTime2Wait=0 [2]
422 * DefaultTime2Retain=0 [2]
423 * MaxOutstandingR2T=1
425 * DataSequenceInOrder=Yes
426 * ErrorRecoveryLevel=0
428 * [1] InitialR2T has an OR resolution function, so the target may
429 * force us to use it. We therefore simplify our logic by always
432 * [2] These ensure that we can safely start a new task once we have
433 * reconnected after a failure, without having to manually tidy up
436 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
437 void *data, size_t len ) {
439 unsigned int used = 0;
442 if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
443 initiator_iqn = iscsi_initiator_iqn;
444 if ( ! initiator_iqn )
445 initiator_iqn = iscsi_default_initiator_iqn;
446 if ( ! initiator_iqn )
447 initiator_iqn = "iqn.2000-09.org.etherboot:UNKNOWN";
448 used += ssnprintf ( data + used, len - used,
451 "SessionType=Normal%c"
452 "AuthMethod=CHAP,None%c",
454 iscsi->target_iqn, 0, 0, 0 );
457 if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
458 used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
461 if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) &&
463 used += ssnprintf ( data + used, len - used,
464 "CHAP_N=%s%cCHAP_R=0x",
466 for ( i = 0 ; i < iscsi->chap.response_len ; i++ ) {
467 used += ssnprintf ( data + used, len - used, "%02x",
468 iscsi->chap.response[i] );
470 used += ssnprintf ( data + used, len - used, "%c", 0 );
473 if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
474 used += ssnprintf ( data + used, len - used,
475 "HeaderDigest=None%c"
478 "DefaultTime2Wait=0%c"
479 "DefaultTime2Retain=0%c"
480 "MaxOutstandingR2T=1%c"
481 "DataPDUInOrder=Yes%c"
482 "DataSequenceInOrder=Yes%c"
483 "ErrorRecoveryLevel=0%c",
484 0, 0, 0, 0, 0, 0, 0, 0, 0 );
491 * Build iSCSI login request BHS
493 * @v iscsi iSCSI session
495 static void iscsi_start_login ( struct iscsi_session *iscsi ) {
496 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
499 /* Construct BHS and initiate transmission */
500 iscsi_start_tx ( iscsi );
501 request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
502 ISCSI_FLAG_IMMEDIATE );
503 request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
504 ISCSI_LOGIN_FLAG_TRANSITION );
505 /* version_max and version_min left as zero */
506 len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
507 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
508 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
509 IANA_EN_FEN_SYSTEMS );
510 /* isid_iana_qual left as zero */
511 request->tsih = htons ( iscsi->tsih );
512 request->itt = htonl ( iscsi->itt );
513 /* cid left as zero */
514 request->cmdsn = htonl ( iscsi->cmdsn );
515 request->expstatsn = htonl ( iscsi->statsn + 1 );
519 * Complete iSCSI login request PDU transmission
521 * @v iscsi iSCSI session
524 static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
526 /* Clear any "strings to send" flags */
527 iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
529 /* Free any dynamically allocated storage used for login */
530 chap_finish ( &iscsi->chap );
534 * Transmit data segment of an iSCSI login request PDU
536 * @v iscsi iSCSI session
537 * @ret rc Return status code
539 * For login requests, the data segment consists of the login strings.
541 static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
542 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
543 struct io_buffer *iobuf;
546 len = ISCSI_DATA_LEN ( request->lengths );
547 iobuf = xfer_alloc_iob ( &iscsi->socket, len );
550 iob_put ( iobuf, len );
551 iscsi_build_login_request_strings ( iscsi, iobuf->data, len );
552 return xfer_deliver_iob ( &iscsi->socket, iobuf );
556 * Handle iSCSI TargetAddress text value
558 * @v iscsi iSCSI session
559 * @v value TargetAddress value
560 * @ret rc Return status code
562 static int iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
563 const char *value ) {
565 DBGC ( iscsi, "iSCSI %p will redirect to %s\n", iscsi, value );
567 /* Replace target address */
568 free ( iscsi->target_address );
569 iscsi->target_address = strdup ( value );
570 if ( ! iscsi->target_address )
576 * Handle iSCSI AuthMethod text value
578 * @v iscsi iSCSI session
579 * @v value AuthMethod value
580 * @ret rc Return status code
582 static int iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
583 const char *value ) {
585 /* If server requests CHAP, send the CHAP_A string */
586 if ( strcmp ( value, "CHAP" ) == 0 ) {
587 DBGC ( iscsi, "iSCSI %p initiating CHAP authentication\n",
589 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_ALGORITHM;
595 * Handle iSCSI CHAP_A text value
597 * @v iscsi iSCSI session
598 * @v value CHAP_A value
599 * @ret rc Return status code
601 static int iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
602 const char *value ) {
605 /* We only ever offer "5" (i.e. MD5) as an algorithm, so if
606 * the server responds with anything else it is a protocol
609 if ( strcmp ( value, "5" ) != 0 ) {
610 DBGC ( iscsi, "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
615 /* Prepare for CHAP with MD5 */
616 if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
617 DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
618 iscsi, strerror ( rc ) );
626 * Handle iSCSI CHAP_I text value
628 * @v iscsi iSCSI session
629 * @v value CHAP_I value
630 * @ret rc Return status code
632 static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
633 const char *value ) {
634 unsigned int identifier;
637 /* The CHAP identifier is an integer value */
638 identifier = strtoul ( value, &endp, 0 );
639 if ( *endp != '\0' ) {
640 DBGC ( iscsi, "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
645 /* Identifier and secret are the first two components of the
648 chap_set_identifier ( &iscsi->chap, identifier );
649 if ( iscsi_password ) {
650 chap_update ( &iscsi->chap, iscsi_password,
651 strlen ( iscsi_password ) );
658 * Handle iSCSI CHAP_C text value
660 * @v iscsi iSCSI session
661 * @v value CHAP_C value
662 * @ret rc Return status code
664 static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
665 const char *value ) {
670 /* Check and strip leading "0x" */
671 if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
672 DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge \"%s\"\n",
677 /* Process challenge an octet at a time */
678 for ( ; ( value[0] && value[1] ) ; value += 2 ) {
679 memcpy ( buf, value, 2 );
681 byte = strtoul ( buf, &endp, 16 );
682 if ( *endp != '\0' ) {
683 DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge "
684 "byte \"%s\"\n", iscsi, buf );
687 chap_update ( &iscsi->chap, &byte, sizeof ( byte ) );
690 /* Build CHAP response */
691 DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );
692 chap_respond ( &iscsi->chap );
693 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_RESPONSE;
698 /** An iSCSI text string that we want to handle */
699 struct iscsi_string_type {
702 * This is the portion up to and including the "=" sign,
703 * e.g. "InitiatorName=", "CHAP_A=", etc.
706 /** Handle iSCSI string value
708 * @v iscsi iSCSI session
709 * @v value iSCSI string value
710 * @ret rc Return status code
712 int ( * handle ) ( struct iscsi_session *iscsi, const char *value );
715 /** iSCSI text strings that we want to handle */
716 static struct iscsi_string_type iscsi_string_types[] = {
717 { "TargetAddress=", iscsi_handle_targetaddress_value },
718 { "AuthMethod=", iscsi_handle_authmethod_value },
719 { "CHAP_A=", iscsi_handle_chap_a_value },
720 { "CHAP_I=", iscsi_handle_chap_i_value },
721 { "CHAP_C=", iscsi_handle_chap_c_value },
726 * Handle iSCSI string
728 * @v iscsi iSCSI session
729 * @v string iSCSI string (in "key=value" format)
730 * @ret rc Return status code
732 static int iscsi_handle_string ( struct iscsi_session *iscsi,
733 const char *string ) {
734 struct iscsi_string_type *type;
738 for ( type = iscsi_string_types ; type->key ; type++ ) {
739 key_len = strlen ( type->key );
740 if ( strncmp ( string, type->key, key_len ) != 0 )
742 DBGC ( iscsi, "iSCSI %p handling %s\n", iscsi, string );
743 if ( ( rc = type->handle ( iscsi,
744 ( string + key_len ) ) ) != 0 ) {
745 DBGC ( iscsi, "iSCSI %p could not handle %s: %s\n",
746 iscsi, string, strerror ( rc ) );
751 DBGC ( iscsi, "iSCSI %p ignoring %s\n", iscsi, string );
756 * Handle iSCSI strings
758 * @v iscsi iSCSI session
759 * @v string iSCSI string buffer
760 * @v len Length of string buffer
761 * @ret rc Return status code
763 static int iscsi_handle_strings ( struct iscsi_session *iscsi,
764 const char *strings, size_t len ) {
768 /* Handle each string in turn, taking care not to overrun the
769 * data buffer in case of badly-terminated data.
772 string_len = ( strnlen ( strings, len ) + 1 );
773 if ( string_len > len )
775 if ( ( rc = iscsi_handle_string ( iscsi, strings ) ) != 0 )
777 strings += string_len;
784 * Receive PDU data into buffer
786 * @v iscsi iSCSI session
787 * @v data Data to receive
788 * @v len Length of data
789 * @ret rc Return status code
791 * This can be used when the RX PDU type handler wishes to buffer up
792 * all received data and process the PDU as a single unit. The caller
793 * is repsonsible for calling iscsi_rx_buffered_data_done() after
794 * processing the data.
796 static int iscsi_rx_buffered_data ( struct iscsi_session *iscsi,
797 const void *data, size_t len ) {
799 /* Allocate buffer on first call */
800 if ( ! iscsi->rx_buffer ) {
801 iscsi->rx_buffer = malloc ( iscsi->rx_len );
802 if ( ! iscsi->rx_buffer )
806 /* Copy data to buffer */
807 assert ( ( iscsi->rx_offset + len ) <= iscsi->rx_len );
808 memcpy ( ( iscsi->rx_buffer + iscsi->rx_offset ), data, len );
814 * Receive data segment of an iSCSI login response PDU
816 * @v iscsi iSCSI session
817 * @v data Received data
818 * @v len Length of received data
819 * @v remaining Data remaining after this data
820 * @ret rc Return status code
822 static int iscsi_rx_login_response ( struct iscsi_session *iscsi,
823 const void *data, size_t len,
825 struct iscsi_bhs_login_response *response
826 = &iscsi->rx_bhs.login_response;
829 /* Buffer up the PDU data */
830 if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
831 DBGC ( iscsi, "iSCSI %p could not buffer login response: %s\n",
832 iscsi, strerror ( rc ) );
838 /* Process string data and discard string buffer */
839 if ( ( rc = iscsi_handle_strings ( iscsi, iscsi->rx_buffer,
840 iscsi->rx_len ) ) != 0 )
842 iscsi_rx_buffered_data_done ( iscsi );
844 /* Check for login redirection */
845 if ( response->status_class == ISCSI_STATUS_REDIRECT ) {
846 DBGC ( iscsi, "iSCSI %p redirecting to new server\n", iscsi );
847 iscsi_close_connection ( iscsi, 0 );
848 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
849 DBGC ( iscsi, "iSCSI %p could not redirect: %s\n ",
850 iscsi, strerror ( rc ) );
856 /* Check for fatal errors */
857 if ( response->status_class != 0 ) {
858 DBGC ( iscsi, "iSCSI login failure: class %02x detail %02x\n",
859 response->status_class, response->status_detail );
860 iscsi->instant_rc = -EPERM;
864 /* Handle login transitions */
865 if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
866 switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
867 case ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION:
869 ( ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE |
870 ISCSI_STATUS_STRINGS_OPERATIONAL );
872 case ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE:
873 iscsi->status = ISCSI_STATUS_FULL_FEATURE_PHASE;
876 DBGC ( iscsi, "iSCSI %p got invalid response flags "
877 "%02x\n", iscsi, response->flags );
882 /* Send next login request PDU if we haven't reached the full
885 if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
886 ISCSI_STATUS_FULL_FEATURE_PHASE ) {
887 iscsi_start_login ( iscsi );
891 /* Reset retry count */
892 iscsi->retry_count = 0;
894 /* Record TSIH for future reference */
895 iscsi->tsih = ntohl ( response->tsih );
897 /* Send the actual SCSI command */
898 iscsi_start_command ( iscsi );
903 /****************************************************************************
905 * iSCSI to socket interface
910 * Start up a new TX PDU
912 * @v iscsi iSCSI session
914 * This initiates the process of sending a new PDU. Only one PDU may
915 * be in transit at any one time.
917 static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
918 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
920 /* Initialise TX BHS */
921 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
923 /* Flag TX engine to start transmitting */
924 iscsi->tx_state = ISCSI_TX_BHS;
930 * @v iscsi iSCSI session
931 * @ret rc Return status code
933 static int iscsi_tx_nothing ( struct iscsi_session *iscsi __unused ) {
938 * Transmit basic header segment of an iSCSI PDU
940 * @v iscsi iSCSI session
941 * @ret rc Return status code
943 static int iscsi_tx_bhs ( struct iscsi_session *iscsi ) {
944 return xfer_deliver_raw ( &iscsi->socket, &iscsi->tx_bhs,
945 sizeof ( iscsi->tx_bhs ) );
949 * Transmit data segment of an iSCSI PDU
951 * @v iscsi iSCSI session
952 * @ret rc Return status code
954 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
955 * will be valid when this is called.
957 static int iscsi_tx_data ( struct iscsi_session *iscsi ) {
958 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
960 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
961 case ISCSI_OPCODE_DATA_OUT:
962 return iscsi_tx_data_out ( iscsi );
963 case ISCSI_OPCODE_LOGIN_REQUEST:
964 return iscsi_tx_login_request ( iscsi );
966 /* Nothing to send in other states */
972 * Transmit data padding of an iSCSI PDU
974 * @v iscsi iSCSI session
975 * @ret rc Return status code
977 * Handle transmission of any data padding in a PDU data segment.
978 * iscsi::tx_bhs will be valid when this is called.
980 static int iscsi_tx_data_padding ( struct iscsi_session *iscsi ) {
981 static const char pad[] = { '\0', '\0', '\0' };
982 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
985 pad_len = ISCSI_DATA_PAD_LEN ( common->lengths );
989 return xfer_deliver_raw ( &iscsi->socket, pad, pad_len );
993 * Complete iSCSI PDU transmission
995 * @v iscsi iSCSI session
997 * Called when a PDU has been completely transmitted and the TX state
998 * machine is about to enter the idle state. iscsi::tx_bhs will be
999 * valid for the just-completed PDU when this is called.
1001 static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
1002 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1004 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
1005 case ISCSI_OPCODE_DATA_OUT:
1006 iscsi_data_out_done ( iscsi );
1007 case ISCSI_OPCODE_LOGIN_REQUEST:
1008 iscsi_login_request_done ( iscsi );
1016 * Transmit iSCSI PDU
1018 * @v iscsi iSCSI session
1019 * @v buf Temporary data buffer
1020 * @v len Length of temporary data buffer
1022 * Constructs data to be sent for the current TX state
1024 static void iscsi_tx_step ( struct process *process ) {
1025 struct iscsi_session *iscsi =
1026 container_of ( process, struct iscsi_session, process );
1027 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1028 int ( * tx ) ( struct iscsi_session *iscsi );
1029 enum iscsi_tx_state next_state;
1033 /* Select fragment to transmit */
1035 switch ( iscsi->tx_state ) {
1037 /* Stop processing */
1041 tx_len = sizeof ( iscsi->tx_bhs );
1042 next_state = ISCSI_TX_AHS;
1045 tx = iscsi_tx_nothing;
1047 next_state = ISCSI_TX_DATA;
1051 tx_len = ISCSI_DATA_LEN ( common->lengths );
1052 next_state = ISCSI_TX_DATA_PADDING;
1054 case ISCSI_TX_DATA_PADDING:
1055 tx = iscsi_tx_data_padding;
1056 tx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1057 next_state = ISCSI_TX_IDLE;
1064 /* Check for window availability, if needed */
1065 if ( tx_len && ( xfer_window ( &iscsi->socket ) == 0 ) ) {
1066 /* Cannot transmit at this point; stop processing */
1071 if ( ( rc = tx ( iscsi ) ) != 0 ) {
1072 DBGC ( iscsi, "iSCSI %p could not transmit: %s\n",
1073 iscsi, strerror ( rc ) );
1077 /* Move to next state */
1078 iscsi->tx_state = next_state;
1079 if ( next_state == ISCSI_TX_IDLE )
1080 iscsi_tx_done ( iscsi );
1085 * Receive basic header segment of an iSCSI PDU
1087 * @v iscsi iSCSI session
1088 * @v data Received data
1089 * @v len Length of received data
1090 * @v remaining Data remaining after this data
1091 * @ret rc Return status code
1093 * This fills in iscsi::rx_bhs with the data from the BHS portion of
1096 static int iscsi_rx_bhs ( struct iscsi_session *iscsi, const void *data,
1097 size_t len, size_t remaining __unused ) {
1098 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
1099 if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
1100 DBGC ( iscsi, "iSCSI %p received PDU opcode %#x len %#lx\n",
1101 iscsi, iscsi->rx_bhs.common.opcode,
1102 ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
1108 * Discard portion of an iSCSI PDU.
1110 * @v iscsi iSCSI session
1111 * @v data Received data
1112 * @v len Length of received data
1113 * @v remaining Data remaining after this data
1114 * @ret rc Return status code
1116 * This discards data from a portion of a received PDU.
1118 static int iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
1119 const void *data __unused, size_t len __unused,
1120 size_t remaining __unused ) {
1126 * Receive data segment of an iSCSI PDU
1128 * @v iscsi iSCSI session
1129 * @v data Received data
1130 * @v len Length of received data
1131 * @v remaining Data remaining after this data
1132 * @ret rc Return status code
1134 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
1135 * will be valid when this is called.
1137 static int iscsi_rx_data ( struct iscsi_session *iscsi, const void *data,
1138 size_t len, size_t remaining ) {
1139 struct iscsi_bhs_common_response *response
1140 = &iscsi->rx_bhs.common_response;
1142 /* Update cmdsn and statsn */
1143 iscsi->cmdsn = ntohl ( response->expcmdsn );
1144 iscsi->statsn = ntohl ( response->statsn );
1146 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
1147 case ISCSI_OPCODE_LOGIN_RESPONSE:
1148 return iscsi_rx_login_response ( iscsi, data, len, remaining );
1149 case ISCSI_OPCODE_SCSI_RESPONSE:
1150 return iscsi_rx_scsi_response ( iscsi, data, len, remaining );
1151 case ISCSI_OPCODE_DATA_IN:
1152 return iscsi_rx_data_in ( iscsi, data, len, remaining );
1153 case ISCSI_OPCODE_R2T:
1154 return iscsi_rx_r2t ( iscsi, data, len, remaining );
1158 DBGC ( iscsi, "iSCSI %p unknown opcode %02x\n", iscsi,
1167 * @v socket Transport layer interface
1168 * @v data Received data
1169 * @v len Length of received data
1170 * @ret rc Return status code
1172 * This handles received PDUs. The receive strategy is to fill in
1173 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
1174 * throw away any AHS portion, and then process each part of the data
1175 * portion as it arrives. The data processing routine therefore
1176 * always has a full copy of the BHS available, even for portions of
1177 * the data in different packets to the BHS.
1179 static int iscsi_socket_deliver_raw ( struct xfer_interface *socket,
1180 const void *data, size_t len ) {
1181 struct iscsi_session *iscsi =
1182 container_of ( socket, struct iscsi_session, socket );
1183 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
1184 int ( * rx ) ( struct iscsi_session *iscsi, const void *data,
1185 size_t len, size_t remaining );
1186 enum iscsi_rx_state next_state;
1192 switch ( iscsi->rx_state ) {
1195 iscsi->rx_len = sizeof ( iscsi->rx_bhs );
1196 next_state = ISCSI_RX_AHS;
1199 rx = iscsi_rx_discard;
1200 iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
1201 next_state = ISCSI_RX_DATA;
1205 iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
1206 next_state = ISCSI_RX_DATA_PADDING;
1208 case ISCSI_RX_DATA_PADDING:
1209 rx = iscsi_rx_discard;
1210 iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1211 next_state = ISCSI_RX_BHS;
1218 frag_len = iscsi->rx_len - iscsi->rx_offset;
1219 if ( frag_len > len )
1221 remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
1222 if ( ( rc = rx ( iscsi, data, frag_len, remaining ) ) != 0 ) {
1223 DBGC ( iscsi, "iSCSI %p could not process received "
1224 "data: %s\n", iscsi, strerror ( rc ) );
1225 iscsi_close_connection ( iscsi, rc );
1226 iscsi_scsi_done ( iscsi, rc );
1230 iscsi->rx_offset += frag_len;
1234 /* If all the data for this state has not yet been
1235 * received, stay in this state for now.
1237 if ( iscsi->rx_offset != iscsi->rx_len )
1240 iscsi->rx_state = next_state;
1241 iscsi->rx_offset = 0;
1248 * Handle stream connection closure
1250 * @v socket Transport layer interface
1251 * @v rc Reason for close
1254 static void iscsi_socket_close ( struct xfer_interface *socket, int rc ) {
1255 struct iscsi_session *iscsi =
1256 container_of ( socket, struct iscsi_session, socket );
1258 /* Even a graceful close counts as an error for iSCSI */
1262 /* Close session cleanly */
1263 iscsi_close_connection ( iscsi, rc );
1265 /* Retry connection if within the retry limit, otherwise fail */
1266 if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
1267 DBGC ( iscsi, "iSCSI %p retrying connection (retry #%d)\n",
1268 iscsi, iscsi->retry_count );
1269 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
1270 DBGC ( iscsi, "iSCSI %p could not reconnect: %s\n",
1271 iscsi, strerror ( rc ) );
1272 iscsi_scsi_done ( iscsi, rc );
1275 DBGC ( iscsi, "iSCSI %p retry count exceeded\n", iscsi );
1276 iscsi->instant_rc = rc;
1277 iscsi_scsi_done ( iscsi, rc );
1281 /** iSCSI socket operations */
1282 static struct xfer_interface_operations iscsi_socket_operations = {
1283 .close = iscsi_socket_close,
1284 .vredirect = xfer_vopen,
1285 .seek = ignore_xfer_seek,
1286 .window = unlimited_xfer_window,
1287 .alloc_iob = default_xfer_alloc_iob,
1288 .deliver_iob = xfer_deliver_as_raw,
1289 .deliver_raw = iscsi_socket_deliver_raw,
1293 /****************************************************************************
1295 * iSCSI command issuing
1300 * Issue SCSI command
1302 * @v scsi SCSI device
1303 * @v command SCSI command
1304 * @ret rc Return status code
1306 static int iscsi_command ( struct scsi_device *scsi,
1307 struct scsi_command *command ) {
1308 struct iscsi_session *iscsi =
1309 container_of ( scsi->backend, struct iscsi_session, refcnt );
1312 /* Record SCSI command */
1313 iscsi->command = command;
1315 /* Abort immediately if we have a recorded permanent failure */
1316 if ( iscsi->instant_rc ) {
1317 rc = iscsi->instant_rc;
1321 /* Issue command or open connection as appropriate */
1322 if ( iscsi->status ) {
1323 iscsi_start_command ( iscsi );
1325 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 )
1329 /* Wait for command to complete */
1330 iscsi->rc = -EINPROGRESS;
1331 while ( iscsi->rc == -EINPROGRESS )
1336 iscsi->command = NULL;
1340 static int iscsi_detached_command ( struct scsi_device *scsi __unused,
1341 struct scsi_command *command __unused ) {
1346 * Shut down iSCSI interface
1348 * @v scsi SCSI device
1350 void iscsi_detach ( struct scsi_device *scsi ) {
1351 struct iscsi_session *iscsi =
1352 container_of ( scsi->backend, struct iscsi_session, refcnt );
1354 xfer_nullify ( &iscsi->socket );
1355 iscsi_close_connection ( iscsi, 0 );
1356 process_del ( &iscsi->process );
1357 scsi->command = iscsi_detached_command;
1358 ref_put ( scsi->backend );
1359 scsi->backend = NULL;
1362 /****************************************************************************
1368 /** iSCSI root path components (as per RFC4173) */
1369 enum iscsi_root_path_component {
1382 * @v iscsi iSCSI session
1383 * @v lun_string LUN string representation (as per RFC4173)
1384 * @ret rc Return status code
1386 static int iscsi_parse_lun ( struct iscsi_session *iscsi,
1387 const char *lun_string ) {
1388 char *p = ( char * ) lun_string;
1395 /* Empty LUN; assume LUN 0 */
1396 if ( ! *lun_string )
1399 for ( i = 0 ; i < 4 ; i++ ) {
1400 lun.u16[i] = strtoul ( p, &p, 16 );
1408 iscsi->lun = lun.u64;
1413 * Parse iSCSI root path
1415 * @v iscsi iSCSI session
1416 * @v root_path iSCSI root path (as per RFC4173)
1417 * @ret rc Return status code
1419 static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
1420 const char *root_path ) {
1421 char rp_copy[ strlen ( root_path ) + 1 ];
1422 char *rp_comp[NUM_RP_COMPONENTS];
1427 /* Split root path into component parts */
1428 strcpy ( rp_copy, root_path );
1431 if ( i == NUM_RP_COMPONENTS )
1433 for ( ; *rp != ':' ; rp++ ) {
1435 DBGC ( iscsi, "iSCSI %p root path \"%s\" "
1436 "too short\n", iscsi, root_path );
1443 /* Use root path components to configure iSCSI session */
1444 iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
1445 if ( ! iscsi->target_address )
1447 iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
1448 if ( ! iscsi->target_port )
1449 iscsi->target_port = ISCSI_PORT;
1450 if ( ( rc = iscsi_parse_lun ( iscsi, rp_comp[RP_LUN] ) ) != 0 ) {
1451 DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
1452 iscsi, rp_comp[RP_LUN] );
1455 iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
1456 if ( ! iscsi->target_iqn )
1463 * Attach iSCSI interface
1465 * @v scsi SCSI device
1466 * @v root_path iSCSI root path (as per RFC4173)
1467 * @ret rc Return status code
1469 int iscsi_attach ( struct scsi_device *scsi, const char *root_path ) {
1470 struct iscsi_session *iscsi;
1473 /* Allocate and initialise structure */
1474 iscsi = zalloc ( sizeof ( *iscsi ) );
1477 iscsi->refcnt.free = iscsi_free;
1478 xfer_init ( &iscsi->socket, &iscsi_socket_operations, &iscsi->refcnt );
1479 process_init ( &iscsi->process, iscsi_tx_step, &iscsi->refcnt );
1481 /* Parse root path */
1482 if ( ( rc = iscsi_parse_root_path ( iscsi, root_path ) ) != 0 )
1486 if ( ! iscsi->target_address ) {
1487 DBGC ( iscsi, "iSCSI %p does not yet support discovery\n",
1492 if ( ! iscsi->target_iqn ) {
1493 DBGC ( iscsi, "iSCSI %p no target address supplied in %s\n",
1499 /* Attach parent interface, mortalise self, and return */
1500 scsi->backend = ref_get ( &iscsi->refcnt );
1501 scsi->command = iscsi_command;
1502 scsi->lun = iscsi->lun;
1503 ref_put ( &iscsi->refcnt );
1507 ref_put ( &iscsi->refcnt );
1511 /****************************************************************************
1513 * DHCP option applicators
1518 * Apply DHCP iSCSI option
1520 * @v tag DHCP option tag
1521 * @v option DHCP option
1522 * @ret rc Return status code
1524 static int apply_dhcp_iscsi_string ( unsigned int tag,
1525 struct dhcp_option *option ) {
1532 /* Identify string and prefix */
1534 case DHCP_ISCSI_INITIATOR_IQN:
1535 string = &iscsi_initiator_iqn;
1537 case DHCP_EB_USERNAME:
1538 string = &iscsi_username;
1540 case DHCP_EB_PASSWORD:
1541 string = &iscsi_password;
1543 case DHCP_HOST_NAME:
1544 string = &iscsi_default_initiator_iqn;
1545 prefix = "iqn.2000-09.org.etherboot:";
1552 /* Free old string */
1556 /* Allocate and fill new string */
1557 prefix_len = strlen ( prefix );
1558 len = ( prefix_len + option->len + 1 );
1559 p = *string = malloc ( len );
1562 strcpy ( p, prefix );
1563 dhcp_snprintf ( ( p + prefix_len ), ( len - prefix_len ), option );
1567 /** DHCP iSCSI option applicators */
1568 struct dhcp_option_applicator dhcp_iscsi_applicators[] __dhcp_applicator = {
1570 .tag = DHCP_ISCSI_INITIATOR_IQN,
1571 .apply = apply_dhcp_iscsi_string,
1574 .tag = DHCP_EB_USERNAME,
1575 .apply = apply_dhcp_iscsi_string,
1578 .tag = DHCP_EB_PASSWORD,
1579 .apply = apply_dhcp_iscsi_string,
1582 .tag = DHCP_HOST_NAME,
1583 .apply = apply_dhcp_iscsi_string,