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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  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. ref_put ( &iscsi->refcnt );
  1571. return 0;
  1572. err:
  1573. ref_put ( &iscsi->refcnt );
  1574. return rc;
  1575. }
  1576. /****************************************************************************
  1577. *
  1578. * Settings
  1579. *
  1580. */
  1581. /** iSCSI initiator IQN setting */
  1582. struct setting initiator_iqn_setting __setting = {
  1583. .name = "initiator-iqn",
  1584. .description = "iSCSI initiator name",
  1585. .tag = DHCP_ISCSI_INITIATOR_IQN,
  1586. .type = &setting_type_string,
  1587. };
  1588. /** iSCSI reverse username setting */
  1589. struct setting reverse_username_setting __setting = {
  1590. .name = "reverse-username",
  1591. .description = "Reverse user name",
  1592. .tag = DHCP_EB_REVERSE_USERNAME,
  1593. .type = &setting_type_string,
  1594. };
  1595. /** iSCSI reverse password setting */
  1596. struct setting reverse_password_setting __setting = {
  1597. .name = "reverse-password",
  1598. .description = "Reverse password",
  1599. .tag = DHCP_EB_REVERSE_PASSWORD,
  1600. .type = &setting_type_string,
  1601. };
  1602. /** An iSCSI string setting */
  1603. struct iscsi_string_setting {
  1604. /** Setting */
  1605. struct setting *setting;
  1606. /** String to update */
  1607. char **string;
  1608. /** String prefix */
  1609. const char *prefix;
  1610. };
  1611. /** iSCSI string settings */
  1612. static struct iscsi_string_setting iscsi_string_settings[] = {
  1613. {
  1614. .setting = &initiator_iqn_setting,
  1615. .string = &iscsi_explicit_initiator_iqn,
  1616. .prefix = "",
  1617. },
  1618. {
  1619. .setting = &username_setting,
  1620. .string = &iscsi_initiator_username,
  1621. .prefix = "",
  1622. },
  1623. {
  1624. .setting = &password_setting,
  1625. .string = &iscsi_initiator_password,
  1626. .prefix = "",
  1627. },
  1628. {
  1629. .setting = &reverse_username_setting,
  1630. .string = &iscsi_target_username,
  1631. .prefix = "",
  1632. },
  1633. {
  1634. .setting = &reverse_password_setting,
  1635. .string = &iscsi_target_password,
  1636. .prefix = "",
  1637. },
  1638. {
  1639. .setting = &hostname_setting,
  1640. .string = &iscsi_default_initiator_iqn,
  1641. .prefix = "iqn.2000-01.org.etherboot:",
  1642. },
  1643. };
  1644. /**
  1645. * Apply iSCSI setting
  1646. *
  1647. * @v setting iSCSI string setting
  1648. * @ret rc Return status code
  1649. */
  1650. static int apply_iscsi_string_setting ( struct iscsi_string_setting *setting ){
  1651. size_t prefix_len;
  1652. int setting_len;
  1653. size_t len;
  1654. int check_len;
  1655. char *p;
  1656. /* Free old string */
  1657. free ( *setting->string );
  1658. *setting->string = NULL;
  1659. /* Allocate new string */
  1660. prefix_len = strlen ( setting->prefix );
  1661. setting_len = fetch_setting_len ( NULL, setting->setting );
  1662. if ( setting_len < 0 ) {
  1663. /* Missing settings are not errors; leave strings as NULL */
  1664. return 0;
  1665. }
  1666. len = ( prefix_len + setting_len + 1 );
  1667. p = *setting->string = malloc ( len );
  1668. if ( ! p )
  1669. return -ENOMEM;
  1670. /* Fill new string */
  1671. strcpy ( p, setting->prefix );
  1672. check_len = fetch_string_setting ( NULL, setting->setting,
  1673. ( p + prefix_len ),
  1674. ( len - prefix_len ) );
  1675. assert ( check_len == setting_len );
  1676. return 0;
  1677. }
  1678. /**
  1679. * Apply iSCSI settings
  1680. *
  1681. * @ret rc Return status code
  1682. */
  1683. static int apply_iscsi_settings ( void ) {
  1684. struct iscsi_string_setting *setting;
  1685. unsigned int i;
  1686. int rc;
  1687. for ( i = 0 ; i < ( sizeof ( iscsi_string_settings ) /
  1688. sizeof ( iscsi_string_settings[0] ) ) ; i++ ) {
  1689. setting = &iscsi_string_settings[i];
  1690. if ( ( rc = apply_iscsi_string_setting ( setting ) ) != 0 ) {
  1691. DBG ( "iSCSI could not apply setting %s\n",
  1692. setting->setting->name );
  1693. return rc;
  1694. }
  1695. }
  1696. return 0;
  1697. }
  1698. /** iSCSI settings applicator */
  1699. struct settings_applicator iscsi_settings_applicator __settings_applicator = {
  1700. .apply = apply_iscsi_settings,
  1701. };
  1702. /****************************************************************************
  1703. *
  1704. * Initiator name
  1705. *
  1706. */
  1707. /**
  1708. * Get iSCSI initiator IQN
  1709. *
  1710. * @v iscsi iSCSI session
  1711. * @ret rc Return status code
  1712. */
  1713. const char * iscsi_initiator_iqn ( void ) {
  1714. if ( iscsi_explicit_initiator_iqn )
  1715. return iscsi_explicit_initiator_iqn;
  1716. if ( iscsi_default_initiator_iqn )
  1717. return iscsi_default_initiator_iqn;
  1718. return "iqn.2000-09.org.etherboot:UNKNOWN";
  1719. }