1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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.conn_socket, 702 &conn->conn_max_recv_data_segment_limit, 703 &conn->conn_max_send_data_segment_limit, 704 &conn->conn_max_burst_limit, 705 &conn->conn_first_burst_limit); 706 707 /* We expect legal, usable values at this point. */ 708 assert(conn->conn_max_recv_data_segment_limit >= 512); 709 assert(conn->conn_max_recv_data_segment_limit < (1 << 24)); 710 assert(conn->conn_max_send_data_segment_limit >= 512); 711 assert(conn->conn_max_send_data_segment_limit < (1 << 24)); 712 assert(conn->conn_max_burst_limit >= 512); 713 assert(conn->conn_max_burst_limit < (1 << 24)); 714 assert(conn->conn_first_burst_limit >= 512); 715 assert(conn->conn_first_burst_limit < (1 << 24)); 716 assert(conn->conn_first_burst_limit <= 717 conn->conn_max_burst_limit); 718 719 /* 720 * Limit default send length in case it won't be negotiated. 721 * We can't do it for other limits, since they may affect both 722 * sender and receiver operation, and we must obey defaults. 723 */ 724 if (conn->conn_max_send_data_segment_limit < 725 conn->conn.conn_max_send_data_segment_length) { 726 conn->conn.conn_max_send_data_segment_length = 727 conn->conn_max_send_data_segment_limit; 728 } 729 } else { 730 conn->conn_max_recv_data_segment_limit = 731 MAX_DATA_SEGMENT_LENGTH; 732 conn->conn_max_send_data_segment_limit = 733 MAX_DATA_SEGMENT_LENGTH; 734 } 735 736 if (request == NULL) { 737 log_debugx("beginning operational parameter negotiation; " 738 "waiting for Login PDU"); 739 request = login_receive(&conn->conn, false); 740 skipped_security = false; 741 } else 742 skipped_security = true; 743 744 /* 745 * RFC 3720, 10.13.5. Status-Class and Status-Detail, says 746 * the redirection SHOULD be accepted by the initiator before 747 * authentication, but MUST be accepted afterwards; that's 748 * why we're doing it here and not earlier. 749 */ 750 redirected = login_target_redirect(conn, request); 751 if (redirected) { 752 log_debugx("initiator redirected; exiting"); 753 exit(0); 754 } 755 756 request_keys = keys_new(); 757 keys_load_pdu(request_keys, request); 758 759 response = login_new_response(request); 760 bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; 761 bhslr2->bhslr_tsih = htons(0xbadd); 762 login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); 763 login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE); 764 response_keys = keys_new(); 765 766 if (skipped_security && 767 conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 768 if (conn->conn_target->t_alias != NULL) 769 keys_add(response_keys, 770 "TargetAlias", conn->conn_target->t_alias); 771 keys_add_int(response_keys, "TargetPortalGroupTag", 772 conn->conn_portal->p_portal_group->pg_tag); 773 } 774 775 for (i = 0; i < KEYS_MAX; i++) { 776 if (request_keys->keys_names[i] == NULL) 777 break; 778 779 login_negotiate_key(request, request_keys->keys_names[i], 780 request_keys->keys_values[i], skipped_security, 781 response_keys); 782 } 783 784 /* 785 * We'd started with usable values at our end. But a bad initiator 786 * could have presented a large FirstBurstLength and then a smaller 787 * MaxBurstLength (in that order) and because we process the key/value 788 * pairs in the order they are in the request we might have ended up 789 * with illegal values here. 790 */ 791 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL && 792 conn->conn.conn_first_burst_length > 793 conn->conn.conn_max_burst_length) { 794 log_errx(1, "initiator sent FirstBurstLength > MaxBurstLength"); 795 } 796 797 conn->conn.conn_max_recv_data_segment_length = 798 conn->conn_max_recv_data_segment_limit; 799 keys_add_int(response_keys, "MaxRecvDataSegmentLength", 800 conn->conn.conn_max_recv_data_segment_length); 801 802 log_debugx("operational parameter negotiation done; " 803 "transitioning to Full Feature Phase"); 804 805 keys_save_pdu(response_keys, response); 806 pdu_send(response); 807 pdu_delete(response); 808 keys_delete(response_keys); 809 pdu_delete(request); 810 keys_delete(request_keys); 811 } 812 813 static void 814 login_wait_transition(struct ctld_connection *conn) 815 { 816 struct pdu *request, *response; 817 struct iscsi_bhs_login_request *bhslr; 818 819 log_debugx("waiting for state transition request"); 820 request = login_receive(&conn->conn, false); 821 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 822 if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0) { 823 login_send_error(request, 0x02, 0x00); 824 log_errx(1, "got no \"T\" flag after answering AuthMethod"); 825 } 826 827 log_debugx("got state transition request"); 828 response = login_new_response(request); 829 pdu_delete(request); 830 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); 831 pdu_send(response); 832 pdu_delete(response); 833 834 login_negotiate(conn, NULL); 835 } 836 837 void 838 login(struct ctld_connection *conn) 839 { 840 struct pdu *request, *response; 841 struct iscsi_bhs_login_request *bhslr; 842 struct keys *request_keys, *response_keys; 843 struct auth_group *ag; 844 struct portal_group *pg; 845 const char *initiator_name, *initiator_alias, *session_type, 846 *target_name, *auth_method; 847 bool redirected, fail, trans; 848 849 /* 850 * Handle the initial Login Request - figure out required authentication 851 * method and either transition to the next phase, if no authentication 852 * is required, or call appropriate authentication code. 853 */ 854 log_debugx("beginning Login Phase; waiting for Login PDU"); 855 request = login_receive(&conn->conn, true); 856 bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; 857 if (bhslr->bhslr_tsih != 0) { 858 login_send_error(request, 0x02, 0x0a); 859 log_errx(1, "received Login PDU with non-zero TSIH"); 860 } 861 862 pg = conn->conn_portal->p_portal_group; 863 864 memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid, 865 sizeof(conn->conn_initiator_isid)); 866 867 /* 868 * XXX: Implement the C flag some day. 869 */ 870 request_keys = keys_new(); 871 keys_load_pdu(request_keys, request); 872 873 assert(conn->conn_initiator_name == NULL); 874 initiator_name = keys_find(request_keys, "InitiatorName"); 875 if (initiator_name == NULL) { 876 login_send_error(request, 0x02, 0x07); 877 log_errx(1, "received Login PDU without InitiatorName"); 878 } 879 if (valid_iscsi_name(initiator_name) == false) { 880 login_send_error(request, 0x02, 0x00); 881 log_errx(1, "received Login PDU with invalid InitiatorName"); 882 } 883 conn->conn_initiator_name = checked_strdup(initiator_name); 884 log_set_peer_name(conn->conn_initiator_name); 885 setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name); 886 887 redirected = login_portal_redirect(conn, request); 888 if (redirected) { 889 log_debugx("initiator redirected; exiting"); 890 exit(0); 891 } 892 893 initiator_alias = keys_find(request_keys, "InitiatorAlias"); 894 if (initiator_alias != NULL) 895 conn->conn_initiator_alias = checked_strdup(initiator_alias); 896 897 assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE); 898 session_type = keys_find(request_keys, "SessionType"); 899 if (session_type != NULL) { 900 if (strcmp(session_type, "Normal") == 0) { 901 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL; 902 } else if (strcmp(session_type, "Discovery") == 0) { 903 conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY; 904 } else { 905 login_send_error(request, 0x02, 0x00); 906 log_errx(1, "received Login PDU with invalid " 907 "SessionType \"%s\"", session_type); 908 } 909 } else 910 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL; 911 912 assert(conn->conn_target == NULL); 913 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 914 target_name = keys_find(request_keys, "TargetName"); 915 if (target_name == NULL) { 916 login_send_error(request, 0x02, 0x07); 917 log_errx(1, "received Login PDU without TargetName"); 918 } 919 920 conn->conn_port = port_find_in_pg(pg, target_name); 921 if (conn->conn_port == NULL) { 922 login_send_error(request, 0x02, 0x03); 923 log_errx(1, "requested target \"%s\" not found", 924 target_name); 925 } 926 conn->conn_target = conn->conn_port->p_target; 927 } 928 929 /* 930 * At this point we know what kind of authentication we need. 931 */ 932 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 933 ag = conn->conn_port->p_auth_group; 934 if (ag == NULL) 935 ag = conn->conn_target->t_auth_group; 936 if (ag->ag_name != NULL) { 937 log_debugx("initiator requests to connect " 938 "to target \"%s\"; auth-group \"%s\"", 939 conn->conn_target->t_name, 940 ag->ag_name); 941 } else { 942 log_debugx("initiator requests to connect " 943 "to target \"%s\"", conn->conn_target->t_name); 944 } 945 } else { 946 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 947 ag = pg->pg_discovery_auth_group; 948 if (ag->ag_name != NULL) { 949 log_debugx("initiator requests " 950 "discovery session; auth-group \"%s\"", ag->ag_name); 951 } else { 952 log_debugx("initiator requests discovery session"); 953 } 954 } 955 956 if (ag->ag_type == AG_TYPE_DENY) { 957 login_send_error(request, 0x02, 0x01); 958 log_errx(1, "auth-type is \"deny\""); 959 } 960 961 if (ag->ag_type == AG_TYPE_UNKNOWN) { 962 /* 963 * This can happen with empty auth-group. 964 */ 965 login_send_error(request, 0x02, 0x01); 966 log_errx(1, "auth-type not set, denying access"); 967 } 968 969 /* 970 * Enforce initiator-name and initiator-portal. 971 */ 972 if (auth_name_check(ag, initiator_name) != 0) { 973 login_send_error(request, 0x02, 0x02); 974 log_errx(1, "initiator does not match allowed initiator names"); 975 } 976 977 if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) { 978 login_send_error(request, 0x02, 0x02); 979 log_errx(1, "initiator does not match allowed " 980 "initiator portals"); 981 } 982 983 /* 984 * Let's see if the initiator intends to do any kind of authentication 985 * at all. 986 */ 987 if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) { 988 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) { 989 login_send_error(request, 0x02, 0x01); 990 log_errx(1, "initiator skipped the authentication, " 991 "but authentication is required"); 992 } 993 994 keys_delete(request_keys); 995 996 log_debugx("initiator skipped the authentication, " 997 "and we don't need it; proceeding with negotiation"); 998 login_negotiate(conn, request); 999 return; 1000 } 1001 1002 fail = false; 1003 response = login_new_response(request); 1004 response_keys = keys_new(); 1005 trans = (bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0; 1006 auth_method = keys_find(request_keys, "AuthMethod"); 1007 if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) { 1008 log_debugx("authentication not required"); 1009 if (auth_method == NULL || 1010 login_list_contains(auth_method, "None")) { 1011 keys_add(response_keys, "AuthMethod", "None"); 1012 } else { 1013 log_warnx("initiator requests " 1014 "AuthMethod \"%s\" instead of \"None\"", 1015 auth_method); 1016 keys_add(response_keys, "AuthMethod", "Reject"); 1017 } 1018 if (trans) 1019 login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); 1020 } else { 1021 log_debugx("CHAP authentication required"); 1022 if (auth_method == NULL || 1023 login_list_contains(auth_method, "CHAP")) { 1024 keys_add(response_keys, "AuthMethod", "CHAP"); 1025 } else { 1026 log_warnx("initiator requests unsupported " 1027 "AuthMethod \"%s\" instead of \"CHAP\"", 1028 auth_method); 1029 keys_add(response_keys, "AuthMethod", "Reject"); 1030 fail = true; 1031 } 1032 } 1033 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 1034 if (conn->conn_target->t_alias != NULL) 1035 keys_add(response_keys, 1036 "TargetAlias", conn->conn_target->t_alias); 1037 keys_add_int(response_keys, 1038 "TargetPortalGroupTag", pg->pg_tag); 1039 } 1040 keys_save_pdu(response_keys, response); 1041 1042 pdu_send(response); 1043 pdu_delete(response); 1044 keys_delete(response_keys); 1045 pdu_delete(request); 1046 keys_delete(request_keys); 1047 1048 if (fail) { 1049 log_debugx("sent reject for AuthMethod; exiting"); 1050 exit(1); 1051 } 1052 1053 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) { 1054 login_chap(conn, ag); 1055 login_negotiate(conn, NULL); 1056 } else if (trans) { 1057 login_negotiate(conn, NULL); 1058 } else { 1059 login_wait_transition(conn); 1060 } 1061 } 1062