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>
42 #include "iscsi_proto.h"
44 #ifdef ICL_KERNEL_PROXY
45 #include <sys/ioctl.h>
48 extern bool proxy_mode;
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;
114 conn = pdu->pdu_connection;
118 len = pdu_ahs_length(pdu);
120 log_errx(1, "protocol error: non-empty AHS");
122 len = pdu_data_segment_length(pdu);
123 assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
124 pdu->pdu_data_len = len;
128 pdu_send_proxy(struct pdu *pdu)
133 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
137 #endif /* ICL_KERNEL_PROXY */
140 pdu_padding(const struct pdu *pdu)
143 if ((pdu->pdu_data_len % 4) != 0)
144 return (4 - (pdu->pdu_data_len % 4));
150 pdu_read(int fd, char *data, size_t len)
155 ret = read(fd, data, len);
158 log_errx(1, "exiting due to timeout");
161 log_errx(1, "read: connection lost");
168 pdu_receive(struct pdu *pdu)
170 struct connection *conn;
174 #ifdef ICL_KERNEL_PROXY
176 return (pdu_receive_proxy(pdu));
179 assert(proxy_mode == false);
180 conn = pdu->pdu_connection;
182 pdu_read(conn->conn_socket, (char *)pdu->pdu_bhs,
183 sizeof(*pdu->pdu_bhs));
185 len = pdu_ahs_length(pdu);
187 log_errx(1, "protocol error: non-empty AHS");
189 len = pdu_data_segment_length(pdu);
191 if (len > (size_t)conn->conn_max_recv_data_segment_length) {
192 log_errx(1, "protocol error: received PDU "
193 "with DataSegmentLength exceeding %d",
194 conn->conn_max_recv_data_segment_length);
197 pdu->pdu_data_len = len;
198 pdu->pdu_data = malloc(len);
199 if (pdu->pdu_data == NULL)
200 log_err(1, "malloc");
202 pdu_read(conn->conn_socket, (char *)pdu->pdu_data,
205 padding = pdu_padding(pdu);
207 assert(padding < sizeof(dummy));
208 pdu_read(conn->conn_socket, (char *)dummy, padding);
214 pdu_send(struct pdu *pdu)
216 ssize_t ret, total_len;
222 #ifdef ICL_KERNEL_PROXY
224 return (pdu_send_proxy(pdu));
227 assert(proxy_mode == false);
229 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
230 iov[0].iov_base = pdu->pdu_bhs;
231 iov[0].iov_len = sizeof(*pdu->pdu_bhs);
232 total_len = iov[0].iov_len;
235 if (pdu->pdu_data_len > 0) {
236 iov[1].iov_base = pdu->pdu_data;
237 iov[1].iov_len = pdu->pdu_data_len;
238 total_len += iov[1].iov_len;
241 padding = pdu_padding(pdu);
243 assert(padding < sizeof(zero));
244 iov[2].iov_base = &zero;
245 iov[2].iov_len = padding;
246 total_len += iov[2].iov_len;
251 ret = writev(pdu->pdu_connection->conn_socket, iov, iovcnt);
254 log_errx(1, "exiting due to timeout");
255 log_err(1, "writev");
257 if (ret != total_len)
258 log_errx(1, "short write");
262 pdu_delete(struct pdu *pdu)