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