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 45KB

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