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$");
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"
49 static void login_send_error(struct pdu *request,
50 char class, char detail);
53 login_set_nsg(struct pdu *response, int nsg)
55 struct iscsi_bhs_login_response *bhslr;
57 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
58 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
59 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
61 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
63 bhslr->bhslr_flags &= 0xFC;
64 bhslr->bhslr_flags |= nsg;
68 login_csg(const struct pdu *request)
70 struct iscsi_bhs_login_request *bhslr;
72 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
74 return ((bhslr->bhslr_flags & 0x0C) >> 2);
78 login_set_csg(struct pdu *response, int csg)
80 struct iscsi_bhs_login_response *bhslr;
82 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
83 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
84 csg == BHSLR_STAGE_FULL_FEATURE_PHASE);
86 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
88 bhslr->bhslr_flags &= 0xF3;
89 bhslr->bhslr_flags |= csg << 2;
93 login_receive(struct connection *conn, bool initial)
96 struct iscsi_bhs_login_request *bhslr;
98 request = pdu_new(conn);
100 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
101 ISCSI_BHS_OPCODE_LOGIN_REQUEST) {
103 * The first PDU in session is special - if we receive any PDU
104 * different than login request, we have to drop the connection
105 * without sending response ("A target receiving any PDU
106 * except a Login request before the Login Phase is started MUST
107 * immediately terminate the connection on which the PDU
110 if (initial == false)
111 login_send_error(request, 0x02, 0x0b);
112 log_errx(1, "protocol error: received invalid opcode 0x%x",
113 request->pdu_bhs->bhs_opcode);
115 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
117 * XXX: Implement the C flag some day.
119 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0) {
120 login_send_error(request, 0x03, 0x00);
121 log_errx(1, "received Login PDU with unsupported \"C\" flag");
123 if (bhslr->bhslr_version_max != 0x00) {
124 login_send_error(request, 0x02, 0x05);
125 log_errx(1, "received Login PDU with unsupported "
126 "Version-max 0x%x", bhslr->bhslr_version_max);
128 if (bhslr->bhslr_version_min != 0x00) {
129 login_send_error(request, 0x02, 0x05);
130 log_errx(1, "received Login PDU with unsupported "
131 "Version-min 0x%x", bhslr->bhslr_version_min);
133 if (ntohl(bhslr->bhslr_cmdsn) < conn->conn_cmdsn) {
134 login_send_error(request, 0x02, 0x05);
135 log_errx(1, "received Login PDU with decreasing CmdSN: "
136 "was %d, is %d", conn->conn_cmdsn,
137 ntohl(bhslr->bhslr_cmdsn));
139 if (initial == false &&
140 ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
141 login_send_error(request, 0x02, 0x05);
142 log_errx(1, "received Login PDU with wrong ExpStatSN: "
143 "is %d, should be %d", ntohl(bhslr->bhslr_expstatsn),
146 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
152 login_new_response(struct pdu *request)
154 struct pdu *response;
155 struct connection *conn;
156 struct iscsi_bhs_login_request *bhslr;
157 struct iscsi_bhs_login_response *bhslr2;
159 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
160 conn = request->pdu_connection;
162 response = pdu_new_response(request);
163 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
164 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_RESPONSE;
165 login_set_csg(response, BHSLR_STAGE_SECURITY_NEGOTIATION);
166 memcpy(bhslr2->bhslr_isid,
167 bhslr->bhslr_isid, sizeof(bhslr2->bhslr_isid));
168 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
169 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
170 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
171 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
177 login_send_error(struct pdu *request, char class, char detail)
179 struct pdu *response;
180 struct iscsi_bhs_login_response *bhslr2;
182 log_debugx("sending Login Response PDU with failure class 0x%x/0x%x; "
183 "see next line for reason", class, detail);
184 response = login_new_response(request);
185 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
186 bhslr2->bhslr_status_class = class;
187 bhslr2->bhslr_status_detail = detail;
190 pdu_delete(response);
194 login_list_contains(const char *list, const char *what)
196 char *tofree, *str, *token;
198 tofree = str = checked_strdup(list);
200 while ((token = strsep(&str, ",")) != NULL) {
201 if (strcmp(token, what) == 0) {
211 login_list_prefers(const char *list,
212 const char *choice1, const char *choice2)
214 char *tofree, *str, *token;
216 tofree = str = checked_strdup(list);
218 while ((token = strsep(&str, ",")) != NULL) {
219 if (strcmp(token, choice1) == 0) {
223 if (strcmp(token, choice2) == 0) {
233 login_hex2int(const char hex)
280 * XXX: Review this _carefully_.
283 login_hex2bin(const char *hex, char **binp, size_t *bin_lenp)
285 int i, hex_len, nibble;
286 bool lo = true; /* As opposed to 'hi'. */
288 size_t bin_off, bin_len;
290 if (strncasecmp(hex, "0x", strlen("0x")) != 0) {
291 log_warnx("malformed variable, should start with \"0x\"");
296 hex_len = strlen(hex);
298 log_warnx("malformed variable; doesn't contain anything "
303 bin_len = hex_len / 2 + hex_len % 2;
304 bin = calloc(bin_len, 1);
306 log_err(1, "calloc");
308 bin_off = bin_len - 1;
309 for (i = hex_len - 1; i >= 0; i--) {
310 nibble = login_hex2int(hex[i]);
312 log_warnx("malformed variable, invalid char \"%c\"",
317 assert(bin_off < bin_len);
319 bin[bin_off] = nibble;
322 bin[bin_off] |= nibble << 4;
334 login_bin2hex(const char *bin, size_t bin_len)
336 unsigned char *hex, *tmp, ch;
340 hex_len = bin_len * 2 + 3; /* +2 for "0x", +1 for '\0'. */
341 hex = malloc(hex_len);
343 log_err(1, "malloc");
346 tmp += sprintf(tmp, "0x");
347 for (i = 0; i < bin_len; i++) {
349 tmp += sprintf(tmp, "%02x", ch);
356 login_compute_md5(const char id, const char *secret,
357 const void *challenge, size_t challenge_len, void *response,
363 assert(response_len == MD5_DIGEST_LENGTH);
366 MD5_Update(&ctx, &id, sizeof(id));
367 MD5_Update(&ctx, secret, strlen(secret));
368 MD5_Update(&ctx, challenge, challenge_len);
369 rv = MD5_Final(response, &ctx);
371 log_errx(1, "MD5_Final");
374 #define LOGIN_CHALLENGE_LEN 1024
377 login_receive_chap_a(struct connection *conn)
380 struct keys *request_keys;
383 request = login_receive(conn, false);
384 request_keys = keys_new();
385 keys_load(request_keys, request);
387 chap_a = keys_find(request_keys, "CHAP_A");
388 if (chap_a == NULL) {
389 login_send_error(request, 0x02, 0x07);
390 log_errx(1, "received CHAP Login PDU without CHAP_A");
392 if (login_list_contains(chap_a, "5") == 0) {
393 login_send_error(request, 0x02, 0x01);
394 log_errx(1, "received CHAP Login PDU with unsupported CHAP_A "
397 keys_delete(request_keys);
403 login_send_chap_c(struct pdu *request, const unsigned char id,
404 const void *challenge, const size_t challenge_len)
406 struct pdu *response;
407 struct keys *response_keys;
408 char *chap_c, chap_i[4];
410 chap_c = login_bin2hex(challenge, challenge_len);
411 snprintf(chap_i, sizeof(chap_i), "%d", id);
413 response = login_new_response(request);
414 response_keys = keys_new();
415 keys_add(response_keys, "CHAP_A", "5");
416 keys_add(response_keys, "CHAP_I", chap_i);
417 keys_add(response_keys, "CHAP_C", chap_c);
419 keys_save(response_keys, response);
421 pdu_delete(response);
422 keys_delete(response_keys);
426 login_receive_chap_r(struct connection *conn,
427 struct auth_group *ag, const unsigned char id, const void *challenge,
428 const size_t challenge_len, const struct auth **cap)
431 struct keys *request_keys;
432 const char *chap_n, *chap_r;
433 char *response_bin, expected_response_bin[MD5_DIGEST_LENGTH];
434 size_t response_bin_len;
435 const struct auth *auth;
438 request = login_receive(conn, false);
439 request_keys = keys_new();
440 keys_load(request_keys, request);
442 chap_n = keys_find(request_keys, "CHAP_N");
443 if (chap_n == NULL) {
444 login_send_error(request, 0x02, 0x07);
445 log_errx(1, "received CHAP Login PDU without CHAP_N");
447 chap_r = keys_find(request_keys, "CHAP_R");
448 if (chap_r == NULL) {
449 login_send_error(request, 0x02, 0x07);
450 log_errx(1, "received CHAP Login PDU without CHAP_R");
452 error = login_hex2bin(chap_r, &response_bin, &response_bin_len);
454 login_send_error(request, 0x02, 0x07);
455 log_errx(1, "received CHAP Login PDU with malformed CHAP_R");
459 * Verify the response.
461 assert(ag->ag_type == AG_TYPE_CHAP ||
462 ag->ag_type == AG_TYPE_CHAP_MUTUAL);
463 auth = auth_find(ag, chap_n);
465 login_send_error(request, 0x02, 0x01);
466 log_errx(1, "received CHAP Login with invalid user \"%s\"",
470 assert(auth->a_secret != NULL);
471 assert(strlen(auth->a_secret) > 0);
472 login_compute_md5(id, auth->a_secret, challenge,
473 challenge_len, expected_response_bin,
474 sizeof(expected_response_bin));
476 if (memcmp(response_bin, expected_response_bin,
477 sizeof(expected_response_bin)) != 0) {
478 login_send_error(request, 0x02, 0x01);
479 log_errx(1, "CHAP authentication failed for user \"%s\"",
483 keys_delete(request_keys);
491 login_send_chap_success(struct pdu *request,
492 const struct auth *auth)
494 struct pdu *response;
495 struct keys *request_keys, *response_keys;
496 struct iscsi_bhs_login_response *bhslr2;
497 const char *chap_i, *chap_c;
498 char *chap_r, *challenge, response_bin[MD5_DIGEST_LENGTH];
499 size_t challenge_len;
503 response = login_new_response(request);
504 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
505 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
506 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
509 * Actually, one more thing: mutual authentication.
511 request_keys = keys_new();
512 keys_load(request_keys, request);
513 chap_i = keys_find(request_keys, "CHAP_I");
514 chap_c = keys_find(request_keys, "CHAP_C");
515 if (chap_i != NULL || chap_c != NULL) {
516 if (chap_i == NULL) {
517 login_send_error(request, 0x02, 0x07);
518 log_errx(1, "initiator requested target "
519 "authentication, but didn't send CHAP_I");
521 if (chap_c == NULL) {
522 login_send_error(request, 0x02, 0x07);
523 log_errx(1, "initiator requested target "
524 "authentication, but didn't send CHAP_C");
526 if (auth->a_auth_group->ag_type != AG_TYPE_CHAP_MUTUAL) {
527 login_send_error(request, 0x02, 0x01);
528 log_errx(1, "initiator requests target authentication "
529 "for user \"%s\", but mutual user/secret "
530 "is not set", auth->a_user);
533 id = strtoul(chap_i, NULL, 10);
534 error = login_hex2bin(chap_c, &challenge, &challenge_len);
536 login_send_error(request, 0x02, 0x07);
537 log_errx(1, "received CHAP Login PDU with malformed "
541 log_debugx("performing mutual authentication as user \"%s\"",
542 auth->a_mutual_user);
543 login_compute_md5(id, auth->a_mutual_secret, challenge,
544 challenge_len, response_bin, sizeof(response_bin));
546 chap_r = login_bin2hex(response_bin,
547 sizeof(response_bin));
548 response_keys = keys_new();
549 keys_add(response_keys, "CHAP_N", auth->a_mutual_user);
550 keys_add(response_keys, "CHAP_R", chap_r);
552 keys_save(response_keys, response);
553 keys_delete(response_keys);
555 log_debugx("initiator did not request target authentication");
558 keys_delete(request_keys);
560 pdu_delete(response);
564 login_chap(struct connection *conn, struct auth_group *ag)
566 const struct auth *auth;
568 char challenge_bin[LOGIN_CHALLENGE_LEN];
573 * Receive CHAP_A PDU.
575 log_debugx("beginning CHAP authentication; waiting for CHAP_A");
576 request = login_receive_chap_a(conn);
579 * Generate the challenge.
581 rv = RAND_bytes(challenge_bin, sizeof(challenge_bin));
583 login_send_error(request, 0x03, 0x02);
584 log_errx(1, "RAND_bytes failed: %s",
585 ERR_error_string(ERR_get_error(), NULL));
587 rv = RAND_bytes(&id, sizeof(id));
589 login_send_error(request, 0x03, 0x02);
590 log_errx(1, "RAND_bytes failed: %s",
591 ERR_error_string(ERR_get_error(), NULL));
595 * Send the challenge.
597 log_debugx("sending CHAP_C, binary challenge size is %zd bytes",
598 sizeof(challenge_bin));
599 login_send_chap_c(request, id, challenge_bin,
600 sizeof(challenge_bin));
604 * Receive CHAP_N/CHAP_R PDU and authenticate.
606 log_debugx("waiting for CHAP_N/CHAP_R");
607 request = login_receive_chap_r(conn, ag, id, challenge_bin,
608 sizeof(challenge_bin), &auth);
611 * Yay, authentication succeeded!
613 log_debugx("authentication succeeded for user \"%s\"; "
614 "transitioning to Negotiation Phase", auth->a_user);
615 login_send_chap_success(request, auth);
620 login_negotiate_key(struct pdu *request, const char *name,
621 const char *value, bool skipped_security, struct keys *response_keys)
624 struct connection *conn;
626 conn = request->pdu_connection;
628 if (strcmp(name, "InitiatorName") == 0) {
629 if (!skipped_security)
630 log_errx(1, "initiator resent InitiatorName");
631 } else if (strcmp(name, "SessionType") == 0) {
632 if (!skipped_security)
633 log_errx(1, "initiator resent SessionType");
634 } else if (strcmp(name, "TargetName") == 0) {
635 if (!skipped_security)
636 log_errx(1, "initiator resent TargetName");
637 } else if (strcmp(name, "InitiatorAlias") == 0) {
638 if (conn->conn_initiator_alias != NULL)
639 free(conn->conn_initiator_alias);
640 conn->conn_initiator_alias = checked_strdup(value);
641 } else if (strcmp(value, "Irrelevant") == 0) {
643 } else if (strcmp(name, "HeaderDigest") == 0) {
645 * We don't handle digests for discovery sessions.
647 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
648 log_debugx("discovery session; digests disabled");
649 keys_add(response_keys, name, "None");
653 which = login_list_prefers(value, "CRC32C", "None");
656 log_debugx("initiator prefers CRC32C "
657 "for header digest; we'll use it");
658 conn->conn_header_digest = CONN_DIGEST_CRC32C;
659 keys_add(response_keys, name, "CRC32C");
662 log_debugx("initiator prefers not to do "
663 "header digest; we'll comply");
664 keys_add(response_keys, name, "None");
667 log_warnx("initiator sent unrecognized "
668 "HeaderDigest value \"%s\"; will use None", value);
669 keys_add(response_keys, name, "None");
672 } else if (strcmp(name, "DataDigest") == 0) {
673 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
674 log_debugx("discovery session; digests disabled");
675 keys_add(response_keys, name, "None");
679 which = login_list_prefers(value, "CRC32C", "None");
682 log_debugx("initiator prefers CRC32C "
683 "for data digest; we'll use it");
684 conn->conn_data_digest = CONN_DIGEST_CRC32C;
685 keys_add(response_keys, name, "CRC32C");
688 log_debugx("initiator prefers not to do "
689 "data digest; we'll comply");
690 keys_add(response_keys, name, "None");
693 log_warnx("initiator sent unrecognized "
694 "DataDigest value \"%s\"; will use None", value);
695 keys_add(response_keys, name, "None");
698 } else if (strcmp(name, "MaxConnections") == 0) {
699 keys_add(response_keys, name, "1");
700 } else if (strcmp(name, "InitialR2T") == 0) {
701 keys_add(response_keys, name, "Yes");
702 } else if (strcmp(name, "ImmediateData") == 0) {
703 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
704 log_debugx("discovery session; ImmediateData irrelevant");
705 keys_add(response_keys, name, "Irrelevant");
707 if (strcmp(value, "Yes") == 0) {
708 conn->conn_immediate_data = true;
709 keys_add(response_keys, name, "Yes");
711 conn->conn_immediate_data = false;
712 keys_add(response_keys, name, "No");
715 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
716 tmp = strtoul(value, NULL, 10);
718 login_send_error(request, 0x02, 0x00);
719 log_errx(1, "received invalid "
720 "MaxRecvDataSegmentLength");
722 if (tmp > MAX_DATA_SEGMENT_LENGTH) {
723 log_debugx("capping MaxDataSegmentLength from %d to %d",
724 tmp, MAX_DATA_SEGMENT_LENGTH);
725 tmp = MAX_DATA_SEGMENT_LENGTH;
727 conn->conn_max_data_segment_length = tmp;
728 keys_add_int(response_keys, name, tmp);
729 } else if (strcmp(name, "MaxBurstLength") == 0) {
730 tmp = strtoul(value, NULL, 10);
732 login_send_error(request, 0x02, 0x00);
733 log_errx(1, "received invalid MaxBurstLength");
735 if (tmp > MAX_BURST_LENGTH) {
736 log_debugx("capping MaxBurstLength from %d to %d",
737 tmp, MAX_BURST_LENGTH);
738 tmp = MAX_BURST_LENGTH;
740 conn->conn_max_burst_length = tmp;
741 keys_add(response_keys, name, value);
742 } else if (strcmp(name, "FirstBurstLength") == 0) {
743 tmp = strtoul(value, NULL, 10);
745 login_send_error(request, 0x02, 0x00);
746 log_errx(1, "received invalid "
749 if (tmp > MAX_DATA_SEGMENT_LENGTH) {
750 log_debugx("capping FirstBurstLength from %d to %d",
751 tmp, MAX_DATA_SEGMENT_LENGTH);
752 tmp = MAX_DATA_SEGMENT_LENGTH;
755 * We don't pass the value to the kernel; it only enforces
756 * hardcoded limit anyway.
758 keys_add_int(response_keys, name, tmp);
759 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
760 keys_add(response_keys, name, value);
761 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
762 keys_add(response_keys, name, "0");
763 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
764 keys_add(response_keys, name, "1");
765 } else if (strcmp(name, "DataPDUInOrder") == 0) {
766 keys_add(response_keys, name, "Yes");
767 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
768 keys_add(response_keys, name, "Yes");
769 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
770 keys_add(response_keys, name, "0");
771 } else if (strcmp(name, "OFMarker") == 0) {
772 keys_add(response_keys, name, "No");
773 } else if (strcmp(name, "IFMarker") == 0) {
774 keys_add(response_keys, name, "No");
776 log_debugx("unknown key \"%s\"; responding "
777 "with NotUnderstood", name);
778 keys_add(response_keys, name, "NotUnderstood");
783 login_negotiate(struct connection *conn, struct pdu *request)
785 struct pdu *response;
786 struct iscsi_bhs_login_response *bhslr2;
787 struct keys *request_keys, *response_keys;
789 bool skipped_security;
791 if (request == NULL) {
792 log_debugx("beginning operational parameter negotiation; "
793 "waiting for Login PDU");
794 request = login_receive(conn, false);
795 skipped_security = false;
797 skipped_security = true;
799 request_keys = keys_new();
800 keys_load(request_keys, request);
802 response = login_new_response(request);
803 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
804 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
805 bhslr2->bhslr_tsih = htons(0xbadd);
806 login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
807 login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE);
808 response_keys = keys_new();
809 for (i = 0; i < KEYS_MAX; i++) {
810 if (request_keys->keys_names[i] == NULL)
813 login_negotiate_key(request, request_keys->keys_names[i],
814 request_keys->keys_values[i], skipped_security,
818 log_debugx("operational parameter negotiation done; "
819 "transitioning to Full Feature Phase");
821 keys_save(response_keys, response);
823 pdu_delete(response);
824 keys_delete(response_keys);
826 keys_delete(request_keys);
830 login(struct connection *conn)
832 struct pdu *request, *response;
833 struct iscsi_bhs_login_request *bhslr;
834 struct iscsi_bhs_login_response *bhslr2;
835 struct keys *request_keys, *response_keys;
836 struct auth_group *ag;
837 const char *initiator_name, *initiator_alias, *session_type,
838 *target_name, *auth_method;
839 char *portal_group_tag;
843 * Handle the initial Login Request - figure out required authentication
844 * method and either transition to the next phase, if no authentication
845 * is required, or call appropriate authentication code.
847 log_debugx("beginning Login Phase; waiting for Login PDU");
848 request = login_receive(conn, true);
849 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
850 if (bhslr->bhslr_tsih != 0) {
851 login_send_error(request, 0x02, 0x0a);
852 log_errx(1, "received Login PDU with non-zero TSIH");
855 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid,
856 sizeof(conn->conn_initiator_isid));
859 * XXX: Implement the C flag some day.
861 request_keys = keys_new();
862 keys_load(request_keys, request);
864 assert(conn->conn_initiator_name == NULL);
865 initiator_name = keys_find(request_keys, "InitiatorName");
866 if (initiator_name == NULL) {
867 login_send_error(request, 0x02, 0x07);
868 log_errx(1, "received Login PDU without InitiatorName");
870 if (valid_iscsi_name(initiator_name) == false) {
871 login_send_error(request, 0x02, 0x00);
872 log_errx(1, "received Login PDU with invalid InitiatorName");
874 conn->conn_initiator_name = checked_strdup(initiator_name);
875 log_set_peer_name(conn->conn_initiator_name);
877 * XXX: This doesn't work (does nothing) because of Capsicum.
879 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name);
881 initiator_alias = keys_find(request_keys, "InitiatorAlias");
882 if (initiator_alias != NULL)
883 conn->conn_initiator_alias = checked_strdup(initiator_alias);
885 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE);
886 session_type = keys_find(request_keys, "SessionType");
887 if (session_type != NULL) {
888 if (strcmp(session_type, "Normal") == 0) {
889 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
890 } else if (strcmp(session_type, "Discovery") == 0) {
891 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY;
893 login_send_error(request, 0x02, 0x00);
894 log_errx(1, "received Login PDU with invalid "
895 "SessionType \"%s\"", session_type);
898 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
900 assert(conn->conn_target == NULL);
901 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
902 target_name = keys_find(request_keys, "TargetName");
903 if (target_name == NULL) {
904 login_send_error(request, 0x02, 0x07);
905 log_errx(1, "received Login PDU without TargetName");
909 target_find(conn->conn_portal->p_portal_group->pg_conf,
911 if (conn->conn_target == NULL) {
912 login_send_error(request, 0x02, 0x03);
913 log_errx(1, "requested target \"%s\" not found",
919 * At this point we know what kind of authentication we need.
921 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
922 ag = conn->conn_target->t_auth_group;
923 if (ag->ag_name != NULL) {
924 log_debugx("initiator requests to connect "
925 "to target \"%s\"; auth-group \"%s\"",
926 conn->conn_target->t_name,
927 conn->conn_target->t_auth_group->ag_name);
929 log_debugx("initiator requests to connect "
930 "to target \"%s\"", conn->conn_target->t_name);
933 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
934 ag = conn->conn_portal->p_portal_group->pg_discovery_auth_group;
935 if (ag->ag_name != NULL) {
936 log_debugx("initiator requests "
937 "discovery session; auth-group \"%s\"", ag->ag_name);
939 log_debugx("initiator requests discovery session");
944 * Enforce initiator-name and initiator-portal.
946 if (auth_name_defined(ag)) {
947 if (auth_name_find(ag, initiator_name) == NULL) {
948 login_send_error(request, 0x02, 0x02);
949 log_errx(1, "initiator does not match allowed "
952 log_debugx("initiator matches allowed initiator names");
954 log_debugx("auth-group does not define initiator name "
958 if (auth_portal_defined(ag)) {
959 if (auth_portal_find(ag, &conn->conn_initiator_sa) == NULL) {
960 login_send_error(request, 0x02, 0x02);
961 log_errx(1, "initiator does not match allowed "
962 "initiator portals");
964 log_debugx("initiator matches allowed initiator portals");
966 log_debugx("auth-group does not define initiator portal "
971 * Let's see if the initiator intends to do any kind of authentication
974 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
975 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
976 login_send_error(request, 0x02, 0x01);
977 log_errx(1, "initiator skipped the authentication, "
978 "but authentication is required");
981 keys_delete(request_keys);
983 log_debugx("initiator skipped the authentication, "
984 "and we don't need it; proceeding with negotiation");
985 login_negotiate(conn, request);
989 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) {
991 * Initiator might want to to authenticate,
992 * but we don't need it.
994 log_debugx("authentication not required; "
995 "transitioning to operational parameter negotiation");
997 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0)
998 log_warnx("initiator did not set the \"T\" flag; "
999 "transitioning anyway");
1001 response = login_new_response(request);
1002 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
1003 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
1004 login_set_nsg(response,
1005 BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
1006 response_keys = keys_new();
1008 * Required by Linux initiator.
1010 auth_method = keys_find(request_keys, "AuthMethod");
1011 if (auth_method != NULL &&
1012 login_list_contains(auth_method, "None"))
1013 keys_add(response_keys, "AuthMethod", "None");
1015 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1016 if (conn->conn_target->t_alias != NULL)
1017 keys_add(response_keys,
1018 "TargetAlias", conn->conn_target->t_alias);
1019 rv = asprintf(&portal_group_tag, "%d",
1020 conn->conn_portal->p_portal_group->pg_tag);
1022 log_err(1, "asprintf");
1023 keys_add(response_keys,
1024 "TargetPortalGroupTag", portal_group_tag);
1025 free(portal_group_tag);
1027 keys_save(response_keys, response);
1029 pdu_delete(response);
1030 keys_delete(response_keys);
1031 pdu_delete(request);
1032 keys_delete(request_keys);
1034 login_negotiate(conn, NULL);
1038 if (ag->ag_type == AG_TYPE_DENY) {
1039 login_send_error(request, 0x02, 0x01);
1040 log_errx(1, "auth-type is \"deny\"");
1043 if (ag->ag_type == AG_TYPE_UNKNOWN) {
1045 * This can happen with empty auth-group.
1047 login_send_error(request, 0x02, 0x01);
1048 log_errx(1, "auth-type not set, denying access");
1051 log_debugx("CHAP authentication required");
1053 auth_method = keys_find(request_keys, "AuthMethod");
1054 if (auth_method == NULL) {
1055 login_send_error(request, 0x02, 0x07);
1056 log_errx(1, "received Login PDU without AuthMethod");
1059 * XXX: This should be Reject, not just a login failure (5.3.2).
1061 if (login_list_contains(auth_method, "CHAP") == 0) {
1062 login_send_error(request, 0x02, 0x01);
1063 log_errx(1, "initiator requests unsupported AuthMethod \"%s\" "
1064 "instead of \"CHAP\"", auth_method);
1067 response = login_new_response(request);
1069 response_keys = keys_new();
1070 keys_add(response_keys, "AuthMethod", "CHAP");
1071 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1072 rv = asprintf(&portal_group_tag, "%d",
1073 conn->conn_portal->p_portal_group->pg_tag);
1075 log_err(1, "asprintf");
1076 keys_add(response_keys,
1077 "TargetPortalGroupTag", portal_group_tag);
1078 free(portal_group_tag);
1079 if (conn->conn_target->t_alias != NULL)
1080 keys_add(response_keys,
1081 "TargetAlias", conn->conn_target->t_alias);
1083 keys_save(response_keys, response);
1086 pdu_delete(response);
1087 keys_delete(response_keys);
1088 pdu_delete(request);
1089 keys_delete(request_keys);
1091 login_chap(conn, ag);
1093 login_negotiate(conn, NULL);