login.c revision 275259
1/*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 * 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: stable/10/usr.sbin/iscsid/login.c 275259 2014-11-29 16:07:15Z trasz $"); 33 34#include <sys/types.h> 35#include <sys/ioctl.h> 36#include <assert.h> 37#include <stdbool.h> 38#include <stdio.h> 39#include <stdlib.h> 40#include <string.h> 41#include <netinet/in.h> 42 43#include "iscsid.h" 44#include "iscsi_proto.h" 45 46static int 47login_nsg(const struct pdu *response) 48{ 49 struct iscsi_bhs_login_response *bhslr; 50 51 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; 52 53 return (bhslr->bhslr_flags & 0x03); 54} 55 56static void 57login_set_nsg(struct pdu *request, int nsg) 58{ 59 struct iscsi_bhs_login_request *bhslr; 60 61 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION || 62 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION || 63 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE); 64 65 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 66 67 bhslr->bhslr_flags &= 0xFC; 68 bhslr->bhslr_flags |= nsg; 69} 70 71static void 72login_set_csg(struct pdu *request, int csg) 73{ 74 struct iscsi_bhs_login_request *bhslr; 75 76 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION || 77 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION || 78 csg == BHSLR_STAGE_FULL_FEATURE_PHASE); 79 80 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 81 82 bhslr->bhslr_flags &= 0xF3; 83 bhslr->bhslr_flags |= csg << 2; 84} 85 86static const char * 87login_target_error_str(int class, int detail) 88{ 89 static char msg[128]; 90 91 /* 92 * RFC 3270, 10.13.5. Status-Class and Status-Detail 93 */ 94 switch (class) { 95 case 0x01: 96 switch (detail) { 97 case 0x01: 98 return ("Target moved temporarily"); 99 case 0x02: 100 return ("Target moved permanently"); 101 default: 102 snprintf(msg, sizeof(msg), "unknown redirection; " 103 "Status-Class 0x%x, Status-Detail 0x%x", 104 class, detail); 105 return (msg); 106 } 107 case 0x02: 108 switch (detail) { 109 case 0x00: 110 return ("Initiator error"); 111 case 0x01: 112 return ("Authentication failure"); 113 case 0x02: 114 return ("Authorization failure"); 115 case 0x03: 116 return ("Not found"); 117 case 0x04: 118 return ("Target removed"); 119 case 0x05: 120 return ("Unsupported version"); 121 case 0x06: 122 return ("Too many connections"); 123 case 0x07: 124 return ("Missing parameter"); 125 case 0x08: 126 return ("Can't include in session"); 127 case 0x09: 128 return ("Session type not supported"); 129 case 0x0a: 130 return ("Session does not exist"); 131 case 0x0b: 132 return ("Invalid during login"); 133 default: 134 snprintf(msg, sizeof(msg), "unknown initiator error; " 135 "Status-Class 0x%x, Status-Detail 0x%x", 136 class, detail); 137 return (msg); 138 } 139 case 0x03: 140 switch (detail) { 141 case 0x00: 142 return ("Target error"); 143 case 0x01: 144 return ("Service unavailable"); 145 case 0x02: 146 return ("Out of resources"); 147 default: 148 snprintf(msg, sizeof(msg), "unknown target error; " 149 "Status-Class 0x%x, Status-Detail 0x%x", 150 class, detail); 151 return (msg); 152 } 153 default: 154 snprintf(msg, sizeof(msg), "unknown error; " 155 "Status-Class 0x%x, Status-Detail 0x%x", 156 class, detail); 157 return (msg); 158 } 159} 160 161static void 162kernel_modify(const struct connection *conn, const char *target_address) 163{ 164 struct iscsi_session_modify ism; 165 int error; 166 167 memset(&ism, 0, sizeof(ism)); 168 ism.ism_session_id = conn->conn_session_id; 169 memcpy(&ism.ism_conf, &conn->conn_conf, sizeof(ism.ism_conf)); 170 strlcpy(ism.ism_conf.isc_target_addr, target_address, 171 sizeof(ism.ism_conf.isc_target)); 172 error = ioctl(conn->conn_iscsi_fd, ISCSISMODIFY, &ism); 173 if (error != 0) { 174 log_err(1, "failed to redirect to %s: ISCSISMODIFY", 175 target_address); 176 } 177} 178 179/* 180 * XXX: The way it works is suboptimal; what should happen is described 181 * in draft-gilligan-iscsi-fault-tolerance-00. That, however, would 182 * be much more complicated: we would need to keep "dependencies" 183 * for sessions, so that, in case described in draft and using draft 184 * terminology, we would have three sessions: one for discovery, 185 * one for initial target portal, and one for redirect portal. 186 * This would allow us to "backtrack" on connection failure, 187 * as described in draft. 188 */ 189static void 190login_handle_redirection(struct connection *conn, struct pdu *response) 191{ 192 struct iscsi_bhs_login_response *bhslr; 193 struct keys *response_keys; 194 const char *target_address; 195 196 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; 197 assert (bhslr->bhslr_status_class == 1); 198 199 response_keys = keys_new(); 200 keys_load(response_keys, response); 201 202 target_address = keys_find(response_keys, "TargetAddress"); 203 if (target_address == NULL) 204 log_errx(1, "received redirection without TargetAddress"); 205 if (target_address[0] == '\0') 206 log_errx(1, "received redirection with empty TargetAddress"); 207 if (strlen(target_address) >= 208 sizeof(conn->conn_conf.isc_target_addr) - 1) 209 log_errx(1, "received TargetAddress is too long"); 210 211 log_debugx("received redirection to \"%s\"", target_address); 212 kernel_modify(conn, target_address); 213 keys_delete(response_keys); 214} 215 216static struct pdu * 217login_receive(struct connection *conn) 218{ 219 struct pdu *response; 220 struct iscsi_bhs_login_response *bhslr; 221 const char *errorstr; 222 static bool initial = true; 223 224 response = pdu_new(conn); 225 pdu_receive(response); 226 if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGIN_RESPONSE) { 227 log_errx(1, "protocol error: received invalid opcode 0x%x", 228 response->pdu_bhs->bhs_opcode); 229 } 230 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; 231 /* 232 * XXX: Implement the C flag some day. 233 */ 234 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0) 235 log_errx(1, "received Login PDU with unsupported \"C\" flag"); 236 if (bhslr->bhslr_version_max != 0x00) 237 log_errx(1, "received Login PDU with unsupported " 238 "Version-max 0x%x", bhslr->bhslr_version_max); 239 if (bhslr->bhslr_version_active != 0x00) 240 log_errx(1, "received Login PDU with unsupported " 241 "Version-active 0x%x", bhslr->bhslr_version_active); 242 if (bhslr->bhslr_status_class == 1) { 243 login_handle_redirection(conn, response); 244 log_debugx("redirection handled; exiting"); 245 exit(0); 246 } 247 if (bhslr->bhslr_status_class != 0) { 248 errorstr = login_target_error_str(bhslr->bhslr_status_class, 249 bhslr->bhslr_status_detail); 250 fail(conn, errorstr); 251 log_errx(1, "target returned error: %s", errorstr); 252 } 253 if (initial == false && 254 ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) { 255 /* 256 * It's a warning, not an error, to work around what seems 257 * to be bug in NetBSD iSCSI target. 258 */ 259 log_warnx("received Login PDU with wrong StatSN: " 260 "is %d, should be %d", ntohl(bhslr->bhslr_statsn), 261 conn->conn_statsn + 1); 262 } 263 conn->conn_tsih = ntohs(bhslr->bhslr_tsih); 264 conn->conn_statsn = ntohl(bhslr->bhslr_statsn); 265 266 initial = false; 267 268 return (response); 269} 270 271static struct pdu * 272login_new_request(struct connection *conn, int csg) 273{ 274 struct pdu *request; 275 struct iscsi_bhs_login_request *bhslr; 276 int nsg; 277 278 request = pdu_new(conn); 279 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 280 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_REQUEST | 281 ISCSI_BHS_OPCODE_IMMEDIATE; 282 283 bhslr->bhslr_flags = BHSLR_FLAGS_TRANSIT; 284 switch (csg) { 285 case BHSLR_STAGE_SECURITY_NEGOTIATION: 286 nsg = BHSLR_STAGE_OPERATIONAL_NEGOTIATION; 287 break; 288 case BHSLR_STAGE_OPERATIONAL_NEGOTIATION: 289 nsg = BHSLR_STAGE_FULL_FEATURE_PHASE; 290 break; 291 default: 292 assert(!"invalid csg"); 293 log_errx(1, "invalid csg %d", csg); 294 } 295 login_set_csg(request, csg); 296 login_set_nsg(request, nsg); 297 298 memcpy(bhslr->bhslr_isid, &conn->conn_isid, sizeof(bhslr->bhslr_isid)); 299 bhslr->bhslr_tsih = htons(conn->conn_tsih); 300 bhslr->bhslr_initiator_task_tag = 0; 301 bhslr->bhslr_cmdsn = 0; 302 bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1); 303 304 return (request); 305} 306 307static int 308login_list_prefers(const char *list, 309 const char *choice1, const char *choice2) 310{ 311 char *tofree, *str, *token; 312 313 tofree = str = checked_strdup(list); 314 315 while ((token = strsep(&str, ",")) != NULL) { 316 if (strcmp(token, choice1) == 0) { 317 free(tofree); 318 return (1); 319 } 320 if (strcmp(token, choice2) == 0) { 321 free(tofree); 322 return (2); 323 } 324 } 325 free(tofree); 326 return (-1); 327} 328 329static void 330login_negotiate_key(struct connection *conn, const char *name, 331 const char *value) 332{ 333 int which, tmp; 334 335 if (strcmp(name, "TargetAlias") == 0) { 336 strlcpy(conn->conn_target_alias, value, 337 sizeof(conn->conn_target_alias)); 338 } else if (strcmp(value, "Irrelevant") == 0) { 339 /* Ignore. */ 340 } else if (strcmp(name, "HeaderDigest") == 0) { 341 which = login_list_prefers(value, "CRC32C", "None"); 342 switch (which) { 343 case 1: 344 log_debugx("target prefers CRC32C " 345 "for header digest; we'll use it"); 346 conn->conn_header_digest = CONN_DIGEST_CRC32C; 347 break; 348 case 2: 349 log_debugx("target prefers not to do " 350 "header digest; we'll comply"); 351 break; 352 default: 353 log_warnx("target sent unrecognized " 354 "HeaderDigest value \"%s\"; will use None", value); 355 break; 356 } 357 } else if (strcmp(name, "DataDigest") == 0) { 358 which = login_list_prefers(value, "CRC32C", "None"); 359 switch (which) { 360 case 1: 361 log_debugx("target prefers CRC32C " 362 "for data digest; we'll use it"); 363 conn->conn_data_digest = CONN_DIGEST_CRC32C; 364 break; 365 case 2: 366 log_debugx("target prefers not to do " 367 "data digest; we'll comply"); 368 break; 369 default: 370 log_warnx("target sent unrecognized " 371 "DataDigest value \"%s\"; will use None", value); 372 break; 373 } 374 } else if (strcmp(name, "MaxConnections") == 0) { 375 /* Ignore. */ 376 } else if (strcmp(name, "InitialR2T") == 0) { 377 if (strcmp(value, "Yes") == 0) 378 conn->conn_initial_r2t = true; 379 else 380 conn->conn_initial_r2t = false; 381 } else if (strcmp(name, "ImmediateData") == 0) { 382 if (strcmp(value, "Yes") == 0) 383 conn->conn_immediate_data = true; 384 else 385 conn->conn_immediate_data = false; 386 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) { 387 tmp = strtoul(value, NULL, 10); 388 if (tmp <= 0) 389 log_errx(1, "received invalid " 390 "MaxRecvDataSegmentLength"); 391 conn->conn_max_data_segment_length = tmp; 392 } else if (strcmp(name, "MaxBurstLength") == 0) { 393 if (conn->conn_immediate_data) { 394 tmp = strtoul(value, NULL, 10); 395 if (tmp <= 0) 396 log_errx(1, "received invalid MaxBurstLength"); 397 conn->conn_max_burst_length = tmp; 398 } 399 } else if (strcmp(name, "FirstBurstLength") == 0) { 400 tmp = strtoul(value, NULL, 10); 401 if (tmp <= 0) 402 log_errx(1, "received invalid FirstBurstLength"); 403 conn->conn_first_burst_length = tmp; 404 } else if (strcmp(name, "DefaultTime2Wait") == 0) { 405 /* Ignore */ 406 } else if (strcmp(name, "DefaultTime2Retain") == 0) { 407 /* Ignore */ 408 } else if (strcmp(name, "MaxOutstandingR2T") == 0) { 409 /* Ignore */ 410 } else if (strcmp(name, "DataPDUInOrder") == 0) { 411 /* Ignore */ 412 } else if (strcmp(name, "DataSequenceInOrder") == 0) { 413 /* Ignore */ 414 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) { 415 /* Ignore */ 416 } else if (strcmp(name, "OFMarker") == 0) { 417 /* Ignore */ 418 } else if (strcmp(name, "IFMarker") == 0) { 419 /* Ignore */ 420 } else if (strcmp(name, "TargetPortalGroupTag") == 0) { 421 /* Ignore */ 422 } else { 423 log_debugx("unknown key \"%s\"; ignoring", name); 424 } 425} 426 427static void 428login_negotiate(struct connection *conn) 429{ 430 struct pdu *request, *response; 431 struct keys *request_keys, *response_keys; 432 struct iscsi_bhs_login_response *bhslr; 433 int i, nrequests = 0; 434 435 log_debugx("beginning operational parameter negotiation"); 436 request = login_new_request(conn, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); 437 request_keys = keys_new(); 438 439 /* 440 * The following keys are irrelevant for discovery sessions. 441 */ 442 if (conn->conn_conf.isc_discovery == 0) { 443 if (conn->conn_conf.isc_header_digest != 0) 444 keys_add(request_keys, "HeaderDigest", "CRC32C"); 445 else 446 keys_add(request_keys, "HeaderDigest", "None"); 447 if (conn->conn_conf.isc_data_digest != 0) 448 keys_add(request_keys, "DataDigest", "CRC32C"); 449 else 450 keys_add(request_keys, "DataDigest", "None"); 451 452 keys_add(request_keys, "ImmediateData", "Yes"); 453 keys_add_int(request_keys, "MaxBurstLength", 454 ISCSI_MAX_DATA_SEGMENT_LENGTH); 455 keys_add_int(request_keys, "FirstBurstLength", 456 ISCSI_MAX_DATA_SEGMENT_LENGTH); 457 keys_add(request_keys, "InitialR2T", "Yes"); 458 } else { 459 keys_add(request_keys, "HeaderDigest", "None"); 460 keys_add(request_keys, "DataDigest", "None"); 461 } 462 463 keys_add_int(request_keys, "MaxRecvDataSegmentLength", 464 ISCSI_MAX_DATA_SEGMENT_LENGTH); 465 keys_add(request_keys, "DefaultTime2Wait", "0"); 466 keys_add(request_keys, "DefaultTime2Retain", "0"); 467 keys_add(request_keys, "ErrorRecoveryLevel", "0"); 468 keys_add(request_keys, "MaxOutstandingR2T", "1"); 469 keys_save(request_keys, request); 470 keys_delete(request_keys); 471 request_keys = NULL; 472 pdu_send(request); 473 pdu_delete(request); 474 request = NULL; 475 476 response = login_receive(conn); 477 response_keys = keys_new(); 478 keys_load(response_keys, response); 479 for (i = 0; i < KEYS_MAX; i++) { 480 if (response_keys->keys_names[i] == NULL) 481 break; 482 483 login_negotiate_key(conn, 484 response_keys->keys_names[i], response_keys->keys_values[i]); 485 } 486 487 keys_delete(response_keys); 488 response_keys = NULL; 489 490 for (;;) { 491 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; 492 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0) 493 break; 494 495 nrequests++; 496 if (nrequests > 5) { 497 log_warnx("received login response " 498 "without the \"T\" flag too many times; giving up"); 499 break; 500 } 501 502 log_debugx("received login response " 503 "without the \"T\" flag; sending another request"); 504 505 pdu_delete(response); 506 507 request = login_new_request(conn, 508 BHSLR_STAGE_OPERATIONAL_NEGOTIATION); 509 pdu_send(request); 510 pdu_delete(request); 511 512 response = login_receive(conn); 513 } 514 515 if (login_nsg(response) != BHSLR_STAGE_FULL_FEATURE_PHASE) 516 log_warnx("received final login response with wrong NSG 0x%x", 517 login_nsg(response)); 518 pdu_delete(response); 519 520 log_debugx("operational parameter negotiation done; " 521 "transitioning to Full Feature phase"); 522} 523 524static void 525login_send_chap_a(struct connection *conn) 526{ 527 struct pdu *request; 528 struct keys *request_keys; 529 530 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION); 531 request_keys = keys_new(); 532 keys_add(request_keys, "CHAP_A", "5"); 533 keys_save(request_keys, request); 534 keys_delete(request_keys); 535 pdu_send(request); 536 pdu_delete(request); 537} 538 539static void 540login_send_chap_r(struct pdu *response) 541{ 542 struct connection *conn; 543 struct pdu *request; 544 struct keys *request_keys, *response_keys; 545 struct rchap *rchap; 546 const char *chap_a, *chap_c, *chap_i; 547 char *chap_r; 548 int error; 549 char *mutual_chap_c, *mutual_chap_i; 550 551 /* 552 * As in the rest of the initiator, 'request' means 553 * 'initiator -> target', and 'response' means 'target -> initiator', 554 * 555 * So, here the 'response' from the target is the packet that contains 556 * CHAP challenge; our CHAP response goes into 'request'. 557 */ 558 559 conn = response->pdu_connection; 560 561 response_keys = keys_new(); 562 keys_load(response_keys, response); 563 564 /* 565 * First, compute the response. 566 */ 567 chap_a = keys_find(response_keys, "CHAP_A"); 568 if (chap_a == NULL) 569 log_errx(1, "received CHAP packet without CHAP_A"); 570 chap_c = keys_find(response_keys, "CHAP_C"); 571 if (chap_c == NULL) 572 log_errx(1, "received CHAP packet without CHAP_C"); 573 chap_i = keys_find(response_keys, "CHAP_I"); 574 if (chap_i == NULL) 575 log_errx(1, "received CHAP packet without CHAP_I"); 576 577 if (strcmp(chap_a, "5") != 0) { 578 log_errx(1, "received CHAP packet " 579 "with unsupported CHAP_A \"%s\"", chap_a); 580 } 581 582 rchap = rchap_new(conn->conn_conf.isc_secret); 583 error = rchap_receive(rchap, chap_i, chap_c); 584 if (error != 0) { 585 log_errx(1, "received CHAP packet " 586 "with malformed CHAP_I or CHAP_C"); 587 } 588 chap_r = rchap_get_response(rchap); 589 rchap_delete(rchap); 590 591 keys_delete(response_keys); 592 593 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION); 594 request_keys = keys_new(); 595 keys_add(request_keys, "CHAP_N", conn->conn_conf.isc_user); 596 keys_add(request_keys, "CHAP_R", chap_r); 597 free(chap_r); 598 599 /* 600 * If we want mutual authentication, we're expected to send 601 * our CHAP_I/CHAP_C now. 602 */ 603 if (conn->conn_conf.isc_mutual_user[0] != '\0') { 604 log_debugx("requesting mutual authentication; " 605 "binary challenge size is %zd bytes", 606 sizeof(conn->conn_mutual_chap->chap_challenge)); 607 608 assert(conn->conn_mutual_chap == NULL); 609 conn->conn_mutual_chap = chap_new(); 610 mutual_chap_i = chap_get_id(conn->conn_mutual_chap); 611 mutual_chap_c = chap_get_challenge(conn->conn_mutual_chap); 612 keys_add(request_keys, "CHAP_I", mutual_chap_i); 613 keys_add(request_keys, "CHAP_C", mutual_chap_c); 614 free(mutual_chap_i); 615 free(mutual_chap_c); 616 } 617 618 keys_save(request_keys, request); 619 keys_delete(request_keys); 620 pdu_send(request); 621 pdu_delete(request); 622} 623 624static void 625login_verify_mutual(const struct pdu *response) 626{ 627 struct connection *conn; 628 struct keys *response_keys; 629 const char *chap_n, *chap_r; 630 int error; 631 632 conn = response->pdu_connection; 633 634 response_keys = keys_new(); 635 keys_load(response_keys, response); 636 637 chap_n = keys_find(response_keys, "CHAP_N"); 638 if (chap_n == NULL) 639 log_errx(1, "received CHAP Response PDU without CHAP_N"); 640 chap_r = keys_find(response_keys, "CHAP_R"); 641 if (chap_r == NULL) 642 log_errx(1, "received CHAP Response PDU without CHAP_R"); 643 644 error = chap_receive(conn->conn_mutual_chap, chap_r); 645 if (error != 0) 646 log_errx(1, "received CHAP Response PDU with invalid CHAP_R"); 647 648 if (strcmp(chap_n, conn->conn_conf.isc_mutual_user) != 0) { 649 fail(conn, "Mutual CHAP failed"); 650 log_errx(1, "mutual CHAP authentication failed: wrong user"); 651 } 652 653 error = chap_authenticate(conn->conn_mutual_chap, 654 conn->conn_conf.isc_mutual_secret); 655 if (error != 0) { 656 fail(conn, "Mutual CHAP failed"); 657 log_errx(1, "mutual CHAP authentication failed: wrong secret"); 658 } 659 660 keys_delete(response_keys); 661 chap_delete(conn->conn_mutual_chap); 662 conn->conn_mutual_chap = NULL; 663 664 log_debugx("mutual CHAP authentication succeeded"); 665} 666 667static void 668login_chap(struct connection *conn) 669{ 670 struct pdu *response; 671 672 log_debugx("beginning CHAP authentication; sending CHAP_A"); 673 login_send_chap_a(conn); 674 675 log_debugx("waiting for CHAP_A/CHAP_C/CHAP_I"); 676 response = login_receive(conn); 677 678 log_debugx("sending CHAP_N/CHAP_R"); 679 login_send_chap_r(response); 680 pdu_delete(response); 681 682 /* 683 * XXX: Make sure this is not susceptible to MITM. 684 */ 685 686 log_debugx("waiting for CHAP result"); 687 response = login_receive(conn); 688 if (conn->conn_conf.isc_mutual_user[0] != '\0') 689 login_verify_mutual(response); 690 pdu_delete(response); 691 692 log_debugx("CHAP authentication done"); 693} 694 695void 696login(struct connection *conn) 697{ 698 struct pdu *request, *response; 699 struct keys *request_keys, *response_keys; 700 struct iscsi_bhs_login_response *bhslr2; 701 const char *auth_method; 702 int i; 703 704 log_debugx("beginning Login phase; sending Login PDU"); 705 request = login_new_request(conn, BHSLR_STAGE_SECURITY_NEGOTIATION); 706 request_keys = keys_new(); 707 if (conn->conn_conf.isc_mutual_user[0] != '\0') { 708 keys_add(request_keys, "AuthMethod", "CHAP"); 709 } else if (conn->conn_conf.isc_user[0] != '\0') { 710 /* 711 * Give target a chance to skip authentication if it 712 * doesn't feel like it. 713 * 714 * None is first, CHAP second; this is to work around 715 * what seems to be LIO (Linux target) bug: otherwise, 716 * if target is configured with no authentication, 717 * and we are configured to authenticate, the target 718 * will erroneously respond with AuthMethod=CHAP 719 * instead of AuthMethod=None, and will subsequently 720 * fail the connection. This usually happens with 721 * Discovery sessions, which default to no authentication. 722 */ 723 keys_add(request_keys, "AuthMethod", "None,CHAP"); 724 } else { 725 keys_add(request_keys, "AuthMethod", "None"); 726 } 727 keys_add(request_keys, "InitiatorName", 728 conn->conn_conf.isc_initiator); 729 if (conn->conn_conf.isc_initiator_alias[0] != '\0') { 730 keys_add(request_keys, "InitiatorAlias", 731 conn->conn_conf.isc_initiator_alias); 732 } 733 if (conn->conn_conf.isc_discovery == 0) { 734 keys_add(request_keys, "SessionType", "Normal"); 735 keys_add(request_keys, 736 "TargetName", conn->conn_conf.isc_target); 737 } else { 738 keys_add(request_keys, "SessionType", "Discovery"); 739 } 740 keys_save(request_keys, request); 741 keys_delete(request_keys); 742 pdu_send(request); 743 pdu_delete(request); 744 745 response = login_receive(conn); 746 747 response_keys = keys_new(); 748 keys_load(response_keys, response); 749 750 for (i = 0; i < KEYS_MAX; i++) { 751 if (response_keys->keys_names[i] == NULL) 752 break; 753 754 /* 755 * Not interested in AuthMethod at this point; we only need 756 * to parse things such as TargetAlias. 757 * 758 * XXX: This is somewhat ugly. We should have a way to apply 759 * all the keys to the session and use that by default 760 * instead of discarding them. 761 */ 762 if (strcmp(response_keys->keys_names[i], "AuthMethod") == 0) 763 continue; 764 765 login_negotiate_key(conn, 766 response_keys->keys_names[i], response_keys->keys_values[i]); 767 } 768 769 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; 770 if ((bhslr2->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0 && 771 login_nsg(response) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) { 772 if (conn->conn_conf.isc_mutual_user[0] != '\0') { 773 log_errx(1, "target requested transition " 774 "to operational parameter negotiation, " 775 "but we require mutual CHAP"); 776 } 777 778 log_debugx("target requested transition " 779 "to operational parameter negotiation"); 780 keys_delete(response_keys); 781 pdu_delete(response); 782 login_negotiate(conn); 783 return; 784 } 785 786 auth_method = keys_find(response_keys, "AuthMethod"); 787 if (auth_method == NULL) 788 log_errx(1, "received response without AuthMethod"); 789 if (strcmp(auth_method, "None") == 0) { 790 if (conn->conn_conf.isc_mutual_user[0] != '\0') { 791 log_errx(1, "target does not require authantication, " 792 "but we require mutual CHAP"); 793 } 794 795 log_debugx("target does not require authentication"); 796 keys_delete(response_keys); 797 pdu_delete(response); 798 login_negotiate(conn); 799 return; 800 } 801 802 if (strcmp(auth_method, "CHAP") != 0) { 803 fail(conn, "Unsupported AuthMethod"); 804 log_errx(1, "received response " 805 "with unsupported AuthMethod \"%s\"", auth_method); 806 } 807 808 if (conn->conn_conf.isc_user[0] == '\0' || 809 conn->conn_conf.isc_secret[0] == '\0') { 810 fail(conn, "Authentication required"); 811 log_errx(1, "target requests CHAP authentication, but we don't " 812 "have user and secret"); 813 } 814 815 keys_delete(response_keys); 816 response_keys = NULL; 817 pdu_delete(response); 818 response = NULL; 819 820 login_chap(conn); 821 login_negotiate(conn); 822} 823