2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
41 #include <netinet/in.h>
44 #include "iscsi_proto.h"
46 static void login_send_error(struct pdu *request,
47 char class, char detail);
50 login_set_nsg(struct pdu *response, int nsg)
52 struct iscsi_bhs_login_response *bhslr;
54 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
55 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
56 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
58 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
60 bhslr->bhslr_flags &= 0xFC;
61 bhslr->bhslr_flags |= nsg;
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 (ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
131 login_send_error(request, 0x02, 0x05);
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, 0x05);
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;
342 struct iscsi_bhs_login_response *bhslr2;
344 const char *chap_i, *chap_c;
348 response = login_new_response(request);
349 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
350 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
351 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
354 * Actually, one more thing: mutual authentication.
356 request_keys = keys_new();
357 keys_load(request_keys, request);
358 chap_i = keys_find(request_keys, "CHAP_I");
359 chap_c = keys_find(request_keys, "CHAP_C");
360 if (chap_i != NULL || chap_c != NULL) {
361 if (chap_i == NULL) {
362 login_send_error(request, 0x02, 0x07);
363 log_errx(1, "initiator requested target "
364 "authentication, but didn't send CHAP_I");
366 if (chap_c == NULL) {
367 login_send_error(request, 0x02, 0x07);
368 log_errx(1, "initiator requested target "
369 "authentication, but didn't send CHAP_C");
371 if (auth->a_auth_group->ag_type != AG_TYPE_CHAP_MUTUAL) {
372 login_send_error(request, 0x02, 0x01);
373 log_errx(1, "initiator requests target authentication "
374 "for user \"%s\", but mutual user/secret "
375 "is not set", auth->a_user);
378 log_debugx("performing mutual authentication as user \"%s\"",
379 auth->a_mutual_user);
381 rchap = rchap_new(auth->a_mutual_secret);
382 error = rchap_receive(rchap, chap_i, chap_c);
384 login_send_error(request, 0x02, 0x07);
385 log_errx(1, "received CHAP Login PDU with malformed "
388 chap_r = rchap_get_response(rchap);
390 response_keys = keys_new();
391 keys_add(response_keys, "CHAP_N", auth->a_mutual_user);
392 keys_add(response_keys, "CHAP_R", chap_r);
394 keys_save(response_keys, response);
395 keys_delete(response_keys);
397 log_debugx("initiator did not request target authentication");
400 keys_delete(request_keys);
402 pdu_delete(response);
406 login_chap(struct connection *conn, struct auth_group *ag)
408 const struct auth *auth;
413 * Receive CHAP_A PDU.
415 log_debugx("beginning CHAP authentication; waiting for CHAP_A");
416 request = login_receive_chap_a(conn);
419 * Generate the challenge.
424 * Send the challenge.
426 log_debugx("sending CHAP_C, binary challenge size is %zd bytes",
427 sizeof(chap->chap_challenge));
428 login_send_chap_c(request, chap);
432 * Receive CHAP_N/CHAP_R PDU and authenticate.
434 log_debugx("waiting for CHAP_N/CHAP_R");
435 request = login_receive_chap_r(conn, ag, chap, &auth);
438 * Yay, authentication succeeded!
440 log_debugx("authentication succeeded for user \"%s\"; "
441 "transitioning to Negotiation Phase", auth->a_user);
442 login_send_chap_success(request, auth);
446 * Leave username and CHAP information for discovery().
448 conn->conn_user = auth->a_user;
449 conn->conn_chap = chap;
453 login_negotiate_key(struct pdu *request, const char *name,
454 const char *value, bool skipped_security, struct keys *response_keys)
458 struct connection *conn;
460 conn = request->pdu_connection;
462 if (strcmp(name, "InitiatorName") == 0) {
463 if (!skipped_security)
464 log_errx(1, "initiator resent InitiatorName");
465 } else if (strcmp(name, "SessionType") == 0) {
466 if (!skipped_security)
467 log_errx(1, "initiator resent SessionType");
468 } else if (strcmp(name, "TargetName") == 0) {
469 if (!skipped_security)
470 log_errx(1, "initiator resent TargetName");
471 } else if (strcmp(name, "InitiatorAlias") == 0) {
472 if (conn->conn_initiator_alias != NULL)
473 free(conn->conn_initiator_alias);
474 conn->conn_initiator_alias = checked_strdup(value);
475 } else if (strcmp(value, "Irrelevant") == 0) {
477 } else if (strcmp(name, "HeaderDigest") == 0) {
479 * We don't handle digests for discovery sessions.
481 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
482 log_debugx("discovery session; digests disabled");
483 keys_add(response_keys, name, "None");
487 which = login_list_prefers(value, "CRC32C", "None");
490 log_debugx("initiator prefers CRC32C "
491 "for header digest; we'll use it");
492 conn->conn_header_digest = CONN_DIGEST_CRC32C;
493 keys_add(response_keys, name, "CRC32C");
496 log_debugx("initiator prefers not to do "
497 "header digest; we'll comply");
498 keys_add(response_keys, name, "None");
501 log_warnx("initiator sent unrecognized "
502 "HeaderDigest value \"%s\"; will use None", value);
503 keys_add(response_keys, name, "None");
506 } else if (strcmp(name, "DataDigest") == 0) {
507 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
508 log_debugx("discovery session; digests disabled");
509 keys_add(response_keys, name, "None");
513 which = login_list_prefers(value, "CRC32C", "None");
516 log_debugx("initiator prefers CRC32C "
517 "for data digest; we'll use it");
518 conn->conn_data_digest = CONN_DIGEST_CRC32C;
519 keys_add(response_keys, name, "CRC32C");
522 log_debugx("initiator prefers not to do "
523 "data digest; we'll comply");
524 keys_add(response_keys, name, "None");
527 log_warnx("initiator sent unrecognized "
528 "DataDigest value \"%s\"; will use None", value);
529 keys_add(response_keys, name, "None");
532 } else if (strcmp(name, "MaxConnections") == 0) {
533 keys_add(response_keys, name, "1");
534 } else if (strcmp(name, "InitialR2T") == 0) {
535 keys_add(response_keys, name, "Yes");
536 } else if (strcmp(name, "ImmediateData") == 0) {
537 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
538 log_debugx("discovery session; ImmediateData irrelevant");
539 keys_add(response_keys, name, "Irrelevant");
541 if (strcmp(value, "Yes") == 0) {
542 conn->conn_immediate_data = true;
543 keys_add(response_keys, name, "Yes");
545 conn->conn_immediate_data = false;
546 keys_add(response_keys, name, "No");
549 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
550 tmp = strtoul(value, NULL, 10);
552 login_send_error(request, 0x02, 0x00);
553 log_errx(1, "received invalid "
554 "MaxRecvDataSegmentLength");
556 if (tmp > conn->conn_data_segment_limit) {
557 log_debugx("capping MaxRecvDataSegmentLength "
558 "from %zd to %zd", tmp, conn->conn_data_segment_limit);
559 tmp = conn->conn_data_segment_limit;
561 conn->conn_max_data_segment_length = tmp;
562 keys_add_int(response_keys, name, tmp);
563 } else if (strcmp(name, "MaxBurstLength") == 0) {
564 tmp = strtoul(value, NULL, 10);
566 login_send_error(request, 0x02, 0x00);
567 log_errx(1, "received invalid MaxBurstLength");
569 if (tmp > MAX_BURST_LENGTH) {
570 log_debugx("capping MaxBurstLength from %zd to %d",
571 tmp, MAX_BURST_LENGTH);
572 tmp = MAX_BURST_LENGTH;
574 conn->conn_max_burst_length = tmp;
575 keys_add(response_keys, name, value);
576 } else if (strcmp(name, "FirstBurstLength") == 0) {
577 tmp = strtoul(value, NULL, 10);
579 login_send_error(request, 0x02, 0x00);
580 log_errx(1, "received invalid "
583 if (tmp > conn->conn_data_segment_limit) {
584 log_debugx("capping FirstBurstLength from %zd to %zd",
585 tmp, conn->conn_data_segment_limit);
586 tmp = conn->conn_data_segment_limit;
589 * We don't pass the value to the kernel; it only enforces
590 * hardcoded limit anyway.
592 keys_add_int(response_keys, name, tmp);
593 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
594 keys_add(response_keys, name, value);
595 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
596 keys_add(response_keys, name, "0");
597 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
598 keys_add(response_keys, name, "1");
599 } else if (strcmp(name, "DataPDUInOrder") == 0) {
600 keys_add(response_keys, name, "Yes");
601 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
602 keys_add(response_keys, name, "Yes");
603 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
604 keys_add(response_keys, name, "0");
605 } else if (strcmp(name, "OFMarker") == 0) {
606 keys_add(response_keys, name, "No");
607 } else if (strcmp(name, "IFMarker") == 0) {
608 keys_add(response_keys, name, "No");
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_target->t_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_flags |= BHSLR_FLAGS_TRANSIT;
722 bhslr2->bhslr_tsih = htons(0xbadd);
723 login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
724 login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE);
725 response_keys = keys_new();
727 if (skipped_security &&
728 conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
729 if (conn->conn_target->t_alias != NULL)
730 keys_add(response_keys,
731 "TargetAlias", conn->conn_target->t_alias);
732 keys_add_int(response_keys, "TargetPortalGroupTag",
733 conn->conn_portal->p_portal_group->pg_tag);
736 for (i = 0; i < KEYS_MAX; i++) {
737 if (request_keys->keys_names[i] == NULL)
740 login_negotiate_key(request, request_keys->keys_names[i],
741 request_keys->keys_values[i], skipped_security,
745 log_debugx("operational parameter negotiation done; "
746 "transitioning to Full Feature Phase");
748 keys_save(response_keys, response);
750 pdu_delete(response);
751 keys_delete(response_keys);
753 keys_delete(request_keys);
757 login(struct connection *conn)
759 struct pdu *request, *response;
760 struct iscsi_bhs_login_request *bhslr;
761 struct iscsi_bhs_login_response *bhslr2;
762 struct keys *request_keys, *response_keys;
763 struct auth_group *ag;
764 struct portal_group *pg;
765 const char *initiator_name, *initiator_alias, *session_type,
766 *target_name, *auth_method;
770 * Handle the initial Login Request - figure out required authentication
771 * method and either transition to the next phase, if no authentication
772 * is required, or call appropriate authentication code.
774 log_debugx("beginning Login Phase; waiting for Login PDU");
775 request = login_receive(conn, true);
776 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
777 if (bhslr->bhslr_tsih != 0) {
778 login_send_error(request, 0x02, 0x0a);
779 log_errx(1, "received Login PDU with non-zero TSIH");
782 pg = conn->conn_portal->p_portal_group;
784 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid,
785 sizeof(conn->conn_initiator_isid));
788 * XXX: Implement the C flag some day.
790 request_keys = keys_new();
791 keys_load(request_keys, request);
793 assert(conn->conn_initiator_name == NULL);
794 initiator_name = keys_find(request_keys, "InitiatorName");
795 if (initiator_name == NULL) {
796 login_send_error(request, 0x02, 0x07);
797 log_errx(1, "received Login PDU without InitiatorName");
799 if (valid_iscsi_name(initiator_name) == false) {
800 login_send_error(request, 0x02, 0x00);
801 log_errx(1, "received Login PDU with invalid InitiatorName");
803 conn->conn_initiator_name = checked_strdup(initiator_name);
804 log_set_peer_name(conn->conn_initiator_name);
806 * XXX: This doesn't work (does nothing) because of Capsicum.
808 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name);
810 redirected = login_portal_redirect(conn, request);
812 log_debugx("initiator redirected; exiting");
816 initiator_alias = keys_find(request_keys, "InitiatorAlias");
817 if (initiator_alias != NULL)
818 conn->conn_initiator_alias = checked_strdup(initiator_alias);
820 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE);
821 session_type = keys_find(request_keys, "SessionType");
822 if (session_type != NULL) {
823 if (strcmp(session_type, "Normal") == 0) {
824 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
825 } else if (strcmp(session_type, "Discovery") == 0) {
826 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY;
828 login_send_error(request, 0x02, 0x00);
829 log_errx(1, "received Login PDU with invalid "
830 "SessionType \"%s\"", session_type);
833 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
835 assert(conn->conn_target == NULL);
836 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
837 target_name = keys_find(request_keys, "TargetName");
838 if (target_name == NULL) {
839 login_send_error(request, 0x02, 0x07);
840 log_errx(1, "received Login PDU without TargetName");
843 conn->conn_port = port_find_in_pg(pg, target_name);
844 if (conn->conn_port == NULL) {
845 login_send_error(request, 0x02, 0x03);
846 log_errx(1, "requested target \"%s\" not found",
849 conn->conn_target = conn->conn_port->p_target;
853 * At this point we know what kind of authentication we need.
855 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
856 ag = conn->conn_port->p_auth_group;
858 ag = conn->conn_target->t_auth_group;
859 if (ag->ag_name != NULL) {
860 log_debugx("initiator requests to connect "
861 "to target \"%s\"; auth-group \"%s\"",
862 conn->conn_target->t_name,
865 log_debugx("initiator requests to connect "
866 "to target \"%s\"", conn->conn_target->t_name);
869 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
870 ag = pg->pg_discovery_auth_group;
871 if (ag->ag_name != NULL) {
872 log_debugx("initiator requests "
873 "discovery session; auth-group \"%s\"", ag->ag_name);
875 log_debugx("initiator requests discovery session");
880 * Enforce initiator-name and initiator-portal.
882 if (auth_name_check(ag, initiator_name) != 0) {
883 login_send_error(request, 0x02, 0x02);
884 log_errx(1, "initiator does not match allowed initiator names");
887 if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
888 login_send_error(request, 0x02, 0x02);
889 log_errx(1, "initiator does not match allowed "
890 "initiator portals");
894 * Let's see if the initiator intends to do any kind of authentication
897 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
898 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
899 login_send_error(request, 0x02, 0x01);
900 log_errx(1, "initiator skipped the authentication, "
901 "but authentication is required");
904 keys_delete(request_keys);
906 log_debugx("initiator skipped the authentication, "
907 "and we don't need it; proceeding with negotiation");
908 login_negotiate(conn, request);
912 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) {
914 * Initiator might want to to authenticate,
915 * but we don't need it.
917 log_debugx("authentication not required; "
918 "transitioning to operational parameter negotiation");
920 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0)
921 log_warnx("initiator did not set the \"T\" flag; "
922 "transitioning anyway");
924 response = login_new_response(request);
925 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
926 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
927 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
928 response_keys = keys_new();
930 * Required by Linux initiator.
932 auth_method = keys_find(request_keys, "AuthMethod");
933 if (auth_method != NULL &&
934 login_list_contains(auth_method, "None"))
935 keys_add(response_keys, "AuthMethod", "None");
937 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
938 if (conn->conn_target->t_alias != NULL)
939 keys_add(response_keys,
940 "TargetAlias", conn->conn_target->t_alias);
941 keys_add_int(response_keys,
942 "TargetPortalGroupTag", pg->pg_tag);
944 keys_save(response_keys, response);
946 pdu_delete(response);
947 keys_delete(response_keys);
949 keys_delete(request_keys);
951 login_negotiate(conn, NULL);
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");
968 log_debugx("CHAP authentication required");
970 auth_method = keys_find(request_keys, "AuthMethod");
971 if (auth_method == NULL) {
972 login_send_error(request, 0x02, 0x07);
973 log_errx(1, "received Login PDU without AuthMethod");
976 * XXX: This should be Reject, not just a login failure (5.3.2).
978 if (login_list_contains(auth_method, "CHAP") == 0) {
979 login_send_error(request, 0x02, 0x01);
980 log_errx(1, "initiator requests unsupported AuthMethod \"%s\" "
981 "instead of \"CHAP\"", auth_method);
984 response = login_new_response(request);
986 response_keys = keys_new();
987 keys_add(response_keys, "AuthMethod", "CHAP");
988 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
989 if (conn->conn_target->t_alias != NULL)
990 keys_add(response_keys,
991 "TargetAlias", conn->conn_target->t_alias);
992 keys_add_int(response_keys,
993 "TargetPortalGroupTag", pg->pg_tag);
995 keys_save(response_keys, response);
998 pdu_delete(response);
999 keys_delete(response_keys);
1000 pdu_delete(request);
1001 keys_delete(request_keys);
1003 login_chap(conn, ag);
1005 login_negotiate(conn, NULL);