VirtualBox

source: vbox/trunk/src/VBox/Storage/ISCSI.cpp@ 67954

Last change on this file since 67954 was 66494, checked in by vboxsync, 7 years ago

Storage: Get rid of duplicated code in the backends for unsupported features

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 197.3 KB
Line 
1/* $Id: ISCSI.cpp 66494 2017-04-10 09:57:25Z vboxsync $ */
2/** @file
3 * iSCSI initiator driver, VD backend.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VD_ISCSI
23#include <VBox/vd-plugin.h>
24#include <VBox/err.h>
25
26#include <VBox/log.h>
27#include <iprt/alloc.h>
28#include <iprt/assert.h>
29#include <iprt/uuid.h>
30#include <iprt/string.h>
31#include <iprt/asm.h>
32#include <iprt/thread.h>
33#include <iprt/semaphore.h>
34#include <iprt/md5.h>
35#include <iprt/tcp.h>
36#include <iprt/time.h>
37#include <VBox/scsi.h>
38
39#include "VDBackends.h"
40#include "VDBackendsInline.h"
41
42
43/*********************************************************************************************************************************
44* Defined Constants And Macros *
45*********************************************************************************************************************************/
46
47/** The maximum number of release log entries per image. */
48#define MAX_LOG_REL_ERRORS 1024
49
50/** Default port number to use for iSCSI. */
51#define ISCSI_DEFAULT_PORT 3260
52
53
54/** Converts a number in the range of 0 - 15 into the corresponding hex char. */
55#define NUM_2_HEX(b) ('0' + (b) + (((b) > 9) ? 39 : 0))
56/** Converts a hex char into the corresponding number in the range 0-15. */
57#define HEX_2_NUM(c) (((c) <= '9') ? ((c) - '0') : (((c - 'A' + 10) & 0xf)))
58/* Converts a base64 char into the corresponding number in the range 0-63. */
59#define B64_2_NUM(c) ((c >= 'A' && c <= 'Z') ? (c - 'A') : (c >= 'a' && c <= 'z') ? (c - 'a' + 26) : (c >= '0' && c <= '9') ? (c - '0' + 52) : (c == '+') ? 62 : (c == '/') ? 63 : -1)
60
61
62/** Minimum CHAP_MD5 challenge length in bytes. */
63#define CHAP_MD5_CHALLENGE_MIN 16
64/** Maximum CHAP_MD5 challenge length in bytes. */
65#define CHAP_MD5_CHALLENGE_MAX 24
66
67
68/**
69 * SCSI peripheral device type. */
70typedef enum SCSIDEVTYPE
71{
72 /** direct-access device. */
73 SCSI_DEVTYPE_DISK = 0,
74 /** sequential-access device. */
75 SCSI_DEVTYPE_TAPE,
76 /** printer device. */
77 SCSI_DEVTYPE_PRINTER,
78 /** processor device. */
79 SCSI_DEVTYPE_PROCESSOR,
80 /** write-once device. */
81 SCSI_DEVTYPE_WORM,
82 /** CD/DVD device. */
83 SCSI_DEVTYPE_CDROM,
84 /** scanner device. */
85 SCSI_DEVTYPE_SCANNER,
86 /** optical memory device. */
87 SCSI_DEVTYPE_OPTICAL,
88 /** medium changer. */
89 SCSI_DEVTYPE_CHANGER,
90 /** communications device. */
91 SCSI_DEVTYPE_COMMUNICATION,
92 /** storage array controller device. */
93 SCSI_DEVTYPE_RAIDCTL = 0x0c,
94 /** enclosure services device. */
95 SCSI_DEVTYPE_ENCLOSURE,
96 /** simplified direct-access device. */
97 SCSI_DEVTYPE_SIMPLEDISK,
98 /** optical card reader/writer device. */
99 SCSI_DEVTYPE_OCRW,
100 /** bridge controller device. */
101 SCSI_DEVTYPE_BRIDGE,
102 /** object-based storage device. */
103 SCSI_DEVTYPE_OSD
104} SCSIDEVTYPE;
105
106/** Mask for extracting the SCSI device type out of the first byte of the INQUIRY response. */
107#define SCSI_DEVTYPE_MASK 0x1f
108
109/** Mask to extract the CmdQue bit out of the seventh byte of the INQUIRY response. */
110#define SCSI_INQUIRY_CMDQUE_MASK 0x02
111
112/** Maximum PDU payload size we can handle in one piece. Greater or equal than
113 * s_iscsiConfigDefaultWriteSplit. */
114#define ISCSI_DATA_LENGTH_MAX _256K
115
116/** Maximum PDU size we can handle in one piece. */
117#define ISCSI_RECV_PDU_BUFFER_SIZE (ISCSI_DATA_LENGTH_MAX + ISCSI_BHS_SIZE)
118
119
120/** Version of the iSCSI standard which this initiator driver can handle. */
121#define ISCSI_MY_VERSION 0
122
123
124/** Length of ISCSI basic header segment. */
125#define ISCSI_BHS_SIZE 48
126
127
128/** Reserved task tag value. */
129#define ISCSI_TASK_TAG_RSVD 0xffffffff
130
131
132/**
133 * iSCSI opcodes. */
134typedef enum ISCSIOPCODE
135{
136 /** NOP-Out. */
137 ISCSIOP_NOP_OUT = 0x00000000,
138 /** SCSI command. */
139 ISCSIOP_SCSI_CMD = 0x01000000,
140 /** SCSI task management request. */
141 ISCSIOP_SCSI_TASKMGMT_REQ = 0x02000000,
142 /** Login request. */
143 ISCSIOP_LOGIN_REQ = 0x03000000,
144 /** Text request. */
145 ISCSIOP_TEXT_REQ = 0x04000000,
146 /** SCSI Data-Out. */
147 ISCSIOP_SCSI_DATA_OUT = 0x05000000,
148 /** Logout request. */
149 ISCSIOP_LOGOUT_REQ = 0x06000000,
150 /** SNACK request. */
151 ISCSIOP_SNACK_REQ = 0x10000000,
152
153 /** NOP-In. */
154 ISCSIOP_NOP_IN = 0x20000000,
155 /** SCSI response. */
156 ISCSIOP_SCSI_RES = 0x21000000,
157 /** SCSI Task Management response. */
158 ISCSIOP_SCSI_TASKMGMT_RES = 0x22000000,
159 /** Login response. */
160 ISCSIOP_LOGIN_RES = 0x23000000,
161 /** Text response. */
162 ISCSIOP_TEXT_RES = 0x24000000,
163 /** SCSI Data-In. */
164 ISCSIOP_SCSI_DATA_IN = 0x25000000,
165 /** Logout response. */
166 ISCSIOP_LOGOUT_RES = 0x26000000,
167 /** Ready To Transfer (R2T). */
168 ISCSIOP_R2T = 0x31000000,
169 /** Asynchronous message. */
170 ISCSIOP_ASYN_MSG = 0x32000000,
171 /** Reject. */
172 ISCSIOP_REJECT = 0x3f000000
173} ISCSIOPCODE;
174
175/** Mask for extracting the iSCSI opcode out of the first header word. */
176#define ISCSIOP_MASK 0x3f000000
177
178
179/** ISCSI BHS word 0: Request should be processed immediately. */
180#define ISCSI_IMMEDIATE_DELIVERY_BIT 0x40000000
181
182/** ISCSI BHS word 0: This is the final PDU for this request/response. */
183#define ISCSI_FINAL_BIT 0x00800000
184/** ISCSI BHS word 0: Mask for extracting the CSG. */
185#define ISCSI_CSG_MASK 0x000c0000
186/** ISCSI BHS word 0: Shift offset for extracting the CSG. */
187#define ISCSI_CSG_SHIFT 18
188/** ISCSI BHS word 0: Mask for extracting the NSG. */
189#define ISCSI_NSG_MASK 0x00030000
190/** ISCSI BHS word 0: Shift offset for extracting the NSG. */
191#define ISCSI_NSG_SHIFT 16
192
193/** ISCSI BHS word 0: task attribute untagged */
194#define ISCSI_TASK_ATTR_UNTAGGED 0x00000000
195/** ISCSI BHS word 0: task attribute simple */
196#define ISCSI_TASK_ATTR_SIMPLE 0x00010000
197/** ISCSI BHS word 0: task attribute ordered */
198#define ISCSI_TASK_ATTR_ORDERED 0x00020000
199/** ISCSI BHS word 0: task attribute head of queue */
200#define ISCSI_TASK_ATTR_HOQ 0x00030000
201/** ISCSI BHS word 0: task attribute ACA */
202#define ISCSI_TASK_ATTR_ACA 0x00040000
203
204/** ISCSI BHS word 0: transit to next login phase. */
205#define ISCSI_TRANSIT_BIT 0x00800000
206/** ISCSI BHS word 0: continue with login negotiation. */
207#define ISCSI_CONTINUE_BIT 0x00400000
208
209/** ISCSI BHS word 0: residual underflow. */
210#define ISCSI_RESIDUAL_UNFL_BIT 0x00020000
211/** ISCSI BHS word 0: residual overflow. */
212#define ISCSI_RESIDUAL_OVFL_BIT 0x00040000
213/** ISCSI BHS word 0: Bidirectional read residual underflow. */
214#define ISCSI_BI_READ_RESIDUAL_UNFL_BIT 0x00080000
215/** ISCSI BHS word 0: Bidirectional read residual overflow. */
216#define ISCSI_BI_READ_RESIDUAL_OVFL_BIT 0x00100000
217
218/** ISCSI BHS word 0: SCSI response mask. */
219#define ISCSI_SCSI_RESPONSE_MASK 0x0000ff00
220/** ISCSI BHS word 0: SCSI status mask. */
221#define ISCSI_SCSI_STATUS_MASK 0x000000ff
222
223/** ISCSI BHS word 0: response includes status. */
224#define ISCSI_STATUS_BIT 0x00010000
225
226/** Maximum number of scatter/gather segments needed to send a PDU. */
227#define ISCSI_SG_SEGMENTS_MAX 4
228
229/** Number of entries in the command table. */
230#define ISCSI_CMD_WAITING_ENTRIES 32
231
232/**
233 * iSCSI login status class. */
234typedef enum ISCSILOGINSTATUSCLASS
235{
236 /** Success. */
237 ISCSI_LOGIN_STATUS_CLASS_SUCCESS = 0,
238 /** Redirection. */
239 ISCSI_LOGIN_STATUS_CLASS_REDIRECTION,
240 /** Initiator error. */
241 ISCSI_LOGIN_STATUS_CLASS_INITIATOR_ERROR,
242 /** Target error. */
243 ISCSI_LOGIN_STATUS_CLASS_TARGET_ERROR
244} ISCSILOGINSTATUSCLASS;
245
246
247/**
248 * iSCSI connection state. */
249typedef enum ISCSISTATE
250{
251 /** Not having a connection/session at all. */
252 ISCSISTATE_FREE,
253 /** Currently trying to login. */
254 ISCSISTATE_IN_LOGIN,
255 /** Normal operation, corresponds roughly to the Full Feature Phase. */
256 ISCSISTATE_NORMAL,
257 /** Currently trying to logout. */
258 ISCSISTATE_IN_LOGOUT
259} ISCSISTATE;
260
261/**
262 * iSCSI PDU send/receive flags (and maybe more in the future). */
263typedef enum ISCSIPDUFLAGS
264{
265 /** No special flags */
266 ISCSIPDU_DEFAULT = 0,
267 /** Do not attempt to re-attach to the target if the connection is lost */
268 ISCSIPDU_NO_REATTACH = RT_BIT(1)
269} ISCSIPDUFLAGS;
270
271
272/*********************************************************************************************************************************
273* Structures and Typedefs *
274*********************************************************************************************************************************/
275
276/**
277 * iSCSI login negotiation parameter
278 */
279typedef struct ISCSIPARAMETER
280{
281 /** Name of the parameter. */
282 const char *pszParamName;
283 /** Value of the parameter. */
284 const char *pszParamValue;
285 /** Length of the binary parameter. 0=zero-terminated string. */
286 size_t cbParamValue;
287} ISCSIPARAMETER;
288
289
290/**
291 * iSCSI Response PDU buffer (scatter).
292 */
293typedef struct ISCSIRES
294{
295 /** Length of PDU segment. */
296 size_t cbSeg;
297 /** Pointer to PDU segment. */
298 void *pvSeg;
299} ISCSIRES;
300/** Pointer to an iSCSI Response PDU buffer. */
301typedef ISCSIRES *PISCSIRES;
302/** Pointer to a const iSCSI Response PDU buffer. */
303typedef ISCSIRES const *PCISCSIRES;
304
305
306/**
307 * iSCSI Request PDU buffer (gather).
308 */
309typedef struct ISCSIREQ
310{
311 /** Length of PDU segment in bytes. */
312 size_t cbSeg;
313 /** Pointer to PDU segment. */
314 const void *pcvSeg;
315} ISCSIREQ;
316/** Pointer to an iSCSI Request PDU buffer. */
317typedef ISCSIREQ *PISCSIREQ;
318/** Pointer to a const iSCSI Request PDU buffer. */
319typedef ISCSIREQ const *PCISCSIREQ;
320
321
322/**
323 * SCSI transfer directions.
324 */
325typedef enum SCSIXFER
326{
327 SCSIXFER_NONE = 0,
328 SCSIXFER_TO_TARGET,
329 SCSIXFER_FROM_TARGET,
330 SCSIXFER_TO_FROM_TARGET
331} SCSIXFER, *PSCSIXFER;
332
333/** Forward declaration. */
334typedef struct ISCSIIMAGE *PISCSIIMAGE;
335
336/**
337 * SCSI request structure.
338 */
339typedef struct SCSIREQ
340{
341 /** I/O context associated with this request. */
342 PVDIOCTX pIoCtx;
343 /** Transfer direction. */
344 SCSIXFER enmXfer;
345 /** Length of command block. */
346 size_t cbCDB;
347 /** Length of Initiator2Target data buffer. */
348 size_t cbI2TData;
349 /** Length of Target2Initiator data buffer. */
350 size_t cbT2IData;
351 /** Length of sense buffer
352 * This contains the number of sense bytes received upon completion. */
353 size_t cbSense;
354 /** Completion status of the command. */
355 uint8_t status;
356 /** The CDB. */
357 uint8_t abCDB[16];
358 /** The sense buffer. */
359 uint8_t abSense[96];
360 /** Status code to return if we got sense data. */
361 int rcSense;
362 /** Pointer to the Initiator2Target S/G list. */
363 PRTSGSEG paI2TSegs;
364 /** Number of entries in the I2T S/G list. */
365 unsigned cI2TSegs;
366 /** Pointer to the Target2Initiator S/G list. */
367 PRTSGSEG paT2ISegs;
368 /** Number of entries in the T2I S/G list. */
369 unsigned cT2ISegs;
370 /** S/G buffer for the target to initiator bits. */
371 RTSGBUF SgBufT2I;
372 /** Number of retries if the command completes with sense
373 * data before we return with an error.
374 */
375 unsigned cSenseRetries;
376 /** The S/G list - variable in size.
377 * This array holds both the I2T and T2I segments.
378 * The I2T segments are first and the T2I are second.
379 */
380 RTSGSEG aSegs[1];
381} SCSIREQ, *PSCSIREQ;
382
383typedef enum ISCSICMDTYPE
384{
385 /** Process a SCSI request. */
386 ISCSICMDTYPE_REQ = 0,
387 /** Call a function in the I/O thread. */
388 ISCSICMDTYPE_EXEC,
389 /** Usual 32bit hack. */
390 ISCSICMDTYPE_32BIT_HACK = 0x7fffffff
391} ISCSICMDTYPE;
392
393
394/** The command completion function. */
395typedef DECLCALLBACK(void) FNISCSICMDCOMPLETED(PISCSIIMAGE pImage, int rcReq, void *pvUser);
396/** Pointer to a command completion function. */
397typedef FNISCSICMDCOMPLETED *PFNISCSICMDCOMPLETED;
398
399/** The command execution function. */
400typedef DECLCALLBACK(int) FNISCSIEXEC(void *pvUser);
401/** Pointer to a command execution function. */
402typedef FNISCSIEXEC *PFNISCSIEXEC;
403
404/**
405 * Structure used to complete a synchronous request.
406 */
407typedef struct ISCSICMDSYNC
408{
409 /** Event semaphore to wakeup the waiting thread. */
410 RTSEMEVENT EventSem;
411 /** Status code of the command. */
412 int rcCmd;
413} ISCSICMDSYNC, *PISCSICMDSYNC;
414
415/**
416 * iSCSI command.
417 * Used to forward requests to the I/O thread
418 * if existing.
419 */
420typedef struct ISCSICMD
421{
422 /** Next one in the list. */
423 struct ISCSICMD *pNext;
424 /** Assigned ITT. */
425 uint32_t Itt;
426 /** Completion callback. */
427 PFNISCSICMDCOMPLETED pfnComplete;
428 /** Opaque user data. */
429 void *pvUser;
430 /** Command to execute. */
431 ISCSICMDTYPE enmCmdType;
432 /** Command type dependent data. */
433 union
434 {
435 /** Process a SCSI request. */
436 struct
437 {
438 /** The SCSI request to process. */
439 PSCSIREQ pScsiReq;
440 } ScsiReq;
441 /** Call a function in the I/O thread. */
442 struct
443 {
444 /** The method to execute. */
445 PFNISCSIEXEC pfnExec;
446 /** User data. */
447 void *pvUser;
448 } Exec;
449 } CmdType;
450} ISCSICMD, *PISCSICMD;
451
452/**
453 * Send iSCSI PDU.
454 * Contains all necessary data to send a PDU.
455 */
456typedef struct ISCSIPDUTX
457{
458 /** Pointer to the next PDu to send. */
459 struct ISCSIPDUTX *pNext;
460 /** The BHS. */
461 uint32_t aBHS[12];
462 /** Assigned CmdSN for this PDU. */
463 uint32_t CmdSN;
464 /** The S/G buffer used for sending. */
465 RTSGBUF SgBuf;
466 /** Number of bytes to send until the PDU completed. */
467 size_t cbSgLeft;
468 /** The iSCSI command this PDU belongs to. */
469 PISCSICMD pIScsiCmd;
470 /** Number of segments in the request segments array. */
471 unsigned cISCSIReq;
472 /** The request segments - variable in size. */
473 RTSGSEG aISCSIReq[1];
474} ISCSIPDUTX, *PISCSIPDUTX;
475
476/**
477 * Block driver instance data.
478 */
479typedef struct ISCSIIMAGE
480{
481 /** Pointer to the filename (location). Not really used. */
482 const char *pszFilename;
483 /** Pointer to the initiator name. */
484 char *pszInitiatorName;
485 /** Pointer to the target name. */
486 char *pszTargetName;
487 /** Pointer to the target address. */
488 char *pszTargetAddress;
489 /** Pointer to the user name for authenticating the Initiator. */
490 char *pszInitiatorUsername;
491 /** Pointer to the secret for authenticating the Initiator. */
492 uint8_t *pbInitiatorSecret;
493 /** Length of the secret for authenticating the Initiator. */
494 size_t cbInitiatorSecret;
495 /** Pointer to the user name for authenticating the Target. */
496 char *pszTargetUsername;
497 /** Pointer to the secret for authenticating the Initiator. */
498 uint8_t *pbTargetSecret;
499 /** Length of the secret for authenticating the Initiator. */
500 size_t cbTargetSecret;
501 /** Limit for iSCSI writes, essentially limiting the amount of data
502 * written in a single write. This is negotiated with the target, so
503 * the actual size might be smaller. */
504 uint32_t cbWriteSplit;
505 /** Initiator session identifier. */
506 uint64_t ISID;
507 /** SCSI Logical Unit Number. */
508 uint64_t LUN;
509 /** Pointer to the per-disk VD interface list. */
510 PVDINTERFACE pVDIfsDisk;
511 /** Pointer to the per-image VD interface list. */
512 PVDINTERFACE pVDIfsImage;
513 /** Error interface. */
514 PVDINTERFACEERROR pIfError;
515 /** Config interface. */
516 PVDINTERFACECONFIG pIfConfig;
517 /** I/O interface. */
518 PVDINTERFACEIOINT pIfIo;
519 /** TCP network stack interface. */
520 PVDINTERFACETCPNET pIfNet;
521 /** Image open flags. */
522 unsigned uOpenFlags;
523 /** Number of re-login retries when a connection fails. */
524 uint32_t cISCSIRetries;
525 /** Sector size on volume. */
526 uint32_t cbSector;
527 /** Size of volume in sectors. */
528 uint64_t cVolume;
529 /** Total volume size in bytes. Easier than multiplying the above values all the time. */
530 uint64_t cbSize;
531
532 /** Negotiated maximum data length when sending to target. */
533 uint32_t cbSendDataLength;
534 /** Negotiated maximum data length when receiving from target. */
535 uint32_t cbRecvDataLength;
536
537 /** Current state of the connection/session. */
538 ISCSISTATE state;
539 /** Flag whether the first Login Response PDU has been seen. */
540 bool FirstRecvPDU;
541 /** Initiator Task Tag of the last iSCSI request PDU. */
542 uint32_t ITT;
543 /** Sequence number of the last command. */
544 uint32_t CmdSN;
545 /** Sequence number of the next command expected by the target. */
546 uint32_t ExpCmdSN;
547 /** Maximum sequence number accepted by the target (determines size of window). */
548 uint32_t MaxCmdSN;
549 /** Expected sequence number of next status. */
550 uint32_t ExpStatSN;
551 /** Currently active request. */
552 PISCSIREQ paCurrReq;
553 /** Segment number of currently active request. */
554 uint32_t cnCurrReq;
555 /** Pointer to receive PDU buffer. (Freed by RT) */
556 void *pvRecvPDUBuf;
557 /** Length of receive PDU buffer. */
558 size_t cbRecvPDUBuf;
559 /** Mutex protecting against concurrent use from several threads. */
560 RTSEMMUTEX Mutex;
561
562 /** Pointer to the target hostname. */
563 char *pszHostname;
564 /** Port to use on the target host. */
565 uint32_t uPort;
566 /** Socket handle of the TCP connection. */
567 VDSOCKET Socket;
568 /** Timeout for read operations on the TCP connection (in milliseconds). */
569 uint32_t uReadTimeout;
570 /** Flag whether to automatically generate the initiator name. */
571 bool fAutomaticInitiatorName;
572 /** Flag whether to use the host IP stack or DevINIP. */
573 bool fHostIP;
574 /** Flag whether to dump malformed packets in the release log. */
575 bool fDumpMalformedPackets;
576 /** Flag whtether the target is readonly. */
577 bool fTargetReadOnly;
578 /** Flag whether to retry the connection before processing new requests. */
579 bool fTryReconnect;
580
581 /** Head of request queue */
582 PISCSICMD pScsiReqQueue;
583 /** Mutex protecting the request queue from concurrent access. */
584 RTSEMMUTEX MutexReqQueue;
585 /** I/O thread. */
586 RTTHREAD hThreadIo;
587 /** Flag whether the thread should be still running. */
588 volatile bool fRunning;
589 /* Flag whether the target supports command queuing. */
590 bool fCmdQueuingSupported;
591 /** Flag whether extended select is supported. */
592 bool fExtendedSelectSupported;
593 /** Padding used for aligning the PDUs. */
594 uint8_t aPadding[4];
595 /** Socket events to poll for. */
596 uint32_t fPollEvents;
597 /** Number of bytes to read to complete the current PDU. */
598 size_t cbRecvPDUResidual;
599 /** Current position in the PDU buffer. */
600 uint8_t *pbRecvPDUBufCur;
601 /** Flag whether we are currently reading the BHS. */
602 bool fRecvPDUBHS;
603 /** List of PDUs waiting to get transmitted. */
604 PISCSIPDUTX pIScsiPDUTxHead;
605 /** Tail of PDUs waiting to get transmitted. */
606 PISCSIPDUTX pIScsiPDUTxTail;
607 /** PDU we are currently transmitting. */
608 PISCSIPDUTX pIScsiPDUTxCur;
609 /** Number of commands waiting for an answer from the target.
610 * Used for timeout handling for poll.
611 */
612 unsigned cCmdsWaiting;
613 /** Table of commands waiting for a response from the target. */
614 PISCSICMD aCmdsWaiting[ISCSI_CMD_WAITING_ENTRIES];
615 /** Number of logins since last successful I/O.
616 * Used to catch the case where logging succeeds but
617 * processing read/write/flushes cause a disconnect.
618 */
619 volatile uint32_t cLoginsSinceIo;
620
621 /** Release log counter. */
622 unsigned cLogRelErrors;
623 /** The static region list. */
624 VDREGIONLIST RegionList;
625} ISCSIIMAGE;
626
627
628/*********************************************************************************************************************************
629* Static Variables *
630*********************************************************************************************************************************/
631
632/** Default initiator basename. */
633static const char *s_iscsiDefaultInitiatorBasename = "iqn.2009-08.com.sun.virtualbox.initiator";
634
635/** Default LUN. */
636static const char *s_iscsiConfigDefaultLUN = "0";
637
638/** Default timeout, 10 seconds. */
639static const char *s_iscsiConfigDefaultTimeout = "10000";
640
641/** Default write split value, less or equal to ISCSI_DATA_LENGTH_MAX. */
642static const char *s_iscsiConfigDefaultWriteSplit = "262144";
643
644/** Default host IP stack. */
645static const char *s_iscsiConfigDefaultHostIPStack = "1";
646
647/** Default dump malformed packet configuration value. */
648static const char *s_iscsiConfigDefaultDumpMalformedPackets = "0";
649
650/** Description of all accepted config parameters. */
651static const VDCONFIGINFO s_iscsiConfigInfo[] =
652{
653 { "TargetName", NULL, VDCFGVALUETYPE_STRING, VD_CFGKEY_MANDATORY },
654 /* LUN is defined of string type to handle the "enc" prefix. */
655 { "LUN", s_iscsiConfigDefaultLUN, VDCFGVALUETYPE_STRING, VD_CFGKEY_MANDATORY },
656 { "TargetAddress", NULL, VDCFGVALUETYPE_STRING, VD_CFGKEY_MANDATORY },
657 { "InitiatorName", NULL, VDCFGVALUETYPE_STRING, 0 },
658 { "InitiatorUsername", NULL, VDCFGVALUETYPE_STRING, 0 },
659 { "InitiatorSecret", NULL, VDCFGVALUETYPE_BYTES, 0 },
660 { "TargetUsername", NULL, VDCFGVALUETYPE_STRING, VD_CFGKEY_EXPERT },
661 { "TargetSecret", NULL, VDCFGVALUETYPE_BYTES, VD_CFGKEY_EXPERT },
662 { "WriteSplit", s_iscsiConfigDefaultWriteSplit, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_EXPERT },
663 { "Timeout", s_iscsiConfigDefaultTimeout, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_EXPERT },
664 { "HostIPStack", s_iscsiConfigDefaultHostIPStack, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_EXPERT },
665 { "DumpMalformedPackets", s_iscsiConfigDefaultDumpMalformedPackets, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_EXPERT },
666 { NULL, NULL, VDCFGVALUETYPE_INTEGER, 0 }
667};
668
669
670/*********************************************************************************************************************************
671* Internal Functions *
672*********************************************************************************************************************************/
673
674/* iSCSI low-level functions (only to be used from the iSCSI high-level functions). */
675static uint32_t iscsiNewITT(PISCSIIMAGE pImage);
676static int iscsiSendPDU(PISCSIIMAGE pImage, PISCSIREQ paReq, uint32_t cnReq, uint32_t uFlags);
677static int iscsiRecvPDU(PISCSIIMAGE pImage, uint32_t itt, PISCSIRES paRes, uint32_t cnRes, uint32_t fFlags);
678static int iscsiRecvPDUAsync(PISCSIIMAGE pImage);
679static int iscsiSendPDUAsync(PISCSIIMAGE pImage);
680static int iscsiValidatePDU(PISCSIRES paRes, uint32_t cnRes);
681static int iscsiRecvPDUProcess(PISCSIIMAGE pImage, PISCSIRES paRes, uint32_t cnRes);
682static int iscsiPDUTxPrepare(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd);
683static int iscsiRecvPDUUpdateRequest(PISCSIIMAGE pImage, PISCSIRES paRes, uint32_t cnRes);
684static void iscsiCmdComplete(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd, int rcCmd);
685static int iscsiTextAddKeyValue(uint8_t *pbBuf, size_t cbBuf, size_t *pcbBufCurr, const char *pcszKey, const char *pcszValue, size_t cbValue);
686static int iscsiTextGetKeyValue(const uint8_t *pbBuf, size_t cbBuf, const char *pcszKey, const char **ppcszValue);
687static int iscsiStrToBinary(const char *pcszValue, uint8_t *pbValue, size_t *pcbValue);
688static int iscsiUpdateParameters(PISCSIIMAGE pImage, const uint8_t *pbBuf, size_t cbBuf);
689
690/* Serial number arithmetic comparison. */
691static bool serial_number_less(uint32_t sn1, uint32_t sn2);
692static bool serial_number_greater(uint32_t sn1, uint32_t sn2);
693
694/* CHAP-MD5 functions. */
695#ifdef IMPLEMENT_TARGET_AUTH
696static void chap_md5_generate_challenge(uint8_t *pbChallenge, size_t *pcbChallenge);
697#endif
698static void chap_md5_compute_response(uint8_t *pbResponse, uint8_t id, const uint8_t *pbChallenge, size_t cbChallenge,
699 const uint8_t *pbSecret, size_t cbSecret);
700
701/**
702 * Internal: release log wrapper limiting the number of entries.
703 */
704DECLINLINE(void) iscsiLogRel(PISCSIIMAGE pImage, const char *pcszFormat, ...)
705{
706 if (pImage->cLogRelErrors++ < MAX_LOG_REL_ERRORS)
707 {
708 va_list va;
709
710 va_start(va, pcszFormat);
711 LogRel(("%N\n", pcszFormat, &va));
712 va_end(va);
713 }
714}
715
716DECLINLINE(bool) iscsiIsClientConnected(PISCSIIMAGE pImage)
717{
718 return pImage->Socket != NIL_VDSOCKET
719 && pImage->pIfNet->pfnIsClientConnected(pImage->Socket);
720}
721
722/**
723 * Calculates the hash for the given ITT used
724 * to look up the command in the table.
725 */
726DECLINLINE(uint32_t) iscsiIttHash(uint32_t Itt)
727{
728 return Itt % ISCSI_CMD_WAITING_ENTRIES;
729}
730
731static PISCSICMD iscsiCmdGetFromItt(PISCSIIMAGE pImage, uint32_t Itt)
732{
733 PISCSICMD pIScsiCmd = NULL;
734
735 pIScsiCmd = pImage->aCmdsWaiting[iscsiIttHash(Itt)];
736
737 while ( pIScsiCmd
738 && pIScsiCmd->Itt != Itt)
739 pIScsiCmd = pIScsiCmd->pNext;
740
741 return pIScsiCmd;
742}
743
744static void iscsiCmdInsert(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd)
745{
746 PISCSICMD pIScsiCmdOld;
747 uint32_t idx = iscsiIttHash(pIScsiCmd->Itt);
748
749 Assert(!pIScsiCmd->pNext);
750
751 pIScsiCmdOld = pImage->aCmdsWaiting[idx];
752 pIScsiCmd->pNext = pIScsiCmdOld;
753 pImage->aCmdsWaiting[idx] = pIScsiCmd;
754 pImage->cCmdsWaiting++;
755}
756
757static PISCSICMD iscsiCmdRemove(PISCSIIMAGE pImage, uint32_t Itt)
758{
759 PISCSICMD pIScsiCmd = NULL;
760 PISCSICMD pIScsiCmdPrev = NULL;
761 uint32_t idx = iscsiIttHash(Itt);
762
763 pIScsiCmd = pImage->aCmdsWaiting[idx];
764
765 while ( pIScsiCmd
766 && pIScsiCmd->Itt != Itt)
767 {
768 pIScsiCmdPrev = pIScsiCmd;
769 pIScsiCmd = pIScsiCmd->pNext;
770 }
771
772 if (pIScsiCmd)
773 {
774 if (pIScsiCmdPrev)
775 {
776 Assert(!pIScsiCmd->pNext || VALID_PTR(pIScsiCmd->pNext));
777 pIScsiCmdPrev->pNext = pIScsiCmd->pNext;
778 }
779 else
780 {
781 pImage->aCmdsWaiting[idx] = pIScsiCmd->pNext;
782 Assert(!pImage->aCmdsWaiting[idx] || VALID_PTR(pImage->aCmdsWaiting[idx]));
783 }
784 pImage->cCmdsWaiting--;
785 }
786
787 return pIScsiCmd;
788}
789
790/**
791 * Removes all commands from the table and returns the
792 * list head
793 *
794 * @returns Pointer to the head of the command list.
795 * @param pImage iSCSI connection to use.
796 */
797static PISCSICMD iscsiCmdRemoveAll(PISCSIIMAGE pImage)
798{
799 PISCSICMD pIScsiCmdHead = NULL;
800
801 for (unsigned idx = 0; idx < RT_ELEMENTS(pImage->aCmdsWaiting); idx++)
802 {
803 PISCSICMD pHead;
804 PISCSICMD pTail;
805
806 pHead = pImage->aCmdsWaiting[idx];
807 pImage->aCmdsWaiting[idx] = NULL;
808
809 if (pHead)
810 {
811 /* Get the tail. */
812 pTail = pHead;
813 while (pTail->pNext)
814 pTail = pTail->pNext;
815
816 /* Concatenate. */
817 pTail->pNext = pIScsiCmdHead;
818 pIScsiCmdHead = pHead;
819 }
820 }
821 pImage->cCmdsWaiting = 0;
822
823 return pIScsiCmdHead;
824}
825
826/**
827 * Dumps an iSCSI packet if enabled.
828 *
829 * @returns nothing.
830 * @param pImage The iSCSI image instance data.
831 * @param paISCSISegs Pointer to the segments array.
832 * @param cnISCSISegs Number of segments in the array.
833 * @param rc Status code for this packet.
834 * @param fRequest Flag whether this is request or response packet.
835 */
836static void iscsiDumpPacket(PISCSIIMAGE pImage, PISCSIREQ paISCSISegs, unsigned cnISCSISegs, int rc, bool fRequest)
837{
838 if (pImage->fDumpMalformedPackets)
839 {
840 LogRel(("iSCSI{%s}: Dumping %s packet completed with status code %Rrc\n", pImage->pszTargetName, fRequest ? "request" : "response", rc));
841 for (unsigned i = 0; i < cnISCSISegs; i++)
842 {
843 if (paISCSISegs[i].cbSeg)
844 {
845 LogRel(("iSCSI{%s}: Segment %u, size %zu\n"
846 "%.*Rhxd\n",
847 pImage->pszTargetName, i, paISCSISegs[i].cbSeg,
848 paISCSISegs[i].cbSeg, paISCSISegs[i].pcvSeg));
849 }
850 }
851 }
852}
853
854static int iscsiTransportConnect(PISCSIIMAGE pImage)
855{
856 int rc;
857 if (!pImage->pszHostname)
858 return VERR_NET_DEST_ADDRESS_REQUIRED;
859
860 rc = pImage->pIfNet->pfnClientConnect(pImage->Socket, pImage->pszHostname, pImage->uPort, pImage->uReadTimeout);
861 if (RT_FAILURE(rc))
862 {
863 if ( rc == VERR_NET_CONNECTION_REFUSED
864 || rc == VERR_NET_CONNECTION_RESET
865 || rc == VERR_NET_UNREACHABLE
866 || rc == VERR_NET_HOST_UNREACHABLE
867 || rc == VERR_NET_CONNECTION_TIMED_OUT)
868 {
869 /* Standardize return value for no connection. */
870 rc = VERR_NET_CONNECTION_REFUSED;
871 }
872 return rc;
873 }
874
875 /* Disable Nagle algorithm, we want things to be sent immediately. */
876 pImage->pIfNet->pfnSetSendCoalescing(pImage->Socket, false);
877
878 /* Make initiator name and ISID unique on this host. */
879 RTNETADDR LocalAddr;
880 rc = pImage->pIfNet->pfnGetLocalAddress(pImage->Socket, &LocalAddr);
881 if (RT_FAILURE(rc))
882 return rc;
883 if ( LocalAddr.uPort == RTNETADDR_PORT_NA
884 || LocalAddr.uPort > 65535)
885 return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
886 pImage->ISID &= ~65535ULL;
887 pImage->ISID |= LocalAddr.uPort;
888 /* Eliminate the port so that it isn't included below. */
889 LocalAddr.uPort = RTNETADDR_PORT_NA;
890 if (pImage->fAutomaticInitiatorName)
891 {
892 if (pImage->pszInitiatorName)
893 RTStrFree(pImage->pszInitiatorName);
894 RTStrAPrintf(&pImage->pszInitiatorName, "%s:01:%RTnaddr",
895 s_iscsiDefaultInitiatorBasename, &LocalAddr);
896 if (!pImage->pszInitiatorName)
897 return VERR_NO_MEMORY;
898 }
899 LogRel(("iSCSI: connect from initiator %s with source port %u\n", pImage->pszInitiatorName, pImage->ISID & 65535));
900 return VINF_SUCCESS;
901}
902
903
904static int iscsiTransportClose(PISCSIIMAGE pImage)
905{
906 int rc;
907
908 LogFlowFunc(("(%s:%d)\n", pImage->pszHostname, pImage->uPort));
909 if (iscsiIsClientConnected(pImage))
910 {
911 LogRel(("iSCSI: disconnect from initiator %s with source port %u\n", pImage->pszInitiatorName, pImage->ISID & 65535));
912 rc = pImage->pIfNet->pfnClientClose(pImage->Socket);
913 }
914 else
915 rc = VINF_SUCCESS;
916 LogFlowFunc(("returns %Rrc\n", rc));
917 return rc;
918}
919
920
921static int iscsiTransportRead(PISCSIIMAGE pImage, PISCSIRES paResponse, unsigned int cnResponse)
922{
923 int rc = VINF_SUCCESS;
924 unsigned int i = 0;
925 size_t cbToRead, cbActuallyRead, residual, cbSegActual = 0, cbAHSLength, cbDataLength;
926 char *pDst;
927
928 LogFlowFunc(("cnResponse=%d (%s:%d)\n", cnResponse, pImage->pszHostname, pImage->uPort));
929 if (!iscsiIsClientConnected(pImage))
930 {
931 /* Reconnecting makes no sense in this case, as there will be nothing
932 * to receive. We would just run into a timeout. */
933 rc = VERR_BROKEN_PIPE;
934 }
935
936 if (RT_SUCCESS(rc) && paResponse[0].cbSeg >= ISCSI_BHS_SIZE)
937 {
938 cbToRead = 0;
939 residual = ISCSI_BHS_SIZE; /* Do not read more than the BHS length before the true PDU length is known. */
940 cbSegActual = residual;
941 pDst = (char *)paResponse[i].pvSeg;
942 uint64_t u64Timeout = RTTimeMilliTS() + pImage->uReadTimeout;
943 do
944 {
945 int64_t cMilliesRemaining = u64Timeout - RTTimeMilliTS();
946 if (cMilliesRemaining <= 0)
947 {
948 rc = VERR_TIMEOUT;
949 break;
950 }
951 Assert(cMilliesRemaining < 1000000);
952 rc = pImage->pIfNet->pfnSelectOne(pImage->Socket, cMilliesRemaining);
953 if (RT_FAILURE(rc))
954 break;
955 rc = pImage->pIfNet->pfnRead(pImage->Socket, pDst, residual, &cbActuallyRead);
956 if (RT_FAILURE(rc))
957 break;
958 if (cbActuallyRead == 0)
959 {
960 /* The other end has closed the connection. */
961 iscsiTransportClose(pImage);
962 pImage->state = ISCSISTATE_FREE;
963 rc = VERR_NET_CONNECTION_RESET;
964 break;
965 }
966 if (cbToRead == 0)
967 {
968 /* Currently reading the BHS. */
969 residual -= cbActuallyRead;
970 pDst += cbActuallyRead;
971 if (residual <= 40)
972 {
973 /* Enough data read to figure out the actual PDU size. */
974 uint32_t word1 = RT_N2H_U32(((uint32_t *)(paResponse[0].pvSeg))[1]);
975 cbAHSLength = (word1 & 0xff000000) >> 24;
976 cbAHSLength = ((cbAHSLength - 1) | 3) + 1; /* Add padding. */
977 cbDataLength = word1 & 0x00ffffff;
978 cbDataLength = ((cbDataLength - 1) | 3) + 1; /* Add padding. */
979 cbToRead = residual + cbAHSLength + cbDataLength;
980 residual += paResponse[0].cbSeg - ISCSI_BHS_SIZE;
981 if (residual > cbToRead)
982 residual = cbToRead;
983 cbSegActual = ISCSI_BHS_SIZE + cbAHSLength + cbDataLength;
984 /* Check whether we are already done with this PDU (no payload). */
985 if (cbToRead == 0)
986 break;
987 }
988 }
989 else
990 {
991 cbToRead -= cbActuallyRead;
992 if (cbToRead == 0)
993 break;
994 pDst += cbActuallyRead;
995 residual -= cbActuallyRead;
996 }
997 if (residual == 0)
998 {
999 i++;
1000 if (i >= cnResponse)
1001 {
1002 /* No space left in receive buffers. */
1003 rc = VERR_BUFFER_OVERFLOW;
1004 break;
1005 }
1006 pDst = (char *)paResponse[i].pvSeg;
1007 residual = paResponse[i].cbSeg;
1008 if (residual > cbToRead)
1009 residual = cbToRead;
1010 cbSegActual = residual;
1011 }
1012 LogFlowFunc(("cbToRead=%u residual=%u cbSegActual=%u cbActuallRead=%u\n",
1013 cbToRead, residual, cbSegActual, cbActuallyRead));
1014 } while (true);
1015 }
1016 else
1017 {
1018 if (RT_SUCCESS(rc))
1019 rc = VERR_BUFFER_OVERFLOW;
1020 }
1021 if (RT_SUCCESS(rc))
1022 {
1023 paResponse[i].cbSeg = cbSegActual;
1024 for (i++; i < cnResponse; i++)
1025 paResponse[i].cbSeg = 0;
1026 }
1027
1028 if (RT_UNLIKELY( RT_FAILURE(rc)
1029 && ( rc == VERR_NET_CONNECTION_RESET
1030 || rc == VERR_NET_CONNECTION_ABORTED
1031 || rc == VERR_NET_CONNECTION_RESET_BY_PEER
1032 || rc == VERR_NET_CONNECTION_REFUSED
1033 || rc == VERR_BROKEN_PIPE)))
1034 {
1035 /* Standardize return value for broken connection. */
1036 rc = VERR_BROKEN_PIPE;
1037 }
1038
1039 LogFlowFunc(("returns %Rrc\n", rc));
1040 return rc;
1041}
1042
1043
1044static int iscsiTransportWrite(PISCSIIMAGE pImage, PISCSIREQ paRequest, unsigned int cnRequest)
1045{
1046 int rc = VINF_SUCCESS;
1047 unsigned int i;
1048
1049 LogFlowFunc(("cnRequest=%d (%s:%d)\n", cnRequest, pImage->pszHostname, pImage->uPort));
1050 if (!iscsiIsClientConnected(pImage))
1051 {
1052 /* Attempt to reconnect if the connection was previously broken. */
1053 rc = iscsiTransportConnect(pImage);
1054 }
1055
1056 if (RT_SUCCESS(rc))
1057 {
1058 /* Construct scatter/gather buffer for entire request, worst case
1059 * needs twice as many entries to allow for padding. */
1060 unsigned cBuf = 0;
1061 for (i = 0; i < cnRequest; i++)
1062 {
1063 cBuf++;
1064 if (paRequest[i].cbSeg & 3)
1065 cBuf++;
1066 }
1067 Assert(cBuf < ISCSI_SG_SEGMENTS_MAX);
1068 RTSGBUF buf;
1069 RTSGSEG aSeg[ISCSI_SG_SEGMENTS_MAX];
1070 static char aPad[4] = { 0, 0, 0, 0 };
1071 RTSgBufInit(&buf, &aSeg[0], cBuf);
1072 unsigned iBuf = 0;
1073 for (i = 0; i < cnRequest; i++)
1074 {
1075 /* Actual data chunk. */
1076 aSeg[iBuf].pvSeg = (void *)paRequest[i].pcvSeg;
1077 aSeg[iBuf].cbSeg = paRequest[i].cbSeg;
1078 iBuf++;
1079 /* Insert proper padding before the next chunk. */
1080 if (paRequest[i].cbSeg & 3)
1081 {
1082 aSeg[iBuf].pvSeg = &aPad[0];
1083 aSeg[iBuf].cbSeg = 4 - (paRequest[i].cbSeg & 3);
1084 iBuf++;
1085 }
1086 }
1087 /* Send out the request, the socket is set to send data immediately,
1088 * avoiding unnecessary delays. */
1089 rc = pImage->pIfNet->pfnSgWrite(pImage->Socket, &buf);
1090
1091 }
1092
1093 if (RT_UNLIKELY( RT_FAILURE(rc)
1094 && ( rc == VERR_NET_CONNECTION_RESET
1095 || rc == VERR_NET_CONNECTION_ABORTED
1096 || rc == VERR_NET_CONNECTION_RESET_BY_PEER
1097 || rc == VERR_NET_CONNECTION_REFUSED
1098 || rc == VERR_BROKEN_PIPE)))
1099 {
1100 /* Standardize return value for broken connection. */
1101 rc = VERR_BROKEN_PIPE;
1102 }
1103
1104 LogFlowFunc(("returns %Rrc\n", rc));
1105 return rc;
1106}
1107
1108
1109static int iscsiTransportOpen(PISCSIIMAGE pImage)
1110{
1111 int rc = VINF_SUCCESS;
1112 size_t cbHostname = 0; /* shut up gcc */
1113 const char *pcszPort = NULL; /* shut up gcc */
1114 char *pszPortEnd;
1115 uint16_t uPort;
1116
1117 /* Clean up previous connection data. */
1118 iscsiTransportClose(pImage);
1119 if (pImage->pszHostname)
1120 {
1121 RTMemFree(pImage->pszHostname);
1122 pImage->pszHostname = NULL;
1123 pImage->uPort = 0;
1124 }
1125
1126 /* Locate the port number via the colon separating the hostname from the port. */
1127 if (*pImage->pszTargetAddress)
1128 {
1129 if (*pImage->pszTargetAddress != '[')
1130 {
1131 /* Normal hostname or IPv4 dotted decimal. */
1132 pcszPort = strchr(pImage->pszTargetAddress, ':');
1133 if (pcszPort != NULL)
1134 {
1135 cbHostname = pcszPort - pImage->pszTargetAddress;
1136 pcszPort++;
1137 }
1138 else
1139 cbHostname = strlen(pImage->pszTargetAddress);
1140 }
1141 else
1142 {
1143 /* IPv6 literal address. Contains colons, so skip to closing square bracket. */
1144 pcszPort = strchr(pImage->pszTargetAddress, ']');
1145 if (pcszPort != NULL)
1146 {
1147 pcszPort++;
1148 cbHostname = pcszPort - pImage->pszTargetAddress;
1149 if (*pcszPort == '\0')
1150 pcszPort = NULL;
1151 else if (*pcszPort != ':')
1152 rc = VERR_PARSE_ERROR;
1153 else
1154 pcszPort++;
1155 }
1156 else
1157 rc = VERR_PARSE_ERROR;
1158 }
1159 }
1160 else
1161 rc = VERR_PARSE_ERROR;
1162
1163 /* Now split address into hostname and port. */
1164 if (RT_SUCCESS(rc))
1165 {
1166 pImage->pszHostname = (char *)RTMemAlloc(cbHostname + 1);
1167 if (!pImage->pszHostname)
1168 rc = VERR_NO_MEMORY;
1169 else
1170 {
1171 if (pImage->pszTargetAddress[0] == '[')
1172 memcpy(pImage->pszHostname, pImage->pszTargetAddress + 1, cbHostname);
1173 else
1174 memcpy(pImage->pszHostname, pImage->pszTargetAddress, cbHostname);
1175 pImage->pszHostname[cbHostname] = '\0';
1176 if (pcszPort != NULL)
1177 {
1178 rc = RTStrToUInt16Ex(pcszPort, &pszPortEnd, 0, &uPort);
1179 /* Note that RT_SUCCESS() macro to check the rc value is not strict enough in this case. */
1180 if (rc == VINF_SUCCESS && *pszPortEnd == '\0' && uPort != 0)
1181 {
1182 pImage->uPort = uPort;
1183 }
1184 else
1185 {
1186 rc = VERR_PARSE_ERROR;
1187 }
1188 }
1189 else
1190 pImage->uPort = ISCSI_DEFAULT_PORT;
1191 }
1192 }
1193
1194 if (RT_SUCCESS(rc))
1195 {
1196 if (!iscsiIsClientConnected(pImage))
1197 rc = iscsiTransportConnect(pImage);
1198 }
1199 else
1200 {
1201 if (pImage->pszHostname)
1202 {
1203 RTMemFree(pImage->pszHostname);
1204 pImage->pszHostname = NULL;
1205 }
1206 pImage->uPort = 0;
1207 }
1208
1209 LogFlowFunc(("returns %Rrc\n", rc));
1210 return rc;
1211}
1212
1213/**
1214 * Returns a human readable version of the given initiator login error detail.
1215 *
1216 * @returns String with the error detail.
1217 * @param u8Detail The detail indicator from the response.
1218 */
1219static const char *iscsiGetLoginErrorDetail(uint8_t u8Detail)
1220{
1221 const char *pszDetail = NULL;
1222
1223 switch (u8Detail)
1224 {
1225 case 0x00:
1226 pszDetail = "Miscelleanous iSCSI intiaitor error";
1227 break;
1228 case 0x01:
1229 pszDetail = "Authentication failure";
1230 break;
1231 case 0x02:
1232 pszDetail = "Authorization failure";
1233 break;
1234 case 0x03:
1235 pszDetail = "Not found";
1236 break;
1237 case 0x04:
1238 pszDetail = "Target removed";
1239 break;
1240 case 0x05:
1241 pszDetail = "Unsupported version";
1242 break;
1243 case 0x06:
1244 pszDetail = "Too many connections";
1245 break;
1246 case 0x07:
1247 pszDetail = "Missing parameter";
1248 break;
1249 case 0x08:
1250 pszDetail = "Can't include in session";
1251 break;
1252 case 0x09:
1253 pszDetail = "Session type not supported";
1254 break;
1255 case 0x0a:
1256 pszDetail = "Session does not exist";
1257 break;
1258 case 0x0b:
1259 pszDetail = "Invalid request type during login";
1260 break;
1261 default:
1262 pszDetail = "Unknown status detail";
1263 }
1264
1265 return pszDetail;
1266}
1267
1268/**
1269 * Attempts one login attempt to the given target.
1270 *
1271 * @returns VBox status code.
1272 * @retval VINF_TRY_AGAIN when getting redirected and having to start over.
1273 * @retval VERR_TRY_AGAIN in case the connection was lost while receiving a reply
1274 * from the target and the login attempt can be repeated.
1275 * @param pImage The iSCSI connection state to be used.
1276 */
1277static int iscsiLogin(PISCSIIMAGE pImage)
1278{
1279 int rc = VINF_SUCCESS;
1280 uint32_t itt;
1281 uint32_t csg, nsg, substate;
1282 uint64_t isid_tsih;
1283 uint8_t bBuf[4096]; /* Should be large enough even for large authentication values. */
1284 size_t cbBuf;
1285 bool transit;
1286 uint8_t pbChallenge[1024]; /* RFC3720 specifies this as maximum. */
1287 size_t cbChallenge = 0; /* shut up gcc */
1288 uint8_t bChapIdx = 0; /* (MSC is used uninitialized) */
1289 uint8_t aResponse[RTMD5HASHSIZE];
1290 uint32_t cnISCSIReq = 0;
1291 ISCSIREQ aISCSIReq[4];
1292 uint32_t aReqBHS[12];
1293 uint32_t cnISCSIRes = 0;
1294 ISCSIRES aISCSIRes[2];
1295 uint32_t aResBHS[12];
1296 char *pszNext;
1297 bool fParameterNeg = true;
1298 pImage->cbRecvDataLength = ISCSI_DATA_LENGTH_MAX;
1299 pImage->cbSendDataLength = RT_MIN(ISCSI_DATA_LENGTH_MAX, pImage->cbWriteSplit);
1300 char szMaxDataLength[16];
1301 RTStrPrintf(szMaxDataLength, sizeof(szMaxDataLength), "%u", ISCSI_DATA_LENGTH_MAX);
1302 ISCSIPARAMETER aParameterNeg[] =
1303 {
1304 { "HeaderDigest", "None", 0 },
1305 { "DataDigest", "None", 0 },
1306 { "MaxConnections", "1", 0 },
1307 { "InitialR2T", "No", 0 },
1308 { "ImmediateData", "Yes", 0 },
1309 { "MaxRecvDataSegmentLength", szMaxDataLength, 0 },
1310 { "MaxBurstLength", szMaxDataLength, 0 },
1311 { "FirstBurstLength", szMaxDataLength, 0 },
1312 { "DefaultTime2Wait", "0", 0 },
1313 { "DefaultTime2Retain", "60", 0 },
1314 { "DataPDUInOrder", "Yes", 0 },
1315 { "DataSequenceInOrder", "Yes", 0 },
1316 { "ErrorRecoveryLevel", "0", 0 },
1317 { "MaxOutstandingR2T", "1", 0 }
1318 };
1319
1320 if (!iscsiIsClientConnected(pImage))
1321 {
1322 rc = iscsiTransportOpen(pImage);
1323 if (RT_FAILURE(rc))
1324 return rc;
1325 }
1326
1327 pImage->state = ISCSISTATE_IN_LOGIN;
1328 pImage->ITT = 1;
1329 pImage->FirstRecvPDU = true;
1330 pImage->CmdSN = 1;
1331 pImage->ExpCmdSN = 0;
1332 pImage->MaxCmdSN = 1;
1333 pImage->ExpStatSN = 0;
1334
1335 /*
1336 * Send login request to target.
1337 */
1338 itt = iscsiNewITT(pImage);
1339 csg = 0;
1340 nsg = 0;
1341 substate = 0;
1342 isid_tsih = pImage->ISID << 16; /* TSIH field currently always 0 */
1343
1344 do
1345 {
1346 transit = false;
1347 cbBuf = 0;
1348 /* Handle all cases with a single switch statement. */
1349 switch (csg << 8 | substate)
1350 {
1351 case 0x0000: /* security negotiation, step 0: propose authentication. */
1352 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "SessionType", "Normal", 0);
1353 if (RT_FAILURE(rc))
1354 break;
1355 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "InitiatorName", pImage->pszInitiatorName, 0);
1356 if (RT_FAILURE(rc))
1357 break;
1358 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "TargetName", pImage->pszTargetName, 0);
1359 if (RT_FAILURE(rc))
1360 break;
1361 if (pImage->pszInitiatorUsername == NULL)
1362 {
1363 /* No authentication. Immediately switch to next phase. */
1364 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "AuthMethod", "None", 0);
1365 if (RT_FAILURE(rc))
1366 break;
1367 nsg = 1;
1368 transit = true;
1369 }
1370 else
1371 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "AuthMethod", "CHAP,None", 0);
1372 break;
1373 case 0x0001: /* security negotiation, step 1: propose CHAP_MD5 variant. */
1374 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "CHAP_A", "5", 0);
1375 break;
1376 case 0x0002: /* security negotiation, step 2: send authentication info. */
1377 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "CHAP_N", pImage->pszInitiatorUsername, 0);
1378 if (RT_FAILURE(rc))
1379 break;
1380 chap_md5_compute_response(aResponse, bChapIdx, pbChallenge, cbChallenge,
1381 pImage->pbInitiatorSecret, pImage->cbInitiatorSecret);
1382 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "CHAP_R", (const char *)aResponse, RTMD5HASHSIZE);
1383 if (RT_FAILURE(rc))
1384 break;
1385 nsg = 1;
1386 transit = true;
1387 break;
1388 case 0x0100: /* login operational negotiation, step 0: set parameters. */
1389 if (fParameterNeg)
1390 {
1391 for (unsigned i = 0; i < RT_ELEMENTS(aParameterNeg); i++)
1392 {
1393 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf,
1394 aParameterNeg[i].pszParamName,
1395 aParameterNeg[i].pszParamValue,
1396 aParameterNeg[i].cbParamValue);
1397 if (RT_FAILURE(rc))
1398 break;
1399 }
1400 fParameterNeg = false;
1401 }
1402
1403 nsg = 3;
1404 transit = true;
1405 break;
1406 case 0x0300: /* full feature phase. */
1407 default:
1408 /* Should never come here. */
1409 AssertMsgFailed(("send: Undefined login state %d substate %d\n", csg, substate));
1410 break;
1411 }
1412
1413 if (RT_FAILURE(rc))
1414 break;
1415
1416 aReqBHS[0] = RT_H2N_U32( ISCSI_IMMEDIATE_DELIVERY_BIT
1417 | (csg << ISCSI_CSG_SHIFT)
1418 | (transit ? (nsg << ISCSI_NSG_SHIFT | ISCSI_TRANSIT_BIT) : 0)
1419 | ISCSI_MY_VERSION /* Minimum version. */
1420 | (ISCSI_MY_VERSION << 8) /* Maximum version. */
1421 | ISCSIOP_LOGIN_REQ); /* C=0 */
1422 aReqBHS[1] = RT_H2N_U32((uint32_t)cbBuf); /* TotalAHSLength=0 */
1423 aReqBHS[2] = RT_H2N_U32(isid_tsih >> 32);
1424 aReqBHS[3] = RT_H2N_U32(isid_tsih & 0xffffffff);
1425 aReqBHS[4] = itt;
1426 aReqBHS[5] = RT_H2N_U32(1 << 16); /* CID=1,reserved */
1427 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
1428 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
1429 aReqBHS[8] = 0; /* reserved */
1430 aReqBHS[9] = 0; /* reserved */
1431 aReqBHS[10] = 0; /* reserved */
1432 aReqBHS[11] = 0; /* reserved */
1433
1434 cnISCSIReq = 0;
1435 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
1436 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
1437 cnISCSIReq++;
1438
1439 aISCSIReq[cnISCSIReq].pcvSeg = bBuf;
1440 aISCSIReq[cnISCSIReq].cbSeg = cbBuf;
1441 cnISCSIReq++;
1442
1443 rc = iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq, ISCSIPDU_NO_REATTACH);
1444 if (RT_SUCCESS(rc))
1445 {
1446 ISCSIOPCODE cmd;
1447 ISCSILOGINSTATUSCLASS loginStatusClass;
1448
1449 cnISCSIRes = 0;
1450 aISCSIRes[cnISCSIRes].pvSeg = aResBHS;
1451 aISCSIRes[cnISCSIRes].cbSeg = sizeof(aResBHS);
1452 cnISCSIRes++;
1453 aISCSIRes[cnISCSIRes].pvSeg = bBuf;
1454 aISCSIRes[cnISCSIRes].cbSeg = sizeof(bBuf);
1455 cnISCSIRes++;
1456
1457 rc = iscsiRecvPDU(pImage, itt, aISCSIRes, cnISCSIRes, ISCSIPDU_NO_REATTACH);
1458 if (RT_FAILURE(rc))
1459 {
1460 /*
1461 * We lost connection to the target while receiving the answer,
1462 * start from the beginning.
1463 */
1464 if (rc == VERR_BROKEN_PIPE || rc == VERR_NET_CONNECTION_REFUSED)
1465 rc = VERR_TRY_AGAIN;
1466 break;
1467 }
1468
1469 /** @todo collect partial login responses with Continue bit set. */
1470 Assert(aISCSIRes[0].pvSeg == aResBHS);
1471 Assert(aISCSIRes[0].cbSeg >= ISCSI_BHS_SIZE);
1472 Assert((RT_N2H_U32(aResBHS[0]) & ISCSI_CONTINUE_BIT) == 0);
1473
1474 cmd = (ISCSIOPCODE)(RT_N2H_U32(aResBHS[0]) & ISCSIOP_MASK);
1475 if (cmd == ISCSIOP_LOGIN_RES)
1476 {
1477 if ((RT_N2H_U32(aResBHS[0]) & 0xff) != ISCSI_MY_VERSION)
1478 {
1479 iscsiTransportClose(pImage);
1480 rc = VERR_PARSE_ERROR;
1481 break; /* Give up immediately, as a RFC violation in version fields is very serious. */
1482 }
1483
1484 loginStatusClass = (ISCSILOGINSTATUSCLASS)(RT_N2H_U32(aResBHS[9]) >> 24);
1485 switch (loginStatusClass)
1486 {
1487 case ISCSI_LOGIN_STATUS_CLASS_SUCCESS:
1488 uint32_t targetCSG;
1489 uint32_t targetNSG;
1490 bool targetTransit;
1491
1492 if (pImage->FirstRecvPDU)
1493 {
1494 pImage->FirstRecvPDU = false;
1495 pImage->ExpStatSN = RT_N2H_U32(aResBHS[6]) + 1;
1496 }
1497
1498 targetCSG = (RT_N2H_U32(aResBHS[0]) & ISCSI_CSG_MASK) >> ISCSI_CSG_SHIFT;
1499 targetNSG = (RT_N2H_U32(aResBHS[0]) & ISCSI_NSG_MASK) >> ISCSI_NSG_SHIFT;
1500 targetTransit = !!(RT_N2H_U32(aResBHS[0]) & ISCSI_TRANSIT_BIT);
1501
1502 /* Handle all cases with a single switch statement. */
1503 switch (csg << 8 | substate)
1504 {
1505 case 0x0000: /* security negotiation, step 0: receive final authentication. */
1506 rc = iscsiUpdateParameters(pImage, bBuf, aISCSIRes[1].cbSeg);
1507 if (RT_FAILURE(rc))
1508 break;
1509
1510 const char *pcszAuthMethod;
1511
1512 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "AuthMethod", &pcszAuthMethod);
1513 if (RT_FAILURE(rc))
1514 {
1515 rc = VERR_PARSE_ERROR;
1516 break;
1517 }
1518 if (strcmp(pcszAuthMethod, "None") == 0)
1519 {
1520 /* Authentication offered, but none required. Skip to operational parameters. */
1521 csg = 1;
1522 nsg = 1;
1523 transit = true;
1524 substate = 0;
1525 break;
1526 }
1527 else if (strcmp(pcszAuthMethod, "CHAP") == 0 && targetNSG == 0 && !targetTransit)
1528 {
1529 /* CHAP authentication required, continue with next substate. */
1530 substate++;
1531 break;
1532 }
1533
1534 /* Unknown auth method or login response PDU headers incorrect. */
1535 rc = VERR_PARSE_ERROR;
1536 break;
1537 case 0x0001: /* security negotiation, step 1: receive final CHAP variant and challenge. */
1538 {
1539 rc = iscsiUpdateParameters(pImage, bBuf, aISCSIRes[1].cbSeg);
1540 if (RT_FAILURE(rc))
1541 break;
1542
1543 const char *pcszChapAuthMethod;
1544 const char *pcszChapIdxTarget;
1545 const char *pcszChapChallengeStr;
1546
1547 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "CHAP_A", &pcszChapAuthMethod);
1548 if (RT_FAILURE(rc))
1549 {
1550 rc = VERR_PARSE_ERROR;
1551 break;
1552 }
1553 if (strcmp(pcszChapAuthMethod, "5") != 0)
1554 {
1555 rc = VERR_PARSE_ERROR;
1556 break;
1557 }
1558 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "CHAP_I", &pcszChapIdxTarget);
1559 if (RT_FAILURE(rc))
1560 {
1561 rc = VERR_PARSE_ERROR;
1562 break;
1563 }
1564 rc = RTStrToUInt8Ex(pcszChapIdxTarget, &pszNext, 0, &bChapIdx);
1565/** @todo r=bird: Unsafe use of pszNext on failure. The code should probably
1566 * use RTStrToUInt8Full and check for rc != VINF_SUCCESS. */
1567 if (rc > VINF_SUCCESS || *pszNext != '\0')
1568 {
1569 rc = VERR_PARSE_ERROR;
1570 break;
1571 }
1572 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "CHAP_C", &pcszChapChallengeStr);
1573 if (RT_FAILURE(rc))
1574 {
1575 rc = VERR_PARSE_ERROR;
1576 break;
1577 }
1578 cbChallenge = sizeof(pbChallenge);
1579 rc = iscsiStrToBinary(pcszChapChallengeStr, pbChallenge, &cbChallenge);
1580 if (RT_FAILURE(rc))
1581 break;
1582 substate++;
1583 transit = true;
1584 break;
1585 }
1586 case 0x0002: /* security negotiation, step 2: check authentication success. */
1587 rc = iscsiUpdateParameters(pImage, bBuf, aISCSIRes[1].cbSeg);
1588 if (RT_FAILURE(rc))
1589 break;
1590
1591 if (targetCSG == 0 && targetNSG == 1 && targetTransit)
1592 {
1593 /* Target wants to continue in login operational state, authentication success. */
1594 csg = 1;
1595 nsg = 3;
1596 substate = 0;
1597 break;
1598 }
1599 rc = VERR_PARSE_ERROR;
1600 break;
1601 case 0x0100: /* login operational negotiation, step 0: check results. */
1602 rc = iscsiUpdateParameters(pImage, bBuf, aISCSIRes[1].cbSeg);
1603 if (RT_FAILURE(rc))
1604 break;
1605
1606 if (targetCSG == 1 && targetNSG == 3 && targetTransit)
1607 {
1608 /* Target wants to continue in full feature phase, login finished. */
1609 csg = 3;
1610 nsg = 3;
1611 substate = 0;
1612 break;
1613 }
1614 else if (targetCSG == 1 && targetNSG == 1 && !targetTransit)
1615 {
1616 /* Target wants to negotiate certain parameters and
1617 * stay in login operational negotiation. */
1618 csg = 1;
1619 nsg = 3;
1620 substate = 0;
1621 }
1622 rc = VERR_PARSE_ERROR;
1623 break;
1624 case 0x0300: /* full feature phase. */
1625 default:
1626 AssertMsgFailed(("recv: Undefined login state %d substate %d\n", csg, substate));
1627 rc = VERR_PARSE_ERROR;
1628 break;
1629 }
1630 break;
1631 case ISCSI_LOGIN_STATUS_CLASS_REDIRECTION:
1632 const char *pcszTargetRedir;
1633
1634 /* Target has moved to some other location, as indicated in the TargetAddress key. */
1635 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "TargetAddress", &pcszTargetRedir);
1636 if (RT_FAILURE(rc))
1637 {
1638 rc = VERR_PARSE_ERROR;
1639 break;
1640 }
1641 if (pImage->pszTargetAddress)
1642 RTMemFree(pImage->pszTargetAddress);
1643 {
1644 size_t cb = strlen(pcszTargetRedir) + 1;
1645 pImage->pszTargetAddress = (char *)RTMemAlloc(cb);
1646 if (!pImage->pszTargetAddress)
1647 {
1648 rc = VERR_NO_MEMORY;
1649 break;
1650 }
1651 memcpy(pImage->pszTargetAddress, pcszTargetRedir, cb);
1652 }
1653 rc = VINF_TRY_AGAIN;
1654 break;
1655 case ISCSI_LOGIN_STATUS_CLASS_INITIATOR_ERROR:
1656 {
1657 LogRel(("iSCSI: login to target failed with: %s\n",
1658 iscsiGetLoginErrorDetail((RT_N2H_U32(aResBHS[9]) >> 16) & 0xff)));
1659 iscsiTransportClose(pImage);
1660 rc = VERR_IO_GEN_FAILURE;
1661 break;
1662 }
1663 case ISCSI_LOGIN_STATUS_CLASS_TARGET_ERROR:
1664 iscsiTransportClose(pImage);
1665 rc = VINF_EOF;
1666 break;
1667 default:
1668 rc = VERR_PARSE_ERROR;
1669 }
1670
1671 if (RT_FAILURE(rc) || rc == VINF_TRY_AGAIN)
1672 break;
1673
1674 if (csg == 3)
1675 {
1676 /*
1677 * Finished login, continuing with Full Feature Phase.
1678 */
1679 rc = VINF_SUCCESS;
1680 break;
1681 }
1682 }
1683 else
1684 AssertMsgFailed(("%s: ignoring unexpected PDU with first word = %#08x\n", __FUNCTION__, RT_N2H_U32(aResBHS[0])));
1685 }
1686 else
1687 break;
1688 } while (true);
1689
1690 if ( RT_FAILURE(rc)
1691 && rc != VERR_TRY_AGAIN)
1692 {
1693 /*
1694 * Dump the last request and response of we are supposed to do so and there is a request
1695 * or response.
1696 */
1697 if (cnISCSIReq)
1698 iscsiDumpPacket(pImage, aISCSIReq, cnISCSIReq, VINF_SUCCESS, true /* fRequest */);
1699
1700 if (cnISCSIRes)
1701 iscsiDumpPacket(pImage, (PISCSIREQ)aISCSIRes, cnISCSIRes, rc, false /* fRequest */);
1702
1703 /*
1704 * Close connection to target.
1705 */
1706 iscsiTransportClose(pImage);
1707 pImage->state = ISCSISTATE_FREE;
1708 }
1709 else if (RT_FAILURE(rc))
1710 pImage->state = ISCSISTATE_NORMAL;
1711
1712 return rc;
1713}
1714
1715/**
1716 * Attach to an iSCSI target. Performs all operations necessary to enter
1717 * Full Feature Phase.
1718 *
1719 * @returns VBox status code.
1720 * @param pvUser The iSCSI connection state to be used as opaque user data.
1721 */
1722static DECLCALLBACK(int) iscsiAttach(void *pvUser)
1723{
1724 int rc = VINF_SUCCESS;
1725 unsigned cRetries = 5;
1726 PISCSIIMAGE pImage = (PISCSIIMAGE)pvUser;
1727
1728 LogFlowFunc(("entering\n"));
1729
1730 Assert(pImage->state == ISCSISTATE_FREE);
1731
1732 /*
1733 * If there were too many logins without any successful I/O just fail
1734 * and assume the target is not working properly.
1735 */
1736 if (ASMAtomicReadU32(&pImage->cLoginsSinceIo) == 3)
1737 return VERR_BROKEN_PIPE;
1738
1739 RTSemMutexRequest(pImage->Mutex, RT_INDEFINITE_WAIT);
1740
1741 /* Make 100% sure the connection isn't reused for a new login. */
1742 iscsiTransportClose(pImage);
1743
1744 /* Try to log in a few number of times. */
1745 while (cRetries > 0)
1746 {
1747 rc = iscsiLogin(pImage);
1748 if (rc == VINF_SUCCESS) /* Login succeeded, continue with full feature phase. */
1749 break;
1750 else if (rc == VERR_TRY_AGAIN) /* Lost connection during receive. */
1751 cRetries--;
1752 else if (RT_FAILURE(rc))
1753 break;
1754 else /* For redirects try again. */
1755 AssertMsg(rc == VINF_TRY_AGAIN, ("Unexpected status code %Rrc\n", rc));
1756 }
1757
1758 if (RT_SUCCESS(rc))
1759 ASMAtomicIncU32(&pImage->cLoginsSinceIo);
1760
1761 RTSemMutexRelease(pImage->Mutex);
1762
1763 LogFlowFunc(("returning %Rrc\n", rc));
1764 LogRel(("iSCSI: login to target %s %s (%Rrc)\n", pImage->pszTargetName, RT_SUCCESS(rc) ? "successful" : "failed", rc));
1765 return rc;
1766}
1767
1768
1769/**
1770 * Detach from an iSCSI target.
1771 *
1772 * @returns VBox status code.
1773 * @param pvUser The iSCSI connection state to be used as opaque user data.
1774 */
1775static DECLCALLBACK(int) iscsiDetach(void *pvUser)
1776{
1777 int rc;
1778 uint32_t itt;
1779 uint32_t cnISCSIReq = 0;
1780 ISCSIREQ aISCSIReq[4];
1781 uint32_t aReqBHS[12];
1782 PISCSIIMAGE pImage = (PISCSIIMAGE)pvUser;
1783
1784 LogFlowFunc(("entering\n"));
1785
1786 RTSemMutexRequest(pImage->Mutex, RT_INDEFINITE_WAIT);
1787
1788 if (pImage->state != ISCSISTATE_FREE && pImage->state != ISCSISTATE_IN_LOGOUT)
1789 {
1790 pImage->state = ISCSISTATE_IN_LOGOUT;
1791
1792 /*
1793 * Send logout request to target.
1794 */
1795 itt = iscsiNewITT(pImage);
1796 aReqBHS[0] = RT_H2N_U32(ISCSI_FINAL_BIT | ISCSIOP_LOGOUT_REQ); /* I=0,F=1,Reason=close session */
1797 aReqBHS[1] = RT_H2N_U32(0); /* TotalAHSLength=0,DataSementLength=0 */
1798 aReqBHS[2] = 0; /* reserved */
1799 aReqBHS[3] = 0; /* reserved */
1800 aReqBHS[4] = itt;
1801 aReqBHS[5] = 0; /* reserved */
1802 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
1803 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
1804 aReqBHS[8] = 0; /* reserved */
1805 aReqBHS[9] = 0; /* reserved */
1806 aReqBHS[10] = 0; /* reserved */
1807 aReqBHS[11] = 0; /* reserved */
1808 pImage->CmdSN++;
1809
1810 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
1811 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
1812 cnISCSIReq++;
1813
1814 rc = iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq, ISCSIPDU_NO_REATTACH);
1815 if (RT_SUCCESS(rc))
1816 {
1817 /*
1818 * Read logout response from target.
1819 */
1820 ISCSIRES aISCSIRes;
1821 uint32_t aResBHS[12];
1822
1823 aISCSIRes.pvSeg = aResBHS;
1824 aISCSIRes.cbSeg = sizeof(aResBHS);
1825 rc = iscsiRecvPDU(pImage, itt, &aISCSIRes, 1, ISCSIPDU_NO_REATTACH);
1826 if (RT_SUCCESS(rc))
1827 {
1828 if (RT_N2H_U32(aResBHS[0]) != (ISCSI_FINAL_BIT | ISCSIOP_LOGOUT_RES))
1829 AssertMsgFailed(("iSCSI Logout response invalid\n"));
1830 }
1831 else
1832 AssertMsgFailed(("iSCSI Logout response error, rc=%Rrc\n", rc));
1833 }
1834 else
1835 AssertMsgFailed(("Could not send iSCSI Logout request, rc=%Rrc\n", rc));
1836 }
1837
1838 if (pImage->state != ISCSISTATE_FREE)
1839 {
1840 /*
1841 * Close connection to target.
1842 */
1843 rc = iscsiTransportClose(pImage);
1844 if (RT_FAILURE(rc))
1845 AssertMsgFailed(("Could not close connection to target, rc=%Rrc\n", rc));
1846 }
1847
1848 pImage->state = ISCSISTATE_FREE;
1849
1850 RTSemMutexRelease(pImage->Mutex);
1851
1852 LogFlowFunc(("leaving\n"));
1853 LogRel(("iSCSI: logout to target %s\n", pImage->pszTargetName));
1854 return VINF_SUCCESS;
1855}
1856
1857
1858/**
1859 * Perform a command on an iSCSI target. Target must be already in
1860 * Full Feature Phase.
1861 *
1862 * @returns VBox status code.
1863 * @param pImage The iSCSI connection state to be used.
1864 * @param pRequest Command descriptor. Contains all information about
1865 * the command, its transfer directions and pointers
1866 * to the buffer(s) used for transferring data and
1867 * status information.
1868 */
1869static int iscsiCommand(PISCSIIMAGE pImage, PSCSIREQ pRequest)
1870{
1871 int rc;
1872 uint32_t itt;
1873 uint32_t cbData;
1874 uint32_t cnISCSIReq = 0;
1875 ISCSIREQ aISCSIReq[4];
1876 uint32_t aReqBHS[12];
1877
1878 uint32_t *pDst = NULL;
1879 size_t cbBufLength;
1880 uint32_t aStatus[256]; /**< Plenty of buffer for status information. */
1881 uint32_t ExpDataSN = 0;
1882 bool final = false;
1883
1884
1885 LogFlowFunc(("entering, CmdSN=%d\n", pImage->CmdSN));
1886
1887 Assert(pRequest->enmXfer != SCSIXFER_TO_FROM_TARGET); /**< @todo not yet supported, would require AHS. */
1888 Assert(pRequest->cbI2TData <= 0xffffff); /* larger transfers would require R2T support. */
1889 Assert(pRequest->cbCDB <= 16); /* would cause buffer overrun below. */
1890
1891 /* If not in normal state, then the transport connection was dropped. Try
1892 * to reestablish by logging in, the target might be responsive again. */
1893 if (pImage->state == ISCSISTATE_FREE)
1894 rc = iscsiAttach(pImage);
1895
1896 /* If still not in normal state, then the underlying transport connection
1897 * cannot be established. Get out before bad things happen (and make
1898 * sure the caller suspends the VM again). */
1899 if (pImage->state == ISCSISTATE_NORMAL)
1900 {
1901 /*
1902 * Send SCSI command to target with all I2T data included.
1903 */
1904 cbData = 0;
1905 if (pRequest->enmXfer == SCSIXFER_FROM_TARGET)
1906 cbData = (uint32_t)pRequest->cbT2IData;
1907 else
1908 cbData = (uint32_t)pRequest->cbI2TData;
1909
1910 RTSemMutexRequest(pImage->Mutex, RT_INDEFINITE_WAIT);
1911
1912 itt = iscsiNewITT(pImage);
1913 memset(aReqBHS, 0, sizeof(aReqBHS));
1914 aReqBHS[0] = RT_H2N_U32( ISCSI_FINAL_BIT | ISCSI_TASK_ATTR_SIMPLE | ISCSIOP_SCSI_CMD
1915 | (pRequest->enmXfer << 21)); /* I=0,F=1,Attr=Simple */
1916 aReqBHS[1] = RT_H2N_U32(0x00000000 | ((uint32_t)pRequest->cbI2TData & 0xffffff)); /* TotalAHSLength=0 */
1917 aReqBHS[2] = RT_H2N_U32(pImage->LUN >> 32);
1918 aReqBHS[3] = RT_H2N_U32(pImage->LUN & 0xffffffff);
1919 aReqBHS[4] = itt;
1920 aReqBHS[5] = RT_H2N_U32(cbData);
1921 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
1922 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
1923 memcpy(aReqBHS + 8, pRequest->abCDB, pRequest->cbCDB);
1924 pImage->CmdSN++;
1925
1926 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
1927 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
1928 cnISCSIReq++;
1929
1930 if ( pRequest->enmXfer == SCSIXFER_TO_TARGET
1931 || pRequest->enmXfer == SCSIXFER_TO_FROM_TARGET)
1932 {
1933 Assert(pRequest->cI2TSegs == 1);
1934 aISCSIReq[cnISCSIReq].pcvSeg = pRequest->paI2TSegs[0].pvSeg;
1935 aISCSIReq[cnISCSIReq].cbSeg = pRequest->paI2TSegs[0].cbSeg; /* Padding done by transport. */
1936 cnISCSIReq++;
1937 }
1938
1939 rc = iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq, ISCSIPDU_DEFAULT);
1940 if (RT_SUCCESS(rc))
1941 {
1942 /* Place SCSI request in queue. */
1943 pImage->paCurrReq = aISCSIReq;
1944 pImage->cnCurrReq = cnISCSIReq;
1945
1946 /*
1947 * Read SCSI response/data in PDUs from target.
1948 */
1949 if ( pRequest->enmXfer == SCSIXFER_FROM_TARGET
1950 || pRequest->enmXfer == SCSIXFER_TO_FROM_TARGET)
1951 {
1952 Assert(pRequest->cT2ISegs == 1);
1953 pDst = (uint32_t *)pRequest->paT2ISegs[0].pvSeg;
1954 cbBufLength = pRequest->paT2ISegs[0].cbSeg;
1955 }
1956 else
1957 cbBufLength = 0;
1958
1959 do
1960 {
1961 uint32_t cnISCSIRes = 0;
1962 ISCSIRES aISCSIRes[4];
1963 uint32_t aResBHS[12];
1964
1965 aISCSIRes[cnISCSIRes].pvSeg = aResBHS;
1966 aISCSIRes[cnISCSIRes].cbSeg = sizeof(aResBHS);
1967 cnISCSIRes++;
1968 if (cbBufLength != 0 &&
1969 ( pRequest->enmXfer == SCSIXFER_FROM_TARGET
1970 || pRequest->enmXfer == SCSIXFER_TO_FROM_TARGET))
1971 {
1972 aISCSIRes[cnISCSIRes].pvSeg = pDst;
1973 aISCSIRes[cnISCSIRes].cbSeg = cbBufLength;
1974 cnISCSIRes++;
1975 }
1976 /* Always reserve space for the status - it's impossible to tell
1977 * beforehand whether this will be the final PDU or not. */
1978 aISCSIRes[cnISCSIRes].pvSeg = aStatus;
1979 aISCSIRes[cnISCSIRes].cbSeg = sizeof(aStatus);
1980 cnISCSIRes++;
1981
1982 rc = iscsiRecvPDU(pImage, itt, aISCSIRes, cnISCSIRes, ISCSIPDU_DEFAULT);
1983 if (RT_FAILURE(rc))
1984 break;
1985
1986 final = !!(RT_N2H_U32(aResBHS[0]) & ISCSI_FINAL_BIT);
1987 ISCSIOPCODE cmd = (ISCSIOPCODE)(RT_N2H_U32(aResBHS[0]) & ISCSIOP_MASK);
1988 if (cmd == ISCSIOP_SCSI_RES)
1989 {
1990 /* This is the final PDU which delivers the status (and may be omitted if
1991 * the last Data-In PDU included successful completion status). Note
1992 * that ExpStatSN has been bumped already in iscsiRecvPDU. */
1993 if (!final || ((RT_N2H_U32(aResBHS[0]) & 0x0000ff00) != 0) || (RT_N2H_U32(aResBHS[6]) != pImage->ExpStatSN - 1))
1994 {
1995 /* SCSI Response in the wrong place or with a (target) failure. */
1996 rc = VERR_PARSE_ERROR;
1997 break;
1998 }
1999 /* The following is a bit tricky, as in error situations we may
2000 * get the status only instead of the result data plus optional
2001 * status. Thus the status may have ended up partially in the
2002 * data area. */
2003 pRequest->status = RT_N2H_U32(aResBHS[0]) & 0x000000ff;
2004 cbData = RT_N2H_U32(aResBHS[1]) & 0x00ffffff;
2005 if (cbData >= 2)
2006 {
2007 uint32_t cbStat = RT_N2H_U32(((uint32_t *)aISCSIRes[1].pvSeg)[0]) >> 16;
2008 if (cbStat + 2 > cbData)
2009 {
2010 rc = VERR_BUFFER_OVERFLOW;
2011 break;
2012 }
2013 /* Truncate sense data if it doesn't fit into the buffer. */
2014 pRequest->cbSense = RT_MIN(cbStat, pRequest->cbSense);
2015 memcpy(pRequest->abSense,
2016 ((const char *)aISCSIRes[1].pvSeg) + 2,
2017 RT_MIN(aISCSIRes[1].cbSeg - 2, pRequest->cbSense));
2018 if ( cnISCSIRes > 2 && aISCSIRes[2].cbSeg
2019 && (ssize_t)pRequest->cbSense - aISCSIRes[1].cbSeg + 2 > 0)
2020 {
2021 memcpy((char *)pRequest->abSense + aISCSIRes[1].cbSeg - 2,
2022 aISCSIRes[2].pvSeg,
2023 pRequest->cbSense - aISCSIRes[1].cbSeg + 2);
2024 }
2025 }
2026 else if (cbData == 1)
2027 {
2028 rc = VERR_PARSE_ERROR;
2029 break;
2030 }
2031 else
2032 pRequest->cbSense = 0;
2033 break;
2034 }
2035 else if (cmd == ISCSIOP_SCSI_DATA_IN)
2036 {
2037 /* A Data-In PDU carries some data that needs to be added to the received
2038 * data in response to the command. There may be both partial and complete
2039 * Data-In PDUs, so collect data until the status is included or the status
2040 * is sent in a separate SCSI Result frame (see above). */
2041 if (final && aISCSIRes[2].cbSeg != 0)
2042 {
2043 /* The received PDU is partially stored in the buffer for status.
2044 * Must not happen under normal circumstances and is a target error. */
2045 rc = VERR_BUFFER_OVERFLOW;
2046 break;
2047 }
2048 uint32_t len = RT_N2H_U32(aResBHS[1]) & 0x00ffffff;
2049 pDst = (uint32_t *)((char *)pDst + len);
2050 cbBufLength -= len;
2051 ExpDataSN++;
2052 if (final && (RT_N2H_U32(aResBHS[0]) & ISCSI_STATUS_BIT) != 0)
2053 {
2054 pRequest->status = RT_N2H_U32(aResBHS[0]) & 0x000000ff;
2055 pRequest->cbSense = 0;
2056 break;
2057 }
2058 }
2059 else
2060 {
2061 rc = VERR_PARSE_ERROR;
2062 break;
2063 }
2064 } while (true);
2065
2066 /* Remove SCSI request from queue. */
2067 pImage->paCurrReq = NULL;
2068 pImage->cnCurrReq = 0;
2069 }
2070
2071 if (rc == VERR_TIMEOUT)
2072 {
2073 /* Drop connection in case the target plays dead. Much better than
2074 * delaying the next requests until the timed out command actually
2075 * finishes. Also keep in mind that command shouldn't take longer than
2076 * about 30-40 seconds, or the guest will lose its patience. */
2077 iscsiTransportClose(pImage);
2078 pImage->state = ISCSISTATE_FREE;
2079 rc = VERR_BROKEN_PIPE;
2080 }
2081 RTSemMutexRelease(pImage->Mutex);
2082 }
2083 else
2084 rc = VERR_NET_CONNECTION_REFUSED;
2085
2086 if (RT_SUCCESS(rc))
2087 ASMAtomicWriteU32(&pImage->cLoginsSinceIo, 0);
2088 LogFlowFunc(("returns %Rrc\n", rc));
2089 return rc;
2090}
2091
2092
2093/**
2094 * Generate a new Initiator Task Tag.
2095 *
2096 * @returns Initiator Task Tag.
2097 * @param pImage The iSCSI connection state to be used.
2098 */
2099static uint32_t iscsiNewITT(PISCSIIMAGE pImage)
2100{
2101 uint32_t next_itt;
2102
2103 next_itt = pImage->ITT++;
2104 if (pImage->ITT == ISCSI_TASK_TAG_RSVD)
2105 pImage->ITT = 0;
2106 return RT_H2N_U32(next_itt);
2107}
2108
2109
2110/**
2111 * Send an iSCSI request. The request can consist of several segments, which
2112 * are padded to 4 byte boundaries and concatenated.
2113 *
2114 * @returns VBOX status
2115 * @param pImage The iSCSI connection state to be used.
2116 * @param paReq Pointer to array of iSCSI request sections.
2117 * @param cnReq Number of valid iSCSI request sections in the array.
2118 * @param uFlags Flags controlling the exact send semantics.
2119 */
2120static int iscsiSendPDU(PISCSIIMAGE pImage, PISCSIREQ paReq, uint32_t cnReq,
2121 uint32_t uFlags)
2122{
2123 int rc = VINF_SUCCESS;
2124 /** @todo return VERR_VD_ISCSI_INVALID_STATE in the appropriate situations,
2125 * needs cleaning up of timeout/disconnect handling a bit, as otherwise
2126 * too many incorrect errors are signalled. */
2127 Assert(cnReq >= 1);
2128 Assert(paReq[0].cbSeg >= ISCSI_BHS_SIZE);
2129
2130 for (uint32_t i = 0; i < pImage->cISCSIRetries; i++)
2131 {
2132 rc = iscsiTransportWrite(pImage, paReq, cnReq);
2133 if (RT_SUCCESS(rc))
2134 break;
2135 if ( (uFlags & ISCSIPDU_NO_REATTACH)
2136 || (rc != VERR_BROKEN_PIPE && rc != VERR_NET_CONNECTION_REFUSED))
2137 break;
2138 /* No point in reestablishing the connection for a logout */
2139 if (pImage->state == ISCSISTATE_IN_LOGOUT)
2140 break;
2141 RTThreadSleep(500);
2142 if (pImage->state != ISCSISTATE_IN_LOGIN)
2143 {
2144 /* Attempt to re-login when a connection fails, but only when not
2145 * currently logging in. */
2146 rc = iscsiAttach(pImage);
2147 if (RT_FAILURE(rc))
2148 break;
2149 }
2150 }
2151 return rc;
2152}
2153
2154
2155/**
2156 * Wait for an iSCSI response with a matching Initiator Target Tag. The response is
2157 * split into several segments, as requested by the caller-provided buffer specification.
2158 * Remember that the response can be split into several PDUs by the sender, so make
2159 * sure that all parts are collected and processed appropriately by the caller.
2160 *
2161 * @returns VBOX status
2162 * @param pImage The iSCSI connection state to be used.
2163 * @param itt The initiator task tag.
2164 * @param paRes Pointer to array of iSCSI response sections.
2165 * @param cnRes Number of valid iSCSI response sections in the array.
2166 * @param fRecvFlags PDU receive flags.
2167 */
2168static int iscsiRecvPDU(PISCSIIMAGE pImage, uint32_t itt, PISCSIRES paRes, uint32_t cnRes,
2169 uint32_t fRecvFlags)
2170{
2171 int rc = VINF_SUCCESS;
2172 ISCSIRES aResBuf;
2173
2174 for (uint32_t i = 0; i < pImage->cISCSIRetries; i++)
2175 {
2176 aResBuf.pvSeg = pImage->pvRecvPDUBuf;
2177 aResBuf.cbSeg = pImage->cbRecvPDUBuf;
2178 rc = iscsiTransportRead(pImage, &aResBuf, 1);
2179 if (RT_FAILURE(rc))
2180 {
2181 if (rc == VERR_BROKEN_PIPE || rc == VERR_NET_CONNECTION_REFUSED)
2182 {
2183 /* No point in reestablishing the connection for a logout */
2184 if (pImage->state == ISCSISTATE_IN_LOGOUT)
2185 break;
2186 /* Connection broken while waiting for a response - wait a while and
2187 * try to restart by re-sending the original request (if any).
2188 * This also handles the connection reestablishment (login etc.). */
2189 RTThreadSleep(500);
2190 if ( pImage->state != ISCSISTATE_IN_LOGIN
2191 && !(fRecvFlags & ISCSIPDU_NO_REATTACH))
2192 {
2193 /* Attempt to re-login when a connection fails, but only when not
2194 * currently logging in. */
2195 rc = iscsiAttach(pImage);
2196 if (RT_FAILURE(rc))
2197 break;
2198
2199 if (pImage->paCurrReq != NULL)
2200 {
2201 rc = iscsiSendPDU(pImage, pImage->paCurrReq, pImage->cnCurrReq, ISCSIPDU_DEFAULT);
2202 if (RT_FAILURE(rc))
2203 break;
2204 }
2205 }
2206 }
2207 else
2208 {
2209 /* Signal other errors (VERR_BUFFER_OVERFLOW etc.) to the caller. */
2210 break;
2211 }
2212 }
2213 else
2214 {
2215 ISCSIOPCODE cmd;
2216 const uint32_t *pcvResSeg = (const uint32_t *)aResBuf.pvSeg;
2217
2218 /* Check whether the received PDU is valid, and update the internal state of
2219 * the iSCSI connection/session. */
2220 rc = iscsiValidatePDU(&aResBuf, 1);
2221 if (RT_FAILURE(rc))
2222 {
2223 iscsiDumpPacket(pImage, (PISCSIREQ)&aResBuf, 1, rc, false /* fRequest */);
2224 continue;
2225 }
2226 cmd = (ISCSIOPCODE)(RT_N2H_U32(pcvResSeg[0]) & ISCSIOP_MASK);
2227 switch (cmd)
2228 {
2229 case ISCSIOP_SCSI_RES:
2230 case ISCSIOP_SCSI_TASKMGMT_RES:
2231 case ISCSIOP_SCSI_DATA_IN:
2232 case ISCSIOP_R2T:
2233 case ISCSIOP_ASYN_MSG:
2234 case ISCSIOP_TEXT_RES:
2235 case ISCSIOP_LOGIN_RES:
2236 case ISCSIOP_LOGOUT_RES:
2237 case ISCSIOP_REJECT:
2238 case ISCSIOP_NOP_IN:
2239 if (serial_number_less(pImage->MaxCmdSN, RT_N2H_U32(pcvResSeg[8])))
2240 pImage->MaxCmdSN = RT_N2H_U32(pcvResSeg[8]);
2241 if (serial_number_less(pImage->ExpCmdSN, RT_N2H_U32(pcvResSeg[7])))
2242 pImage->ExpCmdSN = RT_N2H_U32(pcvResSeg[7]);
2243 break;
2244 default:
2245 rc = VERR_PARSE_ERROR;
2246 iscsiDumpPacket(pImage, (PISCSIREQ)&aResBuf, 1, rc, false /* fRequest */);
2247 }
2248 if (RT_FAILURE(rc))
2249 continue;
2250 if ( !pImage->FirstRecvPDU
2251 && (cmd != ISCSIOP_SCSI_DATA_IN || (RT_N2H_U32(pcvResSeg[0]) & ISCSI_STATUS_BIT))
2252 && ( cmd != ISCSIOP_LOGIN_RES
2253 || (ISCSILOGINSTATUSCLASS)((RT_N2H_U32(pcvResSeg[9]) >> 24) == ISCSI_LOGIN_STATUS_CLASS_SUCCESS)))
2254 {
2255 if (pImage->ExpStatSN == RT_N2H_U32(pcvResSeg[6]))
2256 {
2257 /* StatSN counter is not advanced on R2T and on a target SN update NOP-In. */
2258 if ( (cmd != ISCSIOP_R2T)
2259 && ((cmd != ISCSIOP_NOP_IN) || (RT_N2H_U32(pcvResSeg[4]) != ISCSI_TASK_TAG_RSVD)))
2260 pImage->ExpStatSN++;
2261 }
2262 else
2263 {
2264 rc = VERR_PARSE_ERROR;
2265 iscsiDumpPacket(pImage, (PISCSIREQ)&aResBuf, 1, rc, false /* fRequest */);
2266 continue;
2267 }
2268 }
2269 /* Finally check whether the received PDU matches what the caller wants. */
2270 if ( itt == pcvResSeg[4]
2271 && itt != ISCSI_TASK_TAG_RSVD)
2272 {
2273 /* Copy received PDU (one segment) to caller-provided buffers. */
2274 uint32_t j;
2275 size_t cbSeg;
2276 const uint8_t *pSrc;
2277
2278 pSrc = (const uint8_t *)aResBuf.pvSeg;
2279 cbSeg = aResBuf.cbSeg;
2280 for (j = 0; j < cnRes; j++)
2281 {
2282 if (cbSeg > paRes[j].cbSeg)
2283 {
2284 memcpy(paRes[j].pvSeg, pSrc, paRes[j].cbSeg);
2285 pSrc += paRes[j].cbSeg;
2286 cbSeg -= paRes[j].cbSeg;
2287 }
2288 else
2289 {
2290 memcpy(paRes[j].pvSeg, pSrc, cbSeg);
2291 paRes[j].cbSeg = cbSeg;
2292 cbSeg = 0;
2293 break;
2294 }
2295 }
2296 if (cbSeg != 0)
2297 {
2298 rc = VERR_BUFFER_OVERFLOW;
2299 break;
2300 }
2301 for (j++; j < cnRes; j++)
2302 paRes[j].cbSeg = 0;
2303 break;
2304 }
2305 else if ( cmd == ISCSIOP_NOP_IN
2306 && RT_N2H_U32(pcvResSeg[5]) != ISCSI_TASK_TAG_RSVD)
2307 {
2308 uint32_t cnISCSIReq;
2309 ISCSIREQ aISCSIReq[4];
2310 uint32_t aReqBHS[12];
2311
2312 aReqBHS[0] = RT_H2N_U32(ISCSI_IMMEDIATE_DELIVERY_BIT | ISCSI_FINAL_BIT | ISCSIOP_NOP_OUT);
2313 aReqBHS[1] = RT_H2N_U32(0); /* TotalAHSLength=0,DataSementLength=0 */
2314 aReqBHS[2] = pcvResSeg[2]; /* copy LUN from NOP-In */
2315 aReqBHS[3] = pcvResSeg[3]; /* copy LUN from NOP-In */
2316 aReqBHS[4] = RT_H2N_U32(ISCSI_TASK_TAG_RSVD); /* ITT, reply */
2317 aReqBHS[5] = pcvResSeg[5]; /* copy TTT from NOP-In */
2318 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
2319 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
2320 aReqBHS[8] = 0; /* reserved */
2321 aReqBHS[9] = 0; /* reserved */
2322 aReqBHS[10] = 0; /* reserved */
2323 aReqBHS[11] = 0; /* reserved */
2324
2325 cnISCSIReq = 0;
2326 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
2327 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
2328 cnISCSIReq++;
2329
2330 iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq, ISCSIPDU_NO_REATTACH);
2331 /* Break if the caller wanted to process the NOP-in only. */
2332 if (itt == ISCSI_TASK_TAG_RSVD)
2333 break;
2334 }
2335 }
2336 }
2337
2338 LogFlowFunc(("returns rc=%Rrc\n", rc));
2339 return rc;
2340}
2341
2342
2343/**
2344 * Reset the PDU buffer
2345 *
2346 * @param pImage The iSCSI connection state to be used.
2347 */
2348static void iscsiRecvPDUReset(PISCSIIMAGE pImage)
2349{
2350 pImage->cbRecvPDUResidual = ISCSI_BHS_SIZE;
2351 pImage->fRecvPDUBHS = true;
2352 pImage->pbRecvPDUBufCur = (uint8_t *)pImage->pvRecvPDUBuf;
2353}
2354
2355static void iscsiPDUTxAdd(PISCSIIMAGE pImage, PISCSIPDUTX pIScsiPDUTx, bool fFront)
2356{
2357 if (!fFront)
2358 {
2359 /* Insert PDU at the tail of the list. */
2360 if (!pImage->pIScsiPDUTxHead)
2361 pImage->pIScsiPDUTxHead = pIScsiPDUTx;
2362 else
2363 pImage->pIScsiPDUTxTail->pNext = pIScsiPDUTx;
2364 pImage->pIScsiPDUTxTail = pIScsiPDUTx;
2365 }
2366 else
2367 {
2368 /* Insert PDU at the beginning of the list. */
2369 pIScsiPDUTx->pNext = pImage->pIScsiPDUTxHead;
2370 pImage->pIScsiPDUTxHead = pIScsiPDUTx;
2371 if (!pImage->pIScsiPDUTxTail)
2372 pImage->pIScsiPDUTxTail = pIScsiPDUTx;
2373 }
2374}
2375
2376/**
2377 * Receives a PDU in a non blocking way.
2378 *
2379 * @returns VBOX status code.
2380 * @param pImage The iSCSI connection state to be used.
2381 */
2382static int iscsiRecvPDUAsync(PISCSIIMAGE pImage)
2383{
2384 size_t cbActuallyRead = 0;
2385 int rc = VINF_SUCCESS;
2386
2387 LogFlowFunc(("pImage=%#p\n", pImage));
2388
2389 /* Check if we are in the middle of a PDU receive. */
2390 if (pImage->cbRecvPDUResidual == 0)
2391 {
2392 /*
2393 * We are receiving a new PDU, don't read more than the BHS initially
2394 * until we know the real size of the PDU.
2395 */
2396 iscsiRecvPDUReset(pImage);
2397 LogFlow(("Receiving new PDU\n"));
2398 }
2399
2400 rc = pImage->pIfNet->pfnReadNB(pImage->Socket, pImage->pbRecvPDUBufCur,
2401 pImage->cbRecvPDUResidual, &cbActuallyRead);
2402 if (RT_SUCCESS(rc) && cbActuallyRead == 0)
2403 rc = VERR_BROKEN_PIPE;
2404
2405 if (RT_SUCCESS(rc))
2406 {
2407 LogFlow(("Received %zu bytes\n", cbActuallyRead));
2408 pImage->cbRecvPDUResidual -= cbActuallyRead;
2409 pImage->pbRecvPDUBufCur += cbActuallyRead;
2410
2411 /* Check if we received everything we wanted. */
2412 if ( !pImage->cbRecvPDUResidual
2413 && pImage->fRecvPDUBHS)
2414 {
2415 size_t cbAHSLength, cbDataLength;
2416
2417 /* If we were reading the BHS first get the actual PDU size now. */
2418 uint32_t word1 = RT_N2H_U32(((uint32_t *)(pImage->pvRecvPDUBuf))[1]);
2419 cbAHSLength = (word1 & 0xff000000) >> 24;
2420 cbAHSLength = ((cbAHSLength - 1) | 3) + 1; /* Add padding. */
2421 cbDataLength = word1 & 0x00ffffff;
2422 cbDataLength = ((cbDataLength - 1) | 3) + 1; /* Add padding. */
2423 pImage->cbRecvPDUResidual = cbAHSLength + cbDataLength;
2424 pImage->fRecvPDUBHS = false; /* Start receiving the rest of the PDU. */
2425 }
2426
2427 if (!pImage->cbRecvPDUResidual)
2428 {
2429 /* We received the complete PDU with or without any payload now. */
2430 LogFlow(("Received complete PDU\n"));
2431 ISCSIRES aResBuf;
2432 aResBuf.pvSeg = pImage->pvRecvPDUBuf;
2433 aResBuf.cbSeg = pImage->cbRecvPDUBuf;
2434 rc = iscsiRecvPDUProcess(pImage, &aResBuf, 1);
2435 }
2436 }
2437 else
2438 LogFlowFunc(("Reading from the socket returned with rc=%Rrc\n", rc));
2439
2440 return rc;
2441}
2442
2443static int iscsiSendPDUAsync(PISCSIIMAGE pImage)
2444{
2445 size_t cbSent = 0;
2446 int rc = VINF_SUCCESS;
2447
2448 LogFlowFunc(("pImage=%#p\n", pImage));
2449
2450 do
2451 {
2452 /*
2453 * If there is no PDU active, get the first one from the list.
2454 * Check that we are allowed to transfer the PDU by comparing the
2455 * command sequence number and the maximum sequence number allowed by the target.
2456 */
2457 if (!pImage->pIScsiPDUTxCur)
2458 {
2459 if ( !pImage->pIScsiPDUTxHead
2460 || serial_number_greater(pImage->pIScsiPDUTxHead->CmdSN, pImage->MaxCmdSN))
2461 break;
2462
2463 pImage->pIScsiPDUTxCur = pImage->pIScsiPDUTxHead;
2464 pImage->pIScsiPDUTxHead = pImage->pIScsiPDUTxCur->pNext;
2465 if (!pImage->pIScsiPDUTxHead)
2466 pImage->pIScsiPDUTxTail = NULL;
2467 }
2468
2469 /* Send as much as we can. */
2470 rc = pImage->pIfNet->pfnSgWriteNB(pImage->Socket, &pImage->pIScsiPDUTxCur->SgBuf, &cbSent);
2471 LogFlow(("SgWriteNB returned rc=%Rrc cbSent=%zu\n", rc, cbSent));
2472 if (RT_SUCCESS(rc))
2473 {
2474 LogFlow(("Sent %zu bytes for PDU %#p\n", cbSent, pImage->pIScsiPDUTxCur));
2475 pImage->pIScsiPDUTxCur->cbSgLeft -= cbSent;
2476 RTSgBufAdvance(&pImage->pIScsiPDUTxCur->SgBuf, cbSent);
2477 if (!pImage->pIScsiPDUTxCur->cbSgLeft)
2478 {
2479 /* PDU completed, free it and place the command on the waiting for response list. */
2480 if (pImage->pIScsiPDUTxCur->pIScsiCmd)
2481 {
2482 LogFlow(("Sent complete PDU, placing on waiting list\n"));
2483 iscsiCmdInsert(pImage, pImage->pIScsiPDUTxCur->pIScsiCmd);
2484 }
2485 RTMemFree(pImage->pIScsiPDUTxCur);
2486 pImage->pIScsiPDUTxCur = NULL;
2487 }
2488 }
2489 } while ( RT_SUCCESS(rc)
2490 && !pImage->pIScsiPDUTxCur);
2491
2492 if (rc == VERR_TRY_AGAIN)
2493 rc = VINF_SUCCESS;
2494
2495 /* Add the write poll flag if we still have something to send, clear it otherwise. */
2496 if (pImage->pIScsiPDUTxCur)
2497 pImage->fPollEvents |= VD_INTERFACETCPNET_EVT_WRITE;
2498 else
2499 pImage->fPollEvents &= ~VD_INTERFACETCPNET_EVT_WRITE;
2500
2501 LogFlowFunc(("rc=%Rrc pIScsiPDUTxCur=%#p\n", rc, pImage->pIScsiPDUTxCur));
2502 return rc;
2503}
2504
2505/**
2506 * Process a received PDU.
2507 *
2508 * @return VBOX status code.
2509 * @param pImage The iSCSI connection state to be used.
2510 * @param paRes Pointer to the array of iSCSI response sections.
2511 * @param cnRes Number of valid iSCSI response sections in the array.
2512 */
2513static int iscsiRecvPDUProcess(PISCSIIMAGE pImage, PISCSIRES paRes, uint32_t cnRes)
2514{
2515 int rc = VINF_SUCCESS;
2516
2517 LogFlowFunc(("pImage=%#p paRes=%#p cnRes=%u\n", pImage, paRes, cnRes));
2518
2519 /* Validate the PDU first. */
2520 rc = iscsiValidatePDU(paRes, cnRes);
2521 if (RT_SUCCESS(rc))
2522 {
2523 ISCSIOPCODE cmd;
2524 const uint32_t *pcvResSeg = (const uint32_t *)paRes[0].pvSeg;
2525
2526 Assert(paRes[0].cbSeg > 9 * sizeof(uint32_t));
2527
2528 do
2529 {
2530 cmd = (ISCSIOPCODE)(RT_N2H_U32(pcvResSeg[0]) & ISCSIOP_MASK);
2531 switch (cmd)
2532 {
2533 case ISCSIOP_SCSI_RES:
2534 case ISCSIOP_SCSI_TASKMGMT_RES:
2535 case ISCSIOP_SCSI_DATA_IN:
2536 case ISCSIOP_R2T:
2537 case ISCSIOP_ASYN_MSG:
2538 case ISCSIOP_TEXT_RES:
2539 case ISCSIOP_LOGIN_RES:
2540 case ISCSIOP_LOGOUT_RES:
2541 case ISCSIOP_REJECT:
2542 case ISCSIOP_NOP_IN:
2543 if (serial_number_less(pImage->MaxCmdSN, RT_N2H_U32(pcvResSeg[8])))
2544 pImage->MaxCmdSN = RT_N2H_U32(pcvResSeg[8]);
2545 if (serial_number_less(pImage->ExpCmdSN, RT_N2H_U32(pcvResSeg[7])))
2546 pImage->ExpCmdSN = RT_N2H_U32(pcvResSeg[7]);
2547 break;
2548 default:
2549 rc = VERR_PARSE_ERROR;
2550 iscsiDumpPacket(pImage, (PISCSIREQ)paRes, cnRes, rc, false /* fRequest */);
2551 }
2552
2553 if (RT_FAILURE(rc))
2554 break;
2555
2556 if ( !pImage->FirstRecvPDU
2557 && (cmd != ISCSIOP_SCSI_DATA_IN || (RT_N2H_U32(pcvResSeg[0]) & ISCSI_STATUS_BIT)))
2558 {
2559 if (pImage->ExpStatSN == RT_N2H_U32(pcvResSeg[6]))
2560 {
2561 /* StatSN counter is not advanced on R2T and on a target SN update NOP-In. */
2562 if ( (cmd != ISCSIOP_R2T)
2563 && ((cmd != ISCSIOP_NOP_IN) || (RT_N2H_U32(pcvResSeg[4]) != ISCSI_TASK_TAG_RSVD)))
2564 pImage->ExpStatSN++;
2565 }
2566 else
2567 {
2568 rc = VERR_PARSE_ERROR;
2569 iscsiDumpPacket(pImage, (PISCSIREQ)paRes, cnRes, rc, false /* fRequest */);
2570 break;
2571 }
2572 }
2573
2574 if (pcvResSeg[4] != ISCSI_TASK_TAG_RSVD)
2575 {
2576 /*
2577 * This is a response from the target for a request from the initiator.
2578 * Get the request and update its state.
2579 */
2580 rc = iscsiRecvPDUUpdateRequest(pImage, paRes, cnRes);
2581 /* Try to send more PDUs now that we updated the MaxCmdSN field */
2582 if ( RT_SUCCESS(rc)
2583 && !pImage->pIScsiPDUTxCur)
2584 rc = iscsiSendPDUAsync(pImage);
2585 }
2586 else
2587 {
2588 /* This is a target initiated request (we handle only NOP-In request at the moment). */
2589 if ( cmd == ISCSIOP_NOP_IN
2590 && RT_N2H_U32(pcvResSeg[5]) != ISCSI_TASK_TAG_RSVD)
2591 {
2592 PISCSIPDUTX pIScsiPDUTx;
2593 uint32_t cnISCSIReq;
2594 uint32_t *paReqBHS;
2595
2596 LogFlowFunc(("Sending NOP-Out\n"));
2597
2598 /* Allocate a new PDU initialize it and put onto the waiting list. */
2599 pIScsiPDUTx = (PISCSIPDUTX)RTMemAllocZ(sizeof(ISCSIPDUTX));
2600 if (!pIScsiPDUTx)
2601 {
2602 rc = VERR_NO_MEMORY;
2603 break;
2604 }
2605 paReqBHS = &pIScsiPDUTx->aBHS[0];
2606 paReqBHS[0] = RT_H2N_U32(ISCSI_IMMEDIATE_DELIVERY_BIT | ISCSI_FINAL_BIT | ISCSIOP_NOP_OUT);
2607 paReqBHS[1] = RT_H2N_U32(0); /* TotalAHSLength=0,DataSementLength=0 */
2608 paReqBHS[2] = pcvResSeg[2]; /* copy LUN from NOP-In */
2609 paReqBHS[3] = pcvResSeg[3]; /* copy LUN from NOP-In */
2610 paReqBHS[4] = RT_H2N_U32(ISCSI_TASK_TAG_RSVD); /* ITT, reply */
2611 paReqBHS[5] = pcvResSeg[5]; /* copy TTT from NOP-In */
2612 paReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
2613 paReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
2614 paReqBHS[8] = 0; /* reserved */
2615 paReqBHS[9] = 0; /* reserved */
2616 paReqBHS[10] = 0; /* reserved */
2617 paReqBHS[11] = 0; /* reserved */
2618
2619 cnISCSIReq = 0;
2620 pIScsiPDUTx->aISCSIReq[cnISCSIReq].pvSeg = paReqBHS;
2621 pIScsiPDUTx->aISCSIReq[cnISCSIReq].cbSeg = sizeof(pIScsiPDUTx->aBHS);
2622 cnISCSIReq++;
2623 pIScsiPDUTx->cbSgLeft = sizeof(pIScsiPDUTx->aBHS);
2624 RTSgBufInit(&pIScsiPDUTx->SgBuf, pIScsiPDUTx->aISCSIReq, cnISCSIReq);
2625
2626 /*
2627 * Link the PDU to the list.
2628 * Insert at the front of the list to send the response as soon as possible
2629 * to avoid frequent reconnects for a slow connection when there are many PDUs
2630 * waiting.
2631 */
2632 iscsiPDUTxAdd(pImage, pIScsiPDUTx, true /* fFront */);
2633
2634 /* Start transfer of a PDU if there is no one active at the moment. */
2635 if (!pImage->pIScsiPDUTxCur)
2636 rc = iscsiSendPDUAsync(pImage);
2637 }
2638 }
2639 } while (0);
2640 }
2641 else
2642 iscsiDumpPacket(pImage, (PISCSIREQ)paRes, cnRes, rc, false /* fRequest */);
2643
2644 return rc;
2645}
2646
2647/**
2648 * Check the static (not dependent on the connection/session state) validity of an iSCSI response PDU.
2649 *
2650 * @returns VBOX status
2651 * @param paRes Pointer to array of iSCSI response sections.
2652 * @param cnRes Number of valid iSCSI response sections in the array.
2653 */
2654static int iscsiValidatePDU(PISCSIRES paRes, uint32_t cnRes)
2655{
2656 RT_NOREF1(cnRes);
2657 const uint32_t *pcrgResBHS;
2658 uint32_t hw0;
2659 Assert(cnRes >= 1);
2660 Assert(paRes[0].cbSeg >= ISCSI_BHS_SIZE);
2661
2662 LogFlowFunc(("paRes=%#p cnRes=%u\n", paRes, cnRes));
2663
2664 pcrgResBHS = (const uint32_t *)(paRes[0].pvSeg);
2665 hw0 = RT_N2H_U32(pcrgResBHS[0]);
2666 switch (hw0 & ISCSIOP_MASK)
2667 {
2668 case ISCSIOP_NOP_IN:
2669 /* NOP-In responses must not be split into several PDUs nor it may contain
2670 * ping data for target-initiated pings nor may both task tags be valid task tags. */
2671 if ( (hw0 & ISCSI_FINAL_BIT) == 0
2672 || ( RT_N2H_U32(pcrgResBHS[4]) == ISCSI_TASK_TAG_RSVD
2673 && RT_N2H_U32(pcrgResBHS[1]) != 0)
2674 || ( RT_N2H_U32(pcrgResBHS[4]) != ISCSI_TASK_TAG_RSVD
2675 && RT_N2H_U32(pcrgResBHS[5]) != ISCSI_TASK_TAG_RSVD))
2676 return VERR_PARSE_ERROR;
2677 break;
2678 case ISCSIOP_SCSI_RES:
2679 /* SCSI responses must not be split into several PDUs nor must the residual
2680 * bits be contradicting each other nor may the residual bits be set for PDUs
2681 * containing anything else but a completed command response. Underflow
2682 * is no reason for declaring a PDU as invalid, as the target may choose
2683 * to return less data than we assume to get. */
2684 if ( (hw0 & ISCSI_FINAL_BIT) == 0
2685 || ((hw0 & ISCSI_BI_READ_RESIDUAL_OVFL_BIT) && (hw0 & ISCSI_BI_READ_RESIDUAL_UNFL_BIT))
2686 || ((hw0 & ISCSI_RESIDUAL_OVFL_BIT) && (hw0 & ISCSI_RESIDUAL_UNFL_BIT))
2687 || ( ((hw0 & ISCSI_SCSI_RESPONSE_MASK) == 0)
2688 && ((hw0 & ISCSI_SCSI_STATUS_MASK) == SCSI_STATUS_OK)
2689 && (hw0 & ( ISCSI_BI_READ_RESIDUAL_OVFL_BIT | ISCSI_BI_READ_RESIDUAL_UNFL_BIT
2690 | ISCSI_RESIDUAL_OVFL_BIT))))
2691 return VERR_PARSE_ERROR;
2692 else
2693 LogFlowFunc(("good SCSI response, first word %#08x\n", RT_N2H_U32(pcrgResBHS[0])));
2694 break;
2695 case ISCSIOP_LOGIN_RES:
2696 /* Login responses must not contain contradicting transit and continue bits. */
2697 if ((hw0 & ISCSI_CONTINUE_BIT) && ((hw0 & ISCSI_TRANSIT_BIT) != 0))
2698 return VERR_PARSE_ERROR;
2699 break;
2700 case ISCSIOP_TEXT_RES:
2701 /* Text responses must not contain contradicting final and continue bits nor
2702 * may the final bit be set for PDUs containing a target transfer tag other than
2703 * the reserved transfer tag (and vice versa). */
2704 if ( (((hw0 & ISCSI_CONTINUE_BIT) && (hw0 & ISCSI_FINAL_BIT) != 0))
2705 || (((hw0 & ISCSI_FINAL_BIT) && (RT_N2H_U32(pcrgResBHS[5]) != ISCSI_TASK_TAG_RSVD)))
2706 || (((hw0 & ISCSI_FINAL_BIT) == 0) && (RT_N2H_U32(pcrgResBHS[5]) == ISCSI_TASK_TAG_RSVD)))
2707 return VERR_PARSE_ERROR;
2708 break;
2709 case ISCSIOP_SCSI_DATA_IN:
2710 /* SCSI Data-in responses must not contain contradicting residual bits when
2711 * status bit is set. */
2712 if ((hw0 & ISCSI_STATUS_BIT) && (hw0 & ISCSI_RESIDUAL_OVFL_BIT) && (hw0 & ISCSI_RESIDUAL_UNFL_BIT))
2713 return VERR_PARSE_ERROR;
2714 break;
2715 case ISCSIOP_LOGOUT_RES:
2716 /* Logout responses must not have the final bit unset and may not contain any
2717 * data or additional header segments. */
2718 if ( ((hw0 & ISCSI_FINAL_BIT) == 0)
2719 || (RT_N2H_U32(pcrgResBHS[1]) != 0))
2720 return VERR_PARSE_ERROR;
2721 break;
2722 case ISCSIOP_ASYN_MSG:
2723 /* Asynchronous Messages must not have the final bit unset and may not contain
2724 * an initiator task tag. */
2725 if ( ((hw0 & ISCSI_FINAL_BIT) == 0)
2726 || (RT_N2H_U32(pcrgResBHS[4]) != ISCSI_TASK_TAG_RSVD))
2727 return VERR_PARSE_ERROR;
2728 break;
2729 case ISCSIOP_SCSI_TASKMGMT_RES:
2730 case ISCSIOP_R2T:
2731 case ISCSIOP_REJECT:
2732 default:
2733 /* Do some logging, ignore PDU. */
2734 LogFlowFunc(("ignore unhandled PDU, first word %#08x\n", RT_N2H_U32(pcrgResBHS[0])));
2735 return VERR_PARSE_ERROR;
2736 }
2737 /* A target must not send PDUs with MaxCmdSN less than ExpCmdSN-1. */
2738
2739 if (serial_number_less(RT_N2H_U32(pcrgResBHS[8]), RT_N2H_U32(pcrgResBHS[7])-1))
2740 return VERR_PARSE_ERROR;
2741
2742 return VINF_SUCCESS;
2743}
2744
2745
2746/**
2747 * Prepares a PDU to transfer for the given command and adds it to the list.
2748 */
2749static int iscsiPDUTxPrepare(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd)
2750{
2751 int rc = VINF_SUCCESS;
2752 uint32_t *paReqBHS;
2753 size_t cbData = 0;
2754 size_t cbSegs = 0;
2755 PSCSIREQ pScsiReq;
2756 PISCSIPDUTX pIScsiPDU = NULL;
2757
2758 LogFlowFunc(("pImage=%#p pIScsiCmd=%#p\n", pImage, pIScsiCmd));
2759
2760 Assert(pIScsiCmd->enmCmdType == ISCSICMDTYPE_REQ);
2761
2762 pIScsiCmd->Itt = iscsiNewITT(pImage);
2763 pScsiReq = pIScsiCmd->CmdType.ScsiReq.pScsiReq;
2764
2765 if (pScsiReq->cT2ISegs)
2766 RTSgBufInit(&pScsiReq->SgBufT2I, pScsiReq->paT2ISegs, pScsiReq->cT2ISegs);
2767
2768 /*
2769 * Allocate twice as much entries as required for padding (worst case).
2770 * The additional segment is for the BHS.
2771 */
2772 size_t cI2TSegs = 2*(pScsiReq->cI2TSegs + 1);
2773 pIScsiPDU = (PISCSIPDUTX)RTMemAllocZ(RT_OFFSETOF(ISCSIPDUTX, aISCSIReq[cI2TSegs]));
2774 if (!pIScsiPDU)
2775 return VERR_NO_MEMORY;
2776
2777 pIScsiPDU->pIScsiCmd = pIScsiCmd;
2778
2779 if (pScsiReq->enmXfer == SCSIXFER_FROM_TARGET)
2780 cbData = (uint32_t)pScsiReq->cbT2IData;
2781 else
2782 cbData = (uint32_t)pScsiReq->cbI2TData;
2783
2784 paReqBHS = pIScsiPDU->aBHS;
2785
2786 /* Setup the BHS. */
2787 paReqBHS[0] = RT_H2N_U32( ISCSI_FINAL_BIT | ISCSI_TASK_ATTR_SIMPLE | ISCSIOP_SCSI_CMD
2788 | (pScsiReq->enmXfer << 21)); /* I=0,F=1,Attr=Simple */
2789 paReqBHS[1] = RT_H2N_U32(0x00000000 | ((uint32_t)pScsiReq->cbI2TData & 0xffffff)); /* TotalAHSLength=0 */
2790 paReqBHS[2] = RT_H2N_U32(pImage->LUN >> 32);
2791 paReqBHS[3] = RT_H2N_U32(pImage->LUN & 0xffffffff);
2792 paReqBHS[4] = pIScsiCmd->Itt;
2793 paReqBHS[5] = RT_H2N_U32((uint32_t)cbData); Assert((uint32_t)cbData == cbData);
2794 paReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
2795 paReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
2796 memcpy(paReqBHS + 8, pScsiReq->abCDB, pScsiReq->cbCDB);
2797
2798 pIScsiPDU->CmdSN = pImage->CmdSN;
2799 pImage->CmdSN++;
2800
2801 /* Setup the S/G buffers. */
2802 uint32_t cnISCSIReq = 0;
2803 pIScsiPDU->aISCSIReq[cnISCSIReq].cbSeg = sizeof(pIScsiPDU->aBHS);
2804 pIScsiPDU->aISCSIReq[cnISCSIReq].pvSeg = pIScsiPDU->aBHS;
2805 cnISCSIReq++;
2806 cbSegs = sizeof(pIScsiPDU->aBHS);
2807 /* Padding is not necessary for the BHS. */
2808
2809 if (pScsiReq->cbI2TData)
2810 {
2811 for (unsigned cSeg = 0; cSeg < pScsiReq->cI2TSegs; cSeg++)
2812 {
2813 Assert(cnISCSIReq < cI2TSegs);
2814 pIScsiPDU->aISCSIReq[cnISCSIReq].cbSeg = pScsiReq->paI2TSegs[cSeg].cbSeg;
2815 pIScsiPDU->aISCSIReq[cnISCSIReq].pvSeg = pScsiReq->paI2TSegs[cSeg].pvSeg;
2816 cbSegs += pScsiReq->paI2TSegs[cSeg].cbSeg;
2817 cnISCSIReq++;
2818
2819 /* Add padding if necessary. */
2820 if (pScsiReq->paI2TSegs[cSeg].cbSeg & 3)
2821 {
2822 Assert(cnISCSIReq < cI2TSegs);
2823 pIScsiPDU->aISCSIReq[cnISCSIReq].pvSeg = &pImage->aPadding[0];
2824 pIScsiPDU->aISCSIReq[cnISCSIReq].cbSeg = 4 - (pScsiReq->paI2TSegs[cSeg].cbSeg & 3);
2825 cbSegs += pIScsiPDU->aISCSIReq[cnISCSIReq].cbSeg;
2826 cnISCSIReq++;
2827 }
2828 }
2829 }
2830
2831 pIScsiPDU->cISCSIReq = cnISCSIReq;
2832 pIScsiPDU->cbSgLeft = cbSegs;
2833 RTSgBufInit(&pIScsiPDU->SgBuf, pIScsiPDU->aISCSIReq, cnISCSIReq);
2834
2835 /* Link the PDU to the list. */
2836 iscsiPDUTxAdd(pImage, pIScsiPDU, false /* fFront */);
2837
2838 /* Start transfer of a PDU if there is no one active at the moment. */
2839 if (!pImage->pIScsiPDUTxCur)
2840 rc = iscsiSendPDUAsync(pImage);
2841
2842 return rc;
2843}
2844
2845
2846/**
2847 * Updates the state of a request from the PDU we received.
2848 *
2849 * @return VBox status code.
2850 * @param pImage iSCSI connection state to use.
2851 * @param paRes Pointer to array of iSCSI response sections.
2852 * @param cnRes Number of valid iSCSI response sections in the array.
2853 */
2854static int iscsiRecvPDUUpdateRequest(PISCSIIMAGE pImage, PISCSIRES paRes, uint32_t cnRes)
2855{
2856 int rc = VINF_SUCCESS;
2857 PISCSICMD pIScsiCmd;
2858 uint32_t *paResBHS;
2859
2860 LogFlowFunc(("pImage=%#p paRes=%#p cnRes=%u\n", pImage, paRes, cnRes));
2861
2862 Assert(cnRes == 1);
2863 Assert(paRes[0].cbSeg >= ISCSI_BHS_SIZE);
2864
2865 paResBHS = (uint32_t *)paRes[0].pvSeg;
2866
2867 pIScsiCmd = iscsiCmdGetFromItt(pImage, paResBHS[4]);
2868
2869 if (pIScsiCmd)
2870 {
2871 bool final = false;
2872 PSCSIREQ pScsiReq;
2873
2874 LogFlow(("Found SCSI command %#p for Itt=%#u\n", pIScsiCmd, paResBHS[4]));
2875
2876 Assert(pIScsiCmd->enmCmdType == ISCSICMDTYPE_REQ);
2877 pScsiReq = pIScsiCmd->CmdType.ScsiReq.pScsiReq;
2878
2879 final = !!(RT_N2H_U32(paResBHS[0]) & ISCSI_FINAL_BIT);
2880 ISCSIOPCODE cmd = (ISCSIOPCODE)(RT_N2H_U32(paResBHS[0]) & ISCSIOP_MASK);
2881 if (cmd == ISCSIOP_SCSI_RES)
2882 {
2883 /* This is the final PDU which delivers the status (and may be omitted if
2884 * the last Data-In PDU included successful completion status). Note
2885 * that ExpStatSN has been bumped already in iscsiRecvPDU. */
2886 if (!final || ((RT_N2H_U32(paResBHS[0]) & 0x0000ff00) != 0) || (RT_N2H_U32(paResBHS[6]) != pImage->ExpStatSN - 1))
2887 {
2888 /* SCSI Response in the wrong place or with a (target) failure. */
2889 LogFlow(("Wrong ExpStatSN value in PDU\n"));
2890 rc = VERR_PARSE_ERROR;
2891 }
2892 else
2893 {
2894 pScsiReq->status = RT_N2H_U32(paResBHS[0]) & 0x000000ff;
2895 size_t cbData = RT_N2H_U32(paResBHS[1]) & 0x00ffffff;
2896 void *pvSense = (uint8_t *)paRes[0].pvSeg + ISCSI_BHS_SIZE;
2897
2898 if (cbData >= 2)
2899 {
2900 uint32_t cbStat = RT_N2H_U32(((uint32_t *)pvSense)[0]) >> 16;
2901 if (cbStat + 2 > cbData)
2902 {
2903 rc = VERR_BUFFER_OVERFLOW;
2904 }
2905 else
2906 {
2907 /* Truncate sense data if it doesn't fit into the buffer. */
2908 pScsiReq->cbSense = RT_MIN(cbStat, pScsiReq->cbSense);
2909 memcpy(pScsiReq->abSense, (uint8_t *)pvSense + 2,
2910 RT_MIN(paRes[0].cbSeg - ISCSI_BHS_SIZE - 2, pScsiReq->cbSense));
2911 }
2912 }
2913 else if (cbData == 1)
2914 rc = VERR_PARSE_ERROR;
2915 else
2916 pScsiReq->cbSense = 0;
2917 }
2918 iscsiCmdComplete(pImage, pIScsiCmd, rc);
2919 }
2920 else if (cmd == ISCSIOP_SCSI_DATA_IN)
2921 {
2922 /* A Data-In PDU carries some data that needs to be added to the received
2923 * data in response to the command. There may be both partial and complete
2924 * Data-In PDUs, so collect data until the status is included or the status
2925 * is sent in a separate SCSI Result frame (see above). */
2926 size_t cbData = RT_N2H_U32(paResBHS[1]) & 0x00ffffff;
2927 void *pvData = (uint8_t *)paRes[0].pvSeg + ISCSI_BHS_SIZE;
2928
2929 if (final && cbData > pScsiReq->cbT2IData)
2930 {
2931 /* The received PDU is bigger than what we requested.
2932 * Must not happen under normal circumstances and is a target error. */
2933 rc = VERR_BUFFER_OVERFLOW;
2934 }
2935 else
2936 {
2937 /* Copy data from the received PDU into the T2I segments. */
2938 size_t cbCopied = RTSgBufCopyFromBuf(&pScsiReq->SgBufT2I, pvData, cbData);
2939 Assert(cbCopied == cbData); NOREF(cbCopied);
2940
2941 if (final && (RT_N2H_U32(paResBHS[0]) & ISCSI_STATUS_BIT) != 0)
2942 {
2943 pScsiReq->status = RT_N2H_U32(paResBHS[0]) & 0x000000ff;
2944 pScsiReq->cbSense = 0;
2945 iscsiCmdComplete(pImage, pIScsiCmd, VINF_SUCCESS);
2946 }
2947 }
2948 }
2949 else
2950 rc = VERR_PARSE_ERROR;
2951 }
2952
2953 /* Log any errors here but ignore the PDU. */
2954 if (RT_FAILURE(rc))
2955 {
2956 LogRel(("iSCSI: Received malformed PDU from target %s (rc=%Rrc), ignoring\n", pImage->pszTargetName, rc));
2957 iscsiDumpPacket(pImage, (PISCSIREQ)paRes, cnRes, rc, false /* fRequest */);
2958 rc = VINF_SUCCESS;
2959 }
2960
2961 return rc;
2962}
2963
2964/**
2965 * Appends a key-value pair to the buffer. Normal ASCII strings (cbValue == 0) and large binary values
2966 * of a given length (cbValue > 0) are directly supported. Other value types must be converted to ASCII
2967 * by the caller. Strings must be in UTF-8 encoding.
2968 *
2969 * @returns VBOX status
2970 * @param pbBuf Pointer to the key-value buffer.
2971 * @param cbBuf Length of the key-value buffer.
2972 * @param pcbBufCurr Currently used portion of the key-value buffer.
2973 * @param pcszKey Pointer to a string containing the key.
2974 * @param pcszValue Pointer to either a string containing the value or to a large binary value.
2975 * @param cbValue Length of the binary value if applicable.
2976 */
2977static int iscsiTextAddKeyValue(uint8_t *pbBuf, size_t cbBuf, size_t *pcbBufCurr, const char *pcszKey,
2978 const char *pcszValue, size_t cbValue)
2979{
2980 size_t cbBufTmp = *pcbBufCurr;
2981 size_t cbKey = strlen(pcszKey);
2982 size_t cbValueEnc;
2983 uint8_t *pbCurr;
2984
2985 if (cbValue == 0)
2986 cbValueEnc = strlen(pcszValue);
2987 else
2988 cbValueEnc = cbValue * 2 + 2; /* 2 hex bytes per byte, 2 bytes prefix */
2989
2990 if (cbBuf < cbBufTmp + cbKey + 1 + cbValueEnc + 1)
2991 {
2992 /* Buffer would overflow, signal error. */
2993 return VERR_BUFFER_OVERFLOW;
2994 }
2995
2996 /*
2997 * Append a key=value pair (zero terminated string) to the end of the buffer.
2998 */
2999 pbCurr = pbBuf + cbBufTmp;
3000 memcpy(pbCurr, pcszKey, cbKey);
3001 pbCurr += cbKey;
3002 *pbCurr++ = '=';
3003 if (cbValue == 0)
3004 {
3005 memcpy(pbCurr, pcszValue, cbValueEnc);
3006 pbCurr += cbValueEnc;
3007 }
3008 else
3009 {
3010 *pbCurr++ = '0';
3011 *pbCurr++ = 'x';
3012 for (uint32_t i = 0; i < cbValue; i++)
3013 {
3014 uint8_t b;
3015 b = pcszValue[i];
3016 *pbCurr++ = NUM_2_HEX(b >> 4);
3017 *pbCurr++ = NUM_2_HEX(b & 0xf);
3018 }
3019 }
3020 *pbCurr = '\0';
3021 *pcbBufCurr = cbBufTmp + cbKey + 1 + cbValueEnc + 1;
3022
3023 return VINF_SUCCESS;
3024}
3025
3026
3027/**
3028 * Retrieve the value for a given key from the key=value buffer.
3029 *
3030 * @returns VBox status code.
3031 * @param pbBuf Buffer containing key=value pairs.
3032 * @param cbBuf Length of buffer with key=value pairs.
3033 * @param pcszKey Pointer to key for which to retrieve the value.
3034 * @param ppcszValue Pointer to value string pointer.
3035 */
3036static int iscsiTextGetKeyValue(const uint8_t *pbBuf, size_t cbBuf, const char *pcszKey, const char **ppcszValue)
3037{
3038 size_t cbKey = strlen(pcszKey);
3039
3040 while (cbBuf != 0)
3041 {
3042 size_t cbKeyValNull = strlen((const char *)pbBuf) + 1;
3043
3044 if (strncmp(pcszKey, (const char *)pbBuf, cbKey) == 0 && pbBuf[cbKey] == '=')
3045 {
3046 *ppcszValue = (const char *)(pbBuf + cbKey + 1);
3047 return VINF_SUCCESS;
3048 }
3049 pbBuf += cbKeyValNull;
3050 cbBuf -= cbKeyValNull;
3051 }
3052 return VERR_INVALID_NAME;
3053}
3054
3055
3056/**
3057 * Convert a long-binary value from a value string to the binary representation.
3058 *
3059 * @returns VBOX status
3060 * @param pcszValue Pointer to a string containing the textual value representation.
3061 * @param pbValue Pointer to the value buffer for the binary value.
3062 * @param pcbValue In: length of value buffer, out: actual length of binary value.
3063 */
3064static int iscsiStrToBinary(const char *pcszValue, uint8_t *pbValue, size_t *pcbValue)
3065{
3066 size_t cbValue = *pcbValue;
3067 char c1, c2, c3, c4;
3068 Assert(cbValue >= 1);
3069
3070 if (strlen(pcszValue) < 3)
3071 return VERR_PARSE_ERROR;
3072 if (*pcszValue++ != '0')
3073 return VERR_PARSE_ERROR;
3074 switch (*pcszValue++)
3075 {
3076 case 'x':
3077 case 'X':
3078 if (strlen(pcszValue) & 1)
3079 {
3080 c1 = *pcszValue++;
3081 *pbValue++ = HEX_2_NUM(c1);
3082 cbValue--;
3083 }
3084 while (*pcszValue != '\0')
3085 {
3086 if (cbValue == 0)
3087 return VERR_BUFFER_OVERFLOW;
3088 c1 = *pcszValue++;
3089 if ((c1 < '0' || c1 > '9') && (c1 < 'a' || c1 > 'f') && (c1 < 'A' || c1 > 'F'))
3090 return VERR_PARSE_ERROR;
3091 c2 = *pcszValue++;
3092 if ((c2 < '0' || c2 > '9') && (c2 < 'a' || c2 > 'f') && (c2 < 'A' || c2 > 'F'))
3093 return VERR_PARSE_ERROR;
3094 *pbValue++ = (HEX_2_NUM(c1) << 4) | HEX_2_NUM(c2);
3095 cbValue--;
3096 }
3097 *pcbValue -= cbValue;
3098 break;
3099 case 'b':
3100 case 'B':
3101 if ((strlen(pcszValue) & 3) != 0)
3102 return VERR_PARSE_ERROR;
3103 while (*pcszValue != '\0')
3104 {
3105 uint32_t temp;
3106 if (cbValue == 0)
3107 return VERR_BUFFER_OVERFLOW;
3108 c1 = *pcszValue++;
3109 if ((c1 < 'A' || c1 > 'Z') && (c1 < 'a' || c1 >'z') && (c1 < '0' || c1 > '9') && (c1 != '+') && (c1 != '/'))
3110 return VERR_PARSE_ERROR;
3111 c2 = *pcszValue++;
3112 if ((c2 < 'A' || c2 > 'Z') && (c2 < 'a' || c2 >'z') && (c2 < '0' || c2 > '9') && (c2 != '+') && (c2 != '/'))
3113 return VERR_PARSE_ERROR;
3114 c3 = *pcszValue++;
3115 if ((c3 < 'A' || c3 > 'Z') && (c3 < 'a' || c3 >'z') && (c3 < '0' || c3 > '9') && (c3 != '+') && (c3 != '/') && (c3 != '='))
3116 return VERR_PARSE_ERROR;
3117 c4 = *pcszValue++;
3118 if ( (c3 == '=' && c4 != '=')
3119 || ((c4 < 'A' || c4 > 'Z') && (c4 < 'a' || c4 >'z') && (c4 < '0' || c4 > '9') && (c4 != '+') && (c4 != '/') && (c4 != '=')))
3120 return VERR_PARSE_ERROR;
3121 temp = (B64_2_NUM(c1) << 18) | (B64_2_NUM(c2) << 12);
3122 if (c3 == '=') {
3123 if (*pcszValue != '\0')
3124 return VERR_PARSE_ERROR;
3125 *pbValue++ = temp >> 16;
3126 cbValue--;
3127 } else {
3128 temp |= B64_2_NUM(c3) << 6;
3129 if (c4 == '=') {
3130 if (*pcszValue != '\0')
3131 return VERR_PARSE_ERROR;
3132 if (cbValue < 2)
3133 return VERR_BUFFER_OVERFLOW;
3134 *pbValue++ = temp >> 16;
3135 *pbValue++ = (temp >> 8) & 0xff;
3136 cbValue -= 2;
3137 }
3138 else
3139 {
3140 temp |= B64_2_NUM(c4);
3141 if (cbValue < 3)
3142 return VERR_BUFFER_OVERFLOW;
3143 *pbValue++ = temp >> 16;
3144 *pbValue++ = (temp >> 8) & 0xff;
3145 *pbValue++ = temp & 0xff;
3146 cbValue -= 3;
3147 }
3148 }
3149 }
3150 *pcbValue -= cbValue;
3151 break;
3152 default:
3153 return VERR_PARSE_ERROR;
3154 }
3155 return VINF_SUCCESS;
3156}
3157
3158
3159/**
3160 * Retrieve the relevant parameter values and update the initiator state.
3161 *
3162 * @returns VBox status code.
3163 * @param pImage Current iSCSI initiator state.
3164 * @param pbBuf Buffer containing key=value pairs.
3165 * @param cbBuf Length of buffer with key=value pairs.
3166 */
3167static int iscsiUpdateParameters(PISCSIIMAGE pImage, const uint8_t *pbBuf, size_t cbBuf)
3168{
3169 int rc;
3170 const char *pcszMaxRecvDataSegmentLength = NULL;
3171 const char *pcszMaxBurstLength = NULL;
3172 const char *pcszFirstBurstLength = NULL;
3173 rc = iscsiTextGetKeyValue(pbBuf, cbBuf, "MaxRecvDataSegmentLength", &pcszMaxRecvDataSegmentLength);
3174 if (rc == VERR_INVALID_NAME)
3175 rc = VINF_SUCCESS;
3176 if (RT_FAILURE(rc))
3177 return VERR_PARSE_ERROR;
3178 rc = iscsiTextGetKeyValue(pbBuf, cbBuf, "MaxBurstLength", &pcszMaxBurstLength);
3179 if (rc == VERR_INVALID_NAME)
3180 rc = VINF_SUCCESS;
3181 if (RT_FAILURE(rc))
3182 return VERR_PARSE_ERROR;
3183 rc = iscsiTextGetKeyValue(pbBuf, cbBuf, "FirstBurstLength", &pcszFirstBurstLength);
3184 if (rc == VERR_INVALID_NAME)
3185 rc = VINF_SUCCESS;
3186 if (RT_FAILURE(rc))
3187 return VERR_PARSE_ERROR;
3188 if (pcszMaxRecvDataSegmentLength)
3189 {
3190 uint32_t cb = pImage->cbSendDataLength;
3191 rc = RTStrToUInt32Full(pcszMaxRecvDataSegmentLength, 0, &cb);
3192 AssertRC(rc);
3193 pImage->cbSendDataLength = RT_MIN(pImage->cbSendDataLength, cb);
3194 }
3195 if (pcszMaxBurstLength)
3196 {
3197 uint32_t cb = pImage->cbSendDataLength;
3198 rc = RTStrToUInt32Full(pcszMaxBurstLength, 0, &cb);
3199 AssertRC(rc);
3200 pImage->cbSendDataLength = RT_MIN(pImage->cbSendDataLength, cb);
3201 }
3202 if (pcszFirstBurstLength)
3203 {
3204 uint32_t cb = pImage->cbSendDataLength;
3205 rc = RTStrToUInt32Full(pcszFirstBurstLength, 0, &cb);
3206 AssertRC(rc);
3207 pImage->cbSendDataLength = RT_MIN(pImage->cbSendDataLength, cb);
3208 }
3209 return VINF_SUCCESS;
3210}
3211
3212
3213static bool serial_number_less(uint32_t s1, uint32_t s2)
3214{
3215 return (s1 < s2 && s2 - s1 < 0x80000000) || (s1 > s2 && s1 - s2 > 0x80000000);
3216}
3217
3218static bool serial_number_greater(uint32_t s1, uint32_t s2)
3219{
3220 return (s1 < s2 && s2 - s1 > 0x80000000) || (s1 > s2 && s1 - s2 < 0x80000000);
3221}
3222
3223
3224#ifdef IMPLEMENT_TARGET_AUTH
3225static void chap_md5_generate_challenge(uint8_t *pbChallenge, size_t *pcbChallenge)
3226{
3227 uint8_t cbChallenge;
3228
3229 cbChallenge = RTrand_U8(CHAP_MD5_CHALLENGE_MIN, CHAP_MD5_CHALLENGE_MAX);
3230 RTrand_bytes(pbChallenge, cbChallenge);
3231 *pcbChallenge = cbChallenge;
3232}
3233#endif
3234
3235
3236static void chap_md5_compute_response(uint8_t *pbResponse, uint8_t id, const uint8_t *pbChallenge, size_t cbChallenge,
3237 const uint8_t *pbSecret, size_t cbSecret)
3238{
3239 RTMD5CONTEXT ctx;
3240 uint8_t bId;
3241
3242 bId = id;
3243 RTMd5Init(&ctx);
3244 RTMd5Update(&ctx, &bId, 1);
3245 RTMd5Update(&ctx, pbSecret, cbSecret);
3246 RTMd5Update(&ctx, pbChallenge, cbChallenge);
3247 RTMd5Final(pbResponse, &ctx);
3248}
3249
3250/**
3251 * Internal. - Wrapper around the extended select callback of the net interface.
3252 */
3253DECLINLINE(int) iscsiIoThreadWait(PISCSIIMAGE pImage, RTMSINTERVAL cMillies, uint32_t fEvents, uint32_t *pfEvents)
3254{
3255 return pImage->pIfNet->pfnSelectOneEx(pImage->Socket, fEvents, pfEvents, cMillies);
3256}
3257
3258/**
3259 * Internal. - Pokes a thread waiting for I/O.
3260 */
3261DECLINLINE(int) iscsiIoThreadPoke(PISCSIIMAGE pImage)
3262{
3263 return pImage->pIfNet->pfnPoke(pImage->Socket);
3264}
3265
3266/**
3267 * Internal. - Get the next request from the queue.
3268 */
3269DECLINLINE(PISCSICMD) iscsiCmdGet(PISCSIIMAGE pImage)
3270{
3271 int rc;
3272 PISCSICMD pIScsiCmd = NULL;
3273
3274 rc = RTSemMutexRequest(pImage->MutexReqQueue, RT_INDEFINITE_WAIT);
3275 AssertRC(rc);
3276
3277 pIScsiCmd = pImage->pScsiReqQueue;
3278 if (pIScsiCmd)
3279 {
3280 pImage->pScsiReqQueue = pIScsiCmd->pNext;
3281 pIScsiCmd->pNext = NULL;
3282 }
3283
3284 rc = RTSemMutexRelease(pImage->MutexReqQueue);
3285 AssertRC(rc);
3286
3287 return pIScsiCmd;
3288}
3289
3290
3291/**
3292 * Internal. - Adds the given command to the queue.
3293 */
3294DECLINLINE(int) iscsiCmdPut(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd)
3295{
3296 int rc = RTSemMutexRequest(pImage->MutexReqQueue, RT_INDEFINITE_WAIT);
3297 AssertRC(rc);
3298
3299 pIScsiCmd->pNext = pImage->pScsiReqQueue;
3300 pImage->pScsiReqQueue = pIScsiCmd;
3301
3302 rc = RTSemMutexRelease(pImage->MutexReqQueue);
3303 AssertRC(rc);
3304
3305 iscsiIoThreadPoke(pImage);
3306
3307 return rc;
3308}
3309
3310/**
3311 * Internal. - Completes the request with the appropriate action.
3312 * Synchronous requests are completed with waking up the thread
3313 * and asynchronous ones by continuing the associated I/O context.
3314 */
3315static void iscsiCmdComplete(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd, int rcCmd)
3316{
3317 LogFlowFunc(("pImage=%#p pIScsiCmd=%#p rcCmd=%Rrc\n", pImage, pIScsiCmd, rcCmd));
3318
3319 /* Remove from the table first. */
3320 iscsiCmdRemove(pImage, pIScsiCmd->Itt);
3321
3322 /* Call completion callback. */
3323 pIScsiCmd->pfnComplete(pImage, rcCmd, pIScsiCmd->pvUser);
3324
3325 /* Free command structure. */
3326#ifdef DEBUG
3327 memset(pIScsiCmd, 0xff, sizeof(ISCSICMD));
3328#endif
3329 RTMemFree(pIScsiCmd);
3330}
3331
3332/**
3333 * Clears all RX/TX PDU states and returns the command for the current
3334 * pending TX PDU if existing.
3335 *
3336 * @returns Pointer to the iSCSI command for the current PDU transmitted or NULL
3337 * if none is waiting.
3338 * @param pImage iSCSI connection state.
3339 */
3340static PISCSICMD iscsiPDURxTxClear(PISCSIIMAGE pImage)
3341{
3342 PISCSICMD pIScsiCmdHead = NULL;
3343 PISCSIPDUTX pIScsiPDUTx = NULL;
3344
3345 /* Reset PDU we are receiving. */
3346 iscsiRecvPDUReset(pImage);
3347
3348 /*
3349 * Abort all PDUs we are about to transmit,
3350 * the command need a new Itt if the relogin is successful.
3351 */
3352 while (pImage->pIScsiPDUTxHead)
3353 {
3354 pIScsiPDUTx = pImage->pIScsiPDUTxHead;
3355 pImage->pIScsiPDUTxHead = pIScsiPDUTx->pNext;
3356
3357 PISCSICMD pIScsiCmd = pIScsiPDUTx->pIScsiCmd;
3358 if (pIScsiCmd)
3359 {
3360 /* Place on command list. */
3361 pIScsiCmd->pNext = pIScsiCmdHead;
3362 pIScsiCmdHead = pIScsiCmd;
3363 }
3364 RTMemFree(pIScsiPDUTx);
3365 }
3366
3367 /* Clear the tail pointer (safety precaution). */
3368 pImage->pIScsiPDUTxTail = NULL;
3369
3370 /* Clear the current PDU too. */
3371 if (pImage->pIScsiPDUTxCur)
3372 {
3373 pIScsiPDUTx = pImage->pIScsiPDUTxCur;
3374
3375 pImage->pIScsiPDUTxCur = NULL;
3376 PISCSICMD pIScsiCmd = pIScsiPDUTx->pIScsiCmd;
3377 if (pIScsiCmd)
3378 {
3379 pIScsiCmd->pNext = pIScsiCmdHead;
3380 pIScsiCmdHead = pIScsiCmd;
3381 }
3382 RTMemFree(pIScsiPDUTx);
3383 }
3384
3385 return pIScsiCmdHead;
3386}
3387
3388/**
3389 * Rests the iSCSI connection state and returns a list of iSCSI commands pending
3390 * when this was called.
3391 *
3392 * @returns Pointer to the head of the pending iSCSI command list.
3393 * @param pImage iSCSI connection state.
3394 */
3395static PISCSICMD iscsiReset(PISCSIIMAGE pImage)
3396{
3397 PISCSICMD pIScsiCmdHead = NULL;
3398 PISCSICMD pIScsiCmdCur = NULL;
3399
3400 /* Clear all in flight PDUs. */
3401 pIScsiCmdHead = iscsiPDURxTxClear(pImage);
3402
3403 /*
3404 * Get all commands which are waiting for a response
3405 * They need to be resend too after a successful reconnect.
3406 */
3407 PISCSICMD pIScsiCmd = iscsiCmdRemoveAll(pImage);
3408 if (pIScsiCmd)
3409 {
3410 pIScsiCmdCur = pIScsiCmd;
3411 while (pIScsiCmdCur->pNext)
3412 pIScsiCmdCur = pIScsiCmdCur->pNext;
3413
3414 /*
3415 * Place them in front of the list because they are the oldest requests
3416 * and need to be processed first to minimize the risk to time out.
3417 */
3418 pIScsiCmdCur->pNext = pIScsiCmdHead;
3419 pIScsiCmdHead = pIScsiCmd;
3420 }
3421
3422 return pIScsiCmdHead;
3423}
3424
3425/**
3426 * Reattaches the to the target after an error aborting
3427 * pending commands and resending them.
3428 *
3429 * @param pImage iSCSI connection state.
3430 */
3431static void iscsiReattach(PISCSIIMAGE pImage)
3432{
3433 /* Close connection. */
3434 iscsiTransportClose(pImage);
3435 pImage->state = ISCSISTATE_FREE;
3436
3437 /* Reset the state and get the currently pending commands. */
3438 PISCSICMD pIScsiCmdHead = iscsiReset(pImage);
3439
3440 /* Try to attach. */
3441 int rc = iscsiAttach(pImage);
3442 if (RT_SUCCESS(rc))
3443 {
3444 /* Phew, we have a connection again.
3445 * Prepare new PDUs for the aborted commands.
3446 */
3447 while (pIScsiCmdHead)
3448 {
3449 PISCSICMD pIScsiCmd = pIScsiCmdHead;
3450 pIScsiCmdHead = pIScsiCmdHead->pNext;
3451
3452 pIScsiCmd->pNext = NULL;
3453
3454 rc = iscsiPDUTxPrepare(pImage, pIScsiCmd);
3455 if (RT_FAILURE(rc))
3456 break;
3457 }
3458
3459 if (RT_FAILURE(rc))
3460 {
3461 /* Another error, just give up and report an error. */
3462 PISCSICMD pIScsiCmd = iscsiReset(pImage);
3463
3464 /* Concatenate both lists together so we can abort all requests below. */
3465 if (pIScsiCmd)
3466 {
3467 PISCSICMD pIScsiCmdCur = pIScsiCmd;
3468 while (pIScsiCmdCur->pNext)
3469 pIScsiCmdCur = pIScsiCmdCur->pNext;
3470
3471 pIScsiCmdCur->pNext = pIScsiCmdHead;
3472 pIScsiCmdHead = pIScsiCmd;
3473 }
3474 }
3475 }
3476
3477 if (RT_FAILURE(rc))
3478 {
3479 /*
3480 * Still no luck, complete commands with error so the caller
3481 * has a chance to inform the user and maybe resend the command.
3482 */
3483 while (pIScsiCmdHead)
3484 {
3485 PISCSICMD pIScsiCmd = pIScsiCmdHead;
3486 pIScsiCmdHead = pIScsiCmdHead->pNext;
3487
3488 iscsiCmdComplete(pImage, pIScsiCmd, VERR_BROKEN_PIPE);
3489 }
3490 }
3491}
3492
3493/**
3494 * Internal. Main iSCSI I/O worker.
3495 */
3496static DECLCALLBACK(int) iscsiIoThreadWorker(RTTHREAD hThreadSelf, void *pvUser)
3497{
3498 RT_NOREF1(hThreadSelf);
3499 PISCSIIMAGE pImage = (PISCSIIMAGE)pvUser;
3500
3501 /* Initialize the initial event mask. */
3502 pImage->fPollEvents = VD_INTERFACETCPNET_EVT_READ | VD_INTERFACETCPNET_EVT_ERROR;
3503
3504 while (pImage->fRunning)
3505 {
3506 uint32_t fEvents;
3507 int rc;
3508
3509 fEvents = 0;
3510
3511 /* Wait for work or for data from the target. */
3512 RTMSINTERVAL msWait;
3513
3514 if (pImage->cCmdsWaiting)
3515 {
3516 pImage->fPollEvents &= ~VD_INTERFACETCPNET_HINT_INTERRUPT;
3517 msWait = pImage->uReadTimeout;
3518 }
3519 else
3520 {
3521 pImage->fPollEvents |= VD_INTERFACETCPNET_HINT_INTERRUPT;
3522 msWait = RT_INDEFINITE_WAIT;
3523 }
3524
3525 LogFlow(("Waiting for events fPollEvents=%#x\n", pImage->fPollEvents));
3526 rc = iscsiIoThreadWait(pImage, msWait, pImage->fPollEvents, &fEvents);
3527 if (rc == VERR_INTERRUPTED)
3528 {
3529 /* Check the queue. */
3530 PISCSICMD pIScsiCmd = iscsiCmdGet(pImage);
3531
3532 while (pIScsiCmd)
3533 {
3534 switch (pIScsiCmd->enmCmdType)
3535 {
3536 case ISCSICMDTYPE_REQ:
3537 {
3538 if ( !iscsiIsClientConnected(pImage)
3539 && pImage->fTryReconnect)
3540 {
3541 pImage->fTryReconnect = false;
3542 iscsiReattach(pImage);
3543 }
3544
3545 /* If there is no connection complete the command with an error. */
3546 if (RT_LIKELY(iscsiIsClientConnected(pImage)))
3547 {
3548 rc = iscsiPDUTxPrepare(pImage, pIScsiCmd);
3549 if (RT_FAILURE(rc))
3550 iscsiReattach(pImage);
3551 }
3552 else
3553 iscsiCmdComplete(pImage, pIScsiCmd, VERR_NET_CONNECTION_REFUSED);
3554 break;
3555 }
3556 case ISCSICMDTYPE_EXEC:
3557 {
3558 rc = pIScsiCmd->CmdType.Exec.pfnExec(pIScsiCmd->CmdType.Exec.pvUser);
3559 iscsiCmdComplete(pImage, pIScsiCmd, rc);
3560 break;
3561 }
3562 default:
3563 AssertMsgFailed(("Invalid command type %d\n", pIScsiCmd->enmCmdType));
3564 }
3565
3566 pIScsiCmd = iscsiCmdGet(pImage);
3567 }
3568 }
3569 else if (rc == VERR_TIMEOUT && pImage->cCmdsWaiting)
3570 {
3571 /*
3572 * We are waiting for a response from the target but
3573 * it didn't answered yet.
3574 * We assume the connection is broken and try to reconnect.
3575 */
3576 LogFlow(("Timed out while waiting for an answer from the target, reconnecting\n"));
3577 iscsiReattach(pImage);
3578 }
3579 else if (RT_SUCCESS(rc) || rc == VERR_TIMEOUT)
3580 {
3581 Assert(pImage->state == ISCSISTATE_NORMAL);
3582 LogFlow(("Got socket events %#x\n", fEvents));
3583
3584 if (fEvents & VD_INTERFACETCPNET_EVT_READ)
3585 {
3586 /* Continue or start a new PDU receive task */
3587 LogFlow(("There is data on the socket\n"));
3588 rc = iscsiRecvPDUAsync(pImage);
3589 if (rc == VERR_BROKEN_PIPE)
3590 iscsiReattach(pImage);
3591 else if (RT_FAILURE(rc))
3592 iscsiLogRel(pImage, "iSCSI: Handling incoming request failed %Rrc\n", rc);
3593 }
3594
3595 if (fEvents & VD_INTERFACETCPNET_EVT_WRITE)
3596 {
3597 LogFlow(("The socket is writable\n"));
3598 rc = iscsiSendPDUAsync(pImage);
3599 if (RT_FAILURE(rc))
3600 {
3601 /*
3602 * Something unexpected happened, log the error and try to reset everything
3603 * by reattaching to the target.
3604 */
3605 iscsiLogRel(pImage, "iSCSI: Sending PDU failed %Rrc\n", rc);
3606 iscsiReattach(pImage);
3607 }
3608 }
3609
3610 if (fEvents & VD_INTERFACETCPNET_EVT_ERROR)
3611 {
3612 LogFlow(("An error ocurred\n"));
3613 iscsiReattach(pImage);
3614 }
3615 }
3616 else
3617 iscsiLogRel(pImage, "iSCSI: Waiting for I/O failed rc=%Rrc\n", rc);
3618 }
3619
3620 return VINF_SUCCESS;
3621}
3622
3623/**
3624 * Internal. - Enqueues a request asynchronously.
3625 */
3626static int iscsiCommandAsync(PISCSIIMAGE pImage, PSCSIREQ pScsiReq,
3627 PFNISCSICMDCOMPLETED pfnComplete, void *pvUser)
3628{
3629 int rc;
3630
3631 if (pImage->fExtendedSelectSupported)
3632 {
3633 PISCSICMD pIScsiCmd = (PISCSICMD)RTMemAllocZ(sizeof(ISCSICMD));
3634 if (!pIScsiCmd)
3635 return VERR_NO_MEMORY;
3636
3637 /* Init the command structure. */
3638 pIScsiCmd->pNext = NULL;
3639 pIScsiCmd->enmCmdType = ISCSICMDTYPE_REQ;
3640 pIScsiCmd->pfnComplete = pfnComplete;
3641 pIScsiCmd->pvUser = pvUser;
3642 pIScsiCmd->CmdType.ScsiReq.pScsiReq = pScsiReq;
3643
3644 rc = iscsiCmdPut(pImage, pIScsiCmd);
3645 if (RT_FAILURE(rc))
3646 RTMemFree(pIScsiCmd);
3647 }
3648 else
3649 rc = VERR_NOT_SUPPORTED;
3650
3651 return rc;
3652}
3653
3654static DECLCALLBACK(void) iscsiCommandCompleteSync(PISCSIIMAGE pImage, int rcReq, void *pvUser)
3655{
3656 RT_NOREF1(pImage);
3657 PISCSICMDSYNC pIScsiCmdSync = (PISCSICMDSYNC)pvUser;
3658
3659 pIScsiCmdSync->rcCmd = rcReq;
3660 int rc = RTSemEventSignal(pIScsiCmdSync->EventSem);
3661 AssertRC(rc);
3662}
3663
3664/**
3665 * Internal. - Enqueues a request in a synchronous fashion
3666 * i.e. returns when the request completed.
3667 */
3668static int iscsiCommandSync(PISCSIIMAGE pImage, PSCSIREQ pScsiReq, bool fRetry, int rcSense)
3669{
3670 int rc;
3671
3672 if (pImage->fExtendedSelectSupported)
3673 {
3674 ISCSICMDSYNC IScsiCmdSync;
3675
3676 /* Create event semaphore. */
3677 rc = RTSemEventCreate(&IScsiCmdSync.EventSem);
3678 if (RT_FAILURE(rc))
3679 return rc;
3680
3681 if (fRetry)
3682 {
3683 for (unsigned i = 0; i < 10; i++)
3684 {
3685 rc = iscsiCommandAsync(pImage, pScsiReq, iscsiCommandCompleteSync, &IScsiCmdSync);
3686 if (RT_FAILURE(rc))
3687 break;
3688
3689 rc = RTSemEventWait(IScsiCmdSync.EventSem, RT_INDEFINITE_WAIT);
3690 AssertRC(rc);
3691 rc = IScsiCmdSync.rcCmd;
3692
3693 if ( (RT_SUCCESS(rc) && !pScsiReq->cbSense)
3694 || RT_FAILURE(rc))
3695 break;
3696 rc = rcSense;
3697 }
3698 }
3699 else
3700 {
3701 rc = iscsiCommandAsync(pImage, pScsiReq, iscsiCommandCompleteSync, &IScsiCmdSync);
3702 if (RT_SUCCESS(rc))
3703 {
3704 rc = RTSemEventWait(IScsiCmdSync.EventSem, RT_INDEFINITE_WAIT);
3705 AssertRC(rc);
3706 rc = IScsiCmdSync.rcCmd;
3707
3708 if (RT_FAILURE(rc) || pScsiReq->cbSense > 0)
3709 rc = rcSense;
3710 }
3711 }
3712
3713 RTSemEventDestroy(IScsiCmdSync.EventSem);
3714 }
3715 else
3716 {
3717 if (fRetry)
3718 {
3719 rc = VINF_SUCCESS; /* (MSC incorrectly thinks it can be uninitialized) */
3720 for (unsigned i = 0; i < 10; i++)
3721 {
3722 rc = iscsiCommand(pImage, pScsiReq);
3723 if ( (RT_SUCCESS(rc) && !pScsiReq->cbSense)
3724 || RT_FAILURE(rc))
3725 break;
3726 rc = rcSense;
3727 }
3728 }
3729 else
3730 {
3731 rc = iscsiCommand(pImage, pScsiReq);
3732 if (RT_FAILURE(rc) || pScsiReq->cbSense > 0)
3733 rc = rcSense;
3734 }
3735 }
3736
3737 return rc;
3738}
3739
3740
3741/**
3742 * Internal. - Executes a given function in a synchronous fashion
3743 * on the I/O thread if available.
3744 */
3745static int iscsiExecSync(PISCSIIMAGE pImage, PFNISCSIEXEC pfnExec, void *pvUser)
3746{
3747 int rc;
3748
3749 if (pImage->fExtendedSelectSupported)
3750 {
3751 ISCSICMDSYNC IScsiCmdSync;
3752 PISCSICMD pIScsiCmd = (PISCSICMD)RTMemAllocZ(sizeof(ISCSICMD));
3753 if (!pIScsiCmd)
3754 return VERR_NO_MEMORY;
3755
3756 /* Create event semaphore. */
3757 rc = RTSemEventCreate(&IScsiCmdSync.EventSem);
3758 if (RT_FAILURE(rc))
3759 {
3760 RTMemFree(pIScsiCmd);
3761 return rc;
3762 }
3763
3764 /* Init the command structure. */
3765 pIScsiCmd->pNext = NULL;
3766 pIScsiCmd->enmCmdType = ISCSICMDTYPE_EXEC;
3767 pIScsiCmd->pfnComplete = iscsiCommandCompleteSync;
3768 pIScsiCmd->pvUser = &IScsiCmdSync;
3769 pIScsiCmd->CmdType.Exec.pfnExec = pfnExec;
3770 pIScsiCmd->CmdType.Exec.pvUser = pvUser;
3771
3772 rc = iscsiCmdPut(pImage, pIScsiCmd);
3773 if (RT_FAILURE(rc))
3774 RTMemFree(pIScsiCmd);
3775 else
3776 {
3777 rc = RTSemEventWait(IScsiCmdSync.EventSem, RT_INDEFINITE_WAIT);
3778 AssertRC(rc);
3779 rc = IScsiCmdSync.rcCmd;
3780 }
3781
3782 RTSemEventDestroy(IScsiCmdSync.EventSem);
3783 }
3784 else
3785 {
3786 /* No I/O thread, execute in the current thread. */
3787 rc = pfnExec(pvUser);
3788 }
3789
3790 return rc;
3791}
3792
3793
3794static DECLCALLBACK(void) iscsiCommandAsyncComplete(PISCSIIMAGE pImage, int rcReq, void *pvUser)
3795{
3796 bool fComplete = true;
3797 size_t cbTransfered = 0;
3798 PSCSIREQ pScsiReq = (PSCSIREQ)pvUser;
3799
3800 if (RT_SUCCESS(rcReq))
3801 ASMAtomicWriteU32(&pImage->cLoginsSinceIo, 0);
3802
3803 if ( RT_SUCCESS(rcReq)
3804 && pScsiReq->cbSense > 0)
3805 {
3806 /* Try again if possible. */
3807 if (pScsiReq->cSenseRetries > 0)
3808 {
3809 pScsiReq->cSenseRetries--;
3810 pScsiReq->cbSense = sizeof(pScsiReq->abSense);
3811 int rc = iscsiCommandAsync(pImage, pScsiReq, iscsiCommandAsyncComplete, pScsiReq);
3812 if (RT_SUCCESS(rc))
3813 fComplete = false;
3814 else
3815 rcReq = pScsiReq->rcSense;
3816 }
3817 else
3818 rcReq = pScsiReq->rcSense;
3819 }
3820
3821 if (fComplete)
3822 {
3823 if (pScsiReq->enmXfer == SCSIXFER_FROM_TARGET)
3824 cbTransfered = pScsiReq->cbT2IData;
3825 else if (pScsiReq->enmXfer == SCSIXFER_TO_TARGET)
3826 cbTransfered = pScsiReq->cbI2TData;
3827 else
3828 AssertMsg(pScsiReq->enmXfer == SCSIXFER_NONE, ("To/From transfers are not supported yet\n"));
3829
3830 /* Continue I/O context. */
3831 pImage->pIfIo->pfnIoCtxCompleted(pImage->pIfIo->Core.pvUser,
3832 pScsiReq->pIoCtx, rcReq,
3833 cbTransfered);
3834
3835 RTMemFree(pScsiReq);
3836 }
3837}
3838
3839
3840/**
3841 * Internal. Free all allocated space for representing an image, and optionally
3842 * delete the image from disk.
3843 */
3844static int iscsiFreeImage(PISCSIIMAGE pImage, bool fDelete)
3845{
3846 int rc = VINF_SUCCESS;
3847 Assert(!fDelete); NOREF(fDelete); /* This MUST be false, the flag isn't supported. */
3848
3849 /* Freeing a never allocated image (e.g. because the open failed) is
3850 * not signalled as an error. After all nothing bad happens. */
3851 if (pImage)
3852 {
3853 if (pImage->Mutex != NIL_RTSEMMUTEX)
3854 {
3855 /* Detaching only makes sense when the mutex is there. Otherwise the
3856 * failure happened long before we could attach to the target. */
3857 iscsiExecSync(pImage, iscsiDetach, pImage);
3858 RTSemMutexDestroy(pImage->Mutex);
3859 pImage->Mutex = NIL_RTSEMMUTEX;
3860 }
3861 if (pImage->hThreadIo != NIL_RTTHREAD)
3862 {
3863 ASMAtomicXchgBool(&pImage->fRunning, false);
3864 rc = iscsiIoThreadPoke(pImage);
3865 AssertRC(rc);
3866
3867 /* Wait for the thread to terminate. */
3868 rc = RTThreadWait(pImage->hThreadIo, RT_INDEFINITE_WAIT, NULL);
3869 AssertRC(rc);
3870 }
3871 /* Destroy the socket. */
3872 if (pImage->Socket != NIL_VDSOCKET)
3873 {
3874 pImage->pIfNet->pfnSocketDestroy(pImage->Socket);
3875 }
3876 if (pImage->MutexReqQueue != NIL_RTSEMMUTEX)
3877 {
3878 RTSemMutexDestroy(pImage->MutexReqQueue);
3879 pImage->MutexReqQueue = NIL_RTSEMMUTEX;
3880 }
3881 if (pImage->pszTargetName)
3882 {
3883 RTMemFree(pImage->pszTargetName);
3884 pImage->pszTargetName = NULL;
3885 }
3886 if (pImage->pszTargetAddress)
3887 {
3888 RTMemFree(pImage->pszTargetAddress);
3889 pImage->pszTargetAddress = NULL;
3890 }
3891 if (pImage->pszInitiatorName)
3892 {
3893 if (pImage->fAutomaticInitiatorName)
3894 RTStrFree(pImage->pszInitiatorName);
3895 else
3896 RTMemFree(pImage->pszInitiatorName);
3897 pImage->pszInitiatorName = NULL;
3898 }
3899 if (pImage->pszInitiatorUsername)
3900 {
3901 RTMemFree(pImage->pszInitiatorUsername);
3902 pImage->pszInitiatorUsername = NULL;
3903 }
3904 if (pImage->pbInitiatorSecret)
3905 {
3906 RTMemFree(pImage->pbInitiatorSecret);
3907 pImage->pbInitiatorSecret = NULL;
3908 }
3909 if (pImage->pszTargetUsername)
3910 {
3911 RTMemFree(pImage->pszTargetUsername);
3912 pImage->pszTargetUsername = NULL;
3913 }
3914 if (pImage->pbTargetSecret)
3915 {
3916 RTMemFree(pImage->pbTargetSecret);
3917 pImage->pbTargetSecret = NULL;
3918 }
3919 if (pImage->pvRecvPDUBuf)
3920 {
3921 RTMemFree(pImage->pvRecvPDUBuf);
3922 pImage->pvRecvPDUBuf = NULL;
3923 }
3924 if (pImage->pszHostname)
3925 {
3926 RTMemFree(pImage->pszHostname);
3927 pImage->pszHostname = NULL;
3928 }
3929
3930 pImage->cbRecvPDUResidual = 0;
3931 }
3932
3933 LogFlowFunc(("returns %Rrc\n", rc));
3934 return rc;
3935}
3936
3937/**
3938 * Inits the basic iSCSI image state, allocating vital resources.
3939 *
3940 * @returns VBox status code.
3941 * @param pImage The iSCSI image instance.
3942 */
3943static int iscsiOpenImageInit(PISCSIIMAGE pImage)
3944{
3945 int rc = VINF_SUCCESS;
3946
3947 /* Get error signalling interface. */
3948 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
3949
3950 /* Get TCP network stack interface. */
3951 pImage->pIfNet = VDIfTcpNetGet(pImage->pVDIfsImage);
3952 if (pImage->pIfNet)
3953 {
3954 /* Get configuration interface. */
3955 pImage->pIfConfig = VDIfConfigGet(pImage->pVDIfsImage);
3956 if (pImage->pIfConfig)
3957 {
3958 /* Get I/O interface. */
3959 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
3960 if (pImage->pIfIo)
3961 {
3962 /* This ISID will be adjusted later to make it unique on this host. */
3963 pImage->pszHostname = NULL;
3964 pImage->uPort = 0;
3965 pImage->Socket = NIL_VDSOCKET;
3966 pImage->ISID = 0x800000000000ULL | 0x001234560000ULL;
3967 pImage->cISCSIRetries = 10;
3968 pImage->state = ISCSISTATE_FREE;
3969 pImage->cLoginsSinceIo = 0;
3970 pImage->Mutex = NIL_RTSEMMUTEX;
3971 pImage->MutexReqQueue = NIL_RTSEMMUTEX;
3972 pImage->pszInitiatorUsername = NULL;
3973 pImage->pbInitiatorSecret = NULL;
3974 pImage->cbInitiatorSecret = 0;
3975 pImage->pszTargetUsername = NULL;
3976 pImage->pbTargetSecret = NULL;
3977 pImage->cbTargetSecret = 0;
3978
3979 memset(pImage->aCmdsWaiting, 0, sizeof(pImage->aCmdsWaiting));
3980 pImage->cbRecvPDUResidual = 0;
3981
3982 pImage->pvRecvPDUBuf = RTMemAlloc(ISCSI_RECV_PDU_BUFFER_SIZE);
3983 pImage->cbRecvPDUBuf = ISCSI_RECV_PDU_BUFFER_SIZE;
3984 if (!pImage->pvRecvPDUBuf)
3985 rc = VERR_NO_MEMORY;
3986
3987 if (RT_SUCCESS(rc))
3988 rc = RTSemMutexCreate(&pImage->Mutex);
3989 if (RT_SUCCESS(rc))
3990 rc = RTSemMutexCreate(&pImage->MutexReqQueue);
3991 }
3992 else
3993 rc = vdIfError(pImage->pIfError, VERR_VD_UNKNOWN_INTERFACE,
3994 RT_SRC_POS, N_("iSCSI: I/O interface missing"));
3995 }
3996 else
3997 rc = vdIfError(pImage->pIfError, VERR_VD_UNKNOWN_INTERFACE,
3998 RT_SRC_POS, N_("iSCSI: configuration interface missing"));
3999 }
4000 else
4001 rc = vdIfError(pImage->pIfError, VERR_VD_UNKNOWN_INTERFACE,
4002 RT_SRC_POS, N_("iSCSI: TCP network stack interface missing"));
4003
4004 return rc;
4005}
4006
4007/**
4008 * Parses the user supplied config before opening the connection to the target.
4009 *
4010 * @returns VBox status code.
4011 * @param pImage The iSCSI image instance.
4012 */
4013static int iscsiOpenImageParseCfg(PISCSIIMAGE pImage)
4014{
4015 char *pszLUN = NULL, *pszLUNInitial = NULL;
4016 bool fLunEncoded = false;
4017 uint32_t uWriteSplitDef = 0;
4018 uint32_t uTimeoutDef = 0;
4019 uint64_t uCfgTmp = 0;
4020 bool fHostIPDef = false;
4021 bool fDumpMalformedPacketsDef = false;
4022
4023 int rc = RTStrToUInt32Full(s_iscsiConfigDefaultWriteSplit, 0, &uWriteSplitDef);
4024 AssertRC(rc);
4025 rc = RTStrToUInt32Full(s_iscsiConfigDefaultTimeout, 0, &uTimeoutDef);
4026 AssertRC(rc);
4027 rc = RTStrToUInt64Full(s_iscsiConfigDefaultHostIPStack, 0, &uCfgTmp);
4028 AssertRC(rc);
4029 fHostIPDef = RT_BOOL(uCfgTmp);
4030 rc = RTStrToUInt64Full(s_iscsiConfigDefaultDumpMalformedPackets, 0, &uCfgTmp);
4031 AssertRC(rc);
4032 fDumpMalformedPacketsDef = RT_BOOL(uCfgTmp);
4033
4034 /* Validate configuration, detect unknown keys. */
4035 if (!VDCFGAreKeysValid(pImage->pIfConfig,
4036 "TargetName\0"
4037 "InitiatorName\0"
4038 "LUN\0"
4039 "TargetAddress\0"
4040 "InitiatorUsername\0"
4041 "InitiatorSecret\0"
4042 "InitiatorSecretEncrypted\0"
4043 "TargetUsername\0"
4044 "TargetSecret\0"
4045 "WriteSplit\0"
4046 "Timeout\0"
4047 "HostIPStack\0"
4048 "DumpMalformedPackets\0"))
4049 return vdIfError(pImage->pIfError, VERR_VD_UNKNOWN_CFG_VALUES, RT_SRC_POS, N_("iSCSI: configuration error: unknown configuration keys present"));
4050
4051 /* Query the iSCSI upper level configuration. */
4052 rc = VDCFGQueryStringAlloc(pImage->pIfConfig, "TargetName", &pImage->pszTargetName);
4053 if (RT_FAILURE(rc))
4054 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetName as string"));
4055
4056 rc = VDCFGQueryStringAlloc(pImage->pIfConfig, "InitiatorName", &pImage->pszInitiatorName);
4057 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
4058 pImage->fAutomaticInitiatorName = true;
4059 else if (RT_FAILURE(rc))
4060 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorName as string"));
4061
4062 rc = VDCFGQueryStringAllocDef(pImage->pIfConfig, "LUN", &pszLUN, s_iscsiConfigDefaultLUN);
4063 if (RT_FAILURE(rc))
4064 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read LUN as string"));
4065
4066 pszLUNInitial = pszLUN;
4067 if (!strncmp(pszLUN, "enc", 3))
4068 {
4069 fLunEncoded = true;
4070 pszLUN += 3;
4071 }
4072 rc = RTStrToUInt64Full(pszLUN, 0, &pImage->LUN);
4073 if (RT_FAILURE(rc))
4074 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to convert LUN to integer"));
4075
4076 RTMemFree(pszLUNInitial);
4077 if (RT_SUCCESS(rc) && !fLunEncoded)
4078 {
4079 if (pImage->LUN <= 255)
4080 pImage->LUN = pImage->LUN << 48; /* uses peripheral device addressing method */
4081 else if (pImage->LUN <= 16383)
4082 pImage->LUN = (pImage->LUN << 48) | RT_BIT_64(62); /* uses flat space addressing method */
4083 else
4084 rc = vdIfError(pImage->pIfError, VERR_OUT_OF_RANGE, RT_SRC_POS, N_("iSCSI: configuration error: LUN number out of range (0-16383)"));
4085 }
4086
4087 if (RT_FAILURE(rc))
4088 return rc;
4089
4090 rc = VDCFGQueryStringAlloc(pImage->pIfConfig, "TargetAddress", &pImage->pszTargetAddress);
4091 if (RT_FAILURE(rc))
4092 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetAddress as string"));
4093
4094 rc = VDCFGQueryStringAlloc(pImage->pIfConfig, "InitiatorUsername", &pImage->pszInitiatorUsername);
4095 if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND && rc != VERR_CFGM_NO_PARENT)
4096 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorUsername as string"));
4097
4098 rc = VDCFGQueryBytesAlloc(pImage->pIfConfig, "InitiatorSecret",
4099 (void **)&pImage->pbInitiatorSecret, &pImage->cbInitiatorSecret);
4100 if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND && rc != VERR_CFGM_NO_PARENT)
4101 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorSecret as byte string"));
4102
4103 void *pvInitiatorSecretEncrypted;
4104 size_t cbInitiatorSecretEncrypted;
4105 rc = VDCFGQueryBytesAlloc(pImage->pIfConfig, "InitiatorSecretEncrypted",
4106 &pvInitiatorSecretEncrypted, &cbInitiatorSecretEncrypted);
4107 if (RT_SUCCESS(rc))
4108 {
4109 RTMemFree(pvInitiatorSecretEncrypted);
4110 if (!pImage->pbInitiatorSecret)
4111 {
4112 /* we have an encrypted initiator secret but not an unencrypted one */
4113 return vdIfError(pImage->pIfError, VERR_VD_ISCSI_SECRET_ENCRYPTED, RT_SRC_POS, N_("iSCSI: initiator secret not decrypted"));
4114 }
4115 }
4116
4117 rc = VDCFGQueryStringAlloc(pImage->pIfConfig, "TargetUsername", &pImage->pszTargetUsername);
4118 if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND && rc != VERR_CFGM_NO_PARENT)
4119 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetUsername as string"));
4120
4121 rc = VDCFGQueryBytesAlloc(pImage->pIfConfig, "TargetSecret",
4122 (void **)&pImage->pbTargetSecret, &pImage->cbTargetSecret);
4123 if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND && rc != VERR_CFGM_NO_PARENT)
4124 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetSecret as byte string"));
4125
4126 rc = VDCFGQueryU32Def(pImage->pIfConfig, "WriteSplit", &pImage->cbWriteSplit, uWriteSplitDef);
4127 if (RT_FAILURE(rc))
4128 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read WriteSplit as U32"));
4129
4130 /* Query the iSCSI lower level configuration. */
4131 rc = VDCFGQueryU32Def(pImage->pIfConfig, "Timeout", &pImage->uReadTimeout, uTimeoutDef);
4132 if (RT_FAILURE(rc))
4133 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read Timeout as U32"));
4134
4135 rc = VDCFGQueryBoolDef(pImage->pIfConfig, "HostIPStack", &pImage->fHostIP, fHostIPDef);
4136 if (RT_FAILURE(rc))
4137 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read HostIPStack as boolean"));
4138
4139 rc = VDCFGQueryBoolDef(pImage->pIfConfig, "DumpMalformedPackets",
4140 &pImage->fDumpMalformedPackets, fDumpMalformedPacketsDef);
4141 if (RT_FAILURE(rc))
4142 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read DumpMalformedPackets as boolean"));
4143
4144 return VINF_SUCCESS;
4145}
4146
4147/**
4148 * Creates the necessary socket structure.
4149 *
4150 * @returns VBox status code.
4151 * @param pImage The iSCSI image instance.
4152 */
4153static int iscsiOpenImageSocketCreate(PISCSIIMAGE pImage)
4154{
4155 /* Create the socket structure. */
4156 int rc = pImage->pIfNet->pfnSocketCreate(VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT,
4157 &pImage->Socket);
4158 if (RT_SUCCESS(rc))
4159 {
4160 pImage->fExtendedSelectSupported = true;
4161 pImage->fRunning = true;
4162 rc = RTThreadCreate(&pImage->hThreadIo, iscsiIoThreadWorker, pImage, 0,
4163 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "iSCSI-Io");
4164 if (RT_FAILURE(rc))
4165 LogFunc(("Creating iSCSI I/O thread failed rc=%Rrc\n", rc));
4166 }
4167 else if (rc == VERR_NOT_SUPPORTED)
4168 {
4169 /* Async I/O is not supported without extended select. */
4170 if ((pImage->uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
4171 LogFunc(("Extended select is not supported by the interface but async I/O is requested -> %Rrc\n", rc));
4172 else
4173 {
4174 pImage->fExtendedSelectSupported = false;
4175 rc = pImage->pIfNet->pfnSocketCreate(0, &pImage->Socket);
4176 }
4177 }
4178
4179 if (RT_FAILURE(rc))
4180 LogFunc(("Creating socket failed -> %Rrc\n", rc));
4181
4182 return rc;
4183}
4184
4185/**
4186 * Issues a REPORT LUNS to the target.
4187 *
4188 * @returns VBox status code.
4189 * @param pImage The iSCSI image instance.
4190 */
4191static int iscsiOpenImageReportLuns(PISCSIIMAGE pImage)
4192{
4193 SCSIREQ sr;
4194 RTSGSEG DataSeg;
4195 uint8_t rlundata[16];
4196
4197 /*
4198 * Inquire available LUNs - purely dummy request.
4199 */
4200 RT_ZERO(sr.abCDB);
4201 sr.abCDB[0] = SCSI_REPORT_LUNS;
4202 sr.abCDB[1] = 0; /* reserved */
4203 sr.abCDB[2] = 0; /* reserved */
4204 sr.abCDB[3] = 0; /* reserved */
4205 sr.abCDB[4] = 0; /* reserved */
4206 sr.abCDB[5] = 0; /* reserved */
4207 sr.abCDB[6] = sizeof(rlundata) >> 24;
4208 sr.abCDB[7] = (sizeof(rlundata) >> 16) & 0xff;
4209 sr.abCDB[8] = (sizeof(rlundata) >> 8) & 0xff;
4210 sr.abCDB[9] = sizeof(rlundata) & 0xff;
4211 sr.abCDB[10] = 0; /* reserved */
4212 sr.abCDB[11] = 0; /* control */
4213
4214 DataSeg.pvSeg = rlundata;
4215 DataSeg.cbSeg = sizeof(rlundata);
4216
4217 sr.enmXfer = SCSIXFER_FROM_TARGET;
4218 sr.cbCDB = 12;
4219 sr.cbI2TData = 0;
4220 sr.paI2TSegs = NULL;
4221 sr.cI2TSegs = 0;
4222 sr.cbT2IData = DataSeg.cbSeg;
4223 sr.paT2ISegs = &DataSeg;
4224 sr.cT2ISegs = 1;
4225 sr.cbSense = sizeof(sr.abSense);
4226 int rc = iscsiCommandSync(pImage, &sr, false, VERR_INVALID_STATE);
4227 if (RT_FAILURE(rc))
4228 LogRel(("iSCSI: Could not get LUN info for target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
4229
4230 return rc;
4231}
4232
4233/**
4234 * Issues the INQUIRY command to the target and checks for the correct device type.
4235 *
4236 * @returns VBox status code.
4237 * @param pImage The iSCSI image instance.
4238 */
4239static int iscsiOpenImageInquiry(PISCSIIMAGE pImage)
4240{
4241 SCSIREQ sr;
4242 RTSGSEG DataSeg;
4243 uint8_t data8[8];
4244
4245 /*
4246 * Inquire device characteristics - no tapes, scanners etc., please.
4247 */
4248 RT_ZERO(sr.abCDB);
4249 sr.abCDB[0] = SCSI_INQUIRY;
4250 sr.abCDB[1] = 0; /* reserved */
4251 sr.abCDB[2] = 0; /* reserved */
4252 sr.abCDB[3] = 0; /* reserved */
4253 sr.abCDB[4] = sizeof(data8);
4254 sr.abCDB[5] = 0; /* control */
4255
4256 DataSeg.pvSeg = data8;
4257 DataSeg.cbSeg = sizeof(data8);
4258
4259 sr.enmXfer = SCSIXFER_FROM_TARGET;
4260 sr.cbCDB = 6;
4261 sr.cbI2TData = 0;
4262 sr.paI2TSegs = NULL;
4263 sr.cI2TSegs = 0;
4264 sr.cbT2IData = DataSeg.cbSeg;
4265 sr.paT2ISegs = &DataSeg;
4266 sr.cT2ISegs = 1;
4267 sr.cbSense = sizeof(sr.abSense);
4268 int rc = iscsiCommandSync(pImage, &sr, true /* fRetry */, VERR_INVALID_STATE);
4269 if (RT_SUCCESS(rc))
4270 {
4271 uint8_t devType = (sr.cbT2IData > 0) ? data8[0] & SCSI_DEVTYPE_MASK : 255;
4272 if (devType == SCSI_DEVTYPE_DISK)
4273 {
4274 uint8_t uCmdQueue = (sr.cbT2IData >= 8) ? data8[7] & SCSI_INQUIRY_CMDQUE_MASK : 0;
4275 if (uCmdQueue > 0)
4276 pImage->fCmdQueuingSupported = true;
4277 else if (pImage->uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)
4278 rc = VERR_NOT_SUPPORTED;
4279 else
4280 LogRel(("iSCSI: target address %s, target name %s, %s command queuing\n",
4281 pImage->pszTargetAddress, pImage->pszTargetName,
4282 pImage->fCmdQueuingSupported ? "supports" : "doesn't support"));
4283 }
4284 else
4285 {
4286 rc = vdIfError(pImage->pIfError, VERR_VD_ISCSI_INVALID_TYPE,
4287 RT_SRC_POS, N_("iSCSI: target address %s, target name %s, SCSI LUN %lld reports device type=%u"),
4288 pImage->pszTargetAddress, pImage->pszTargetName,
4289 pImage->LUN, devType);
4290 LogRel(("iSCSI: Unsupported SCSI peripheral device type %d for target %s\n", devType & SCSI_DEVTYPE_MASK, pImage->pszTargetName));
4291 }
4292 }
4293 else
4294 LogRel(("iSCSI: Could not get INQUIRY info for target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
4295
4296 return rc;
4297}
4298
4299/**
4300 * Checks that the target allows write access if the caller requested it.
4301 *
4302 * @returns VBox status code.
4303 * @param pImage The iSCSI image instance.
4304 */
4305static int iscsiOpenImageCheckWriteAccess(PISCSIIMAGE pImage)
4306{
4307 SCSIREQ sr;
4308 RTSGSEG DataSeg;
4309 uint8_t data4[4];
4310
4311 /*
4312 * Query write disable bit in the device specific parameter entry in the
4313 * mode parameter header. Refuse read/write opening of read only disks.
4314 */
4315 RT_ZERO(sr.abCDB);
4316 sr.abCDB[0] = SCSI_MODE_SENSE_6;
4317 sr.abCDB[1] = 0; /* dbd=0/reserved */
4318 sr.abCDB[2] = 0x3f; /* pc=0/page code=0x3f, ask for all pages */
4319 sr.abCDB[3] = 0; /* subpage code=0, return everything in page_0 format */
4320 sr.abCDB[4] = sizeof(data4); /* allocation length=4 */
4321 sr.abCDB[5] = 0; /* control */
4322
4323 DataSeg.pvSeg = data4;
4324 DataSeg.cbSeg = sizeof(data4);
4325
4326 sr.enmXfer = SCSIXFER_FROM_TARGET;
4327 sr.cbCDB = 6;
4328 sr.cbI2TData = 0;
4329 sr.paI2TSegs = NULL;
4330 sr.cI2TSegs = 0;
4331 sr.cbT2IData = DataSeg.cbSeg;
4332 sr.paT2ISegs = &DataSeg;
4333 sr.cT2ISegs = 1;
4334 sr.cbSense = sizeof(sr.abSense);
4335 int rc = iscsiCommandSync(pImage, &sr, true /* fRetry */, VERR_INVALID_STATE);
4336 if (RT_SUCCESS(rc))
4337 {
4338 pImage->fTargetReadOnly = !!(data4[2] & 0x80);
4339 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) && pImage->fTargetReadOnly)
4340 rc = VERR_VD_IMAGE_READ_ONLY;
4341 }
4342 else
4343 LogRel(("iSCSI: Could not get MODE SENSE info for target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
4344
4345 return rc;
4346}
4347
4348/**
4349 * Queries the media and sector size of the target.
4350 *
4351 * @returns VBox status code.
4352 * @param pImage The iSCSI image instance.
4353 */
4354static int iscsiOpenImageQueryTargetSizes(PISCSIIMAGE pImage)
4355{
4356 SCSIREQ sr;
4357 RTSGSEG DataSeg;
4358 uint8_t data12[12];
4359
4360 /*
4361 * Determine sector size and capacity of the volume immediately.
4362 */
4363 RT_ZERO(data12);
4364 RT_ZERO(sr.abCDB);
4365 sr.abCDB[0] = SCSI_SERVICE_ACTION_IN_16;
4366 sr.abCDB[1] = SCSI_SVC_ACTION_IN_READ_CAPACITY_16; /* subcommand */
4367 sr.abCDB[10+3] = sizeof(data12); /* allocation length (dword) */
4368
4369 DataSeg.pvSeg = data12;
4370 DataSeg.cbSeg = sizeof(data12);
4371
4372 sr.enmXfer = SCSIXFER_FROM_TARGET;
4373 sr.cbCDB = 16;
4374 sr.cbI2TData = 0;
4375 sr.paI2TSegs = NULL;
4376 sr.cI2TSegs = 0;
4377 sr.cbT2IData = DataSeg.cbSeg;
4378 sr.paT2ISegs = &DataSeg;
4379 sr.cT2ISegs = 1;
4380 sr.cbSense = sizeof(sr.abSense);
4381
4382 int rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4383 if (RT_SUCCESS(rc))
4384 {
4385 bool fEnd = false;
4386 uint8_t cMaxRetries = 10;
4387 do
4388 {
4389 switch (sr.status)
4390 {
4391 case SCSI_STATUS_OK:
4392 {
4393 pImage->cVolume = RT_BE2H_U64(*(uint64_t *)&data12[0]);
4394 pImage->cVolume++;
4395 pImage->cbSector = RT_BE2H_U32(*(uint32_t *)&data12[8]);
4396 pImage->cbSize = pImage->cVolume * pImage->cbSector;
4397 if (pImage->cVolume == 0 || pImage->cbSize < pImage->cVolume)
4398 {
4399 rc = vdIfError(pImage->pIfError, VERR_VD_ISCSI_INVALID_TYPE,
4400 RT_SRC_POS, N_("iSCSI: target address %s, target name %s, SCSI LUN %lld reports media sector count=%llu sector size=%u"),
4401 pImage->pszTargetAddress, pImage->pszTargetName,
4402 pImage->LUN, pImage->cVolume, pImage->cbSector);
4403 }
4404 fEnd = true;
4405 break;
4406 }
4407 case SCSI_STATUS_CHECK_CONDITION:
4408 {
4409 if((sr.abSense[2] & 0x0f) == SCSI_SENSE_UNIT_ATTENTION)
4410 {
4411 if( sr.abSense[12] == SCSI_ASC_POWER_ON_RESET_BUS_DEVICE_RESET_OCCURRED
4412 && sr.abSense[13] == SCSI_ASCQ_POWER_ON_RESET_BUS_DEVICE_RESET_OCCURRED)
4413 {
4414 /** @todo for future: prepare and send command "REQUEST SENSE" which will
4415 * return the status of target and will clear any unit attention
4416 * condition that it reports */
4417 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4418 if (RT_FAILURE(rc))
4419 fEnd = true;
4420 cMaxRetries--;
4421 break;
4422 }
4423 }
4424 break;
4425 }
4426 default:
4427 {
4428 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4429 if (RT_FAILURE(rc))
4430 fEnd = true;
4431 cMaxRetries--;
4432 break;
4433 }
4434 }
4435 if (!cMaxRetries)
4436 fEnd = true;
4437 } while(!fEnd);
4438 }
4439 else
4440 {
4441 uint8_t data8[8];
4442
4443 RT_ZERO(data8);
4444 sr.abCDB[0] = SCSI_READ_CAPACITY;
4445 sr.abCDB[1] = 0; /* reserved */
4446 sr.abCDB[2] = 0; /* reserved */
4447 sr.abCDB[3] = 0; /* reserved */
4448 sr.abCDB[4] = 0; /* reserved */
4449 sr.abCDB[5] = 0; /* reserved */
4450 sr.abCDB[6] = 0; /* reserved */
4451 sr.abCDB[7] = 0; /* reserved */
4452 sr.abCDB[8] = 0; /* reserved */
4453 sr.abCDB[9] = 0; /* control */
4454
4455 DataSeg.pvSeg = data8;
4456 DataSeg.cbSeg = sizeof(data8);
4457
4458 sr.enmXfer = SCSIXFER_FROM_TARGET;
4459 sr.cbCDB = 10;
4460 sr.cbI2TData = 0;
4461 sr.paI2TSegs = NULL;
4462 sr.cI2TSegs = 0;
4463 sr.cbT2IData = DataSeg.cbSeg;
4464 sr.paT2ISegs = &DataSeg;
4465 sr.cT2ISegs = 1;
4466 sr.cbSense = sizeof(sr.abSense);
4467 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4468 if (RT_SUCCESS(rc))
4469 {
4470 bool fEnd = false;
4471 uint8_t cMaxRetries = 10;
4472 do
4473 {
4474 switch (sr.status)
4475 {
4476 case SCSI_STATUS_OK:
4477 {
4478 pImage->cVolume = (data8[0] << 24) | (data8[1] << 16) | (data8[2] << 8) | data8[3];
4479 pImage->cVolume++;
4480 pImage->cbSector = (data8[4] << 24) | (data8[5] << 16) | (data8[6] << 8) | data8[7];
4481 pImage->cbSize = pImage->cVolume * pImage->cbSector;
4482 if (pImage->cVolume == 0)
4483 {
4484 rc = vdIfError(pImage->pIfError, VERR_VD_ISCSI_INVALID_TYPE,
4485 RT_SRC_POS, N_("iSCSI: fallback capacity detection for target address %s, target name %s, SCSI LUN %lld reports media sector count=%llu sector size=%u"),
4486 pImage->pszTargetAddress, pImage->pszTargetName,
4487 pImage->LUN, pImage->cVolume, pImage->cbSector);
4488 }
4489
4490 fEnd = true;
4491 break;
4492 }
4493 case SCSI_STATUS_CHECK_CONDITION:
4494 {
4495 if((sr.abSense[2] & 0x0f) == SCSI_SENSE_UNIT_ATTENTION)
4496 {
4497 if( sr.abSense[12] == SCSI_ASC_POWER_ON_RESET_BUS_DEVICE_RESET_OCCURRED
4498 && sr.abSense[13] == SCSI_ASCQ_POWER_ON_RESET_BUS_DEVICE_RESET_OCCURRED)
4499 {
4500 /** @todo for future: prepare and send command "REQUEST SENSE" which will
4501 * return the status of target and will clear any unit attention
4502 * condition that it reports */
4503 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4504 if (RT_FAILURE(rc))
4505 fEnd = true;
4506 cMaxRetries--;
4507 break;
4508
4509 }
4510 }
4511 break;
4512 }
4513 default:
4514 {
4515 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4516 if (RT_FAILURE(rc))
4517 fEnd = true;
4518 cMaxRetries--;
4519 break;
4520 }
4521 }
4522 if (!cMaxRetries)
4523 fEnd = true;
4524 } while(!fEnd);
4525 }
4526 else
4527 LogRel(("iSCSI: Could not determine capacity of target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
4528 }
4529
4530 return rc;
4531}
4532
4533/**
4534 * Queries the state of the read/write caches and tries to enable them if disabled.
4535 *
4536 * @returns VBox status code.
4537 * @param pImage The iSCSI image instance.
4538 */
4539static int iscsiOpenImageEnableReadWriteCache(PISCSIIMAGE pImage)
4540{
4541 /*
4542 * Check the read and write cache bits.
4543 * Try to enable the cache if it is disabled.
4544 *
4545 * We already checked that this is a block access device. No need
4546 * to do it again.
4547 */
4548 SCSIREQ sr;
4549 RTSGSEG DataSeg;
4550 uint8_t aCachingModePage[32];
4551
4552 memset(aCachingModePage, '\0', sizeof(aCachingModePage));
4553 sr.abCDB[0] = SCSI_MODE_SENSE_6;
4554 sr.abCDB[1] = 0;
4555 sr.abCDB[2] = (0x00 << 6) | (0x08 & 0x3f); /* Current values and caching mode page */
4556 sr.abCDB[3] = 0; /* Sub page code. */
4557 sr.abCDB[4] = sizeof(aCachingModePage) & 0xff;
4558 sr.abCDB[5] = 0;
4559
4560 DataSeg.pvSeg = aCachingModePage;
4561 DataSeg.cbSeg = sizeof(aCachingModePage);
4562
4563 sr.enmXfer = SCSIXFER_FROM_TARGET;
4564 sr.cbCDB = 6;
4565 sr.cbI2TData = 0;
4566 sr.paI2TSegs = NULL;
4567 sr.cI2TSegs = 0;
4568 sr.cbT2IData = DataSeg.cbSeg;
4569 sr.paT2ISegs = &DataSeg;
4570 sr.cT2ISegs = 1;
4571 sr.cbSense = sizeof(sr.abSense);
4572 int rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4573 if ( RT_SUCCESS(rc)
4574 && (sr.status == SCSI_STATUS_OK)
4575 && (aCachingModePage[0] >= 15)
4576 && (aCachingModePage[4 + aCachingModePage[3]] & 0x3f) == 0x08
4577 && (aCachingModePage[4 + aCachingModePage[3] + 1] > 3))
4578 {
4579 uint32_t Offset = 4 + aCachingModePage[3];
4580 /*
4581 * Check if the read and/or the write cache is disabled.
4582 * The write cache is disabled if bit 2 (WCE) is zero and
4583 * the read cache is disabled if bit 0 (RCD) is set.
4584 */
4585 if (!ASMBitTest(&aCachingModePage[Offset + 2], 2) || ASMBitTest(&aCachingModePage[Offset + 2], 0))
4586 {
4587 /*
4588 * Write Cache Enable (WCE) bit is zero or the Read Cache Disable (RCD) is one
4589 * So one of the caches is disabled. Enable both caches.
4590 * The rest is unchanged.
4591 */
4592 ASMBitSet(&aCachingModePage[Offset + 2], 2);
4593 ASMBitClear(&aCachingModePage[Offset + 2], 0);
4594
4595 sr.abCDB[0] = SCSI_MODE_SELECT_6;
4596 sr.abCDB[1] = 0; /* Don't write the page into NV RAM. */
4597 sr.abCDB[2] = 0;
4598 sr.abCDB[3] = 0;
4599 sr.abCDB[4] = sizeof(aCachingModePage) & 0xff;
4600 sr.abCDB[5] = 0;
4601
4602 DataSeg.pvSeg = aCachingModePage;
4603 DataSeg.cbSeg = sizeof(aCachingModePage);
4604
4605 sr.enmXfer = SCSIXFER_TO_TARGET;
4606 sr.cbCDB = 6;
4607 sr.cbI2TData = DataSeg.cbSeg;
4608 sr.paI2TSegs = &DataSeg;
4609 sr.cI2TSegs = 1;
4610 sr.cbT2IData = 0;
4611 sr.paT2ISegs = NULL;
4612 sr.cT2ISegs = 0;
4613 sr.cbSense = sizeof(sr.abSense);
4614 sr.status = 0;
4615 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4616 if ( RT_SUCCESS(rc)
4617 && (sr.status == SCSI_STATUS_OK))
4618 LogRel(("iSCSI: Enabled read and write cache of target %s\n", pImage->pszTargetName));
4619 else
4620 {
4621 /* Log failures but continue. */
4622 LogRel(("iSCSI: Could not enable read and write cache of target %s, rc=%Rrc status=%#x\n",
4623 pImage->pszTargetName, rc, sr.status));
4624 LogRel(("iSCSI: Sense:\n%.*Rhxd\n", sr.cbSense, sr.abSense));
4625 rc = VINF_SUCCESS;
4626 }
4627 }
4628 }
4629 else
4630 {
4631 /* Log errors but continue. */
4632 LogRel(("iSCSI: Could not check write cache of target %s, rc=%Rrc, got mode page %#x\n", pImage->pszTargetName, rc, aCachingModePage[0] & 0x3f));
4633 LogRel(("iSCSI: Sense:\n%.*Rhxd\n", sr.cbSense, sr.abSense));
4634 rc = VINF_SUCCESS;
4635 }
4636
4637 return rc;
4638}
4639
4640/**
4641 * Internal: Open an image, constructing all necessary data structures.
4642 */
4643static int iscsiOpenImage(PISCSIIMAGE pImage, unsigned uOpenFlags)
4644{
4645 pImage->uOpenFlags = uOpenFlags;
4646
4647 int rc = iscsiOpenImageInit(pImage);
4648 if (RT_SUCCESS(rc))
4649 rc = iscsiOpenImageParseCfg(pImage);
4650
4651 if (RT_SUCCESS(rc))
4652 {
4653 /* Don't actually establish iSCSI transport connection if this is just an
4654 * open to query the image information and the host IP stack isn't used.
4655 * Even trying is rather useless, as in this context the InTnet IP stack
4656 * isn't present. Returning dummies is the best possible result anyway. */
4657 if ((uOpenFlags & VD_OPEN_FLAGS_INFO) && !pImage->fHostIP)
4658 LogFunc(("Not opening the transport connection as IntNet IP stack is not available. Will return dummies\n"));
4659 else
4660 {
4661 rc = iscsiOpenImageSocketCreate(pImage);
4662 if (RT_SUCCESS(rc))
4663 {
4664 /*
4665 * Attach to the iSCSI target. This implicitly establishes the iSCSI
4666 * transport connection.
4667 */
4668 rc = iscsiExecSync(pImage, iscsiAttach, pImage);
4669 if (RT_SUCCESS(rc))
4670 {
4671 LogFlowFunc(("target '%s' opened successfully\n", pImage->pszTargetName));
4672
4673 rc = iscsiOpenImageReportLuns(pImage);
4674 if (RT_SUCCESS(rc))
4675 rc = iscsiOpenImageInquiry(pImage);
4676 if (RT_SUCCESS(rc))
4677 rc = iscsiOpenImageCheckWriteAccess(pImage);
4678 if (RT_SUCCESS(rc))
4679 rc = iscsiOpenImageQueryTargetSizes(pImage);
4680 if (RT_SUCCESS(rc))
4681 rc = iscsiOpenImageEnableReadWriteCache(pImage);
4682 }
4683 else
4684 LogRel(("iSCSI: could not open target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
4685 }
4686 }
4687 }
4688
4689 if (RT_SUCCESS(rc))
4690 {
4691 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
4692 pImage->RegionList.fFlags = 0;
4693 pImage->RegionList.cRegions = 1;
4694
4695 pRegion->offRegion = 0; /* Disk start. */
4696 pRegion->cbBlock = pImage->cbSector;
4697 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
4698 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
4699 pRegion->cbData = pImage->cbSector;
4700 pRegion->cbMetadata = 0;
4701 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
4702 }
4703 else
4704 iscsiFreeImage(pImage, false);
4705 return rc;
4706}
4707
4708
4709/** @copydoc VDIMAGEBACKEND::pfnProbe */
4710static DECLCALLBACK(int) iscsiProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
4711 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
4712{
4713 RT_NOREF4(pszFilename, pVDIfsDisk, pVDIfsImage, penmType);
4714 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
4715
4716 /* iSCSI images can't be checked for validity this way, as the filename
4717 * just can't supply enough configuration information. */
4718 int rc = VERR_VD_ISCSI_INVALID_HEADER;
4719
4720 LogFlowFunc(("returns %Rrc\n", rc));
4721 return rc;
4722}
4723
4724/** @copydoc VDIMAGEBACKEND::pfnOpen */
4725static DECLCALLBACK(int) iscsiOpen(const char *pszFilename, unsigned uOpenFlags,
4726 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
4727 VDTYPE enmType, void **ppBackendData)
4728{
4729 RT_NOREF1(enmType); /**< @todo r=klaus make use of the type info. */
4730
4731 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n",
4732 pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
4733 int rc;
4734
4735 /* Check open flags. All valid flags are supported. */
4736 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
4737 AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
4738
4739 PISCSIIMAGE pImage = (PISCSIIMAGE)RTMemAllocZ(RT_UOFFSETOF(ISCSIIMAGE, RegionList.aRegions[1]));
4740 if (RT_LIKELY(pImage))
4741 {
4742 pImage->pszFilename = pszFilename;
4743 pImage->pszInitiatorName = NULL;
4744 pImage->pszTargetName = NULL;
4745 pImage->pszTargetAddress = NULL;
4746 pImage->pszInitiatorUsername = NULL;
4747 pImage->pbInitiatorSecret = NULL;
4748 pImage->pszTargetUsername = NULL;
4749 pImage->pbTargetSecret = NULL;
4750 pImage->paCurrReq = NULL;
4751 pImage->pvRecvPDUBuf = NULL;
4752 pImage->pszHostname = NULL;
4753 pImage->pVDIfsDisk = pVDIfsDisk;
4754 pImage->pVDIfsImage = pVDIfsImage;
4755 pImage->cLogRelErrors = 0;
4756
4757 rc = iscsiOpenImage(pImage, uOpenFlags);
4758 if (RT_SUCCESS(rc))
4759 {
4760 LogFlowFunc(("target %s cVolume %d, cbSector %d\n", pImage->pszTargetName, pImage->cVolume, pImage->cbSector));
4761 LogRel(("iSCSI: target address %s, target name %s, SCSI LUN %lld\n", pImage->pszTargetAddress, pImage->pszTargetName, pImage->LUN));
4762 *ppBackendData = pImage;
4763 }
4764 else
4765 RTMemFree(pImage);
4766 }
4767 else
4768 rc = VERR_NO_MEMORY;
4769
4770 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
4771 return rc;
4772}
4773
4774/** @copydoc VDIMAGEBACKEND::pfnCreate */
4775static DECLCALLBACK(int) iscsiCreate(const char *pszFilename, uint64_t cbSize,
4776 unsigned uImageFlags, const char *pszComment,
4777 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
4778 PCRTUUID pUuid, unsigned uOpenFlags,
4779 unsigned uPercentStart, unsigned uPercentSpan,
4780 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
4781 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
4782 void **ppBackendData)
4783{
4784 RT_NOREF8(pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags);
4785 RT_NOREF7(uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData);
4786 LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p enmType=%u ppBackendData=%#p",
4787 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
4788 int rc = VERR_NOT_SUPPORTED;
4789
4790 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
4791 return rc;
4792}
4793
4794/** @copydoc VDIMAGEBACKEND::pfnClose */
4795static DECLCALLBACK(int) iscsiClose(void *pBackendData, bool fDelete)
4796{
4797 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
4798 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
4799 int rc;
4800
4801 Assert(!fDelete); /* This flag is unsupported. */
4802
4803 rc = iscsiFreeImage(pImage, fDelete);
4804 RTMemFree(pImage);
4805
4806 LogFlowFunc(("returns %Rrc\n", rc));
4807 return rc;
4808}
4809
4810/** @copydoc VDIMAGEBACKEND::pfnRead */
4811static DECLCALLBACK(int) iscsiRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
4812 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
4813{
4814 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
4815 int rc = VINF_SUCCESS;
4816
4817 LogFlowFunc(("pBackendData=%p uOffset=%#llx pIoCtx=%#p cbToRead=%u pcbActuallyRead=%p\n",
4818 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
4819
4820 if ( uOffset + cbToRead > pImage->cbSize
4821 || cbToRead == 0)
4822 return VERR_INVALID_PARAMETER;
4823
4824 /*
4825 * Clip read size to a value which is supported by the target.
4826 */
4827 cbToRead = RT_MIN(cbToRead, pImage->cbRecvDataLength);
4828
4829 unsigned cT2ISegs = 0;
4830 size_t cbSegs = 0;
4831
4832 /* Get the number of segments. */
4833 cbSegs = pImage->pIfIo->pfnIoCtxSegArrayCreate(pImage->pIfIo->Core.pvUser, pIoCtx,
4834 NULL, &cT2ISegs, cbToRead);
4835 Assert(cbSegs == cbToRead);
4836
4837 PSCSIREQ pReq = (PSCSIREQ)RTMemAllocZ(RT_OFFSETOF(SCSIREQ, aSegs[cT2ISegs]));
4838 if (RT_LIKELY(pReq))
4839 {
4840 uint64_t lba;
4841 uint16_t tls;
4842 uint8_t *pbCDB = &pReq->abCDB[0];
4843 size_t cbCDB;
4844
4845 lba = uOffset / pImage->cbSector;
4846 tls = (uint16_t)(cbToRead / pImage->cbSector);
4847
4848 cbSegs = pImage->pIfIo->pfnIoCtxSegArrayCreate(pImage->pIfIo->Core.pvUser, pIoCtx,
4849 &pReq->aSegs[0],
4850 &cT2ISegs, cbToRead);
4851 Assert(cbSegs == cbToRead);
4852
4853 if (pImage->cVolume < _4G)
4854 {
4855 cbCDB = 10;
4856 pbCDB[0] = SCSI_READ_10;
4857 pbCDB[1] = 0; /* reserved */
4858 pbCDB[2] = (lba >> 24) & 0xff;
4859 pbCDB[3] = (lba >> 16) & 0xff;
4860 pbCDB[4] = (lba >> 8) & 0xff;
4861 pbCDB[5] = lba & 0xff;
4862 pbCDB[6] = 0; /* reserved */
4863 pbCDB[7] = (tls >> 8) & 0xff;
4864 pbCDB[8] = tls & 0xff;
4865 pbCDB[9] = 0; /* control */
4866 }
4867 else
4868 {
4869 cbCDB = 16;
4870 pbCDB[0] = SCSI_READ_16;
4871 pbCDB[1] = 0; /* reserved */
4872 pbCDB[2] = (lba >> 56) & 0xff;
4873 pbCDB[3] = (lba >> 48) & 0xff;
4874 pbCDB[4] = (lba >> 40) & 0xff;
4875 pbCDB[5] = (lba >> 32) & 0xff;
4876 pbCDB[6] = (lba >> 24) & 0xff;
4877 pbCDB[7] = (lba >> 16) & 0xff;
4878 pbCDB[8] = (lba >> 8) & 0xff;
4879 pbCDB[9] = lba & 0xff;
4880 pbCDB[10] = 0; /* tls unused */
4881 pbCDB[11] = 0; /* tls unused */
4882 pbCDB[12] = (tls >> 8) & 0xff;
4883 pbCDB[13] = tls & 0xff;
4884 pbCDB[14] = 0; /* reserved */
4885 pbCDB[15] = 0; /* reserved */
4886 }
4887
4888 pReq->enmXfer = SCSIXFER_FROM_TARGET;
4889 pReq->cbCDB = cbCDB;
4890 pReq->cbI2TData = 0;
4891 pReq->paI2TSegs = NULL;
4892 pReq->cI2TSegs = 0;
4893 pReq->cbT2IData = cbToRead;
4894 pReq->paT2ISegs = &pReq->aSegs[pReq->cI2TSegs];
4895 pReq->cbSense = sizeof(pReq->abSense);
4896 pReq->cT2ISegs = cT2ISegs;
4897 pReq->pIoCtx = pIoCtx;
4898 pReq->cSenseRetries = 10;
4899 pReq->rcSense = VERR_READ_ERROR;
4900
4901 if (vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx))
4902 {
4903 rc = iscsiCommandSync(pImage, pReq, true, VERR_READ_ERROR);
4904 if (RT_FAILURE(rc))
4905 {
4906 LogFlow(("iscsiCommandSync(%s, %#llx) -> %Rrc\n", pImage->pszTargetName, uOffset, rc));
4907 *pcbActuallyRead = 0;
4908 }
4909 else
4910 *pcbActuallyRead = pReq->cbT2IData;
4911 }
4912 else
4913 {
4914 rc = iscsiCommandAsync(pImage, pReq, iscsiCommandAsyncComplete, pReq);
4915 if (RT_FAILURE(rc))
4916 AssertMsgFailed(("iscsiCommandAsync(%s, %#llx) -> %Rrc\n", pImage->pszTargetName, uOffset, rc));
4917 else
4918 {
4919 *pcbActuallyRead = cbToRead;
4920 return VERR_VD_IOCTX_HALT; /* Halt the I/O context until further notification from the I/O thread. */
4921 }
4922 }
4923
4924 RTMemFree(pReq);
4925 }
4926 else
4927 rc = VERR_NO_MEMORY;
4928
4929 LogFlowFunc(("returns rc=%Rrc\n", rc));
4930 return rc;
4931}
4932
4933/** @copydoc VDIMAGEBACKEND::pfnWrite */
4934static DECLCALLBACK(int) iscsiWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
4935 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
4936 size_t *pcbPostRead, unsigned fWrite)
4937{
4938 RT_NOREF3(pcbPreRead, pcbPostRead, fWrite);
4939 LogFlowFunc(("pBackendData=%p uOffset=%llu pIoCtx=%#p cbToWrite=%u pcbWriteProcess=%p pcbPreRead=%p pcbPostRead=%p fWrite=%u\n",
4940 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead, fWrite));
4941 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
4942 int rc = VINF_SUCCESS;
4943
4944 AssertPtr(pImage);
4945 Assert(uOffset % 512 == 0);
4946 Assert(cbToWrite % 512 == 0);
4947
4948 if (uOffset + cbToWrite > pImage->cbSize)
4949 return VERR_INVALID_PARAMETER;
4950
4951 /*
4952 * Clip read size to a value which is supported by the target.
4953 */
4954 cbToWrite = RT_MIN(cbToWrite, pImage->cbSendDataLength);
4955
4956 unsigned cI2TSegs = 0;
4957 size_t cbSegs = 0;
4958
4959 /* Get the number of segments. */
4960 cbSegs = pImage->pIfIo->pfnIoCtxSegArrayCreate(pImage->pIfIo->Core.pvUser, pIoCtx,
4961 NULL, &cI2TSegs, cbToWrite);
4962 Assert(cbSegs == cbToWrite);
4963
4964 PSCSIREQ pReq = (PSCSIREQ)RTMemAllocZ(RT_OFFSETOF(SCSIREQ, aSegs[cI2TSegs]));
4965 if (RT_LIKELY(pReq))
4966 {
4967 uint64_t lba;
4968 uint16_t tls;
4969 uint8_t *pbCDB = &pReq->abCDB[0];
4970 size_t cbCDB;
4971
4972 lba = uOffset / pImage->cbSector;
4973 tls = (uint16_t)(cbToWrite / pImage->cbSector);
4974
4975 cbSegs = pImage->pIfIo->pfnIoCtxSegArrayCreate(pImage->pIfIo->Core.pvUser, pIoCtx,
4976 &pReq->aSegs[0],
4977 &cI2TSegs, cbToWrite);
4978 Assert(cbSegs == cbToWrite);
4979
4980 if (pImage->cVolume < _4G)
4981 {
4982 cbCDB = 10;
4983 pbCDB[0] = SCSI_WRITE_10;
4984 pbCDB[1] = 0; /* reserved */
4985 pbCDB[2] = (lba >> 24) & 0xff;
4986 pbCDB[3] = (lba >> 16) & 0xff;
4987 pbCDB[4] = (lba >> 8) & 0xff;
4988 pbCDB[5] = lba & 0xff;
4989 pbCDB[6] = 0; /* reserved */
4990 pbCDB[7] = (tls >> 8) & 0xff;
4991 pbCDB[8] = tls & 0xff;
4992 pbCDB[9] = 0; /* control */
4993 }
4994 else
4995 {
4996 cbCDB = 16;
4997 pbCDB[0] = SCSI_WRITE_16;
4998 pbCDB[1] = 0; /* reserved */
4999 pbCDB[2] = (lba >> 56) & 0xff;
5000 pbCDB[3] = (lba >> 48) & 0xff;
5001 pbCDB[4] = (lba >> 40) & 0xff;
5002 pbCDB[5] = (lba >> 32) & 0xff;
5003 pbCDB[6] = (lba >> 24) & 0xff;
5004 pbCDB[7] = (lba >> 16) & 0xff;
5005 pbCDB[8] = (lba >> 8) & 0xff;
5006 pbCDB[9] = lba & 0xff;
5007 pbCDB[10] = 0; /* tls unused */
5008 pbCDB[11] = 0; /* tls unused */
5009 pbCDB[12] = (tls >> 8) & 0xff;
5010 pbCDB[13] = tls & 0xff;
5011 pbCDB[14] = 0; /* reserved */
5012 pbCDB[15] = 0; /* reserved */
5013 }
5014
5015 pReq->enmXfer = SCSIXFER_TO_TARGET;
5016 pReq->cbCDB = cbCDB;
5017 pReq->cbI2TData = cbToWrite;
5018 pReq->paI2TSegs = &pReq->aSegs[0];
5019 pReq->cI2TSegs = cI2TSegs;
5020 pReq->cbT2IData = 0;
5021 pReq->paT2ISegs = NULL;
5022 pReq->cT2ISegs = 0;
5023 pReq->cbSense = sizeof(pReq->abSense);
5024 pReq->pIoCtx = pIoCtx;
5025 pReq->cSenseRetries = 10;
5026 pReq->rcSense = VERR_WRITE_ERROR;
5027
5028 if (vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx))
5029 {
5030 rc = iscsiCommandSync(pImage, pReq, true, VERR_WRITE_ERROR);
5031 if (RT_FAILURE(rc))
5032 {
5033 LogFlow(("iscsiCommandSync(%s, %#llx) -> %Rrc\n", pImage->pszTargetName, uOffset, rc));
5034 *pcbWriteProcess = 0;
5035 }
5036 else
5037 *pcbWriteProcess = cbToWrite;
5038 }
5039 else
5040 {
5041 rc = iscsiCommandAsync(pImage, pReq, iscsiCommandAsyncComplete, pReq);
5042 if (RT_FAILURE(rc))
5043 AssertMsgFailed(("iscsiCommandAsync(%s, %#llx) -> %Rrc\n", pImage->pszTargetName, uOffset, rc));
5044 else
5045 {
5046 *pcbWriteProcess = cbToWrite;
5047 return VERR_VD_IOCTX_HALT; /* Halt the I/O context until further notification from the I/O thread. */
5048 }
5049 }
5050
5051 RTMemFree(pReq);
5052 }
5053 else
5054 rc = VERR_NO_MEMORY;
5055
5056 LogFlowFunc(("returns rc=%Rrc\n", rc));
5057 return rc;
5058}
5059
5060/** @copydoc VDIMAGEBACKEND::pfnFlush */
5061static DECLCALLBACK(int) iscsiFlush(void *pBackendData, PVDIOCTX pIoCtx)
5062{
5063 LogFlowFunc(("pBackendData=%p pIoCtx=%#p\n", pBackendData, pIoCtx));
5064 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5065 int rc = VINF_SUCCESS;
5066
5067 PSCSIREQ pReq = (PSCSIREQ)RTMemAllocZ(sizeof(SCSIREQ));
5068 if (RT_LIKELY(pReq))
5069 {
5070 uint8_t *pbCDB = &pReq->abCDB[0];
5071
5072 pbCDB[0] = SCSI_SYNCHRONIZE_CACHE;
5073 pbCDB[1] = 0; /* reserved */
5074 pbCDB[2] = 0; /* reserved */
5075 pbCDB[3] = 0; /* reserved */
5076 pbCDB[4] = 0; /* reserved */
5077 pbCDB[5] = 0; /* reserved */
5078 pbCDB[6] = 0; /* reserved */
5079 pbCDB[7] = 0; /* reserved */
5080 pbCDB[8] = 0; /* reserved */
5081 pbCDB[9] = 0; /* control */
5082
5083 pReq->enmXfer = SCSIXFER_NONE;
5084 pReq->cbCDB = 10;
5085 pReq->cbI2TData = 0;
5086 pReq->paI2TSegs = NULL;
5087 pReq->cI2TSegs = 0;
5088 pReq->cbT2IData = 0;
5089 pReq->paT2ISegs = NULL;
5090 pReq->cT2ISegs = 0;
5091 pReq->cbSense = sizeof(pReq->abSense);
5092 pReq->pIoCtx = pIoCtx;
5093 pReq->cSenseRetries = 0;
5094 pReq->rcSense = VINF_SUCCESS;
5095
5096 if (vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx))
5097 {
5098 rc = iscsiCommandSync(pImage, pReq, false, VINF_SUCCESS);
5099 if (RT_FAILURE(rc))
5100 AssertMsgFailed(("iscsiCommand(%s) -> %Rrc\n", pImage->pszTargetName, rc));
5101 }
5102 else
5103 {
5104 rc = iscsiCommandAsync(pImage, pReq, iscsiCommandAsyncComplete, pReq);
5105 if (RT_FAILURE(rc))
5106 AssertMsgFailed(("iscsiCommand(%s) -> %Rrc\n", pImage->pszTargetName, rc));
5107 else
5108 return VERR_VD_IOCTX_HALT; /* Halt the I/O context until further notification from the I/O thread. */
5109 }
5110
5111 RTMemFree(pReq);
5112 }
5113 else
5114 rc = VERR_NO_MEMORY;
5115
5116 LogFlowFunc(("returns rc=%Rrc\n", rc));
5117 return rc;
5118}
5119
5120/** @copydoc VDIMAGEBACKEND::pfnGetVersion */
5121static DECLCALLBACK(unsigned) iscsiGetVersion(void *pBackendData)
5122{
5123 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
5124 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5125
5126 AssertPtr(pImage);
5127 RT_NOREF1(pImage);
5128
5129 return 0;
5130}
5131
5132/** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
5133static DECLCALLBACK(uint64_t) iscsiGetFileSize(void *pBackendData)
5134{
5135 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
5136 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5137
5138 AssertPtrReturn(pImage, 0);
5139
5140 return pImage->cbSize;
5141}
5142
5143/** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
5144static DECLCALLBACK(int) iscsiGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
5145{
5146 RT_NOREF1(pPCHSGeometry);
5147 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
5148 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5149
5150 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5151
5152 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", VERR_VD_GEOMETRY_NOT_SET,
5153 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
5154 return VERR_VD_GEOMETRY_NOT_SET;
5155}
5156
5157/** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
5158static DECLCALLBACK(int) iscsiSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
5159{
5160 RT_NOREF1(pPCHSGeometry);
5161 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
5162 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5163
5164 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5165
5166 int rc;
5167 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
5168 rc = VERR_VD_IMAGE_READ_ONLY;
5169 else
5170 rc = VERR_VD_GEOMETRY_NOT_SET;
5171
5172 LogFlowFunc(("returns %Rrc\n", rc));
5173 return rc;
5174}
5175
5176/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
5177static DECLCALLBACK(int) iscsiGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
5178{
5179 RT_NOREF1(pLCHSGeometry);
5180 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
5181 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5182
5183 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5184
5185 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", VERR_VD_GEOMETRY_NOT_SET,
5186 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
5187 return VERR_VD_GEOMETRY_NOT_SET;
5188}
5189
5190/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
5191static DECLCALLBACK(int) iscsiSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
5192{
5193 RT_NOREF1(pLCHSGeometry);
5194 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->