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

iscsi.c 44KB

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