VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

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

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