2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
42 #include <netinet/in.h>
45 #include "iscsi_proto.h"
48 login_nsg(const struct pdu *response)
50 struct iscsi_bhs_login_response *bhslr;
52 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
54 return (bhslr->bhslr_flags & 0x03);
58 login_set_nsg(struct pdu *request, int nsg)
60 struct iscsi_bhs_login_request *bhslr;
62 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
63 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
64 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
66 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
68 bhslr->bhslr_flags &= 0xFC;
69 bhslr->bhslr_flags |= nsg;
73 login_set_csg(struct pdu *request, int csg)
75 struct iscsi_bhs_login_request *bhslr;
77 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
78 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
79 csg == BHSLR_STAGE_FULL_FEATURE_PHASE);
81 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
83 bhslr->bhslr_flags &= 0xF3;
84 bhslr->bhslr_flags |= csg << 2;
88 login_target_error_str(int class, int detail)
93 * RFC 3270, 10.13.5. Status-Class and Status-Detail
99 return ("Target moved temporarily");
101 return ("Target moved permanently");
103 snprintf(msg, sizeof(msg), "unknown redirection; "
104 "Status-Class 0x%x, Status-Detail 0x%x",
111 return ("Initiator error");
113 return ("Authentication failure");
115 return ("Authorization failure");
117 return ("Not found");
119 return ("Target removed");
121 return ("Unsupported version");
123 return ("Too many connections");
125 return ("Missing parameter");
127 return ("Can't include in session");
129 return ("Session type not supported");
131 return ("Session does not exist");
133 return ("Invalid during login");
135 snprintf(msg, sizeof(msg), "unknown initiator error; "
136 "Status-Class 0x%x, Status-Detail 0x%x",
143 return ("Target error");
145 return ("Service unavailable");
147 return ("Out of resources");
149 snprintf(msg, sizeof(msg), "unknown target error; "
150 "Status-Class 0x%x, Status-Detail 0x%x",
155 snprintf(msg, sizeof(msg), "unknown error; "
156 "Status-Class 0x%x, Status-Detail 0x%x",
163 kernel_modify(const struct iscsid_connection *conn, const char *target_address)
165 struct iscsi_session_modify ism;
168 memset(&ism, 0, sizeof(ism));
169 ism.ism_session_id = conn->conn_session_id;
170 memcpy(&ism.ism_conf, &conn->conn_conf, sizeof(ism.ism_conf));
171 strlcpy(ism.ism_conf.isc_target_addr, target_address,
172 sizeof(ism.ism_conf.isc_target_addr));
173 error = ioctl(conn->conn_iscsi_fd, ISCSISMODIFY, &ism);
175 log_err(1, "failed to redirect to %s: ISCSISMODIFY",
181 * XXX: The way it works is suboptimal; what should happen is described
182 * in draft-gilligan-iscsi-fault-tolerance-00. That, however, would
183 * be much more complicated: we would need to keep "dependencies"
184 * for sessions, so that, in case described in draft and using draft
185 * terminology, we would have three sessions: one for discovery,
186 * one for initial target portal, and one for redirect portal.
187 * This would allow us to "backtrack" on connection failure,
188 * as described in draft.
191 login_handle_redirection(struct iscsid_connection *conn, struct pdu *response)
193 struct iscsi_bhs_login_response *bhslr;
194 struct keys *response_keys;
195 const char *target_address;
197 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
198 assert (bhslr->bhslr_status_class == 1);
200 response_keys = keys_new();
201 keys_load_pdu(response_keys, response);
203 target_address = keys_find(response_keys, "TargetAddress");
204 if (target_address == NULL)
205 log_errx(1, "received redirection without TargetAddress");
206 if (target_address[0] == '\0')
207 log_errx(1, "received redirection with empty TargetAddress");
208 if (strlen(target_address) >=
209 sizeof(conn->conn_conf.isc_target_addr) - 1)
210 log_errx(1, "received TargetAddress is too long");
212 log_debugx("received redirection to \"%s\"", target_address);
213 kernel_modify(conn, target_address);
214 keys_delete(response_keys);
218 login_receive(struct connection *conn)
220 struct pdu *response;
221 struct iscsi_bhs_login_response *bhslr;
222 const char *errorstr;
223 static bool initial = true;
225 response = pdu_new(conn);
226 pdu_receive(response);
227 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGIN_RESPONSE) {
228 log_errx(1, "protocol error: received invalid opcode 0x%x",
229 response->pdu_bhs->bhs_opcode);
231 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
233 * XXX: Implement the C flag some day.
235 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0)
236 log_errx(1, "received Login PDU with unsupported \"C\" flag");
237 if (bhslr->bhslr_version_max != 0x00)
238 log_errx(1, "received Login PDU with unsupported "
239 "Version-max 0x%x", bhslr->bhslr_version_max);
240 if (bhslr->bhslr_version_active != 0x00)
241 log_errx(1, "received Login PDU with unsupported "
242 "Version-active 0x%x", bhslr->bhslr_version_active);
243 if (bhslr->bhslr_status_class == 1) {
244 login_handle_redirection((struct iscsid_connection *)conn,
246 log_debugx("redirection handled; exiting");
249 if (bhslr->bhslr_status_class != 0) {
250 errorstr = login_target_error_str(bhslr->bhslr_status_class,
251 bhslr->bhslr_status_detail);
252 fail(conn, errorstr);
253 log_errx(1, "target returned error: %s", errorstr);
255 if (initial == false &&
256 ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) {
258 * It's a warning, not an error, to work around what seems
259 * to be bug in NetBSD iSCSI target.
261 log_warnx("received Login PDU with wrong StatSN: "
262 "is %u, should be %u", ntohl(bhslr->bhslr_statsn),
263 conn->conn_statsn + 1);
265 conn->conn_tsih = ntohs(bhslr->bhslr_tsih);
266 conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
274 login_new_request(struct connection *conn, int csg)
277 struct iscsi_bhs_login_request *bhslr;
280 request = pdu_new(conn);
281 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
282 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_REQUEST |
283 ISCSI_BHS_OPCODE_IMMEDIATE;
285 bhslr->bhslr_flags = BHSLR_FLAGS_TRANSIT;
287 case BHSLR_STAGE_SECURITY_NEGOTIATION:
288 nsg = BHSLR_STAGE_OPERATIONAL_NEGOTIATION;
290 case BHSLR_STAGE_OPERATIONAL_NEGOTIATION:
291 nsg = BHSLR_STAGE_FULL_FEATURE_PHASE;
294 assert(!"invalid csg");
295 log_errx(1, "invalid csg %d", csg);
297 login_set_csg(request, csg);
298 login_set_nsg(request, nsg);
300 memcpy(bhslr->bhslr_isid, &conn->conn_isid, sizeof(bhslr->bhslr_isid));
301 bhslr->bhslr_tsih = htons(conn->conn_tsih);
302 bhslr->bhslr_initiator_task_tag = 0;
303 bhslr->bhslr_cmdsn = 0;
304 bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1);
310 login_list_prefers(const char *list,
311 const char *choice1, const char *choice2)
313 char *tofree, *str, *token;
315 tofree = str = checked_strdup(list);
317 while ((token = strsep(&str, ",")) != NULL) {
318 if (strcmp(token, choice1) == 0) {
322 if (strcmp(token, choice2) == 0) {
332 login_negotiate_key(struct iscsid_connection *conn, const char *name,
335 struct iscsi_session_limits *isl;
338 isl = &conn->conn_limits;
339 if (strcmp(name, "TargetAlias") == 0) {
340 strlcpy(conn->conn_target_alias, value,
341 sizeof(conn->conn_target_alias));
342 } else if (strcmp(value, "Irrelevant") == 0) {
344 } else if (strcmp(name, "iSCSIProtocolLevel") == 0) {
345 tmp = strtoul(value, NULL, 10);
346 if (tmp < 0 || tmp > 31)
347 log_errx(1, "received invalid iSCSIProtocolLevel");
348 conn->conn_protocol_level = tmp;
349 } else if (strcmp(name, "HeaderDigest") == 0) {
350 which = login_list_prefers(value, "CRC32C", "None");
353 log_debugx("target prefers CRC32C "
354 "for header digest; we'll use it");
355 conn->conn.conn_header_digest = CONN_DIGEST_CRC32C;
358 log_debugx("target prefers not to do "
359 "header digest; we'll comply");
362 log_warnx("target sent unrecognized "
363 "HeaderDigest value \"%s\"; will use None", value);
366 } else if (strcmp(name, "DataDigest") == 0) {
367 which = login_list_prefers(value, "CRC32C", "None");
370 log_debugx("target prefers CRC32C "
371 "for data digest; we'll use it");
372 conn->conn.conn_data_digest = CONN_DIGEST_CRC32C;
375 log_debugx("target prefers not to do "
376 "data digest; we'll comply");
379 log_warnx("target sent unrecognized "
380 "DataDigest value \"%s\"; will use None", value);
383 } else if (strcmp(name, "MaxConnections") == 0) {
385 } else if (strcmp(name, "InitialR2T") == 0) {
386 if (strcmp(value, "Yes") == 0)
387 conn->conn_initial_r2t = true;
389 conn->conn_initial_r2t = false;
390 } else if (strcmp(name, "ImmediateData") == 0) {
391 if (strcmp(value, "Yes") == 0)
392 conn->conn.conn_immediate_data = true;
394 conn->conn.conn_immediate_data = false;
395 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
396 tmp = strtoul(value, NULL, 10);
398 log_errx(1, "received invalid "
399 "MaxRecvDataSegmentLength");
400 if (tmp > isl->isl_max_send_data_segment_length) {
401 log_debugx("capping max_send_data_segment_length "
402 "from %d to %d", tmp,
403 isl->isl_max_send_data_segment_length);
404 tmp = isl->isl_max_send_data_segment_length;
406 conn->conn.conn_max_send_data_segment_length = tmp;
407 } else if (strcmp(name, "MaxBurstLength") == 0) {
408 tmp = strtoul(value, NULL, 10);
410 log_errx(1, "received invalid MaxBurstLength");
411 if (tmp > isl->isl_max_burst_length) {
412 log_debugx("capping MaxBurstLength "
413 "from %d to %d", tmp, isl->isl_max_burst_length);
414 tmp = isl->isl_max_burst_length;
416 conn->conn.conn_max_burst_length = tmp;
417 } else if (strcmp(name, "FirstBurstLength") == 0) {
418 tmp = strtoul(value, NULL, 10);
420 log_errx(1, "received invalid FirstBurstLength");
421 if (tmp > isl->isl_first_burst_length) {
422 log_debugx("capping FirstBurstLength "
423 "from %d to %d", tmp, isl->isl_first_burst_length);
424 tmp = isl->isl_first_burst_length;
426 conn->conn.conn_first_burst_length = tmp;
427 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
429 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
431 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
433 } else if (strcmp(name, "DataPDUInOrder") == 0) {
435 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
437 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
439 } else if (strcmp(name, "OFMarker") == 0) {
441 } else if (strcmp(name, "IFMarker") == 0) {
443 } else if (strcmp(name, "RDMAExtensions") == 0) {
444 if (conn->conn_conf.isc_iser == 1 &&
445 strcmp(value, "Yes") != 0) {
446 log_errx(1, "received unsupported RDMAExtensions");
448 } else if (strcmp(name, "InitiatorRecvDataSegmentLength") == 0) {
449 tmp = strtoul(value, NULL, 10);
451 log_errx(1, "received invalid "
452 "InitiatorRecvDataSegmentLength");
453 if ((int)tmp > isl->isl_max_recv_data_segment_length) {
454 log_debugx("capping InitiatorRecvDataSegmentLength "
455 "from %d to %d", tmp,
456 isl->isl_max_recv_data_segment_length);
457 tmp = isl->isl_max_recv_data_segment_length;
459 conn->conn.conn_max_recv_data_segment_length = tmp;
460 } else if (strcmp(name, "TargetPortalGroupTag") == 0) {
462 } else if (strcmp(name, "TargetRecvDataSegmentLength") == 0) {
463 tmp = strtoul(value, NULL, 10);
466 "received invalid TargetRecvDataSegmentLength");
468 if (tmp > isl->isl_max_send_data_segment_length) {
469 log_debugx("capping TargetRecvDataSegmentLength "
470 "from %d to %d", tmp,
471 isl->isl_max_send_data_segment_length);
472 tmp = isl->isl_max_send_data_segment_length;
474 conn->conn.conn_max_send_data_segment_length = tmp;
476 log_debugx("unknown key \"%s\"; ignoring", name);
481 login_negotiate(struct iscsid_connection *conn)
483 struct pdu *request, *response;
484 struct keys *request_keys, *response_keys;
485 struct iscsi_bhs_login_response *bhslr;
486 int i, nrequests = 0;
487 struct iscsi_session_limits *isl;
489 log_debugx("beginning operational parameter negotiation");
490 request = login_new_request(&conn->conn,
491 BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
492 request_keys = keys_new();
494 isl = &conn->conn_limits;
495 log_debugx("Limits for offload \"%s\" are "
496 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
497 "MaxBurstLength=%d, FirstBurstLength=%d",
498 conn->conn_conf.isc_offload, isl->isl_max_recv_data_segment_length,
499 isl->isl_max_send_data_segment_length, isl->isl_max_burst_length,
500 isl->isl_first_burst_length);
503 * The following keys are irrelevant for discovery sessions.
505 if (conn->conn_conf.isc_discovery == 0) {
506 keys_add(request_keys, "iSCSIProtocolLevel", "2");
507 if (conn->conn_conf.isc_header_digest != 0)
508 keys_add(request_keys, "HeaderDigest", "CRC32C");
510 keys_add(request_keys, "HeaderDigest", "None");
511 if (conn->conn_conf.isc_data_digest != 0)
512 keys_add(request_keys, "DataDigest", "CRC32C");
514 keys_add(request_keys, "DataDigest", "None");
516 keys_add(request_keys, "ImmediateData", "Yes");
517 keys_add_int(request_keys, "MaxBurstLength",
518 isl->isl_max_burst_length);
519 keys_add_int(request_keys, "FirstBurstLength",
520 isl->isl_first_burst_length);
521 keys_add(request_keys, "InitialR2T", "Yes");
522 keys_add(request_keys, "MaxOutstandingR2T", "1");
523 if (conn->conn_conf.isc_iser == 1) {
524 keys_add_int(request_keys, "InitiatorRecvDataSegmentLength",
525 isl->isl_max_recv_data_segment_length);
526 keys_add_int(request_keys, "TargetRecvDataSegmentLength",
527 isl->isl_max_send_data_segment_length);
528 keys_add(request_keys, "RDMAExtensions", "Yes");
530 keys_add_int(request_keys, "MaxRecvDataSegmentLength",
531 isl->isl_max_recv_data_segment_length);
534 keys_add(request_keys, "HeaderDigest", "None");
535 keys_add(request_keys, "DataDigest", "None");
536 keys_add_int(request_keys, "MaxRecvDataSegmentLength",
537 isl->isl_max_recv_data_segment_length);
540 conn->conn.conn_max_recv_data_segment_length =
541 isl->isl_max_recv_data_segment_length;
543 keys_add(request_keys, "DefaultTime2Wait", "0");
544 keys_add(request_keys, "DefaultTime2Retain", "0");
545 keys_add(request_keys, "ErrorRecoveryLevel", "0");
546 keys_save_pdu(request_keys, request);
547 keys_delete(request_keys);
553 response = login_receive(&conn->conn);
554 response_keys = keys_new();
555 keys_load_pdu(response_keys, response);
556 for (i = 0; i < KEYS_MAX; i++) {
557 if (response_keys->keys_names[i] == NULL)
560 login_negotiate_key(conn,
561 response_keys->keys_names[i], response_keys->keys_values[i]);
564 keys_delete(response_keys);
565 response_keys = NULL;
568 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
569 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0)
574 log_warnx("received login response "
575 "without the \"T\" flag too many times; giving up");
579 log_debugx("received login response "
580 "without the \"T\" flag; sending another request");
582 pdu_delete(response);
584 request = login_new_request(&conn->conn,
585 BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
589 response = login_receive(&conn->conn);
592 if (login_nsg(response) != BHSLR_STAGE_FULL_FEATURE_PHASE)
593 log_warnx("received final login response with wrong NSG 0x%x",
594 login_nsg(response));
595 pdu_delete(response);
597 log_debugx("operational parameter negotiation done; "
598 "transitioning to Full Feature phase");
602 login_send_chap_a(struct connection *conn)
605 struct keys *request_keys;
607 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION);
608 request_keys = keys_new();
609 keys_add(request_keys, "CHAP_A", "5");
610 keys_save_pdu(request_keys, request);
611 keys_delete(request_keys);
617 login_send_chap_r(struct pdu *response)
619 struct iscsid_connection *conn;
621 struct keys *request_keys, *response_keys;
623 const char *chap_a, *chap_c, *chap_i;
626 char *mutual_chap_c, *mutual_chap_i;
629 * As in the rest of the initiator, 'request' means
630 * 'initiator -> target', and 'response' means 'target -> initiator',
632 * So, here the 'response' from the target is the packet that contains
633 * CHAP challenge; our CHAP response goes into 'request'.
636 conn = (struct iscsid_connection *)response->pdu_connection;
638 response_keys = keys_new();
639 keys_load_pdu(response_keys, response);
642 * First, compute the response.
644 chap_a = keys_find(response_keys, "CHAP_A");
646 log_errx(1, "received CHAP packet without CHAP_A");
647 chap_c = keys_find(response_keys, "CHAP_C");
649 log_errx(1, "received CHAP packet without CHAP_C");
650 chap_i = keys_find(response_keys, "CHAP_I");
652 log_errx(1, "received CHAP packet without CHAP_I");
654 if (strcmp(chap_a, "5") != 0) {
655 log_errx(1, "received CHAP packet "
656 "with unsupported CHAP_A \"%s\"", chap_a);
659 rchap = rchap_new(conn->conn_conf.isc_secret);
660 error = rchap_receive(rchap, chap_i, chap_c);
662 log_errx(1, "received CHAP packet "
663 "with malformed CHAP_I or CHAP_C");
665 chap_r = rchap_get_response(rchap);
668 keys_delete(response_keys);
670 request = login_new_request(&conn->conn,
671 BHSLR_STAGE_SECURITY_NEGOTIATION);
672 request_keys = keys_new();
673 keys_add(request_keys, "CHAP_N", conn->conn_conf.isc_user);
674 keys_add(request_keys, "CHAP_R", chap_r);
678 * If we want mutual authentication, we're expected to send
679 * our CHAP_I/CHAP_C now.
681 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
682 log_debugx("requesting mutual authentication; "
683 "binary challenge size is %zd bytes",
684 sizeof(conn->conn_mutual_chap->chap_challenge));
686 assert(conn->conn_mutual_chap == NULL);
687 conn->conn_mutual_chap = chap_new();
688 mutual_chap_i = chap_get_id(conn->conn_mutual_chap);
689 mutual_chap_c = chap_get_challenge(conn->conn_mutual_chap);
690 keys_add(request_keys, "CHAP_I", mutual_chap_i);
691 keys_add(request_keys, "CHAP_C", mutual_chap_c);
696 keys_save_pdu(request_keys, request);
697 keys_delete(request_keys);
703 login_verify_mutual(const struct pdu *response)
705 struct iscsid_connection *conn;
706 struct keys *response_keys;
707 const char *chap_n, *chap_r;
710 conn = (struct iscsid_connection *)response->pdu_connection;
712 response_keys = keys_new();
713 keys_load_pdu(response_keys, response);
715 chap_n = keys_find(response_keys, "CHAP_N");
717 log_errx(1, "received CHAP Response PDU without CHAP_N");
718 chap_r = keys_find(response_keys, "CHAP_R");
720 log_errx(1, "received CHAP Response PDU without CHAP_R");
722 error = chap_receive(conn->conn_mutual_chap, chap_r);
724 log_errx(1, "received CHAP Response PDU with invalid CHAP_R");
726 if (strcmp(chap_n, conn->conn_conf.isc_mutual_user) != 0) {
727 fail(&conn->conn, "Mutual CHAP failed");
728 log_errx(1, "mutual CHAP authentication failed: wrong user");
731 error = chap_authenticate(conn->conn_mutual_chap,
732 conn->conn_conf.isc_mutual_secret);
734 fail(&conn->conn, "Mutual CHAP failed");
735 log_errx(1, "mutual CHAP authentication failed: wrong secret");
738 keys_delete(response_keys);
739 chap_delete(conn->conn_mutual_chap);
740 conn->conn_mutual_chap = NULL;
742 log_debugx("mutual CHAP authentication succeeded");
746 login_chap(struct iscsid_connection *conn)
748 struct pdu *response;
750 log_debugx("beginning CHAP authentication; sending CHAP_A");
751 login_send_chap_a(&conn->conn);
753 log_debugx("waiting for CHAP_A/CHAP_C/CHAP_I");
754 response = login_receive(&conn->conn);
756 log_debugx("sending CHAP_N/CHAP_R");
757 login_send_chap_r(response);
758 pdu_delete(response);
761 * XXX: Make sure this is not susceptible to MITM.
764 log_debugx("waiting for CHAP result");
765 response = login_receive(&conn->conn);
766 if (conn->conn_conf.isc_mutual_user[0] != '\0')
767 login_verify_mutual(response);
768 pdu_delete(response);
770 log_debugx("CHAP authentication done");
774 login(struct iscsid_connection *conn)
776 struct pdu *request, *response;
777 struct keys *request_keys, *response_keys;
778 struct iscsi_bhs_login_response *bhslr2;
779 const char *auth_method;
782 log_debugx("beginning Login phase; sending Login PDU");
783 request = login_new_request(&conn->conn,
784 BHSLR_STAGE_SECURITY_NEGOTIATION);
785 request_keys = keys_new();
786 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
787 keys_add(request_keys, "AuthMethod", "CHAP");
788 } else if (conn->conn_conf.isc_user[0] != '\0') {
790 * Give target a chance to skip authentication if it
791 * doesn't feel like it.
793 * None is first, CHAP second; this is to work around
794 * what seems to be LIO (Linux target) bug: otherwise,
795 * if target is configured with no authentication,
796 * and we are configured to authenticate, the target
797 * will erroneously respond with AuthMethod=CHAP
798 * instead of AuthMethod=None, and will subsequently
799 * fail the connection. This usually happens with
800 * Discovery sessions, which default to no authentication.
802 keys_add(request_keys, "AuthMethod", "None,CHAP");
804 keys_add(request_keys, "AuthMethod", "None");
806 keys_add(request_keys, "InitiatorName",
807 conn->conn_conf.isc_initiator);
808 if (conn->conn_conf.isc_initiator_alias[0] != '\0') {
809 keys_add(request_keys, "InitiatorAlias",
810 conn->conn_conf.isc_initiator_alias);
812 if (conn->conn_conf.isc_discovery == 0) {
813 keys_add(request_keys, "SessionType", "Normal");
814 keys_add(request_keys,
815 "TargetName", conn->conn_conf.isc_target);
817 keys_add(request_keys, "SessionType", "Discovery");
819 keys_save_pdu(request_keys, request);
820 keys_delete(request_keys);
824 response = login_receive(&conn->conn);
826 response_keys = keys_new();
827 keys_load_pdu(response_keys, response);
829 for (i = 0; i < KEYS_MAX; i++) {
830 if (response_keys->keys_names[i] == NULL)
834 * Not interested in AuthMethod at this point; we only need
835 * to parse things such as TargetAlias.
837 * XXX: This is somewhat ugly. We should have a way to apply
838 * all the keys to the session and use that by default
839 * instead of discarding them.
841 if (strcmp(response_keys->keys_names[i], "AuthMethod") == 0)
844 login_negotiate_key(conn,
845 response_keys->keys_names[i], response_keys->keys_values[i]);
848 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
849 if ((bhslr2->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0 &&
850 login_nsg(response) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
851 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
852 log_errx(1, "target requested transition "
853 "to operational parameter negotiation, "
854 "but we require mutual CHAP");
857 log_debugx("target requested transition "
858 "to operational parameter negotiation");
859 keys_delete(response_keys);
860 pdu_delete(response);
861 login_negotiate(conn);
865 auth_method = keys_find(response_keys, "AuthMethod");
866 if (auth_method == NULL)
867 log_errx(1, "received response without AuthMethod");
868 if (strcmp(auth_method, "None") == 0) {
869 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
870 log_errx(1, "target does not require authantication, "
871 "but we require mutual CHAP");
874 log_debugx("target does not require authentication");
875 keys_delete(response_keys);
876 pdu_delete(response);
877 login_negotiate(conn);
881 if (strcmp(auth_method, "CHAP") != 0) {
882 fail(&conn->conn, "Unsupported AuthMethod");
883 log_errx(1, "received response "
884 "with unsupported AuthMethod \"%s\"", auth_method);
887 if (conn->conn_conf.isc_user[0] == '\0' ||
888 conn->conn_conf.isc_secret[0] == '\0') {
889 fail(&conn->conn, "Authentication required");
890 log_errx(1, "target requests CHAP authentication, but we don't "
891 "have user and secret");
894 keys_delete(response_keys);
895 response_keys = NULL;
896 pdu_delete(response);
900 login_negotiate(conn);