VirtualBox

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

Last change on this file since 24178 was 24178, checked in by vboxsync, 15 years ago

ISCSIHDDCore.cpp: Fixed regression from >2TB target change - check the status and not just the iscsiCommand return value. netbsd-iscsi target hack (disabled).

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