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/settings.h>
35 #include <gpxe/features.h>
36 #include <gpxe/iscsi.h>
44 FEATURE ( FEATURE_PROTOCOL, "iSCSI", DHCP_EB_FEATURE_ISCSI, 1 );
46 /** iSCSI initiator name (explicitly specified) */
47 static char *iscsi_explicit_initiator_iqn;
49 /** Default iSCSI initiator name (constructed from hostname) */
50 static char *iscsi_default_initiator_iqn;
52 /** iSCSI initiator username */
53 static char *iscsi_initiator_username;
55 /** iSCSI initiator password */
56 static char *iscsi_initiator_password;
58 /** iSCSI target username */
59 static char *iscsi_target_username;
61 /** iSCSI target password */
62 static char *iscsi_target_password;
64 static void iscsi_start_tx ( struct iscsi_session *iscsi );
65 static void iscsi_start_login ( struct iscsi_session *iscsi );
66 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
67 unsigned int datasn );
70 * Finish receiving PDU data into buffer
72 * @v iscsi iSCSI session
74 static void iscsi_rx_buffered_data_done ( struct iscsi_session *iscsi ) {
75 free ( iscsi->rx_buffer );
76 iscsi->rx_buffer = NULL;
82 * @v refcnt Reference counter
84 static void iscsi_free ( struct refcnt *refcnt ) {
85 struct iscsi_session *iscsi =
86 container_of ( refcnt, struct iscsi_session, refcnt );
88 free ( iscsi->target_address );
89 free ( iscsi->target_iqn );
90 free ( iscsi->initiator_username );
91 free ( iscsi->initiator_password );
92 free ( iscsi->target_username );
93 free ( iscsi->target_password );
94 chap_finish ( &iscsi->chap );
95 iscsi_rx_buffered_data_done ( iscsi );
100 * Open iSCSI transport-layer connection
102 * @v iscsi iSCSI session
103 * @ret rc Return status code
105 static int iscsi_open_connection ( struct iscsi_session *iscsi ) {
106 struct sockaddr_tcpip target;
109 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
110 assert ( iscsi->rx_state == ISCSI_RX_BHS );
111 assert ( iscsi->rx_offset == 0 );
114 memset ( &target, 0, sizeof ( target ) );
115 target.st_port = htons ( iscsi->target_port );
116 if ( ( rc = xfer_open_named_socket ( &iscsi->socket, SOCK_STREAM,
117 ( struct sockaddr * ) &target,
118 iscsi->target_address,
120 DBGC ( iscsi, "iSCSI %p could not open socket: %s\n",
121 iscsi, strerror ( rc ) );
125 /* Enter security negotiation phase */
126 iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
127 ISCSI_STATUS_STRINGS_SECURITY );
129 /* Assign fresh initiator task tag */
133 iscsi_start_login ( iscsi );
139 * Close iSCSI transport-layer connection
141 * @v iscsi iSCSI session
142 * @v rc Reason for close
144 * Closes the transport-layer connection and resets the session state
145 * ready to attempt a fresh login.
147 static void iscsi_close_connection ( struct iscsi_session *iscsi, int rc ) {
149 /* Close all data transfer interfaces */
150 xfer_close ( &iscsi->socket, rc );
152 /* Clear connection status */
155 /* Deauthenticate target */
156 iscsi->target_auth_ok = 0;
158 /* Reset TX and RX state machines */
159 iscsi->tx_state = ISCSI_TX_IDLE;
160 iscsi->rx_state = ISCSI_RX_BHS;
161 iscsi->rx_offset = 0;
163 /* Free any temporary dynamically allocated memory */
164 chap_finish ( &iscsi->chap );
165 iscsi_rx_buffered_data_done ( iscsi );
169 * Mark iSCSI SCSI operation as complete
171 * @v iscsi iSCSI session
172 * @v rc Return status code
174 * Note that iscsi_scsi_done() will not close the connection, and must
175 * therefore be called only when the internal state machines are in an
176 * appropriate state, otherwise bad things may happen on the next call
177 * to iscsi_issue(). The general rule is to call iscsi_scsi_done()
178 * only at the end of receiving a PDU; at this point the TX and RX
179 * engines should both be idle.
181 static void iscsi_scsi_done ( struct iscsi_session *iscsi, int rc ) {
183 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
185 iscsi->command = NULL;
189 /****************************************************************************
191 * iSCSI SCSI command issuing
196 * Build iSCSI SCSI command BHS
198 * @v iscsi iSCSI session
200 * We don't currently support bidirectional commands (i.e. with both
201 * Data-In and Data-Out segments); these would require providing code
202 * to generate an AHS, and there doesn't seem to be any need for it at
205 static void iscsi_start_command ( struct iscsi_session *iscsi ) {
206 struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
208 assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
210 /* Construct BHS and initiate transmission */
211 iscsi_start_tx ( iscsi );
212 command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
213 command->flags = ( ISCSI_FLAG_FINAL |
214 ISCSI_COMMAND_ATTR_SIMPLE );
215 if ( iscsi->command->data_in )
216 command->flags |= ISCSI_COMMAND_FLAG_READ;
217 if ( iscsi->command->data_out )
218 command->flags |= ISCSI_COMMAND_FLAG_WRITE;
219 /* lengths left as zero */
220 command->lun = iscsi->lun;
221 command->itt = htonl ( ++iscsi->itt );
222 command->exp_len = htonl ( iscsi->command->data_in_len |
223 iscsi->command->data_out_len );
224 command->cmdsn = htonl ( iscsi->cmdsn );
225 command->expstatsn = htonl ( iscsi->statsn + 1 );
226 memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
227 DBGC2 ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#zx\n",
228 iscsi, SCSI_CDB_DATA ( command->cdb ),
229 ( iscsi->command->data_in ? "in" : "out" ),
230 ( iscsi->command->data_in ?
231 iscsi->command->data_in_len :
232 iscsi->command->data_out_len ) );
236 * Receive data segment of an iSCSI SCSI response PDU
238 * @v iscsi iSCSI session
239 * @v data Received data
240 * @v len Length of received data
241 * @v remaining Data remaining after this data
242 * @ret rc Return status code
244 static int iscsi_rx_scsi_response ( struct iscsi_session *iscsi,
245 const void *data, size_t len,
247 struct iscsi_bhs_scsi_response *response
248 = &iscsi->rx_bhs.scsi_response;
251 /* Capture the sense response code as it floats past, if present */
252 sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
253 if ( ( sense_offset >= 0 ) && len ) {
254 iscsi->command->sense_response =
255 * ( ( char * ) data + sense_offset );
258 /* Wait for whole SCSI response to arrive */
262 /* Record SCSI status code */
263 iscsi->command->status = response->status;
265 /* Check for errors */
266 if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
269 /* Mark as completed */
270 iscsi_scsi_done ( iscsi, 0 );
275 * Receive data segment of an iSCSI data-in PDU
277 * @v iscsi iSCSI session
278 * @v data Received data
279 * @v len Length of received data
280 * @v remaining Data remaining after this data
281 * @ret rc Return status code
283 static int iscsi_rx_data_in ( struct iscsi_session *iscsi,
284 const void *data, size_t len,
286 struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
287 unsigned long offset;
289 /* Copy data to data-in buffer */
290 offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
291 assert ( iscsi->command != NULL );
292 assert ( iscsi->command->data_in );
293 assert ( ( offset + len ) <= iscsi->command->data_in_len );
294 copy_to_user ( iscsi->command->data_in, offset, data, len );
296 /* Wait for whole SCSI response to arrive */
300 /* Mark as completed if status is present */
301 if ( data_in->flags & ISCSI_DATA_FLAG_STATUS ) {
302 assert ( ( offset + len ) == iscsi->command->data_in_len );
303 assert ( data_in->flags & ISCSI_FLAG_FINAL );
304 iscsi->command->status = data_in->status;
305 /* iSCSI cannot return an error status via a data-in */
306 iscsi_scsi_done ( iscsi, 0 );
313 * Receive data segment of an iSCSI R2T PDU
315 * @v iscsi iSCSI session
316 * @v data Received data
317 * @v len Length of received data
318 * @v remaining Data remaining after this data
319 * @ret rc Return status code
321 static int iscsi_rx_r2t ( struct iscsi_session *iscsi,
322 const void *data __unused, size_t len __unused,
323 size_t remaining __unused ) {
324 struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
326 /* Record transfer parameters and trigger first data-out */
327 iscsi->ttt = ntohl ( r2t->ttt );
328 iscsi->transfer_offset = ntohl ( r2t->offset );
329 iscsi->transfer_len = ntohl ( r2t->len );
330 iscsi_start_data_out ( iscsi, 0 );
336 * Build iSCSI data-out BHS
338 * @v iscsi iSCSI session
339 * @v datasn Data sequence number within the transfer
342 static void iscsi_start_data_out ( struct iscsi_session *iscsi,
343 unsigned int datasn ) {
344 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
345 unsigned long offset;
346 unsigned long remaining;
349 /* We always send 512-byte Data-Out PDUs; this removes the
350 * need to worry about the target's MaxRecvDataSegmentLength.
352 offset = datasn * 512;
353 remaining = iscsi->transfer_len - offset;
358 /* Construct BHS and initiate transmission */
359 iscsi_start_tx ( iscsi );
360 data_out->opcode = ISCSI_OPCODE_DATA_OUT;
361 if ( len == remaining )
362 data_out->flags = ( ISCSI_FLAG_FINAL );
363 ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
364 data_out->lun = iscsi->lun;
365 data_out->itt = htonl ( iscsi->itt );
366 data_out->ttt = htonl ( iscsi->ttt );
367 data_out->expstatsn = htonl ( iscsi->statsn + 1 );
368 data_out->datasn = htonl ( datasn );
369 data_out->offset = htonl ( iscsi->transfer_offset + offset );
370 DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
371 iscsi, datasn, len );
375 * Complete iSCSI data-out PDU transmission
377 * @v iscsi iSCSI session
380 static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
381 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
383 /* If we haven't reached the end of the sequence, start
384 * sending the next data-out PDU.
386 if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
387 iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
391 * Send iSCSI data-out data segment
393 * @v iscsi iSCSI session
394 * @ret rc Return status code
396 static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
397 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
398 struct io_buffer *iobuf;
399 unsigned long offset;
402 offset = ntohl ( data_out->offset );
403 len = ISCSI_DATA_LEN ( data_out->lengths );
405 assert ( iscsi->command != NULL );
406 assert ( iscsi->command->data_out );
407 assert ( ( offset + len ) <= iscsi->command->data_out_len );
409 iobuf = xfer_alloc_iob ( &iscsi->socket, len );
413 copy_from_user ( iob_put ( iobuf, len ),
414 iscsi->command->data_out, offset, len );
416 return xfer_deliver_iob ( &iscsi->socket, iobuf );
419 /****************************************************************************
426 * Build iSCSI login request strings
428 * @v iscsi iSCSI session
430 * These are the initial set of strings sent in the first login
431 * request PDU. We want the following settings:
435 * MaxConnections is irrelevant; we make only one connection anyway
437 * ImmediateData is irrelevant; we never send immediate data
438 * MaxRecvDataSegmentLength=8192 (default; we don't care) [3]
439 * MaxBurstLength=262144 (default; we don't care) [3]
440 * FirstBurstLength=262144 (default; we don't care)
441 * DefaultTime2Wait=0 [2]
442 * DefaultTime2Retain=0 [2]
443 * MaxOutstandingR2T=1
445 * DataSequenceInOrder=Yes
446 * ErrorRecoveryLevel=0
448 * [1] InitialR2T has an OR resolution function, so the target may
449 * force us to use it. We therefore simplify our logic by always
452 * [2] These ensure that we can safely start a new task once we have
453 * reconnected after a failure, without having to manually tidy up
456 * [3] We are quite happy to use the RFC-defined default values for
457 * these parameters, but some targets (notably OpenSolaris)
458 * incorrectly assume a default value of zero, so we explicitly
459 * specify the default values.
461 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
462 void *data, size_t len ) {
463 unsigned int used = 0;
465 const char *auth_method;
467 if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
468 /* Default to allowing no authentication */
469 auth_method = "None";
470 /* If we have a credential to supply, permit CHAP */
471 if ( iscsi->initiator_username )
472 auth_method = "CHAP,None";
473 /* If we have a credential to check, force CHAP */
474 if ( iscsi->target_username )
475 auth_method = "CHAP";
476 used += ssnprintf ( data + used, len - used,
479 "SessionType=Normal%c"
481 iscsi_initiator_iqn(), 0,
482 iscsi->target_iqn, 0, 0,
486 if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
487 used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
490 if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
491 assert ( iscsi->initiator_username != NULL );
492 used += ssnprintf ( data + used, len - used,
493 "CHAP_N=%s%cCHAP_R=0x",
494 iscsi->initiator_username, 0 );
495 for ( i = 0 ; i < iscsi->chap.response_len ; i++ ) {
496 used += ssnprintf ( data + used, len - used, "%02x",
497 iscsi->chap.response[i] );
499 used += ssnprintf ( data + used, len - used, "%c", 0 );
502 if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_CHALLENGE ) ) {
503 used += ssnprintf ( data + used, len - used,
504 "CHAP_I=%d%cCHAP_C=0x",
505 iscsi->chap_challenge[0], 0 );
506 for ( i = 1 ; i < sizeof ( iscsi->chap_challenge ) ; i++ ) {
507 used += ssnprintf ( data + used, len - used, "%02x",
508 iscsi->chap_challenge[i] );
510 used += ssnprintf ( data + used, len - used, "%c", 0 );
513 if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
514 used += ssnprintf ( data + used, len - used,
515 "HeaderDigest=None%c"
518 "MaxRecvDataSegmentLength=8192%c"
519 "MaxBurstLength=262144%c"
520 "DefaultTime2Wait=0%c"
521 "DefaultTime2Retain=0%c"
522 "MaxOutstandingR2T=1%c"
523 "DataPDUInOrder=Yes%c"
524 "DataSequenceInOrder=Yes%c"
525 "ErrorRecoveryLevel=0%c",
526 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
533 * Build iSCSI login request BHS
535 * @v iscsi iSCSI session
537 static void iscsi_start_login ( struct iscsi_session *iscsi ) {
538 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
541 /* Construct BHS and initiate transmission */
542 iscsi_start_tx ( iscsi );
543 request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
544 ISCSI_FLAG_IMMEDIATE );
545 request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
546 ISCSI_LOGIN_FLAG_TRANSITION );
547 /* version_max and version_min left as zero */
548 len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
549 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
550 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
551 IANA_EN_FEN_SYSTEMS );
552 /* isid_iana_qual left as zero */
553 request->tsih = htons ( iscsi->tsih );
554 request->itt = htonl ( iscsi->itt );
555 /* cid left as zero */
556 request->cmdsn = htonl ( iscsi->cmdsn );
557 request->expstatsn = htonl ( iscsi->statsn + 1 );
561 * Complete iSCSI login request PDU transmission
563 * @v iscsi iSCSI session
566 static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
568 /* Clear any "strings to send" flags */
569 iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
571 /* Free any dynamically allocated storage used for login */
572 chap_finish ( &iscsi->chap );
576 * Transmit data segment of an iSCSI login request PDU
578 * @v iscsi iSCSI session
579 * @ret rc Return status code
581 * For login requests, the data segment consists of the login strings.
583 static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
584 struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
585 struct io_buffer *iobuf;
588 len = ISCSI_DATA_LEN ( request->lengths );
589 iobuf = xfer_alloc_iob ( &iscsi->socket, len );
592 iob_put ( iobuf, len );
593 iscsi_build_login_request_strings ( iscsi, iobuf->data, len );
594 return xfer_deliver_iob ( &iscsi->socket, iobuf );
598 * Handle iSCSI TargetAddress text value
600 * @v iscsi iSCSI session
601 * @v value TargetAddress value
602 * @ret rc Return status code
604 static int iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
605 const char *value ) {
608 DBGC ( iscsi, "iSCSI %p will redirect to %s\n", iscsi, value );
610 /* Replace target address */
611 free ( iscsi->target_address );
612 iscsi->target_address = strdup ( value );
613 if ( ! iscsi->target_address )
616 /* Replace target port */
617 iscsi->target_port = htons ( ISCSI_PORT );
618 separator = strchr ( iscsi->target_address, ':' );
621 iscsi->target_port = strtoul ( ( separator + 1 ), NULL, 0 );
628 * Handle iSCSI AuthMethod text value
630 * @v iscsi iSCSI session
631 * @v value AuthMethod value
632 * @ret rc Return status code
634 static int iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
635 const char *value ) {
637 /* Mark target as authenticated if no authentication required */
638 if ( ! iscsi->target_username )
639 iscsi->target_auth_ok = 1;
641 /* If server requests CHAP, send the CHAP_A string */
642 if ( strcmp ( value, "CHAP" ) == 0 ) {
643 DBGC ( iscsi, "iSCSI %p initiating CHAP authentication\n",
645 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_ALGORITHM;
652 * Handle iSCSI CHAP_A text value
654 * @v iscsi iSCSI session
655 * @v value CHAP_A value
656 * @ret rc Return status code
658 static int iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
659 const char *value ) {
661 /* We only ever offer "5" (i.e. MD5) as an algorithm, so if
662 * the server responds with anything else it is a protocol
665 if ( strcmp ( value, "5" ) != 0 ) {
666 DBGC ( iscsi, "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
675 * Handle iSCSI CHAP_I text value
677 * @v iscsi iSCSI session
678 * @v value CHAP_I value
679 * @ret rc Return status code
681 static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
682 const char *value ) {
683 unsigned int identifier;
687 /* The CHAP identifier is an integer value */
688 identifier = strtoul ( value, &endp, 0 );
689 if ( *endp != '\0' ) {
690 DBGC ( iscsi, "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
695 /* Prepare for CHAP with MD5 */
696 chap_finish ( &iscsi->chap );
697 if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
698 DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
699 iscsi, strerror ( rc ) );
703 /* Identifier and secret are the first two components of the
706 chap_set_identifier ( &iscsi->chap, identifier );
707 if ( iscsi->initiator_password ) {
708 chap_update ( &iscsi->chap, iscsi->initiator_password,
709 strlen ( iscsi->initiator_password ) );
716 * Handle iSCSI CHAP_C text value
718 * @v iscsi iSCSI session
719 * @v value CHAP_C value
720 * @ret rc Return status code
722 static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
723 const char *value ) {
729 /* Check and strip leading "0x" */
730 if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
731 DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge \"%s\"\n",
737 /* Process challenge an octet at a time */
738 for ( ; ( value[0] && value[1] ) ; value += 2 ) {
739 memcpy ( buf, value, 2 );
741 byte = strtoul ( buf, &endp, 16 );
742 if ( *endp != '\0' ) {
743 DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge "
744 "byte \"%s\"\n", iscsi, buf );
747 chap_update ( &iscsi->chap, &byte, sizeof ( byte ) );
750 /* Build CHAP response */
751 DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );
752 chap_respond ( &iscsi->chap );
753 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_RESPONSE;
755 /* Send CHAP challenge, if applicable */
756 if ( iscsi->target_username ) {
757 iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_CHALLENGE;
758 /* Generate CHAP challenge data */
759 for ( i = 0 ; i < sizeof ( iscsi->chap_challenge ) ; i++ ) {
760 iscsi->chap_challenge[i] = random();
768 * Handle iSCSI CHAP_N text value
770 * @v iscsi iSCSI session
771 * @v value CHAP_N value
772 * @ret rc Return status code
774 static int iscsi_handle_chap_n_value ( struct iscsi_session *iscsi,
775 const char *value ) {
777 /* The target username isn't actually involved at any point in
778 * the authentication process; it merely serves to identify
779 * which password the target is using to generate the CHAP
780 * response. We unnecessarily verify that the username is as
781 * expected, in order to provide mildly helpful diagnostics if
782 * the target is supplying the wrong username/password
785 if ( iscsi->target_username &&
786 ( strcmp ( iscsi->target_username, value ) != 0 ) ) {
787 DBGC ( iscsi, "iSCSI %p target username \"%s\" incorrect "
789 iscsi, value, iscsi->target_username );
797 * Handle iSCSI CHAP_R text value
799 * @v iscsi iSCSI session
800 * @v value CHAP_R value
801 * @ret rc Return status code
803 static int iscsi_handle_chap_r_value ( struct iscsi_session *iscsi,
804 const char *value ) {
811 /* Generate CHAP response for verification */
812 chap_finish ( &iscsi->chap );
813 if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
814 DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
815 iscsi, strerror ( rc ) );
818 chap_set_identifier ( &iscsi->chap, iscsi->chap_challenge[0] );
819 if ( iscsi->target_password ) {
820 chap_update ( &iscsi->chap, iscsi->target_password,
821 strlen ( iscsi->target_password ) );
823 chap_update ( &iscsi->chap, &iscsi->chap_challenge[1],
824 ( sizeof ( iscsi->chap_challenge ) - 1 ) );
825 chap_respond ( &iscsi->chap );
827 /* Check and strip leading "0x" */
828 if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
829 DBGC ( iscsi, "iSCSI %p saw invalid CHAP response \"%s\"\n",
835 /* Check CHAP response length */
836 if ( strlen ( value ) != ( 2 * iscsi->chap.response_len ) ) {
837 DBGC ( iscsi, "iSCSI %p invalid CHAP response length\n",
842 /* Process response an octet at a time */
843 for ( i = 0 ; ( value[0] && value[1] ) ; value += 2, i++ ) {
844 memcpy ( buf, value, 2 );
846 byte = strtoul ( buf, &endp, 16 );
847 if ( *endp != '\0' ) {
848 DBGC ( iscsi, "iSCSI %p saw invalid CHAP response "
849 "byte \"%s\"\n", iscsi, buf );
852 if ( byte != iscsi->chap.response[i] ) {
853 DBGC ( iscsi, "iSCSI %p saw incorrect CHAP "
854 "response\n", iscsi );
858 assert ( i == iscsi->chap.response_len );
860 /* Mark session as authenticated */
861 iscsi->target_auth_ok = 1;
866 /** An iSCSI text string that we want to handle */
867 struct iscsi_string_type {
870 * This is the portion up to and including the "=" sign,
871 * e.g. "InitiatorName=", "CHAP_A=", etc.
874 /** Handle iSCSI string value
876 * @v iscsi iSCSI session
877 * @v value iSCSI string value
878 * @ret rc Return status code
880 int ( * handle ) ( struct iscsi_session *iscsi, const char *value );
883 /** iSCSI text strings that we want to handle */
884 static struct iscsi_string_type iscsi_string_types[] = {
885 { "TargetAddress=", iscsi_handle_targetaddress_value },
886 { "AuthMethod=", iscsi_handle_authmethod_value },
887 { "CHAP_A=", iscsi_handle_chap_a_value },
888 { "CHAP_I=", iscsi_handle_chap_i_value },
889 { "CHAP_C=", iscsi_handle_chap_c_value },
890 { "CHAP_N=", iscsi_handle_chap_n_value },
891 { "CHAP_R=", iscsi_handle_chap_r_value },
896 * Handle iSCSI string
898 * @v iscsi iSCSI session
899 * @v string iSCSI string (in "key=value" format)
900 * @ret rc Return status code
902 static int iscsi_handle_string ( struct iscsi_session *iscsi,
903 const char *string ) {
904 struct iscsi_string_type *type;
908 for ( type = iscsi_string_types ; type->key ; type++ ) {
909 key_len = strlen ( type->key );
910 if ( strncmp ( string, type->key, key_len ) != 0 )
912 DBGC ( iscsi, "iSCSI %p handling %s\n", iscsi, string );
913 if ( ( rc = type->handle ( iscsi,
914 ( string + key_len ) ) ) != 0 ) {
915 DBGC ( iscsi, "iSCSI %p could not handle %s: %s\n",
916 iscsi, string, strerror ( rc ) );
921 DBGC ( iscsi, "iSCSI %p ignoring %s\n", iscsi, string );
926 * Handle iSCSI strings
928 * @v iscsi iSCSI session
929 * @v string iSCSI string buffer
930 * @v len Length of string buffer
931 * @ret rc Return status code
933 static int iscsi_handle_strings ( struct iscsi_session *iscsi,
934 const char *strings, size_t len ) {
938 /* Handle each string in turn, taking care not to overrun the
939 * data buffer in case of badly-terminated data.
942 string_len = ( strnlen ( strings, len ) + 1 );
943 if ( string_len > len )
945 if ( ( rc = iscsi_handle_string ( iscsi, strings ) ) != 0 )
947 strings += string_len;
954 * Receive PDU data into buffer
956 * @v iscsi iSCSI session
957 * @v data Data to receive
958 * @v len Length of data
959 * @ret rc Return status code
961 * This can be used when the RX PDU type handler wishes to buffer up
962 * all received data and process the PDU as a single unit. The caller
963 * is repsonsible for calling iscsi_rx_buffered_data_done() after
964 * processing the data.
966 static int iscsi_rx_buffered_data ( struct iscsi_session *iscsi,
967 const void *data, size_t len ) {
969 /* Allocate buffer on first call */
970 if ( ! iscsi->rx_buffer ) {
971 iscsi->rx_buffer = malloc ( iscsi->rx_len );
972 if ( ! iscsi->rx_buffer )
976 /* Copy data to buffer */
977 assert ( ( iscsi->rx_offset + len ) <= iscsi->rx_len );
978 memcpy ( ( iscsi->rx_buffer + iscsi->rx_offset ), data, len );
984 * Convert iSCSI response status to return status code
986 * @v status_class iSCSI status class
987 * @v status_detail iSCSI status detail
988 * @ret rc Return status code
990 static int iscsi_status_to_rc ( unsigned int status_class,
991 unsigned int status_detail ) {
992 switch ( status_class ) {
993 case ISCSI_STATUS_INITIATOR_ERROR :
994 switch ( status_detail ) {
995 case ISCSI_STATUS_INITIATOR_ERROR_AUTHENTICATION :
997 case ISCSI_STATUS_INITIATOR_ERROR_AUTHORISATION :
999 case ISCSI_STATUS_INITIATOR_ERROR_NOT_FOUND :
1000 case ISCSI_STATUS_INITIATOR_ERROR_REMOVED :
1005 case ISCSI_STATUS_TARGET_ERROR :
1013 * Receive data segment of an iSCSI login response PDU
1015 * @v iscsi iSCSI session
1016 * @v data Received data
1017 * @v len Length of received data
1018 * @v remaining Data remaining after this data
1019 * @ret rc Return status code
1021 static int iscsi_rx_login_response ( struct iscsi_session *iscsi,
1022 const void *data, size_t len,
1023 size_t remaining ) {
1024 struct iscsi_bhs_login_response *response
1025 = &iscsi->rx_bhs.login_response;
1028 /* Buffer up the PDU data */
1029 if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
1030 DBGC ( iscsi, "iSCSI %p could not buffer login response: %s\n",
1031 iscsi, strerror ( rc ) );
1037 /* Process string data and discard string buffer */
1038 if ( ( rc = iscsi_handle_strings ( iscsi, iscsi->rx_buffer,
1039 iscsi->rx_len ) ) != 0 )
1041 iscsi_rx_buffered_data_done ( iscsi );
1043 /* Check for login redirection */
1044 if ( response->status_class == ISCSI_STATUS_REDIRECT ) {
1045 DBGC ( iscsi, "iSCSI %p redirecting to new server\n", iscsi );
1046 iscsi_close_connection ( iscsi, 0 );
1047 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
1048 DBGC ( iscsi, "iSCSI %p could not redirect: %s\n ",
1049 iscsi, strerror ( rc ) );
1055 /* Check for fatal errors */
1056 if ( response->status_class != 0 ) {
1057 DBGC ( iscsi, "iSCSI login failure: class %02x detail %02x\n",
1058 response->status_class, response->status_detail );
1059 rc = iscsi_status_to_rc ( response->status_class,
1060 response->status_detail );
1061 iscsi->instant_rc = rc;
1065 /* Handle login transitions */
1066 if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
1067 switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
1068 case ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION:
1070 ( ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE |
1071 ISCSI_STATUS_STRINGS_OPERATIONAL );
1073 case ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE:
1074 iscsi->status = ISCSI_STATUS_FULL_FEATURE_PHASE;
1077 DBGC ( iscsi, "iSCSI %p got invalid response flags "
1078 "%02x\n", iscsi, response->flags );
1083 /* Send next login request PDU if we haven't reached the full
1084 * feature phase yet.
1086 if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
1087 ISCSI_STATUS_FULL_FEATURE_PHASE ) {
1088 iscsi_start_login ( iscsi );
1092 /* Check that target authentication was successful (if required) */
1093 if ( ! iscsi->target_auth_ok ) {
1094 DBGC ( iscsi, "iSCSI %p nefarious target tried to bypass "
1095 "authentication\n", iscsi );
1099 /* Reset retry count */
1100 iscsi->retry_count = 0;
1102 /* Record TSIH for future reference */
1103 iscsi->tsih = ntohl ( response->tsih );
1105 /* Send the actual SCSI command */
1106 iscsi_start_command ( iscsi );
1111 /****************************************************************************
1113 * iSCSI to socket interface
1118 * Start up a new TX PDU
1120 * @v iscsi iSCSI session
1122 * This initiates the process of sending a new PDU. Only one PDU may
1123 * be in transit at any one time.
1125 static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
1126 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
1128 /* Initialise TX BHS */
1129 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
1131 /* Flag TX engine to start transmitting */
1132 iscsi->tx_state = ISCSI_TX_BHS;
1138 * @v iscsi iSCSI session
1139 * @ret rc Return status code
1141 static int iscsi_tx_nothing ( struct iscsi_session *iscsi __unused ) {
1146 * Transmit basic header segment of an iSCSI PDU
1148 * @v iscsi iSCSI session
1149 * @ret rc Return status code
1151 static int iscsi_tx_bhs ( struct iscsi_session *iscsi ) {
1152 return xfer_deliver_raw ( &iscsi->socket, &iscsi->tx_bhs,
1153 sizeof ( iscsi->tx_bhs ) );
1157 * Transmit data segment of an iSCSI PDU
1159 * @v iscsi iSCSI session
1160 * @ret rc Return status code
1162 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
1163 * will be valid when this is called.
1165 static int iscsi_tx_data ( struct iscsi_session *iscsi ) {
1166 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1168 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
1169 case ISCSI_OPCODE_DATA_OUT:
1170 return iscsi_tx_data_out ( iscsi );
1171 case ISCSI_OPCODE_LOGIN_REQUEST:
1172 return iscsi_tx_login_request ( iscsi );
1174 /* Nothing to send in other states */
1180 * Transmit data padding of an iSCSI PDU
1182 * @v iscsi iSCSI session
1183 * @ret rc Return status code
1185 * Handle transmission of any data padding in a PDU data segment.
1186 * iscsi::tx_bhs will be valid when this is called.
1188 static int iscsi_tx_data_padding ( struct iscsi_session *iscsi ) {
1189 static const char pad[] = { '\0', '\0', '\0' };
1190 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1193 pad_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1197 return xfer_deliver_raw ( &iscsi->socket, pad, pad_len );
1201 * Complete iSCSI PDU transmission
1203 * @v iscsi iSCSI session
1205 * Called when a PDU has been completely transmitted and the TX state
1206 * machine is about to enter the idle state. iscsi::tx_bhs will be
1207 * valid for the just-completed PDU when this is called.
1209 static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
1210 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1212 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
1213 case ISCSI_OPCODE_DATA_OUT:
1214 iscsi_data_out_done ( iscsi );
1215 case ISCSI_OPCODE_LOGIN_REQUEST:
1216 iscsi_login_request_done ( iscsi );
1224 * Transmit iSCSI PDU
1226 * @v iscsi iSCSI session
1227 * @v buf Temporary data buffer
1228 * @v len Length of temporary data buffer
1230 * Constructs data to be sent for the current TX state
1232 static void iscsi_tx_step ( struct process *process ) {
1233 struct iscsi_session *iscsi =
1234 container_of ( process, struct iscsi_session, process );
1235 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1236 int ( * tx ) ( struct iscsi_session *iscsi );
1237 enum iscsi_tx_state next_state;
1241 /* Select fragment to transmit */
1243 switch ( iscsi->tx_state ) {
1245 /* Stop processing */
1249 tx_len = sizeof ( iscsi->tx_bhs );
1250 next_state = ISCSI_TX_AHS;
1253 tx = iscsi_tx_nothing;
1255 next_state = ISCSI_TX_DATA;
1259 tx_len = ISCSI_DATA_LEN ( common->lengths );
1260 next_state = ISCSI_TX_DATA_PADDING;
1262 case ISCSI_TX_DATA_PADDING:
1263 tx = iscsi_tx_data_padding;
1264 tx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1265 next_state = ISCSI_TX_IDLE;
1272 /* Check for window availability, if needed */
1273 if ( tx_len && ( xfer_window ( &iscsi->socket ) == 0 ) ) {
1274 /* Cannot transmit at this point; stop processing */
1279 if ( ( rc = tx ( iscsi ) ) != 0 ) {
1280 DBGC ( iscsi, "iSCSI %p could not transmit: %s\n",
1281 iscsi, strerror ( rc ) );
1285 /* Move to next state */
1286 iscsi->tx_state = next_state;
1287 if ( next_state == ISCSI_TX_IDLE )
1288 iscsi_tx_done ( iscsi );
1293 * Receive basic header segment of an iSCSI PDU
1295 * @v iscsi iSCSI session
1296 * @v data Received data
1297 * @v len Length of received data
1298 * @v remaining Data remaining after this data
1299 * @ret rc Return status code
1301 * This fills in iscsi::rx_bhs with the data from the BHS portion of
1304 static int iscsi_rx_bhs ( struct iscsi_session *iscsi, const void *data,
1305 size_t len, size_t remaining __unused ) {
1306 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
1307 if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
1308 DBGC2 ( iscsi, "iSCSI %p received PDU opcode %#x len %#lx\n",
1309 iscsi, iscsi->rx_bhs.common.opcode,
1310 ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
1316 * Discard portion of an iSCSI PDU.
1318 * @v iscsi iSCSI session
1319 * @v data Received data
1320 * @v len Length of received data
1321 * @v remaining Data remaining after this data
1322 * @ret rc Return status code
1324 * This discards data from a portion of a received PDU.
1326 static int iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
1327 const void *data __unused, size_t len __unused,
1328 size_t remaining __unused ) {
1334 * Receive data segment of an iSCSI PDU
1336 * @v iscsi iSCSI session
1337 * @v data Received data
1338 * @v len Length of received data
1339 * @v remaining Data remaining after this data
1340 * @ret rc Return status code
1342 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
1343 * will be valid when this is called.
1345 static int iscsi_rx_data ( struct iscsi_session *iscsi, const void *data,
1346 size_t len, size_t remaining ) {
1347 struct iscsi_bhs_common_response *response
1348 = &iscsi->rx_bhs.common_response;
1350 /* Update cmdsn and statsn */
1351 iscsi->cmdsn = ntohl ( response->expcmdsn );
1352 iscsi->statsn = ntohl ( response->statsn );
1354 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
1355 case ISCSI_OPCODE_LOGIN_RESPONSE:
1356 return iscsi_rx_login_response ( iscsi, data, len, remaining );
1357 case ISCSI_OPCODE_SCSI_RESPONSE:
1358 return iscsi_rx_scsi_response ( iscsi, data, len, remaining );
1359 case ISCSI_OPCODE_DATA_IN:
1360 return iscsi_rx_data_in ( iscsi, data, len, remaining );
1361 case ISCSI_OPCODE_R2T:
1362 return iscsi_rx_r2t ( iscsi, data, len, remaining );
1366 DBGC ( iscsi, "iSCSI %p unknown opcode %02x\n", iscsi,
1375 * @v socket Transport layer interface
1376 * @v data Received data
1377 * @v len Length of received data
1378 * @ret rc Return status code
1380 * This handles received PDUs. The receive strategy is to fill in
1381 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
1382 * throw away any AHS portion, and then process each part of the data
1383 * portion as it arrives. The data processing routine therefore
1384 * always has a full copy of the BHS available, even for portions of
1385 * the data in different packets to the BHS.
1387 static int iscsi_socket_deliver_raw ( struct xfer_interface *socket,
1388 const void *data, size_t len ) {
1389 struct iscsi_session *iscsi =
1390 container_of ( socket, struct iscsi_session, socket );
1391 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
1392 int ( * rx ) ( struct iscsi_session *iscsi, const void *data,
1393 size_t len, size_t remaining );
1394 enum iscsi_rx_state next_state;
1400 switch ( iscsi->rx_state ) {
1403 iscsi->rx_len = sizeof ( iscsi->rx_bhs );
1404 next_state = ISCSI_RX_AHS;
1407 rx = iscsi_rx_discard;
1408 iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
1409 next_state = ISCSI_RX_DATA;
1413 iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
1414 next_state = ISCSI_RX_DATA_PADDING;
1416 case ISCSI_RX_DATA_PADDING:
1417 rx = iscsi_rx_discard;
1418 iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1419 next_state = ISCSI_RX_BHS;
1426 frag_len = iscsi->rx_len - iscsi->rx_offset;
1427 if ( frag_len > len )
1429 remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
1430 if ( ( rc = rx ( iscsi, data, frag_len, remaining ) ) != 0 ) {
1431 DBGC ( iscsi, "iSCSI %p could not process received "
1432 "data: %s\n", iscsi, strerror ( rc ) );
1433 iscsi_close_connection ( iscsi, rc );
1434 iscsi_scsi_done ( iscsi, rc );
1438 iscsi->rx_offset += frag_len;
1442 /* If all the data for this state has not yet been
1443 * received, stay in this state for now.
1445 if ( iscsi->rx_offset != iscsi->rx_len )
1448 iscsi->rx_state = next_state;
1449 iscsi->rx_offset = 0;
1456 * Handle stream connection closure
1458 * @v socket Transport layer interface
1459 * @v rc Reason for close
1462 static void iscsi_socket_close ( struct xfer_interface *socket, int rc ) {
1463 struct iscsi_session *iscsi =
1464 container_of ( socket, struct iscsi_session, socket );
1466 /* Even a graceful close counts as an error for iSCSI */
1470 /* Close session cleanly */
1471 iscsi_close_connection ( iscsi, rc );
1473 /* Retry connection if within the retry limit, otherwise fail */
1474 if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
1475 DBGC ( iscsi, "iSCSI %p retrying connection (retry #%d)\n",
1476 iscsi, iscsi->retry_count );
1477 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
1478 DBGC ( iscsi, "iSCSI %p could not reconnect: %s\n",
1479 iscsi, strerror ( rc ) );
1480 iscsi_scsi_done ( iscsi, rc );
1483 DBGC ( iscsi, "iSCSI %p retry count exceeded\n", iscsi );
1484 iscsi->instant_rc = rc;
1485 iscsi_scsi_done ( iscsi, rc );
1490 * Handle redirection event
1492 * @v socket Transport layer interface
1493 * @v type Location type
1494 * @v args Remaining arguments depend upon location type
1495 * @ret rc Return status code
1497 static int iscsi_vredirect ( struct xfer_interface *socket, int type,
1499 struct iscsi_session *iscsi =
1500 container_of ( socket, struct iscsi_session, socket );
1502 struct sockaddr *peer;
1504 /* Intercept redirects to a LOCATION_SOCKET and record the IP
1505 * address for the iBFT. This is a bit of a hack, but avoids
1506 * inventing an ioctl()-style call to retrieve the socket
1507 * address from a data-xfer interface.
1509 if ( type == LOCATION_SOCKET ) {
1510 va_copy ( tmp, args );
1511 ( void ) va_arg ( tmp, int ); /* Discard "semantics" */
1512 peer = va_arg ( tmp, struct sockaddr * );
1513 memcpy ( &iscsi->target_sockaddr, peer,
1514 sizeof ( iscsi->target_sockaddr ) );
1518 return xfer_vopen ( socket, type, args );
1522 /** iSCSI socket operations */
1523 static struct xfer_interface_operations iscsi_socket_operations = {
1524 .close = iscsi_socket_close,
1525 .vredirect = iscsi_vredirect,
1526 .window = unlimited_xfer_window,
1527 .alloc_iob = default_xfer_alloc_iob,
1528 .deliver_iob = xfer_deliver_as_raw,
1529 .deliver_raw = iscsi_socket_deliver_raw,
1533 /****************************************************************************
1535 * iSCSI command issuing
1540 * Issue SCSI command
1542 * @v scsi SCSI device
1543 * @v command SCSI command
1544 * @ret rc Return status code
1546 static int iscsi_command ( struct scsi_device *scsi,
1547 struct scsi_command *command ) {
1548 struct iscsi_session *iscsi =
1549 container_of ( scsi->backend, struct iscsi_session, refcnt );
1552 /* Record SCSI command */
1553 iscsi->command = command;
1555 /* Abort immediately if we have a recorded permanent failure */
1556 if ( iscsi->instant_rc ) {
1557 rc = iscsi->instant_rc;
1561 /* Issue command or open connection as appropriate */
1562 if ( iscsi->status ) {
1563 iscsi_start_command ( iscsi );
1565 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 )
1569 /* Wait for command to complete */
1570 iscsi->rc = -EINPROGRESS;
1571 while ( iscsi->rc == -EINPROGRESS )
1576 iscsi->command = NULL;
1580 static int iscsi_detached_command ( struct scsi_device *scsi __unused,
1581 struct scsi_command *command __unused ) {
1586 * Shut down iSCSI interface
1588 * @v scsi SCSI device
1590 void iscsi_detach ( struct scsi_device *scsi ) {
1591 struct iscsi_session *iscsi =
1592 container_of ( scsi->backend, struct iscsi_session, refcnt );
1594 xfer_nullify ( &iscsi->socket );
1595 iscsi_close_connection ( iscsi, 0 );
1596 process_del ( &iscsi->process );
1597 scsi->command = iscsi_detached_command;
1598 ref_put ( scsi->backend );
1599 scsi->backend = NULL;
1602 /****************************************************************************
1608 /** iSCSI root path components (as per RFC4173) */
1609 enum iscsi_root_path_component {
1622 * @v iscsi iSCSI session
1623 * @v lun_string LUN string representation (as per RFC4173)
1624 * @ret rc Return status code
1626 static int iscsi_parse_lun ( struct iscsi_session *iscsi,
1627 const char *lun_string ) {
1635 memset ( &lun, 0, sizeof ( lun ) );
1637 p = ( char * ) lun_string;
1639 for ( i = 0 ; i < 4 ; i++ ) {
1640 lun.u16[i] = htons ( strtoul ( p, &p, 16 ) );
1651 iscsi->lun = lun.u64;
1656 * Parse iSCSI root path
1658 * @v iscsi iSCSI session
1659 * @v root_path iSCSI root path (as per RFC4173)
1660 * @ret rc Return status code
1662 static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
1663 const char *root_path ) {
1664 char rp_copy[ strlen ( root_path ) + 1 ];
1665 char *rp_comp[NUM_RP_COMPONENTS];
1670 /* Split root path into component parts */
1671 strcpy ( rp_copy, root_path );
1674 if ( i == NUM_RP_COMPONENTS )
1676 for ( ; *rp != ':' ; rp++ ) {
1678 DBGC ( iscsi, "iSCSI %p root path \"%s\" "
1679 "too short\n", iscsi, root_path );
1686 /* Use root path components to configure iSCSI session */
1687 iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
1688 if ( ! iscsi->target_address )
1690 iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
1691 if ( ! iscsi->target_port )
1692 iscsi->target_port = ISCSI_PORT;
1693 if ( ( rc = iscsi_parse_lun ( iscsi, rp_comp[RP_LUN] ) ) != 0 ) {
1694 DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
1695 iscsi, rp_comp[RP_LUN] );
1698 iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
1699 if ( ! iscsi->target_iqn )
1706 * Set iSCSI authentication details
1708 * @v iscsi iSCSI session
1709 * @v initiator_username Initiator username, if any
1710 * @v initiator_password Initiator password, if any
1711 * @v target_username Target username, if any
1712 * @v target_password Target password, if any
1713 * @ret rc Return status code
1715 static int iscsi_set_auth ( struct iscsi_session *iscsi,
1716 const char *initiator_username,
1717 const char *initiator_password,
1718 const char *target_username,
1719 const char *target_password ) {
1721 /* Check for initiator or target credentials */
1722 if ( initiator_username || initiator_password ||
1723 target_username || target_password ) {
1725 /* We must have at least an initiator username+password */
1726 if ( ! ( initiator_username && initiator_password ) )
1729 /* Store initiator credentials */
1730 iscsi->initiator_username = strdup ( initiator_username );
1731 if ( ! iscsi->initiator_username )
1733 iscsi->initiator_password = strdup ( initiator_password );
1734 if ( ! iscsi->initiator_password )
1737 /* Check for target credentials */
1738 if ( target_username || target_password ) {
1740 /* We must have target username+password */
1741 if ( ! ( target_username && target_password ) )
1744 /* Store target credentials */
1745 iscsi->target_username = strdup ( target_username );
1746 if ( ! iscsi->target_username )
1748 iscsi->target_password = strdup ( target_password );
1749 if ( ! iscsi->target_password )
1757 DBGC ( iscsi, "iSCSI %p invalid credentials: initiator "
1758 "%sname,%spw, target %sname,%spw\n", iscsi,
1759 ( initiator_username ? "" : "no " ),
1760 ( initiator_password ? "" : "no " ),
1761 ( target_username ? "" : "no " ),
1762 ( target_password ? "" : "no " ) );
1767 * Attach iSCSI interface
1769 * @v scsi SCSI device
1770 * @v root_path iSCSI root path (as per RFC4173)
1771 * @ret rc Return status code
1773 int iscsi_attach ( struct scsi_device *scsi, const char *root_path ) {
1774 struct iscsi_session *iscsi;
1777 /* Allocate and initialise structure */
1778 iscsi = zalloc ( sizeof ( *iscsi ) );
1781 iscsi->refcnt.free = iscsi_free;
1782 xfer_init ( &iscsi->socket, &iscsi_socket_operations, &iscsi->refcnt );
1783 process_init ( &iscsi->process, iscsi_tx_step, &iscsi->refcnt );
1785 /* Parse root path */
1786 if ( ( rc = iscsi_parse_root_path ( iscsi, root_path ) ) != 0 )
1788 /* Set fields not specified by root path */
1789 if ( ( rc = iscsi_set_auth ( iscsi,
1790 iscsi_initiator_username,
1791 iscsi_initiator_password,
1792 iscsi_target_username,
1793 iscsi_target_password ) ) != 0 )
1797 if ( ! iscsi->target_address ) {
1798 DBGC ( iscsi, "iSCSI %p does not yet support discovery\n",
1803 if ( ! iscsi->target_iqn ) {
1804 DBGC ( iscsi, "iSCSI %p no target address supplied in %s\n",
1810 /* Attach parent interface, mortalise self, and return */
1811 scsi->backend = ref_get ( &iscsi->refcnt );
1812 scsi->command = iscsi_command;
1813 scsi->lun = iscsi->lun;
1814 ref_put ( &iscsi->refcnt );
1818 ref_put ( &iscsi->refcnt );
1822 /****************************************************************************
1828 /** iSCSI initiator IQN setting */
1829 struct setting initiator_iqn_setting __setting = {
1830 .name = "initiator-iqn",
1831 .description = "iSCSI initiator name",
1832 .tag = DHCP_ISCSI_INITIATOR_IQN,
1833 .type = &setting_type_string,
1836 /** iSCSI reverse username setting */
1837 struct setting reverse_username_setting __setting = {
1838 .name = "reverse-username",
1839 .description = "Reverse user name",
1840 .tag = DHCP_EB_REVERSE_USERNAME,
1841 .type = &setting_type_string,
1844 /** iSCSI reverse password setting */
1845 struct setting reverse_password_setting __setting = {
1846 .name = "reverse-password",
1847 .description = "Reverse password",
1848 .tag = DHCP_EB_REVERSE_PASSWORD,
1849 .type = &setting_type_string,
1852 /** An iSCSI string setting */
1853 struct iscsi_string_setting {
1855 struct setting *setting;
1856 /** String to update */
1858 /** String prefix */
1862 /** iSCSI string settings */
1863 static struct iscsi_string_setting iscsi_string_settings[] = {
1865 .setting = &initiator_iqn_setting,
1866 .string = &iscsi_explicit_initiator_iqn,
1870 .setting = &username_setting,
1871 .string = &iscsi_initiator_username,
1875 .setting = &password_setting,
1876 .string = &iscsi_initiator_password,
1880 .setting = &reverse_username_setting,
1881 .string = &iscsi_target_username,
1885 .setting = &reverse_password_setting,
1886 .string = &iscsi_target_password,
1890 .setting = &hostname_setting,
1891 .string = &iscsi_default_initiator_iqn,
1892 .prefix = "iqn.2000-01.org.etherboot:",
1897 * Apply iSCSI setting
1899 * @v setting iSCSI string setting
1900 * @ret rc Return status code
1902 static int apply_iscsi_string_setting ( struct iscsi_string_setting *setting ){
1909 /* Free old string */
1910 free ( *setting->string );
1911 *setting->string = NULL;
1913 /* Allocate new string */
1914 prefix_len = strlen ( setting->prefix );
1915 setting_len = fetch_setting_len ( NULL, setting->setting );
1916 if ( setting_len < 0 ) {
1917 /* Missing settings are not errors; leave strings as NULL */
1920 len = ( prefix_len + setting_len + 1 );
1921 p = *setting->string = malloc ( len );
1925 /* Fill new string */
1926 strcpy ( p, setting->prefix );
1927 check_len = fetch_string_setting ( NULL, setting->setting,
1929 ( len - prefix_len ) );
1930 assert ( check_len == setting_len );
1936 * Apply iSCSI settings
1938 * @ret rc Return status code
1940 static int apply_iscsi_settings ( void ) {
1941 struct iscsi_string_setting *setting;
1945 for ( i = 0 ; i < ( sizeof ( iscsi_string_settings ) /
1946 sizeof ( iscsi_string_settings[0] ) ) ; i++ ) {
1947 setting = &iscsi_string_settings[i];
1948 if ( ( rc = apply_iscsi_string_setting ( setting ) ) != 0 ) {
1949 DBG ( "iSCSI could not apply setting %s\n",
1950 setting->setting->name );
1958 /** iSCSI settings applicator */
1959 struct settings_applicator iscsi_settings_applicator __settings_applicator = {
1960 .apply = apply_iscsi_settings,
1963 /****************************************************************************
1970 * Get iSCSI initiator IQN
1972 * @v iscsi iSCSI session
1973 * @ret rc Return status code
1975 const char * iscsi_initiator_iqn ( void ) {
1977 if ( iscsi_explicit_initiator_iqn )
1978 return iscsi_explicit_initiator_iqn;
1979 if ( iscsi_default_initiator_iqn )
1980 return iscsi_default_initiator_iqn;
1981 return "iqn.2000-09.org.etherboot:UNKNOWN";