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