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