2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
44 #include "iscsi_proto.h"
46 #ifdef ICL_KERNEL_PROXY
47 #include <sys/ioctl.h>
51 pdu_ahs_length(const struct pdu *pdu)
54 return (pdu->pdu_bhs->bhs_total_ahs_len * 4);
58 pdu_data_segment_length(const struct pdu *pdu)
62 len += pdu->pdu_bhs->bhs_data_segment_len[0];
64 len += pdu->pdu_bhs->bhs_data_segment_len[1];
66 len += pdu->pdu_bhs->bhs_data_segment_len[2];
72 pdu_set_data_segment_length(struct pdu *pdu, uint32_t len)
75 pdu->pdu_bhs->bhs_data_segment_len[2] = len;
76 pdu->pdu_bhs->bhs_data_segment_len[1] = len >> 8;
77 pdu->pdu_bhs->bhs_data_segment_len[0] = len >> 16;
81 pdu_new(struct connection *conn)
85 pdu = calloc(1, sizeof(*pdu));
89 pdu->pdu_bhs = calloc(1, sizeof(*pdu->pdu_bhs));
90 if (pdu->pdu_bhs == NULL)
93 pdu->pdu_connection = conn;
99 pdu_new_response(struct pdu *request)
102 return (pdu_new(request->pdu_connection));
105 #ifdef ICL_KERNEL_PROXY
108 pdu_receive_proxy(struct pdu *pdu)
110 struct connection *conn;
111 struct iscsi_daemon_receive *idr;
115 conn = pdu->pdu_connection;
116 assert(conn->conn_conf.isc_iser != 0);
118 pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
119 if (pdu->pdu_data == NULL)
120 log_err(1, "malloc");
122 idr = calloc(1, sizeof(*idr));
124 log_err(1, "calloc");
126 idr->idr_session_id = conn->conn_session_id;
127 idr->idr_bhs = pdu->pdu_bhs;
128 idr->idr_data_segment_len = conn->conn_max_recv_data_segment_length;
129 idr->idr_data_segment = pdu->pdu_data;
131 error = ioctl(conn->conn_iscsi_fd, ISCSIDRECEIVE, idr);
133 log_err(1, "ISCSIDRECEIVE");
135 len = pdu_ahs_length(pdu);
137 log_errx(1, "protocol error: non-empty AHS");
139 len = pdu_data_segment_length(pdu);
140 assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
141 pdu->pdu_data_len = len;
147 pdu_send_proxy(struct pdu *pdu)
149 struct connection *conn;
150 struct iscsi_daemon_send *ids;
153 conn = pdu->pdu_connection;
154 assert(conn->conn_conf.isc_iser != 0);
156 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
158 ids = calloc(1, sizeof(*ids));
160 log_err(1, "calloc");
162 ids->ids_session_id = conn->conn_session_id;
163 ids->ids_bhs = pdu->pdu_bhs;
164 ids->ids_data_segment_len = pdu->pdu_data_len;
165 ids->ids_data_segment = pdu->pdu_data;
167 error = ioctl(conn->conn_iscsi_fd, ISCSIDSEND, ids);
169 log_err(1, "ISCSIDSEND");
174 #endif /* ICL_KERNEL_PROXY */
177 pdu_padding(const struct pdu *pdu)
180 if ((pdu->pdu_data_len % 4) != 0)
181 return (4 - (pdu->pdu_data_len % 4));
187 pdu_read(const struct connection *conn, char *data, size_t len)
192 ret = read(conn->conn_socket, data, len);
195 fail(conn, "Login Phase timeout");
196 log_errx(1, "exiting due to timeout");
198 fail(conn, strerror(errno));
200 } else if (ret == 0) {
201 fail(conn, "connection lost");
202 log_errx(1, "read: connection lost");
210 pdu_receive(struct pdu *pdu)
212 struct connection *conn;
216 conn = pdu->pdu_connection;
217 #ifdef ICL_KERNEL_PROXY
218 if (conn->conn_conf.isc_iser != 0)
219 return (pdu_receive_proxy(pdu));
221 assert(conn->conn_conf.isc_iser == 0);
223 pdu_read(conn, (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));
225 len = pdu_ahs_length(pdu);
227 log_errx(1, "protocol error: non-empty AHS");
229 len = pdu_data_segment_length(pdu);
231 if (len > (size_t)conn->conn_max_recv_data_segment_length) {
232 log_errx(1, "protocol error: received PDU "
233 "with DataSegmentLength exceeding %d",
234 conn->conn_max_recv_data_segment_length);
237 pdu->pdu_data_len = len;
238 pdu->pdu_data = malloc(len);
239 if (pdu->pdu_data == NULL)
240 log_err(1, "malloc");
242 pdu_read(conn, (char *)pdu->pdu_data, pdu->pdu_data_len);
244 padding = pdu_padding(pdu);
246 assert(padding < sizeof(dummy));
247 pdu_read(conn, (char *)dummy, padding);
253 pdu_send(struct pdu *pdu)
255 struct connection *conn;
256 ssize_t ret, total_len;
262 conn = pdu->pdu_connection;
263 #ifdef ICL_KERNEL_PROXY
264 if (conn->conn_conf.isc_iser != 0)
265 return (pdu_send_proxy(pdu));
268 assert(conn->conn_conf.isc_iser == 0);
270 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
271 iov[0].iov_base = pdu->pdu_bhs;
272 iov[0].iov_len = sizeof(*pdu->pdu_bhs);
273 total_len = iov[0].iov_len;
276 if (pdu->pdu_data_len > 0) {
277 iov[1].iov_base = pdu->pdu_data;
278 iov[1].iov_len = pdu->pdu_data_len;
279 total_len += iov[1].iov_len;
282 padding = pdu_padding(pdu);
284 assert(padding < sizeof(zero));
285 iov[2].iov_base = &zero;
286 iov[2].iov_len = padding;
287 total_len += iov[2].iov_len;
292 ret = writev(conn->conn_socket, iov, iovcnt);
295 log_errx(1, "exiting due to timeout");
296 log_err(1, "writev");
298 if (ret != total_len)
299 log_errx(1, "short write");
303 pdu_delete(struct pdu *pdu)