iscsi.c 43KB

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