1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 The FreeBSD Foundation
5 *
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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
28 * SUCH DAMAGE.
29 *
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/types.h>
36#include <sys/uio.h>
37#include <assert.h>
38#include <errno.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43#include "iscsid.h"
44#include "iscsi_proto.h"
45
46#ifdef ICL_KERNEL_PROXY
47#include <sys/ioctl.h>
48#endif
49
50static int
51pdu_ahs_length(const struct pdu *pdu)
52{
53
54	return (pdu->pdu_bhs->bhs_total_ahs_len * 4);
55}
56
57static int
58pdu_data_segment_length(const struct pdu *pdu)
59{
60	uint32_t len = 0;
61
62	len += pdu->pdu_bhs->bhs_data_segment_len[0];
63	len <<= 8;
64	len += pdu->pdu_bhs->bhs_data_segment_len[1];
65	len <<= 8;
66	len += pdu->pdu_bhs->bhs_data_segment_len[2];
67
68	return (len);
69}
70
71static void
72pdu_set_data_segment_length(struct pdu *pdu, uint32_t len)
73{
74
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;
78}
79
80struct pdu *
81pdu_new(struct connection *conn)
82{
83	struct pdu *pdu;
84
85	pdu = calloc(1, sizeof(*pdu));
86	if (pdu == NULL)
87		log_err(1, "calloc");
88
89	pdu->pdu_bhs = calloc(1, sizeof(*pdu->pdu_bhs));
90	if (pdu->pdu_bhs == NULL)
91		log_err(1, "calloc");
92
93	pdu->pdu_connection = conn;
94
95	return (pdu);
96}
97
98struct pdu *
99pdu_new_response(struct pdu *request)
100{
101
102	return (pdu_new(request->pdu_connection));
103}
104
105#ifdef ICL_KERNEL_PROXY
106
107static void
108pdu_receive_proxy(struct pdu *pdu)
109{
110	struct connection *conn;
111	struct iscsi_daemon_receive *idr;
112	size_t len;
113	int error;
114
115	conn = pdu->pdu_connection;
116	assert(conn->conn_conf.isc_iser != 0);
117
118	pdu->pdu_data = malloc(conn->conn_max_recv_data_segment_length);
119	if (pdu->pdu_data == NULL)
120		log_err(1, "malloc");
121
122	idr = calloc(1, sizeof(*idr));
123	if (idr == NULL)
124		log_err(1, "calloc");
125
126	idr->idr_session_id = conn->conn_session_id;
127	idr->idr_bhs = pdu->pdu_bhs;
128	idr->idr_data_segment_len = conn->conn_max_recv_data_segment_length;
129	idr->idr_data_segment = pdu->pdu_data;
130
131	error = ioctl(conn->conn_iscsi_fd, ISCSIDRECEIVE, idr);
132	if (error != 0)
133		log_err(1, "ISCSIDRECEIVE");
134
135	len = pdu_ahs_length(pdu);
136	if (len > 0)
137		log_errx(1, "protocol error: non-empty AHS");
138
139	len = pdu_data_segment_length(pdu);
140	assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
141	pdu->pdu_data_len = len;
142
143	free(idr);
144}
145
146static void
147pdu_send_proxy(struct pdu *pdu)
148{
149	struct connection *conn;
150	struct iscsi_daemon_send *ids;
151	int error;
152
153	conn = pdu->pdu_connection;
154	assert(conn->conn_conf.isc_iser != 0);
155
156	pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
157
158	ids = calloc(1, sizeof(*ids));
159	if (ids == NULL)
160		log_err(1, "calloc");
161
162	ids->ids_session_id = conn->conn_session_id;
163	ids->ids_bhs = pdu->pdu_bhs;
164	ids->ids_data_segment_len = pdu->pdu_data_len;
165	ids->ids_data_segment = pdu->pdu_data;
166
167	error = ioctl(conn->conn_iscsi_fd, ISCSIDSEND, ids);
168	if (error != 0)
169		log_err(1, "ISCSIDSEND");
170
171	free(ids);
172}
173
174#endif /* ICL_KERNEL_PROXY */
175
176static size_t
177pdu_padding(const struct pdu *pdu)
178{
179
180	if ((pdu->pdu_data_len % 4) != 0)
181		return (4 - (pdu->pdu_data_len % 4));
182
183	return (0);
184}
185
186static void
187pdu_read(const struct connection *conn, char *data, size_t len)
188{
189	ssize_t ret;
190
191	while (len > 0) {
192		ret = read(conn->conn_socket, data, len);
193		if (ret < 0) {
194			if (timed_out()) {
195				fail(conn, "Login Phase timeout");
196				log_errx(1, "exiting due to timeout");
197			}
198			fail(conn, strerror(errno));
199			log_err(1, "read");
200		} else if (ret == 0) {
201			fail(conn, "connection lost");
202			log_errx(1, "read: connection lost");
203		}
204		len -= ret;
205		data += ret;
206	}
207}
208
209void
210pdu_receive(struct pdu *pdu)
211{
212	struct connection *conn;
213	size_t len, padding;
214	char dummy[4];
215
216	conn = pdu->pdu_connection;
217#ifdef ICL_KERNEL_PROXY
218	if (conn->conn_conf.isc_iser != 0)
219		return (pdu_receive_proxy(pdu));
220#endif
221	assert(conn->conn_conf.isc_iser == 0);
222
223	pdu_read(conn, (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));
224
225	len = pdu_ahs_length(pdu);
226	if (len > 0)
227		log_errx(1, "protocol error: non-empty AHS");
228
229	len = pdu_data_segment_length(pdu);
230	if (len > 0) {
231		if (len > (size_t)conn->conn_max_recv_data_segment_length) {
232			log_errx(1, "protocol error: received PDU "
233			    "with DataSegmentLength exceeding %d",
234			    conn->conn_max_recv_data_segment_length);
235		}
236
237		pdu->pdu_data_len = len;
238		pdu->pdu_data = malloc(len);
239		if (pdu->pdu_data == NULL)
240			log_err(1, "malloc");
241
242		pdu_read(conn, (char *)pdu->pdu_data, pdu->pdu_data_len);
243
244		padding = pdu_padding(pdu);
245		if (padding != 0) {
246			assert(padding < sizeof(dummy));
247			pdu_read(conn, (char *)dummy, padding);
248		}
249	}
250}
251
252void
253pdu_send(struct pdu *pdu)
254{
255	struct connection *conn;
256	ssize_t ret, total_len;
257	size_t padding;
258	uint32_t zero = 0;
259	struct iovec iov[3];
260	int iovcnt;
261
262	conn = pdu->pdu_connection;
263#ifdef ICL_KERNEL_PROXY
264	if (conn->conn_conf.isc_iser != 0)
265		return (pdu_send_proxy(pdu));
266#endif
267
268	assert(conn->conn_conf.isc_iser == 0);
269
270	pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
271	iov[0].iov_base = pdu->pdu_bhs;
272	iov[0].iov_len = sizeof(*pdu->pdu_bhs);
273	total_len = iov[0].iov_len;
274	iovcnt = 1;
275
276	if (pdu->pdu_data_len > 0) {
277		iov[1].iov_base = pdu->pdu_data;
278		iov[1].iov_len = pdu->pdu_data_len;
279		total_len += iov[1].iov_len;
280		iovcnt = 2;
281
282		padding = pdu_padding(pdu);
283		if (padding > 0) {
284			assert(padding < sizeof(zero));
285			iov[2].iov_base = &zero;
286			iov[2].iov_len = padding;
287			total_len += iov[2].iov_len;
288			iovcnt = 3;
289		}
290	}
291
292	ret = writev(conn->conn_socket, iov, iovcnt);
293	if (ret < 0) {
294		if (timed_out())
295			log_errx(1, "exiting due to timeout");
296		log_err(1, "writev");
297	}
298	if (ret != total_len)
299		log_errx(1, "short write");
300}
301
302void
303pdu_delete(struct pdu *pdu)
304{
305
306	free(pdu->pdu_data);
307	free(pdu->pdu_bhs);
308	free(pdu);
309}
310