2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
41 #include <netinet/in.h>
42 #include <openssl/err.h>
43 #include <openssl/md5.h>
44 #include <openssl/rand.h>
47 #include "iscsi_proto.h"
50 login_nsg(const struct pdu *response)
52 struct iscsi_bhs_login_response *bhslr;
54 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
56 return (bhslr->bhslr_flags & 0x03);
60 login_set_nsg(struct pdu *request, int nsg)
62 struct iscsi_bhs_login_request *bhslr;
64 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
65 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
66 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
68 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
70 bhslr->bhslr_flags &= 0xFC;
71 bhslr->bhslr_flags |= nsg;
75 login_set_csg(struct pdu *request, int csg)
77 struct iscsi_bhs_login_request *bhslr;
79 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
80 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
81 csg == BHSLR_STAGE_FULL_FEATURE_PHASE);
83 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
85 bhslr->bhslr_flags &= 0xF3;
86 bhslr->bhslr_flags |= csg << 2;
90 login_target_error_str(int class, int detail)
95 * RFC 3270, 10.13.5. Status-Class and Status-Detail
101 return ("Target moved temporarily");
103 return ("Target moved permanently");
105 snprintf(msg, sizeof(msg), "unknown redirection; "
106 "Status-Class 0x%x, Status-Detail 0x%x",
113 return ("Initiator error");
115 return ("Authentication failure");
117 return ("Authorization failure");
119 return ("Not found");
121 return ("Target removed");
123 return ("Unsupported version");
125 return ("Too many connections");
127 return ("Missing parameter");
129 return ("Can't include in session");
131 return ("Session type not supported");
133 return ("Session does not exist");
135 return ("Invalid during login");
137 snprintf(msg, sizeof(msg), "unknown initiator error; "
138 "Status-Class 0x%x, Status-Detail 0x%x",
145 return ("Target error");
147 return ("Service unavailable");
149 return ("Out of resources");
151 snprintf(msg, sizeof(msg), "unknown target error; "
152 "Status-Class 0x%x, Status-Detail 0x%x",
157 snprintf(msg, sizeof(msg), "unknown error; "
158 "Status-Class 0x%x, Status-Detail 0x%x",
165 kernel_modify(const struct connection *conn, const char *target_address)
167 struct iscsi_session_modify ism;
170 memset(&ism, 0, sizeof(ism));
171 ism.ism_session_id = conn->conn_session_id;
172 memcpy(&ism.ism_conf, &conn->conn_conf, sizeof(ism.ism_conf));
173 strlcpy(ism.ism_conf.isc_target_addr, target_address,
174 sizeof(ism.ism_conf.isc_target));
175 error = ioctl(conn->conn_iscsi_fd, ISCSISMODIFY, &ism);
177 log_err(1, "failed to redirect to %s: ISCSISMODIFY",
183 * XXX: The way it works is suboptimal; what should happen is described
184 * in draft-gilligan-iscsi-fault-tolerance-00. That, however, would
185 * be much more complicated: we would need to keep "dependencies"
186 * for sessions, so that, in case described in draft and using draft
187 * terminology, we would have three sessions: one for discovery,
188 * one for initial target portal, and one for redirect portal.
189 * This would allow us to "backtrack" on connection failure,
190 * as described in draft.
193 login_handle_redirection(struct connection *conn, struct pdu *response)
195 struct iscsi_bhs_login_response *bhslr;
196 struct keys *response_keys;
197 const char *target_address;
199 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
200 assert (bhslr->bhslr_status_class == 1);
202 response_keys = keys_new();
203 keys_load(response_keys, response);
205 target_address = keys_find(response_keys, "TargetAddress");
206 if (target_address == NULL)
207 log_errx(1, "received redirection without TargetAddress");
208 if (target_address[0] == '\0')
209 log_errx(1, "received redirection with empty TargetAddress");
210 if (strlen(target_address) >=
211 sizeof(conn->conn_conf.isc_target_addr) - 1)
212 log_errx(1, "received TargetAddress is too long");
214 log_debugx("received redirection to \"%s\"", target_address);
215 kernel_modify(conn, target_address);
219 login_receive(struct connection *conn)
221 struct pdu *response;
222 struct iscsi_bhs_login_response *bhslr;
223 const char *errorstr;
224 static bool initial = true;
226 response = pdu_new(conn);
227 pdu_receive(response);
228 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGIN_RESPONSE) {
229 log_errx(1, "protocol error: received invalid opcode 0x%x",
230 response->pdu_bhs->bhs_opcode);
232 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
234 * XXX: Implement the C flag some day.
236 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0)
237 log_errx(1, "received Login PDU with unsupported \"C\" flag");
238 if (bhslr->bhslr_version_max != 0x00)
239 log_errx(1, "received Login PDU with unsupported "
240 "Version-max 0x%x", bhslr->bhslr_version_max);
241 if (bhslr->bhslr_version_active != 0x00)
242 log_errx(1, "received Login PDU with unsupported "
243 "Version-active 0x%x", bhslr->bhslr_version_active);
244 if (bhslr->bhslr_status_class == 1) {
245 login_handle_redirection(conn, response);
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 %d, should be %d", 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_hex2int(const char hex)
379 * XXX: Review this _carefully_.
382 login_hex2bin(const char *hex, char **binp, size_t *bin_lenp)
384 int i, hex_len, nibble;
385 bool lo = true; /* As opposed to 'hi'. */
387 size_t bin_off, bin_len;
389 if (strncasecmp(hex, "0x", strlen("0x")) != 0) {
390 log_warnx("malformed variable, should start with \"0x\"");
395 hex_len = strlen(hex);
397 log_warnx("malformed variable; doesn't contain anything "
402 bin_len = hex_len / 2 + hex_len % 2;
403 bin = calloc(bin_len, 1);
405 log_err(1, "calloc");
407 bin_off = bin_len - 1;
408 for (i = hex_len - 1; i >= 0; i--) {
409 nibble = login_hex2int(hex[i]);
411 log_warnx("malformed variable, invalid char \"%c\"",
416 assert(bin_off < bin_len);
418 bin[bin_off] = nibble;
421 bin[bin_off] |= nibble << 4;
433 login_bin2hex(const char *bin, size_t bin_len)
435 unsigned char *hex, *tmp, ch;
439 hex_len = bin_len * 2 + 3; /* +2 for "0x", +1 for '\0'. */
440 hex = malloc(hex_len);
442 log_err(1, "malloc");
445 tmp += sprintf(tmp, "0x");
446 for (i = 0; i < bin_len; i++) {
448 tmp += sprintf(tmp, "%02x", ch);
455 login_compute_md5(const char id, const char *secret,
456 const void *challenge, size_t challenge_len, void *response,
462 assert(response_len == MD5_DIGEST_LENGTH);
465 MD5_Update(&ctx, &id, sizeof(id));
466 MD5_Update(&ctx, secret, strlen(secret));
467 MD5_Update(&ctx, challenge, challenge_len);
468 rv = MD5_Final(response, &ctx);
470 log_errx(1, "MD5_Final");
474 login_negotiate_key(struct connection *conn, const char *name,
479 if (strcmp(name, "TargetAlias") == 0) {
480 strlcpy(conn->conn_target_alias, value,
481 sizeof(conn->conn_target_alias));
482 } else if (strcmp(value, "Irrelevant") == 0) {
484 } else if (strcmp(name, "HeaderDigest") == 0) {
485 which = login_list_prefers(value, "CRC32C", "None");
488 log_debugx("target prefers CRC32C "
489 "for header digest; we'll use it");
490 conn->conn_header_digest = CONN_DIGEST_CRC32C;
493 log_debugx("target prefers not to do "
494 "header digest; we'll comply");
497 log_warnx("target sent unrecognized "
498 "HeaderDigest value \"%s\"; will use None", value);
501 } else if (strcmp(name, "DataDigest") == 0) {
502 which = login_list_prefers(value, "CRC32C", "None");
505 log_debugx("target prefers CRC32C "
506 "for data digest; we'll use it");
507 conn->conn_data_digest = CONN_DIGEST_CRC32C;
510 log_debugx("target prefers not to do "
511 "data digest; we'll comply");
514 log_warnx("target sent unrecognized "
515 "DataDigest value \"%s\"; will use None", value);
518 } else if (strcmp(name, "MaxConnections") == 0) {
520 } else if (strcmp(name, "InitialR2T") == 0) {
521 if (strcmp(value, "Yes") == 0)
522 conn->conn_initial_r2t = true;
524 conn->conn_initial_r2t = false;
525 } else if (strcmp(name, "ImmediateData") == 0) {
526 if (strcmp(value, "Yes") == 0)
527 conn->conn_immediate_data = true;
529 conn->conn_immediate_data = false;
530 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
531 tmp = strtoul(value, NULL, 10);
533 log_errx(1, "received invalid "
534 "MaxRecvDataSegmentLength");
535 conn->conn_max_data_segment_length = tmp;
536 } else if (strcmp(name, "MaxBurstLength") == 0) {
537 if (conn->conn_immediate_data) {
538 tmp = strtoul(value, NULL, 10);
540 log_errx(1, "received invalid MaxBurstLength");
541 conn->conn_max_burst_length = tmp;
543 } else if (strcmp(name, "FirstBurstLength") == 0) {
544 tmp = strtoul(value, NULL, 10);
546 log_errx(1, "received invalid FirstBurstLength");
547 conn->conn_first_burst_length = tmp;
548 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
550 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
552 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
554 } else if (strcmp(name, "DataPDUInOrder") == 0) {
556 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
558 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
560 } else if (strcmp(name, "OFMarker") == 0) {
562 } else if (strcmp(name, "IFMarker") == 0) {
564 } else if (strcmp(name, "TargetPortalGroupTag") == 0) {
567 log_debugx("unknown key \"%s\"; ignoring", name);
572 login_negotiate(struct connection *conn)
574 struct pdu *request, *response;
575 struct keys *request_keys, *response_keys;
576 struct iscsi_bhs_login_response *bhslr;
577 int i, nrequests = 0;
579 log_debugx("beginning operational parameter negotiation");
580 request = login_new_request(conn, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
581 request_keys = keys_new();
584 * The following keys are irrelevant for discovery sessions.
586 if (conn->conn_conf.isc_discovery == 0) {
587 if (conn->conn_conf.isc_header_digest != 0)
588 keys_add(request_keys, "HeaderDigest", "CRC32C");
590 keys_add(request_keys, "HeaderDigest", "None");
591 if (conn->conn_conf.isc_data_digest != 0)
592 keys_add(request_keys, "DataDigest", "CRC32C");
594 keys_add(request_keys, "DataDigest", "None");
596 keys_add(request_keys, "ImmediateData", "Yes");
597 keys_add_int(request_keys, "MaxBurstLength",
598 ISCSI_MAX_DATA_SEGMENT_LENGTH);
599 keys_add_int(request_keys, "FirstBurstLength",
600 ISCSI_MAX_DATA_SEGMENT_LENGTH);
601 keys_add(request_keys, "InitialR2T", "Yes");
603 keys_add(request_keys, "HeaderDigest", "None");
604 keys_add(request_keys, "DataDigest", "None");
607 keys_add_int(request_keys, "MaxRecvDataSegmentLength",
608 ISCSI_MAX_DATA_SEGMENT_LENGTH);
609 keys_add(request_keys, "DefaultTime2Wait", "0");
610 keys_add(request_keys, "DefaultTime2Retain", "0");
611 keys_add(request_keys, "ErrorRecoveryLevel", "0");
612 keys_add(request_keys, "MaxOutstandingR2T", "1");
613 keys_save(request_keys, request);
614 keys_delete(request_keys);
620 response = login_receive(conn);
621 response_keys = keys_new();
622 keys_load(response_keys, response);
623 for (i = 0; i < KEYS_MAX; i++) {
624 if (response_keys->keys_names[i] == NULL)
627 login_negotiate_key(conn,
628 response_keys->keys_names[i], response_keys->keys_values[i]);
631 keys_delete(response_keys);
632 response_keys = NULL;
635 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
636 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0)
641 log_warnx("received login response "
642 "without the \"T\" flag too many times; giving up");
646 log_debugx("received login response "
647 "without the \"T\" flag; sending another request");
649 pdu_delete(response);
651 request = login_new_request(conn,
652 BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
656 response = login_receive(conn);
659 if (login_nsg(response) != BHSLR_STAGE_FULL_FEATURE_PHASE)
660 log_warnx("received final login response with wrong NSG 0x%x",
661 login_nsg(response));
662 pdu_delete(response);
664 log_debugx("operational parameter negotiation done; "
665 "transitioning to Full Feature phase");
669 login_send_chap_a(struct connection *conn)
672 struct keys *request_keys;
674 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION);
675 request_keys = keys_new();
676 keys_add(request_keys, "CHAP_A", "5");
677 keys_save(request_keys, request);
678 keys_delete(request_keys);
684 login_send_chap_r(struct pdu *response)
686 struct connection *conn;
688 struct keys *request_keys, *response_keys;
689 const char *chap_a, *chap_c, *chap_i;
690 char *chap_r, *challenge, response_bin[MD5_DIGEST_LENGTH];
691 size_t challenge_len;
694 char *mutual_chap_c, mutual_chap_i[4];
697 * As in the rest of the initiator, 'request' means
698 * 'initiator -> target', and 'response' means 'target -> initiator',
700 * So, here the 'response' from the target is the packet that contains
701 * CHAP challenge; our CHAP response goes into 'request'.
704 conn = response->pdu_connection;
706 response_keys = keys_new();
707 keys_load(response_keys, response);
710 * First, compute the response.
712 chap_a = keys_find(response_keys, "CHAP_A");
714 log_errx(1, "received CHAP packet without CHAP_A");
715 chap_c = keys_find(response_keys, "CHAP_C");
717 log_errx(1, "received CHAP packet without CHAP_C");
718 chap_i = keys_find(response_keys, "CHAP_I");
720 log_errx(1, "received CHAP packet without CHAP_I");
722 if (strcmp(chap_a, "5") != 0)
723 log_errx(1, "received CHAP packet "
724 "with unsupported CHAP_A \"%s\"", chap_a);
725 id = strtoul(chap_i, NULL, 10);
726 error = login_hex2bin(chap_c, &challenge, &challenge_len);
728 log_errx(1, "received CHAP packet with malformed CHAP_C");
729 login_compute_md5(id, conn->conn_conf.isc_secret,
730 challenge, challenge_len, response_bin, sizeof(response_bin));
732 chap_r = login_bin2hex(response_bin, sizeof(response_bin));
734 keys_delete(response_keys);
736 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION);
737 request_keys = keys_new();
738 keys_add(request_keys, "CHAP_N", conn->conn_conf.isc_user);
739 keys_add(request_keys, "CHAP_R", chap_r);
743 * If we want mutual authentication, we're expected to send
744 * our CHAP_I/CHAP_C now.
746 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
747 log_debugx("requesting mutual authentication; "
748 "binary challenge size is %zd bytes",
749 sizeof(conn->conn_mutual_challenge));
751 rv = RAND_bytes(conn->conn_mutual_challenge,
752 sizeof(conn->conn_mutual_challenge));
754 log_errx(1, "RAND_bytes failed: %s",
755 ERR_error_string(ERR_get_error(), NULL));
757 rv = RAND_bytes(&conn->conn_mutual_id,
758 sizeof(conn->conn_mutual_id));
760 log_errx(1, "RAND_bytes failed: %s",
761 ERR_error_string(ERR_get_error(), NULL));
763 mutual_chap_c = login_bin2hex(conn->conn_mutual_challenge,
764 sizeof(conn->conn_mutual_challenge));
765 snprintf(mutual_chap_i, sizeof(mutual_chap_i),
766 "%d", conn->conn_mutual_id);
767 keys_add(request_keys, "CHAP_I", mutual_chap_i);
768 keys_add(request_keys, "CHAP_C", mutual_chap_c);
772 keys_save(request_keys, request);
773 keys_delete(request_keys);
779 login_verify_mutual(const struct pdu *response)
781 struct connection *conn;
782 struct keys *response_keys;
783 const char *chap_n, *chap_r;
784 char *response_bin, expected_response_bin[MD5_DIGEST_LENGTH];
785 size_t response_bin_len;
788 conn = response->pdu_connection;
790 response_keys = keys_new();
791 keys_load(response_keys, response);
793 chap_n = keys_find(response_keys, "CHAP_N");
795 log_errx(1, "received CHAP Response PDU without CHAP_N");
796 chap_r = keys_find(response_keys, "CHAP_R");
798 log_errx(1, "received CHAP Response PDU without CHAP_R");
799 error = login_hex2bin(chap_r, &response_bin, &response_bin_len);
801 log_errx(1, "received CHAP Response PDU with malformed CHAP_R");
803 if (strcmp(chap_n, conn->conn_conf.isc_mutual_user) != 0) {
804 fail(conn, "Mutual CHAP failed");
805 log_errx(1, "mutual CHAP authentication failed: wrong user");
808 login_compute_md5(conn->conn_mutual_id,
809 conn->conn_conf.isc_mutual_secret, conn->conn_mutual_challenge,
810 sizeof(conn->conn_mutual_challenge), expected_response_bin,
811 sizeof(expected_response_bin));
813 if (memcmp(response_bin, expected_response_bin,
814 sizeof(expected_response_bin)) != 0) {
815 fail(conn, "Mutual CHAP failed");
816 log_errx(1, "mutual CHAP authentication failed: wrong secret");
819 keys_delete(response_keys);
822 log_debugx("mutual CHAP authentication succeeded");
826 login_chap(struct connection *conn)
828 struct pdu *response;
830 log_debugx("beginning CHAP authentication; sending CHAP_A");
831 login_send_chap_a(conn);
833 log_debugx("waiting for CHAP_A/CHAP_C/CHAP_I");
834 response = login_receive(conn);
836 log_debugx("sending CHAP_N/CHAP_R");
837 login_send_chap_r(response);
838 pdu_delete(response);
841 * XXX: Make sure this is not susceptible to MITM.
844 log_debugx("waiting for CHAP result");
845 response = login_receive(conn);
846 if (conn->conn_conf.isc_mutual_user[0] != '\0')
847 login_verify_mutual(response);
848 pdu_delete(response);
850 log_debugx("CHAP authentication done");
854 login(struct connection *conn)
856 struct pdu *request, *response;
857 struct keys *request_keys, *response_keys;
858 struct iscsi_bhs_login_response *bhslr2;
859 const char *auth_method;
862 log_debugx("beginning Login phase; sending Login PDU");
863 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION);
864 request_keys = keys_new();
865 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
866 keys_add(request_keys, "AuthMethod", "CHAP");
867 } else if (conn->conn_conf.isc_user[0] != '\0') {
869 * Give target a chance to skip authentication if it
870 * doesn't feel like it.
872 * None is first, CHAP second; this is to work around
873 * what seems to be LIO (Linux target) bug: otherwise,
874 * if target is configured with no authentication,
875 * and we are configured to authenticate, the target
876 * will erroneously respond with AuthMethod=CHAP
877 * instead of AuthMethod=None, and will subsequently
878 * fail the connection. This usually happens with
879 * Discovery sessions, which default to no authentication.
881 keys_add(request_keys, "AuthMethod", "None,CHAP");
883 keys_add(request_keys, "AuthMethod", "None");
885 keys_add(request_keys, "InitiatorName",
886 conn->conn_conf.isc_initiator);
887 if (conn->conn_conf.isc_initiator_alias[0] != '\0') {
888 keys_add(request_keys, "InitiatorAlias",
889 conn->conn_conf.isc_initiator_alias);
891 if (conn->conn_conf.isc_discovery == 0) {
892 keys_add(request_keys, "SessionType", "Normal");
893 keys_add(request_keys,
894 "TargetName", conn->conn_conf.isc_target);
896 keys_add(request_keys, "SessionType", "Discovery");
898 keys_save(request_keys, request);
899 keys_delete(request_keys);
903 response = login_receive(conn);
905 response_keys = keys_new();
906 keys_load(response_keys, response);
908 for (i = 0; i < KEYS_MAX; i++) {
909 if (response_keys->keys_names[i] == NULL)
913 * Not interested in AuthMethod at this point; we only need
914 * to parse things such as TargetAlias.
916 * XXX: This is somewhat ugly. We should have a way to apply
917 * all the keys to the session and use that by default
918 * instead of discarding them.
920 if (strcmp(response_keys->keys_names[i], "AuthMethod") == 0)
923 login_negotiate_key(conn,
924 response_keys->keys_names[i], response_keys->keys_values[i]);
927 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
928 if ((bhslr2->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0 &&
929 login_nsg(response) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
930 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
931 log_errx(1, "target requested transition "
932 "to operational parameter negotiation, "
933 "but we require mutual CHAP");
936 log_debugx("target requested transition "
937 "to operational parameter negotiation");
938 keys_delete(response_keys);
939 pdu_delete(response);
940 login_negotiate(conn);
944 auth_method = keys_find(response_keys, "AuthMethod");
945 if (auth_method == NULL)
946 log_errx(1, "received response without AuthMethod");
947 if (strcmp(auth_method, "None") == 0) {
948 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
949 log_errx(1, "target does not require authantication, "
950 "but we require mutual CHAP");
953 log_debugx("target does not require authentication");
954 keys_delete(response_keys);
955 pdu_delete(response);
956 login_negotiate(conn);
960 if (strcmp(auth_method, "CHAP") != 0) {
961 fail(conn, "Unsupported AuthMethod");
962 log_errx(1, "received response "
963 "with unsupported AuthMethod \"%s\"", auth_method);
966 if (conn->conn_conf.isc_user[0] == '\0' ||
967 conn->conn_conf.isc_secret[0] == '\0') {
968 fail(conn, "Authentication required");
969 log_errx(1, "target requests CHAP authentication, but we don't "
970 "have user and secret");
973 keys_delete(response_keys);
974 response_keys = NULL;
975 pdu_delete(response);
979 login_negotiate(conn);