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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  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. intf_shutdown ( &iscsi->socket, rc );
  217. intf_shutdown ( &iscsi->control, rc );
  218. intf_shutdown ( &iscsi->data, rc );
  219. }
  220. /**
  221. * Assign new iSCSI initiator task tag
  222. *
  223. * @v iscsi iSCSI session
  224. */
  225. static void iscsi_new_itt ( struct iscsi_session *iscsi ) {
  226. static uint16_t itt_idx;
  227. iscsi->itt = ( ISCSI_TAG_MAGIC | (++itt_idx) );
  228. }
  229. /**
  230. * Open iSCSI transport-layer connection
  231. *
  232. * @v iscsi iSCSI session
  233. * @ret rc Return status code
  234. */
  235. static int iscsi_open_connection ( struct iscsi_session *iscsi ) {
  236. struct sockaddr_tcpip target;
  237. int rc;
  238. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  239. assert ( iscsi->rx_state == ISCSI_RX_BHS );
  240. assert ( iscsi->rx_offset == 0 );
  241. /* Open socket */
  242. memset ( &target, 0, sizeof ( target ) );
  243. target.st_port = htons ( iscsi->target_port );
  244. if ( ( rc = xfer_open_named_socket ( &iscsi->socket, SOCK_STREAM,
  245. ( struct sockaddr * ) &target,
  246. iscsi->target_address,
  247. NULL ) ) != 0 ) {
  248. DBGC ( iscsi, "iSCSI %p could not open socket: %s\n",
  249. iscsi, strerror ( rc ) );
  250. return rc;
  251. }
  252. /* Enter security negotiation phase */
  253. iscsi->status = ( ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE |
  254. ISCSI_STATUS_STRINGS_SECURITY );
  255. if ( iscsi->target_username )
  256. iscsi->status |= ISCSI_STATUS_AUTH_REVERSE_REQUIRED;
  257. /* Assign new ISID */
  258. iscsi->isid_iana_qual = ( random() & 0xffff );
  259. /* Assign fresh initiator task tag */
  260. iscsi_new_itt ( iscsi );
  261. /* Initiate login */
  262. iscsi_start_login ( iscsi );
  263. return 0;
  264. }
  265. /**
  266. * Close iSCSI transport-layer connection
  267. *
  268. * @v iscsi iSCSI session
  269. * @v rc Reason for close
  270. *
  271. * Closes the transport-layer connection and resets the session state
  272. * ready to attempt a fresh login.
  273. */
  274. static void iscsi_close_connection ( struct iscsi_session *iscsi, int rc ) {
  275. /* Close all data transfer interfaces */
  276. intf_restart ( &iscsi->socket, rc );
  277. /* Clear connection status */
  278. iscsi->status = 0;
  279. /* Reset TX and RX state machines */
  280. iscsi->tx_state = ISCSI_TX_IDLE;
  281. iscsi->rx_state = ISCSI_RX_BHS;
  282. iscsi->rx_offset = 0;
  283. /* Free any temporary dynamically allocated memory */
  284. chap_finish ( &iscsi->chap );
  285. iscsi_rx_buffered_data_done ( iscsi );
  286. }
  287. /**
  288. * Mark iSCSI SCSI operation as complete
  289. *
  290. * @v iscsi iSCSI session
  291. * @v rc Return status code
  292. * @v rsp SCSI response, if any
  293. *
  294. * Note that iscsi_scsi_done() will not close the connection, and must
  295. * therefore be called only when the internal state machines are in an
  296. * appropriate state, otherwise bad things may happen on the next call
  297. * to iscsi_scsi_command(). The general rule is to call
  298. * iscsi_scsi_done() only at the end of receiving a PDU; at this point
  299. * the TX and RX engines should both be idle.
  300. */
  301. static void iscsi_scsi_done ( struct iscsi_session *iscsi, int rc,
  302. struct scsi_rsp *rsp ) {
  303. uint32_t itt = iscsi->itt;
  304. assert ( iscsi->tx_state == ISCSI_TX_IDLE );
  305. /* Clear command */
  306. free ( iscsi->command );
  307. iscsi->command = NULL;
  308. /* Send SCSI response, if any */
  309. if ( rsp )
  310. scsi_response ( &iscsi->data, rsp );
  311. /* Close SCSI command, if this is still the same command. (It
  312. * is possible that the command interface has already been
  313. * closed as a result of the SCSI response we sent.)
  314. */
  315. if ( iscsi->itt == itt )
  316. intf_restart ( &iscsi->data, rc );
  317. }
  318. /****************************************************************************
  319. *
  320. * iSCSI SCSI command issuing
  321. *
  322. */
  323. /**
  324. * Build iSCSI SCSI command BHS
  325. *
  326. * @v iscsi iSCSI session
  327. *
  328. * We don't currently support bidirectional commands (i.e. with both
  329. * Data-In and Data-Out segments); these would require providing code
  330. * to generate an AHS, and there doesn't seem to be any need for it at
  331. * the moment.
  332. */
  333. static void iscsi_start_command ( struct iscsi_session *iscsi ) {
  334. struct iscsi_bhs_scsi_command *command = &iscsi->tx_bhs.scsi_command;
  335. assert ( ! ( iscsi->command->data_in && iscsi->command->data_out ) );
  336. /* Construct BHS and initiate transmission */
  337. iscsi_start_tx ( iscsi );
  338. command->opcode = ISCSI_OPCODE_SCSI_COMMAND;
  339. command->flags = ( ISCSI_FLAG_FINAL |
  340. ISCSI_COMMAND_ATTR_SIMPLE );
  341. if ( iscsi->command->data_in )
  342. command->flags |= ISCSI_COMMAND_FLAG_READ;
  343. if ( iscsi->command->data_out )
  344. command->flags |= ISCSI_COMMAND_FLAG_WRITE;
  345. /* lengths left as zero */
  346. memcpy ( &command->lun, &iscsi->command->lun,
  347. sizeof ( command->lun ) );
  348. command->itt = htonl ( iscsi->itt );
  349. command->exp_len = htonl ( iscsi->command->data_in_len |
  350. iscsi->command->data_out_len );
  351. command->cmdsn = htonl ( iscsi->cmdsn );
  352. command->expstatsn = htonl ( iscsi->statsn + 1 );
  353. memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
  354. DBGC2 ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#zx\n",
  355. iscsi, SCSI_CDB_DATA ( command->cdb ),
  356. ( iscsi->command->data_in ? "in" : "out" ),
  357. ( iscsi->command->data_in ?
  358. iscsi->command->data_in_len :
  359. iscsi->command->data_out_len ) );
  360. }
  361. /**
  362. * Receive data segment of an iSCSI SCSI response PDU
  363. *
  364. * @v iscsi iSCSI session
  365. * @v data Received data
  366. * @v len Length of received data
  367. * @v remaining Data remaining after this data
  368. * @ret rc Return status code
  369. */
  370. static int iscsi_rx_scsi_response ( struct iscsi_session *iscsi,
  371. const void *data, size_t len,
  372. size_t remaining ) {
  373. struct iscsi_bhs_scsi_response *response
  374. = &iscsi->rx_bhs.scsi_response;
  375. struct scsi_rsp rsp;
  376. uint32_t residual_count;
  377. size_t data_len;
  378. int rc;
  379. /* Buffer up the PDU data */
  380. if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
  381. DBGC ( iscsi, "iSCSI %p could not buffer SCSI response: %s\n",
  382. iscsi, strerror ( rc ) );
  383. return rc;
  384. }
  385. if ( remaining )
  386. return 0;
  387. /* Parse SCSI response and discard buffer */
  388. memset ( &rsp, 0, sizeof ( rsp ) );
  389. rsp.status = response->status;
  390. residual_count = ntohl ( response->residual_count );
  391. if ( response->flags & ISCSI_DATA_FLAG_OVERFLOW ) {
  392. rsp.overrun = residual_count;
  393. } else if ( response->flags & ISCSI_DATA_FLAG_UNDERFLOW ) {
  394. rsp.overrun = -(residual_count);
  395. }
  396. data_len = ISCSI_DATA_LEN ( response->lengths );
  397. if ( data_len ) {
  398. scsi_parse_sense ( ( iscsi->rx_buffer + 2 ), ( data_len - 2 ),
  399. &rsp.sense );
  400. }
  401. iscsi_rx_buffered_data_done ( iscsi );
  402. /* Check for errors */
  403. if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
  404. return -EIO;
  405. /* Mark as completed */
  406. iscsi_scsi_done ( iscsi, 0, &rsp );
  407. return 0;
  408. }
  409. /**
  410. * Receive data segment of an iSCSI data-in PDU
  411. *
  412. * @v iscsi iSCSI session
  413. * @v data Received data
  414. * @v len Length of received data
  415. * @v remaining Data remaining after this data
  416. * @ret rc Return status code
  417. */
  418. static int iscsi_rx_data_in ( struct iscsi_session *iscsi,
  419. const void *data, size_t len,
  420. size_t remaining ) {
  421. struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
  422. unsigned long offset;
  423. /* Copy data to data-in buffer */
  424. offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
  425. assert ( iscsi->command != NULL );
  426. assert ( iscsi->command->data_in );
  427. assert ( ( offset + len ) <= iscsi->command->data_in_len );
  428. copy_to_user ( iscsi->command->data_in, offset, data, len );
  429. /* Wait for whole SCSI response to arrive */
  430. if ( remaining )
  431. return 0;
  432. /* Mark as completed if status is present */
  433. if ( data_in->flags & ISCSI_DATA_FLAG_STATUS ) {
  434. assert ( ( offset + len ) == iscsi->command->data_in_len );
  435. assert ( data_in->flags & ISCSI_FLAG_FINAL );
  436. /* iSCSI cannot return an error status via a data-in */
  437. iscsi_scsi_done ( iscsi, 0, NULL );
  438. }
  439. return 0;
  440. }
  441. /**
  442. * Receive data segment of an iSCSI R2T PDU
  443. *
  444. * @v iscsi iSCSI session
  445. * @v data Received data
  446. * @v len Length of received data
  447. * @v remaining Data remaining after this data
  448. * @ret rc Return status code
  449. */
  450. static int iscsi_rx_r2t ( struct iscsi_session *iscsi,
  451. const void *data __unused, size_t len __unused,
  452. size_t remaining __unused ) {
  453. struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
  454. /* Record transfer parameters and trigger first data-out */
  455. iscsi->ttt = ntohl ( r2t->ttt );
  456. iscsi->transfer_offset = ntohl ( r2t->offset );
  457. iscsi->transfer_len = ntohl ( r2t->len );
  458. iscsi_start_data_out ( iscsi, 0 );
  459. return 0;
  460. }
  461. /**
  462. * Build iSCSI data-out BHS
  463. *
  464. * @v iscsi iSCSI session
  465. * @v datasn Data sequence number within the transfer
  466. *
  467. */
  468. static void iscsi_start_data_out ( struct iscsi_session *iscsi,
  469. unsigned int datasn ) {
  470. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  471. unsigned long offset;
  472. unsigned long remaining;
  473. unsigned long len;
  474. /* We always send 512-byte Data-Out PDUs; this removes the
  475. * need to worry about the target's MaxRecvDataSegmentLength.
  476. */
  477. offset = datasn * 512;
  478. remaining = iscsi->transfer_len - offset;
  479. len = remaining;
  480. if ( len > 512 )
  481. len = 512;
  482. /* Construct BHS and initiate transmission */
  483. iscsi_start_tx ( iscsi );
  484. data_out->opcode = ISCSI_OPCODE_DATA_OUT;
  485. if ( len == remaining )
  486. data_out->flags = ( ISCSI_FLAG_FINAL );
  487. ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
  488. data_out->lun = iscsi->command->lun;
  489. data_out->itt = htonl ( iscsi->itt );
  490. data_out->ttt = htonl ( iscsi->ttt );
  491. data_out->expstatsn = htonl ( iscsi->statsn + 1 );
  492. data_out->datasn = htonl ( datasn );
  493. data_out->offset = htonl ( iscsi->transfer_offset + offset );
  494. DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
  495. iscsi, datasn, len );
  496. }
  497. /**
  498. * Complete iSCSI data-out PDU transmission
  499. *
  500. * @v iscsi iSCSI session
  501. *
  502. */
  503. static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
  504. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  505. /* If we haven't reached the end of the sequence, start
  506. * sending the next data-out PDU.
  507. */
  508. if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
  509. iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
  510. }
  511. /**
  512. * Send iSCSI data-out data segment
  513. *
  514. * @v iscsi iSCSI session
  515. * @ret rc Return status code
  516. */
  517. static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
  518. struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
  519. struct io_buffer *iobuf;
  520. unsigned long offset;
  521. size_t len;
  522. size_t pad_len;
  523. offset = ntohl ( data_out->offset );
  524. len = ISCSI_DATA_LEN ( data_out->lengths );
  525. pad_len = ISCSI_DATA_PAD_LEN ( data_out->lengths );
  526. assert ( iscsi->command != NULL );
  527. assert ( iscsi->command->data_out );
  528. assert ( ( offset + len ) <= iscsi->command->data_out_len );
  529. iobuf = xfer_alloc_iob ( &iscsi->socket, ( len + pad_len ) );
  530. if ( ! iobuf )
  531. return -ENOMEM;
  532. copy_from_user ( iob_put ( iobuf, len ),
  533. iscsi->command->data_out, offset, len );
  534. memset ( iob_put ( iobuf, pad_len ), 0, pad_len );
  535. return xfer_deliver_iob ( &iscsi->socket, iobuf );
  536. }
  537. /**
  538. * Receive data segment of an iSCSI NOP-In
  539. *
  540. * @v iscsi iSCSI session
  541. * @v data Received data
  542. * @v len Length of received data
  543. * @v remaining Data remaining after this data
  544. * @ret rc Return status code
  545. */
  546. static int iscsi_rx_nop_in ( struct iscsi_session *iscsi,
  547. const void *data __unused, size_t len __unused,
  548. size_t remaining __unused ) {
  549. struct iscsi_nop_in *nop_in = &iscsi->rx_bhs.nop_in;
  550. DBGC2 ( iscsi, "iSCSI %p received NOP-In\n", iscsi );
  551. /* We don't currently have the ability to respond to NOP-Ins
  552. * sent as ping requests, but we can happily accept NOP-Ins
  553. * sent merely to update CmdSN.
  554. */
  555. if ( nop_in->ttt != htonl ( ISCSI_TAG_RESERVED ) ) {
  556. DBGC ( iscsi, "iSCSI %p received unsupported NOP-In with TTT "
  557. "%08x\n", iscsi, ntohl ( nop_in->ttt ) );
  558. return -ENOTSUP_NOP_IN;
  559. }
  560. return 0;
  561. }
  562. /****************************************************************************
  563. *
  564. * iSCSI login
  565. *
  566. */
  567. /**
  568. * Build iSCSI login request strings
  569. *
  570. * @v iscsi iSCSI session
  571. *
  572. * These are the initial set of strings sent in the first login
  573. * request PDU. We want the following settings:
  574. *
  575. * HeaderDigest=None
  576. * DataDigest=None
  577. * MaxConnections is irrelevant; we make only one connection anyway [4]
  578. * InitialR2T=Yes [1]
  579. * ImmediateData is irrelevant; we never send immediate data [4]
  580. * MaxRecvDataSegmentLength=8192 (default; we don't care) [3]
  581. * MaxBurstLength=262144 (default; we don't care) [3]
  582. * FirstBurstLength=262144 (default; we don't care)
  583. * DefaultTime2Wait=0 [2]
  584. * DefaultTime2Retain=0 [2]
  585. * MaxOutstandingR2T=1
  586. * DataPDUInOrder=Yes
  587. * DataSequenceInOrder=Yes
  588. * ErrorRecoveryLevel=0
  589. *
  590. * [1] InitialR2T has an OR resolution function, so the target may
  591. * force us to use it. We therefore simplify our logic by always
  592. * using it.
  593. *
  594. * [2] These ensure that we can safely start a new task once we have
  595. * reconnected after a failure, without having to manually tidy up
  596. * after the old one.
  597. *
  598. * [3] We are quite happy to use the RFC-defined default values for
  599. * these parameters, but some targets (notably OpenSolaris)
  600. * incorrectly assume a default value of zero, so we explicitly
  601. * specify the default values.
  602. *
  603. * [4] We are quite happy to use the RFC-defined default values for
  604. * these parameters, but some targets (notably a QNAP TS-639Pro) fail
  605. * unless they are supplied, so we explicitly specify the default
  606. * values.
  607. */
  608. static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
  609. void *data, size_t len ) {
  610. unsigned int used = 0;
  611. const char *auth_method;
  612. if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
  613. /* Default to allowing no authentication */
  614. auth_method = "None";
  615. /* If we have a credential to supply, permit CHAP */
  616. if ( iscsi->initiator_username )
  617. auth_method = "CHAP,None";
  618. /* If we have a credential to check, force CHAP */
  619. if ( iscsi->target_username )
  620. auth_method = "CHAP";
  621. used += ssnprintf ( data + used, len - used,
  622. "InitiatorName=%s%c"
  623. "TargetName=%s%c"
  624. "SessionType=Normal%c"
  625. "AuthMethod=%s%c",
  626. iscsi->initiator_iqn, 0,
  627. iscsi->target_iqn, 0, 0,
  628. auth_method, 0 );
  629. }
  630. if ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_ALGORITHM ) {
  631. used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
  632. }
  633. if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
  634. char buf[ base16_encoded_len ( iscsi->chap.response_len ) + 1 ];
  635. assert ( iscsi->initiator_username != NULL );
  636. base16_encode ( iscsi->chap.response, iscsi->chap.response_len,
  637. buf, sizeof ( buf ) );
  638. used += ssnprintf ( data + used, len - used,
  639. "CHAP_N=%s%cCHAP_R=0x%s%c",
  640. iscsi->initiator_username, 0, buf, 0 );
  641. }
  642. if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_CHALLENGE ) ) {
  643. size_t challenge_len = ( sizeof ( iscsi->chap_challenge ) - 1 );
  644. char buf[ base16_encoded_len ( challenge_len ) + 1 ];
  645. base16_encode ( ( iscsi->chap_challenge + 1 ), challenge_len,
  646. buf, sizeof ( buf ) );
  647. used += ssnprintf ( data + used, len - used,
  648. "CHAP_I=%d%cCHAP_C=0x%s%c",
  649. iscsi->chap_challenge[0], 0, buf, 0 );
  650. }
  651. if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
  652. used += ssnprintf ( data + used, len - used,
  653. "HeaderDigest=None%c"
  654. "DataDigest=None%c"
  655. "MaxConnections=1%c"
  656. "InitialR2T=Yes%c"
  657. "ImmediateData=No%c"
  658. "MaxRecvDataSegmentLength=8192%c"
  659. "MaxBurstLength=262144%c"
  660. "DefaultTime2Wait=0%c"
  661. "DefaultTime2Retain=0%c"
  662. "MaxOutstandingR2T=1%c"
  663. "DataPDUInOrder=Yes%c"
  664. "DataSequenceInOrder=Yes%c"
  665. "ErrorRecoveryLevel=0%c",
  666. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  667. }
  668. return used;
  669. }
  670. /**
  671. * Build iSCSI login request BHS
  672. *
  673. * @v iscsi iSCSI session
  674. */
  675. static void iscsi_start_login ( struct iscsi_session *iscsi ) {
  676. struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
  677. int len;
  678. switch ( iscsi->status & ISCSI_LOGIN_CSG_MASK ) {
  679. case ISCSI_LOGIN_CSG_SECURITY_NEGOTIATION:
  680. DBGC ( iscsi, "iSCSI %p entering security negotiation\n",
  681. iscsi );
  682. break;
  683. case ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION:
  684. DBGC ( iscsi, "iSCSI %p entering operational negotiation\n",
  685. iscsi );
  686. break;
  687. default:
  688. assert ( 0 );
  689. }
  690. /* Construct BHS and initiate transmission */
  691. iscsi_start_tx ( iscsi );
  692. request->opcode = ( ISCSI_OPCODE_LOGIN_REQUEST |
  693. ISCSI_FLAG_IMMEDIATE );
  694. request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
  695. ISCSI_LOGIN_FLAG_TRANSITION );
  696. /* version_max and version_min left as zero */
  697. len = iscsi_build_login_request_strings ( iscsi, NULL, 0 );
  698. ISCSI_SET_LENGTHS ( request->lengths, 0, len );
  699. request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
  700. IANA_EN_FEN_SYSTEMS );
  701. request->isid_iana_qual = htons ( iscsi->isid_iana_qual );
  702. /* tsih left as zero */
  703. request->itt = htonl ( iscsi->itt );
  704. /* cid left as zero */
  705. request->cmdsn = htonl ( iscsi->cmdsn );
  706. request->expstatsn = htonl ( iscsi->statsn + 1 );
  707. }
  708. /**
  709. * Complete iSCSI login request PDU transmission
  710. *
  711. * @v iscsi iSCSI session
  712. *
  713. */
  714. static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
  715. /* Clear any "strings to send" flags */
  716. iscsi->status &= ~ISCSI_STATUS_STRINGS_MASK;
  717. /* Free any dynamically allocated storage used for login */
  718. chap_finish ( &iscsi->chap );
  719. }
  720. /**
  721. * Transmit data segment of an iSCSI login request PDU
  722. *
  723. * @v iscsi iSCSI session
  724. * @ret rc Return status code
  725. *
  726. * For login requests, the data segment consists of the login strings.
  727. */
  728. static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
  729. struct iscsi_bhs_login_request *request = &iscsi->tx_bhs.login_request;
  730. struct io_buffer *iobuf;
  731. size_t len;
  732. size_t pad_len;
  733. len = ISCSI_DATA_LEN ( request->lengths );
  734. pad_len = ISCSI_DATA_PAD_LEN ( request->lengths );
  735. iobuf = xfer_alloc_iob ( &iscsi->socket, ( len + pad_len ) );
  736. if ( ! iobuf )
  737. return -ENOMEM;
  738. iob_put ( iobuf, len );
  739. iscsi_build_login_request_strings ( iscsi, iobuf->data, len );
  740. memset ( iob_put ( iobuf, pad_len ), 0, pad_len );
  741. return xfer_deliver_iob ( &iscsi->socket, iobuf );
  742. }
  743. /**
  744. * Decode large binary value
  745. *
  746. * @v encoded Encoded large binary value
  747. * @v raw Raw data
  748. * @v len Length of data buffer
  749. * @ret len Length of raw data, or negative error
  750. */
  751. static int iscsi_large_binary_decode ( const char *encoded, uint8_t *raw,
  752. size_t len ) {
  753. /* Check for initial '0x' or '0b' and decode as appropriate */
  754. if ( *(encoded++) == '0' ) {
  755. switch ( tolower ( *(encoded++) ) ) {
  756. case 'x' :
  757. return base16_decode ( encoded, raw, len );
  758. case 'b' :
  759. return base64_decode ( encoded, raw, len );
  760. }
  761. }
  762. return -EPROTO_INVALID_LARGE_BINARY;
  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[ strlen ( value ) ]; /* Decoding never expands data */
  873. unsigned int i;
  874. int len;
  875. int rc;
  876. /* Process challenge */
  877. len = iscsi_large_binary_decode ( value, buf, sizeof ( buf ) );
  878. if ( len < 0 ) {
  879. rc = len;
  880. DBGC ( iscsi, "iSCSI %p invalid CHAP challenge \"%s\": %s\n",
  881. iscsi, value, strerror ( rc ) );
  882. return rc;
  883. }
  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[ strlen ( value ) ]; /* Decoding never expands data */
  935. int 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. len = iscsi_large_binary_decode ( value, buf, sizeof ( buf ) );
  954. if ( len < 0 ) {
  955. rc = len;
  956. DBGC ( iscsi, "iSCSI %p invalid CHAP response \"%s\": %s\n",
  957. iscsi, value, strerror ( rc ) );
  958. return rc;
  959. }
  960. /* Check CHAP response */
  961. if ( len != ( int ) 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. const struct setting initiator_iqn_setting __setting ( SETTING_SANBOOT_EXTRA,
  1662. initiator-iqn ) = {
  1663. .name = "initiator-iqn",
  1664. .description = "iSCSI initiator name",
  1665. .tag = DHCP_ISCSI_INITIATOR_IQN,
  1666. .type = &setting_type_string,
  1667. };
  1668. /** iSCSI reverse username setting */
  1669. const struct setting reverse_username_setting __setting ( SETTING_AUTH_EXTRA,
  1670. reverse-username ) = {
  1671. .name = "reverse-username",
  1672. .description = "Reverse user name",
  1673. .tag = DHCP_EB_REVERSE_USERNAME,
  1674. .type = &setting_type_string,
  1675. };
  1676. /** iSCSI reverse password setting */
  1677. const struct setting reverse_password_setting __setting ( SETTING_AUTH_EXTRA,
  1678. reverse-password ) = {
  1679. .name = "reverse-password",
  1680. .description = "Reverse password",
  1681. .tag = DHCP_EB_REVERSE_PASSWORD,
  1682. .type = &setting_type_string,
  1683. };
  1684. /**
  1685. * Parse iSCSI root path
  1686. *
  1687. * @v iscsi iSCSI session
  1688. * @v root_path iSCSI root path (as per RFC4173)
  1689. * @ret rc Return status code
  1690. */
  1691. static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
  1692. const char *root_path ) {
  1693. char rp_copy[ strlen ( root_path ) + 1 ];
  1694. char *rp_comp[NUM_RP_COMPONENTS];
  1695. char *rp = rp_copy;
  1696. int i = 0;
  1697. int rc;
  1698. /* Split root path into component parts */
  1699. strcpy ( rp_copy, root_path );
  1700. while ( 1 ) {
  1701. rp_comp[i++] = rp;
  1702. if ( i == NUM_RP_COMPONENTS )
  1703. break;
  1704. for ( ; *rp != ':' ; rp++ ) {
  1705. if ( ! *rp ) {
  1706. DBGC ( iscsi, "iSCSI %p root path \"%s\" "
  1707. "too short\n", iscsi, root_path );
  1708. return -EINVAL_ROOT_PATH_TOO_SHORT;
  1709. }
  1710. }
  1711. *(rp++) = '\0';
  1712. }
  1713. /* Use root path components to configure iSCSI session */
  1714. iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
  1715. if ( ! iscsi->target_address )
  1716. return -ENOMEM;
  1717. iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
  1718. if ( ! iscsi->target_port )
  1719. iscsi->target_port = ISCSI_PORT;
  1720. if ( ( rc = scsi_parse_lun ( rp_comp[RP_LUN], &iscsi->lun ) ) != 0 ) {
  1721. DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
  1722. iscsi, rp_comp[RP_LUN] );
  1723. return rc;
  1724. }
  1725. iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
  1726. if ( ! iscsi->target_iqn )
  1727. return -ENOMEM;
  1728. return 0;
  1729. }
  1730. /**
  1731. * Fetch iSCSI settings
  1732. *
  1733. * @v iscsi iSCSI session
  1734. * @ret rc Return status code
  1735. */
  1736. static int iscsi_fetch_settings ( struct iscsi_session *iscsi ) {
  1737. char *hostname;
  1738. union uuid uuid;
  1739. int len;
  1740. /* Fetch relevant settings. Don't worry about freeing on
  1741. * error, since iscsi_free() will take care of that anyway.
  1742. */
  1743. fetch_string_setting_copy ( NULL, &username_setting,
  1744. &iscsi->initiator_username );
  1745. fetch_string_setting_copy ( NULL, &password_setting,
  1746. &iscsi->initiator_password );
  1747. fetch_string_setting_copy ( NULL, &reverse_username_setting,
  1748. &iscsi->target_username );
  1749. fetch_string_setting_copy ( NULL, &reverse_password_setting,
  1750. &iscsi->target_password );
  1751. /* Use explicit initiator IQN if provided */
  1752. fetch_string_setting_copy ( NULL, &initiator_iqn_setting,
  1753. &iscsi->initiator_iqn );
  1754. if ( iscsi->initiator_iqn )
  1755. return 0;
  1756. /* Otherwise, try to construct an initiator IQN from the hostname */
  1757. fetch_string_setting_copy ( NULL, &hostname_setting, &hostname );
  1758. if ( hostname ) {
  1759. len = asprintf ( &iscsi->initiator_iqn,
  1760. ISCSI_DEFAULT_IQN_PREFIX ":%s", hostname );
  1761. free ( hostname );
  1762. if ( len < 0 ) {
  1763. DBGC ( iscsi, "iSCSI %p could not allocate initiator "
  1764. "IQN\n", iscsi );
  1765. return -ENOMEM;
  1766. }
  1767. assert ( iscsi->initiator_iqn );
  1768. return 0;
  1769. }
  1770. /* Otherwise, try to construct an initiator IQN from the UUID */
  1771. if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting, &uuid ) ) < 0 ) {
  1772. DBGC ( iscsi, "iSCSI %p has no suitable initiator IQN\n",
  1773. iscsi );
  1774. return -EINVAL_NO_INITIATOR_IQN;
  1775. }
  1776. if ( ( len = asprintf ( &iscsi->initiator_iqn,
  1777. ISCSI_DEFAULT_IQN_PREFIX ":%s",
  1778. uuid_ntoa ( &uuid ) ) ) < 0 ) {
  1779. DBGC ( iscsi, "iSCSI %p could not allocate initiator IQN\n",
  1780. iscsi );
  1781. return -ENOMEM;
  1782. }
  1783. assert ( iscsi->initiator_iqn );
  1784. return 0;
  1785. }
  1786. /**
  1787. * Check iSCSI authentication details
  1788. *
  1789. * @v iscsi iSCSI session
  1790. * @ret rc Return status code
  1791. */
  1792. static int iscsi_check_auth ( struct iscsi_session *iscsi ) {
  1793. /* Check for invalid authentication combinations */
  1794. if ( ( /* Initiator username without password (or vice-versa) */
  1795. ( !! iscsi->initiator_username ) ^
  1796. ( !! iscsi->initiator_password ) ) ||
  1797. ( /* Target username without password (or vice-versa) */
  1798. ( !! iscsi->target_username ) ^
  1799. ( !! iscsi->target_password ) ) ||
  1800. ( /* Target (reverse) without initiator (forward) */
  1801. ( iscsi->target_username &&
  1802. ( ! iscsi->initiator_username ) ) ) ) {
  1803. DBGC ( iscsi, "iSCSI %p invalid credentials: initiator "
  1804. "%sname,%spw, target %sname,%spw\n", iscsi,
  1805. ( iscsi->initiator_username ? "" : "no " ),
  1806. ( iscsi->initiator_password ? "" : "no " ),
  1807. ( iscsi->target_username ? "" : "no " ),
  1808. ( iscsi->target_password ? "" : "no " ) );
  1809. return -EINVAL_BAD_CREDENTIAL_MIX;
  1810. }
  1811. return 0;
  1812. }
  1813. /**
  1814. * Open iSCSI URI
  1815. *
  1816. * @v parent Parent interface
  1817. * @v uri URI
  1818. * @ret rc Return status code
  1819. */
  1820. static int iscsi_open ( struct interface *parent, struct uri *uri ) {
  1821. struct iscsi_session *iscsi;
  1822. int rc;
  1823. /* Sanity check */
  1824. if ( ! uri->opaque ) {
  1825. rc = -EINVAL_NO_ROOT_PATH;
  1826. goto err_sanity_uri;
  1827. }
  1828. /* Allocate and initialise structure */
  1829. iscsi = zalloc ( sizeof ( *iscsi ) );
  1830. if ( ! iscsi ) {
  1831. rc = -ENOMEM;
  1832. goto err_zalloc;
  1833. }
  1834. ref_init ( &iscsi->refcnt, iscsi_free );
  1835. intf_init ( &iscsi->control, &iscsi_control_desc, &iscsi->refcnt );
  1836. intf_init ( &iscsi->data, &iscsi_data_desc, &iscsi->refcnt );
  1837. intf_init ( &iscsi->socket, &iscsi_socket_desc, &iscsi->refcnt );
  1838. process_init_stopped ( &iscsi->process, &iscsi_process_desc,
  1839. &iscsi->refcnt );
  1840. /* Parse root path */
  1841. if ( ( rc = iscsi_parse_root_path ( iscsi, uri->opaque ) ) != 0 )
  1842. goto err_parse_root_path;
  1843. /* Set fields not specified by root path */
  1844. if ( ( rc = iscsi_fetch_settings ( iscsi ) ) != 0 )
  1845. goto err_fetch_settings;
  1846. /* Validate authentication */
  1847. if ( ( rc = iscsi_check_auth ( iscsi ) ) != 0 )
  1848. goto err_check_auth;
  1849. /* Sanity checks */
  1850. if ( ! iscsi->target_address ) {
  1851. DBGC ( iscsi, "iSCSI %p does not yet support discovery\n",
  1852. iscsi );
  1853. rc = -ENOTSUP_DISCOVERY;
  1854. goto err_sanity_address;
  1855. }
  1856. if ( ! iscsi->target_iqn ) {
  1857. DBGC ( iscsi, "iSCSI %p no target address supplied in %s\n",
  1858. iscsi, uri->opaque );
  1859. rc = -EINVAL_NO_TARGET_IQN;
  1860. goto err_sanity_iqn;
  1861. }
  1862. DBGC ( iscsi, "iSCSI %p initiator %s\n",iscsi, iscsi->initiator_iqn );
  1863. DBGC ( iscsi, "iSCSI %p target %s %s\n",
  1864. iscsi, iscsi->target_address, iscsi->target_iqn );
  1865. /* Open socket */
  1866. if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 )
  1867. goto err_open_connection;
  1868. /* Attach SCSI device to parent interface */
  1869. if ( ( rc = scsi_open ( parent, &iscsi->control,
  1870. &iscsi->lun ) ) != 0 ) {
  1871. DBGC ( iscsi, "iSCSI %p could not create SCSI device: %s\n",
  1872. iscsi, strerror ( rc ) );
  1873. goto err_scsi_open;
  1874. }
  1875. /* Mortalise self, and return */
  1876. ref_put ( &iscsi->refcnt );
  1877. return 0;
  1878. err_scsi_open:
  1879. err_open_connection:
  1880. err_sanity_iqn:
  1881. err_sanity_address:
  1882. err_check_auth:
  1883. err_fetch_settings:
  1884. err_parse_root_path:
  1885. iscsi_close ( iscsi, rc );
  1886. ref_put ( &iscsi->refcnt );
  1887. err_zalloc:
  1888. err_sanity_uri:
  1889. return rc;
  1890. }
  1891. /** iSCSI URI opener */
  1892. struct uri_opener iscsi_uri_opener __uri_opener = {
  1893. .scheme = "iscsi",
  1894. .open = iscsi_open,
  1895. };