Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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