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