VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/ISCSIHDDCore.cpp@ 22256

Last change on this file since 22256 was 22256, checked in by vboxsync, 16 years ago

Storage/iSCSI: bunch of fixes which improve RFC compliance and error handling

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 128.5 KB
Line 
1/** @file
2 * iSCSI initiator driver, VD backend.
3 */
4
5/*
6 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
17 * Clara, CA 95054 USA or visit http://www.sun.com if you need
18 * additional information or have any questions.
19 */
20
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_VD_ISCSI
26#include "VBoxHDD-Internal.h"
27#define VBOX_VDICORE_VD /* Signal that the header is included from here. */
28#include "VDICore.h"
29#include <VBox/err.h>
30
31#include <VBox/log.h>
32#include <iprt/alloc.h>
33#include <iprt/assert.h>
34#include <iprt/uuid.h>
35#include <iprt/file.h>
36#include <iprt/string.h>
37#include <iprt/asm.h>
38#include <iprt/thread.h>
39#include <iprt/semaphore.h>
40#include <iprt/md5.h>
41#include <iprt/tcp.h>
42#include <iprt/time.h>
43#include <VBox/scsi.h>
44
45
46/*******************************************************************************
47* Defined Constants And Macros *
48*******************************************************************************/
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/** Minumum 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
110/** Maximum PDU payload size we can handle in one piece. */
111#define ISCSI_DATA_LENGTH_MAX _256K
112
113/** Maximum PDU size we can handle in one piece. */
114#define ISCSI_RECV_PDU_BUFFER_SIZE (ISCSI_DATA_LENGTH_MAX + ISCSI_BHS_SIZE)
115
116
117/** Version of the iSCSI standard which this initiator driver can handle. */
118#define ISCSI_MY_VERSION 0
119
120
121/** Length of ISCSI basic header segment. */
122#define ISCSI_BHS_SIZE 48
123
124
125/** Reserved task tag value. */
126#define ISCSI_TASK_TAG_RSVD 0xffffffff
127
128
129/**
130 * iSCSI opcodes. */
131typedef enum ISCSIOPCODE
132{
133 /** NOP-Out. */
134 ISCSIOP_NOP_OUT = 0x00000000,
135 /** SCSI command. */
136 ISCSIOP_SCSI_CMD = 0x01000000,
137 /** SCSI task management request. */
138 ISCSIOP_SCSI_TASKMGMT_REQ = 0x02000000,
139 /** Login request. */
140 ISCSIOP_LOGIN_REQ = 0x03000000,
141 /** Text request. */
142 ISCSIOP_TEXT_REQ = 0x04000000,
143 /** SCSI Data-Out. */
144 ISCSIOP_SCSI_DATA_OUT = 0x05000000,
145 /** Logout request. */
146 ISCSIOP_LOGOUT_REQ = 0x06000000,
147 /** SNACK request. */
148 ISCSIOP_SNACK_REQ = 0x10000000,
149
150 /** NOP-In. */
151 ISCSIOP_NOP_IN = 0x20000000,
152 /** SCSI response. */
153 ISCSIOP_SCSI_RES = 0x21000000,
154 /** SCSI Task Management response. */
155 ISCSIOP_SCSI_TASKMGMT_RES = 0x22000000,
156 /** Login response. */
157 ISCSIOP_LOGIN_RES = 0x23000000,
158 /** Text response. */
159 ISCSIOP_TEXT_RES = 0x24000000,
160 /** SCSI Data-In. */
161 ISCSIOP_SCSI_DATA_IN = 0x25000000,
162 /** Logout response. */
163 ISCSIOP_LOGOUT_RES = 0x26000000,
164 /** Ready To Transfer (R2T). */
165 ISCSIOP_R2T = 0x31000000,
166 /** Asynchronous message. */
167 ISCSIOP_ASYN_MSG = 0x32000000,
168 /** Reject. */
169 ISCSIOP_REJECT = 0x3f000000
170} ISCSIOPCODE;
171
172/** Mask for extracting the iSCSI opcode out of the first header word. */
173#define ISCSIOP_MASK 0x3f000000
174
175
176/** ISCSI BHS word 0: Request should be processed immediately. */
177#define ISCSI_IMMEDIATE_DELIVERY_BIT 0x40000000
178
179/** ISCSI BHS word 0: This is the final PDU for this request/response. */
180#define ISCSI_FINAL_BIT 0x00800000
181/** ISCSI BHS word 0: Mask for extracting the CSG. */
182#define ISCSI_CSG_MASK 0x000c0000
183/** ISCSI BHS word 0: Shift offset for extracting the CSG. */
184#define ISCSI_CSG_SHIFT 18
185/** ISCSI BHS word 0: Mask for extracting the NSG. */
186#define ISCSI_NSG_MASK 0x00030000
187/** ISCSI BHS word 0: Shift offset for extracting the NSG. */
188#define ISCSI_NSG_SHIFT 16
189
190/** ISCSI BHS word 0: task attribute untagged */
191#define ISCSI_TASK_ATTR_UNTAGGED 0x00000000
192/** ISCSI BHS word 0: task attribute simple */
193#define ISCSI_TASK_ATTR_SIMPLE 0x00010000
194/** ISCSI BHS word 0: task attribute ordered */
195#define ISCSI_TASK_ATTR_ORDERED 0x00020000
196/** ISCSI BHS word 0: task attribute head of queue */
197#define ISCSI_TASK_ATTR_HOQ 0x00030000
198/** ISCSI BHS word 0: task attribute ACA */
199#define ISCSI_TASK_ATTR_ACA 0x00040000
200
201/** ISCSI BHS word 0: transit to next login phase. */
202#define ISCSI_TRANSIT_BIT 0x00800000
203/** ISCSI BHS word 0: continue with login negotiation. */
204#define ISCSI_CONTINUE_BIT 0x00400000
205
206/** ISCSI BHS word 0: residual underflow. */
207#define ISCSI_RESIDUAL_UNFL_BIT 0x00020000
208/** ISCSI BHS word 0: residual overflow. */
209#define ISCSI_RESIDUAL_OVFL_BIT 0x00040000
210/** ISCSI BHS word 0: Bidirectional read residual underflow. */
211#define ISCSI_BI_READ_RESIDUAL_UNFL_BIT 0x00080000
212/** ISCSI BHS word 0: Bidirectional read residual overflow. */
213#define ISCSI_BI_READ_RESIDUAL_OVFL_BIT 0x00100000
214
215/** ISCSI BHS word 0: SCSI response mask. */
216#define ISCSI_SCSI_RESPONSE_MASK 0x0000ff00
217/** ISCSI BHS word 0: SCSI status mask. */
218#define ISCSI_SCSI_STATUS_MASK 0x000000ff
219
220/** ISCSI BHS word 0: response includes status. */
221#define ISCSI_STATUS_BIT 0x00010000
222
223
224/**
225 * iSCSI login status class. */
226typedef enum ISCSILOGINSTATUSCLASS
227{
228 /** Success. */
229 ISCSI_LOGIN_STATUS_CLASS_SUCCESS = 0,
230 /** Redirection. */
231 ISCSI_LOGIN_STATUS_CLASS_REDIRECTION,
232 /** Initiator error. */
233 ISCSI_LOGIN_STATUS_CLASS_INITIATOR_ERROR,
234 /** Target error. */
235 ISCSI_LOGIN_STATUS_CLASS_TARGET_ERROR
236} ISCSILOGINSTATUSCLASS;
237
238
239/**
240 * iSCSI connection state. */
241typedef enum ISCSISTATE
242{
243 /** Not having a connection/session at all. */
244 ISCSISTATE_FREE,
245 /** Currently trying to login. */
246 ISCSISTATE_IN_LOGIN,
247 /** Normal operation, corresponds roughly to the Full Feature Phase. */
248 ISCSISTATE_NORMAL,
249 /** Currently trying to logout. */
250 ISCSISTATE_IN_LOGOUT
251} ISCSISTATE;
252
253
254/*******************************************************************************
255* Structures and Typedefs *
256*******************************************************************************/
257/**
258 * Block driver instance data.
259 */
260typedef struct ISCSIIMAGE
261{
262 /** Pointer to the filename (location). Not really used. */
263 const char *pszFilename;
264 /** Pointer to the initiator name. */
265 char *pszInitiatorName;
266 /** Pointer to the target name. */
267 char *pszTargetName;
268 /** Pointer to the target address. */
269 char *pszTargetAddress;
270 /** Pointer to the user name for authenticating the Initiator. */
271 char *pszInitiatorUsername;
272 /** Pointer to the secret for authenticating the Initiator. */
273 uint8_t *pbInitiatorSecret;
274 /** Length of the secret for authenticating the Initiator. */
275 size_t cbInitiatorSecret;
276 /** Pointer to the user name for authenticating the Target. */
277 char *pszTargetUsername;
278 /** Pointer to the secret for authenticating the Initiator. */
279 uint8_t *pbTargetSecret;
280 /** Length of the secret for authenticating the Initiator. */
281 size_t cbTargetSecret;
282 /** Initiator session identifier. */
283 uint64_t ISID;
284 /** SCSI Logical Unit Number. */
285 uint64_t LUN;
286 /** Pointer to the per-disk VD interface list. */
287 PVDINTERFACE pVDIfsDisk;
288 /** Error interface. */
289 PVDINTERFACE pInterfaceError;
290 /** Error interface callback table. */
291 PVDINTERFACEERROR pInterfaceErrorCallbacks;
292 /** TCP network stack interface. */
293 PVDINTERFACE pInterfaceNet;
294 /** TCP network stack interface callback table. */
295 PVDINTERFACETCPNET pInterfaceNetCallbacks;
296 /** Pointer to the per-image VD interface list. */
297 PVDINTERFACE pVDIfsImage;
298 /** Config interface. */
299 PVDINTERFACE pInterfaceConfig;
300 /** Config interface callback table. */
301 PVDINTERFACECONFIG pInterfaceConfigCallbacks;
302 /** Image open flags. */
303 unsigned uOpenFlags;
304 /** Number of re-login retries when a connection fails. */
305 uint32_t cISCSIRetries;
306 /** Size of volume in sectors. */
307 uint32_t cVolume;
308 /** Sector size on volume. */
309 uint32_t cbSector;
310 /** Total volume size in bytes. Easiert that multiplying the above values all the time. */
311 uint64_t cbSize;
312
313 /** Negotiated maximum data length when sending to target. */
314 uint32_t cbSendDataLength;
315 /** Negotiated maximum data length when receiving from target. */
316 uint32_t cbRecvDataLength;
317
318 /** Current state of the connection/session. */
319 ISCSISTATE state;
320 /** Flag whether the first Login Response PDU has been seen. */
321 bool FirstRecvPDU;
322 /** Initiator Task Tag of the last iSCSI request PDU. */
323 uint32_t ITT;
324 /** Sequence number of the last command. */
325 uint32_t CmdSN;
326 /** Sequence number of the next command expected by the target. */
327 uint32_t ExpCmdSN;
328 /** Maximum sequence number accepted by the target (determines size of window). */
329 uint32_t MaxCmdSN;
330 /** Expected sequence number of next status. */
331 uint32_t ExpStatSN;
332 /** Currently active request. */
333 PISCSIREQ paCurrReq;
334 /** Segment number of currently active request. */
335 uint32_t cnCurrReq;
336 /** Pointer to receive PDU buffer. (Freed by RT) */
337 void *pvRecvPDUBuf;
338 /** Length of receive PDU buffer. */
339 size_t cbRecvPDUBuf;
340 /** Mutex protecting against concurrent use from several threads. */
341 RTSEMMUTEX Mutex;
342
343 /** Pointer to the target hostname. */
344 char *pszHostname;
345 /** Pointer to the target hostname. */
346 uint32_t uPort;
347 /** Socket handle of the TCP connection. */
348 RTSOCKET Socket;
349 /** Timeout for read operations on the TCP connection (in milliseconds). */
350 uint32_t uReadTimeout;
351 /** Flag whether to use the host IP stack or DevINIP. */
352 bool fHostIP;
353} ISCSIIMAGE, *PISCSIIMAGE;
354
355
356/**
357 * SCSI transfer directions.
358 */
359typedef enum SCSIXFER
360{
361 SCSIXFER_NONE = 0,
362 SCSIXFER_TO_TARGET,
363 SCSIXFER_FROM_TARGET,
364 SCSIXFER_TO_FROM_TARGET
365} SCSIXFER, *PSCSIXFER;
366
367
368/**
369 * SCSI request structure.
370 */
371typedef struct SCSIREQ
372{
373 /** Transfer direction. */
374 SCSIXFER enmXfer;
375 /** Length of command block. */
376 size_t cbCmd;
377 /** Length of Initiator2Target data buffer. */
378 size_t cbI2TData;
379 /** Length of Target2Initiator data buffer. */
380 size_t cbT2IData;
381 /** Length of sense buffer. */
382 size_t cbSense;
383 /** Completion status of the command. */
384 uint8_t status;
385 /** Pointer to command block. */
386 void *pvCmd;
387 /** Pointer to Initiator2Target data buffer. */
388 const void *pcvI2TData;
389 /** Pointer to Target2Initiator data buffer. */
390 void *pvT2IData;
391 /** Pointer to sense buffer. */
392 void *pvSense;
393} SCSIREQ, *PSCSIREQ;
394
395
396/**
397 * iSCSI login negotiation parameter
398 */
399typedef struct ISCSIPARAMETER
400{
401 /** Name of the parameter. */
402 const char *pszParamName;
403 /** Value of the parameter. */
404 const char *pszParamValue;
405 /** Length of the binary parameter. 0=zero-terminated string. */
406 size_t cbParamValue;
407} ISCSIPARAMETER;
408
409
410/*******************************************************************************
411* Static Variables *
412*******************************************************************************/
413
414/** Counter for getting unique instance IDs. */
415static uint32_t s_u32iscsiID = 0;
416
417/** Default LUN. */
418static const char *s_iscsiConfigDefaultLUN = "0";
419
420/** Default initiator name. */
421static const char *s_iscsiConfigDefaultInitiatorName = "iqn.2009-08.com.sun.virtualbox.initiator";
422
423/** Default timeout, 10 seconds. */
424static const char *s_iscsiConfigDefaultTimeout = "10000";
425
426/** Default host IP stack. */
427static const char *s_iscsiConfigDefaultHostIPStack = "1";
428
429/** Description of all accepted config parameters. */
430static const VDCONFIGINFO s_iscsiConfigInfo[] =
431{
432 { "TargetName", NULL, VDCFGVALUETYPE_STRING, VD_CFGKEY_MANDATORY },
433 /* LUN is defined of string type to handle the "enc" prefix. */
434 { "LUN", s_iscsiConfigDefaultLUN, VDCFGVALUETYPE_STRING, VD_CFGKEY_MANDATORY },
435 { "TargetAddress", NULL, VDCFGVALUETYPE_STRING, VD_CFGKEY_MANDATORY },
436 { "InitiatorName", s_iscsiConfigDefaultInitiatorName, VDCFGVALUETYPE_STRING, 0 },
437 { "InitiatorUsername", NULL, VDCFGVALUETYPE_STRING, 0 },
438 { "InitiatorSecret", NULL, VDCFGVALUETYPE_BYTES, 0 },
439 { "TargetUsername", NULL, VDCFGVALUETYPE_STRING, VD_CFGKEY_EXPERT },
440 { "TargetSecret", NULL, VDCFGVALUETYPE_BYTES, VD_CFGKEY_EXPERT },
441 { "Timeout", s_iscsiConfigDefaultTimeout, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_EXPERT },
442 { "HostIPStack", s_iscsiConfigDefaultHostIPStack, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_EXPERT },
443 { NULL, NULL, VDCFGVALUETYPE_INTEGER, 0 }
444};
445
446/*******************************************************************************
447* Internal Functions *
448*******************************************************************************/
449
450/* iSCSI low-level functions (only to be used from the iSCSI high-level functions). */
451static uint32_t iscsiNewITT(PISCSIIMAGE pImage);
452static int iscsiSendPDU(PISCSIIMAGE pImage, PISCSIREQ paReq, uint32_t cnReq);
453static int iscsiRecvPDU(PISCSIIMAGE pImage, uint32_t itt, PISCSIRES paRes, uint32_t cnRes);
454static int drvISCSIValidatePDU(PISCSIRES paRes, uint32_t cnRes);
455static int iscsiTextAddKeyValue(uint8_t *pbBuf, size_t cbBuf, size_t *pcbBufCurr, const char *pcszKey, const char *pcszValue, size_t cbValue);
456static int iscsiTextGetKeyValue(const uint8_t *pbBuf, size_t cbBuf, const char *pcszKey, const char **ppcszValue);
457static int iscsiStrToBinary(const char *pcszValue, uint8_t *pbValue, size_t *pcbValue);
458
459/* Serial number arithmetic comparison. */
460static bool serial_number_less(uint32_t sn1, uint32_t sn2);
461
462/* CHAP-MD5 functions. */
463#ifdef IMPLEMENT_TARGET_AUTH
464static void chap_md5_generate_challenge(uint8_t *pbChallenge, size_t *pcbChallenge);
465#endif
466static void chap_md5_compute_response(uint8_t *pbResponse, uint8_t id, const uint8_t *pbChallenge, size_t cbChallenge,
467 const uint8_t *pbSecret, size_t cbSecret);
468
469
470/**
471 * Internal: signal an error to the frontend.
472 */
473DECLINLINE(int) iscsiError(PISCSIIMAGE pImage, int rc, RT_SRC_POS_DECL,
474 const char *pszFormat, ...)
475{
476 va_list va;
477 va_start(va, pszFormat);
478 if (pImage->pInterfaceError)
479 pImage->pInterfaceErrorCallbacks->pfnError(pImage->pInterfaceError->pvUser, rc, RT_SRC_POS_ARGS,
480 pszFormat, va);
481 va_end(va);
482 return rc;
483}
484
485
486static int iscsiTransportRead(PISCSIIMAGE pImage, PISCSIRES paResponse, unsigned int cnResponse)
487{
488 int rc = VINF_SUCCESS;
489 unsigned int i = 0;
490 size_t cbToRead, cbActuallyRead, residual, cbSegActual = 0, cbAHSLength, cbDataLength;
491 char *pDst;
492
493 LogFlowFunc(("cnResponse=%d (%s:%d)\n", cnResponse, pImage->pszHostname, pImage->uPort));
494 if (pImage->Socket == NIL_RTSOCKET)
495 {
496 /* Attempt to reconnect if the connection was previously broken. */
497 if (pImage->pszHostname != NULL)
498 {
499 rc = pImage->pInterfaceNetCallbacks->pfnClientConnect(pImage->pszHostname, pImage->uPort, &pImage->Socket);
500 if (RT_UNLIKELY( RT_FAILURE(rc)
501 && ( rc == VERR_NET_CONNECTION_REFUSED
502 || rc == VERR_NET_CONNECTION_RESET
503 || rc == VERR_NET_UNREACHABLE
504 || rc == VERR_NET_HOST_UNREACHABLE
505 || rc == VERR_NET_CONNECTION_TIMED_OUT)))
506 {
507 /* Standardize return value for no connection. */
508 rc = VERR_NET_CONNECTION_REFUSED;
509 }
510 }
511 }
512
513 if (RT_SUCCESS(rc) && paResponse[0].cbSeg >= 48)
514 {
515 cbToRead = 0;
516 residual = 48; /* Do not read more than the BHS length before the true PDU length is known. */
517 cbSegActual = residual;
518 pDst = (char *)paResponse[i].pvSeg;
519 uint64_t u64Timeout = RTTimeMilliTS() + pImage->uReadTimeout;
520 do
521 {
522 int64_t cMilliesRemaining = u64Timeout - RTTimeMilliTS();
523 if (cMilliesRemaining <= 0)
524 {
525 rc = VERR_TIMEOUT;
526 break;
527 }
528 Assert(cMilliesRemaining < 1000000);
529 rc = pImage->pInterfaceNetCallbacks->pfnSelectOne(pImage->Socket,
530 cMilliesRemaining);
531 if (RT_FAILURE(rc))
532 break;
533 rc = pImage->pInterfaceNetCallbacks->pfnRead(pImage->Socket,
534 pDst, residual,
535 &cbActuallyRead);
536 if (RT_FAILURE(rc))
537 break;
538 if (cbActuallyRead == 0)
539 {
540 /* The other end has closed the connection. */
541 pImage->pInterfaceNetCallbacks->pfnClientClose(pImage->Socket);
542 pImage->Socket = NIL_RTSOCKET;
543 rc = VERR_NET_CONNECTION_RESET;
544 break;
545 }
546 if (cbToRead == 0)
547 {
548 /* Currently reading the BHS. */
549 residual -= cbActuallyRead;
550 pDst += cbActuallyRead;
551 if (residual <= 40)
552 {
553 /* Enough data read to figure out the actual PDU size. */
554 uint32_t word1 = RT_N2H_U32(((uint32_t *)(paResponse[0].pvSeg))[1]);
555 cbAHSLength = (word1 & 0xff000000) >> 24;
556 cbAHSLength = ((cbAHSLength - 1) | 3) + 1; /* Add padding. */
557 cbDataLength = word1 & 0x00ffffff;
558 cbDataLength = ((cbDataLength - 1) | 3) + 1; /* Add padding. */
559 cbToRead = residual + cbAHSLength + cbDataLength;
560 residual += paResponse[0].cbSeg - 48;
561 if (residual > cbToRead)
562 residual = cbToRead;
563 cbSegActual = 48 + cbAHSLength + cbDataLength;
564 /* Check whether we are already done with this PDU (no payload). */
565 if (cbToRead == 0)
566 break;
567 }
568 }
569 else
570 {
571 cbToRead -= cbActuallyRead;
572 if (cbToRead == 0)
573 break;
574 pDst += cbActuallyRead;
575 residual -= cbActuallyRead;
576 }
577 if (residual == 0)
578 {
579 i++;
580 if (i >= cnResponse)
581 {
582 /* No space left in receive buffers. */
583 rc = VERR_BUFFER_OVERFLOW;
584 break;
585 }
586 pDst = (char *)paResponse[i].pvSeg;
587 residual = paResponse[i].cbSeg;
588 if (residual > cbToRead)
589 residual = cbToRead;
590 cbSegActual = residual;
591 }
592 } while (true);
593 }
594 else
595 {
596 if (RT_SUCCESS(rc))
597 rc = VERR_BUFFER_OVERFLOW;
598 }
599 if (RT_SUCCESS(rc))
600 {
601 paResponse[i].cbSeg = cbSegActual;
602 for (i++; i < cnResponse; i++)
603 paResponse[i].cbSeg = 0;
604 }
605
606 if (RT_UNLIKELY( RT_FAILURE(rc)
607 && ( rc == VERR_NET_CONNECTION_RESET
608 || rc == VERR_NET_CONNECTION_ABORTED
609 || rc == VERR_NET_CONNECTION_RESET_BY_PEER
610 || rc == VERR_NET_CONNECTION_REFUSED
611 || rc == VERR_BROKEN_PIPE)))
612 {
613 /* Standardize return value for broken connection. */
614 rc = VERR_BROKEN_PIPE;
615 }
616
617 LogFlowFunc(("returns %Rrc\n", rc));
618 return rc;
619}
620
621
622static int iscsiTransportWrite(PISCSIIMAGE pImage, PISCSIREQ paRequest, unsigned int cnRequest)
623{
624 int rc = VINF_SUCCESS;
625 uint32_t pad = 0;
626 unsigned int i;
627
628 LogFlow(("drvISCSITransportTcpWrite: cnRequest=%d (%s:%d)\n", cnRequest, pImage->pszHostname, pImage->uPort));
629 if (pImage->Socket == NIL_RTSOCKET)
630 {
631 /* Attempt to reconnect if the connection was previously broken. */
632 if (pImage->pszHostname != NULL)
633 {
634 rc = pImage->pInterfaceNetCallbacks->pfnClientConnect(pImage->pszHostname, pImage->uPort, &pImage->Socket);
635 if (RT_UNLIKELY( RT_FAILURE(rc)
636 && ( rc == VERR_NET_CONNECTION_REFUSED
637 || rc == VERR_NET_CONNECTION_RESET
638 || rc == VERR_NET_UNREACHABLE
639 || rc == VERR_NET_HOST_UNREACHABLE
640 || rc == VERR_NET_CONNECTION_TIMED_OUT)))
641 {
642 /* Standardize return value for no connection. */
643 rc = VERR_NET_CONNECTION_REFUSED;
644 }
645 }
646 }
647
648 if (RT_SUCCESS(rc))
649 {
650 for (i = 0; i < cnRequest; i++)
651 {
652 /* Write one chunk of data. */
653 rc = pImage->pInterfaceNetCallbacks->pfnWrite(pImage->Socket,
654 paRequest[i].pcvSeg,
655 paRequest[i].cbSeg);
656 if (RT_FAILURE(rc))
657 break;
658 /* Insert proper padding before the next chunk us written. */
659 if (paRequest[i].cbSeg & 3)
660 {
661 rc = pImage->pInterfaceNetCallbacks->pfnWrite(pImage->Socket,
662 &pad,
663 4 - (paRequest[i].cbSeg & 3));
664 if (RT_FAILURE(rc))
665 break;
666 }
667 }
668 /* Send out the request as soon as possible, otherwise the target will
669 * answer after an unnecessary delay. */
670 pImage->pInterfaceNetCallbacks->pfnFlush(pImage->Socket);
671 }
672
673 if (RT_UNLIKELY( RT_FAILURE(rc)
674 && ( rc == VERR_NET_CONNECTION_RESET
675 || rc == VERR_NET_CONNECTION_ABORTED
676 || rc == VERR_NET_CONNECTION_RESET_BY_PEER
677 || rc == VERR_NET_CONNECTION_REFUSED
678 || rc == VERR_BROKEN_PIPE)))
679 {
680 /* Standardize return value for broken connection. */
681 rc = VERR_BROKEN_PIPE;
682 }
683
684 LogFlow(("drvISCSITransportTcpWrite: returns %Rrc\n", rc));
685 return rc;
686}
687
688
689static int iscsiTransportOpen(PISCSIIMAGE pImage)
690{
691 int rc = VINF_SUCCESS;
692 size_t cbHostname = 0; /* shut up gcc */
693 const char *pcszPort = NULL; /* shut up gcc */
694 char *pszPortEnd;
695 uint16_t uPort;
696
697 /* Clean up previous connection data. */
698 if (pImage->Socket != NIL_RTSOCKET)
699 {
700 pImage->pInterfaceNetCallbacks->pfnClientClose(pImage->Socket);
701 pImage->Socket = NIL_RTSOCKET;
702 }
703 if (pImage->pszHostname)
704 {
705 RTMemFree(pImage->pszHostname);
706 pImage->pszHostname = NULL;
707 pImage->uPort = 0;
708 }
709
710 /* Locate the port number via the colon separating the hostname from the port. */
711 if (*pImage->pszTargetAddress)
712 {
713 if (*pImage->pszTargetAddress != '[')
714 {
715 /* Normal hostname or IPv4 dotted decimal. */
716 pcszPort = strchr(pImage->pszTargetAddress, ':');
717 if (pcszPort != NULL)
718 {
719 cbHostname = pcszPort - pImage->pszTargetAddress;
720 pcszPort++;
721 }
722 else
723 cbHostname = strlen(pImage->pszTargetAddress);
724 }
725 else
726 {
727 /* IPv6 literal address. Contains colons, so skip to closing square bracket. */
728 pcszPort = strchr(pImage->pszTargetAddress, ']');
729 if (pcszPort != NULL)
730 {
731 pcszPort++;
732 cbHostname = pcszPort - pImage->pszTargetAddress;
733 if (*pcszPort == '\0')
734 pcszPort = NULL;
735 else if (*pcszPort != ':')
736 rc = VERR_PARSE_ERROR;
737 else
738 pcszPort++;
739 }
740 else
741 rc = VERR_PARSE_ERROR;
742 }
743 }
744 else
745 rc = VERR_PARSE_ERROR;
746
747 /* Now split address into hostname and port. */
748 if (RT_SUCCESS(rc))
749 {
750 pImage->pszHostname = (char *)RTMemAlloc(cbHostname + 1);
751 if (!pImage->pszHostname)
752 rc = VERR_NO_MEMORY;
753 else
754 {
755 memcpy(pImage->pszHostname, pImage->pszTargetAddress, cbHostname);
756 pImage->pszHostname[cbHostname] = '\0';
757 if (pcszPort != NULL)
758 {
759 rc = RTStrToUInt16Ex(pcszPort, &pszPortEnd, 0, &uPort);
760 /* Note that RT_SUCCESS() macro to check the rc value is not strict enough in this case. */
761 if (rc == VINF_SUCCESS && *pszPortEnd == '\0' && uPort != 0)
762 {
763 pImage->uPort = uPort;
764 }
765 else
766 {
767 rc = VERR_PARSE_ERROR;
768 }
769 }
770 else
771 pImage->uPort = ISCSI_DEFAULT_PORT;
772 }
773 }
774
775 if (RT_FAILURE(rc))
776 {
777 if (pImage->pszHostname)
778 {
779 RTMemFree(pImage->pszHostname);
780 pImage->pszHostname = NULL;
781 }
782 pImage->uPort = 0;
783 }
784
785 /* Note that in this implementation the actual connection establishment is
786 * delayed until a PDU is read or written. */
787 LogFlowFunc(("returns %Rrc\n", rc));
788 return rc;
789}
790
791
792static int iscsiTransportClose(PISCSIIMAGE pImage)
793{
794 int rc;
795
796 LogFlowFunc(("(%s:%d)\n", pImage->pszHostname, pImage->uPort));
797 if (pImage->Socket != NIL_RTSOCKET)
798 {
799 rc = pImage->pInterfaceNetCallbacks->pfnClientClose(pImage->Socket);
800 pImage->Socket = NIL_RTSOCKET;
801 }
802 else
803 rc = VINF_SUCCESS;
804 LogFlowFunc(("returns %Rrc\n", rc));
805 return rc;
806}
807
808
809/**
810 * Attach to an iSCSI target. Performs all operations necessary to enter
811 * Full Feature Phase.
812 *
813 * @returns VBox status.
814 * @param pImage The iSCSI connection state to be used.
815 */
816static int iscsiAttach(PISCSIIMAGE pImage)
817{
818 int rc;
819 uint32_t itt;
820 uint32_t csg, nsg, substate;
821 uint64_t isid_tsih;
822 uint8_t bBuf[4096]; /* Should be large enough even for large authentication values. */
823 size_t cbBuf;
824 bool transit;
825 uint8_t pbChallenge[1024]; /* RFC3720 specifies this as maximum. */
826 size_t cbChallenge = 0; /* shut up gcc */
827 uint8_t bChapIdx;
828 uint8_t aResponse[RTMD5HASHSIZE];
829 uint32_t cnISCSIReq;
830 ISCSIREQ aISCSIReq[4];
831 uint32_t aReqBHS[12];
832 uint32_t cnISCSIRes;
833 ISCSIRES aISCSIRes[2];
834 uint32_t aResBHS[12];
835 char *pszNext;
836
837 bool fParameterNeg = true;;
838 pImage->cbRecvDataLength = ISCSI_DATA_LENGTH_MAX;
839 pImage->cbSendDataLength = ISCSI_DATA_LENGTH_MAX;
840 char szMaxRecvDataSegmentLength[16];
841 RTStrPrintf(szMaxRecvDataSegmentLength, sizeof(szMaxRecvDataSegmentLength), "%u", ISCSI_DATA_LENGTH_MAX);
842 char szMaxBurstLength[16];
843 RTStrPrintf(szMaxBurstLength, sizeof(szMaxBurstLength), "%u", ISCSI_DATA_LENGTH_MAX);
844 char szFirstBurstLength[16];
845 RTStrPrintf(szFirstBurstLength, sizeof(szFirstBurstLength), "%u", ISCSI_DATA_LENGTH_MAX);
846 ISCSIPARAMETER aParameterNeg[] =
847 {
848 { "HeaderDigest", "None", 0 },
849 { "DataDigest", "None", 0 },
850 { "MaxConnections", "1", 0 },
851 { "InitialR2T", "No", 0 },
852 { "ImmediateData", "Yes", 0 },
853 { "MaxRecvDataSegmentLength", szMaxRecvDataSegmentLength, 0 },
854 { "MaxBurstLength", szMaxBurstLength, 0 },
855 { "FirstBurstLength", szFirstBurstLength, 0 },
856 { "DefaultTime2Wait", "0", 0 },
857 { "DefaultTime2Retain", "60", 0 },
858 { "DataPDUInOrder", "Yes", 0 },
859 { "DataSequenceInOrder", "Yes", 0 },
860 { "ErrorRecoveryLevel", "0", 0 },
861 { "MaxOutstandingR2T", "1", 0 }
862 };
863
864 LogFlowFunc(("entering\n"));
865
866 Assert(pImage->state == ISCSISTATE_FREE);
867
868 RTSemMutexRequest(pImage->Mutex, RT_INDEFINITE_WAIT);
869
870 /* Make 100% sure the connection isn't reused for a new login. */
871 iscsiTransportClose(pImage);
872
873restart:
874 pImage->state = ISCSISTATE_IN_LOGIN;
875 pImage->ITT = 1;
876 pImage->FirstRecvPDU = true;
877 pImage->CmdSN = 1;
878 pImage->ExpCmdSN = 0;
879 pImage->MaxCmdSN = 1;
880 pImage->ExpStatSN = 1;
881
882 /*
883 * Send login request to target.
884 */
885 itt = iscsiNewITT(pImage);
886 csg = 0;
887 nsg = 0;
888 substate = 0;
889 isid_tsih = pImage->ISID << 16; /* TSIH field currently always 0 */
890
891 do {
892 transit = false;
893 cbBuf = 0;
894 /* Handle all cases with a single switch statement. */
895 switch (csg << 8 | substate)
896 {
897 case 0x0000: /* security negotiation, step 0: propose authentication. */
898 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "SessionType", "Normal", 0);
899 if (RT_FAILURE(rc))
900 goto out;
901 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "InitiatorName", pImage->pszInitiatorName, 0);
902 if (RT_FAILURE(rc))
903 goto out;
904 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "TargetName", pImage->pszTargetName, 0);
905 if (RT_FAILURE(rc))
906 goto out;
907 if (pImage->pszInitiatorUsername == NULL)
908 {
909 /* No authentication. Immediately switch to next phase. */
910 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "AuthMethod", "None", 0);
911 if (RT_FAILURE(rc))
912 goto out;
913 nsg = 1;
914 transit = true;
915 }
916 else
917 {
918 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "AuthMethod", "CHAP,None", 0);
919 if (RT_FAILURE(rc))
920 goto out;
921 }
922 break;
923 case 0x0001: /* security negotiation, step 1: propose CHAP_MD5 variant. */
924 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "CHAP_A", "5", 0);
925 if (RT_FAILURE(rc))
926 goto out;
927 break;
928 case 0x0002: /* security negotiation, step 2: send authentication info. */
929 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "CHAP_N", pImage->pszInitiatorUsername, 0);
930 if (RT_FAILURE(rc))
931 goto out;
932 chap_md5_compute_response(aResponse, bChapIdx, pbChallenge, cbChallenge,
933 pImage->pbInitiatorSecret, pImage->cbInitiatorSecret);
934 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "CHAP_R", (const char *)aResponse, RTMD5HASHSIZE);
935 if (RT_FAILURE(rc))
936 goto out;
937 nsg = 1;
938 transit = true;
939 break;
940 case 0x0100: /* login operational negotiation, step 0: set parameters. */
941 if (fParameterNeg)
942 {
943 for (unsigned i = 0; i < RT_ELEMENTS(aParameterNeg); i++)
944 {
945 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf,
946 aParameterNeg[i].pszParamName,
947 aParameterNeg[i].pszParamValue,
948 aParameterNeg[i].cbParamValue);
949 if (RT_FAILURE(rc))
950 goto out;
951 }
952 fParameterNeg = false;
953 }
954
955 nsg = 3;
956 transit = true;
957 break;
958 case 0x0300: /* full feature phase. */
959 default:
960 /* Should never come here. */
961 AssertMsgFailed(("send: Undefined login state %d substate %d\n", csg, substate));
962 break;
963 }
964
965 aReqBHS[0] = RT_H2N_U32( ISCSI_IMMEDIATE_DELIVERY_BIT
966 | (csg << ISCSI_CSG_SHIFT)
967 | (transit ? (nsg << ISCSI_NSG_SHIFT | ISCSI_TRANSIT_BIT) : 0)
968 | ISCSI_MY_VERSION /* Minimum version. */
969 | (ISCSI_MY_VERSION << 8) /* Maximum version. */
970 | ISCSIOP_LOGIN_REQ); /* C=0 */
971 aReqBHS[1] = RT_H2N_U32((uint32_t)cbBuf); /* TotalAHSLength=0 */
972 aReqBHS[2] = RT_H2N_U32(isid_tsih >> 32);
973 aReqBHS[3] = RT_H2N_U32(isid_tsih & 0xffffffff);
974 aReqBHS[4] = itt;
975 aReqBHS[5] = RT_H2N_U32(1 << 16); /* CID=1,reserved */
976 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
977 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
978 aReqBHS[8] = 0; /* reserved */
979 aReqBHS[9] = 0; /* reserved */
980 aReqBHS[10] = 0; /* reserved */
981 aReqBHS[11] = 0; /* reserved */
982
983 cnISCSIReq = 0;
984 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
985 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
986 cnISCSIReq++;
987
988 aISCSIReq[cnISCSIReq].pcvSeg = bBuf;
989 aISCSIReq[cnISCSIReq].cbSeg = cbBuf;
990 cnISCSIReq++;
991
992 rc = iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq);
993 if (RT_SUCCESS(rc))
994 {
995 ISCSIOPCODE cmd;
996 ISCSILOGINSTATUSCLASS loginStatusClass;
997
998 cnISCSIRes = 0;
999 aISCSIRes[cnISCSIRes].pvSeg = aResBHS;
1000 aISCSIRes[cnISCSIRes].cbSeg = sizeof(aResBHS);
1001 cnISCSIRes++;
1002 aISCSIRes[cnISCSIRes].pvSeg = bBuf;
1003 aISCSIRes[cnISCSIRes].cbSeg = sizeof(bBuf);
1004 cnISCSIRes++;
1005
1006 rc = iscsiRecvPDU(pImage, itt, aISCSIRes, cnISCSIRes);
1007 if (RT_FAILURE(rc))
1008 break;
1009 /** @todo collect partial login responses with Continue bit set. */
1010 Assert(aISCSIRes[0].pvSeg == aResBHS);
1011 Assert(aISCSIRes[0].cbSeg >= ISCSI_BHS_SIZE);
1012 Assert((RT_N2H_U32(aResBHS[0]) & ISCSI_CONTINUE_BIT) == 0);
1013
1014 cmd = (ISCSIOPCODE)(RT_N2H_U32(aResBHS[0]) & ISCSIOP_MASK);
1015 if (cmd == ISCSIOP_LOGIN_RES)
1016 {
1017 if ((RT_N2H_U32(aResBHS[0]) & 0xff) != ISCSI_MY_VERSION)
1018 {
1019 iscsiTransportClose(pImage);
1020 rc = VERR_PARSE_ERROR;
1021 break; /* Give up immediately, as a RFC violation in version fields is very serious. */
1022 }
1023
1024 loginStatusClass = (ISCSILOGINSTATUSCLASS)(RT_N2H_U32(aResBHS[9]) >> 24);
1025 switch (loginStatusClass)
1026 {
1027 case ISCSI_LOGIN_STATUS_CLASS_SUCCESS:
1028 uint32_t targetCSG;
1029 uint32_t targetNSG;
1030 bool targetTransit;
1031
1032 if (pImage->FirstRecvPDU)
1033 {
1034 pImage->FirstRecvPDU = false;
1035 pImage->ExpStatSN = RT_N2H_U32(aResBHS[6]) + 1;
1036 }
1037
1038 targetCSG = (RT_N2H_U32(aResBHS[0]) & ISCSI_CSG_MASK) >> ISCSI_CSG_SHIFT;
1039 targetNSG = (RT_N2H_U32(aResBHS[0]) & ISCSI_NSG_MASK) >> ISCSI_NSG_SHIFT;
1040 targetTransit = !!(RT_N2H_U32(aResBHS[0]) & ISCSI_TRANSIT_BIT);
1041
1042 /* Handle all cases with a single switch statement. */
1043 switch (csg << 8 | substate)
1044 {
1045 case 0x0000: /* security negotiation, step 0: receive final authentication. */
1046 const char *pcszAuthMethod;
1047
1048 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "AuthMethod", &pcszAuthMethod);
1049 if (RT_FAILURE(rc))
1050 {
1051 rc = VERR_PARSE_ERROR;
1052 break;
1053 }
1054 if (strcmp(pcszAuthMethod, "None") == 0)
1055 {
1056 /* Authentication offered, but none required. Skip to operational parameters. */
1057 csg = 1;
1058 nsg = 1;
1059 transit = true;
1060 substate = 0;
1061 break;
1062 }
1063 else if (strcmp(pcszAuthMethod, "CHAP") == 0 && targetNSG == 0 && !targetTransit)
1064 {
1065 /* CHAP authentication required, continue with next substate. */
1066 substate++;
1067 break;
1068 }
1069
1070 /* Unknown auth method or login response PDU headers incorrect. */
1071 rc = VERR_PARSE_ERROR;
1072 break;
1073 case 0x0001: /* security negotiation, step 1: receive final CHAP variant and challenge. */
1074 const char *pcszChapAuthMethod;
1075 const char *pcszChapIdxTarget;
1076 const char *pcszChapChallengeStr;
1077
1078 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "CHAP_A", &pcszChapAuthMethod);
1079 if (RT_FAILURE(rc))
1080 {
1081 rc = VERR_PARSE_ERROR;
1082 break;
1083 }
1084 if (strcmp(pcszChapAuthMethod, "5") != 0)
1085 {
1086 rc = VERR_PARSE_ERROR;
1087 break;
1088 }
1089 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "CHAP_I", &pcszChapIdxTarget);
1090 if (RT_FAILURE(rc))
1091 {
1092 rc = VERR_PARSE_ERROR;
1093 break;
1094 }
1095 rc = RTStrToUInt8Ex(pcszChapIdxTarget, &pszNext, 0, &bChapIdx);
1096 if ((rc > VINF_SUCCESS) || *pszNext != '\0')
1097 {
1098 rc = VERR_PARSE_ERROR;
1099 break;
1100 }
1101 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "CHAP_C", &pcszChapChallengeStr);
1102 if (RT_FAILURE(rc))
1103 {
1104 rc = VERR_PARSE_ERROR;
1105 break;
1106 }
1107 cbChallenge = sizeof(pbChallenge);
1108 rc = iscsiStrToBinary(pcszChapChallengeStr, pbChallenge, &cbChallenge);
1109 if (RT_FAILURE(rc))
1110 break;
1111 substate++;
1112 transit = true;
1113 break;
1114 case 0x0002: /* security negotiation, step 2: check authentication success. */
1115 if (targetCSG == 0 && targetNSG == 1 && targetTransit)
1116 {
1117 /* Target wants to continue in login operational state, authentication success. */
1118 csg = 1;
1119 nsg = 3;
1120 substate = 0;
1121 break;
1122 }
1123 rc = VERR_PARSE_ERROR;
1124 break;
1125 case 0x0100: /* login operational negotiation, step 0: check results. */
1126 {
1127 const char *pcszMaxRecvDataSegmentLength = NULL;
1128 const char *pcszMaxBurstLength = NULL;
1129 const char *pcszFirstBurstLength = NULL;
1130 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "MaxRecvDataSegmentLength", &pcszMaxRecvDataSegmentLength);
1131 if (rc == VERR_INVALID_NAME)
1132 rc = VINF_SUCCESS;
1133 if (RT_FAILURE(rc))
1134 {
1135 rc = VERR_PARSE_ERROR;
1136 break;
1137 }
1138 if (pcszMaxRecvDataSegmentLength && strlen(pcszMaxRecvDataSegmentLength) >= sizeof(szMaxRecvDataSegmentLength))
1139 {
1140 rc = VERR_BUFFER_OVERFLOW;
1141 break;
1142 }
1143 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "MaxBurstLength", &pcszMaxBurstLength);
1144 if (rc == VERR_INVALID_NAME)
1145 rc = VINF_SUCCESS;
1146 if (RT_FAILURE(rc))
1147 {
1148 rc = VERR_PARSE_ERROR;
1149 break;
1150 }
1151 if (pcszMaxBurstLength && strlen(pcszMaxBurstLength) >= sizeof(szMaxBurstLength))
1152 {
1153 rc = VERR_BUFFER_OVERFLOW;
1154 break;
1155 }
1156 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "FirstBurstLength", &pcszFirstBurstLength);
1157 if (rc == VERR_INVALID_NAME)
1158 rc = VINF_SUCCESS;
1159 if (RT_FAILURE(rc))
1160 {
1161 rc = VERR_PARSE_ERROR;
1162 break;
1163 }
1164 if (pcszFirstBurstLength && strlen(pcszFirstBurstLength) >= sizeof(szFirstBurstLength))
1165 {
1166 rc = VERR_BUFFER_OVERFLOW;
1167 break;
1168 }
1169 if (pcszMaxRecvDataSegmentLength)
1170 {
1171 uint32_t cb = pImage->cbSendDataLength;
1172 rc = RTStrToUInt32Full(pcszMaxRecvDataSegmentLength, 0, &cb);
1173 AssertRC(rc);
1174 pImage->cbSendDataLength = RT_MIN(pImage->cbSendDataLength, cb);
1175 }
1176 if (pcszMaxBurstLength)
1177 {
1178 uint32_t cb = pImage->cbSendDataLength;
1179 rc = RTStrToUInt32Full(pcszMaxBurstLength, 0, &cb);
1180 AssertRC(rc);
1181 pImage->cbSendDataLength = RT_MIN(pImage->cbSendDataLength, cb);
1182 }
1183 if (pcszFirstBurstLength)
1184 {
1185 uint32_t cb = pImage->cbSendDataLength;
1186 rc = RTStrToUInt32Full(pcszFirstBurstLength, 0, &cb);
1187 AssertRC(rc);
1188 pImage->cbSendDataLength = RT_MIN(pImage->cbSendDataLength, cb);
1189 }
1190
1191 if (targetCSG == 1 && targetNSG == 3 && targetTransit)
1192 {
1193 /* Target wants to continue in full feature phase, login finished. */
1194 csg = 3;
1195 nsg = 3;
1196 substate = 0;
1197 break;
1198 }
1199 else if (targetCSG == 1 && targetNSG == 1 && !targetTransit)
1200 {
1201 /* Target wants to negotiate certain parameters and
1202 * stay in login operational negotiation. */
1203 csg = 1;
1204 nsg = 3;
1205 substate = 0;
1206 }
1207 rc = VERR_PARSE_ERROR;
1208 break;
1209 }
1210 case 0x0300: /* full feature phase. */
1211 default:
1212 AssertMsgFailed(("recv: Undefined login state %d substate %d\n", csg, substate));
1213 rc = VERR_PARSE_ERROR;
1214 break;
1215 }
1216 break;
1217 case ISCSI_LOGIN_STATUS_CLASS_REDIRECTION:
1218 const char *pcszTargetRedir;
1219
1220 /* Target has moved to some other location, as indicated in the TargetAddress key. */
1221 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "TargetAddress", &pcszTargetRedir);
1222 if (RT_FAILURE(rc))
1223 {
1224 rc = VERR_PARSE_ERROR;
1225 break;
1226 }
1227 if (pImage->pszTargetAddress)
1228 RTMemFree(pImage->pszTargetAddress);
1229 {
1230 size_t cb = strlen(pcszTargetRedir) + 1;
1231 pImage->pszTargetAddress = (char *)RTMemAlloc(cb);
1232 if (!pImage->pszTargetAddress)
1233 {
1234 rc = VERR_NO_MEMORY;
1235 break;
1236 }
1237 memcpy(pImage->pszTargetAddress, pcszTargetRedir, cb);
1238 }
1239 rc = iscsiTransportOpen(pImage);
1240 goto restart;
1241 case ISCSI_LOGIN_STATUS_CLASS_INITIATOR_ERROR:
1242 iscsiTransportClose(pImage);
1243 rc = VERR_IO_GEN_FAILURE;
1244 goto out;
1245 case ISCSI_LOGIN_STATUS_CLASS_TARGET_ERROR:
1246 iscsiTransportClose(pImage);
1247 rc = VINF_EOF;
1248 break;
1249 default:
1250 rc = VERR_PARSE_ERROR;
1251 }
1252
1253 if (csg == 3)
1254 {
1255 /*
1256 * Finished login, continuing with Full Feature Phase.
1257 */
1258 rc = VINF_SUCCESS;
1259 break;
1260 }
1261 }
1262 else
1263 {
1264 AssertMsgFailed(("%s: ignoring unexpected PDU with first word = %#08x\n", __FUNCTION__, RT_N2H_U32(aResBHS[0])));
1265 }
1266 }
1267 else
1268 break;
1269 } while (true);
1270
1271out:
1272 if (RT_FAILURE(rc))
1273 {
1274 /*
1275 * Close connection to target.
1276 */
1277 iscsiTransportClose(pImage);
1278 pImage->state = ISCSISTATE_FREE;
1279 }
1280 else
1281 pImage->state = ISCSISTATE_NORMAL;
1282
1283 RTSemMutexRelease(pImage->Mutex);
1284
1285 LogFlowFunc(("returning %Rrc\n", rc));
1286 LogRel(("iSCSI: login to target %s %s\n", pImage->pszTargetName, RT_SUCCESS(rc) ? "successful" : "failed"));
1287 return rc;
1288}
1289
1290
1291/**
1292 * Detach from an iSCSI target.
1293 *
1294 * @returns VBox status.
1295 * @param pImage The iSCSI connection state to be used.
1296 */
1297static int iscsiDetach(PISCSIIMAGE pImage)
1298{
1299 int rc;
1300 uint32_t itt;
1301 uint32_t cnISCSIReq = 0;
1302 ISCSIREQ aISCSIReq[4];
1303 uint32_t aReqBHS[12];
1304 LogFlow(("drvISCSIDetach: entering\n"));
1305
1306 RTSemMutexRequest(pImage->Mutex, RT_INDEFINITE_WAIT);
1307
1308 if (pImage->state != ISCSISTATE_FREE && pImage->state != ISCSISTATE_IN_LOGOUT)
1309 {
1310 pImage->state = ISCSISTATE_IN_LOGOUT;
1311
1312 /*
1313 * Send logout request to target.
1314 */
1315 itt = iscsiNewITT(pImage);
1316 aReqBHS[0] = RT_H2N_U32(ISCSI_FINAL_BIT | ISCSIOP_LOGOUT_REQ); /* I=0,F=1,Reason=close session */
1317 aReqBHS[1] = RT_H2N_U32(0); /* TotalAHSLength=0,DataSementLength=0 */
1318 aReqBHS[2] = 0; /* reserved */
1319 aReqBHS[3] = 0; /* reserved */
1320 aReqBHS[4] = itt;
1321 aReqBHS[5] = 0; /* reserved */
1322 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
1323 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
1324 aReqBHS[8] = 0; /* reserved */
1325 aReqBHS[9] = 0; /* reserved */
1326 aReqBHS[10] = 0; /* reserved */
1327 aReqBHS[11] = 0; /* reserved */
1328 pImage->CmdSN++;
1329
1330 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
1331 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
1332 cnISCSIReq++;
1333
1334 rc = iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq);
1335 if (RT_SUCCESS(rc))
1336 {
1337 /*
1338 * Read logout response from target.
1339 */
1340 ISCSIRES aISCSIRes;
1341 uint32_t aResBHS[12];
1342
1343 aISCSIRes.pvSeg = aResBHS;
1344 aISCSIRes.cbSeg = sizeof(aResBHS);
1345 rc = iscsiRecvPDU(pImage, itt, &aISCSIRes, 1);
1346 if (RT_SUCCESS(rc))
1347 {
1348 if (RT_N2H_U32(aResBHS[0]) != (ISCSI_FINAL_BIT | ISCSIOP_LOGOUT_RES))
1349 AssertMsgFailed(("iSCSI Logout response invalid\n"));
1350 }
1351 else
1352 AssertMsgFailed(("iSCSI Logout response error, rc=%Rrc\n", rc));
1353 }
1354 else
1355 AssertMsgFailed(("Could not send iSCSI Logout request, rc=%Rrc\n", rc));
1356 }
1357
1358 if (pImage->state != ISCSISTATE_FREE)
1359 {
1360 /*
1361 * Close connection to target.
1362 */
1363 rc = iscsiTransportClose(pImage);
1364 if (RT_FAILURE(rc))
1365 AssertMsgFailed(("Could not close connection to target, rc=%Rrc\n", rc));
1366 }
1367
1368 pImage->state = ISCSISTATE_FREE;
1369
1370 RTSemMutexRelease(pImage->Mutex);
1371
1372 LogFlow(("drvISCSIDetach: leaving\n"));
1373 LogRel(("iSCSI: logout to target %s\n", pImage->pszTargetName));
1374 return VINF_SUCCESS;
1375}
1376
1377
1378/**
1379 * Perform a command on an iSCSI target. Target must be already in
1380 * Full Feature Phase.
1381 *
1382 * @returns VBOX status.
1383 * @param pImage The iSCSI connection state to be used.
1384 * @param pRequest Command descriptor. Contains all information about
1385 * the command, its transfer directions and pointers
1386 * to the buffer(s) used for transferring data and
1387 * status information.
1388 */
1389static int iscsiCommand(PISCSIIMAGE pImage, PSCSIREQ pRequest)
1390{
1391 int rc;
1392 uint32_t itt;
1393 uint32_t cbData;
1394 uint32_t cnISCSIReq = 0;
1395 ISCSIREQ aISCSIReq[4];
1396 uint32_t aReqBHS[12];
1397
1398 uint32_t *pDst = NULL;
1399 size_t cbBufLength;
1400 uint32_t aStat[64];
1401 uint32_t ExpDataSN = 0;
1402 bool final = false;
1403
1404 LogFlow(("iscsiCommand: entering, CmdSN=%d\n", pImage->CmdSN));
1405
1406 Assert(pRequest->enmXfer != SCSIXFER_TO_FROM_TARGET); /**< @todo not yet supported, would require AHS. */
1407 Assert(pRequest->cbI2TData <= 0xffffff); /* larger transfers would require R2T support. */
1408 Assert(pRequest->cbCmd <= 16); /* would cause buffer overrun below. */
1409
1410 /* If not in normal state, then the transport connection was dropped. Try
1411 * to reestablish by logging in, the target might be responsive again. */
1412 if (pImage->state == ISCSISTATE_FREE)
1413 rc = iscsiAttach(pImage);
1414
1415 /* If still not in normal state, then the underlying transport connection
1416 * cannot be established. Get out before bad things happen (and make
1417 * sure the caller suspends the VM again). */
1418 if (pImage->state != ISCSISTATE_NORMAL)
1419 {
1420 rc = VERR_NET_CONNECTION_REFUSED;
1421 goto out;
1422 }
1423
1424 /*
1425 * Send SCSI command to target with all I2T data included.
1426 */
1427 cbData = 0;
1428 if (pRequest->enmXfer == SCSIXFER_FROM_TARGET)
1429 cbData = (uint32_t)pRequest->cbT2IData;
1430 else
1431 cbData = (uint32_t)pRequest->cbI2TData;
1432
1433 RTSemMutexRequest(pImage->Mutex, RT_INDEFINITE_WAIT);
1434
1435 itt = iscsiNewITT(pImage);
1436 memset(aReqBHS, 0, sizeof(aReqBHS));
1437 aReqBHS[0] = RT_H2N_U32( ISCSI_FINAL_BIT | ISCSI_TASK_ATTR_ORDERED | ISCSIOP_SCSI_CMD
1438 | (pRequest->enmXfer << 21)); /* I=0,F=1,Attr=Ordered */
1439 aReqBHS[1] = RT_H2N_U32(0x00000000 | ((uint32_t)pRequest->cbI2TData & 0xffffff)); /* TotalAHSLength=0 */
1440 aReqBHS[2] = RT_H2N_U32(pImage->LUN >> 32);
1441 aReqBHS[3] = RT_H2N_U32(pImage->LUN & 0xffffffff);
1442 aReqBHS[4] = itt;
1443 aReqBHS[5] = RT_H2N_U32(cbData);
1444 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
1445 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
1446 memcpy(aReqBHS + 8, pRequest->pvCmd, pRequest->cbCmd);
1447 pImage->CmdSN++;
1448
1449 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
1450 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
1451 cnISCSIReq++;
1452
1453 if ( pRequest->enmXfer == SCSIXFER_TO_TARGET
1454 || pRequest->enmXfer == SCSIXFER_TO_FROM_TARGET)
1455 {
1456 aISCSIReq[cnISCSIReq].pcvSeg = pRequest->pcvI2TData;
1457 aISCSIReq[cnISCSIReq].cbSeg = pRequest->cbI2TData; /* Padding done by transport. */
1458 cnISCSIReq++;
1459 }
1460
1461 rc = iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq);
1462 if (RT_FAILURE(rc))
1463 goto out_release;
1464
1465 /* Place SCSI request in queue. */
1466 pImage->paCurrReq = aISCSIReq;
1467 pImage->cnCurrReq = cnISCSIReq;
1468
1469 /*
1470 * Read SCSI response/data in PDUs from target.
1471 */
1472 if ( pRequest->enmXfer == SCSIXFER_FROM_TARGET
1473 || pRequest->enmXfer == SCSIXFER_TO_FROM_TARGET)
1474 {
1475 pDst = (uint32_t *)pRequest->pvT2IData;
1476 cbBufLength = pRequest->cbT2IData;
1477 }
1478 else
1479 cbBufLength = 0;
1480
1481 do {
1482 uint32_t cnISCSIRes = 0;
1483 ISCSIRES aISCSIRes[4];
1484 uint32_t aResBHS[12];
1485
1486 aISCSIRes[cnISCSIRes].pvSeg = aResBHS;
1487 aISCSIRes[cnISCSIRes].cbSeg = sizeof(aResBHS);
1488 cnISCSIRes++;
1489 if (cbBufLength != 0 &&
1490 ( pRequest->enmXfer == SCSIXFER_FROM_TARGET
1491 || pRequest->enmXfer == SCSIXFER_TO_FROM_TARGET))
1492 {
1493 aISCSIRes[cnISCSIRes].pvSeg = pDst;
1494 aISCSIRes[cnISCSIRes].cbSeg = cbBufLength;
1495 cnISCSIRes++;
1496 }
1497 /* Always reserve space for the status - it's impossible to tell
1498 * beforehand whether this will be the final PDU or not. */
1499 aISCSIRes[cnISCSIRes].pvSeg = aStat;
1500 aISCSIRes[cnISCSIRes].cbSeg = sizeof(aStat);
1501 cnISCSIRes++;
1502
1503 rc = iscsiRecvPDU(pImage, itt, aISCSIRes, cnISCSIRes);
1504 if (RT_FAILURE(rc))
1505 break;
1506
1507 final = !!(RT_N2H_U32(aResBHS[0]) & ISCSI_FINAL_BIT);
1508 ISCSIOPCODE cmd = (ISCSIOPCODE)(RT_N2H_U32(aResBHS[0]) & ISCSIOP_MASK);
1509 if (cmd == ISCSIOP_SCSI_RES)
1510 {
1511 /* This is the final PDU which delivers the status (and may be omitted if
1512 * the last Data-In PDU included successful completion status). Note
1513 * that ExpStatSN has been bumped already in iscsiRecvPDU. */
1514 if (!final || ((RT_N2H_U32(aResBHS[0]) & 0x0000ff00) != 0) || (RT_N2H_U32(aResBHS[6]) != pImage->ExpStatSN - 1))
1515 {
1516 /* SCSI Response in the wrong place or with a (target) failure. */
1517 rc = VERR_PARSE_ERROR;
1518 break;
1519 }
1520 /* The following is a bit tricky, as in error situations we may
1521 * get the status only instead of the result data plus optional
1522 * status. Thus the status may have ended up partially in the
1523 * data area. */
1524 pRequest->status = RT_N2H_U32(aResBHS[0]) & 0x000000ff;
1525 uint32_t cbData = RT_N2H_U32(aResBHS[1]) & 0x00ffffff;
1526 if (cbData >= 2)
1527 {
1528 uint32_t cbStat = RT_N2H_U32(((uint32_t *)aISCSIRes[1].pvSeg)[0]) >> 16;
1529 if (cbStat + 2 > cbData || cbStat > pRequest->cbSense)
1530 {
1531 rc = VERR_BUFFER_OVERFLOW;
1532 break;
1533 }
1534 pRequest->cbSense = cbStat;
1535 memcpy(pRequest->pvSense, ((const uint8_t *)aISCSIRes[1].pvSeg) + 2, aISCSIRes[1].cbSeg - 2);
1536 if (cnISCSIRes > 2 && aISCSIRes[2].cbSeg && (ssize_t)cbStat - aISCSIRes[1].cbSeg - 2 > 0)
1537 memcpy((char *)pRequest->pvSense + aISCSIRes[1].cbSeg, aISCSIRes[2].pvSeg, cbStat - aISCSIRes[1].cbSeg - 2);
1538 }
1539 else if (cbData == 1)
1540 {
1541 rc = VERR_PARSE_ERROR;
1542 break;
1543 }
1544 else
1545 pRequest->cbSense = 0;
1546 break;
1547 }
1548 else if (cmd == ISCSIOP_SCSI_DATA_IN)
1549 {
1550 /* A Data-In PDU carries some data that needs to be added to the received
1551 * data in response to the command. There may be both partial and complete
1552 * Data-In PDUs, so collect data until the status is included or the status
1553 * is sent in a separate SCSI Result frame (see above). */
1554 if (final && aISCSIRes[2].cbSeg != 0)
1555 {
1556 /* The received PDU is partially stored in the buffer for status.
1557 * Must not happen under normal circumstances and is a target error. */
1558 rc = VERR_BUFFER_OVERFLOW;
1559 break;
1560 }
1561 uint32_t len = RT_N2H_U32(aResBHS[1]) & 0x00ffffff;
1562 pDst = (uint32_t *)((char *)pDst + len);
1563 cbBufLength -= len;
1564 ExpDataSN++;
1565 if (final && (RT_N2H_U32(aResBHS[0]) & ISCSI_STATUS_BIT) != 0)
1566 {
1567 pRequest->status = RT_N2H_U32(aResBHS[0]) & 0x000000ff;
1568 pRequest->cbSense = 0;
1569 break;
1570 }
1571 }
1572 else
1573 {
1574 rc = VERR_PARSE_ERROR;
1575 break;
1576 }
1577 } while (true);
1578
1579 /* Remove SCSI request from queue. */
1580 pImage->paCurrReq = NULL;
1581 pImage->cnCurrReq = 0;
1582
1583out_release:
1584 if (rc == VERR_TIMEOUT)
1585 {
1586 /* Drop connection in case the target plays dead. Much better than
1587 * delaying the next requests until the timed out command actually
1588 * finishes. Also keep in mind that command shouldn't take longer than
1589 * about 30-40 seconds, or the guest will lose its patience. */
1590 iscsiTransportClose(pImage);
1591 pImage->state = ISCSISTATE_FREE;
1592 }
1593 RTSemMutexRelease(pImage->Mutex);
1594
1595out:
1596 LogFlow(("iscsiCommand: returns %Rrc\n", rc));
1597 return rc;
1598}
1599
1600
1601/**
1602 * Generate a new Initiator Task Tag.
1603 *
1604 * @returns Initiator Task Tag.
1605 * @param pImage The iSCSI connection state to be used.
1606 */
1607static uint32_t iscsiNewITT(PISCSIIMAGE pImage)
1608{
1609 uint32_t next_itt;
1610
1611 next_itt = pImage->ITT++;
1612 if (pImage->ITT == ISCSI_TASK_TAG_RSVD)
1613 pImage->ITT = 0;
1614 return RT_H2N_U32(next_itt);
1615}
1616
1617
1618/**
1619 * Send an iSCSI request. The request can consist of several segments, which
1620 * are padded to 4 byte boundaries and concatenated.
1621 *
1622 * @returns VBOX status
1623 * @param pImage The iSCSI connection state to be used.
1624 * @param paReq Pointer to array of iSCSI request sections.
1625 * @param cnReq Number of valid iSCSI request sections in the array.
1626 */
1627static int iscsiSendPDU(PISCSIIMAGE pImage, PISCSIREQ paReq, uint32_t cnReq)
1628{
1629 int rc = VINF_SUCCESS;
1630 uint32_t i;
1631 /** @todo return VERR_VD_ISCSI_INVALID_STATE in the appropriate situations,
1632 * needs cleaning up of timeout/disconnect handling a bit, as otherwise
1633 * too many incorrect errors are signalled. */
1634 Assert(pImage->paCurrReq == NULL);
1635 Assert(cnReq >= 1);
1636 Assert(paReq[0].cbSeg >= ISCSI_BHS_SIZE);
1637
1638 for (i = 0; i < pImage->cISCSIRetries; i++)
1639 {
1640 rc = iscsiTransportWrite(pImage, paReq, cnReq);
1641 if (RT_SUCCESS(rc))
1642 break;
1643 if (rc != VERR_BROKEN_PIPE && rc != VERR_NET_CONNECTION_REFUSED)
1644 break;
1645 /* No point in reestablishing the connection for a logout */
1646 if (pImage->state == ISCSISTATE_IN_LOGOUT)
1647 break;
1648 RTThreadSleep(500);
1649 if (pImage->state != ISCSISTATE_IN_LOGIN)
1650 {
1651 /* Attempt to re-login when a connection fails, but only when not
1652 * currently logging in. */
1653 rc = iscsiAttach(pImage);
1654 if (RT_FAILURE(rc))
1655 break;
1656 }
1657 }
1658 return rc;
1659}
1660
1661
1662/**
1663 * Wait for an iSCSI response with a matching Initiator Target Tag. The response is
1664 * split into several segments, as requested by the caller-provided buffer specification.
1665 * Remember that the response can be split into several PDUs by the sender, so make
1666 * sure that all parts are collected and processed appropriately by the caller.
1667 *
1668 * @returns VBOX status
1669 * @param pImage The iSCSI connection state to be used.
1670 * @param paRes Pointer to array of iSCSI response sections.
1671 * @param cnRes Number of valid iSCSI response sections in the array.
1672 */
1673static int iscsiRecvPDU(PISCSIIMAGE pImage, uint32_t itt, PISCSIRES paRes, uint32_t cnRes)
1674{
1675 int rc = VINF_SUCCESS;
1676 uint32_t i;
1677 ISCSIRES aResBuf;
1678
1679 for (i = 0; i < pImage->cISCSIRetries; i++)
1680 {
1681 aResBuf.pvSeg = pImage->pvRecvPDUBuf;
1682 aResBuf.cbSeg = pImage->cbRecvPDUBuf;
1683 rc = iscsiTransportRead(pImage, &aResBuf, 1);
1684 if (RT_FAILURE(rc))
1685 {
1686 if (rc == VERR_BROKEN_PIPE || rc == VERR_NET_CONNECTION_REFUSED)
1687 {
1688 /* No point in reestablishing the connection for a logout */
1689 if (pImage->state == ISCSISTATE_IN_LOGOUT)
1690 break;
1691 /* Connection broken while waiting for a response - wait a while and
1692 * try to restart by re-sending the original request (if any).
1693 * This also handles the connection reestablishment (login etc.). */
1694 RTThreadSleep(500);
1695 if (pImage->state != ISCSISTATE_IN_LOGIN)
1696 {
1697 /* Attempt to re-login when a connection fails, but only when not
1698 * currently logging in. */
1699 rc = iscsiAttach(pImage);
1700 if (RT_FAILURE(rc))
1701 break;
1702 }
1703 if (pImage->paCurrReq != NULL)
1704 {
1705 rc = iscsiSendPDU(pImage, pImage->paCurrReq, pImage->cnCurrReq);
1706 if (RT_FAILURE(rc))
1707 break;
1708 }
1709 }
1710 else
1711 {
1712 /* Signal other errors (VERR_BUFFER_OVERFLOW etc.) to the caller. */
1713 break;
1714 }
1715 }
1716 else
1717 {
1718 ISCSIOPCODE cmd;
1719 const uint32_t *pcvResSeg = (const uint32_t *)aResBuf.pvSeg;
1720
1721 /* Check whether the received PDU is valid, and update the internal state of
1722 * the iSCSI connection/session. */
1723 rc = drvISCSIValidatePDU(&aResBuf, 1);
1724 if (RT_FAILURE(rc))
1725 continue;
1726 cmd = (ISCSIOPCODE)(RT_N2H_U32(pcvResSeg[0]) & ISCSIOP_MASK);
1727 switch (cmd)
1728 {
1729 case ISCSIOP_SCSI_RES:
1730 case ISCSIOP_SCSI_TASKMGMT_RES:
1731 case ISCSIOP_SCSI_DATA_IN:
1732 case ISCSIOP_R2T:
1733 case ISCSIOP_ASYN_MSG:
1734 case ISCSIOP_TEXT_RES:
1735 case ISCSIOP_LOGIN_RES:
1736 case ISCSIOP_LOGOUT_RES:
1737 case ISCSIOP_REJECT:
1738 case ISCSIOP_NOP_IN:
1739 if (serial_number_less(pImage->MaxCmdSN, RT_N2H_U32(pcvResSeg[8])))
1740 pImage->MaxCmdSN = RT_N2H_U32(pcvResSeg[8]);
1741 if (serial_number_less(pImage->ExpCmdSN, RT_N2H_U32(pcvResSeg[7])))
1742 pImage->ExpCmdSN = RT_N2H_U32(pcvResSeg[7]);
1743 break;
1744 default:
1745 rc = VERR_PARSE_ERROR;
1746 }
1747 if (RT_FAILURE(rc))
1748 continue;
1749 if ( !pImage->FirstRecvPDU
1750 && (cmd != ISCSIOP_SCSI_DATA_IN || (RT_N2H_U32(pcvResSeg[0]) & ISCSI_STATUS_BIT)))
1751 {
1752 if (pImage->ExpStatSN == RT_N2H_U32(pcvResSeg[6]))
1753 {
1754 /* StatSN counter is not advanced on R2T and on a target SN update NOP-In. */
1755 if ( (cmd != ISCSIOP_R2T)
1756 && ((cmd != ISCSIOP_NOP_IN) || (RT_N2H_U32(pcvResSeg[4]) != ISCSI_TASK_TAG_RSVD)))
1757 pImage->ExpStatSN++;
1758 }
1759 else
1760 {
1761 rc = VERR_PARSE_ERROR;
1762 continue;
1763 }
1764 }
1765 /* Finally check whether the received PDU matches what the caller wants. */
1766 if (itt == pcvResSeg[4])
1767 {
1768 /* Copy received PDU (one segment) to caller-provided buffers. */
1769 uint32_t i;
1770 size_t cbSeg;
1771 const uint8_t *pSrc;
1772
1773 pSrc = (const uint8_t *)aResBuf.pvSeg;
1774 cbSeg = aResBuf.cbSeg;
1775 for (i = 0; i < cnRes; i++)
1776 {
1777 if (cbSeg > paRes[i].cbSeg)
1778 {
1779 memcpy(paRes[i].pvSeg, pSrc, paRes[i].cbSeg);
1780 pSrc += paRes[i].cbSeg;
1781 cbSeg -= paRes[i].cbSeg;
1782 }
1783 else
1784 {
1785 memcpy(paRes[i].pvSeg, pSrc, cbSeg);
1786 paRes[i].cbSeg = cbSeg;
1787 cbSeg = 0;
1788 break;
1789 }
1790 }
1791 if (cbSeg != 0)
1792 {
1793 rc = VERR_BUFFER_OVERFLOW;
1794 break;
1795 }
1796 for (i++; i < cnRes; i++)
1797 paRes[i].cbSeg = 0;
1798 break;
1799 }
1800 else if ( cmd == ISCSIOP_NOP_IN
1801 && RT_N2H_U32(pcvResSeg[5]) != ISCSI_TASK_TAG_RSVD)
1802 {
1803 const uint32_t *pcvResSeg = (const uint32_t *)aResBuf.pvSeg;
1804 uint32_t cnISCSIReq;
1805 ISCSIREQ aISCSIReq[4];
1806 uint32_t aReqBHS[12];
1807
1808 aReqBHS[0] = RT_H2N_U32(ISCSI_IMMEDIATE_DELIVERY_BIT | ISCSI_FINAL_BIT | ISCSIOP_NOP_OUT);
1809 aReqBHS[1] = RT_H2N_U32(0); /* TotalAHSLength=0,DataSementLength=0 */
1810 aReqBHS[2] = pcvResSeg[2]; /* copy LUN from NOP-In */
1811 aReqBHS[3] = pcvResSeg[3]; /* copy LUN from NOP-In */
1812 aReqBHS[4] = RT_H2N_U32(ISCSI_TASK_TAG_RSVD); /* ITT, reply */
1813 aReqBHS[5] = pcvResSeg[5]; /* copy TTT from NOP-In */
1814 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
1815 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
1816 aReqBHS[8] = 0; /* reserved */
1817 aReqBHS[9] = 0; /* reserved */
1818 aReqBHS[10] = 0; /* reserved */
1819 aReqBHS[11] = 0; /* reserved */
1820
1821 cnISCSIReq = 0;
1822 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
1823 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
1824 cnISCSIReq++;
1825
1826 iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq);
1827 }
1828 }
1829 }
1830 return rc;
1831}
1832
1833
1834/**
1835 * Check the static (not dependent on the connection/session state) validity of an iSCSI response PDU.
1836 *
1837 * @returns VBOX status
1838 * @param paRes Pointer to array of iSCSI response sections.
1839 * @param cnRes Number of valid iSCSI response sections in the array.
1840 */
1841static int drvISCSIValidatePDU(PISCSIRES paRes, uint32_t cnRes)
1842{
1843 const uint32_t *pcrgResBHS;
1844 uint32_t hw0;
1845 Assert(cnRes >= 1);
1846 Assert(paRes[0].cbSeg >= ISCSI_BHS_SIZE);
1847
1848 pcrgResBHS = (const uint32_t *)(paRes[0].pvSeg);
1849 hw0 = RT_N2H_U32(pcrgResBHS[0]);
1850 switch (hw0 & ISCSIOP_MASK)
1851 {
1852 case ISCSIOP_NOP_IN:
1853 /* NOP-In responses must not be split into several PDUs nor it may contain
1854 * ping data for target-initiated pings nor may both task tags be valid task tags. */
1855 if ( (hw0 & ISCSI_FINAL_BIT) == 0
1856 || ( RT_N2H_U32(pcrgResBHS[4]) == ISCSI_TASK_TAG_RSVD
1857 && RT_N2H_U32(pcrgResBHS[1]) != 0)
1858 || ( RT_N2H_U32(pcrgResBHS[4]) != ISCSI_TASK_TAG_RSVD
1859 && RT_N2H_U32(pcrgResBHS[5]) != ISCSI_TASK_TAG_RSVD))
1860 return VERR_PARSE_ERROR;
1861 break;
1862 case ISCSIOP_SCSI_RES:
1863 /* SCSI responses must not be split into several PDUs nor must the residual
1864 * bits be contradicting each other nor may the residual bits be set for PDUs
1865 * containing anything else but a completed command response. Underflow
1866 * is no reason for declaring a PDU as invalid, as the target may choose
1867 * to return less data than we assume to get. */
1868 if ( (hw0 & ISCSI_FINAL_BIT) == 0
1869 || ((hw0 & ISCSI_BI_READ_RESIDUAL_OVFL_BIT) && (hw0 & ISCSI_BI_READ_RESIDUAL_UNFL_BIT))
1870 || ((hw0 & ISCSI_RESIDUAL_OVFL_BIT) && (hw0 & ISCSI_RESIDUAL_UNFL_BIT))
1871 || ( ((hw0 & ISCSI_SCSI_RESPONSE_MASK) == 0)
1872 && ((hw0 & ISCSI_SCSI_STATUS_MASK) == SCSI_STATUS_OK)
1873 && (hw0 & ( ISCSI_BI_READ_RESIDUAL_OVFL_BIT | ISCSI_BI_READ_RESIDUAL_UNFL_BIT
1874 | ISCSI_RESIDUAL_OVFL_BIT))))
1875 return VERR_PARSE_ERROR;
1876 break;
1877 case ISCSIOP_LOGIN_RES:
1878 /* Login responses must not contain contradicting transit and continue bits. */
1879 if ((hw0 & ISCSI_CONTINUE_BIT) && ((hw0 & ISCSI_TRANSIT_BIT) != 0))
1880 return VERR_PARSE_ERROR;
1881 break;
1882 case ISCSIOP_TEXT_RES:
1883 /* Text responses must not contain contradicting final and continue bits nor
1884 * may the final bit be set for PDUs containing a target transfer tag other than
1885 * the reserved transfer tag (and vice versa). */
1886 if ( (((hw0 & ISCSI_CONTINUE_BIT) && (hw0 & ISCSI_FINAL_BIT) != 0))
1887 || (((hw0 & ISCSI_FINAL_BIT) && (RT_N2H_U32(pcrgResBHS[5]) != ISCSI_TASK_TAG_RSVD)))
1888 || (((hw0 & ISCSI_FINAL_BIT) == 0) && (RT_N2H_U32(pcrgResBHS[5]) == ISCSI_TASK_TAG_RSVD)))
1889 return VERR_PARSE_ERROR;
1890 break;
1891 case ISCSIOP_SCSI_DATA_IN:
1892 /* SCSI Data-in responses must not contain contradicting residual bits when
1893 * status bit is set. */
1894 if ((hw0 & ISCSI_STATUS_BIT) && (hw0 & ISCSI_RESIDUAL_OVFL_BIT) && (hw0 & ISCSI_RESIDUAL_UNFL_BIT))
1895 return VERR_PARSE_ERROR;
1896 break;
1897 case ISCSIOP_LOGOUT_RES:
1898 /* Logout responses must not have the final bit unset and may not contain any
1899 * data or additional header segments. */
1900 if ( ((hw0 & ISCSI_FINAL_BIT) == 0)
1901 || (RT_N2H_U32(pcrgResBHS[1]) != 0))
1902 return VERR_PARSE_ERROR;
1903 break;
1904 case ISCSIOP_ASYN_MSG:
1905 /* Asynchronous Messages must not have the final bit unser and may not contain
1906 * an initiator task tag. */
1907 if ( ((hw0 & ISCSI_FINAL_BIT) == 0)
1908 || (RT_N2H_U32(pcrgResBHS[4]) != ISCSI_TASK_TAG_RSVD))
1909 return VERR_PARSE_ERROR;
1910 break;
1911 case ISCSIOP_SCSI_TASKMGMT_RES:
1912 case ISCSIOP_R2T:
1913 case ISCSIOP_REJECT:
1914 default:
1915 /* Do some logging, ignore PDU. */
1916 LogFlow(("drvISCSIValidatePDU: ignore unhandled PDU, first word %#08x\n", RT_N2H_U32(pcrgResBHS[0])));
1917 return VERR_PARSE_ERROR;
1918 }
1919 /* A target must not send PDUs with MaxCmdSN less than ExpCmdSN-1. */
1920
1921 if (serial_number_less(RT_N2H_U32(pcrgResBHS[8]), RT_N2H_U32(pcrgResBHS[7])-1))
1922 return VERR_PARSE_ERROR;
1923
1924 return VINF_SUCCESS;
1925}
1926
1927
1928/**
1929 * Appends a key-value pair to the buffer. Normal ASCII strings (cbValue == 0) and large binary values
1930 * of a given length (cbValue > 0) are directly supported. Other value types must be converted to ASCII
1931 * by the caller. Strings must be in UTF-8 encoding.
1932 *
1933 * @returns VBOX status
1934 * @param pbBuf Pointer to the key-value buffer.
1935 * @param cbBuf Length of the key-value buffer.
1936 * @param pcbBufCurr Currently used portion of the key-value buffer.
1937 * @param pszKey Pointer to a string containing the key.
1938 * @param pszValue Pointer to either a string containing the value or to a large binary value.
1939 * @param cbValue Length of the binary value if applicable.
1940 */
1941static int iscsiTextAddKeyValue(uint8_t *pbBuf, size_t cbBuf, size_t *pcbBufCurr, const char *pcszKey,
1942 const char *pcszValue, size_t cbValue)
1943{
1944 size_t cbBufTmp = *pcbBufCurr;
1945 size_t cbKey = strlen(pcszKey);
1946 size_t cbValueEnc;
1947 uint8_t *pbCurr;
1948
1949 if (cbValue == 0)
1950 cbValueEnc = strlen(pcszValue);
1951 else
1952 cbValueEnc = cbValue * 2 + 2; /* 2 hex bytes per byte, 2 bytes prefix */
1953
1954 if (cbBuf < cbBufTmp + cbKey + 1 + cbValueEnc + 1)
1955 {
1956 /* Buffer would overflow, signal error. */
1957 return VERR_BUFFER_OVERFLOW;
1958 }
1959
1960 /*
1961 * Append a key=value pair (zero terminated string) to the end of the buffer.
1962 */
1963 pbCurr = pbBuf + cbBufTmp;
1964 memcpy(pbCurr, pcszKey, cbKey);
1965 pbCurr += cbKey;
1966 *pbCurr++ = '=';
1967 if (cbValue == 0)
1968 {
1969 memcpy(pbCurr, pcszValue, cbValueEnc);
1970 pbCurr += cbValueEnc;
1971 }
1972 else
1973 {
1974 *pbCurr++ = '0';
1975 *pbCurr++ = 'x';
1976 for (uint32_t i = 0; i < cbValue; i++)
1977 {
1978 uint8_t b;
1979 b = pcszValue[i];
1980 *pbCurr++ = NUM_2_HEX(b >> 4);
1981 *pbCurr++ = NUM_2_HEX(b & 0xf);
1982 }
1983 }
1984 *pbCurr = '\0';
1985 *pcbBufCurr = cbBufTmp + cbKey + 1 + cbValueEnc + 1;
1986
1987 return VINF_SUCCESS;
1988}
1989
1990
1991/**
1992 * Retrieve the value for a given key from the key=value buffer.
1993 *
1994 * @returns VBOX status.
1995 * @param pbBuf Buffer containing key=value pairs.
1996 * @param cbBuf Length of buffer with key=value pairs.
1997 * @param pszKey Pointer to key for which to retrieve the value.
1998 * @param ppszValue Pointer to value string pointer.
1999 */
2000static int iscsiTextGetKeyValue(const uint8_t *pbBuf, size_t cbBuf, const char *pcszKey, const char **ppcszValue)
2001{
2002 size_t cbKey = strlen(pcszKey);
2003
2004 while (cbBuf != 0)
2005 {
2006 size_t cbKeyValNull = strlen((const char *)pbBuf) + 1;
2007
2008 if (strncmp(pcszKey, (const char *)pbBuf, cbKey) == 0 && pbBuf[cbKey] == '=')
2009 {
2010 *ppcszValue = (const char *)(pbBuf + cbKey + 1);
2011 return VINF_SUCCESS;
2012 }
2013 pbBuf += cbKeyValNull;
2014 cbBuf -= cbKeyValNull;
2015 }
2016 return VERR_INVALID_NAME;
2017}
2018
2019
2020/**
2021 * Convert a long-binary value from a value string to the binary representation.
2022 *
2023 * @returns VBOX status
2024 * @param pszValue Pointer to a string containing the textual value representation.
2025 * @param pbValue Pointer to the value buffer for the binary value.
2026 * @param pcbValue In: length of value buffer, out: actual length of binary value.
2027 */
2028static int iscsiStrToBinary(const char *pcszValue, uint8_t *pbValue, size_t *pcbValue)
2029{
2030 size_t cbValue = *pcbValue;
2031 char c1, c2, c3, c4;
2032 Assert(cbValue >= 1);
2033
2034 if (strlen(pcszValue) < 3)
2035 return VERR_PARSE_ERROR;
2036 if (*pcszValue++ != '0')
2037 return VERR_PARSE_ERROR;
2038 switch (*pcszValue++)
2039 {
2040 case 'x':
2041 case 'X':
2042 if (strlen(pcszValue) & 1)
2043 {
2044 c1 = *pcszValue++;
2045 *pbValue++ = HEX_2_NUM(c1);
2046 cbValue--;
2047 }
2048 while (*pcszValue != '\0')
2049 {
2050 if (cbValue == 0)
2051 return VERR_BUFFER_OVERFLOW;
2052 c1 = *pcszValue++;
2053 if ((c1 < '0' || c1 > '9') && (c1 < 'a' || c1 > 'f') && (c1 < 'A' || c1 > 'F'))
2054 return VERR_PARSE_ERROR;
2055 c2 = *pcszValue++;
2056 if ((c2 < '0' || c2 > '9') && (c2 < 'a' || c2 > 'f') && (c2 < 'A' || c2 > 'F'))
2057 return VERR_PARSE_ERROR;
2058 *pbValue++ = (HEX_2_NUM(c1) << 4) | HEX_2_NUM(c2);
2059 cbValue--;
2060 }
2061 *pcbValue -= cbValue;
2062 break;
2063 case 'b':
2064 case 'B':
2065 if ((strlen(pcszValue) & 3) != 0)
2066 return VERR_PARSE_ERROR;
2067 while (*pcszValue != '\0')
2068 {
2069 uint32_t temp;
2070 if (cbValue == 0)
2071 return VERR_BUFFER_OVERFLOW;
2072 c1 = *pcszValue++;
2073 if ((c1 < 'A' || c1 > 'Z') && (c1 < 'a' || c1 >'z') && (c1 < '0' || c1 > '9') && (c1 != '+') && (c1 != '/'))
2074 return VERR_PARSE_ERROR;
2075 c2 = *pcszValue++;
2076 if ((c2 < 'A' || c2 > 'Z') && (c2 < 'a' || c2 >'z') && (c2 < '0' || c2 > '9') && (c2 != '+') && (c2 != '/'))
2077 return VERR_PARSE_ERROR;
2078 c3 = *pcszValue++;
2079 if ((c3 < 'A' || c3 > 'Z') && (c3 < 'a' || c3 >'z') && (c3 < '0' || c3 > '9') && (c3 != '+') && (c3 != '/') && (c3 != '='))
2080 return VERR_PARSE_ERROR;
2081 c4 = *pcszValue++;
2082 if ( (c3 == '=' && c4 != '=')
2083 || ((c4 < 'A' || c4 > 'Z') && (c4 < 'a' || c4 >'z') && (c4 < '0' || c4 > '9') && (c4 != '+') && (c4 != '/') && (c4 != '=')))
2084 return VERR_PARSE_ERROR;
2085 temp = (B64_2_NUM(c1) << 18) | (B64_2_NUM(c2) << 12);
2086 if (c3 == '=') {
2087 if (*pcszValue != '\0')
2088 return VERR_PARSE_ERROR;
2089 *pbValue++ = temp >> 16;
2090 cbValue--;
2091 } else {
2092 temp |= B64_2_NUM(c3) << 6;
2093 if (c4 == '=') {
2094 if (*pcszValue != '\0')
2095 return VERR_PARSE_ERROR;
2096 if (cbValue < 2)
2097 return VERR_BUFFER_OVERFLOW;
2098 *pbValue++ = temp >> 16;
2099 *pbValue++ = (temp >> 8) & 0xff;
2100 cbValue -= 2;
2101 }
2102 else
2103 {
2104 temp |= B64_2_NUM(c4);
2105 if (cbValue < 3)
2106 return VERR_BUFFER_OVERFLOW;
2107 *pbValue++ = temp >> 16;
2108 *pbValue++ = (temp >> 8) & 0xff;
2109 *pbValue++ = temp & 0xff;
2110 cbValue -= 3;
2111 }
2112 }
2113 }
2114 *pcbValue -= cbValue;
2115 break;
2116 default:
2117 return VERR_PARSE_ERROR;
2118 }
2119 return VINF_SUCCESS;
2120}
2121
2122
2123static bool serial_number_less(uint32_t s1, uint32_t s2)
2124{
2125 return (s1 < s2 && s2 - s1 < 0x80000000) || (s1 > s2 && s1 - s2 > 0x80000000);
2126}
2127
2128
2129#ifdef IMPLEMENT_TARGET_AUTH
2130static void chap_md5_generate_challenge(uint8_t *pbChallenge, size_t *pcbChallenge)
2131{
2132 uint8_t cbChallenge;
2133
2134 cbChallenge = RTrand_U8(CHAP_MD5_CHALLENGE_MIN, CHAP_MD5_CHALLENGE_MAX);
2135 RTrand_bytes(pbChallenge, cbChallenge);
2136 *pcbChallenge = cbChallenge;
2137}
2138#endif
2139
2140
2141static void chap_md5_compute_response(uint8_t *pbResponse, uint8_t id, const uint8_t *pbChallenge, size_t cbChallenge,
2142 const uint8_t *pbSecret, size_t cbSecret)
2143{
2144 RTMD5CONTEXT ctx;
2145 uint8_t bId;
2146
2147 bId = id;
2148 RTMd5Init(&ctx);
2149 RTMd5Update(&ctx, &bId, 1);
2150 RTMd5Update(&ctx, pbSecret, cbSecret);
2151 RTMd5Update(&ctx, pbChallenge, cbChallenge);
2152 RTMd5Final(pbResponse, &ctx);
2153}
2154
2155/**
2156 * Internal. Free all allocated space for representing an image, and optionally
2157 * delete the image from disk.
2158 */
2159static void iscsiFreeImage(PISCSIIMAGE pImage, bool fDelete)
2160{
2161 Assert(pImage);
2162 Assert(!fDelete); /* This MUST be false, the flag isn't supported. */
2163
2164 if (pImage->Mutex != NIL_RTSEMMUTEX)
2165 {
2166 /* Detaching only makes sense when the mutex is there. Otherwise the
2167 * failure happened long before we could attach to the target. */
2168 iscsiDetach(pImage);
2169 RTSemMutexDestroy(pImage->Mutex);
2170 pImage->Mutex = NIL_RTSEMMUTEX;
2171 }
2172 if (pImage->pszTargetName)
2173 {
2174 RTMemFree(pImage->pszTargetName);
2175 pImage->pszTargetName = NULL;
2176 }
2177 if (pImage->pszInitiatorName)
2178 {
2179 RTMemFree(pImage->pszInitiatorName);
2180 pImage->pszInitiatorName = NULL;
2181 }
2182 if (pImage->pszInitiatorUsername)
2183 {
2184 RTMemFree(pImage->pszInitiatorUsername);
2185 pImage->pszInitiatorUsername = NULL;
2186 }
2187 if (pImage->pbInitiatorSecret)
2188 {
2189 RTMemFree(pImage->pbInitiatorSecret);
2190 pImage->pbInitiatorSecret = NULL;
2191 }
2192 if (pImage->pszTargetUsername)
2193 {
2194 RTMemFree(pImage->pszTargetUsername);
2195 pImage->pszTargetUsername = NULL;
2196 }
2197 if (pImage->pbTargetSecret)
2198 {
2199 RTMemFree(pImage->pbTargetSecret);
2200 pImage->pbTargetSecret = NULL;
2201 }
2202 if (pImage->pvRecvPDUBuf)
2203 {
2204 RTMemFree(pImage->pvRecvPDUBuf);
2205 pImage->pvRecvPDUBuf = NULL;
2206 }
2207}
2208
2209/**
2210 * Internal: Open an image, constructing all necessary data structures.
2211 */
2212static int iscsiOpenImage(PISCSIIMAGE pImage, unsigned uOpenFlags)
2213{
2214 int rc;
2215 char *pszLUN = NULL, *pszLUNInitial = NULL;
2216 bool fLunEncoded = false;
2217 uint32_t uTimeoutDef = 0;
2218 uint64_t uHostIPTmp = 0;
2219 bool fHostIPDef = 0;
2220 rc = RTStrToUInt32Full(s_iscsiConfigDefaultTimeout, 0, &uTimeoutDef);
2221 AssertRC(rc);
2222 rc = RTStrToUInt64Full(s_iscsiConfigDefaultHostIPStack, 0, &uHostIPTmp);
2223 AssertRC(rc);
2224 fHostIPDef = !!uHostIPTmp;
2225
2226 pImage->uOpenFlags = uOpenFlags;
2227
2228 /* Get error signalling interface. */
2229 pImage->pInterfaceError = VDInterfaceGet(pImage->pVDIfsDisk, VDINTERFACETYPE_ERROR);
2230 if (pImage->pInterfaceError)
2231 pImage->pInterfaceErrorCallbacks = VDGetInterfaceError(pImage->pInterfaceError);
2232
2233 /* Get TCP network stack interface. */
2234 pImage->pInterfaceNet = VDInterfaceGet(pImage->pVDIfsDisk, VDINTERFACETYPE_TCPNET);
2235 if (pImage->pInterfaceNet)
2236 pImage->pInterfaceNetCallbacks = VDGetInterfaceTcpNet(pImage->pInterfaceNet);
2237 else
2238 {
2239 rc = iscsiError(pImage, VERR_VD_ISCSI_UNKNOWN_INTERFACE,
2240 RT_SRC_POS, N_("iSCSI: TCP network stack interface missing"));
2241 goto out;
2242 }
2243
2244 /* Get configuration interface. */
2245 pImage->pInterfaceConfig = VDInterfaceGet(pImage->pVDIfsImage, VDINTERFACETYPE_CONFIG);
2246 if (pImage->pInterfaceConfig)
2247 pImage->pInterfaceConfigCallbacks = VDGetInterfaceConfig(pImage->pInterfaceConfig);
2248 else
2249 {
2250 rc = iscsiError(pImage, VERR_VD_ISCSI_UNKNOWN_INTERFACE,
2251 RT_SRC_POS, N_("iSCSI: configuration interface missing"));
2252 goto out;
2253 }
2254
2255 pImage->ISID = 0x800000000000ULL | 0x001234560000ULL | (0x00000000cba0ULL + ASMAtomicIncU32(&s_u32iscsiID));
2256 pImage->cISCSIRetries = 10;
2257 pImage->state = ISCSISTATE_FREE;
2258 pImage->pvRecvPDUBuf = RTMemAlloc(ISCSI_RECV_PDU_BUFFER_SIZE);
2259 pImage->cbRecvPDUBuf = ISCSI_RECV_PDU_BUFFER_SIZE;
2260 if (pImage->pvRecvPDUBuf == NULL)
2261 {
2262 rc = VERR_NO_MEMORY;
2263 goto out;
2264 }
2265 pImage->Mutex = NIL_RTSEMMUTEX;
2266 rc = RTSemMutexCreate(&pImage->Mutex);
2267 if (RT_FAILURE(rc))
2268 goto out;
2269
2270 /* Validate configuration, detect unknown keys. */
2271 if (!VDCFGAreKeysValid(pImage->pInterfaceConfigCallbacks,
2272 pImage->pInterfaceConfig->pvUser,
2273 "TargetName\0InitiatorName\0LUN\0TargetAddress\0InitiatorUsername\0InitiatorSecret\0TargetUsername\0TargetSecret\0Timeout\0HostIPStack\0"))
2274 {
2275 rc = iscsiError(pImage, VERR_VD_ISCSI_UNKNOWN_CFG_VALUES, RT_SRC_POS, N_("iSCSI: configuration error: unknown configuration keys present"));
2276 goto out;
2277 }
2278
2279 /* Query the iSCSI upper level configuration. */
2280 rc = VDCFGQueryStringAlloc(pImage->pInterfaceConfigCallbacks,
2281 pImage->pInterfaceConfig->pvUser,
2282 "TargetName", &pImage->pszTargetName);
2283 if (RT_FAILURE(rc))
2284 {
2285 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetName as string"));
2286 goto out;
2287 }
2288 rc = VDCFGQueryStringAllocDef(pImage->pInterfaceConfigCallbacks,
2289 pImage->pInterfaceConfig->pvUser,
2290 "InitiatorName", &pImage->pszInitiatorName,
2291 s_iscsiConfigDefaultInitiatorName);
2292 if (RT_FAILURE(rc))
2293 {
2294 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorName as string"));
2295 goto out;
2296 }
2297 rc = VDCFGQueryStringAllocDef(pImage->pInterfaceConfigCallbacks,
2298 pImage->pInterfaceConfig->pvUser,
2299 "LUN", &pszLUN, s_iscsiConfigDefaultLUN);
2300 if (RT_FAILURE(rc))
2301 {
2302 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read LUN as string"));
2303 goto out;
2304 }
2305 pszLUNInitial = pszLUN;
2306 if (!strncmp(pszLUN, "enc", 3))
2307 {
2308 fLunEncoded = true;
2309 pszLUN += 3;
2310 }
2311 rc = RTStrToUInt64Full(pszLUN, 0, &pImage->LUN);
2312 if (RT_FAILURE(rc))
2313 {
2314 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to convert LUN to integer"));
2315 goto out;
2316 }
2317 if (!fLunEncoded)
2318 {
2319 if (pImage->LUN <= 255)
2320 {
2321 pImage->LUN = pImage->LUN << 48; /* uses peripheral device addressing method */
2322 }
2323 else if (pImage->LUN <= 16383)
2324 {
2325 pImage->LUN = (pImage->LUN << 48) | RT_BIT_64(62); /* uses flat space addressing method */
2326 }
2327 else
2328 {
2329 rc = VERR_OUT_OF_RANGE;
2330 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: LUN number out of range (0-16383)"));
2331 goto out;
2332 }
2333 }
2334 rc = VDCFGQueryStringAlloc(pImage->pInterfaceConfigCallbacks,
2335 pImage->pInterfaceConfig->pvUser,
2336 "TargetAddress", &pImage->pszTargetAddress);
2337 if (RT_FAILURE(rc))
2338 {
2339 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetAddress as string"));
2340 goto out;
2341 }
2342 pImage->pszInitiatorUsername = NULL;
2343 rc = VDCFGQueryStringAlloc(pImage->pInterfaceConfigCallbacks,
2344 pImage->pInterfaceConfig->pvUser,
2345 "InitiatorUsername",
2346 &pImage->pszInitiatorUsername);
2347 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
2348 rc = VINF_SUCCESS;
2349 if (RT_FAILURE(rc))
2350 {
2351 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorUsername as string"));
2352 goto out;
2353 }
2354 pImage->pbInitiatorSecret = NULL;
2355 pImage->cbInitiatorSecret = 0;
2356 rc = VDCFGQueryBytesAlloc(pImage->pInterfaceConfigCallbacks,
2357 pImage->pInterfaceConfig->pvUser,
2358 "InitiatorSecret",
2359 (void **)&pImage->pbInitiatorSecret,
2360 &pImage->cbInitiatorSecret);
2361 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
2362 rc = VINF_SUCCESS;
2363 if (RT_FAILURE(rc))
2364 {
2365 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorSecret as byte string"));
2366 goto out;
2367 }
2368 pImage->pszTargetUsername = NULL;
2369 rc = VDCFGQueryStringAlloc(pImage->pInterfaceConfigCallbacks,
2370 pImage->pInterfaceConfig->pvUser,
2371 "TargetUsername",
2372 &pImage->pszTargetUsername);
2373 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
2374 rc = VINF_SUCCESS;
2375 if (RT_FAILURE(rc))
2376 {
2377 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetUsername as string"));
2378 goto out;
2379 }
2380 pImage->pbTargetSecret = NULL;
2381 pImage->cbTargetSecret = 0;
2382 rc = VDCFGQueryBytesAlloc(pImage->pInterfaceConfigCallbacks,
2383 pImage->pInterfaceConfig->pvUser,
2384 "TargetSecret", (void **)&pImage->pbTargetSecret,
2385 &pImage->cbTargetSecret);
2386 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
2387 rc = VINF_SUCCESS;
2388 if (RT_FAILURE(rc))
2389 {
2390 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetSecret as byte string"));
2391 goto out;
2392 }
2393
2394 pImage->pszHostname = NULL;
2395 pImage->uPort = 0;
2396 pImage->Socket = NIL_RTSOCKET;
2397 /* Query the iSCSI lower level configuration. */
2398 rc = VDCFGQueryU32Def(pImage->pInterfaceConfigCallbacks,
2399 pImage->pInterfaceConfig->pvUser,
2400 "Timeout", &pImage->uReadTimeout,
2401 uTimeoutDef);
2402 if (RT_FAILURE(rc))
2403 {
2404 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read Timeout as U32"));
2405 goto out;
2406 }
2407 rc = VDCFGQueryBoolDef(pImage->pInterfaceConfigCallbacks,
2408 pImage->pInterfaceConfig->pvUser,
2409 "HostIPStack", &pImage->fHostIP,
2410 fHostIPDef);
2411 if (RT_FAILURE(rc))
2412 {
2413 rc = iscsiError(pImage, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read HostIPStack as boolean"));
2414 goto out;
2415 }
2416
2417 /* Don't actually establish iSCSI transport connection if this is just an
2418 * open to query the image information and the host IP stack isn't used.
2419 * Even trying is rather useless, as in this context the InTnet IP stack
2420 * isn't present. Returning dummies is the best possible result anyway. */
2421 if ((uOpenFlags & VD_OPEN_FLAGS_INFO) && !pImage->fHostIP)
2422 {
2423 LogFunc(("Not opening the transport connection as IntNet IP stack is not available. Will return dummies\n"));
2424 goto out;
2425 }
2426
2427 /*
2428 * Establish the iSCSI transport connection.
2429 */
2430 rc = iscsiTransportOpen(pImage);
2431 if (RT_SUCCESS(rc))
2432 rc = iscsiAttach(pImage);
2433
2434 if (RT_FAILURE(rc))
2435 {
2436 LogRel(("iSCSI: could not open target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
2437 goto out;
2438 }
2439 LogFlowFunc(("target '%s' opened successfully\n", pImage->pszTargetName));
2440
2441 SCSIREQ sr;
2442 uint8_t sense[32];
2443 uint8_t data8[8];
2444
2445 /*
2446 * Inquire available LUNs - purely dummy request.
2447 */
2448 uint8_t cdb_rlun[12];
2449 uint8_t rlundata[16];
2450 cdb_rlun[0] = SCSI_REPORT_LUNS;
2451 cdb_rlun[1] = 0; /* reserved */
2452 cdb_rlun[2] = 0; /* reserved */
2453 cdb_rlun[3] = 0; /* reserved */
2454 cdb_rlun[4] = 0; /* reserved */
2455 cdb_rlun[5] = 0; /* reserved */
2456 cdb_rlun[6] = sizeof(rlundata) >> 24;
2457 cdb_rlun[7] = (sizeof(rlundata) >> 16) & 0xff;
2458 cdb_rlun[8] = (sizeof(rlundata) >> 8) & 0xff;
2459 cdb_rlun[9] = sizeof(rlundata) & 0xff;
2460 cdb_rlun[10] = 0; /* reserved */
2461 cdb_rlun[11] = 0; /* control */
2462
2463 sr.enmXfer = SCSIXFER_FROM_TARGET;
2464 sr.cbCmd = sizeof(cdb_rlun);
2465 sr.pvCmd = cdb_rlun;
2466 sr.cbI2TData = 0;
2467 sr.pcvI2TData = NULL;
2468 sr.cbT2IData = sizeof(rlundata);
2469 sr.pvT2IData = rlundata;
2470 sr.cbSense = sizeof(sense);
2471 sr.pvSense = sense;
2472
2473 rc = iscsiCommand(pImage, &sr);
2474 if (RT_FAILURE(rc))
2475 {
2476 LogRel(("iSCSI: Could not get LUN info for target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
2477 return rc;
2478 }
2479
2480 /*
2481 * Inquire device characteristics - no tapes, scanners etc., please.
2482 */
2483 uint8_t cdb_inq[6];
2484 cdb_inq[0] = SCSI_INQUIRY;
2485 cdb_inq[1] = 0; /* reserved */
2486 cdb_inq[2] = 0; /* reserved */
2487 cdb_inq[3] = 0; /* reserved */
2488 cdb_inq[4] = sizeof(data8);
2489 cdb_inq[5] = 0; /* control */
2490
2491 sr.enmXfer = SCSIXFER_FROM_TARGET;
2492 sr.cbCmd = sizeof(cdb_inq);
2493 sr.pvCmd = cdb_inq;
2494 sr.cbI2TData = 0;
2495 sr.pcvI2TData = NULL;
2496 sr.cbT2IData = sizeof(data8);
2497 sr.pvT2IData = data8;
2498 sr.cbSense = sizeof(sense);
2499 sr.pvSense = sense;
2500
2501 rc = iscsiCommand(pImage, &sr);
2502 if (RT_SUCCESS(rc))
2503 {
2504 if ((data8[0] & SCSI_DEVTYPE_MASK) != SCSI_DEVTYPE_DISK)
2505 {
2506 rc = iscsiError(pImage, VERR_VD_ISCSI_INVALID_TYPE,
2507 RT_SRC_POS, N_("iSCSI: target address %s, target name %s, SCSI LUN %lld reports device type=%u"),
2508 pImage->pszTargetAddress, pImage->pszTargetName,
2509 pImage->LUN, data8[0]);
2510 LogRel(("iSCSI: Unsupported SCSI peripheral device type %d for target %s\n", data8[0] & SCSI_DEVTYPE_MASK, pImage->pszTargetName));
2511 goto out;
2512 }
2513 }
2514 else
2515 {
2516 LogRel(("iSCSI: Could not get INQUIRY info for target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
2517 goto out;
2518 }
2519
2520 /*
2521 * Query write disable bit in the device specific parameter entry in the
2522 * mode parameter header. Refuse read/write opening of read only disks.
2523 */
2524
2525 uint8_t cdb_ms[6];
2526 uint8_t data4[4];
2527 cdb_ms[0] = SCSI_MODE_SENSE_6;
2528 cdb_ms[1] = 0; /* dbd=0/reserved */
2529 cdb_ms[2] = 0x3f; /* pc=0/page code=0x3f, ask for all pages */
2530 cdb_ms[3] = 0; /* subpage code=0, return everything in page_0 format */
2531 cdb_ms[4] = sizeof(data4); /* allocation length=4 */
2532 cdb_ms[5] = 0; /* control */
2533
2534 sr.enmXfer = SCSIXFER_FROM_TARGET;
2535 sr.cbCmd = sizeof(cdb_ms);
2536 sr.pvCmd = cdb_ms;
2537 sr.cbI2TData = 0;
2538 sr.pcvI2TData = NULL;
2539 sr.cbT2IData = sizeof(data4);
2540 sr.pvT2IData = data4;
2541 sr.cbSense = sizeof(sense);
2542 sr.pvSense = sense;
2543
2544 rc = iscsiCommand(pImage, &sr);
2545 if (RT_SUCCESS(rc))
2546 {
2547 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY) && data4[2] & 0x80)
2548 {
2549 rc = VERR_VD_IMAGE_READ_ONLY;
2550 goto out;
2551 }
2552 }
2553 else
2554 {
2555 LogRel(("iSCSI: Could not get MODE SENSE info for target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
2556 goto out;
2557 }
2558
2559 /*
2560 * Determine sector size and capacity of the volume immediately.
2561 */
2562 uint8_t cdb_cap[10];
2563
2564 cdb_cap[0] = SCSI_READ_CAPACITY;
2565 cdb_cap[1] = 0; /* reserved */
2566 cdb_cap[2] = 0; /* reserved */
2567 cdb_cap[3] = 0; /* reserved */
2568 cdb_cap[4] = 0; /* reserved */
2569 cdb_cap[5] = 0; /* reserved */
2570 cdb_cap[6] = 0; /* reserved */
2571 cdb_cap[7] = 0; /* reserved */
2572 cdb_cap[8] = 0; /* reserved */
2573 cdb_cap[9] = 0; /* control */
2574
2575 sr.enmXfer = SCSIXFER_FROM_TARGET;
2576 sr.cbCmd = sizeof(cdb_cap);
2577 sr.pvCmd = cdb_cap;
2578 sr.cbI2TData = 0;
2579 sr.pcvI2TData = NULL;
2580 sr.cbT2IData = sizeof(data8);
2581 sr.pvT2IData = data8;
2582 sr.cbSense = sizeof(sense);
2583 sr.pvSense = sense;
2584
2585 rc = iscsiCommand(pImage, &sr);
2586 if (RT_SUCCESS(rc))
2587 {
2588 pImage->cVolume = (data8[0] << 24) | (data8[1] << 16) | (data8[2] << 8) | data8[3];
2589 pImage->cVolume++;
2590 pImage->cbSector = (data8[4] << 24) | (data8[5] << 16) | (data8[6] << 8) | data8[7];
2591 pImage->cbSize = (uint64_t)(pImage->cVolume) * pImage->cbSector;
2592 if (pImage->cVolume == 0 || pImage->cbSector == 0)
2593 {
2594 rc = iscsiError(pImage, VERR_VD_ISCSI_INVALID_TYPE,
2595 RT_SRC_POS, N_("iSCSI: target address %s, target name %s, SCSI LUN %lld reports media sector count=%lu sector size=%lu"),
2596 pImage->pszTargetAddress, pImage->pszTargetName,
2597 pImage->LUN, pImage->cVolume, pImage->cbSector);
2598 }
2599 }
2600 else
2601 {
2602 LogRel(("iSCSI: Could not determine capacity of target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
2603 goto out;
2604 }
2605
2606 /*
2607 * Check the read and write cache bits.
2608 * Try to enable the cache if it is disabled.
2609 *
2610 * We already checked that this is a block access device. No need
2611 * to do it again.
2612 */
2613 uint8_t aCachingModePage[32];
2614 uint8_t aCDBModeSense6[6];
2615
2616 memset(aCachingModePage, '\0', sizeof(aCachingModePage));
2617 aCDBModeSense6[0] = SCSI_MODE_SENSE_6;
2618 aCDBModeSense6[1] = 0;
2619 aCDBModeSense6[2] = (0x00 << 6) | (0x08 & 0x3f); /* Current values and caching mode page */
2620 aCDBModeSense6[3] = 0; /* Sub page code. */
2621 aCDBModeSense6[4] = sizeof(aCachingModePage) & 0xff;
2622 aCDBModeSense6[5] = 0;
2623 sr.enmXfer = SCSIXFER_FROM_TARGET;
2624 sr.cbCmd = sizeof(aCDBModeSense6);
2625 sr.pvCmd = aCDBModeSense6;
2626 sr.cbI2TData = 0;
2627 sr.pcvI2TData = NULL;
2628 sr.cbT2IData = sizeof(aCachingModePage);
2629 sr.pvT2IData = aCachingModePage;
2630 sr.cbSense = sizeof(sense);
2631 sr.pvSense = sense;
2632 rc = iscsiCommand(pImage, &sr);
2633 if ( RT_SUCCESS(rc)
2634 && (sr.status == SCSI_STATUS_OK)
2635 && (aCachingModePage[0] >= 15)
2636 && (aCachingModePage[4 + aCachingModePage[3]] & 0x3f) == 0x08
2637 && (aCachingModePage[4 + aCachingModePage[3] + 1] > 3))
2638 {
2639 uint32_t Offset = 4 + aCachingModePage[3];
2640 /*
2641 * Check if the read and/or the write cache is disabled.
2642 * The write cache is disabled if bit 2 (WCE) is zero and
2643 * the read cache is disabled if bit 0 (RCD) is set.
2644 */
2645 if (!ASMBitTest(&aCachingModePage[Offset + 2], 2) || ASMBitTest(&aCachingModePage[Offset + 2], 0))
2646 {
2647 /*
2648 * Write Cache Enable (WCE) bit is zero or the Read Cache Disable (RCD) is one
2649 * So one of the caches is disabled. Enable both caches.
2650 * The rest is unchanged.
2651 */
2652 ASMBitSet(&aCachingModePage[Offset + 2], 2);
2653 ASMBitClear(&aCachingModePage[Offset + 2], 0);
2654
2655 uint8_t aCDBCaching[6];
2656 aCDBCaching[0] = SCSI_MODE_SELECT_6;
2657 aCDBCaching[1] = 0; /* Don't write the page into NV RAM. */
2658 aCDBCaching[2] = 0;
2659 aCDBCaching[3] = 0;
2660 aCDBCaching[4] = sizeof(aCachingModePage) & 0xff;
2661 aCDBCaching[5] = 0;
2662 sr.enmXfer = SCSIXFER_TO_TARGET;
2663 sr.cbCmd = sizeof(aCDBCaching);
2664 sr.pvCmd = aCDBCaching;
2665 sr.cbI2TData = sizeof(aCachingModePage);
2666 sr.pcvI2TData = aCachingModePage;
2667 sr.cbT2IData = 0;
2668 sr.pvT2IData = NULL;
2669 sr.cbSense = sizeof(sense);
2670 sr.pvSense = sense;
2671 sr.status = 0;
2672 rc = iscsiCommand(pImage, &sr);
2673 if ( RT_SUCCESS(rc)
2674 && (sr.status == SCSI_STATUS_OK))
2675 {
2676 LogRel(("iSCSI: Enabled read and write cache of target %s\n", pImage->pszTargetName));
2677 }
2678 else
2679 {
2680 /* Log failures but continue. */
2681 LogRel(("iSCSI: Could not enable read and write cache of target %s, rc=%Rrc status=%#x\n",
2682 pImage->pszTargetName, rc, sr.status));
2683 LogRel(("iSCSI: Sense:\n%.*Rhxd\n", sr.cbSense, sense));
2684 rc = VINF_SUCCESS;
2685 }
2686 }
2687 }
2688 else
2689 {
2690 /* Log errors but continue. */
2691 LogRel(("iSCSI: Could not check write cache of target %s, rc=%Rrc, got mode page %#x\n", pImage->pszTargetName, rc,aCachingModePage[0] & 0x3f));
2692 LogRel(("iSCSI: Sense:\n%.*Rhxd\n", sr.cbSense, sense));
2693 rc = VINF_SUCCESS;
2694 }
2695
2696
2697out:
2698 if (RT_FAILURE(rc))
2699 iscsiFreeImage(pImage, false);
2700 return rc;
2701}
2702
2703
2704/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
2705static int iscsiCheckIfValid(const char *pszFilename)
2706{
2707 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
2708
2709 /* iSCSI images can't be checked for validity this way, as the filename
2710 * just can't supply enough configuration information. */
2711 int rc = VERR_VD_ISCSI_INVALID_HEADER;
2712
2713 LogFlowFunc(("returns %Rrc\n", rc));
2714 return rc;
2715}
2716
2717
2718/** @copydoc VBOXHDDBACKEND::pfnOpen */
2719static int iscsiOpen(const char *pszFilename, unsigned uOpenFlags,
2720 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
2721 void **ppBackendData)
2722{
2723 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
2724 int rc;
2725 PISCSIIMAGE pImage;
2726
2727 /* Check open flags. All valid flags are supported. */
2728 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
2729 {
2730 rc = VERR_INVALID_PARAMETER;
2731 goto out;
2732 }
2733
2734 /* Check remaining arguments. */
2735 if ( !VALID_PTR(pszFilename)
2736 || !*pszFilename
2737 || strchr(pszFilename, '"'))
2738 {
2739 rc = VERR_INVALID_PARAMETER;
2740 goto out;
2741 }
2742
2743 pImage = (PISCSIIMAGE)RTMemAllocZ(sizeof(ISCSIIMAGE));
2744 if (!pImage)
2745 {
2746 rc = VERR_NO_MEMORY;
2747 goto out;
2748 }
2749
2750 pImage->pszFilename = pszFilename;
2751 pImage->pszInitiatorName = NULL;
2752 pImage->pszTargetName = NULL;
2753 pImage->pszTargetAddress = NULL;
2754 pImage->pszInitiatorUsername = NULL;
2755 pImage->pbInitiatorSecret = NULL;
2756 pImage->pszTargetUsername = NULL;
2757 pImage->pbTargetSecret = NULL;
2758 pImage->paCurrReq = NULL;
2759 pImage->pvRecvPDUBuf = NULL;
2760 pImage->pszHostname = NULL;
2761 pImage->pVDIfsDisk = pVDIfsDisk;
2762 pImage->pVDIfsImage = pVDIfsImage;
2763
2764 rc = iscsiOpenImage(pImage, uOpenFlags);
2765 if (RT_SUCCESS(rc))
2766 *ppBackendData = pImage;
2767
2768out:
2769 if (RT_SUCCESS(rc))
2770 {
2771 LogFlowFunc(("target %s cVolume %d, cbSector %d\n", pImage->pszTargetName, pImage->cVolume, pImage->cbSector));
2772 LogRel(("iSCSI: target address %s, target name %s, SCSI LUN %lld\n", pImage->pszTargetAddress, pImage->pszTargetName, pImage->LUN));
2773 }
2774 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
2775 return rc;
2776}
2777
2778
2779/** @copydoc VBOXHDDBACKEND::pfnCreate */
2780static int iscsiCreate(const char *pszFilename, uint64_t cbSize,
2781 unsigned uImageFlags, const char *pszComment,
2782 PCPDMMEDIAGEOMETRY pPCHSGeometry,
2783 PCPDMMEDIAGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
2784 unsigned uOpenFlags, unsigned uPercentStart,
2785 unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
2786 PVDINTERFACE pVDIfsImage, PVDINTERFACE pVDIfsOperation,
2787 void **ppBackendData)
2788{
2789 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 ppBackendData=%#p", pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
2790 int rc = VERR_NOT_SUPPORTED;
2791
2792 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
2793 return rc;
2794}
2795
2796
2797/** @copydoc VBOXHDDBACKEND::pfnRename */
2798static int iscsiRename(void *pBackendData, const char *pszFilename)
2799{
2800 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
2801 int rc = VERR_NOT_SUPPORTED;
2802
2803 LogFlowFunc(("returns %Rrc\n", rc));
2804 return rc;
2805}
2806
2807
2808/** @copydoc VBOXHDDBACKEND::pfnClose */
2809static int iscsiClose(void *pBackendData, bool fDelete)
2810{
2811 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
2812 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
2813 int rc = VINF_SUCCESS;
2814
2815 Assert(!fDelete); /* This flag is unsupported. */
2816
2817 /* Freeing a never allocated image (e.g. because the open failed) is
2818 * not signalled as an error. After all nothing bad happens. */
2819 if (pImage)
2820 iscsiFreeImage(pImage, fDelete);
2821
2822 LogFlowFunc(("returns %Rrc\n", rc));
2823 return rc;
2824}
2825
2826
2827/** @copydoc VBOXHDDBACKEND::pfnRead */
2828static int iscsiRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
2829 size_t cbToRead, size_t *pcbActuallyRead)
2830{
2831 /** @todo reinstate logging of the target everywhere - dropped temporarily */
2832 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
2833 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
2834 uint64_t lba;
2835 uint16_t tls;
2836 int rc;
2837
2838 Assert(pImage);
2839 Assert(uOffset % 512 == 0);
2840 Assert(cbToRead % 512 == 0);
2841
2842 Assert(pImage->cbSector);
2843 AssertPtr(pvBuf);
2844
2845 if ( uOffset + cbToRead > pImage->cbSize
2846 || cbToRead == 0)
2847 {
2848 rc = VERR_INVALID_PARAMETER;
2849 goto out;
2850 }
2851
2852 /*
2853 * Clip read size to a value which is supported by the target.
2854 */
2855 cbToRead = RT_MIN(cbToRead, pImage->cbRecvDataLength);
2856
2857 lba = uOffset / pImage->cbSector;
2858 tls = (uint16_t)(cbToRead / pImage->cbSector);
2859 SCSIREQ sr;
2860 uint8_t cdb[10];
2861 uint8_t sense[32];
2862
2863 cdb[0] = SCSI_READ_10;
2864 cdb[1] = 0; /* reserved */
2865 cdb[2] = (lba >> 24) & 0xff;
2866 cdb[3] = (lba >> 16) & 0xff;
2867 cdb[4] = (lba >> 8) & 0xff;
2868 cdb[5] = lba & 0xff;
2869 cdb[6] = 0; /* reserved */
2870 cdb[7] = (tls >> 8) & 0xff;
2871 cdb[8] = tls & 0xff;
2872 cdb[9] = 0; /* control */
2873
2874 sr.enmXfer = SCSIXFER_FROM_TARGET;
2875 sr.cbCmd = sizeof(cdb);
2876 sr.pvCmd = cdb;
2877 sr.cbI2TData = 0;
2878 sr.pcvI2TData = NULL;
2879 sr.cbT2IData = cbToRead;
2880 sr.pvT2IData = pvBuf;
2881 sr.cbSense = sizeof(sense);
2882 sr.pvSense = sense;
2883
2884 for (unsigned i = 0; i < 10; i++)
2885 {
2886 rc = iscsiCommand(pImage, &sr);
2887 if ( (RT_SUCCESS(rc) && !sr.cbSense)
2888 || RT_FAILURE(rc))
2889 break;
2890 rc = VERR_READ_ERROR;
2891 }
2892 if (RT_FAILURE(rc))
2893 {
2894 AssertMsgFailed(("iscsiCommand(%s, %#llx) -> %Rrc\n", pImage->pszTargetName, uOffset, rc));
2895 *pcbActuallyRead = 0;
2896 }
2897 else
2898 *pcbActuallyRead = sr.cbT2IData;
2899
2900out:
2901 LogFlowFunc(("returns %Rrc\n", rc));
2902 return rc;
2903}
2904
2905
2906/** @copydoc VBOXHDDBACKEND::pfnWrite */
2907static int iscsiWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
2908 size_t cbToWrite, size_t *pcbWriteProcess,
2909 size_t *pcbPreRead, size_t *pcbPostRead, unsigned fWrite)
2910{
2911 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n", pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
2912 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
2913 uint64_t lba;
2914 uint16_t tls;
2915 int rc;
2916
2917 Assert(pImage);
2918 Assert(uOffset % 512 == 0);
2919 Assert(cbToWrite % 512 == 0);
2920
2921 Assert(pImage->cbSector);
2922 Assert(pvBuf);
2923
2924 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2925 {
2926 rc = VERR_VD_IMAGE_READ_ONLY;
2927 goto out;
2928 }
2929
2930 *pcbPreRead = 0;
2931 *pcbPostRead = 0;
2932
2933 /*
2934 * Clip write size to a value which is supported by the target.
2935 */
2936 cbToWrite = RT_MIN(cbToWrite, pImage->cbSendDataLength);
2937
2938 lba = uOffset / pImage->cbSector;
2939 tls = (uint16_t)(cbToWrite / pImage->cbSector);
2940 SCSIREQ sr;
2941 uint8_t cdb[10];
2942 uint8_t sense[32];
2943
2944 cdb[0] = SCSI_WRITE_10;
2945 cdb[1] = 0; /* reserved */
2946 cdb[2] = (lba >> 24) & 0xff;
2947 cdb[3] = (lba >> 16) & 0xff;
2948 cdb[4] = (lba >> 8) & 0xff;
2949 cdb[5] = lba & 0xff;
2950 cdb[6] = 0; /* reserved */
2951 cdb[7] = (tls >> 8) & 0xff;
2952 cdb[8] = tls & 0xff;
2953 cdb[9] = 0; /* control */
2954
2955 sr.enmXfer = SCSIXFER_TO_TARGET;
2956 sr.cbCmd = sizeof(cdb);
2957 sr.pvCmd = cdb;
2958 sr.cbI2TData = cbToWrite;
2959 sr.pcvI2TData = pvBuf;
2960 sr.cbT2IData = 0;
2961 sr.pvT2IData = NULL;
2962 sr.cbSense = sizeof(sense);
2963 sr.pvSense = sense;
2964
2965 for (unsigned i = 0; i < 10; i++)
2966 {
2967 rc = iscsiCommand(pImage, &sr);
2968 if ( (RT_SUCCESS(rc) && !sr.cbSense)
2969 || RT_FAILURE(rc))
2970 break;
2971 rc = VERR_WRITE_ERROR;
2972 }
2973 if (RT_FAILURE(rc))
2974 {
2975 AssertMsgFailed(("iscsiCommand(%s, %#llx) -> %Rrc\n", pImage->pszTargetName, uOffset, rc));
2976 *pcbWriteProcess = 0;
2977 }
2978 else
2979 *pcbWriteProcess = cbToWrite;
2980
2981out:
2982 LogFlowFunc(("returns %Rrc\n", rc));
2983 return rc;
2984}
2985
2986
2987/** @copydoc VBOXHDDBACKEND::pfnFlush */
2988static int iscsiFlush(void *pBackendData)
2989{
2990 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2991 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
2992 int rc;
2993
2994 Assert(pImage);
2995
2996 SCSIREQ sr;
2997 uint8_t cdb[10];
2998 uint8_t sense[32];
2999
3000 cdb[0] = SCSI_SYNCHRONIZE_CACHE;
3001 cdb[1] = 0; /* reserved */
3002 cdb[2] = 0; /* LBA 0 */
3003 cdb[3] = 0; /* LBA 0 */
3004 cdb[4] = 0; /* LBA 0 */
3005 cdb[5] = 0; /* LBA 0 */
3006 cdb[6] = 0; /* reserved */
3007 cdb[7] = 0; /* transfer everything to disk */
3008 cdb[8] = 0; /* transfer everything to disk */
3009 cdb[9] = 0; /* control */
3010
3011 sr.enmXfer = SCSIXFER_TO_TARGET;
3012 sr.cbCmd = sizeof(cdb);
3013 sr.pvCmd = cdb;
3014 sr.cbI2TData = 0;
3015 sr.pcvI2TData = NULL;
3016 sr.cbT2IData = 0;
3017 sr.pvT2IData = NULL;
3018 sr.cbSense = sizeof(sense);
3019 sr.pvSense = sense;
3020
3021 rc = iscsiCommand(pImage, &sr);
3022 if (RT_FAILURE(rc))
3023 AssertMsgFailed(("iscsiCommand(%s) -> %Rrc\n", pImage->pszTargetName, rc));
3024 LogFlowFunc(("returns %Rrc\n", rc));
3025 return rc;
3026}
3027
3028
3029/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
3030static unsigned iscsiGetVersion(void *pBackendData)
3031{
3032 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
3033 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3034
3035 Assert(pImage);
3036 NOREF(pImage);
3037
3038 return 0;
3039}
3040
3041
3042/** @copydoc VBOXHDDBACKEND::pfnGetSize */
3043static uint64_t iscsiGetSize(void *pBackendData)
3044{
3045 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
3046 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3047
3048 Assert(pImage);
3049
3050 if (pImage)
3051 return pImage->cbSize;
3052 else
3053 return 0;
3054}
3055
3056
3057/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
3058static uint64_t iscsiGetFileSize(void *pBackendData)
3059{
3060 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
3061 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3062
3063 Assert(pImage);
3064 NOREF(pImage);
3065
3066 if (pImage)
3067 return pImage->cbSize;
3068 else
3069 return 0;
3070}
3071
3072
3073/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
3074static int iscsiGetPCHSGeometry(void *pBackendData,
3075 PPDMMEDIAGEOMETRY pPCHSGeometry)
3076{
3077 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
3078 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3079 int rc;
3080
3081 Assert(pImage);
3082
3083 if (pImage)
3084 rc = VERR_VD_GEOMETRY_NOT_SET;
3085 else
3086 rc = VERR_VD_NOT_OPENED;
3087
3088 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
3089 return rc;
3090}
3091
3092
3093/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
3094static int iscsiSetPCHSGeometry(void *pBackendData,
3095 PCPDMMEDIAGEOMETRY pPCHSGeometry)
3096{
3097 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
3098 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3099 int rc;
3100
3101 Assert(pImage);
3102
3103 if (pImage)
3104 {
3105 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3106 {
3107 rc = VERR_VD_IMAGE_READ_ONLY;
3108 goto out;
3109 }
3110 rc = VERR_VD_GEOMETRY_NOT_SET;
3111 }
3112 else
3113 rc = VERR_VD_NOT_OPENED;
3114
3115out:
3116 LogFlowFunc(("returns %Rrc\n", rc));
3117 return rc;
3118}
3119
3120
3121/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
3122static int iscsiGetLCHSGeometry(void *pBackendData,
3123 PPDMMEDIAGEOMETRY pLCHSGeometry)
3124{
3125 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
3126 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3127 int rc;
3128
3129 Assert(pImage);
3130
3131 if (pImage)
3132 rc = VERR_VD_GEOMETRY_NOT_SET;
3133 else
3134 rc = VERR_VD_NOT_OPENED;
3135
3136 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
3137 return rc;
3138}
3139
3140
3141/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
3142static unsigned iscsiGetImageFlags(void *pBackendData)
3143{
3144 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
3145 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3146 unsigned uImageFlags;
3147
3148 Assert(pImage);
3149 NOREF(pImage);
3150
3151 uImageFlags = VD_IMAGE_FLAGS_FIXED;
3152
3153 LogFlowFunc(("returns %#x\n", uImageFlags));
3154 return uImageFlags;
3155}
3156
3157
3158/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
3159static unsigned iscsiGetOpenFlags(void *pBackendData)
3160{
3161 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
3162 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3163 unsigned uOpenFlags;
3164
3165 Assert(pImage);
3166
3167 if (pImage)
3168 uOpenFlags = pImage->uOpenFlags;
3169 else
3170 uOpenFlags = 0;
3171
3172 LogFlowFunc(("returns %#x\n", uOpenFlags));
3173 return uOpenFlags;
3174}
3175
3176
3177/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
3178static int iscsiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
3179{
3180 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
3181 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3182 int rc;
3183
3184 /* Image must be opened and the new flags must be valid. Just readonly and
3185 * info flags are supported. */
3186 if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO)))
3187 {
3188 rc = VERR_INVALID_PARAMETER;
3189 goto out;
3190 }
3191
3192 /* Implement this operation via reopening the image. */
3193 iscsiFreeImage(pImage, false);
3194 rc = iscsiOpenImage(pImage, uOpenFlags);
3195
3196out:
3197 LogFlowFunc(("returns %Rrc\n", rc));
3198 return rc;
3199}
3200
3201
3202/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
3203static int iscsiSetLCHSGeometry(void *pBackendData,
3204 PCPDMMEDIAGEOMETRY pLCHSGeometry)
3205{
3206 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
3207 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3208 int rc;
3209
3210 Assert(pImage);
3211
3212 if (pImage)
3213 {
3214 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3215 {
3216 rc = VERR_VD_IMAGE_READ_ONLY;
3217 goto out;
3218 }
3219 rc = VERR_VD_GEOMETRY_NOT_SET;
3220 }
3221 else
3222 rc = VERR_VD_NOT_OPENED;
3223
3224out:
3225 LogFlowFunc(("returns %Rrc\n", rc));
3226 return rc;
3227}
3228
3229
3230/** @copydoc VBOXHDDBACKEND::pfnGetComment */
3231static int iscsiGetComment(void *pBackendData, char *pszComment,
3232 size_t cbComment)
3233{
3234 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
3235 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3236 int rc;
3237
3238 Assert(pImage);
3239
3240 if (pImage)
3241 rc = VERR_NOT_SUPPORTED;
3242 else
3243 rc = VERR_VD_NOT_OPENED;
3244
3245 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
3246 return rc;
3247}
3248
3249
3250/** @copydoc VBOXHDDBACKEND::pfnSetComment */
3251static int iscsiSetComment(void *pBackendData, const char *pszComment)
3252{
3253 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
3254 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3255 int rc;
3256
3257 Assert(pImage);
3258
3259 if (pImage)
3260 {
3261 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3262 rc = VERR_NOT_SUPPORTED;
3263 else
3264 rc = VERR_VD_IMAGE_READ_ONLY;
3265 }
3266 else
3267 rc = VERR_VD_NOT_OPENED;
3268
3269 LogFlowFunc(("returns %Rrc\n", rc));
3270 return rc;
3271}
3272
3273
3274/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
3275static int iscsiGetUuid(void *pBackendData, PRTUUID pUuid)
3276{
3277 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
3278 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3279 int rc;
3280
3281 Assert(pImage);
3282
3283 if (pImage)
3284 rc = VERR_NOT_SUPPORTED;
3285 else
3286 rc = VERR_VD_NOT_OPENED;
3287
3288 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
3289 return rc;
3290}
3291
3292
3293/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
3294static int iscsiSetUuid(void *pBackendData, PCRTUUID pUuid)
3295{
3296 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
3297 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3298 int rc;
3299
3300 LogFlowFunc(("%RTuuid\n", pUuid));
3301 Assert(pImage);
3302
3303 if (pImage)
3304 {
3305 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3306 rc = VERR_NOT_SUPPORTED;
3307 else
3308 rc = VERR_VD_IMAGE_READ_ONLY;
3309 }
3310 else
3311 rc = VERR_VD_NOT_OPENED;
3312
3313 LogFlowFunc(("returns %Rrc\n", rc));
3314 return rc;
3315}
3316
3317
3318/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
3319static int iscsiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
3320{
3321 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
3322 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3323 int rc;
3324
3325 Assert(pImage);
3326
3327 if (pImage)
3328 rc = VERR_NOT_SUPPORTED;
3329 else
3330 rc = VERR_VD_NOT_OPENED;
3331
3332 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
3333 return rc;
3334}
3335
3336
3337/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
3338static int iscsiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
3339{
3340 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
3341 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3342 int rc;
3343
3344 LogFlowFunc(("%RTuuid\n", pUuid));
3345 Assert(pImage);
3346
3347 if (pImage)
3348 {
3349 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3350 rc = VERR_NOT_SUPPORTED;
3351 else
3352 rc = VERR_VD_IMAGE_READ_ONLY;
3353 }
3354 else
3355 rc = VERR_VD_NOT_OPENED;
3356
3357 LogFlowFunc(("returns %Rrc\n", rc));
3358 return rc;
3359}
3360
3361
3362/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
3363static int iscsiGetParentUuid(void *pBackendData, PRTUUID pUuid)
3364{
3365 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
3366 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3367 int rc;
3368
3369 Assert(pImage);
3370
3371 if (pImage)
3372 rc = VERR_NOT_SUPPORTED;
3373 else
3374 rc = VERR_VD_NOT_OPENED;
3375
3376 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
3377 return rc;
3378}
3379
3380
3381/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
3382static int iscsiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
3383{
3384 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
3385 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3386 int rc;
3387
3388 LogFlowFunc(("%RTuuid\n", pUuid));
3389 Assert(pImage);
3390
3391 if (pImage)
3392 {
3393 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3394 rc = VERR_NOT_SUPPORTED;
3395 else
3396 rc = VERR_VD_IMAGE_READ_ONLY;
3397 }
3398 else
3399 rc = VERR_VD_NOT_OPENED;
3400
3401 LogFlowFunc(("returns %Rrc\n", rc));
3402 return rc;
3403}
3404
3405
3406/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
3407static int iscsiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
3408{
3409 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
3410 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3411 int rc;
3412
3413 Assert(pImage);
3414
3415 if (pImage)
3416 rc = VERR_NOT_SUPPORTED;
3417 else
3418 rc = VERR_VD_NOT_OPENED;
3419
3420 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
3421 return rc;
3422}
3423
3424
3425/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
3426static int iscsiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
3427{
3428 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
3429 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3430 int rc;
3431
3432 LogFlowFunc(("%RTuuid\n", pUuid));
3433 Assert(pImage);
3434
3435 if (pImage)
3436 {
3437 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3438 rc = VERR_NOT_SUPPORTED;
3439 else
3440 rc = VERR_VD_IMAGE_READ_ONLY;
3441 }
3442 else
3443 rc = VERR_VD_NOT_OPENED;
3444
3445 LogFlowFunc(("returns %Rrc\n", rc));
3446 return rc;
3447}
3448
3449
3450/** @copydoc VBOXHDDBACKEND::pfnDump */
3451static void iscsiDump(void *pBackendData)
3452{
3453 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3454
3455 Assert(pImage);
3456 if (pImage)
3457 {
3458 /** @todo put something useful here */
3459 pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: cVolume=%u\n", pImage->cVolume);
3460 }
3461}
3462
3463
3464/** @copydoc VBOXHDDBACKEND::pfnGetTimeStamp */
3465static int iscsiGetTimeStamp(void *pBackendData, PRTTIMESPEC pTimeStamp)
3466{
3467 LogFlowFunc(("pBackendData=%#p pTimeStamp=%#p\n", pBackendData, pTimeStamp));
3468 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3469 int rc = VERR_NOT_SUPPORTED;
3470
3471 Assert(pImage);
3472 NOREF(pImage);
3473
3474 LogFlowFunc(("returns %Rrc\n", rc));
3475 return rc;
3476}
3477
3478
3479/** @copydoc VBOXHDDBACKEND::pfnGetParentTimeStamp */
3480static int iscsiGetParentTimeStamp(void *pBackendData, PRTTIMESPEC pTimeStamp)
3481{
3482 LogFlowFunc(("pBackendData=%#p pTimeStamp=%#p\n", pBackendData, pTimeStamp));
3483 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3484 int rc = VERR_NOT_SUPPORTED;
3485
3486 Assert(pImage);
3487 NOREF(pImage);
3488
3489 LogFlowFunc(("returns %Rrc\n", rc));
3490 return rc;
3491}
3492
3493
3494/** @copydoc VBOXHDDBACKEND::pfnSetParentTimeStamp */
3495static int iscsiSetParentTimeStamp(void *pBackendData, PCRTTIMESPEC pTimeStamp)
3496{
3497 LogFlowFunc(("pBackendData=%#p pTimeStamp=%#p\n", pBackendData, pTimeStamp));
3498 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3499 int rc = VERR_NOT_SUPPORTED;
3500
3501 Assert(pImage);
3502 NOREF(pImage);
3503
3504 LogFlowFunc(("returns %Rrc\n", rc));
3505 return rc;
3506}
3507
3508
3509/** @copydoc VBOXHDDBACKEND::pfnGetParentFilename */
3510static int iscsiGetParentFilename(void *pBackendData, char **ppszParentFilename)
3511{
3512 LogFlowFunc(("pBackendData=%#p ppszParentFilename=%#p\n", pBackendData, ppszParentFilename));
3513 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3514 int rc = VERR_NOT_SUPPORTED;
3515
3516 Assert(pImage);
3517 NOREF(pImage);
3518
3519 LogFlowFunc(("returns %Rrc\n", rc));
3520 return rc;
3521}
3522
3523
3524/** @copydoc VBOXHDDBACKEND::pfnSetParentFilename */
3525static int iscsiSetParentFilename(void *pBackendData, const char *pszParentFilename)
3526{
3527 LogFlowFunc(("pBackendData=%#p pszParentFilename=%s\n", pBackendData, pszParentFilename));
3528 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
3529 int rc = VERR_NOT_SUPPORTED;
3530
3531 Assert(pImage);
3532 NOREF(pImage);
3533
3534 LogFlowFunc(("returns %Rrc\n", rc));
3535 return rc;
3536}
3537
3538/** @copydoc VBOXHDDBACKEND::pfnComposeLocation */
3539static int iscsiComposeLocation(PVDINTERFACE pConfig, char **pszLocation)
3540{
3541 char *pszTarget = NULL;
3542 char *pszLUN = NULL;
3543 char *pszAddress = NULL;
3544 int rc = VDCFGQueryStringAlloc(VDGetInterfaceConfig(pConfig), pConfig->pvUser, "TargetName", &pszTarget);
3545 if (RT_SUCCESS(rc))
3546 {
3547 rc = VDCFGQueryStringAlloc(VDGetInterfaceConfig(pConfig), pConfig->pvUser, "LUN", &pszLUN);
3548 if (RT_SUCCESS(rc))
3549 {
3550 rc = VDCFGQueryStringAlloc(VDGetInterfaceConfig(pConfig), pConfig->pvUser, "TargetAddress", &pszAddress);
3551 if (RT_SUCCESS(rc))
3552 {
3553 if (RTStrAPrintf(pszLocation, "iscsi://%s/%s/%s",
3554 pszAddress, pszTarget, pszLUN) < 0)
3555 rc = VERR_NO_MEMORY;
3556 }
3557 }
3558 }
3559 RTMemFree(pszTarget);
3560 RTMemFree(pszLUN);
3561 RTMemFree(pszAddress);
3562 return rc;
3563}
3564
3565/** @copydoc VBOXHDDBACKEND::pfnComposeName */
3566static int iscsiComposeName(PVDINTERFACE pConfig, char **pszName)
3567{
3568 char *pszTarget = NULL;
3569 char *pszLUN = NULL;
3570 char *pszAddress = NULL;
3571 int rc = VDCFGQueryStringAlloc(VDGetInterfaceConfig(pConfig), pConfig->pvUser, "TargetName", &pszTarget);
3572 if (RT_SUCCESS(rc))
3573 {
3574 rc = VDCFGQueryStringAlloc(VDGetInterfaceConfig(pConfig), pConfig->pvUser, "LUN", &pszLUN);
3575 if (RT_SUCCESS(rc))
3576 {
3577 rc = VDCFGQueryStringAlloc(VDGetInterfaceConfig(pConfig), pConfig->pvUser, "TargetAddress", &pszAddress);
3578 if (RT_SUCCESS(rc))
3579 {
3580 /** @todo think about a nicer looking location scheme for iSCSI */
3581 if (RTStrAPrintf(pszName, "%s/%s/%s",
3582 pszAddress, pszTarget, pszLUN) < 0)
3583 rc = VERR_NO_MEMORY;
3584 }
3585 }
3586 }
3587 RTMemFree(pszTarget);
3588 RTMemFree(pszLUN);
3589 RTMemFree(pszAddress);
3590
3591 return rc;
3592}
3593
3594
3595VBOXHDDBACKEND g_ISCSIBackend =
3596{
3597 /* pszBackendName */
3598 "iSCSI",
3599 /* cbSize */
3600 sizeof(VBOXHDDBACKEND),
3601 /* uBackendCaps */
3602 VD_CAP_CONFIG | VD_CAP_TCPNET,
3603 /* papszFileExtensions */
3604 NULL,
3605 /* paConfigInfo */
3606 s_iscsiConfigInfo,
3607 /* hPlugin */
3608 NIL_RTLDRMOD,
3609 /* pfnCheckIfValid */
3610 iscsiCheckIfValid,
3611 /* pfnOpen */
3612 iscsiOpen,
3613 /* pfnCreate */
3614 iscsiCreate,
3615 /* pfnRename */
3616 iscsiRename,
3617 /* pfnClose */
3618 iscsiClose,
3619 /* pfnRead */
3620 iscsiRead,
3621 /* pfnWrite */
3622 iscsiWrite,
3623 /* pfnFlush */
3624 iscsiFlush,
3625 /* pfnGetVersion */
3626 iscsiGetVersion,
3627 /* pfnGetSize */
3628 iscsiGetSize,
3629 /* pfnGetFileSize */
3630 iscsiGetFileSize,
3631 /* pfnGetPCHSGeometry */
3632 iscsiGetPCHSGeometry,
3633 /* pfnSetPCHSGeometry */
3634 iscsiSetPCHSGeometry,
3635 /* pfnGetLCHSGeometry */
3636 iscsiGetLCHSGeometry,
3637 /* pfnSetLCHSGeometry */
3638 iscsiSetLCHSGeometry,
3639 /* pfnGetImageFlags */
3640 iscsiGetImageFlags,
3641 /* pfnGetOpenFlags */
3642 iscsiGetOpenFlags,
3643 /* pfnSetOpenFlags */
3644 iscsiSetOpenFlags,
3645 /* pfnGetComment */
3646 iscsiGetComment,
3647 /* pfnSetComment */
3648 iscsiSetComment,
3649 /* pfnGetUuid */
3650 iscsiGetUuid,
3651 /* pfnSetUuid */
3652 iscsiSetUuid,
3653 /* pfnGetModificationUuid */
3654 iscsiGetModificationUuid,
3655 /* pfnSetModificationUuid */
3656 iscsiSetModificationUuid,
3657 /* pfnGetParentUuid */
3658 iscsiGetParentUuid,
3659 /* pfnSetParentUuid */
3660 iscsiSetParentUuid,
3661 /* pfnGetParentModificationUuid */
3662 iscsiGetParentModificationUuid,
3663 /* pfnSetParentModificationUuid */
3664 iscsiSetParentModificationUuid,
3665 /* pfnDump */
3666 iscsiDump,
3667 /* pfnGetTimeStamp */
3668 iscsiGetTimeStamp,
3669 /* pfnGetParentTimeStamp */
3670 iscsiGetParentTimeStamp,
3671 /* pfnSetParentTimeStamp */
3672 iscsiSetParentTimeStamp,
3673 /* pfnGetParentFilename */
3674 iscsiGetParentFilename,
3675 /* pfnSetParentFilename */
3676 iscsiSetParentFilename,
3677 /* pfnIsAsyncIOSupported */
3678 NULL,
3679 /* pfnAsyncRead */
3680 NULL,
3681 /* pfnAsyncWrite */
3682 NULL,
3683 /* pfnComposeLocation */
3684 iscsiComposeLocation,
3685 /* pfnComposeName */
3686 iscsiComposeName
3687};
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette