summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ctld/pdu.c
blob: be3598e2b5a722d8b78a59268433ea8defec0aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*-
 * Copyright (c) 2012 The FreeBSD Foundation
 * All rights reserved.
 *
 * This software was developed by Edward Tomasz Napierala under sponsorship
 * from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/uio.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>

#include "ctld.h"
#include "iscsi_proto.h"

#ifdef ICL_KERNEL_PROXY
#include <sys/ioctl.h>
#endif

extern bool proxy_mode;

static int
pdu_ahs_length(const struct pdu *pdu)
{

	return (pdu->pdu_bhs->bhs_total_ahs_len * 4);
}

static int
pdu_data_segment_length(const struct pdu *pdu)
{
	uint32_t len = 0;

	len += pdu->pdu_bhs->bhs_data_segment_len[0];
	len <<= 8;
	len += pdu->pdu_bhs->bhs_data_segment_len[1];
	len <<= 8;
	len += pdu->pdu_bhs->bhs_data_segment_len[2];

	return (len);
}

static void
pdu_set_data_segment_length(struct pdu *pdu, uint32_t len)
{

	pdu->pdu_bhs->bhs_data_segment_len[2] = len;
	pdu->pdu_bhs->bhs_data_segment_len[1] = len >> 8;
	pdu->pdu_bhs->bhs_data_segment_len[0] = len >> 16;
}

struct pdu *
pdu_new(struct connection *conn)
{
	struct pdu *pdu;

	pdu = calloc(sizeof(*pdu), 1);
	if (pdu == NULL)
		log_err(1, "calloc");

	pdu->pdu_bhs = calloc(sizeof(*pdu->pdu_bhs), 1);
	if (pdu->pdu_bhs == NULL)
		log_err(1, "calloc");

	pdu->pdu_connection = conn;

	return (pdu);
}

struct pdu *
pdu_new_response(struct pdu *request)
{

	return (pdu_new(request->pdu_connection));
}

#ifdef ICL_KERNEL_PROXY

static void
pdu_receive_proxy(struct pdu *pdu)
{
	size_t len;

	assert(proxy_mode);

	kernel_receive(pdu);

	len = pdu_ahs_length(pdu);
	if (len > 0)
		log_errx(1, "protocol error: non-empty AHS");

	len = pdu_data_segment_length(pdu);
	assert(len <= MAX_DATA_SEGMENT_LENGTH);
	pdu->pdu_data_len = len;
}

static void
pdu_send_proxy(struct pdu *pdu)
{

	assert(proxy_mode);

	pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
	kernel_send(pdu);
}

#endif /* ICL_KERNEL_PROXY */

static size_t
pdu_padding(const struct pdu *pdu)
{

	if ((pdu->pdu_data_len % 4) != 0)
		return (4 - (pdu->pdu_data_len % 4));

	return (0);
}

static void
pdu_read(int fd, char *data, size_t len)
{
	ssize_t ret;

	while (len > 0) {
		ret = read(fd, data, len);
		if (ret < 0) {
			if (timed_out())
				log_errx(1, "exiting due to timeout");
			log_err(1, "read");
		} else if (ret == 0)
			log_errx(1, "read: connection lost");
		len -= ret;
		data += ret;
	}
}

void
pdu_receive(struct pdu *pdu)
{
	size_t len, padding;
	char dummy[4];

#ifdef ICL_KERNEL_PROXY
	if (proxy_mode)
		return (pdu_receive_proxy(pdu));
#endif

	assert(proxy_mode == false);

	pdu_read(pdu->pdu_connection->conn_socket,
	    (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));

	len = pdu_ahs_length(pdu);
	if (len > 0)
		log_errx(1, "protocol error: non-empty AHS");

	len = pdu_data_segment_length(pdu);
	if (len > 0) {
		if (len > MAX_DATA_SEGMENT_LENGTH) {
			log_errx(1, "protocol error: received PDU "
			    "with DataSegmentLength exceeding %d",
			    MAX_DATA_SEGMENT_LENGTH);
		}

		pdu->pdu_data_len = len;
		pdu->pdu_data = malloc(len);
		if (pdu->pdu_data == NULL)
			log_err(1, "malloc");

		pdu_read(pdu->pdu_connection->conn_socket,
		    (char *)pdu->pdu_data, pdu->pdu_data_len);

		padding = pdu_padding(pdu);
		if (padding != 0) {
			assert(padding < sizeof(dummy));
			pdu_read(pdu->pdu_connection->conn_socket,
			    (char *)dummy, padding);
		}
	}
}

void
pdu_send(struct pdu *pdu)
{
	ssize_t ret, total_len;
	size_t padding;
	uint32_t zero = 0;
	struct iovec iov[3];
	int iovcnt;

#ifdef ICL_KERNEL_PROXY
	if (proxy_mode)
		return (pdu_send_proxy(pdu));
#endif

	assert(proxy_mode == false);

	pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
	iov[0].iov_base = pdu->pdu_bhs;
	iov[0].iov_len = sizeof(*pdu->pdu_bhs);
	total_len = iov[0].iov_len;
	iovcnt = 1;

	if (pdu->pdu_data_len > 0) {
		iov[1].iov_base = pdu->pdu_data;
		iov[1].iov_len = pdu->pdu_data_len;
		total_len += iov[1].iov_len;
		iovcnt = 2;

		padding = pdu_padding(pdu);
		if (padding > 0) {
			assert(padding < sizeof(zero));
			iov[2].iov_base = &zero;
			iov[2].iov_len = padding;
			total_len += iov[2].iov_len;
			iovcnt = 3;
		}
	}

	ret = writev(pdu->pdu_connection->conn_socket, iov, iovcnt);
	if (ret < 0) {
		if (timed_out())
			log_errx(1, "exiting due to timeout");
		log_err(1, "writev");
	}
	if (ret != total_len)
		log_errx(1, "short write");
}

void
pdu_delete(struct pdu *pdu)
{

	free(pdu->pdu_data);
	free(pdu->pdu_bhs);
	free(pdu);
}
OpenPOWER on IntegriCloud