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 static char *iscsi_explicit_initiator_iqn;
46 /** Default iSCSI initiator name (constructed from hostname) */
47 static char *iscsi_default_initiator_iqn;
50 static char *iscsi_username;
53 static char *iscsi_password;
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 free ( iscsi->username );
82 free ( iscsi->password );
83 chap_finish ( &iscsi->chap );
84 iscsi_rx_buffered_data_done ( iscsi );
89 * Open iSCSI transport-layer connection
91 * @v iscsi iSCSI session
92 * @ret rc Return status code
94 static int iscsi_open_connection ( struct iscsi_session *iscsi ) {
95 struct sockaddr_tcpip target;
98 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
99 assert ( iscsi->rx_state == ISCSI_RX_BHS );
100 assert ( iscsi->rx_offset == 0 );
103 memset ( &target, 0, sizeof ( target ) );
104 target.st_port = htons ( iscsi->target_port );
105 if ( ( rc = xfer_open_named_socket ( &iscsi->socket, SOCK_STREAM,
106 ( struct sockaddr * ) &target,
107 iscsi->target_address,
109 DBGC ( iscsi, "iSCSI %p could not open socket: %s\n",
110 iscsi, strerror ( rc ) );
114 /* Enter security negotiation phase */
115 iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
116 ISCSI_STATUS_STRINGS_SECURITY );
118 /* Assign fresh initiator task tag */
122 iscsi_start_login ( iscsi );
128 * Close iSCSI transport-layer connection
130 * @v iscsi iSCSI session
131 * @v rc Reason for close
133 * Closes the transport-layer connection and resets the session state
134 * ready to attempt a fresh login.
136 static void iscsi_close_connection ( struct iscsi_session *iscsi, int rc ) {
138 /* Close all data transfer interfaces */
139 xfer_close ( &iscsi->socket, rc );
141 /* Clear connection status */
144 /* Reset TX and RX state machines */
145 iscsi->tx_state = ISCSI_TX_IDLE;
146 iscsi->rx_state = ISCSI_RX_BHS;
147 iscsi->rx_offset = 0;
149 /* Free any temporary dynamically allocated memory */
150 chap_finish ( &iscsi->chap );
151 iscsi_rx_buffered_data_done ( iscsi );
155 * Mark iSCSI SCSI operation as complete
157 * @v iscsi iSCSI session
158 * @v rc Return status code
160 * Note that iscsi_scsi_done() will not close the connection, and must
161 * therefore be called only when the internal state machines are in an
162 * appropriate state, otherwise bad things may happen on the next call
163 * to iscsi_issue(). The general rule is to call iscsi_scsi_done()
164 * only at the end of receiving a PDU; at this point the TX and RX
165 * engines should both be idle.
167 static void iscsi_scsi_done ( struct iscsi_session *iscsi, int rc ) {
169 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
171 iscsi->command = NULL;
175 /****************************************************************************
177 * iSCSI SCSI command issuing
182 * Build iSCSI SCSI command BHS
184 * @v iscsi iSCSI session
186 * We don't currently support bidirectional commands (i.e. with both
187 * Data-In and Data-Out segments); these would require providing code
188 * to generate an AHS, and there doesn't seem to be any need for it at
191 static void iscsi_start_command ( struct iscsi_session *iscsi ) {
192 struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
194 assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
196 /* Construct BHS and initiate transmission */
197 iscsi_start_tx ( iscsi );
198 command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
199 command->flags = ( ISCSI_FLAG_FINAL |
200 ISCSI_COMMAND_ATTR_SIMPLE );
201 if ( iscsi->command->data_in )
202 command->flags |= ISCSI_COMMAND_FLAG_READ;
203 if ( iscsi->command->data_out )
204 command->flags |= ISCSI_COMMAND_FLAG_WRITE;
205 /* lengths left as zero */
206 command->lun = iscsi->lun;
207 command->itt = htonl ( ++iscsi->itt );
208 command->exp_len = htonl ( iscsi->command->data_in_len |
209 iscsi->command->data_out_len );
210 command->cmdsn = htonl ( iscsi->cmdsn );
211 command->expstatsn = htonl ( iscsi->statsn + 1 );
212 memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
213 DBGC ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#x\n",
214 iscsi, SCSI_CDB_DATA ( command->cdb ),
215 ( iscsi->command->data_in ? "in" : "out" ),
216 ( iscsi->command->data_in ?
217 iscsi->command->data_in_len : iscsi->command->data_out_len ));
221 * Receive data segment of an iSCSI SCSI response PDU
223 * @v iscsi iSCSI session
224 * @v data Received data
225 * @v len Length of received data
226 * @v remaining Data remaining after this data
227 * @ret rc Return status code
229 static int iscsi_rx_scsi_response ( struct iscsi_session *iscsi,
230 const void *data, size_t len,
232 struct iscsi_bhs_scsi_response *response
233 = &iscsi->rx_bhs.scsi_response;
236 /* Capture the sense response code as it floats past, if present */
237 sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
238 if ( ( sense_offset >= 0 ) && len ) {
239 iscsi->command->sense_response =
240 * ( ( char * ) data + sense_offset );
243 /* Wait for whole SCSI response to arrive */
247 /* Record SCSI status code */
248 iscsi->command->status = response->status;
250 /* Check for errors */
251 if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
254 /* Mark as completed */
255 iscsi_scsi_done ( iscsi, 0 );
260 * Receive data segment of an iSCSI data-in PDU
262 * @v iscsi iSCSI session
263 * @v data Received data
264 * @v len Length of received data
265 * @v remaining Data remaining after this data
266 * @ret rc Return status code
268 static int iscsi_rx_data_in ( struct iscsi_session *iscsi,
269 const void *data, size_t len,
270 size_t remaining __unused ) {
271 struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
272 unsigned long offset;
274 /* Copy data to data-in buffer */
275 offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
276 assert ( iscsi->command != NULL );
277 assert ( iscsi->command->data_in );
278 assert ( ( offset + len ) <= iscsi->command->data_in_len );
279 copy_to_user ( iscsi->command->data_in, offset, data, len );
281 /* Record SCSI status, if present */
282 if ( data_in->flags & ISCSI_DATA_FLAG_STATUS )
283 iscsi->command->status = data_in->status;
285 /* If this is the end, flag as complete */
286 if ( ( offset + len ) == iscsi->command->data_in_len ) {
287 assert ( data_in->flags & ISCSI_FLAG_FINAL );
288 assert ( remaining == 0 );
289 iscsi_scsi_done ( iscsi, 0 );
296 * Receive data segment of an iSCSI R2T PDU
298 * @v iscsi iSCSI session
299 * @v data Received data
300 * @v len Length of received data
301 * @v remaining Data remaining after this data
302 * @ret rc Return status code
304 static int iscsi_rx_r2t ( struct iscsi_session *iscsi,
305 const void *data __unused, size_t len __unused,
306 size_t remaining __unused ) {
307 struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
309 /* Record transfer parameters and trigger first data-out */
310 iscsi->ttt = ntohl ( r2t->ttt );
311 iscsi->transfer_offset = ntohl ( r2t->offset );
312 iscsi->transfer_len = ntohl ( r2t->len );
313 iscsi_start_data_out ( iscsi, 0 );
319 * Build iSCSI data-out BHS
321 * @v iscsi iSCSI session
322 * @v datasn Data sequence number within the transfer
325 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
326 unsigned int datasn ) {
327 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
328 unsigned long offset;
329 unsigned long remaining;
332 /* We always send 512-byte Data-Out PDUs; this removes the
333 * need to worry about the target's MaxRecvDataSegmentLength.
335 offset = datasn * 512;
336 remaining = iscsi->transfer_len - offset;
341 /* Construct BHS and initiate transmission */
342 iscsi_start_tx ( iscsi );
343 data_out->opcode = ISCSI_OPCODE_DATA_OUT;
344 if ( len == remaining )
345 data_out->flags = ( ISCSI_FLAG_FINAL );
346 ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
347 data_out->lun = iscsi->lun;
348 data_out->itt = htonl ( iscsi->itt );
349 data_out->ttt = htonl ( iscsi->ttt );
350 data_out->expstatsn = htonl ( iscsi->statsn + 1 );
351 data_out->datasn = htonl ( datasn );
352 data_out->offset = htonl ( iscsi->transfer_offset + offset );
353 DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
354 iscsi, datasn, len );
358 * Complete iSCSI data-out PDU transmission
360 * @v iscsi iSCSI session
363 static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
364 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
366 /* If we haven't reached the end of the sequence, start
367 * sending the next data-out PDU.
369 if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
370 iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
374 * Send iSCSI data-out data segment
376 * @v iscsi iSCSI session
377 * @ret rc Return status code
379 static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
380 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
381 struct io_buffer *iobuf;
382 unsigned long offset;
385 offset = ntohl ( data_out->offset );
386 len = ISCSI_DATA_LEN ( data_out->lengths );
388 assert ( iscsi->command != NULL );
389 assert ( iscsi->command->data_out );
390 assert ( ( offset + len ) <= iscsi->command->data_out_len );
392 iobuf = xfer_alloc_iob ( &iscsi->socket, len );
396 copy_from_user ( iob_put ( iobuf, len ),
397 iscsi->command->data_out, offset, len );
399 return xfer_deliver_iob ( &iscsi->socket, iobuf );
402 /****************************************************************************
409 * Build iSCSI login request strings
411 * @v iscsi iSCSI session
413 * These are the initial set of strings sent in the first login
414 * request PDU. We want the following settings:
418 * MaxConnections is irrelevant; we make only one connection anyway
420 * ImmediateData is irrelevant; we never send immediate data
421 * MaxRecvDataSegmentLength=8192 (default; we don't care)
422 * MaxBurstLength=262144 (default; we don't care)
423 * FirstBurstLength=262144 (default; we don't care)
424 * DefaultTime2Wait=0 [2]
425 * DefaultTime2Retain=0 [2]
426 * MaxOutstandingR2T=1
428 * DataSequenceInOrder=Yes
429 * ErrorRecoveryLevel=0
431 * [1] InitialR2T has an OR resolution function, so the target may
432 * force us to use it. We therefore simplify our logic by always
435 * [2] These ensure that we can safely start a new task once we have
436 * reconnected after a failure, without having to manually tidy up
439 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
440 void *data, size_t len ) {
441 unsigned int used = 0;
444 if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
445 used += ssnprintf ( data + used, len - used,
448 "SessionType=Normal%c"
449 "AuthMethod=CHAP,None%c",
450 iscsi_initiator_iqn(), 0,
451 iscsi->target_iqn, 0, 0, 0 );
454 if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
455 used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
458 if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) &&
460 used += ssnprintf ( data + used, len - used,
461 "CHAP_N=%s%cCHAP_R=0x",
462 iscsi->username, 0 );
463 for ( i = 0 ; i < iscsi->chap.response_len ; i++ ) {
464 used += ssnprintf ( data + used, len - used, "%02x",
465 iscsi->chap.response[i] );
467 used += ssnprintf ( data + used, len - used, "%c", 0 );
470 if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
471 used += ssnprintf ( data + used, len - used,
472 "HeaderDigest=None%c"
475 "DefaultTime2Wait=0%c"
476 "DefaultTime2Retain=0%c"
477 "MaxOutstandingR2T=1%c"
478 "DataPDUInOrder=Yes%c"
479 "DataSequenceInOrder=Yes%c"
480 "ErrorRecoveryLevel=0%c",
481 0, 0, 0, 0, 0, 0, 0, 0, 0 );
488 * Build iSCSI login request BHS
490 * @v iscsi iSCSI session
492 static void iscsi_start_login ( struct iscsi_session *iscsi ) {
493 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
496 /* Construct BHS and initiate transmission */
497 iscsi_start_tx ( iscsi );
498 request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
499 ISCSI_FLAG_IMMEDIATE );
500 request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
501 ISCSI_LOGIN_FLAG_TRANSITION );
502 /* version_max and version_min left as zero */
503 len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
504 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
505 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
506 IANA_EN_FEN_SYSTEMS );
507 /* isid_iana_qual left as zero */
508 request->tsih = htons ( iscsi->tsih );
509 request->itt = htonl ( iscsi->itt );
510 /* cid left as zero */
511 request->cmdsn = htonl ( iscsi->cmdsn );
512 request->expstatsn = htonl ( iscsi->statsn + 1 );
516 * Complete iSCSI login request PDU transmission
518 * @v iscsi iSCSI session
521 static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
523 /* Clear any "strings to send" flags */
524 iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
526 /* Free any dynamically allocated storage used for login */
527 chap_finish ( &iscsi->chap );
531 * Transmit data segment of an iSCSI login request PDU
533 * @v iscsi iSCSI session
534 * @ret rc Return status code
536 * For login requests, the data segment consists of the login strings.
538 static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
539 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
540 struct io_buffer *iobuf;
543 len = ISCSI_DATA_LEN ( request->lengths );
544 iobuf = xfer_alloc_iob ( &iscsi->socket, len );
547 iob_put ( iobuf, len );
548 iscsi_build_login_request_strings ( iscsi, iobuf->data, len );
549 return xfer_deliver_iob ( &iscsi->socket, iobuf );
553 * Handle iSCSI TargetAddress text value
555 * @v iscsi iSCSI session
556 * @v value TargetAddress value
557 * @ret rc Return status code
559 static int iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
560 const char *value ) {
562 DBGC ( iscsi, "iSCSI %p will redirect to %s\n", iscsi, value );
564 /* Replace target address */
565 free ( iscsi->target_address );
566 iscsi->target_address = strdup ( value );
567 if ( ! iscsi->target_address )
573 * Handle iSCSI AuthMethod text value
575 * @v iscsi iSCSI session
576 * @v value AuthMethod value
577 * @ret rc Return status code
579 static int iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
580 const char *value ) {
582 /* If server requests CHAP, send the CHAP_A string */
583 if ( strcmp ( value, "CHAP" ) == 0 ) {
584 DBGC ( iscsi, "iSCSI %p initiating CHAP authentication\n",
586 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_ALGORITHM;
592 * Handle iSCSI CHAP_A text value
594 * @v iscsi iSCSI session
595 * @v value CHAP_A value
596 * @ret rc Return status code
598 static int iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
599 const char *value ) {
602 /* We only ever offer "5" (i.e. MD5) as an algorithm, so if
603 * the server responds with anything else it is a protocol
606 if ( strcmp ( value, "5" ) != 0 ) {
607 DBGC ( iscsi, "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
612 /* Prepare for CHAP with MD5 */
613 if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
614 DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
615 iscsi, strerror ( rc ) );
623 * Handle iSCSI CHAP_I text value
625 * @v iscsi iSCSI session
626 * @v value CHAP_I value
627 * @ret rc Return status code
629 static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
630 const char *value ) {
631 unsigned int identifier;
634 /* The CHAP identifier is an integer value */
635 identifier = strtoul ( value, &endp, 0 );
636 if ( *endp != '\0' ) {
637 DBGC ( iscsi, "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
642 /* Identifier and secret are the first two components of the
645 chap_set_identifier ( &iscsi->chap, identifier );
646 if ( iscsi->password ) {
647 chap_update ( &iscsi->chap, iscsi->password,
648 strlen ( iscsi->password ) );
655 * Handle iSCSI CHAP_C text value
657 * @v iscsi iSCSI session
658 * @v value CHAP_C value
659 * @ret rc Return status code
661 static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
662 const char *value ) {
667 /* Check and strip leading "0x" */
668 if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
669 DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge \"%s\"\n",
674 /* Process challenge an octet at a time */
675 for ( ; ( value[0] && value[1] ) ; value += 2 ) {
676 memcpy ( buf, value, 2 );
678 byte = strtoul ( buf, &endp, 16 );
679 if ( *endp != '\0' ) {
680 DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge "
681 "byte \"%s\"\n", iscsi, buf );
684 chap_update ( &iscsi->chap, &byte, sizeof ( byte ) );
687 /* Build CHAP response */
688 DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );
689 chap_respond ( &iscsi->chap );
690 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_RESPONSE;
695 /** An iSCSI text string that we want to handle */
696 struct iscsi_string_type {
699 * This is the portion up to and including the "=" sign,
700 * e.g. "InitiatorName=", "CHAP_A=", etc.
703 /** Handle iSCSI string value
705 * @v iscsi iSCSI session
706 * @v value iSCSI string value
707 * @ret rc Return status code
709 int ( * handle ) ( struct iscsi_session *iscsi, const char *value );
712 /** iSCSI text strings that we want to handle */
713 static struct iscsi_string_type iscsi_string_types[] = {
714 { "TargetAddress=", iscsi_handle_targetaddress_value },
715 { "AuthMethod=", iscsi_handle_authmethod_value },
716 { "CHAP_A=", iscsi_handle_chap_a_value },
717 { "CHAP_I=", iscsi_handle_chap_i_value },
718 { "CHAP_C=", iscsi_handle_chap_c_value },
723 * Handle iSCSI string
725 * @v iscsi iSCSI session
726 * @v string iSCSI string (in "key=value" format)
727 * @ret rc Return status code
729 static int iscsi_handle_string ( struct iscsi_session *iscsi,
730 const char *string ) {
731 struct iscsi_string_type *type;
735 for ( type = iscsi_string_types ; type->key ; type++ ) {
736 key_len = strlen ( type->key );
737 if ( strncmp ( string, type->key, key_len ) != 0 )
739 DBGC ( iscsi, "iSCSI %p handling %s\n", iscsi, string );
740 if ( ( rc = type->handle ( iscsi,
741 ( string + key_len ) ) ) != 0 ) {
742 DBGC ( iscsi, "iSCSI %p could not handle %s: %s\n",
743 iscsi, string, strerror ( rc ) );
748 DBGC ( iscsi, "iSCSI %p ignoring %s\n", iscsi, string );
753 * Handle iSCSI strings
755 * @v iscsi iSCSI session
756 * @v string iSCSI string buffer
757 * @v len Length of string buffer
758 * @ret rc Return status code
760 static int iscsi_handle_strings ( struct iscsi_session *iscsi,
761 const char *strings, size_t len ) {
765 /* Handle each string in turn, taking care not to overrun the
766 * data buffer in case of badly-terminated data.
769 string_len = ( strnlen ( strings, len ) + 1 );
770 if ( string_len > len )
772 if ( ( rc = iscsi_handle_string ( iscsi, strings ) ) != 0 )
774 strings += string_len;
781 * Receive PDU data into buffer
783 * @v iscsi iSCSI session
784 * @v data Data to receive
785 * @v len Length of data
786 * @ret rc Return status code
788 * This can be used when the RX PDU type handler wishes to buffer up
789 * all received data and process the PDU as a single unit. The caller
790 * is repsonsible for calling iscsi_rx_buffered_data_done() after
791 * processing the data.
793 static int iscsi_rx_buffered_data ( struct iscsi_session *iscsi,
794 const void *data, size_t len ) {
796 /* Allocate buffer on first call */
797 if ( ! iscsi->rx_buffer ) {
798 iscsi->rx_buffer = malloc ( iscsi->rx_len );
799 if ( ! iscsi->rx_buffer )
803 /* Copy data to buffer */
804 assert ( ( iscsi->rx_offset + len ) <= iscsi->rx_len );
805 memcpy ( ( iscsi->rx_buffer + iscsi->rx_offset ), data, len );
811 * Receive data segment of an iSCSI login response PDU
813 * @v iscsi iSCSI session
814 * @v data Received data
815 * @v len Length of received data
816 * @v remaining Data remaining after this data
817 * @ret rc Return status code
819 static int iscsi_rx_login_response ( struct iscsi_session *iscsi,
820 const void *data, size_t len,
822 struct iscsi_bhs_login_response *response
823 = &iscsi->rx_bhs.login_response;
826 /* Buffer up the PDU data */
827 if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
828 DBGC ( iscsi, "iSCSI %p could not buffer login response: %s\n",
829 iscsi, strerror ( rc ) );
835 /* Process string data and discard string buffer */
836 if ( ( rc = iscsi_handle_strings ( iscsi, iscsi->rx_buffer,
837 iscsi->rx_len ) ) != 0 )
839 iscsi_rx_buffered_data_done ( iscsi );
841 /* Check for login redirection */
842 if ( response->status_class == ISCSI_STATUS_REDIRECT ) {
843 DBGC ( iscsi, "iSCSI %p redirecting to new server\n", iscsi );
844 iscsi_close_connection ( iscsi, 0 );
845 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
846 DBGC ( iscsi, "iSCSI %p could not redirect: %s\n ",
847 iscsi, strerror ( rc ) );
853 /* Check for fatal errors */
854 if ( response->status_class != 0 ) {
855 DBGC ( iscsi, "iSCSI login failure: class %02x detail %02x\n",
856 response->status_class, response->status_detail );
857 iscsi->instant_rc = -EPERM;
861 /* Handle login transitions */
862 if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
863 switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
864 case ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION:
866 ( ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE |
867 ISCSI_STATUS_STRINGS_OPERATIONAL );
869 case ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE:
870 iscsi->status = ISCSI_STATUS_FULL_FEATURE_PHASE;
873 DBGC ( iscsi, "iSCSI %p got invalid response flags "
874 "%02x\n", iscsi, response->flags );
879 /* Send next login request PDU if we haven't reached the full
882 if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
883 ISCSI_STATUS_FULL_FEATURE_PHASE ) {
884 iscsi_start_login ( iscsi );
888 /* Reset retry count */
889 iscsi->retry_count = 0;
891 /* Record TSIH for future reference */
892 iscsi->tsih = ntohl ( response->tsih );
894 /* Send the actual SCSI command */
895 iscsi_start_command ( iscsi );
900 /****************************************************************************
902 * iSCSI to socket interface
907 * Start up a new TX PDU
909 * @v iscsi iSCSI session
911 * This initiates the process of sending a new PDU. Only one PDU may
912 * be in transit at any one time.
914 static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
915 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
917 /* Initialise TX BHS */
918 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
920 /* Flag TX engine to start transmitting */
921 iscsi->tx_state = ISCSI_TX_BHS;
927 * @v iscsi iSCSI session
928 * @ret rc Return status code
930 static int iscsi_tx_nothing ( struct iscsi_session *iscsi __unused ) {
935 * Transmit basic header segment of an iSCSI PDU
937 * @v iscsi iSCSI session
938 * @ret rc Return status code
940 static int iscsi_tx_bhs ( struct iscsi_session *iscsi ) {
941 return xfer_deliver_raw ( &iscsi->socket, &iscsi->tx_bhs,
942 sizeof ( iscsi->tx_bhs ) );
946 * Transmit data segment of an iSCSI PDU
948 * @v iscsi iSCSI session
949 * @ret rc Return status code
951 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
952 * will be valid when this is called.
954 static int iscsi_tx_data ( struct iscsi_session *iscsi ) {
955 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
957 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
958 case ISCSI_OPCODE_DATA_OUT:
959 return iscsi_tx_data_out ( iscsi );
960 case ISCSI_OPCODE_LOGIN_REQUEST:
961 return iscsi_tx_login_request ( iscsi );
963 /* Nothing to send in other states */
969 * Transmit data padding of an iSCSI PDU
971 * @v iscsi iSCSI session
972 * @ret rc Return status code
974 * Handle transmission of any data padding in a PDU data segment.
975 * iscsi::tx_bhs will be valid when this is called.
977 static int iscsi_tx_data_padding ( struct iscsi_session *iscsi ) {
978 static const char pad[] = { '\0', '\0', '\0' };
979 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
982 pad_len = ISCSI_DATA_PAD_LEN ( common->lengths );
986 return xfer_deliver_raw ( &iscsi->socket, pad, pad_len );
990 * Complete iSCSI PDU transmission
992 * @v iscsi iSCSI session
994 * Called when a PDU has been completely transmitted and the TX state
995 * machine is about to enter the idle state. iscsi::tx_bhs will be
996 * valid for the just-completed PDU when this is called.
998 static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
999 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1001 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
1002 case ISCSI_OPCODE_DATA_OUT:
1003 iscsi_data_out_done ( iscsi );
1004 case ISCSI_OPCODE_LOGIN_REQUEST:
1005 iscsi_login_request_done ( iscsi );
1013 * Transmit iSCSI PDU
1015 * @v iscsi iSCSI session
1016 * @v buf Temporary data buffer
1017 * @v len Length of temporary data buffer
1019 * Constructs data to be sent for the current TX state
1021 static void iscsi_tx_step ( struct process *process ) {
1022 struct iscsi_session *iscsi =
1023 container_of ( process, struct iscsi_session, process );
1024 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1025 int ( * tx ) ( struct iscsi_session *iscsi );
1026 enum iscsi_tx_state next_state;
1030 /* Select fragment to transmit */
1032 switch ( iscsi->tx_state ) {
1034 /* Stop processing */
1038 tx_len = sizeof ( iscsi->tx_bhs );
1039 next_state = ISCSI_TX_AHS;
1042 tx = iscsi_tx_nothing;
1044 next_state = ISCSI_TX_DATA;
1048 tx_len = ISCSI_DATA_LEN ( common->lengths );
1049 next_state = ISCSI_TX_DATA_PADDING;
1051 case ISCSI_TX_DATA_PADDING:
1052 tx = iscsi_tx_data_padding;
1053 tx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1054 next_state = ISCSI_TX_IDLE;
1061 /* Check for window availability, if needed */
1062 if ( tx_len && ( xfer_window ( &iscsi->socket ) == 0 ) ) {
1063 /* Cannot transmit at this point; stop processing */
1068 if ( ( rc = tx ( iscsi ) ) != 0 ) {
1069 DBGC ( iscsi, "iSCSI %p could not transmit: %s\n",
1070 iscsi, strerror ( rc ) );
1074 /* Move to next state */
1075 iscsi->tx_state = next_state;
1076 if ( next_state == ISCSI_TX_IDLE )
1077 iscsi_tx_done ( iscsi );
1082 * Receive basic header segment of an iSCSI PDU
1084 * @v iscsi iSCSI session
1085 * @v data Received data
1086 * @v len Length of received data
1087 * @v remaining Data remaining after this data
1088 * @ret rc Return status code
1090 * This fills in iscsi::rx_bhs with the data from the BHS portion of
1093 static int iscsi_rx_bhs ( struct iscsi_session *iscsi, const void *data,
1094 size_t len, size_t remaining __unused ) {
1095 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
1096 if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
1097 DBGC ( iscsi, "iSCSI %p received PDU opcode %#x len %#lx\n",
1098 iscsi, iscsi->rx_bhs.common.opcode,
1099 ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
1105 * Discard portion of an iSCSI PDU.
1107 * @v iscsi iSCSI session
1108 * @v data Received data
1109 * @v len Length of received data
1110 * @v remaining Data remaining after this data
1111 * @ret rc Return status code
1113 * This discards data from a portion of a received PDU.
1115 static int iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
1116 const void *data __unused, size_t len __unused,
1117 size_t remaining __unused ) {
1123 * Receive data segment of an iSCSI PDU
1125 * @v iscsi iSCSI session
1126 * @v data Received data
1127 * @v len Length of received data
1128 * @v remaining Data remaining after this data
1129 * @ret rc Return status code
1131 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
1132 * will be valid when this is called.
1134 static int iscsi_rx_data ( struct iscsi_session *iscsi, const void *data,
1135 size_t len, size_t remaining ) {
1136 struct iscsi_bhs_common_response *response
1137 = &iscsi->rx_bhs.common_response;
1139 /* Update cmdsn and statsn */
1140 iscsi->cmdsn = ntohl ( response->expcmdsn );
1141 iscsi->statsn = ntohl ( response->statsn );
1143 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
1144 case ISCSI_OPCODE_LOGIN_RESPONSE:
1145 return iscsi_rx_login_response ( iscsi, data, len, remaining );
1146 case ISCSI_OPCODE_SCSI_RESPONSE:
1147 return iscsi_rx_scsi_response ( iscsi, data, len, remaining );
1148 case ISCSI_OPCODE_DATA_IN:
1149 return iscsi_rx_data_in ( iscsi, data, len, remaining );
1150 case ISCSI_OPCODE_R2T:
1151 return iscsi_rx_r2t ( iscsi, data, len, remaining );
1155 DBGC ( iscsi, "iSCSI %p unknown opcode %02x\n", iscsi,
1164 * @v socket Transport layer interface
1165 * @v data Received data
1166 * @v len Length of received data
1167 * @ret rc Return status code
1169 * This handles received PDUs. The receive strategy is to fill in
1170 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
1171 * throw away any AHS portion, and then process each part of the data
1172 * portion as it arrives. The data processing routine therefore
1173 * always has a full copy of the BHS available, even for portions of
1174 * the data in different packets to the BHS.
1176 static int iscsi_socket_deliver_raw ( struct xfer_interface *socket,
1177 const void *data, size_t len ) {
1178 struct iscsi_session *iscsi =
1179 container_of ( socket, struct iscsi_session, socket );
1180 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
1181 int ( * rx ) ( struct iscsi_session *iscsi, const void *data,
1182 size_t len, size_t remaining );
1183 enum iscsi_rx_state next_state;
1189 switch ( iscsi->rx_state ) {
1192 iscsi->rx_len = sizeof ( iscsi->rx_bhs );
1193 next_state = ISCSI_RX_AHS;
1196 rx = iscsi_rx_discard;
1197 iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
1198 next_state = ISCSI_RX_DATA;
1202 iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
1203 next_state = ISCSI_RX_DATA_PADDING;
1205 case ISCSI_RX_DATA_PADDING:
1206 rx = iscsi_rx_discard;
1207 iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1208 next_state = ISCSI_RX_BHS;
1215 frag_len = iscsi->rx_len - iscsi->rx_offset;
1216 if ( frag_len > len )
1218 remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
1219 if ( ( rc = rx ( iscsi, data, frag_len, remaining ) ) != 0 ) {
1220 DBGC ( iscsi, "iSCSI %p could not process received "
1221 "data: %s\n", iscsi, strerror ( rc ) );
1222 iscsi_close_connection ( iscsi, rc );
1223 iscsi_scsi_done ( iscsi, rc );
1227 iscsi->rx_offset += frag_len;
1231 /* If all the data for this state has not yet been
1232 * received, stay in this state for now.
1234 if ( iscsi->rx_offset != iscsi->rx_len )
1237 iscsi->rx_state = next_state;
1238 iscsi->rx_offset = 0;
1245 * Handle stream connection closure
1247 * @v socket Transport layer interface
1248 * @v rc Reason for close
1251 static void iscsi_socket_close ( struct xfer_interface *socket, int rc ) {
1252 struct iscsi_session *iscsi =
1253 container_of ( socket, struct iscsi_session, socket );
1255 /* Even a graceful close counts as an error for iSCSI */
1259 /* Close session cleanly */
1260 iscsi_close_connection ( iscsi, rc );
1262 /* Retry connection if within the retry limit, otherwise fail */
1263 if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
1264 DBGC ( iscsi, "iSCSI %p retrying connection (retry #%d)\n",
1265 iscsi, iscsi->retry_count );
1266 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
1267 DBGC ( iscsi, "iSCSI %p could not reconnect: %s\n",
1268 iscsi, strerror ( rc ) );
1269 iscsi_scsi_done ( iscsi, rc );
1272 DBGC ( iscsi, "iSCSI %p retry count exceeded\n", iscsi );
1273 iscsi->instant_rc = rc;
1274 iscsi_scsi_done ( iscsi, rc );
1279 * Handle redirection event
1281 * @v socket Transport layer interface
1282 * @v type Location type
1283 * @v args Remaining arguments depend upon location type
1284 * @ret rc Return status code
1286 static int iscsi_vredirect ( struct xfer_interface *socket, int type,
1288 struct iscsi_session *iscsi =
1289 container_of ( socket, struct iscsi_session, socket );
1291 struct sockaddr *peer;
1293 /* Intercept redirects to a LOCATION_SOCKET and record the IP
1294 * address for the iBFT. This is a bit of a hack, but avoids
1295 * inventing an ioctl()-style call to retrieve the socket
1296 * address from a data-xfer interface.
1298 if ( type == LOCATION_SOCKET ) {
1299 va_copy ( tmp, args );
1300 ( void ) va_arg ( tmp, int ); /* Discard "semantics" */
1301 peer = va_arg ( tmp, struct sockaddr * );
1302 memcpy ( &iscsi->target_sockaddr, peer,
1303 sizeof ( iscsi->target_sockaddr ) );
1307 return xfer_vopen ( socket, type, args );
1311 /** iSCSI socket operations */
1312 static struct xfer_interface_operations iscsi_socket_operations = {
1313 .close = iscsi_socket_close,
1314 .vredirect = iscsi_vredirect,
1315 .seek = ignore_xfer_seek,
1316 .window = unlimited_xfer_window,
1317 .alloc_iob = default_xfer_alloc_iob,
1318 .deliver_iob = xfer_deliver_as_raw,
1319 .deliver_raw = iscsi_socket_deliver_raw,
1323 /****************************************************************************
1325 * iSCSI command issuing
1330 * Issue SCSI command
1332 * @v scsi SCSI device
1333 * @v command SCSI command
1334 * @ret rc Return status code
1336 static int iscsi_command ( struct scsi_device *scsi,
1337 struct scsi_command *command ) {
1338 struct iscsi_session *iscsi =
1339 container_of ( scsi->backend, struct iscsi_session, refcnt );
1342 /* Record SCSI command */
1343 iscsi->command = command;
1345 /* Abort immediately if we have a recorded permanent failure */
1346 if ( iscsi->instant_rc ) {
1347 rc = iscsi->instant_rc;
1351 /* Issue command or open connection as appropriate */
1352 if ( iscsi->status ) {
1353 iscsi_start_command ( iscsi );
1355 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 )
1359 /* Wait for command to complete */
1360 iscsi->rc = -EINPROGRESS;
1361 while ( iscsi->rc == -EINPROGRESS )
1366 iscsi->command = NULL;
1370 static int iscsi_detached_command ( struct scsi_device *scsi __unused,
1371 struct scsi_command *command __unused ) {
1376 * Shut down iSCSI interface
1378 * @v scsi SCSI device
1380 void iscsi_detach ( struct scsi_device *scsi ) {
1381 struct iscsi_session *iscsi =
1382 container_of ( scsi->backend, struct iscsi_session, refcnt );
1384 xfer_nullify ( &iscsi->socket );
1385 iscsi_close_connection ( iscsi, 0 );
1386 process_del ( &iscsi->process );
1387 scsi->command = iscsi_detached_command;
1388 ref_put ( scsi->backend );
1389 scsi->backend = NULL;
1392 /****************************************************************************
1398 /** iSCSI root path components (as per RFC4173) */
1399 enum iscsi_root_path_component {
1412 * @v iscsi iSCSI session
1413 * @v lun_string LUN string representation (as per RFC4173)
1414 * @ret rc Return status code
1416 static int iscsi_parse_lun ( struct iscsi_session *iscsi,
1417 const char *lun_string ) {
1418 char *p = ( char * ) lun_string;
1425 /* Empty LUN; assume LUN 0 */
1426 if ( ! *lun_string )
1429 for ( i = 0 ; i < 4 ; i++ ) {
1430 lun.u16[i] = strtoul ( p, &p, 16 );
1438 iscsi->lun = lun.u64;
1443 * Parse iSCSI root path
1445 * @v iscsi iSCSI session
1446 * @v root_path iSCSI root path (as per RFC4173)
1447 * @ret rc Return status code
1449 static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
1450 const char *root_path ) {
1451 char rp_copy[ strlen ( root_path ) + 1 ];
1452 char *rp_comp[NUM_RP_COMPONENTS];
1457 /* Split root path into component parts */
1458 strcpy ( rp_copy, root_path );
1461 if ( i == NUM_RP_COMPONENTS )
1463 for ( ; *rp != ':' ; rp++ ) {
1465 DBGC ( iscsi, "iSCSI %p root path \"%s\" "
1466 "too short\n", iscsi, root_path );
1473 /* Use root path components to configure iSCSI session */
1474 iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
1475 if ( ! iscsi->target_address )
1477 iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
1478 if ( ! iscsi->target_port )
1479 iscsi->target_port = ISCSI_PORT;
1480 if ( ( rc = iscsi_parse_lun ( iscsi, rp_comp[RP_LUN] ) ) != 0 ) {
1481 DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
1482 iscsi, rp_comp[RP_LUN] );
1485 iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
1486 if ( ! iscsi->target_iqn )
1493 * Set iSCSI authentication details
1495 * @v iscsi iSCSI session
1496 * @v username Username, if any
1497 * @v password Password, if any
1498 * @ret rc Return status code
1500 static int iscsi_set_auth ( struct iscsi_session *iscsi,
1501 const char *username, const char *password ) {
1504 iscsi->username = strdup ( username );
1505 if ( ! iscsi->username )
1510 iscsi->password = strdup ( password );
1511 if ( ! iscsi->password )
1519 * Attach iSCSI interface
1521 * @v scsi SCSI device
1522 * @v root_path iSCSI root path (as per RFC4173)
1523 * @ret rc Return status code
1525 int iscsi_attach ( struct scsi_device *scsi, const char *root_path ) {
1526 struct iscsi_session *iscsi;
1529 /* Allocate and initialise structure */
1530 iscsi = zalloc ( sizeof ( *iscsi ) );
1533 iscsi->refcnt.free = iscsi_free;
1534 xfer_init ( &iscsi->socket, &iscsi_socket_operations, &iscsi->refcnt );
1535 process_init ( &iscsi->process, iscsi_tx_step, &iscsi->refcnt );
1537 /* Parse root path */
1538 if ( ( rc = iscsi_parse_root_path ( iscsi, root_path ) ) != 0 )
1540 /* Set fields not specified by root path */
1541 if ( ( rc = iscsi_set_auth ( iscsi, iscsi_username,
1542 iscsi_password ) ) != 0 )
1546 if ( ! iscsi->target_address ) {
1547 DBGC ( iscsi, "iSCSI %p does not yet support discovery\n",
1552 if ( ! iscsi->target_iqn ) {
1553 DBGC ( iscsi, "iSCSI %p no target address supplied in %s\n",
1559 /* Attach parent interface, mortalise self, and return */
1560 scsi->backend = ref_get ( &iscsi->refcnt );
1561 scsi->command = iscsi_command;
1562 scsi->lun = iscsi->lun;
1563 ref_put ( &iscsi->refcnt );
1567 ref_put ( &iscsi->refcnt );
1571 /****************************************************************************
1573 * DHCP option applicators
1578 * Apply DHCP iSCSI option
1580 * @v tag DHCP option tag
1581 * @v option DHCP option
1582 * @ret rc Return status code
1584 static int apply_dhcp_iscsi_string ( unsigned int tag,
1585 struct dhcp_option *option ) {
1592 /* Identify string and prefix */
1594 case DHCP_ISCSI_INITIATOR_IQN:
1595 string = &iscsi_explicit_initiator_iqn;
1597 case DHCP_EB_USERNAME:
1598 string = &iscsi_username;
1600 case DHCP_EB_PASSWORD:
1601 string = &iscsi_password;
1603 case DHCP_HOST_NAME:
1604 string = &iscsi_default_initiator_iqn;
1605 prefix = "iqn.2000-09.org.etherboot:";
1612 /* Free old string */
1616 /* Allocate and fill new string */
1617 prefix_len = strlen ( prefix );
1618 len = ( prefix_len + option->len + 1 );
1619 p = *string = malloc ( len );
1622 strcpy ( p, prefix );
1623 dhcp_snprintf ( ( p + prefix_len ), ( len - prefix_len ), option );
1627 /** DHCP iSCSI option applicators */
1628 struct dhcp_option_applicator dhcp_iscsi_applicators[] __dhcp_applicator = {
1630 .tag = DHCP_ISCSI_INITIATOR_IQN,
1631 .apply = apply_dhcp_iscsi_string,
1634 .tag = DHCP_EB_USERNAME,
1635 .apply = apply_dhcp_iscsi_string,
1638 .tag = DHCP_EB_PASSWORD,
1639 .apply = apply_dhcp_iscsi_string,
1642 .tag = DHCP_HOST_NAME,
1643 .apply = apply_dhcp_iscsi_string,
1647 /****************************************************************************
1654 * Get iSCSI initiator IQN
1656 * @v iscsi iSCSI session
1657 * @ret rc Return status code
1659 const char * iscsi_initiator_iqn ( void ) {
1661 if ( iscsi_explicit_initiator_iqn )
1662 return iscsi_explicit_initiator_iqn;
1663 if ( iscsi_default_initiator_iqn )
1664 return iscsi_default_initiator_iqn;
1665 return "iqn.2000-09.org.etherboot:UNKNOWN";