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