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.

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