2013-09-14 15:29:06 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2013-09-14 15:29:06 +00:00
|
|
|
* Copyright (c) 2012 The FreeBSD Foundation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Edward Tomasz Napierala under sponsorship
|
|
|
|
* from the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* 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 AUTHOR 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 AUTHOR 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-21 15:32:38 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2013-09-14 15:29:06 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <assert.h>
|
2016-01-27 18:12:42 +00:00
|
|
|
#include <errno.h>
|
2013-09-14 15:29:06 +00:00
|
|
|
#include <stdlib.h>
|
2016-01-27 18:12:42 +00:00
|
|
|
#include <string.h>
|
2013-09-14 15:29:06 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "iscsid.h"
|
|
|
|
#include "iscsi_proto.h"
|
|
|
|
|
|
|
|
#ifdef ICL_KERNEL_PROXY
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
pdu_ahs_length(const struct pdu *pdu)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (pdu->pdu_bhs->bhs_total_ahs_len * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pdu_data_segment_length(const struct pdu *pdu)
|
|
|
|
{
|
|
|
|
uint32_t len = 0;
|
|
|
|
|
|
|
|
len += pdu->pdu_bhs->bhs_data_segment_len[0];
|
|
|
|
len <<= 8;
|
|
|
|
len += pdu->pdu_bhs->bhs_data_segment_len[1];
|
|
|
|
len <<= 8;
|
|
|
|
len += pdu->pdu_bhs->bhs_data_segment_len[2];
|
|
|
|
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pdu_set_data_segment_length(struct pdu *pdu, uint32_t len)
|
|
|
|
{
|
|
|
|
|
|
|
|
pdu->pdu_bhs->bhs_data_segment_len[2] = len;
|
|
|
|
pdu->pdu_bhs->bhs_data_segment_len[1] = len >> 8;
|
|
|
|
pdu->pdu_bhs->bhs_data_segment_len[0] = len >> 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pdu *
|
|
|
|
pdu_new(struct connection *conn)
|
|
|
|
{
|
|
|
|
struct pdu *pdu;
|
|
|
|
|
2016-08-22 15:01:39 +00:00
|
|
|
pdu = calloc(1, sizeof(*pdu));
|
2013-09-14 15:29:06 +00:00
|
|
|
if (pdu == NULL)
|
|
|
|
log_err(1, "calloc");
|
|
|
|
|
2016-08-22 15:01:39 +00:00
|
|
|
pdu->pdu_bhs = calloc(1, sizeof(*pdu->pdu_bhs));
|
2013-09-14 15:29:06 +00:00
|
|
|
if (pdu->pdu_bhs == NULL)
|
|
|
|
log_err(1, "calloc");
|
|
|
|
|
|
|
|
pdu->pdu_connection = conn;
|
|
|
|
|
|
|
|
return (pdu);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pdu *
|
|
|
|
pdu_new_response(struct pdu *request)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (pdu_new(request->pdu_connection));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ICL_KERNEL_PROXY
|
|
|
|
|
2014-04-16 18:23:36 +00:00
|
|
|
static void
|
|
|
|
pdu_receive_proxy(struct pdu *pdu)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2017-01-20 17:14:10 +00:00
|
|
|
struct connection *conn;
|
2013-09-14 15:29:06 +00:00
|
|
|
struct iscsi_daemon_receive *idr;
|
|
|
|
size_t len;
|
|
|
|
int error;
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
conn = pdu->pdu_connection;
|
|
|
|
assert(conn->conn_conf.isc_iser != 0);
|
2014-04-16 18:23:36 +00:00
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
|
2013-09-14 15:29:06 +00:00
|
|
|
if (pdu->pdu_data == NULL)
|
|
|
|
log_err(1, "malloc");
|
|
|
|
|
|
|
|
idr = calloc(1, sizeof(*idr));
|
|
|
|
if (idr == NULL)
|
|
|
|
log_err(1, "calloc");
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
idr->idr_session_id = conn->conn_session_id;
|
2013-09-14 15:29:06 +00:00
|
|
|
idr->idr_bhs = pdu->pdu_bhs;
|
2017-01-20 17:14:10 +00:00
|
|
|
idr->idr_data_segment_len = conn->conn_max_recv_data_segment_length;
|
2013-09-14 15:29:06 +00:00
|
|
|
idr->idr_data_segment = pdu->pdu_data;
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
error = ioctl(conn->conn_iscsi_fd, ISCSIDRECEIVE, idr);
|
2013-09-14 15:29:06 +00:00
|
|
|
if (error != 0)
|
|
|
|
log_err(1, "ISCSIDRECEIVE");
|
|
|
|
|
|
|
|
len = pdu_ahs_length(pdu);
|
|
|
|
if (len > 0)
|
|
|
|
log_errx(1, "protocol error: non-empty AHS");
|
|
|
|
|
|
|
|
len = pdu_data_segment_length(pdu);
|
2017-01-20 17:14:10 +00:00
|
|
|
assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
|
2013-09-14 15:29:06 +00:00
|
|
|
pdu->pdu_data_len = len;
|
|
|
|
|
|
|
|
free(idr);
|
|
|
|
}
|
|
|
|
|
2014-04-16 18:23:36 +00:00
|
|
|
static void
|
|
|
|
pdu_send_proxy(struct pdu *pdu)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2017-01-20 17:14:10 +00:00
|
|
|
struct connection *conn;
|
2013-09-14 15:29:06 +00:00
|
|
|
struct iscsi_daemon_send *ids;
|
|
|
|
int error;
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
conn = pdu->pdu_connection;
|
|
|
|
assert(conn->conn_conf.isc_iser != 0);
|
2014-04-16 18:23:36 +00:00
|
|
|
|
2013-09-14 15:29:06 +00:00
|
|
|
pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
|
|
|
|
|
|
|
|
ids = calloc(1, sizeof(*ids));
|
|
|
|
if (ids == NULL)
|
|
|
|
log_err(1, "calloc");
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
ids->ids_session_id = conn->conn_session_id;
|
2013-09-14 15:29:06 +00:00
|
|
|
ids->ids_bhs = pdu->pdu_bhs;
|
|
|
|
ids->ids_data_segment_len = pdu->pdu_data_len;
|
|
|
|
ids->ids_data_segment = pdu->pdu_data;
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
error = ioctl(conn->conn_iscsi_fd, ISCSIDSEND, ids);
|
2013-09-14 15:29:06 +00:00
|
|
|
if (error != 0)
|
|
|
|
log_err(1, "ISCSIDSEND");
|
|
|
|
|
|
|
|
free(ids);
|
|
|
|
}
|
|
|
|
|
2014-04-16 18:23:36 +00:00
|
|
|
#endif /* ICL_KERNEL_PROXY */
|
2013-09-14 15:29:06 +00:00
|
|
|
|
|
|
|
static size_t
|
|
|
|
pdu_padding(const struct pdu *pdu)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((pdu->pdu_data_len % 4) != 0)
|
|
|
|
return (4 - (pdu->pdu_data_len % 4));
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-27 18:12:42 +00:00
|
|
|
pdu_read(const struct connection *conn, char *data, size_t len)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
while (len > 0) {
|
2016-01-27 18:12:42 +00:00
|
|
|
ret = read(conn->conn_socket, data, len);
|
2013-09-14 15:29:06 +00:00
|
|
|
if (ret < 0) {
|
2016-01-27 18:12:42 +00:00
|
|
|
if (timed_out()) {
|
|
|
|
fail(conn, "Login Phase timeout");
|
2013-09-14 15:29:06 +00:00
|
|
|
log_errx(1, "exiting due to timeout");
|
2016-01-27 18:12:42 +00:00
|
|
|
}
|
|
|
|
fail(conn, strerror(errno));
|
2013-09-14 15:29:06 +00:00
|
|
|
log_err(1, "read");
|
2016-01-27 18:12:42 +00:00
|
|
|
} else if (ret == 0) {
|
|
|
|
fail(conn, "connection lost");
|
2013-09-14 15:29:06 +00:00
|
|
|
log_errx(1, "read: connection lost");
|
2016-01-27 18:12:42 +00:00
|
|
|
}
|
2013-09-14 15:29:06 +00:00
|
|
|
len -= ret;
|
|
|
|
data += ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pdu_receive(struct pdu *pdu)
|
|
|
|
{
|
2017-01-20 17:14:10 +00:00
|
|
|
struct connection *conn;
|
2013-09-14 15:29:06 +00:00
|
|
|
size_t len, padding;
|
|
|
|
char dummy[4];
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
conn = pdu->pdu_connection;
|
2014-04-16 18:23:36 +00:00
|
|
|
#ifdef ICL_KERNEL_PROXY
|
2017-01-20 17:14:10 +00:00
|
|
|
if (conn->conn_conf.isc_iser != 0)
|
2014-04-16 18:23:36 +00:00
|
|
|
return (pdu_receive_proxy(pdu));
|
|
|
|
#endif
|
2017-01-20 17:14:10 +00:00
|
|
|
assert(conn->conn_conf.isc_iser == 0);
|
2014-04-16 18:23:36 +00:00
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
pdu_read(conn, (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));
|
2013-09-14 15:29:06 +00:00
|
|
|
|
|
|
|
len = pdu_ahs_length(pdu);
|
|
|
|
if (len > 0)
|
|
|
|
log_errx(1, "protocol error: non-empty AHS");
|
|
|
|
|
|
|
|
len = pdu_data_segment_length(pdu);
|
|
|
|
if (len > 0) {
|
2017-01-20 17:14:10 +00:00
|
|
|
if (len > (size_t)conn->conn_max_recv_data_segment_length) {
|
2013-09-14 15:29:06 +00:00
|
|
|
log_errx(1, "protocol error: received PDU "
|
|
|
|
"with DataSegmentLength exceeding %d",
|
2017-01-20 17:14:10 +00:00
|
|
|
conn->conn_max_recv_data_segment_length);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pdu->pdu_data_len = len;
|
|
|
|
pdu->pdu_data = malloc(len);
|
|
|
|
if (pdu->pdu_data == NULL)
|
|
|
|
log_err(1, "malloc");
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
pdu_read(conn, (char *)pdu->pdu_data, pdu->pdu_data_len);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
|
|
|
padding = pdu_padding(pdu);
|
|
|
|
if (padding != 0) {
|
|
|
|
assert(padding < sizeof(dummy));
|
2017-01-20 17:14:10 +00:00
|
|
|
pdu_read(conn, (char *)dummy, padding);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pdu_send(struct pdu *pdu)
|
|
|
|
{
|
2017-01-20 17:14:10 +00:00
|
|
|
struct connection *conn;
|
2013-09-14 15:29:06 +00:00
|
|
|
ssize_t ret, total_len;
|
|
|
|
size_t padding;
|
|
|
|
uint32_t zero = 0;
|
|
|
|
struct iovec iov[3];
|
|
|
|
int iovcnt;
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
conn = pdu->pdu_connection;
|
2014-04-16 18:23:36 +00:00
|
|
|
#ifdef ICL_KERNEL_PROXY
|
2017-01-20 17:14:10 +00:00
|
|
|
if (conn->conn_conf.isc_iser != 0)
|
2014-04-16 18:23:36 +00:00
|
|
|
return (pdu_send_proxy(pdu));
|
|
|
|
#endif
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
assert(conn->conn_conf.isc_iser == 0);
|
2014-04-16 18:23:36 +00:00
|
|
|
|
2013-09-14 15:29:06 +00:00
|
|
|
pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
|
|
|
|
iov[0].iov_base = pdu->pdu_bhs;
|
|
|
|
iov[0].iov_len = sizeof(*pdu->pdu_bhs);
|
|
|
|
total_len = iov[0].iov_len;
|
|
|
|
iovcnt = 1;
|
|
|
|
|
|
|
|
if (pdu->pdu_data_len > 0) {
|
|
|
|
iov[1].iov_base = pdu->pdu_data;
|
|
|
|
iov[1].iov_len = pdu->pdu_data_len;
|
|
|
|
total_len += iov[1].iov_len;
|
|
|
|
iovcnt = 2;
|
|
|
|
|
|
|
|
padding = pdu_padding(pdu);
|
|
|
|
if (padding > 0) {
|
|
|
|
assert(padding < sizeof(zero));
|
|
|
|
iov[2].iov_base = &zero;
|
|
|
|
iov[2].iov_len = padding;
|
|
|
|
total_len += iov[2].iov_len;
|
|
|
|
iovcnt = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 17:14:10 +00:00
|
|
|
ret = writev(conn->conn_socket, iov, iovcnt);
|
2013-09-14 15:29:06 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (timed_out())
|
|
|
|
log_errx(1, "exiting due to timeout");
|
|
|
|
log_err(1, "writev");
|
|
|
|
}
|
|
|
|
if (ret != total_len)
|
|
|
|
log_errx(1, "short write");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pdu_delete(struct pdu *pdu)
|
|
|
|
{
|
|
|
|
|
|
|
|
free(pdu->pdu_data);
|
|
|
|
free(pdu->pdu_bhs);
|
|
|
|
free(pdu);
|
|
|
|
}
|