]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ctld/pdu.c
Merge ACPICA 20170929.
[FreeBSD/FreeBSD.git] / usr.sbin / ctld / 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 <stdlib.h>
38 #include <unistd.h>
39
40 #include "ctld.h"
41 #include "iscsi_proto.h"
42
43 #ifdef ICL_KERNEL_PROXY
44 #include <sys/ioctl.h>
45 #endif
46
47 extern bool proxy_mode;
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         size_t len;
111
112         assert(proxy_mode);
113         conn = pdu->pdu_connection;
114
115         kernel_receive(pdu);
116
117         len = pdu_ahs_length(pdu);
118         if (len > 0)
119                 log_errx(1, "protocol error: non-empty AHS");
120
121         len = pdu_data_segment_length(pdu);
122         assert(len <= (size_t)conn->conn_max_recv_data_segment_length);
123         pdu->pdu_data_len = len;
124 }
125
126 static void
127 pdu_send_proxy(struct pdu *pdu)
128 {
129
130         assert(proxy_mode);
131
132         pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
133         kernel_send(pdu);
134 }
135
136 #endif /* ICL_KERNEL_PROXY */
137
138 static size_t
139 pdu_padding(const struct pdu *pdu)
140 {
141
142         if ((pdu->pdu_data_len % 4) != 0)
143                 return (4 - (pdu->pdu_data_len % 4));
144
145         return (0);
146 }
147
148 static void
149 pdu_read(int fd, char *data, size_t len)
150 {
151         ssize_t ret;
152
153         while (len > 0) {
154                 ret = read(fd, data, len);
155                 if (ret < 0) {
156                         if (timed_out())
157                                 log_errx(1, "exiting due to timeout");
158                         log_err(1, "read");
159                 } else if (ret == 0)
160                         log_errx(1, "read: connection lost");
161                 len -= ret;
162                 data += ret;
163         }
164 }
165
166 void
167 pdu_receive(struct pdu *pdu)
168 {
169         struct connection *conn;
170         size_t len, padding;
171         char dummy[4];
172
173 #ifdef ICL_KERNEL_PROXY
174         if (proxy_mode)
175                 return (pdu_receive_proxy(pdu));
176 #endif
177
178         assert(proxy_mode == false);
179         conn = pdu->pdu_connection;
180
181         pdu_read(conn->conn_socket, (char *)pdu->pdu_bhs,
182             sizeof(*pdu->pdu_bhs));
183
184         len = pdu_ahs_length(pdu);
185         if (len > 0)
186                 log_errx(1, "protocol error: non-empty AHS");
187
188         len = pdu_data_segment_length(pdu);
189         if (len > 0) {
190                 if (len > (size_t)conn->conn_max_recv_data_segment_length) {
191                         log_errx(1, "protocol error: received PDU "
192                             "with DataSegmentLength exceeding %d",
193                             conn->conn_max_recv_data_segment_length);
194                 }
195
196                 pdu->pdu_data_len = len;
197                 pdu->pdu_data = malloc(len);
198                 if (pdu->pdu_data == NULL)
199                         log_err(1, "malloc");
200
201                 pdu_read(conn->conn_socket, (char *)pdu->pdu_data,
202                     pdu->pdu_data_len);
203
204                 padding = pdu_padding(pdu);
205                 if (padding != 0) {
206                         assert(padding < sizeof(dummy));
207                         pdu_read(conn->conn_socket, (char *)dummy, padding);
208                 }
209         }
210 }
211
212 void
213 pdu_send(struct pdu *pdu)
214 {
215         ssize_t ret, total_len;
216         size_t padding;
217         uint32_t zero = 0;
218         struct iovec iov[3];
219         int iovcnt;
220
221 #ifdef ICL_KERNEL_PROXY
222         if (proxy_mode)
223                 return (pdu_send_proxy(pdu));
224 #endif
225
226         assert(proxy_mode == false);
227
228         pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
229         iov[0].iov_base = pdu->pdu_bhs;
230         iov[0].iov_len = sizeof(*pdu->pdu_bhs);
231         total_len = iov[0].iov_len;
232         iovcnt = 1;
233
234         if (pdu->pdu_data_len > 0) {
235                 iov[1].iov_base = pdu->pdu_data;
236                 iov[1].iov_len = pdu->pdu_data_len;
237                 total_len += iov[1].iov_len;
238                 iovcnt = 2;
239
240                 padding = pdu_padding(pdu);
241                 if (padding > 0) {
242                         assert(padding < sizeof(zero));
243                         iov[2].iov_base = &zero;
244                         iov[2].iov_len = padding;
245                         total_len += iov[2].iov_len;
246                         iovcnt = 3;
247                 }
248         }
249
250         ret = writev(pdu->pdu_connection->conn_socket, iov, iovcnt);
251         if (ret < 0) {
252                 if (timed_out())
253                         log_errx(1, "exiting due to timeout");
254                 log_err(1, "writev");
255         }
256         if (ret != total_len)
257                 log_errx(1, "short write");
258 }
259
260 void
261 pdu_delete(struct pdu *pdu)
262 {
263
264         free(pdu->pdu_data);
265         free(pdu->pdu_bhs);
266         free(pdu);
267 }