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