You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iscsi.c 52KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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.
  8. *
  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.
  13. *
  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.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stddef.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <assert.h>
  25. #include <byteswap.h>
  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>
  37. /** @file
  38. *
  39. * iSCSI protocol
  40. *
  41. */
  42. FEATURE ( FEATURE_PROTOCOL, "iSCSI", DHCP_EB_FEATURE_ISCSI, 1 );
  43. /** iSCSI initiator name (explicitly specified) */
  44. static char *iscsi_explicit_initiator_iqn;
  45. /** Default iSCSI initiator name (constructed from hostname) */
  46. static char *iscsi_default_initiator_iqn;
  47. /** iSCSI initiator username */
  48. static char *iscsi_initiator_username;
  49. /** iSCSI initiator password */
  50. static char *iscsi_initiator_password;
  51. /** iSCSI target username */
  52. static char *iscsi_target_username;
  53. /** iSCSI target password */
  54. static char *iscsi_target_password;
  55. static void iscsi_start_tx ( struct iscsi_session *iscsi );
  56. static void iscsi_start_login ( struct iscsi_session *iscsi );
  57. static void iscsi_start_data_out ( struct iscsi_session *iscsi,
  58. unsigned int datasn );
  59. /**
  60. * Finish receiving PDU data into buffer
  61. *
  62. * @v iscsi iSCSI session
  63. */
  64. static void iscsi_rx_buffered_data_done ( struct iscsi_session *iscsi ) {
  65. free ( iscsi->rx_buffer );
  66. iscsi->rx_buffer = NULL;
  67. }
  68. /**
  69. * Free iSCSI session
  70. *
  71. * @v refcnt Reference counter
  72. */
  73. static void iscsi_free ( struct refcnt *refcnt ) {
  74. struct iscsi_session *iscsi =
  75. container_of ( refcnt, struct iscsi_session, refcnt );
  76. free ( iscsi->target_address );
  77. free ( iscsi->target_iqn );
  78. free ( iscsi->initiator_username );
  79. free ( iscsi->initiator_password );
  80. free ( iscsi->target_username );
  81. free ( iscsi->target_password );
  82. chap_finish ( &iscsi->chap );
  83. iscsi_rx_buffered_data_done ( iscsi );
  84. free ( iscsi );
  85. }
  86. /**
  87. * Open iSCSI transport-layer connection
  88. *
  89. * @v iscsi iSCSI session
  90. * @ret rc Return status code
  91. */
  92. static int iscsi_open_connection ( struct iscsi_session *iscsi ) {
  93. struct sockaddr_tcpip target;
  94. int rc;
  95. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  96. assert ( iscsi->rx_state == ISCSI_RX_BHS );
  97. assert ( iscsi->rx_offset == 0 );
  98. /* Open socket */
  99. memset ( &target, 0, sizeof ( target ) );
  100. target.st_port = htons ( iscsi->target_port );
  101. if ( ( rc = xfer_open_named_socket ( &iscsi->socket, SOCK_STREAM,
  102. ( struct sockaddr * ) &target,
  103. iscsi->target_address,
  104. NULL ) ) != 0 ) {
  105. DBGC ( iscsi, "iSCSI %p could not open socket: %s\n",
  106. iscsi, strerror ( rc ) );
  107. return rc;
  108. }
  109. /* Enter security negotiation phase */
  110. iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
  111. ISCSI_STATUS_STRINGS_SECURITY );
  112. if ( iscsi->target_username )
  113. iscsi->status |= ISCSI_STATUS_AUTH_REVERSE_REQUIRED;
  114. /* Assign fresh initiator task tag */
  115. iscsi->itt++;
  116. /* Initiate login */
  117. iscsi_start_login ( iscsi );
  118. return 0;
  119. }
  120. /**
  121. * Close iSCSI transport-layer connection
  122. *
  123. * @v iscsi iSCSI session
  124. * @v rc Reason for close
  125. *
  126. * Closes the transport-layer connection and resets the session state
  127. * ready to attempt a fresh login.
  128. */
  129. static void iscsi_close_connection ( struct iscsi_session *iscsi, int rc ) {
  130. /* Close all data transfer interfaces */
  131. xfer_close ( &iscsi->socket, rc );
  132. /* Clear connection status */
  133. iscsi->status = 0;
  134. /* Reset TX and RX state machines */
  135. iscsi->tx_state = ISCSI_TX_IDLE;
  136. iscsi->rx_state = ISCSI_RX_BHS;
  137. iscsi->rx_offset = 0;
  138. /* Free any temporary dynamically allocated memory */
  139. chap_finish ( &iscsi->chap );
  140. iscsi_rx_buffered_data_done ( iscsi );
  141. }
  142. /**
  143. * Mark iSCSI SCSI operation as complete
  144. *
  145. * @v iscsi iSCSI session
  146. * @v rc Return status code
  147. *
  148. * Note that iscsi_scsi_done() will not close the connection, and must
  149. * therefore be called only when the internal state machines are in an
  150. * appropriate state, otherwise bad things may happen on the next call
  151. * to iscsi_issue(). The general rule is to call iscsi_scsi_done()
  152. * only at the end of receiving a PDU; at this point the TX and RX
  153. * engines should both be idle.
  154. */
  155. static void iscsi_scsi_done ( struct iscsi_session *iscsi, int rc ) {
  156. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  157. assert ( iscsi->command != NULL );
  158. iscsi->command->rc = rc;
  159. iscsi->command = NULL;
  160. }
  161. /****************************************************************************
  162. *
  163. * iSCSI SCSI command issuing
  164. *
  165. */
  166. /**
  167. * Build iSCSI SCSI command BHS
  168. *
  169. * @v iscsi iSCSI session
  170. *
  171. * We don't currently support bidirectional commands (i.e. with both
  172. * Data-In and Data-Out segments); these would require providing code
  173. * to generate an AHS, and there doesn't seem to be any need for it at
  174. * the moment.
  175. */
  176. static void iscsi_start_command ( struct iscsi_session *iscsi ) {
  177. struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
  178. assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
  179. /* Construct BHS and initiate transmission */
  180. iscsi_start_tx ( iscsi );
  181. command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
  182. command->flags = ( ISCSI_FLAG_FINAL |
  183. ISCSI_COMMAND_ATTR_SIMPLE );
  184. if ( iscsi->command->data_in )
  185. command->flags |= ISCSI_COMMAND_FLAG_READ;
  186. if ( iscsi->command->data_out )
  187. command->flags |= ISCSI_COMMAND_FLAG_WRITE;
  188. /* lengths left as zero */
  189. command->lun = iscsi->lun;
  190. command->itt = htonl ( ++iscsi->itt );
  191. command->exp_len = htonl ( iscsi->command->data_in_len |
  192. iscsi->command->data_out_len );
  193. command->cmdsn = htonl ( iscsi->cmdsn );
  194. command->expstatsn = htonl ( iscsi->statsn + 1 );
  195. memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
  196. DBGC2 ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#zx\n",
  197. iscsi, SCSI_CDB_DATA ( command->cdb ),
  198. ( iscsi->command->data_in ? "in" : "out" ),
  199. ( iscsi->command->data_in ?
  200. iscsi->command->data_in_len :
  201. iscsi->command->data_out_len ) );
  202. }
  203. /**
  204. * Receive data segment of an iSCSI SCSI response PDU
  205. *
  206. * @v iscsi iSCSI session
  207. * @v data Received data
  208. * @v len Length of received data
  209. * @v remaining Data remaining after this data
  210. * @ret rc Return status code
  211. */
  212. static int iscsi_rx_scsi_response ( struct iscsi_session *iscsi,
  213. const void *data, size_t len,
  214. size_t remaining ) {
  215. struct iscsi_bhs_scsi_response *response
  216. = &iscsi->rx_bhs.scsi_response;
  217. int sense_offset;
  218. /* Capture the sense response code as it floats past, if present */
  219. sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
  220. if ( ( sense_offset >= 0 ) && len ) {
  221. iscsi->command->sense_response =
  222. * ( ( char * ) data + sense_offset );
  223. }
  224. /* Wait for whole SCSI response to arrive */
  225. if ( remaining )
  226. return 0;
  227. /* Record SCSI status code */
  228. iscsi->command->status = response->status;
  229. /* Check for errors */
  230. if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
  231. return -EIO;
  232. /* Mark as completed */
  233. iscsi_scsi_done ( iscsi, 0 );
  234. return 0;
  235. }
  236. /**
  237. * Receive data segment of an iSCSI data-in PDU
  238. *
  239. * @v iscsi iSCSI session
  240. * @v data Received data
  241. * @v len Length of received data
  242. * @v remaining Data remaining after this data
  243. * @ret rc Return status code
  244. */
  245. static int iscsi_rx_data_in ( struct iscsi_session *iscsi,
  246. const void *data, size_t len,
  247. size_t remaining ) {
  248. struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
  249. unsigned long offset;
  250. /* Copy data to data-in buffer */
  251. offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
  252. assert ( iscsi->command != NULL );
  253. assert ( iscsi->command->data_in );
  254. assert ( ( offset + len ) <= iscsi->command->data_in_len );
  255. copy_to_user ( iscsi->command->data_in, offset, data, len );
  256. /* Wait for whole SCSI response to arrive */
  257. if ( remaining )
  258. return 0;
  259. /* Mark as completed if status is present */
  260. if ( data_in->flags & ISCSI_DATA_FLAG_STATUS ) {
  261. assert ( ( offset + len ) == iscsi->command->data_in_len );
  262. assert ( data_in->flags & ISCSI_FLAG_FINAL );
  263. iscsi->command->status = data_in->status;
  264. /* iSCSI cannot return an error status via a data-in */
  265. iscsi_scsi_done ( iscsi, 0 );
  266. }
  267. return 0;
  268. }
  269. /**
  270. * Receive data segment of an iSCSI R2T PDU
  271. *
  272. * @v iscsi iSCSI session
  273. * @v data Received data
  274. * @v len Length of received data
  275. * @v remaining Data remaining after this data
  276. * @ret rc Return status code
  277. */
  278. static int iscsi_rx_r2t ( struct iscsi_session *iscsi,
  279. const void *data __unused, size_t len __unused,
  280. size_t remaining __unused ) {
  281. struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
  282. /* Record transfer parameters and trigger first data-out */
  283. iscsi->ttt = ntohl ( r2t->ttt );
  284. iscsi->transfer_offset = ntohl ( r2t->offset );
  285. iscsi->transfer_len = ntohl ( r2t->len );
  286. iscsi_start_data_out ( iscsi, 0 );
  287. return 0;
  288. }
  289. /**
  290. * Build iSCSI data-out BHS
  291. *
  292. * @v iscsi iSCSI session
  293. * @v datasn Data sequence number within the transfer
  294. *
  295. */
  296. static void iscsi_start_data_out ( struct iscsi_session *iscsi,
  297. unsigned int datasn ) {
  298. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  299. unsigned long offset;
  300. unsigned long remaining;
  301. unsigned long len;
  302. /* We always send 512-byte Data-Out PDUs; this removes the
  303. * need to worry about the target's MaxRecvDataSegmentLength.
  304. */
  305. offset = datasn * 512;
  306. remaining = iscsi->transfer_len - offset;
  307. len = remaining;
  308. if ( len > 512 )
  309. len = 512;
  310. /* Construct BHS and initiate transmission */
  311. iscsi_start_tx ( iscsi );
  312. data_out->opcode = ISCSI_OPCODE_DATA_OUT;
  313. if ( len == remaining )
  314. data_out->flags = ( ISCSI_FLAG_FINAL );
  315. ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
  316. data_out->lun = iscsi->lun;
  317. data_out->itt = htonl ( iscsi->itt );
  318. data_out->ttt = htonl ( iscsi->ttt );
  319. data_out->expstatsn = htonl ( iscsi->statsn + 1 );
  320. data_out->datasn = htonl ( datasn );
  321. data_out->offset = htonl ( iscsi->transfer_offset + offset );
  322. DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
  323. iscsi, datasn, len );
  324. }
  325. /**
  326. * Complete iSCSI data-out PDU transmission
  327. *
  328. * @v iscsi iSCSI session
  329. *
  330. */
  331. static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
  332. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  333. /* If we haven't reached the end of the sequence, start
  334. * sending the next data-out PDU.
  335. */
  336. if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
  337. iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
  338. }
  339. /**
  340. * Send iSCSI data-out data segment
  341. *
  342. * @v iscsi iSCSI session
  343. * @ret rc Return status code
  344. */
  345. static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
  346. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  347. struct io_buffer *iobuf;
  348. unsigned long offset;
  349. size_t len;
  350. offset = ntohl ( data_out->offset );
  351. len = ISCSI_DATA_LEN ( data_out->lengths );
  352. assert ( iscsi->command != NULL );
  353. assert ( iscsi->command->data_out );
  354. assert ( ( offset + len ) <= iscsi->command->data_out_len );
  355. iobuf = xfer_alloc_iob ( &iscsi->socket, len );
  356. if ( ! iobuf )
  357. return -ENOMEM;
  358. copy_from_user ( iob_put ( iobuf, len ),
  359. iscsi->command->data_out, offset, len );
  360. return xfer_deliver_iob ( &iscsi->socket, iobuf );
  361. }
  362. /****************************************************************************
  363. *
  364. * iSCSI login
  365. *
  366. */
  367. /**
  368. * Build iSCSI login request strings
  369. *
  370. * @v iscsi iSCSI session
  371. *
  372. * These are the initial set of strings sent in the first login
  373. * request PDU. We want the following settings:
  374. *
  375. * HeaderDigest=None
  376. * DataDigest=None
  377. * MaxConnections is irrelevant; we make only one connection anyway
  378. * InitialR2T=Yes [1]
  379. * ImmediateData is irrelevant; we never send immediate data
  380. * MaxRecvDataSegmentLength=8192 (default; we don't care) [3]
  381. * MaxBurstLength=262144 (default; we don't care) [3]
  382. * FirstBurstLength=262144 (default; we don't care)
  383. * DefaultTime2Wait=0 [2]
  384. * DefaultTime2Retain=0 [2]
  385. * MaxOutstandingR2T=1
  386. * DataPDUInOrder=Yes
  387. * DataSequenceInOrder=Yes
  388. * ErrorRecoveryLevel=0
  389. *
  390. * [1] InitialR2T has an OR resolution function, so the target may
  391. * force us to use it. We therefore simplify our logic by always
  392. * using it.
  393. *
  394. * [2] These ensure that we can safely start a new task once we have
  395. * reconnected after a failure, without having to manually tidy up
  396. * after the old one.
  397. *
  398. * [3] We are quite happy to use the RFC-defined default values for
  399. * these parameters, but some targets (notably OpenSolaris)
  400. * incorrectly assume a default value of zero, so we explicitly
  401. * specify the default values.
  402. */
  403. static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
  404. void *data, size_t len ) {
  405. unsigned int used = 0;
  406. unsigned int i;
  407. const char *auth_method;
  408. if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
  409. /* Default to allowing no authentication */
  410. auth_method = "None";
  411. /* If we have a credential to supply, permit CHAP */
  412. if ( iscsi->initiator_username )
  413. auth_method = "CHAP,None";
  414. /* If we have a credential to check, force CHAP */
  415. if ( iscsi->target_username )
  416. auth_method = "CHAP";
  417. used += ssnprintf ( data + used, len - used,
  418. "InitiatorName=%s%c"
  419. "TargetName=%s%c"
  420. "SessionType=Normal%c"
  421. "AuthMethod=%s%c",
  422. iscsi_initiator_iqn(), 0,
  423. iscsi->target_iqn, 0, 0,
  424. auth_method, 0 );
  425. }
  426. if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
  427. used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
  428. }
  429. if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
  430. assert ( iscsi->initiator_username != NULL );
  431. used += ssnprintf ( data + used, len - used,
  432. "CHAP_N=%s%cCHAP_R=0x",
  433. iscsi->initiator_username, 0 );
  434. for ( i = 0 ; i < iscsi->chap.response_len ; i++ ) {
  435. used += ssnprintf ( data + used, len - used, "%02x",
  436. iscsi->chap.response[i] );
  437. }
  438. used += ssnprintf ( data + used, len - used, "%c", 0 );
  439. }
  440. if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_CHALLENGE ) ) {
  441. used += ssnprintf ( data + used, len - used,
  442. "CHAP_I=%d%cCHAP_C=0x",
  443. iscsi->chap_challenge[0], 0 );
  444. for ( i = 1 ; i < sizeof ( iscsi->chap_challenge ) ; i++ ) {
  445. used += ssnprintf ( data + used, len - used, "%02x",
  446. iscsi->chap_challenge[i] );
  447. }
  448. used += ssnprintf ( data + used, len - used, "%c", 0 );
  449. }
  450. if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
  451. used += ssnprintf ( data + used, len - used,
  452. "HeaderDigest=None%c"
  453. "DataDigest=None%c"
  454. "InitialR2T=Yes%c"
  455. "MaxRecvDataSegmentLength=8192%c"
  456. "MaxBurstLength=262144%c"
  457. "DefaultTime2Wait=0%c"
  458. "DefaultTime2Retain=0%c"
  459. "MaxOutstandingR2T=1%c"
  460. "DataPDUInOrder=Yes%c"
  461. "DataSequenceInOrder=Yes%c"
  462. "ErrorRecoveryLevel=0%c",
  463. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  464. }
  465. return used;
  466. }
  467. /**
  468. * Build iSCSI login request BHS
  469. *
  470. * @v iscsi iSCSI session
  471. */
  472. static void iscsi_start_login ( struct iscsi_session *iscsi ) {
  473. struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
  474. int len;
  475. /* Construct BHS and initiate transmission */
  476. iscsi_start_tx ( iscsi );
  477. request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
  478. ISCSI_FLAG_IMMEDIATE );
  479. request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
  480. ISCSI_LOGIN_FLAG_TRANSITION );
  481. /* version_max and version_min left as zero */
  482. len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
  483. ISCSI_SET_LENGTHS ( request->lengths, 0, len );
  484. request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
  485. IANA_EN_FEN_SYSTEMS );
  486. /* isid_iana_qual left as zero */
  487. request->tsih = htons ( iscsi->tsih );
  488. request->itt = htonl ( iscsi->itt );
  489. /* cid left as zero */
  490. request->cmdsn = htonl ( iscsi->cmdsn );
  491. request->expstatsn = htonl ( iscsi->statsn + 1 );
  492. }
  493. /**
  494. * Complete iSCSI login request PDU transmission
  495. *
  496. * @v iscsi iSCSI session
  497. *
  498. */
  499. static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
  500. /* Clear any "strings to send" flags */
  501. iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
  502. /* Free any dynamically allocated storage used for login */
  503. chap_finish ( &iscsi->chap );
  504. }
  505. /**
  506. * Transmit data segment of an iSCSI login request PDU
  507. *
  508. * @v iscsi iSCSI session
  509. * @ret rc Return status code
  510. *
  511. * For login requests, the data segment consists of the login strings.
  512. */
  513. static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
  514. struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
  515. struct io_buffer *iobuf;
  516. size_t len;
  517. len = ISCSI_DATA_LEN ( request->lengths );
  518. iobuf = xfer_alloc_iob ( &iscsi->socket, len );
  519. if ( ! iobuf )
  520. return -ENOMEM;
  521. iob_put ( iobuf, len );
  522. iscsi_build_login_request_strings ( iscsi, iobuf->data, len );
  523. return xfer_deliver_iob ( &iscsi->socket, iobuf );
  524. }
  525. /**
  526. * Handle iSCSI TargetAddress text value
  527. *
  528. * @v iscsi iSCSI session
  529. * @v value TargetAddress value
  530. * @ret rc Return status code
  531. */
  532. static int iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
  533. const char *value ) {
  534. char *separator;
  535. DBGC ( iscsi, "iSCSI %p will redirect to %s\n", iscsi, value );
  536. /* Replace target address */
  537. free ( iscsi->target_address );
  538. iscsi->target_address = strdup ( value );
  539. if ( ! iscsi->target_address )
  540. return -ENOMEM;
  541. /* Replace target port */
  542. iscsi->target_port = htons ( ISCSI_PORT );
  543. separator = strchr ( iscsi->target_address, ':' );
  544. if ( separator ) {
  545. *separator = '\0';
  546. iscsi->target_port = strtoul ( ( separator + 1 ), NULL, 0 );
  547. }
  548. return 0;
  549. }
  550. /**
  551. * Handle iSCSI AuthMethod text value
  552. *
  553. * @v iscsi iSCSI session
  554. * @v value AuthMethod value
  555. * @ret rc Return status code
  556. */
  557. static int iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
  558. const char *value ) {
  559. /* If server requests CHAP, send the CHAP_A string */
  560. if ( strcmp ( value, "CHAP" ) == 0 ) {
  561. DBGC ( iscsi, "iSCSI %p initiating CHAP authentication\n",
  562. iscsi );
  563. iscsi->status |= ( ISCSI_STATUS_STRINGS_CHAP_ALGORITHM |
  564. ISCSI_STATUS_AUTH_FORWARD_REQUIRED );
  565. }
  566. return 0;
  567. }
  568. /**
  569. * Handle iSCSI CHAP_A text value
  570. *
  571. * @v iscsi iSCSI session
  572. * @v value CHAP_A value
  573. * @ret rc Return status code
  574. */
  575. static int iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
  576. const char *value ) {
  577. /* We only ever offer "5" (i.e. MD5) as an algorithm, so if
  578. * the server responds with anything else it is a protocol
  579. * violation.
  580. */
  581. if ( strcmp ( value, "5" ) != 0 ) {
  582. DBGC ( iscsi, "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
  583. iscsi, value );
  584. return -EPROTO;
  585. }
  586. return 0;
  587. }
  588. /**
  589. * Handle iSCSI CHAP_I text value
  590. *
  591. * @v iscsi iSCSI session
  592. * @v value CHAP_I value
  593. * @ret rc Return status code
  594. */
  595. static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
  596. const char *value ) {
  597. unsigned int identifier;
  598. char *endp;
  599. int rc;
  600. /* The CHAP identifier is an integer value */
  601. identifier = strtoul ( value, &endp, 0 );
  602. if ( *endp != '\0' ) {
  603. DBGC ( iscsi, "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
  604. iscsi, value );
  605. return -EPROTO;
  606. }
  607. /* Prepare for CHAP with MD5 */
  608. chap_finish ( &iscsi->chap );
  609. if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
  610. DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
  611. iscsi, strerror ( rc ) );
  612. return rc;
  613. }
  614. /* Identifier and secret are the first two components of the
  615. * challenge.
  616. */
  617. chap_set_identifier ( &iscsi->chap, identifier );
  618. if ( iscsi->initiator_password ) {
  619. chap_update ( &iscsi->chap, iscsi->initiator_password,
  620. strlen ( iscsi->initiator_password ) );
  621. }
  622. return 0;
  623. }
  624. /**
  625. * Handle iSCSI CHAP_C text value
  626. *
  627. * @v iscsi iSCSI session
  628. * @v value CHAP_C value
  629. * @ret rc Return status code
  630. */
  631. static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
  632. const char *value ) {
  633. char buf[3];
  634. char *endp;
  635. uint8_t byte;
  636. unsigned int i;
  637. /* Check and strip leading "0x" */
  638. if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
  639. DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge \"%s\"\n",
  640. iscsi, value );
  641. return -EPROTO;
  642. }
  643. value += 2;
  644. /* Process challenge an octet at a time */
  645. for ( ; ( value[0] && value[1] ) ; value += 2 ) {
  646. memcpy ( buf, value, 2 );
  647. buf[2] = 0;
  648. byte = strtoul ( buf, &endp, 16 );
  649. if ( *endp != '\0' ) {
  650. DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge "
  651. "byte \"%s\"\n", iscsi, buf );
  652. return -EPROTO;
  653. }
  654. chap_update ( &iscsi->chap, &byte, sizeof ( byte ) );
  655. }
  656. /* Build CHAP response */
  657. DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );
  658. chap_respond ( &iscsi->chap );
  659. iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_RESPONSE;
  660. /* Send CHAP challenge, if applicable */
  661. if ( iscsi->target_username ) {
  662. iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_CHALLENGE;
  663. /* Generate CHAP challenge data */
  664. for ( i = 0 ; i < sizeof ( iscsi->chap_challenge ) ; i++ ) {
  665. iscsi->chap_challenge[i] = random();
  666. }
  667. }
  668. return 0;
  669. }
  670. /**
  671. * Handle iSCSI CHAP_N text value
  672. *
  673. * @v iscsi iSCSI session
  674. * @v value CHAP_N value
  675. * @ret rc Return status code
  676. */
  677. static int iscsi_handle_chap_n_value ( struct iscsi_session *iscsi,
  678. const char *value ) {
  679. /* The target username isn't actually involved at any point in
  680. * the authentication process; it merely serves to identify
  681. * which password the target is using to generate the CHAP
  682. * response. We unnecessarily verify that the username is as
  683. * expected, in order to provide mildly helpful diagnostics if
  684. * the target is supplying the wrong username/password
  685. * combination.
  686. */
  687. if ( iscsi->target_username &&
  688. ( strcmp ( iscsi->target_username, value ) != 0 ) ) {
  689. DBGC ( iscsi, "iSCSI %p target username \"%s\" incorrect "
  690. "(wanted \"%s\")\n",
  691. iscsi, value, iscsi->target_username );
  692. return -EACCES;
  693. }
  694. return 0;
  695. }
  696. /**
  697. * Handle iSCSI CHAP_R text value
  698. *
  699. * @v iscsi iSCSI session
  700. * @v value CHAP_R value
  701. * @ret rc Return status code
  702. */
  703. static int iscsi_handle_chap_r_value ( struct iscsi_session *iscsi,
  704. const char *value ) {
  705. char buf[3];
  706. char *endp;
  707. uint8_t byte;
  708. unsigned int i;
  709. int rc;
  710. /* Generate CHAP response for verification */
  711. chap_finish ( &iscsi->chap );
  712. if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
  713. DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
  714. iscsi, strerror ( rc ) );
  715. return rc;
  716. }
  717. chap_set_identifier ( &iscsi->chap, iscsi->chap_challenge[0] );
  718. if ( iscsi->target_password ) {
  719. chap_update ( &iscsi->chap, iscsi->target_password,
  720. strlen ( iscsi->target_password ) );
  721. }
  722. chap_update ( &iscsi->chap, &iscsi->chap_challenge[1],
  723. ( sizeof ( iscsi->chap_challenge ) - 1 ) );
  724. chap_respond ( &iscsi->chap );
  725. /* Check and strip leading "0x" */
  726. if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
  727. DBGC ( iscsi, "iSCSI %p saw invalid CHAP response \"%s\"\n",
  728. iscsi, value );
  729. return -EPROTO;
  730. }
  731. value += 2;
  732. /* Check CHAP response length */
  733. if ( strlen ( value ) != ( 2 * iscsi->chap.response_len ) ) {
  734. DBGC ( iscsi, "iSCSI %p invalid CHAP response length\n",
  735. iscsi );
  736. return -EPROTO;
  737. }
  738. /* Process response an octet at a time */
  739. for ( i = 0 ; ( value[0] && value[1] ) ; value += 2, i++ ) {
  740. memcpy ( buf, value, 2 );
  741. buf[2] = 0;
  742. byte = strtoul ( buf, &endp, 16 );
  743. if ( *endp != '\0' ) {
  744. DBGC ( iscsi, "iSCSI %p saw invalid CHAP response "
  745. "byte \"%s\"\n", iscsi, buf );
  746. return -EPROTO;
  747. }
  748. if ( byte != iscsi->chap.response[i] ) {
  749. DBGC ( iscsi, "iSCSI %p saw incorrect CHAP "
  750. "response\n", iscsi );
  751. return -EACCES;
  752. }
  753. }
  754. assert ( i == iscsi->chap.response_len );
  755. /* Mark session as authenticated */
  756. iscsi->status |= ISCSI_STATUS_AUTH_REVERSE_OK;
  757. return 0;
  758. }
  759. /** An iSCSI text string that we want to handle */
  760. struct iscsi_string_type {
  761. /** String key
  762. *
  763. * This is the portion up to and including the "=" sign,
  764. * e.g. "InitiatorName=", "CHAP_A=", etc.
  765. */
  766. const char *key;
  767. /** Handle iSCSI string value
  768. *
  769. * @v iscsi iSCSI session
  770. * @v value iSCSI string value
  771. * @ret rc Return status code
  772. */
  773. int ( * handle ) ( struct iscsi_session *iscsi, const char *value );
  774. };
  775. /** iSCSI text strings that we want to handle */
  776. static struct iscsi_string_type iscsi_string_types[] = {
  777. { "TargetAddress=", iscsi_handle_targetaddress_value },
  778. { "AuthMethod=", iscsi_handle_authmethod_value },
  779. { "CHAP_A=", iscsi_handle_chap_a_value },
  780. { "CHAP_I=", iscsi_handle_chap_i_value },
  781. { "CHAP_C=", iscsi_handle_chap_c_value },
  782. { "CHAP_N=", iscsi_handle_chap_n_value },
  783. { "CHAP_R=", iscsi_handle_chap_r_value },
  784. { NULL, NULL }
  785. };
  786. /**
  787. * Handle iSCSI string
  788. *
  789. * @v iscsi iSCSI session
  790. * @v string iSCSI string (in "key=value" format)
  791. * @ret rc Return status code
  792. */
  793. static int iscsi_handle_string ( struct iscsi_session *iscsi,
  794. const char *string ) {
  795. struct iscsi_string_type *type;
  796. size_t key_len;
  797. int rc;
  798. for ( type = iscsi_string_types ; type->key ; type++ ) {
  799. key_len = strlen ( type->key );
  800. if ( strncmp ( string, type->key, key_len ) != 0 )
  801. continue;
  802. DBGC ( iscsi, "iSCSI %p handling %s\n", iscsi, string );
  803. if ( ( rc = type->handle ( iscsi,
  804. ( string + key_len ) ) ) != 0 ) {
  805. DBGC ( iscsi, "iSCSI %p could not handle %s: %s\n",
  806. iscsi, string, strerror ( rc ) );
  807. return rc;
  808. }
  809. return 0;
  810. }
  811. DBGC ( iscsi, "iSCSI %p ignoring %s\n", iscsi, string );
  812. return 0;
  813. }
  814. /**
  815. * Handle iSCSI strings
  816. *
  817. * @v iscsi iSCSI session
  818. * @v string iSCSI string buffer
  819. * @v len Length of string buffer
  820. * @ret rc Return status code
  821. */
  822. static int iscsi_handle_strings ( struct iscsi_session *iscsi,
  823. const char *strings, size_t len ) {
  824. size_t string_len;
  825. int rc;
  826. /* Handle each string in turn, taking care not to overrun the
  827. * data buffer in case of badly-terminated data.
  828. */
  829. while ( 1 ) {
  830. string_len = ( strnlen ( strings, len ) + 1 );
  831. if ( string_len > len )
  832. break;
  833. if ( ( rc = iscsi_handle_string ( iscsi, strings ) ) != 0 )
  834. return rc;
  835. strings += string_len;
  836. len -= string_len;
  837. }
  838. return 0;
  839. }
  840. /**
  841. * Receive PDU data into buffer
  842. *
  843. * @v iscsi iSCSI session
  844. * @v data Data to receive
  845. * @v len Length of data
  846. * @ret rc Return status code
  847. *
  848. * This can be used when the RX PDU type handler wishes to buffer up
  849. * all received data and process the PDU as a single unit. The caller
  850. * is repsonsible for calling iscsi_rx_buffered_data_done() after
  851. * processing the data.
  852. */
  853. static int iscsi_rx_buffered_data ( struct iscsi_session *iscsi,
  854. const void *data, size_t len ) {
  855. /* Allocate buffer on first call */
  856. if ( ! iscsi->rx_buffer ) {
  857. iscsi->rx_buffer = malloc ( iscsi->rx_len );
  858. if ( ! iscsi->rx_buffer )
  859. return -ENOMEM;
  860. }
  861. /* Copy data to buffer */
  862. assert ( ( iscsi->rx_offset + len ) <= iscsi->rx_len );
  863. memcpy ( ( iscsi->rx_buffer + iscsi->rx_offset ), data, len );
  864. return 0;
  865. }
  866. /**
  867. * Convert iSCSI response status to return status code
  868. *
  869. * @v status_class iSCSI status class
  870. * @v status_detail iSCSI status detail
  871. * @ret rc Return status code
  872. */
  873. static int iscsi_status_to_rc ( unsigned int status_class,
  874. unsigned int status_detail ) {
  875. switch ( status_class ) {
  876. case ISCSI_STATUS_INITIATOR_ERROR :
  877. switch ( status_detail ) {
  878. case ISCSI_STATUS_INITIATOR_ERROR_AUTHENTICATION :
  879. return -EACCES;
  880. case ISCSI_STATUS_INITIATOR_ERROR_AUTHORISATION :
  881. return -EPERM;
  882. case ISCSI_STATUS_INITIATOR_ERROR_NOT_FOUND :
  883. case ISCSI_STATUS_INITIATOR_ERROR_REMOVED :
  884. return -ENODEV;
  885. default :
  886. return -ENOTSUP;
  887. }
  888. case ISCSI_STATUS_TARGET_ERROR :
  889. return -EIO;
  890. default :
  891. return -EINVAL;
  892. }
  893. }
  894. /**
  895. * Receive data segment of an iSCSI login response PDU
  896. *
  897. * @v iscsi iSCSI session
  898. * @v data Received data
  899. * @v len Length of received data
  900. * @v remaining Data remaining after this data
  901. * @ret rc Return status code
  902. */
  903. static int iscsi_rx_login_response ( struct iscsi_session *iscsi,
  904. const void *data, size_t len,
  905. size_t remaining ) {
  906. struct iscsi_bhs_login_response *response
  907. = &iscsi->rx_bhs.login_response;
  908. int rc;
  909. /* Buffer up the PDU data */
  910. if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
  911. DBGC ( iscsi, "iSCSI %p could not buffer login response: %s\n",
  912. iscsi, strerror ( rc ) );
  913. return rc;
  914. }
  915. if ( remaining )
  916. return 0;
  917. /* Process string data and discard string buffer */
  918. if ( ( rc = iscsi_handle_strings ( iscsi, iscsi->rx_buffer,
  919. iscsi->rx_len ) ) != 0 )
  920. return rc;
  921. iscsi_rx_buffered_data_done ( iscsi );
  922. /* Check for login redirection */
  923. if ( response->status_class == ISCSI_STATUS_REDIRECT ) {
  924. DBGC ( iscsi, "iSCSI %p redirecting to new server\n", iscsi );
  925. iscsi_close_connection ( iscsi, 0 );
  926. if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
  927. DBGC ( iscsi, "iSCSI %p could not redirect: %s\n ",
  928. iscsi, strerror ( rc ) );
  929. return rc;
  930. }
  931. return 0;
  932. }
  933. /* Check for fatal errors */
  934. if ( response->status_class != 0 ) {
  935. DBGC ( iscsi, "iSCSI login failure: class %02x detail %02x\n",
  936. response->status_class, response->status_detail );
  937. rc = iscsi_status_to_rc ( response->status_class,
  938. response->status_detail );
  939. iscsi->instant_rc = rc;
  940. return rc;
  941. }
  942. /* Handle login transitions */
  943. if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
  944. iscsi->status &= ~( ISCSI_STATUS_PHASE_MASK |
  945. ISCSI_STATUS_STRINGS_MASK );
  946. switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
  947. case ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION:
  948. iscsi->status |=
  949. ( ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE |
  950. ISCSI_STATUS_STRINGS_OPERATIONAL );
  951. break;
  952. case ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE:
  953. iscsi->status |= ISCSI_STATUS_FULL_FEATURE_PHASE;
  954. break;
  955. default:
  956. DBGC ( iscsi, "iSCSI %p got invalid response flags "
  957. "%02x\n", iscsi, response->flags );
  958. return -EIO;
  959. }
  960. }
  961. /* Send next login request PDU if we haven't reached the full
  962. * feature phase yet.
  963. */
  964. if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
  965. ISCSI_STATUS_FULL_FEATURE_PHASE ) {
  966. iscsi_start_login ( iscsi );
  967. return 0;
  968. }
  969. /* Check that target authentication was successful (if required) */
  970. if ( ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) &&
  971. ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_OK ) ) {
  972. DBGC ( iscsi, "iSCSI %p nefarious target tried to bypass "
  973. "authentication\n", iscsi );
  974. return -EPROTO;
  975. }
  976. /* Reset retry count */
  977. iscsi->retry_count = 0;
  978. /* Record TSIH for future reference */
  979. iscsi->tsih = ntohl ( response->tsih );
  980. /* Send the actual SCSI command */
  981. iscsi_start_command ( iscsi );
  982. return 0;
  983. }
  984. /****************************************************************************
  985. *
  986. * iSCSI to socket interface
  987. *
  988. */
  989. /**
  990. * Start up a new TX PDU
  991. *
  992. * @v iscsi iSCSI session
  993. *
  994. * This initiates the process of sending a new PDU. Only one PDU may
  995. * be in transit at any one time.
  996. */
  997. static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
  998. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  999. /* Initialise TX BHS */
  1000. memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
  1001. /* Flag TX engine to start transmitting */
  1002. iscsi->tx_state = ISCSI_TX_BHS;
  1003. }
  1004. /**
  1005. * Transmit nothing
  1006. *
  1007. * @v iscsi iSCSI session
  1008. * @ret rc Return status code
  1009. */
  1010. static int iscsi_tx_nothing ( struct iscsi_session *iscsi __unused ) {
  1011. return 0;
  1012. }
  1013. /**
  1014. * Transmit basic header segment of an iSCSI PDU
  1015. *
  1016. * @v iscsi iSCSI session
  1017. * @ret rc Return status code
  1018. */
  1019. static int iscsi_tx_bhs ( struct iscsi_session *iscsi ) {
  1020. return xfer_deliver_raw ( &iscsi->socket, &iscsi->tx_bhs,
  1021. sizeof ( iscsi->tx_bhs ) );
  1022. }
  1023. /**
  1024. * Transmit data segment of an iSCSI PDU
  1025. *
  1026. * @v iscsi iSCSI session
  1027. * @ret rc Return status code
  1028. *
  1029. * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
  1030. * will be valid when this is called.
  1031. */
  1032. static int iscsi_tx_data ( struct iscsi_session *iscsi ) {
  1033. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1034. switch ( common->opcode & ISCSI_OPCODE_MASK ) {
  1035. case ISCSI_OPCODE_DATA_OUT:
  1036. return iscsi_tx_data_out ( iscsi );
  1037. case ISCSI_OPCODE_LOGIN_REQUEST:
  1038. return iscsi_tx_login_request ( iscsi );
  1039. default:
  1040. /* Nothing to send in other states */
  1041. return 0;
  1042. }
  1043. }
  1044. /**
  1045. * Transmit data padding of an iSCSI PDU
  1046. *
  1047. * @v iscsi iSCSI session
  1048. * @ret rc Return status code
  1049. *
  1050. * Handle transmission of any data padding in a PDU data segment.
  1051. * iscsi::tx_bhs will be valid when this is called.
  1052. */
  1053. static int iscsi_tx_data_padding ( struct iscsi_session *iscsi ) {
  1054. static const char pad[] = { '\0', '\0', '\0' };
  1055. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1056. size_t pad_len;
  1057. pad_len = ISCSI_DATA_PAD_LEN ( common->lengths );
  1058. if ( ! pad_len )
  1059. return 0;
  1060. return xfer_deliver_raw ( &iscsi->socket, pad, pad_len );
  1061. }
  1062. /**
  1063. * Complete iSCSI PDU transmission
  1064. *
  1065. * @v iscsi iSCSI session
  1066. *
  1067. * Called when a PDU has been completely transmitted and the TX state
  1068. * machine is about to enter the idle state. iscsi::tx_bhs will be
  1069. * valid for the just-completed PDU when this is called.
  1070. */
  1071. static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
  1072. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1073. switch ( common->opcode & ISCSI_OPCODE_MASK ) {
  1074. case ISCSI_OPCODE_DATA_OUT:
  1075. iscsi_data_out_done ( iscsi );
  1076. case ISCSI_OPCODE_LOGIN_REQUEST:
  1077. iscsi_login_request_done ( iscsi );
  1078. default:
  1079. /* No action */
  1080. break;
  1081. }
  1082. }
  1083. /**
  1084. * Transmit iSCSI PDU
  1085. *
  1086. * @v iscsi iSCSI session
  1087. * @v buf Temporary data buffer
  1088. * @v len Length of temporary data buffer
  1089. *
  1090. * Constructs data to be sent for the current TX state
  1091. */
  1092. static void iscsi_tx_step ( struct process *process ) {
  1093. struct iscsi_session *iscsi =
  1094. container_of ( process, struct iscsi_session, process );
  1095. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1096. int ( * tx ) ( struct iscsi_session *iscsi );
  1097. enum iscsi_tx_state next_state;
  1098. size_t tx_len;
  1099. int rc;
  1100. /* Select fragment to transmit */
  1101. while ( 1 ) {
  1102. switch ( iscsi->tx_state ) {
  1103. case ISCSI_TX_IDLE:
  1104. /* Stop processing */
  1105. return;
  1106. case ISCSI_TX_BHS:
  1107. tx = iscsi_tx_bhs;
  1108. tx_len = sizeof ( iscsi->tx_bhs );
  1109. next_state = ISCSI_TX_AHS;
  1110. break;
  1111. case ISCSI_TX_AHS:
  1112. tx = iscsi_tx_nothing;
  1113. tx_len = 0;
  1114. next_state = ISCSI_TX_DATA;
  1115. break;
  1116. case ISCSI_TX_DATA:
  1117. tx = iscsi_tx_data;
  1118. tx_len = ISCSI_DATA_LEN ( common->lengths );
  1119. next_state = ISCSI_TX_DATA_PADDING;
  1120. break;
  1121. case ISCSI_TX_DATA_PADDING:
  1122. tx = iscsi_tx_data_padding;
  1123. tx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
  1124. next_state = ISCSI_TX_IDLE;
  1125. break;
  1126. default:
  1127. assert ( 0 );
  1128. return;
  1129. }
  1130. /* Check for window availability, if needed */
  1131. if ( tx_len && ( xfer_window ( &iscsi->socket ) == 0 ) ) {
  1132. /* Cannot transmit at this point; stop processing */
  1133. return;
  1134. }
  1135. /* Transmit data */
  1136. if ( ( rc = tx ( iscsi ) ) != 0 ) {
  1137. DBGC ( iscsi, "iSCSI %p could not transmit: %s\n",
  1138. iscsi, strerror ( rc ) );
  1139. return;
  1140. }
  1141. /* Move to next state */
  1142. iscsi->tx_state = next_state;
  1143. if ( next_state == ISCSI_TX_IDLE )
  1144. iscsi_tx_done ( iscsi );
  1145. }
  1146. }
  1147. /**
  1148. * Receive basic header segment of an iSCSI PDU
  1149. *
  1150. * @v iscsi iSCSI session
  1151. * @v data Received data
  1152. * @v len Length of received data
  1153. * @v remaining Data remaining after this data
  1154. * @ret rc Return status code
  1155. *
  1156. * This fills in iscsi::rx_bhs with the data from the BHS portion of
  1157. * the received PDU.
  1158. */
  1159. static int iscsi_rx_bhs ( struct iscsi_session *iscsi, const void *data,
  1160. size_t len, size_t remaining __unused ) {
  1161. memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
  1162. if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
  1163. DBGC2 ( iscsi, "iSCSI %p received PDU opcode %#x len %#x\n",
  1164. iscsi, iscsi->rx_bhs.common.opcode,
  1165. ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
  1166. }
  1167. return 0;
  1168. }
  1169. /**
  1170. * Discard portion of an iSCSI PDU.
  1171. *
  1172. * @v iscsi iSCSI session
  1173. * @v data Received data
  1174. * @v len Length of received data
  1175. * @v remaining Data remaining after this data
  1176. * @ret rc Return status code
  1177. *
  1178. * This discards data from a portion of a received PDU.
  1179. */
  1180. static int iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
  1181. const void *data __unused, size_t len __unused,
  1182. size_t remaining __unused ) {
  1183. /* Do nothing */
  1184. return 0;
  1185. }
  1186. /**
  1187. * Receive data segment of an iSCSI PDU
  1188. *
  1189. * @v iscsi iSCSI session
  1190. * @v data Received data
  1191. * @v len Length of received data
  1192. * @v remaining Data remaining after this data
  1193. * @ret rc Return status code
  1194. *
  1195. * Handle processing of part of a PDU data segment. iscsi::rx_bhs
  1196. * will be valid when this is called.
  1197. */
  1198. static int iscsi_rx_data ( struct iscsi_session *iscsi, const void *data,
  1199. size_t len, size_t remaining ) {
  1200. struct iscsi_bhs_common_response *response
  1201. = &iscsi->rx_bhs.common_response;
  1202. /* Update cmdsn and statsn */
  1203. iscsi->cmdsn = ntohl ( response->expcmdsn );
  1204. iscsi->statsn = ntohl ( response->statsn );
  1205. switch ( response->opcode & ISCSI_OPCODE_MASK ) {
  1206. case ISCSI_OPCODE_LOGIN_RESPONSE:
  1207. return iscsi_rx_login_response ( iscsi, data, len, remaining );
  1208. case ISCSI_OPCODE_SCSI_RESPONSE:
  1209. return iscsi_rx_scsi_response ( iscsi, data, len, remaining );
  1210. case ISCSI_OPCODE_DATA_IN:
  1211. return iscsi_rx_data_in ( iscsi, data, len, remaining );
  1212. case ISCSI_OPCODE_R2T:
  1213. return iscsi_rx_r2t ( iscsi, data, len, remaining );
  1214. default:
  1215. if ( remaining )
  1216. return 0;
  1217. DBGC ( iscsi, "iSCSI %p unknown opcode %02x\n", iscsi,
  1218. response->opcode );
  1219. return -ENOTSUP;
  1220. }
  1221. }
  1222. /**
  1223. * Receive new data
  1224. *
  1225. * @v socket Transport layer interface
  1226. * @v data Received data
  1227. * @v len Length of received data
  1228. * @ret rc Return status code
  1229. *
  1230. * This handles received PDUs. The receive strategy is to fill in
  1231. * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
  1232. * throw away any AHS portion, and then process each part of the data
  1233. * portion as it arrives. The data processing routine therefore
  1234. * always has a full copy of the BHS available, even for portions of
  1235. * the data in different packets to the BHS.
  1236. */
  1237. static int iscsi_socket_deliver_raw ( struct xfer_interface *socket,
  1238. const void *data, size_t len ) {
  1239. struct iscsi_session *iscsi =
  1240. container_of ( socket, struct iscsi_session, socket );
  1241. struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
  1242. int ( * rx ) ( struct iscsi_session *iscsi, const void *data,
  1243. size_t len, size_t remaining );
  1244. enum iscsi_rx_state next_state;
  1245. size_t frag_len;
  1246. size_t remaining;
  1247. int rc;
  1248. while ( 1 ) {
  1249. switch ( iscsi->rx_state ) {
  1250. case ISCSI_RX_BHS:
  1251. rx = iscsi_rx_bhs;
  1252. iscsi->rx_len = sizeof ( iscsi->rx_bhs );
  1253. next_state = ISCSI_RX_AHS;
  1254. break;
  1255. case ISCSI_RX_AHS:
  1256. rx = iscsi_rx_discard;
  1257. iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
  1258. next_state = ISCSI_RX_DATA;
  1259. break;
  1260. case ISCSI_RX_DATA:
  1261. rx = iscsi_rx_data;
  1262. iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
  1263. next_state = ISCSI_RX_DATA_PADDING;
  1264. break;
  1265. case ISCSI_RX_DATA_PADDING:
  1266. rx = iscsi_rx_discard;
  1267. iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
  1268. next_state = ISCSI_RX_BHS;
  1269. break;
  1270. default:
  1271. assert ( 0 );
  1272. return -EINVAL;
  1273. }
  1274. frag_len = iscsi->rx_len - iscsi->rx_offset;
  1275. if ( frag_len > len )
  1276. frag_len = len;
  1277. remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
  1278. if ( ( rc = rx ( iscsi, data, frag_len, remaining ) ) != 0 ) {
  1279. DBGC ( iscsi, "iSCSI %p could not process received "
  1280. "data: %s\n", iscsi, strerror ( rc ) );
  1281. iscsi_close_connection ( iscsi, rc );
  1282. iscsi_scsi_done ( iscsi, rc );
  1283. return rc;
  1284. }
  1285. iscsi->rx_offset += frag_len;
  1286. data += frag_len;
  1287. len -= frag_len;
  1288. /* If all the data for this state has not yet been
  1289. * received, stay in this state for now.
  1290. */
  1291. if ( iscsi->rx_offset != iscsi->rx_len )
  1292. return 0;
  1293. iscsi->rx_state = next_state;
  1294. iscsi->rx_offset = 0;
  1295. }
  1296. return 0;
  1297. }
  1298. /**
  1299. * Handle stream connection closure
  1300. *
  1301. * @v socket Transport layer interface
  1302. * @v rc Reason for close
  1303. *
  1304. */
  1305. static void iscsi_socket_close ( struct xfer_interface *socket, int rc ) {
  1306. struct iscsi_session *iscsi =
  1307. container_of ( socket, struct iscsi_session, socket );
  1308. /* Even a graceful close counts as an error for iSCSI */
  1309. if ( ! rc )
  1310. rc = -ECONNRESET;
  1311. /* Close session cleanly */
  1312. iscsi_close_connection ( iscsi, rc );
  1313. /* Retry connection if within the retry limit, otherwise fail */
  1314. if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
  1315. DBGC ( iscsi, "iSCSI %p retrying connection (retry #%d)\n",
  1316. iscsi, iscsi->retry_count );
  1317. if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
  1318. DBGC ( iscsi, "iSCSI %p could not reconnect: %s\n",
  1319. iscsi, strerror ( rc ) );
  1320. iscsi_scsi_done ( iscsi, rc );
  1321. }
  1322. } else {
  1323. DBGC ( iscsi, "iSCSI %p retry count exceeded\n", iscsi );
  1324. iscsi->instant_rc = rc;
  1325. iscsi_scsi_done ( iscsi, rc );
  1326. }
  1327. }
  1328. /**
  1329. * Handle redirection event
  1330. *
  1331. * @v socket Transport layer interface
  1332. * @v type Location type
  1333. * @v args Remaining arguments depend upon location type
  1334. * @ret rc Return status code
  1335. */
  1336. static int iscsi_vredirect ( struct xfer_interface *socket, int type,
  1337. va_list args ) {
  1338. struct iscsi_session *iscsi =
  1339. container_of ( socket, struct iscsi_session, socket );
  1340. va_list tmp;
  1341. struct sockaddr *peer;
  1342. /* Intercept redirects to a LOCATION_SOCKET and record the IP
  1343. * address for the iBFT. This is a bit of a hack, but avoids
  1344. * inventing an ioctl()-style call to retrieve the socket
  1345. * address from a data-xfer interface.
  1346. */
  1347. if ( type == LOCATION_SOCKET ) {
  1348. va_copy ( tmp, args );
  1349. ( void ) va_arg ( tmp, int ); /* Discard "semantics" */
  1350. peer = va_arg ( tmp, struct sockaddr * );
  1351. memcpy ( &iscsi->target_sockaddr, peer,
  1352. sizeof ( iscsi->target_sockaddr ) );
  1353. va_end ( tmp );
  1354. }
  1355. return xfer_vreopen ( socket, type, args );
  1356. }
  1357. /** iSCSI socket operations */
  1358. static struct xfer_interface_operations iscsi_socket_operations = {
  1359. .close = iscsi_socket_close,
  1360. .vredirect = iscsi_vredirect,
  1361. .window = unlimited_xfer_window,
  1362. .alloc_iob = default_xfer_alloc_iob,
  1363. .deliver_iob = xfer_deliver_as_raw,
  1364. .deliver_raw = iscsi_socket_deliver_raw,
  1365. };
  1366. /****************************************************************************
  1367. *
  1368. * iSCSI command issuing
  1369. *
  1370. */
  1371. /**
  1372. * Issue SCSI command
  1373. *
  1374. * @v scsi SCSI device
  1375. * @v command SCSI command
  1376. * @ret rc Return status code
  1377. */
  1378. static int iscsi_command ( struct scsi_device *scsi,
  1379. struct scsi_command *command ) {
  1380. struct iscsi_session *iscsi =
  1381. container_of ( scsi->backend, struct iscsi_session, refcnt );
  1382. int rc;
  1383. /* Abort immediately if we have a recorded permanent failure */
  1384. if ( iscsi->instant_rc )
  1385. return iscsi->instant_rc;
  1386. /* Record SCSI command */
  1387. iscsi->command = command;
  1388. /* Issue command or open connection as appropriate */
  1389. if ( iscsi->status ) {
  1390. iscsi_start_command ( iscsi );
  1391. } else {
  1392. if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
  1393. iscsi->command = NULL;
  1394. return rc;
  1395. }
  1396. }
  1397. return 0;
  1398. }
  1399. /**
  1400. * Shut down iSCSI interface
  1401. *
  1402. * @v scsi SCSI device
  1403. */
  1404. void iscsi_detach ( struct scsi_device *scsi ) {
  1405. struct iscsi_session *iscsi =
  1406. container_of ( scsi->backend, struct iscsi_session, refcnt );
  1407. xfer_nullify ( &iscsi->socket );
  1408. iscsi_close_connection ( iscsi, 0 );
  1409. process_del ( &iscsi->process );
  1410. scsi->command = scsi_detached_command;
  1411. ref_put ( scsi->backend );
  1412. scsi->backend = NULL;
  1413. }
  1414. /****************************************************************************
  1415. *
  1416. * Instantiator
  1417. *
  1418. */
  1419. /** iSCSI root path components (as per RFC4173) */
  1420. enum iscsi_root_path_component {
  1421. RP_LITERAL = 0,
  1422. RP_SERVERNAME,
  1423. RP_PROTOCOL,
  1424. RP_PORT,
  1425. RP_LUN,
  1426. RP_TARGETNAME,
  1427. NUM_RP_COMPONENTS
  1428. };
  1429. /**
  1430. * Parse iSCSI root path
  1431. *
  1432. * @v iscsi iSCSI session
  1433. * @v root_path iSCSI root path (as per RFC4173)
  1434. * @ret rc Return status code
  1435. */
  1436. static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
  1437. const char *root_path ) {
  1438. char rp_copy[ strlen ( root_path ) + 1 ];
  1439. char *rp_comp[NUM_RP_COMPONENTS];
  1440. char *rp = rp_copy;
  1441. int i = 0;
  1442. int rc;
  1443. /* Split root path into component parts */
  1444. strcpy ( rp_copy, root_path );
  1445. while ( 1 ) {
  1446. rp_comp[i++] = rp;
  1447. if ( i == NUM_RP_COMPONENTS )
  1448. break;
  1449. for ( ; *rp != ':' ; rp++ ) {
  1450. if ( ! *rp ) {
  1451. DBGC ( iscsi, "iSCSI %p root path \"%s\" "
  1452. "too short\n", iscsi, root_path );
  1453. return -EINVAL;
  1454. }
  1455. }
  1456. *(rp++) = '\0';
  1457. }
  1458. /* Use root path components to configure iSCSI session */
  1459. iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
  1460. if ( ! iscsi->target_address )
  1461. return -ENOMEM;
  1462. iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
  1463. if ( ! iscsi->target_port )
  1464. iscsi->target_port = ISCSI_PORT;
  1465. if ( ( rc = scsi_parse_lun ( rp_comp[RP_LUN], &iscsi->lun ) ) != 0 ) {
  1466. DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
  1467. iscsi, rp_comp[RP_LUN] );
  1468. return rc;
  1469. }
  1470. iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
  1471. if ( ! iscsi->target_iqn )
  1472. return -ENOMEM;
  1473. return 0;
  1474. }
  1475. /**
  1476. * Set iSCSI authentication details
  1477. *
  1478. * @v iscsi iSCSI session
  1479. * @v initiator_username Initiator username, if any
  1480. * @v initiator_password Initiator password, if any
  1481. * @v target_username Target username, if any
  1482. * @v target_password Target password, if any
  1483. * @ret rc Return status code
  1484. */
  1485. static int iscsi_set_auth ( struct iscsi_session *iscsi,
  1486. const char *initiator_username,
  1487. const char *initiator_password,
  1488. const char *target_username,
  1489. const char *target_password ) {
  1490. /* Check for initiator or target credentials */
  1491. if ( initiator_username || initiator_password ||
  1492. target_username || target_password ) {
  1493. /* We must have at least an initiator username+password */
  1494. if ( ! ( initiator_username && initiator_password ) )
  1495. goto invalid_auth;
  1496. /* Store initiator credentials */
  1497. iscsi->initiator_username = strdup ( initiator_username );
  1498. if ( ! iscsi->initiator_username )
  1499. return -ENOMEM;
  1500. iscsi->initiator_password = strdup ( initiator_password );
  1501. if ( ! iscsi->initiator_password )
  1502. return -ENOMEM;
  1503. /* Check for target credentials */
  1504. if ( target_username || target_password ) {
  1505. /* We must have target username+password */
  1506. if ( ! ( target_username && target_password ) )
  1507. goto invalid_auth;
  1508. /* Store target credentials */
  1509. iscsi->target_username = strdup ( target_username );
  1510. if ( ! iscsi->target_username )
  1511. return -ENOMEM;
  1512. iscsi->target_password = strdup ( target_password );
  1513. if ( ! iscsi->target_password )
  1514. return -ENOMEM;
  1515. }
  1516. }
  1517. return 0;
  1518. invalid_auth:
  1519. DBGC ( iscsi, "iSCSI %p invalid credentials: initiator "
  1520. "%sname,%spw, target %sname,%spw\n", iscsi,
  1521. ( initiator_username ? "" : "no " ),
  1522. ( initiator_password ? "" : "no " ),
  1523. ( target_username ? "" : "no " ),
  1524. ( target_password ? "" : "no " ) );
  1525. return -EINVAL;
  1526. }
  1527. /**
  1528. * Attach iSCSI interface
  1529. *
  1530. * @v scsi SCSI device
  1531. * @v root_path iSCSI root path (as per RFC4173)
  1532. * @ret rc Return status code
  1533. */
  1534. int iscsi_attach ( struct scsi_device *scsi, const char *root_path ) {
  1535. struct iscsi_session *iscsi;
  1536. int rc;
  1537. /* Allocate and initialise structure */
  1538. iscsi = zalloc ( sizeof ( *iscsi ) );
  1539. if ( ! iscsi )
  1540. return -ENOMEM;
  1541. iscsi->refcnt.free = iscsi_free;
  1542. xfer_init ( &iscsi->socket, &iscsi_socket_operations, &iscsi->refcnt );
  1543. process_init ( &iscsi->process, iscsi_tx_step, &iscsi->refcnt );
  1544. /* Parse root path */
  1545. if ( ( rc = iscsi_parse_root_path ( iscsi, root_path ) ) != 0 )
  1546. goto err;
  1547. /* Set fields not specified by root path */
  1548. if ( ( rc = iscsi_set_auth ( iscsi,
  1549. iscsi_initiator_username,
  1550. iscsi_initiator_password,
  1551. iscsi_target_username,
  1552. iscsi_target_password ) ) != 0 )
  1553. goto err;
  1554. /* Sanity checks */
  1555. if ( ! iscsi->target_address ) {
  1556. DBGC ( iscsi, "iSCSI %p does not yet support discovery\n",
  1557. iscsi );
  1558. rc = -ENOTSUP;
  1559. goto err;
  1560. }
  1561. if ( ! iscsi->target_iqn ) {
  1562. DBGC ( iscsi, "iSCSI %p no target address supplied in %s\n",
  1563. iscsi, root_path );
  1564. rc = -EINVAL;
  1565. goto err;
  1566. }
  1567. /* Attach parent interface, mortalise self, and return */
  1568. scsi->backend = ref_get ( &iscsi->refcnt );
  1569. scsi->command = iscsi_command;
  1570. scsi->lun = iscsi->lun;
  1571. ref_put ( &iscsi->refcnt );
  1572. return 0;
  1573. err:
  1574. ref_put ( &iscsi->refcnt );
  1575. return rc;
  1576. }
  1577. /****************************************************************************
  1578. *
  1579. * Settings
  1580. *
  1581. */
  1582. /** iSCSI initiator IQN setting */
  1583. struct setting initiator_iqn_setting __setting = {
  1584. .name = "initiator-iqn",
  1585. .description = "iSCSI initiator name",
  1586. .tag = DHCP_ISCSI_INITIATOR_IQN,
  1587. .type = &setting_type_string,
  1588. };
  1589. /** iSCSI reverse username setting */
  1590. struct setting reverse_username_setting __setting = {
  1591. .name = "reverse-username",
  1592. .description = "Reverse user name",
  1593. .tag = DHCP_EB_REVERSE_USERNAME,
  1594. .type = &setting_type_string,
  1595. };
  1596. /** iSCSI reverse password setting */
  1597. struct setting reverse_password_setting __setting = {
  1598. .name = "reverse-password",
  1599. .description = "Reverse password",
  1600. .tag = DHCP_EB_REVERSE_PASSWORD,
  1601. .type = &setting_type_string,
  1602. };
  1603. /** An iSCSI string setting */
  1604. struct iscsi_string_setting {
  1605. /** Setting */
  1606. struct setting *setting;
  1607. /** String to update */
  1608. char **string;
  1609. /** String prefix */
  1610. const char *prefix;
  1611. };
  1612. /** iSCSI string settings */
  1613. static struct iscsi_string_setting iscsi_string_settings[] = {
  1614. {
  1615. .setting = &initiator_iqn_setting,
  1616. .string = &iscsi_explicit_initiator_iqn,
  1617. .prefix = "",
  1618. },
  1619. {
  1620. .setting = &username_setting,
  1621. .string = &iscsi_initiator_username,
  1622. .prefix = "",
  1623. },
  1624. {
  1625. .setting = &password_setting,
  1626. .string = &iscsi_initiator_password,
  1627. .prefix = "",
  1628. },
  1629. {
  1630. .setting = &reverse_username_setting,
  1631. .string = &iscsi_target_username,
  1632. .prefix = "",
  1633. },
  1634. {
  1635. .setting = &reverse_password_setting,
  1636. .string = &iscsi_target_password,
  1637. .prefix = "",
  1638. },
  1639. {
  1640. .setting = &hostname_setting,
  1641. .string = &iscsi_default_initiator_iqn,
  1642. .prefix = "iqn.2000-01.org.etherboot:",
  1643. },
  1644. };
  1645. /**
  1646. * Apply iSCSI setting
  1647. *
  1648. * @v setting iSCSI string setting
  1649. * @ret rc Return status code
  1650. */
  1651. static int apply_iscsi_string_setting ( struct iscsi_string_setting *setting ){
  1652. size_t prefix_len;
  1653. int setting_len;
  1654. size_t len;
  1655. int check_len;
  1656. char *p;
  1657. /* Free old string */
  1658. free ( *setting->string );
  1659. *setting->string = NULL;
  1660. /* Allocate new string */
  1661. prefix_len = strlen ( setting->prefix );
  1662. setting_len = fetch_setting_len ( NULL, setting->setting );
  1663. if ( setting_len < 0 ) {
  1664. /* Missing settings are not errors; leave strings as NULL */
  1665. return 0;
  1666. }
  1667. len = ( prefix_len + setting_len + 1 );
  1668. p = *setting->string = malloc ( len );
  1669. if ( ! p )
  1670. return -ENOMEM;
  1671. /* Fill new string */
  1672. strcpy ( p, setting->prefix );
  1673. check_len = fetch_string_setting ( NULL, setting->setting,
  1674. ( p + prefix_len ),
  1675. ( len - prefix_len ) );
  1676. assert ( check_len == setting_len );
  1677. return 0;
  1678. }
  1679. /**
  1680. * Apply iSCSI settings
  1681. *
  1682. * @ret rc Return status code
  1683. */
  1684. static int apply_iscsi_settings ( void ) {
  1685. struct iscsi_string_setting *setting;
  1686. unsigned int i;
  1687. int rc;
  1688. for ( i = 0 ; i < ( sizeof ( iscsi_string_settings ) /
  1689. sizeof ( iscsi_string_settings[0] ) ) ; i++ ) {
  1690. setting = &iscsi_string_settings[i];
  1691. if ( ( rc = apply_iscsi_string_setting ( setting ) ) != 0 ) {
  1692. DBG ( "iSCSI could not apply setting %s\n",
  1693. setting->setting->name );
  1694. return rc;
  1695. }
  1696. }
  1697. return 0;
  1698. }
  1699. /** iSCSI settings applicator */
  1700. struct settings_applicator iscsi_settings_applicator __settings_applicator = {
  1701. .apply = apply_iscsi_settings,
  1702. };
  1703. /****************************************************************************
  1704. *
  1705. * Initiator name
  1706. *
  1707. */
  1708. /**
  1709. * Get iSCSI initiator IQN
  1710. *
  1711. * @v iscsi iSCSI session
  1712. * @ret rc Return status code
  1713. */
  1714. const char * iscsi_initiator_iqn ( void ) {
  1715. if ( iscsi_explicit_initiator_iqn )
  1716. return iscsi_explicit_initiator_iqn;
  1717. if ( iscsi_default_initiator_iqn )
  1718. return iscsi_default_initiator_iqn;
  1719. return "iqn.2000-09.org.etherboot:UNKNOWN";
  1720. }