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

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