2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __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 (initial == false &&
132 ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
133 login_send_error(request, 0x02, 0x00);
134 log_errx(1, "received Login PDU with decreasing CmdSN: "
135 "was %u, is %u", conn->conn_cmdsn,
136 ntohl(bhslr->bhslr_cmdsn));
138 if (initial == false &&
139 ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
140 login_send_error(request, 0x02, 0x00);
141 log_errx(1, "received Login PDU with wrong ExpStatSN: "
142 "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
145 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
151 login_new_response(struct pdu *request)
153 struct pdu *response;
154 struct connection *conn;
155 struct iscsi_bhs_login_request *bhslr;
156 struct iscsi_bhs_login_response *bhslr2;
158 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
159 conn = request->pdu_connection;
161 response = pdu_new_response(request);
162 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
163 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_RESPONSE;
164 login_set_csg(response, BHSLR_STAGE_SECURITY_NEGOTIATION);
165 memcpy(bhslr2->bhslr_isid,
166 bhslr->bhslr_isid, sizeof(bhslr2->bhslr_isid));
167 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
168 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
169 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
170 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
176 login_send_error(struct pdu *request, char class, char detail)
178 struct pdu *response;
179 struct iscsi_bhs_login_response *bhslr2;
181 log_debugx("sending Login Response PDU with failure class 0x%x/0x%x; "
182 "see next line for reason", class, detail);
183 response = login_new_response(request);
184 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
185 bhslr2->bhslr_status_class = class;
186 bhslr2->bhslr_status_detail = detail;
189 pdu_delete(response);
193 login_list_contains(const char *list, const char *what)
195 char *tofree, *str, *token;
197 tofree = str = checked_strdup(list);
199 while ((token = strsep(&str, ",")) != NULL) {
200 if (strcmp(token, what) == 0) {
210 login_list_prefers(const char *list,
211 const char *choice1, const char *choice2)
213 char *tofree, *str, *token;
215 tofree = str = checked_strdup(list);
217 while ((token = strsep(&str, ",")) != NULL) {
218 if (strcmp(token, choice1) == 0) {
222 if (strcmp(token, choice2) == 0) {
232 login_receive_chap_a(struct connection *conn)
235 struct keys *request_keys;
238 request = login_receive(conn, false);
239 request_keys = keys_new();
240 keys_load(request_keys, request);
242 chap_a = keys_find(request_keys, "CHAP_A");
243 if (chap_a == NULL) {
244 login_send_error(request, 0x02, 0x07);
245 log_errx(1, "received CHAP Login PDU without CHAP_A");
247 if (login_list_contains(chap_a, "5") == 0) {
248 login_send_error(request, 0x02, 0x01);
249 log_errx(1, "received CHAP Login PDU with unsupported CHAP_A "
252 keys_delete(request_keys);
258 login_send_chap_c(struct pdu *request, struct chap *chap)
260 struct pdu *response;
261 struct keys *response_keys;
262 char *chap_c, *chap_i;
264 chap_c = chap_get_challenge(chap);
265 chap_i = chap_get_id(chap);
267 response = login_new_response(request);
268 response_keys = keys_new();
269 keys_add(response_keys, "CHAP_A", "5");
270 keys_add(response_keys, "CHAP_I", chap_i);
271 keys_add(response_keys, "CHAP_C", chap_c);
274 keys_save(response_keys, response);
276 pdu_delete(response);
277 keys_delete(response_keys);
281 login_receive_chap_r(struct connection *conn, struct auth_group *ag,
282 struct chap *chap, const struct auth **authp)
285 struct keys *request_keys;
286 const char *chap_n, *chap_r;
287 const struct auth *auth;
290 request = login_receive(conn, false);
291 request_keys = keys_new();
292 keys_load(request_keys, request);
294 chap_n = keys_find(request_keys, "CHAP_N");
295 if (chap_n == NULL) {
296 login_send_error(request, 0x02, 0x07);
297 log_errx(1, "received CHAP Login PDU without CHAP_N");
299 chap_r = keys_find(request_keys, "CHAP_R");
300 if (chap_r == NULL) {
301 login_send_error(request, 0x02, 0x07);
302 log_errx(1, "received CHAP Login PDU without CHAP_R");
304 error = chap_receive(chap, chap_r);
306 login_send_error(request, 0x02, 0x07);
307 log_errx(1, "received CHAP Login PDU with malformed CHAP_R");
311 * Verify the response.
313 assert(ag->ag_type == AG_TYPE_CHAP ||
314 ag->ag_type == AG_TYPE_CHAP_MUTUAL);
315 auth = auth_find(ag, chap_n);
317 login_send_error(request, 0x02, 0x01);
318 log_errx(1, "received CHAP Login with invalid user \"%s\"",
322 assert(auth->a_secret != NULL);
323 assert(strlen(auth->a_secret) > 0);
325 error = chap_authenticate(chap, auth->a_secret);
327 login_send_error(request, 0x02, 0x01);
328 log_errx(1, "CHAP authentication failed for user \"%s\"",
332 keys_delete(request_keys);
339 login_send_chap_success(struct pdu *request,
340 const struct auth *auth)
342 struct pdu *response;
343 struct keys *request_keys, *response_keys;
345 const char *chap_i, *chap_c;
349 response = login_new_response(request);
350 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
353 * Actually, one more thing: mutual authentication.
355 request_keys = keys_new();
356 keys_load(request_keys, request);
357 chap_i = keys_find(request_keys, "CHAP_I");
358 chap_c = keys_find(request_keys, "CHAP_C");
359 if (chap_i != NULL || chap_c != NULL) {
360 if (chap_i == NULL) {
361 login_send_error(request, 0x02, 0x07);
362 log_errx(1, "initiator requested target "
363 "authentication, but didn't send CHAP_I");
365 if (chap_c == NULL) {
366 login_send_error(request, 0x02, 0x07);
367 log_errx(1, "initiator requested target "
368 "authentication, but didn't send CHAP_C");
370 if (auth->a_auth_group->ag_type != AG_TYPE_CHAP_MUTUAL) {
371 login_send_error(request, 0x02, 0x01);
372 log_errx(1, "initiator requests target authentication "
373 "for user \"%s\", but mutual user/secret "
374 "is not set", auth->a_user);
377 log_debugx("performing mutual authentication as user \"%s\"",
378 auth->a_mutual_user);
380 rchap = rchap_new(auth->a_mutual_secret);
381 error = rchap_receive(rchap, chap_i, chap_c);
383 login_send_error(request, 0x02, 0x07);
384 log_errx(1, "received CHAP Login PDU with malformed "
387 chap_r = rchap_get_response(rchap);
389 response_keys = keys_new();
390 keys_add(response_keys, "CHAP_N", auth->a_mutual_user);
391 keys_add(response_keys, "CHAP_R", chap_r);
393 keys_save(response_keys, response);
394 keys_delete(response_keys);
396 log_debugx("initiator did not request target authentication");
399 keys_delete(request_keys);
401 pdu_delete(response);
405 login_chap(struct connection *conn, struct auth_group *ag)
407 const struct auth *auth;
412 * Receive CHAP_A PDU.
414 log_debugx("beginning CHAP authentication; waiting for CHAP_A");
415 request = login_receive_chap_a(conn);
418 * Generate the challenge.
423 * Send the challenge.
425 log_debugx("sending CHAP_C, binary challenge size is %zd bytes",
426 sizeof(chap->chap_challenge));
427 login_send_chap_c(request, chap);
431 * Receive CHAP_N/CHAP_R PDU and authenticate.
433 log_debugx("waiting for CHAP_N/CHAP_R");
434 request = login_receive_chap_r(conn, ag, chap, &auth);
437 * Yay, authentication succeeded!
439 log_debugx("authentication succeeded for user \"%s\"; "
440 "transitioning to operational parameter negotiation", auth->a_user);
441 login_send_chap_success(request, auth);
445 * Leave username and CHAP information for discovery().
447 conn->conn_user = auth->a_user;
448 conn->conn_chap = chap;
452 login_negotiate_key(struct pdu *request, const char *name,
453 const char *value, bool skipped_security, struct keys *response_keys)
457 struct connection *conn;
459 conn = request->pdu_connection;
461 if (strcmp(name, "InitiatorName") == 0) {
462 if (!skipped_security)
463 log_errx(1, "initiator resent InitiatorName");
464 } else if (strcmp(name, "SessionType") == 0) {
465 if (!skipped_security)
466 log_errx(1, "initiator resent SessionType");
467 } else if (strcmp(name, "TargetName") == 0) {
468 if (!skipped_security)
469 log_errx(1, "initiator resent TargetName");
470 } else if (strcmp(name, "InitiatorAlias") == 0) {
471 if (conn->conn_initiator_alias != NULL)
472 free(conn->conn_initiator_alias);
473 conn->conn_initiator_alias = checked_strdup(value);
474 } else if (strcmp(value, "Irrelevant") == 0) {
476 } else if (strcmp(name, "HeaderDigest") == 0) {
478 * We don't handle digests for discovery sessions.
480 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
481 log_debugx("discovery session; digests disabled");
482 keys_add(response_keys, name, "None");
486 which = login_list_prefers(value, "CRC32C", "None");
489 log_debugx("initiator prefers CRC32C "
490 "for header digest; we'll use it");
491 conn->conn_header_digest = CONN_DIGEST_CRC32C;
492 keys_add(response_keys, name, "CRC32C");
495 log_debugx("initiator prefers not to do "
496 "header digest; we'll comply");
497 keys_add(response_keys, name, "None");
500 log_warnx("initiator sent unrecognized "
501 "HeaderDigest value \"%s\"; will use None", value);
502 keys_add(response_keys, name, "None");
505 } else if (strcmp(name, "DataDigest") == 0) {
506 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
507 log_debugx("discovery session; digests disabled");
508 keys_add(response_keys, name, "None");
512 which = login_list_prefers(value, "CRC32C", "None");
515 log_debugx("initiator prefers CRC32C "
516 "for data digest; we'll use it");
517 conn->conn_data_digest = CONN_DIGEST_CRC32C;
518 keys_add(response_keys, name, "CRC32C");
521 log_debugx("initiator prefers not to do "
522 "data digest; we'll comply");
523 keys_add(response_keys, name, "None");
526 log_warnx("initiator sent unrecognized "
527 "DataDigest value \"%s\"; will use None", value);
528 keys_add(response_keys, name, "None");
531 } else if (strcmp(name, "MaxConnections") == 0) {
532 keys_add(response_keys, name, "1");
533 } else if (strcmp(name, "InitialR2T") == 0) {
534 keys_add(response_keys, name, "Yes");
535 } else if (strcmp(name, "ImmediateData") == 0) {
536 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
537 log_debugx("discovery session; ImmediateData irrelevant");
538 keys_add(response_keys, name, "Irrelevant");
540 if (strcmp(value, "Yes") == 0) {
541 conn->conn_immediate_data = true;
542 keys_add(response_keys, name, "Yes");
544 conn->conn_immediate_data = false;
545 keys_add(response_keys, name, "No");
548 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
549 tmp = strtoul(value, NULL, 10);
551 login_send_error(request, 0x02, 0x00);
552 log_errx(1, "received invalid "
553 "MaxRecvDataSegmentLength");
555 if (tmp > conn->conn_data_segment_limit) {
556 log_debugx("capping MaxRecvDataSegmentLength "
557 "from %zd to %zd", tmp, conn->conn_data_segment_limit);
558 tmp = conn->conn_data_segment_limit;
560 conn->conn_max_data_segment_length = tmp;
561 keys_add_int(response_keys, name, conn->conn_data_segment_limit);
562 } else if (strcmp(name, "MaxBurstLength") == 0) {
563 tmp = strtoul(value, NULL, 10);
565 login_send_error(request, 0x02, 0x00);
566 log_errx(1, "received invalid MaxBurstLength");
568 if (tmp > MAX_BURST_LENGTH) {
569 log_debugx("capping MaxBurstLength from %zd to %d",
570 tmp, MAX_BURST_LENGTH);
571 tmp = MAX_BURST_LENGTH;
573 conn->conn_max_burst_length = tmp;
574 keys_add_int(response_keys, name, tmp);
575 } else if (strcmp(name, "FirstBurstLength") == 0) {
576 tmp = strtoul(value, NULL, 10);
578 login_send_error(request, 0x02, 0x00);
579 log_errx(1, "received invalid FirstBurstLength");
581 if (tmp > FIRST_BURST_LENGTH) {
582 log_debugx("capping FirstBurstLength from %zd to %d",
583 tmp, FIRST_BURST_LENGTH);
584 tmp = FIRST_BURST_LENGTH;
586 conn->conn_first_burst_length = tmp;
587 keys_add_int(response_keys, name, tmp);
588 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
589 keys_add(response_keys, name, value);
590 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
591 keys_add(response_keys, name, "0");
592 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
593 keys_add(response_keys, name, "1");
594 } else if (strcmp(name, "DataPDUInOrder") == 0) {
595 keys_add(response_keys, name, "Yes");
596 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
597 keys_add(response_keys, name, "Yes");
598 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
599 keys_add(response_keys, name, "0");
600 } else if (strcmp(name, "OFMarker") == 0) {
601 keys_add(response_keys, name, "No");
602 } else if (strcmp(name, "IFMarker") == 0) {
603 keys_add(response_keys, name, "No");
604 } else if (strcmp(name, "iSCSIProtocolLevel") == 0) {
605 tmp = strtoul(value, NULL, 10);
608 keys_add_int(response_keys, name, tmp);
610 log_debugx("unknown key \"%s\"; responding "
611 "with NotUnderstood", name);
612 keys_add(response_keys, name, "NotUnderstood");
617 login_redirect(struct pdu *request, const char *target_address)
619 struct pdu *response;
620 struct iscsi_bhs_login_response *bhslr2;
621 struct keys *response_keys;
623 response = login_new_response(request);
624 login_set_csg(response, login_csg(request));
625 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
626 bhslr2->bhslr_status_class = 0x01;
627 bhslr2->bhslr_status_detail = 0x01;
629 response_keys = keys_new();
630 keys_add(response_keys, "TargetAddress", target_address);
632 keys_save(response_keys, response);
634 pdu_delete(response);
635 keys_delete(response_keys);
639 login_portal_redirect(struct connection *conn, struct pdu *request)
641 const struct portal_group *pg;
643 pg = conn->conn_portal->p_portal_group;
644 if (pg->pg_redirection == NULL)
647 log_debugx("portal-group \"%s\" configured to redirect to %s",
648 pg->pg_name, pg->pg_redirection);
649 login_redirect(request, pg->pg_redirection);
655 login_target_redirect(struct connection *conn, struct pdu *request)
657 const char *target_address;
659 assert(conn->conn_portal->p_portal_group->pg_redirection == NULL);
661 if (conn->conn_target == NULL)
664 target_address = conn->conn_target->t_redirection;
665 if (target_address == NULL)
668 log_debugx("target \"%s\" configured to redirect to %s",
669 conn->conn_target->t_name, target_address);
670 login_redirect(request, target_address);
676 login_negotiate(struct connection *conn, struct pdu *request)
678 struct pdu *response;
679 struct iscsi_bhs_login_response *bhslr2;
680 struct keys *request_keys, *response_keys;
682 bool redirected, skipped_security;
684 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
686 * Query the kernel for MaxDataSegmentLength it can handle.
687 * In case of offload, it depends on hardware capabilities.
689 assert(conn->conn_target != NULL);
690 kernel_limits(conn->conn_portal->p_portal_group->pg_offload,
691 &conn->conn_data_segment_limit);
693 conn->conn_data_segment_limit = MAX_DATA_SEGMENT_LENGTH;
696 if (request == NULL) {
697 log_debugx("beginning operational parameter negotiation; "
698 "waiting for Login PDU");
699 request = login_receive(conn, false);
700 skipped_security = false;
702 skipped_security = true;
705 * RFC 3720, 10.13.5. Status-Class and Status-Detail, says
706 * the redirection SHOULD be accepted by the initiator before
707 * authentication, but MUST be be accepted afterwards; that's
708 * why we're doing it here and not earlier.
710 redirected = login_target_redirect(conn, request);
712 log_debugx("initiator redirected; exiting");
716 request_keys = keys_new();
717 keys_load(request_keys, request);
719 response = login_new_response(request);
720 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
721 bhslr2->bhslr_tsih = htons(0xbadd);
722 login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
723 login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE);
724 response_keys = keys_new();
726 if (skipped_security &&
727 conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
728 if (conn->conn_target->t_alias != NULL)
729 keys_add(response_keys,
730 "TargetAlias", conn->conn_target->t_alias);
731 keys_add_int(response_keys, "TargetPortalGroupTag",
732 conn->conn_portal->p_portal_group->pg_tag);
735 for (i = 0; i < KEYS_MAX; i++) {
736 if (request_keys->keys_names[i] == NULL)
739 login_negotiate_key(request, request_keys->keys_names[i],
740 request_keys->keys_values[i], skipped_security,
744 log_debugx("operational parameter negotiation done; "
745 "transitioning to Full Feature Phase");
747 keys_save(response_keys, response);
749 pdu_delete(response);
750 keys_delete(response_keys);
752 keys_delete(request_keys);
756 login_wait_transition(struct connection *conn)
758 struct pdu *request, *response;
759 struct iscsi_bhs_login_request *bhslr;
761 log_debugx("waiting for state transition request");
762 request = login_receive(conn, false);
763 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
764 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0) {
765 login_send_error(request, 0x02, 0x00);
766 log_errx(1, "got no \"T\" flag after answering AuthMethod");
769 log_debugx("got state transition request");
770 response = login_new_response(request);
772 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
774 pdu_delete(response);
776 login_negotiate(conn, NULL);
780 login(struct connection *conn)
782 struct pdu *request, *response;
783 struct iscsi_bhs_login_request *bhslr;
784 struct keys *request_keys, *response_keys;
785 struct auth_group *ag;
786 struct portal_group *pg;
787 const char *initiator_name, *initiator_alias, *session_type,
788 *target_name, *auth_method;
789 bool redirected, fail, trans;
792 * Handle the initial Login Request - figure out required authentication
793 * method and either transition to the next phase, if no authentication
794 * is required, or call appropriate authentication code.
796 log_debugx("beginning Login Phase; waiting for Login PDU");
797 request = login_receive(conn, true);
798 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
799 if (bhslr->bhslr_tsih != 0) {
800 login_send_error(request, 0x02, 0x0a);
801 log_errx(1, "received Login PDU with non-zero TSIH");
804 pg = conn->conn_portal->p_portal_group;
806 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid,
807 sizeof(conn->conn_initiator_isid));
810 * XXX: Implement the C flag some day.
812 request_keys = keys_new();
813 keys_load(request_keys, request);
815 assert(conn->conn_initiator_name == NULL);
816 initiator_name = keys_find(request_keys, "InitiatorName");
817 if (initiator_name == NULL) {
818 login_send_error(request, 0x02, 0x07);
819 log_errx(1, "received Login PDU without InitiatorName");
821 if (valid_iscsi_name(initiator_name) == false) {
822 login_send_error(request, 0x02, 0x00);
823 log_errx(1, "received Login PDU with invalid InitiatorName");
825 conn->conn_initiator_name = checked_strdup(initiator_name);
826 log_set_peer_name(conn->conn_initiator_name);
827 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name);
829 redirected = login_portal_redirect(conn, request);
831 log_debugx("initiator redirected; exiting");
835 initiator_alias = keys_find(request_keys, "InitiatorAlias");
836 if (initiator_alias != NULL)
837 conn->conn_initiator_alias = checked_strdup(initiator_alias);
839 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE);
840 session_type = keys_find(request_keys, "SessionType");
841 if (session_type != NULL) {
842 if (strcmp(session_type, "Normal") == 0) {
843 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
844 } else if (strcmp(session_type, "Discovery") == 0) {
845 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY;
847 login_send_error(request, 0x02, 0x00);
848 log_errx(1, "received Login PDU with invalid "
849 "SessionType \"%s\"", session_type);
852 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
854 assert(conn->conn_target == NULL);
855 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
856 target_name = keys_find(request_keys, "TargetName");
857 if (target_name == NULL) {
858 login_send_error(request, 0x02, 0x07);
859 log_errx(1, "received Login PDU without TargetName");
862 conn->conn_port = port_find_in_pg(pg, target_name);
863 if (conn->conn_port == NULL) {
864 login_send_error(request, 0x02, 0x03);
865 log_errx(1, "requested target \"%s\" not found",
868 conn->conn_target = conn->conn_port->p_target;
872 * At this point we know what kind of authentication we need.
874 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
875 ag = conn->conn_port->p_auth_group;
877 ag = conn->conn_target->t_auth_group;
878 if (ag->ag_name != NULL) {
879 log_debugx("initiator requests to connect "
880 "to target \"%s\"; auth-group \"%s\"",
881 conn->conn_target->t_name,
884 log_debugx("initiator requests to connect "
885 "to target \"%s\"", conn->conn_target->t_name);
888 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
889 ag = pg->pg_discovery_auth_group;
890 if (ag->ag_name != NULL) {
891 log_debugx("initiator requests "
892 "discovery session; auth-group \"%s\"", ag->ag_name);
894 log_debugx("initiator requests discovery session");
898 if (ag->ag_type == AG_TYPE_DENY) {
899 login_send_error(request, 0x02, 0x01);
900 log_errx(1, "auth-type is \"deny\"");
903 if (ag->ag_type == AG_TYPE_UNKNOWN) {
905 * This can happen with empty auth-group.
907 login_send_error(request, 0x02, 0x01);
908 log_errx(1, "auth-type not set, denying access");
912 * Enforce initiator-name and initiator-portal.
914 if (auth_name_check(ag, initiator_name) != 0) {
915 login_send_error(request, 0x02, 0x02);
916 log_errx(1, "initiator does not match allowed initiator names");
919 if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
920 login_send_error(request, 0x02, 0x02);
921 log_errx(1, "initiator does not match allowed "
922 "initiator portals");
926 * Let's see if the initiator intends to do any kind of authentication
929 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
930 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
931 login_send_error(request, 0x02, 0x01);
932 log_errx(1, "initiator skipped the authentication, "
933 "but authentication is required");
936 keys_delete(request_keys);
938 log_debugx("initiator skipped the authentication, "
939 "and we don't need it; proceeding with negotiation");
940 login_negotiate(conn, request);
945 response = login_new_response(request);
946 response_keys = keys_new();
947 trans = (bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0;
948 auth_method = keys_find(request_keys, "AuthMethod");
949 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) {
950 log_debugx("authentication not required");
951 if (auth_method == NULL ||
952 login_list_contains(auth_method, "None")) {
953 keys_add(response_keys, "AuthMethod", "None");
955 log_warnx("initiator requests "
956 "AuthMethod \"%s\" instead of \"None\"",
958 keys_add(response_keys, "AuthMethod", "Reject");
961 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
963 log_debugx("CHAP authentication required");
964 if (auth_method == NULL ||
965 login_list_contains(auth_method, "CHAP")) {
966 keys_add(response_keys, "AuthMethod", "CHAP");
968 log_warnx("initiator requests unsupported "
969 "AuthMethod \"%s\" instead of \"CHAP\"",
971 keys_add(response_keys, "AuthMethod", "Reject");
975 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
976 if (conn->conn_target->t_alias != NULL)
977 keys_add(response_keys,
978 "TargetAlias", conn->conn_target->t_alias);
979 keys_add_int(response_keys,
980 "TargetPortalGroupTag", pg->pg_tag);
982 keys_save(response_keys, response);
985 pdu_delete(response);
986 keys_delete(response_keys);
988 keys_delete(request_keys);
991 log_debugx("sent reject for AuthMethod; exiting");
995 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
996 login_chap(conn, ag);
997 login_negotiate(conn, NULL);
999 login_negotiate(conn, NULL);
1001 login_wait_transition(conn);