Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

iscsi.c 52KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  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. #include <stddef.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <byteswap.h>
  25. #include <gpxe/vsprintf.h>
  26. #include <gpxe/socket.h>
  27. #include <gpxe/xfer.h>
  28. #include <gpxe/open.h>
  29. #include <gpxe/scsi.h>
  30. #include <gpxe/process.h>
  31. #include <gpxe/uaccess.h>
  32. #include <gpxe/tcpip.h>
  33. #include <gpxe/settings.h>
  34. #include <gpxe/features.h>
  35. #include <gpxe/iscsi.h>
  36. /** @file
  37. *
  38. * iSCSI protocol
  39. *
  40. */
  41. FEATURE ( FEATURE_PROTOCOL, "iSCSI", DHCP_EB_FEATURE_ISCSI, 1 );
  42. /** iSCSI initiator name (explicitly specified) */
  43. static char *iscsi_explicit_initiator_iqn;
  44. /** Default iSCSI initiator name (constructed from hostname) */
  45. static char *iscsi_default_initiator_iqn;
  46. /** iSCSI initiator username */
  47. static char *iscsi_initiator_username;
  48. /** iSCSI initiator password */
  49. static char *iscsi_initiator_password;
  50. /** iSCSI target username */
  51. static char *iscsi_target_username;
  52. /** iSCSI target password */
  53. static char *iscsi_target_password;
  54. static void iscsi_start_tx ( struct iscsi_session *iscsi );
  55. static void iscsi_start_login ( struct iscsi_session *iscsi );
  56. static void iscsi_start_data_out ( struct iscsi_session *iscsi,
  57. unsigned int datasn );
  58. /**
  59. * Finish receiving PDU data into buffer
  60. *
  61. * @v iscsi iSCSI session
  62. */
  63. static void iscsi_rx_buffered_data_done ( struct iscsi_session *iscsi ) {
  64. free ( iscsi->rx_buffer );
  65. iscsi->rx_buffer = NULL;
  66. }
  67. /**
  68. * Free iSCSI session
  69. *
  70. * @v refcnt Reference counter
  71. */
  72. static void iscsi_free ( struct refcnt *refcnt ) {
  73. struct iscsi_session *iscsi =
  74. container_of ( refcnt, struct iscsi_session, refcnt );
  75. free ( iscsi->target_address );
  76. free ( iscsi->target_iqn );
  77. free ( iscsi->initiator_username );
  78. free ( iscsi->initiator_password );
  79. free ( iscsi->target_username );
  80. free ( iscsi->target_password );
  81. chap_finish ( &iscsi->chap );
  82. iscsi_rx_buffered_data_done ( iscsi );
  83. free ( iscsi );
  84. }
  85. /**
  86. * Open iSCSI transport-layer connection
  87. *
  88. * @v iscsi iSCSI session
  89. * @ret rc Return status code
  90. */
  91. static int iscsi_open_connection ( struct iscsi_session *iscsi ) {
  92. struct sockaddr_tcpip target;
  93. int rc;
  94. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  95. assert ( iscsi->rx_state == ISCSI_RX_BHS );
  96. assert ( iscsi->rx_offset == 0 );
  97. /* Open socket */
  98. memset ( &target, 0, sizeof ( target ) );
  99. target.st_port = htons ( iscsi->target_port );
  100. if ( ( rc = xfer_open_named_socket ( &iscsi->socket, SOCK_STREAM,
  101. ( struct sockaddr * ) &target,
  102. iscsi->target_address,
  103. NULL ) ) != 0 ) {
  104. DBGC ( iscsi, "iSCSI %p could not open socket: %s\n",
  105. iscsi, strerror ( rc ) );
  106. return rc;
  107. }
  108. /* Enter security negotiation phase */
  109. iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
  110. ISCSI_STATUS_STRINGS_SECURITY );
  111. /* Assign fresh initiator task tag */
  112. iscsi->itt++;
  113. /* Initiate login */
  114. iscsi_start_login ( iscsi );
  115. return 0;
  116. }
  117. /**
  118. * Close iSCSI transport-layer connection
  119. *
  120. * @v iscsi iSCSI session
  121. * @v rc Reason for close
  122. *
  123. * Closes the transport-layer connection and resets the session state
  124. * ready to attempt a fresh login.
  125. */
  126. static void iscsi_close_connection ( struct iscsi_session *iscsi, int rc ) {
  127. /* Close all data transfer interfaces */
  128. xfer_close ( &iscsi->socket, rc );
  129. /* Clear connection status */
  130. iscsi->status = 0;
  131. /* Deauthenticate target */
  132. iscsi->target_auth_ok = 0;
  133. /* Reset TX and RX state machines */
  134. iscsi->tx_state = ISCSI_TX_IDLE;
  135. iscsi->rx_state = ISCSI_RX_BHS;
  136. iscsi->rx_offset = 0;
  137. /* Free any temporary dynamically allocated memory */
  138. chap_finish ( &iscsi->chap );
  139. iscsi_rx_buffered_data_done ( iscsi );
  140. }
  141. /**
  142. * Mark iSCSI SCSI operation as complete
  143. *
  144. * @v iscsi iSCSI session
  145. * @v rc Return status code
  146. *
  147. * Note that iscsi_scsi_done() will not close the connection, and must
  148. * therefore be called only when the internal state machines are in an
  149. * appropriate state, otherwise bad things may happen on the next call
  150. * to iscsi_issue(). The general rule is to call iscsi_scsi_done()
  151. * only at the end of receiving a PDU; at this point the TX and RX
  152. * engines should both be idle.
  153. */
  154. static void iscsi_scsi_done ( struct iscsi_session *iscsi, int rc ) {
  155. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  156. iscsi->command = NULL;
  157. iscsi->rc = rc;
  158. }
  159. /****************************************************************************
  160. *
  161. * iSCSI SCSI command issuing
  162. *
  163. */
  164. /**
  165. * Build iSCSI SCSI command BHS
  166. *
  167. * @v iscsi iSCSI session
  168. *
  169. * We don't currently support bidirectional commands (i.e. with both
  170. * Data-In and Data-Out segments); these would require providing code
  171. * to generate an AHS, and there doesn't seem to be any need for it at
  172. * the moment.
  173. */
  174. static void iscsi_start_command ( struct iscsi_session *iscsi ) {
  175. struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
  176. assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
  177. /* Construct BHS and initiate transmission */
  178. iscsi_start_tx ( iscsi );
  179. command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
  180. command->flags = ( ISCSI_FLAG_FINAL |
  181. ISCSI_COMMAND_ATTR_SIMPLE );
  182. if ( iscsi->command->data_in )
  183. command->flags |= ISCSI_COMMAND_FLAG_READ;
  184. if ( iscsi->command->data_out )
  185. command->flags |= ISCSI_COMMAND_FLAG_WRITE;
  186. /* lengths left as zero */
  187. command->lun = iscsi->lun;
  188. command->itt = htonl ( ++iscsi->itt );
  189. command->exp_len = htonl ( iscsi->command->data_in_len |
  190. iscsi->command->data_out_len );
  191. command->cmdsn = htonl ( iscsi->cmdsn );
  192. command->expstatsn = htonl ( iscsi->statsn + 1 );
  193. memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
  194. DBGC2 ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#zx\n",
  195. iscsi, SCSI_CDB_DATA ( command->cdb ),
  196. ( iscsi->command->data_in ? "in" : "out" ),
  197. ( iscsi->command->data_in ?
  198. iscsi->command->data_in_len :
  199. iscsi->command->data_out_len ) );
  200. }
  201. /**
  202. * Receive data segment of an iSCSI SCSI response PDU
  203. *
  204. * @v iscsi iSCSI session
  205. * @v data Received data
  206. * @v len Length of received data
  207. * @v remaining Data remaining after this data
  208. * @ret rc Return status code
  209. */
  210. static int iscsi_rx_scsi_response ( struct iscsi_session *iscsi,
  211. const void *data, size_t len,
  212. size_t remaining ) {
  213. struct iscsi_bhs_scsi_response *response
  214. = &iscsi->rx_bhs.scsi_response;
  215. int sense_offset;
  216. /* Capture the sense response code as it floats past, if present */
  217. sense_offset = ISCSI_SENSE_RESPONSE_CODE_OFFSET - iscsi->rx_offset;
  218. if ( ( sense_offset >= 0 ) && len ) {
  219. iscsi->command->sense_response =
  220. * ( ( char * ) data + sense_offset );
  221. }
  222. /* Wait for whole SCSI response to arrive */
  223. if ( remaining )
  224. return 0;
  225. /* Record SCSI status code */
  226. iscsi->command->status = response->status;
  227. /* Check for errors */
  228. if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
  229. return -EIO;
  230. /* Mark as completed */
  231. iscsi_scsi_done ( iscsi, 0 );
  232. return 0;
  233. }
  234. /**
  235. * Receive data segment of an iSCSI data-in PDU
  236. *
  237. * @v iscsi iSCSI session
  238. * @v data Received data
  239. * @v len Length of received data
  240. * @v remaining Data remaining after this data
  241. * @ret rc Return status code
  242. */
  243. static int iscsi_rx_data_in ( struct iscsi_session *iscsi,
  244. const void *data, size_t len,
  245. size_t remaining ) {
  246. struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
  247. unsigned long offset;
  248. /* Copy data to data-in buffer */
  249. offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
  250. assert ( iscsi->command != NULL );
  251. assert ( iscsi->command->data_in );
  252. assert ( ( offset + len ) <= iscsi->command->data_in_len );
  253. copy_to_user ( iscsi->command->data_in, offset, data, len );
  254. /* Wait for whole SCSI response to arrive */
  255. if ( remaining )
  256. return 0;
  257. /* Mark as completed if status is present */
  258. if ( data_in->flags & ISCSI_DATA_FLAG_STATUS ) {
  259. assert ( ( offset + len ) == iscsi->command->data_in_len );
  260. assert ( data_in->flags & ISCSI_FLAG_FINAL );
  261. iscsi->command->status = data_in->status;
  262. /* iSCSI cannot return an error status via a data-in */
  263. iscsi_scsi_done ( iscsi, 0 );
  264. }
  265. return 0;
  266. }
  267. /**
  268. * Receive data segment of an iSCSI R2T PDU
  269. *
  270. * @v iscsi iSCSI session
  271. * @v data Received data
  272. * @v len Length of received data
  273. * @v remaining Data remaining after this data
  274. * @ret rc Return status code
  275. */
  276. static int iscsi_rx_r2t ( struct iscsi_session *iscsi,
  277. const void *data __unused, size_t len __unused,
  278. size_t remaining __unused ) {
  279. struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
  280. /* Record transfer parameters and trigger first data-out */
  281. iscsi->ttt = ntohl ( r2t->ttt );
  282. iscsi->transfer_offset = ntohl ( r2t->offset );
  283. iscsi->transfer_len = ntohl ( r2t->len );
  284. iscsi_start_data_out ( iscsi, 0 );
  285. return 0;
  286. }
  287. /**
  288. * Build iSCSI data-out BHS
  289. *
  290. * @v iscsi iSCSI session
  291. * @v datasn Data sequence number within the transfer
  292. *
  293. */
  294. static void iscsi_start_data_out ( struct iscsi_session *iscsi,
  295. unsigned int datasn ) {
  296. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  297. unsigned long offset;
  298. unsigned long remaining;
  299. unsigned long len;
  300. /* We always send 512-byte Data-Out PDUs; this removes the
  301. * need to worry about the target's MaxRecvDataSegmentLength.
  302. */
  303. offset = datasn * 512;
  304. remaining = iscsi->transfer_len - offset;
  305. len = remaining;
  306. if ( len > 512 )
  307. len = 512;
  308. /* Construct BHS and initiate transmission */
  309. iscsi_start_tx ( iscsi );
  310. data_out->opcode = ISCSI_OPCODE_DATA_OUT;
  311. if ( len == remaining )
  312. data_out->flags = ( ISCSI_FLAG_FINAL );
  313. ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
  314. data_out->lun = iscsi->lun;
  315. data_out->itt = htonl ( iscsi->itt );
  316. data_out->ttt = htonl ( iscsi->ttt );
  317. data_out->expstatsn = htonl ( iscsi->statsn + 1 );
  318. data_out->datasn = htonl ( datasn );
  319. data_out->offset = htonl ( iscsi->transfer_offset + offset );
  320. DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
  321. iscsi, datasn, len );
  322. }
  323. /**
  324. * Complete iSCSI data-out PDU transmission
  325. *
  326. * @v iscsi iSCSI session
  327. *
  328. */
  329. static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
  330. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  331. /* If we haven't reached the end of the sequence, start
  332. * sending the next data-out PDU.
  333. */
  334. if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
  335. iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
  336. }
  337. /**
  338. * Send iSCSI data-out data segment
  339. *
  340. * @v iscsi iSCSI session
  341. * @ret rc Return status code
  342. */
  343. static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
  344. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  345. struct io_buffer *iobuf;
  346. unsigned long offset;
  347. size_t len;
  348. offset = ntohl ( data_out->offset );
  349. len = ISCSI_DATA_LEN ( data_out->lengths );
  350. assert ( iscsi->command != NULL );
  351. assert ( iscsi->command->data_out );
  352. assert ( ( offset + len ) <= iscsi->command->data_out_len );
  353. iobuf = xfer_alloc_iob ( &iscsi->socket, len );
  354. if ( ! iobuf )
  355. return -ENOMEM;
  356. copy_from_user ( iob_put ( iobuf, len ),
  357. iscsi->command->data_out, offset, len );
  358. return xfer_deliver_iob ( &iscsi->socket, iobuf );
  359. }
  360. /****************************************************************************
  361. *
  362. * iSCSI login
  363. *
  364. */
  365. /**
  366. * Build iSCSI login request strings
  367. *
  368. * @v iscsi iSCSI session
  369. *
  370. * These are the initial set of strings sent in the first login
  371. * request PDU. We want the following settings:
  372. *
  373. * HeaderDigest=None
  374. * DataDigest=None
  375. * MaxConnections is irrelevant; we make only one connection anyway
  376. * InitialR2T=Yes [1]
  377. * ImmediateData is irrelevant; we never send immediate data
  378. * MaxRecvDataSegmentLength=8192 (default; we don't care) [3]
  379. * MaxBurstLength=262144 (default; we don't care) [3]
  380. * FirstBurstLength=262144 (default; we don't care)
  381. * DefaultTime2Wait=0 [2]
  382. * DefaultTime2Retain=0 [2]
  383. * MaxOutstandingR2T=1
  384. * DataPDUInOrder=Yes
  385. * DataSequenceInOrder=Yes
  386. * ErrorRecoveryLevel=0
  387. *
  388. * [1] InitialR2T has an OR resolution function, so the target may
  389. * force us to use it. We therefore simplify our logic by always
  390. * using it.
  391. *
  392. * [2] These ensure that we can safely start a new task once we have
  393. * reconnected after a failure, without having to manually tidy up
  394. * after the old one.
  395. *
  396. * [3] We are quite happy to use the RFC-defined default values for
  397. * these parameters, but some targets (notably OpenSolaris)
  398. * incorrectly assume a default value of zero, so we explicitly
  399. * specify the default values.
  400. */
  401. static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
  402. void *data, size_t len ) {
  403. unsigned int used = 0;
  404. unsigned int i;
  405. const char *auth_method;
  406. if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
  407. /* Default to allowing no authentication */
  408. auth_method = "None";
  409. /* If we have a credential to supply, permit CHAP */
  410. if ( iscsi->initiator_username )
  411. auth_method = "CHAP,None";
  412. /* If we have a credential to check, force CHAP */
  413. if ( iscsi->target_username )
  414. auth_method = "CHAP";
  415. used += ssnprintf ( data + used, len - used,
  416. "InitiatorName=%s%c"
  417. "TargetName=%s%c"
  418. "SessionType=Normal%c"
  419. "AuthMethod=%s%c",
  420. iscsi_initiator_iqn(), 0,
  421. iscsi->target_iqn, 0, 0,
  422. auth_method, 0 );
  423. }
  424. if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
  425. used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
  426. }
  427. if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
  428. assert ( iscsi->initiator_username != NULL );
  429. used += ssnprintf ( data + used, len - used,
  430. "CHAP_N=%s%cCHAP_R=0x",
  431. iscsi->initiator_username, 0 );
  432. for ( i = 0 ; i < iscsi->chap.response_len ; i++ ) {
  433. used += ssnprintf ( data + used, len - used, "%02x",
  434. iscsi->chap.response[i] );
  435. }
  436. used += ssnprintf ( data + used, len - used, "%c", 0 );
  437. }
  438. if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_CHALLENGE ) ) {
  439. used += ssnprintf ( data + used, len - used,
  440. "CHAP_I=%d%cCHAP_C=0x",
  441. iscsi->chap_challenge[0], 0 );
  442. for ( i = 1 ; i < sizeof ( iscsi->chap_challenge ) ; i++ ) {
  443. used += ssnprintf ( data + used, len - used, "%02x",
  444. iscsi->chap_challenge[i] );
  445. }
  446. used += ssnprintf ( data + used, len - used, "%c", 0 );
  447. }
  448. if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
  449. used += ssnprintf ( data + used, len - used,
  450. "HeaderDigest=None%c"
  451. "DataDigest=None%c"
  452. "InitialR2T=Yes%c"
  453. "MaxRecvDataSegmentLength=8192%c"
  454. "MaxBurstLength=262144%c"
  455. "DefaultTime2Wait=0%c"
  456. "DefaultTime2Retain=0%c"
  457. "MaxOutstandingR2T=1%c"
  458. "DataPDUInOrder=Yes%c"
  459. "DataSequenceInOrder=Yes%c"
  460. "ErrorRecoveryLevel=0%c",
  461. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  462. }
  463. return used;
  464. }
  465. /**
  466. * Build iSCSI login request BHS
  467. *
  468. * @v iscsi iSCSI session
  469. */
  470. static void iscsi_start_login ( struct iscsi_session *iscsi ) {
  471. struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
  472. int len;
  473. /* Construct BHS and initiate transmission */
  474. iscsi_start_tx ( iscsi );
  475. request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
  476. ISCSI_FLAG_IMMEDIATE );
  477. request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
  478. ISCSI_LOGIN_FLAG_TRANSITION );
  479. /* version_max and version_min left as zero */
  480. len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
  481. ISCSI_SET_LENGTHS ( request->lengths, 0, len );
  482. request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
  483. IANA_EN_FEN_SYSTEMS );
  484. /* isid_iana_qual left as zero */
  485. request->tsih = htons ( iscsi->tsih );
  486. request->itt = htonl ( iscsi->itt );
  487. /* cid left as zero */
  488. request->cmdsn = htonl ( iscsi->cmdsn );
  489. request->expstatsn = htonl ( iscsi->statsn + 1 );
  490. }
  491. /**
  492. * Complete iSCSI login request PDU transmission
  493. *
  494. * @v iscsi iSCSI session
  495. *
  496. */
  497. static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
  498. /* Clear any "strings to send" flags */
  499. iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
  500. /* Free any dynamically allocated storage used for login */
  501. chap_finish ( &iscsi->chap );
  502. }
  503. /**
  504. * Transmit data segment of an iSCSI login request PDU
  505. *
  506. * @v iscsi iSCSI session
  507. * @ret rc Return status code
  508. *
  509. * For login requests, the data segment consists of the login strings.
  510. */
  511. static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
  512. struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
  513. struct io_buffer *iobuf;
  514. size_t len;
  515. len = ISCSI_DATA_LEN ( request->lengths );
  516. iobuf = xfer_alloc_iob ( &iscsi->socket, len );
  517. if ( ! iobuf )
  518. return -ENOMEM;
  519. iob_put ( iobuf, len );
  520. iscsi_build_login_request_strings ( iscsi, iobuf->data, len );
  521. return xfer_deliver_iob ( &iscsi->socket, iobuf );
  522. }
  523. /**
  524. * Handle iSCSI TargetAddress text value
  525. *
  526. * @v iscsi iSCSI session
  527. * @v value TargetAddress value
  528. * @ret rc Return status code
  529. */
  530. static int iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
  531. const char *value ) {
  532. char *separator;
  533. DBGC ( iscsi, "iSCSI %p will redirect to %s\n", iscsi, value );
  534. /* Replace target address */
  535. free ( iscsi->target_address );
  536. iscsi->target_address = strdup ( value );
  537. if ( ! iscsi->target_address )
  538. return -ENOMEM;
  539. /* Replace target port */
  540. iscsi->target_port = htons ( ISCSI_PORT );
  541. separator = strchr ( iscsi->target_address, ':' );
  542. if ( separator ) {
  543. *separator = '\0';
  544. iscsi->target_port = strtoul ( ( separator + 1 ), NULL, 0 );
  545. }
  546. return 0;
  547. }
  548. /**
  549. * Handle iSCSI AuthMethod text value
  550. *
  551. * @v iscsi iSCSI session
  552. * @v value AuthMethod value
  553. * @ret rc Return status code
  554. */
  555. static int iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
  556. const char *value ) {
  557. /* Mark target as authenticated if no authentication required */
  558. if ( ! iscsi->target_username )
  559. iscsi->target_auth_ok = 1;
  560. /* If server requests CHAP, send the CHAP_A string */
  561. if ( strcmp ( value, "CHAP" ) == 0 ) {
  562. DBGC ( iscsi, "iSCSI %p initiating CHAP authentication\n",
  563. iscsi );
  564. iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_ALGORITHM;
  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->target_auth_ok = 1;
  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. switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
  945. case ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION:
  946. iscsi->status =
  947. ( ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE |
  948. ISCSI_STATUS_STRINGS_OPERATIONAL );
  949. break;
  950. case ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE:
  951. iscsi->status = ISCSI_STATUS_FULL_FEATURE_PHASE;
  952. break;
  953. default:
  954. DBGC ( iscsi, "iSCSI %p got invalid response flags "
  955. "%02x\n", iscsi, response->flags );
  956. return -EIO;
  957. }
  958. }
  959. /* Send next login request PDU if we haven't reached the full
  960. * feature phase yet.
  961. */
  962. if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
  963. ISCSI_STATUS_FULL_FEATURE_PHASE ) {
  964. iscsi_start_login ( iscsi );
  965. return 0;
  966. }
  967. /* Check that target authentication was successful (if required) */
  968. if ( ! iscsi->target_auth_ok ) {
  969. DBGC ( iscsi, "iSCSI %p nefarious target tried to bypass "
  970. "authentication\n", iscsi );
  971. return -EPROTO;
  972. }
  973. /* Reset retry count */
  974. iscsi->retry_count = 0;
  975. /* Record TSIH for future reference */
  976. iscsi->tsih = ntohl ( response->tsih );
  977. /* Send the actual SCSI command */
  978. iscsi_start_command ( iscsi );
  979. return 0;
  980. }
  981. /****************************************************************************
  982. *
  983. * iSCSI to socket interface
  984. *
  985. */
  986. /**
  987. * Start up a new TX PDU
  988. *
  989. * @v iscsi iSCSI session
  990. *
  991. * This initiates the process of sending a new PDU. Only one PDU may
  992. * be in transit at any one time.
  993. */
  994. static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
  995. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  996. /* Initialise TX BHS */
  997. memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
  998. /* Flag TX engine to start transmitting */
  999. iscsi->tx_state = ISCSI_TX_BHS;
  1000. }
  1001. /**
  1002. * Transmit nothing
  1003. *
  1004. * @v iscsi iSCSI session
  1005. * @ret rc Return status code
  1006. */
  1007. static int iscsi_tx_nothing ( struct iscsi_session *iscsi __unused ) {
  1008. return 0;
  1009. }
  1010. /**
  1011. * Transmit basic header segment of an iSCSI PDU
  1012. *
  1013. * @v iscsi iSCSI session
  1014. * @ret rc Return status code
  1015. */
  1016. static int iscsi_tx_bhs ( struct iscsi_session *iscsi ) {
  1017. return xfer_deliver_raw ( &iscsi->socket, &iscsi->tx_bhs,
  1018. sizeof ( iscsi->tx_bhs ) );
  1019. }
  1020. /**
  1021. * Transmit data segment of an iSCSI PDU
  1022. *
  1023. * @v iscsi iSCSI session
  1024. * @ret rc Return status code
  1025. *
  1026. * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
  1027. * will be valid when this is called.
  1028. */
  1029. static int iscsi_tx_data ( struct iscsi_session *iscsi ) {
  1030. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1031. switch ( common->opcode & ISCSI_OPCODE_MASK ) {
  1032. case ISCSI_OPCODE_DATA_OUT:
  1033. return iscsi_tx_data_out ( iscsi );
  1034. case ISCSI_OPCODE_LOGIN_REQUEST:
  1035. return iscsi_tx_login_request ( iscsi );
  1036. default:
  1037. /* Nothing to send in other states */
  1038. return 0;
  1039. }
  1040. }
  1041. /**
  1042. * Transmit data padding of an iSCSI PDU
  1043. *
  1044. * @v iscsi iSCSI session
  1045. * @ret rc Return status code
  1046. *
  1047. * Handle transmission of any data padding in a PDU data segment.
  1048. * iscsi::tx_bhs will be valid when this is called.
  1049. */
  1050. static int iscsi_tx_data_padding ( struct iscsi_session *iscsi ) {
  1051. static const char pad[] = { '\0', '\0', '\0' };
  1052. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1053. size_t pad_len;
  1054. pad_len = ISCSI_DATA_PAD_LEN ( common->lengths );
  1055. if ( ! pad_len )
  1056. return 0;
  1057. return xfer_deliver_raw ( &iscsi->socket, pad, pad_len );
  1058. }
  1059. /**
  1060. * Complete iSCSI PDU transmission
  1061. *
  1062. * @v iscsi iSCSI session
  1063. *
  1064. * Called when a PDU has been completely transmitted and the TX state
  1065. * machine is about to enter the idle state. iscsi::tx_bhs will be
  1066. * valid for the just-completed PDU when this is called.
  1067. */
  1068. static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
  1069. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1070. switch ( common->opcode & ISCSI_OPCODE_MASK ) {
  1071. case ISCSI_OPCODE_DATA_OUT:
  1072. iscsi_data_out_done ( iscsi );
  1073. case ISCSI_OPCODE_LOGIN_REQUEST:
  1074. iscsi_login_request_done ( iscsi );
  1075. default:
  1076. /* No action */
  1077. break;
  1078. }
  1079. }
  1080. /**
  1081. * Transmit iSCSI PDU
  1082. *
  1083. * @v iscsi iSCSI session
  1084. * @v buf Temporary data buffer
  1085. * @v len Length of temporary data buffer
  1086. *
  1087. * Constructs data to be sent for the current TX state
  1088. */
  1089. static void iscsi_tx_step ( struct process *process ) {
  1090. struct iscsi_session *iscsi =
  1091. container_of ( process, struct iscsi_session, process );
  1092. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1093. int ( * tx ) ( struct iscsi_session *iscsi );
  1094. enum iscsi_tx_state next_state;
  1095. size_t tx_len;
  1096. int rc;
  1097. /* Select fragment to transmit */
  1098. while ( 1 ) {
  1099. switch ( iscsi->tx_state ) {
  1100. case ISCSI_TX_IDLE:
  1101. /* Stop processing */
  1102. return;
  1103. case ISCSI_TX_BHS:
  1104. tx = iscsi_tx_bhs;
  1105. tx_len = sizeof ( iscsi->tx_bhs );
  1106. next_state = ISCSI_TX_AHS;
  1107. break;
  1108. case ISCSI_TX_AHS:
  1109. tx = iscsi_tx_nothing;
  1110. tx_len = 0;
  1111. next_state = ISCSI_TX_DATA;
  1112. break;
  1113. case ISCSI_TX_DATA:
  1114. tx = iscsi_tx_data;
  1115. tx_len = ISCSI_DATA_LEN ( common->lengths );
  1116. next_state = ISCSI_TX_DATA_PADDING;
  1117. break;
  1118. case ISCSI_TX_DATA_PADDING:
  1119. tx = iscsi_tx_data_padding;
  1120. tx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
  1121. next_state = ISCSI_TX_IDLE;
  1122. break;
  1123. default:
  1124. assert ( 0 );
  1125. return;
  1126. }
  1127. /* Check for window availability, if needed */
  1128. if ( tx_len && ( xfer_window ( &iscsi->socket ) == 0 ) ) {
  1129. /* Cannot transmit at this point; stop processing */
  1130. return;
  1131. }
  1132. /* Transmit data */
  1133. if ( ( rc = tx ( iscsi ) ) != 0 ) {
  1134. DBGC ( iscsi, "iSCSI %p could not transmit: %s\n",
  1135. iscsi, strerror ( rc ) );
  1136. return;
  1137. }
  1138. /* Move to next state */
  1139. iscsi->tx_state = next_state;
  1140. if ( next_state == ISCSI_TX_IDLE )
  1141. iscsi_tx_done ( iscsi );
  1142. }
  1143. }
  1144. /**
  1145. * Receive basic header segment of an iSCSI PDU
  1146. *
  1147. * @v iscsi iSCSI session
  1148. * @v data Received data
  1149. * @v len Length of received data
  1150. * @v remaining Data remaining after this data
  1151. * @ret rc Return status code
  1152. *
  1153. * This fills in iscsi::rx_bhs with the data from the BHS portion of
  1154. * the received PDU.
  1155. */
  1156. static int iscsi_rx_bhs ( struct iscsi_session *iscsi, const void *data,
  1157. size_t len, size_t remaining __unused ) {
  1158. memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
  1159. if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
  1160. DBGC2 ( iscsi, "iSCSI %p received PDU opcode %#x len %#lx\n",
  1161. iscsi, iscsi->rx_bhs.common.opcode,
  1162. ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
  1163. }
  1164. return 0;
  1165. }
  1166. /**
  1167. * Discard portion of an iSCSI PDU.
  1168. *
  1169. * @v iscsi iSCSI session
  1170. * @v data Received data
  1171. * @v len Length of received data
  1172. * @v remaining Data remaining after this data
  1173. * @ret rc Return status code
  1174. *
  1175. * This discards data from a portion of a received PDU.
  1176. */
  1177. static int iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
  1178. const void *data __unused, size_t len __unused,
  1179. size_t remaining __unused ) {
  1180. /* Do nothing */
  1181. return 0;
  1182. }
  1183. /**
  1184. * Receive data segment of an iSCSI PDU
  1185. *
  1186. * @v iscsi iSCSI session
  1187. * @v data Received data
  1188. * @v len Length of received data
  1189. * @v remaining Data remaining after this data
  1190. * @ret rc Return status code
  1191. *
  1192. * Handle processing of part of a PDU data segment. iscsi::rx_bhs
  1193. * will be valid when this is called.
  1194. */
  1195. static int iscsi_rx_data ( struct iscsi_session *iscsi, const void *data,
  1196. size_t len, size_t remaining ) {
  1197. struct iscsi_bhs_common_response *response
  1198. = &iscsi->rx_bhs.common_response;
  1199. /* Update cmdsn and statsn */
  1200. iscsi->cmdsn = ntohl ( response->expcmdsn );
  1201. iscsi->statsn = ntohl ( response->statsn );
  1202. switch ( response->opcode & ISCSI_OPCODE_MASK ) {
  1203. case ISCSI_OPCODE_LOGIN_RESPONSE:
  1204. return iscsi_rx_login_response ( iscsi, data, len, remaining );
  1205. case ISCSI_OPCODE_SCSI_RESPONSE:
  1206. return iscsi_rx_scsi_response ( iscsi, data, len, remaining );
  1207. case ISCSI_OPCODE_DATA_IN:
  1208. return iscsi_rx_data_in ( iscsi, data, len, remaining );
  1209. case ISCSI_OPCODE_R2T:
  1210. return iscsi_rx_r2t ( iscsi, data, len, remaining );
  1211. default:
  1212. if ( remaining )
  1213. return 0;
  1214. DBGC ( iscsi, "iSCSI %p unknown opcode %02x\n", iscsi,
  1215. response->opcode );
  1216. return -ENOTSUP;
  1217. }
  1218. }
  1219. /**
  1220. * Receive new data
  1221. *
  1222. * @v socket Transport layer interface
  1223. * @v data Received data
  1224. * @v len Length of received data
  1225. * @ret rc Return status code
  1226. *
  1227. * This handles received PDUs. The receive strategy is to fill in
  1228. * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
  1229. * throw away any AHS portion, and then process each part of the data
  1230. * portion as it arrives. The data processing routine therefore
  1231. * always has a full copy of the BHS available, even for portions of
  1232. * the data in different packets to the BHS.
  1233. */
  1234. static int iscsi_socket_deliver_raw ( struct xfer_interface *socket,
  1235. const void *data, size_t len ) {
  1236. struct iscsi_session *iscsi =
  1237. container_of ( socket, struct iscsi_session, socket );
  1238. struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
  1239. int ( * rx ) ( struct iscsi_session *iscsi, const void *data,
  1240. size_t len, size_t remaining );
  1241. enum iscsi_rx_state next_state;
  1242. size_t frag_len;
  1243. size_t remaining;
  1244. int rc;
  1245. while ( 1 ) {
  1246. switch ( iscsi->rx_state ) {
  1247. case ISCSI_RX_BHS:
  1248. rx = iscsi_rx_bhs;
  1249. iscsi->rx_len = sizeof ( iscsi->rx_bhs );
  1250. next_state = ISCSI_RX_AHS;
  1251. break;
  1252. case ISCSI_RX_AHS:
  1253. rx = iscsi_rx_discard;
  1254. iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
  1255. next_state = ISCSI_RX_DATA;
  1256. break;
  1257. case ISCSI_RX_DATA:
  1258. rx = iscsi_rx_data;
  1259. iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
  1260. next_state = ISCSI_RX_DATA_PADDING;
  1261. break;
  1262. case ISCSI_RX_DATA_PADDING:
  1263. rx = iscsi_rx_discard;
  1264. iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
  1265. next_state = ISCSI_RX_BHS;
  1266. break;
  1267. default:
  1268. assert ( 0 );
  1269. return -EINVAL;
  1270. }
  1271. frag_len = iscsi->rx_len - iscsi->rx_offset;
  1272. if ( frag_len > len )
  1273. frag_len = len;
  1274. remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
  1275. if ( ( rc = rx ( iscsi, data, frag_len, remaining ) ) != 0 ) {
  1276. DBGC ( iscsi, "iSCSI %p could not process received "
  1277. "data: %s\n", iscsi, strerror ( rc ) );
  1278. iscsi_close_connection ( iscsi, rc );
  1279. iscsi_scsi_done ( iscsi, rc );
  1280. return rc;
  1281. }
  1282. iscsi->rx_offset += frag_len;
  1283. data += frag_len;
  1284. len -= frag_len;
  1285. /* If all the data for this state has not yet been
  1286. * received, stay in this state for now.
  1287. */
  1288. if ( iscsi->rx_offset != iscsi->rx_len )
  1289. return 0;
  1290. iscsi->rx_state = next_state;
  1291. iscsi->rx_offset = 0;
  1292. }
  1293. return 0;
  1294. }
  1295. /**
  1296. * Handle stream connection closure
  1297. *
  1298. * @v socket Transport layer interface
  1299. * @v rc Reason for close
  1300. *
  1301. */
  1302. static void iscsi_socket_close ( struct xfer_interface *socket, int rc ) {
  1303. struct iscsi_session *iscsi =
  1304. container_of ( socket, struct iscsi_session, socket );
  1305. /* Even a graceful close counts as an error for iSCSI */
  1306. if ( ! rc )
  1307. rc = -ECONNRESET;
  1308. /* Close session cleanly */
  1309. iscsi_close_connection ( iscsi, rc );
  1310. /* Retry connection if within the retry limit, otherwise fail */
  1311. if ( ++iscsi->retry_count <= ISCSI_MAX_RETRIES ) {
  1312. DBGC ( iscsi, "iSCSI %p retrying connection (retry #%d)\n",
  1313. iscsi, iscsi->retry_count );
  1314. if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
  1315. DBGC ( iscsi, "iSCSI %p could not reconnect: %s\n",
  1316. iscsi, strerror ( rc ) );
  1317. iscsi_scsi_done ( iscsi, rc );
  1318. }
  1319. } else {
  1320. DBGC ( iscsi, "iSCSI %p retry count exceeded\n", iscsi );
  1321. iscsi->instant_rc = rc;
  1322. iscsi_scsi_done ( iscsi, rc );
  1323. }
  1324. }
  1325. /**
  1326. * Handle redirection event
  1327. *
  1328. * @v socket Transport layer interface
  1329. * @v type Location type
  1330. * @v args Remaining arguments depend upon location type
  1331. * @ret rc Return status code
  1332. */
  1333. static int iscsi_vredirect ( struct xfer_interface *socket, int type,
  1334. va_list args ) {
  1335. struct iscsi_session *iscsi =
  1336. container_of ( socket, struct iscsi_session, socket );
  1337. va_list tmp;
  1338. struct sockaddr *peer;
  1339. /* Intercept redirects to a LOCATION_SOCKET and record the IP
  1340. * address for the iBFT. This is a bit of a hack, but avoids
  1341. * inventing an ioctl()-style call to retrieve the socket
  1342. * address from a data-xfer interface.
  1343. */
  1344. if ( type == LOCATION_SOCKET ) {
  1345. va_copy ( tmp, args );
  1346. ( void ) va_arg ( tmp, int ); /* Discard "semantics" */
  1347. peer = va_arg ( tmp, struct sockaddr * );
  1348. memcpy ( &iscsi->target_sockaddr, peer,
  1349. sizeof ( iscsi->target_sockaddr ) );
  1350. va_end ( tmp );
  1351. }
  1352. return xfer_vopen ( socket, type, args );
  1353. }
  1354. /** iSCSI socket operations */
  1355. static struct xfer_interface_operations iscsi_socket_operations = {
  1356. .close = iscsi_socket_close,
  1357. .vredirect = iscsi_vredirect,
  1358. .window = unlimited_xfer_window,
  1359. .alloc_iob = default_xfer_alloc_iob,
  1360. .deliver_iob = xfer_deliver_as_raw,
  1361. .deliver_raw = iscsi_socket_deliver_raw,
  1362. };
  1363. /****************************************************************************
  1364. *
  1365. * iSCSI command issuing
  1366. *
  1367. */
  1368. /**
  1369. * Issue SCSI command
  1370. *
  1371. * @v scsi SCSI device
  1372. * @v command SCSI command
  1373. * @ret rc Return status code
  1374. */
  1375. static int iscsi_command ( struct scsi_device *scsi,
  1376. struct scsi_command *command ) {
  1377. struct iscsi_session *iscsi =
  1378. container_of ( scsi->backend, struct iscsi_session, refcnt );
  1379. int rc;
  1380. /* Record SCSI command */
  1381. iscsi->command = command;
  1382. /* Abort immediately if we have a recorded permanent failure */
  1383. if ( iscsi->instant_rc ) {
  1384. rc = iscsi->instant_rc;
  1385. goto done;
  1386. }
  1387. /* Issue command or open connection as appropriate */
  1388. if ( iscsi->status ) {
  1389. iscsi_start_command ( iscsi );
  1390. } else {
  1391. if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 )
  1392. goto done;
  1393. }
  1394. /* Wait for command to complete */
  1395. iscsi->rc = -EINPROGRESS;
  1396. while ( iscsi->rc == -EINPROGRESS )
  1397. step();
  1398. rc = iscsi->rc;
  1399. done:
  1400. iscsi->command = NULL;
  1401. return rc;
  1402. }
  1403. static int iscsi_detached_command ( struct scsi_device *scsi __unused,
  1404. struct scsi_command *command __unused ) {
  1405. return -ENODEV;
  1406. }
  1407. /**
  1408. * Shut down iSCSI interface
  1409. *
  1410. * @v scsi SCSI device
  1411. */
  1412. void iscsi_detach ( struct scsi_device *scsi ) {
  1413. struct iscsi_session *iscsi =
  1414. container_of ( scsi->backend, struct iscsi_session, refcnt );
  1415. xfer_nullify ( &iscsi->socket );
  1416. iscsi_close_connection ( iscsi, 0 );
  1417. process_del ( &iscsi->process );
  1418. scsi->command = iscsi_detached_command;
  1419. ref_put ( scsi->backend );
  1420. scsi->backend = NULL;
  1421. }
  1422. /****************************************************************************
  1423. *
  1424. * Instantiator
  1425. *
  1426. */
  1427. /** iSCSI root path components (as per RFC4173) */
  1428. enum iscsi_root_path_component {
  1429. RP_LITERAL = 0,
  1430. RP_SERVERNAME,
  1431. RP_PROTOCOL,
  1432. RP_PORT,
  1433. RP_LUN,
  1434. RP_TARGETNAME,
  1435. NUM_RP_COMPONENTS
  1436. };
  1437. /**
  1438. * Parse iSCSI LUN
  1439. *
  1440. * @v iscsi iSCSI session
  1441. * @v lun_string LUN string representation (as per RFC4173)
  1442. * @ret rc Return status code
  1443. */
  1444. static int iscsi_parse_lun ( struct iscsi_session *iscsi,
  1445. const char *lun_string ) {
  1446. union {
  1447. uint64_t u64;
  1448. uint16_t u16[4];
  1449. } lun;
  1450. char *p;
  1451. int i;
  1452. memset ( &lun, 0, sizeof ( lun ) );
  1453. if ( lun_string ) {
  1454. p = ( char * ) lun_string;
  1455. for ( i = 0 ; i < 4 ; i++ ) {
  1456. lun.u16[i] = htons ( strtoul ( p, &p, 16 ) );
  1457. if ( *p == '\0' )
  1458. break;
  1459. if ( *p != '-' )
  1460. return -EINVAL;
  1461. p++;
  1462. }
  1463. if ( *p )
  1464. return -EINVAL;
  1465. }
  1466. iscsi->lun = lun.u64;
  1467. return 0;
  1468. }
  1469. /**
  1470. * Parse iSCSI root path
  1471. *
  1472. * @v iscsi iSCSI session
  1473. * @v root_path iSCSI root path (as per RFC4173)
  1474. * @ret rc Return status code
  1475. */
  1476. static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
  1477. const char *root_path ) {
  1478. char rp_copy[ strlen ( root_path ) + 1 ];
  1479. char *rp_comp[NUM_RP_COMPONENTS];
  1480. char *rp = rp_copy;
  1481. int i = 0;
  1482. int rc;
  1483. /* Split root path into component parts */
  1484. strcpy ( rp_copy, root_path );
  1485. while ( 1 ) {
  1486. rp_comp[i++] = rp;
  1487. if ( i == NUM_RP_COMPONENTS )
  1488. break;
  1489. for ( ; *rp != ':' ; rp++ ) {
  1490. if ( ! *rp ) {
  1491. DBGC ( iscsi, "iSCSI %p root path \"%s\" "
  1492. "too short\n", iscsi, root_path );
  1493. return -EINVAL;
  1494. }
  1495. }
  1496. *(rp++) = '\0';
  1497. }
  1498. /* Use root path components to configure iSCSI session */
  1499. iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
  1500. if ( ! iscsi->target_address )
  1501. return -ENOMEM;
  1502. iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
  1503. if ( ! iscsi->target_port )
  1504. iscsi->target_port = ISCSI_PORT;
  1505. if ( ( rc = iscsi_parse_lun ( iscsi, rp_comp[RP_LUN] ) ) != 0 ) {
  1506. DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
  1507. iscsi, rp_comp[RP_LUN] );
  1508. return rc;
  1509. }
  1510. iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
  1511. if ( ! iscsi->target_iqn )
  1512. return -ENOMEM;
  1513. return 0;
  1514. }
  1515. /**
  1516. * Set iSCSI authentication details
  1517. *
  1518. * @v iscsi iSCSI session
  1519. * @v initiator_username Initiator username, if any
  1520. * @v initiator_password Initiator password, if any
  1521. * @v target_username Target username, if any
  1522. * @v target_password Target password, if any
  1523. * @ret rc Return status code
  1524. */
  1525. static int iscsi_set_auth ( struct iscsi_session *iscsi,
  1526. const char *initiator_username,
  1527. const char *initiator_password,
  1528. const char *target_username,
  1529. const char *target_password ) {
  1530. /* Check for initiator or target credentials */
  1531. if ( initiator_username || initiator_password ||
  1532. target_username || target_password ) {
  1533. /* We must have at least an initiator username+password */
  1534. if ( ! ( initiator_username && initiator_password ) )
  1535. goto invalid_auth;
  1536. /* Store initiator credentials */
  1537. iscsi->initiator_username = strdup ( initiator_username );
  1538. if ( ! iscsi->initiator_username )
  1539. return -ENOMEM;
  1540. iscsi->initiator_password = strdup ( initiator_password );
  1541. if ( ! iscsi->initiator_password )
  1542. return -ENOMEM;
  1543. /* Check for target credentials */
  1544. if ( target_username || target_password ) {
  1545. /* We must have target username+password */
  1546. if ( ! ( target_username && target_password ) )
  1547. goto invalid_auth;
  1548. /* Store target credentials */
  1549. iscsi->target_username = strdup ( target_username );
  1550. if ( ! iscsi->target_username )
  1551. return -ENOMEM;
  1552. iscsi->target_password = strdup ( target_password );
  1553. if ( ! iscsi->target_password )
  1554. return -ENOMEM;
  1555. }
  1556. }
  1557. return 0;
  1558. invalid_auth:
  1559. DBGC ( iscsi, "iSCSI %p invalid credentials: initiator "
  1560. "%sname,%spw, target %sname,%spw\n", iscsi,
  1561. ( initiator_username ? "" : "no " ),
  1562. ( initiator_password ? "" : "no " ),
  1563. ( target_username ? "" : "no " ),
  1564. ( target_password ? "" : "no " ) );
  1565. return -EINVAL;
  1566. }
  1567. /**
  1568. * Attach iSCSI interface
  1569. *
  1570. * @v scsi SCSI device
  1571. * @v root_path iSCSI root path (as per RFC4173)
  1572. * @ret rc Return status code
  1573. */
  1574. int iscsi_attach ( struct scsi_device *scsi, const char *root_path ) {
  1575. struct iscsi_session *iscsi;
  1576. int rc;
  1577. /* Allocate and initialise structure */
  1578. iscsi = zalloc ( sizeof ( *iscsi ) );
  1579. if ( ! iscsi )
  1580. return -ENOMEM;
  1581. iscsi->refcnt.free = iscsi_free;
  1582. xfer_init ( &iscsi->socket, &iscsi_socket_operations, &iscsi->refcnt );
  1583. process_init ( &iscsi->process, iscsi_tx_step, &iscsi->refcnt );
  1584. /* Parse root path */
  1585. if ( ( rc = iscsi_parse_root_path ( iscsi, root_path ) ) != 0 )
  1586. goto err;
  1587. /* Set fields not specified by root path */
  1588. if ( ( rc = iscsi_set_auth ( iscsi,
  1589. iscsi_initiator_username,
  1590. iscsi_initiator_password,
  1591. iscsi_target_username,
  1592. iscsi_target_password ) ) != 0 )
  1593. goto err;
  1594. /* Sanity checks */
  1595. if ( ! iscsi->target_address ) {
  1596. DBGC ( iscsi, "iSCSI %p does not yet support discovery\n",
  1597. iscsi );
  1598. rc = -ENOTSUP;
  1599. goto err;
  1600. }
  1601. if ( ! iscsi->target_iqn ) {
  1602. DBGC ( iscsi, "iSCSI %p no target address supplied in %s\n",
  1603. iscsi, root_path );
  1604. rc = -EINVAL;
  1605. goto err;
  1606. }
  1607. /* Attach parent interface, mortalise self, and return */
  1608. scsi->backend = ref_get ( &iscsi->refcnt );
  1609. scsi->command = iscsi_command;
  1610. scsi->lun = iscsi->lun;
  1611. ref_put ( &iscsi->refcnt );
  1612. return 0;
  1613. err:
  1614. ref_put ( &iscsi->refcnt );
  1615. return rc;
  1616. }
  1617. /****************************************************************************
  1618. *
  1619. * Settings
  1620. *
  1621. */
  1622. /** iSCSI initiator IQN setting */
  1623. struct setting initiator_iqn_setting __setting = {
  1624. .name = "initiator-iqn",
  1625. .description = "iSCSI initiator name",
  1626. .tag = DHCP_ISCSI_INITIATOR_IQN,
  1627. .type = &setting_type_string,
  1628. };
  1629. /** iSCSI reverse username setting */
  1630. struct setting reverse_username_setting __setting = {
  1631. .name = "reverse-username",
  1632. .description = "Reverse user name",
  1633. .tag = DHCP_EB_REVERSE_USERNAME,
  1634. .type = &setting_type_string,
  1635. };
  1636. /** iSCSI reverse password setting */
  1637. struct setting reverse_password_setting __setting = {
  1638. .name = "reverse-password",
  1639. .description = "Reverse password",
  1640. .tag = DHCP_EB_REVERSE_PASSWORD,
  1641. .type = &setting_type_string,
  1642. };
  1643. /** An iSCSI string setting */
  1644. struct iscsi_string_setting {
  1645. /** Setting */
  1646. struct setting *setting;
  1647. /** String to update */
  1648. char **string;
  1649. /** String prefix */
  1650. const char *prefix;
  1651. };
  1652. /** iSCSI string settings */
  1653. static struct iscsi_string_setting iscsi_string_settings[] = {
  1654. {
  1655. .setting = &initiator_iqn_setting,
  1656. .string = &iscsi_explicit_initiator_iqn,
  1657. .prefix = "",
  1658. },
  1659. {
  1660. .setting = &username_setting,
  1661. .string = &iscsi_initiator_username,
  1662. .prefix = "",
  1663. },
  1664. {
  1665. .setting = &password_setting,
  1666. .string = &iscsi_initiator_password,
  1667. .prefix = "",
  1668. },
  1669. {
  1670. .setting = &reverse_username_setting,
  1671. .string = &iscsi_target_username,
  1672. .prefix = "",
  1673. },
  1674. {
  1675. .setting = &reverse_password_setting,
  1676. .string = &iscsi_target_password,
  1677. .prefix = "",
  1678. },
  1679. {
  1680. .setting = &hostname_setting,
  1681. .string = &iscsi_default_initiator_iqn,
  1682. .prefix = "iqn.2000-01.org.etherboot:",
  1683. },
  1684. };
  1685. /**
  1686. * Apply iSCSI setting
  1687. *
  1688. * @v setting iSCSI string setting
  1689. * @ret rc Return status code
  1690. */
  1691. static int apply_iscsi_string_setting ( struct iscsi_string_setting *setting ){
  1692. size_t prefix_len;
  1693. int setting_len;
  1694. size_t len;
  1695. int check_len;
  1696. char *p;
  1697. /* Free old string */
  1698. free ( *setting->string );
  1699. *setting->string = NULL;
  1700. /* Allocate new string */
  1701. prefix_len = strlen ( setting->prefix );
  1702. setting_len = fetch_setting_len ( NULL, setting->setting );
  1703. if ( setting_len < 0 ) {
  1704. /* Missing settings are not errors; leave strings as NULL */
  1705. return 0;
  1706. }
  1707. len = ( prefix_len + setting_len + 1 );
  1708. p = *setting->string = malloc ( len );
  1709. if ( ! p )
  1710. return -ENOMEM;
  1711. /* Fill new string */
  1712. strcpy ( p, setting->prefix );
  1713. check_len = fetch_string_setting ( NULL, setting->setting,
  1714. ( p + prefix_len ),
  1715. ( len - prefix_len ) );
  1716. assert ( check_len == setting_len );
  1717. return 0;
  1718. }
  1719. /**
  1720. * Apply iSCSI settings
  1721. *
  1722. * @ret rc Return status code
  1723. */
  1724. static int apply_iscsi_settings ( void ) {
  1725. struct iscsi_string_setting *setting;
  1726. unsigned int i;
  1727. int rc;
  1728. for ( i = 0 ; i < ( sizeof ( iscsi_string_settings ) /
  1729. sizeof ( iscsi_string_settings[0] ) ) ; i++ ) {
  1730. setting = &iscsi_string_settings[i];
  1731. if ( ( rc = apply_iscsi_string_setting ( setting ) ) != 0 ) {
  1732. DBG ( "iSCSI could not apply setting %s\n",
  1733. setting->setting->name );
  1734. return rc;
  1735. }
  1736. }
  1737. return 0;
  1738. }
  1739. /** iSCSI settings applicator */
  1740. struct settings_applicator iscsi_settings_applicator __settings_applicator = {
  1741. .apply = apply_iscsi_settings,
  1742. };
  1743. /****************************************************************************
  1744. *
  1745. * Initiator name
  1746. *
  1747. */
  1748. /**
  1749. * Get iSCSI initiator IQN
  1750. *
  1751. * @v iscsi iSCSI session
  1752. * @ret rc Return status code
  1753. */
  1754. const char * iscsi_initiator_iqn ( void ) {
  1755. if ( iscsi_explicit_initiator_iqn )
  1756. return iscsi_explicit_initiator_iqn;
  1757. if ( iscsi_default_initiator_iqn )
  1758. return iscsi_default_initiator_iqn;
  1759. return "iqn.2000-09.org.etherboot:UNKNOWN";
  1760. }