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>
44 #include "iscsi_proto.h"
47 login_nsg(const struct pdu *response)
49 struct iscsi_bhs_login_response *bhslr;
51 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
53 return (bhslr->bhslr_flags & 0x03);
57 login_set_nsg(struct pdu *request, int nsg)
59 struct iscsi_bhs_login_request *bhslr;
61 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
62 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
63 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
65 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
67 bhslr->bhslr_flags &= 0xFC;
68 bhslr->bhslr_flags |= nsg;
72 login_set_csg(struct pdu *request, int csg)
74 struct iscsi_bhs_login_request *bhslr;
76 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
77 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
78 csg == BHSLR_STAGE_FULL_FEATURE_PHASE);
80 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
82 bhslr->bhslr_flags &= 0xF3;
83 bhslr->bhslr_flags |= csg << 2;
87 login_target_error_str(int class, int detail)
92 * RFC 3270, 10.13.5. Status-Class and Status-Detail
98 return ("Target moved temporarily");
100 return ("Target moved permanently");
102 snprintf(msg, sizeof(msg), "unknown redirection; "
103 "Status-Class 0x%x, Status-Detail 0x%x",
110 return ("Initiator error");
112 return ("Authentication failure");
114 return ("Authorization failure");
116 return ("Not found");
118 return ("Target removed");
120 return ("Unsupported version");
122 return ("Too many connections");
124 return ("Missing parameter");
126 return ("Can't include in session");
128 return ("Session type not supported");
130 return ("Session does not exist");
132 return ("Invalid during login");
134 snprintf(msg, sizeof(msg), "unknown initiator error; "
135 "Status-Class 0x%x, Status-Detail 0x%x",
142 return ("Target error");
144 return ("Service unavailable");
146 return ("Out of resources");
148 snprintf(msg, sizeof(msg), "unknown target error; "
149 "Status-Class 0x%x, Status-Detail 0x%x",
154 snprintf(msg, sizeof(msg), "unknown error; "
155 "Status-Class 0x%x, Status-Detail 0x%x",
162 kernel_modify(const struct connection *conn, const char *target_address)
164 struct iscsi_session_modify ism;
167 memset(&ism, 0, sizeof(ism));
168 ism.ism_session_id = conn->conn_session_id;
169 memcpy(&ism.ism_conf, &conn->conn_conf, sizeof(ism.ism_conf));
170 strlcpy(ism.ism_conf.isc_target_addr, target_address,
171 sizeof(ism.ism_conf.isc_target));
172 error = ioctl(conn->conn_iscsi_fd, ISCSISMODIFY, &ism);
174 log_err(1, "failed to redirect to %s: ISCSISMODIFY",
180 * XXX: The way it works is suboptimal; what should happen is described
181 * in draft-gilligan-iscsi-fault-tolerance-00. That, however, would
182 * be much more complicated: we would need to keep "dependencies"
183 * for sessions, so that, in case described in draft and using draft
184 * terminology, we would have three sessions: one for discovery,
185 * one for initial target portal, and one for redirect portal.
186 * This would allow us to "backtrack" on connection failure,
187 * as described in draft.
190 login_handle_redirection(struct connection *conn, struct pdu *response)
192 struct iscsi_bhs_login_response *bhslr;
193 struct keys *response_keys;
194 const char *target_address;
196 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
197 assert (bhslr->bhslr_status_class == 1);
199 response_keys = keys_new();
200 keys_load(response_keys, response);
202 target_address = keys_find(response_keys, "TargetAddress");
203 if (target_address == NULL)
204 log_errx(1, "received redirection without TargetAddress");
205 if (target_address[0] == '\0')
206 log_errx(1, "received redirection with empty TargetAddress");
207 if (strlen(target_address) >=
208 sizeof(conn->conn_conf.isc_target_addr) - 1)
209 log_errx(1, "received TargetAddress is too long");
211 log_debugx("received redirection to \"%s\"", target_address);
212 kernel_modify(conn, target_address);
213 keys_delete(response_keys);
217 login_receive(struct connection *conn)
219 struct pdu *response;
220 struct iscsi_bhs_login_response *bhslr;
221 const char *errorstr;
222 static bool initial = true;
224 response = pdu_new(conn);
225 pdu_receive(response);
226 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGIN_RESPONSE) {
227 log_errx(1, "protocol error: received invalid opcode 0x%x",
228 response->pdu_bhs->bhs_opcode);
230 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
232 * XXX: Implement the C flag some day.
234 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0)
235 log_errx(1, "received Login PDU with unsupported \"C\" flag");
236 if (bhslr->bhslr_version_max != 0x00)
237 log_errx(1, "received Login PDU with unsupported "
238 "Version-max 0x%x", bhslr->bhslr_version_max);
239 if (bhslr->bhslr_version_active != 0x00)
240 log_errx(1, "received Login PDU with unsupported "
241 "Version-active 0x%x", bhslr->bhslr_version_active);
242 if (bhslr->bhslr_status_class == 1) {
243 login_handle_redirection(conn, response);
244 log_debugx("redirection handled; exiting");
247 if (bhslr->bhslr_status_class != 0) {
248 errorstr = login_target_error_str(bhslr->bhslr_status_class,
249 bhslr->bhslr_status_detail);
250 fail(conn, errorstr);
251 log_errx(1, "target returned error: %s", errorstr);
253 if (initial == false &&
254 ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) {
256 * It's a warning, not an error, to work around what seems
257 * to be bug in NetBSD iSCSI target.
259 log_warnx("received Login PDU with wrong StatSN: "
260 "is %u, should be %u", ntohl(bhslr->bhslr_statsn),
261 conn->conn_statsn + 1);
263 conn->conn_tsih = ntohs(bhslr->bhslr_tsih);
264 conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
272 login_new_request(struct connection *conn, int csg)
275 struct iscsi_bhs_login_request *bhslr;
278 request = pdu_new(conn);
279 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
280 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_REQUEST |
281 ISCSI_BHS_OPCODE_IMMEDIATE;
283 bhslr->bhslr_flags = BHSLR_FLAGS_TRANSIT;
285 case BHSLR_STAGE_SECURITY_NEGOTIATION:
286 nsg = BHSLR_STAGE_OPERATIONAL_NEGOTIATION;
288 case BHSLR_STAGE_OPERATIONAL_NEGOTIATION:
289 nsg = BHSLR_STAGE_FULL_FEATURE_PHASE;
292 assert(!"invalid csg");
293 log_errx(1, "invalid csg %d", csg);
295 login_set_csg(request, csg);
296 login_set_nsg(request, nsg);
298 memcpy(bhslr->bhslr_isid, &conn->conn_isid, sizeof(bhslr->bhslr_isid));
299 bhslr->bhslr_tsih = htons(conn->conn_tsih);
300 bhslr->bhslr_initiator_task_tag = 0;
301 bhslr->bhslr_cmdsn = 0;
302 bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1);
308 login_list_prefers(const char *list,
309 const char *choice1, const char *choice2)
311 char *tofree, *str, *token;
313 tofree = str = checked_strdup(list);
315 while ((token = strsep(&str, ",")) != NULL) {
316 if (strcmp(token, choice1) == 0) {
320 if (strcmp(token, choice2) == 0) {
330 login_negotiate_key(struct connection *conn, const char *name,
333 struct iscsi_session_limits *isl;
336 isl = &conn->conn_limits;
337 if (strcmp(name, "TargetAlias") == 0) {
338 strlcpy(conn->conn_target_alias, value,
339 sizeof(conn->conn_target_alias));
340 } else if (strcmp(value, "Irrelevant") == 0) {
342 } else if (strcmp(name, "HeaderDigest") == 0) {
343 which = login_list_prefers(value, "CRC32C", "None");
346 log_debugx("target prefers CRC32C "
347 "for header digest; we'll use it");
348 conn->conn_header_digest = CONN_DIGEST_CRC32C;
351 log_debugx("target prefers not to do "
352 "header digest; we'll comply");
355 log_warnx("target sent unrecognized "
356 "HeaderDigest value \"%s\"; will use None", value);
359 } else if (strcmp(name, "DataDigest") == 0) {
360 which = login_list_prefers(value, "CRC32C", "None");
363 log_debugx("target prefers CRC32C "
364 "for data digest; we'll use it");
365 conn->conn_data_digest = CONN_DIGEST_CRC32C;
368 log_debugx("target prefers not to do "
369 "data digest; we'll comply");
372 log_warnx("target sent unrecognized "
373 "DataDigest value \"%s\"; will use None", value);
376 } else if (strcmp(name, "MaxConnections") == 0) {
378 } else if (strcmp(name, "InitialR2T") == 0) {
379 if (strcmp(value, "Yes") == 0)
380 conn->conn_initial_r2t = true;
382 conn->conn_initial_r2t = false;
383 } else if (strcmp(name, "ImmediateData") == 0) {
384 if (strcmp(value, "Yes") == 0)
385 conn->conn_immediate_data = true;
387 conn->conn_immediate_data = false;
388 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
389 tmp = strtoul(value, NULL, 10);
391 log_errx(1, "received invalid "
392 "MaxRecvDataSegmentLength");
393 if (tmp > isl->isl_max_send_data_segment_length) {
394 log_debugx("capping max_send_data_segment_length "
395 "from %d to %d", tmp,
396 isl->isl_max_send_data_segment_length);
397 tmp = isl->isl_max_send_data_segment_length;
399 conn->conn_max_send_data_segment_length = tmp;
400 } else if (strcmp(name, "MaxBurstLength") == 0) {
401 tmp = strtoul(value, NULL, 10);
403 log_errx(1, "received invalid MaxBurstLength");
404 if (tmp > isl->isl_max_burst_length) {
405 log_debugx("capping MaxBurstLength "
406 "from %d to %d", tmp, isl->isl_max_burst_length);
407 tmp = isl->isl_max_burst_length;
409 conn->conn_max_burst_length = tmp;
410 } else if (strcmp(name, "FirstBurstLength") == 0) {
411 tmp = strtoul(value, NULL, 10);
413 log_errx(1, "received invalid FirstBurstLength");
414 if (tmp > isl->isl_first_burst_length) {
415 log_debugx("capping FirstBurstLength "
416 "from %d to %d", tmp, isl->isl_first_burst_length);
417 tmp = isl->isl_first_burst_length;
419 conn->conn_first_burst_length = tmp;
420 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
422 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
424 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
426 } else if (strcmp(name, "DataPDUInOrder") == 0) {
428 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
430 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
432 } else if (strcmp(name, "OFMarker") == 0) {
434 } else if (strcmp(name, "IFMarker") == 0) {
436 } else if (strcmp(name, "RDMAExtensions") == 0) {
437 if (conn->conn_conf.isc_iser == 1 &&
438 strcmp(value, "Yes") != 0) {
439 log_errx(1, "received unsupported RDMAExtensions");
441 } else if (strcmp(name, "InitiatorRecvDataSegmentLength") == 0) {
442 tmp = strtoul(value, NULL, 10);
444 log_errx(1, "received invalid "
445 "InitiatorRecvDataSegmentLength");
446 if ((int)tmp > isl->isl_max_recv_data_segment_length) {
447 log_debugx("capping InitiatorRecvDataSegmentLength "
448 "from %d to %d", tmp,
449 isl->isl_max_recv_data_segment_length);
450 tmp = isl->isl_max_recv_data_segment_length;
452 conn->conn_max_recv_data_segment_length = tmp;
453 } else if (strcmp(name, "TargetPortalGroupTag") == 0) {
455 } else if (strcmp(name, "TargetRecvDataSegmentLength") == 0) {
456 tmp = strtoul(value, NULL, 10);
459 "received invalid TargetRecvDataSegmentLength");
461 if (tmp > isl->isl_max_send_data_segment_length) {
462 log_debugx("capping TargetRecvDataSegmentLength "
463 "from %d to %d", tmp,
464 isl->isl_max_send_data_segment_length);
465 tmp = isl->isl_max_send_data_segment_length;
467 conn->conn_max_send_data_segment_length = tmp;
469 log_debugx("unknown key \"%s\"; ignoring", name);
474 login_negotiate(struct connection *conn)
476 struct pdu *request, *response;
477 struct keys *request_keys, *response_keys;
478 struct iscsi_bhs_login_response *bhslr;
479 int i, nrequests = 0;
480 struct iscsi_session_limits *isl;
482 log_debugx("beginning operational parameter negotiation");
483 request = login_new_request(conn, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
484 request_keys = keys_new();
486 isl = &conn->conn_limits;
487 log_debugx("Limits for offload \"%s\" are "
488 "MaxRecvDataSegment=%d, max_send_dsl=%d, "
489 "MaxBurstLength=%d, FirstBurstLength=%d",
490 conn->conn_conf.isc_offload, isl->isl_max_recv_data_segment_length,
491 isl->isl_max_send_data_segment_length, isl->isl_max_burst_length,
492 isl->isl_first_burst_length);
495 * The following keys are irrelevant for discovery sessions.
497 if (conn->conn_conf.isc_discovery == 0) {
498 if (conn->conn_conf.isc_header_digest != 0)
499 keys_add(request_keys, "HeaderDigest", "CRC32C");
501 keys_add(request_keys, "HeaderDigest", "None");
502 if (conn->conn_conf.isc_data_digest != 0)
503 keys_add(request_keys, "DataDigest", "CRC32C");
505 keys_add(request_keys, "DataDigest", "None");
507 keys_add(request_keys, "ImmediateData", "Yes");
508 keys_add_int(request_keys, "MaxBurstLength",
509 isl->isl_max_burst_length);
510 keys_add_int(request_keys, "FirstBurstLength",
511 isl->isl_first_burst_length);
512 keys_add(request_keys, "InitialR2T", "Yes");
513 keys_add(request_keys, "MaxOutstandingR2T", "1");
514 if (conn->conn_conf.isc_iser == 1) {
515 keys_add_int(request_keys, "InitiatorRecvDataSegmentLength",
516 isl->isl_max_recv_data_segment_length);
517 keys_add_int(request_keys, "TargetRecvDataSegmentLength",
518 isl->isl_max_send_data_segment_length);
519 keys_add(request_keys, "RDMAExtensions", "Yes");
521 keys_add_int(request_keys, "MaxRecvDataSegmentLength",
522 isl->isl_max_recv_data_segment_length);
525 keys_add(request_keys, "HeaderDigest", "None");
526 keys_add(request_keys, "DataDigest", "None");
527 keys_add_int(request_keys, "MaxRecvDataSegmentLength",
528 isl->isl_max_recv_data_segment_length);
531 keys_add(request_keys, "DefaultTime2Wait", "0");
532 keys_add(request_keys, "DefaultTime2Retain", "0");
533 keys_add(request_keys, "ErrorRecoveryLevel", "0");
534 keys_save(request_keys, request);
535 keys_delete(request_keys);
541 response = login_receive(conn);
542 response_keys = keys_new();
543 keys_load(response_keys, response);
544 for (i = 0; i < KEYS_MAX; i++) {
545 if (response_keys->keys_names[i] == NULL)
548 login_negotiate_key(conn,
549 response_keys->keys_names[i], response_keys->keys_values[i]);
552 keys_delete(response_keys);
553 response_keys = NULL;
556 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
557 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0)
562 log_warnx("received login response "
563 "without the \"T\" flag too many times; giving up");
567 log_debugx("received login response "
568 "without the \"T\" flag; sending another request");
570 pdu_delete(response);
572 request = login_new_request(conn,
573 BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
577 response = login_receive(conn);
580 if (login_nsg(response) != BHSLR_STAGE_FULL_FEATURE_PHASE)
581 log_warnx("received final login response with wrong NSG 0x%x",
582 login_nsg(response));
583 pdu_delete(response);
585 log_debugx("operational parameter negotiation done; "
586 "transitioning to Full Feature phase");
590 login_send_chap_a(struct connection *conn)
593 struct keys *request_keys;
595 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION);
596 request_keys = keys_new();
597 keys_add(request_keys, "CHAP_A", "5");
598 keys_save(request_keys, request);
599 keys_delete(request_keys);
605 login_send_chap_r(struct pdu *response)
607 struct connection *conn;
609 struct keys *request_keys, *response_keys;
611 const char *chap_a, *chap_c, *chap_i;
614 char *mutual_chap_c, *mutual_chap_i;
617 * As in the rest of the initiator, 'request' means
618 * 'initiator -> target', and 'response' means 'target -> initiator',
620 * So, here the 'response' from the target is the packet that contains
621 * CHAP challenge; our CHAP response goes into 'request'.
624 conn = response->pdu_connection;
626 response_keys = keys_new();
627 keys_load(response_keys, response);
630 * First, compute the response.
632 chap_a = keys_find(response_keys, "CHAP_A");
634 log_errx(1, "received CHAP packet without CHAP_A");
635 chap_c = keys_find(response_keys, "CHAP_C");
637 log_errx(1, "received CHAP packet without CHAP_C");
638 chap_i = keys_find(response_keys, "CHAP_I");
640 log_errx(1, "received CHAP packet without CHAP_I");
642 if (strcmp(chap_a, "5") != 0) {
643 log_errx(1, "received CHAP packet "
644 "with unsupported CHAP_A \"%s\"", chap_a);
647 rchap = rchap_new(conn->conn_conf.isc_secret);
648 error = rchap_receive(rchap, chap_i, chap_c);
650 log_errx(1, "received CHAP packet "
651 "with malformed CHAP_I or CHAP_C");
653 chap_r = rchap_get_response(rchap);
656 keys_delete(response_keys);
658 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION);
659 request_keys = keys_new();
660 keys_add(request_keys, "CHAP_N", conn->conn_conf.isc_user);
661 keys_add(request_keys, "CHAP_R", chap_r);
665 * If we want mutual authentication, we're expected to send
666 * our CHAP_I/CHAP_C now.
668 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
669 log_debugx("requesting mutual authentication; "
670 "binary challenge size is %zd bytes",
671 sizeof(conn->conn_mutual_chap->chap_challenge));
673 assert(conn->conn_mutual_chap == NULL);
674 conn->conn_mutual_chap = chap_new();
675 mutual_chap_i = chap_get_id(conn->conn_mutual_chap);
676 mutual_chap_c = chap_get_challenge(conn->conn_mutual_chap);
677 keys_add(request_keys, "CHAP_I", mutual_chap_i);
678 keys_add(request_keys, "CHAP_C", mutual_chap_c);
683 keys_save(request_keys, request);
684 keys_delete(request_keys);
690 login_verify_mutual(const struct pdu *response)
692 struct connection *conn;
693 struct keys *response_keys;
694 const char *chap_n, *chap_r;
697 conn = response->pdu_connection;
699 response_keys = keys_new();
700 keys_load(response_keys, response);
702 chap_n = keys_find(response_keys, "CHAP_N");
704 log_errx(1, "received CHAP Response PDU without CHAP_N");
705 chap_r = keys_find(response_keys, "CHAP_R");
707 log_errx(1, "received CHAP Response PDU without CHAP_R");
709 error = chap_receive(conn->conn_mutual_chap, chap_r);
711 log_errx(1, "received CHAP Response PDU with invalid CHAP_R");
713 if (strcmp(chap_n, conn->conn_conf.isc_mutual_user) != 0) {
714 fail(conn, "Mutual CHAP failed");
715 log_errx(1, "mutual CHAP authentication failed: wrong user");
718 error = chap_authenticate(conn->conn_mutual_chap,
719 conn->conn_conf.isc_mutual_secret);
721 fail(conn, "Mutual CHAP failed");
722 log_errx(1, "mutual CHAP authentication failed: wrong secret");
725 keys_delete(response_keys);
726 chap_delete(conn->conn_mutual_chap);
727 conn->conn_mutual_chap = NULL;
729 log_debugx("mutual CHAP authentication succeeded");
733 login_chap(struct connection *conn)
735 struct pdu *response;
737 log_debugx("beginning CHAP authentication; sending CHAP_A");
738 login_send_chap_a(conn);
740 log_debugx("waiting for CHAP_A/CHAP_C/CHAP_I");
741 response = login_receive(conn);
743 log_debugx("sending CHAP_N/CHAP_R");
744 login_send_chap_r(response);
745 pdu_delete(response);
748 * XXX: Make sure this is not susceptible to MITM.
751 log_debugx("waiting for CHAP result");
752 response = login_receive(conn);
753 if (conn->conn_conf.isc_mutual_user[0] != '\0')
754 login_verify_mutual(response);
755 pdu_delete(response);
757 log_debugx("CHAP authentication done");
761 login(struct connection *conn)
763 struct pdu *request, *response;
764 struct keys *request_keys, *response_keys;
765 struct iscsi_bhs_login_response *bhslr2;
766 const char *auth_method;
769 log_debugx("beginning Login phase; sending Login PDU");
770 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION);
771 request_keys = keys_new();
772 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
773 keys_add(request_keys, "AuthMethod", "CHAP");
774 } else if (conn->conn_conf.isc_user[0] != '\0') {
776 * Give target a chance to skip authentication if it
777 * doesn't feel like it.
779 * None is first, CHAP second; this is to work around
780 * what seems to be LIO (Linux target) bug: otherwise,
781 * if target is configured with no authentication,
782 * and we are configured to authenticate, the target
783 * will erroneously respond with AuthMethod=CHAP
784 * instead of AuthMethod=None, and will subsequently
785 * fail the connection. This usually happens with
786 * Discovery sessions, which default to no authentication.
788 keys_add(request_keys, "AuthMethod", "None,CHAP");
790 keys_add(request_keys, "AuthMethod", "None");
792 keys_add(request_keys, "InitiatorName",
793 conn->conn_conf.isc_initiator);
794 if (conn->conn_conf.isc_initiator_alias[0] != '\0') {
795 keys_add(request_keys, "InitiatorAlias",
796 conn->conn_conf.isc_initiator_alias);
798 if (conn->conn_conf.isc_discovery == 0) {
799 keys_add(request_keys, "SessionType", "Normal");
800 keys_add(request_keys,
801 "TargetName", conn->conn_conf.isc_target);
803 keys_add(request_keys, "SessionType", "Discovery");
805 keys_save(request_keys, request);
806 keys_delete(request_keys);
810 response = login_receive(conn);
812 response_keys = keys_new();
813 keys_load(response_keys, response);
815 for (i = 0; i < KEYS_MAX; i++) {
816 if (response_keys->keys_names[i] == NULL)
820 * Not interested in AuthMethod at this point; we only need
821 * to parse things such as TargetAlias.
823 * XXX: This is somewhat ugly. We should have a way to apply
824 * all the keys to the session and use that by default
825 * instead of discarding them.
827 if (strcmp(response_keys->keys_names[i], "AuthMethod") == 0)
830 login_negotiate_key(conn,
831 response_keys->keys_names[i], response_keys->keys_values[i]);
834 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
835 if ((bhslr2->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0 &&
836 login_nsg(response) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
837 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
838 log_errx(1, "target requested transition "
839 "to operational parameter negotiation, "
840 "but we require mutual CHAP");
843 log_debugx("target requested transition "
844 "to operational parameter negotiation");
845 keys_delete(response_keys);
846 pdu_delete(response);
847 login_negotiate(conn);
851 auth_method = keys_find(response_keys, "AuthMethod");
852 if (auth_method == NULL)
853 log_errx(1, "received response without AuthMethod");
854 if (strcmp(auth_method, "None") == 0) {
855 if (conn->conn_conf.isc_mutual_user[0] != '\0') {
856 log_errx(1, "target does not require authantication, "
857 "but we require mutual CHAP");
860 log_debugx("target does not require authentication");
861 keys_delete(response_keys);
862 pdu_delete(response);
863 login_negotiate(conn);
867 if (strcmp(auth_method, "CHAP") != 0) {
868 fail(conn, "Unsupported AuthMethod");
869 log_errx(1, "received response "
870 "with unsupported AuthMethod \"%s\"", auth_method);
873 if (conn->conn_conf.isc_user[0] == '\0' ||
874 conn->conn_conf.isc_secret[0] == '\0') {
875 fail(conn, "Authentication required");
876 log_errx(1, "target requests CHAP authentication, but we don't "
877 "have user and secret");
880 keys_delete(response_keys);
881 response_keys = NULL;
882 pdu_delete(response);
886 login_negotiate(conn);