iPXE
iscsi.c
Go to the documentation of this file.
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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stddef.h>
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <ctype.h>
32#include <errno.h>
33#include <assert.h>
34#include <byteswap.h>
35#include <ipxe/vsprintf.h>
36#include <ipxe/socket.h>
37#include <ipxe/iobuf.h>
38#include <ipxe/uri.h>
39#include <ipxe/xfer.h>
40#include <ipxe/open.h>
41#include <ipxe/scsi.h>
42#include <ipxe/process.h>
43#include <ipxe/tcpip.h>
44#include <ipxe/settings.h>
45#include <ipxe/features.h>
46#include <ipxe/base16.h>
47#include <ipxe/base64.h>
48#include <ipxe/ibft.h>
49#include <ipxe/blockdev.h>
50#include <ipxe/efi/efi_path.h>
51#include <ipxe/iscsi.h>
52
53/** @file
54 *
55 * iSCSI protocol
56 *
57 */
58
60
61/* Disambiguate the various error causes */
62#define EACCES_INCORRECT_TARGET_USERNAME \
63 __einfo_error ( EINFO_EACCES_INCORRECT_TARGET_USERNAME )
64#define EINFO_EACCES_INCORRECT_TARGET_USERNAME \
65 __einfo_uniqify ( EINFO_EACCES, 0x01, "Incorrect target username" )
66#define EACCES_INCORRECT_TARGET_PASSWORD \
67 __einfo_error ( EINFO_EACCES_INCORRECT_TARGET_PASSWORD )
68#define EINFO_EACCES_INCORRECT_TARGET_PASSWORD \
69 __einfo_uniqify ( EINFO_EACCES, 0x02, "Incorrect target password" )
70#define EINVAL_ROOT_PATH_TOO_SHORT \
71 __einfo_error ( EINFO_EINVAL_ROOT_PATH_TOO_SHORT )
72#define EINFO_EINVAL_ROOT_PATH_TOO_SHORT \
73 __einfo_uniqify ( EINFO_EINVAL, 0x01, "Root path too short" )
74#define EINVAL_BAD_CREDENTIAL_MIX \
75 __einfo_error ( EINFO_EINVAL_BAD_CREDENTIAL_MIX )
76#define EINFO_EINVAL_BAD_CREDENTIAL_MIX \
77 __einfo_uniqify ( EINFO_EINVAL, 0x02, "Bad credential mix" )
78#define EINVAL_NO_ROOT_PATH \
79 __einfo_error ( EINFO_EINVAL_NO_ROOT_PATH )
80#define EINFO_EINVAL_NO_ROOT_PATH \
81 __einfo_uniqify ( EINFO_EINVAL, 0x03, "No root path" )
82#define EINVAL_NO_TARGET_IQN \
83 __einfo_error ( EINFO_EINVAL_NO_TARGET_IQN )
84#define EINFO_EINVAL_NO_TARGET_IQN \
85 __einfo_uniqify ( EINFO_EINVAL, 0x04, "No target IQN" )
86#define EINVAL_NO_INITIATOR_IQN \
87 __einfo_error ( EINFO_EINVAL_NO_INITIATOR_IQN )
88#define EINFO_EINVAL_NO_INITIATOR_IQN \
89 __einfo_uniqify ( EINFO_EINVAL, 0x05, "No initiator IQN" )
90#define EINVAL_MAXBURSTLENGTH \
91 __einfo_error ( EINFO_EINVAL_MAXBURSTLENGTH )
92#define EINFO_EINVAL_MAXBURSTLENGTH \
93 __einfo_uniqify ( EINFO_EINVAL, 0x06, "Invalid MaxBurstLength" )
94#define EIO_TARGET_UNAVAILABLE \
95 __einfo_error ( EINFO_EIO_TARGET_UNAVAILABLE )
96#define EINFO_EIO_TARGET_UNAVAILABLE \
97 __einfo_uniqify ( EINFO_EIO, 0x01, "Target not currently operational" )
98#define EIO_TARGET_NO_RESOURCES \
99 __einfo_error ( EINFO_EIO_TARGET_NO_RESOURCES )
100#define EINFO_EIO_TARGET_NO_RESOURCES \
101 __einfo_uniqify ( EINFO_EIO, 0x02, "Target out of resources" )
102#define ENOTSUP_INITIATOR_STATUS \
103 __einfo_error ( EINFO_ENOTSUP_INITIATOR_STATUS )
104#define EINFO_ENOTSUP_INITIATOR_STATUS \
105 __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported initiator status" )
106#define ENOTSUP_OPCODE \
107 __einfo_error ( EINFO_ENOTSUP_OPCODE )
108#define EINFO_ENOTSUP_OPCODE \
109 __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported opcode" )
110#define ENOTSUP_DISCOVERY \
111 __einfo_error ( EINFO_ENOTSUP_DISCOVERY )
112#define EINFO_ENOTSUP_DISCOVERY \
113 __einfo_uniqify ( EINFO_ENOTSUP, 0x03, "Discovery not supported" )
114#define ENOTSUP_TARGET_STATUS \
115 __einfo_error ( EINFO_ENOTSUP_TARGET_STATUS )
116#define EINFO_ENOTSUP_TARGET_STATUS \
117 __einfo_uniqify ( EINFO_ENOTSUP, 0x04, "Unsupported target status" )
118#define EPERM_INITIATOR_AUTHENTICATION \
119 __einfo_error ( EINFO_EPERM_INITIATOR_AUTHENTICATION )
120#define EINFO_EPERM_INITIATOR_AUTHENTICATION \
121 __einfo_uniqify ( EINFO_EPERM, 0x01, "Initiator authentication failed" )
122#define EPERM_INITIATOR_AUTHORISATION \
123 __einfo_error ( EINFO_EPERM_INITIATOR_AUTHORISATION )
124#define EINFO_EPERM_INITIATOR_AUTHORISATION \
125 __einfo_uniqify ( EINFO_EPERM, 0x02, "Initiator not authorised" )
126#define EPROTO_INVALID_CHAP_ALGORITHM \
127 __einfo_error ( EINFO_EPROTO_INVALID_CHAP_ALGORITHM )
128#define EINFO_EPROTO_INVALID_CHAP_ALGORITHM \
129 __einfo_uniqify ( EINFO_EPROTO, 0x01, "Invalid CHAP algorithm" )
130#define EPROTO_INVALID_CHAP_IDENTIFIER \
131 __einfo_error ( EINFO_EPROTO_INVALID_CHAP_IDENTIFIER )
132#define EINFO_EPROTO_INVALID_CHAP_IDENTIFIER \
133 __einfo_uniqify ( EINFO_EPROTO, 0x02, "Invalid CHAP identifier" )
134#define EPROTO_INVALID_LARGE_BINARY \
135 __einfo_error ( EINFO_EPROTO_INVALID_LARGE_BINARY )
136#define EINFO_EPROTO_INVALID_LARGE_BINARY \
137 __einfo_uniqify ( EINFO_EPROTO, 0x03, "Invalid large binary value" )
138#define EPROTO_INVALID_CHAP_RESPONSE \
139 __einfo_error ( EINFO_EPROTO_INVALID_CHAP_RESPONSE )
140#define EINFO_EPROTO_INVALID_CHAP_RESPONSE \
141 __einfo_uniqify ( EINFO_EPROTO, 0x04, "Invalid CHAP response" )
142#define EPROTO_INVALID_KEY_VALUE_PAIR \
143 __einfo_error ( EINFO_EPROTO_INVALID_KEY_VALUE_PAIR )
144#define EINFO_EPROTO_INVALID_KEY_VALUE_PAIR \
145 __einfo_uniqify ( EINFO_EPROTO, 0x05, "Invalid key/value pair" )
146#define EPROTO_VALUE_REJECTED \
147 __einfo_error ( EINFO_EPROTO_VALUE_REJECTED )
148#define EINFO_EPROTO_VALUE_REJECTED \
149 __einfo_uniqify ( EINFO_EPROTO, 0x06, "Parameter rejected" )
150
151static void iscsi_start_tx ( struct iscsi_session *iscsi );
152static void iscsi_start_login ( struct iscsi_session *iscsi );
153static void iscsi_start_data_out ( struct iscsi_session *iscsi,
154 unsigned int datasn );
155
156/**
157 * Finish receiving PDU data into buffer
158 *
159 * @v iscsi iSCSI session
160 */
161static void iscsi_rx_buffered_data_done ( struct iscsi_session *iscsi ) {
162 free ( iscsi->rx_buffer );
163 iscsi->rx_buffer = NULL;
164}
165
166/**
167 * Receive PDU data into buffer
168 *
169 * @v iscsi iSCSI session
170 * @v data Data to receive
171 * @v len Length of data
172 * @ret rc Return status code
173 *
174 * This can be used when the RX PDU type handler wishes to buffer up
175 * all received data and process the PDU as a single unit. The caller
176 * is repsonsible for calling iscsi_rx_buffered_data_done() after
177 * processing the data.
178 */
179static int iscsi_rx_buffered_data ( struct iscsi_session *iscsi,
180 const void *data, size_t len ) {
181
182 /* Allocate buffer on first call */
183 if ( ! iscsi->rx_buffer ) {
184 iscsi->rx_buffer = malloc ( iscsi->rx_len );
185 if ( ! iscsi->rx_buffer )
186 return -ENOMEM;
187 }
188
189 /* Copy data to buffer */
190 assert ( ( iscsi->rx_offset + len ) <= iscsi->rx_len );
191 memcpy ( ( iscsi->rx_buffer + iscsi->rx_offset ), data, len );
192
193 return 0;
194}
195
196/**
197 * Free iSCSI session
198 *
199 * @v refcnt Reference counter
200 */
201static void iscsi_free ( struct refcnt *refcnt ) {
202 struct iscsi_session *iscsi =
204
205 free ( iscsi->initiator_iqn );
206 free ( iscsi->target_address );
207 free ( iscsi->target_iqn );
208 free ( iscsi->initiator_username );
209 free ( iscsi->initiator_password );
210 free ( iscsi->target_username );
211 free ( iscsi->target_password );
212 chap_finish ( &iscsi->chap );
214 free ( iscsi->command );
215 free ( iscsi );
216}
217
218/**
219 * Shut down iSCSI interface
220 *
221 * @v iscsi iSCSI session
222 * @v rc Reason for close
223 */
224static void iscsi_close ( struct iscsi_session *iscsi, int rc ) {
225
226 /* A TCP graceful close is still an error from our point of view */
227 if ( rc == 0 )
228 rc = -ECONNRESET;
229
230 DBGC ( iscsi, "iSCSI %p closed: %s\n", iscsi, strerror ( rc ) );
231
232 /* Stop transmission process */
233 process_del ( &iscsi->process );
234
235 /* Shut down interfaces */
236 intfs_shutdown ( rc, &iscsi->socket, &iscsi->control, &iscsi->data,
237 NULL );
238}
239
240/**
241 * Assign new iSCSI initiator task tag
242 *
243 * @v iscsi iSCSI session
244 */
245static void iscsi_new_itt ( struct iscsi_session *iscsi ) {
246 static uint16_t itt_idx;
247
248 iscsi->itt = ( ISCSI_TAG_MAGIC | (++itt_idx) );
249}
250
251/**
252 * Open iSCSI transport-layer connection
253 *
254 * @v iscsi iSCSI session
255 * @ret rc Return status code
256 */
257static int iscsi_open_connection ( struct iscsi_session *iscsi ) {
258 struct sockaddr_tcpip target;
259 int rc;
260
261 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
262 assert ( iscsi->rx_state == ISCSI_RX_BHS );
263 assert ( iscsi->rx_offset == 0 );
264
265 /* Open socket */
266 memset ( &target, 0, sizeof ( target ) );
267 target.st_port = htons ( iscsi->target_port );
268 if ( ( rc = xfer_open_named_socket ( &iscsi->socket, SOCK_STREAM,
269 ( struct sockaddr * ) &target,
270 iscsi->target_address,
271 NULL ) ) != 0 ) {
272 DBGC ( iscsi, "iSCSI %p could not open socket: %s\n",
273 iscsi, strerror ( rc ) );
274 return rc;
275 }
276
277 /* Enter security negotiation phase */
280 if ( iscsi->target_username )
282
283 /* Assign new ISID */
284 iscsi->isid_iana_qual = ( random() & 0xffff );
285
286 /* Assign fresh initiator task tag */
287 iscsi_new_itt ( iscsi );
288
289 /* Set default operational parameters */
291
292 /* Initiate login */
293 iscsi_start_login ( iscsi );
294
295 return 0;
296}
297
298/**
299 * Close iSCSI transport-layer connection
300 *
301 * @v iscsi iSCSI session
302 * @v rc Reason for close
303 *
304 * Closes the transport-layer connection and resets the session state
305 * ready to attempt a fresh login.
306 */
307static void iscsi_close_connection ( struct iscsi_session *iscsi, int rc ) {
308
309 /* Close all data transfer interfaces */
310 intf_restart ( &iscsi->socket, rc );
311
312 /* Clear connection status */
313 iscsi->status = 0;
314
315 /* Reset TX and RX state machines */
316 iscsi->tx_state = ISCSI_TX_IDLE;
317 iscsi->rx_state = ISCSI_RX_BHS;
318 iscsi->rx_offset = 0;
319
320 /* Free any temporary dynamically allocated memory */
321 chap_finish ( &iscsi->chap );
323}
324
325/**
326 * Mark iSCSI SCSI operation as complete
327 *
328 * @v iscsi iSCSI session
329 * @v rc Return status code
330 * @v rsp SCSI response, if any
331 *
332 * Note that iscsi_scsi_done() will not close the connection, and must
333 * therefore be called only when the internal state machines are in an
334 * appropriate state, otherwise bad things may happen on the next call
335 * to iscsi_scsi_command(). The general rule is to call
336 * iscsi_scsi_done() only at the end of receiving a PDU; at this point
337 * the TX and RX engines should both be idle.
338 */
339static void iscsi_scsi_done ( struct iscsi_session *iscsi, int rc,
340 struct scsi_rsp *rsp ) {
341 uint32_t itt = iscsi->itt;
342
343 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
344
345 /* Clear command */
346 free ( iscsi->command );
347 iscsi->command = NULL;
348
349 /* Send SCSI response, if any */
350 if ( rsp )
351 scsi_response ( &iscsi->data, rsp );
352
353 /* Close SCSI command, if this is still the same command. (It
354 * is possible that the command interface has already been
355 * closed as a result of the SCSI response we sent.)
356 */
357 if ( iscsi->itt == itt )
358 intf_restart ( &iscsi->data, rc );
359}
360
361/****************************************************************************
362 *
363 * iSCSI SCSI command issuing
364 *
365 */
366
367/**
368 * Build iSCSI SCSI command BHS
369 *
370 * @v iscsi iSCSI session
371 *
372 * We don't currently support bidirectional commands (i.e. with both
373 * Data-In and Data-Out segments); these would require providing code
374 * to generate an AHS, and there doesn't seem to be any need for it at
375 * the moment.
376 */
377static void iscsi_start_command ( struct iscsi_session *iscsi ) {
379
380 assert ( ! ( iscsi->command->data_in.len &&
381 iscsi->command->data_out.len ) );
382
383 /* Construct BHS and initiate transmission */
384 iscsi_start_tx ( iscsi );
386 command->flags = ( ISCSI_FLAG_FINAL |
388 if ( iscsi->command->data_in.len )
390 if ( iscsi->command->data_out.len )
392 /* lengths left as zero */
393 memcpy ( &command->lun, &iscsi->command->lun,
394 sizeof ( command->lun ) );
395 command->itt = htonl ( iscsi->itt );
396 command->exp_len = htonl ( iscsi->command->data_in.len |
397 iscsi->command->data_out.len );
398 command->cmdsn = htonl ( iscsi->cmdsn );
399 command->expstatsn = htonl ( iscsi->statsn + 1 );
400 memcpy ( &command->cdb, &iscsi->command->cdb, sizeof ( command->cdb ));
401 DBGC2 ( iscsi, "iSCSI %p start " SCSI_CDB_FORMAT " %s %#zx\n",
402 iscsi, SCSI_CDB_DATA ( command->cdb ),
403 ( iscsi->command->data_in.len ? "in" : "out" ),
404 ( iscsi->command->data_in.len +
405 iscsi->command->data_out.len ) );
406}
407
408/**
409 * Receive data segment of an iSCSI SCSI response PDU
410 *
411 * @v iscsi iSCSI session
412 * @v data Received data
413 * @v len Length of received data
414 * @v remaining Data remaining after this data
415 * @ret rc Return status code
416 */
417static int iscsi_rx_scsi_response ( struct iscsi_session *iscsi,
418 const void *data, size_t len,
419 size_t remaining ) {
421 = &iscsi->rx_bhs.scsi_response;
422 struct scsi_rsp rsp;
423 uint32_t residual_count;
424 size_t data_len;
425 int rc;
426
427 /* Buffer up the PDU data */
428 if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
429 DBGC ( iscsi, "iSCSI %p could not buffer SCSI response: %s\n",
430 iscsi, strerror ( rc ) );
431 return rc;
432 }
433 if ( remaining )
434 return 0;
435
436 /* Parse SCSI response and discard buffer */
437 memset ( &rsp, 0, sizeof ( rsp ) );
438 rsp.status = response->status;
439 residual_count = ntohl ( response->residual_count );
440 if ( response->flags & ISCSI_DATA_FLAG_OVERFLOW ) {
441 rsp.overrun = residual_count;
442 } else if ( response->flags & ISCSI_DATA_FLAG_UNDERFLOW ) {
443 rsp.overrun = -(residual_count);
444 }
445 data_len = ISCSI_DATA_LEN ( response->lengths );
446 if ( data_len >= 2 ) {
447 scsi_parse_sense ( ( iscsi->rx_buffer + 2 ), ( data_len - 2 ),
448 &rsp.sense );
449 }
451
452 /* Check for errors */
453 if ( response->response != ISCSI_RESPONSE_COMMAND_COMPLETE )
454 return -EIO;
455
456 /* Mark as completed */
457 iscsi_scsi_done ( iscsi, 0, &rsp );
458 return 0;
459}
460
461/**
462 * Receive data segment of an iSCSI data-in PDU
463 *
464 * @v iscsi iSCSI session
465 * @v data Received data
466 * @v len Length of received data
467 * @v remaining Data remaining after this data
468 * @ret rc Return status code
469 */
470static int iscsi_rx_data_in ( struct iscsi_session *iscsi,
471 const void *data, size_t len,
472 size_t remaining ) {
473 struct iscsi_bhs_data_in *data_in = &iscsi->rx_bhs.data_in;
474 unsigned long offset;
475 int rc;
476
477 /* Copy data to data-in buffer */
478 offset = ntohl ( data_in->offset ) + iscsi->rx_offset;
479 assert ( iscsi->command != NULL );
480 if ( ( rc = xferbuf_write ( &iscsi->command->data_in, offset,
481 data, len ) ) != 0 )
482 return rc;
483
484 /* Wait for whole SCSI response to arrive */
485 if ( remaining )
486 return 0;
487
488 /* Mark as completed if status is present */
489 if ( data_in->flags & ISCSI_DATA_FLAG_STATUS ) {
490 assert ( ( offset + len ) == iscsi->command->data_in.len );
491 assert ( data_in->flags & ISCSI_FLAG_FINAL );
492 /* iSCSI cannot return an error status via a data-in */
493 iscsi_scsi_done ( iscsi, 0, NULL );
494 }
495
496 return 0;
497}
498
499/**
500 * Receive data segment of an iSCSI R2T PDU
501 *
502 * @v iscsi iSCSI session
503 * @v data Received data
504 * @v len Length of received data
505 * @v remaining Data remaining after this data
506 * @ret rc Return status code
507 */
508static int iscsi_rx_r2t ( struct iscsi_session *iscsi,
509 const void *data __unused, size_t len __unused,
510 size_t remaining __unused ) {
511 struct iscsi_bhs_r2t *r2t = &iscsi->rx_bhs.r2t;
512
513 /* Record transfer parameters and trigger first data-out */
514 iscsi->ttt = ntohl ( r2t->ttt );
515 iscsi->transfer_offset = ntohl ( r2t->offset );
516 iscsi->transfer_len = ntohl ( r2t->len );
517 iscsi_start_data_out ( iscsi, 0 );
518
519 return 0;
520}
521
522/**
523 * Build iSCSI data-out BHS
524 *
525 * @v iscsi iSCSI session
526 * @v datasn Data sequence number within the transfer
527 *
528 */
529static void iscsi_start_data_out ( struct iscsi_session *iscsi,
530 unsigned int datasn ) {
531 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
532 unsigned long offset;
533 unsigned long remaining;
534 unsigned long len;
535
536 /* We always send 512-byte Data-Out PDUs; this removes the
537 * need to worry about the target's MaxRecvDataSegmentLength.
538 */
539 offset = datasn * 512;
540 remaining = iscsi->transfer_len - offset;
541 len = remaining;
542 if ( len > 512 )
543 len = 512;
544
545 /* Construct BHS and initiate transmission */
546 iscsi_start_tx ( iscsi );
547 data_out->opcode = ISCSI_OPCODE_DATA_OUT;
548 if ( len == remaining )
549 data_out->flags = ( ISCSI_FLAG_FINAL );
550 ISCSI_SET_LENGTHS ( data_out->lengths, 0, len );
551 data_out->lun = iscsi->command->lun;
552 data_out->itt = htonl ( iscsi->itt );
553 data_out->ttt = htonl ( iscsi->ttt );
554 data_out->expstatsn = htonl ( iscsi->statsn + 1 );
555 data_out->datasn = htonl ( datasn );
556 data_out->offset = htonl ( iscsi->transfer_offset + offset );
557 DBGC ( iscsi, "iSCSI %p start data out DataSN %#x len %#lx\n",
558 iscsi, datasn, len );
559}
560
561/**
562 * Complete iSCSI data-out PDU transmission
563 *
564 * @v iscsi iSCSI session
565 *
566 */
567static void iscsi_data_out_done ( struct iscsi_session *iscsi ) {
568 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
569
570 /* If we haven't reached the end of the sequence, start
571 * sending the next data-out PDU.
572 */
573 if ( ! ( data_out->flags & ISCSI_FLAG_FINAL ) )
574 iscsi_start_data_out ( iscsi, ntohl ( data_out->datasn ) + 1 );
575}
576
577/**
578 * Send iSCSI data-out data segment
579 *
580 * @v iscsi iSCSI session
581 * @ret rc Return status code
582 */
583static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
584 struct iscsi_bhs_data_out *data_out = &iscsi->tx_bhs.data_out;
585 struct io_buffer *iobuf;
586 unsigned long offset;
587 size_t len;
588 size_t pad_len;
589 int rc;
590
591 /* Calculate offset and lengths */
592 offset = ntohl ( data_out->offset );
593 len = ISCSI_DATA_LEN ( data_out->lengths );
594 pad_len = ISCSI_DATA_PAD_LEN ( data_out->lengths );
595
596 /* Allocate I/O buffer */
597 iobuf = xfer_alloc_iob ( &iscsi->socket, ( len + pad_len ) );
598 if ( ! iobuf ) {
599 rc = -ENOMEM;
600 goto err_alloc;
601 }
602
603 /* Copy data to I/O buffer */
604 assert ( iscsi->command != NULL );
605 if ( ( rc = xferbuf_read ( &iscsi->command->data_out, offset,
606 iob_put ( iobuf, len ), len ) ) != 0 ) {
607 goto err_read;
608 }
609 memset ( iob_put ( iobuf, pad_len ), 0, pad_len );
610
611 /* Deliver I/O buffer */
612 if ( ( rc = xfer_deliver_iob ( &iscsi->socket,
613 iob_disown ( iobuf ) ) ) != 0 ) {
614 goto err_deliver;
615 }
616
617 return 0;
618
619 err_deliver:
620 err_read:
621 free_iob ( iobuf );
622 err_alloc:
623 return rc;
624}
625
626/**
627 * Receive data segment of an iSCSI NOP-In
628 *
629 * @v iscsi iSCSI session
630 * @v data Received data
631 * @v len Length of received data
632 * @v remaining Data remaining after this data
633 * @ret rc Return status code
634 */
635static int iscsi_rx_nop_in ( struct iscsi_session *iscsi,
636 const void *data __unused, size_t len __unused,
637 size_t remaining __unused ) {
638 struct iscsi_nop_in *nop_in = &iscsi->rx_bhs.nop_in;
639
640 DBGC2 ( iscsi, "iSCSI %p received NOP-In\n", iscsi );
641
642 /* We don't currently have the ability to respond to NOP-Ins
643 * sent as ping requests, but we can happily accept NOP-Ins
644 * sent merely to update CmdSN.
645 */
646 if ( nop_in->ttt == htonl ( ISCSI_TAG_RESERVED ) )
647 return 0;
648
649 /* Ignore any other NOP-Ins. The target may eventually
650 * disconnect us for failing to respond, but this minimises
651 * unnecessary connection closures.
652 */
653 DBGC ( iscsi, "iSCSI %p received unsupported NOP-In with TTT %08x\n",
654 iscsi, ntohl ( nop_in->ttt ) );
655 return 0;
656}
657
658/****************************************************************************
659 *
660 * iSCSI login
661 *
662 */
663
664/**
665 * Build iSCSI login request strings
666 *
667 * @v iscsi iSCSI session
668 *
669 * These are the initial set of strings sent in the first login
670 * request PDU. We want the following settings:
671 *
672 * HeaderDigest=None
673 * DataDigest=None
674 * MaxConnections=1 (irrelevant; we make only one connection anyway) [4]
675 * InitialR2T=Yes [1]
676 * ImmediateData=No (irrelevant; we never send immediate data) [4]
677 * MaxRecvDataSegmentLength=8192 (default; we don't care) [3]
678 * MaxBurstLength=262144 (default; we don't care) [3]
679 * FirstBurstLength=65536 (irrelevant due to other settings) [5]
680 * DefaultTime2Wait=0 [2]
681 * DefaultTime2Retain=0 [2]
682 * MaxOutstandingR2T=1
683 * DataPDUInOrder=Yes
684 * DataSequenceInOrder=Yes
685 * ErrorRecoveryLevel=0
686 *
687 * [1] InitialR2T has an OR resolution function, so the target may
688 * force us to use it. We therefore simplify our logic by always
689 * using it.
690 *
691 * [2] These ensure that we can safely start a new task once we have
692 * reconnected after a failure, without having to manually tidy up
693 * after the old one.
694 *
695 * [3] We are quite happy to use the RFC-defined default values for
696 * these parameters, but some targets (notably OpenSolaris)
697 * incorrectly assume a default value of zero, so we explicitly
698 * specify the default values.
699 *
700 * [4] We are quite happy to use the RFC-defined default values for
701 * these parameters, but some targets (notably a QNAP TS-639Pro) fail
702 * unless they are supplied, so we explicitly specify the default
703 * values.
704 *
705 * [5] FirstBurstLength is defined to be irrelevant since we already
706 * force InitialR2T=Yes and ImmediateData=No, but some targets
707 * (notably LIO as of kernel 4.11) fail unless it is specified, so we
708 * explicitly specify the default value.
709 */
711 void *data, size_t len ) {
712 unsigned int used = 0;
713 const char *auth_method;
714
715 if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
716 /* Default to allowing no authentication */
717 auth_method = "None";
718 /* If we have a credential to supply, permit CHAP */
719 if ( iscsi->initiator_username )
720 auth_method = "CHAP,None";
721 /* If we have a credential to check, force CHAP */
722 if ( iscsi->target_username )
723 auth_method = "CHAP";
724 used += ssnprintf ( data + used, len - used,
725 "InitiatorName=%s%c"
726 "TargetName=%s%c"
727 "SessionType=Normal%c"
728 "AuthMethod=%s%c",
729 iscsi->initiator_iqn, 0,
730 iscsi->target_iqn, 0, 0,
731 auth_method, 0 );
732 }
733
735 used += ssnprintf ( data + used, len - used, "CHAP_A=5%c", 0 );
736 }
737
738 if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
739 char buf[ base16_encoded_len ( iscsi->chap.response_len ) + 1 ];
740 assert ( iscsi->initiator_username != NULL );
741 base16_encode ( iscsi->chap.response, iscsi->chap.response_len,
742 buf, sizeof ( buf ) );
743 used += ssnprintf ( data + used, len - used,
744 "CHAP_N=%s%cCHAP_R=0x%s%c",
745 iscsi->initiator_username, 0, buf, 0 );
746 }
747
748 if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_CHALLENGE ) ) {
749 size_t challenge_len = ( sizeof ( iscsi->chap_challenge ) - 1 );
750 char buf[ base16_encoded_len ( challenge_len ) + 1 ];
751 base16_encode ( ( iscsi->chap_challenge + 1 ), challenge_len,
752 buf, sizeof ( buf ) );
753 used += ssnprintf ( data + used, len - used,
754 "CHAP_I=%d%cCHAP_C=0x%s%c",
755 iscsi->chap_challenge[0], 0, buf, 0 );
756 }
757
759 used += ssnprintf ( data + used, len - used,
760 "HeaderDigest=None%c"
761 "DataDigest=None%c"
762 "MaxConnections=1%c"
763 "InitialR2T=Yes%c"
764 "ImmediateData=No%c"
765 "MaxRecvDataSegmentLength=%d%c"
766 "MaxBurstLength=%d%c"
767 "FirstBurstLength=%d%c"
768 "DefaultTime2Wait=0%c"
769 "DefaultTime2Retain=0%c"
770 "MaxOutstandingR2T=1%c"
771 "DataPDUInOrder=Yes%c"
772 "DataSequenceInOrder=Yes%c"
773 "ErrorRecoveryLevel=0%c",
774 0, 0, 0, 0, 0,
778 0, 0, 0, 0, 0, 0 );
779 }
780
781 return used;
782}
783
784/**
785 * Build iSCSI login request BHS
786 *
787 * @v iscsi iSCSI session
788 */
789static void iscsi_start_login ( struct iscsi_session *iscsi ) {
791 int len;
792
793 switch ( iscsi->status & ISCSI_LOGIN_CSG_MASK ) {
795 DBGC ( iscsi, "iSCSI %p entering security negotiation\n",
796 iscsi );
797 break;
799 DBGC ( iscsi, "iSCSI %p entering operational negotiation\n",
800 iscsi );
801 break;
802 default:
803 assert ( 0 );
804 }
805
806 /* Construct BHS and initiate transmission */
807 iscsi_start_tx ( iscsi );
810 request->flags = ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) |
812 /* version_max and version_min left as zero */
814 ISCSI_SET_LENGTHS ( request->lengths, 0, len );
815 request->isid_iana_en = htonl ( ISCSI_ISID_IANA |
817 request->isid_iana_qual = htons ( iscsi->isid_iana_qual );
818 /* tsih left as zero */
819 request->itt = htonl ( iscsi->itt );
820 /* cid left as zero */
821 request->cmdsn = htonl ( iscsi->cmdsn );
822 request->expstatsn = htonl ( iscsi->statsn + 1 );
823}
824
825/**
826 * Complete iSCSI login request PDU transmission
827 *
828 * @v iscsi iSCSI session
829 *
830 */
831static void iscsi_login_request_done ( struct iscsi_session *iscsi ) {
832
833 /* Clear any "strings to send" flags */
835
836 /* Free any dynamically allocated storage used for login */
837 chap_finish ( &iscsi->chap );
838}
839
840/**
841 * Transmit data segment of an iSCSI login request PDU
842 *
843 * @v iscsi iSCSI session
844 * @ret rc Return status code
845 *
846 * For login requests, the data segment consists of the login strings.
847 */
848static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
850 struct io_buffer *iobuf;
851 size_t len;
852 size_t pad_len;
853
854 len = ISCSI_DATA_LEN ( request->lengths );
855 pad_len = ISCSI_DATA_PAD_LEN ( request->lengths );
856 iobuf = xfer_alloc_iob ( &iscsi->socket, ( len + pad_len ) );
857 if ( ! iobuf )
858 return -ENOMEM;
859 iob_put ( iobuf, len );
860 iscsi_build_login_request_strings ( iscsi, iobuf->data, len );
861 memset ( iob_put ( iobuf, pad_len ), 0, pad_len );
862
863 return xfer_deliver_iob ( &iscsi->socket, iobuf );
864}
865
866/**
867 * Decode large binary value
868 *
869 * @v encoded Encoded large binary value
870 * @v raw Raw data
871 * @v len Length of data buffer
872 * @ret len Length of raw data, or negative error
873 */
874static int iscsi_large_binary_decode ( const char *encoded, uint8_t *raw,
875 size_t len ) {
876
877 /* Check for initial '0x' or '0b' and decode as appropriate */
878 if ( *(encoded++) == '0' ) {
879 switch ( tolower ( *(encoded++) ) ) {
880 case 'x' :
881 return base16_decode ( encoded, raw, len );
882 case 'b' :
883 return base64_decode ( encoded, raw, len );
884 }
885 }
886
888}
889
890/**
891 * Handle iSCSI TargetAddress text value
892 *
893 * @v iscsi iSCSI session
894 * @v value TargetAddress value
895 * @ret rc Return status code
896 */
898 const char *value ) {
899 char *separator;
900
901 DBGC ( iscsi, "iSCSI %p will redirect to %s\n", iscsi, value );
902
903 /* Replace target address */
904 free ( iscsi->target_address );
905 iscsi->target_address = strdup ( value );
906 if ( ! iscsi->target_address )
907 return -ENOMEM;
908
909 /* Replace target port */
910 iscsi->target_port = htons ( ISCSI_PORT );
911 separator = strchr ( iscsi->target_address, ':' );
912 if ( separator ) {
913 *separator = '\0';
914 iscsi->target_port = strtoul ( ( separator + 1 ), NULL, 0 );
915 }
916
917 return 0;
918}
919
920/**
921 * Handle iSCSI AuthMethod text value
922 *
923 * @v iscsi iSCSI session
924 * @v value AuthMethod value
925 * @ret rc Return status code
926 */
928 const char *value ) {
929
930 /* If server requests CHAP, send the CHAP_A string */
931 if ( strcmp ( value, "CHAP" ) == 0 ) {
932 DBGC ( iscsi, "iSCSI %p initiating CHAP authentication\n",
933 iscsi );
936 }
937
938 return 0;
939}
940
941/**
942 * Handle iSCSI MaxBurstLength text value
943 *
944 * @v iscsi iSCSI session
945 * @v value MaxBurstLength value
946 * @ret rc Return status code
947 */
949 const char *value ) {
950 unsigned long max_burst_len;
951 char *end;
952
953 /* Update maximum burst length */
954 max_burst_len = strtoul ( value, &end, 0 );
955 if ( *end ) {
956 DBGC ( iscsi, "iSCSI %p invalid MaxBurstLength \"%s\"\n",
957 iscsi, value );
958 return -EINVAL_MAXBURSTLENGTH;
959 }
960 if ( max_burst_len < iscsi->max_burst_len )
961 iscsi->max_burst_len = max_burst_len;
962
963 return 0;
964}
965
966/**
967 * Handle iSCSI CHAP_A text value
968 *
969 * @v iscsi iSCSI session
970 * @v value CHAP_A value
971 * @ret rc Return status code
972 */
973static int iscsi_handle_chap_a_value ( struct iscsi_session *iscsi,
974 const char *value ) {
975
976 /* We only ever offer "5" (i.e. MD5) as an algorithm, so if
977 * the server responds with anything else it is a protocol
978 * violation.
979 */
980 if ( strcmp ( value, "5" ) != 0 ) {
981 DBGC ( iscsi, "iSCSI %p got invalid CHAP algorithm \"%s\"\n",
982 iscsi, value );
984 }
985
986 return 0;
987}
988
989/**
990 * Handle iSCSI CHAP_I text value
991 *
992 * @v iscsi iSCSI session
993 * @v value CHAP_I value
994 * @ret rc Return status code
995 */
996static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
997 const char *value ) {
998 unsigned int identifier;
999 char *endp;
1000 int rc;
1001
1002 /* The CHAP identifier is an integer value */
1003 identifier = strtoul ( value, &endp, 0 );
1004 if ( *endp != '\0' ) {
1005 DBGC ( iscsi, "iSCSI %p saw invalid CHAP identifier \"%s\"\n",
1006 iscsi, value );
1008 }
1009
1010 /* Prepare for CHAP with MD5 */
1011 chap_finish ( &iscsi->chap );
1012 if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
1013 DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
1014 iscsi, strerror ( rc ) );
1015 return rc;
1016 }
1017
1018 /* Identifier and secret are the first two components of the
1019 * challenge.
1020 */
1021 chap_set_identifier ( &iscsi->chap, identifier );
1022 if ( iscsi->initiator_password ) {
1023 chap_update ( &iscsi->chap, iscsi->initiator_password,
1024 strlen ( iscsi->initiator_password ) );
1025 }
1026
1027 return 0;
1028}
1029
1030/**
1031 * Handle iSCSI CHAP_C text value
1032 *
1033 * @v iscsi iSCSI session
1034 * @v value CHAP_C value
1035 * @ret rc Return status code
1036 */
1037static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
1038 const char *value ) {
1039 uint8_t *buf;
1040 unsigned int i;
1041 int len;
1042 int rc;
1043
1044 /* Allocate decoding buffer */
1045 len = strlen ( value ); /* Decoding never expands data */
1046 buf = malloc ( len );
1047 if ( ! buf ) {
1048 rc = -ENOMEM;
1049 goto err_alloc;
1050 }
1051
1052 /* Process challenge */
1054 if ( len < 0 ) {
1055 rc = len;
1056 DBGC ( iscsi, "iSCSI %p invalid CHAP challenge \"%s\": %s\n",
1057 iscsi, value, strerror ( rc ) );
1058 goto err_decode;
1059 }
1060 chap_update ( &iscsi->chap, buf, len );
1061
1062 /* Build CHAP response */
1063 DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );
1064 chap_respond ( &iscsi->chap );
1066
1067 /* Send CHAP challenge, if applicable */
1068 if ( iscsi->target_username ) {
1070 /* Generate CHAP challenge data */
1071 for ( i = 0 ; i < sizeof ( iscsi->chap_challenge ) ; i++ ) {
1072 iscsi->chap_challenge[i] = random();
1073 }
1074 }
1075
1076 /* Success */
1077 rc = 0;
1078
1079 err_decode:
1080 free ( buf );
1081 err_alloc:
1082 return rc;
1083}
1084
1085/**
1086 * Handle iSCSI CHAP_N text value
1087 *
1088 * @v iscsi iSCSI session
1089 * @v value CHAP_N value
1090 * @ret rc Return status code
1091 */
1092static int iscsi_handle_chap_n_value ( struct iscsi_session *iscsi,
1093 const char *value ) {
1094
1095 /* The target username isn't actually involved at any point in
1096 * the authentication process; it merely serves to identify
1097 * which password the target is using to generate the CHAP
1098 * response. We unnecessarily verify that the username is as
1099 * expected, in order to provide mildly helpful diagnostics if
1100 * the target is supplying the wrong username/password
1101 * combination.
1102 */
1103 if ( iscsi->target_username &&
1104 ( strcmp ( iscsi->target_username, value ) != 0 ) ) {
1105 DBGC ( iscsi, "iSCSI %p target username \"%s\" incorrect "
1106 "(wanted \"%s\")\n",
1107 iscsi, value, iscsi->target_username );
1109 }
1110
1111 return 0;
1112}
1113
1114/**
1115 * Handle iSCSI CHAP_R text value
1116 *
1117 * @v iscsi iSCSI session
1118 * @v value CHAP_R value
1119 * @ret rc Return status code
1120 */
1121static int iscsi_handle_chap_r_value ( struct iscsi_session *iscsi,
1122 const char *value ) {
1123 uint8_t *buf;
1124 int len;
1125 int rc;
1126
1127 /* Generate CHAP response for verification */
1128 chap_finish ( &iscsi->chap );
1129 if ( ( rc = chap_init ( &iscsi->chap, &md5_algorithm ) ) != 0 ) {
1130 DBGC ( iscsi, "iSCSI %p could not initialise CHAP: %s\n",
1131 iscsi, strerror ( rc ) );
1132 goto err_chap_init;
1133 }
1134 chap_set_identifier ( &iscsi->chap, iscsi->chap_challenge[0] );
1135 if ( iscsi->target_password ) {
1136 chap_update ( &iscsi->chap, iscsi->target_password,
1137 strlen ( iscsi->target_password ) );
1138 }
1139 chap_update ( &iscsi->chap, &iscsi->chap_challenge[1],
1140 ( sizeof ( iscsi->chap_challenge ) - 1 ) );
1141 chap_respond ( &iscsi->chap );
1142
1143 /* Allocate decoding buffer */
1144 len = strlen ( value ); /* Decoding never expands data */
1145 buf = malloc ( len );
1146 if ( ! buf ) {
1147 rc = -ENOMEM;
1148 goto err_alloc;
1149 }
1150
1151 /* Process response */
1153 if ( len < 0 ) {
1154 rc = len;
1155 DBGC ( iscsi, "iSCSI %p invalid CHAP response \"%s\": %s\n",
1156 iscsi, value, strerror ( rc ) );
1157 goto err_decode;
1158 }
1159
1160 /* Check CHAP response */
1161 if ( len != ( int ) iscsi->chap.response_len ) {
1162 DBGC ( iscsi, "iSCSI %p invalid CHAP response length\n",
1163 iscsi );
1165 goto err_response_len;
1166 }
1167 if ( memcmp ( buf, iscsi->chap.response, len ) != 0 ) {
1168 DBGC ( iscsi, "iSCSI %p incorrect CHAP response \"%s\"\n",
1169 iscsi, value );
1171 goto err_response;
1172 }
1173
1174 /* Mark session as authenticated */
1176
1177 err_response:
1178 err_response_len:
1179 err_decode:
1180 free ( buf );
1181 err_alloc:
1182 err_chap_init:
1183 return rc;
1184}
1185
1186/** An iSCSI text string that we want to handle */
1188 /** String key
1189 *
1190 * This is the portion preceding the "=" sign,
1191 * e.g. "InitiatorName", "CHAP_A", etc.
1192 */
1193 const char *key;
1194 /** Handle iSCSI string value
1195 *
1196 * @v iscsi iSCSI session
1197 * @v value iSCSI string value
1198 * @ret rc Return status code
1199 */
1200 int ( * handle ) ( struct iscsi_session *iscsi, const char *value );
1201};
1202
1203/** iSCSI text strings that we want to handle */
1205 { "TargetAddress", iscsi_handle_targetaddress_value },
1206 { "MaxBurstLength", iscsi_handle_maxburstlength_value },
1207 { "AuthMethod", iscsi_handle_authmethod_value },
1208 { "CHAP_A", iscsi_handle_chap_a_value },
1209 { "CHAP_I", iscsi_handle_chap_i_value },
1210 { "CHAP_C", iscsi_handle_chap_c_value },
1211 { "CHAP_N", iscsi_handle_chap_n_value },
1212 { "CHAP_R", iscsi_handle_chap_r_value },
1213 { NULL, NULL }
1214};
1215
1216/**
1217 * Handle iSCSI string
1218 *
1219 * @v iscsi iSCSI session
1220 * @v string iSCSI string (in "key=value" format)
1221 * @ret rc Return status code
1222 */
1223static int iscsi_handle_string ( struct iscsi_session *iscsi,
1224 const char *string ) {
1225 struct iscsi_string_type *type;
1226 const char *separator;
1227 const char *value;
1228 size_t key_len;
1229 int rc;
1230
1231 /* Find separator */
1232 separator = strchr ( string, '=' );
1233 if ( ! separator ) {
1234 DBGC ( iscsi, "iSCSI %p malformed string %s\n",
1235 iscsi, string );
1237 }
1238 key_len = ( separator - string );
1239 value = ( separator + 1 );
1240
1241 /* Check for rejections. Since we send only non-rejectable
1242 * values, any rejection is a fatal protocol error.
1243 */
1244 if ( strcmp ( value, "Reject" ) == 0 ) {
1245 DBGC ( iscsi, "iSCSI %p rejection: %s\n", iscsi, string );
1246 return -EPROTO_VALUE_REJECTED;
1247 }
1248
1249 /* Handle key/value pair */
1250 for ( type = iscsi_string_types ; type->key ; type++ ) {
1251 if ( strncmp ( string, type->key, key_len ) != 0 )
1252 continue;
1253 DBGC ( iscsi, "iSCSI %p handling %s\n", iscsi, string );
1254 if ( ( rc = type->handle ( iscsi, value ) ) != 0 ) {
1255 DBGC ( iscsi, "iSCSI %p could not handle %s: %s\n",
1256 iscsi, string, strerror ( rc ) );
1257 return rc;
1258 }
1259 return 0;
1260 }
1261 DBGC ( iscsi, "iSCSI %p ignoring %s\n", iscsi, string );
1262 return 0;
1263}
1264
1265/**
1266 * Handle iSCSI strings
1267 *
1268 * @v iscsi iSCSI session
1269 * @v string iSCSI string buffer
1270 * @v len Length of string buffer
1271 * @ret rc Return status code
1272 */
1273static int iscsi_handle_strings ( struct iscsi_session *iscsi,
1274 const char *strings, size_t len ) {
1275 size_t string_len;
1276 int rc;
1277
1278 /* Handle each string in turn, taking care not to overrun the
1279 * data buffer in case of badly-terminated data.
1280 */
1281 while ( 1 ) {
1282 string_len = ( strnlen ( strings, len ) + 1 );
1283 if ( string_len > len )
1284 break;
1285 if ( ( rc = iscsi_handle_string ( iscsi, strings ) ) != 0 )
1286 return rc;
1287 strings += string_len;
1288 len -= string_len;
1289 }
1290 return 0;
1291}
1292
1293/**
1294 * Convert iSCSI response status to return status code
1295 *
1296 * @v status_class iSCSI status class
1297 * @v status_detail iSCSI status detail
1298 * @ret rc Return status code
1299 */
1300static int iscsi_status_to_rc ( unsigned int status_class,
1301 unsigned int status_detail ) {
1302 switch ( status_class ) {
1304 switch ( status_detail ) {
1311 return -ENODEV;
1312 default :
1314 }
1316 switch ( status_detail ) {
1318 return -EIO_TARGET_UNAVAILABLE;
1321 default:
1322 return -ENOTSUP_TARGET_STATUS;
1323 }
1324 default :
1325 return -EINVAL;
1326 }
1327}
1328
1329/**
1330 * Receive data segment of an iSCSI login response PDU
1331 *
1332 * @v iscsi iSCSI session
1333 * @v data Received data
1334 * @v len Length of received data
1335 * @v remaining Data remaining after this data
1336 * @ret rc Return status code
1337 */
1338static int iscsi_rx_login_response ( struct iscsi_session *iscsi,
1339 const void *data, size_t len,
1340 size_t remaining ) {
1341 struct iscsi_bhs_login_response *response
1342 = &iscsi->rx_bhs.login_response;
1343 int rc;
1344
1345 /* Buffer up the PDU data */
1346 if ( ( rc = iscsi_rx_buffered_data ( iscsi, data, len ) ) != 0 ) {
1347 DBGC ( iscsi, "iSCSI %p could not buffer login response: %s\n",
1348 iscsi, strerror ( rc ) );
1349 return rc;
1350 }
1351 if ( remaining )
1352 return 0;
1353
1354 /* Process string data and discard string buffer */
1355 if ( ( rc = iscsi_handle_strings ( iscsi, iscsi->rx_buffer,
1356 iscsi->rx_len ) ) != 0 )
1357 return rc;
1359
1360 /* Check for login redirection */
1361 if ( response->status_class == ISCSI_STATUS_REDIRECT ) {
1362 DBGC ( iscsi, "iSCSI %p redirecting to new server\n", iscsi );
1363 iscsi_close_connection ( iscsi, 0 );
1364 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 ) {
1365 DBGC ( iscsi, "iSCSI %p could not redirect: %s\n ",
1366 iscsi, strerror ( rc ) );
1367 return rc;
1368 }
1369 return 0;
1370 }
1371
1372 /* Check for fatal errors */
1373 if ( response->status_class != 0 ) {
1374 DBGC ( iscsi, "iSCSI login failure: class %02x detail %02x\n",
1375 response->status_class, response->status_detail );
1376 rc = iscsi_status_to_rc ( response->status_class,
1377 response->status_detail );
1378 return rc;
1379 }
1380
1381 /* Handle login transitions */
1382 if ( response->flags & ISCSI_LOGIN_FLAG_TRANSITION ) {
1383 iscsi->status &= ~( ISCSI_STATUS_PHASE_MASK |
1385 switch ( response->flags & ISCSI_LOGIN_NSG_MASK ) {
1387 iscsi->status |=
1390 break;
1393 break;
1394 default:
1395 DBGC ( iscsi, "iSCSI %p got invalid response flags "
1396 "%02x\n", iscsi, response->flags );
1397 return -EIO;
1398 }
1399 }
1400
1401 /* Send next login request PDU if we haven't reached the full
1402 * feature phase yet.
1403 */
1404 if ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) !=
1406 iscsi_start_login ( iscsi );
1407 return 0;
1408 }
1409
1410 /* Check that target authentication was successful (if required) */
1411 if ( ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) &&
1412 ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_OK ) ) {
1413 DBGC ( iscsi, "iSCSI %p nefarious target tried to bypass "
1414 "authentication\n", iscsi );
1415 return -EPROTO;
1416 }
1417
1418 /* Notify SCSI layer of window change */
1419 DBGC ( iscsi, "iSCSI %p entering full feature phase\n", iscsi );
1420 xfer_window_changed ( &iscsi->control );
1421
1422 return 0;
1423}
1424
1425/****************************************************************************
1426 *
1427 * iSCSI to socket interface
1428 *
1429 */
1430
1431/**
1432 * Pause TX engine
1433 *
1434 * @v iscsi iSCSI session
1435 */
1436static void iscsi_tx_pause ( struct iscsi_session *iscsi ) {
1437 process_del ( &iscsi->process );
1438}
1439
1440/**
1441 * Resume TX engine
1442 *
1443 * @v iscsi iSCSI session
1444 */
1445static void iscsi_tx_resume ( struct iscsi_session *iscsi ) {
1446 process_add ( &iscsi->process );
1447}
1448
1449/**
1450 * Start up a new TX PDU
1451 *
1452 * @v iscsi iSCSI session
1453 *
1454 * This initiates the process of sending a new PDU. Only one PDU may
1455 * be in transit at any one time.
1456 */
1457static void iscsi_start_tx ( struct iscsi_session *iscsi ) {
1458
1459 assert ( iscsi->tx_state == ISCSI_TX_IDLE );
1460
1461 /* Initialise TX BHS */
1462 memset ( &iscsi->tx_bhs, 0, sizeof ( iscsi->tx_bhs ) );
1463
1464 /* Flag TX engine to start transmitting */
1465 iscsi->tx_state = ISCSI_TX_BHS;
1466
1467 /* Start transmission process */
1468 iscsi_tx_resume ( iscsi );
1469}
1470
1471/**
1472 * Transmit nothing
1473 *
1474 * @v iscsi iSCSI session
1475 * @ret rc Return status code
1476 */
1477static int iscsi_tx_nothing ( struct iscsi_session *iscsi __unused ) {
1478 return 0;
1479}
1480
1481/**
1482 * Transmit basic header segment of an iSCSI PDU
1483 *
1484 * @v iscsi iSCSI session
1485 * @ret rc Return status code
1486 */
1487static int iscsi_tx_bhs ( struct iscsi_session *iscsi ) {
1488 return xfer_deliver_raw ( &iscsi->socket, &iscsi->tx_bhs,
1489 sizeof ( iscsi->tx_bhs ) );
1490}
1491
1492/**
1493 * Transmit data segment of an iSCSI PDU
1494 *
1495 * @v iscsi iSCSI session
1496 * @ret rc Return status code
1497 *
1498 * Handle transmission of part of a PDU data segment. iscsi::tx_bhs
1499 * will be valid when this is called.
1500 */
1501static int iscsi_tx_data ( struct iscsi_session *iscsi ) {
1502 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1503
1504 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
1506 return iscsi_tx_data_out ( iscsi );
1508 return iscsi_tx_login_request ( iscsi );
1509 default:
1510 /* Nothing to send in other states */
1511 return 0;
1512 }
1513}
1514
1515/**
1516 * Complete iSCSI PDU transmission
1517 *
1518 * @v iscsi iSCSI session
1519 *
1520 * Called when a PDU has been completely transmitted and the TX state
1521 * machine is about to enter the idle state. iscsi::tx_bhs will be
1522 * valid for the just-completed PDU when this is called.
1523 */
1524static void iscsi_tx_done ( struct iscsi_session *iscsi ) {
1525 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1526
1527 /* Stop transmission process */
1528 iscsi_tx_pause ( iscsi );
1529
1530 switch ( common->opcode & ISCSI_OPCODE_MASK ) {
1532 iscsi_data_out_done ( iscsi );
1533 break;
1535 iscsi_login_request_done ( iscsi );
1536 break;
1537 default:
1538 /* No action */
1539 break;
1540 }
1541}
1542
1543/**
1544 * Transmit iSCSI PDU
1545 *
1546 * @v iscsi iSCSI session
1547 * @v buf Temporary data buffer
1548 * @v len Length of temporary data buffer
1549 *
1550 * Constructs data to be sent for the current TX state
1551 */
1552static void iscsi_tx_step ( struct iscsi_session *iscsi ) {
1553 struct iscsi_bhs_common *common = &iscsi->tx_bhs.common;
1554 int ( * tx ) ( struct iscsi_session *iscsi );
1555 enum iscsi_tx_state next_state;
1556 size_t tx_len;
1557 int rc;
1558
1559 /* Select fragment to transmit */
1560 while ( 1 ) {
1561 switch ( iscsi->tx_state ) {
1562 case ISCSI_TX_BHS:
1563 tx = iscsi_tx_bhs;
1564 tx_len = sizeof ( iscsi->tx_bhs );
1565 next_state = ISCSI_TX_AHS;
1566 break;
1567 case ISCSI_TX_AHS:
1569 tx_len = 0;
1570 next_state = ISCSI_TX_DATA;
1571 break;
1572 case ISCSI_TX_DATA:
1573 tx = iscsi_tx_data;
1574 tx_len = ISCSI_DATA_LEN ( common->lengths );
1575 next_state = ISCSI_TX_IDLE;
1576 break;
1577 case ISCSI_TX_IDLE:
1578 /* Nothing to do; pause processing */
1579 iscsi_tx_pause ( iscsi );
1580 return;
1581 default:
1582 assert ( 0 );
1583 return;
1584 }
1585
1586 /* Check for window availability, if needed */
1587 if ( tx_len && ( xfer_window ( &iscsi->socket ) == 0 ) ) {
1588 /* Cannot transmit at this point; pause
1589 * processing and wait for window to reopen
1590 */
1591 iscsi_tx_pause ( iscsi );
1592 return;
1593 }
1594
1595 /* Transmit data */
1596 if ( ( rc = tx ( iscsi ) ) != 0 ) {
1597 DBGC ( iscsi, "iSCSI %p could not transmit: %s\n",
1598 iscsi, strerror ( rc ) );
1599 /* Transmission errors are fatal */
1600 iscsi_close ( iscsi, rc );
1601 return;
1602 }
1603
1604 /* Move to next state */
1605 iscsi->tx_state = next_state;
1606
1607 /* If we have moved to the idle state, mark
1608 * transmission as complete
1609 */
1610 if ( iscsi->tx_state == ISCSI_TX_IDLE )
1611 iscsi_tx_done ( iscsi );
1612 }
1613}
1614
1615/** iSCSI TX process descriptor */
1618
1619/**
1620 * Receive basic header segment of an iSCSI PDU
1621 *
1622 * @v iscsi iSCSI session
1623 * @v data Received data
1624 * @v len Length of received data
1625 * @v remaining Data remaining after this data
1626 * @ret rc Return status code
1627 *
1628 * This fills in iscsi::rx_bhs with the data from the BHS portion of
1629 * the received PDU.
1630 */
1631static int iscsi_rx_bhs ( struct iscsi_session *iscsi, const void *data,
1632 size_t len, size_t remaining __unused ) {
1633 memcpy ( &iscsi->rx_bhs.bytes[iscsi->rx_offset], data, len );
1634 if ( ( iscsi->rx_offset + len ) >= sizeof ( iscsi->rx_bhs ) ) {
1635 DBGC2 ( iscsi, "iSCSI %p received PDU opcode %#x len %#x\n",
1636 iscsi, iscsi->rx_bhs.common.opcode,
1637 ISCSI_DATA_LEN ( iscsi->rx_bhs.common.lengths ) );
1638 }
1639 return 0;
1640}
1641
1642/**
1643 * Discard portion of an iSCSI PDU.
1644 *
1645 * @v iscsi iSCSI session
1646 * @v data Received data
1647 * @v len Length of received data
1648 * @v remaining Data remaining after this data
1649 * @ret rc Return status code
1650 *
1651 * This discards data from a portion of a received PDU.
1652 */
1653static int iscsi_rx_discard ( struct iscsi_session *iscsi __unused,
1654 const void *data __unused, size_t len __unused,
1655 size_t remaining __unused ) {
1656 /* Do nothing */
1657 return 0;
1658}
1659
1660/**
1661 * Receive data segment of an iSCSI PDU
1662 *
1663 * @v iscsi iSCSI session
1664 * @v data Received data
1665 * @v len Length of received data
1666 * @v remaining Data remaining after this data
1667 * @ret rc Return status code
1668 *
1669 * Handle processing of part of a PDU data segment. iscsi::rx_bhs
1670 * will be valid when this is called.
1671 */
1672static int iscsi_rx_data ( struct iscsi_session *iscsi, const void *data,
1673 size_t len, size_t remaining ) {
1674 struct iscsi_bhs_common_response *response
1675 = &iscsi->rx_bhs.common_response;
1676
1677 /* Update cmdsn and statsn */
1678 iscsi->cmdsn = ntohl ( response->expcmdsn );
1679 iscsi->statsn = ntohl ( response->statsn );
1680
1681 switch ( response->opcode & ISCSI_OPCODE_MASK ) {
1683 return iscsi_rx_login_response ( iscsi, data, len, remaining );
1685 return iscsi_rx_scsi_response ( iscsi, data, len, remaining );
1687 return iscsi_rx_data_in ( iscsi, data, len, remaining );
1688 case ISCSI_OPCODE_R2T:
1689 return iscsi_rx_r2t ( iscsi, data, len, remaining );
1691 return iscsi_rx_nop_in ( iscsi, data, len, remaining );
1692 default:
1693 if ( remaining )
1694 return 0;
1695 DBGC ( iscsi, "iSCSI %p unknown opcode %02x\n", iscsi,
1696 response->opcode );
1697 return -ENOTSUP_OPCODE;
1698 }
1699}
1700
1701/**
1702 * Receive new data
1703 *
1704 * @v iscsi iSCSI session
1705 * @v iobuf I/O buffer
1706 * @v meta Data transfer metadata
1707 * @ret rc Return status code
1708 *
1709 * This handles received PDUs. The receive strategy is to fill in
1710 * iscsi::rx_bhs with the contents of the BHS portion of the PDU,
1711 * throw away any AHS portion, and then process each part of the data
1712 * portion as it arrives. The data processing routine therefore
1713 * always has a full copy of the BHS available, even for portions of
1714 * the data in different packets to the BHS.
1715 */
1716static int iscsi_socket_deliver ( struct iscsi_session *iscsi,
1717 struct io_buffer *iobuf,
1718 struct xfer_metadata *meta __unused ) {
1719 struct iscsi_bhs_common *common = &iscsi->rx_bhs.common;
1720 int ( * rx ) ( struct iscsi_session *iscsi, const void *data,
1721 size_t len, size_t remaining );
1722 enum iscsi_rx_state next_state;
1723 size_t frag_len;
1724 size_t remaining;
1725 int rc;
1726
1727 while ( 1 ) {
1728 switch ( iscsi->rx_state ) {
1729 case ISCSI_RX_BHS:
1730 rx = iscsi_rx_bhs;
1731 iscsi->rx_len = sizeof ( iscsi->rx_bhs );
1732 next_state = ISCSI_RX_AHS;
1733 break;
1734 case ISCSI_RX_AHS:
1736 iscsi->rx_len = 4 * ISCSI_AHS_LEN ( common->lengths );
1737 next_state = ISCSI_RX_DATA;
1738 break;
1739 case ISCSI_RX_DATA:
1740 rx = iscsi_rx_data;
1741 iscsi->rx_len = ISCSI_DATA_LEN ( common->lengths );
1742 next_state = ISCSI_RX_DATA_PADDING;
1743 break;
1746 iscsi->rx_len = ISCSI_DATA_PAD_LEN ( common->lengths );
1747 next_state = ISCSI_RX_BHS;
1748 break;
1749 default:
1750 assert ( 0 );
1751 rc = -EINVAL;
1752 goto done;
1753 }
1754
1755 frag_len = iscsi->rx_len - iscsi->rx_offset;
1756 if ( frag_len > iob_len ( iobuf ) )
1757 frag_len = iob_len ( iobuf );
1758 remaining = iscsi->rx_len - iscsi->rx_offset - frag_len;
1759 if ( ( rc = rx ( iscsi, iobuf->data, frag_len,
1760 remaining ) ) != 0 ) {
1761 DBGC ( iscsi, "iSCSI %p could not process received "
1762 "data: %s\n", iscsi, strerror ( rc ) );
1763 goto done;
1764 }
1765
1766 iscsi->rx_offset += frag_len;
1767 iob_pull ( iobuf, frag_len );
1768
1769 /* If all the data for this state has not yet been
1770 * received, stay in this state for now.
1771 */
1772 if ( iscsi->rx_offset != iscsi->rx_len ) {
1773 rc = 0;
1774 goto done;
1775 }
1776
1777 iscsi->rx_state = next_state;
1778 iscsi->rx_offset = 0;
1779 }
1780
1781 done:
1782 /* Free I/O buffer */
1783 free_iob ( iobuf );
1784
1785 /* Destroy session on error */
1786 if ( rc != 0 )
1787 iscsi_close ( iscsi, rc );
1788
1789 return rc;
1790}
1791
1792/**
1793 * Handle redirection event
1794 *
1795 * @v iscsi iSCSI session
1796 * @v type Location type
1797 * @v args Remaining arguments depend upon location type
1798 * @ret rc Return status code
1799 */
1800static int iscsi_vredirect ( struct iscsi_session *iscsi, int type,
1801 va_list args ) {
1802 va_list tmp;
1803 struct sockaddr *peer;
1804 int rc;
1805
1806 /* Intercept redirects to a LOCATION_SOCKET and record the IP
1807 * address for the iBFT. This is a bit of a hack, but avoids
1808 * inventing an ioctl()-style call to retrieve the socket
1809 * address from a data-xfer interface.
1810 */
1811 if ( type == LOCATION_SOCKET ) {
1812 va_copy ( tmp, args );
1813 ( void ) va_arg ( tmp, int ); /* Discard "semantics" */
1814 peer = va_arg ( tmp, struct sockaddr * );
1815 memcpy ( &iscsi->target_sockaddr, peer,
1816 sizeof ( iscsi->target_sockaddr ) );
1817 va_end ( tmp );
1818 }
1819
1820 /* Redirect to new location */
1821 if ( ( rc = xfer_vreopen ( &iscsi->socket, type, args ) ) != 0 )
1822 goto err;
1823
1824 return 0;
1825
1826 err:
1827 iscsi_close ( iscsi, rc );
1828 return rc;
1829}
1830
1831/** iSCSI socket interface operations */
1839
1840/** iSCSI socket interface descriptor */
1843
1844/****************************************************************************
1845 *
1846 * iSCSI command issuing
1847 *
1848 */
1849
1850/**
1851 * Check iSCSI flow-control window
1852 *
1853 * @v iscsi iSCSI session
1854 * @ret len Length of window
1855 */
1856static size_t iscsi_scsi_window ( struct iscsi_session *iscsi ) {
1857
1858 if ( ( ( iscsi->status & ISCSI_STATUS_PHASE_MASK ) ==
1860 ( iscsi->command == NULL ) ) {
1861 /* We cannot handle concurrent commands */
1862 return 1;
1863 } else {
1864 return 0;
1865 }
1866}
1867
1868/**
1869 * Issue iSCSI SCSI command
1870 *
1871 * @v iscsi iSCSI session
1872 * @v parent Parent interface
1873 * @v command SCSI command
1874 * @ret tag Command tag, or negative error
1875 */
1876static int iscsi_scsi_command ( struct iscsi_session *iscsi,
1877 struct interface *parent,
1878 struct scsi_cmd *command ) {
1879
1880 /* This iSCSI implementation cannot handle multiple concurrent
1881 * commands or commands arriving before login is complete.
1882 */
1883 if ( iscsi_scsi_window ( iscsi ) == 0 ) {
1884 DBGC ( iscsi, "iSCSI %p cannot handle concurrent commands\n",
1885 iscsi );
1886 return -EOPNOTSUPP;
1887 }
1888
1889 /* Store command */
1890 iscsi->command = malloc ( sizeof ( *command ) );
1891 if ( ! iscsi->command )
1892 return -ENOMEM;
1893 memcpy ( iscsi->command, command, sizeof ( *command ) );
1894
1895 /* Assign new ITT */
1896 iscsi_new_itt ( iscsi );
1897
1898 /* Start sending command */
1899 iscsi_start_command ( iscsi );
1900
1901 /* Attach to parent interface and return */
1902 intf_plug_plug ( &iscsi->data, parent );
1903 return iscsi->itt;
1904}
1905
1906/**
1907 * Update SCSI block device capacity
1908 *
1909 * @v iscsi iSCSI session
1910 * @v capacity Block device capacity
1911 */
1912static void iscsi_scsi_capacity ( struct iscsi_session *iscsi,
1913 struct block_device_capacity *capacity ) {
1914 unsigned int max_count;
1915
1916 /* Limit maximum number of blocks per transfer to fit MaxBurstLength */
1917 if ( capacity->blksize ) {
1918 max_count = ( iscsi->max_burst_len / capacity->blksize );
1919 if ( max_count < capacity->max_count )
1920 capacity->max_count = max_count;
1921 }
1922}
1923
1924/**
1925 * Get iSCSI ACPI descriptor
1926 *
1927 * @v iscsi iSCSI session
1928 * @ret desc ACPI descriptor
1929 */
1930static struct acpi_descriptor * iscsi_describe ( struct iscsi_session *iscsi ) {
1931
1932 return &iscsi->desc;
1933}
1934
1935/** iSCSI SCSI command-issuing interface operations */
1944
1945/** iSCSI SCSI command-issuing interface descriptor */
1948
1949/**
1950 * Close iSCSI command
1951 *
1952 * @v iscsi iSCSI session
1953 * @v rc Reason for close
1954 */
1955static void iscsi_command_close ( struct iscsi_session *iscsi, int rc ) {
1956
1957 /* Restart interface */
1958 intf_restart ( &iscsi->data, rc );
1959
1960 /* Treat unsolicited command closures mid-command as fatal,
1961 * because we have no code to handle partially-completed PDUs.
1962 */
1963 if ( iscsi->command != NULL )
1964 iscsi_close ( iscsi, ( ( rc == 0 ) ? -ECANCELED : rc ) );
1965}
1966
1967/** iSCSI SCSI command interface operations */
1971
1972/** iSCSI SCSI command interface descriptor */
1975
1976/****************************************************************************
1977 *
1978 * Instantiator
1979 *
1980 */
1981
1982/** iSCSI root path components (as per RFC4173) */
1991
1992/** iSCSI initiator IQN setting */
1993const struct setting initiator_iqn_setting __setting ( SETTING_SANBOOT_EXTRA,
1994 initiator-iqn ) = {
1995 .name = "initiator-iqn",
1996 .description = "iSCSI initiator name",
1998 .type = &setting_type_string,
1999};
2000
2001/** iSCSI reverse username setting */
2002const struct setting reverse_username_setting __setting ( SETTING_AUTH_EXTRA,
2003 reverse-username ) = {
2004 .name = "reverse-username",
2005 .description = "Reverse user name",
2007 .type = &setting_type_string,
2008};
2009
2010/** iSCSI reverse password setting */
2011const struct setting reverse_password_setting __setting ( SETTING_AUTH_EXTRA,
2012 reverse-password ) = {
2013 .name = "reverse-password",
2014 .description = "Reverse password",
2016 .type = &setting_type_string,
2017};
2018
2019/**
2020 * Parse iSCSI root path
2021 *
2022 * @v iscsi iSCSI session
2023 * @v root_path iSCSI root path (as per RFC4173)
2024 * @ret rc Return status code
2025 */
2026static int iscsi_parse_root_path ( struct iscsi_session *iscsi,
2027 const char *root_path ) {
2028 char *rp_copy;
2029 char *rp_comp[NUM_RP_COMPONENTS];
2030 char *rp;
2031 int skip = 0;
2032 int i = 0;
2033 int rc;
2034
2035 /* Create modifiable copy of root path */
2036 rp_copy = strdup ( root_path );
2037 if ( ! rp_copy ) {
2038 rc = -ENOMEM;
2039 goto err_strdup;
2040 }
2041 rp = rp_copy;
2042
2043 /* Split root path into component parts */
2044 while ( 1 ) {
2045 rp_comp[i++] = rp;
2046 if ( i == NUM_RP_COMPONENTS )
2047 break;
2048 for ( ; ( ( *rp != ':' ) || skip ) ; rp++ ) {
2049 if ( ! *rp ) {
2050 DBGC ( iscsi, "iSCSI %p root path \"%s\" "
2051 "too short\n", iscsi, root_path );
2053 goto err_split;
2054 } else if ( *rp == '[' ) {
2055 skip = 1;
2056 } else if ( *rp == ']' ) {
2057 skip = 0;
2058 }
2059 }
2060 *(rp++) = '\0';
2061 }
2062
2063 /* Use root path components to configure iSCSI session */
2064 iscsi->target_address = strdup ( rp_comp[RP_SERVERNAME] );
2065 if ( ! iscsi->target_address ) {
2066 rc = -ENOMEM;
2067 goto err_servername;
2068 }
2069 iscsi->target_port = strtoul ( rp_comp[RP_PORT], NULL, 10 );
2070 if ( ! iscsi->target_port )
2071 iscsi->target_port = ISCSI_PORT;
2072 if ( ( rc = scsi_parse_lun ( rp_comp[RP_LUN], &iscsi->lun ) ) != 0 ) {
2073 DBGC ( iscsi, "iSCSI %p invalid LUN \"%s\"\n",
2074 iscsi, rp_comp[RP_LUN] );
2075 goto err_lun;
2076 }
2077 iscsi->target_iqn = strdup ( rp_comp[RP_TARGETNAME] );
2078 if ( ! iscsi->target_iqn ) {
2079 rc = -ENOMEM;
2080 goto err_targetname;
2081 }
2082
2083 err_targetname:
2084 err_lun:
2085 err_servername:
2086 err_split:
2087 free ( rp_copy );
2088 err_strdup:
2089 return rc;
2090}
2091
2092/**
2093 * Fetch iSCSI settings
2094 *
2095 * @v iscsi iSCSI session
2096 * @ret rc Return status code
2097 */
2098static int iscsi_fetch_settings ( struct iscsi_session *iscsi ) {
2099 char *hostname;
2100 union uuid uuid;
2101 int len;
2102
2103 /* Fetch relevant settings. Don't worry about freeing on
2104 * error, since iscsi_free() will take care of that anyway.
2105 */
2106 fetch_string_setting_copy ( NULL, &username_setting,
2107 &iscsi->initiator_username );
2108 fetch_string_setting_copy ( NULL, &password_setting,
2109 &iscsi->initiator_password );
2110 fetch_string_setting_copy ( NULL, &reverse_username_setting,
2111 &iscsi->target_username );
2112 fetch_string_setting_copy ( NULL, &reverse_password_setting,
2113 &iscsi->target_password );
2114
2115 /* Use explicit initiator IQN if provided */
2116 fetch_string_setting_copy ( NULL, &initiator_iqn_setting,
2117 &iscsi->initiator_iqn );
2118 if ( iscsi->initiator_iqn )
2119 return 0;
2120
2121 /* Otherwise, try to construct an initiator IQN from the hostname */
2122 fetch_string_setting_copy ( NULL, &hostname_setting, &hostname );
2123 if ( hostname ) {
2124 len = asprintf ( &iscsi->initiator_iqn,
2125 ISCSI_DEFAULT_IQN_PREFIX ":%s", hostname );
2126 free ( hostname );
2127 if ( len < 0 ) {
2128 DBGC ( iscsi, "iSCSI %p could not allocate initiator "
2129 "IQN\n", iscsi );
2130 return -ENOMEM;
2131 }
2132 assert ( iscsi->initiator_iqn );
2133 return 0;
2134 }
2135
2136 /* Otherwise, try to construct an initiator IQN from the UUID */
2137 if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting, &uuid ) ) < 0 ) {
2138 DBGC ( iscsi, "iSCSI %p has no suitable initiator IQN\n",
2139 iscsi );
2141 }
2142 if ( ( len = asprintf ( &iscsi->initiator_iqn,
2144 uuid_ntoa ( &uuid ) ) ) < 0 ) {
2145 DBGC ( iscsi, "iSCSI %p could not allocate initiator IQN\n",
2146 iscsi );
2147 return -ENOMEM;
2148 }
2149 assert ( iscsi->initiator_iqn );
2150
2151 return 0;
2152}
2153
2154
2155/**
2156 * Check iSCSI authentication details
2157 *
2158 * @v iscsi iSCSI session
2159 * @ret rc Return status code
2160 */
2161static int iscsi_check_auth ( struct iscsi_session *iscsi ) {
2162
2163 /* Check for invalid authentication combinations */
2164 if ( ( /* Initiator username without password (or vice-versa) */
2165 ( !! iscsi->initiator_username ) ^
2166 ( !! iscsi->initiator_password ) ) ||
2167 ( /* Target username without password (or vice-versa) */
2168 ( !! iscsi->target_username ) ^
2169 ( !! iscsi->target_password ) ) ||
2170 ( /* Target (reverse) without initiator (forward) */
2171 ( iscsi->target_username &&
2172 ( ! iscsi->initiator_username ) ) ) ) {
2173 DBGC ( iscsi, "iSCSI %p invalid credentials: initiator "
2174 "%sname,%spw, target %sname,%spw\n", iscsi,
2175 ( iscsi->initiator_username ? "" : "no " ),
2176 ( iscsi->initiator_password ? "" : "no " ),
2177 ( iscsi->target_username ? "" : "no " ),
2178 ( iscsi->target_password ? "" : "no " ) );
2180 }
2181
2182 return 0;
2183}
2184
2185/**
2186 * Open iSCSI URI
2187 *
2188 * @v parent Parent interface
2189 * @v uri URI
2190 * @ret rc Return status code
2191 */
2192static int iscsi_open ( struct interface *parent, struct uri *uri ) {
2193 struct iscsi_session *iscsi;
2194 int rc;
2195
2196 /* Sanity check */
2197 if ( ! uri->opaque ) {
2199 goto err_sanity_uri;
2200 }
2201
2202 /* Allocate and initialise structure */
2203 iscsi = zalloc ( sizeof ( *iscsi ) );
2204 if ( ! iscsi ) {
2205 rc = -ENOMEM;
2206 goto err_zalloc;
2207 }
2208 ref_init ( &iscsi->refcnt, iscsi_free );
2209 intf_init ( &iscsi->control, &iscsi_control_desc, &iscsi->refcnt );
2210 intf_init ( &iscsi->data, &iscsi_data_desc, &iscsi->refcnt );
2211 intf_init ( &iscsi->socket, &iscsi_socket_desc, &iscsi->refcnt );
2213 &iscsi->refcnt );
2214 acpi_init ( &iscsi->desc, &ibft_model, &iscsi->refcnt );
2215
2216 /* Parse root path */
2217 if ( ( rc = iscsi_parse_root_path ( iscsi, uri->opaque ) ) != 0 )
2218 goto err_parse_root_path;
2219 /* Set fields not specified by root path */
2220 if ( ( rc = iscsi_fetch_settings ( iscsi ) ) != 0 )
2221 goto err_fetch_settings;
2222 /* Validate authentication */
2223 if ( ( rc = iscsi_check_auth ( iscsi ) ) != 0 )
2224 goto err_check_auth;
2225
2226 /* Sanity checks */
2227 if ( ! iscsi->target_address ) {
2228 DBGC ( iscsi, "iSCSI %p does not yet support discovery\n",
2229 iscsi );
2231 goto err_sanity_address;
2232 }
2233 if ( ! iscsi->target_iqn ) {
2234 DBGC ( iscsi, "iSCSI %p no target address supplied in %s\n",
2235 iscsi, uri->opaque );
2237 goto err_sanity_iqn;
2238 }
2239 DBGC ( iscsi, "iSCSI %p initiator %s\n",iscsi, iscsi->initiator_iqn );
2240 DBGC ( iscsi, "iSCSI %p target %s %s\n",
2241 iscsi, iscsi->target_address, iscsi->target_iqn );
2242
2243 /* Open socket */
2244 if ( ( rc = iscsi_open_connection ( iscsi ) ) != 0 )
2245 goto err_open_connection;
2246
2247 /* Attach SCSI device to parent interface */
2248 if ( ( rc = scsi_open ( parent, &iscsi->control,
2249 &iscsi->lun ) ) != 0 ) {
2250 DBGC ( iscsi, "iSCSI %p could not create SCSI device: %s\n",
2251 iscsi, strerror ( rc ) );
2252 goto err_scsi_open;
2253 }
2254
2255 /* Mortalise self, and return */
2256 ref_put ( &iscsi->refcnt );
2257 return 0;
2258
2259 err_scsi_open:
2260 err_open_connection:
2261 err_sanity_iqn:
2262 err_sanity_address:
2263 err_check_auth:
2264 err_fetch_settings:
2265 err_parse_root_path:
2266 iscsi_close ( iscsi, rc );
2267 ref_put ( &iscsi->refcnt );
2268 err_zalloc:
2269 err_sanity_uri:
2270 return rc;
2271}
2272
2273/** iSCSI URI opener */
2274struct uri_opener iscsi_uri_opener __uri_opener = {
2275 .scheme = "iscsi",
2276 .open = iscsi_open,
2277};
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
__be32 raw[7]
Definition CIB_PRM.h:0
struct acpi_descriptor * acpi_describe(struct interface *intf)
Get object's ACPI descriptor.
Definition acpi.c:321
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
pseudo_bit_t value[0x00020]
Definition arbel.h:2
unsigned short uint16_t
Definition stdint.h:11
unsigned int uint32_t
Definition stdint.h:12
unsigned char uint8_t
Definition stdint.h:10
int asprintf(char **strp, const char *fmt,...)
Write a formatted string to newly allocated memory.
Definition asprintf.c:42
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
Base16 encoding.
static size_t base16_encoded_len(size_t raw_len)
Calculate length of base16-encoded data.
Definition base16.h:25
int base64_decode(const char *encoded, void *data, size_t len)
Base64-decode string.
Definition base64.c:92
Base64 encoding.
void block_capacity(struct interface *intf, struct block_device_capacity *capacity)
Report block device capacity.
Definition blockdev.c:130
Block devices.
struct bofm_section_header done
Definition bofm_test.c:46
uint16_t offset
Offset to command line.
Definition bzimage.h:3
void chap_respond(struct chap_response *chap)
Respond to the CHAP challenge.
Definition chap.c:105
void chap_update(struct chap_response *chap, const void *data, size_t len)
Add data to the CHAP challenge.
Definition chap.c:86
void chap_finish(struct chap_response *chap)
Free resources used by a CHAP response.
Definition chap.c:123
int chap_init(struct chap_response *chap, struct digest_algorithm *digest)
Initialise CHAP challenge/response.
Definition chap.c:52
static void chap_set_identifier(struct chap_response *chap, unsigned int identifier)
Add identifier data to the CHAP challenge.
Definition chap.h:47
Character types.
static int tolower(int character)
Convert character to lower case.
Definition ctype.h:109
ring len
Length.
Definition dwmac.h:226
EFI_DEVICE_PATH_PROTOCOL * efi_describe(struct interface *intf)
Describe object as an EFI device path.
Definition efi_path.c:945
EFI_DEVICE_PATH_PROTOCOL * efi_iscsi_path(struct iscsi_session *iscsi)
Construct EFI device path for iSCSI device.
Definition efi_path.c:531
EFI device paths.
uint32_t type
Operating system type.
Definition ena.h:1
uint8_t data[48]
Additional event data.
Definition ena.h:11
uint8_t meta
Metadata flags.
Definition ena.h:3
Error codes.
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:598
#define SOCK_STREAM
Definition socket.h:25
#define DBGC2(...)
Definition compiler.h:547
#define DBGC(...)
Definition compiler.h:530
#define DHCP_EB_FEATURE_ISCSI
iSCSI protocol
Definition features.h:38
#define DHCP_EB_REVERSE_PASSWORD
Reverse password.
Definition dhcp.h:515
#define DHCP_ISCSI_INITIATOR_IQN
iSCSI initiator IQN
Definition dhcp.h:539
#define DHCP_EB_REVERSE_USERNAME
Reverse username.
Definition dhcp.h:506
#define FEATURE_PROTOCOL
Network protocols.
Definition features.h:22
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define EINVAL
Invalid argument.
Definition errno.h:429
#define EOPNOTSUPP
Operation not supported on socket.
Definition errno.h:605
#define EPROTO
Protocol error.
Definition errno.h:625
#define ENOMEM
Not enough space.
Definition errno.h:535
#define EIO
Input/output error.
Definition errno.h:434
#define ECANCELED
Operation canceled.
Definition errno.h:344
#define ENODEV
No such device.
Definition errno.h:510
#define ECONNRESET
Connection reset.
Definition errno.h:364
#define SCSI_CDB_DATA(cdb)
printf() parameters for dumping a scsi_cdb
Definition scsi.h:224
#define SCSI_CDB_FORMAT
printf() format for dumping a scsi_cdb
Definition scsi.h:220
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
#define SETTING_AUTH_EXTRA
Authentication additional settings.
Definition settings.h:79
#define SETTING_SANBOOT_EXTRA
SAN boot additional settings.
Definition settings.h:75
struct ib_cm_common common
Definition ib_mad.h:0
iSCSI boot firmware table
u8 request[0]
List of IEs requested.
Definition ieee80211.h:2
#define ntohl(value)
Definition byteswap.h:135
#define htonl(value)
Definition byteswap.h:134
#define htons(value)
Definition byteswap.h:136
static void acpi_init(struct acpi_descriptor *desc, struct acpi_model *model, struct refcnt *refcnt)
Initialise ACPI descriptor.
Definition acpi.h:312
#define EFI_INTF_OP
Definition efi.h:381
Configuration settings.
#define __setting(setting_order, name)
Declare a configuration setting.
Definition settings.h:57
Transport-network layer interface.
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition interface.c:250
void intfs_shutdown(int rc,...)
Shut down multiple object interfaces.
Definition interface.c:327
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition interface.c:108
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition interface.c:344
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition interface.h:81
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition interface.h:204
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition interface.h:33
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition iobuf.c:153
I/O buffers.
#define iob_put(iobuf, len)
Definition iobuf.h:125
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition iobuf.h:217
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition iobuf.h:160
#define iob_pull(iobuf, len)
Definition iobuf.h:107
Feature list.
#define FEATURE(category, text, feature_opt, version)
Declare a feature.
Definition features.h:101
#define ENOTSUP_TARGET_STATUS
Definition iscsi.c:114
#define EPROTO_VALUE_REJECTED
Definition iscsi.c:146
static size_t iscsi_scsi_window(struct iscsi_session *iscsi)
Check iSCSI flow-control window.
Definition iscsi.c:1856
static void iscsi_scsi_capacity(struct iscsi_session *iscsi, struct block_device_capacity *capacity)
Update SCSI block device capacity.
Definition iscsi.c:1912
static int iscsi_tx_nothing(struct iscsi_session *iscsi __unused)
Transmit nothing.
Definition iscsi.c:1477
static void iscsi_free(struct refcnt *refcnt)
Free iSCSI session.
Definition iscsi.c:201
static struct iscsi_string_type iscsi_string_types[]
iSCSI text strings that we want to handle
Definition iscsi.c:1204
#define EPERM_INITIATOR_AUTHORISATION
Definition iscsi.c:122
static int iscsi_rx_data(struct iscsi_session *iscsi, const void *data, size_t len, size_t remaining)
Receive data segment of an iSCSI PDU.
Definition iscsi.c:1672
static int iscsi_rx_scsi_response(struct iscsi_session *iscsi, const void *data, size_t len, size_t remaining)
Receive data segment of an iSCSI SCSI response PDU.
Definition iscsi.c:417
static int iscsi_tx_bhs(struct iscsi_session *iscsi)
Transmit basic header segment of an iSCSI PDU.
Definition iscsi.c:1487
static void iscsi_start_data_out(struct iscsi_session *iscsi, unsigned int datasn)
Build iSCSI data-out BHS.
Definition iscsi.c:529
#define EACCES_INCORRECT_TARGET_USERNAME
Definition iscsi.c:62
static int iscsi_handle_chap_a_value(struct iscsi_session *iscsi, const char *value)
Handle iSCSI CHAP_A text value.
Definition iscsi.c:973
#define EACCES_INCORRECT_TARGET_PASSWORD
Definition iscsi.c:66
static int iscsi_check_auth(struct iscsi_session *iscsi)
Check iSCSI authentication details.
Definition iscsi.c:2161
static void iscsi_tx_resume(struct iscsi_session *iscsi)
Resume TX engine.
Definition iscsi.c:1445
#define EINVAL_NO_TARGET_IQN
Definition iscsi.c:82
static int iscsi_rx_data_in(struct iscsi_session *iscsi, const void *data, size_t len, size_t remaining)
Receive data segment of an iSCSI data-in PDU.
Definition iscsi.c:470
static void iscsi_tx_pause(struct iscsi_session *iscsi)
Pause TX engine.
Definition iscsi.c:1436
static struct interface_descriptor iscsi_socket_desc
iSCSI socket interface descriptor
Definition iscsi.c:1841
static int iscsi_handle_maxburstlength_value(struct iscsi_session *iscsi, const char *value)
Handle iSCSI MaxBurstLength text value.
Definition iscsi.c:948
#define EPROTO_INVALID_LARGE_BINARY
Definition iscsi.c:134
static void iscsi_start_login(struct iscsi_session *iscsi)
Build iSCSI login request BHS.
Definition iscsi.c:789
static struct interface_operation iscsi_socket_operations[]
iSCSI socket interface operations
Definition iscsi.c:1832
static void iscsi_tx_step(struct iscsi_session *iscsi)
Transmit iSCSI PDU.
Definition iscsi.c:1552
iscsi_root_path_component
iSCSI root path components (as per RFC4173)
Definition iscsi.c:1983
@ NUM_RP_COMPONENTS
Definition iscsi.c:1989
@ RP_SERVERNAME
Definition iscsi.c:1984
@ RP_PORT
Definition iscsi.c:1986
@ RP_PROTOCOL
Definition iscsi.c:1985
@ RP_TARGETNAME
Definition iscsi.c:1988
@ RP_LUN
Definition iscsi.c:1987
static int iscsi_handle_chap_c_value(struct iscsi_session *iscsi, const char *value)
Handle iSCSI CHAP_C text value.
Definition iscsi.c:1037
#define EINVAL_ROOT_PATH_TOO_SHORT
Definition iscsi.c:70
static int iscsi_rx_login_response(struct iscsi_session *iscsi, const void *data, size_t len, size_t remaining)
Receive data segment of an iSCSI login response PDU.
Definition iscsi.c:1338
static int iscsi_rx_discard(struct iscsi_session *iscsi __unused, const void *data __unused, size_t len __unused, size_t remaining __unused)
Discard portion of an iSCSI PDU.
Definition iscsi.c:1653
static int iscsi_socket_deliver(struct iscsi_session *iscsi, struct io_buffer *iobuf, struct xfer_metadata *meta __unused)
Receive new data.
Definition iscsi.c:1716
static struct interface_operation iscsi_data_op[]
iSCSI SCSI command interface operations
Definition iscsi.c:1968
static void iscsi_login_request_done(struct iscsi_session *iscsi)
Complete iSCSI login request PDU transmission.
Definition iscsi.c:831
#define EIO_TARGET_NO_RESOURCES
Definition iscsi.c:98
#define EINVAL_MAXBURSTLENGTH
Definition iscsi.c:90
static void iscsi_tx_done(struct iscsi_session *iscsi)
Complete iSCSI PDU transmission.
Definition iscsi.c:1524
#define ENOTSUP_INITIATOR_STATUS
Definition iscsi.c:102
static int iscsi_handle_chap_i_value(struct iscsi_session *iscsi, const char *value)
Handle iSCSI CHAP_I text value.
Definition iscsi.c:996
static int iscsi_handle_authmethod_value(struct iscsi_session *iscsi, const char *value)
Handle iSCSI AuthMethod text value.
Definition iscsi.c:927
static struct interface_descriptor iscsi_control_desc
iSCSI SCSI command-issuing interface descriptor
Definition iscsi.c:1946
#define EPERM_INITIATOR_AUTHENTICATION
Definition iscsi.c:118
#define EPROTO_INVALID_CHAP_IDENTIFIER
Definition iscsi.c:130
static int iscsi_handle_strings(struct iscsi_session *iscsi, const char *strings, size_t len)
Handle iSCSI strings.
Definition iscsi.c:1273
#define EINVAL_NO_INITIATOR_IQN
Definition iscsi.c:86
static int iscsi_parse_root_path(struct iscsi_session *iscsi, const char *root_path)
Parse iSCSI root path.
Definition iscsi.c:2026
#define ENOTSUP_DISCOVERY
Definition iscsi.c:110
static int iscsi_rx_r2t(struct iscsi_session *iscsi, const void *data __unused, size_t len __unused, size_t remaining __unused)
Receive data segment of an iSCSI R2T PDU.
Definition iscsi.c:508
static void iscsi_command_close(struct iscsi_session *iscsi, int rc)
Close iSCSI command.
Definition iscsi.c:1955
static int iscsi_fetch_settings(struct iscsi_session *iscsi)
Fetch iSCSI settings.
Definition iscsi.c:2098
static int iscsi_status_to_rc(unsigned int status_class, unsigned int status_detail)
Convert iSCSI response status to return status code.
Definition iscsi.c:1300
static struct interface_operation iscsi_control_op[]
iSCSI SCSI command-issuing interface operations
Definition iscsi.c:1936
static void iscsi_new_itt(struct iscsi_session *iscsi)
Assign new iSCSI initiator task tag.
Definition iscsi.c:245
static void iscsi_close(struct iscsi_session *iscsi, int rc)
Shut down iSCSI interface.
Definition iscsi.c:224
#define EINVAL_NO_ROOT_PATH
Definition iscsi.c:78
static void iscsi_scsi_done(struct iscsi_session *iscsi, int rc, struct scsi_rsp *rsp)
Mark iSCSI SCSI operation as complete.
Definition iscsi.c:339
static int iscsi_tx_login_request(struct iscsi_session *iscsi)
Transmit data segment of an iSCSI login request PDU.
Definition iscsi.c:848
static int iscsi_build_login_request_strings(struct iscsi_session *iscsi, void *data, size_t len)
Build iSCSI login request strings.
Definition iscsi.c:710
static struct process_descriptor iscsi_process_desc
iSCSI TX process descriptor
Definition iscsi.c:1616
#define EINVAL_BAD_CREDENTIAL_MIX
Definition iscsi.c:74
static struct interface_descriptor iscsi_data_desc
iSCSI SCSI command interface descriptor
Definition iscsi.c:1973
static int iscsi_rx_nop_in(struct iscsi_session *iscsi, const void *data __unused, size_t len __unused, size_t remaining __unused)
Receive data segment of an iSCSI NOP-In.
Definition iscsi.c:635
#define EPROTO_INVALID_KEY_VALUE_PAIR
Definition iscsi.c:142
static void iscsi_rx_buffered_data_done(struct iscsi_session *iscsi)
Finish receiving PDU data into buffer.
Definition iscsi.c:161
static int iscsi_scsi_command(struct iscsi_session *iscsi, struct interface *parent, struct scsi_cmd *command)
Issue iSCSI SCSI command.
Definition iscsi.c:1876
#define EPROTO_INVALID_CHAP_RESPONSE
Definition iscsi.c:138
static struct acpi_descriptor * iscsi_describe(struct iscsi_session *iscsi)
Get iSCSI ACPI descriptor.
Definition iscsi.c:1930
static int iscsi_rx_bhs(struct iscsi_session *iscsi, const void *data, size_t len, size_t remaining __unused)
Receive basic header segment of an iSCSI PDU.
Definition iscsi.c:1631
static int iscsi_large_binary_decode(const char *encoded, uint8_t *raw, size_t len)
Decode large binary value.
Definition iscsi.c:874
static void iscsi_start_tx(struct iscsi_session *iscsi)
Start up a new TX PDU.
Definition iscsi.c:1457
static int iscsi_handle_chap_r_value(struct iscsi_session *iscsi, const char *value)
Handle iSCSI CHAP_R text value.
Definition iscsi.c:1121
static int iscsi_rx_buffered_data(struct iscsi_session *iscsi, const void *data, size_t len)
Receive PDU data into buffer.
Definition iscsi.c:179
static int iscsi_handle_chap_n_value(struct iscsi_session *iscsi, const char *value)
Handle iSCSI CHAP_N text value.
Definition iscsi.c:1092
static int iscsi_handle_targetaddress_value(struct iscsi_session *iscsi, const char *value)
Handle iSCSI TargetAddress text value.
Definition iscsi.c:897
#define EIO_TARGET_UNAVAILABLE
Definition iscsi.c:94
#define ENOTSUP_OPCODE
Definition iscsi.c:106
static int iscsi_open(struct interface *parent, struct uri *uri)
Open iSCSI URI.
Definition iscsi.c:2192
#define EPROTO_INVALID_CHAP_ALGORITHM
Definition iscsi.c:126
static int iscsi_tx_data_out(struct iscsi_session *iscsi)
Send iSCSI data-out data segment.
Definition iscsi.c:583
static void iscsi_data_out_done(struct iscsi_session *iscsi)
Complete iSCSI data-out PDU transmission.
Definition iscsi.c:567
static int iscsi_open_connection(struct iscsi_session *iscsi)
Open iSCSI transport-layer connection.
Definition iscsi.c:257
static int iscsi_handle_string(struct iscsi_session *iscsi, const char *string)
Handle iSCSI string.
Definition iscsi.c:1223
static void iscsi_close_connection(struct iscsi_session *iscsi, int rc)
Close iSCSI transport-layer connection.
Definition iscsi.c:307
static void iscsi_start_command(struct iscsi_session *iscsi)
Build iSCSI SCSI command BHS.
Definition iscsi.c:377
static int iscsi_tx_data(struct iscsi_session *iscsi)
Transmit data segment of an iSCSI PDU.
Definition iscsi.c:1501
static int iscsi_vredirect(struct iscsi_session *iscsi, int type, va_list args)
Handle redirection event.
Definition iscsi.c:1800
iSCSI protocol
#define ISCSI_STATUS_AUTH_FORWARD_REQUIRED
Target has requested forward (initiator) authentication.
Definition iscsi.h:706
#define ISCSI_OPCODE_LOGIN_RESPONSE
Login response opcode.
Definition iscsi.h:244
#define ISCSI_STATUS_TARGET_ERROR
Definition iscsi.h:254
#define ISCSI_LOGIN_CSG_OPERATIONAL_NEGOTIATION
Definition iscsi.h:185
#define ISCSI_RESPONSE_COMMAND_COMPLETE
SCSI command completed at target.
Definition iscsi.h:340
#define ISCSI_FLAG_IMMEDIATE
Immediate delivery.
Definition iscsi.h:100
#define ISCSI_DATA_LEN(segment_lengths)
The length of the data segment, in bytes, excluding any padding.
Definition iscsi.h:62
#define ISCSI_STATUS_SECURITY_NEGOTIATION_PHASE
iSCSI session is currently in the security negotiation phase
Definition iscsi.h:670
#define ISCSI_COMMAND_ATTR_SIMPLE
Definition iscsi.h:296
#define ISCSI_TAG_RESERVED
iSCSI reserved tag value
Definition iscsi.h:109
#define ISCSI_STATUS_STRINGS_OPERATIONAL
iSCSI session needs to send the operational negotiation strings
Definition iscsi.h:700
#define ISCSI_FIRST_BURST_LEN
Default iSCSI first burst length.
Definition iscsi.h:27
#define ISCSI_OPCODE_DATA_IN
Data-in opcode.
Definition iscsi.h:387
#define ISCSI_STATUS_OPERATIONAL_NEGOTIATION_PHASE
iSCSI session is currently in the operational parameter negotiation phase
Definition iscsi.h:677
#define ISCSI_COMMAND_FLAG_READ
Command will read data.
Definition iscsi.h:289
#define ISCSI_STATUS_INITIATOR_ERROR
Definition iscsi.h:249
#define ISCSI_OPCODE_LOGIN_REQUEST
Login request opcode.
Definition iscsi.h:174
#define ISCSI_STATUS_FULL_FEATURE_PHASE
iSCSI session is currently in the full feature phase
Definition iscsi.h:682
#define ISCSI_STATUS_AUTH_REVERSE_REQUIRED
Initiator requires target (reverse) authentication.
Definition iscsi.h:709
#define ISCSI_STATUS_PHASE_MASK
Mask for all iSCSI session phases.
Definition iscsi.h:685
#define ISCSI_STATUS_STRINGS_CHAP_ALGORITHM
iSCSI session needs to send the CHAP_A string
Definition iscsi.h:691
#define ISCSI_DATA_FLAG_OVERFLOW
Data overflow occurred.
Definition iscsi.h:393
#define ISCSI_MAX_BURST_LEN
Default iSCSI maximum burst length.
Definition iscsi.h:30
#define ISCSI_PORT
Default iSCSI port.
Definition iscsi.h:24
#define ISCSI_LOGIN_FLAG_TRANSITION
Willingness to transition to next stage.
Definition iscsi.h:177
#define ISCSI_COMMAND_FLAG_WRITE
Command will write data.
Definition iscsi.h:292
#define ISCSI_TAG_MAGIC
iSCSI tag magic marker
Definition iscsi.h:106
#define ISCSI_DATA_FLAG_STATUS
SCSI status code and overflow/underflow flags are valid.
Definition iscsi.h:399
#define ISCSI_DEFAULT_IQN_PREFIX
Default initiator IQN prefix.
Definition iscsi.h:715
#define ISCSI_OPCODE_SCSI_COMMAND
SCSI command opcode.
Definition iscsi.h:286
#define ISCSI_STATUS_TARGET_ERROR_UNAVAILABLE
Definition iscsi.h:255
#define ISCSI_LOGIN_NSG_MASK
Definition iscsi.h:189
#define ISCSI_AHS_LEN(segment_lengths)
The length of the additional header segment, in dwords.
Definition iscsi.h:58
#define ISCSI_OPCODE_DATA_OUT
Data-out opcode.
Definition iscsi.h:435
#define ISCSI_OPCODE_R2T
R2T opcode.
Definition iscsi.h:471
#define ISCSI_STATUS_STRINGS_CHAP_CHALLENGE
iSCSI session needs to send the mutual CHAP challenge
Definition iscsi.h:697
#define ISCSI_LOGIN_CSG_MASK
Definition iscsi.h:183
#define ISCSI_LOGIN_NSG_FULL_FEATURE_PHASE
Definition iscsi.h:192
#define ISCSI_STATUS_INITIATOR_ERROR_NOT_FOUND
Definition iscsi.h:252
#define ISCSI_STATUS_INITIATOR_ERROR_AUTHENTICATION
Definition iscsi.h:250
#define ISCSI_OPCODE_NOP_IN
NOP-In opcode.
Definition iscsi.h:501
#define ISCSI_STATUS_AUTH_REVERSE_OK
Target authenticated itself correctly.
Definition iscsi.h:712
#define ISCSI_STATUS_TARGET_ERROR_NO_RESOURCES
Definition iscsi.h:256
#define ISCSI_STATUS_REDIRECT
Definition iscsi.h:248
#define IANA_EN_FEN_SYSTEMS
Fen Systems Ltd.
Definition iscsi.h:202
#define ISCSI_LOGIN_NSG_OPERATIONAL_NEGOTIATION
Definition iscsi.h:191
iscsi_rx_state
State of an iSCSI RX engine.
Definition iscsi.h:533
@ ISCSI_RX_AHS
Receiving the additional header segment.
Definition iscsi.h:537
@ ISCSI_RX_DATA
Receiving the data segment.
Definition iscsi.h:539
@ ISCSI_RX_BHS
Receiving the basic header segment.
Definition iscsi.h:535
@ ISCSI_RX_DATA_PADDING
Receiving the data segment padding.
Definition iscsi.h:541
#define ISCSI_ISID_IANA
ISID IANA format marker.
Definition iscsi.h:195
#define ISCSI_STATUS_STRINGS_MASK
Mask for all iSCSI "needs to send" flags.
Definition iscsi.h:703
#define ISCSI_STATUS_INITIATOR_ERROR_AUTHORISATION
Definition iscsi.h:251
#define ISCSI_LOGIN_CSG_SECURITY_NEGOTIATION
Definition iscsi.h:184
#define ISCSI_DATA_PAD_LEN(segment_lengths)
The padding of the data segment, in bytes.
Definition iscsi.h:66
#define ISCSI_STATUS_STRINGS_CHAP_RESPONSE
iSCSI session needs to send the CHAP response
Definition iscsi.h:694
#define ISCSI_FLAG_FINAL
Final PDU of a sequence.
Definition iscsi.h:103
#define ISCSI_MAX_RECV_DATA_SEG_LEN
Default iSCSI maximum receive data segment length.
Definition iscsi.h:33
#define ISCSI_DATA_FLAG_UNDERFLOW
Data underflow occurred.
Definition iscsi.h:396
#define ISCSI_STATUS_INITIATOR_ERROR_REMOVED
Definition iscsi.h:253
#define ISCSI_STATUS_STRINGS_SECURITY
iSCSI session needs to send the initial security negotiation strings
Definition iscsi.h:688
iscsi_tx_state
State of an iSCSI TX engine.
Definition iscsi.h:521
@ ISCSI_TX_BHS
Sending the basic header segment.
Definition iscsi.h:525
@ ISCSI_TX_AHS
Sending the additional header segment.
Definition iscsi.h:527
@ ISCSI_TX_DATA
Sending the data segment.
Definition iscsi.h:529
@ ISCSI_TX_IDLE
Nothing to send.
Definition iscsi.h:523
#define ISCSI_OPCODE_MASK
Opcode mask.
Definition iscsi.h:97
#define ISCSI_SET_LENGTHS(segment_lengths, ahs_len, data_len)
Set additional header and data segment lengths.
Definition iscsi.h:70
#define ISCSI_OPCODE_SCSI_RESPONSE
SCSI response opcode.
Definition iscsi.h:337
uint64_t rsp
Definition librm.h:18
unsigned long tmp
Definition linux_pci.h:65
static struct dynamic_item username
Definition login_ui.c:36
static struct dynamic_item password
Definition login_ui.c:37
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
void * malloc(size_t size)
Allocate memory.
Definition malloc.c:621
struct digest_algorithm md5_algorithm
struct mschapv2_challenge peer
Peer challenge.
Definition mschapv2.h:1
uint32_t string
Definition multiboot.h:2
uint32_t control
Control.
Definition myson.h:3
uint32_t end
Ending offset.
Definition netvsc.h:7
int xfer_vreopen(struct interface *intf, int type, va_list args)
Reopen location.
Definition open.c:234
Data transfer interface opening.
#define __uri_opener
Register a URI opener.
Definition open.h:68
@ LOCATION_SOCKET
Location is a socket.
Definition open.h:44
void process_del(struct process *process)
Remove process from process list.
Definition process.c:80
void process_add(struct process *process)
Add process to process list.
Definition process.c:60
Processes.
static void process_init_stopped(struct process *process, struct process_descriptor *desc, struct refcnt *refcnt)
Initialise process without adding to process list.
Definition process.h:146
#define PROC_DESC(object_type, process, _step)
Define a process descriptor.
Definition process.h:83
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition random.c:32
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition refcnt.h:65
int xfer_open_named_socket(struct interface *xfer, int semantics, struct sockaddr *peer, const char *name, struct sockaddr *local)
Open named socket.
Definition resolv.c:403
int scsi_open(struct interface *block, struct interface *scsi, struct scsi_lun *lun)
Open SCSI device.
Definition scsi.c:987
void scsi_response(struct interface *intf, struct scsi_rsp *response)
Report SCSI response.
Definition scsi.c:207
void scsi_parse_sense(const void *data, size_t len, struct scsi_sns_descriptor *sense)
Parse SCSI sense data.
Definition scsi.c:147
int scsi_parse_lun(const char *lun_string, struct scsi_lun *lun)
Parse SCSI LUN.
Definition scsi.c:118
SCSI devices.
int fetch_string_setting_copy(struct settings *settings, const struct setting *setting, char **data)
Fetch value of string setting.
Definition settings.c:874
int fetch_uuid_setting(struct settings *settings, const struct setting *setting, union uuid *uuid)
Fetch value of UUID setting.
Definition settings.c:1085
Socket addresses.
#define va_copy(dest, src)
Definition stdarg.h:11
#define va_arg(ap, type)
Definition stdarg.h:9
#define va_end(ap)
Definition stdarg.h:10
__builtin_va_list va_list
Definition stdarg.h:7
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition string.c:516
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
char * strchr(const char *src, int character)
Find character within a string.
Definition string.c:288
char * strdup(const char *src)
Duplicate string.
Definition string.c:425
int strncmp(const char *first, const char *second, size_t max)
Compare strings.
Definition string.c:187
size_t strnlen(const char *src, size_t max)
Get length of string.
Definition string.c:256
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
An ACPI descriptor (used to construct ACPI tables).
Definition acpi.h:295
Block device capacity.
Definition blockdev.h:18
unsigned int max_count
Maximum number of blocks per single transfer.
Definition blockdev.h:24
size_t blksize
Block size.
Definition blockdev.h:22
uint8_t * response
CHAP response.
Definition chap.h:25
size_t response_len
Length of CHAP response.
Definition chap.h:27
A command-line command.
Definition command.h:10
An object interface descriptor.
Definition interface.h:56
An object interface operation.
Definition interface.h:18
An object interface.
Definition interface.h:125
A persistent I/O buffer.
Definition iobuf.h:38
void * data
Start of data.
Definition iobuf.h:53
iSCSI basic header segment common request fields
Definition iscsi.h:115
uint32_t expcmdsn
Expected command sequence number.
Definition iscsi.h:133
uint8_t opcode
Opcode.
Definition iscsi.h:117
uint32_t statsn
Status sequence number.
Definition iscsi.h:131
iSCSI basic header segment common fields
Definition iscsi.h:79
uint8_t opcode
Opcode.
Definition iscsi.h:81
union iscsi_segment_lengths lengths
Segment lengths.
Definition iscsi.h:87
iSCSI data-in basic header segment
Definition iscsi.h:355
uint32_t offset
Buffer offset.
Definition iscsi.h:381
uint8_t flags
Flags.
Definition iscsi.h:359
iSCSI data-out basic header segment
Definition iscsi.h:405
uint8_t opcode
Opcode.
Definition iscsi.h:407
union iscsi_segment_lengths lengths
Segment lengths.
Definition iscsi.h:413
uint32_t datasn
Data sequence number.
Definition iscsi.h:427
struct scsi_lun lun
Logical Unit Number.
Definition iscsi.h:415
uint32_t itt
Initiator Task Tag.
Definition iscsi.h:417
uint32_t expstatsn
Expected status sequence number.
Definition iscsi.h:423
uint32_t ttt
Target Transfer Tag.
Definition iscsi.h:419
uint32_t offset
Buffer offset.
Definition iscsi.h:429
uint8_t flags
Flags.
Definition iscsi.h:409
iSCSI login request basic header segment
Definition iscsi.h:142
iSCSI login response basic header segment
Definition iscsi.h:208
uint8_t status_detail
Status detail.
Definition iscsi.h:238
uint8_t status_class
Status class.
Definition iscsi.h:236
uint8_t flags
Flags.
Definition iscsi.h:212
iSCSI request to transfer basic header segment
Definition iscsi.h:441
uint32_t ttt
Target Transfer Tag.
Definition iscsi.h:455
uint32_t len
Desired data transfer length.
Definition iscsi.h:467
uint32_t offset
Buffer offset.
Definition iscsi.h:465
iSCSI SCSI command basic header segment
Definition iscsi.h:262
iSCSI SCSI response basic header segment
Definition iscsi.h:305
uint8_t status
SCSI status code.
Definition iscsi.h:313
uint32_t residual_count
Residual count.
Definition iscsi.h:333
uint8_t response
Response code.
Definition iscsi.h:311
uint8_t flags
Flags.
Definition iscsi.h:309
union iscsi_segment_lengths lengths
Segment lengths.
Definition iscsi.h:315
iSCSI NOP-In basic header segment
Definition iscsi.h:477
uint32_t ttt
Target Transfer Tag.
Definition iscsi.h:489
An iSCSI session.
Definition iscsi.h:545
void * rx_buffer
Buffer for received data (not always used).
Definition iscsi.h:656
char * target_iqn
Target IQN.
Definition iscsi.h:563
char * initiator_password
Initiator password (if any).
Definition iscsi.h:575
union iscsi_bhs tx_bhs
Basic header segment for current TX PDU.
Definition iscsi.h:641
uint32_t cmdsn
Command sequence number.
Definition iscsi.h:630
uint32_t ttt
Target transfer tag.
Definition iscsi.h:610
enum iscsi_rx_state rx_state
State of the RX engine.
Definition iscsi.h:650
struct scsi_lun lun
SCSI LUN (for boot firmware table).
Definition iscsi.h:664
uint32_t transfer_len
Transfer length.
Definition iscsi.h:622
char * target_password
Target password (if any).
Definition iscsi.h:579
unsigned char chap_challenge[17]
CHAP challenge (for target auth only).
Definition iscsi.h:586
uint32_t transfer_offset
Transfer offset.
Definition iscsi.h:616
union iscsi_bhs rx_bhs
Basic header segment for current RX PDU.
Definition iscsi.h:648
struct refcnt refcnt
Reference counter.
Definition iscsi.h:547
struct sockaddr target_sockaddr
Target socket address (for boot firmware table).
Definition iscsi.h:662
size_t max_burst_len
Maximum burst length.
Definition iscsi.h:591
size_t rx_offset
Byte offset within the current RX state.
Definition iscsi.h:652
char * initiator_username
Initiator username (if any).
Definition iscsi.h:573
char * initiator_iqn
Initiator IQN.
Definition iscsi.h:557
struct interface control
SCSI command-issuing interface.
Definition iscsi.h:550
enum iscsi_tx_state tx_state
State of the TX engine.
Definition iscsi.h:643
char * target_username
Target username (if any).
Definition iscsi.h:577
uint32_t itt
Initiator task tag.
Definition iscsi.h:604
struct chap_response chap
CHAP response (used for both initiator and target auth).
Definition iscsi.h:588
uint16_t isid_iana_qual
Initiator session ID (IANA format) qualifier.
Definition iscsi.h:598
int status
Session status.
Definition iscsi.h:570
uint32_t statsn
Status sequence number.
Definition iscsi.h:638
unsigned int target_port
Target port.
Definition iscsi.h:561
struct interface data
SCSI command interface.
Definition iscsi.h:552
size_t rx_len
Length of the current RX state.
Definition iscsi.h:654
struct process process
TX process.
Definition iscsi.h:645
struct scsi_cmd * command
Current SCSI command, if any.
Definition iscsi.h:659
struct acpi_descriptor desc
ACPI descriptor.
Definition iscsi.h:666
char * target_address
Target address.
Definition iscsi.h:559
struct interface socket
Transport-layer socket.
Definition iscsi.h:554
An iSCSI text string that we want to handle.
Definition iscsi.c:1187
const char * key
String key.
Definition iscsi.c:1193
int(* handle)(struct iscsi_session *iscsi, const char *value)
Handle iSCSI string value.
Definition iscsi.c:1200
A process descriptor.
Definition process.h:32
A process.
Definition process.h:18
A reference counter.
Definition refcnt.h:27
A SCSI command information unit.
Definition scsi.h:250
struct scsi_lun lun
LUN.
Definition scsi.h:252
struct xfer_buffer data_in
Data-in buffer.
Definition scsi.h:258
union scsi_cdb cdb
CDB for this command.
Definition scsi.h:254
struct xfer_buffer data_out
Data-out buffer.
Definition scsi.h:256
A SCSI command.
Definition scsi.c:263
A SCSI response information unit.
Definition scsi.h:313
A setting.
Definition settings.h:24
TCP/IP socket address.
Definition tcpip.h:76
uint16_t st_port
TCP/IP port.
Definition tcpip.h:82
Generalized socket address structure.
Definition socket.h:97
A URI opener.
Definition open.h:48
A Uniform Resource Identifier.
Definition uri.h:65
const char * opaque
Opaque part.
Definition uri.h:71
size_t len
Size of data.
Definition xferbuf.h:25
Data transfer metadata.
Definition xfer.h:23
uint32_t data_len
Microcode data size (or 0 to indicate 2000 bytes).
Definition ucode.h:15
struct iscsi_bhs_r2t r2t
Definition iscsi.h:515
struct iscsi_bhs_data_in data_in
Definition iscsi.h:513
unsigned char bytes[sizeof(struct iscsi_bhs_common)]
Definition iscsi.h:517
struct iscsi_bhs_login_request login_request
Definition iscsi.h:509
struct iscsi_bhs_common common
Definition iscsi.h:507
struct iscsi_bhs_scsi_response scsi_response
Definition iscsi.h:512
struct iscsi_bhs_scsi_command scsi_command
Definition iscsi.h:511
struct iscsi_bhs_common_response common_response
Definition iscsi.h:508
struct iscsi_nop_in nop_in
Definition iscsi.h:516
struct iscsi_bhs_login_response login_response
Definition iscsi.h:510
struct iscsi_bhs_data_out data_out
Definition iscsi.h:514
A universally unique ID.
Definition uuid.h:16
Uniform Resource Identifiers.
const char * uuid_ntoa(const union uuid *uuid)
Convert UUID to printable string.
Definition uuid.c:46
int ssnprintf(char *buf, ssize_t ssize, const char *fmt,...)
Version of vsnprintf() that accepts a signed buffer size.
Definition vsprintf.c:421
printf() and friends
u8 tx[WPA_TKIP_MIC_KEY_LEN]
MIC key for packets to the AP.
Definition wpa.h:4
u8 rx[WPA_TKIP_MIC_KEY_LEN]
MIC key for packets from the AP.
Definition wpa.h:1
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition xfer.c:117
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition xfer.c:195
struct io_buffer * xfer_alloc_iob(struct interface *intf, size_t len)
Allocate I/O buffer.
Definition xfer.c:159
void xfer_window_changed(struct interface *intf)
Report change of flow control window.
Definition xfer.c:147
int xfer_vredirect(struct interface *intf, int type, va_list args)
Send redirection event.
Definition xfer.c:63
int xfer_deliver_iob(struct interface *intf, struct io_buffer *iobuf)
Deliver datagram as I/O buffer without metadata.
Definition xfer.c:256
int xfer_deliver_raw(struct interface *intf, const void *data, size_t len)
Deliver datagram as raw data without metadata.
Definition xfer.c:289
Data transfer interfaces.
int xferbuf_read(struct xfer_buffer *xferbuf, size_t offset, void *data, size_t len)
Read from data transfer buffer.
Definition xferbuf.c:138
int xferbuf_write(struct xfer_buffer *xferbuf, size_t offset, const void *data, size_t len)
Write to data transfer buffer.
Definition xferbuf.c:105