1/* $NetBSD: iscsi_rcv.c,v 1.1.8.1 2012/06/12 19:41:25 riz 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 51STATIC int 52my_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 84STATIC void 85try_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 124STATIC ccb_t * 125ccb_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 157STATIC int 158read_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 */ 270STATIC int 271collect_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 req_ccb->status = ISCSI_STATUS_NO_RESOURCES; 282 return 1; 283 } 284 memcpy(newp, req_ccb->text_data, req_ccb->text_len); 285 memcpy(&newp[req_ccb->text_len], pdu->temp_data, pdu->temp_data_len); 286 287 free(req_ccb->text_data, M_TEMP); 288 free(pdu->temp_data, M_TEMP); 289 290 req_ccb->text_data = NULL; 291 pdu->temp_data = newp; 292 pdu->temp_data_len = nlen; 293 } 294 295 if (pdu->pdu.Flags & FLAG_CONTINUE) { 296 req_ccb->text_data = pdu->temp_data; 297 req_ccb->text_len = pdu->temp_data_len; 298 pdu->temp_data = NULL; 299 300 acknowledge_text(req_ccb->connection, pdu, req_ccb); 301 return -1; 302 } 303 return 0; 304} 305 306 307/* 308 * check_StatSN 309 * Check received vs. expected StatSN 310 * 311 * Parameter: 312 * conn The connection 313 * nw_sn The received StatSN in network byte order 314 * ack Acknowledge this SN if TRUE 315 */ 316 317STATIC int 318check_StatSN(connection_t *conn, uint32_t nw_sn, bool ack) 319{ 320 int rc; 321 uint32_t sn = ntohl(nw_sn); 322 323 rc = add_sernum(&conn->StatSN_buf, sn); 324 325 if (ack) 326 ack_sernum(&conn->StatSN_buf, sn); 327 328 if (rc != 1) { 329 if (rc == 0) { 330 DEBOUT(("Duplicate PDU, ExpSN %d, Recvd: %d\n", 331 conn->StatSN_buf.ExpSN, sn)); 332 return -1; 333 } 334 335 if (rc < 0) { 336 DEBOUT(("Excessive outstanding Status PDUs, ExpSN %d, Recvd: %d\n", 337 conn->StatSN_buf.ExpSN, sn)); 338 handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST, 339 RECOVER_CONNECTION); 340 return rc; 341 } 342 343 DEBOUT(("Missing Status PDUs: First %d, num: %d\n", 344 conn->StatSN_buf.ExpSN, rc - 1)); 345 if (conn->state == ST_FULL_FEATURE && 346 conn->session->ErrorRecoveryLevel) { 347 snack_missing(conn, NULL, SNACK_STATUS_NAK, 348 conn->StatSN_buf.ExpSN, rc - 1); 349 } else { 350 DEBOUT(("StatSN killing connection (State = %d, " 351 "ErrorRecoveryLevel = %d)\n", 352 conn->state, conn->session->ErrorRecoveryLevel)); 353 handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST, 354 RECOVER_CONNECTION); 355 return -1; 356 } 357 } 358 return 0; 359} 360 361 362/* 363 * check_CmdSN 364 * Check received vs. expected CmdSN 365 * 366 * Parameter: 367 * conn The connection 368 * nw_sn The received ExpCmdSN in network byte order 369 */ 370 371STATIC void 372check_CmdSN(connection_t *conn, uint32_t nw_sn) 373{ 374 uint32_t sn = ntohl(nw_sn); 375 ccb_t *ccb, *nxt; 376 377 for (ccb = TAILQ_FIRST(&conn->ccbs_waiting); ccb != NULL; ccb = nxt) { 378 nxt = TAILQ_NEXT(ccb, chain); 379 DEBC(conn, 10, 380 ("CheckCmdSN - CmdSN=%d, ExpCmdSn=%d, waiting=%p, flags=%x\n", 381 ccb->CmdSN, sn, ccb->pdu_waiting, ccb->flags)); 382 if (ccb->pdu_waiting != NULL && ccb->CmdSN > sn && 383 !(ccb->flags & CCBF_GOT_RSP)) { 384 DEBC(conn, 1, ("CheckCmdSN resending - CmdSN=%d, ExpCmdSn=%d\n", 385 ccb->CmdSN, sn)); 386 387 ccb->total_tries++; 388 389 if (++ccb->num_timeouts > MAX_CCB_TIMEOUTS || 390 ccb->total_tries > MAX_CCB_TRIES) { 391 handle_connection_error(conn, ISCSI_STATUS_TIMEOUT, 392 (ccb->total_tries <= MAX_CCB_TRIES) ? RECOVER_CONNECTION 393 : LOGOUT_CONNECTION); 394 break; 395 } else { 396 resend_pdu(ccb); 397 } 398 } 399 } 400} 401 402 403/* 404 * receive_login_pdu 405 * Handle receipt of a login response PDU. 406 * 407 * Parameter: 408 * conn The connection 409 * pdu The PDU 410 * req_CCB The CCB associated with the original request (if any) 411 */ 412 413STATIC int 414receive_login_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb) 415{ 416 int rc; 417 418 DEBC(conn, 9, ("Received Login Response PDU, op=%x, flags=%x, sn=%u\n", 419 pdu->pdu.Opcode, pdu->pdu.Flags, 420 ntohl(pdu->pdu.p.login_rsp.StatSN))); 421 422 if (req_ccb == NULL) { 423 /* Duplicate?? */ 424 DEBOUT(("Received duplicate login response (no associated CCB)\n")); 425 return -1; 426 } 427 428 if (pdu->pdu.p.login_rsp.StatusClass) { 429 DEBC(conn, 1, ("Login problem - Class = %x, Detail = %x\n", 430 pdu->pdu.p.login_rsp.StatusClass, 431 pdu->pdu.p.login_rsp.StatusDetail)); 432 433 req_ccb->status = ISCSI_STATUS_LOGIN_FAILED; 434 /* XXX */ 435 wake_ccb(req_ccb); 436 return 0; 437 } 438 439 if (!conn->StatSN_buf.next_sn) { 440 conn->StatSN_buf.next_sn = conn->StatSN_buf.ExpSN = 441 ntohl(pdu->pdu.p.login_rsp.StatSN) + 1; 442 } else if (check_StatSN(conn, pdu->pdu.p.login_rsp.StatSN, TRUE)) 443 return -1; 444 445 if (pdu->temp_data_len) { 446 if ((rc = collect_text_data(pdu, req_ccb)) != 0) 447 return max(rc, 0); 448 } 449 450 negotiate_login(conn, pdu, req_ccb); 451 452 /* negotiate_login will decide whether login is complete or not */ 453 return 0; 454} 455 456 457/* 458 * receive_text_response_pdu 459 * Handle receipt of a text response PDU. 460 * 461 * Parameter: 462 * conn The connection 463 * pdu The PDU 464 * req_CCB The CCB associated with the original request (if any) 465 */ 466 467STATIC int 468receive_text_response_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb) 469{ 470 int rc; 471 472 DEBC(conn, 9, ("Received Text Response PDU, op=%x, flags=%x\n", 473 pdu->pdu.Opcode, pdu->pdu.Flags)); 474 475 if (check_StatSN(conn, pdu->pdu.p.text_rsp.StatSN, TRUE)) { 476 return -1; 477 } 478 if (req_ccb == NULL) { 479 DEBOUT(("Received unsolicited text response\n")); 480 handle_connection_error(conn, ISCSI_STATUS_TARGET_ERROR, 481 LOGOUT_CONNECTION); 482 return -1; 483 } 484 485 if (req_ccb->pdu_waiting != NULL) { 486 SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT); 487 req_ccb->num_timeouts = 0; 488 } 489 490 if ((rc = collect_text_data(pdu, req_ccb)) != 0) { 491 return max(0, rc); 492 } 493 negotiate_text(conn, pdu, req_ccb); 494 495 return 0; 496} 497 498 499/* 500 * receive_logout_pdu 501 * Handle receipt of a logout response PDU. 502 * 503 * Parameter: 504 * conn The connection 505 * pdu The PDU 506 * req_CCB The CCB associated with the original request (if any) 507 */ 508 509STATIC int 510receive_logout_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb) 511{ 512 bool otherconn; 513 uint8_t response; 514 515 otherconn = (req_ccb != NULL) ? (req_ccb->flags & CCBF_OTHERCONN) != 0 : 1; 516 response = pdu->pdu.OpcodeSpecific [0]; 517 DEBC(conn, 1, 518 ("Received Logout PDU - CCB = %p, otherconn=%d, response=%d\n", 519 req_ccb, otherconn, response)); 520 521 if (req_ccb == NULL) 522 return 0; 523 524 if (otherconn && check_StatSN(conn, pdu->pdu.p.logout_rsp.StatSN, TRUE)) 525 return -1; 526 527 switch (response) { 528 case 0: 529 req_ccb->status = ISCSI_STATUS_SUCCESS; 530 break; 531 case 1: 532 req_ccb->status = ISCSI_STATUS_LOGOUT_CID_NOT_FOUND; 533 break; 534 case 2: 535 req_ccb->status = ISCSI_STATUS_LOGOUT_RECOVERY_NS; 536 break; 537 default: 538 req_ccb->status = ISCSI_STATUS_LOGOUT_ERROR; 539 break; 540 } 541 542 if (conn->session->ErrorRecoveryLevel >= 2 && response != 1) { 543 connection_t *refconn = (otherconn) ? req_ccb->par : conn; 544 545 refconn->Time2Wait = ntohs(pdu->pdu.p.logout_rsp.Time2Wait); 546 refconn->Time2Retain = ntohs(pdu->pdu.p.logout_rsp.Time2Retain); 547 } 548 549 wake_ccb(req_ccb); 550 551 if (!otherconn && conn->state == ST_LOGOUT_SENT) { 552 conn->terminating = ISCSI_STATUS_LOGOUT; 553 conn->state = ST_SETTLING; 554 conn->loggedout = (response) ? LOGOUT_FAILED : LOGOUT_SUCCESS; 555 556 callout_stop(&conn->timeout); 557 558#ifdef ISCSI_TEST_MODE 559 test_remove_connection(conn); 560#endif 561 /* let send thread take over next step of cleanup */ 562 wakeup(&conn->pdus_to_send); 563 } 564 565 return !otherconn; 566} 567 568 569/* 570 * receive_data_in_pdu 571 * Handle receipt of a data in PDU. 572 * 573 * Parameter: 574 * conn The connection 575 * pdu The PDU 576 * req_CCB The CCB associated with the original request (if any) 577 */ 578 579STATIC int 580receive_data_in_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb) 581{ 582 uint32_t dsl, sn; 583 bool done; 584 int rc; 585 586 dsl = ntoh3(pdu->pdu.DataSegmentLength); 587 588 if (req_ccb == NULL || !req_ccb->data_in || !req_ccb->data_len) { 589 DEBOUT(("Received Data In, but req_ccb not waiting for it, ignored\n")); 590 return 0; 591 } 592 req_ccb->flags |= CCBF_GOT_RSP; 593 594 if (req_ccb->pdu_waiting != NULL) { 595 SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT); 596 req_ccb->num_timeouts = 0; 597 } 598 599 sn = ntohl(pdu->pdu.p.data_in.DataSN); 600 601 if ((rc = add_sernum(&req_ccb->DataSN_buf, sn)) != 1) { 602 if (!rc) { 603 return -1; 604 } 605 if (rc < 0) { 606 DEBOUT(("Excessive outstanding Data PDUs\n")); 607 handle_connection_error(req_ccb->connection, 608 ISCSI_STATUS_PDUS_LOST, LOGOUT_CONNECTION); 609 return -1; 610 } 611 DEBOUT(("Missing Data PDUs: First %d, num: %d\n", 612 req_ccb->DataSN_buf.ExpSN, rc - 1)); 613 614 if (conn->state == ST_FULL_FEATURE && 615 conn->session->ErrorRecoveryLevel) { 616 snack_missing(req_ccb->connection, req_ccb, 617 SNACK_DATA_NAK, req_ccb->DataSN_buf.ExpSN, 618 rc - 1); 619 } else { 620 DEBOUT(("Killing connection (State=%d, ErrorRecoveryLevel=%d)\n", 621 conn->state, conn->session->ErrorRecoveryLevel)); 622 handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST, 623 LOGOUT_CONNECTION); 624 return -1; 625 } 626 } 627 628 ack_sernum(&req_ccb->DataSN_buf, sn); 629 630 req_ccb->xfer_len += dsl; 631 632 if ((pdu->pdu.Flags & FLAG_ACK) && conn->session->ErrorRecoveryLevel) 633 send_snack(conn, pdu, req_ccb, SNACK_DATA_ACK); 634 635 done = sn_empty(&req_ccb->DataSN_buf); 636 637 if (pdu->pdu.Flags & FLAG_STATUS) { 638 DEBC(conn, 10, ("Rx Data In Complete, done = %d\n", done)); 639 640 req_ccb->flags |= CCBF_COMPLETE; 641 /* successful transfer, reset recover count */ 642 conn->recover = 0; 643 644 if (done) { 645 wake_ccb(req_ccb); 646 } 647 if (check_StatSN(conn, pdu->pdu.p.data_in.StatSN, done)) { 648 return -1; 649 } 650 } else if (done && (req_ccb->flags & CCBF_COMPLETE)) { 651 wake_ccb(req_ccb); 652 } 653 /* else wait for command response */ 654 655 return 0; 656} 657 658 659/* 660 * receive_r2t_pdu 661 * Handle receipt of a R2T PDU. 662 * 663 * Parameter: 664 * conn The connection 665 * pdu The PDU 666 * req_CCB The CCB associated with the original request (if any) 667 */ 668 669STATIC int 670receive_r2t_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb) 671{ 672 673 DEBC(conn, 10, ("Received R2T PDU - CCB = %p\n", req_ccb)); 674 675 if (req_ccb != NULL) { 676 if (req_ccb->pdu_waiting != NULL) { 677 SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT); 678 req_ccb->num_timeouts = 0; 679 } 680 send_data_out(conn, pdu, req_ccb, CCBDISP_NOWAIT, TRUE); 681 } 682 683 return 0; 684} 685 686 687/* 688 * receive_command_response_pdu 689 * Handle receipt of a command response PDU. 690 * 691 * Parameter: 692 * conn The connection 693 * pdu The PDU 694 * req_CCB The CCB associated with the original request (if any) 695 */ 696 697STATIC int 698receive_command_response_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb) 699{ 700 int len, rc; 701 bool done; 702 703 /* Read any provided data */ 704 if (pdu->temp_data_len && req_ccb != NULL && req_ccb->sense_len_req) { 705 len = min(req_ccb->sense_len_req, 706 ntohs(*((uint16_t *) pdu->temp_data))); 707 memcpy(req_ccb->sense_ptr, ((uint16_t *) pdu->temp_data) + 1, 708 len); 709 req_ccb->sense_len_got = len; 710 } 711 712 if (req_ccb == NULL) { 713 /* Assume duplicate... */ 714 DEBOUT(("Possibly duplicate command response (no associated CCB)\n")); 715 return -1; 716 } 717 718 PERF_SNAP(req_ccb, PERF_PDURCVSTS); 719 720 if (req_ccb->pdu_waiting != NULL) { 721 SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT); 722 req_ccb->num_timeouts = 0; 723 } 724 725 req_ccb->flags |= CCBF_COMPLETE; 726 conn->recover = 0; /* successful transfer, reset recover count */ 727 728 if (pdu->pdu.OpcodeSpecific[0]) { /* Response */ 729 req_ccb->status = ISCSI_STATUS_TARGET_FAILURE; 730 } else { 731 switch (pdu->pdu.OpcodeSpecific[1]) { /* Status */ 732 case 0x00: 733 /* success */ 734 break; 735 736 case 0x02: 737 req_ccb->status = ISCSI_STATUS_CHECK_CONDITION; 738 break; 739 740 case 0x08: 741 req_ccb->status = ISCSI_STATUS_TARGET_BUSY; 742 break; 743 744 default: 745 req_ccb->status = ISCSI_STATUS_TARGET_ERROR; 746 break; 747 } 748 } 749 750 if (pdu->pdu.Flags & (FLAG_OVERFLOW | FLAG_UNDERFLOW)) 751 req_ccb->residual = ntohl(pdu->pdu.p.response.ResidualCount); 752 753 done = req_ccb->status || sn_empty(&req_ccb->DataSN_buf); 754 755 DEBC(conn, 10, ("Rx Command Response rsp = %x, status = %x\n", 756 pdu->pdu.OpcodeSpecific[0], pdu->pdu.OpcodeSpecific[1])); 757 758 rc = check_StatSN(conn, pdu->pdu.p.response.StatSN, done); 759 760 if (done) 761 wake_ccb(req_ccb); 762 763 return rc; 764} 765 766 767/* 768 * receive_asynch_pdu 769 * Handle receipt of an asynchronous message PDU. 770 * 771 * Parameter: 772 * conn The connection 773 * pdu The PDU 774 */ 775 776STATIC int 777receive_asynch_pdu(connection_t *conn, pdu_t *pdu) 778{ 779 780 DEBOUT(("Received Asynch PDU, Event %d\n", pdu->pdu.p.asynch.AsyncEvent)); 781 782 switch (pdu->pdu.p.asynch.AsyncEvent) { 783 case 0: /* SCSI Asynch event. Don't know what to do with it... */ 784 break; 785 786 case 1: /* Target requests logout. */ 787 if (conn->session->active_connections > 1) { 788 kill_connection(conn, ISCSI_STATUS_TARGET_LOGOUT, 789 LOGOUT_CONNECTION, FALSE); 790 } else { 791 kill_session(conn->session, ISCSI_STATUS_TARGET_LOGOUT, 792 LOGOUT_SESSION, FALSE); 793 } 794 break; 795 796 case 2: /* Target is dropping connection */ 797 conn = find_connection(conn->session, 798 ntohs(pdu->pdu.p.asynch.Parameter1)); 799 if (conn != NULL) { 800 conn->Time2Wait = ntohs(pdu->pdu.p.asynch.Parameter2); 801 conn->Time2Retain = ntohs(pdu->pdu.p.asynch.Parameter3); 802 kill_connection(conn, ISCSI_STATUS_TARGET_DROP, 803 NO_LOGOUT, TRUE); 804 } 805 break; 806 807 case 3: /* Target is dropping all connections of session */ 808 conn->session->DefaultTime2Wait = ntohs(pdu->pdu.p.asynch.Parameter2); 809 conn->session->DefaultTime2Retain = ntohs(pdu->pdu.p.asynch.Parameter3); 810 kill_session(conn->session, ISCSI_STATUS_TARGET_DROP, NO_LOGOUT, TRUE); 811 break; 812 813 case 4: /* Target requests parameter negotiation */ 814 start_text_negotiation(conn); 815 break; 816 817 default: 818 /* ignore */ 819 break; 820 } 821 return 0; 822} 823 824 825/* 826 * receive_reject_pdu 827 * Handle receipt of a reject PDU. 828 * 829 * Parameter: 830 * conn The connection 831 * pdu The PDU 832 */ 833 834STATIC int 835receive_reject_pdu(connection_t *conn, pdu_t *pdu) 836{ 837 pdu_header_t *hpdu; 838 ccb_t *req_ccb; 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 req_ccb->status = ISCSI_STATUS_PROTOCOL_ERROR; 868 break; 869 870 case REJECT_CMD_NOT_SUPPORTED: 871 req_ccb->status = ISCSI_STATUS_CMD_NOT_SUPPORTED; 872 break; 873 874 case REJECT_INVALID_PDU_FIELD: 875 req_ccb->status = ISCSI_STATUS_PDU_ERROR; 876 break; 877 878 default: 879 req_ccb->status = ISCSI_STATUS_GENERAL_ERROR; 880 break; 881 } 882 883 wake_ccb(req_ccb); 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 901STATIC int 902receive_task_management_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb) 903{ 904 905 DEBC(conn, 2, ("Received Task Management PDU, response %d, req_ccb %p\n", 906 pdu->pdu.OpcodeSpecific[0], req_ccb)); 907 908 if (req_ccb != NULL) { 909 switch (pdu->pdu.OpcodeSpecific[0]) { /* Response */ 910 case 0: 911 req_ccb->status = ISCSI_STATUS_SUCCESS; 912 break; 913 case 1: 914 req_ccb->status = ISCSI_STATUS_TASK_NOT_FOUND; 915 break; 916 case 2: 917 req_ccb->status = ISCSI_STATUS_LUN_NOT_FOUND; 918 break; 919 case 3: 920 req_ccb->status = ISCSI_STATUS_TASK_ALLEGIANT; 921 break; 922 case 4: 923 req_ccb->status = ISCSI_STATUS_CANT_REASSIGN; 924 break; 925 case 5: 926 req_ccb->status = ISCSI_STATUS_FUNCTION_UNSUPPORTED; 927 break; 928 case 6: 929 req_ccb->status = ISCSI_STATUS_FUNCTION_NOT_AUTHORIZED; 930 break; 931 case 255: 932 req_ccb->status = ISCSI_STATUS_FUNCTION_REJECTED; 933 break; 934 default: 935 req_ccb->status = ISCSI_STATUS_UNKNOWN_REASON; 936 break; 937 } 938 wake_ccb(req_ccb); 939 } 940 941 check_StatSN(conn, pdu->pdu.p.task_rsp.StatSN, TRUE); 942 943 return 0; 944} 945 946 947/* 948 * receive_nop_in_pdu 949 * Handle receipt of a Nop-In PDU. 950 * 951 * Parameter: 952 * conn The connection 953 * pdu The PDU 954 * req_CCB The CCB associated with the original request (if any) 955 */ 956 957STATIC int 958receive_nop_in_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb) 959{ 960 DEBC(conn, 10, 961 ("Received NOP-In PDU, req_ccb=%p, ITT=%x, TTT=%x, StatSN=%x\n", 962 req_ccb, pdu->pdu.InitiatorTaskTag, 963 pdu->pdu.p.nop_in.TargetTransferTag, 964 ntohl(pdu->pdu.p.nop_in.StatSN))); 965 966 if (pdu->pdu.InitiatorTaskTag == 0xffffffff) { 967 /* this is a target ping - respond with a pong */ 968 if (pdu->pdu.p.nop_in.TargetTransferTag != 0xffffffff) 969 send_nop_out(conn, pdu); 970 971 /* 972 Any receive resets the connection timeout, but we got a ping, which 973 means that it's likely the other side was waiting for something to 974 happen on the connection. If we aren't idle, send a ping right 975 away to synch counters (don't synch on this ping because other 976 PDUs may be on the way). 977 */ 978 if (TAILQ_FIRST(&conn->ccbs_waiting) != NULL) 979 send_nop_out(conn, NULL); 980 } else if (req_ccb != NULL) { 981 /* this is a solicited ping, check CmdSN for lost commands */ 982 /* and advance StatSN */ 983 check_CmdSN(conn, pdu->pdu.p.nop_in.ExpCmdSN); 984 985 wake_ccb(req_ccb); 986 987 check_StatSN(conn, pdu->pdu.p.nop_in.StatSN, TRUE); 988 } 989 990 return 0; 991} 992 993 994/* 995 * receive_pdu 996 * Get parameters, call the appropriate handler for a received PDU. 997 * 998 * Parameter: 999 * conn The connection 1000 * pdu The PDU 1001 * 1002 * Returns: 0 on success, nonzero if the connection is broken. 1003 */ 1004 1005STATIC int 1006receive_pdu(connection_t *conn, pdu_t *pdu) 1007{ 1008 ccb_t *req_ccb; 1009 ccb_list_t waiting; 1010 int rc; 1011 uint32_t MaxCmdSN, digest; 1012 session_t *sess = conn->session; 1013 1014 if (conn->HeaderDigest) { 1015 digest = gen_digest(&pdu->pdu, BHS_SIZE); 1016 if (digest != pdu->pdu.HeaderDigest) { 1017 DEBOUT(("Header Digest Error: comp = %08x, rx = %08x\n", 1018 digest, pdu->pdu.HeaderDigest)); 1019#ifdef ISCSI_TEST_MODE 1020 test_mode_rx(conn, pdu, TEST_INVALID_HEADER_CRC); 1021#endif 1022 /* try to skip to next PDU */ 1023 try_resynch_receive(conn); 1024 free_pdu(pdu); 1025 return 0; 1026 } 1027 } 1028 1029 req_ccb = ccb_from_itt(conn, pdu->pdu.InitiatorTaskTag); 1030 1031 if (req_ccb != NULL && req_ccb->data_in && req_ccb->data_len && 1032 (pdu->pdu.Opcode & OPCODE_MASK) == TOP_SCSI_Data_in) { 1033 uint32_t dsl, offset; 1034 1035 dsl = ntoh3(pdu->pdu.DataSegmentLength); 1036 offset = ntohl(pdu->pdu.p.data_in.BufferOffset); 1037 1038 if ((offset + dsl) > req_ccb->data_len) { 1039 DEBOUT(("Received more data than requested (len %d, offset %d)\n", 1040 dsl, offset)); 1041 handle_connection_error(conn, ISCSI_STATUS_TARGET_ERROR, NO_LOGOUT); 1042 return 1; 1043 } 1044 DEBC(conn, 10, 1045 ("Received Data in PDU - CCB = %p, Datalen = %d, Offset = %d\n", 1046 req_ccb, dsl, offset)); 1047 1048 PERF_SNAPC(req_ccb, PERF_BEGIN_PDURCVDATA); 1049 rc = read_pdu_data(pdu, req_ccb->data_ptr, offset); 1050 PERF_SNAP(req_ccb, PERF_END_PDURCVDATA); 1051 } else { 1052 rc = read_pdu_data(pdu, NULL, 0); 1053 } 1054#ifdef ISCSI_TEST_MODE 1055 if (test_mode_rx(conn, pdu, rc)) { 1056 free_pdu(pdu); 1057 return rc; 1058 } 1059#endif 1060 if (!rc && (conn->state <= ST_WINDING_DOWN || 1061 (pdu->pdu.Opcode & OPCODE_MASK) == TOP_Logout_Response)) { 1062 1063 switch (pdu->pdu.Opcode & OPCODE_MASK) { 1064 case TOP_NOP_In: 1065 rc = receive_nop_in_pdu(conn, pdu, req_ccb); 1066 break; 1067 1068 case TOP_SCSI_Response: 1069 rc = receive_command_response_pdu(conn, pdu, req_ccb); 1070 break; 1071 1072 case TOP_SCSI_Task_Management: 1073 rc = receive_task_management_pdu(conn, pdu, req_ccb); 1074 break; 1075 1076 case TOP_Login_Response: 1077 rc = receive_login_pdu(conn, pdu, req_ccb); 1078 break; 1079 1080 case TOP_Text_Response: 1081 rc = receive_text_response_pdu(conn, pdu, req_ccb); 1082 break; 1083 1084 case TOP_SCSI_Data_in: 1085 rc = receive_data_in_pdu(conn, pdu, req_ccb); 1086 break; 1087 1088 case TOP_Logout_Response: 1089 rc = receive_logout_pdu(conn, pdu, req_ccb); 1090 break; 1091 1092 case TOP_R2T: 1093 rc = receive_r2t_pdu(conn, pdu, req_ccb); 1094 break; 1095 1096 case TOP_Asynchronous_Message: 1097 rc = receive_asynch_pdu(conn, pdu); 1098 break; 1099 1100 case TOP_Reject: 1101 rc = receive_reject_pdu(conn, pdu); 1102 break; 1103 1104 default: 1105 DEBOUT(("Received Invalid Opcode %x\n", pdu->pdu.Opcode)); 1106 try_resynch_receive(conn); 1107 rc = -1; 1108 break; 1109 } 1110 } 1111 1112 free_pdu(pdu); 1113 if (rc) 1114 return rc; 1115 1116 /* MaxCmdSN and ExpCmdSN are in the same place in all received PDUs */ 1117 sess->ExpCmdSN = max(sess->ExpCmdSN, ntohl(pdu->pdu.p.nop_in.ExpCmdSN)); 1118 MaxCmdSN = ntohl(pdu->pdu.p.nop_in.MaxCmdSN); 1119 1120 /* received a valid frame, reset timeout */ 1121 1122 SET_CONN_TIMEOUT(conn, 1123 (((pdu->pdu.Opcode & OPCODE_MASK) == TOP_NOP_In) && 1124 (TAILQ_FIRST(&conn->ccbs_waiting) == NULL)) ? 1125 conn->idle_timeout_val : CONNECTION_TIMEOUT); 1126 conn->num_timeouts = 0; 1127 1128 /* 1129 * Un-throttle - wakeup all CCBs waiting for MaxCmdSN to increase. 1130 * We have to handle wait/nowait CCBs a bit differently. 1131 */ 1132 if (MaxCmdSN != sess->MaxCmdSN) { 1133 sess->MaxCmdSN = MaxCmdSN; 1134#if 0 1135/* XXX - agc */ 1136 if (TAILQ_FIRST(&sess->ccbs_throttled) == NULL || 1137 !sn_a_lt_b(sess->CmdSN, MaxCmdSN)) 1138 return 0; 1139#else 1140 if (TAILQ_FIRST(&sess->ccbs_throttled) == NULL) 1141 return 0; 1142#endif 1143 1144 DEBC(conn, 1, ("Unthrottling - MaxCmdSN = %d\n", MaxCmdSN)); 1145 1146 CS_BEGIN; 1147 TAILQ_INIT(&waiting); 1148 TAILQ_CONCAT(&waiting, &sess->ccbs_throttled, chain); 1149 CS_END; 1150 while ((req_ccb = TAILQ_FIRST(&waiting)) != NULL) { 1151 TAILQ_REMOVE(&waiting, req_ccb, chain); 1152 DEBC(conn, 1, ("Unthrottling - ccb = %p, disp = %d\n", 1153 req_ccb, req_ccb->disp)); 1154 1155 if (req_ccb->flags & CCBF_WAITING) 1156 wakeup(req_ccb); 1157 else 1158 send_command(req_ccb, req_ccb->disp, FALSE, FALSE); 1159 } 1160 } 1161 1162 return 0; 1163} 1164 1165/*****************************************************************************/ 1166 1167/* 1168 * iscsi_receive_thread 1169 * Per connection thread handling receive data. 1170 * 1171 * Parameter: 1172 * conn The connection 1173 */ 1174 1175void 1176iscsi_rcv_thread(void *par) 1177{ 1178 connection_t *conn = (connection_t *) par; 1179 pdu_t *pdu; 1180 size_t hlen; 1181 1182 do { 1183 while (!conn->terminating) { 1184 pdu = get_pdu(conn); 1185 pdu->uio.uio_iov = pdu->io_vec; 1186 UIO_SETUP_SYSSPACE(&pdu->uio); 1187 pdu->uio.uio_iovcnt = 1; 1188 pdu->uio.uio_rw = UIO_READ; 1189 1190 pdu->io_vec[0].iov_base = &pdu->pdu; 1191 hlen = (conn->HeaderDigest) ? BHS_SIZE + 4 : BHS_SIZE; 1192 pdu->io_vec[0].iov_len = hlen; 1193 pdu->uio.uio_resid = hlen; 1194 1195 DEBC(conn, 99, ("Receive thread waiting for data\n")); 1196 if (my_soo_read(conn, &pdu->uio, MSG_WAITALL)) { 1197 free_pdu(pdu); 1198 break; 1199 } 1200 /* Check again for header digest */ 1201 /* (it may have changed during the wait) */ 1202 if (hlen == BHS_SIZE && conn->HeaderDigest) { 1203 pdu->uio.uio_iov = pdu->io_vec; 1204 pdu->uio.uio_iovcnt = 1; 1205 pdu->io_vec[0].iov_base = &pdu->pdu.HeaderDigest; 1206 pdu->io_vec[0].iov_len = 4; 1207 pdu->uio.uio_resid = 4; 1208 if (my_soo_read(conn, &pdu->uio, MSG_WAITALL)) { 1209 free_pdu(pdu); 1210 break; 1211 } 1212 } 1213 1214 if (receive_pdu(conn, pdu) > 0) { 1215 break; 1216 } 1217 } 1218 if (!conn->destroy) { 1219 tsleep(conn, PRIBIO, "conn_idle", 0); 1220 } 1221 } while (!conn->destroy); 1222 1223 conn->rcvproc = NULL; 1224 DEBC(conn, 5, ("Receive thread exits\n")); 1225 kthread_exit(0); 1226} 1227