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(sizeof(*pdu), 1);
88 pdu->pdu_bhs = calloc(sizeof(*pdu->pdu_bhs), 1);
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)
115 len = pdu_ahs_length(pdu);
117 log_errx(1, "protocol error: non-empty AHS");
119 len = pdu_data_segment_length(pdu);
120 assert(len <= MAX_DATA_SEGMENT_LENGTH);
121 pdu->pdu_data_len = len;
125 pdu_send_proxy(struct pdu *pdu)
130 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
134 #endif /* ICL_KERNEL_PROXY */
137 pdu_padding(const struct pdu *pdu)
140 if ((pdu->pdu_data_len % 4) != 0)
141 return (4 - (pdu->pdu_data_len % 4));
147 pdu_read(int fd, char *data, size_t len)
152 ret = read(fd, data, len);
155 log_errx(1, "exiting due to timeout");
158 log_errx(1, "read: connection lost");
165 pdu_receive(struct pdu *pdu)
170 #ifdef ICL_KERNEL_PROXY
172 return (pdu_receive_proxy(pdu));
175 assert(proxy_mode == false);
177 pdu_read(pdu->pdu_connection->conn_socket,
178 (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));
180 len = pdu_ahs_length(pdu);
182 log_errx(1, "protocol error: non-empty AHS");
184 len = pdu_data_segment_length(pdu);
186 if (len > MAX_DATA_SEGMENT_LENGTH) {
187 log_errx(1, "protocol error: received PDU "
188 "with DataSegmentLength exceeding %d",
189 MAX_DATA_SEGMENT_LENGTH);
192 pdu->pdu_data_len = len;
193 pdu->pdu_data = malloc(len);
194 if (pdu->pdu_data == NULL)
195 log_err(1, "malloc");
197 pdu_read(pdu->pdu_connection->conn_socket,
198 (char *)pdu->pdu_data, pdu->pdu_data_len);
200 padding = pdu_padding(pdu);
202 assert(padding < sizeof(dummy));
203 pdu_read(pdu->pdu_connection->conn_socket,
204 (char *)dummy, padding);
210 pdu_send(struct pdu *pdu)
212 ssize_t ret, total_len;
218 #ifdef ICL_KERNEL_PROXY
220 return (pdu_send_proxy(pdu));
223 assert(proxy_mode == false);
225 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
226 iov[0].iov_base = pdu->pdu_bhs;
227 iov[0].iov_len = sizeof(*pdu->pdu_bhs);
228 total_len = iov[0].iov_len;
231 if (pdu->pdu_data_len > 0) {
232 iov[1].iov_base = pdu->pdu_data;
233 iov[1].iov_len = pdu->pdu_data_len;
234 total_len += iov[1].iov_len;
237 padding = pdu_padding(pdu);
239 assert(padding < sizeof(zero));
240 iov[2].iov_base = &zero;
241 iov[2].iov_len = padding;
242 total_len += iov[2].iov_len;
247 ret = writev(pdu->pdu_connection->conn_socket, iov, iovcnt);
250 log_errx(1, "exiting due to timeout");
251 log_err(1, "writev");
253 if (ret != total_len)
254 log_errx(1, "short write");
258 pdu_delete(struct pdu *pdu)