iscsi_rcv.c revision 1.8 1 /* $NetBSD: iscsi_rcv.c,v 1.8 2015/05/30 16:12:34 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 SET_CCB_TIMEOUT(conn, req_ccb, 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 SET_CCB_TIMEOUT(conn, req_ccb, 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 SET_CCB_TIMEOUT(conn, req_ccb, 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 PERF_SNAP(req_ccb, PERF_PDURCVSTS);
702
703 if (req_ccb->pdu_waiting != NULL) {
704 SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT);
705 req_ccb->num_timeouts = 0;
706 }
707
708 req_ccb->flags |= CCBF_COMPLETE;
709 conn->recover = 0; /* successful transfer, reset recover count */
710
711 if (pdu->pdu.OpcodeSpecific[0]) { /* Response */
712 status = ISCSI_STATUS_TARGET_FAILURE;
713 } else {
714 switch (pdu->pdu.OpcodeSpecific[1]) { /* Status */
715 case 0x00:
716 status = ISCSI_STATUS_SUCCESS;
717 break;
718
719 case 0x02:
720 status = ISCSI_STATUS_CHECK_CONDITION;
721 break;
722
723 case 0x08:
724 status = ISCSI_STATUS_TARGET_BUSY;
725 break;
726
727 default:
728 status = ISCSI_STATUS_TARGET_ERROR;
729 break;
730 }
731 }
732
733 if (pdu->pdu.Flags & (FLAG_OVERFLOW | FLAG_UNDERFLOW))
734 req_ccb->residual = ntohl(pdu->pdu.p.response.ResidualCount);
735
736 done = status || sn_empty(&req_ccb->DataSN_buf);
737
738 DEBC(conn, 10, ("Rx Command Response rsp = %x, status = %x\n",
739 pdu->pdu.OpcodeSpecific[0], pdu->pdu.OpcodeSpecific[1]));
740
741 rc = check_StatSN(conn, pdu->pdu.p.response.StatSN, done);
742
743 if (done)
744 wake_ccb(req_ccb, status);
745
746 return rc;
747 }
748
749
750 /*
751 * receive_asynch_pdu
752 * Handle receipt of an asynchronous message PDU.
753 *
754 * Parameter:
755 * conn The connection
756 * pdu The PDU
757 */
758
759 STATIC int
760 receive_asynch_pdu(connection_t *conn, pdu_t *pdu)
761 {
762
763 DEBOUT(("Received Asynch PDU, Event %d\n", pdu->pdu.p.asynch.AsyncEvent));
764
765 switch (pdu->pdu.p.asynch.AsyncEvent) {
766 case 0: /* SCSI Asynch event. Don't know what to do with it... */
767 break;
768
769 case 1: /* Target requests logout. */
770 if (conn->session->active_connections > 1) {
771 kill_connection(conn, ISCSI_STATUS_TARGET_LOGOUT,
772 LOGOUT_CONNECTION, FALSE);
773 } else {
774 kill_session(conn->session, ISCSI_STATUS_TARGET_LOGOUT,
775 LOGOUT_SESSION, FALSE);
776 }
777 break;
778
779 case 2: /* Target is dropping connection */
780 conn = find_connection(conn->session,
781 ntohs(pdu->pdu.p.asynch.Parameter1));
782 if (conn != NULL) {
783 conn->Time2Wait = ntohs(pdu->pdu.p.asynch.Parameter2);
784 conn->Time2Retain = ntohs(pdu->pdu.p.asynch.Parameter3);
785 kill_connection(conn, ISCSI_STATUS_TARGET_DROP,
786 NO_LOGOUT, TRUE);
787 }
788 break;
789
790 case 3: /* Target is dropping all connections of session */
791 conn->session->DefaultTime2Wait = ntohs(pdu->pdu.p.asynch.Parameter2);
792 conn->session->DefaultTime2Retain = ntohs(pdu->pdu.p.asynch.Parameter3);
793 kill_session(conn->session, ISCSI_STATUS_TARGET_DROP, NO_LOGOUT, TRUE);
794 break;
795
796 case 4: /* Target requests parameter negotiation */
797 start_text_negotiation(conn);
798 break;
799
800 default:
801 /* ignore */
802 break;
803 }
804 return 0;
805 }
806
807
808 /*
809 * receive_reject_pdu
810 * Handle receipt of a reject PDU.
811 *
812 * Parameter:
813 * conn The connection
814 * pdu The PDU
815 */
816
817 STATIC int
818 receive_reject_pdu(connection_t *conn, pdu_t *pdu)
819 {
820 pdu_header_t *hpdu;
821 ccb_t *req_ccb;
822 uint32_t status;
823
824 DEBOUT(("Received Reject PDU, reason = %x, data_len = %d\n",
825 pdu->pdu.OpcodeSpecific[0], pdu->temp_data_len));
826
827 if (pdu->temp_data_len >= BHS_SIZE) {
828 hpdu = (pdu_header_t *) pdu->temp_data;
829 req_ccb = ccb_from_itt(conn, hpdu->InitiatorTaskTag);
830
831 DEBC(conn, 9, ("Reject PDU ITT (ccb)= %x (%p)\n",
832 hpdu->InitiatorTaskTag, req_ccb));
833 if (!req_ccb) {
834 return 0;
835 }
836 switch (pdu->pdu.OpcodeSpecific[0]) {
837 case REJECT_DIGEST_ERROR:
838 /* don't retransmit data out */
839 if ((hpdu->Opcode & OPCODE_MASK) == IOP_SCSI_Data_out)
840 return 0;
841 resend_pdu(req_ccb);
842 return 0;
843
844 case REJECT_IMMED_COMMAND:
845 case REJECT_LONG_OPERATION:
846 resend_pdu(req_ccb);
847 return 0;
848
849 case REJECT_SNACK:
850 case REJECT_PROTOCOL_ERROR:
851 status = ISCSI_STATUS_PROTOCOL_ERROR;
852 break;
853
854 case REJECT_CMD_NOT_SUPPORTED:
855 status = ISCSI_STATUS_CMD_NOT_SUPPORTED;
856 break;
857
858 case REJECT_INVALID_PDU_FIELD:
859 status = ISCSI_STATUS_PDU_ERROR;
860 break;
861
862 default:
863 status = ISCSI_STATUS_GENERAL_ERROR;
864 break;
865 }
866
867 wake_ccb(req_ccb, status);
868 handle_connection_error(conn, ISCSI_STATUS_PROTOCOL_ERROR,
869 LOGOUT_CONNECTION);
870 }
871 return 0;
872 }
873
874
875 /*
876 * receive_task_management_pdu
877 * Handle receipt of a task management PDU.
878 *
879 * Parameter:
880 * conn The connection
881 * pdu The PDU
882 * req_CCB The CCB associated with the original request (if any)
883 */
884
885 STATIC int
886 receive_task_management_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
887 {
888 uint32_t status;
889
890 DEBC(conn, 2, ("Received Task Management PDU, response %d, req_ccb %p\n",
891 pdu->pdu.OpcodeSpecific[0], req_ccb));
892
893 if (req_ccb != NULL) {
894 switch (pdu->pdu.OpcodeSpecific[0]) { /* Response */
895 case 0:
896 status = ISCSI_STATUS_SUCCESS;
897 break;
898 case 1:
899 status = ISCSI_STATUS_TASK_NOT_FOUND;
900 break;
901 case 2:
902 status = ISCSI_STATUS_LUN_NOT_FOUND;
903 break;
904 case 3:
905 status = ISCSI_STATUS_TASK_ALLEGIANT;
906 break;
907 case 4:
908 status = ISCSI_STATUS_CANT_REASSIGN;
909 break;
910 case 5:
911 status = ISCSI_STATUS_FUNCTION_UNSUPPORTED;
912 break;
913 case 6:
914 status = ISCSI_STATUS_FUNCTION_NOT_AUTHORIZED;
915 break;
916 case 255:
917 status = ISCSI_STATUS_FUNCTION_REJECTED;
918 break;
919 default:
920 status = ISCSI_STATUS_UNKNOWN_REASON;
921 break;
922 }
923 wake_ccb(req_ccb, status);
924 }
925
926 check_StatSN(conn, pdu->pdu.p.task_rsp.StatSN, TRUE);
927
928 return 0;
929 }
930
931
932 /*
933 * receive_nop_in_pdu
934 * Handle receipt of a Nop-In PDU.
935 *
936 * Parameter:
937 * conn The connection
938 * pdu The PDU
939 * req_CCB The CCB associated with the original request (if any)
940 */
941
942 STATIC int
943 receive_nop_in_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
944 {
945 DEBC(conn, 10,
946 ("Received NOP-In PDU, req_ccb=%p, ITT=%x, TTT=%x, StatSN=%x\n",
947 req_ccb, pdu->pdu.InitiatorTaskTag,
948 pdu->pdu.p.nop_in.TargetTransferTag,
949 ntohl(pdu->pdu.p.nop_in.StatSN)));
950
951 if (pdu->pdu.InitiatorTaskTag == 0xffffffff) {
952 /* this is a target ping - respond with a pong */
953 if (pdu->pdu.p.nop_in.TargetTransferTag != 0xffffffff)
954 send_nop_out(conn, pdu);
955
956 /*
957 Any receive resets the connection timeout, but we got a ping, which
958 means that it's likely the other side was waiting for something to
959 happen on the connection. If we aren't idle, send a ping right
960 away to synch counters (don't synch on this ping because other
961 PDUs may be on the way).
962 */
963 if (TAILQ_FIRST(&conn->ccbs_waiting) != NULL)
964 send_nop_out(conn, NULL);
965 } else if (req_ccb != NULL) {
966 /* this is a solicited ping, check CmdSN for lost commands */
967 /* and advance StatSN */
968 check_CmdSN(conn, pdu->pdu.p.nop_in.ExpCmdSN);
969
970 wake_ccb(req_ccb, ISCSI_STATUS_SUCCESS);
971
972 check_StatSN(conn, pdu->pdu.p.nop_in.StatSN, TRUE);
973 }
974
975 return 0;
976 }
977
978
979 /*
980 * receive_pdu
981 * Get parameters, call the appropriate handler for a received PDU.
982 *
983 * Parameter:
984 * conn The connection
985 * pdu The PDU
986 *
987 * Returns: 0 on success, nonzero if the connection is broken.
988 */
989
990 STATIC int
991 receive_pdu(connection_t *conn, pdu_t *pdu)
992 {
993 ccb_t *req_ccb;
994 ccb_list_t waiting;
995 int rc, s;
996 uint32_t MaxCmdSN, digest;
997 session_t *sess = conn->session;
998
999 if (conn->HeaderDigest) {
1000 digest = gen_digest(&pdu->pdu, BHS_SIZE);
1001 if (digest != pdu->pdu.HeaderDigest) {
1002 DEBOUT(("Header Digest Error: comp = %08x, rx = %08x\n",
1003 digest, pdu->pdu.HeaderDigest));
1004 /* try to skip to next PDU */
1005 try_resynch_receive(conn);
1006 free_pdu(pdu);
1007 return 0;
1008 }
1009 }
1010
1011 req_ccb = ccb_from_itt(conn, pdu->pdu.InitiatorTaskTag);
1012
1013 if (req_ccb != NULL && req_ccb->data_in && req_ccb->data_len &&
1014 (pdu->pdu.Opcode & OPCODE_MASK) == TOP_SCSI_Data_in) {
1015 uint32_t dsl, offset;
1016
1017 dsl = ntoh3(pdu->pdu.DataSegmentLength);
1018 offset = ntohl(pdu->pdu.p.data_in.BufferOffset);
1019
1020 if ((offset + dsl) > req_ccb->data_len) {
1021 DEBOUT(("Received more data than requested (len %d, offset %d)\n",
1022 dsl, offset));
1023 handle_connection_error(conn, ISCSI_STATUS_TARGET_ERROR, NO_LOGOUT);
1024 return 1;
1025 }
1026 DEBC(conn, 10,
1027 ("Received Data in PDU - CCB = %p, Datalen = %d, Offset = %d\n",
1028 req_ccb, dsl, offset));
1029
1030 PERF_SNAPC(req_ccb, PERF_BEGIN_PDURCVDATA);
1031 rc = read_pdu_data(pdu, req_ccb->data_ptr, offset);
1032 PERF_SNAP(req_ccb, PERF_END_PDURCVDATA);
1033 } else {
1034 rc = read_pdu_data(pdu, NULL, 0);
1035 }
1036 if (!rc && (conn->state <= ST_WINDING_DOWN ||
1037 (pdu->pdu.Opcode & OPCODE_MASK) == TOP_Logout_Response)) {
1038
1039 switch (pdu->pdu.Opcode & OPCODE_MASK) {
1040 case TOP_NOP_In:
1041 rc = receive_nop_in_pdu(conn, pdu, req_ccb);
1042 break;
1043
1044 case TOP_SCSI_Response:
1045 rc = receive_command_response_pdu(conn, pdu, req_ccb);
1046 break;
1047
1048 case TOP_SCSI_Task_Management:
1049 rc = receive_task_management_pdu(conn, pdu, req_ccb);
1050 break;
1051
1052 case TOP_Login_Response:
1053 rc = receive_login_pdu(conn, pdu, req_ccb);
1054 break;
1055
1056 case TOP_Text_Response:
1057 rc = receive_text_response_pdu(conn, pdu, req_ccb);
1058 break;
1059
1060 case TOP_SCSI_Data_in:
1061 rc = receive_data_in_pdu(conn, pdu, req_ccb);
1062 break;
1063
1064 case TOP_Logout_Response:
1065 rc = receive_logout_pdu(conn, pdu, req_ccb);
1066 break;
1067
1068 case TOP_R2T:
1069 rc = receive_r2t_pdu(conn, pdu, req_ccb);
1070 break;
1071
1072 case TOP_Asynchronous_Message:
1073 rc = receive_asynch_pdu(conn, pdu);
1074 break;
1075
1076 case TOP_Reject:
1077 rc = receive_reject_pdu(conn, pdu);
1078 break;
1079
1080 default:
1081 DEBOUT(("Received Invalid Opcode %x\n", pdu->pdu.Opcode));
1082 try_resynch_receive(conn);
1083 rc = -1;
1084 break;
1085 }
1086 }
1087
1088 free_pdu(pdu);
1089 if (rc)
1090 return rc;
1091
1092 /* MaxCmdSN and ExpCmdSN are in the same place in all received PDUs */
1093 sess->ExpCmdSN = max(sess->ExpCmdSN, ntohl(pdu->pdu.p.nop_in.ExpCmdSN));
1094 MaxCmdSN = ntohl(pdu->pdu.p.nop_in.MaxCmdSN);
1095
1096 /* received a valid frame, reset timeout */
1097
1098 SET_CONN_TIMEOUT(conn,
1099 (((pdu->pdu.Opcode & OPCODE_MASK) == TOP_NOP_In) &&
1100 (TAILQ_FIRST(&conn->ccbs_waiting) == NULL)) ?
1101 conn->idle_timeout_val : CONNECTION_TIMEOUT);
1102 conn->num_timeouts = 0;
1103
1104 /*
1105 * Un-throttle - wakeup all CCBs waiting for MaxCmdSN to increase.
1106 * We have to handle wait/nowait CCBs a bit differently.
1107 */
1108 if (MaxCmdSN != sess->MaxCmdSN) {
1109 sess->MaxCmdSN = MaxCmdSN;
1110 if (TAILQ_FIRST(&sess->ccbs_throttled) == NULL)
1111 return 0;
1112
1113 DEBC(conn, 1, ("Unthrottling - MaxCmdSN = %d\n", MaxCmdSN));
1114
1115 s = splbio();
1116 TAILQ_INIT(&waiting);
1117 while ((req_ccb = TAILQ_FIRST(&sess->ccbs_throttled)) != NULL) {
1118 throttle_ccb(req_ccb, FALSE);
1119 TAILQ_INSERT_TAIL(&waiting, req_ccb, chain);
1120 }
1121 splx(s);
1122
1123 while ((req_ccb = TAILQ_FIRST(&waiting)) != NULL) {
1124 TAILQ_REMOVE(&waiting, req_ccb, chain);
1125
1126 DEBC(conn, 1, ("Unthrottling - ccb = %p, disp = %d\n",
1127 req_ccb, req_ccb->disp));
1128
1129 if (req_ccb->flags & CCBF_WAITING)
1130 wakeup(req_ccb);
1131 else
1132 send_command(req_ccb, req_ccb->disp, FALSE, FALSE);
1133 }
1134 }
1135
1136 return 0;
1137 }
1138
1139 /*****************************************************************************/
1140
1141 /*
1142 * iscsi_receive_thread
1143 * Per connection thread handling receive data.
1144 *
1145 * Parameter:
1146 * conn The connection
1147 */
1148
1149 void
1150 iscsi_rcv_thread(void *par)
1151 {
1152 connection_t *conn = (connection_t *) par;
1153 pdu_t *pdu;
1154 size_t hlen;
1155
1156 do {
1157 while (!conn->terminating) {
1158 pdu = get_pdu(conn, TRUE);
1159 pdu->uio.uio_iov = pdu->io_vec;
1160 UIO_SETUP_SYSSPACE(&pdu->uio);
1161 pdu->uio.uio_iovcnt = 1;
1162 pdu->uio.uio_rw = UIO_READ;
1163
1164 pdu->io_vec[0].iov_base = &pdu->pdu;
1165 hlen = (conn->HeaderDigest) ? BHS_SIZE + 4 : BHS_SIZE;
1166 pdu->io_vec[0].iov_len = hlen;
1167 pdu->uio.uio_resid = hlen;
1168
1169 DEBC(conn, 99, ("Receive thread waiting for data\n"));
1170 if (my_soo_read(conn, &pdu->uio, MSG_WAITALL)) {
1171 free_pdu(pdu);
1172 break;
1173 }
1174 /* Check again for header digest */
1175 /* (it may have changed during the wait) */
1176 if (hlen == BHS_SIZE && conn->HeaderDigest) {
1177 pdu->uio.uio_iov = pdu->io_vec;
1178 pdu->uio.uio_iovcnt = 1;
1179 pdu->io_vec[0].iov_base = &pdu->pdu.HeaderDigest;
1180 pdu->io_vec[0].iov_len = 4;
1181 pdu->uio.uio_resid = 4;
1182 if (my_soo_read(conn, &pdu->uio, MSG_WAITALL)) {
1183 free_pdu(pdu);
1184 break;
1185 }
1186 }
1187
1188 if (receive_pdu(conn, pdu) > 0) {
1189 break;
1190 }
1191 }
1192 if (!conn->destroy) {
1193 tsleep(conn, PRIBIO, "conn_idle", 30 * hz);
1194 }
1195 } while (!conn->destroy);
1196
1197 conn->rcvproc = NULL;
1198 DEBC(conn, 5, ("Receive thread exits\n"));
1199 kthread_exit(0);
1200 }
1201