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