iscsi.c 53KB

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