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>
44 #include "iscsi_proto.h"
46 static void login_send_error(struct pdu *request,
47 char class, char detail);
50 login_set_nsg(struct pdu *response, int nsg)
52 struct iscsi_bhs_login_response *bhslr;
54 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
55 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
56 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
58 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
60 bhslr->bhslr_flags &= 0xFC;
61 bhslr->bhslr_flags |= nsg;
62 bhslr->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
66 login_csg(const struct pdu *request)
68 struct iscsi_bhs_login_request *bhslr;
70 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
72 return ((bhslr->bhslr_flags & 0x0C) >> 2);
76 login_set_csg(struct pdu *response, int csg)
78 struct iscsi_bhs_login_response *bhslr;
80 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
81 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
82 csg == BHSLR_STAGE_FULL_FEATURE_PHASE);
84 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
86 bhslr->bhslr_flags &= 0xF3;
87 bhslr->bhslr_flags |= csg << 2;
91 login_receive(struct connection *conn, bool initial)
94 struct iscsi_bhs_login_request *bhslr;
96 request = pdu_new(conn);
98 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
99 ISCSI_BHS_OPCODE_LOGIN_REQUEST) {
101 * The first PDU in session is special - if we receive any PDU
102 * different than login request, we have to drop the connection
103 * without sending response ("A target receiving any PDU
104 * except a Login request before the Login Phase is started MUST
105 * immediately terminate the connection on which the PDU
108 if (initial == false)
109 login_send_error(request, 0x02, 0x0b);
110 log_errx(1, "protocol error: received invalid opcode 0x%x",
111 request->pdu_bhs->bhs_opcode);
113 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
115 * XXX: Implement the C flag some day.
117 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0) {
118 login_send_error(request, 0x03, 0x00);
119 log_errx(1, "received Login PDU with unsupported \"C\" flag");
121 if (bhslr->bhslr_version_max != 0x00) {
122 login_send_error(request, 0x02, 0x05);
123 log_errx(1, "received Login PDU with unsupported "
124 "Version-max 0x%x", bhslr->bhslr_version_max);
126 if (bhslr->bhslr_version_min != 0x00) {
127 login_send_error(request, 0x02, 0x05);
128 log_errx(1, "received Login PDU with unsupported "
129 "Version-min 0x%x", bhslr->bhslr_version_min);
131 if (ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
132 login_send_error(request, 0x02, 0x05);
133 log_errx(1, "received Login PDU with decreasing CmdSN: "
134 "was %u, is %u", conn->conn_cmdsn,
135 ntohl(bhslr->bhslr_cmdsn));
137 if (initial == false &&
138 ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
139 login_send_error(request, 0x02, 0x05);
140 log_errx(1, "received Login PDU with wrong ExpStatSN: "
141 "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
144 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
150 login_new_response(struct pdu *request)
152 struct pdu *response;
153 struct connection *conn;
154 struct iscsi_bhs_login_request *bhslr;
155 struct iscsi_bhs_login_response *bhslr2;
157 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
158 conn = request->pdu_connection;
160 response = pdu_new_response(request);
161 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
162 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_RESPONSE;
163 login_set_csg(response, BHSLR_STAGE_SECURITY_NEGOTIATION);
164 memcpy(bhslr2->bhslr_isid,
165 bhslr->bhslr_isid, sizeof(bhslr2->bhslr_isid));
166 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
167 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
168 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
169 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
175 login_send_error(struct pdu *request, char class, char detail)
177 struct pdu *response;
178 struct iscsi_bhs_login_response *bhslr2;
180 log_debugx("sending Login Response PDU with failure class 0x%x/0x%x; "
181 "see next line for reason", class, detail);
182 response = login_new_response(request);
183 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
184 bhslr2->bhslr_status_class = class;
185 bhslr2->bhslr_status_detail = detail;
188 pdu_delete(response);
192 login_list_contains(const char *list, const char *what)
194 char *tofree, *str, *token;
196 tofree = str = checked_strdup(list);
198 while ((token = strsep(&str, ",")) != NULL) {
199 if (strcmp(token, what) == 0) {
209 login_list_prefers(const char *list,
210 const char *choice1, const char *choice2)
212 char *tofree, *str, *token;
214 tofree = str = checked_strdup(list);
216 while ((token = strsep(&str, ",")) != NULL) {
217 if (strcmp(token, choice1) == 0) {
221 if (strcmp(token, choice2) == 0) {
231 login_receive_chap_a(struct connection *conn)
234 struct keys *request_keys;
237 request = login_receive(conn, false);
238 request_keys = keys_new();
239 keys_load(request_keys, request);
241 chap_a = keys_find(request_keys, "CHAP_A");
242 if (chap_a == NULL) {
243 login_send_error(request, 0x02, 0x07);
244 log_errx(1, "received CHAP Login PDU without CHAP_A");
246 if (login_list_contains(chap_a, "5") == 0) {
247 login_send_error(request, 0x02, 0x01);
248 log_errx(1, "received CHAP Login PDU with unsupported CHAP_A "
251 keys_delete(request_keys);
257 login_send_chap_c(struct pdu *request, struct chap *chap)
259 struct pdu *response;
260 struct keys *response_keys;
261 char *chap_c, *chap_i;
263 chap_c = chap_get_challenge(chap);
264 chap_i = chap_get_id(chap);
266 response = login_new_response(request);
267 response_keys = keys_new();
268 keys_add(response_keys, "CHAP_A", "5");
269 keys_add(response_keys, "CHAP_I", chap_i);
270 keys_add(response_keys, "CHAP_C", chap_c);
273 keys_save(response_keys, response);
275 pdu_delete(response);
276 keys_delete(response_keys);
280 login_receive_chap_r(struct connection *conn, struct auth_group *ag,
281 struct chap *chap, const struct auth **authp)
284 struct keys *request_keys;
285 const char *chap_n, *chap_r;
286 const struct auth *auth;
289 request = login_receive(conn, false);
290 request_keys = keys_new();
291 keys_load(request_keys, request);
293 chap_n = keys_find(request_keys, "CHAP_N");
294 if (chap_n == NULL) {
295 login_send_error(request, 0x02, 0x07);
296 log_errx(1, "received CHAP Login PDU without CHAP_N");
298 chap_r = keys_find(request_keys, "CHAP_R");
299 if (chap_r == NULL) {
300 login_send_error(request, 0x02, 0x07);
301 log_errx(1, "received CHAP Login PDU without CHAP_R");
303 error = chap_receive(chap, chap_r);
305 login_send_error(request, 0x02, 0x07);
306 log_errx(1, "received CHAP Login PDU with malformed CHAP_R");
310 * Verify the response.
312 assert(ag->ag_type == AG_TYPE_CHAP ||
313 ag->ag_type == AG_TYPE_CHAP_MUTUAL);
314 auth = auth_find(ag, chap_n);
316 login_send_error(request, 0x02, 0x01);
317 log_errx(1, "received CHAP Login with invalid user \"%s\"",
321 assert(auth->a_secret != NULL);
322 assert(strlen(auth->a_secret) > 0);
324 error = chap_authenticate(chap, auth->a_secret);
326 login_send_error(request, 0x02, 0x01);
327 log_errx(1, "CHAP authentication failed for user \"%s\"",
331 keys_delete(request_keys);
338 login_send_chap_success(struct pdu *request,
339 const struct auth *auth)
341 struct pdu *response;
342 struct keys *request_keys, *response_keys;
344 const char *chap_i, *chap_c;
348 response = login_new_response(request);
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)
455 struct connection *conn;
457 conn = request->pdu_connection;
459 if (strcmp(name, "InitiatorName") == 0) {
460 if (!skipped_security)
461 log_errx(1, "initiator resent InitiatorName");
462 } else if (strcmp(name, "SessionType") == 0) {
463 if (!skipped_security)
464 log_errx(1, "initiator resent SessionType");
465 } else if (strcmp(name, "TargetName") == 0) {
466 if (!skipped_security)
467 log_errx(1, "initiator resent TargetName");
468 } else if (strcmp(name, "InitiatorAlias") == 0) {
469 if (conn->conn_initiator_alias != NULL)
470 free(conn->conn_initiator_alias);
471 conn->conn_initiator_alias = checked_strdup(value);
472 } else if (strcmp(value, "Irrelevant") == 0) {
474 } else if (strcmp(name, "HeaderDigest") == 0) {
476 * We don't handle digests for discovery sessions.
478 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
479 log_debugx("discovery session; digests disabled");
480 keys_add(response_keys, name, "None");
484 which = login_list_prefers(value, "CRC32C", "None");
487 log_debugx("initiator prefers CRC32C "
488 "for header digest; we'll use it");
489 conn->conn_header_digest = CONN_DIGEST_CRC32C;
490 keys_add(response_keys, name, "CRC32C");
493 log_debugx("initiator prefers not to do "
494 "header digest; we'll comply");
495 keys_add(response_keys, name, "None");
498 log_warnx("initiator sent unrecognized "
499 "HeaderDigest value \"%s\"; will use None", value);
500 keys_add(response_keys, name, "None");
503 } else if (strcmp(name, "DataDigest") == 0) {
504 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
505 log_debugx("discovery session; digests disabled");
506 keys_add(response_keys, name, "None");
510 which = login_list_prefers(value, "CRC32C", "None");
513 log_debugx("initiator prefers CRC32C "
514 "for data digest; we'll use it");
515 conn->conn_data_digest = CONN_DIGEST_CRC32C;
516 keys_add(response_keys, name, "CRC32C");
519 log_debugx("initiator prefers not to do "
520 "data digest; we'll comply");
521 keys_add(response_keys, name, "None");
524 log_warnx("initiator sent unrecognized "
525 "DataDigest value \"%s\"; will use None", value);
526 keys_add(response_keys, name, "None");
529 } else if (strcmp(name, "MaxConnections") == 0) {
530 keys_add(response_keys, name, "1");
531 } else if (strcmp(name, "InitialR2T") == 0) {
532 keys_add(response_keys, name, "Yes");
533 } else if (strcmp(name, "ImmediateData") == 0) {
534 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
535 log_debugx("discovery session; ImmediateData irrelevant");
536 keys_add(response_keys, name, "Irrelevant");
538 if (strcmp(value, "Yes") == 0) {
539 conn->conn_immediate_data = true;
540 keys_add(response_keys, name, "Yes");
542 conn->conn_immediate_data = false;
543 keys_add(response_keys, name, "No");
546 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
547 tmp = strtoul(value, NULL, 10);
549 login_send_error(request, 0x02, 0x00);
550 log_errx(1, "received invalid "
551 "MaxRecvDataSegmentLength");
553 if (tmp > MAX_DATA_SEGMENT_LENGTH) {
554 log_debugx("capping MaxRecvDataSegmentLength "
555 "from %d to %d", tmp, MAX_DATA_SEGMENT_LENGTH);
556 tmp = MAX_DATA_SEGMENT_LENGTH;
558 conn->conn_max_data_segment_length = tmp;
559 keys_add_int(response_keys, name, MAX_DATA_SEGMENT_LENGTH);
560 } else if (strcmp(name, "MaxBurstLength") == 0) {
561 tmp = strtoul(value, NULL, 10);
563 login_send_error(request, 0x02, 0x00);
564 log_errx(1, "received invalid MaxBurstLength");
566 if (tmp > MAX_BURST_LENGTH) {
567 log_debugx("capping MaxBurstLength from %d to %d",
568 tmp, MAX_BURST_LENGTH);
569 tmp = MAX_BURST_LENGTH;
571 conn->conn_max_burst_length = tmp;
572 keys_add(response_keys, name, value);
573 } else if (strcmp(name, "FirstBurstLength") == 0) {
574 tmp = strtoul(value, NULL, 10);
576 login_send_error(request, 0x02, 0x00);
577 log_errx(1, "received invalid "
580 if (tmp > MAX_DATA_SEGMENT_LENGTH) {
581 log_debugx("capping FirstBurstLength from %d to %d",
582 tmp, MAX_DATA_SEGMENT_LENGTH);
583 tmp = MAX_DATA_SEGMENT_LENGTH;
586 * We don't pass the value to the kernel; it only enforces
587 * hardcoded limit anyway.
589 keys_add_int(response_keys, name, tmp);
590 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
591 keys_add(response_keys, name, value);
592 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
593 keys_add(response_keys, name, "0");
594 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
595 keys_add(response_keys, name, "1");
596 } else if (strcmp(name, "DataPDUInOrder") == 0) {
597 keys_add(response_keys, name, "Yes");
598 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
599 keys_add(response_keys, name, "Yes");
600 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
601 keys_add(response_keys, name, "0");
602 } else if (strcmp(name, "OFMarker") == 0) {
603 keys_add(response_keys, name, "No");
604 } else if (strcmp(name, "IFMarker") == 0) {
605 keys_add(response_keys, name, "No");
607 log_debugx("unknown key \"%s\"; responding "
608 "with NotUnderstood", name);
609 keys_add(response_keys, name, "NotUnderstood");
614 login_redirect(struct pdu *request, const char *target_address)
616 struct pdu *response;
617 struct iscsi_bhs_login_response *bhslr2;
618 struct keys *response_keys;
620 response = login_new_response(request);
621 login_set_csg(response, login_csg(request));
622 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
623 bhslr2->bhslr_status_class = 0x01;
624 bhslr2->bhslr_status_detail = 0x01;
626 response_keys = keys_new();
627 keys_add(response_keys, "TargetAddress", target_address);
629 keys_save(response_keys, response);
631 pdu_delete(response);
632 keys_delete(response_keys);
636 login_portal_redirect(struct connection *conn, struct pdu *request)
638 const struct portal_group *pg;
640 pg = conn->conn_portal->p_portal_group;
641 if (pg->pg_redirection == NULL)
644 log_debugx("portal-group \"%s\" configured to redirect to %s",
645 pg->pg_name, pg->pg_redirection);
646 login_redirect(request, pg->pg_redirection);
652 login_target_redirect(struct connection *conn, struct pdu *request)
654 const char *target_address;
656 assert(conn->conn_portal->p_portal_group->pg_redirection == NULL);
658 if (conn->conn_target == NULL)
661 target_address = conn->conn_target->t_redirection;
662 if (target_address == NULL)
665 log_debugx("target \"%s\" configured to redirect to %s",
666 conn->conn_target->t_name, target_address);
667 login_redirect(request, target_address);
673 login_negotiate(struct connection *conn, struct pdu *request)
675 struct pdu *response;
676 struct iscsi_bhs_login_response *bhslr2;
677 struct keys *request_keys, *response_keys;
679 bool redirected, skipped_security;
681 if (request == NULL) {
682 log_debugx("beginning operational parameter negotiation; "
683 "waiting for Login PDU");
684 request = login_receive(conn, false);
685 skipped_security = false;
687 skipped_security = true;
690 * RFC 3720, 10.13.5. Status-Class and Status-Detail, says
691 * the redirection SHOULD be accepted by the initiator before
692 * authentication, but MUST be be accepted afterwards; that's
693 * why we're doing it here and not earlier.
695 redirected = login_target_redirect(conn, request);
697 log_debugx("initiator redirected; exiting");
701 request_keys = keys_new();
702 keys_load(request_keys, request);
704 response = login_new_response(request);
705 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
706 bhslr2->bhslr_tsih = htons(0xbadd);
707 login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
708 login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE);
709 response_keys = keys_new();
711 if (skipped_security &&
712 conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
713 if (conn->conn_target->t_alias != NULL)
714 keys_add(response_keys,
715 "TargetAlias", conn->conn_target->t_alias);
716 keys_add_int(response_keys, "TargetPortalGroupTag",
717 conn->conn_portal->p_portal_group->pg_tag);
720 for (i = 0; i < KEYS_MAX; i++) {
721 if (request_keys->keys_names[i] == NULL)
724 login_negotiate_key(request, request_keys->keys_names[i],
725 request_keys->keys_values[i], skipped_security,
729 log_debugx("operational parameter negotiation done; "
730 "transitioning to Full Feature Phase");
732 keys_save(response_keys, response);
734 pdu_delete(response);
735 keys_delete(response_keys);
737 keys_delete(request_keys);
741 login(struct connection *conn)
743 struct pdu *request, *response;
744 struct iscsi_bhs_login_request *bhslr;
745 struct keys *request_keys, *response_keys;
746 struct auth_group *ag;
747 struct portal_group *pg;
748 const char *initiator_name, *initiator_alias, *session_type,
749 *target_name, *auth_method;
753 * Handle the initial Login Request - figure out required authentication
754 * method and either transition to the next phase, if no authentication
755 * is required, or call appropriate authentication code.
757 log_debugx("beginning Login Phase; waiting for Login PDU");
758 request = login_receive(conn, true);
759 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
760 if (bhslr->bhslr_tsih != 0) {
761 login_send_error(request, 0x02, 0x0a);
762 log_errx(1, "received Login PDU with non-zero TSIH");
765 pg = conn->conn_portal->p_portal_group;
767 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid,
768 sizeof(conn->conn_initiator_isid));
771 * XXX: Implement the C flag some day.
773 request_keys = keys_new();
774 keys_load(request_keys, request);
776 assert(conn->conn_initiator_name == NULL);
777 initiator_name = keys_find(request_keys, "InitiatorName");
778 if (initiator_name == NULL) {
779 login_send_error(request, 0x02, 0x07);
780 log_errx(1, "received Login PDU without InitiatorName");
782 if (valid_iscsi_name(initiator_name) == false) {
783 login_send_error(request, 0x02, 0x00);
784 log_errx(1, "received Login PDU with invalid InitiatorName");
786 conn->conn_initiator_name = checked_strdup(initiator_name);
787 log_set_peer_name(conn->conn_initiator_name);
788 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name);
790 redirected = login_portal_redirect(conn, request);
792 log_debugx("initiator redirected; exiting");
796 initiator_alias = keys_find(request_keys, "InitiatorAlias");
797 if (initiator_alias != NULL)
798 conn->conn_initiator_alias = checked_strdup(initiator_alias);
800 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE);
801 session_type = keys_find(request_keys, "SessionType");
802 if (session_type != NULL) {
803 if (strcmp(session_type, "Normal") == 0) {
804 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
805 } else if (strcmp(session_type, "Discovery") == 0) {
806 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY;
808 login_send_error(request, 0x02, 0x00);
809 log_errx(1, "received Login PDU with invalid "
810 "SessionType \"%s\"", session_type);
813 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
815 assert(conn->conn_target == NULL);
816 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
817 target_name = keys_find(request_keys, "TargetName");
818 if (target_name == NULL) {
819 login_send_error(request, 0x02, 0x07);
820 log_errx(1, "received Login PDU without TargetName");
823 conn->conn_port = port_find_in_pg(pg, target_name);
824 if (conn->conn_port == NULL) {
825 login_send_error(request, 0x02, 0x03);
826 log_errx(1, "requested target \"%s\" not found",
829 conn->conn_target = conn->conn_port->p_target;
833 * At this point we know what kind of authentication we need.
835 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
836 ag = conn->conn_port->p_auth_group;
838 ag = conn->conn_target->t_auth_group;
839 if (ag->ag_name != NULL) {
840 log_debugx("initiator requests to connect "
841 "to target \"%s\"; auth-group \"%s\"",
842 conn->conn_target->t_name,
845 log_debugx("initiator requests to connect "
846 "to target \"%s\"", conn->conn_target->t_name);
849 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
850 ag = pg->pg_discovery_auth_group;
851 if (ag->ag_name != NULL) {
852 log_debugx("initiator requests "
853 "discovery session; auth-group \"%s\"", ag->ag_name);
855 log_debugx("initiator requests discovery session");
860 * Enforce initiator-name and initiator-portal.
862 if (auth_name_check(ag, initiator_name) != 0) {
863 login_send_error(request, 0x02, 0x02);
864 log_errx(1, "initiator does not match allowed initiator names");
867 if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
868 login_send_error(request, 0x02, 0x02);
869 log_errx(1, "initiator does not match allowed "
870 "initiator portals");
874 * Let's see if the initiator intends to do any kind of authentication
877 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
878 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
879 login_send_error(request, 0x02, 0x01);
880 log_errx(1, "initiator skipped the authentication, "
881 "but authentication is required");
884 keys_delete(request_keys);
886 log_debugx("initiator skipped the authentication, "
887 "and we don't need it; proceeding with negotiation");
888 login_negotiate(conn, request);
892 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) {
894 * Initiator might want to to authenticate,
895 * but we don't need it.
897 log_debugx("authentication not required; "
898 "transitioning to operational parameter negotiation");
900 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0)
901 log_warnx("initiator did not set the \"T\" flag; "
902 "transitioning anyway");
904 response = login_new_response(request);
905 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
906 response_keys = keys_new();
908 * Required by Linux initiator.
910 auth_method = keys_find(request_keys, "AuthMethod");
911 if (auth_method != NULL &&
912 login_list_contains(auth_method, "None"))
913 keys_add(response_keys, "AuthMethod", "None");
915 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
916 if (conn->conn_target->t_alias != NULL)
917 keys_add(response_keys,
918 "TargetAlias", conn->conn_target->t_alias);
919 keys_add_int(response_keys,
920 "TargetPortalGroupTag", pg->pg_tag);
922 keys_save(response_keys, response);
924 pdu_delete(response);
925 keys_delete(response_keys);
927 keys_delete(request_keys);
929 login_negotiate(conn, NULL);
933 if (ag->ag_type == AG_TYPE_DENY) {
934 login_send_error(request, 0x02, 0x01);
935 log_errx(1, "auth-type is \"deny\"");
938 if (ag->ag_type == AG_TYPE_UNKNOWN) {
940 * This can happen with empty auth-group.
942 login_send_error(request, 0x02, 0x01);
943 log_errx(1, "auth-type not set, denying access");
946 log_debugx("CHAP authentication required");
948 auth_method = keys_find(request_keys, "AuthMethod");
949 if (auth_method == NULL) {
950 login_send_error(request, 0x02, 0x07);
951 log_errx(1, "received Login PDU without AuthMethod");
954 * XXX: This should be Reject, not just a login failure (5.3.2).
956 if (login_list_contains(auth_method, "CHAP") == 0) {
957 login_send_error(request, 0x02, 0x01);
958 log_errx(1, "initiator requests unsupported AuthMethod \"%s\" "
959 "instead of \"CHAP\"", auth_method);
962 response = login_new_response(request);
964 response_keys = keys_new();
965 keys_add(response_keys, "AuthMethod", "CHAP");
966 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
967 if (conn->conn_target->t_alias != NULL)
968 keys_add(response_keys,
969 "TargetAlias", conn->conn_target->t_alias);
970 keys_add_int(response_keys,
971 "TargetPortalGroupTag", pg->pg_tag);
973 keys_save(response_keys, response);
976 pdu_delete(response);
977 keys_delete(response_keys);
979 keys_delete(request_keys);
981 login_chap(conn, ag);
983 login_negotiate(conn, NULL);