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