2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
41 #include "iscsi_proto.h"
43 #ifdef ICL_KERNEL_PROXY
44 #include <sys/ioctl.h>
47 extern bool proxy_mode;
50 pdu_ahs_length(const struct pdu *pdu)
53 return (pdu->pdu_bhs->bhs_total_ahs_len * 4);
57 pdu_data_segment_length(const struct pdu *pdu)
61 len += pdu->pdu_bhs->bhs_data_segment_len[0];
63 len += pdu->pdu_bhs->bhs_data_segment_len[1];
65 len += pdu->pdu_bhs->bhs_data_segment_len[2];
71 pdu_set_data_segment_length(struct pdu *pdu, uint32_t len)
74 pdu->pdu_bhs->bhs_data_segment_len[2] = len;
75 pdu->pdu_bhs->bhs_data_segment_len[1] = len >> 8;
76 pdu->pdu_bhs->bhs_data_segment_len[0] = len >> 16;
80 pdu_new(struct connection *conn)
84 pdu = calloc(1, sizeof(*pdu));
88 pdu->pdu_bhs = calloc(1, sizeof(*pdu->pdu_bhs));
89 if (pdu->pdu_bhs == NULL)
92 pdu->pdu_connection = conn;
98 pdu_new_response(struct pdu *request)
101 return (pdu_new(request->pdu_connection));
104 #ifdef ICL_KERNEL_PROXY
107 pdu_receive_proxy(struct pdu *pdu)
109 struct connection *conn;
113 conn = pdu->pdu_connection;
117 len = pdu_ahs_length(pdu);
119 log_errx(1, "protocol error: non-empty AHS");
121 len = pdu_data_segment_length(pdu);
122 assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
123 pdu->pdu_data_len = len;
127 pdu_send_proxy(struct pdu *pdu)
132 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
136 #endif /* ICL_KERNEL_PROXY */
139 pdu_padding(const struct pdu *pdu)
142 if ((pdu->pdu_data_len % 4) != 0)
143 return (4 - (pdu->pdu_data_len % 4));
149 pdu_read(int fd, char *data, size_t len)
154 ret = read(fd, data, len);
157 log_errx(1, "exiting due to timeout");
160 log_errx(1, "read: connection lost");
167 pdu_receive(struct pdu *pdu)
169 struct connection *conn;
173 #ifdef ICL_KERNEL_PROXY
175 return (pdu_receive_proxy(pdu));
178 assert(proxy_mode == false);
179 conn = pdu->pdu_connection;
181 pdu_read(conn->conn_socket, (char *)pdu->pdu_bhs,
182 sizeof(*pdu->pdu_bhs));
184 len = pdu_ahs_length(pdu);
186 log_errx(1, "protocol error: non-empty AHS");
188 len = pdu_data_segment_length(pdu);
190 if (len > (size_t)conn->conn_max_recv_data_segment_length) {
191 log_errx(1, "protocol error: received PDU "
192 "with DataSegmentLength exceeding %d",
193 conn->conn_max_recv_data_segment_length);
196 pdu->pdu_data_len = len;
197 pdu->pdu_data = malloc(len);
198 if (pdu->pdu_data == NULL)
199 log_err(1, "malloc");
201 pdu_read(conn->conn_socket, (char *)pdu->pdu_data,
204 padding = pdu_padding(pdu);
206 assert(padding < sizeof(dummy));
207 pdu_read(conn->conn_socket, (char *)dummy, padding);
213 pdu_send(struct pdu *pdu)
215 ssize_t ret, total_len;
221 #ifdef ICL_KERNEL_PROXY
223 return (pdu_send_proxy(pdu));
226 assert(proxy_mode == false);
228 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
229 iov[0].iov_base = pdu->pdu_bhs;
230 iov[0].iov_len = sizeof(*pdu->pdu_bhs);
231 total_len = iov[0].iov_len;
234 if (pdu->pdu_data_len > 0) {
235 iov[1].iov_base = pdu->pdu_data;
236 iov[1].iov_len = pdu->pdu_data_len;
237 total_len += iov[1].iov_len;
240 padding = pdu_padding(pdu);
242 assert(padding < sizeof(zero));
243 iov[2].iov_base = &zero;
244 iov[2].iov_len = padding;
245 total_len += iov[2].iov_len;
250 ret = writev(pdu->pdu_connection->conn_socket, iov, iovcnt);
253 log_errx(1, "exiting due to timeout");
254 log_err(1, "writev");
256 if (ret != total_len)
257 log_errx(1, "short write");
261 pdu_delete(struct pdu *pdu)