12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676 |
-
-
- #include <stddef.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <assert.h>
- #include <byteswap.h>
- #include <gpxe/vsprintf.h>
- #include <gpxe/socket.h>
- #include <gpxe/xfer.h>
- #include <gpxe/open.h>
- #include <gpxe/scsi.h>
- #include <gpxe/process.h>
- #include <gpxe/uaccess.h>
- #include <gpxe/tcpip.h>
- #include <gpxe/dhcp.h>
- #include <gpxe/features.h>
- #include <gpxe/iscsi.h>
-
-
-
- FEATURE ( FEATURE_PROTOCOL, "iSCSI", DHCP_EB_FEATURE_ISCSI, 1 );
-
-
- static char *iscsi_explicit_initiator_iqn;
-
-
- static char *iscsi_default_initiator_iqn;
-
-
- static char *iscsi_username;
-
-
- static char *iscsi_password;
-
- static void iscsi_start_tx ( struct iscsi_session *iscsi );
- static void iscsi_start_login ( struct iscsi_session *iscsi );
- static void iscsi_start_data_out ( struct iscsi_session *iscsi,
- unsigned int datasn );
-
-
- static void iscsi_rx_buffered_data_done ( struct iscsi_session *iscsi ) {
- free ( iscsi->rx_buffer );
- iscsi->rx_buffer = NULL;
- }
-
-
- static void iscsi_free ( struct refcnt *refcnt ) {
- struct iscsi_session *iscsi =
- container_of ( refcnt, struct iscsi_session, refcnt );
-
- free ( iscsi->target_address );
- free ( iscsi->target_iqn );
- free ( iscsi->username );
- free ( iscsi->password );
- chap_finish ( &iscsi->chap );
- iscsi_rx_buffered_data_done ( iscsi );
- free ( iscsi );
- }
-
-
- static int iscsi_open_connection ( struct iscsi_session *iscsi ) {
- struct sockaddr_tcpip target;
- int rc;
-
- assert ( iscsi->tx_state == ISCSI_TX_IDLE );
- assert ( iscsi->rx_state == ISCSI_RX_BHS );
- assert ( iscsi->rx_offset == 0 );
-
-
- memset ( &target, 0, sizeof ( target ) );
- target.st_port = htons ( iscsi->target_port );
- if ( ( rc = xfer_open_named_socket ( &iscsi->socket, SOCK_STREAM,
- ( struct sockaddr * ) &target,
- iscsi->target_address,
- NULL ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p could not open socket: %s\n",
- iscsi, strerror ( rc ) );
- return rc;
- }
-
-
- iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
- ISCSI_STATUS_STRINGS_SECURITY );
-
-
- iscsi->itt++;
-
-
- iscsi_start_login ( iscsi );
-
- return 0;
- }
-
-
- static void iscsi_close_connection ( struct iscsi_session *iscsi, int rc ) {
-
-
- xfer_close ( &iscsi->socket, rc );
-
-
- iscsi->status = 0;
-
-
- iscsi->tx_state = ISCSI_TX_IDLE;
- iscsi->rx_state = ISCSI_RX_BHS;
- iscsi->rx_offset = 0;
-
-
- chap_finish ( &iscsi->chap );
- iscsi_rx_buffered_data_done ( iscsi );
- }
-
-
- static void iscsi_scsi_done ( struct iscsi_session *iscsi, int rc ) {
-
- assert ( iscsi->tx_state == ISCSI_TX_IDLE );
-
- iscsi->command = NULL;
- iscsi->rc = rc;
- }
-
-
-
-
- static void iscsi_start_command ( struct iscsi_session *iscsi ) {
- struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
-
- assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
-
-
- iscsi_start_tx ( iscsi );
- command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
- command->flags = ( ISCSI_FLAG_FINAL |
- ISCSI_COMMAND_ATTR_SIMPLE );
- if ( iscsi->command->data_in )
- command->flags |= ISCSI_COMMAND_FLAG_READ;
- if ( iscsi->command->data_out )
- command->flags |= ISCSI_COMMAND_FLAG_WRITE;
-
- command->lun = iscsi->lun;
- command->itt = htonl ( ++iscsi->itt );
- command->exp_len = htonl ( iscsi->command->data_in_len |
- iscsi->command->data_out_len );
- command->cmdsn = htonl ( iscsi->cmdsn );
- command->expstatsn = htonl ( iscsi->statsn + 1 );
- memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
- DBGC ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#x\n",
- iscsi, SCSI_CDB_DATA ( command->cdb ),
- ( iscsi->command->data_in ? "in" : "out" ),
- ( iscsi->command->data_in ?
- iscsi->command->data_in_len : iscsi->command->data_out_len ));
- }
-
-
- static int iscsi_rx_scsi_response ( struct iscsi_session *iscsi,
- const void *data, size_t len,
- size_t remaining ) {
- struct iscsi_bhs_scsi_response *response
- = &iscsi->rx_bhs.scsi_response;
- int sense_offset;
-
-
- sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
- if ( ( sense_offset >= 0 ) && len ) {
- iscsi->command->sense_response =
- * ( ( char * ) data + sense_offset );
- }
-
-
- if ( remaining )
- return 0;
-
-
- iscsi->command->status = response->status;
-
-
- if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
- return -EIO;
-
-
- iscsi_scsi_done ( iscsi, 0 );
- return 0;
- }
-
-
- static int iscsi_rx_data_in ( struct iscsi_session *iscsi,
- const void *data, size_t len,
- size_t remaining __unused ) {
- struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
- unsigned long offset;
-
-
- offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
- assert ( iscsi->command != NULL );
- assert ( iscsi->command->data_in );
- assert ( ( offset + len ) <= iscsi->command->data_in_len );
- copy_to_user ( iscsi->command->data_in, offset, data, len );
-
-
- if ( data_in->flags & ISCSI_DATA_FLAG_STATUS )
- iscsi->command->status = data_in->status;
-
-
- if ( ( offset + len ) == iscsi->command->data_in_len ) {
- assert ( data_in->flags & ISCSI_FLAG_FINAL );
- assert ( remaining == 0 );
- iscsi_scsi_done ( iscsi, 0 );
- }
-
- return 0;
- }
-
-
- static int iscsi_rx_r2t ( struct iscsi_session *iscsi,
- const void *data __unused, size_t len __unused,
- size_t remaining __unused ) {
- struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
-
-
- iscsi->ttt = ntohl ( r2t->ttt );
- iscsi->transfer_offset = ntohl ( r2t->offset );
- iscsi->transfer_len = ntohl ( r2t->len );
- iscsi_start_data_out ( iscsi, 0 );
-
- return 0;
- }
-
-
- static void iscsi_start_data_out ( struct iscsi_session *iscsi,
- unsigned int datasn ) {
- struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
- unsigned long offset;
- unsigned long remaining;
- unsigned long len;
-
-
-
- offset = datasn * 512;
- remaining = iscsi->transfer_len - offset;
- len = remaining;
- if ( len > 512 )
- len = 512;
-
-
- iscsi_start_tx ( iscsi );
- data_out->opcode = ISCSI_OPCODE_DATA_OUT;
- if ( len == remaining )
- data_out->flags = ( ISCSI_FLAG_FINAL );
- ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
- data_out->lun = iscsi->lun;
- data_out->itt = htonl ( iscsi->itt );
- data_out->ttt = htonl ( iscsi->ttt );
- data_out->expstatsn = htonl ( iscsi->statsn + 1 );
- data_out->datasn = htonl ( datasn );
- data_out->offset = htonl ( iscsi->transfer_offset + offset );
- DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
- iscsi, datasn, len );
- }
-
-
- static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
- struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
-
-
-
- if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
- iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
- }
-
-
- static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
- struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
- struct io_buffer *iobuf;
- unsigned long offset;
- size_t len;
-
- offset = ntohl ( data_out->offset );
- len = ISCSI_DATA_LEN ( data_out->lengths );
-
- assert ( iscsi->command != NULL );
- assert ( iscsi->command->data_out );
- assert ( ( offset + len ) <= iscsi->command->data_out_len );
-
- iobuf = xfer_alloc_iob ( &iscsi->socket, len );
- if ( ! iobuf )
- return -ENOMEM;
-
- copy_from_user ( iob_put ( iobuf, len ),
- iscsi->command->data_out, offset, len );
-
- return xfer_deliver_iob ( &iscsi->socket, iobuf );
- }
-
-
-
-
- static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
- void *data, size_t len ) {
- unsigned int used = 0;
- unsigned int i;
-
- if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
- used += ssnprintf ( data + used, len - used,
- "InitiatorName=%s%c"
- "TargetName=%s%c"
- "SessionType=Normal%c"
- "AuthMethod=CHAP,None%c",
- iscsi_initiator_iqn(), 0,
- iscsi->target_iqn, 0, 0, 0 );
- }
-
- if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
- used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
- }
-
- if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) &&
- iscsi->username ) {
- used += ssnprintf ( data + used, len - used,
- "CHAP_N=%s%cCHAP_R=0x",
- iscsi->username, 0 );
- for ( i = 0 ; i < iscsi->chap.response_len ; i++ ) {
- used += ssnprintf ( data + used, len - used, "%02x",
- iscsi->chap.response[i] );
- }
- used += ssnprintf ( data + used, len - used, "%c", 0 );
- }
-
- if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
- used += ssnprintf ( data + used, len - used,
- "HeaderDigest=None%c"
- "DataDigest=None%c"
- "InitialR2T=Yes%c"
- "MaxRecvDataSegmentLength=8192%c"
- "MaxBurstLength=262144%c"
- "DefaultTime2Wait=0%c"
- "DefaultTime2Retain=0%c"
- "MaxOutstandingR2T=1%c"
- "DataPDUInOrder=Yes%c"
- "DataSequenceInOrder=Yes%c"
- "ErrorRecoveryLevel=0%c",
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
- }
-
- return used;
- }
-
-
- static void iscsi_start_login ( struct iscsi_session *iscsi ) {
- struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
- int len;
-
-
- iscsi_start_tx ( iscsi );
- request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
- ISCSI_FLAG_IMMEDIATE );
- request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
- ISCSI_LOGIN_FLAG_TRANSITION );
-
- len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
- ISCSI_SET_LENGTHS ( request->lengths, 0, len );
- request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
- IANA_EN_FEN_SYSTEMS );
-
- request->tsih = htons ( iscsi->tsih );
- request->itt = htonl ( iscsi->itt );
-
- request->cmdsn = htonl ( iscsi->cmdsn );
- request->expstatsn = htonl ( iscsi->statsn + 1 );
- }
-
-
- static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
-
-
- iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
-
-
- chap_finish ( &iscsi->chap );
- }
-
-
- static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
- struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
- struct io_buffer *iobuf;
- size_t len;
-
- len = ISCSI_DATA_LEN ( request->lengths );
- iobuf = xfer_alloc_iob ( &iscsi->socket, len );
- if ( ! iobuf )
- return -ENOMEM;
- iob_put ( iobuf, len );
- iscsi_build_login_request_strings ( iscsi, iobuf->data, len );
- return xfer_deliver_iob ( &iscsi->socket, iobuf );
- }
-
-
- static int iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
- const char *value ) {
-
- DBGC ( iscsi, "iSCSI %p will redirect to %s\n", iscsi, value );
-
-
- free ( iscsi->target_address );
- iscsi->target_address = strdup ( value );
- if ( ! iscsi->target_address )
- return -ENOMEM;
- return 0;
- }
-
-
- static int iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
- const char *value ) {
-
-
- if ( strcmp ( value, "CHAP" ) == 0 ) {
- DBGC ( iscsi, "iSCSI %p initiating CHAP authentication\n",
- iscsi );
- iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_ALGORITHM;
- }
- return 0;
- }
-
-
- static int iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
- const char *value ) {
- int rc;
-
-
-
- if ( strcmp ( value, "5" ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
- iscsi, value );
- return -EPROTO;
- }
-
-
- if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
- iscsi, strerror ( rc ) );
- return rc;
- }
-
- return 0;
- }
-
-
- static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
- const char *value ) {
- unsigned int identifier;
- char *endp;
-
-
- identifier = strtoul ( value, &endp, 0 );
- if ( *endp != '\0' ) {
- DBGC ( iscsi, "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
- iscsi, value );
- return -EPROTO;
- }
-
-
-
- chap_set_identifier ( &iscsi->chap, identifier );
- if ( iscsi->password ) {
- chap_update ( &iscsi->chap, iscsi->password,
- strlen ( iscsi->password ) );
- }
-
- return 0;
- }
-
-
- static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
- const char *value ) {
- char buf[3];
- char *endp;
- uint8_t byte;
-
-
- if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
- DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge \"%s\"\n",
- iscsi, value );
- }
- value += 2;
-
-
- for ( ; ( value[0] && value[1] ) ; value += 2 ) {
- memcpy ( buf, value, 2 );
- buf[3] = 0;
- byte = strtoul ( buf, &endp, 16 );
- if ( *endp != '\0' ) {
- DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge "
- "byte \"%s\"\n", iscsi, buf );
- return -EPROTO;
- }
- chap_update ( &iscsi->chap, &byte, sizeof ( byte ) );
- }
-
-
- DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );
- chap_respond ( &iscsi->chap );
- iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_RESPONSE;
-
- return 0;
- }
-
-
- struct iscsi_string_type {
-
-
- const char *key;
-
-
- int ( * handle ) ( struct iscsi_session *iscsi, const char *value );
- };
-
-
- static struct iscsi_string_type iscsi_string_types[] = {
- { "TargetAddress=", iscsi_handle_targetaddress_value },
- { "AuthMethod=", iscsi_handle_authmethod_value },
- { "CHAP_A=", iscsi_handle_chap_a_value },
- { "CHAP_I=", iscsi_handle_chap_i_value },
- { "CHAP_C=", iscsi_handle_chap_c_value },
- { NULL, NULL }
- };
-
-
- static int iscsi_handle_string ( struct iscsi_session *iscsi,
- const char *string ) {
- struct iscsi_string_type *type;
- size_t key_len;
- int rc;
-
- for ( type = iscsi_string_types ; type->key ; type++ ) {
- key_len = strlen ( type->key );
- if ( strncmp ( string, type->key, key_len ) != 0 )
- continue;
- DBGC ( iscsi, "iSCSI %p handling %s\n", iscsi, string );
- if ( ( rc = type->handle ( iscsi,
- ( string + key_len ) ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p could not handle %s: %s\n",
- iscsi, string, strerror ( rc ) );
- return rc;
- }
- return 0;
- }
- DBGC ( iscsi, "iSCSI %p ignoring %s\n", iscsi, string );
- return 0;
- }
-
-
- static int iscsi_handle_strings ( struct iscsi_session *iscsi,
- const char *strings, size_t len ) {
- size_t string_len;
- int rc;
-
-
-
- while ( 1 ) {
- string_len = ( strnlen ( strings, len ) + 1 );
- if ( string_len > len )
- break;
- if ( ( rc = iscsi_handle_string ( iscsi, strings ) ) != 0 )
- return rc;
- strings += string_len;
- len -= string_len;
- }
- return 0;
- }
-
-
- static int iscsi_rx_buffered_data ( struct iscsi_session *iscsi,
- const void *data, size_t len ) {
-
-
- if ( ! iscsi->rx_buffer ) {
- iscsi->rx_buffer = malloc ( iscsi->rx_len );
- if ( ! iscsi->rx_buffer )
- return -ENOMEM;
- }
-
-
- assert ( ( iscsi->rx_offset + len ) <= iscsi->rx_len );
- memcpy ( ( iscsi->rx_buffer + iscsi->rx_offset ), data, len );
-
- return 0;
- }
-
-
- static int iscsi_rx_login_response ( struct iscsi_session *iscsi,
- const void *data, size_t len,
- size_t remaining ) {
- struct iscsi_bhs_login_response *response
- = &iscsi->rx_bhs.login_response;
- int rc;
-
-
- if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p could not buffer login response: %s\n",
- iscsi, strerror ( rc ) );
- return rc;
- }
- if ( remaining )
- return 0;
-
-
- if ( ( rc = iscsi_handle_strings ( iscsi, iscsi->rx_buffer,
- iscsi->rx_len ) ) != 0 )
- return rc;
- iscsi_rx_buffered_data_done ( iscsi );
-
-
- if ( response->status_class == ISCSI_STATUS_REDIRECT ) {
- DBGC ( iscsi, "iSCSI %p redirecting to new server\n", iscsi );
- iscsi_close_connection ( iscsi, 0 );
- if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p could not redirect: %s\n ",
- iscsi, strerror ( rc ) );
- return rc;
- }
- return 0;
- }
-
-
- if ( response->status_class != 0 ) {
- DBGC ( iscsi, "iSCSI login failure: class %02x detail %02x\n",
- response->status_class, response->status_detail );
- iscsi->instant_rc = -EPERM;
- return -EPERM;
- }
-
-
- if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
- switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
- case ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION:
- iscsi->status =
- ( ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE |
- ISCSI_STATUS_STRINGS_OPERATIONAL );
- break;
- case ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE:
- iscsi->status = ISCSI_STATUS_FULL_FEATURE_PHASE;
- break;
- default:
- DBGC ( iscsi, "iSCSI %p got invalid response flags "
- "%02x\n", iscsi, response->flags );
- return -EIO;
- }
- }
-
-
-
- if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
- ISCSI_STATUS_FULL_FEATURE_PHASE ) {
- iscsi_start_login ( iscsi );
- return 0;
- }
-
-
- iscsi->retry_count = 0;
-
-
- iscsi->tsih = ntohl ( response->tsih );
-
-
- iscsi_start_command ( iscsi );
-
- return 0;
- }
-
-
-
-
- static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
- assert ( iscsi->tx_state == ISCSI_TX_IDLE );
-
-
- memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
-
-
- iscsi->tx_state = ISCSI_TX_BHS;
- }
-
-
- static int iscsi_tx_nothing ( struct iscsi_session *iscsi __unused ) {
- return 0;
- }
-
-
- static int iscsi_tx_bhs ( struct iscsi_session *iscsi ) {
- return xfer_deliver_raw ( &iscsi->socket, &iscsi->tx_bhs,
- sizeof ( iscsi->tx_bhs ) );
- }
-
-
- static int iscsi_tx_data ( struct iscsi_session *iscsi ) {
- struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
-
- switch ( common->opcode & ISCSI_OPCODE_MASK ) {
- case ISCSI_OPCODE_DATA_OUT:
- return iscsi_tx_data_out ( iscsi );
- case ISCSI_OPCODE_LOGIN_REQUEST:
- return iscsi_tx_login_request ( iscsi );
- default:
-
- return 0;
- }
- }
-
-
- static int iscsi_tx_data_padding ( struct iscsi_session *iscsi ) {
- static const char pad[] = { '\0', '\0', '\0' };
- struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
- size_t pad_len;
-
- pad_len = ISCSI_DATA_PAD_LEN ( common->lengths );
- if ( ! pad_len )
- return 0;
-
- return xfer_deliver_raw ( &iscsi->socket, pad, pad_len );
- }
-
-
- static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
- struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
-
- switch ( common->opcode & ISCSI_OPCODE_MASK ) {
- case ISCSI_OPCODE_DATA_OUT:
- iscsi_data_out_done ( iscsi );
- case ISCSI_OPCODE_LOGIN_REQUEST:
- iscsi_login_request_done ( iscsi );
- default:
-
- break;
- }
- }
-
-
- static void iscsi_tx_step ( struct process *process ) {
- struct iscsi_session *iscsi =
- container_of ( process, struct iscsi_session, process );
- struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
- int ( * tx ) ( struct iscsi_session *iscsi );
- enum iscsi_tx_state next_state;
- size_t tx_len;
- int rc;
-
-
- while ( 1 ) {
- switch ( iscsi->tx_state ) {
- case ISCSI_TX_IDLE:
-
- return;
- case ISCSI_TX_BHS:
- tx = iscsi_tx_bhs;
- tx_len = sizeof ( iscsi->tx_bhs );
- next_state = ISCSI_TX_AHS;
- break;
- case ISCSI_TX_AHS:
- tx = iscsi_tx_nothing;
- tx_len = 0;
- next_state = ISCSI_TX_DATA;
- break;
- case ISCSI_TX_DATA:
- tx = iscsi_tx_data;
- tx_len = ISCSI_DATA_LEN ( common->lengths );
- next_state = ISCSI_TX_DATA_PADDING;
- break;
- case ISCSI_TX_DATA_PADDING:
- tx = iscsi_tx_data_padding;
- tx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
- next_state = ISCSI_TX_IDLE;
- break;
- default:
- assert ( 0 );
- return;
- }
-
-
- if ( tx_len && ( xfer_window ( &iscsi->socket ) == 0 ) ) {
-
- return;
- }
-
-
- if ( ( rc = tx ( iscsi ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p could not transmit: %s\n",
- iscsi, strerror ( rc ) );
- return;
- }
-
-
- iscsi->tx_state = next_state;
- if ( next_state == ISCSI_TX_IDLE )
- iscsi_tx_done ( iscsi );
- }
- }
-
-
- static int iscsi_rx_bhs ( struct iscsi_session *iscsi, const void *data,
- size_t len, size_t remaining __unused ) {
- memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
- if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
- DBGC ( iscsi, "iSCSI %p received PDU opcode %#x len %#lx\n",
- iscsi, iscsi->rx_bhs.common.opcode,
- ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
- }
- return 0;
- }
-
-
- static int iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
- const void *data __unused, size_t len __unused,
- size_t remaining __unused ) {
-
- return 0;
- }
-
-
- static int iscsi_rx_data ( struct iscsi_session *iscsi, const void *data,
- size_t len, size_t remaining ) {
- struct iscsi_bhs_common_response *response
- = &iscsi->rx_bhs.common_response;
-
-
- iscsi->cmdsn = ntohl ( response->expcmdsn );
- iscsi->statsn = ntohl ( response->statsn );
-
- switch ( response->opcode & ISCSI_OPCODE_MASK ) {
- case ISCSI_OPCODE_LOGIN_RESPONSE:
- return iscsi_rx_login_response ( iscsi, data, len, remaining );
- case ISCSI_OPCODE_SCSI_RESPONSE:
- return iscsi_rx_scsi_response ( iscsi, data, len, remaining );
- case ISCSI_OPCODE_DATA_IN:
- return iscsi_rx_data_in ( iscsi, data, len, remaining );
- case ISCSI_OPCODE_R2T:
- return iscsi_rx_r2t ( iscsi, data, len, remaining );
- default:
- if ( remaining )
- return 0;
- DBGC ( iscsi, "iSCSI %p unknown opcode %02x\n", iscsi,
- response->opcode );
- return -EOPNOTSUPP;
- }
- }
-
-
- static int iscsi_socket_deliver_raw ( struct xfer_interface *socket,
- const void *data, size_t len ) {
- struct iscsi_session *iscsi =
- container_of ( socket, struct iscsi_session, socket );
- struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
- int ( * rx ) ( struct iscsi_session *iscsi, const void *data,
- size_t len, size_t remaining );
- enum iscsi_rx_state next_state;
- size_t frag_len;
- size_t remaining;
- int rc;
-
- while ( 1 ) {
- switch ( iscsi->rx_state ) {
- case ISCSI_RX_BHS:
- rx = iscsi_rx_bhs;
- iscsi->rx_len = sizeof ( iscsi->rx_bhs );
- next_state = ISCSI_RX_AHS;
- break;
- case ISCSI_RX_AHS:
- rx = iscsi_rx_discard;
- iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
- next_state = ISCSI_RX_DATA;
- break;
- case ISCSI_RX_DATA:
- rx = iscsi_rx_data;
- iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
- next_state = ISCSI_RX_DATA_PADDING;
- break;
- case ISCSI_RX_DATA_PADDING:
- rx = iscsi_rx_discard;
- iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
- next_state = ISCSI_RX_BHS;
- break;
- default:
- assert ( 0 );
- return -EINVAL;
- }
-
- frag_len = iscsi->rx_len - iscsi->rx_offset;
- if ( frag_len > len )
- frag_len = len;
- remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
- if ( ( rc = rx ( iscsi, data, frag_len, remaining ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p could not process received "
- "data: %s\n", iscsi, strerror ( rc ) );
- iscsi_close_connection ( iscsi, rc );
- iscsi_scsi_done ( iscsi, rc );
- return rc;
- }
-
- iscsi->rx_offset += frag_len;
- data += frag_len;
- len -= frag_len;
-
-
-
- if ( iscsi->rx_offset != iscsi->rx_len )
- return 0;
-
- iscsi->rx_state = next_state;
- iscsi->rx_offset = 0;
- }
-
- return 0;
- }
-
-
- static void iscsi_socket_close ( struct xfer_interface *socket, int rc ) {
- struct iscsi_session *iscsi =
- container_of ( socket, struct iscsi_session, socket );
-
-
- if ( ! rc )
- rc = -ECONNRESET;
-
-
- iscsi_close_connection ( iscsi, rc );
-
-
- if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
- DBGC ( iscsi, "iSCSI %p retrying connection (retry #%d)\n",
- iscsi, iscsi->retry_count );
- if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p could not reconnect: %s\n",
- iscsi, strerror ( rc ) );
- iscsi_scsi_done ( iscsi, rc );
- }
- } else {
- DBGC ( iscsi, "iSCSI %p retry count exceeded\n", iscsi );
- iscsi->instant_rc = rc;
- iscsi_scsi_done ( iscsi, rc );
- }
- }
-
-
- static int iscsi_vredirect ( struct xfer_interface *socket, int type,
- va_list args ) {
- struct iscsi_session *iscsi =
- container_of ( socket, struct iscsi_session, socket );
- va_list tmp;
- struct sockaddr *peer;
-
-
-
- if ( type == LOCATION_SOCKET ) {
- va_copy ( tmp, args );
- ( void ) va_arg ( tmp, int );
- peer = va_arg ( tmp, struct sockaddr * );
- memcpy ( &iscsi->target_sockaddr, peer,
- sizeof ( iscsi->target_sockaddr ) );
- va_end ( tmp );
- }
-
- return xfer_vopen ( socket, type, args );
- }
-
-
-
- static struct xfer_interface_operations iscsi_socket_operations = {
- .close = iscsi_socket_close,
- .vredirect = iscsi_vredirect,
- .seek = ignore_xfer_seek,
- .window = unlimited_xfer_window,
- .alloc_iob = default_xfer_alloc_iob,
- .deliver_iob = xfer_deliver_as_raw,
- .deliver_raw = iscsi_socket_deliver_raw,
- };
-
-
-
-
-
- static int iscsi_command ( struct scsi_device *scsi,
- struct scsi_command *command ) {
- struct iscsi_session *iscsi =
- container_of ( scsi->backend, struct iscsi_session, refcnt );
- int rc;
-
-
- iscsi->command = command;
-
-
- if ( iscsi->instant_rc ) {
- rc = iscsi->instant_rc;
- goto done;
- }
-
-
- if ( iscsi->status ) {
- iscsi_start_command ( iscsi );
- } else {
- if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 )
- goto done;
- }
-
-
- iscsi->rc = -EINPROGRESS;
- while ( iscsi->rc == -EINPROGRESS )
- step();
- rc = iscsi->rc;
-
- done:
- iscsi->command = NULL;
- return rc;
- }
-
- static int iscsi_detached_command ( struct scsi_device *scsi __unused,
- struct scsi_command *command __unused ) {
- return -ENODEV;
- }
-
-
- void iscsi_detach ( struct scsi_device *scsi ) {
- struct iscsi_session *iscsi =
- container_of ( scsi->backend, struct iscsi_session, refcnt );
-
- xfer_nullify ( &iscsi->socket );
- iscsi_close_connection ( iscsi, 0 );
- process_del ( &iscsi->process );
- scsi->command = iscsi_detached_command;
- ref_put ( scsi->backend );
- scsi->backend = NULL;
- }
-
-
-
-
- enum iscsi_root_path_component {
- RP_LITERAL = 0,
- RP_SERVERNAME,
- RP_PROTOCOL,
- RP_PORT,
- RP_LUN,
- RP_TARGETNAME,
- NUM_RP_COMPONENTS
- };
-
-
- static int iscsi_parse_lun ( struct iscsi_session *iscsi,
- const char *lun_string ) {
- char *p = ( char * ) lun_string;
- union {
- uint64_t u64;
- uint16_t u16[4];
- } lun;
- int i;
-
-
- if ( ! *lun_string )
- return 0;
-
- for ( i = 0 ; i < 4 ; i++ ) {
- lun.u16[i] = strtoul ( p, &p, 16 );
- if ( *p != '-' )
- return -EINVAL;
- p++;
- }
- if ( *p )
- return -EINVAL;
-
- iscsi->lun = lun.u64;
- return 0;
- }
-
-
- static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
- const char *root_path ) {
- char rp_copy[ strlen ( root_path ) + 1 ];
- char *rp_comp[NUM_RP_COMPONENTS];
- char *rp = rp_copy;
- int i = 0;
- int rc;
-
-
- strcpy ( rp_copy, root_path );
- while ( 1 ) {
- rp_comp[i++] = rp;
- if ( i == NUM_RP_COMPONENTS )
- break;
- for ( ; *rp != ':' ; rp++ ) {
- if ( ! *rp ) {
- DBGC ( iscsi, "iSCSI %p root path \"%s\" "
- "too short\n", iscsi, root_path );
- return -EINVAL;
- }
- }
- *(rp++) = '\0';
- }
-
-
- iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
- if ( ! iscsi->target_address )
- return -ENOMEM;
- iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
- if ( ! iscsi->target_port )
- iscsi->target_port = ISCSI_PORT;
- if ( ( rc = iscsi_parse_lun ( iscsi, rp_comp[RP_LUN] ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
- iscsi, rp_comp[RP_LUN] );
- return rc;
- }
- iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
- if ( ! iscsi->target_iqn )
- return -ENOMEM;
-
- return 0;
- }
-
-
- static int iscsi_set_auth ( struct iscsi_session *iscsi,
- const char *username, const char *password ) {
-
- if ( username ) {
- iscsi->username = strdup ( username );
- if ( ! iscsi->username )
- return -ENOMEM;
- }
-
- if ( password ) {
- iscsi->password = strdup ( password );
- if ( ! iscsi->password )
- return -ENOMEM;
- }
-
- return 0;
- }
-
-
- int iscsi_attach ( struct scsi_device *scsi, const char *root_path ) {
- struct iscsi_session *iscsi;
- int rc;
-
-
- iscsi = zalloc ( sizeof ( *iscsi ) );
- if ( ! iscsi )
- return -ENOMEM;
- iscsi->refcnt.free = iscsi_free;
- xfer_init ( &iscsi->socket, &iscsi_socket_operations, &iscsi->refcnt );
- process_init ( &iscsi->process, iscsi_tx_step, &iscsi->refcnt );
-
-
- if ( ( rc = iscsi_parse_root_path ( iscsi, root_path ) ) != 0 )
- goto err;
-
- if ( ( rc = iscsi_set_auth ( iscsi, iscsi_username,
- iscsi_password ) ) != 0 )
- goto err;
-
-
- if ( ! iscsi->target_address ) {
- DBGC ( iscsi, "iSCSI %p does not yet support discovery\n",
- iscsi );
- rc = -ENOTSUP;
- goto err;
- }
- if ( ! iscsi->target_iqn ) {
- DBGC ( iscsi, "iSCSI %p no target address supplied in %s\n",
- iscsi, root_path );
- rc = -EINVAL;
- goto err;
- }
-
-
- scsi->backend = ref_get ( &iscsi->refcnt );
- scsi->command = iscsi_command;
- scsi->lun = iscsi->lun;
- ref_put ( &iscsi->refcnt );
- return 0;
-
- err:
- ref_put ( &iscsi->refcnt );
- return rc;
- }
-
-
-
-
- static int apply_dhcp_iscsi_string ( unsigned int tag,
- struct dhcp_option *option ) {
- char *prefix = "";
- size_t prefix_len;
- size_t len;
- char **string;
- char *p;
-
-
- switch ( tag ) {
- case DHCP_ISCSI_INITIATOR_IQN:
- string = &iscsi_explicit_initiator_iqn;
- break;
- case DHCP_EB_USERNAME:
- string = &iscsi_username;
- break;
- case DHCP_EB_PASSWORD:
- string = &iscsi_password;
- break;
- case DHCP_HOST_NAME:
- string = &iscsi_default_initiator_iqn;
- prefix = "iqn.2000-09.org.etherboot:";
- break;
- default:
- assert ( 0 );
- return -EINVAL;
- }
-
-
- free ( *string );
- *string = NULL;
-
-
- prefix_len = strlen ( prefix );
- len = ( prefix_len + option->len + 1 );
- p = *string = malloc ( len );
- if ( ! p )
- return -ENOMEM;
- strcpy ( p, prefix );
- dhcp_snprintf ( ( p + prefix_len ), ( len - prefix_len ), option );
- return 0;
- }
-
-
- struct dhcp_option_applicator dhcp_iscsi_applicators[] __dhcp_applicator = {
- {
- .tag = DHCP_ISCSI_INITIATOR_IQN,
- .apply = apply_dhcp_iscsi_string,
- },
- {
- .tag = DHCP_EB_USERNAME,
- .apply = apply_dhcp_iscsi_string,
- },
- {
- .tag = DHCP_EB_PASSWORD,
- .apply = apply_dhcp_iscsi_string,
- },
- {
- .tag = DHCP_HOST_NAME,
- .apply = apply_dhcp_iscsi_string,
- },
- };
-
-
-
-
- const char * iscsi_initiator_iqn ( void ) {
-
- if ( iscsi_explicit_initiator_iqn )
- return iscsi_explicit_initiator_iqn;
- if ( iscsi_default_initiator_iqn )
- return iscsi_default_initiator_iqn;
- return "iqn.2000-09.org.etherboot:UNKNOWN";
- }
|