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>
43 #include "iscsi_proto.h"
45 #ifdef ICL_KERNEL_PROXY
46 #include <sys/ioctl.h>
49 extern bool proxy_mode;
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;
115 conn = pdu->pdu_connection;
119 len = pdu_ahs_length(pdu);
121 log_errx(1, "protocol error: non-empty AHS");
123 len = pdu_data_segment_length(pdu);
124 assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
125 pdu->pdu_data_len = len;
129 pdu_send_proxy(struct pdu *pdu)
134 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
138 #endif /* ICL_KERNEL_PROXY */
141 pdu_padding(const struct pdu *pdu)
144 if ((pdu->pdu_data_len % 4) != 0)
145 return (4 - (pdu->pdu_data_len % 4));
151 pdu_read(int fd, char *data, size_t len)
156 ret = read(fd, data, len);
159 log_errx(1, "exiting due to timeout");
162 log_errx(1, "read: connection lost");
169 pdu_receive(struct pdu *pdu)
171 struct connection *conn;
175 #ifdef ICL_KERNEL_PROXY
177 return (pdu_receive_proxy(pdu));
180 assert(proxy_mode == false);
181 conn = pdu->pdu_connection;
183 pdu_read(conn->conn_socket, (char *)pdu->pdu_bhs,
184 sizeof(*pdu->pdu_bhs));
186 len = pdu_ahs_length(pdu);
188 log_errx(1, "protocol error: non-empty AHS");
190 len = pdu_data_segment_length(pdu);
192 if (len > (size_t)conn->conn_max_recv_data_segment_length) {
193 log_errx(1, "protocol error: received PDU "
194 "with DataSegmentLength exceeding %d",
195 conn->conn_max_recv_data_segment_length);
198 pdu->pdu_data_len = len;
199 pdu->pdu_data = malloc(len);
200 if (pdu->pdu_data == NULL)
201 log_err(1, "malloc");
203 pdu_read(conn->conn_socket, (char *)pdu->pdu_data,
206 padding = pdu_padding(pdu);
208 assert(padding < sizeof(dummy));
209 pdu_read(conn->conn_socket, (char *)dummy, padding);
215 pdu_send(struct pdu *pdu)
217 ssize_t ret, total_len;
223 #ifdef ICL_KERNEL_PROXY
225 return (pdu_send_proxy(pdu));
228 assert(proxy_mode == false);
230 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
231 iov[0].iov_base = pdu->pdu_bhs;
232 iov[0].iov_len = sizeof(*pdu->pdu_bhs);
233 total_len = iov[0].iov_len;
236 if (pdu->pdu_data_len > 0) {
237 iov[1].iov_base = pdu->pdu_data;
238 iov[1].iov_len = pdu->pdu_data_len;
239 total_len += iov[1].iov_len;
242 padding = pdu_padding(pdu);
244 assert(padding < sizeof(zero));
245 iov[2].iov_base = &zero;
246 iov[2].iov_len = padding;
247 total_len += iov[2].iov_len;
252 ret = writev(pdu->pdu_connection->conn_socket, iov, iovcnt);
255 log_errx(1, "exiting due to timeout");
256 log_err(1, "writev");
258 if (ret != total_len)
259 log_errx(1, "short write");
263 pdu_delete(struct pdu *pdu)