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