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