2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
39 #include <netinet/in.h>
42 #include "iscsi_proto.h"
44 static void login_send_error(struct pdu *request,
45 char class, char detail);
48 login_set_nsg(struct pdu *response, int nsg)
50 struct iscsi_bhs_login_response *bhslr;
52 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
53 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
54 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
56 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
58 bhslr->bhslr_flags &= 0xFC;
59 bhslr->bhslr_flags |= nsg;
60 bhslr->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
64 login_csg(const struct pdu *request)
66 struct iscsi_bhs_login_request *bhslr;
68 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
70 return ((bhslr->bhslr_flags & 0x0C) >> 2);
74 login_set_csg(struct pdu *response, int csg)
76 struct iscsi_bhs_login_response *bhslr;
78 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
79 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
80 csg == BHSLR_STAGE_FULL_FEATURE_PHASE);
82 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
84 bhslr->bhslr_flags &= 0xF3;
85 bhslr->bhslr_flags |= csg << 2;
89 login_receive(struct connection *conn, bool initial)
92 struct iscsi_bhs_login_request *bhslr;
94 request = pdu_new(conn);
96 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
97 ISCSI_BHS_OPCODE_LOGIN_REQUEST) {
99 * The first PDU in session is special - if we receive any PDU
100 * different than login request, we have to drop the connection
101 * without sending response ("A target receiving any PDU
102 * except a Login request before the Login Phase is started MUST
103 * immediately terminate the connection on which the PDU
106 if (initial == false)
107 login_send_error(request, 0x02, 0x0b);
108 log_errx(1, "protocol error: received invalid opcode 0x%x",
109 request->pdu_bhs->bhs_opcode);
111 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
113 * XXX: Implement the C flag some day.
115 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0) {
116 login_send_error(request, 0x03, 0x00);
117 log_errx(1, "received Login PDU with unsupported \"C\" flag");
119 if (bhslr->bhslr_version_max != 0x00) {
120 login_send_error(request, 0x02, 0x05);
121 log_errx(1, "received Login PDU with unsupported "
122 "Version-max 0x%x", bhslr->bhslr_version_max);
124 if (bhslr->bhslr_version_min != 0x00) {
125 login_send_error(request, 0x02, 0x05);
126 log_errx(1, "received Login PDU with unsupported "
127 "Version-min 0x%x", bhslr->bhslr_version_min);
129 if (initial == false &&
130 ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
131 login_send_error(request, 0x02, 0x00);
132 log_errx(1, "received Login PDU with decreasing CmdSN: "
133 "was %u, is %u", conn->conn_cmdsn,
134 ntohl(bhslr->bhslr_cmdsn));
136 if (initial == false &&
137 ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
138 login_send_error(request, 0x02, 0x00);
139 log_errx(1, "received Login PDU with wrong ExpStatSN: "
140 "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
143 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
149 login_new_response(struct pdu *request)
151 struct pdu *response;
152 struct connection *conn;
153 struct iscsi_bhs_login_request *bhslr;
154 struct iscsi_bhs_login_response *bhslr2;
156 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
157 conn = request->pdu_connection;
159 response = pdu_new_response(request);
160 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
161 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_RESPONSE;
162 login_set_csg(response, BHSLR_STAGE_SECURITY_NEGOTIATION);
163 memcpy(bhslr2->bhslr_isid,
164 bhslr->bhslr_isid, sizeof(bhslr2->bhslr_isid));
165 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
166 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
167 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
168 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
174 login_send_error(struct pdu *request, char class, char detail)
176 struct pdu *response;
177 struct iscsi_bhs_login_response *bhslr2;
179 log_debugx("sending Login Response PDU with failure class 0x%x/0x%x; "
180 "see next line for reason", class, detail);
181 response = login_new_response(request);
182 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
183 bhslr2->bhslr_status_class = class;
184 bhslr2->bhslr_status_detail = detail;
187 pdu_delete(response);
191 login_list_contains(const char *list, const char *what)
193 char *tofree, *str, *token;
195 tofree = str = checked_strdup(list);
197 while ((token = strsep(&str, ",")) != NULL) {
198 if (strcmp(token, what) == 0) {
208 login_list_prefers(const char *list,
209 const char *choice1, const char *choice2)
211 char *tofree, *str, *token;
213 tofree = str = checked_strdup(list);
215 while ((token = strsep(&str, ",")) != NULL) {
216 if (strcmp(token, choice1) == 0) {
220 if (strcmp(token, choice2) == 0) {
230 login_receive_chap_a(struct connection *conn)
233 struct keys *request_keys;
236 request = login_receive(conn, false);
237 request_keys = keys_new();
238 keys_load(request_keys, request);
240 chap_a = keys_find(request_keys, "CHAP_A");
241 if (chap_a == NULL) {
242 login_send_error(request, 0x02, 0x07);
243 log_errx(1, "received CHAP Login PDU without CHAP_A");
245 if (login_list_contains(chap_a, "5") == 0) {
246 login_send_error(request, 0x02, 0x01);
247 log_errx(1, "received CHAP Login PDU with unsupported CHAP_A "
250 keys_delete(request_keys);
256 login_send_chap_c(struct pdu *request, struct chap *chap)
258 struct pdu *response;
259 struct keys *response_keys;
260 char *chap_c, *chap_i;
262 chap_c = chap_get_challenge(chap);
263 chap_i = chap_get_id(chap);
265 response = login_new_response(request);
266 response_keys = keys_new();
267 keys_add(response_keys, "CHAP_A", "5");
268 keys_add(response_keys, "CHAP_I", chap_i);
269 keys_add(response_keys, "CHAP_C", chap_c);
272 keys_save(response_keys, response);
274 pdu_delete(response);
275 keys_delete(response_keys);
279 login_receive_chap_r(struct connection *conn, struct auth_group *ag,
280 struct chap *chap, const struct auth **authp)
283 struct keys *request_keys;
284 const char *chap_n, *chap_r;
285 const struct auth *auth;
288 request = login_receive(conn, false);
289 request_keys = keys_new();
290 keys_load(request_keys, request);
292 chap_n = keys_find(request_keys, "CHAP_N");
293 if (chap_n == NULL) {
294 login_send_error(request, 0x02, 0x07);
295 log_errx(1, "received CHAP Login PDU without CHAP_N");
297 chap_r = keys_find(request_keys, "CHAP_R");
298 if (chap_r == NULL) {
299 login_send_error(request, 0x02, 0x07);
300 log_errx(1, "received CHAP Login PDU without CHAP_R");
302 error = chap_receive(chap, chap_r);
304 login_send_error(request, 0x02, 0x07);
305 log_errx(1, "received CHAP Login PDU with malformed CHAP_R");
309 * Verify the response.
311 assert(ag->ag_type == AG_TYPE_CHAP ||
312 ag->ag_type == AG_TYPE_CHAP_MUTUAL);
313 auth = auth_find(ag, chap_n);
315 login_send_error(request, 0x02, 0x01);
316 log_errx(1, "received CHAP Login with invalid user \"%s\"",
320 assert(auth->a_secret != NULL);
321 assert(strlen(auth->a_secret) > 0);
323 error = chap_authenticate(chap, auth->a_secret);
325 login_send_error(request, 0x02, 0x01);
326 log_errx(1, "CHAP authentication failed for user \"%s\"",
330 keys_delete(request_keys);
337 login_send_chap_success(struct pdu *request,
338 const struct auth *auth)
340 struct pdu *response;
341 struct keys *request_keys, *response_keys;
343 const char *chap_i, *chap_c;
347 response = login_new_response(request);
348 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
351 * Actually, one more thing: mutual authentication.
353 request_keys = keys_new();
354 keys_load(request_keys, request);
355 chap_i = keys_find(request_keys, "CHAP_I");
356 chap_c = keys_find(request_keys, "CHAP_C");
357 if (chap_i != NULL || chap_c != NULL) {
358 if (chap_i == NULL) {
359 login_send_error(request, 0x02, 0x07);
360 log_errx(1, "initiator requested target "
361 "authentication, but didn't send CHAP_I");
363 if (chap_c == NULL) {
364 login_send_error(request, 0x02, 0x07);
365 log_errx(1, "initiator requested target "
366 "authentication, but didn't send CHAP_C");
368 if (auth->a_auth_group->ag_type != AG_TYPE_CHAP_MUTUAL) {
369 login_send_error(request, 0x02, 0x01);
370 log_errx(1, "initiator requests target authentication "
371 "for user \"%s\", but mutual user/secret "
372 "is not set", auth->a_user);
375 log_debugx("performing mutual authentication as user \"%s\"",
376 auth->a_mutual_user);
378 rchap = rchap_new(auth->a_mutual_secret);
379 error = rchap_receive(rchap, chap_i, chap_c);
381 login_send_error(request, 0x02, 0x07);
382 log_errx(1, "received CHAP Login PDU with malformed "
385 chap_r = rchap_get_response(rchap);
387 response_keys = keys_new();
388 keys_add(response_keys, "CHAP_N", auth->a_mutual_user);
389 keys_add(response_keys, "CHAP_R", chap_r);
391 keys_save(response_keys, response);
392 keys_delete(response_keys);
394 log_debugx("initiator did not request target authentication");
397 keys_delete(request_keys);
399 pdu_delete(response);
403 login_chap(struct connection *conn, struct auth_group *ag)
405 const struct auth *auth;
410 * Receive CHAP_A PDU.
412 log_debugx("beginning CHAP authentication; waiting for CHAP_A");
413 request = login_receive_chap_a(conn);
416 * Generate the challenge.
421 * Send the challenge.
423 log_debugx("sending CHAP_C, binary challenge size is %zd bytes",
424 sizeof(chap->chap_challenge));
425 login_send_chap_c(request, chap);
429 * Receive CHAP_N/CHAP_R PDU and authenticate.
431 log_debugx("waiting for CHAP_N/CHAP_R");
432 request = login_receive_chap_r(conn, ag, chap, &auth);
435 * Yay, authentication succeeded!
437 log_debugx("authentication succeeded for user \"%s\"; "
438 "transitioning to operational parameter negotiation", auth->a_user);
439 login_send_chap_success(request, auth);
443 * Leave username and CHAP information for discovery().
445 conn->conn_user = auth->a_user;
446 conn->conn_chap = chap;
450 login_negotiate_key(struct pdu *request, const char *name,
451 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");
555 * MaxRecvDataSegmentLength is a direction-specific parameter.
556 * We'll limit our _send_ to what the initiator can handle but
557 * our MaxRecvDataSegmentLength is not influenced by the
558 * initiator in any way.
560 if ((int)tmp > conn->conn_max_send_data_segment_limit) {
561 log_debugx("capping MaxRecvDataSegmentLength "
562 "from %zd to %d", tmp,
563 conn->conn_max_send_data_segment_limit);
564 tmp = conn->conn_max_send_data_segment_limit;
566 conn->conn_max_send_data_segment_length = tmp;
567 conn->conn_max_recv_data_segment_length =
568 conn->conn_max_recv_data_segment_limit;
569 keys_add_int(response_keys, name,
570 conn->conn_max_recv_data_segment_length);
571 } else if (strcmp(name, "MaxBurstLength") == 0) {
572 tmp = strtoul(value, NULL, 10);
574 login_send_error(request, 0x02, 0x00);
575 log_errx(1, "received invalid MaxBurstLength");
577 if ((int)tmp > conn->conn_max_burst_limit) {
578 log_debugx("capping MaxBurstLength from %zd to %d",
579 tmp, conn->conn_max_burst_limit);
580 tmp = conn->conn_max_burst_limit;
582 conn->conn_max_burst_length = tmp;
583 keys_add_int(response_keys, name, tmp);
584 } else if (strcmp(name, "FirstBurstLength") == 0) {
585 tmp = strtoul(value, NULL, 10);
587 login_send_error(request, 0x02, 0x00);
588 log_errx(1, "received invalid FirstBurstLength");
590 if ((int)tmp > conn->conn_first_burst_limit) {
591 log_debugx("capping FirstBurstLength from %zd to %d",
592 tmp, conn->conn_first_burst_limit);
593 tmp = conn->conn_first_burst_limit;
595 conn->conn_first_burst_length = tmp;
596 keys_add_int(response_keys, name, tmp);
597 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
598 keys_add(response_keys, name, value);
599 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
600 keys_add(response_keys, name, "0");
601 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
602 keys_add(response_keys, name, "1");
603 } else if (strcmp(name, "DataPDUInOrder") == 0) {
604 keys_add(response_keys, name, "Yes");
605 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
606 keys_add(response_keys, name, "Yes");
607 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
608 keys_add(response_keys, name, "0");
609 } else if (strcmp(name, "OFMarker") == 0) {
610 keys_add(response_keys, name, "No");
611 } else if (strcmp(name, "IFMarker") == 0) {
612 keys_add(response_keys, name, "No");
613 } else if (strcmp(name, "iSCSIProtocolLevel") == 0) {
614 tmp = strtoul(value, NULL, 10);
617 keys_add_int(response_keys, name, tmp);
619 log_debugx("unknown key \"%s\"; responding "
620 "with NotUnderstood", name);
621 keys_add(response_keys, name, "NotUnderstood");
626 login_redirect(struct pdu *request, const char *target_address)
628 struct pdu *response;
629 struct iscsi_bhs_login_response *bhslr2;
630 struct keys *response_keys;
632 response = login_new_response(request);
633 login_set_csg(response, login_csg(request));
634 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
635 bhslr2->bhslr_status_class = 0x01;
636 bhslr2->bhslr_status_detail = 0x01;
638 response_keys = keys_new();
639 keys_add(response_keys, "TargetAddress", target_address);
641 keys_save(response_keys, response);
643 pdu_delete(response);
644 keys_delete(response_keys);
648 login_portal_redirect(struct connection *conn, struct pdu *request)
650 const struct portal_group *pg;
652 pg = conn->conn_portal->p_portal_group;
653 if (pg->pg_redirection == NULL)
656 log_debugx("portal-group \"%s\" configured to redirect to %s",
657 pg->pg_name, pg->pg_redirection);
658 login_redirect(request, pg->pg_redirection);
664 login_target_redirect(struct connection *conn, struct pdu *request)
666 const char *target_address;
668 assert(conn->conn_portal->p_portal_group->pg_redirection == NULL);
670 if (conn->conn_target == NULL)
673 target_address = conn->conn_target->t_redirection;
674 if (target_address == NULL)
677 log_debugx("target \"%s\" configured to redirect to %s",
678 conn->conn_target->t_name, target_address);
679 login_redirect(request, target_address);
685 login_negotiate(struct connection *conn, struct pdu *request)
687 struct pdu *response;
688 struct iscsi_bhs_login_response *bhslr2;
689 struct keys *request_keys, *response_keys;
691 bool redirected, skipped_security;
693 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
695 * Query the kernel for various size limits. In case of
696 * offload, it depends on hardware capabilities.
698 assert(conn->conn_target != NULL);
699 conn->conn_max_recv_data_segment_limit = (1 << 24) - 1;
700 conn->conn_max_send_data_segment_limit = (1 << 24) - 1;
701 conn->conn_max_burst_limit = (1 << 24) - 1;
702 conn->conn_first_burst_limit = (1 << 24) - 1;
703 kernel_limits(conn->conn_portal->p_portal_group->pg_offload,
704 &conn->conn_max_recv_data_segment_limit,
705 &conn->conn_max_send_data_segment_limit,
706 &conn->conn_max_burst_limit,
707 &conn->conn_first_burst_limit);
709 /* We expect legal, usable values at this point. */
710 assert(conn->conn_max_recv_data_segment_limit >= 512);
711 assert(conn->conn_max_recv_data_segment_limit < (1 << 24));
712 assert(conn->conn_max_send_data_segment_limit >= 512);
713 assert(conn->conn_max_send_data_segment_limit < (1 << 24));
714 assert(conn->conn_max_burst_limit >= 512);
715 assert(conn->conn_max_burst_limit < (1 << 24));
716 assert(conn->conn_first_burst_limit >= 512);
717 assert(conn->conn_first_burst_limit < (1 << 24));
718 assert(conn->conn_first_burst_limit <=
719 conn->conn_max_burst_limit);
722 * Limit default send length in case it won't be negotiated.
723 * We can't do it for other limits, since they may affect both
724 * sender and receiver operation, and we must obey defaults.
726 if (conn->conn_max_send_data_segment_limit <
727 conn->conn_max_send_data_segment_length) {
728 conn->conn_max_send_data_segment_length =
729 conn->conn_max_send_data_segment_limit;
732 conn->conn_max_recv_data_segment_limit =
733 MAX_DATA_SEGMENT_LENGTH;
734 conn->conn_max_send_data_segment_limit =
735 MAX_DATA_SEGMENT_LENGTH;
738 if (request == NULL) {
739 log_debugx("beginning operational parameter negotiation; "
740 "waiting for Login PDU");
741 request = login_receive(conn, false);
742 skipped_security = false;
744 skipped_security = true;
747 * RFC 3720, 10.13.5. Status-Class and Status-Detail, says
748 * the redirection SHOULD be accepted by the initiator before
749 * authentication, but MUST be be accepted afterwards; that's
750 * why we're doing it here and not earlier.
752 redirected = login_target_redirect(conn, request);
754 log_debugx("initiator redirected; exiting");
758 request_keys = keys_new();
759 keys_load(request_keys, request);
761 response = login_new_response(request);
762 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
763 bhslr2->bhslr_tsih = htons(0xbadd);
764 login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
765 login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE);
766 response_keys = keys_new();
768 if (skipped_security &&
769 conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
770 if (conn->conn_target->t_alias != NULL)
771 keys_add(response_keys,
772 "TargetAlias", conn->conn_target->t_alias);
773 keys_add_int(response_keys, "TargetPortalGroupTag",
774 conn->conn_portal->p_portal_group->pg_tag);
777 for (i = 0; i < KEYS_MAX; i++) {
778 if (request_keys->keys_names[i] == NULL)
781 login_negotiate_key(request, request_keys->keys_names[i],
782 request_keys->keys_values[i], skipped_security,
787 * We'd started with usable values at our end. But a bad initiator
788 * could have presented a large FirstBurstLength and then a smaller
789 * MaxBurstLength (in that order) and because we process the key/value
790 * pairs in the order they are in the request we might have ended up
791 * with illegal values here.
793 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL &&
794 conn->conn_first_burst_length > conn->conn_max_burst_length) {
795 log_errx(1, "initiator sent FirstBurstLength > MaxBurstLength");
798 log_debugx("operational parameter negotiation done; "
799 "transitioning to Full Feature Phase");
801 keys_save(response_keys, response);
803 pdu_delete(response);
804 keys_delete(response_keys);
806 keys_delete(request_keys);
810 login_wait_transition(struct connection *conn)
812 struct pdu *request, *response;
813 struct iscsi_bhs_login_request *bhslr;
815 log_debugx("waiting for state transition request");
816 request = login_receive(conn, false);
817 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
818 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0) {
819 login_send_error(request, 0x02, 0x00);
820 log_errx(1, "got no \"T\" flag after answering AuthMethod");
823 log_debugx("got state transition request");
824 response = login_new_response(request);
826 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
828 pdu_delete(response);
830 login_negotiate(conn, NULL);
834 login(struct connection *conn)
836 struct pdu *request, *response;
837 struct iscsi_bhs_login_request *bhslr;
838 struct keys *request_keys, *response_keys;
839 struct auth_group *ag;
840 struct portal_group *pg;
841 const char *initiator_name, *initiator_alias, *session_type,
842 *target_name, *auth_method;
843 bool redirected, fail, trans;
846 * Handle the initial Login Request - figure out required authentication
847 * method and either transition to the next phase, if no authentication
848 * is required, or call appropriate authentication code.
850 log_debugx("beginning Login Phase; waiting for Login PDU");
851 request = login_receive(conn, true);
852 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
853 if (bhslr->bhslr_tsih != 0) {
854 login_send_error(request, 0x02, 0x0a);
855 log_errx(1, "received Login PDU with non-zero TSIH");
858 pg = conn->conn_portal->p_portal_group;
860 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid,
861 sizeof(conn->conn_initiator_isid));
864 * XXX: Implement the C flag some day.
866 request_keys = keys_new();
867 keys_load(request_keys, request);
869 assert(conn->conn_initiator_name == NULL);
870 initiator_name = keys_find(request_keys, "InitiatorName");
871 if (initiator_name == NULL) {
872 login_send_error(request, 0x02, 0x07);
873 log_errx(1, "received Login PDU without InitiatorName");
875 if (valid_iscsi_name(initiator_name) == false) {
876 login_send_error(request, 0x02, 0x00);
877 log_errx(1, "received Login PDU with invalid InitiatorName");
879 conn->conn_initiator_name = checked_strdup(initiator_name);
880 log_set_peer_name(conn->conn_initiator_name);
881 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name);
883 redirected = login_portal_redirect(conn, request);
885 log_debugx("initiator redirected; exiting");
889 initiator_alias = keys_find(request_keys, "InitiatorAlias");
890 if (initiator_alias != NULL)
891 conn->conn_initiator_alias = checked_strdup(initiator_alias);
893 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE);
894 session_type = keys_find(request_keys, "SessionType");
895 if (session_type != NULL) {
896 if (strcmp(session_type, "Normal") == 0) {
897 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
898 } else if (strcmp(session_type, "Discovery") == 0) {
899 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY;
901 login_send_error(request, 0x02, 0x00);
902 log_errx(1, "received Login PDU with invalid "
903 "SessionType \"%s\"", session_type);
906 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
908 assert(conn->conn_target == NULL);
909 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
910 target_name = keys_find(request_keys, "TargetName");
911 if (target_name == NULL) {
912 login_send_error(request, 0x02, 0x07);
913 log_errx(1, "received Login PDU without TargetName");
916 conn->conn_port = port_find_in_pg(pg, target_name);
917 if (conn->conn_port == NULL) {
918 login_send_error(request, 0x02, 0x03);
919 log_errx(1, "requested target \"%s\" not found",
922 conn->conn_target = conn->conn_port->p_target;
926 * At this point we know what kind of authentication we need.
928 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
929 ag = conn->conn_port->p_auth_group;
931 ag = conn->conn_target->t_auth_group;
932 if (ag->ag_name != NULL) {
933 log_debugx("initiator requests to connect "
934 "to target \"%s\"; auth-group \"%s\"",
935 conn->conn_target->t_name,
938 log_debugx("initiator requests to connect "
939 "to target \"%s\"", conn->conn_target->t_name);
942 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
943 ag = pg->pg_discovery_auth_group;
944 if (ag->ag_name != NULL) {
945 log_debugx("initiator requests "
946 "discovery session; auth-group \"%s\"", ag->ag_name);
948 log_debugx("initiator requests discovery session");
952 if (ag->ag_type == AG_TYPE_DENY) {
953 login_send_error(request, 0x02, 0x01);
954 log_errx(1, "auth-type is \"deny\"");
957 if (ag->ag_type == AG_TYPE_UNKNOWN) {
959 * This can happen with empty auth-group.
961 login_send_error(request, 0x02, 0x01);
962 log_errx(1, "auth-type not set, denying access");
966 * Enforce initiator-name and initiator-portal.
968 if (auth_name_check(ag, initiator_name) != 0) {
969 login_send_error(request, 0x02, 0x02);
970 log_errx(1, "initiator does not match allowed initiator names");
973 if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
974 login_send_error(request, 0x02, 0x02);
975 log_errx(1, "initiator does not match allowed "
976 "initiator portals");
980 * Let's see if the initiator intends to do any kind of authentication
983 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
984 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
985 login_send_error(request, 0x02, 0x01);
986 log_errx(1, "initiator skipped the authentication, "
987 "but authentication is required");
990 keys_delete(request_keys);
992 log_debugx("initiator skipped the authentication, "
993 "and we don't need it; proceeding with negotiation");
994 login_negotiate(conn, request);
999 response = login_new_response(request);
1000 response_keys = keys_new();
1001 trans = (bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0;
1002 auth_method = keys_find(request_keys, "AuthMethod");
1003 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) {
1004 log_debugx("authentication not required");
1005 if (auth_method == NULL ||
1006 login_list_contains(auth_method, "None")) {
1007 keys_add(response_keys, "AuthMethod", "None");
1009 log_warnx("initiator requests "
1010 "AuthMethod \"%s\" instead of \"None\"",
1012 keys_add(response_keys, "AuthMethod", "Reject");
1015 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
1017 log_debugx("CHAP authentication required");
1018 if (auth_method == NULL ||
1019 login_list_contains(auth_method, "CHAP")) {
1020 keys_add(response_keys, "AuthMethod", "CHAP");
1022 log_warnx("initiator requests unsupported "
1023 "AuthMethod \"%s\" instead of \"CHAP\"",
1025 keys_add(response_keys, "AuthMethod", "Reject");
1029 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1030 if (conn->conn_target->t_alias != NULL)
1031 keys_add(response_keys,
1032 "TargetAlias", conn->conn_target->t_alias);
1033 keys_add_int(response_keys,
1034 "TargetPortalGroupTag", pg->pg_tag);
1036 keys_save(response_keys, response);
1039 pdu_delete(response);
1040 keys_delete(response_keys);
1041 pdu_delete(request);
1042 keys_delete(request_keys);
1045 log_debugx("sent reject for AuthMethod; exiting");
1049 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
1050 login_chap(conn, ag);
1051 login_negotiate(conn, NULL);
1053 login_negotiate(conn, NULL);
1055 login_wait_transition(conn);