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>
43 #include <iscsi_proto.h>
44 #include "libiscsiutil.h"
47 pdu_ahs_length(const struct pdu *pdu)
50 return (pdu->pdu_bhs->bhs_total_ahs_len * 4);
54 pdu_data_segment_length(const struct pdu *pdu)
58 len += pdu->pdu_bhs->bhs_data_segment_len[0];
60 len += pdu->pdu_bhs->bhs_data_segment_len[1];
62 len += pdu->pdu_bhs->bhs_data_segment_len[2];
68 pdu_set_data_segment_length(struct pdu *pdu, uint32_t len)
71 pdu->pdu_bhs->bhs_data_segment_len[2] = len;
72 pdu->pdu_bhs->bhs_data_segment_len[1] = len >> 8;
73 pdu->pdu_bhs->bhs_data_segment_len[0] = len >> 16;
77 pdu_new(struct connection *conn)
81 pdu = calloc(1, sizeof(*pdu));
85 pdu->pdu_bhs = calloc(1, sizeof(*pdu->pdu_bhs));
86 if (pdu->pdu_bhs == NULL)
89 pdu->pdu_connection = conn;
95 pdu_new_response(struct pdu *request)
98 return (pdu_new(request->pdu_connection));
102 pdu_padding(const struct pdu *pdu)
105 if ((pdu->pdu_data_len % 4) != 0)
106 return (4 - (pdu->pdu_data_len % 4));
112 pdu_read(const struct connection *conn, char *data, size_t len)
117 ret = read(conn->conn_socket, data, len);
119 if (conn->conn_ops->timed_out()) {
120 conn->conn_ops->fail(conn,
121 "Login Phase timeout");
122 log_errx(1, "exiting due to timeout");
124 conn->conn_ops->fail(conn, strerror(errno));
126 } else if (ret == 0) {
127 conn->conn_ops->fail(conn, "connection lost");
128 log_errx(1, "read: connection lost");
136 pdu_receive(struct pdu *pdu)
138 struct connection *conn;
142 conn = pdu->pdu_connection;
143 if (conn->conn_use_proxy)
144 return (conn->conn_ops->pdu_receive_proxy(pdu));
146 pdu_read(conn, (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));
148 len = pdu_ahs_length(pdu);
150 log_errx(1, "protocol error: non-empty AHS");
152 len = pdu_data_segment_length(pdu);
154 if (len > (size_t)conn->conn_max_recv_data_segment_length) {
155 log_errx(1, "protocol error: received PDU "
156 "with DataSegmentLength exceeding %d",
157 conn->conn_max_recv_data_segment_length);
160 pdu->pdu_data_len = len;
161 pdu->pdu_data = malloc(len);
162 if (pdu->pdu_data == NULL)
163 log_err(1, "malloc");
165 pdu_read(conn, (char *)pdu->pdu_data, pdu->pdu_data_len);
167 padding = pdu_padding(pdu);
169 assert(padding < sizeof(dummy));
170 pdu_read(conn, (char *)dummy, padding);
176 pdu_send(struct pdu *pdu)
178 struct connection *conn;
179 ssize_t ret, total_len;
185 conn = pdu->pdu_connection;
186 if (conn->conn_use_proxy)
187 return (conn->conn_ops->pdu_send_proxy(pdu));
189 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
190 iov[0].iov_base = pdu->pdu_bhs;
191 iov[0].iov_len = sizeof(*pdu->pdu_bhs);
192 total_len = iov[0].iov_len;
195 if (pdu->pdu_data_len > 0) {
196 iov[1].iov_base = pdu->pdu_data;
197 iov[1].iov_len = pdu->pdu_data_len;
198 total_len += iov[1].iov_len;
201 padding = pdu_padding(pdu);
203 assert(padding < sizeof(zero));
204 iov[2].iov_base = &zero;
205 iov[2].iov_len = padding;
206 total_len += iov[2].iov_len;
211 ret = writev(conn->conn_socket, iov, iovcnt);
213 if (conn->conn_ops->timed_out())
214 log_errx(1, "exiting due to timeout");
215 log_err(1, "writev");
217 if (ret != total_len)
218 log_errx(1, "short write");
222 pdu_delete(struct pdu *pdu)