1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945 |
-
-
- FILE_LICENCE ( GPL2_OR_LATER );
-
- #include <stddef.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <assert.h>
- #include <byteswap.h>
- #include <ipxe/vsprintf.h>
- #include <ipxe/socket.h>
- #include <ipxe/xfer.h>
- #include <ipxe/open.h>
- #include <ipxe/scsi.h>
- #include <ipxe/process.h>
- #include <ipxe/uaccess.h>
- #include <ipxe/tcpip.h>
- #include <ipxe/settings.h>
- #include <ipxe/features.h>
- #include <ipxe/base16.h>
- #include <ipxe/iscsi.h>
-
-
-
- FEATURE ( FEATURE_PROTOCOL, "iSCSI", DHCP_EB_FEATURE_ISCSI, 1 );
-
-
- #define EACCES_INCORRECT_TARGET_USERNAME ( EACCES | EUNIQ_01 )
- #define EACCES_INCORRECT_TARGET_PASSWORD ( EACCES | EUNIQ_02 )
- #define ENOTSUP_INITIATOR_STATUS ( ENOTSUP | EUNIQ_01 )
- #define ENOTSUP_OPCODE ( ENOTSUP | EUNIQ_02 )
- #define ENOTSUP_DISCOVERY ( ENOTSUP | EUNIQ_03 )
- #define EPERM_INITIATOR_AUTHENTICATION ( EPERM | EUNIQ_01 )
- #define EPERM_INITIATOR_AUTHORISATION ( EPERM | EUNIQ_02 )
- #define EPROTO_INVALID_CHAP_ALGORITHM ( EPROTO | EUNIQ_01 )
- #define EPROTO_INVALID_CHAP_IDENTIFIER ( EPROTO | EUNIQ_02 )
- #define EPROTO_INVALID_CHAP_CHALLENGE ( EPROTO | EUNIQ_03 )
- #define EPROTO_INVALID_CHAP_RESPONSE ( EPROTO | EUNIQ_04 )
-
-
- static char *iscsi_explicit_initiator_iqn;
-
-
- static char *iscsi_default_initiator_iqn;
-
-
- static char *iscsi_initiator_username;
-
-
- static char *iscsi_initiator_password;
-
-
- static char *iscsi_target_username;
-
-
- static char *iscsi_target_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->initiator_username );
- free ( iscsi->initiator_password );
- free ( iscsi->target_username );
- free ( iscsi->target_password );
- chap_finish ( &iscsi->chap );
- iscsi_rx_buffered_data_done ( iscsi );
- free ( iscsi );
- }
-
-
- 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 );
- if ( iscsi->target_username )
- iscsi->status |= ISCSI_STATUS_AUTH_REVERSE_REQUIRED;
-
-
- 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 );
- assert ( iscsi->command != NULL );
-
- iscsi->command->rc = rc;
- iscsi->command = NULL;
- }
-
-
-
-
- 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 ));
- DBGC2 ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#zx\n",
- iscsi, SCSI_CDB_DATA ( command->cdb ),
- ( iscsi->command->data_in ? "in" : "out" ),
- ( iscsi->command->data_in ?
- iscsi->command->data_in_len :
- iscsi->command->data_out_len ) );
- }
-
-
- 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 ) {
- 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 ( remaining )
- return 0;
-
-
- if ( data_in->flags & ISCSI_DATA_FLAG_STATUS ) {
- assert ( ( offset + len ) == iscsi->command->data_in_len );
- assert ( data_in->flags & ISCSI_FLAG_FINAL );
- iscsi->command->status = data_in->status;
-
- 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;
- const char *auth_method;
-
- if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
-
- auth_method = "None";
-
- if ( iscsi->initiator_username )
- auth_method = "CHAP,None";
-
- if ( iscsi->target_username )
- auth_method = "CHAP";
- used += ssnprintf ( data + used, len - used,
- "InitiatorName=%s%c"
- "TargetName=%s%c"
- "SessionType=Normal%c"
- "AuthMethod=%s%c",
- iscsi_initiator_iqn(), 0,
- iscsi->target_iqn, 0, 0,
- auth_method, 0 );
- }
-
- if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
- used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
- }
-
- if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
- char buf[ base16_encoded_len ( iscsi->chap.response_len ) + 1 ];
- assert ( iscsi->initiator_username != NULL );
- base16_encode ( iscsi->chap.response, iscsi->chap.response_len,
- buf );
- used += ssnprintf ( data + used, len - used,
- "CHAP_N=%s%cCHAP_R=0x%s%c",
- iscsi->initiator_username, 0, buf, 0 );
- }
-
- if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_CHALLENGE ) ) {
- size_t challenge_len = ( sizeof ( iscsi->chap_challenge ) - 1 );
- char buf[ base16_encoded_len ( challenge_len ) + 1 ];
- base16_encode ( ( iscsi->chap_challenge + 1 ), challenge_len,
- buf );
- used += ssnprintf ( data + used, len - used,
- "CHAP_I=%d%cCHAP_C=0x%s%c",
- iscsi->chap_challenge[0], 0, buf, 0 );
- }
-
- if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
- used += ssnprintf ( data + used, len - used,
- "HeaderDigest=None%c"
- "DataDigest=None%c"
- "MaxConnections=1%c"
- "InitialR2T=Yes%c"
- "ImmediateData=No%c"
- "MaxRecvDataSegmentLength=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, 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 ) {
- char *separator;
-
- 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;
-
-
- iscsi->target_port = htons ( ISCSI_PORT );
- separator = strchr ( iscsi->target_address, ':' );
- if ( separator ) {
- *separator = '\0';
- iscsi->target_port = strtoul ( ( separator + 1 ), NULL, 0 );
- }
-
- return 0;
- }
-
-
- 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 |
- ISCSI_STATUS_AUTH_FORWARD_REQUIRED );
- }
-
- return 0;
- }
-
-
- static int iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
- const char *value ) {
-
-
-
- if ( strcmp ( value, "5" ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
- iscsi, value );
- return -EPROTO_INVALID_CHAP_ALGORITHM;
- }
-
- return 0;
- }
-
-
- static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
- const char *value ) {
- unsigned int identifier;
- char *endp;
- int rc;
-
-
- identifier = strtoul ( value, &endp, 0 );
- if ( *endp != '\0' ) {
- DBGC ( iscsi, "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
- iscsi, value );
- return -EPROTO_INVALID_CHAP_IDENTIFIER;
- }
-
-
- chap_finish ( &iscsi->chap );
- if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
- iscsi, strerror ( rc ) );
- return rc;
- }
-
-
-
- chap_set_identifier ( &iscsi->chap, identifier );
- if ( iscsi->initiator_password ) {
- chap_update ( &iscsi->chap, iscsi->initiator_password,
- strlen ( iscsi->initiator_password ) );
- }
-
- return 0;
- }
-
-
- static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
- const char *value ) {
- uint8_t buf[ strlen ( value ) ];
- unsigned int i;
- int len;
-
-
- if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
- DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge \"%s\"\n",
- iscsi, value );
- return -EPROTO_INVALID_CHAP_CHALLENGE;
- }
-
-
- len = base16_decode ( ( value + 2 ), buf );
- if ( len < 0 ) {
- DBGC ( iscsi, "iSCSI %p invalid CHAP challenge \"%s\": %s\n",
- iscsi, value, strerror ( len ) );
- return len;
- }
- chap_update ( &iscsi->chap, buf, len );
-
-
- DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );
- chap_respond ( &iscsi->chap );
- iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_RESPONSE;
-
-
- if ( iscsi->target_username ) {
- iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_CHALLENGE;
-
- for ( i = 0 ; i < sizeof ( iscsi->chap_challenge ) ; i++ ) {
- iscsi->chap_challenge[i] = random();
- }
- }
-
- return 0;
- }
-
-
- static int iscsi_handle_chap_n_value ( struct iscsi_session *iscsi,
- const char *value ) {
-
-
-
- if ( iscsi->target_username &&
- ( strcmp ( iscsi->target_username, value ) != 0 ) ) {
- DBGC ( iscsi, "iSCSI %p target username \"%s\" incorrect "
- "(wanted \"%s\")\n",
- iscsi, value, iscsi->target_username );
- return -EACCES_INCORRECT_TARGET_USERNAME;
- }
-
- return 0;
- }
-
-
- static int iscsi_handle_chap_r_value ( struct iscsi_session *iscsi,
- const char *value ) {
- char buf[3];
- char *endp;
- uint8_t byte;
- unsigned int i;
- int rc;
-
-
- chap_finish ( &iscsi->chap );
- if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
- DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
- iscsi, strerror ( rc ) );
- return rc;
- }
- chap_set_identifier ( &iscsi->chap, iscsi->chap_challenge[0] );
- if ( iscsi->target_password ) {
- chap_update ( &iscsi->chap, iscsi->target_password,
- strlen ( iscsi->target_password ) );
- }
- chap_update ( &iscsi->chap, &iscsi->chap_challenge[1],
- ( sizeof ( iscsi->chap_challenge ) - 1 ) );
- chap_respond ( &iscsi->chap );
-
-
- if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
- DBGC ( iscsi, "iSCSI %p saw invalid CHAP response \"%s\"\n",
- iscsi, value );
- return -EPROTO_INVALID_CHAP_RESPONSE;
- }
- value += 2;
-
-
- if ( strlen ( value ) != ( 2 * iscsi->chap.response_len ) ) {
- DBGC ( iscsi, "iSCSI %p invalid CHAP response length\n",
- iscsi );
- return -EPROTO_INVALID_CHAP_RESPONSE;
- }
-
-
- for ( i = 0 ; ( value[0] && value[1] ) ; value += 2, i++ ) {
- memcpy ( buf, value, 2 );
- buf[2] = 0;
- byte = strtoul ( buf, &endp, 16 );
- if ( *endp != '\0' ) {
- DBGC ( iscsi, "iSCSI %p saw invalid CHAP response "
- "byte \"%s\"\n", iscsi, buf );
- return -EPROTO_INVALID_CHAP_RESPONSE;
- }
- if ( byte != iscsi->chap.response[i] ) {
- DBGC ( iscsi, "iSCSI %p saw incorrect CHAP "
- "response\n", iscsi );
- return -EACCES_INCORRECT_TARGET_PASSWORD;
- }
- }
- assert ( i == iscsi->chap.response_len );
-
-
- iscsi->status |= ISCSI_STATUS_AUTH_REVERSE_OK;
-
- 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 },
- { "CHAP_N=", iscsi_handle_chap_n_value },
- { "CHAP_R=", iscsi_handle_chap_r_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_status_to_rc ( unsigned int status_class,
- unsigned int status_detail ) {
- switch ( status_class ) {
- case ISCSI_STATUS_INITIATOR_ERROR :
- switch ( status_detail ) {
- case ISCSI_STATUS_INITIATOR_ERROR_AUTHENTICATION :
- return -EPERM_INITIATOR_AUTHENTICATION;
- case ISCSI_STATUS_INITIATOR_ERROR_AUTHORISATION :
- return -EPERM_INITIATOR_AUTHORISATION;
- case ISCSI_STATUS_INITIATOR_ERROR_NOT_FOUND :
- case ISCSI_STATUS_INITIATOR_ERROR_REMOVED :
- return -ENODEV;
- default :
- return -ENOTSUP_INITIATOR_STATUS;
- }
- case ISCSI_STATUS_TARGET_ERROR :
- return -EIO;
- default :
- return -EINVAL;
- }
- }
-
-
- 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 );
- rc = iscsi_status_to_rc ( response->status_class,
- response->status_detail );
- iscsi->instant_rc = rc;
- return rc;
- }
-
-
- if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
- iscsi->status &= ~( ISCSI_STATUS_PHASE_MASK |
- ISCSI_STATUS_STRINGS_MASK );
- 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;
- }
-
-
- if ( ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) &&
- ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_OK ) ) {
- DBGC ( iscsi, "iSCSI %p nefarious target tried to bypass "
- "authentication\n", iscsi );
- return -EPROTO;
- }
-
-
- 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 ) ) {
- DBGC2 ( iscsi, "iSCSI %p received PDU opcode %#x len %#x\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 -ENOTSUP_OPCODE;
- }
- }
-
-
- 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_vreopen ( socket, type, args );
- }
-
-
-
- static struct xfer_interface_operations iscsi_socket_operations = {
- .close = iscsi_socket_close,
- .vredirect = iscsi_vredirect,
- .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;
-
-
- if ( iscsi->instant_rc )
- return iscsi->instant_rc;
-
-
- iscsi->command = command;
-
-
- if ( iscsi->status ) {
- iscsi_start_command ( iscsi );
- } else {
- if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
- iscsi->command = NULL;
- return rc;
- }
- }
-
- return 0;
- }
-
-
- 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 = scsi_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_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 = scsi_parse_lun ( rp_comp[RP_LUN], &iscsi->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 *initiator_username,
- const char *initiator_password,
- const char *target_username,
- const char *target_password ) {
-
-
- if ( initiator_username || initiator_password ||
- target_username || target_password ) {
-
-
- if ( ! ( initiator_username && initiator_password ) )
- goto invalid_auth;
-
-
- iscsi->initiator_username = strdup ( initiator_username );
- if ( ! iscsi->initiator_username )
- return -ENOMEM;
- iscsi->initiator_password = strdup ( initiator_password );
- if ( ! iscsi->initiator_password )
- return -ENOMEM;
-
-
- if ( target_username || target_password ) {
-
-
- if ( ! ( target_username && target_password ) )
- goto invalid_auth;
-
-
- iscsi->target_username = strdup ( target_username );
- if ( ! iscsi->target_username )
- return -ENOMEM;
- iscsi->target_password = strdup ( target_password );
- if ( ! iscsi->target_password )
- return -ENOMEM;
- }
- }
-
- return 0;
-
- invalid_auth:
- DBGC ( iscsi, "iSCSI %p invalid credentials: initiator "
- "%sname,%spw, target %sname,%spw\n", iscsi,
- ( initiator_username ? "" : "no " ),
- ( initiator_password ? "" : "no " ),
- ( target_username ? "" : "no " ),
- ( target_password ? "" : "no " ) );
- return -EINVAL;
- }
-
-
- 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_initiator_username,
- iscsi_initiator_password,
- iscsi_target_username,
- iscsi_target_password ) ) != 0 )
- goto err;
-
-
- if ( ! iscsi->target_address ) {
- DBGC ( iscsi, "iSCSI %p does not yet support discovery\n",
- iscsi );
- rc = -ENOTSUP_DISCOVERY;
- 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;
- ref_put ( &iscsi->refcnt );
- return 0;
-
- err:
- ref_put ( &iscsi->refcnt );
- return rc;
- }
-
-
-
-
- struct setting initiator_iqn_setting __setting = {
- .name = "initiator-iqn",
- .description = "iSCSI initiator name",
- .tag = DHCP_ISCSI_INITIATOR_IQN,
- .type = &setting_type_string,
- };
-
-
- struct setting reverse_username_setting __setting = {
- .name = "reverse-username",
- .description = "Reverse user name",
- .tag = DHCP_EB_REVERSE_USERNAME,
- .type = &setting_type_string,
- };
-
-
- struct setting reverse_password_setting __setting = {
- .name = "reverse-password",
- .description = "Reverse password",
- .tag = DHCP_EB_REVERSE_PASSWORD,
- .type = &setting_type_string,
- };
-
-
- struct iscsi_string_setting {
-
- struct setting *setting;
-
- char **string;
-
- const char *prefix;
- };
-
-
- static struct iscsi_string_setting iscsi_string_settings[] = {
- {
- .setting = &initiator_iqn_setting,
- .string = &iscsi_explicit_initiator_iqn,
- .prefix = "",
- },
- {
- .setting = &username_setting,
- .string = &iscsi_initiator_username,
- .prefix = "",
- },
- {
- .setting = &password_setting,
- .string = &iscsi_initiator_password,
- .prefix = "",
- },
- {
- .setting = &reverse_username_setting,
- .string = &iscsi_target_username,
- .prefix = "",
- },
- {
- .setting = &reverse_password_setting,
- .string = &iscsi_target_password,
- .prefix = "",
- },
- {
- .setting = &hostname_setting,
- .string = &iscsi_default_initiator_iqn,
- .prefix = "iqn.2000-01.org.etherboot:",
- },
- };
-
-
- static int apply_iscsi_string_setting ( struct iscsi_string_setting *setting ){
- size_t prefix_len;
- int setting_len;
- size_t len;
- int check_len;
- char *p;
-
-
- free ( *setting->string );
- *setting->string = NULL;
-
-
- prefix_len = strlen ( setting->prefix );
- setting_len = fetch_setting_len ( NULL, setting->setting );
- if ( setting_len < 0 ) {
-
- return 0;
- }
- len = ( prefix_len + setting_len + 1 );
- p = *setting->string = malloc ( len );
- if ( ! p )
- return -ENOMEM;
-
-
- strcpy ( p, setting->prefix );
- check_len = fetch_string_setting ( NULL, setting->setting,
- ( p + prefix_len ),
- ( len - prefix_len ) );
- assert ( check_len == setting_len );
-
- return 0;
- }
-
-
- static int apply_iscsi_settings ( void ) {
- struct iscsi_string_setting *setting;
- unsigned int i;
- int rc;
-
- for ( i = 0 ; i < ( sizeof ( iscsi_string_settings ) /
- sizeof ( iscsi_string_settings[0] ) ) ; i++ ) {
- setting = &iscsi_string_settings[i];
- if ( ( rc = apply_iscsi_string_setting ( setting ) ) != 0 ) {
- DBG ( "iSCSI could not apply setting %s\n",
- setting->setting->name );
- return rc;
- }
- }
-
- return 0;
- }
-
-
- struct settings_applicator iscsi_settings_applicator __settings_applicator = {
- .apply = apply_iscsi_settings,
- };
-
-
-
-
- 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";
- }
|