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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stddef.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <assert.h>
  31. #include <byteswap.h>
  32. #include <ipxe/vsprintf.h>
  33. #include <ipxe/socket.h>
  34. #include <ipxe/iobuf.h>
  35. #include <ipxe/uri.h>
  36. #include <ipxe/xfer.h>
  37. #include <ipxe/open.h>
  38. #include <ipxe/scsi.h>
  39. #include <ipxe/process.h>
  40. #include <ipxe/uaccess.h>
  41. #include <ipxe/tcpip.h>
  42. #include <ipxe/settings.h>
  43. #include <ipxe/features.h>
  44. #include <ipxe/base16.h>
  45. #include <ipxe/base64.h>
  46. #include <ipxe/ibft.h>
  47. #include <ipxe/iscsi.h>
  48. /** @file
  49. *
  50. * iSCSI protocol
  51. *
  52. */
  53. FEATURE ( FEATURE_PROTOCOL, "iSCSI", DHCP_EB_FEATURE_ISCSI, 1 );
  54. /* Disambiguate the various error causes */
  55. #define EACCES_INCORRECT_TARGET_USERNAME \
  56. __einfo_error ( EINFO_EACCES_INCORRECT_TARGET_USERNAME )
  57. #define EINFO_EACCES_INCORRECT_TARGET_USERNAME \
  58. __einfo_uniqify ( EINFO_EACCES, 0x01, "Incorrect target username" )
  59. #define EACCES_INCORRECT_TARGET_PASSWORD \
  60. __einfo_error ( EINFO_EACCES_INCORRECT_TARGET_PASSWORD )
  61. #define EINFO_EACCES_INCORRECT_TARGET_PASSWORD \
  62. __einfo_uniqify ( EINFO_EACCES, 0x02, "Incorrect target password" )
  63. #define EINVAL_ROOT_PATH_TOO_SHORT \
  64. __einfo_error ( EINFO_EINVAL_ROOT_PATH_TOO_SHORT )
  65. #define EINFO_EINVAL_ROOT_PATH_TOO_SHORT \
  66. __einfo_uniqify ( EINFO_EINVAL, 0x01, "Root path too short" )
  67. #define EINVAL_BAD_CREDENTIAL_MIX \
  68. __einfo_error ( EINFO_EINVAL_BAD_CREDENTIAL_MIX )
  69. #define EINFO_EINVAL_BAD_CREDENTIAL_MIX \
  70. __einfo_uniqify ( EINFO_EINVAL, 0x02, "Bad credential mix" )
  71. #define EINVAL_NO_ROOT_PATH \
  72. __einfo_error ( EINFO_EINVAL_NO_ROOT_PATH )
  73. #define EINFO_EINVAL_NO_ROOT_PATH \
  74. __einfo_uniqify ( EINFO_EINVAL, 0x03, "No root path" )
  75. #define EINVAL_NO_TARGET_IQN \
  76. __einfo_error ( EINFO_EINVAL_NO_TARGET_IQN )
  77. #define EINFO_EINVAL_NO_TARGET_IQN \
  78. __einfo_uniqify ( EINFO_EINVAL, 0x04, "No target IQN" )
  79. #define EINVAL_NO_INITIATOR_IQN \
  80. __einfo_error ( EINFO_EINVAL_NO_INITIATOR_IQN )
  81. #define EINFO_EINVAL_NO_INITIATOR_IQN \
  82. __einfo_uniqify ( EINFO_EINVAL, 0x05, "No initiator IQN" )
  83. #define EIO_TARGET_UNAVAILABLE \
  84. __einfo_error ( EINFO_EIO_TARGET_UNAVAILABLE )
  85. #define EINFO_EIO_TARGET_UNAVAILABLE \
  86. __einfo_uniqify ( EINFO_EIO, 0x01, "Target not currently operational" )
  87. #define EIO_TARGET_NO_RESOURCES \
  88. __einfo_error ( EINFO_EIO_TARGET_NO_RESOURCES )
  89. #define EINFO_EIO_TARGET_NO_RESOURCES \
  90. __einfo_uniqify ( EINFO_EIO, 0x02, "Target out of resources" )
  91. #define ENOTSUP_INITIATOR_STATUS \
  92. __einfo_error ( EINFO_ENOTSUP_INITIATOR_STATUS )
  93. #define EINFO_ENOTSUP_INITIATOR_STATUS \
  94. __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported initiator status" )
  95. #define ENOTSUP_OPCODE \
  96. __einfo_error ( EINFO_ENOTSUP_OPCODE )
  97. #define EINFO_ENOTSUP_OPCODE \
  98. __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported opcode" )
  99. #define ENOTSUP_DISCOVERY \
  100. __einfo_error ( EINFO_ENOTSUP_DISCOVERY )
  101. #define EINFO_ENOTSUP_DISCOVERY \
  102. __einfo_uniqify ( EINFO_ENOTSUP, 0x03, "Discovery not supported" )
  103. #define ENOTSUP_TARGET_STATUS \
  104. __einfo_error ( EINFO_ENOTSUP_TARGET_STATUS )
  105. #define EINFO_ENOTSUP_TARGET_STATUS \
  106. __einfo_uniqify ( EINFO_ENOTSUP, 0x04, "Unsupported target status" )
  107. #define EPERM_INITIATOR_AUTHENTICATION \
  108. __einfo_error ( EINFO_EPERM_INITIATOR_AUTHENTICATION )
  109. #define EINFO_EPERM_INITIATOR_AUTHENTICATION \
  110. __einfo_uniqify ( EINFO_EPERM, 0x01, "Initiator authentication failed" )
  111. #define EPERM_INITIATOR_AUTHORISATION \
  112. __einfo_error ( EINFO_EPERM_INITIATOR_AUTHORISATION )
  113. #define EINFO_EPERM_INITIATOR_AUTHORISATION \
  114. __einfo_uniqify ( EINFO_EPERM, 0x02, "Initiator not authorised" )
  115. #define EPROTO_INVALID_CHAP_ALGORITHM \
  116. __einfo_error ( EINFO_EPROTO_INVALID_CHAP_ALGORITHM )
  117. #define EINFO_EPROTO_INVALID_CHAP_ALGORITHM \
  118. __einfo_uniqify ( EINFO_EPROTO, 0x01, "Invalid CHAP algorithm" )
  119. #define EPROTO_INVALID_CHAP_IDENTIFIER \
  120. __einfo_error ( EINFO_EPROTO_INVALID_CHAP_IDENTIFIER )
  121. #define EINFO_EPROTO_INVALID_CHAP_IDENTIFIER \
  122. __einfo_uniqify ( EINFO_EPROTO, 0x02, "Invalid CHAP identifier" )
  123. #define EPROTO_INVALID_LARGE_BINARY \
  124. __einfo_error ( EINFO_EPROTO_INVALID_LARGE_BINARY )
  125. #define EINFO_EPROTO_INVALID_LARGE_BINARY \
  126. __einfo_uniqify ( EINFO_EPROTO, 0x03, "Invalid large binary value" )
  127. #define EPROTO_INVALID_CHAP_RESPONSE \
  128. __einfo_error ( EINFO_EPROTO_INVALID_CHAP_RESPONSE )
  129. #define EINFO_EPROTO_INVALID_CHAP_RESPONSE \
  130. __einfo_uniqify ( EINFO_EPROTO, 0x04, "Invalid CHAP response" )
  131. #define EPROTO_INVALID_KEY_VALUE_PAIR \
  132. __einfo_error ( EINFO_EPROTO_INVALID_KEY_VALUE_PAIR )
  133. #define EINFO_EPROTO_INVALID_KEY_VALUE_PAIR \
  134. __einfo_uniqify ( EINFO_EPROTO, 0x05, "Invalid key/value pair" )
  135. #define EPROTO_VALUE_REJECTED \
  136. __einfo_error ( EINFO_EPROTO_VALUE_REJECTED )
  137. #define EINFO_EPROTO_VALUE_REJECTED \
  138. __einfo_uniqify ( EINFO_EPROTO, 0x06, "Parameter rejected" )
  139. static void iscsi_start_tx ( struct iscsi_session *iscsi );
  140. static void iscsi_start_login ( struct iscsi_session *iscsi );
  141. static void iscsi_start_data_out ( struct iscsi_session *iscsi,
  142. unsigned int datasn );
  143. /**
  144. * Finish receiving PDU data into buffer
  145. *
  146. * @v iscsi iSCSI session
  147. */
  148. static void iscsi_rx_buffered_data_done ( struct iscsi_session *iscsi ) {
  149. free ( iscsi->rx_buffer );
  150. iscsi->rx_buffer = NULL;
  151. }
  152. /**
  153. * Receive PDU data into buffer
  154. *
  155. * @v iscsi iSCSI session
  156. * @v data Data to receive
  157. * @v len Length of data
  158. * @ret rc Return status code
  159. *
  160. * This can be used when the RX PDU type handler wishes to buffer up
  161. * all received data and process the PDU as a single unit. The caller
  162. * is repsonsible for calling iscsi_rx_buffered_data_done() after
  163. * processing the data.
  164. */
  165. static int iscsi_rx_buffered_data ( struct iscsi_session *iscsi,
  166. const void *data, size_t len ) {
  167. /* Allocate buffer on first call */
  168. if ( ! iscsi->rx_buffer ) {
  169. iscsi->rx_buffer = malloc ( iscsi->rx_len );
  170. if ( ! iscsi->rx_buffer )
  171. return -ENOMEM;
  172. }
  173. /* Copy data to buffer */
  174. assert ( ( iscsi->rx_offset + len ) <= iscsi->rx_len );
  175. memcpy ( ( iscsi->rx_buffer + iscsi->rx_offset ), data, len );
  176. return 0;
  177. }
  178. /**
  179. * Free iSCSI session
  180. *
  181. * @v refcnt Reference counter
  182. */
  183. static void iscsi_free ( struct refcnt *refcnt ) {
  184. struct iscsi_session *iscsi =
  185. container_of ( refcnt, struct iscsi_session, refcnt );
  186. free ( iscsi->initiator_iqn );
  187. free ( iscsi->target_address );
  188. free ( iscsi->target_iqn );
  189. free ( iscsi->initiator_username );
  190. free ( iscsi->initiator_password );
  191. free ( iscsi->target_username );
  192. free ( iscsi->target_password );
  193. chap_finish ( &iscsi->chap );
  194. iscsi_rx_buffered_data_done ( iscsi );
  195. free ( iscsi->command );
  196. free ( iscsi );
  197. }
  198. /**
  199. * Shut down iSCSI interface
  200. *
  201. * @v iscsi iSCSI session
  202. * @v rc Reason for close
  203. */
  204. static void iscsi_close ( struct iscsi_session *iscsi, int rc ) {
  205. /* A TCP graceful close is still an error from our point of view */
  206. if ( rc == 0 )
  207. rc = -ECONNRESET;
  208. DBGC ( iscsi, "iSCSI %p closed: %s\n", iscsi, strerror ( rc ) );
  209. /* Stop transmission process */
  210. process_del ( &iscsi->process );
  211. /* Shut down interfaces */
  212. intfs_shutdown ( rc, &iscsi->socket, &iscsi->control, &iscsi->data,
  213. NULL );
  214. }
  215. /**
  216. * Assign new iSCSI initiator task tag
  217. *
  218. * @v iscsi iSCSI session
  219. */
  220. static void iscsi_new_itt ( struct iscsi_session *iscsi ) {
  221. static uint16_t itt_idx;
  222. iscsi->itt = ( ISCSI_TAG_MAGIC | (++itt_idx) );
  223. }
  224. /**
  225. * Open iSCSI transport-layer connection
  226. *
  227. * @v iscsi iSCSI session
  228. * @ret rc Return status code
  229. */
  230. static int iscsi_open_connection ( struct iscsi_session *iscsi ) {
  231. struct sockaddr_tcpip target;
  232. int rc;
  233. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  234. assert ( iscsi->rx_state == ISCSI_RX_BHS );
  235. assert ( iscsi->rx_offset == 0 );
  236. /* Open socket */
  237. memset ( &target, 0, sizeof ( target ) );
  238. target.st_port = htons ( iscsi->target_port );
  239. if ( ( rc = xfer_open_named_socket ( &iscsi->socket, SOCK_STREAM,
  240. ( struct sockaddr * ) &target,
  241. iscsi->target_address,
  242. NULL ) ) != 0 ) {
  243. DBGC ( iscsi, "iSCSI %p could not open socket: %s\n",
  244. iscsi, strerror ( rc ) );
  245. return rc;
  246. }
  247. /* Enter security negotiation phase */
  248. iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
  249. ISCSI_STATUS_STRINGS_SECURITY );
  250. if ( iscsi->target_username )
  251. iscsi->status |= ISCSI_STATUS_AUTH_REVERSE_REQUIRED;
  252. /* Assign new ISID */
  253. iscsi->isid_iana_qual = ( random() & 0xffff );
  254. /* Assign fresh initiator task tag */
  255. iscsi_new_itt ( iscsi );
  256. /* Initiate login */
  257. iscsi_start_login ( iscsi );
  258. return 0;
  259. }
  260. /**
  261. * Close iSCSI transport-layer connection
  262. *
  263. * @v iscsi iSCSI session
  264. * @v rc Reason for close
  265. *
  266. * Closes the transport-layer connection and resets the session state
  267. * ready to attempt a fresh login.
  268. */
  269. static void iscsi_close_connection ( struct iscsi_session *iscsi, int rc ) {
  270. /* Close all data transfer interfaces */
  271. intf_restart ( &iscsi->socket, rc );
  272. /* Clear connection status */
  273. iscsi->status = 0;
  274. /* Reset TX and RX state machines */
  275. iscsi->tx_state = ISCSI_TX_IDLE;
  276. iscsi->rx_state = ISCSI_RX_BHS;
  277. iscsi->rx_offset = 0;
  278. /* Free any temporary dynamically allocated memory */
  279. chap_finish ( &iscsi->chap );
  280. iscsi_rx_buffered_data_done ( iscsi );
  281. }
  282. /**
  283. * Mark iSCSI SCSI operation as complete
  284. *
  285. * @v iscsi iSCSI session
  286. * @v rc Return status code
  287. * @v rsp SCSI response, if any
  288. *
  289. * Note that iscsi_scsi_done() will not close the connection, and must
  290. * therefore be called only when the internal state machines are in an
  291. * appropriate state, otherwise bad things may happen on the next call
  292. * to iscsi_scsi_command(). The general rule is to call
  293. * iscsi_scsi_done() only at the end of receiving a PDU; at this point
  294. * the TX and RX engines should both be idle.
  295. */
  296. static void iscsi_scsi_done ( struct iscsi_session *iscsi, int rc,
  297. struct scsi_rsp *rsp ) {
  298. uint32_t itt = iscsi->itt;
  299. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  300. /* Clear command */
  301. free ( iscsi->command );
  302. iscsi->command = NULL;
  303. /* Send SCSI response, if any */
  304. if ( rsp )
  305. scsi_response ( &iscsi->data, rsp );
  306. /* Close SCSI command, if this is still the same command. (It
  307. * is possible that the command interface has already been
  308. * closed as a result of the SCSI response we sent.)
  309. */
  310. if ( iscsi->itt == itt )
  311. intf_restart ( &iscsi->data, rc );
  312. }
  313. /****************************************************************************
  314. *
  315. * iSCSI SCSI command issuing
  316. *
  317. */
  318. /**
  319. * Build iSCSI SCSI command BHS
  320. *
  321. * @v iscsi iSCSI session
  322. *
  323. * We don't currently support bidirectional commands (i.e. with both
  324. * Data-In and Data-Out segments); these would require providing code
  325. * to generate an AHS, and there doesn't seem to be any need for it at
  326. * the moment.
  327. */
  328. static void iscsi_start_command ( struct iscsi_session *iscsi ) {
  329. struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
  330. assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
  331. /* Construct BHS and initiate transmission */
  332. iscsi_start_tx ( iscsi );
  333. command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
  334. command->flags = ( ISCSI_FLAG_FINAL |
  335. ISCSI_COMMAND_ATTR_SIMPLE );
  336. if ( iscsi->command->data_in )
  337. command->flags |= ISCSI_COMMAND_FLAG_READ;
  338. if ( iscsi->command->data_out )
  339. command->flags |= ISCSI_COMMAND_FLAG_WRITE;
  340. /* lengths left as zero */
  341. memcpy ( &command->lun, &iscsi->command->lun,
  342. sizeof ( command->lun ) );
  343. command->itt = htonl ( iscsi->itt );
  344. command->exp_len = htonl ( iscsi->command->data_in_len |
  345. iscsi->command->data_out_len );
  346. command->cmdsn = htonl ( iscsi->cmdsn );
  347. command->expstatsn = htonl ( iscsi->statsn + 1 );
  348. memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
  349. DBGC2 ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#zx\n",
  350. iscsi, SCSI_CDB_DATA ( command->cdb ),
  351. ( iscsi->command->data_in ? "in" : "out" ),
  352. ( iscsi->command->data_in ?
  353. iscsi->command->data_in_len :
  354. iscsi->command->data_out_len ) );
  355. }
  356. /**
  357. * Receive data segment of an iSCSI SCSI response PDU
  358. *
  359. * @v iscsi iSCSI session
  360. * @v data Received data
  361. * @v len Length of received data
  362. * @v remaining Data remaining after this data
  363. * @ret rc Return status code
  364. */
  365. static int iscsi_rx_scsi_response ( struct iscsi_session *iscsi,
  366. const void *data, size_t len,
  367. size_t remaining ) {
  368. struct iscsi_bhs_scsi_response *response
  369. = &iscsi->rx_bhs.scsi_response;
  370. struct scsi_rsp rsp;
  371. uint32_t residual_count;
  372. size_t data_len;
  373. int rc;
  374. /* Buffer up the PDU data */
  375. if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
  376. DBGC ( iscsi, "iSCSI %p could not buffer SCSI response: %s\n",
  377. iscsi, strerror ( rc ) );
  378. return rc;
  379. }
  380. if ( remaining )
  381. return 0;
  382. /* Parse SCSI response and discard buffer */
  383. memset ( &rsp, 0, sizeof ( rsp ) );
  384. rsp.status = response->status;
  385. residual_count = ntohl ( response->residual_count );
  386. if ( response->flags & ISCSI_DATA_FLAG_OVERFLOW ) {
  387. rsp.overrun = residual_count;
  388. } else if ( response->flags & ISCSI_DATA_FLAG_UNDERFLOW ) {
  389. rsp.overrun = -(residual_count);
  390. }
  391. data_len = ISCSI_DATA_LEN ( response->lengths );
  392. if ( data_len ) {
  393. scsi_parse_sense ( ( iscsi->rx_buffer + 2 ), ( data_len - 2 ),
  394. &rsp.sense );
  395. }
  396. iscsi_rx_buffered_data_done ( iscsi );
  397. /* Check for errors */
  398. if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
  399. return -EIO;
  400. /* Mark as completed */
  401. iscsi_scsi_done ( iscsi, 0, &rsp );
  402. return 0;
  403. }
  404. /**
  405. * Receive data segment of an iSCSI data-in PDU
  406. *
  407. * @v iscsi iSCSI session
  408. * @v data Received data
  409. * @v len Length of received data
  410. * @v remaining Data remaining after this data
  411. * @ret rc Return status code
  412. */
  413. static int iscsi_rx_data_in ( struct iscsi_session *iscsi,
  414. const void *data, size_t len,
  415. size_t remaining ) {
  416. struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
  417. unsigned long offset;
  418. /* Copy data to data-in buffer */
  419. offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
  420. assert ( iscsi->command != NULL );
  421. assert ( iscsi->command->data_in );
  422. assert ( ( offset + len ) <= iscsi->command->data_in_len );
  423. copy_to_user ( iscsi->command->data_in, offset, data, len );
  424. /* Wait for whole SCSI response to arrive */
  425. if ( remaining )
  426. return 0;
  427. /* Mark as completed if status is present */
  428. if ( data_in->flags & ISCSI_DATA_FLAG_STATUS ) {
  429. assert ( ( offset + len ) == iscsi->command->data_in_len );
  430. assert ( data_in->flags & ISCSI_FLAG_FINAL );
  431. /* iSCSI cannot return an error status via a data-in */
  432. iscsi_scsi_done ( iscsi, 0, NULL );
  433. }
  434. return 0;
  435. }
  436. /**
  437. * Receive data segment of an iSCSI R2T PDU
  438. *
  439. * @v iscsi iSCSI session
  440. * @v data Received data
  441. * @v len Length of received data
  442. * @v remaining Data remaining after this data
  443. * @ret rc Return status code
  444. */
  445. static int iscsi_rx_r2t ( struct iscsi_session *iscsi,
  446. const void *data __unused, size_t len __unused,
  447. size_t remaining __unused ) {
  448. struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
  449. /* Record transfer parameters and trigger first data-out */
  450. iscsi->ttt = ntohl ( r2t->ttt );
  451. iscsi->transfer_offset = ntohl ( r2t->offset );
  452. iscsi->transfer_len = ntohl ( r2t->len );
  453. iscsi_start_data_out ( iscsi, 0 );
  454. return 0;
  455. }
  456. /**
  457. * Build iSCSI data-out BHS
  458. *
  459. * @v iscsi iSCSI session
  460. * @v datasn Data sequence number within the transfer
  461. *
  462. */
  463. static void iscsi_start_data_out ( struct iscsi_session *iscsi,
  464. unsigned int datasn ) {
  465. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  466. unsigned long offset;
  467. unsigned long remaining;
  468. unsigned long len;
  469. /* We always send 512-byte Data-Out PDUs; this removes the
  470. * need to worry about the target's MaxRecvDataSegmentLength.
  471. */
  472. offset = datasn * 512;
  473. remaining = iscsi->transfer_len - offset;
  474. len = remaining;
  475. if ( len > 512 )
  476. len = 512;
  477. /* Construct BHS and initiate transmission */
  478. iscsi_start_tx ( iscsi );
  479. data_out->opcode = ISCSI_OPCODE_DATA_OUT;
  480. if ( len == remaining )
  481. data_out->flags = ( ISCSI_FLAG_FINAL );
  482. ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
  483. data_out->lun = iscsi->command->lun;
  484. data_out->itt = htonl ( iscsi->itt );
  485. data_out->ttt = htonl ( iscsi->ttt );
  486. data_out->expstatsn = htonl ( iscsi->statsn + 1 );
  487. data_out->datasn = htonl ( datasn );
  488. data_out->offset = htonl ( iscsi->transfer_offset + offset );
  489. DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
  490. iscsi, datasn, len );
  491. }
  492. /**
  493. * Complete iSCSI data-out PDU transmission
  494. *
  495. * @v iscsi iSCSI session
  496. *
  497. */
  498. static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
  499. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  500. /* If we haven't reached the end of the sequence, start
  501. * sending the next data-out PDU.
  502. */
  503. if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
  504. iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
  505. }
  506. /**
  507. * Send iSCSI data-out data segment
  508. *
  509. * @v iscsi iSCSI session
  510. * @ret rc Return status code
  511. */
  512. static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
  513. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  514. struct io_buffer *iobuf;
  515. unsigned long offset;
  516. size_t len;
  517. size_t pad_len;
  518. offset = ntohl ( data_out->offset );
  519. len = ISCSI_DATA_LEN ( data_out->lengths );
  520. pad_len = ISCSI_DATA_PAD_LEN ( data_out->lengths );
  521. assert ( iscsi->command != NULL );
  522. assert ( iscsi->command->data_out );
  523. assert ( ( offset + len ) <= iscsi->command->data_out_len );
  524. iobuf = xfer_alloc_iob ( &iscsi->socket, ( len + pad_len ) );
  525. if ( ! iobuf )
  526. return -ENOMEM;
  527. copy_from_user ( iob_put ( iobuf, len ),
  528. iscsi->command->data_out, offset, len );
  529. memset ( iob_put ( iobuf, pad_len ), 0, pad_len );
  530. return xfer_deliver_iob ( &iscsi->socket, iobuf );
  531. }
  532. /**
  533. * Receive data segment of an iSCSI NOP-In
  534. *
  535. * @v iscsi iSCSI session
  536. * @v data Received data
  537. * @v len Length of received data
  538. * @v remaining Data remaining after this data
  539. * @ret rc Return status code
  540. */
  541. static int iscsi_rx_nop_in ( struct iscsi_session *iscsi,
  542. const void *data __unused, size_t len __unused,
  543. size_t remaining __unused ) {
  544. struct iscsi_nop_in *nop_in = &iscsi->rx_bhs.nop_in;
  545. DBGC2 ( iscsi, "iSCSI %p received NOP-In\n", iscsi );
  546. /* We don't currently have the ability to respond to NOP-Ins
  547. * sent as ping requests, but we can happily accept NOP-Ins
  548. * sent merely to update CmdSN.
  549. */
  550. if ( nop_in->ttt == htonl ( ISCSI_TAG_RESERVED ) )
  551. return 0;
  552. /* Ignore any other NOP-Ins. The target may eventually
  553. * disconnect us for failing to respond, but this minimises
  554. * unnecessary connection closures.
  555. */
  556. DBGC ( iscsi, "iSCSI %p received unsupported NOP-In with TTT %08x\n",
  557. iscsi, ntohl ( nop_in->ttt ) );
  558. return 0;
  559. }
  560. /****************************************************************************
  561. *
  562. * iSCSI login
  563. *
  564. */
  565. /**
  566. * Build iSCSI login request strings
  567. *
  568. * @v iscsi iSCSI session
  569. *
  570. * These are the initial set of strings sent in the first login
  571. * request PDU. We want the following settings:
  572. *
  573. * HeaderDigest=None
  574. * DataDigest=None
  575. * MaxConnections=1 (irrelevant; we make only one connection anyway) [4]
  576. * InitialR2T=Yes [1]
  577. * ImmediateData=No (irrelevant; we never send immediate data) [4]
  578. * MaxRecvDataSegmentLength=8192 (default; we don't care) [3]
  579. * MaxBurstLength=262144 (default; we don't care) [3]
  580. * FirstBurstLength=65536 (irrelevant due to other settings) [5]
  581. * DefaultTime2Wait=0 [2]
  582. * DefaultTime2Retain=0 [2]
  583. * MaxOutstandingR2T=1
  584. * DataPDUInOrder=Yes
  585. * DataSequenceInOrder=Yes
  586. * ErrorRecoveryLevel=0
  587. *
  588. * [1] InitialR2T has an OR resolution function, so the target may
  589. * force us to use it. We therefore simplify our logic by always
  590. * using it.
  591. *
  592. * [2] These ensure that we can safely start a new task once we have
  593. * reconnected after a failure, without having to manually tidy up
  594. * after the old one.
  595. *
  596. * [3] We are quite happy to use the RFC-defined default values for
  597. * these parameters, but some targets (notably OpenSolaris)
  598. * incorrectly assume a default value of zero, so we explicitly
  599. * specify the default values.
  600. *
  601. * [4] We are quite happy to use the RFC-defined default values for
  602. * these parameters, but some targets (notably a QNAP TS-639Pro) fail
  603. * unless they are supplied, so we explicitly specify the default
  604. * values.
  605. *
  606. * [5] FirstBurstLength is defined to be irrelevant since we already
  607. * force InitialR2T=Yes and ImmediateData=No, but some targets
  608. * (notably LIO as of kernel 4.11) fail unless it is specified, so we
  609. * explicitly specify the default value.
  610. */
  611. static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
  612. void *data, size_t len ) {
  613. unsigned int used = 0;
  614. const char *auth_method;
  615. if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
  616. /* Default to allowing no authentication */
  617. auth_method = "None";
  618. /* If we have a credential to supply, permit CHAP */
  619. if ( iscsi->initiator_username )
  620. auth_method = "CHAP,None";
  621. /* If we have a credential to check, force CHAP */
  622. if ( iscsi->target_username )
  623. auth_method = "CHAP";
  624. used += ssnprintf ( data + used, len - used,
  625. "InitiatorName=%s%c"
  626. "TargetName=%s%c"
  627. "SessionType=Normal%c"
  628. "AuthMethod=%s%c",
  629. iscsi->initiator_iqn, 0,
  630. iscsi->target_iqn, 0, 0,
  631. auth_method, 0 );
  632. }
  633. if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
  634. used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
  635. }
  636. if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
  637. char buf[ base16_encoded_len ( iscsi->chap.response_len ) + 1 ];
  638. assert ( iscsi->initiator_username != NULL );
  639. base16_encode ( iscsi->chap.response, iscsi->chap.response_len,
  640. buf, sizeof ( buf ) );
  641. used += ssnprintf ( data + used, len - used,
  642. "CHAP_N=%s%cCHAP_R=0x%s%c",
  643. iscsi->initiator_username, 0, buf, 0 );
  644. }
  645. if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_CHALLENGE ) ) {
  646. size_t challenge_len = ( sizeof ( iscsi->chap_challenge ) - 1 );
  647. char buf[ base16_encoded_len ( challenge_len ) + 1 ];
  648. base16_encode ( ( iscsi->chap_challenge + 1 ), challenge_len,
  649. buf, sizeof ( buf ) );
  650. used += ssnprintf ( data + used, len - used,
  651. "CHAP_I=%d%cCHAP_C=0x%s%c",
  652. iscsi->chap_challenge[0], 0, buf, 0 );
  653. }
  654. if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
  655. used += ssnprintf ( data + used, len - used,
  656. "HeaderDigest=None%c"
  657. "DataDigest=None%c"
  658. "MaxConnections=1%c"
  659. "InitialR2T=Yes%c"
  660. "ImmediateData=No%c"
  661. "MaxRecvDataSegmentLength=8192%c"
  662. "MaxBurstLength=262144%c"
  663. "FirstBurstLength=65536%c"
  664. "DefaultTime2Wait=0%c"
  665. "DefaultTime2Retain=0%c"
  666. "MaxOutstandingR2T=1%c"
  667. "DataPDUInOrder=Yes%c"
  668. "DataSequenceInOrder=Yes%c"
  669. "ErrorRecoveryLevel=0%c",
  670. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  671. }
  672. return used;
  673. }
  674. /**
  675. * Build iSCSI login request BHS
  676. *
  677. * @v iscsi iSCSI session
  678. */
  679. static void iscsi_start_login ( struct iscsi_session *iscsi ) {
  680. struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
  681. int len;
  682. switch ( iscsi->status & ISCSI_LOGIN_CSG_MASK ) {
  683. case ISCSI_LOGIN_CSG_SECURITY_NEGOTIATION:
  684. DBGC ( iscsi, "iSCSI %p entering security negotiation\n",
  685. iscsi );
  686. break;
  687. case ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION:
  688. DBGC ( iscsi, "iSCSI %p entering operational negotiation\n",
  689. iscsi );
  690. break;
  691. default:
  692. assert ( 0 );
  693. }
  694. /* Construct BHS and initiate transmission */
  695. iscsi_start_tx ( iscsi );
  696. request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
  697. ISCSI_FLAG_IMMEDIATE );
  698. request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
  699. ISCSI_LOGIN_FLAG_TRANSITION );
  700. /* version_max and version_min left as zero */
  701. len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
  702. ISCSI_SET_LENGTHS ( request->lengths, 0, len );
  703. request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
  704. IANA_EN_FEN_SYSTEMS );
  705. request->isid_iana_qual = htons ( iscsi->isid_iana_qual );
  706. /* tsih left as zero */
  707. request->itt = htonl ( iscsi->itt );
  708. /* cid left as zero */
  709. request->cmdsn = htonl ( iscsi->cmdsn );
  710. request->expstatsn = htonl ( iscsi->statsn + 1 );
  711. }
  712. /**
  713. * Complete iSCSI login request PDU transmission
  714. *
  715. * @v iscsi iSCSI session
  716. *
  717. */
  718. static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
  719. /* Clear any "strings to send" flags */
  720. iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
  721. /* Free any dynamically allocated storage used for login */
  722. chap_finish ( &iscsi->chap );
  723. }
  724. /**
  725. * Transmit data segment of an iSCSI login request PDU
  726. *
  727. * @v iscsi iSCSI session
  728. * @ret rc Return status code
  729. *
  730. * For login requests, the data segment consists of the login strings.
  731. */
  732. static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
  733. struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
  734. struct io_buffer *iobuf;
  735. size_t len;
  736. size_t pad_len;
  737. len = ISCSI_DATA_LEN ( request->lengths );
  738. pad_len = ISCSI_DATA_PAD_LEN ( request->lengths );
  739. iobuf = xfer_alloc_iob ( &iscsi->socket, ( len + pad_len ) );
  740. if ( ! iobuf )
  741. return -ENOMEM;
  742. iob_put ( iobuf, len );
  743. iscsi_build_login_request_strings ( iscsi, iobuf->data, len );
  744. memset ( iob_put ( iobuf, pad_len ), 0, pad_len );
  745. return xfer_deliver_iob ( &iscsi->socket, iobuf );
  746. }
  747. /**
  748. * Decode large binary value
  749. *
  750. * @v encoded Encoded large binary value
  751. * @v raw Raw data
  752. * @v len Length of data buffer
  753. * @ret len Length of raw data, or negative error
  754. */
  755. static int iscsi_large_binary_decode ( const char *encoded, uint8_t *raw,
  756. size_t len ) {
  757. /* Check for initial '0x' or '0b' and decode as appropriate */
  758. if ( *(encoded++) == '0' ) {
  759. switch ( tolower ( *(encoded++) ) ) {
  760. case 'x' :
  761. return base16_decode ( encoded, raw, len );
  762. case 'b' :
  763. return base64_decode ( encoded, raw, len );
  764. }
  765. }
  766. return -EPROTO_INVALID_LARGE_BINARY;
  767. }
  768. /**
  769. * Handle iSCSI TargetAddress text value
  770. *
  771. * @v iscsi iSCSI session
  772. * @v value TargetAddress value
  773. * @ret rc Return status code
  774. */
  775. static int iscsi_handle_targetaddress_value ( struct iscsi_session *iscsi,
  776. const char *value ) {
  777. char *separator;
  778. DBGC ( iscsi, "iSCSI %p will redirect to %s\n", iscsi, value );
  779. /* Replace target address */
  780. free ( iscsi->target_address );
  781. iscsi->target_address = strdup ( value );
  782. if ( ! iscsi->target_address )
  783. return -ENOMEM;
  784. /* Replace target port */
  785. iscsi->target_port = htons ( ISCSI_PORT );
  786. separator = strchr ( iscsi->target_address, ':' );
  787. if ( separator ) {
  788. *separator = '\0';
  789. iscsi->target_port = strtoul ( ( separator + 1 ), NULL, 0 );
  790. }
  791. return 0;
  792. }
  793. /**
  794. * Handle iSCSI AuthMethod text value
  795. *
  796. * @v iscsi iSCSI session
  797. * @v value AuthMethod value
  798. * @ret rc Return status code
  799. */
  800. static int iscsi_handle_authmethod_value ( struct iscsi_session *iscsi,
  801. const char *value ) {
  802. /* If server requests CHAP, send the CHAP_A string */
  803. if ( strcmp ( value, "CHAP" ) == 0 ) {
  804. DBGC ( iscsi, "iSCSI %p initiating CHAP authentication\n",
  805. iscsi );
  806. iscsi->status |= ( ISCSI_STATUS_STRINGS_CHAP_ALGORITHM |
  807. ISCSI_STATUS_AUTH_FORWARD_REQUIRED );
  808. }
  809. return 0;
  810. }
  811. /**
  812. * Handle iSCSI CHAP_A text value
  813. *
  814. * @v iscsi iSCSI session
  815. * @v value CHAP_A value
  816. * @ret rc Return status code
  817. */
  818. static int iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
  819. const char *value ) {
  820. /* We only ever offer "5" (i.e. MD5) as an algorithm, so if
  821. * the server responds with anything else it is a protocol
  822. * violation.
  823. */
  824. if ( strcmp ( value, "5" ) != 0 ) {
  825. DBGC ( iscsi, "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
  826. iscsi, value );
  827. return -EPROTO_INVALID_CHAP_ALGORITHM;
  828. }
  829. return 0;
  830. }
  831. /**
  832. * Handle iSCSI CHAP_I text value
  833. *
  834. * @v iscsi iSCSI session
  835. * @v value CHAP_I value
  836. * @ret rc Return status code
  837. */
  838. static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
  839. const char *value ) {
  840. unsigned int identifier;
  841. char *endp;
  842. int rc;
  843. /* The CHAP identifier is an integer value */
  844. identifier = strtoul ( value, &endp, 0 );
  845. if ( *endp != '\0' ) {
  846. DBGC ( iscsi, "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
  847. iscsi, value );
  848. return -EPROTO_INVALID_CHAP_IDENTIFIER;
  849. }
  850. /* Prepare for CHAP with MD5 */
  851. chap_finish ( &iscsi->chap );
  852. if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
  853. DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
  854. iscsi, strerror ( rc ) );
  855. return rc;
  856. }
  857. /* Identifier and secret are the first two components of the
  858. * challenge.
  859. */
  860. chap_set_identifier ( &iscsi->chap, identifier );
  861. if ( iscsi->initiator_password ) {
  862. chap_update ( &iscsi->chap, iscsi->initiator_password,
  863. strlen ( iscsi->initiator_password ) );
  864. }
  865. return 0;
  866. }
  867. /**
  868. * Handle iSCSI CHAP_C text value
  869. *
  870. * @v iscsi iSCSI session
  871. * @v value CHAP_C value
  872. * @ret rc Return status code
  873. */
  874. static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
  875. const char *value ) {
  876. uint8_t *buf;
  877. unsigned int i;
  878. int len;
  879. int rc;
  880. /* Allocate decoding buffer */
  881. len = strlen ( value ); /* Decoding never expands data */
  882. buf = malloc ( len );
  883. if ( ! buf ) {
  884. rc = -ENOMEM;
  885. goto err_alloc;
  886. }
  887. /* Process challenge */
  888. len = iscsi_large_binary_decode ( value, buf, len );
  889. if ( len < 0 ) {
  890. rc = len;
  891. DBGC ( iscsi, "iSCSI %p invalid CHAP challenge \"%s\": %s\n",
  892. iscsi, value, strerror ( rc ) );
  893. goto err_decode;
  894. }
  895. chap_update ( &iscsi->chap, buf, len );
  896. /* Build CHAP response */
  897. DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );
  898. chap_respond ( &iscsi->chap );
  899. iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_RESPONSE;
  900. /* Send CHAP challenge, if applicable */
  901. if ( iscsi->target_username ) {
  902. iscsi->status |= ISCSI_STATUS_STRINGS_CHAP_CHALLENGE;
  903. /* Generate CHAP challenge data */
  904. for ( i = 0 ; i < sizeof ( iscsi->chap_challenge ) ; i++ ) {
  905. iscsi->chap_challenge[i] = random();
  906. }
  907. }
  908. /* Success */
  909. rc = 0;
  910. err_decode:
  911. free ( buf );
  912. err_alloc:
  913. return rc;
  914. }
  915. /**
  916. * Handle iSCSI CHAP_N text value
  917. *
  918. * @v iscsi iSCSI session
  919. * @v value CHAP_N value
  920. * @ret rc Return status code
  921. */
  922. static int iscsi_handle_chap_n_value ( struct iscsi_session *iscsi,
  923. const char *value ) {
  924. /* The target username isn't actually involved at any point in
  925. * the authentication process; it merely serves to identify
  926. * which password the target is using to generate the CHAP
  927. * response. We unnecessarily verify that the username is as
  928. * expected, in order to provide mildly helpful diagnostics if
  929. * the target is supplying the wrong username/password
  930. * combination.
  931. */
  932. if ( iscsi->target_username &&
  933. ( strcmp ( iscsi->target_username, value ) != 0 ) ) {
  934. DBGC ( iscsi, "iSCSI %p target username \"%s\" incorrect "
  935. "(wanted \"%s\")\n",
  936. iscsi, value, iscsi->target_username );
  937. return -EACCES_INCORRECT_TARGET_USERNAME;
  938. }
  939. return 0;
  940. }
  941. /**
  942. * Handle iSCSI CHAP_R text value
  943. *
  944. * @v iscsi iSCSI session
  945. * @v value CHAP_R value
  946. * @ret rc Return status code
  947. */
  948. static int iscsi_handle_chap_r_value ( struct iscsi_session *iscsi,
  949. const char *value ) {
  950. uint8_t *buf;
  951. int len;
  952. int rc;
  953. /* Generate CHAP response for verification */
  954. chap_finish ( &iscsi->chap );
  955. if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
  956. DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
  957. iscsi, strerror ( rc ) );
  958. goto err_chap_init;
  959. }
  960. chap_set_identifier ( &iscsi->chap, iscsi->chap_challenge[0] );
  961. if ( iscsi->target_password ) {
  962. chap_update ( &iscsi->chap, iscsi->target_password,
  963. strlen ( iscsi->target_password ) );
  964. }
  965. chap_update ( &iscsi->chap, &iscsi->chap_challenge[1],
  966. ( sizeof ( iscsi->chap_challenge ) - 1 ) );
  967. chap_respond ( &iscsi->chap );
  968. /* Allocate decoding buffer */
  969. len = strlen ( value ); /* Decoding never expands data */
  970. buf = malloc ( len );
  971. if ( ! buf ) {
  972. rc = -ENOMEM;
  973. goto err_alloc;
  974. }
  975. /* Process response */
  976. len = iscsi_large_binary_decode ( value, buf, len );
  977. if ( len < 0 ) {
  978. rc = len;
  979. DBGC ( iscsi, "iSCSI %p invalid CHAP response \"%s\": %s\n",
  980. iscsi, value, strerror ( rc ) );
  981. goto err_decode;
  982. }
  983. /* Check CHAP response */
  984. if ( len != ( int ) iscsi->chap.response_len ) {
  985. DBGC ( iscsi, "iSCSI %p invalid CHAP response length\n",
  986. iscsi );
  987. rc = -EPROTO_INVALID_CHAP_RESPONSE;
  988. goto err_response_len;
  989. }
  990. if ( memcmp ( buf, iscsi->chap.response, len ) != 0 ) {
  991. DBGC ( iscsi, "iSCSI %p incorrect CHAP response \"%s\"\n",
  992. iscsi, value );
  993. rc = -EACCES_INCORRECT_TARGET_PASSWORD;
  994. goto err_response;
  995. }
  996. /* Mark session as authenticated */
  997. iscsi->status |= ISCSI_STATUS_AUTH_REVERSE_OK;
  998. err_response:
  999. err_response_len:
  1000. err_decode:
  1001. free ( buf );
  1002. err_alloc:
  1003. err_chap_init:
  1004. return rc;
  1005. }
  1006. /** An iSCSI text string that we want to handle */
  1007. struct iscsi_string_type {
  1008. /** String key
  1009. *
  1010. * This is the portion preceding the "=" sign,
  1011. * e.g. "InitiatorName", "CHAP_A", etc.
  1012. */
  1013. const char *key;
  1014. /** Handle iSCSI string value
  1015. *
  1016. * @v iscsi iSCSI session
  1017. * @v value iSCSI string value
  1018. * @ret rc Return status code
  1019. */
  1020. int ( * handle ) ( struct iscsi_session *iscsi, const char *value );
  1021. };
  1022. /** iSCSI text strings that we want to handle */
  1023. static struct iscsi_string_type iscsi_string_types[] = {
  1024. { "TargetAddress", iscsi_handle_targetaddress_value },
  1025. { "AuthMethod", iscsi_handle_authmethod_value },
  1026. { "CHAP_A", iscsi_handle_chap_a_value },
  1027. { "CHAP_I", iscsi_handle_chap_i_value },
  1028. { "CHAP_C", iscsi_handle_chap_c_value },
  1029. { "CHAP_N", iscsi_handle_chap_n_value },
  1030. { "CHAP_R", iscsi_handle_chap_r_value },
  1031. { NULL, NULL }
  1032. };
  1033. /**
  1034. * Handle iSCSI string
  1035. *
  1036. * @v iscsi iSCSI session
  1037. * @v string iSCSI string (in "key=value" format)
  1038. * @ret rc Return status code
  1039. */
  1040. static int iscsi_handle_string ( struct iscsi_session *iscsi,
  1041. const char *string ) {
  1042. struct iscsi_string_type *type;
  1043. const char *separator;
  1044. const char *value;
  1045. size_t key_len;
  1046. int rc;
  1047. /* Find separator */
  1048. separator = strchr ( string, '=' );
  1049. if ( ! separator ) {
  1050. DBGC ( iscsi, "iSCSI %p malformed string %s\n",
  1051. iscsi, string );
  1052. return -EPROTO_INVALID_KEY_VALUE_PAIR;
  1053. }
  1054. key_len = ( separator - string );
  1055. value = ( separator + 1 );
  1056. /* Check for rejections. Since we send only non-rejectable
  1057. * values, any rejection is a fatal protocol error.
  1058. */
  1059. if ( strcmp ( value, "Reject" ) == 0 ) {
  1060. DBGC ( iscsi, "iSCSI %p rejection: %s\n", iscsi, string );
  1061. return -EPROTO_VALUE_REJECTED;
  1062. }
  1063. /* Handle key/value pair */
  1064. for ( type = iscsi_string_types ; type->key ; type++ ) {
  1065. if ( strncmp ( string, type->key, key_len ) != 0 )
  1066. continue;
  1067. DBGC ( iscsi, "iSCSI %p handling %s\n", iscsi, string );
  1068. if ( ( rc = type->handle ( iscsi, value ) ) != 0 ) {
  1069. DBGC ( iscsi, "iSCSI %p could not handle %s: %s\n",
  1070. iscsi, string, strerror ( rc ) );
  1071. return rc;
  1072. }
  1073. return 0;
  1074. }
  1075. DBGC ( iscsi, "iSCSI %p ignoring %s\n", iscsi, string );
  1076. return 0;
  1077. }
  1078. /**
  1079. * Handle iSCSI strings
  1080. *
  1081. * @v iscsi iSCSI session
  1082. * @v string iSCSI string buffer
  1083. * @v len Length of string buffer
  1084. * @ret rc Return status code
  1085. */
  1086. static int iscsi_handle_strings ( struct iscsi_session *iscsi,
  1087. const char *strings, size_t len ) {
  1088. size_t string_len;
  1089. int rc;
  1090. /* Handle each string in turn, taking care not to overrun the
  1091. * data buffer in case of badly-terminated data.
  1092. */
  1093. while ( 1 ) {
  1094. string_len = ( strnlen ( strings, len ) + 1 );
  1095. if ( string_len > len )
  1096. break;
  1097. if ( ( rc = iscsi_handle_string ( iscsi, strings ) ) != 0 )
  1098. return rc;
  1099. strings += string_len;
  1100. len -= string_len;
  1101. }
  1102. return 0;
  1103. }
  1104. /**
  1105. * Convert iSCSI response status to return status code
  1106. *
  1107. * @v status_class iSCSI status class
  1108. * @v status_detail iSCSI status detail
  1109. * @ret rc Return status code
  1110. */
  1111. static int iscsi_status_to_rc ( unsigned int status_class,
  1112. unsigned int status_detail ) {
  1113. switch ( status_class ) {
  1114. case ISCSI_STATUS_INITIATOR_ERROR :
  1115. switch ( status_detail ) {
  1116. case ISCSI_STATUS_INITIATOR_ERROR_AUTHENTICATION :
  1117. return -EPERM_INITIATOR_AUTHENTICATION;
  1118. case ISCSI_STATUS_INITIATOR_ERROR_AUTHORISATION :
  1119. return -EPERM_INITIATOR_AUTHORISATION;
  1120. case ISCSI_STATUS_INITIATOR_ERROR_NOT_FOUND :
  1121. case ISCSI_STATUS_INITIATOR_ERROR_REMOVED :
  1122. return -ENODEV;
  1123. default :
  1124. return -ENOTSUP_INITIATOR_STATUS;
  1125. }
  1126. case ISCSI_STATUS_TARGET_ERROR :
  1127. switch ( status_detail ) {
  1128. case ISCSI_STATUS_TARGET_ERROR_UNAVAILABLE:
  1129. return -EIO_TARGET_UNAVAILABLE;
  1130. case ISCSI_STATUS_TARGET_ERROR_NO_RESOURCES:
  1131. return -EIO_TARGET_NO_RESOURCES;
  1132. default:
  1133. return -ENOTSUP_TARGET_STATUS;
  1134. }
  1135. default :
  1136. return -EINVAL;
  1137. }
  1138. }
  1139. /**
  1140. * Receive data segment of an iSCSI login response PDU
  1141. *
  1142. * @v iscsi iSCSI session
  1143. * @v data Received data
  1144. * @v len Length of received data
  1145. * @v remaining Data remaining after this data
  1146. * @ret rc Return status code
  1147. */
  1148. static int iscsi_rx_login_response ( struct iscsi_session *iscsi,
  1149. const void *data, size_t len,
  1150. size_t remaining ) {
  1151. struct iscsi_bhs_login_response *response
  1152. = &iscsi->rx_bhs.login_response;
  1153. int rc;
  1154. /* Buffer up the PDU data */
  1155. if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
  1156. DBGC ( iscsi, "iSCSI %p could not buffer login response: %s\n",
  1157. iscsi, strerror ( rc ) );
  1158. return rc;
  1159. }
  1160. if ( remaining )
  1161. return 0;
  1162. /* Process string data and discard string buffer */
  1163. if ( ( rc = iscsi_handle_strings ( iscsi, iscsi->rx_buffer,
  1164. iscsi->rx_len ) ) != 0 )
  1165. return rc;
  1166. iscsi_rx_buffered_data_done ( iscsi );
  1167. /* Check for login redirection */
  1168. if ( response->status_class == ISCSI_STATUS_REDIRECT ) {
  1169. DBGC ( iscsi, "iSCSI %p redirecting to new server\n", iscsi );
  1170. iscsi_close_connection ( iscsi, 0 );
  1171. if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
  1172. DBGC ( iscsi, "iSCSI %p could not redirect: %s\n ",
  1173. iscsi, strerror ( rc ) );
  1174. return rc;
  1175. }
  1176. return 0;
  1177. }
  1178. /* Check for fatal errors */
  1179. if ( response->status_class != 0 ) {
  1180. DBGC ( iscsi, "iSCSI login failure: class %02x detail %02x\n",
  1181. response->status_class, response->status_detail );
  1182. rc = iscsi_status_to_rc ( response->status_class,
  1183. response->status_detail );
  1184. return rc;
  1185. }
  1186. /* Handle login transitions */
  1187. if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
  1188. iscsi->status &= ~( ISCSI_STATUS_PHASE_MASK |
  1189. ISCSI_STATUS_STRINGS_MASK );
  1190. switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
  1191. case ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION:
  1192. iscsi->status |=
  1193. ( ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE |
  1194. ISCSI_STATUS_STRINGS_OPERATIONAL );
  1195. break;
  1196. case ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE:
  1197. iscsi->status |= ISCSI_STATUS_FULL_FEATURE_PHASE;
  1198. break;
  1199. default:
  1200. DBGC ( iscsi, "iSCSI %p got invalid response flags "
  1201. "%02x\n", iscsi, response->flags );
  1202. return -EIO;
  1203. }
  1204. }
  1205. /* Send next login request PDU if we haven't reached the full
  1206. * feature phase yet.
  1207. */
  1208. if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
  1209. ISCSI_STATUS_FULL_FEATURE_PHASE ) {
  1210. iscsi_start_login ( iscsi );
  1211. return 0;
  1212. }
  1213. /* Check that target authentication was successful (if required) */
  1214. if ( ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) &&
  1215. ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_OK ) ) {
  1216. DBGC ( iscsi, "iSCSI %p nefarious target tried to bypass "
  1217. "authentication\n", iscsi );
  1218. return -EPROTO;
  1219. }
  1220. /* Notify SCSI layer of window change */
  1221. DBGC ( iscsi, "iSCSI %p entering full feature phase\n", iscsi );
  1222. xfer_window_changed ( &iscsi->control );
  1223. return 0;
  1224. }
  1225. /****************************************************************************
  1226. *
  1227. * iSCSI to socket interface
  1228. *
  1229. */
  1230. /**
  1231. * Pause TX engine
  1232. *
  1233. * @v iscsi iSCSI session
  1234. */
  1235. static void iscsi_tx_pause ( struct iscsi_session *iscsi ) {
  1236. process_del ( &iscsi->process );
  1237. }
  1238. /**
  1239. * Resume TX engine
  1240. *
  1241. * @v iscsi iSCSI session
  1242. */
  1243. static void iscsi_tx_resume ( struct iscsi_session *iscsi ) {
  1244. process_add ( &iscsi->process );
  1245. }
  1246. /**
  1247. * Start up a new TX PDU
  1248. *
  1249. * @v iscsi iSCSI session
  1250. *
  1251. * This initiates the process of sending a new PDU. Only one PDU may
  1252. * be in transit at any one time.
  1253. */
  1254. static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
  1255. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  1256. /* Initialise TX BHS */
  1257. memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
  1258. /* Flag TX engine to start transmitting */
  1259. iscsi->tx_state = ISCSI_TX_BHS;
  1260. /* Start transmission process */
  1261. iscsi_tx_resume ( iscsi );
  1262. }
  1263. /**
  1264. * Transmit nothing
  1265. *
  1266. * @v iscsi iSCSI session
  1267. * @ret rc Return status code
  1268. */
  1269. static int iscsi_tx_nothing ( struct iscsi_session *iscsi __unused ) {
  1270. return 0;
  1271. }
  1272. /**
  1273. * Transmit basic header segment of an iSCSI PDU
  1274. *
  1275. * @v iscsi iSCSI session
  1276. * @ret rc Return status code
  1277. */
  1278. static int iscsi_tx_bhs ( struct iscsi_session *iscsi ) {
  1279. return xfer_deliver_raw ( &iscsi->socket, &iscsi->tx_bhs,
  1280. sizeof ( iscsi->tx_bhs ) );
  1281. }
  1282. /**
  1283. * Transmit data segment of an iSCSI PDU
  1284. *
  1285. * @v iscsi iSCSI session
  1286. * @ret rc Return status code
  1287. *
  1288. * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
  1289. * will be valid when this is called.
  1290. */
  1291. static int iscsi_tx_data ( struct iscsi_session *iscsi ) {
  1292. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1293. switch ( common->opcode & ISCSI_OPCODE_MASK ) {
  1294. case ISCSI_OPCODE_DATA_OUT:
  1295. return iscsi_tx_data_out ( iscsi );
  1296. case ISCSI_OPCODE_LOGIN_REQUEST:
  1297. return iscsi_tx_login_request ( iscsi );
  1298. default:
  1299. /* Nothing to send in other states */
  1300. return 0;
  1301. }
  1302. }
  1303. /**
  1304. * Complete iSCSI PDU transmission
  1305. *
  1306. * @v iscsi iSCSI session
  1307. *
  1308. * Called when a PDU has been completely transmitted and the TX state
  1309. * machine is about to enter the idle state. iscsi::tx_bhs will be
  1310. * valid for the just-completed PDU when this is called.
  1311. */
  1312. static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
  1313. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1314. /* Stop transmission process */
  1315. iscsi_tx_pause ( iscsi );
  1316. switch ( common->opcode & ISCSI_OPCODE_MASK ) {
  1317. case ISCSI_OPCODE_DATA_OUT:
  1318. iscsi_data_out_done ( iscsi );
  1319. break;
  1320. case ISCSI_OPCODE_LOGIN_REQUEST:
  1321. iscsi_login_request_done ( iscsi );
  1322. break;
  1323. default:
  1324. /* No action */
  1325. break;
  1326. }
  1327. }
  1328. /**
  1329. * Transmit iSCSI PDU
  1330. *
  1331. * @v iscsi iSCSI session
  1332. * @v buf Temporary data buffer
  1333. * @v len Length of temporary data buffer
  1334. *
  1335. * Constructs data to be sent for the current TX state
  1336. */
  1337. static void iscsi_tx_step ( struct iscsi_session *iscsi ) {
  1338. struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
  1339. int ( * tx ) ( struct iscsi_session *iscsi );
  1340. enum iscsi_tx_state next_state;
  1341. size_t tx_len;
  1342. int rc;
  1343. /* Select fragment to transmit */
  1344. while ( 1 ) {
  1345. switch ( iscsi->tx_state ) {
  1346. case ISCSI_TX_BHS:
  1347. tx = iscsi_tx_bhs;
  1348. tx_len = sizeof ( iscsi->tx_bhs );
  1349. next_state = ISCSI_TX_AHS;
  1350. break;
  1351. case ISCSI_TX_AHS:
  1352. tx = iscsi_tx_nothing;
  1353. tx_len = 0;
  1354. next_state = ISCSI_TX_DATA;
  1355. break;
  1356. case ISCSI_TX_DATA:
  1357. tx = iscsi_tx_data;
  1358. tx_len = ISCSI_DATA_LEN ( common->lengths );
  1359. next_state = ISCSI_TX_IDLE;
  1360. break;
  1361. case ISCSI_TX_IDLE:
  1362. /* Nothing to do; pause processing */
  1363. iscsi_tx_pause ( iscsi );
  1364. return;
  1365. default:
  1366. assert ( 0 );
  1367. return;
  1368. }
  1369. /* Check for window availability, if needed */
  1370. if ( tx_len && ( xfer_window ( &iscsi->socket ) == 0 ) ) {
  1371. /* Cannot transmit at this point; pause
  1372. * processing and wait for window to reopen
  1373. */
  1374. iscsi_tx_pause ( iscsi );
  1375. return;
  1376. }
  1377. /* Transmit data */
  1378. if ( ( rc = tx ( iscsi ) ) != 0 ) {
  1379. DBGC ( iscsi, "iSCSI %p could not transmit: %s\n",
  1380. iscsi, strerror ( rc ) );
  1381. /* Transmission errors are fatal */
  1382. iscsi_close ( iscsi, rc );
  1383. return;
  1384. }
  1385. /* Move to next state */
  1386. iscsi->tx_state = next_state;
  1387. /* If we have moved to the idle state, mark
  1388. * transmission as complete
  1389. */
  1390. if ( iscsi->tx_state == ISCSI_TX_IDLE )
  1391. iscsi_tx_done ( iscsi );
  1392. }
  1393. }
  1394. /** iSCSI TX process descriptor */
  1395. static struct process_descriptor iscsi_process_desc =
  1396. PROC_DESC ( struct iscsi_session, process, iscsi_tx_step );
  1397. /**
  1398. * Receive basic header segment of an iSCSI PDU
  1399. *
  1400. * @v iscsi iSCSI session
  1401. * @v data Received data
  1402. * @v len Length of received data
  1403. * @v remaining Data remaining after this data
  1404. * @ret rc Return status code
  1405. *
  1406. * This fills in iscsi::rx_bhs with the data from the BHS portion of
  1407. * the received PDU.
  1408. */
  1409. static int iscsi_rx_bhs ( struct iscsi_session *iscsi, const void *data,
  1410. size_t len, size_t remaining __unused ) {
  1411. memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
  1412. if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
  1413. DBGC2 ( iscsi, "iSCSI %p received PDU opcode %#x len %#x\n",
  1414. iscsi, iscsi->rx_bhs.common.opcode,
  1415. ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
  1416. }
  1417. return 0;
  1418. }
  1419. /**
  1420. * Discard portion of an iSCSI PDU.
  1421. *
  1422. * @v iscsi iSCSI session
  1423. * @v data Received data
  1424. * @v len Length of received data
  1425. * @v remaining Data remaining after this data
  1426. * @ret rc Return status code
  1427. *
  1428. * This discards data from a portion of a received PDU.
  1429. */
  1430. static int iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
  1431. const void *data __unused, size_t len __unused,
  1432. size_t remaining __unused ) {
  1433. /* Do nothing */
  1434. return 0;
  1435. }
  1436. /**
  1437. * Receive data segment of an iSCSI PDU
  1438. *
  1439. * @v iscsi iSCSI session
  1440. * @v data Received data
  1441. * @v len Length of received data
  1442. * @v remaining Data remaining after this data
  1443. * @ret rc Return status code
  1444. *
  1445. * Handle processing of part of a PDU data segment. iscsi::rx_bhs
  1446. * will be valid when this is called.
  1447. */
  1448. static int iscsi_rx_data ( struct iscsi_session *iscsi, const void *data,
  1449. size_t len, size_t remaining ) {
  1450. struct iscsi_bhs_common_response *response
  1451. = &iscsi->rx_bhs.common_response;
  1452. /* Update cmdsn and statsn */
  1453. iscsi->cmdsn = ntohl ( response->expcmdsn );
  1454. iscsi->statsn = ntohl ( response->statsn );
  1455. switch ( response->opcode & ISCSI_OPCODE_MASK ) {
  1456. case ISCSI_OPCODE_LOGIN_RESPONSE:
  1457. return iscsi_rx_login_response ( iscsi, data, len, remaining );
  1458. case ISCSI_OPCODE_SCSI_RESPONSE:
  1459. return iscsi_rx_scsi_response ( iscsi, data, len, remaining );
  1460. case ISCSI_OPCODE_DATA_IN:
  1461. return iscsi_rx_data_in ( iscsi, data, len, remaining );
  1462. case ISCSI_OPCODE_R2T:
  1463. return iscsi_rx_r2t ( iscsi, data, len, remaining );
  1464. case ISCSI_OPCODE_NOP_IN:
  1465. return iscsi_rx_nop_in ( iscsi, data, len, remaining );
  1466. default:
  1467. if ( remaining )
  1468. return 0;
  1469. DBGC ( iscsi, "iSCSI %p unknown opcode %02x\n", iscsi,
  1470. response->opcode );
  1471. return -ENOTSUP_OPCODE;
  1472. }
  1473. }
  1474. /**
  1475. * Receive new data
  1476. *
  1477. * @v iscsi iSCSI session
  1478. * @v iobuf I/O buffer
  1479. * @v meta Data transfer metadata
  1480. * @ret rc Return status code
  1481. *
  1482. * This handles received PDUs. The receive strategy is to fill in
  1483. * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
  1484. * throw away any AHS portion, and then process each part of the data
  1485. * portion as it arrives. The data processing routine therefore
  1486. * always has a full copy of the BHS available, even for portions of
  1487. * the data in different packets to the BHS.
  1488. */
  1489. static int iscsi_socket_deliver ( struct iscsi_session *iscsi,
  1490. struct io_buffer *iobuf,
  1491. struct xfer_metadata *meta __unused ) {
  1492. struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
  1493. int ( * rx ) ( struct iscsi_session *iscsi, const void *data,
  1494. size_t len, size_t remaining );
  1495. enum iscsi_rx_state next_state;
  1496. size_t frag_len;
  1497. size_t remaining;
  1498. int rc;
  1499. while ( 1 ) {
  1500. switch ( iscsi->rx_state ) {
  1501. case ISCSI_RX_BHS:
  1502. rx = iscsi_rx_bhs;
  1503. iscsi->rx_len = sizeof ( iscsi->rx_bhs );
  1504. next_state = ISCSI_RX_AHS;
  1505. break;
  1506. case ISCSI_RX_AHS:
  1507. rx = iscsi_rx_discard;
  1508. iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
  1509. next_state = ISCSI_RX_DATA;
  1510. break;
  1511. case ISCSI_RX_DATA:
  1512. rx = iscsi_rx_data;
  1513. iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
  1514. next_state = ISCSI_RX_DATA_PADDING;
  1515. break;
  1516. case ISCSI_RX_DATA_PADDING:
  1517. rx = iscsi_rx_discard;
  1518. iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
  1519. next_state = ISCSI_RX_BHS;
  1520. break;
  1521. default:
  1522. assert ( 0 );
  1523. rc = -EINVAL;
  1524. goto done;
  1525. }
  1526. frag_len = iscsi->rx_len - iscsi->rx_offset;
  1527. if ( frag_len > iob_len ( iobuf ) )
  1528. frag_len = iob_len ( iobuf );
  1529. remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
  1530. if ( ( rc = rx ( iscsi, iobuf->data, frag_len,
  1531. remaining ) ) != 0 ) {
  1532. DBGC ( iscsi, "iSCSI %p could not process received "
  1533. "data: %s\n", iscsi, strerror ( rc ) );
  1534. goto done;
  1535. }
  1536. iscsi->rx_offset += frag_len;
  1537. iob_pull ( iobuf, frag_len );
  1538. /* If all the data for this state has not yet been
  1539. * received, stay in this state for now.
  1540. */
  1541. if ( iscsi->rx_offset != iscsi->rx_len ) {
  1542. rc = 0;
  1543. goto done;
  1544. }
  1545. iscsi->rx_state = next_state;
  1546. iscsi->rx_offset = 0;
  1547. }
  1548. done:
  1549. /* Free I/O buffer */
  1550. free_iob ( iobuf );
  1551. /* Destroy session on error */
  1552. if ( rc != 0 )
  1553. iscsi_close ( iscsi, rc );
  1554. return rc;
  1555. }
  1556. /**
  1557. * Handle redirection event
  1558. *
  1559. * @v iscsi iSCSI session
  1560. * @v type Location type
  1561. * @v args Remaining arguments depend upon location type
  1562. * @ret rc Return status code
  1563. */
  1564. static int iscsi_vredirect ( struct iscsi_session *iscsi, int type,
  1565. va_list args ) {
  1566. va_list tmp;
  1567. struct sockaddr *peer;
  1568. int rc;
  1569. /* Intercept redirects to a LOCATION_SOCKET and record the IP
  1570. * address for the iBFT. This is a bit of a hack, but avoids
  1571. * inventing an ioctl()-style call to retrieve the socket
  1572. * address from a data-xfer interface.
  1573. */
  1574. if ( type == LOCATION_SOCKET ) {
  1575. va_copy ( tmp, args );
  1576. ( void ) va_arg ( tmp, int ); /* Discard "semantics" */
  1577. peer = va_arg ( tmp, struct sockaddr * );
  1578. memcpy ( &iscsi->target_sockaddr, peer,
  1579. sizeof ( iscsi->target_sockaddr ) );
  1580. va_end ( tmp );
  1581. }
  1582. /* Redirect to new location */
  1583. if ( ( rc = xfer_vreopen ( &iscsi->socket, type, args ) ) != 0 )
  1584. goto err;
  1585. return 0;
  1586. err:
  1587. iscsi_close ( iscsi, rc );
  1588. return rc;
  1589. }
  1590. /** iSCSI socket interface operations */
  1591. static struct interface_operation iscsi_socket_operations[] = {
  1592. INTF_OP ( xfer_deliver, struct iscsi_session *, iscsi_socket_deliver ),
  1593. INTF_OP ( xfer_window_changed, struct iscsi_session *,
  1594. iscsi_tx_resume ),
  1595. INTF_OP ( xfer_vredirect, struct iscsi_session *, iscsi_vredirect ),
  1596. INTF_OP ( intf_close, struct iscsi_session *, iscsi_close ),
  1597. };
  1598. /** iSCSI socket interface descriptor */
  1599. static struct interface_descriptor iscsi_socket_desc =
  1600. INTF_DESC ( struct iscsi_session, socket, iscsi_socket_operations );
  1601. /****************************************************************************
  1602. *
  1603. * iSCSI command issuing
  1604. *
  1605. */
  1606. /**
  1607. * Check iSCSI flow-control window
  1608. *
  1609. * @v iscsi iSCSI session
  1610. * @ret len Length of window
  1611. */
  1612. static size_t iscsi_scsi_window ( struct iscsi_session *iscsi ) {
  1613. if ( ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) ==
  1614. ISCSI_STATUS_FULL_FEATURE_PHASE ) &&
  1615. ( iscsi->command == NULL ) ) {
  1616. /* We cannot handle concurrent commands */
  1617. return 1;
  1618. } else {
  1619. return 0;
  1620. }
  1621. }
  1622. /**
  1623. * Issue iSCSI SCSI command
  1624. *
  1625. * @v iscsi iSCSI session
  1626. * @v parent Parent interface
  1627. * @v command SCSI command
  1628. * @ret tag Command tag, or negative error
  1629. */
  1630. static int iscsi_scsi_command ( struct iscsi_session *iscsi,
  1631. struct interface *parent,
  1632. struct scsi_cmd *command ) {
  1633. /* This iSCSI implementation cannot handle multiple concurrent
  1634. * commands or commands arriving before login is complete.
  1635. */
  1636. if ( iscsi_scsi_window ( iscsi ) == 0 ) {
  1637. DBGC ( iscsi, "iSCSI %p cannot handle concurrent commands\n",
  1638. iscsi );
  1639. return -EOPNOTSUPP;
  1640. }
  1641. /* Store command */
  1642. iscsi->command = malloc ( sizeof ( *command ) );
  1643. if ( ! iscsi->command )
  1644. return -ENOMEM;
  1645. memcpy ( iscsi->command, command, sizeof ( *command ) );
  1646. /* Assign new ITT */
  1647. iscsi_new_itt ( iscsi );
  1648. /* Start sending command */
  1649. iscsi_start_command ( iscsi );
  1650. /* Attach to parent interface and return */
  1651. intf_plug_plug ( &iscsi->data, parent );
  1652. return iscsi->itt;
  1653. }
  1654. /**
  1655. * Get iSCSI ACPI descriptor
  1656. *
  1657. * @v iscsi iSCSI session
  1658. * @ret desc ACPI descriptor
  1659. */
  1660. static struct acpi_descriptor * iscsi_describe ( struct iscsi_session *iscsi ) {
  1661. return &iscsi->desc;
  1662. }
  1663. /** iSCSI SCSI command-issuing interface operations */
  1664. static struct interface_operation iscsi_control_op[] = {
  1665. INTF_OP ( scsi_command, struct iscsi_session *, iscsi_scsi_command ),
  1666. INTF_OP ( xfer_window, struct iscsi_session *, iscsi_scsi_window ),
  1667. INTF_OP ( intf_close, struct iscsi_session *, iscsi_close ),
  1668. INTF_OP ( acpi_describe, struct iscsi_session *, iscsi_describe ),
  1669. };
  1670. /** iSCSI SCSI command-issuing interface descriptor */
  1671. static struct interface_descriptor iscsi_control_desc =
  1672. INTF_DESC ( struct iscsi_session, control, iscsi_control_op );
  1673. /**
  1674. * Close iSCSI command
  1675. *
  1676. * @v iscsi iSCSI session
  1677. * @v rc Reason for close
  1678. */
  1679. static void iscsi_command_close ( struct iscsi_session *iscsi, int rc ) {
  1680. /* Restart interface */
  1681. intf_restart ( &iscsi->data, rc );
  1682. /* Treat unsolicited command closures mid-command as fatal,
  1683. * because we have no code to handle partially-completed PDUs.
  1684. */
  1685. if ( iscsi->command != NULL )
  1686. iscsi_close ( iscsi, ( ( rc == 0 ) ? -ECANCELED : rc ) );
  1687. }
  1688. /** iSCSI SCSI command interface operations */
  1689. static struct interface_operation iscsi_data_op[] = {
  1690. INTF_OP ( intf_close, struct iscsi_session *, iscsi_command_close ),
  1691. };
  1692. /** iSCSI SCSI command interface descriptor */
  1693. static struct interface_descriptor iscsi_data_desc =
  1694. INTF_DESC ( struct iscsi_session, data, iscsi_data_op );
  1695. /****************************************************************************
  1696. *
  1697. * Instantiator
  1698. *
  1699. */
  1700. /** iSCSI root path components (as per RFC4173) */
  1701. enum iscsi_root_path_component {
  1702. RP_SERVERNAME = 0,
  1703. RP_PROTOCOL,
  1704. RP_PORT,
  1705. RP_LUN,
  1706. RP_TARGETNAME,
  1707. NUM_RP_COMPONENTS
  1708. };
  1709. /** iSCSI initiator IQN setting */
  1710. const struct setting initiator_iqn_setting __setting ( SETTING_SANBOOT_EXTRA,
  1711. initiator-iqn ) = {
  1712. .name = "initiator-iqn",
  1713. .description = "iSCSI initiator name",
  1714. .tag = DHCP_ISCSI_INITIATOR_IQN,
  1715. .type = &setting_type_string,
  1716. };
  1717. /** iSCSI reverse username setting */
  1718. const struct setting reverse_username_setting __setting ( SETTING_AUTH_EXTRA,
  1719. reverse-username ) = {
  1720. .name = "reverse-username",
  1721. .description = "Reverse user name",
  1722. .tag = DHCP_EB_REVERSE_USERNAME,
  1723. .type = &setting_type_string,
  1724. };
  1725. /** iSCSI reverse password setting */
  1726. const struct setting reverse_password_setting __setting ( SETTING_AUTH_EXTRA,
  1727. reverse-password ) = {
  1728. .name = "reverse-password",
  1729. .description = "Reverse password",
  1730. .tag = DHCP_EB_REVERSE_PASSWORD,
  1731. .type = &setting_type_string,
  1732. };
  1733. /**
  1734. * Parse iSCSI root path
  1735. *
  1736. * @v iscsi iSCSI session
  1737. * @v root_path iSCSI root path (as per RFC4173)
  1738. * @ret rc Return status code
  1739. */
  1740. static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
  1741. const char *root_path ) {
  1742. char rp_copy[ strlen ( root_path ) + 1 ];
  1743. char *rp_comp[NUM_RP_COMPONENTS];
  1744. char *rp = rp_copy;
  1745. int skip = 0;
  1746. int i = 0;
  1747. int rc;
  1748. /* Split root path into component parts */
  1749. strcpy ( rp_copy, root_path );
  1750. while ( 1 ) {
  1751. rp_comp[i++] = rp;
  1752. if ( i == NUM_RP_COMPONENTS )
  1753. break;
  1754. for ( ; ( ( *rp != ':' ) || skip ) ; rp++ ) {
  1755. if ( ! *rp ) {
  1756. DBGC ( iscsi, "iSCSI %p root path \"%s\" "
  1757. "too short\n", iscsi, root_path );
  1758. return -EINVAL_ROOT_PATH_TOO_SHORT;
  1759. } else if ( *rp == '[' ) {
  1760. skip = 1;
  1761. } else if ( *rp == ']' ) {
  1762. skip = 0;
  1763. }
  1764. }
  1765. *(rp++) = '\0';
  1766. }
  1767. /* Use root path components to configure iSCSI session */
  1768. iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
  1769. if ( ! iscsi->target_address )
  1770. return -ENOMEM;
  1771. iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
  1772. if ( ! iscsi->target_port )
  1773. iscsi->target_port = ISCSI_PORT;
  1774. if ( ( rc = scsi_parse_lun ( rp_comp[RP_LUN], &iscsi->lun ) ) != 0 ) {
  1775. DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
  1776. iscsi, rp_comp[RP_LUN] );
  1777. return rc;
  1778. }
  1779. iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
  1780. if ( ! iscsi->target_iqn )
  1781. return -ENOMEM;
  1782. return 0;
  1783. }
  1784. /**
  1785. * Fetch iSCSI settings
  1786. *
  1787. * @v iscsi iSCSI session
  1788. * @ret rc Return status code
  1789. */
  1790. static int iscsi_fetch_settings ( struct iscsi_session *iscsi ) {
  1791. char *hostname;
  1792. union uuid uuid;
  1793. int len;
  1794. /* Fetch relevant settings. Don't worry about freeing on
  1795. * error, since iscsi_free() will take care of that anyway.
  1796. */
  1797. fetch_string_setting_copy ( NULL, &username_setting,
  1798. &iscsi->initiator_username );
  1799. fetch_string_setting_copy ( NULL, &password_setting,
  1800. &iscsi->initiator_password );
  1801. fetch_string_setting_copy ( NULL, &reverse_username_setting,
  1802. &iscsi->target_username );
  1803. fetch_string_setting_copy ( NULL, &reverse_password_setting,
  1804. &iscsi->target_password );
  1805. /* Use explicit initiator IQN if provided */
  1806. fetch_string_setting_copy ( NULL, &initiator_iqn_setting,
  1807. &iscsi->initiator_iqn );
  1808. if ( iscsi->initiator_iqn )
  1809. return 0;
  1810. /* Otherwise, try to construct an initiator IQN from the hostname */
  1811. fetch_string_setting_copy ( NULL, &hostname_setting, &hostname );
  1812. if ( hostname ) {
  1813. len = asprintf ( &iscsi->initiator_iqn,
  1814. ISCSI_DEFAULT_IQN_PREFIX ":%s", hostname );
  1815. free ( hostname );
  1816. if ( len < 0 ) {
  1817. DBGC ( iscsi, "iSCSI %p could not allocate initiator "
  1818. "IQN\n", iscsi );
  1819. return -ENOMEM;
  1820. }
  1821. assert ( iscsi->initiator_iqn );
  1822. return 0;
  1823. }
  1824. /* Otherwise, try to construct an initiator IQN from the UUID */
  1825. if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting, &uuid ) ) < 0 ) {
  1826. DBGC ( iscsi, "iSCSI %p has no suitable initiator IQN\n",
  1827. iscsi );
  1828. return -EINVAL_NO_INITIATOR_IQN;
  1829. }
  1830. if ( ( len = asprintf ( &iscsi->initiator_iqn,
  1831. ISCSI_DEFAULT_IQN_PREFIX ":%s",
  1832. uuid_ntoa ( &uuid ) ) ) < 0 ) {
  1833. DBGC ( iscsi, "iSCSI %p could not allocate initiator IQN\n",
  1834. iscsi );
  1835. return -ENOMEM;
  1836. }
  1837. assert ( iscsi->initiator_iqn );
  1838. return 0;
  1839. }
  1840. /**
  1841. * Check iSCSI authentication details
  1842. *
  1843. * @v iscsi iSCSI session
  1844. * @ret rc Return status code
  1845. */
  1846. static int iscsi_check_auth ( struct iscsi_session *iscsi ) {
  1847. /* Check for invalid authentication combinations */
  1848. if ( ( /* Initiator username without password (or vice-versa) */
  1849. ( !! iscsi->initiator_username ) ^
  1850. ( !! iscsi->initiator_password ) ) ||
  1851. ( /* Target username without password (or vice-versa) */
  1852. ( !! iscsi->target_username ) ^
  1853. ( !! iscsi->target_password ) ) ||
  1854. ( /* Target (reverse) without initiator (forward) */
  1855. ( iscsi->target_username &&
  1856. ( ! iscsi->initiator_username ) ) ) ) {
  1857. DBGC ( iscsi, "iSCSI %p invalid credentials: initiator "
  1858. "%sname,%spw, target %sname,%spw\n", iscsi,
  1859. ( iscsi->initiator_username ? "" : "no " ),
  1860. ( iscsi->initiator_password ? "" : "no " ),
  1861. ( iscsi->target_username ? "" : "no " ),
  1862. ( iscsi->target_password ? "" : "no " ) );
  1863. return -EINVAL_BAD_CREDENTIAL_MIX;
  1864. }
  1865. return 0;
  1866. }
  1867. /**
  1868. * Open iSCSI URI
  1869. *
  1870. * @v parent Parent interface
  1871. * @v uri URI
  1872. * @ret rc Return status code
  1873. */
  1874. static int iscsi_open ( struct interface *parent, struct uri *uri ) {
  1875. struct iscsi_session *iscsi;
  1876. int rc;
  1877. /* Sanity check */
  1878. if ( ! uri->opaque ) {
  1879. rc = -EINVAL_NO_ROOT_PATH;
  1880. goto err_sanity_uri;
  1881. }
  1882. /* Allocate and initialise structure */
  1883. iscsi = zalloc ( sizeof ( *iscsi ) );
  1884. if ( ! iscsi ) {
  1885. rc = -ENOMEM;
  1886. goto err_zalloc;
  1887. }
  1888. ref_init ( &iscsi->refcnt, iscsi_free );
  1889. intf_init ( &iscsi->control, &iscsi_control_desc, &iscsi->refcnt );
  1890. intf_init ( &iscsi->data, &iscsi_data_desc, &iscsi->refcnt );
  1891. intf_init ( &iscsi->socket, &iscsi_socket_desc, &iscsi->refcnt );
  1892. process_init_stopped ( &iscsi->process, &iscsi_process_desc,
  1893. &iscsi->refcnt );
  1894. acpi_init ( &iscsi->desc, &ibft_model, &iscsi->refcnt );
  1895. /* Parse root path */
  1896. if ( ( rc = iscsi_parse_root_path ( iscsi, uri->opaque ) ) != 0 )
  1897. goto err_parse_root_path;
  1898. /* Set fields not specified by root path */
  1899. if ( ( rc = iscsi_fetch_settings ( iscsi ) ) != 0 )
  1900. goto err_fetch_settings;
  1901. /* Validate authentication */
  1902. if ( ( rc = iscsi_check_auth ( iscsi ) ) != 0 )
  1903. goto err_check_auth;
  1904. /* Sanity checks */
  1905. if ( ! iscsi->target_address ) {
  1906. DBGC ( iscsi, "iSCSI %p does not yet support discovery\n",
  1907. iscsi );
  1908. rc = -ENOTSUP_DISCOVERY;
  1909. goto err_sanity_address;
  1910. }
  1911. if ( ! iscsi->target_iqn ) {
  1912. DBGC ( iscsi, "iSCSI %p no target address supplied in %s\n",
  1913. iscsi, uri->opaque );
  1914. rc = -EINVAL_NO_TARGET_IQN;
  1915. goto err_sanity_iqn;
  1916. }
  1917. DBGC ( iscsi, "iSCSI %p initiator %s\n",iscsi, iscsi->initiator_iqn );
  1918. DBGC ( iscsi, "iSCSI %p target %s %s\n",
  1919. iscsi, iscsi->target_address, iscsi->target_iqn );
  1920. /* Open socket */
  1921. if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 )
  1922. goto err_open_connection;
  1923. /* Attach SCSI device to parent interface */
  1924. if ( ( rc = scsi_open ( parent, &iscsi->control,
  1925. &iscsi->lun ) ) != 0 ) {
  1926. DBGC ( iscsi, "iSCSI %p could not create SCSI device: %s\n",
  1927. iscsi, strerror ( rc ) );
  1928. goto err_scsi_open;
  1929. }
  1930. /* Mortalise self, and return */
  1931. ref_put ( &iscsi->refcnt );
  1932. return 0;
  1933. err_scsi_open:
  1934. err_open_connection:
  1935. err_sanity_iqn:
  1936. err_sanity_address:
  1937. err_check_auth:
  1938. err_fetch_settings:
  1939. err_parse_root_path:
  1940. iscsi_close ( iscsi, rc );
  1941. ref_put ( &iscsi->refcnt );
  1942. err_zalloc:
  1943. err_sanity_uri:
  1944. return rc;
  1945. }
  1946. /** iSCSI URI opener */
  1947. struct uri_opener iscsi_uri_opener __uri_opener = {
  1948. .scheme = "iscsi",
  1949. .open = iscsi_open,
  1950. };