VirtualBox

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

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

Storage/iSCSI: Handle target triggered disconnects properly, without losing requests sporadically. Additionally eliminate some annoying assertions and clean up logging.

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

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