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$"); 33 34 #include <assert.h> 35 #include <stdbool.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <unistd.h> 39 #include <netinet/in.h> 40 41 #include "ctld.h" 42 #include "iscsi_proto.h" 43 44 static void login_send_error(struct pdu *request, 45 char class, char detail); 46 47 static void 48 login_set_nsg(struct pdu *response, int nsg) 49 { 50 struct iscsi_bhs_login_response *bhslr; 51 52 assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION || 53 nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION || 54 nsg == BHSLR_STAGE_FULL_FEATURE_PHASE); 55 56 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; 57 58 bhslr->bhslr_flags &= 0xFC; 59 bhslr->bhslr_flags |= nsg; 60 } 61 62 static int 63 login_csg(const struct pdu *request) 64 { 65 struct iscsi_bhs_login_request *bhslr; 66 67 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 68 69 return ((bhslr->bhslr_flags & 0x0C) >> 2); 70 } 71 72 static void 73 login_set_csg(struct pdu *response, int csg) 74 { 75 struct iscsi_bhs_login_response *bhslr; 76 77 assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION || 78 csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION || 79 csg == BHSLR_STAGE_FULL_FEATURE_PHASE); 80 81 bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; 82 83 bhslr->bhslr_flags &= 0xF3; 84 bhslr->bhslr_flags |= csg << 2; 85 } 86 87 static struct pdu * 88 login_receive(struct connection *conn, bool initial) 89 { 90 struct pdu *request; 91 struct iscsi_bhs_login_request *bhslr; 92 93 request = pdu_new(conn); 94 pdu_receive(request); 95 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) != 96 ISCSI_BHS_OPCODE_LOGIN_REQUEST) { 97 /* 98 * The first PDU in session is special - if we receive any PDU 99 * different than login request, we have to drop the connection 100 * without sending response ("A target receiving any PDU 101 * except a Login request before the Login Phase is started MUST 102 * immediately terminate the connection on which the PDU 103 * was received.") 104 */ 105 if (initial == false) 106 login_send_error(request, 0x02, 0x0b); 107 log_errx(1, "protocol error: received invalid opcode 0x%x", 108 request->pdu_bhs->bhs_opcode); 109 } 110 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 111 /* 112 * XXX: Implement the C flag some day. 113 */ 114 if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0) { 115 login_send_error(request, 0x03, 0x00); 116 log_errx(1, "received Login PDU with unsupported \"C\" flag"); 117 } 118 if (bhslr->bhslr_version_max != 0x00) { 119 login_send_error(request, 0x02, 0x05); 120 log_errx(1, "received Login PDU with unsupported " 121 "Version-max 0x%x", bhslr->bhslr_version_max); 122 } 123 if (bhslr->bhslr_version_min != 0x00) { 124 login_send_error(request, 0x02, 0x05); 125 log_errx(1, "received Login PDU with unsupported " 126 "Version-min 0x%x", bhslr->bhslr_version_min); 127 } 128 if (ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) { 129 login_send_error(request, 0x02, 0x05); 130 log_errx(1, "received Login PDU with decreasing CmdSN: " 131 "was %u, is %u", conn->conn_cmdsn, 132 ntohl(bhslr->bhslr_cmdsn)); 133 } 134 if (initial == false && 135 ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) { 136 login_send_error(request, 0x02, 0x05); 137 log_errx(1, "received Login PDU with wrong ExpStatSN: " 138 "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn), 139 conn->conn_statsn); 140 } 141 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn); 142 143 return (request); 144 } 145 146 static struct pdu * 147 login_new_response(struct pdu *request) 148 { 149 struct pdu *response; 150 struct connection *conn; 151 struct iscsi_bhs_login_request *bhslr; 152 struct iscsi_bhs_login_response *bhslr2; 153 154 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 155 conn = request->pdu_connection; 156 157 response = pdu_new_response(request); 158 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; 159 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_RESPONSE; 160 login_set_csg(response, BHSLR_STAGE_SECURITY_NEGOTIATION); 161 memcpy(bhslr2->bhslr_isid, 162 bhslr->bhslr_isid, sizeof(bhslr2->bhslr_isid)); 163 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag; 164 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++); 165 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn); 166 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn); 167 168 return (response); 169 } 170 171 static void 172 login_send_error(struct pdu *request, char class, char detail) 173 { 174 struct pdu *response; 175 struct iscsi_bhs_login_response *bhslr2; 176 177 log_debugx("sending Login Response PDU with failure class 0x%x/0x%x; " 178 "see next line for reason", class, detail); 179 response = login_new_response(request); 180 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; 181 bhslr2->bhslr_status_class = class; 182 bhslr2->bhslr_status_detail = detail; 183 184 pdu_send(response); 185 pdu_delete(response); 186 } 187 188 static int 189 login_list_contains(const char *list, const char *what) 190 { 191 char *tofree, *str, *token; 192 193 tofree = str = checked_strdup(list); 194 195 while ((token = strsep(&str, ",")) != NULL) { 196 if (strcmp(token, what) == 0) { 197 free(tofree); 198 return (1); 199 } 200 } 201 free(tofree); 202 return (0); 203 } 204 205 static int 206 login_list_prefers(const char *list, 207 const char *choice1, const char *choice2) 208 { 209 char *tofree, *str, *token; 210 211 tofree = str = checked_strdup(list); 212 213 while ((token = strsep(&str, ",")) != NULL) { 214 if (strcmp(token, choice1) == 0) { 215 free(tofree); 216 return (1); 217 } 218 if (strcmp(token, choice2) == 0) { 219 free(tofree); 220 return (2); 221 } 222 } 223 free(tofree); 224 return (-1); 225 } 226 227 static struct pdu * 228 login_receive_chap_a(struct connection *conn) 229 { 230 struct pdu *request; 231 struct keys *request_keys; 232 const char *chap_a; 233 234 request = login_receive(conn, false); 235 request_keys = keys_new(); 236 keys_load(request_keys, request); 237 238 chap_a = keys_find(request_keys, "CHAP_A"); 239 if (chap_a == NULL) { 240 login_send_error(request, 0x02, 0x07); 241 log_errx(1, "received CHAP Login PDU without CHAP_A"); 242 } 243 if (login_list_contains(chap_a, "5") == 0) { 244 login_send_error(request, 0x02, 0x01); 245 log_errx(1, "received CHAP Login PDU with unsupported CHAP_A " 246 "\"%s\"", chap_a); 247 } 248 keys_delete(request_keys); 249 250 return (request); 251 } 252 253 static void 254 login_send_chap_c(struct pdu *request, struct chap *chap) 255 { 256 struct pdu *response; 257 struct keys *response_keys; 258 char *chap_c, *chap_i; 259 260 chap_c = chap_get_challenge(chap); 261 chap_i = chap_get_id(chap); 262 263 response = login_new_response(request); 264 response_keys = keys_new(); 265 keys_add(response_keys, "CHAP_A", "5"); 266 keys_add(response_keys, "CHAP_I", chap_i); 267 keys_add(response_keys, "CHAP_C", chap_c); 268 free(chap_i); 269 free(chap_c); 270 keys_save(response_keys, response); 271 pdu_send(response); 272 pdu_delete(response); 273 keys_delete(response_keys); 274 } 275 276 static struct pdu * 277 login_receive_chap_r(struct connection *conn, struct auth_group *ag, 278 struct chap *chap, const struct auth **authp) 279 { 280 struct pdu *request; 281 struct keys *request_keys; 282 const char *chap_n, *chap_r; 283 const struct auth *auth; 284 int error; 285 286 request = login_receive(conn, false); 287 request_keys = keys_new(); 288 keys_load(request_keys, request); 289 290 chap_n = keys_find(request_keys, "CHAP_N"); 291 if (chap_n == NULL) { 292 login_send_error(request, 0x02, 0x07); 293 log_errx(1, "received CHAP Login PDU without CHAP_N"); 294 } 295 chap_r = keys_find(request_keys, "CHAP_R"); 296 if (chap_r == NULL) { 297 login_send_error(request, 0x02, 0x07); 298 log_errx(1, "received CHAP Login PDU without CHAP_R"); 299 } 300 error = chap_receive(chap, chap_r); 301 if (error != 0) { 302 login_send_error(request, 0x02, 0x07); 303 log_errx(1, "received CHAP Login PDU with malformed CHAP_R"); 304 } 305 306 /* 307 * Verify the response. 308 */ 309 assert(ag->ag_type == AG_TYPE_CHAP || 310 ag->ag_type == AG_TYPE_CHAP_MUTUAL); 311 auth = auth_find(ag, chap_n); 312 if (auth == NULL) { 313 login_send_error(request, 0x02, 0x01); 314 log_errx(1, "received CHAP Login with invalid user \"%s\"", 315 chap_n); 316 } 317 318 assert(auth->a_secret != NULL); 319 assert(strlen(auth->a_secret) > 0); 320 321 error = chap_authenticate(chap, auth->a_secret); 322 if (error != 0) { 323 login_send_error(request, 0x02, 0x01); 324 log_errx(1, "CHAP authentication failed for user \"%s\"", 325 auth->a_user); 326 } 327 328 keys_delete(request_keys); 329 330 *authp = auth; 331 return (request); 332 } 333 334 static void 335 login_send_chap_success(struct pdu *request, 336 const struct auth *auth) 337 { 338 struct pdu *response; 339 struct keys *request_keys, *response_keys; 340 struct iscsi_bhs_login_response *bhslr2; 341 struct rchap *rchap; 342 const char *chap_i, *chap_c; 343 char *chap_r; 344 int error; 345 346 response = login_new_response(request); 347 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; 348 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT; 349 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); 350 351 /* 352 * Actually, one more thing: mutual authentication. 353 */ 354 request_keys = keys_new(); 355 keys_load(request_keys, request); 356 chap_i = keys_find(request_keys, "CHAP_I"); 357 chap_c = keys_find(request_keys, "CHAP_C"); 358 if (chap_i != NULL || chap_c != NULL) { 359 if (chap_i == NULL) { 360 login_send_error(request, 0x02, 0x07); 361 log_errx(1, "initiator requested target " 362 "authentication, but didn't send CHAP_I"); 363 } 364 if (chap_c == NULL) { 365 login_send_error(request, 0x02, 0x07); 366 log_errx(1, "initiator requested target " 367 "authentication, but didn't send CHAP_C"); 368 } 369 if (auth->a_auth_group->ag_type != AG_TYPE_CHAP_MUTUAL) { 370 login_send_error(request, 0x02, 0x01); 371 log_errx(1, "initiator requests target authentication " 372 "for user \"%s\", but mutual user/secret " 373 "is not set", auth->a_user); 374 } 375 376 log_debugx("performing mutual authentication as user \"%s\"", 377 auth->a_mutual_user); 378 379 rchap = rchap_new(auth->a_mutual_secret); 380 error = rchap_receive(rchap, chap_i, chap_c); 381 if (error != 0) { 382 login_send_error(request, 0x02, 0x07); 383 log_errx(1, "received CHAP Login PDU with malformed " 384 "CHAP_I or CHAP_C"); 385 } 386 chap_r = rchap_get_response(rchap); 387 rchap_delete(rchap); 388 response_keys = keys_new(); 389 keys_add(response_keys, "CHAP_N", auth->a_mutual_user); 390 keys_add(response_keys, "CHAP_R", chap_r); 391 free(chap_r); 392 keys_save(response_keys, response); 393 keys_delete(response_keys); 394 } else { 395 log_debugx("initiator did not request target authentication"); 396 } 397 398 keys_delete(request_keys); 399 pdu_send(response); 400 pdu_delete(response); 401 } 402 403 static void 404 login_chap(struct connection *conn, struct auth_group *ag) 405 { 406 const struct auth *auth; 407 struct chap *chap; 408 struct pdu *request; 409 410 /* 411 * Receive CHAP_A PDU. 412 */ 413 log_debugx("beginning CHAP authentication; waiting for CHAP_A"); 414 request = login_receive_chap_a(conn); 415 416 /* 417 * Generate the challenge. 418 */ 419 chap = chap_new(); 420 421 /* 422 * Send the challenge. 423 */ 424 log_debugx("sending CHAP_C, binary challenge size is %zd bytes", 425 sizeof(chap->chap_challenge)); 426 login_send_chap_c(request, chap); 427 pdu_delete(request); 428 429 /* 430 * Receive CHAP_N/CHAP_R PDU and authenticate. 431 */ 432 log_debugx("waiting for CHAP_N/CHAP_R"); 433 request = login_receive_chap_r(conn, ag, chap, &auth); 434 435 /* 436 * Yay, authentication succeeded! 437 */ 438 log_debugx("authentication succeeded for user \"%s\"; " 439 "transitioning to Negotiation Phase", auth->a_user); 440 login_send_chap_success(request, auth); 441 pdu_delete(request); 442 443 /* 444 * Leave username and CHAP information for discovery(). 445 */ 446 conn->conn_user = auth->a_user; 447 conn->conn_chap = chap; 448 } 449 450 static void 451 login_negotiate_key(struct pdu *request, const char *name, 452 const char *value, bool skipped_security, struct keys *response_keys) 453 { 454 int which; 455 size_t tmp; 456 struct connection *conn; 457 458 conn = request->pdu_connection; 459 460 if (strcmp(name, "InitiatorName") == 0) { 461 if (!skipped_security) 462 log_errx(1, "initiator resent InitiatorName"); 463 } else if (strcmp(name, "SessionType") == 0) { 464 if (!skipped_security) 465 log_errx(1, "initiator resent SessionType"); 466 } else if (strcmp(name, "TargetName") == 0) { 467 if (!skipped_security) 468 log_errx(1, "initiator resent TargetName"); 469 } else if (strcmp(name, "InitiatorAlias") == 0) { 470 if (conn->conn_initiator_alias != NULL) 471 free(conn->conn_initiator_alias); 472 conn->conn_initiator_alias = checked_strdup(value); 473 } else if (strcmp(value, "Irrelevant") == 0) { 474 /* Ignore. */ 475 } else if (strcmp(name, "HeaderDigest") == 0) { 476 /* 477 * We don't handle digests for discovery sessions. 478 */ 479 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) { 480 log_debugx("discovery session; digests disabled"); 481 keys_add(response_keys, name, "None"); 482 return; 483 } 484 485 which = login_list_prefers(value, "CRC32C", "None"); 486 switch (which) { 487 case 1: 488 log_debugx("initiator prefers CRC32C " 489 "for header digest; we'll use it"); 490 conn->conn_header_digest = CONN_DIGEST_CRC32C; 491 keys_add(response_keys, name, "CRC32C"); 492 break; 493 case 2: 494 log_debugx("initiator prefers not to do " 495 "header digest; we'll comply"); 496 keys_add(response_keys, name, "None"); 497 break; 498 default: 499 log_warnx("initiator sent unrecognized " 500 "HeaderDigest value \"%s\"; will use None", value); 501 keys_add(response_keys, name, "None"); 502 break; 503 } 504 } else if (strcmp(name, "DataDigest") == 0) { 505 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) { 506 log_debugx("discovery session; digests disabled"); 507 keys_add(response_keys, name, "None"); 508 return; 509 } 510 511 which = login_list_prefers(value, "CRC32C", "None"); 512 switch (which) { 513 case 1: 514 log_debugx("initiator prefers CRC32C " 515 "for data digest; we'll use it"); 516 conn->conn_data_digest = CONN_DIGEST_CRC32C; 517 keys_add(response_keys, name, "CRC32C"); 518 break; 519 case 2: 520 log_debugx("initiator prefers not to do " 521 "data digest; we'll comply"); 522 keys_add(response_keys, name, "None"); 523 break; 524 default: 525 log_warnx("initiator sent unrecognized " 526 "DataDigest value \"%s\"; will use None", value); 527 keys_add(response_keys, name, "None"); 528 break; 529 } 530 } else if (strcmp(name, "MaxConnections") == 0) { 531 keys_add(response_keys, name, "1"); 532 } else if (strcmp(name, "InitialR2T") == 0) { 533 keys_add(response_keys, name, "Yes"); 534 } else if (strcmp(name, "ImmediateData") == 0) { 535 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) { 536 log_debugx("discovery session; ImmediateData irrelevant"); 537 keys_add(response_keys, name, "Irrelevant"); 538 } else { 539 if (strcmp(value, "Yes") == 0) { 540 conn->conn_immediate_data = true; 541 keys_add(response_keys, name, "Yes"); 542 } else { 543 conn->conn_immediate_data = false; 544 keys_add(response_keys, name, "No"); 545 } 546 } 547 } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) { 548 tmp = strtoul(value, NULL, 10); 549 if (tmp <= 0) { 550 login_send_error(request, 0x02, 0x00); 551 log_errx(1, "received invalid " 552 "MaxRecvDataSegmentLength"); 553 } 554 if (tmp > conn->conn_data_segment_limit) { 555 log_debugx("capping MaxRecvDataSegmentLength " 556 "from %zd to %zd", tmp, conn->conn_data_segment_limit); 557 tmp = conn->conn_data_segment_limit; 558 } 559 conn->conn_max_data_segment_length = tmp; 560 keys_add_int(response_keys, name, tmp); 561 } else if (strcmp(name, "MaxBurstLength") == 0) { 562 tmp = strtoul(value, NULL, 10); 563 if (tmp <= 0) { 564 login_send_error(request, 0x02, 0x00); 565 log_errx(1, "received invalid MaxBurstLength"); 566 } 567 if (tmp > MAX_BURST_LENGTH) { 568 log_debugx("capping MaxBurstLength from %zd to %d", 569 tmp, MAX_BURST_LENGTH); 570 tmp = MAX_BURST_LENGTH; 571 } 572 conn->conn_max_burst_length = tmp; 573 keys_add(response_keys, name, value); 574 } else if (strcmp(name, "FirstBurstLength") == 0) { 575 tmp = strtoul(value, NULL, 10); 576 if (tmp <= 0) { 577 login_send_error(request, 0x02, 0x00); 578 log_errx(1, "received invalid " 579 "FirstBurstLength"); 580 } 581 if (tmp > conn->conn_data_segment_limit) { 582 log_debugx("capping FirstBurstLength from %zd to %zd", 583 tmp, conn->conn_data_segment_limit); 584 tmp = conn->conn_data_segment_limit; 585 } 586 /* 587 * We don't pass the value to the kernel; it only enforces 588 * hardcoded limit anyway. 589 */ 590 keys_add_int(response_keys, name, tmp); 591 } else if (strcmp(name, "DefaultTime2Wait") == 0) { 592 keys_add(response_keys, name, value); 593 } else if (strcmp(name, "DefaultTime2Retain") == 0) { 594 keys_add(response_keys, name, "0"); 595 } else if (strcmp(name, "MaxOutstandingR2T") == 0) { 596 keys_add(response_keys, name, "1"); 597 } else if (strcmp(name, "DataPDUInOrder") == 0) { 598 keys_add(response_keys, name, "Yes"); 599 } else if (strcmp(name, "DataSequenceInOrder") == 0) { 600 keys_add(response_keys, name, "Yes"); 601 } else if (strcmp(name, "ErrorRecoveryLevel") == 0) { 602 keys_add(response_keys, name, "0"); 603 } else if (strcmp(name, "OFMarker") == 0) { 604 keys_add(response_keys, name, "No"); 605 } else if (strcmp(name, "IFMarker") == 0) { 606 keys_add(response_keys, name, "No"); 607 } else { 608 log_debugx("unknown key \"%s\"; responding " 609 "with NotUnderstood", name); 610 keys_add(response_keys, name, "NotUnderstood"); 611 } 612 } 613 614 static void 615 login_redirect(struct pdu *request, const char *target_address) 616 { 617 struct pdu *response; 618 struct iscsi_bhs_login_response *bhslr2; 619 struct keys *response_keys; 620 621 response = login_new_response(request); 622 login_set_csg(response, login_csg(request)); 623 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; 624 bhslr2->bhslr_status_class = 0x01; 625 bhslr2->bhslr_status_detail = 0x01; 626 627 response_keys = keys_new(); 628 keys_add(response_keys, "TargetAddress", target_address); 629 630 keys_save(response_keys, response); 631 pdu_send(response); 632 pdu_delete(response); 633 keys_delete(response_keys); 634 } 635 636 static bool 637 login_portal_redirect(struct connection *conn, struct pdu *request) 638 { 639 const struct portal_group *pg; 640 641 pg = conn->conn_portal->p_portal_group; 642 if (pg->pg_redirection == NULL) 643 return (false); 644 645 log_debugx("portal-group \"%s\" configured to redirect to %s", 646 pg->pg_name, pg->pg_redirection); 647 login_redirect(request, pg->pg_redirection); 648 649 return (true); 650 } 651 652 static bool 653 login_target_redirect(struct connection *conn, struct pdu *request) 654 { 655 const char *target_address; 656 657 assert(conn->conn_portal->p_portal_group->pg_redirection == NULL); 658 659 if (conn->conn_target == NULL) 660 return (false); 661 662 target_address = conn->conn_target->t_redirection; 663 if (target_address == NULL) 664 return (false); 665 666 log_debugx("target \"%s\" configured to redirect to %s", 667 conn->conn_target->t_name, target_address); 668 login_redirect(request, target_address); 669 670 return (true); 671 } 672 673 static void 674 login_negotiate(struct connection *conn, struct pdu *request) 675 { 676 struct pdu *response; 677 struct iscsi_bhs_login_response *bhslr2; 678 struct keys *request_keys, *response_keys; 679 int i; 680 bool redirected, skipped_security; 681 682 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 683 /* 684 * Query the kernel for MaxDataSegmentLength it can handle. 685 * In case of offload, it depends on hardware capabilities. 686 */ 687 assert(conn->conn_target != NULL); 688 kernel_limits(conn->conn_portal->p_portal_group->pg_offload, 689 &conn->conn_data_segment_limit); 690 } else { 691 conn->conn_data_segment_limit = MAX_DATA_SEGMENT_LENGTH; 692 } 693 694 if (request == NULL) { 695 log_debugx("beginning operational parameter negotiation; " 696 "waiting for Login PDU"); 697 request = login_receive(conn, false); 698 skipped_security = false; 699 } else 700 skipped_security = true; 701 702 /* 703 * RFC 3720, 10.13.5. Status-Class and Status-Detail, says 704 * the redirection SHOULD be accepted by the initiator before 705 * authentication, but MUST be be accepted afterwards; that's 706 * why we're doing it here and not earlier. 707 */ 708 redirected = login_target_redirect(conn, request); 709 if (redirected) { 710 log_debugx("initiator redirected; exiting"); 711 exit(0); 712 } 713 714 request_keys = keys_new(); 715 keys_load(request_keys, request); 716 717 response = login_new_response(request); 718 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; 719 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT; 720 bhslr2->bhslr_tsih = htons(0xbadd); 721 login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); 722 login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE); 723 response_keys = keys_new(); 724 725 if (skipped_security && 726 conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 727 if (conn->conn_target->t_alias != NULL) 728 keys_add(response_keys, 729 "TargetAlias", conn->conn_target->t_alias); 730 keys_add_int(response_keys, "TargetPortalGroupTag", 731 conn->conn_portal->p_portal_group->pg_tag); 732 } 733 734 for (i = 0; i < KEYS_MAX; i++) { 735 if (request_keys->keys_names[i] == NULL) 736 break; 737 738 login_negotiate_key(request, request_keys->keys_names[i], 739 request_keys->keys_values[i], skipped_security, 740 response_keys); 741 } 742 743 log_debugx("operational parameter negotiation done; " 744 "transitioning to Full Feature Phase"); 745 746 keys_save(response_keys, response); 747 pdu_send(response); 748 pdu_delete(response); 749 keys_delete(response_keys); 750 pdu_delete(request); 751 keys_delete(request_keys); 752 } 753 754 void 755 login(struct connection *conn) 756 { 757 struct pdu *request, *response; 758 struct iscsi_bhs_login_request *bhslr; 759 struct iscsi_bhs_login_response *bhslr2; 760 struct keys *request_keys, *response_keys; 761 struct auth_group *ag; 762 struct portal_group *pg; 763 const char *initiator_name, *initiator_alias, *session_type, 764 *target_name, *auth_method; 765 bool redirected; 766 767 /* 768 * Handle the initial Login Request - figure out required authentication 769 * method and either transition to the next phase, if no authentication 770 * is required, or call appropriate authentication code. 771 */ 772 log_debugx("beginning Login Phase; waiting for Login PDU"); 773 request = login_receive(conn, true); 774 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 775 if (bhslr->bhslr_tsih != 0) { 776 login_send_error(request, 0x02, 0x0a); 777 log_errx(1, "received Login PDU with non-zero TSIH"); 778 } 779 780 pg = conn->conn_portal->p_portal_group; 781 782 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid, 783 sizeof(conn->conn_initiator_isid)); 784 785 /* 786 * XXX: Implement the C flag some day. 787 */ 788 request_keys = keys_new(); 789 keys_load(request_keys, request); 790 791 assert(conn->conn_initiator_name == NULL); 792 initiator_name = keys_find(request_keys, "InitiatorName"); 793 if (initiator_name == NULL) { 794 login_send_error(request, 0x02, 0x07); 795 log_errx(1, "received Login PDU without InitiatorName"); 796 } 797 if (valid_iscsi_name(initiator_name) == false) { 798 login_send_error(request, 0x02, 0x00); 799 log_errx(1, "received Login PDU with invalid InitiatorName"); 800 } 801 conn->conn_initiator_name = checked_strdup(initiator_name); 802 log_set_peer_name(conn->conn_initiator_name); 803 /* 804 * XXX: This doesn't work (does nothing) because of Capsicum. 805 */ 806 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name); 807 808 redirected = login_portal_redirect(conn, request); 809 if (redirected) { 810 log_debugx("initiator redirected; exiting"); 811 exit(0); 812 } 813 814 initiator_alias = keys_find(request_keys, "InitiatorAlias"); 815 if (initiator_alias != NULL) 816 conn->conn_initiator_alias = checked_strdup(initiator_alias); 817 818 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE); 819 session_type = keys_find(request_keys, "SessionType"); 820 if (session_type != NULL) { 821 if (strcmp(session_type, "Normal") == 0) { 822 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL; 823 } else if (strcmp(session_type, "Discovery") == 0) { 824 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY; 825 } else { 826 login_send_error(request, 0x02, 0x00); 827 log_errx(1, "received Login PDU with invalid " 828 "SessionType \"%s\"", session_type); 829 } 830 } else 831 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL; 832 833 assert(conn->conn_target == NULL); 834 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 835 target_name = keys_find(request_keys, "TargetName"); 836 if (target_name == NULL) { 837 login_send_error(request, 0x02, 0x07); 838 log_errx(1, "received Login PDU without TargetName"); 839 } 840 841 conn->conn_port = port_find_in_pg(pg, target_name); 842 if (conn->conn_port == NULL) { 843 login_send_error(request, 0x02, 0x03); 844 log_errx(1, "requested target \"%s\" not found", 845 target_name); 846 } 847 conn->conn_target = conn->conn_port->p_target; 848 } 849 850 /* 851 * At this point we know what kind of authentication we need. 852 */ 853 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 854 ag = conn->conn_port->p_auth_group; 855 if (ag == NULL) 856 ag = conn->conn_target->t_auth_group; 857 if (ag->ag_name != NULL) { 858 log_debugx("initiator requests to connect " 859 "to target \"%s\"; auth-group \"%s\"", 860 conn->conn_target->t_name, 861 ag->ag_name); 862 } else { 863 log_debugx("initiator requests to connect " 864 "to target \"%s\"", conn->conn_target->t_name); 865 } 866 } else { 867 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 868 ag = pg->pg_discovery_auth_group; 869 if (ag->ag_name != NULL) { 870 log_debugx("initiator requests " 871 "discovery session; auth-group \"%s\"", ag->ag_name); 872 } else { 873 log_debugx("initiator requests discovery session"); 874 } 875 } 876 877 /* 878 * Enforce initiator-name and initiator-portal. 879 */ 880 if (auth_name_check(ag, initiator_name) != 0) { 881 login_send_error(request, 0x02, 0x02); 882 log_errx(1, "initiator does not match allowed initiator names"); 883 } 884 885 if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) { 886 login_send_error(request, 0x02, 0x02); 887 log_errx(1, "initiator does not match allowed " 888 "initiator portals"); 889 } 890 891 /* 892 * Let's see if the initiator intends to do any kind of authentication 893 * at all. 894 */ 895 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) { 896 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) { 897 login_send_error(request, 0x02, 0x01); 898 log_errx(1, "initiator skipped the authentication, " 899 "but authentication is required"); 900 } 901 902 keys_delete(request_keys); 903 904 log_debugx("initiator skipped the authentication, " 905 "and we don't need it; proceeding with negotiation"); 906 login_negotiate(conn, request); 907 return; 908 } 909 910 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) { 911 /* 912 * Initiator might want to to authenticate, 913 * but we don't need it. 914 */ 915 log_debugx("authentication not required; " 916 "transitioning to operational parameter negotiation"); 917 918 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0) 919 log_warnx("initiator did not set the \"T\" flag; " 920 "transitioning anyway"); 921 922 response = login_new_response(request); 923 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; 924 bhslr2->bhslr_flags |= BHSLR_FLAGS_TRANSIT; 925 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); 926 response_keys = keys_new(); 927 /* 928 * Required by Linux initiator. 929 */ 930 auth_method = keys_find(request_keys, "AuthMethod"); 931 if (auth_method != NULL && 932 login_list_contains(auth_method, "None")) 933 keys_add(response_keys, "AuthMethod", "None"); 934 935 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 936 if (conn->conn_target->t_alias != NULL) 937 keys_add(response_keys, 938 "TargetAlias", conn->conn_target->t_alias); 939 keys_add_int(response_keys, 940 "TargetPortalGroupTag", pg->pg_tag); 941 } 942 keys_save(response_keys, response); 943 pdu_send(response); 944 pdu_delete(response); 945 keys_delete(response_keys); 946 pdu_delete(request); 947 keys_delete(request_keys); 948 949 login_negotiate(conn, NULL); 950 return; 951 } 952 953 if (ag->ag_type == AG_TYPE_DENY) { 954 login_send_error(request, 0x02, 0x01); 955 log_errx(1, "auth-type is \"deny\""); 956 } 957 958 if (ag->ag_type == AG_TYPE_UNKNOWN) { 959 /* 960 * This can happen with empty auth-group. 961 */ 962 login_send_error(request, 0x02, 0x01); 963 log_errx(1, "auth-type not set, denying access"); 964 } 965 966 log_debugx("CHAP authentication required"); 967 968 auth_method = keys_find(request_keys, "AuthMethod"); 969 if (auth_method == NULL) { 970 login_send_error(request, 0x02, 0x07); 971 log_errx(1, "received Login PDU without AuthMethod"); 972 } 973 /* 974 * XXX: This should be Reject, not just a login failure (5.3.2). 975 */ 976 if (login_list_contains(auth_method, "CHAP") == 0) { 977 login_send_error(request, 0x02, 0x01); 978 log_errx(1, "initiator requests unsupported AuthMethod \"%s\" " 979 "instead of \"CHAP\"", auth_method); 980 } 981 982 response = login_new_response(request); 983 984 response_keys = keys_new(); 985 keys_add(response_keys, "AuthMethod", "CHAP"); 986 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 987 if (conn->conn_target->t_alias != NULL) 988 keys_add(response_keys, 989 "TargetAlias", conn->conn_target->t_alias); 990 keys_add_int(response_keys, 991 "TargetPortalGroupTag", pg->pg_tag); 992 } 993 keys_save(response_keys, response); 994 995 pdu_send(response); 996 pdu_delete(response); 997 keys_delete(response_keys); 998 pdu_delete(request); 999 keys_delete(request_keys); 1000 1001 login_chap(conn, ag); 1002 1003 login_negotiate(conn, NULL); 1004 } 1005