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>
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(sizeof(*pdu), 1);
90 pdu->pdu_bhs = calloc(sizeof(*pdu->pdu_bhs), 1);
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)
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 <= MAX_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)
172 #ifdef ICL_KERNEL_PROXY
174 return (pdu_receive_proxy(pdu));
177 assert(proxy_mode == false);
179 pdu_read(pdu->pdu_connection->conn_socket,
180 (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));
182 len = pdu_ahs_length(pdu);
184 log_errx(1, "protocol error: non-empty AHS");
186 len = pdu_data_segment_length(pdu);
188 if (len > MAX_DATA_SEGMENT_LENGTH) {
189 log_errx(1, "protocol error: received PDU "
190 "with DataSegmentLength exceeding %d",
191 MAX_DATA_SEGMENT_LENGTH);
194 pdu->pdu_data_len = len;
195 pdu->pdu_data = malloc(len);
196 if (pdu->pdu_data == NULL)
197 log_err(1, "malloc");
199 pdu_read(pdu->pdu_connection->conn_socket,
200 (char *)pdu->pdu_data, pdu->pdu_data_len);
202 padding = pdu_padding(pdu);
204 assert(padding < sizeof(dummy));
205 pdu_read(pdu->pdu_connection->conn_socket,
206 (char *)dummy, padding);
212 pdu_send(struct pdu *pdu)
214 ssize_t ret, total_len;
220 #ifdef ICL_KERNEL_PROXY
222 return (pdu_send_proxy(pdu));
225 assert(proxy_mode == false);
227 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
228 iov[0].iov_base = pdu->pdu_bhs;
229 iov[0].iov_len = sizeof(*pdu->pdu_bhs);
230 total_len = iov[0].iov_len;
233 if (pdu->pdu_data_len > 0) {
234 iov[1].iov_base = pdu->pdu_data;
235 iov[1].iov_len = pdu->pdu_data_len;
236 total_len += iov[1].iov_len;
239 padding = pdu_padding(pdu);
241 assert(padding < sizeof(zero));
242 iov[2].iov_base = &zero;
243 iov[2].iov_len = padding;
244 total_len += iov[2].iov_len;
249 ret = writev(pdu->pdu_connection->conn_socket, iov, iovcnt);
252 log_errx(1, "exiting due to timeout");
253 log_err(1, "writev");
255 if (ret != total_len)
256 log_errx(1, "short write");
260 pdu_delete(struct pdu *pdu)