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$");
39 #include <netinet/in.h>
42 #include "iscsi_proto.h"
44 static void login_send_error(struct pdu *request,
45 char class, char detail);
48 login_set_nsg(struct pdu *response, int nsg)
50 struct iscsi_bhs_login_response *bhslr;
52 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
53 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
54 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
56 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
58 bhslr->bhslr_flags &= 0xFC;
59 bhslr->bhslr_flags |= nsg;
63 login_csg(const struct pdu *request)
65 struct iscsi_bhs_login_request *bhslr;
67 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
69 return ((bhslr->bhslr_flags & 0x0C) >> 2);
73 login_set_csg(struct pdu *response, int csg)
75 struct iscsi_bhs_login_response *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_response *)response->pdu_bhs;
83 bhslr->bhslr_flags &= 0xF3;
84 bhslr->bhslr_flags |= csg << 2;
88 login_receive(struct connection *conn, bool initial)
91 struct iscsi_bhs_login_request *bhslr;
93 request = pdu_new(conn);
95 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
96 ISCSI_BHS_OPCODE_LOGIN_REQUEST) {
98 * The first PDU in session is special - if we receive any PDU
99 * different than login request, we have to drop the connection
100 * without sending response ("A target receiving any PDU
101 * except a Login request before the Login Phase is started MUST
102 * immediately terminate the connection on which the PDU
105 if (initial == false)
106 login_send_error(request, 0x02, 0x0b);
107 log_errx(1, "protocol error: received invalid opcode 0x%x",
108 request->pdu_bhs->bhs_opcode);
110 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
112 * XXX: Implement the C flag some day.
114 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0) {
115 login_send_error(request, 0x03, 0x00);
116 log_errx(1, "received Login PDU with unsupported \"C\" flag");
118 if (bhslr->bhslr_version_max != 0x00) {
119 login_send_error(request, 0x02, 0x05);
120 log_errx(1, "received Login PDU with unsupported "
121 "Version-max 0x%x", bhslr->bhslr_version_max);
123 if (bhslr->bhslr_version_min != 0x00) {
124 login_send_error(request, 0x02, 0x05);
125 log_errx(1, "received Login PDU with unsupported "
126 "Version-min 0x%x", bhslr->bhslr_version_min);
128 if (ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
129 login_send_error(request, 0x02, 0x05);
130 log_errx(1, "received Login PDU with decreasing CmdSN: "
131 "was %u, is %u", conn->conn_cmdsn,
132 ntohl(bhslr->bhslr_cmdsn));
134 if (initial == false &&
135 ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
136 login_send_error(request, 0x02, 0x05);
137 log_errx(1, "received Login PDU with wrong ExpStatSN: "
138 "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
141 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
147 login_new_response(struct pdu *request)
149 struct pdu *response;
150 struct connection *conn;
151 struct iscsi_bhs_login_request *bhslr;
152 struct iscsi_bhs_login_response *bhslr2;
154 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
155 conn = request->pdu_connection;
157 response = pdu_new_response(request);
158 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
159 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_RESPONSE;
160 login_set_csg(response, BHSLR_STAGE_SECURITY_NEGOTIATION);
161 memcpy(bhslr2->bhslr_isid,
162 bhslr->bhslr_isid, sizeof(bhslr2->bhslr_isid));
163 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
164 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
165 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
166 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
172 login_send_error(struct pdu *request, char class, char detail)
174 struct pdu *response;
175 struct iscsi_bhs_login_response *bhslr2;
177 log_debugx("sending Login Response PDU with failure class 0x%x/0x%x; "
178 "see next line for reason", class, detail);
179 response = login_new_response(request);
180 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
181 bhslr2->bhslr_status_class = class;
182 bhslr2->bhslr_status_detail = detail;
185 pdu_delete(response);
189 login_list_contains(const char *list, const char *what)
191 char *tofree, *str, *token;
193 tofree = str = checked_strdup(list);
195 while ((token = strsep(&str, ",")) != NULL) {
196 if (strcmp(token, what) == 0) {
206 login_list_prefers(const char *list,
207 const char *choice1, const char *choice2)
209 char *tofree, *str, *token;
211 tofree = str = checked_strdup(list);
213 while ((token = strsep(&str, ",")) != NULL) {
214 if (strcmp(token, choice1) == 0) {
218 if (strcmp(token, choice2) == 0) {
228 login_receive_chap_a(struct connection *conn)
231 struct keys *request_keys;
234 request = login_receive(conn, false);
235 request_keys = keys_new();
236 keys_load(request_keys, request);
238 chap_a = keys_find(request_keys, "CHAP_A");
239 if (chap_a == NULL) {
240 login_send_error(request, 0x02, 0x07);
241 log_errx(1, "received CHAP Login PDU without CHAP_A");
243 if (login_list_contains(chap_a, "5") == 0) {
244 login_send_error(request, 0x02, 0x01);
245 log_errx(1, "received CHAP Login PDU with unsupported CHAP_A "
248 keys_delete(request_keys);
254 login_send_chap_c(struct pdu *request, struct chap *chap)
256 struct pdu *response;
257 struct keys *response_keys;
258 char *chap_c, *chap_i;
260 chap_c = chap_get_challenge(chap);
261 chap_i = chap_get_id(chap);
263 response = login_new_response(request);
264 response_keys = keys_new();
265 keys_add(response_keys, "CHAP_A", "5");
266 keys_add(response_keys, "CHAP_I", chap_i);
267 keys_add(response_keys, "CHAP_C", chap_c);
270 keys_save(response_keys, response);
272 pdu_delete(response);
273 keys_delete(response_keys);
277 login_receive_chap_r(struct connection *conn, struct auth_group *ag,
278 struct chap *chap, const struct auth **authp)
281 struct keys *request_keys;
282 const char *chap_n, *chap_r;
283 const struct auth *auth;
286 request = login_receive(conn, false);
287 request_keys = keys_new();
288 keys_load(request_keys, request);
290 chap_n = keys_find(request_keys, "CHAP_N");
291 if (chap_n == NULL) {
292 login_send_error(request, 0x02, 0x07);
293 log_errx(1, "received CHAP Login PDU without CHAP_N");
295 chap_r = keys_find(request_keys, "CHAP_R");
296 if (chap_r == NULL) {
297 login_send_error(request, 0x02, 0x07);
298 log_errx(1, "received CHAP Login PDU without CHAP_R");
300 error = chap_receive(chap, chap_r);
302 login_send_error(request, 0x02, 0x07);
303 log_errx(1, "received CHAP Login PDU with malformed CHAP_R");
307 * Verify the response.
309 assert(ag->ag_type == AG_TYPE_CHAP ||
310 ag->ag_type == AG_TYPE_CHAP_MUTUAL);
311 auth = auth_find(ag, chap_n);
313 login_send_error(request, 0x02, 0x01);
314 log_errx(1, "received CHAP Login with invalid user \"%s\"",
318 assert(auth->a_secret != NULL);
319 assert(strlen(auth->a_secret) > 0);
321 error = chap_authenticate(chap, auth->a_secret);
323 login_send_error(request, 0x02, 0x01);
324 log_errx(1, "CHAP authentication failed for user \"%s\"",
328 keys_delete(request_keys);
335 login_send_chap_success(struct pdu *request,
336 const struct auth *auth)
338 struct pdu *response;
339 struct keys *request_keys, *response_keys;
340 struct iscsi_bhs_login_response *bhslr2;
342 const char *chap_i, *chap_c;
346 response = login_new_response(request);
347 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
348 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
349 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
352 * Actually, one more thing: mutual authentication.
354 request_keys = keys_new();
355 keys_load(request_keys, request);
356 chap_i = keys_find(request_keys, "CHAP_I");
357 chap_c = keys_find(request_keys, "CHAP_C");
358 if (chap_i != NULL || chap_c != NULL) {
359 if (chap_i == NULL) {
360 login_send_error(request, 0x02, 0x07);
361 log_errx(1, "initiator requested target "
362 "authentication, but didn't send CHAP_I");
364 if (chap_c == NULL) {
365 login_send_error(request, 0x02, 0x07);
366 log_errx(1, "initiator requested target "
367 "authentication, but didn't send CHAP_C");
369 if (auth->a_auth_group->ag_type != AG_TYPE_CHAP_MUTUAL) {
370 login_send_error(request, 0x02, 0x01);
371 log_errx(1, "initiator requests target authentication "
372 "for user \"%s\", but mutual user/secret "
373 "is not set", auth->a_user);
376 log_debugx("performing mutual authentication as user \"%s\"",
377 auth->a_mutual_user);
379 rchap = rchap_new(auth->a_mutual_secret);
380 error = rchap_receive(rchap, chap_i, chap_c);
382 login_send_error(request, 0x02, 0x07);
383 log_errx(1, "received CHAP Login PDU with malformed "
386 chap_r = rchap_get_response(rchap);
388 response_keys = keys_new();
389 keys_add(response_keys, "CHAP_N", auth->a_mutual_user);
390 keys_add(response_keys, "CHAP_R", chap_r);
392 keys_save(response_keys, response);
393 keys_delete(response_keys);
395 log_debugx("initiator did not request target authentication");
398 keys_delete(request_keys);
400 pdu_delete(response);
404 login_chap(struct connection *conn, struct auth_group *ag)
406 const struct auth *auth;
411 * Receive CHAP_A PDU.
413 log_debugx("beginning CHAP authentication; waiting for CHAP_A");
414 request = login_receive_chap_a(conn);
417 * Generate the challenge.
422 * Send the challenge.
424 log_debugx("sending CHAP_C, binary challenge size is %zd bytes",
425 sizeof(chap->chap_challenge));
426 login_send_chap_c(request, chap);
430 * Receive CHAP_N/CHAP_R PDU and authenticate.
432 log_debugx("waiting for CHAP_N/CHAP_R");
433 request = login_receive_chap_r(conn, ag, chap, &auth);
436 * Yay, authentication succeeded!
438 log_debugx("authentication succeeded for user \"%s\"; "
439 "transitioning to Negotiation Phase", auth->a_user);
440 login_send_chap_success(request, auth);
444 * Leave username and CHAP information for discovery().
446 conn->conn_user = auth->a_user;
447 conn->conn_chap = chap;
451 login_negotiate_key(struct pdu *request, const char *name,
452 const char *value, bool skipped_security, struct keys *response_keys)
456 struct connection *conn;
458 conn = request->pdu_connection;
460 if (strcmp(name, "InitiatorName") == 0) {
461 if (!skipped_security)
462 log_errx(1, "initiator resent InitiatorName");
463 } else if (strcmp(name, "SessionType") == 0) {
464 if (!skipped_security)
465 log_errx(1, "initiator resent SessionType");
466 } else if (strcmp(name, "TargetName") == 0) {
467 if (!skipped_security)
468 log_errx(1, "initiator resent TargetName");
469 } else if (strcmp(name, "InitiatorAlias") == 0) {
470 if (conn->conn_initiator_alias != NULL)
471 free(conn->conn_initiator_alias);
472 conn->conn_initiator_alias = checked_strdup(value);
473 } else if (strcmp(value, "Irrelevant") == 0) {
475 } else if (strcmp(name, "HeaderDigest") == 0) {
477 * We don't handle digests for discovery sessions.
479 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
480 log_debugx("discovery session; digests disabled");
481 keys_add(response_keys, name, "None");
485 which = login_list_prefers(value, "CRC32C", "None");
488 log_debugx("initiator prefers CRC32C "
489 "for header digest; we'll use it");
490 conn->conn_header_digest = CONN_DIGEST_CRC32C;
491 keys_add(response_keys, name, "CRC32C");
494 log_debugx("initiator prefers not to do "
495 "header digest; we'll comply");
496 keys_add(response_keys, name, "None");
499 log_warnx("initiator sent unrecognized "
500 "HeaderDigest value \"%s\"; will use None", value);
501 keys_add(response_keys, name, "None");
504 } else if (strcmp(name, "DataDigest") == 0) {
505 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
506 log_debugx("discovery session; digests disabled");
507 keys_add(response_keys, name, "None");
511 which = login_list_prefers(value, "CRC32C", "None");
514 log_debugx("initiator prefers CRC32C "
515 "for data digest; we'll use it");
516 conn->conn_data_digest = CONN_DIGEST_CRC32C;
517 keys_add(response_keys, name, "CRC32C");
520 log_debugx("initiator prefers not to do "
521 "data digest; we'll comply");
522 keys_add(response_keys, name, "None");
525 log_warnx("initiator sent unrecognized "
526 "DataDigest value \"%s\"; will use None", value);
527 keys_add(response_keys, name, "None");
530 } else if (strcmp(name, "MaxConnections") == 0) {
531 keys_add(response_keys, name, "1");
532 } else if (strcmp(name, "InitialR2T") == 0) {
533 keys_add(response_keys, name, "Yes");
534 } else if (strcmp(name, "ImmediateData") == 0) {
535 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
536 log_debugx("discovery session; ImmediateData irrelevant");
537 keys_add(response_keys, name, "Irrelevant");
539 if (strcmp(value, "Yes") == 0) {
540 conn->conn_immediate_data = true;
541 keys_add(response_keys, name, "Yes");
543 conn->conn_immediate_data = false;
544 keys_add(response_keys, name, "No");
547 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
548 tmp = strtoul(value, NULL, 10);
550 login_send_error(request, 0x02, 0x00);
551 log_errx(1, "received invalid "
552 "MaxRecvDataSegmentLength");
554 if (tmp > conn->conn_data_segment_limit) {
555 log_debugx("capping MaxRecvDataSegmentLength "
556 "from %zd to %zd", tmp, conn->conn_data_segment_limit);
557 tmp = conn->conn_data_segment_limit;
559 conn->conn_max_data_segment_length = tmp;
560 keys_add_int(response_keys, name, tmp);
561 } else if (strcmp(name, "MaxBurstLength") == 0) {
562 tmp = strtoul(value, NULL, 10);
564 login_send_error(request, 0x02, 0x00);
565 log_errx(1, "received invalid MaxBurstLength");
567 if (tmp > MAX_BURST_LENGTH) {
568 log_debugx("capping MaxBurstLength from %zd to %d",
569 tmp, MAX_BURST_LENGTH);
570 tmp = MAX_BURST_LENGTH;
572 conn->conn_max_burst_length = tmp;
573 keys_add(response_keys, name, value);
574 } else if (strcmp(name, "FirstBurstLength") == 0) {
575 tmp = strtoul(value, NULL, 10);
577 login_send_error(request, 0x02, 0x00);
578 log_errx(1, "received invalid "
581 if (tmp > conn->conn_data_segment_limit) {
582 log_debugx("capping FirstBurstLength from %zd to %zd",
583 tmp, conn->conn_data_segment_limit);
584 tmp = conn->conn_data_segment_limit;
587 * We don't pass the value to the kernel; it only enforces
588 * hardcoded limit anyway.
590 keys_add_int(response_keys, name, tmp);
591 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
592 keys_add(response_keys, name, value);
593 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
594 keys_add(response_keys, name, "0");
595 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
596 keys_add(response_keys, name, "1");
597 } else if (strcmp(name, "DataPDUInOrder") == 0) {
598 keys_add(response_keys, name, "Yes");
599 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
600 keys_add(response_keys, name, "Yes");
601 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
602 keys_add(response_keys, name, "0");
603 } else if (strcmp(name, "OFMarker") == 0) {
604 keys_add(response_keys, name, "No");
605 } else if (strcmp(name, "IFMarker") == 0) {
606 keys_add(response_keys, name, "No");
608 log_debugx("unknown key \"%s\"; responding "
609 "with NotUnderstood", name);
610 keys_add(response_keys, name, "NotUnderstood");
615 login_redirect(struct pdu *request, const char *target_address)
617 struct pdu *response;
618 struct iscsi_bhs_login_response *bhslr2;
619 struct keys *response_keys;
621 response = login_new_response(request);
622 login_set_csg(response, login_csg(request));
623 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
624 bhslr2->bhslr_status_class = 0x01;
625 bhslr2->bhslr_status_detail = 0x01;
627 response_keys = keys_new();
628 keys_add(response_keys, "TargetAddress", target_address);
630 keys_save(response_keys, response);
632 pdu_delete(response);
633 keys_delete(response_keys);
637 login_portal_redirect(struct connection *conn, struct pdu *request)
639 const struct portal_group *pg;
641 pg = conn->conn_portal->p_portal_group;
642 if (pg->pg_redirection == NULL)
645 log_debugx("portal-group \"%s\" configured to redirect to %s",
646 pg->pg_name, pg->pg_redirection);
647 login_redirect(request, pg->pg_redirection);
653 login_target_redirect(struct connection *conn, struct pdu *request)
655 const char *target_address;
657 assert(conn->conn_portal->p_portal_group->pg_redirection == NULL);
659 if (conn->conn_target == NULL)
662 target_address = conn->conn_target->t_redirection;
663 if (target_address == NULL)
666 log_debugx("target \"%s\" configured to redirect to %s",
667 conn->conn_target->t_name, target_address);
668 login_redirect(request, target_address);
674 login_negotiate(struct connection *conn, struct pdu *request)
676 struct pdu *response;
677 struct iscsi_bhs_login_response *bhslr2;
678 struct keys *request_keys, *response_keys;
680 bool redirected, skipped_security;
682 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
684 * Query the kernel for MaxDataSegmentLength it can handle.
685 * In case of offload, it depends on hardware capabilities.
687 assert(conn->conn_target != NULL);
688 kernel_limits(conn->conn_portal->p_portal_group->pg_offload,
689 &conn->conn_data_segment_limit);
691 conn->conn_data_segment_limit = MAX_DATA_SEGMENT_LENGTH;
694 if (request == NULL) {
695 log_debugx("beginning operational parameter negotiation; "
696 "waiting for Login PDU");
697 request = login_receive(conn, false);
698 skipped_security = false;
700 skipped_security = true;
703 * RFC 3720, 10.13.5. Status-Class and Status-Detail, says
704 * the redirection SHOULD be accepted by the initiator before
705 * authentication, but MUST be be accepted afterwards; that's
706 * why we're doing it here and not earlier.
708 redirected = login_target_redirect(conn, request);
710 log_debugx("initiator redirected; exiting");
714 request_keys = keys_new();
715 keys_load(request_keys, request);
717 response = login_new_response(request);
718 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
719 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
720 bhslr2->bhslr_tsih = htons(0xbadd);
721 login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
722 login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE);
723 response_keys = keys_new();
725 if (skipped_security &&
726 conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
727 if (conn->conn_target->t_alias != NULL)
728 keys_add(response_keys,
729 "TargetAlias", conn->conn_target->t_alias);
730 keys_add_int(response_keys, "TargetPortalGroupTag",
731 conn->conn_portal->p_portal_group->pg_tag);
734 for (i = 0; i < KEYS_MAX; i++) {
735 if (request_keys->keys_names[i] == NULL)
738 login_negotiate_key(request, request_keys->keys_names[i],
739 request_keys->keys_values[i], skipped_security,
743 log_debugx("operational parameter negotiation done; "
744 "transitioning to Full Feature Phase");
746 keys_save(response_keys, response);
748 pdu_delete(response);
749 keys_delete(response_keys);
751 keys_delete(request_keys);
755 login(struct connection *conn)
757 struct pdu *request, *response;
758 struct iscsi_bhs_login_request *bhslr;
759 struct iscsi_bhs_login_response *bhslr2;
760 struct keys *request_keys, *response_keys;
761 struct auth_group *ag;
762 struct portal_group *pg;
763 const char *initiator_name, *initiator_alias, *session_type,
764 *target_name, *auth_method;
768 * Handle the initial Login Request - figure out required authentication
769 * method and either transition to the next phase, if no authentication
770 * is required, or call appropriate authentication code.
772 log_debugx("beginning Login Phase; waiting for Login PDU");
773 request = login_receive(conn, true);
774 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
775 if (bhslr->bhslr_tsih != 0) {
776 login_send_error(request, 0x02, 0x0a);
777 log_errx(1, "received Login PDU with non-zero TSIH");
780 pg = conn->conn_portal->p_portal_group;
782 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid,
783 sizeof(conn->conn_initiator_isid));
786 * XXX: Implement the C flag some day.
788 request_keys = keys_new();
789 keys_load(request_keys, request);
791 assert(conn->conn_initiator_name == NULL);
792 initiator_name = keys_find(request_keys, "InitiatorName");
793 if (initiator_name == NULL) {
794 login_send_error(request, 0x02, 0x07);
795 log_errx(1, "received Login PDU without InitiatorName");
797 if (valid_iscsi_name(initiator_name) == false) {
798 login_send_error(request, 0x02, 0x00);
799 log_errx(1, "received Login PDU with invalid InitiatorName");
801 conn->conn_initiator_name = checked_strdup(initiator_name);
802 log_set_peer_name(conn->conn_initiator_name);
804 * XXX: This doesn't work (does nothing) because of Capsicum.
806 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name);
808 redirected = login_portal_redirect(conn, request);
810 log_debugx("initiator redirected; exiting");
814 initiator_alias = keys_find(request_keys, "InitiatorAlias");
815 if (initiator_alias != NULL)
816 conn->conn_initiator_alias = checked_strdup(initiator_alias);
818 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE);
819 session_type = keys_find(request_keys, "SessionType");
820 if (session_type != NULL) {
821 if (strcmp(session_type, "Normal") == 0) {
822 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
823 } else if (strcmp(session_type, "Discovery") == 0) {
824 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY;
826 login_send_error(request, 0x02, 0x00);
827 log_errx(1, "received Login PDU with invalid "
828 "SessionType \"%s\"", session_type);
831 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
833 assert(conn->conn_target == NULL);
834 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
835 target_name = keys_find(request_keys, "TargetName");
836 if (target_name == NULL) {
837 login_send_error(request, 0x02, 0x07);
838 log_errx(1, "received Login PDU without TargetName");
841 conn->conn_port = port_find_in_pg(pg, target_name);
842 if (conn->conn_port == NULL) {
843 login_send_error(request, 0x02, 0x03);
844 log_errx(1, "requested target \"%s\" not found",
847 conn->conn_target = conn->conn_port->p_target;
851 * At this point we know what kind of authentication we need.
853 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
854 ag = conn->conn_port->p_auth_group;
856 ag = conn->conn_target->t_auth_group;
857 if (ag->ag_name != NULL) {
858 log_debugx("initiator requests to connect "
859 "to target \"%s\"; auth-group \"%s\"",
860 conn->conn_target->t_name,
863 log_debugx("initiator requests to connect "
864 "to target \"%s\"", conn->conn_target->t_name);
867 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
868 ag = pg->pg_discovery_auth_group;
869 if (ag->ag_name != NULL) {
870 log_debugx("initiator requests "
871 "discovery session; auth-group \"%s\"", ag->ag_name);
873 log_debugx("initiator requests discovery session");
878 * Enforce initiator-name and initiator-portal.
880 if (auth_name_check(ag, initiator_name) != 0) {
881 login_send_error(request, 0x02, 0x02);
882 log_errx(1, "initiator does not match allowed initiator names");
885 if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
886 login_send_error(request, 0x02, 0x02);
887 log_errx(1, "initiator does not match allowed "
888 "initiator portals");
892 * Let's see if the initiator intends to do any kind of authentication
895 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
896 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
897 login_send_error(request, 0x02, 0x01);
898 log_errx(1, "initiator skipped the authentication, "
899 "but authentication is required");
902 keys_delete(request_keys);
904 log_debugx("initiator skipped the authentication, "
905 "and we don't need it; proceeding with negotiation");
906 login_negotiate(conn, request);
910 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) {
912 * Initiator might want to to authenticate,
913 * but we don't need it.
915 log_debugx("authentication not required; "
916 "transitioning to operational parameter negotiation");
918 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0)
919 log_warnx("initiator did not set the \"T\" flag; "
920 "transitioning anyway");
922 response = login_new_response(request);
923 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
924 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
925 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
926 response_keys = keys_new();
928 * Required by Linux initiator.
930 auth_method = keys_find(request_keys, "AuthMethod");
931 if (auth_method != NULL &&
932 login_list_contains(auth_method, "None"))
933 keys_add(response_keys, "AuthMethod", "None");
935 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
936 if (conn->conn_target->t_alias != NULL)
937 keys_add(response_keys,
938 "TargetAlias", conn->conn_target->t_alias);
939 keys_add_int(response_keys,
940 "TargetPortalGroupTag", pg->pg_tag);
942 keys_save(response_keys, response);
944 pdu_delete(response);
945 keys_delete(response_keys);
947 keys_delete(request_keys);
949 login_negotiate(conn, NULL);
953 if (ag->ag_type == AG_TYPE_DENY) {
954 login_send_error(request, 0x02, 0x01);
955 log_errx(1, "auth-type is \"deny\"");
958 if (ag->ag_type == AG_TYPE_UNKNOWN) {
960 * This can happen with empty auth-group.
962 login_send_error(request, 0x02, 0x01);
963 log_errx(1, "auth-type not set, denying access");
966 log_debugx("CHAP authentication required");
968 auth_method = keys_find(request_keys, "AuthMethod");
969 if (auth_method == NULL) {
970 login_send_error(request, 0x02, 0x07);
971 log_errx(1, "received Login PDU without AuthMethod");
974 * XXX: This should be Reject, not just a login failure (5.3.2).
976 if (login_list_contains(auth_method, "CHAP") == 0) {
977 login_send_error(request, 0x02, 0x01);
978 log_errx(1, "initiator requests unsupported AuthMethod \"%s\" "
979 "instead of \"CHAP\"", auth_method);
982 response = login_new_response(request);
984 response_keys = keys_new();
985 keys_add(response_keys, "AuthMethod", "CHAP");
986 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
987 if (conn->conn_target->t_alias != NULL)
988 keys_add(response_keys,
989 "TargetAlias", conn->conn_target->t_alias);
990 keys_add_int(response_keys,
991 "TargetPortalGroupTag", pg->pg_tag);
993 keys_save(response_keys, response);
996 pdu_delete(response);
997 keys_delete(response_keys);
999 keys_delete(request_keys);
1001 login_chap(conn, ag);
1003 login_negotiate(conn, NULL);