iscsi_rcv.c revision 1.27 1 1.27 mlelstv /* $NetBSD: iscsi_rcv.c,v 1.27 2024/11/03 10:50:21 mlelstv Exp $ */
2 1.1 agc
3 1.1 agc /*-
4 1.1 agc * Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
5 1.1 agc * All rights reserved.
6 1.1 agc *
7 1.1 agc * This code is derived from software contributed to The NetBSD Foundation
8 1.1 agc * by Wasabi Systems, Inc.
9 1.1 agc *
10 1.1 agc * Redistribution and use in source and binary forms, with or without
11 1.1 agc * modification, are permitted provided that the following conditions
12 1.1 agc * are met:
13 1.1 agc * 1. Redistributions of source code must retain the above copyright
14 1.1 agc * notice, this list of conditions and the following disclaimer.
15 1.1 agc * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 agc * notice, this list of conditions and the following disclaimer in the
17 1.1 agc * documentation and/or other materials provided with the distribution.
18 1.1 agc *
19 1.1 agc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 agc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 agc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 agc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 agc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 agc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 agc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 agc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 agc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 agc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 agc * POSSIBILITY OF SUCH DAMAGE.
30 1.1 agc */
31 1.1 agc #include "iscsi_globals.h"
32 1.1 agc
33 1.1 agc #include <sys/file.h>
34 1.1 agc #include <sys/socket.h>
35 1.1 agc #include <sys/socketvar.h>
36 1.1 agc
37 1.1 agc /*****************************************************************************/
38 1.1 agc
39 1.1 agc /*
40 1.1 agc * my_soo_read:
41 1.1 agc * Replacement for soo_read with flag handling.
42 1.1 agc *
43 1.1 agc * Parameter:
44 1.1 agc * conn The connection
45 1.1 agc * u The uio descriptor
46 1.1 agc * flags Read flags
47 1.1 agc *
48 1.1 agc * Returns: 0 on success, else 1
49 1.1 agc */
50 1.1 agc
51 1.1 agc STATIC int
52 1.1 agc my_soo_read(connection_t *conn, struct uio *u, int flags)
53 1.1 agc {
54 1.26 mlelstv struct socket *so;
55 1.1 agc int ret;
56 1.1 agc #ifdef ISCSI_DEBUG
57 1.1 agc size_t resid = u->uio_resid;
58 1.1 agc #endif
59 1.1 agc
60 1.1 agc DEBC(conn, 99, ("soo_read req: %zu\n", resid));
61 1.1 agc
62 1.26 mlelstv rw_enter(&conn->c_sock_rw, RW_READER);
63 1.26 mlelstv if (conn->c_sock == NULL) {
64 1.26 mlelstv ret = EIO;
65 1.26 mlelstv } else {
66 1.26 mlelstv so = conn->c_sock->f_socket;
67 1.26 mlelstv if (flags & MSG_WAITALL) {
68 1.26 mlelstv flags &= ~MSG_WAITALL;
69 1.26 mlelstv do {
70 1.26 mlelstv int oresid = u->uio_resid;
71 1.26 mlelstv ret = (*so->so_receive)(so, NULL, u,
72 1.26 mlelstv NULL, NULL, &flags);
73 1.26 mlelstv if (!ret && u->uio_resid == oresid)
74 1.26 mlelstv break;
75 1.26 mlelstv } while (!ret && u->uio_resid > 0);
76 1.26 mlelstv } else {
77 1.26 mlelstv ret = (*so->so_receive)(so, NULL, u,
78 1.26 mlelstv NULL, NULL, &flags);
79 1.26 mlelstv }
80 1.26 mlelstv }
81 1.26 mlelstv
82 1.26 mlelstv rw_exit(&conn->c_sock_rw);
83 1.1 agc
84 1.1 agc if (ret || (flags != MSG_DONTWAIT && u->uio_resid)) {
85 1.13 mlelstv DEBC(conn, 1, ("Read failed (ret: %d, req: %zu, out: %zu)\n",
86 1.13 mlelstv ret, resid, u->uio_resid));
87 1.26 mlelstv if (ret)
88 1.26 mlelstv handle_connection_error(conn, ISCSI_STATUS_SOCKET_ERROR,
89 1.26 mlelstv RECOVER_CONNECTION);
90 1.1 agc return 1;
91 1.1 agc }
92 1.1 agc return 0;
93 1.1 agc }
94 1.1 agc
95 1.1 agc
96 1.1 agc /*
97 1.1 agc * try_resynch_receive:
98 1.1 agc * Skip over everything in the socket's receive buffer, in the hope of
99 1.1 agc * ending up at the start of a new PDU.
100 1.1 agc *
101 1.1 agc * Parameter:
102 1.1 agc * conn The connection
103 1.1 agc */
104 1.1 agc
105 1.1 agc STATIC void
106 1.1 agc try_resynch_receive(connection_t *conn)
107 1.1 agc {
108 1.1 agc uint8_t buffer[64];
109 1.1 agc struct uio uio;
110 1.1 agc struct iovec io_vec;
111 1.1 agc int rc;
112 1.1 agc
113 1.1 agc uio.uio_rw = UIO_READ;
114 1.1 agc UIO_SETUP_SYSSPACE(&uio);
115 1.1 agc
116 1.1 agc do {
117 1.1 agc io_vec.iov_base = buffer;
118 1.1 agc uio.uio_iov = &io_vec;
119 1.1 agc uio.uio_iovcnt = 1;
120 1.1 agc uio.uio_resid = io_vec.iov_len = sizeof(buffer);
121 1.1 agc
122 1.1 agc rc = my_soo_read(conn, &uio, MSG_DONTWAIT);
123 1.1 agc DEBC(conn, 9, ("try_resynch_receive: rc = %d, resid = %zu\n",
124 1.1 agc rc, uio.uio_resid));
125 1.1 agc } while (!rc && !uio.uio_resid);
126 1.1 agc }
127 1.1 agc
128 1.1 agc
129 1.1 agc /*
130 1.1 agc * ccb_from_itt
131 1.1 agc * Translate ITT into CCB pointer.
132 1.1 agc *
133 1.1 agc * Parameter:
134 1.1 agc * conn The connection
135 1.1 agc * itt The Initiator Task Tag
136 1.1 agc *
137 1.1 agc * Returns:
138 1.1 agc * Pointer to CCB, or NULL if ITT is not a valid CCB index.
139 1.1 agc */
140 1.1 agc
141 1.1 agc STATIC ccb_t *
142 1.1 agc ccb_from_itt(connection_t *conn, uint32_t itt)
143 1.1 agc {
144 1.1 agc ccb_t *ccb;
145 1.1 agc int cidx;
146 1.1 agc
147 1.14 mlelstv if (itt == 0xffffffff)
148 1.14 mlelstv return NULL;
149 1.14 mlelstv
150 1.1 agc cidx = itt & 0xff;
151 1.18 mlelstv if (cidx >= CCBS_PER_SESSION)
152 1.1 agc return NULL;
153 1.18 mlelstv
154 1.24 christos ccb = &conn->c_session->s_ccb[cidx];
155 1.18 mlelstv
156 1.24 christos if (ccb->ccb_ITT != itt) {
157 1.13 mlelstv DEBC(conn, 0,
158 1.13 mlelstv ("ccb_from_itt: received invalid CCB itt %08x != %08x\n",
159 1.24 christos itt, ccb->ccb_ITT));
160 1.1 agc return NULL;
161 1.1 agc }
162 1.18 mlelstv
163 1.24 christos if (ccb->ccb_disp <= CCBDISP_BUSY) {
164 1.19 mlelstv DEBC(conn, 0,
165 1.19 mlelstv ("ccb_from_itt: received CCB with invalid disp %d\n",
166 1.24 christos ccb->ccb_disp));
167 1.19 mlelstv return NULL;
168 1.19 mlelstv }
169 1.19 mlelstv
170 1.1 agc return ccb;
171 1.1 agc }
172 1.1 agc
173 1.1 agc
174 1.1 agc /*
175 1.1 agc * read_pdu_data:
176 1.1 agc * Initialize the uio structure for receiving everything after the
177 1.1 agc * header, including data (if present), and padding. Read the data.
178 1.1 agc *
179 1.1 agc * Parameter:
180 1.1 agc * pdu The PDU
181 1.1 agc * data Pointer to data (may be NULL for auto-allocation)
182 1.1 agc * offset The offset into the data pointer
183 1.1 agc *
184 1.1 agc * Returns: 0 on success
185 1.1 agc * 1 if an error occurs during read
186 1.1 agc * -1 if the data digest was incorrect (PDU must be ignored)
187 1.1 agc */
188 1.1 agc
189 1.1 agc STATIC int
190 1.1 agc read_pdu_data(pdu_t *pdu, uint8_t *data, uint32_t offset)
191 1.1 agc {
192 1.1 agc static uint8_t pad_bytes[4];
193 1.1 agc uint32_t len, digest;
194 1.1 agc struct uio *uio;
195 1.1 agc int i, pad;
196 1.24 christos connection_t *conn = pdu->pdu_connection;
197 1.1 agc
198 1.13 mlelstv DEB(15, ("read_pdu_data: data segment length = %d\n",
199 1.24 christos ntoh3(pdu->pdu_hdr.pduh_DataSegmentLength)));
200 1.24 christos if (!(len = ntoh3(pdu->pdu_hdr.pduh_DataSegmentLength))) {
201 1.1 agc return 0;
202 1.1 agc }
203 1.1 agc pad = len & 0x03;
204 1.1 agc if (pad) {
205 1.1 agc pad = 4 - pad;
206 1.1 agc }
207 1.11 mlelstv
208 1.11 mlelstv KASSERT(data != NULL || offset == 0);
209 1.1 agc
210 1.1 agc if (data == NULL) {
211 1.1 agc /*
212 1.1 agc * NOTE: Always allocate 2 extra bytes when reading temp data,
213 1.1 agc * since temp data is mostly used for received text, and we can
214 1.1 agc * make sure there's a double zero at the end of the data to mark EOF.
215 1.1 agc */
216 1.1 agc if ((data = (uint8_t *) malloc(len + 2, M_TEMP, M_WAITOK)) == NULL) {
217 1.1 agc DEBOUT(("ran out of mem on receive\n"));
218 1.24 christos handle_connection_error(pdu->pdu_connection,
219 1.1 agc ISCSI_STATUS_NO_RESOURCES, LOGOUT_SESSION);
220 1.1 agc return 1;
221 1.1 agc }
222 1.24 christos pdu->pdu_temp_data = data;
223 1.24 christos pdu->pdu_temp_data_len = len;
224 1.1 agc }
225 1.1 agc
226 1.24 christos pdu->pdu_io_vec[0].iov_base = data + offset;
227 1.24 christos pdu->pdu_io_vec[0].iov_len = len;
228 1.1 agc
229 1.24 christos uio = &pdu->pdu_uio;
230 1.1 agc
231 1.24 christos uio->uio_iov = pdu->pdu_io_vec;
232 1.1 agc uio->uio_iovcnt = 1;
233 1.1 agc uio->uio_rw = UIO_READ;
234 1.1 agc uio->uio_resid = len;
235 1.1 agc UIO_SETUP_SYSSPACE(uio);
236 1.1 agc
237 1.1 agc if (pad) {
238 1.1 agc uio->uio_iovcnt++;
239 1.1 agc uio->uio_iov[1].iov_base = pad_bytes;
240 1.1 agc uio->uio_iov[1].iov_len = pad;
241 1.1 agc uio->uio_resid += pad;
242 1.1 agc }
243 1.1 agc
244 1.24 christos if (conn->c_DataDigest) {
245 1.1 agc i = uio->uio_iovcnt++;
246 1.24 christos pdu->pdu_io_vec[i].iov_base = &pdu->pdu_data_digest;
247 1.24 christos pdu->pdu_io_vec[i].iov_len = 4;
248 1.1 agc uio->uio_resid += 4;
249 1.1 agc }
250 1.1 agc
251 1.1 agc /* get the data */
252 1.24 christos if (my_soo_read(conn, &pdu->pdu_uio, MSG_WAITALL) != 0) {
253 1.1 agc return 1;
254 1.1 agc }
255 1.24 christos if (conn->c_DataDigest) {
256 1.1 agc digest = gen_digest_2(data, len, pad_bytes, pad);
257 1.1 agc
258 1.24 christos if (digest != pdu->pdu_data_digest) {
259 1.1 agc DEBOUT(("Data Digest Error: comp = %08x, rx = %08x\n",
260 1.24 christos digest, pdu->pdu_data_digest));
261 1.24 christos switch (pdu->pdu_hdr.pduh_Opcode & OPCODE_MASK) {
262 1.1 agc case TOP_SCSI_Response:
263 1.1 agc case TOP_Text_Response:
264 1.24 christos send_snack(pdu->pdu_connection, pdu, NULL, SNACK_STATUS_NAK);
265 1.1 agc break;
266 1.1 agc
267 1.1 agc case TOP_SCSI_Data_in:
268 1.24 christos send_snack(pdu->pdu_connection, pdu, NULL, SNACK_DATA_NAK);
269 1.1 agc break;
270 1.1 agc
271 1.1 agc default:
272 1.1 agc /* ignore all others */
273 1.1 agc break;
274 1.1 agc }
275 1.1 agc return -1;
276 1.1 agc }
277 1.1 agc }
278 1.1 agc return 0;
279 1.1 agc }
280 1.1 agc
281 1.1 agc
282 1.1 agc /*
283 1.1 agc * collect_text_data
284 1.1 agc * Handle text continuation in login and text response PDUs
285 1.1 agc *
286 1.1 agc * Parameter:
287 1.1 agc * pdu The received PDU
288 1.1 agc * req_CCB The CCB associated with the original request
289 1.1 agc *
290 1.1 agc * Returns: -1 if continue flag is set
291 1.1 agc * 0 if text is complete
292 1.1 agc * +1 if an error occurred (out of resources)
293 1.1 agc */
294 1.1 agc STATIC int
295 1.1 agc collect_text_data(pdu_t *pdu, ccb_t *req_ccb)
296 1.1 agc {
297 1.1 agc
298 1.24 christos if (req_ccb->ccb_text_data) {
299 1.1 agc int nlen;
300 1.1 agc uint8_t *newp;
301 1.1 agc
302 1.24 christos nlen = req_ccb->ccb_text_len + pdu->pdu_temp_data_len;
303 1.1 agc /* Note: allocate extra 2 bytes for text terminator */
304 1.1 agc if ((newp = malloc(nlen + 2, M_TEMP, M_WAITOK)) == NULL) {
305 1.4 mlelstv DEBOUT(("Collect Text Data: Out of Memory, ccb = %p\n", req_ccb));
306 1.24 christos req_ccb->ccb_status = ISCSI_STATUS_NO_RESOURCES;
307 1.4 mlelstv /* XXX where is CCB freed? */
308 1.1 agc return 1;
309 1.1 agc }
310 1.24 christos memcpy(newp, req_ccb->ccb_text_data, req_ccb->ccb_text_len);
311 1.24 christos memcpy(&newp[req_ccb->ccb_text_len], pdu->pdu_temp_data, pdu->pdu_temp_data_len);
312 1.1 agc
313 1.24 christos free(req_ccb->ccb_text_data, M_TEMP);
314 1.24 christos free(pdu->pdu_temp_data, M_TEMP);
315 1.1 agc
316 1.24 christos req_ccb->ccb_text_data = NULL;
317 1.24 christos pdu->pdu_temp_data = newp;
318 1.24 christos pdu->pdu_temp_data_len = nlen;
319 1.1 agc }
320 1.1 agc
321 1.24 christos if (pdu->pdu_hdr.pduh_Flags & FLAG_CONTINUE) {
322 1.24 christos req_ccb->ccb_text_data = pdu->pdu_temp_data;
323 1.24 christos req_ccb->ccb_text_len = pdu->pdu_temp_data_len;
324 1.24 christos pdu->pdu_temp_data = NULL;
325 1.1 agc
326 1.24 christos acknowledge_text(req_ccb->ccb_connection, pdu, req_ccb);
327 1.1 agc return -1;
328 1.1 agc }
329 1.1 agc return 0;
330 1.1 agc }
331 1.1 agc
332 1.1 agc
333 1.1 agc /*
334 1.1 agc * check_StatSN
335 1.1 agc * Check received vs. expected StatSN
336 1.1 agc *
337 1.1 agc * Parameter:
338 1.1 agc * conn The connection
339 1.1 agc * nw_sn The received StatSN in network byte order
340 1.1 agc * ack Acknowledge this SN if TRUE
341 1.1 agc */
342 1.1 agc
343 1.1 agc STATIC int
344 1.1 agc check_StatSN(connection_t *conn, uint32_t nw_sn, bool ack)
345 1.1 agc {
346 1.1 agc int rc;
347 1.1 agc uint32_t sn = ntohl(nw_sn);
348 1.1 agc
349 1.24 christos rc = add_sernum(&conn->c_StatSN_buf, sn);
350 1.1 agc
351 1.1 agc if (ack)
352 1.24 christos ack_sernum(&conn->c_StatSN_buf, sn);
353 1.1 agc
354 1.1 agc if (rc != 1) {
355 1.3 mlelstv if (rc == 0) {
356 1.3 mlelstv DEBOUT(("Duplicate PDU, ExpSN %d, Recvd: %d\n",
357 1.24 christos conn->c_StatSN_buf.ExpSN, sn));
358 1.1 agc return -1;
359 1.3 mlelstv }
360 1.1 agc
361 1.1 agc if (rc < 0) {
362 1.1 agc DEBOUT(("Excessive outstanding Status PDUs, ExpSN %d, Recvd: %d\n",
363 1.24 christos conn->c_StatSN_buf.ExpSN, sn));
364 1.1 agc handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST,
365 1.1 agc RECOVER_CONNECTION);
366 1.1 agc return rc;
367 1.1 agc }
368 1.1 agc
369 1.1 agc DEBOUT(("Missing Status PDUs: First %d, num: %d\n",
370 1.24 christos conn->c_StatSN_buf.ExpSN, rc - 1));
371 1.24 christos if (conn->c_state == ST_FULL_FEATURE &&
372 1.24 christos conn->c_session->s_ErrorRecoveryLevel) {
373 1.1 agc snack_missing(conn, NULL, SNACK_STATUS_NAK,
374 1.24 christos conn->c_StatSN_buf.ExpSN, rc - 1);
375 1.1 agc } else {
376 1.1 agc DEBOUT(("StatSN killing connection (State = %d, "
377 1.1 agc "ErrorRecoveryLevel = %d)\n",
378 1.24 christos conn->c_state, conn->c_session->s_ErrorRecoveryLevel));
379 1.1 agc handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST,
380 1.1 agc RECOVER_CONNECTION);
381 1.1 agc return -1;
382 1.1 agc }
383 1.1 agc }
384 1.1 agc return 0;
385 1.1 agc }
386 1.1 agc
387 1.1 agc
388 1.1 agc /*
389 1.1 agc * check_CmdSN
390 1.1 agc * Check received vs. expected CmdSN
391 1.1 agc *
392 1.1 agc * Parameter:
393 1.1 agc * conn The connection
394 1.1 agc * nw_sn The received ExpCmdSN in network byte order
395 1.1 agc */
396 1.1 agc
397 1.1 agc STATIC void
398 1.1 agc check_CmdSN(connection_t *conn, uint32_t nw_sn)
399 1.1 agc {
400 1.1 agc uint32_t sn = ntohl(nw_sn);
401 1.1 agc ccb_t *ccb, *nxt;
402 1.1 agc
403 1.24 christos TAILQ_FOREACH_SAFE(ccb, &conn->c_ccbs_waiting, ccb_chain, nxt) {
404 1.1 agc DEBC(conn, 10,
405 1.1 agc ("CheckCmdSN - CmdSN=%d, ExpCmdSn=%d, waiting=%p, flags=%x\n",
406 1.24 christos ccb->ccb_CmdSN, sn, ccb->ccb_pdu_waiting, ccb->ccb_flags));
407 1.24 christos if (ccb->ccb_pdu_waiting != NULL &&
408 1.24 christos sn_a_lt_b(sn, ccb->ccb_CmdSN) &&
409 1.24 christos !(ccb->ccb_flags & CCBF_GOT_RSP)) {
410 1.1 agc DEBC(conn, 1, ("CheckCmdSN resending - CmdSN=%d, ExpCmdSn=%d\n",
411 1.24 christos ccb->ccb_CmdSN, sn));
412 1.1 agc
413 1.24 christos ccb->ccb_total_tries++;
414 1.1 agc
415 1.24 christos if (++ccb->ccb_num_timeouts > MAX_CCB_TIMEOUTS ||
416 1.24 christos ccb->ccb_total_tries > MAX_CCB_TRIES) {
417 1.15 mlelstv handle_connection_error(conn,
418 1.15 mlelstv ISCSI_STATUS_TIMEOUT,
419 1.24 christos (ccb->ccb_total_tries <= MAX_CCB_TRIES)
420 1.15 mlelstv ? RECOVER_CONNECTION
421 1.15 mlelstv : LOGOUT_CONNECTION);
422 1.1 agc break;
423 1.1 agc } else {
424 1.1 agc resend_pdu(ccb);
425 1.1 agc }
426 1.1 agc }
427 1.15 mlelstv
428 1.15 mlelstv /*
429 1.15 mlelstv * The target can respond to a NOP-In before subsequent
430 1.15 mlelstv * commands are processed. So our CmdSN can exceed the
431 1.15 mlelstv * returned ExpCmdSN by the number of commands that are
432 1.15 mlelstv * in flight. Adjust the expected value accordingly.
433 1.15 mlelstv */
434 1.15 mlelstv sn++;
435 1.1 agc }
436 1.1 agc }
437 1.1 agc
438 1.1 agc
439 1.1 agc /*
440 1.1 agc * receive_login_pdu
441 1.1 agc * Handle receipt of a login response PDU.
442 1.1 agc *
443 1.1 agc * Parameter:
444 1.1 agc * conn The connection
445 1.1 agc * pdu The PDU
446 1.1 agc * req_CCB The CCB associated with the original request (if any)
447 1.1 agc */
448 1.1 agc
449 1.1 agc STATIC int
450 1.1 agc receive_login_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
451 1.1 agc {
452 1.1 agc int rc;
453 1.1 agc
454 1.3 mlelstv DEBC(conn, 9, ("Received Login Response PDU, op=%x, flags=%x, sn=%u\n",
455 1.24 christos pdu->pdu_hdr.pduh_Opcode, pdu->pdu_hdr.pduh_Flags,
456 1.24 christos ntohl(pdu->pdu_hdr.pduh_p.login_rsp.StatSN)));
457 1.1 agc
458 1.1 agc if (req_ccb == NULL) {
459 1.1 agc /* Duplicate?? */
460 1.1 agc DEBOUT(("Received duplicate login response (no associated CCB)\n"));
461 1.1 agc return -1;
462 1.1 agc }
463 1.1 agc
464 1.24 christos if (pdu->pdu_hdr.pduh_p.login_rsp.StatusClass) {
465 1.3 mlelstv DEBC(conn, 1, ("Login problem - Class = %x, Detail = %x\n",
466 1.24 christos pdu->pdu_hdr.pduh_p.login_rsp.StatusClass,
467 1.24 christos pdu->pdu_hdr.pduh_p.login_rsp.StatusDetail));
468 1.4 mlelstv wake_ccb(req_ccb, ISCSI_STATUS_LOGIN_FAILED);
469 1.3 mlelstv return 0;
470 1.3 mlelstv }
471 1.3 mlelstv
472 1.24 christos if (!conn->c_StatSN_buf.next_sn) {
473 1.24 christos conn->c_StatSN_buf.next_sn = conn->c_StatSN_buf.ExpSN =
474 1.24 christos ntohl(pdu->pdu_hdr.pduh_p.login_rsp.StatSN) + 1;
475 1.24 christos } else if (check_StatSN(conn, pdu->pdu_hdr.pduh_p.login_rsp.StatSN, TRUE))
476 1.1 agc return -1;
477 1.1 agc
478 1.24 christos if (pdu->pdu_temp_data_len) {
479 1.3 mlelstv if ((rc = collect_text_data(pdu, req_ccb)) != 0)
480 1.1 agc return max(rc, 0);
481 1.1 agc }
482 1.1 agc
483 1.3 mlelstv negotiate_login(conn, pdu, req_ccb);
484 1.1 agc
485 1.3 mlelstv /* negotiate_login will decide whether login is complete or not */
486 1.1 agc return 0;
487 1.1 agc }
488 1.1 agc
489 1.1 agc
490 1.1 agc /*
491 1.1 agc * receive_text_response_pdu
492 1.1 agc * Handle receipt of a text response PDU.
493 1.1 agc *
494 1.1 agc * Parameter:
495 1.1 agc * conn The connection
496 1.1 agc * pdu The PDU
497 1.1 agc * req_CCB The CCB associated with the original request (if any)
498 1.1 agc */
499 1.1 agc
500 1.1 agc STATIC int
501 1.1 agc receive_text_response_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
502 1.1 agc {
503 1.1 agc int rc;
504 1.1 agc
505 1.1 agc DEBC(conn, 9, ("Received Text Response PDU, op=%x, flags=%x\n",
506 1.24 christos pdu->pdu_hdr.pduh_Opcode, pdu->pdu_hdr.pduh_Flags));
507 1.1 agc
508 1.24 christos if (check_StatSN(conn, pdu->pdu_hdr.pduh_p.text_rsp.StatSN, TRUE)) {
509 1.1 agc return -1;
510 1.1 agc }
511 1.1 agc if (req_ccb == NULL) {
512 1.1 agc DEBOUT(("Received unsolicited text response\n"));
513 1.1 agc handle_connection_error(conn, ISCSI_STATUS_TARGET_ERROR,
514 1.1 agc LOGOUT_CONNECTION);
515 1.1 agc return -1;
516 1.1 agc }
517 1.1 agc
518 1.24 christos if (req_ccb->ccb_pdu_waiting != NULL) {
519 1.12 mlelstv ccb_timeout_start(req_ccb, COMMAND_TIMEOUT);
520 1.24 christos req_ccb->ccb_num_timeouts = 0;
521 1.1 agc }
522 1.1 agc
523 1.1 agc if ((rc = collect_text_data(pdu, req_ccb)) != 0) {
524 1.1 agc return max(0, rc);
525 1.1 agc }
526 1.1 agc negotiate_text(conn, pdu, req_ccb);
527 1.1 agc
528 1.1 agc return 0;
529 1.1 agc }
530 1.1 agc
531 1.1 agc
532 1.1 agc /*
533 1.1 agc * receive_logout_pdu
534 1.1 agc * Handle receipt of a logout response PDU.
535 1.1 agc *
536 1.1 agc * Parameter:
537 1.1 agc * conn The connection
538 1.1 agc * pdu The PDU
539 1.1 agc * req_CCB The CCB associated with the original request (if any)
540 1.1 agc */
541 1.1 agc
542 1.1 agc STATIC int
543 1.1 agc receive_logout_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
544 1.1 agc {
545 1.1 agc bool otherconn;
546 1.1 agc uint8_t response;
547 1.4 mlelstv uint32_t status;
548 1.1 agc
549 1.24 christos otherconn = (req_ccb != NULL) ? (req_ccb->ccb_flags & CCBF_OTHERCONN) != 0 : 1;
550 1.24 christos response = pdu->pdu_hdr.pduh_OpcodeSpecific [0];
551 1.1 agc DEBC(conn, 1,
552 1.1 agc ("Received Logout PDU - CCB = %p, otherconn=%d, response=%d\n",
553 1.1 agc req_ccb, otherconn, response));
554 1.1 agc
555 1.1 agc if (req_ccb == NULL)
556 1.1 agc return 0;
557 1.1 agc
558 1.24 christos if (otherconn && check_StatSN(conn, pdu->pdu_hdr.pduh_p.logout_rsp.StatSN, TRUE))
559 1.1 agc return -1;
560 1.1 agc
561 1.1 agc switch (response) {
562 1.1 agc case 0:
563 1.4 mlelstv status = ISCSI_STATUS_SUCCESS;
564 1.1 agc break;
565 1.1 agc case 1:
566 1.4 mlelstv status = ISCSI_STATUS_LOGOUT_CID_NOT_FOUND;
567 1.1 agc break;
568 1.1 agc case 2:
569 1.4 mlelstv status = ISCSI_STATUS_LOGOUT_RECOVERY_NS;
570 1.1 agc break;
571 1.1 agc default:
572 1.4 mlelstv status = ISCSI_STATUS_LOGOUT_ERROR;
573 1.1 agc break;
574 1.1 agc }
575 1.1 agc
576 1.24 christos if (conn->c_session->s_ErrorRecoveryLevel >= 2 && response != 1) {
577 1.24 christos connection_t *refconn = (otherconn) ? req_ccb->ccb_par : conn;
578 1.1 agc
579 1.24 christos refconn->c_Time2Wait = ntohs(pdu->pdu_hdr.pduh_p.logout_rsp.Time2Wait);
580 1.24 christos refconn->c_Time2Retain = ntohs(pdu->pdu_hdr.pduh_p.logout_rsp.Time2Retain);
581 1.1 agc }
582 1.1 agc
583 1.4 mlelstv wake_ccb(req_ccb, status);
584 1.1 agc
585 1.27 mlelstv mutex_enter(&conn->c_lock);
586 1.24 christos if (!otherconn && conn->c_state == ST_LOGOUT_SENT) {
587 1.24 christos conn->c_terminating = ISCSI_STATUS_LOGOUT;
588 1.24 christos conn->c_state = ST_SETTLING;
589 1.24 christos conn->c_loggedout = (response) ? LOGOUT_FAILED : LOGOUT_SUCCESS;
590 1.27 mlelstv mutex_exit(&conn->c_lock);
591 1.1 agc
592 1.12 mlelstv connection_timeout_stop(conn);
593 1.1 agc
594 1.1 agc /* let send thread take over next step of cleanup */
595 1.24 christos mutex_enter(&conn->c_lock);
596 1.24 christos cv_broadcast(&conn->c_conn_cv);
597 1.1 agc }
598 1.27 mlelstv mutex_exit(&conn->c_lock);
599 1.1 agc
600 1.1 agc return !otherconn;
601 1.1 agc }
602 1.1 agc
603 1.1 agc
604 1.1 agc /*
605 1.1 agc * receive_data_in_pdu
606 1.1 agc * Handle receipt of a data in PDU.
607 1.1 agc *
608 1.1 agc * Parameter:
609 1.1 agc * conn The connection
610 1.1 agc * pdu The PDU
611 1.1 agc * req_CCB The CCB associated with the original request (if any)
612 1.1 agc */
613 1.1 agc
614 1.1 agc STATIC int
615 1.1 agc receive_data_in_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
616 1.1 agc {
617 1.1 agc uint32_t dsl, sn;
618 1.1 agc bool done;
619 1.1 agc int rc;
620 1.1 agc
621 1.24 christos dsl = ntoh3(pdu->pdu_hdr.pduh_DataSegmentLength);
622 1.1 agc
623 1.24 christos if (req_ccb == NULL || !req_ccb->ccb_data_in || !req_ccb->ccb_data_len) {
624 1.1 agc DEBOUT(("Received Data In, but req_ccb not waiting for it, ignored\n"));
625 1.1 agc return 0;
626 1.1 agc }
627 1.24 christos req_ccb->ccb_flags |= CCBF_GOT_RSP;
628 1.1 agc
629 1.24 christos if (req_ccb->ccb_pdu_waiting != NULL) {
630 1.12 mlelstv ccb_timeout_start(req_ccb, COMMAND_TIMEOUT);
631 1.24 christos req_ccb->ccb_num_timeouts = 0;
632 1.1 agc }
633 1.1 agc
634 1.24 christos sn = ntohl(pdu->pdu_hdr.pduh_p.data_in.DataSN);
635 1.1 agc
636 1.24 christos if ((rc = add_sernum(&req_ccb->ccb_DataSN_buf, sn)) != 1) {
637 1.1 agc if (!rc) {
638 1.1 agc return -1;
639 1.1 agc }
640 1.1 agc if (rc < 0) {
641 1.1 agc DEBOUT(("Excessive outstanding Data PDUs\n"));
642 1.24 christos handle_connection_error(req_ccb->ccb_connection,
643 1.1 agc ISCSI_STATUS_PDUS_LOST, LOGOUT_CONNECTION);
644 1.1 agc return -1;
645 1.1 agc }
646 1.1 agc DEBOUT(("Missing Data PDUs: First %d, num: %d\n",
647 1.24 christos req_ccb->ccb_DataSN_buf.ExpSN, rc - 1));
648 1.1 agc
649 1.24 christos if (conn->c_state == ST_FULL_FEATURE &&
650 1.24 christos conn->c_session->s_ErrorRecoveryLevel) {
651 1.24 christos snack_missing(req_ccb->ccb_connection, req_ccb,
652 1.24 christos SNACK_DATA_NAK, req_ccb->ccb_DataSN_buf.ExpSN,
653 1.1 agc rc - 1);
654 1.1 agc } else {
655 1.1 agc DEBOUT(("Killing connection (State=%d, ErrorRecoveryLevel=%d)\n",
656 1.24 christos conn->c_state, conn->c_session->s_ErrorRecoveryLevel));
657 1.1 agc handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST,
658 1.1 agc LOGOUT_CONNECTION);
659 1.1 agc return -1;
660 1.1 agc }
661 1.1 agc }
662 1.1 agc
663 1.24 christos ack_sernum(&req_ccb->ccb_DataSN_buf, sn);
664 1.1 agc
665 1.24 christos req_ccb->ccb_xfer_len += dsl;
666 1.1 agc
667 1.24 christos if ((pdu->pdu_hdr.pduh_Flags & FLAG_ACK) && conn->c_session->s_ErrorRecoveryLevel)
668 1.1 agc send_snack(conn, pdu, req_ccb, SNACK_DATA_ACK);
669 1.1 agc
670 1.24 christos done = sn_empty(&req_ccb->ccb_DataSN_buf);
671 1.1 agc
672 1.24 christos if (pdu->pdu_hdr.pduh_Flags & FLAG_STATUS) {
673 1.21 mlelstv DEBC(conn, 10, ("Rx Data In %d, done = %d\n",
674 1.24 christos req_ccb->ccb_CmdSN, done));
675 1.1 agc
676 1.24 christos req_ccb->ccb_flags |= CCBF_COMPLETE;
677 1.1 agc /* successful transfer, reset recover count */
678 1.24 christos conn->c_recover = 0;
679 1.1 agc
680 1.4 mlelstv if (done)
681 1.4 mlelstv wake_ccb(req_ccb, ISCSI_STATUS_SUCCESS);
682 1.24 christos if (check_StatSN(conn, pdu->pdu_hdr.pduh_p.data_in.StatSN, done))
683 1.1 agc return -1;
684 1.4 mlelstv
685 1.24 christos } else if (done && (req_ccb->ccb_flags & CCBF_COMPLETE)) {
686 1.4 mlelstv wake_ccb(req_ccb, ISCSI_STATUS_SUCCESS);
687 1.1 agc }
688 1.1 agc /* else wait for command response */
689 1.1 agc
690 1.1 agc return 0;
691 1.1 agc }
692 1.1 agc
693 1.1 agc
694 1.1 agc /*
695 1.1 agc * receive_r2t_pdu
696 1.1 agc * Handle receipt of a R2T PDU.
697 1.1 agc *
698 1.1 agc * Parameter:
699 1.1 agc * conn The connection
700 1.1 agc * pdu The PDU
701 1.1 agc * req_CCB The CCB associated with the original request (if any)
702 1.1 agc */
703 1.1 agc
704 1.1 agc STATIC int
705 1.1 agc receive_r2t_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
706 1.1 agc {
707 1.1 agc
708 1.1 agc DEBC(conn, 10, ("Received R2T PDU - CCB = %p\n", req_ccb));
709 1.1 agc
710 1.1 agc if (req_ccb != NULL) {
711 1.24 christos if (req_ccb->ccb_pdu_waiting != NULL) {
712 1.12 mlelstv ccb_timeout_start(req_ccb, COMMAND_TIMEOUT);
713 1.24 christos req_ccb->ccb_num_timeouts = 0;
714 1.1 agc }
715 1.1 agc send_data_out(conn, pdu, req_ccb, CCBDISP_NOWAIT, TRUE);
716 1.1 agc }
717 1.1 agc
718 1.1 agc return 0;
719 1.1 agc }
720 1.1 agc
721 1.1 agc
722 1.1 agc /*
723 1.1 agc * receive_command_response_pdu
724 1.1 agc * Handle receipt of a command response PDU.
725 1.1 agc *
726 1.1 agc * Parameter:
727 1.1 agc * conn The connection
728 1.1 agc * pdu The PDU
729 1.1 agc * req_CCB The CCB associated with the original request (if any)
730 1.1 agc */
731 1.1 agc
732 1.1 agc STATIC int
733 1.1 agc receive_command_response_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
734 1.1 agc {
735 1.1 agc int len, rc;
736 1.1 agc bool done;
737 1.4 mlelstv uint32_t status;
738 1.1 agc
739 1.1 agc /* Read any provided data */
740 1.24 christos if (pdu->pdu_temp_data_len && req_ccb != NULL && req_ccb->ccb_sense_len_req) {
741 1.24 christos len = min(req_ccb->ccb_sense_len_req,
742 1.24 christos ntohs(*((uint16_t *) pdu->pdu_temp_data)));
743 1.24 christos memcpy(req_ccb->ccb_sense_ptr, ((uint16_t *) pdu->pdu_temp_data) + 1,
744 1.1 agc len);
745 1.24 christos req_ccb->ccb_sense_len_got = len;
746 1.1 agc }
747 1.1 agc
748 1.1 agc if (req_ccb == NULL) {
749 1.1 agc /* Assume duplicate... */
750 1.1 agc DEBOUT(("Possibly duplicate command response (no associated CCB)\n"));
751 1.1 agc return -1;
752 1.1 agc }
753 1.1 agc
754 1.25 mlelstv if (req_ccb->ccb_flags & CCBF_COMPLETE) {
755 1.25 mlelstv DEBOUT(("Possibly duplicate command response (tagged as COMPLETE)\n"));
756 1.25 mlelstv return -1;
757 1.25 mlelstv }
758 1.25 mlelstv
759 1.24 christos if (req_ccb->ccb_pdu_waiting != NULL) {
760 1.12 mlelstv ccb_timeout_start(req_ccb, COMMAND_TIMEOUT);
761 1.24 christos req_ccb->ccb_num_timeouts = 0;
762 1.1 agc }
763 1.1 agc
764 1.24 christos req_ccb->ccb_flags |= CCBF_COMPLETE;
765 1.24 christos conn->c_recover = 0; /* successful transfer, reset recover count */
766 1.1 agc
767 1.24 christos if (pdu->pdu_hdr.pduh_OpcodeSpecific[0]) { /* Response */
768 1.4 mlelstv status = ISCSI_STATUS_TARGET_FAILURE;
769 1.1 agc } else {
770 1.24 christos switch (pdu->pdu_hdr.pduh_OpcodeSpecific[1]) { /* Status */
771 1.1 agc case 0x00:
772 1.4 mlelstv status = ISCSI_STATUS_SUCCESS;
773 1.1 agc break;
774 1.1 agc
775 1.1 agc case 0x02:
776 1.4 mlelstv status = ISCSI_STATUS_CHECK_CONDITION;
777 1.1 agc break;
778 1.1 agc
779 1.1 agc case 0x08:
780 1.4 mlelstv status = ISCSI_STATUS_TARGET_BUSY;
781 1.1 agc break;
782 1.1 agc
783 1.1 agc default:
784 1.4 mlelstv status = ISCSI_STATUS_TARGET_ERROR;
785 1.1 agc break;
786 1.1 agc }
787 1.1 agc }
788 1.1 agc
789 1.24 christos if (pdu->pdu_hdr.pduh_Flags & (FLAG_OVERFLOW | FLAG_UNDERFLOW))
790 1.24 christos req_ccb->ccb_residual = ntohl(pdu->pdu_hdr.pduh_p.response.ResidualCount);
791 1.1 agc
792 1.24 christos done = status || sn_empty(&req_ccb->ccb_DataSN_buf);
793 1.1 agc
794 1.21 mlelstv DEBC(conn, 10, ("Rx Response: CmdSN %d, rsp = %x, status = %x\n",
795 1.24 christos req_ccb->ccb_CmdSN,
796 1.24 christos pdu->pdu_hdr.pduh_OpcodeSpecific[0],
797 1.24 christos pdu->pdu_hdr.pduh_OpcodeSpecific[1]));
798 1.1 agc
799 1.24 christos rc = check_StatSN(conn, pdu->pdu_hdr.pduh_p.response.StatSN, done);
800 1.1 agc
801 1.1 agc if (done)
802 1.4 mlelstv wake_ccb(req_ccb, status);
803 1.1 agc
804 1.1 agc return rc;
805 1.1 agc }
806 1.1 agc
807 1.1 agc
808 1.1 agc /*
809 1.1 agc * receive_asynch_pdu
810 1.1 agc * Handle receipt of an asynchronous message PDU.
811 1.1 agc *
812 1.1 agc * Parameter:
813 1.1 agc * conn The connection
814 1.1 agc * pdu The PDU
815 1.1 agc */
816 1.1 agc
817 1.1 agc STATIC int
818 1.1 agc receive_asynch_pdu(connection_t *conn, pdu_t *pdu)
819 1.1 agc {
820 1.1 agc
821 1.24 christos DEBOUT(("Received Asynch PDU, Event %d\n", pdu->pdu_hdr.pduh_p.asynch.AsyncEvent));
822 1.1 agc
823 1.24 christos switch (pdu->pdu_hdr.pduh_p.asynch.AsyncEvent) {
824 1.1 agc case 0: /* SCSI Asynch event. Don't know what to do with it... */
825 1.1 agc break;
826 1.1 agc
827 1.26 mlelstv case 1: /* Target requests logout. */
828 1.24 christos if (conn->c_session->s_active_connections > 1) {
829 1.1 agc kill_connection(conn, ISCSI_STATUS_TARGET_LOGOUT,
830 1.1 agc LOGOUT_CONNECTION, FALSE);
831 1.1 agc } else {
832 1.26 mlelstv kill_session(conn->c_session->s_id,
833 1.26 mlelstv ISCSI_STATUS_TARGET_LOGOUT, LOGOUT_SESSION,
834 1.26 mlelstv FALSE);
835 1.1 agc }
836 1.1 agc break;
837 1.1 agc
838 1.26 mlelstv case 2: /* Target is dropping connection */
839 1.24 christos conn = find_connection(conn->c_session,
840 1.26 mlelstv ntohs(pdu->pdu_hdr.pduh_p.asynch.Parameter1));
841 1.1 agc if (conn != NULL) {
842 1.26 mlelstv conn->c_Time2Wait =
843 1.26 mlelstv ntohs(pdu->pdu_hdr.pduh_p.asynch.Parameter2);
844 1.26 mlelstv conn->c_Time2Retain =
845 1.26 mlelstv ntohs(pdu->pdu_hdr.pduh_p.asynch.Parameter3);
846 1.1 agc kill_connection(conn, ISCSI_STATUS_TARGET_DROP,
847 1.1 agc NO_LOGOUT, TRUE);
848 1.1 agc }
849 1.1 agc break;
850 1.1 agc
851 1.26 mlelstv case 3: /* Target is dropping all connections of session */
852 1.26 mlelstv conn->c_session->s_DefaultTime2Wait =
853 1.26 mlelstv ntohs(pdu->pdu_hdr.pduh_p.asynch.Parameter2);
854 1.26 mlelstv conn->c_session->s_DefaultTime2Retain =
855 1.26 mlelstv ntohs(pdu->pdu_hdr.pduh_p.asynch.Parameter3);
856 1.26 mlelstv kill_session(conn->c_session->s_id,
857 1.26 mlelstv ISCSI_STATUS_TARGET_DROP, NO_LOGOUT,
858 1.26 mlelstv TRUE);
859 1.1 agc break;
860 1.1 agc
861 1.26 mlelstv case 4: /* Target requests parameter negotiation */
862 1.1 agc start_text_negotiation(conn);
863 1.1 agc break;
864 1.1 agc
865 1.1 agc default:
866 1.1 agc /* ignore */
867 1.1 agc break;
868 1.1 agc }
869 1.1 agc return 0;
870 1.1 agc }
871 1.1 agc
872 1.1 agc
873 1.1 agc /*
874 1.1 agc * receive_reject_pdu
875 1.1 agc * Handle receipt of a reject PDU.
876 1.1 agc *
877 1.1 agc * Parameter:
878 1.1 agc * conn The connection
879 1.1 agc * pdu The PDU
880 1.1 agc */
881 1.1 agc
882 1.1 agc STATIC int
883 1.1 agc receive_reject_pdu(connection_t *conn, pdu_t *pdu)
884 1.1 agc {
885 1.1 agc pdu_header_t *hpdu;
886 1.1 agc ccb_t *req_ccb;
887 1.4 mlelstv uint32_t status;
888 1.1 agc
889 1.1 agc DEBOUT(("Received Reject PDU, reason = %x, data_len = %d\n",
890 1.24 christos pdu->pdu_hdr.pduh_OpcodeSpecific[0], pdu->pdu_temp_data_len));
891 1.1 agc
892 1.24 christos if (pdu->pdu_temp_data_len >= BHS_SIZE) {
893 1.24 christos hpdu = (pdu_header_t *) pdu->pdu_temp_data;
894 1.24 christos req_ccb = ccb_from_itt(conn, hpdu->pduh_InitiatorTaskTag);
895 1.1 agc
896 1.1 agc DEBC(conn, 9, ("Reject PDU ITT (ccb)= %x (%p)\n",
897 1.24 christos hpdu->pduh_InitiatorTaskTag, req_ccb));
898 1.1 agc if (!req_ccb) {
899 1.1 agc return 0;
900 1.1 agc }
901 1.24 christos switch (pdu->pdu_hdr.pduh_OpcodeSpecific[0]) {
902 1.1 agc case REJECT_DIGEST_ERROR:
903 1.1 agc /* don't retransmit data out */
904 1.24 christos if ((hpdu->pduh_Opcode & OPCODE_MASK) == IOP_SCSI_Data_out)
905 1.1 agc return 0;
906 1.1 agc resend_pdu(req_ccb);
907 1.1 agc return 0;
908 1.1 agc
909 1.1 agc case REJECT_IMMED_COMMAND:
910 1.1 agc case REJECT_LONG_OPERATION:
911 1.1 agc resend_pdu(req_ccb);
912 1.1 agc return 0;
913 1.1 agc
914 1.1 agc case REJECT_SNACK:
915 1.1 agc case REJECT_PROTOCOL_ERROR:
916 1.4 mlelstv status = ISCSI_STATUS_PROTOCOL_ERROR;
917 1.1 agc break;
918 1.1 agc
919 1.1 agc case REJECT_CMD_NOT_SUPPORTED:
920 1.4 mlelstv status = ISCSI_STATUS_CMD_NOT_SUPPORTED;
921 1.1 agc break;
922 1.1 agc
923 1.1 agc case REJECT_INVALID_PDU_FIELD:
924 1.4 mlelstv status = ISCSI_STATUS_PDU_ERROR;
925 1.1 agc break;
926 1.1 agc
927 1.1 agc default:
928 1.4 mlelstv status = ISCSI_STATUS_GENERAL_ERROR;
929 1.1 agc break;
930 1.1 agc }
931 1.1 agc
932 1.4 mlelstv wake_ccb(req_ccb, status);
933 1.1 agc handle_connection_error(conn, ISCSI_STATUS_PROTOCOL_ERROR,
934 1.1 agc LOGOUT_CONNECTION);
935 1.1 agc }
936 1.1 agc return 0;
937 1.1 agc }
938 1.1 agc
939 1.1 agc
940 1.1 agc /*
941 1.1 agc * receive_task_management_pdu
942 1.1 agc * Handle receipt of a task management PDU.
943 1.1 agc *
944 1.1 agc * Parameter:
945 1.1 agc * conn The connection
946 1.1 agc * pdu The PDU
947 1.1 agc * req_CCB The CCB associated with the original request (if any)
948 1.1 agc */
949 1.1 agc
950 1.1 agc STATIC int
951 1.1 agc receive_task_management_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
952 1.1 agc {
953 1.4 mlelstv uint32_t status;
954 1.1 agc
955 1.1 agc DEBC(conn, 2, ("Received Task Management PDU, response %d, req_ccb %p\n",
956 1.24 christos pdu->pdu_hdr.pduh_OpcodeSpecific[0], req_ccb));
957 1.1 agc
958 1.1 agc if (req_ccb != NULL) {
959 1.24 christos switch (pdu->pdu_hdr.pduh_OpcodeSpecific[0]) { /* Response */
960 1.1 agc case 0:
961 1.4 mlelstv status = ISCSI_STATUS_SUCCESS;
962 1.1 agc break;
963 1.1 agc case 1:
964 1.4 mlelstv status = ISCSI_STATUS_TASK_NOT_FOUND;
965 1.1 agc break;
966 1.1 agc case 2:
967 1.4 mlelstv status = ISCSI_STATUS_LUN_NOT_FOUND;
968 1.1 agc break;
969 1.1 agc case 3:
970 1.4 mlelstv status = ISCSI_STATUS_TASK_ALLEGIANT;
971 1.1 agc break;
972 1.1 agc case 4:
973 1.4 mlelstv status = ISCSI_STATUS_CANT_REASSIGN;
974 1.1 agc break;
975 1.1 agc case 5:
976 1.4 mlelstv status = ISCSI_STATUS_FUNCTION_UNSUPPORTED;
977 1.1 agc break;
978 1.1 agc case 6:
979 1.4 mlelstv status = ISCSI_STATUS_FUNCTION_NOT_AUTHORIZED;
980 1.1 agc break;
981 1.1 agc case 255:
982 1.4 mlelstv status = ISCSI_STATUS_FUNCTION_REJECTED;
983 1.1 agc break;
984 1.1 agc default:
985 1.4 mlelstv status = ISCSI_STATUS_UNKNOWN_REASON;
986 1.1 agc break;
987 1.1 agc }
988 1.4 mlelstv wake_ccb(req_ccb, status);
989 1.1 agc }
990 1.1 agc
991 1.24 christos check_StatSN(conn, pdu->pdu_hdr.pduh_p.task_rsp.StatSN, TRUE);
992 1.1 agc
993 1.1 agc return 0;
994 1.1 agc }
995 1.1 agc
996 1.1 agc
997 1.1 agc /*
998 1.1 agc * receive_nop_in_pdu
999 1.1 agc * Handle receipt of a Nop-In PDU.
1000 1.1 agc *
1001 1.1 agc * Parameter:
1002 1.1 agc * conn The connection
1003 1.1 agc * pdu The PDU
1004 1.1 agc * req_CCB The CCB associated with the original request (if any)
1005 1.1 agc */
1006 1.1 agc
1007 1.1 agc STATIC int
1008 1.1 agc receive_nop_in_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
1009 1.1 agc {
1010 1.1 agc DEBC(conn, 10,
1011 1.21 mlelstv ("Received NOP_In PDU, req_ccb=%p, ITT=%x, TTT=%x, StatSN=%u\n",
1012 1.24 christos req_ccb, pdu->pdu_hdr.pduh_InitiatorTaskTag,
1013 1.24 christos pdu->pdu_hdr.pduh_p.nop_in.TargetTransferTag,
1014 1.24 christos ntohl(pdu->pdu_hdr.pduh_p.nop_in.StatSN)));
1015 1.1 agc
1016 1.24 christos if (pdu->pdu_hdr.pduh_InitiatorTaskTag == 0xffffffff) {
1017 1.1 agc /* this is a target ping - respond with a pong */
1018 1.24 christos if (pdu->pdu_hdr.pduh_p.nop_in.TargetTransferTag != 0xffffffff)
1019 1.1 agc send_nop_out(conn, pdu);
1020 1.1 agc
1021 1.1 agc /*
1022 1.1 agc Any receive resets the connection timeout, but we got a ping, which
1023 1.1 agc means that it's likely the other side was waiting for something to
1024 1.1 agc happen on the connection. If we aren't idle, send a ping right
1025 1.1 agc away to synch counters (don't synch on this ping because other
1026 1.1 agc PDUs may be on the way).
1027 1.1 agc */
1028 1.24 christos if (TAILQ_FIRST(&conn->c_ccbs_waiting) != NULL)
1029 1.1 agc send_nop_out(conn, NULL);
1030 1.1 agc } else if (req_ccb != NULL) {
1031 1.1 agc /* this is a solicited ping, check CmdSN for lost commands */
1032 1.1 agc /* and advance StatSN */
1033 1.24 christos check_CmdSN(conn, pdu->pdu_hdr.pduh_p.nop_in.ExpCmdSN);
1034 1.1 agc
1035 1.4 mlelstv wake_ccb(req_ccb, ISCSI_STATUS_SUCCESS);
1036 1.1 agc
1037 1.24 christos check_StatSN(conn, pdu->pdu_hdr.pduh_p.nop_in.StatSN, TRUE);
1038 1.13 mlelstv } else {
1039 1.13 mlelstv DEBC(conn, 0, ("Received unsolicted NOP_In, itt=%08x\n",
1040 1.24 christos pdu->pdu_hdr.pduh_InitiatorTaskTag));
1041 1.1 agc }
1042 1.1 agc
1043 1.1 agc return 0;
1044 1.1 agc }
1045 1.1 agc
1046 1.1 agc
1047 1.1 agc /*
1048 1.1 agc * receive_pdu
1049 1.1 agc * Get parameters, call the appropriate handler for a received PDU.
1050 1.1 agc *
1051 1.1 agc * Parameter:
1052 1.1 agc * conn The connection
1053 1.1 agc * pdu The PDU
1054 1.1 agc *
1055 1.1 agc * Returns: 0 on success, nonzero if the connection is broken.
1056 1.1 agc */
1057 1.1 agc
1058 1.1 agc STATIC int
1059 1.1 agc receive_pdu(connection_t *conn, pdu_t *pdu)
1060 1.1 agc {
1061 1.1 agc ccb_t *req_ccb;
1062 1.11 mlelstv int rc;
1063 1.16 mlelstv uint32_t MaxCmdSN, ExpCmdSN, digest;
1064 1.24 christos session_t *sess = conn->c_session;
1065 1.1 agc
1066 1.24 christos if (conn->c_HeaderDigest) {
1067 1.24 christos digest = gen_digest(&pdu->pdu_hdr, BHS_SIZE);
1068 1.24 christos if (digest != pdu->pdu_hdr.pduh_HeaderDigest) {
1069 1.1 agc DEBOUT(("Header Digest Error: comp = %08x, rx = %08x\n",
1070 1.24 christos digest, pdu->pdu_hdr.pduh_HeaderDigest));
1071 1.1 agc /* try to skip to next PDU */
1072 1.1 agc try_resynch_receive(conn);
1073 1.1 agc free_pdu(pdu);
1074 1.1 agc return 0;
1075 1.1 agc }
1076 1.1 agc }
1077 1.1 agc
1078 1.21 mlelstv DEBC(conn, 10, ("Received PDU StatSN=%u, ExpCmdSN=%u MaxCmdSN=%u ExpDataSN=%u\n",
1079 1.24 christos ntohl(pdu->pdu_hdr.pduh_p.response.StatSN),
1080 1.24 christos ntohl(pdu->pdu_hdr.pduh_p.response.ExpCmdSN),
1081 1.24 christos ntohl(pdu->pdu_hdr.pduh_p.response.MaxCmdSN),
1082 1.24 christos ntohl(pdu->pdu_hdr.pduh_p.response.ExpDataSN)));
1083 1.13 mlelstv
1084 1.24 christos req_ccb = ccb_from_itt(conn, pdu->pdu_hdr.pduh_InitiatorTaskTag);
1085 1.1 agc
1086 1.24 christos if (req_ccb != NULL && req_ccb->ccb_data_in && req_ccb->ccb_data_len &&
1087 1.24 christos (pdu->pdu_hdr.pduh_Opcode & OPCODE_MASK) == TOP_SCSI_Data_in) {
1088 1.1 agc uint32_t dsl, offset;
1089 1.1 agc
1090 1.24 christos dsl = ntoh3(pdu->pdu_hdr.pduh_DataSegmentLength);
1091 1.24 christos offset = ntohl(pdu->pdu_hdr.pduh_p.data_in.BufferOffset);
1092 1.1 agc
1093 1.24 christos if ((offset + dsl) > req_ccb->ccb_data_len) {
1094 1.1 agc DEBOUT(("Received more data than requested (len %d, offset %d)\n",
1095 1.1 agc dsl, offset));
1096 1.1 agc handle_connection_error(conn, ISCSI_STATUS_TARGET_ERROR, NO_LOGOUT);
1097 1.1 agc return 1;
1098 1.1 agc }
1099 1.1 agc DEBC(conn, 10,
1100 1.1 agc ("Received Data in PDU - CCB = %p, Datalen = %d, Offset = %d\n",
1101 1.1 agc req_ccb, dsl, offset));
1102 1.1 agc
1103 1.24 christos rc = read_pdu_data(pdu, req_ccb->ccb_data_ptr, offset);
1104 1.1 agc } else {
1105 1.1 agc rc = read_pdu_data(pdu, NULL, 0);
1106 1.1 agc }
1107 1.24 christos if (!rc && (conn->c_state <= ST_WINDING_DOWN ||
1108 1.24 christos (pdu->pdu_hdr.pduh_Opcode & OPCODE_MASK) == TOP_Logout_Response)) {
1109 1.1 agc
1110 1.24 christos switch (pdu->pdu_hdr.pduh_Opcode & OPCODE_MASK) {
1111 1.1 agc case TOP_NOP_In:
1112 1.1 agc rc = receive_nop_in_pdu(conn, pdu, req_ccb);
1113 1.1 agc break;
1114 1.1 agc
1115 1.1 agc case TOP_SCSI_Response:
1116 1.1 agc rc = receive_command_response_pdu(conn, pdu, req_ccb);
1117 1.1 agc break;
1118 1.1 agc
1119 1.1 agc case TOP_SCSI_Task_Management:
1120 1.1 agc rc = receive_task_management_pdu(conn, pdu, req_ccb);
1121 1.1 agc break;
1122 1.1 agc
1123 1.1 agc case TOP_Login_Response:
1124 1.1 agc rc = receive_login_pdu(conn, pdu, req_ccb);
1125 1.1 agc break;
1126 1.1 agc
1127 1.1 agc case TOP_Text_Response:
1128 1.1 agc rc = receive_text_response_pdu(conn, pdu, req_ccb);
1129 1.1 agc break;
1130 1.1 agc
1131 1.1 agc case TOP_SCSI_Data_in:
1132 1.1 agc rc = receive_data_in_pdu(conn, pdu, req_ccb);
1133 1.1 agc break;
1134 1.1 agc
1135 1.1 agc case TOP_Logout_Response:
1136 1.1 agc rc = receive_logout_pdu(conn, pdu, req_ccb);
1137 1.1 agc break;
1138 1.1 agc
1139 1.1 agc case TOP_R2T:
1140 1.1 agc rc = receive_r2t_pdu(conn, pdu, req_ccb);
1141 1.1 agc break;
1142 1.1 agc
1143 1.1 agc case TOP_Asynchronous_Message:
1144 1.1 agc rc = receive_asynch_pdu(conn, pdu);
1145 1.1 agc break;
1146 1.1 agc
1147 1.1 agc case TOP_Reject:
1148 1.1 agc rc = receive_reject_pdu(conn, pdu);
1149 1.1 agc break;
1150 1.1 agc
1151 1.1 agc default:
1152 1.24 christos DEBOUT(("Received Invalid Opcode %x\n", pdu->pdu_hdr.pduh_Opcode));
1153 1.1 agc try_resynch_receive(conn);
1154 1.1 agc rc = -1;
1155 1.1 agc break;
1156 1.1 agc }
1157 1.1 agc }
1158 1.1 agc
1159 1.1 agc free_pdu(pdu);
1160 1.1 agc if (rc)
1161 1.1 agc return rc;
1162 1.1 agc
1163 1.1 agc /* MaxCmdSN and ExpCmdSN are in the same place in all received PDUs */
1164 1.24 christos ExpCmdSN = ntohl(pdu->pdu_hdr.pduh_p.nop_in.ExpCmdSN);
1165 1.24 christos MaxCmdSN = ntohl(pdu->pdu_hdr.pduh_p.nop_in.MaxCmdSN);
1166 1.1 agc
1167 1.1 agc /* received a valid frame, reset timeout */
1168 1.26 mlelstv conn->c_num_timeouts = 0;
1169 1.24 christos if ((pdu->pdu_hdr.pduh_Opcode & OPCODE_MASK) == TOP_NOP_In &&
1170 1.24 christos TAILQ_EMPTY(&conn->c_ccbs_waiting))
1171 1.24 christos connection_timeout_start(conn, conn->c_idle_timeout_val);
1172 1.10 joerg else
1173 1.12 mlelstv connection_timeout_start(conn, CONNECTION_TIMEOUT);
1174 1.1 agc
1175 1.21 mlelstv /* Update session window */
1176 1.24 christos mutex_enter(&sess->s_lock);
1177 1.21 mlelstv if (sn_a_le_b(ExpCmdSN - 1, MaxCmdSN)) {
1178 1.24 christos if (sn_a_lt_b(sess->s_ExpCmdSN, ExpCmdSN))
1179 1.24 christos sess->s_ExpCmdSN = ExpCmdSN;
1180 1.24 christos if (sn_a_lt_b(sess->s_MaxCmdSN, MaxCmdSN))
1181 1.24 christos sess->s_MaxCmdSN = MaxCmdSN;
1182 1.16 mlelstv }
1183 1.24 christos mutex_exit(&sess->s_lock);
1184 1.1 agc
1185 1.1 agc return 0;
1186 1.1 agc }
1187 1.1 agc
1188 1.1 agc /*****************************************************************************/
1189 1.1 agc
1190 1.1 agc /*
1191 1.1 agc * iscsi_receive_thread
1192 1.1 agc * Per connection thread handling receive data.
1193 1.1 agc *
1194 1.1 agc * Parameter:
1195 1.1 agc * conn The connection
1196 1.1 agc */
1197 1.1 agc
1198 1.1 agc void
1199 1.1 agc iscsi_rcv_thread(void *par)
1200 1.1 agc {
1201 1.1 agc connection_t *conn = (connection_t *) par;
1202 1.1 agc pdu_t *pdu;
1203 1.1 agc size_t hlen;
1204 1.1 agc
1205 1.1 agc do {
1206 1.24 christos while (!conn->c_terminating) {
1207 1.4 mlelstv pdu = get_pdu(conn, TRUE);
1208 1.21 mlelstv if (pdu == NULL) {
1209 1.24 christos KASSERT(conn->c_terminating);
1210 1.21 mlelstv break;
1211 1.21 mlelstv }
1212 1.21 mlelstv
1213 1.24 christos pdu->pdu_uio.uio_iov = pdu->pdu_io_vec;
1214 1.24 christos UIO_SETUP_SYSSPACE(&pdu->pdu_uio);
1215 1.24 christos pdu->pdu_uio.uio_iovcnt = 1;
1216 1.24 christos pdu->pdu_uio.uio_rw = UIO_READ;
1217 1.24 christos
1218 1.24 christos pdu->pdu_io_vec[0].iov_base = &pdu->pdu_hdr;
1219 1.24 christos hlen = (conn->c_HeaderDigest) ? BHS_SIZE + 4 : BHS_SIZE;
1220 1.24 christos pdu->pdu_io_vec[0].iov_len = hlen;
1221 1.24 christos pdu->pdu_uio.uio_resid = hlen;
1222 1.1 agc
1223 1.1 agc DEBC(conn, 99, ("Receive thread waiting for data\n"));
1224 1.24 christos if (my_soo_read(conn, &pdu->pdu_uio, MSG_WAITALL)) {
1225 1.1 agc free_pdu(pdu);
1226 1.1 agc break;
1227 1.1 agc }
1228 1.1 agc /* Check again for header digest */
1229 1.1 agc /* (it may have changed during the wait) */
1230 1.24 christos if (hlen == BHS_SIZE && conn->c_HeaderDigest) {
1231 1.24 christos pdu->pdu_uio.uio_iov = pdu->pdu_io_vec;
1232 1.24 christos pdu->pdu_uio.uio_iovcnt = 1;
1233 1.24 christos pdu->pdu_io_vec[0].iov_base = &pdu->pdu_hdr.pduh_HeaderDigest;
1234 1.24 christos pdu->pdu_io_vec[0].iov_len = 4;
1235 1.24 christos pdu->pdu_uio.uio_resid = 4;
1236 1.24 christos if (my_soo_read(conn, &pdu->pdu_uio, MSG_WAITALL)) {
1237 1.1 agc free_pdu(pdu);
1238 1.1 agc break;
1239 1.1 agc }
1240 1.1 agc }
1241 1.1 agc
1242 1.1 agc if (receive_pdu(conn, pdu) > 0) {
1243 1.1 agc break;
1244 1.1 agc }
1245 1.1 agc }
1246 1.24 christos mutex_enter(&conn->c_lock);
1247 1.24 christos if (!conn->c_destroy) {
1248 1.24 christos cv_timedwait(&conn->c_idle_cv, &conn->c_lock, CONNECTION_IDLE_TIMEOUT);
1249 1.1 agc }
1250 1.24 christos mutex_exit(&conn->c_lock);
1251 1.24 christos } while (!conn->c_destroy);
1252 1.1 agc
1253 1.24 christos conn->c_rcvproc = NULL;
1254 1.1 agc DEBC(conn, 5, ("Receive thread exits\n"));
1255 1.1 agc kthread_exit(0);
1256 1.1 agc }
1257