/*-
* Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "iscsi_globals.h"
/*
* my_soo_read:
* Replacement for soo_read with flag handling.
*
* Parameter:
* conn The connection
* u The uio descriptor
* flags Read flags
*
* Returns: 0 on success, else 1
*/
STATIC int
my_soo_read(connection_t *conn, struct uio *u, int flags)
{
struct socket *so;
int ret;
#ifdef ISCSI_DEBUG
size_t resid = u->uio_resid;
#endif
DEBC(conn, 99, ("soo_read req: %zu\n", resid));
rw_enter(&conn->c_sock_rw, RW_READER);
if (conn->c_sock == NULL) {
ret = EIO;
} else {
so = conn->c_sock->f_socket;
if (flags & MSG_WAITALL) {
flags &= ~MSG_WAITALL;
do {
int oresid = u->uio_resid;
ret = (*so->so_receive)(so, NULL, u,
NULL, NULL, &flags);
if (!ret && u->uio_resid == oresid)
break;
} while (!ret && u->uio_resid > 0);
} else {
ret = (*so->so_receive)(so, NULL, u,
NULL, NULL, &flags);
}
}
/*
* try_resynch_receive:
* Skip over everything in the socket's receive buffer, in the hope of
* ending up at the start of a new PDU.
*
* Parameter:
* conn The connection
*/
/*
* ccb_from_itt
* Translate ITT into CCB pointer.
*
* Parameter:
* conn The connection
* itt The Initiator Task Tag
*
* Returns:
* Pointer to CCB, or NULL if ITT is not a valid CCB index.
*/
cidx = itt & 0xff;
if (cidx >= CCBS_PER_SESSION)
return NULL;
ccb = &conn->c_session->s_ccb[cidx];
if (ccb->ccb_ITT != itt) {
DEBC(conn, 0,
("ccb_from_itt: received invalid CCB itt %08x != %08x\n",
itt, ccb->ccb_ITT));
return NULL;
}
if (ccb->ccb_disp <= CCBDISP_BUSY) {
DEBC(conn, 0,
("ccb_from_itt: received CCB with invalid disp %d\n",
ccb->ccb_disp));
return NULL;
}
return ccb;
}
/*
* read_pdu_data:
* Initialize the uio structure for receiving everything after the
* header, including data (if present), and padding. Read the data.
*
* Parameter:
* pdu The PDU
* data Pointer to data (may be NULL for auto-allocation)
* offset The offset into the data pointer
*
* Returns: 0 on success
* 1 if an error occurs during read
* -1 if the data digest was incorrect (PDU must be ignored)
*/
STATIC int
read_pdu_data(pdu_t *pdu, uint8_t *data, uint32_t offset)
{
static uint8_t pad_bytes[4];
uint32_t len, digest;
struct uio *uio;
int i, pad;
connection_t *conn = pdu->pdu_connection;
DEB(15, ("read_pdu_data: data segment length = %d\n",
ntoh3(pdu->pdu_hdr.pduh_DataSegmentLength)));
if (!(len = ntoh3(pdu->pdu_hdr.pduh_DataSegmentLength))) {
return 0;
}
pad = len & 0x03;
if (pad) {
pad = 4 - pad;
}
KASSERT(data != NULL || offset == 0);
if (data == NULL) {
/*
* NOTE: Always allocate 2 extra bytes when reading temp data,
* since temp data is mostly used for received text, and we can
* make sure there's a double zero at the end of the data to mark EOF.
*/
if ((data = (uint8_t *) malloc(len + 2, M_TEMP, M_WAITOK)) == NULL) {
DEBOUT(("ran out of mem on receive\n"));
handle_connection_error(pdu->pdu_connection,
ISCSI_STATUS_NO_RESOURCES, LOGOUT_SESSION);
return 1;
}
pdu->pdu_temp_data = data;
pdu->pdu_temp_data_len = len;
}
pdu->pdu_io_vec[0].iov_base = data + offset;
pdu->pdu_io_vec[0].iov_len = len;
if (conn->c_DataDigest) {
i = uio->uio_iovcnt++;
pdu->pdu_io_vec[i].iov_base = &pdu->pdu_data_digest;
pdu->pdu_io_vec[i].iov_len = 4;
uio->uio_resid += 4;
}
/* get the data */
if (my_soo_read(conn, &pdu->pdu_uio, MSG_WAITALL) != 0) {
return 1;
}
if (conn->c_DataDigest) {
digest = gen_digest_2(data, len, pad_bytes, pad);
if (digest != pdu->pdu_data_digest) {
DEBOUT(("Data Digest Error: comp = %08x, rx = %08x\n",
digest, pdu->pdu_data_digest));
switch (pdu->pdu_hdr.pduh_Opcode & OPCODE_MASK) {
case TOP_SCSI_Response:
case TOP_Text_Response:
send_snack(pdu->pdu_connection, pdu, NULL, SNACK_STATUS_NAK);
break;
case TOP_SCSI_Data_in:
send_snack(pdu->pdu_connection, pdu, NULL, SNACK_DATA_NAK);
break;
/*
* collect_text_data
* Handle text continuation in login and text response PDUs
*
* Parameter:
* pdu The received PDU
* req_CCB The CCB associated with the original request
*
* Returns: -1 if continue flag is set
* 0 if text is complete
* +1 if an error occurred (out of resources)
*/
STATIC int
collect_text_data(pdu_t *pdu, ccb_t *req_ccb)
{
if (req_ccb->ccb_text_data) {
int nlen;
uint8_t *newp;
nlen = req_ccb->ccb_text_len + pdu->pdu_temp_data_len;
/* Note: allocate extra 2 bytes for text terminator */
if ((newp = malloc(nlen + 2, M_TEMP, M_WAITOK)) == NULL) {
DEBOUT(("Collect Text Data: Out of Memory, ccb = %p\n", req_ccb));
req_ccb->ccb_status = ISCSI_STATUS_NO_RESOURCES;
/* XXX where is CCB freed? */
return 1;
}
memcpy(newp, req_ccb->ccb_text_data, req_ccb->ccb_text_len);
memcpy(&newp[req_ccb->ccb_text_len], pdu->pdu_temp_data, pdu->pdu_temp_data_len);
/*
* check_StatSN
* Check received vs. expected StatSN
*
* Parameter:
* conn The connection
* nw_sn The received StatSN in network byte order
* ack Acknowledge this SN if TRUE
*/
STATIC int
check_StatSN(connection_t *conn, uint32_t nw_sn, bool ack)
{
int rc;
uint32_t sn = ntohl(nw_sn);
/*
* The target can respond to a NOP-In before subsequent
* commands are processed. So our CmdSN can exceed the
* returned ExpCmdSN by the number of commands that are
* in flight. Adjust the expected value accordingly.
*/
sn++;
}
}
/*
* receive_login_pdu
* Handle receipt of a login response PDU.
*
* Parameter:
* conn The connection
* pdu The PDU
* req_CCB The CCB associated with the original request (if any)
*/
STATIC int
receive_login_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
{
int rc;
if (req_ccb == NULL) {
/* Duplicate?? */
DEBOUT(("Received duplicate login response (no associated CCB)\n"));
return -1;
}
if (pdu->pdu_hdr.pduh_p.login_rsp.StatusClass) {
DEBC(conn, 1, ("Login problem - Class = %x, Detail = %x\n",
pdu->pdu_hdr.pduh_p.login_rsp.StatusClass,
pdu->pdu_hdr.pduh_p.login_rsp.StatusDetail));
wake_ccb(req_ccb, ISCSI_STATUS_LOGIN_FAILED);
return 0;
}
if (!conn->c_StatSN_buf.next_sn) {
conn->c_StatSN_buf.next_sn = conn->c_StatSN_buf.ExpSN =
ntohl(pdu->pdu_hdr.pduh_p.login_rsp.StatSN) + 1;
} else if (check_StatSN(conn, pdu->pdu_hdr.pduh_p.login_rsp.StatSN, TRUE))
return -1;
if (pdu->pdu_temp_data_len) {
if ((rc = collect_text_data(pdu, req_ccb)) != 0)
return max(rc, 0);
}
negotiate_login(conn, pdu, req_ccb);
/* negotiate_login will decide whether login is complete or not */
return 0;
}
/*
* receive_text_response_pdu
* Handle receipt of a text response PDU.
*
* Parameter:
* conn The connection
* pdu The PDU
* req_CCB The CCB associated with the original request (if any)
*/
STATIC int
receive_text_response_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
{
int rc;
DEBC(conn, 9, ("Received Text Response PDU, op=%x, flags=%x\n",
pdu->pdu_hdr.pduh_Opcode, pdu->pdu_hdr.pduh_Flags));
if (check_StatSN(conn, pdu->pdu_hdr.pduh_p.text_rsp.StatSN, TRUE)) {
return -1;
}
if (req_ccb == NULL) {
DEBOUT(("Received unsolicited text response\n"));
handle_connection_error(conn, ISCSI_STATUS_TARGET_ERROR,
LOGOUT_CONNECTION);
return -1;
}
/*
* receive_logout_pdu
* Handle receipt of a logout response PDU.
*
* Parameter:
* conn The connection
* pdu The PDU
* req_CCB The CCB associated with the original request (if any)
*/
if (otherconn && check_StatSN(conn, pdu->pdu_hdr.pduh_p.logout_rsp.StatSN, TRUE))
return -1;
switch (response) {
case 0:
status = ISCSI_STATUS_SUCCESS;
break;
case 1:
status = ISCSI_STATUS_LOGOUT_CID_NOT_FOUND;
break;
case 2:
status = ISCSI_STATUS_LOGOUT_RECOVERY_NS;
break;
default:
status = ISCSI_STATUS_LOGOUT_ERROR;
break;
}
/* let send thread take over next step of cleanup */
mutex_enter(&conn->c_lock);
cv_broadcast(&conn->c_conn_cv);
}
mutex_exit(&conn->c_lock);
return !otherconn;
}
/*
* receive_data_in_pdu
* Handle receipt of a data in PDU.
*
* Parameter:
* conn The connection
* pdu The PDU
* req_CCB The CCB associated with the original request (if any)
*/
STATIC int
receive_data_in_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
{
uint32_t dsl, sn;
bool done;
int rc;
dsl = ntoh3(pdu->pdu_hdr.pduh_DataSegmentLength);
if (req_ccb == NULL || !req_ccb->ccb_data_in || !req_ccb->ccb_data_len) {
DEBOUT(("Received Data In, but req_ccb not waiting for it, ignored\n"));
return 0;
}
req_ccb->ccb_flags |= CCBF_GOT_RSP;
if (done)
wake_ccb(req_ccb, ISCSI_STATUS_SUCCESS);
if (check_StatSN(conn, pdu->pdu_hdr.pduh_p.data_in.StatSN, done))
return -1;
} else if (done && (req_ccb->ccb_flags & CCBF_COMPLETE)) {
wake_ccb(req_ccb, ISCSI_STATUS_SUCCESS);
}
/* else wait for command response */
return 0;
}
/*
* receive_r2t_pdu
* Handle receipt of a R2T PDU.
*
* Parameter:
* conn The connection
* pdu The PDU
* req_CCB The CCB associated with the original request (if any)
*/
STATIC int
receive_r2t_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
{
/*
* receive_command_response_pdu
* Handle receipt of a command response PDU.
*
* Parameter:
* conn The connection
* pdu The PDU
* req_CCB The CCB associated with the original request (if any)
*/
STATIC int
receive_command_response_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
{
int len, rc;
bool done;
uint32_t status;
/* Read any provided data */
if (pdu->pdu_temp_data_len && req_ccb != NULL && req_ccb->ccb_sense_len_req) {
len = min(req_ccb->ccb_sense_len_req,
ntohs(*((uint16_t *) pdu->pdu_temp_data)));
memcpy(req_ccb->ccb_sense_ptr, ((uint16_t *) pdu->pdu_temp_data) + 1,
len);
req_ccb->ccb_sense_len_got = len;
}
if (req_ccb == NULL) {
/* Assume duplicate... */
DEBOUT(("Possibly duplicate command response (no associated CCB)\n"));
return -1;
}
if (req_ccb->ccb_flags & CCBF_COMPLETE) {
DEBOUT(("Possibly duplicate command response (tagged as COMPLETE)\n"));
return -1;
}
if (pdu->pdu_hdr.pduh_OpcodeSpecific[0]) { /* Response */
status = ISCSI_STATUS_TARGET_FAILURE;
} else {
switch (pdu->pdu_hdr.pduh_OpcodeSpecific[1]) { /* Status */
case 0x00:
status = ISCSI_STATUS_SUCCESS;
break;
case 0x02:
status = ISCSI_STATUS_CHECK_CONDITION;
break;
case 0x08:
status = ISCSI_STATUS_TARGET_BUSY;
break;
default:
status = ISCSI_STATUS_TARGET_ERROR;
break;
}
}
if (pdu->pdu_hdr.pduh_Flags & (FLAG_OVERFLOW | FLAG_UNDERFLOW))
req_ccb->ccb_residual = ntohl(pdu->pdu_hdr.pduh_p.response.ResidualCount);
done = status || sn_empty(&req_ccb->ccb_DataSN_buf);
/*
* receive_task_management_pdu
* Handle receipt of a task management PDU.
*
* Parameter:
* conn The connection
* pdu The PDU
* req_CCB The CCB associated with the original request (if any)
*/
if (req_ccb != NULL) {
switch (pdu->pdu_hdr.pduh_OpcodeSpecific[0]) { /* Response */
case 0:
status = ISCSI_STATUS_SUCCESS;
break;
case 1:
status = ISCSI_STATUS_TASK_NOT_FOUND;
break;
case 2:
status = ISCSI_STATUS_LUN_NOT_FOUND;
break;
case 3:
status = ISCSI_STATUS_TASK_ALLEGIANT;
break;
case 4:
status = ISCSI_STATUS_CANT_REASSIGN;
break;
case 5:
status = ISCSI_STATUS_FUNCTION_UNSUPPORTED;
break;
case 6:
status = ISCSI_STATUS_FUNCTION_NOT_AUTHORIZED;
break;
case 255:
status = ISCSI_STATUS_FUNCTION_REJECTED;
break;
default:
status = ISCSI_STATUS_UNKNOWN_REASON;
break;
}
wake_ccb(req_ccb, status);
}
/*
* receive_nop_in_pdu
* Handle receipt of a Nop-In PDU.
*
* Parameter:
* conn The connection
* pdu The PDU
* req_CCB The CCB associated with the original request (if any)
*/
if (pdu->pdu_hdr.pduh_InitiatorTaskTag == 0xffffffff) {
/* this is a target ping - respond with a pong */
if (pdu->pdu_hdr.pduh_p.nop_in.TargetTransferTag != 0xffffffff)
send_nop_out(conn, pdu);
/*
Any receive resets the connection timeout, but we got a ping, which
means that it's likely the other side was waiting for something to
happen on the connection. If we aren't idle, send a ping right
away to synch counters (don't synch on this ping because other
PDUs may be on the way).
*/
if (TAILQ_FIRST(&conn->c_ccbs_waiting) != NULL)
send_nop_out(conn, NULL);
} else if (req_ccb != NULL) {
/* this is a solicited ping, check CmdSN for lost commands */
/* and advance StatSN */
check_CmdSN(conn, pdu->pdu_hdr.pduh_p.nop_in.ExpCmdSN);
/*
* receive_pdu
* Get parameters, call the appropriate handler for a received PDU.
*
* Parameter:
* conn The connection
* pdu The PDU
*
* Returns: 0 on success, nonzero if the connection is broken.
*/
STATIC int
receive_pdu(connection_t *conn, pdu_t *pdu)
{
ccb_t *req_ccb;
int rc;
uint32_t MaxCmdSN, ExpCmdSN, digest;
session_t *sess = conn->c_session;
if (conn->c_HeaderDigest) {
digest = gen_digest(&pdu->pdu_hdr, BHS_SIZE);
if (digest != pdu->pdu_hdr.pduh_HeaderDigest) {
DEBOUT(("Header Digest Error: comp = %08x, rx = %08x\n",
digest, pdu->pdu_hdr.pduh_HeaderDigest));
/* try to skip to next PDU */
try_resynch_receive(conn);
free_pdu(pdu);
return 0;
}
}
/* MaxCmdSN and ExpCmdSN are in the same place in all received PDUs */
ExpCmdSN = ntohl(pdu->pdu_hdr.pduh_p.nop_in.ExpCmdSN);
MaxCmdSN = ntohl(pdu->pdu_hdr.pduh_p.nop_in.MaxCmdSN);
/* received a valid frame, reset timeout */
conn->c_num_timeouts = 0;
if ((pdu->pdu_hdr.pduh_Opcode & OPCODE_MASK) == TOP_NOP_In &&
TAILQ_EMPTY(&conn->c_ccbs_waiting))
connection_timeout_start(conn, conn->c_idle_timeout_val);
else
connection_timeout_start(conn, CONNECTION_TIMEOUT);
/* Update session window */
mutex_enter(&sess->s_lock);
if (sn_a_le_b(ExpCmdSN - 1, MaxCmdSN)) {
if (sn_a_lt_b(sess->s_ExpCmdSN, ExpCmdSN))
sess->s_ExpCmdSN = ExpCmdSN;
if (sn_a_lt_b(sess->s_MaxCmdSN, MaxCmdSN))
sess->s_MaxCmdSN = MaxCmdSN;
}
mutex_exit(&sess->s_lock);