2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
40 #include <netinet/in.h>
43 #include "iscsi_proto.h"
45 static void login_send_error(struct pdu *request,
46 char class, char detail);
49 login_set_nsg(struct pdu *response, int nsg)
51 struct iscsi_bhs_login_response *bhslr;
53 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
54 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
55 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
57 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
59 bhslr->bhslr_flags &= 0xFC;
60 bhslr->bhslr_flags |= nsg;
61 bhslr->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
65 login_csg(const struct pdu *request)
67 struct iscsi_bhs_login_request *bhslr;
69 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
71 return ((bhslr->bhslr_flags & 0x0C) >> 2);
75 login_set_csg(struct pdu *response, int csg)
77 struct iscsi_bhs_login_response *bhslr;
79 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
80 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
81 csg == BHSLR_STAGE_FULL_FEATURE_PHASE);
83 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
85 bhslr->bhslr_flags &= 0xF3;
86 bhslr->bhslr_flags |= csg << 2;
90 login_receive(struct connection *conn, bool initial)
93 struct iscsi_bhs_login_request *bhslr;
95 request = pdu_new(conn);
97 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
98 ISCSI_BHS_OPCODE_LOGIN_REQUEST) {
100 * The first PDU in session is special - if we receive any PDU
101 * different than login request, we have to drop the connection
102 * without sending response ("A target receiving any PDU
103 * except a Login request before the Login Phase is started MUST
104 * immediately terminate the connection on which the PDU
107 if (initial == false)
108 login_send_error(request, 0x02, 0x0b);
109 log_errx(1, "protocol error: received invalid opcode 0x%x",
110 request->pdu_bhs->bhs_opcode);
112 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
114 * XXX: Implement the C flag some day.
116 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0) {
117 login_send_error(request, 0x03, 0x00);
118 log_errx(1, "received Login PDU with unsupported \"C\" flag");
120 if (bhslr->bhslr_version_max != 0x00) {
121 login_send_error(request, 0x02, 0x05);
122 log_errx(1, "received Login PDU with unsupported "
123 "Version-max 0x%x", bhslr->bhslr_version_max);
125 if (bhslr->bhslr_version_min != 0x00) {
126 login_send_error(request, 0x02, 0x05);
127 log_errx(1, "received Login PDU with unsupported "
128 "Version-min 0x%x", bhslr->bhslr_version_min);
130 if (initial == false &&
131 ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
132 login_send_error(request, 0x02, 0x00);
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, 0x00);
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_pdu(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_pdu(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_pdu(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_pdu(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_pdu(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 ctld_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->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->conn, ag, chap, &auth);
436 * Yay, authentication succeeded!
438 log_debugx("authentication succeeded for user \"%s\"; "
439 "transitioning to operational parameter negotiation", 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 ctld_connection *conn;
458 conn = (struct ctld_connection *)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.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.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.conn_immediate_data = true;
541 keys_add(response_keys, name, "Yes");
543 conn->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");
556 * MaxRecvDataSegmentLength is a direction-specific parameter.
557 * We'll limit our _send_ to what the initiator can handle but
558 * our MaxRecvDataSegmentLength is not influenced by the
559 * initiator in any way.
561 if ((int)tmp > conn->conn_max_send_data_segment_limit) {
562 log_debugx("capping MaxRecvDataSegmentLength "
563 "from %zd to %d", tmp,
564 conn->conn_max_send_data_segment_limit);
565 tmp = conn->conn_max_send_data_segment_limit;
567 conn->conn.conn_max_send_data_segment_length = tmp;
568 } else if (strcmp(name, "MaxBurstLength") == 0) {
569 tmp = strtoul(value, NULL, 10);
571 login_send_error(request, 0x02, 0x00);
572 log_errx(1, "received invalid MaxBurstLength");
574 if ((int)tmp > conn->conn_max_burst_limit) {
575 log_debugx("capping MaxBurstLength from %zd to %d",
576 tmp, conn->conn_max_burst_limit);
577 tmp = conn->conn_max_burst_limit;
579 conn->conn.conn_max_burst_length = tmp;
580 keys_add_int(response_keys, name, tmp);
581 } else if (strcmp(name, "FirstBurstLength") == 0) {
582 tmp = strtoul(value, NULL, 10);
584 login_send_error(request, 0x02, 0x00);
585 log_errx(1, "received invalid FirstBurstLength");
587 if ((int)tmp > conn->conn_first_burst_limit) {
588 log_debugx("capping FirstBurstLength from %zd to %d",
589 tmp, conn->conn_first_burst_limit);
590 tmp = conn->conn_first_burst_limit;
592 conn->conn.conn_first_burst_length = tmp;
593 keys_add_int(response_keys, name, tmp);
594 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
595 keys_add(response_keys, name, value);
596 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
597 keys_add(response_keys, name, "0");
598 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
599 keys_add(response_keys, name, "1");
600 } else if (strcmp(name, "DataPDUInOrder") == 0) {
601 keys_add(response_keys, name, "Yes");
602 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
603 keys_add(response_keys, name, "Yes");
604 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
605 keys_add(response_keys, name, "0");
606 } else if (strcmp(name, "OFMarker") == 0) {
607 keys_add(response_keys, name, "No");
608 } else if (strcmp(name, "IFMarker") == 0) {
609 keys_add(response_keys, name, "No");
610 } else if (strcmp(name, "iSCSIProtocolLevel") == 0) {
611 tmp = strtoul(value, NULL, 10);
614 keys_add_int(response_keys, name, tmp);
616 log_debugx("unknown key \"%s\"; responding "
617 "with NotUnderstood", name);
618 keys_add(response_keys, name, "NotUnderstood");
623 login_redirect(struct pdu *request, const char *target_address)
625 struct pdu *response;
626 struct iscsi_bhs_login_response *bhslr2;
627 struct keys *response_keys;
629 response = login_new_response(request);
630 login_set_csg(response, login_csg(request));
631 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
632 bhslr2->bhslr_status_class = 0x01;
633 bhslr2->bhslr_status_detail = 0x01;
635 response_keys = keys_new();
636 keys_add(response_keys, "TargetAddress", target_address);
638 keys_save_pdu(response_keys, response);
640 pdu_delete(response);
641 keys_delete(response_keys);
645 login_portal_redirect(struct ctld_connection *conn, struct pdu *request)
647 const struct portal_group *pg;
649 pg = conn->conn_portal->p_portal_group;
650 if (pg->pg_redirection == NULL)
653 log_debugx("portal-group \"%s\" configured to redirect to %s",
654 pg->pg_name, pg->pg_redirection);
655 login_redirect(request, pg->pg_redirection);
661 login_target_redirect(struct ctld_connection *conn, struct pdu *request)
663 const char *target_address;
665 assert(conn->conn_portal->p_portal_group->pg_redirection == NULL);
667 if (conn->conn_target == NULL)
670 target_address = conn->conn_target->t_redirection;
671 if (target_address == NULL)
674 log_debugx("target \"%s\" configured to redirect to %s",
675 conn->conn_target->t_name, target_address);
676 login_redirect(request, target_address);
682 login_negotiate(struct ctld_connection *conn, struct pdu *request)
684 struct pdu *response;
685 struct iscsi_bhs_login_response *bhslr2;
686 struct keys *request_keys, *response_keys;
688 bool redirected, skipped_security;
690 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
692 * Query the kernel for various size limits. In case of
693 * offload, it depends on hardware capabilities.
695 assert(conn->conn_target != NULL);
696 conn->conn_max_recv_data_segment_limit = (1 << 24) - 1;
697 conn->conn_max_send_data_segment_limit = (1 << 24) - 1;
698 conn->conn_max_burst_limit = (1 << 24) - 1;
699 conn->conn_first_burst_limit = (1 << 24) - 1;
700 kernel_limits(conn->conn_portal->p_portal_group->pg_offload,
701 &conn->conn_max_recv_data_segment_limit,
702 &conn->conn_max_send_data_segment_limit,
703 &conn->conn_max_burst_limit,
704 &conn->conn_first_burst_limit);
706 /* We expect legal, usable values at this point. */
707 assert(conn->conn_max_recv_data_segment_limit >= 512);
708 assert(conn->conn_max_recv_data_segment_limit < (1 << 24));
709 assert(conn->conn_max_send_data_segment_limit >= 512);
710 assert(conn->conn_max_send_data_segment_limit < (1 << 24));
711 assert(conn->conn_max_burst_limit >= 512);
712 assert(conn->conn_max_burst_limit < (1 << 24));
713 assert(conn->conn_first_burst_limit >= 512);
714 assert(conn->conn_first_burst_limit < (1 << 24));
715 assert(conn->conn_first_burst_limit <=
716 conn->conn_max_burst_limit);
719 * Limit default send length in case it won't be negotiated.
720 * We can't do it for other limits, since they may affect both
721 * sender and receiver operation, and we must obey defaults.
723 if (conn->conn_max_send_data_segment_limit <
724 conn->conn.conn_max_send_data_segment_length) {
725 conn->conn.conn_max_send_data_segment_length =
726 conn->conn_max_send_data_segment_limit;
729 conn->conn_max_recv_data_segment_limit =
730 MAX_DATA_SEGMENT_LENGTH;
731 conn->conn_max_send_data_segment_limit =
732 MAX_DATA_SEGMENT_LENGTH;
735 if (request == NULL) {
736 log_debugx("beginning operational parameter negotiation; "
737 "waiting for Login PDU");
738 request = login_receive(&conn->conn, false);
739 skipped_security = false;
741 skipped_security = true;
744 * RFC 3720, 10.13.5. Status-Class and Status-Detail, says
745 * the redirection SHOULD be accepted by the initiator before
746 * authentication, but MUST be accepted afterwards; that's
747 * why we're doing it here and not earlier.
749 redirected = login_target_redirect(conn, request);
751 log_debugx("initiator redirected; exiting");
755 request_keys = keys_new();
756 keys_load_pdu(request_keys, request);
758 response = login_new_response(request);
759 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
760 bhslr2->bhslr_tsih = htons(0xbadd);
761 login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
762 login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE);
763 response_keys = keys_new();
765 if (skipped_security &&
766 conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
767 if (conn->conn_target->t_alias != NULL)
768 keys_add(response_keys,
769 "TargetAlias", conn->conn_target->t_alias);
770 keys_add_int(response_keys, "TargetPortalGroupTag",
771 conn->conn_portal->p_portal_group->pg_tag);
774 for (i = 0; i < KEYS_MAX; i++) {
775 if (request_keys->keys_names[i] == NULL)
778 login_negotiate_key(request, request_keys->keys_names[i],
779 request_keys->keys_values[i], skipped_security,
784 * We'd started with usable values at our end. But a bad initiator
785 * could have presented a large FirstBurstLength and then a smaller
786 * MaxBurstLength (in that order) and because we process the key/value
787 * pairs in the order they are in the request we might have ended up
788 * with illegal values here.
790 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL &&
791 conn->conn.conn_first_burst_length >
792 conn->conn.conn_max_burst_length) {
793 log_errx(1, "initiator sent FirstBurstLength > MaxBurstLength");
796 conn->conn.conn_max_recv_data_segment_length =
797 conn->conn_max_recv_data_segment_limit;
798 keys_add_int(response_keys, "MaxRecvDataSegmentLength",
799 conn->conn.conn_max_recv_data_segment_length);
801 log_debugx("operational parameter negotiation done; "
802 "transitioning to Full Feature Phase");
804 keys_save_pdu(response_keys, response);
806 pdu_delete(response);
807 keys_delete(response_keys);
809 keys_delete(request_keys);
813 login_wait_transition(struct ctld_connection *conn)
815 struct pdu *request, *response;
816 struct iscsi_bhs_login_request *bhslr;
818 log_debugx("waiting for state transition request");
819 request = login_receive(&conn->conn, false);
820 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
821 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0) {
822 login_send_error(request, 0x02, 0x00);
823 log_errx(1, "got no \"T\" flag after answering AuthMethod");
826 log_debugx("got state transition request");
827 response = login_new_response(request);
829 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
831 pdu_delete(response);
833 login_negotiate(conn, NULL);
837 login(struct ctld_connection *conn)
839 struct pdu *request, *response;
840 struct iscsi_bhs_login_request *bhslr;
841 struct keys *request_keys, *response_keys;
842 struct auth_group *ag;
843 struct portal_group *pg;
844 const char *initiator_name, *initiator_alias, *session_type,
845 *target_name, *auth_method;
846 bool redirected, fail, trans;
849 * Handle the initial Login Request - figure out required authentication
850 * method and either transition to the next phase, if no authentication
851 * is required, or call appropriate authentication code.
853 log_debugx("beginning Login Phase; waiting for Login PDU");
854 request = login_receive(&conn->conn, true);
855 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
856 if (bhslr->bhslr_tsih != 0) {
857 login_send_error(request, 0x02, 0x0a);
858 log_errx(1, "received Login PDU with non-zero TSIH");
861 pg = conn->conn_portal->p_portal_group;
863 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid,
864 sizeof(conn->conn_initiator_isid));
867 * XXX: Implement the C flag some day.
869 request_keys = keys_new();
870 keys_load_pdu(request_keys, request);
872 assert(conn->conn_initiator_name == NULL);
873 initiator_name = keys_find(request_keys, "InitiatorName");
874 if (initiator_name == NULL) {
875 login_send_error(request, 0x02, 0x07);
876 log_errx(1, "received Login PDU without InitiatorName");
878 if (valid_iscsi_name(initiator_name) == false) {
879 login_send_error(request, 0x02, 0x00);
880 log_errx(1, "received Login PDU with invalid InitiatorName");
882 conn->conn_initiator_name = checked_strdup(initiator_name);
883 log_set_peer_name(conn->conn_initiator_name);
884 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name);
886 redirected = login_portal_redirect(conn, request);
888 log_debugx("initiator redirected; exiting");
892 initiator_alias = keys_find(request_keys, "InitiatorAlias");
893 if (initiator_alias != NULL)
894 conn->conn_initiator_alias = checked_strdup(initiator_alias);
896 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE);
897 session_type = keys_find(request_keys, "SessionType");
898 if (session_type != NULL) {
899 if (strcmp(session_type, "Normal") == 0) {
900 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
901 } else if (strcmp(session_type, "Discovery") == 0) {
902 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY;
904 login_send_error(request, 0x02, 0x00);
905 log_errx(1, "received Login PDU with invalid "
906 "SessionType \"%s\"", session_type);
909 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
911 assert(conn->conn_target == NULL);
912 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
913 target_name = keys_find(request_keys, "TargetName");
914 if (target_name == NULL) {
915 login_send_error(request, 0x02, 0x07);
916 log_errx(1, "received Login PDU without TargetName");
919 conn->conn_port = port_find_in_pg(pg, target_name);
920 if (conn->conn_port == NULL) {
921 login_send_error(request, 0x02, 0x03);
922 log_errx(1, "requested target \"%s\" not found",
925 conn->conn_target = conn->conn_port->p_target;
929 * At this point we know what kind of authentication we need.
931 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
932 ag = conn->conn_port->p_auth_group;
934 ag = conn->conn_target->t_auth_group;
935 if (ag->ag_name != NULL) {
936 log_debugx("initiator requests to connect "
937 "to target \"%s\"; auth-group \"%s\"",
938 conn->conn_target->t_name,
941 log_debugx("initiator requests to connect "
942 "to target \"%s\"", conn->conn_target->t_name);
945 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
946 ag = pg->pg_discovery_auth_group;
947 if (ag->ag_name != NULL) {
948 log_debugx("initiator requests "
949 "discovery session; auth-group \"%s\"", ag->ag_name);
951 log_debugx("initiator requests discovery session");
955 if (ag->ag_type == AG_TYPE_DENY) {
956 login_send_error(request, 0x02, 0x01);
957 log_errx(1, "auth-type is \"deny\"");
960 if (ag->ag_type == AG_TYPE_UNKNOWN) {
962 * This can happen with empty auth-group.
964 login_send_error(request, 0x02, 0x01);
965 log_errx(1, "auth-type not set, denying access");
969 * Enforce initiator-name and initiator-portal.
971 if (auth_name_check(ag, initiator_name) != 0) {
972 login_send_error(request, 0x02, 0x02);
973 log_errx(1, "initiator does not match allowed initiator names");
976 if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
977 login_send_error(request, 0x02, 0x02);
978 log_errx(1, "initiator does not match allowed "
979 "initiator portals");
983 * Let's see if the initiator intends to do any kind of authentication
986 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
987 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
988 login_send_error(request, 0x02, 0x01);
989 log_errx(1, "initiator skipped the authentication, "
990 "but authentication is required");
993 keys_delete(request_keys);
995 log_debugx("initiator skipped the authentication, "
996 "and we don't need it; proceeding with negotiation");
997 login_negotiate(conn, request);
1002 response = login_new_response(request);
1003 response_keys = keys_new();
1004 trans = (bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0;
1005 auth_method = keys_find(request_keys, "AuthMethod");
1006 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) {
1007 log_debugx("authentication not required");
1008 if (auth_method == NULL ||
1009 login_list_contains(auth_method, "None")) {
1010 keys_add(response_keys, "AuthMethod", "None");
1012 log_warnx("initiator requests "
1013 "AuthMethod \"%s\" instead of \"None\"",
1015 keys_add(response_keys, "AuthMethod", "Reject");
1018 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
1020 log_debugx("CHAP authentication required");
1021 if (auth_method == NULL ||
1022 login_list_contains(auth_method, "CHAP")) {
1023 keys_add(response_keys, "AuthMethod", "CHAP");
1025 log_warnx("initiator requests unsupported "
1026 "AuthMethod \"%s\" instead of \"CHAP\"",
1028 keys_add(response_keys, "AuthMethod", "Reject");
1032 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1033 if (conn->conn_target->t_alias != NULL)
1034 keys_add(response_keys,
1035 "TargetAlias", conn->conn_target->t_alias);
1036 keys_add_int(response_keys,
1037 "TargetPortalGroupTag", pg->pg_tag);
1039 keys_save_pdu(response_keys, response);
1042 pdu_delete(response);
1043 keys_delete(response_keys);
1044 pdu_delete(request);
1045 keys_delete(request_keys);
1048 log_debugx("sent reject for AuthMethod; exiting");
1052 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
1053 login_chap(conn, ag);
1054 login_negotiate(conn, NULL);
1056 login_negotiate(conn, NULL);
1058 login_wait_transition(conn);