1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
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/types.h>
33#include <sys/uio.h>
34#include <assert.h>
35#include <errno.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39
40#include <iscsi_proto.h>
41#include "libiscsiutil.h"
42
43int
44pdu_ahs_length(const struct pdu *pdu)
45{
46
47	return (pdu->pdu_bhs->bhs_total_ahs_len * 4);
48}
49
50int
51pdu_data_segment_length(const struct pdu *pdu)
52{
53	uint32_t len = 0;
54
55	len += pdu->pdu_bhs->bhs_data_segment_len[0];
56	len <<= 8;
57	len += pdu->pdu_bhs->bhs_data_segment_len[1];
58	len <<= 8;
59	len += pdu->pdu_bhs->bhs_data_segment_len[2];
60
61	return (len);
62}
63
64void
65pdu_set_data_segment_length(struct pdu *pdu, uint32_t len)
66{
67
68	pdu->pdu_bhs->bhs_data_segment_len[2] = len;
69	pdu->pdu_bhs->bhs_data_segment_len[1] = len >> 8;
70	pdu->pdu_bhs->bhs_data_segment_len[0] = len >> 16;
71}
72
73struct pdu *
74pdu_new(struct connection *conn)
75{
76	struct pdu *pdu;
77
78	pdu = calloc(1, sizeof(*pdu));
79	if (pdu == NULL)
80		log_err(1, "calloc");
81
82	pdu->pdu_bhs = calloc(1, sizeof(*pdu->pdu_bhs));
83	if (pdu->pdu_bhs == NULL)
84		log_err(1, "calloc");
85
86	pdu->pdu_connection = conn;
87
88	return (pdu);
89}
90
91struct pdu *
92pdu_new_response(struct pdu *request)
93{
94
95	return (pdu_new(request->pdu_connection));
96}
97
98static size_t
99pdu_padding(const struct pdu *pdu)
100{
101
102	if ((pdu->pdu_data_len % 4) != 0)
103		return (4 - (pdu->pdu_data_len % 4));
104
105	return (0);
106}
107
108static void
109pdu_read(const struct connection *conn, char *data, size_t len)
110{
111	ssize_t ret;
112
113	while (len > 0) {
114		ret = read(conn->conn_socket, data, len);
115		if (ret < 0) {
116			if (conn->conn_ops->timed_out()) {
117				conn->conn_ops->fail(conn,
118				    "Login Phase timeout");
119				log_errx(1, "exiting due to timeout");
120			}
121			conn->conn_ops->fail(conn, strerror(errno));
122			log_err(1, "read");
123		} else if (ret == 0) {
124			conn->conn_ops->fail(conn, "connection lost");
125			log_errx(1, "read: connection lost");
126		}
127		len -= ret;
128		data += ret;
129	}
130}
131
132void
133pdu_receive(struct pdu *pdu)
134{
135	struct connection *conn;
136	size_t len, padding;
137	char dummy[4];
138
139	conn = pdu->pdu_connection;
140	if (conn->conn_use_proxy)
141		return (conn->conn_ops->pdu_receive_proxy(pdu));
142
143	pdu_read(conn, (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));
144
145	len = pdu_ahs_length(pdu);
146	if (len > 0)
147		log_errx(1, "protocol error: non-empty AHS");
148
149	len = pdu_data_segment_length(pdu);
150	if (len > 0) {
151		if (len > (size_t)conn->conn_max_recv_data_segment_length) {
152			log_errx(1, "protocol error: received PDU "
153			    "with DataSegmentLength exceeding %d",
154			    conn->conn_max_recv_data_segment_length);
155		}
156
157		pdu->pdu_data_len = len;
158		pdu->pdu_data = malloc(len);
159		if (pdu->pdu_data == NULL)
160			log_err(1, "malloc");
161
162		pdu_read(conn, (char *)pdu->pdu_data, pdu->pdu_data_len);
163
164		padding = pdu_padding(pdu);
165		if (padding != 0) {
166			assert(padding < sizeof(dummy));
167			pdu_read(conn, (char *)dummy, padding);
168		}
169	}
170}
171
172void
173pdu_send(struct pdu *pdu)
174{
175	struct connection *conn;
176	ssize_t ret, total_len;
177	size_t padding;
178	uint32_t zero = 0;
179	struct iovec iov[3];
180	int iovcnt;
181
182	conn = pdu->pdu_connection;
183	if (conn->conn_use_proxy)
184		return (conn->conn_ops->pdu_send_proxy(pdu));
185
186	pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
187	iov[0].iov_base = pdu->pdu_bhs;
188	iov[0].iov_len = sizeof(*pdu->pdu_bhs);
189	total_len = iov[0].iov_len;
190	iovcnt = 1;
191
192	if (pdu->pdu_data_len > 0) {
193		iov[1].iov_base = pdu->pdu_data;
194		iov[1].iov_len = pdu->pdu_data_len;
195		total_len += iov[1].iov_len;
196		iovcnt = 2;
197
198		padding = pdu_padding(pdu);
199		if (padding > 0) {
200			assert(padding < sizeof(zero));
201			iov[2].iov_base = &zero;
202			iov[2].iov_len = padding;
203			total_len += iov[2].iov_len;
204			iovcnt = 3;
205		}
206	}
207
208	ret = writev(conn->conn_socket, iov, iovcnt);
209	if (ret < 0) {
210		if (conn->conn_ops->timed_out())
211			log_errx(1, "exiting due to timeout");
212		log_err(1, "writev");
213	}
214	if (ret != total_len)
215		log_errx(1, "short write");
216}
217
218void
219pdu_delete(struct pdu *pdu)
220{
221
222	free(pdu->pdu_data);
223	free(pdu->pdu_bhs);
224	free(pdu);
225}
226