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");
557 * MaxRecvDataSegmentLength is a direction-specific parameter.
558 * We'll limit our _send_ to what the initiator can handle but
559 * our MaxRecvDataSegmentLength is not influenced by the
560 * initiator in any way.
562 if ((int)tmp > conn->conn_max_send_data_segment_limit) {
563 log_debugx("capping MaxRecvDataSegmentLength "
564 "from %zd to %d", tmp,
565 conn->conn_max_send_data_segment_limit);
566 tmp = conn->conn_max_send_data_segment_limit;
568 conn->conn_max_send_data_segment_length = tmp;
569 conn->conn_max_recv_data_segment_length =
570 conn->conn_max_recv_data_segment_limit;
571 keys_add_int(response_keys, name,
572 conn->conn_max_recv_data_segment_length);
573 } else if (strcmp(name, "MaxBurstLength") == 0) {
574 tmp = strtoul(value, NULL, 10);
576 login_send_error(request, 0x02, 0x00);
577 log_errx(1, "received invalid MaxBurstLength");
579 if ((int)tmp > conn->conn_max_burst_limit) {
580 log_debugx("capping MaxBurstLength from %zd to %d",
581 tmp, conn->conn_max_burst_limit);
582 tmp = conn->conn_max_burst_limit;
584 conn->conn_max_burst_length = tmp;
585 keys_add_int(response_keys, name, tmp);
586 } else if (strcmp(name, "FirstBurstLength") == 0) {
587 tmp = strtoul(value, NULL, 10);
589 login_send_error(request, 0x02, 0x00);
590 log_errx(1, "received invalid FirstBurstLength");
592 if ((int)tmp > conn->conn_first_burst_limit) {
593 log_debugx("capping FirstBurstLength from %zd to %d",
594 tmp, conn->conn_first_burst_limit);
595 tmp = conn->conn_first_burst_limit;
597 conn->conn_first_burst_length = tmp;
598 keys_add_int(response_keys, name, tmp);
599 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
600 keys_add(response_keys, name, value);
601 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
602 keys_add(response_keys, name, "0");
603 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
604 keys_add(response_keys, name, "1");
605 } else if (strcmp(name, "DataPDUInOrder") == 0) {
606 keys_add(response_keys, name, "Yes");
607 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
608 keys_add(response_keys, name, "Yes");
609 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
610 keys_add(response_keys, name, "0");
611 } else if (strcmp(name, "OFMarker") == 0) {
612 keys_add(response_keys, name, "No");
613 } else if (strcmp(name, "IFMarker") == 0) {
614 keys_add(response_keys, name, "No");
615 } else if (strcmp(name, "iSCSIProtocolLevel") == 0) {
616 tmp = strtoul(value, NULL, 10);
619 keys_add_int(response_keys, name, tmp);
621 log_debugx("unknown key \"%s\"; responding "
622 "with NotUnderstood", name);
623 keys_add(response_keys, name, "NotUnderstood");
628 login_redirect(struct pdu *request, const char *target_address)
630 struct pdu *response;
631 struct iscsi_bhs_login_response *bhslr2;
632 struct keys *response_keys;
634 response = login_new_response(request);
635 login_set_csg(response, login_csg(request));
636 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
637 bhslr2->bhslr_status_class = 0x01;
638 bhslr2->bhslr_status_detail = 0x01;
640 response_keys = keys_new();
641 keys_add(response_keys, "TargetAddress", target_address);
643 keys_save(response_keys, response);
645 pdu_delete(response);
646 keys_delete(response_keys);
650 login_portal_redirect(struct connection *conn, struct pdu *request)
652 const struct portal_group *pg;
654 pg = conn->conn_portal->p_portal_group;
655 if (pg->pg_redirection == NULL)
658 log_debugx("portal-group \"%s\" configured to redirect to %s",
659 pg->pg_name, pg->pg_redirection);
660 login_redirect(request, pg->pg_redirection);
666 login_target_redirect(struct connection *conn, struct pdu *request)
668 const char *target_address;
670 assert(conn->conn_portal->p_portal_group->pg_redirection == NULL);
672 if (conn->conn_target == NULL)
675 target_address = conn->conn_target->t_redirection;
676 if (target_address == NULL)
679 log_debugx("target \"%s\" configured to redirect to %s",
680 conn->conn_target->t_name, target_address);
681 login_redirect(request, target_address);
687 login_negotiate(struct connection *conn, struct pdu *request)
689 struct pdu *response;
690 struct iscsi_bhs_login_response *bhslr2;
691 struct keys *request_keys, *response_keys;
693 bool redirected, skipped_security;
695 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
697 * Query the kernel for various size limits. In case of
698 * offload, it depends on hardware capabilities.
700 assert(conn->conn_target != NULL);
701 conn->conn_max_recv_data_segment_limit = (1 << 24) - 1;
702 conn->conn_max_send_data_segment_limit = (1 << 24) - 1;
703 conn->conn_max_burst_limit = (1 << 24) - 1;
704 conn->conn_first_burst_limit = (1 << 24) - 1;
705 kernel_limits(conn->conn_portal->p_portal_group->pg_offload,
706 &conn->conn_max_recv_data_segment_limit,
707 &conn->conn_max_send_data_segment_limit,
708 &conn->conn_max_burst_limit,
709 &conn->conn_first_burst_limit);
711 /* We expect legal, usable values at this point. */
712 assert(conn->conn_max_recv_data_segment_limit >= 512);
713 assert(conn->conn_max_recv_data_segment_limit < (1 << 24));
714 assert(conn->conn_max_send_data_segment_limit >= 512);
715 assert(conn->conn_max_send_data_segment_limit < (1 << 24));
716 assert(conn->conn_max_burst_limit >= 512);
717 assert(conn->conn_max_burst_limit < (1 << 24));
718 assert(conn->conn_first_burst_limit >= 512);
719 assert(conn->conn_first_burst_limit < (1 << 24));
720 assert(conn->conn_first_burst_limit <=
721 conn->conn_max_burst_limit);
724 * Limit default send length in case it won't be negotiated.
725 * We can't do it for other limits, since they may affect both
726 * sender and receiver operation, and we must obey defaults.
728 if (conn->conn_max_send_data_segment_limit <
729 conn->conn_max_send_data_segment_length) {
730 conn->conn_max_send_data_segment_length =
731 conn->conn_max_send_data_segment_limit;
734 conn->conn_max_recv_data_segment_limit =
735 MAX_DATA_SEGMENT_LENGTH;
736 conn->conn_max_send_data_segment_limit =
737 MAX_DATA_SEGMENT_LENGTH;
740 if (request == NULL) {
741 log_debugx("beginning operational parameter negotiation; "
742 "waiting for Login PDU");
743 request = login_receive(conn, false);
744 skipped_security = false;
746 skipped_security = true;
749 * RFC 3720, 10.13.5. Status-Class and Status-Detail, says
750 * the redirection SHOULD be accepted by the initiator before
751 * authentication, but MUST be accepted afterwards; that's
752 * why we're doing it here and not earlier.
754 redirected = login_target_redirect(conn, request);
756 log_debugx("initiator redirected; exiting");
760 request_keys = keys_new();
761 keys_load(request_keys, request);
763 response = login_new_response(request);
764 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
765 bhslr2->bhslr_tsih = htons(0xbadd);
766 login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
767 login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE);
768 response_keys = keys_new();
770 if (skipped_security &&
771 conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
772 if (conn->conn_target->t_alias != NULL)
773 keys_add(response_keys,
774 "TargetAlias", conn->conn_target->t_alias);
775 keys_add_int(response_keys, "TargetPortalGroupTag",
776 conn->conn_portal->p_portal_group->pg_tag);
779 for (i = 0; i < KEYS_MAX; i++) {
780 if (request_keys->keys_names[i] == NULL)
783 login_negotiate_key(request, request_keys->keys_names[i],
784 request_keys->keys_values[i], skipped_security,
789 * We'd started with usable values at our end. But a bad initiator
790 * could have presented a large FirstBurstLength and then a smaller
791 * MaxBurstLength (in that order) and because we process the key/value
792 * pairs in the order they are in the request we might have ended up
793 * with illegal values here.
795 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL &&
796 conn->conn_first_burst_length > conn->conn_max_burst_length) {
797 log_errx(1, "initiator sent FirstBurstLength > MaxBurstLength");
800 log_debugx("operational parameter negotiation done; "
801 "transitioning to Full Feature Phase");
803 keys_save(response_keys, response);
805 pdu_delete(response);
806 keys_delete(response_keys);
808 keys_delete(request_keys);
812 login_wait_transition(struct connection *conn)
814 struct pdu *request, *response;
815 struct iscsi_bhs_login_request *bhslr;
817 log_debugx("waiting for state transition request");
818 request = login_receive(conn, false);
819 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
820 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0) {
821 login_send_error(request, 0x02, 0x00);
822 log_errx(1, "got no \"T\" flag after answering AuthMethod");
825 log_debugx("got state transition request");
826 response = login_new_response(request);
828 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
830 pdu_delete(response);
832 login_negotiate(conn, NULL);
836 login(struct connection *conn)
838 struct pdu *request, *response;
839 struct iscsi_bhs_login_request *bhslr;
840 struct keys *request_keys, *response_keys;
841 struct auth_group *ag;
842 struct portal_group *pg;
843 const char *initiator_name, *initiator_alias, *session_type,
844 *target_name, *auth_method;
845 bool redirected, fail, trans;
848 * Handle the initial Login Request - figure out required authentication
849 * method and either transition to the next phase, if no authentication
850 * is required, or call appropriate authentication code.
852 log_debugx("beginning Login Phase; waiting for Login PDU");
853 request = login_receive(conn, true);
854 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
855 if (bhslr->bhslr_tsih != 0) {
856 login_send_error(request, 0x02, 0x0a);
857 log_errx(1, "received Login PDU with non-zero TSIH");
860 pg = conn->conn_portal->p_portal_group;
862 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid,
863 sizeof(conn->conn_initiator_isid));
866 * XXX: Implement the C flag some day.
868 request_keys = keys_new();
869 keys_load(request_keys, request);
871 assert(conn->conn_initiator_name == NULL);
872 initiator_name = keys_find(request_keys, "InitiatorName");
873 if (initiator_name == NULL) {
874 login_send_error(request, 0x02, 0x07);
875 log_errx(1, "received Login PDU without InitiatorName");
877 if (valid_iscsi_name(initiator_name) == false) {
878 login_send_error(request, 0x02, 0x00);
879 log_errx(1, "received Login PDU with invalid InitiatorName");
881 conn->conn_initiator_name = checked_strdup(initiator_name);
882 log_set_peer_name(conn->conn_initiator_name);
883 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name);
885 redirected = login_portal_redirect(conn, request);
887 log_debugx("initiator redirected; exiting");
891 initiator_alias = keys_find(request_keys, "InitiatorAlias");
892 if (initiator_alias != NULL)
893 conn->conn_initiator_alias = checked_strdup(initiator_alias);
895 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE);
896 session_type = keys_find(request_keys, "SessionType");
897 if (session_type != NULL) {
898 if (strcmp(session_type, "Normal") == 0) {
899 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
900 } else if (strcmp(session_type, "Discovery") == 0) {
901 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY;
903 login_send_error(request, 0x02, 0x00);
904 log_errx(1, "received Login PDU with invalid "
905 "SessionType \"%s\"", session_type);
908 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
910 assert(conn->conn_target == NULL);
911 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
912 target_name = keys_find(request_keys, "TargetName");
913 if (target_name == NULL) {
914 login_send_error(request, 0x02, 0x07);
915 log_errx(1, "received Login PDU without TargetName");
918 conn->conn_port = port_find_in_pg(pg, target_name);
919 if (conn->conn_port == NULL) {
920 login_send_error(request, 0x02, 0x03);
921 log_errx(1, "requested target \"%s\" not found",
924 conn->conn_target = conn->conn_port->p_target;
928 * At this point we know what kind of authentication we need.
930 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
931 ag = conn->conn_port->p_auth_group;
933 ag = conn->conn_target->t_auth_group;
934 if (ag->ag_name != NULL) {
935 log_debugx("initiator requests to connect "
936 "to target \"%s\"; auth-group \"%s\"",
937 conn->conn_target->t_name,
940 log_debugx("initiator requests to connect "
941 "to target \"%s\"", conn->conn_target->t_name);
944 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
945 ag = pg->pg_discovery_auth_group;
946 if (ag->ag_name != NULL) {
947 log_debugx("initiator requests "
948 "discovery session; auth-group \"%s\"", ag->ag_name);
950 log_debugx("initiator requests discovery session");
954 if (ag->ag_type == AG_TYPE_DENY) {
955 login_send_error(request, 0x02, 0x01);
956 log_errx(1, "auth-type is \"deny\"");
959 if (ag->ag_type == AG_TYPE_UNKNOWN) {
961 * This can happen with empty auth-group.
963 login_send_error(request, 0x02, 0x01);
964 log_errx(1, "auth-type not set, denying access");
968 * Enforce initiator-name and initiator-portal.
970 if (auth_name_check(ag, initiator_name) != 0) {
971 login_send_error(request, 0x02, 0x02);
972 log_errx(1, "initiator does not match allowed initiator names");
975 if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
976 login_send_error(request, 0x02, 0x02);
977 log_errx(1, "initiator does not match allowed "
978 "initiator portals");
982 * Let's see if the initiator intends to do any kind of authentication
985 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
986 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
987 login_send_error(request, 0x02, 0x01);
988 log_errx(1, "initiator skipped the authentication, "
989 "but authentication is required");
992 keys_delete(request_keys);
994 log_debugx("initiator skipped the authentication, "
995 "and we don't need it; proceeding with negotiation");
996 login_negotiate(conn, request);
1001 response = login_new_response(request);
1002 response_keys = keys_new();
1003 trans = (bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0;
1004 auth_method = keys_find(request_keys, "AuthMethod");
1005 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) {
1006 log_debugx("authentication not required");
1007 if (auth_method == NULL ||
1008 login_list_contains(auth_method, "None")) {
1009 keys_add(response_keys, "AuthMethod", "None");
1011 log_warnx("initiator requests "
1012 "AuthMethod \"%s\" instead of \"None\"",
1014 keys_add(response_keys, "AuthMethod", "Reject");
1017 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
1019 log_debugx("CHAP authentication required");
1020 if (auth_method == NULL ||
1021 login_list_contains(auth_method, "CHAP")) {
1022 keys_add(response_keys, "AuthMethod", "CHAP");
1024 log_warnx("initiator requests unsupported "
1025 "AuthMethod \"%s\" instead of \"CHAP\"",
1027 keys_add(response_keys, "AuthMethod", "Reject");
1031 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1032 if (conn->conn_target->t_alias != NULL)
1033 keys_add(response_keys,
1034 "TargetAlias", conn->conn_target->t_alias);
1035 keys_add_int(response_keys,
1036 "TargetPortalGroupTag", pg->pg_tag);
1038 keys_save(response_keys, response);
1041 pdu_delete(response);
1042 keys_delete(response_keys);
1043 pdu_delete(request);
1044 keys_delete(request_keys);
1047 log_debugx("sent reject for AuthMethod; exiting");
1051 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
1052 login_chap(conn, ag);
1053 login_negotiate(conn, NULL);
1055 login_negotiate(conn, NULL);
1057 login_wait_transition(conn);