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 (ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
130 login_send_error(request, 0x02, 0x05);
131 log_errx(1, "received Login PDU with decreasing CmdSN: "
132 "was %u, is %u", conn->conn_cmdsn,
133 ntohl(bhslr->bhslr_cmdsn));
135 if (initial == false &&
136 ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
137 login_send_error(request, 0x02, 0x05);
138 log_errx(1, "received Login PDU with wrong ExpStatSN: "
139 "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
142 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
148 login_new_response(struct pdu *request)
150 struct pdu *response;
151 struct connection *conn;
152 struct iscsi_bhs_login_request *bhslr;
153 struct iscsi_bhs_login_response *bhslr2;
155 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
156 conn = request->pdu_connection;
158 response = pdu_new_response(request);
159 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
160 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_RESPONSE;
161 login_set_csg(response, BHSLR_STAGE_SECURITY_NEGOTIATION);
162 memcpy(bhslr2->bhslr_isid,
163 bhslr->bhslr_isid, sizeof(bhslr2->bhslr_isid));
164 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
165 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
166 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
167 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
173 login_send_error(struct pdu *request, char class, char detail)
175 struct pdu *response;
176 struct iscsi_bhs_login_response *bhslr2;
178 log_debugx("sending Login Response PDU with failure class 0x%x/0x%x; "
179 "see next line for reason", class, detail);
180 response = login_new_response(request);
181 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
182 bhslr2->bhslr_status_class = class;
183 bhslr2->bhslr_status_detail = detail;
186 pdu_delete(response);
190 login_list_contains(const char *list, const char *what)
192 char *tofree, *str, *token;
194 tofree = str = checked_strdup(list);
196 while ((token = strsep(&str, ",")) != NULL) {
197 if (strcmp(token, what) == 0) {
207 login_list_prefers(const char *list,
208 const char *choice1, const char *choice2)
210 char *tofree, *str, *token;
212 tofree = str = checked_strdup(list);
214 while ((token = strsep(&str, ",")) != NULL) {
215 if (strcmp(token, choice1) == 0) {
219 if (strcmp(token, choice2) == 0) {
229 login_receive_chap_a(struct connection *conn)
232 struct keys *request_keys;
235 request = login_receive(conn, false);
236 request_keys = keys_new();
237 keys_load(request_keys, request);
239 chap_a = keys_find(request_keys, "CHAP_A");
240 if (chap_a == NULL) {
241 login_send_error(request, 0x02, 0x07);
242 log_errx(1, "received CHAP Login PDU without CHAP_A");
244 if (login_list_contains(chap_a, "5") == 0) {
245 login_send_error(request, 0x02, 0x01);
246 log_errx(1, "received CHAP Login PDU with unsupported CHAP_A "
249 keys_delete(request_keys);
255 login_send_chap_c(struct pdu *request, struct chap *chap)
257 struct pdu *response;
258 struct keys *response_keys;
259 char *chap_c, *chap_i;
261 chap_c = chap_get_challenge(chap);
262 chap_i = chap_get_id(chap);
264 response = login_new_response(request);
265 response_keys = keys_new();
266 keys_add(response_keys, "CHAP_A", "5");
267 keys_add(response_keys, "CHAP_I", chap_i);
268 keys_add(response_keys, "CHAP_C", chap_c);
271 keys_save(response_keys, response);
273 pdu_delete(response);
274 keys_delete(response_keys);
278 login_receive_chap_r(struct connection *conn, struct auth_group *ag,
279 struct chap *chap, const struct auth **authp)
282 struct keys *request_keys;
283 const char *chap_n, *chap_r;
284 const struct auth *auth;
287 request = login_receive(conn, false);
288 request_keys = keys_new();
289 keys_load(request_keys, request);
291 chap_n = keys_find(request_keys, "CHAP_N");
292 if (chap_n == NULL) {
293 login_send_error(request, 0x02, 0x07);
294 log_errx(1, "received CHAP Login PDU without CHAP_N");
296 chap_r = keys_find(request_keys, "CHAP_R");
297 if (chap_r == NULL) {
298 login_send_error(request, 0x02, 0x07);
299 log_errx(1, "received CHAP Login PDU without CHAP_R");
301 error = chap_receive(chap, chap_r);
303 login_send_error(request, 0x02, 0x07);
304 log_errx(1, "received CHAP Login PDU with malformed CHAP_R");
308 * Verify the response.
310 assert(ag->ag_type == AG_TYPE_CHAP ||
311 ag->ag_type == AG_TYPE_CHAP_MUTUAL);
312 auth = auth_find(ag, chap_n);
314 login_send_error(request, 0x02, 0x01);
315 log_errx(1, "received CHAP Login with invalid user \"%s\"",
319 assert(auth->a_secret != NULL);
320 assert(strlen(auth->a_secret) > 0);
322 error = chap_authenticate(chap, auth->a_secret);
324 login_send_error(request, 0x02, 0x01);
325 log_errx(1, "CHAP authentication failed for user \"%s\"",
329 keys_delete(request_keys);
336 login_send_chap_success(struct pdu *request,
337 const struct auth *auth)
339 struct pdu *response;
340 struct keys *request_keys, *response_keys;
342 const char *chap_i, *chap_c;
346 response = login_new_response(request);
347 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
350 * Actually, one more thing: mutual authentication.
352 request_keys = keys_new();
353 keys_load(request_keys, request);
354 chap_i = keys_find(request_keys, "CHAP_I");
355 chap_c = keys_find(request_keys, "CHAP_C");
356 if (chap_i != NULL || chap_c != NULL) {
357 if (chap_i == NULL) {
358 login_send_error(request, 0x02, 0x07);
359 log_errx(1, "initiator requested target "
360 "authentication, but didn't send CHAP_I");
362 if (chap_c == NULL) {
363 login_send_error(request, 0x02, 0x07);
364 log_errx(1, "initiator requested target "
365 "authentication, but didn't send CHAP_C");
367 if (auth->a_auth_group->ag_type != AG_TYPE_CHAP_MUTUAL) {
368 login_send_error(request, 0x02, 0x01);
369 log_errx(1, "initiator requests target authentication "
370 "for user \"%s\", but mutual user/secret "
371 "is not set", auth->a_user);
374 log_debugx("performing mutual authentication as user \"%s\"",
375 auth->a_mutual_user);
377 rchap = rchap_new(auth->a_mutual_secret);
378 error = rchap_receive(rchap, chap_i, chap_c);
380 login_send_error(request, 0x02, 0x07);
381 log_errx(1, "received CHAP Login PDU with malformed "
384 chap_r = rchap_get_response(rchap);
386 response_keys = keys_new();
387 keys_add(response_keys, "CHAP_N", auth->a_mutual_user);
388 keys_add(response_keys, "CHAP_R", chap_r);
390 keys_save(response_keys, response);
391 keys_delete(response_keys);
393 log_debugx("initiator did not request target authentication");
396 keys_delete(request_keys);
398 pdu_delete(response);
402 login_chap(struct connection *conn, struct auth_group *ag)
404 const struct auth *auth;
409 * Receive CHAP_A PDU.
411 log_debugx("beginning CHAP authentication; waiting for CHAP_A");
412 request = login_receive_chap_a(conn);
415 * Generate the challenge.
420 * Send the challenge.
422 log_debugx("sending CHAP_C, binary challenge size is %zd bytes",
423 sizeof(chap->chap_challenge));
424 login_send_chap_c(request, chap);
428 * Receive CHAP_N/CHAP_R PDU and authenticate.
430 log_debugx("waiting for CHAP_N/CHAP_R");
431 request = login_receive_chap_r(conn, ag, chap, &auth);
434 * Yay, authentication succeeded!
436 log_debugx("authentication succeeded for user \"%s\"; "
437 "transitioning to Negotiation Phase", auth->a_user);
438 login_send_chap_success(request, auth);
442 * Leave username and CHAP information for discovery().
444 conn->conn_user = auth->a_user;
445 conn->conn_chap = chap;
449 login_negotiate_key(struct pdu *request, const char *name,
450 const char *value, bool skipped_security, struct keys *response_keys)
454 struct connection *conn;
456 conn = request->pdu_connection;
458 if (strcmp(name, "InitiatorName") == 0) {
459 if (!skipped_security)
460 log_errx(1, "initiator resent InitiatorName");
461 } else if (strcmp(name, "SessionType") == 0) {
462 if (!skipped_security)
463 log_errx(1, "initiator resent SessionType");
464 } else if (strcmp(name, "TargetName") == 0) {
465 if (!skipped_security)
466 log_errx(1, "initiator resent TargetName");
467 } else if (strcmp(name, "InitiatorAlias") == 0) {
468 if (conn->conn_initiator_alias != NULL)
469 free(conn->conn_initiator_alias);
470 conn->conn_initiator_alias = checked_strdup(value);
471 } else if (strcmp(value, "Irrelevant") == 0) {
473 } else if (strcmp(name, "HeaderDigest") == 0) {
475 * We don't handle digests for discovery sessions.
477 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
478 log_debugx("discovery session; digests disabled");
479 keys_add(response_keys, name, "None");
483 which = login_list_prefers(value, "CRC32C", "None");
486 log_debugx("initiator prefers CRC32C "
487 "for header digest; we'll use it");
488 conn->conn_header_digest = CONN_DIGEST_CRC32C;
489 keys_add(response_keys, name, "CRC32C");
492 log_debugx("initiator prefers not to do "
493 "header digest; we'll comply");
494 keys_add(response_keys, name, "None");
497 log_warnx("initiator sent unrecognized "
498 "HeaderDigest value \"%s\"; will use None", value);
499 keys_add(response_keys, name, "None");
502 } else if (strcmp(name, "DataDigest") == 0) {
503 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
504 log_debugx("discovery session; digests disabled");
505 keys_add(response_keys, name, "None");
509 which = login_list_prefers(value, "CRC32C", "None");
512 log_debugx("initiator prefers CRC32C "
513 "for data digest; we'll use it");
514 conn->conn_data_digest = CONN_DIGEST_CRC32C;
515 keys_add(response_keys, name, "CRC32C");
518 log_debugx("initiator prefers not to do "
519 "data digest; we'll comply");
520 keys_add(response_keys, name, "None");
523 log_warnx("initiator sent unrecognized "
524 "DataDigest value \"%s\"; will use None", value);
525 keys_add(response_keys, name, "None");
528 } else if (strcmp(name, "MaxConnections") == 0) {
529 keys_add(response_keys, name, "1");
530 } else if (strcmp(name, "InitialR2T") == 0) {
531 keys_add(response_keys, name, "Yes");
532 } else if (strcmp(name, "ImmediateData") == 0) {
533 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
534 log_debugx("discovery session; ImmediateData irrelevant");
535 keys_add(response_keys, name, "Irrelevant");
537 if (strcmp(value, "Yes") == 0) {
538 conn->conn_immediate_data = true;
539 keys_add(response_keys, name, "Yes");
541 conn->conn_immediate_data = false;
542 keys_add(response_keys, name, "No");
545 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
546 tmp = strtoul(value, NULL, 10);
548 login_send_error(request, 0x02, 0x00);
549 log_errx(1, "received invalid "
550 "MaxRecvDataSegmentLength");
552 if (tmp > conn->conn_data_segment_limit) {
553 log_debugx("capping MaxRecvDataSegmentLength "
554 "from %zd to %zd", tmp, conn->conn_data_segment_limit);
555 tmp = conn->conn_data_segment_limit;
557 conn->conn_max_data_segment_length = tmp;
558 keys_add_int(response_keys, name, conn->conn_data_segment_limit);
559 } else if (strcmp(name, "MaxBurstLength") == 0) {
560 tmp = strtoul(value, NULL, 10);
562 login_send_error(request, 0x02, 0x00);
563 log_errx(1, "received invalid MaxBurstLength");
565 if (tmp > MAX_BURST_LENGTH) {
566 log_debugx("capping MaxBurstLength from %zd to %d",
567 tmp, MAX_BURST_LENGTH);
568 tmp = MAX_BURST_LENGTH;
570 conn->conn_max_burst_length = tmp;
571 keys_add(response_keys, name, value);
572 } else if (strcmp(name, "FirstBurstLength") == 0) {
573 tmp = strtoul(value, NULL, 10);
575 login_send_error(request, 0x02, 0x00);
576 log_errx(1, "received invalid "
579 if (tmp > conn->conn_data_segment_limit) {
580 log_debugx("capping FirstBurstLength from %zd to %zd",
581 tmp, conn->conn_data_segment_limit);
582 tmp = conn->conn_data_segment_limit;
585 * We don't pass the value to the kernel; it only enforces
586 * hardcoded limit anyway.
588 keys_add_int(response_keys, name, tmp);
589 } else if (strcmp(name, "DefaultTime2Wait") == 0) {
590 keys_add(response_keys, name, value);
591 } else if (strcmp(name, "DefaultTime2Retain") == 0) {
592 keys_add(response_keys, name, "0");
593 } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
594 keys_add(response_keys, name, "1");
595 } else if (strcmp(name, "DataPDUInOrder") == 0) {
596 keys_add(response_keys, name, "Yes");
597 } else if (strcmp(name, "DataSequenceInOrder") == 0) {
598 keys_add(response_keys, name, "Yes");
599 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
600 keys_add(response_keys, name, "0");
601 } else if (strcmp(name, "OFMarker") == 0) {
602 keys_add(response_keys, name, "No");
603 } else if (strcmp(name, "IFMarker") == 0) {
604 keys_add(response_keys, name, "No");
605 } else if (strcmp(name, "iSCSIProtocolLevel") == 0) {
606 tmp = strtoul(value, NULL, 10);
609 keys_add_int(response_keys, name, tmp);
611 log_debugx("unknown key \"%s\"; responding "
612 "with NotUnderstood", name);
613 keys_add(response_keys, name, "NotUnderstood");
618 login_redirect(struct pdu *request, const char *target_address)
620 struct pdu *response;
621 struct iscsi_bhs_login_response *bhslr2;
622 struct keys *response_keys;
624 response = login_new_response(request);
625 login_set_csg(response, login_csg(request));
626 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
627 bhslr2->bhslr_status_class = 0x01;
628 bhslr2->bhslr_status_detail = 0x01;
630 response_keys = keys_new();
631 keys_add(response_keys, "TargetAddress", target_address);
633 keys_save(response_keys, response);
635 pdu_delete(response);
636 keys_delete(response_keys);
640 login_portal_redirect(struct connection *conn, struct pdu *request)
642 const struct portal_group *pg;
644 pg = conn->conn_portal->p_portal_group;
645 if (pg->pg_redirection == NULL)
648 log_debugx("portal-group \"%s\" configured to redirect to %s",
649 pg->pg_name, pg->pg_redirection);
650 login_redirect(request, pg->pg_redirection);
656 login_target_redirect(struct connection *conn, struct pdu *request)
658 const char *target_address;
660 assert(conn->conn_portal->p_portal_group->pg_redirection == NULL);
662 if (conn->conn_target == NULL)
665 target_address = conn->conn_target->t_redirection;
666 if (target_address == NULL)
669 log_debugx("target \"%s\" configured to redirect to %s",
670 conn->conn_target->t_name, target_address);
671 login_redirect(request, target_address);
677 login_negotiate(struct connection *conn, struct pdu *request)
679 struct pdu *response;
680 struct iscsi_bhs_login_response *bhslr2;
681 struct keys *request_keys, *response_keys;
683 bool redirected, skipped_security;
685 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
687 * Query the kernel for MaxDataSegmentLength it can handle.
688 * In case of offload, it depends on hardware capabilities.
690 assert(conn->conn_target != NULL);
691 kernel_limits(conn->conn_portal->p_portal_group->pg_offload,
692 &conn->conn_data_segment_limit);
694 conn->conn_data_segment_limit = MAX_DATA_SEGMENT_LENGTH;
697 if (request == NULL) {
698 log_debugx("beginning operational parameter negotiation; "
699 "waiting for Login PDU");
700 request = login_receive(conn, false);
701 skipped_security = false;
703 skipped_security = true;
706 * RFC 3720, 10.13.5. Status-Class and Status-Detail, says
707 * the redirection SHOULD be accepted by the initiator before
708 * authentication, but MUST be be accepted afterwards; that's
709 * why we're doing it here and not earlier.
711 redirected = login_target_redirect(conn, request);
713 log_debugx("initiator redirected; exiting");
717 request_keys = keys_new();
718 keys_load(request_keys, request);
720 response = login_new_response(request);
721 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
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_wait_transition(struct connection *conn)
759 struct pdu *request, *response;
760 struct iscsi_bhs_login_request *bhslr;
762 log_debugx("waiting for state transition request");
763 request = login_receive(conn, false);
764 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
765 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0) {
766 login_send_error(request, 0x02, 0x00);
767 log_errx(1, "got no \"T\" flag after answering AuthMethod");
771 log_debugx("got state transition request");
772 response = login_new_response(request);
773 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
775 pdu_delete(response);
777 login_negotiate(conn, NULL);
781 login(struct connection *conn)
783 struct pdu *request, *response;
784 struct iscsi_bhs_login_request *bhslr;
785 struct keys *request_keys, *response_keys;
786 struct auth_group *ag;
787 struct portal_group *pg;
788 const char *initiator_name, *initiator_alias, *session_type,
789 *target_name, *auth_method;
790 bool redirected, fail, trans;
793 * Handle the initial Login Request - figure out required authentication
794 * method and either transition to the next phase, if no authentication
795 * is required, or call appropriate authentication code.
797 log_debugx("beginning Login Phase; waiting for Login PDU");
798 request = login_receive(conn, true);
799 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
800 if (bhslr->bhslr_tsih != 0) {
801 login_send_error(request, 0x02, 0x0a);
802 log_errx(1, "received Login PDU with non-zero TSIH");
805 pg = conn->conn_portal->p_portal_group;
807 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid,
808 sizeof(conn->conn_initiator_isid));
811 * XXX: Implement the C flag some day.
813 request_keys = keys_new();
814 keys_load(request_keys, request);
816 assert(conn->conn_initiator_name == NULL);
817 initiator_name = keys_find(request_keys, "InitiatorName");
818 if (initiator_name == NULL) {
819 login_send_error(request, 0x02, 0x07);
820 log_errx(1, "received Login PDU without InitiatorName");
822 if (valid_iscsi_name(initiator_name) == false) {
823 login_send_error(request, 0x02, 0x00);
824 log_errx(1, "received Login PDU with invalid InitiatorName");
826 conn->conn_initiator_name = checked_strdup(initiator_name);
827 log_set_peer_name(conn->conn_initiator_name);
828 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name);
830 redirected = login_portal_redirect(conn, request);
832 log_debugx("initiator redirected; exiting");
836 initiator_alias = keys_find(request_keys, "InitiatorAlias");
837 if (initiator_alias != NULL)
838 conn->conn_initiator_alias = checked_strdup(initiator_alias);
840 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE);
841 session_type = keys_find(request_keys, "SessionType");
842 if (session_type != NULL) {
843 if (strcmp(session_type, "Normal") == 0) {
844 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
845 } else if (strcmp(session_type, "Discovery") == 0) {
846 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY;
848 login_send_error(request, 0x02, 0x00);
849 log_errx(1, "received Login PDU with invalid "
850 "SessionType \"%s\"", session_type);
853 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
855 assert(conn->conn_target == NULL);
856 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
857 target_name = keys_find(request_keys, "TargetName");
858 if (target_name == NULL) {
859 login_send_error(request, 0x02, 0x07);
860 log_errx(1, "received Login PDU without TargetName");
863 conn->conn_port = port_find_in_pg(pg, target_name);
864 if (conn->conn_port == NULL) {
865 login_send_error(request, 0x02, 0x03);
866 log_errx(1, "requested target \"%s\" not found",
869 conn->conn_target = conn->conn_port->p_target;
873 * At this point we know what kind of authentication we need.
875 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
876 ag = conn->conn_port->p_auth_group;
878 ag = conn->conn_target->t_auth_group;
879 if (ag->ag_name != NULL) {
880 log_debugx("initiator requests to connect "
881 "to target \"%s\"; auth-group \"%s\"",
882 conn->conn_target->t_name,
885 log_debugx("initiator requests to connect "
886 "to target \"%s\"", conn->conn_target->t_name);
889 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
890 ag = pg->pg_discovery_auth_group;
891 if (ag->ag_name != NULL) {
892 log_debugx("initiator requests "
893 "discovery session; auth-group \"%s\"", ag->ag_name);
895 log_debugx("initiator requests discovery session");
899 if (ag->ag_type == AG_TYPE_DENY) {
900 login_send_error(request, 0x02, 0x01);
901 log_errx(1, "auth-type is \"deny\"");
904 if (ag->ag_type == AG_TYPE_UNKNOWN) {
906 * This can happen with empty auth-group.
908 login_send_error(request, 0x02, 0x01);
909 log_errx(1, "auth-type not set, denying access");
913 * Enforce initiator-name and initiator-portal.
915 if (auth_name_check(ag, initiator_name) != 0) {
916 login_send_error(request, 0x02, 0x02);
917 log_errx(1, "initiator does not match allowed initiator names");
920 if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
921 login_send_error(request, 0x02, 0x02);
922 log_errx(1, "initiator does not match allowed "
923 "initiator portals");
927 * Let's see if the initiator intends to do any kind of authentication
930 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
931 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
932 login_send_error(request, 0x02, 0x01);
933 log_errx(1, "initiator skipped the authentication, "
934 "but authentication is required");
937 keys_delete(request_keys);
939 log_debugx("initiator skipped the authentication, "
940 "and we don't need it; proceeding with negotiation");
941 login_negotiate(conn, request);
946 response = login_new_response(request);
947 response_keys = keys_new();
948 trans = (bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0;
949 auth_method = keys_find(request_keys, "AuthMethod");
950 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) {
951 log_debugx("authentication not required");
952 if (auth_method == NULL ||
953 login_list_contains(auth_method, "None")) {
954 keys_add(response_keys, "AuthMethod", "None");
956 log_warnx("initiator requests "
957 "AuthMethod \"%s\" instead of \"None\"",
959 keys_add(response_keys, "AuthMethod", "Reject");
962 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
964 log_debugx("CHAP authentication required");
965 if (auth_method == NULL ||
966 login_list_contains(auth_method, "CHAP")) {
967 keys_add(response_keys, "AuthMethod", "CHAP");
969 log_warnx("initiator requests unsupported "
970 "AuthMethod \"%s\" instead of \"CHAP\"",
972 keys_add(response_keys, "AuthMethod", "Reject");
976 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
977 if (conn->conn_target->t_alias != NULL)
978 keys_add(response_keys,
979 "TargetAlias", conn->conn_target->t_alias);
980 keys_add_int(response_keys,
981 "TargetPortalGroupTag", pg->pg_tag);
983 keys_save(response_keys, response);
986 pdu_delete(response);
987 keys_delete(response_keys);
989 keys_delete(request_keys);
992 log_debugx("sent reject for AuthMethod; exiting");
996 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
997 login_chap(conn, ag);
998 login_negotiate(conn, NULL);
1000 login_negotiate(conn, NULL);
1002 login_wait_transition(conn);