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