2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
45 #include "iscsi_proto.h"
47 #ifdef ICL_KERNEL_PROXY
48 #include <sys/ioctl.h>
52 pdu_ahs_length(const struct pdu *pdu)
55 return (pdu->pdu_bhs->bhs_total_ahs_len * 4);
59 pdu_data_segment_length(const struct pdu *pdu)
63 len += pdu->pdu_bhs->bhs_data_segment_len[0];
65 len += pdu->pdu_bhs->bhs_data_segment_len[1];
67 len += pdu->pdu_bhs->bhs_data_segment_len[2];
73 pdu_set_data_segment_length(struct pdu *pdu, uint32_t len)
76 pdu->pdu_bhs->bhs_data_segment_len[2] = len;
77 pdu->pdu_bhs->bhs_data_segment_len[1] = len >> 8;
78 pdu->pdu_bhs->bhs_data_segment_len[0] = len >> 16;
82 pdu_new(struct connection *conn)
86 pdu = calloc(1, sizeof(*pdu));
90 pdu->pdu_bhs = calloc(1, sizeof(*pdu->pdu_bhs));
91 if (pdu->pdu_bhs == NULL)
94 pdu->pdu_connection = conn;
100 pdu_new_response(struct pdu *request)
103 return (pdu_new(request->pdu_connection));
106 #ifdef ICL_KERNEL_PROXY
109 pdu_receive_proxy(struct pdu *pdu)
111 struct connection *conn;
112 struct iscsi_daemon_receive *idr;
116 conn = pdu->pdu_connection;
117 assert(conn->conn_conf.isc_iser != 0);
119 pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
120 if (pdu->pdu_data == NULL)
121 log_err(1, "malloc");
123 idr = calloc(1, sizeof(*idr));
125 log_err(1, "calloc");
127 idr->idr_session_id = conn->conn_session_id;
128 idr->idr_bhs = pdu->pdu_bhs;
129 idr->idr_data_segment_len = conn->conn_max_recv_data_segment_length;
130 idr->idr_data_segment = pdu->pdu_data;
132 error = ioctl(conn->conn_iscsi_fd, ISCSIDRECEIVE, idr);
134 log_err(1, "ISCSIDRECEIVE");
136 len = pdu_ahs_length(pdu);
138 log_errx(1, "protocol error: non-empty AHS");
140 len = pdu_data_segment_length(pdu);
141 assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
142 pdu->pdu_data_len = len;
148 pdu_send_proxy(struct pdu *pdu)
150 struct connection *conn;
151 struct iscsi_daemon_send *ids;
154 conn = pdu->pdu_connection;
155 assert(conn->conn_conf.isc_iser != 0);
157 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
159 ids = calloc(1, sizeof(*ids));
161 log_err(1, "calloc");
163 ids->ids_session_id = conn->conn_session_id;
164 ids->ids_bhs = pdu->pdu_bhs;
165 ids->ids_data_segment_len = pdu->pdu_data_len;
166 ids->ids_data_segment = pdu->pdu_data;
168 error = ioctl(conn->conn_iscsi_fd, ISCSIDSEND, ids);
170 log_err(1, "ISCSIDSEND");
175 #endif /* ICL_KERNEL_PROXY */
178 pdu_padding(const struct pdu *pdu)
181 if ((pdu->pdu_data_len % 4) != 0)
182 return (4 - (pdu->pdu_data_len % 4));
188 pdu_read(const struct connection *conn, char *data, size_t len)
193 ret = read(conn->conn_socket, data, len);
196 fail(conn, "Login Phase timeout");
197 log_errx(1, "exiting due to timeout");
199 fail(conn, strerror(errno));
201 } else if (ret == 0) {
202 fail(conn, "connection lost");
203 log_errx(1, "read: connection lost");
211 pdu_receive(struct pdu *pdu)
213 struct connection *conn;
217 conn = pdu->pdu_connection;
218 #ifdef ICL_KERNEL_PROXY
219 if (conn->conn_conf.isc_iser != 0)
220 return (pdu_receive_proxy(pdu));
222 assert(conn->conn_conf.isc_iser == 0);
224 pdu_read(conn, (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));
226 len = pdu_ahs_length(pdu);
228 log_errx(1, "protocol error: non-empty AHS");
230 len = pdu_data_segment_length(pdu);
232 if (len > (size_t)conn->conn_max_recv_data_segment_length) {
233 log_errx(1, "protocol error: received PDU "
234 "with DataSegmentLength exceeding %d",
235 conn->conn_max_recv_data_segment_length);
238 pdu->pdu_data_len = len;
239 pdu->pdu_data = malloc(len);
240 if (pdu->pdu_data == NULL)
241 log_err(1, "malloc");
243 pdu_read(conn, (char *)pdu->pdu_data, pdu->pdu_data_len);
245 padding = pdu_padding(pdu);
247 assert(padding < sizeof(dummy));
248 pdu_read(conn, (char *)dummy, padding);
254 pdu_send(struct pdu *pdu)
256 struct connection *conn;
257 ssize_t ret, total_len;
263 conn = pdu->pdu_connection;
264 #ifdef ICL_KERNEL_PROXY
265 if (conn->conn_conf.isc_iser != 0)
266 return (pdu_send_proxy(pdu));
269 assert(conn->conn_conf.isc_iser == 0);
271 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
272 iov[0].iov_base = pdu->pdu_bhs;
273 iov[0].iov_len = sizeof(*pdu->pdu_bhs);
274 total_len = iov[0].iov_len;
277 if (pdu->pdu_data_len > 0) {
278 iov[1].iov_base = pdu->pdu_data;
279 iov[1].iov_len = pdu->pdu_data_len;
280 total_len += iov[1].iov_len;
283 padding = pdu_padding(pdu);
285 assert(padding < sizeof(zero));
286 iov[2].iov_base = &zero;
287 iov[2].iov_len = padding;
288 total_len += iov[2].iov_len;
293 ret = writev(conn->conn_socket, iov, iovcnt);
296 log_errx(1, "exiting due to timeout");
297 log_err(1, "writev");
299 if (ret != total_len)
300 log_errx(1, "short write");
304 pdu_delete(struct pdu *pdu)