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