Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

iscsi.c 45KB

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