1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/cpuvar.h> 27 #include <sys/types.h> 28 #include <sys/conf.h> 29 #include <sys/file.h> 30 #include <sys/ddi.h> 31 #include <sys/sunddi.h> 32 #include <sys/modctl.h> 33 34 #include <sys/socket.h> 35 #include <sys/strsubr.h> 36 #include <sys/sysmacros.h> 37 #include <sys/note.h> 38 #include <sys/sdt.h> 39 40 #include <sys/stmf.h> 41 #include <sys/stmf_ioctl.h> 42 #include <sys/portif.h> 43 #include <sys/idm/idm.h> 44 #include <sys/idm/idm_text.h> 45 46 #define ISCSIT_LOGIN_SM_STRINGS 47 #include <iscsit.h> 48 #include <iscsit_auth.h> 49 50 typedef struct { 51 list_node_t le_ctx_node; 52 iscsit_login_event_t le_ctx_event; 53 idm_pdu_t *le_pdu; 54 } login_event_ctx_t; 55 56 #ifndef TRUE 57 #define TRUE B_TRUE 58 #endif 59 60 #ifndef FALSE 61 #define FALSE B_FALSE 62 #endif 63 64 #define DEFAULT_RADIUS_PORT 1812 65 66 static void 67 login_sm_complete(void *ict_void); 68 69 static void 70 login_sm_event_dispatch(iscsit_conn_login_t *lsm, iscsit_conn_t *ict, 71 login_event_ctx_t *ctx); 72 73 static void 74 login_sm_init(iscsit_conn_t *ict, login_event_ctx_t *ctx); 75 76 static void 77 login_sm_waiting(iscsit_conn_t *ict, login_event_ctx_t *ctx); 78 79 static void 80 login_sm_processing(iscsit_conn_t *ict, login_event_ctx_t *ctx); 81 82 static void 83 login_sm_responding(iscsit_conn_t *ict, login_event_ctx_t *ctx); 84 85 static void 86 login_sm_responded(iscsit_conn_t *ict, login_event_ctx_t *ctx); 87 88 static void 89 login_sm_ffp(iscsit_conn_t *ict, login_event_ctx_t *ctx); 90 91 static void 92 login_sm_done(iscsit_conn_t *ict, login_event_ctx_t *ctx); 93 94 static void 95 login_sm_error(iscsit_conn_t *ict, login_event_ctx_t *ctx); 96 97 static void 98 login_sm_new_state(iscsit_conn_t *ict, login_event_ctx_t *ctx, 99 iscsit_login_state_t new_state); 100 101 static void 102 login_sm_send_ack(iscsit_conn_t *ict, idm_pdu_t *pdu); 103 104 static idm_status_t 105 login_sm_validate_ack(iscsit_conn_t *ict, idm_pdu_t *pdu); 106 107 static boolean_t 108 login_sm_is_last_response(idm_pdu_t *pdu); 109 110 static void 111 login_sm_handle_initial_login(iscsit_conn_t *ict, idm_pdu_t *pdu); 112 113 static void 114 login_sm_send_next_response(iscsit_conn_t *ict, idm_pdu_t *pdu); 115 116 static void 117 login_sm_process_request(iscsit_conn_t *ict); 118 119 static idm_status_t 120 login_sm_req_pdu_check(iscsit_conn_t *ict, idm_pdu_t *pdu); 121 122 static idm_status_t 123 login_sm_process_nvlist(iscsit_conn_t *ict); 124 125 static idm_status_t 126 login_sm_check_security(iscsit_conn_t *ict); 127 128 static idm_pdu_t * 129 login_sm_build_login_response(iscsit_conn_t *ict); 130 131 static void 132 login_sm_ffp_actions(iscsit_conn_t *ict); 133 134 static idm_status_t 135 login_sm_validate_initial_parameters(iscsit_conn_t *ict); 136 137 static idm_status_t 138 login_sm_session_bind(iscsit_conn_t *ict); 139 140 static idm_status_t 141 login_sm_set_auth(iscsit_conn_t *ict); 142 143 static idm_status_t 144 login_sm_session_register(iscsit_conn_t *ict); 145 146 static kv_status_t 147 iscsit_handle_key(iscsit_conn_t *ict, nvpair_t *nvp, char *nvp_name); 148 149 static kv_status_t 150 iscsit_handle_common_key(iscsit_conn_t *ict, nvpair_t *nvp, 151 const idm_kv_xlate_t *ikvx); 152 153 static kv_status_t 154 iscsit_handle_security_key(iscsit_conn_t *ict, nvpair_t *nvp, 155 const idm_kv_xlate_t *ikvx); 156 157 static kv_status_t 158 iscsit_reply_security_key(iscsit_conn_t *ict); 159 160 static kv_status_t 161 iscsit_handle_operational_key(iscsit_conn_t *ict, nvpair_t *nvp, 162 const idm_kv_xlate_t *ikvx); 163 164 static kv_status_t 165 iscsit_reply_numerical(iscsit_conn_t *ict, 166 const char *nvp_name, const uint64_t value); 167 168 static kv_status_t 169 iscsit_reply_string(iscsit_conn_t *ict, 170 const char *nvp_name, const char *text); 171 172 static kv_status_t 173 iscsit_handle_digest(iscsit_conn_t *ict, nvpair_t *choices, 174 const idm_kv_xlate_t *ikvx); 175 176 static kv_status_t 177 iscsit_handle_boolean(iscsit_conn_t *ict, nvpair_t *nvp, boolean_t value, 178 const idm_kv_xlate_t *ikvx, boolean_t iscsit_value); 179 180 static kv_status_t 181 iscsit_handle_numerical(iscsit_conn_t *ict, nvpair_t *nvp, uint64_t value, 182 const idm_kv_xlate_t *ikvx, 183 uint64_t iscsi_min_value, uint64_t iscsi_max_value, 184 uint64_t iscsit_max_value); 185 186 static void 187 iscsit_process_negotiated_values(iscsit_conn_t *ict); 188 189 static void 190 login_resp_complete_cb(idm_pdu_t *pdu, idm_status_t status); 191 192 static idm_status_t 193 iscsit_add_declarative_keys(iscsit_conn_t *ict); 194 195 uint64_t max_dataseglen_target = ISCSIT_MAX_RECV_DATA_SEGMENT_LENGTH; 196 197 idm_status_t 198 iscsit_login_sm_init(iscsit_conn_t *ict) 199 { 200 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 201 202 bzero(lsm, sizeof (iscsit_conn_login_t)); 203 204 (void) nvlist_alloc(&lsm->icl_negotiated_values, NV_UNIQUE_NAME, 205 KM_SLEEP); 206 207 /* 208 * Hold connection until the login state machine completes 209 */ 210 iscsit_conn_hold(ict); 211 212 /* 213 * Pre-allocating a login response PDU means we will always be 214 * able to respond to a login request -- even if we can't allocate 215 * a data buffer to hold the text responses we can at least send 216 * a login failure. 217 */ 218 lsm->icl_login_resp_tmpl = kmem_zalloc(sizeof (iscsi_login_rsp_hdr_t), 219 KM_SLEEP); 220 221 idm_sm_audit_init(&lsm->icl_state_audit); 222 mutex_init(&lsm->icl_mutex, NULL, MUTEX_DEFAULT, NULL); 223 list_create(&lsm->icl_login_events, sizeof (login_event_ctx_t), 224 offsetof(login_event_ctx_t, le_ctx_node)); 225 list_create(&lsm->icl_pdu_list, sizeof (idm_pdu_t), 226 offsetof(idm_pdu_t, isp_client_lnd)); 227 228 lsm->icl_login_state = ILS_LOGIN_INIT; 229 lsm->icl_login_last_state = ILS_LOGIN_INIT; 230 231 /* 232 * Initialize operational parameters to default values. Anything 233 * we don't specifically negotiate stays at the default. 234 */ 235 ict->ict_op.op_discovery_session = B_FALSE; 236 ict->ict_op.op_initial_r2t = ISCSI_DEFAULT_INITIALR2T; 237 ict->ict_op.op_immed_data = ISCSI_DEFAULT_IMMEDIATE_DATA; 238 ict->ict_op.op_data_pdu_in_order = ISCSI_DEFAULT_DATA_PDU_IN_ORDER; 239 ict->ict_op.op_data_sequence_in_order = 240 ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER; 241 ict->ict_op.op_max_connections = ISCSI_DEFAULT_MAX_CONNECTIONS; 242 ict->ict_op.op_max_recv_data_segment_length = 243 ISCSI_DEFAULT_MAX_RECV_SEG_LEN; 244 ict->ict_op.op_max_burst_length = ISCSI_DEFAULT_MAX_BURST_LENGTH; 245 ict->ict_op.op_first_burst_length = ISCSI_DEFAULT_FIRST_BURST_LENGTH; 246 ict->ict_op.op_default_time_2_wait = ISCSI_DEFAULT_TIME_TO_WAIT; 247 ict->ict_op.op_default_time_2_retain = ISCSI_DEFAULT_TIME_TO_RETAIN; 248 ict->ict_op.op_max_outstanding_r2t = ISCSI_DEFAULT_MAX_OUT_R2T; 249 ict->ict_op.op_error_recovery_level = 250 ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL; 251 252 return (IDM_STATUS_SUCCESS); 253 } 254 255 static void 256 login_resp_complete_cb(idm_pdu_t *pdu, idm_status_t status) 257 { 258 iscsit_conn_t *ict = pdu->isp_private; 259 260 /* 261 * Check that this is a login pdu 262 */ 263 ASSERT((pdu->isp_flags & IDM_PDU_LOGIN_TX) != 0); 264 idm_pdu_free(pdu); 265 266 if ((status != IDM_STATUS_SUCCESS) || 267 (ict->ict_login_sm.icl_login_resp_err_class != 0)) { 268 /* 269 * Transport or login error occurred. 270 */ 271 iscsit_login_sm_event(ict, ILE_LOGIN_ERROR, NULL); 272 } 273 iscsit_conn_rele(ict); 274 } 275 276 void 277 iscsit_login_sm_fini(iscsit_conn_t *ict) 278 { 279 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 280 281 mutex_enter(&lsm->icl_mutex); 282 list_destroy(&lsm->icl_pdu_list); 283 list_destroy(&lsm->icl_login_events); 284 285 kmem_free(lsm->icl_login_resp_tmpl, sizeof (iscsi_login_rsp_hdr_t)); 286 287 /* clean up the login response idm text buffer */ 288 if (lsm->icl_login_resp_itb != NULL) { 289 idm_itextbuf_free(lsm->icl_login_resp_itb); 290 lsm->icl_login_resp_itb = NULL; 291 } 292 293 nvlist_free(lsm->icl_negotiated_values); 294 mutex_destroy(&lsm->icl_mutex); 295 } 296 297 void 298 iscsit_login_sm_event(iscsit_conn_t *ict, iscsit_login_event_t event, 299 idm_pdu_t *pdu) 300 { 301 /* 302 * This is a bit ugly but if we're already in ILS_LOGIN_ERROR 303 * or ILS_LOGIN_DONE then just drop any additional events. They 304 * won't change the state and it's possible we've already called 305 * iscsit_login_sm_fini in which case the mutex is destroyed. 306 */ 307 if ((ict->ict_login_sm.icl_login_state == ILS_LOGIN_ERROR) || 308 (ict->ict_login_sm.icl_login_state == ILS_LOGIN_DONE)) 309 return; 310 311 mutex_enter(&ict->ict_login_sm.icl_mutex); 312 iscsit_login_sm_event_locked(ict, event, pdu); 313 mutex_exit(&ict->ict_login_sm.icl_mutex); 314 } 315 void 316 iscsit_login_sm_event_locked(iscsit_conn_t *ict, iscsit_login_event_t event, 317 idm_pdu_t *pdu) 318 { 319 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 320 login_event_ctx_t *ctx; 321 322 ASSERT(mutex_owned(&lsm->icl_mutex)); 323 ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP); 324 325 ctx->le_ctx_event = event; 326 ctx->le_pdu = pdu; 327 328 list_insert_tail(&lsm->icl_login_events, ctx); 329 330 /* 331 * Use the icl_busy flag to keep the state machine single threaded. 332 * This also serves as recursion avoidance since this flag will 333 * always be set if we call login_sm_event from within the 334 * state machine code. 335 */ 336 if (!lsm->icl_busy) { 337 lsm->icl_busy = B_TRUE; 338 while (!list_is_empty(&lsm->icl_login_events)) { 339 ctx = list_head(&lsm->icl_login_events); 340 list_remove(&lsm->icl_login_events, ctx); 341 idm_sm_audit_event(&lsm->icl_state_audit, 342 SAS_ISCSIT_LOGIN, (int)lsm->icl_login_state, 343 (int)ctx->le_ctx_event, (uintptr_t)pdu); 344 345 /* 346 * If the lsm is in a terminal state, just drain 347 * any remaining events. 348 */ 349 if ((lsm->icl_login_state == ILS_LOGIN_ERROR) || 350 (lsm->icl_login_state == ILS_LOGIN_DONE)) { 351 kmem_free(ctx, sizeof (*ctx)); 352 continue; 353 } 354 mutex_exit(&lsm->icl_mutex); 355 login_sm_event_dispatch(lsm, ict, ctx); 356 mutex_enter(&lsm->icl_mutex); 357 } 358 lsm->icl_busy = B_FALSE; 359 360 /* 361 * When the state machine reaches ILS_LOGIN_DONE or 362 * ILS_LOGIN_ERROR state the login process has completed 363 * and it's time to cleanup. The state machine code will 364 * mark itself "complete" when this happens. 365 * 366 * To protect against spurious events (which shouldn't 367 * happen) set icl_busy again. 368 */ 369 if (lsm->icl_login_complete) { 370 lsm->icl_busy = B_TRUE; 371 if (taskq_dispatch(iscsit_global.global_dispatch_taskq, 372 login_sm_complete, ict, DDI_SLEEP) == NULL) { 373 cmn_err(CE_WARN, "iscsit_login_sm_event_locked:" 374 " Failed to dispatch task"); 375 } 376 } 377 } 378 } 379 380 static void 381 login_sm_complete(void *ict_void) 382 { 383 iscsit_conn_t *ict = ict_void; 384 385 /* 386 * State machine has run to completion, resources 387 * will be cleaned up when connection is destroyed. 388 */ 389 iscsit_conn_rele(ict); 390 } 391 392 static void 393 login_sm_event_dispatch(iscsit_conn_login_t *lsm, iscsit_conn_t *ict, 394 login_event_ctx_t *ctx) 395 { 396 idm_pdu_t *pdu = ctx->le_pdu; /* Only valid for some events */ 397 398 DTRACE_PROBE2(login__event, iscsit_conn_t *, ict, 399 login_event_ctx_t *, ctx); 400 401 IDM_SM_LOG(CE_NOTE, "login_sm_event_dispatch: ict %p event %s(%d)", 402 (void *)ict, 403 iscsit_ile_name[ctx->le_ctx_event], ctx->le_ctx_event); 404 405 /* State independent actions */ 406 switch (ctx->le_ctx_event) { 407 case ILE_LOGIN_RCV: 408 /* Perform basic sanity checks on the header */ 409 if (login_sm_req_pdu_check(ict, pdu) != IDM_STATUS_SUCCESS) { 410 idm_pdu_t *rpdu; 411 412 SET_LOGIN_ERROR(ict, ISCSI_STATUS_CLASS_INITIATOR_ERR, 413 ISCSI_LOGIN_STATUS_INVALID_REQUEST); 414 /* 415 * If we haven't processed any PDU's yet then use 416 * this one as a template for the response 417 */ 418 if (ict->ict_login_sm.icl_login_resp_tmpl->opcode == 0) 419 login_sm_handle_initial_login(ict, pdu); 420 rpdu = login_sm_build_login_response(ict); 421 login_sm_send_next_response(ict, rpdu); 422 idm_pdu_complete(pdu, IDM_STATUS_SUCCESS); 423 kmem_free(ctx, sizeof (*ctx)); 424 return; 425 } 426 break; 427 default: 428 break; 429 } 430 431 /* State dependent actions */ 432 switch (lsm->icl_login_state) { 433 case ILS_LOGIN_INIT: 434 login_sm_init(ict, ctx); 435 break; 436 case ILS_LOGIN_WAITING: 437 login_sm_waiting(ict, ctx); 438 break; 439 case ILS_LOGIN_PROCESSING: 440 login_sm_processing(ict, ctx); 441 break; 442 case ILS_LOGIN_RESPONDING: 443 login_sm_responding(ict, ctx); 444 break; 445 case ILS_LOGIN_RESPONDED: 446 login_sm_responded(ict, ctx); 447 break; 448 case ILS_LOGIN_FFP: 449 login_sm_ffp(ict, ctx); 450 break; 451 case ILS_LOGIN_DONE: 452 login_sm_done(ict, ctx); 453 break; 454 case ILS_LOGIN_ERROR: 455 login_sm_error(ict, ctx); 456 break; 457 } 458 459 kmem_free(ctx, sizeof (*ctx)); 460 } 461 462 static void 463 login_sm_init(iscsit_conn_t *ict, login_event_ctx_t *ctx) 464 { 465 idm_pdu_t *pdu; 466 467 switch (ctx->le_ctx_event) { 468 case ILE_LOGIN_RCV: 469 pdu = ctx->le_pdu; 470 471 /* 472 * This is the first login PDU we've received so use 473 * it to build the login response template and set our CSG. 474 */ 475 login_sm_handle_initial_login(ict, pdu); 476 477 /* 478 * Accumulate all the login PDU's that make up this 479 * request on a queue. 480 */ 481 mutex_enter(&ict->ict_login_sm.icl_mutex); 482 list_insert_tail(&ict->ict_login_sm.icl_pdu_list, pdu); 483 mutex_exit(&ict->ict_login_sm.icl_mutex); 484 485 if (pdu->isp_hdr->flags & ISCSI_FLAG_LOGIN_CONTINUE) { 486 login_sm_send_ack(ict, pdu); 487 login_sm_new_state(ict, ctx, ILS_LOGIN_WAITING); 488 } else { 489 login_sm_new_state(ict, ctx, ILS_LOGIN_PROCESSING); 490 } 491 break; 492 case ILE_LOGIN_CONN_ERROR: 493 case ILE_LOGIN_ERROR: 494 login_sm_new_state(ict, ctx, ILS_LOGIN_ERROR); 495 break; 496 default: 497 ASSERT(0); 498 } 499 } 500 501 static void 502 login_sm_waiting(iscsit_conn_t *ict, login_event_ctx_t *ctx) 503 { 504 idm_pdu_t *pdu; 505 506 switch (ctx->le_ctx_event) { 507 case ILE_LOGIN_RCV: 508 pdu = ctx->le_pdu; 509 mutex_enter(&ict->ict_login_sm.icl_mutex); 510 list_insert_tail(&ict->ict_login_sm.icl_pdu_list, pdu); 511 mutex_exit(&ict->ict_login_sm.icl_mutex); 512 if (!(pdu->isp_hdr->flags & ISCSI_FLAG_LOGIN_CONTINUE)) { 513 login_sm_new_state(ict, ctx, ILS_LOGIN_PROCESSING); 514 } else { 515 login_sm_send_ack(ict, pdu); 516 } 517 break; 518 case ILE_LOGIN_ERROR: 519 login_sm_new_state(ict, ctx, ILS_LOGIN_ERROR); 520 break; 521 case ILE_LOGIN_RESP_COMPLETE: 522 break; 523 default: 524 ASSERT(0); 525 } 526 } 527 528 static void 529 login_sm_processing(iscsit_conn_t *ict, login_event_ctx_t *ctx) 530 { 531 switch (ctx->le_ctx_event) { 532 case ILE_LOGIN_RESP_READY: 533 login_sm_new_state(ict, ctx, ILS_LOGIN_RESPONDING); 534 break; 535 case ILE_LOGIN_RCV: 536 idm_pdu_complete(ctx->le_pdu, IDM_STATUS_SUCCESS); 537 /*FALLTHROUGH*/ 538 case ILE_LOGIN_CONN_ERROR: 539 case ILE_LOGIN_ERROR: 540 login_sm_new_state(ict, ctx, ILS_LOGIN_ERROR); 541 break; 542 default: 543 ASSERT(0); 544 } 545 } 546 547 static void 548 login_sm_responding(iscsit_conn_t *ict, login_event_ctx_t *ctx) 549 { 550 idm_pdu_t *pdu, *rpdu; 551 552 switch (ctx->le_ctx_event) { 553 case ILE_LOGIN_RCV: 554 pdu = ctx->le_pdu; 555 /* 556 * We should only be in "responding" state if we have not 557 * sent the last PDU of a multi-PDU login response sequence. 558 * In that case we expect this received PDU to be an 559 * acknowledgement from the initiator (login PDU with C 560 * bit cleared and no data). If it's the acknowledgement 561 * we are expecting then we send the next PDU in the login 562 * response sequence. Otherwise it's a protocol error and 563 * the login fails. 564 */ 565 if (login_sm_validate_ack(ict, pdu) == IDM_STATUS_SUCCESS) { 566 rpdu = login_sm_build_login_response(ict); 567 login_sm_send_next_response(ict, rpdu); 568 } else { 569 login_sm_new_state(ict, ctx, ILS_LOGIN_ERROR); 570 } 571 idm_pdu_complete(pdu, IDM_STATUS_SUCCESS); 572 break; 573 case ILE_LOGIN_FFP: 574 login_sm_new_state(ict, ctx, ILS_LOGIN_FFP); 575 break; 576 case ILE_LOGIN_RESP_COMPLETE: 577 login_sm_new_state(ict, ctx, ILS_LOGIN_RESPONDED); 578 break; 579 case ILE_LOGIN_CONN_ERROR: 580 case ILE_LOGIN_ERROR: 581 login_sm_new_state(ict, ctx, ILS_LOGIN_ERROR); 582 break; 583 default: 584 ASSERT(0); 585 } 586 } 587 588 static void 589 login_sm_responded(iscsit_conn_t *ict, login_event_ctx_t *ctx) 590 { 591 idm_pdu_t *pdu; 592 iscsi_login_hdr_t *lh; 593 594 switch (ctx->le_ctx_event) { 595 case ILE_LOGIN_RCV: 596 pdu = ctx->le_pdu; 597 lh = (iscsi_login_hdr_t *)pdu->isp_hdr; 598 /* 599 * Set the CSG, NSG and Transit bits based on the this PDU. 600 * The CSG already validated in login_sm_req_pdu_check(). 601 * We'll clear the transit bit if we encounter any login 602 * parameters in the request that required an additional 603 * login transfer (i.e. no acceptable 604 * choices in range or we needed to change a boolean 605 * value from "Yes" to "No"). 606 */ 607 ict->ict_login_sm.icl_login_csg = 608 ISCSI_LOGIN_CURRENT_STAGE(lh->flags); 609 ict->ict_login_sm.icl_login_nsg = 610 ISCSI_LOGIN_NEXT_STAGE(lh->flags); 611 ict->ict_login_sm.icl_login_transit = 612 lh->flags & ISCSI_FLAG_LOGIN_TRANSIT; 613 mutex_enter(&ict->ict_login_sm.icl_mutex); 614 list_insert_tail(&ict->ict_login_sm.icl_pdu_list, pdu); 615 mutex_exit(&ict->ict_login_sm.icl_mutex); 616 if (pdu->isp_hdr->flags & ISCSI_FLAG_LOGIN_CONTINUE) { 617 login_sm_send_ack(ict, pdu); 618 login_sm_new_state(ict, ctx, ILS_LOGIN_WAITING); 619 } else { 620 login_sm_new_state(ict, ctx, ILS_LOGIN_PROCESSING); 621 } 622 break; 623 case ILE_LOGIN_CONN_ERROR: 624 case ILE_LOGIN_ERROR: 625 login_sm_new_state(ict, ctx, ILS_LOGIN_ERROR); 626 break; 627 default: 628 ASSERT(0); 629 } 630 } 631 632 static void 633 login_sm_ffp(iscsit_conn_t *ict, login_event_ctx_t *ctx) 634 { 635 switch (ctx->le_ctx_event) { 636 case ILE_LOGIN_RESP_COMPLETE: 637 login_sm_new_state(ict, ctx, ILS_LOGIN_DONE); 638 break; 639 case ILE_LOGIN_CONN_ERROR: 640 case ILE_LOGIN_ERROR: 641 login_sm_new_state(ict, ctx, ILS_LOGIN_ERROR); 642 break; 643 default: 644 ASSERT(0); 645 } 646 647 } 648 649 /*ARGSUSED*/ 650 static void 651 login_sm_done(iscsit_conn_t *ict, login_event_ctx_t *ctx) 652 { 653 /* Terminal state, we should get no events */ 654 switch (ctx->le_ctx_event) { 655 case ILE_LOGIN_RCV: 656 /* 657 * We've already processed everything we're going to 658 * process. Drop any additional login PDU's. 659 */ 660 idm_pdu_complete(ctx->le_pdu, IDM_STATUS_SUCCESS); 661 break; 662 case ILE_LOGIN_CONN_ERROR: 663 /* Don't care */ 664 break; 665 default: 666 ASSERT(0); 667 } 668 } 669 670 /*ARGSUSED*/ 671 static void 672 login_sm_error(iscsit_conn_t *ict, login_event_ctx_t *ctx) 673 { 674 switch (ctx->le_ctx_event) { 675 case ILE_LOGIN_RCV: 676 /* 677 * We've already processed everything we're going to 678 * process. Drop any additional login PDU's. 679 */ 680 idm_pdu_complete(ctx->le_pdu, IDM_STATUS_SUCCESS); 681 break; 682 case ILE_LOGIN_CONN_ERROR: 683 /* Don't care */ 684 break; 685 default: 686 ASSERT(0); 687 } 688 } 689 690 static void 691 login_sm_new_state(iscsit_conn_t *ict, login_event_ctx_t *ctx, 692 iscsit_login_state_t new_state) 693 { 694 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 695 idm_pdu_t *rpdu; 696 697 /* 698 * Validate new state 699 */ 700 ASSERT(new_state != ILS_UNDEFINED); 701 ASSERT3U(new_state, <, ILS_MAX_STATE); 702 703 new_state = (new_state < ILS_MAX_STATE) ? 704 new_state : ILS_UNDEFINED; 705 706 IDM_SM_LOG(CE_NOTE, "login_sm_new_state: conn %p " 707 "%s (%d) --> %s (%d)\n", (void *)ict->ict_ic, 708 iscsit_ils_name[lsm->icl_login_state], lsm->icl_login_state, 709 iscsit_ils_name[new_state], new_state); 710 711 DTRACE_PROBE3(login__state__change, 712 iscsit_conn_t *, ict, login_event_ctx_t *, ctx, 713 iscsit_login_state_t, new_state); 714 715 mutex_enter(&lsm->icl_mutex); 716 idm_sm_audit_state_change(&lsm->icl_state_audit, SAS_ISCSIT_LOGIN, 717 (int)lsm->icl_login_state, (int)new_state); 718 lsm->icl_login_last_state = lsm->icl_login_state; 719 lsm->icl_login_state = new_state; 720 mutex_exit(&lsm->icl_mutex); 721 722 switch (lsm->icl_login_state) { 723 case ILS_LOGIN_WAITING: 724 /* Do nothing, waiting for more login PDU's */ 725 break; 726 case ILS_LOGIN_PROCESSING: 727 /* All login PDU's received, process login request */ 728 login_sm_process_request(ict); 729 break; 730 case ILS_LOGIN_RESPONDING: 731 rpdu = login_sm_build_login_response(ict); 732 login_sm_send_next_response(ict, rpdu); 733 break; 734 case ILS_LOGIN_RESPONDED: 735 /* clean up the login response idm text buffer */ 736 if (lsm->icl_login_resp_itb != NULL) { 737 idm_itextbuf_free(lsm->icl_login_resp_itb); 738 lsm->icl_login_resp_itb = NULL; 739 } 740 break; 741 case ILS_LOGIN_FFP: 742 login_sm_ffp_actions(ict); 743 break; 744 case ILS_LOGIN_DONE: 745 case ILS_LOGIN_ERROR: 746 /* 747 * Flag the terminal state for the dispatcher 748 */ 749 lsm->icl_login_complete = B_TRUE; 750 break; 751 case ILS_LOGIN_INIT: /* Initial state, can't return */ 752 default: 753 ASSERT(0); 754 /*NOTREACHED*/ 755 } 756 } 757 758 /*ARGSUSED*/ 759 static void 760 login_sm_send_ack(iscsit_conn_t *ict, idm_pdu_t *pdu) 761 { 762 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 763 idm_pdu_t *lack; 764 765 /* 766 * allocate the response pdu 767 */ 768 lack = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0); 769 idm_pdu_init(lack, ict->ict_ic, ict, login_resp_complete_cb); 770 lack->isp_flags |= IDM_PDU_LOGIN_TX; 771 772 /* 773 * copy the response template into the response pdu 774 */ 775 bcopy(lsm->icl_login_resp_tmpl, lack->isp_hdr, sizeof (iscsi_hdr_t)); 776 777 iscsit_conn_hold(ict); 778 idm_pdu_tx(lack); 779 } 780 781 /*ARGSUSED*/ 782 static idm_status_t 783 login_sm_validate_ack(iscsit_conn_t *ict, idm_pdu_t *pdu) 784 { 785 iscsi_hdr_t *ihp = pdu->isp_hdr; 786 if (ihp->flags & ISCSI_FLAG_TEXT_CONTINUE) { 787 return (IDM_STATUS_FAIL); 788 } 789 if (ntoh24(ihp->dlength) != 0) { 790 return (IDM_STATUS_FAIL); 791 } 792 return (IDM_STATUS_SUCCESS); 793 } 794 795 static boolean_t 796 login_sm_is_last_response(idm_pdu_t *pdu) 797 { 798 799 if (pdu->isp_hdr->flags & ISCSI_FLAG_LOGIN_CONTINUE) { 800 return (B_FALSE); 801 } 802 return (B_TRUE); 803 } 804 805 806 static void 807 login_sm_handle_initial_login(iscsit_conn_t *ict, idm_pdu_t *pdu) 808 { 809 iscsi_login_hdr_t *lh_req = (iscsi_login_hdr_t *)pdu->isp_hdr; 810 iscsi_login_rsp_hdr_t *lh_resp = 811 ict->ict_login_sm.icl_login_resp_tmpl; 812 813 /* 814 * First login PDU, this connection should not have a sesssion 815 * associated. 816 */ 817 ASSERT(ict->ict_sess == NULL); 818 819 /* 820 * Save off TSIH and ISID for later use in finding a session 821 */ 822 ict->ict_login_sm.icl_cmdsn = ntohl(lh_req->cmdsn); 823 ict->ict_login_sm.icl_tsih = ntohs(lh_req->tsid); 824 bcopy(lh_req->isid, ict->ict_login_sm.icl_isid, ISCSI_ISID_LEN); 825 826 /* 827 * We'll need the CID as well 828 */ 829 ict->ict_cid = ntohs(lh_req->cid); 830 831 /* 832 * Set the CSG, NSG and Transit bits based on the first PDU 833 * in the login sequence. The CSG already validated in 834 * login_sm_req_pdu_check(). We'll clear the transit bit if 835 * we encounter any login parameters in the request that 836 * required an additional login transfer (i.e. no acceptable 837 * choices in range or we needed to change a boolean 838 * value from "Yes" to "No"). 839 */ 840 ict->ict_login_sm.icl_login_csg = 841 ISCSI_LOGIN_CURRENT_STAGE(lh_req->flags); 842 ict->ict_login_sm.icl_login_nsg = 843 ISCSI_LOGIN_NEXT_STAGE(lh_req->flags); 844 ict->ict_login_sm.icl_login_transit = 845 lh_req->flags & ISCSI_FLAG_LOGIN_TRANSIT; 846 847 /* 848 * Initialize header for login reject response. This will also 849 * be copied for use as a template for other login responses 850 */ 851 lh_resp->opcode = ISCSI_OP_LOGIN_RSP; 852 lh_resp->max_version = ISCSIT_MAX_VERSION; 853 854 /* 855 * We already validated that we can support one of the initiator's 856 * versions in login_sm_req_pdu_check(). 857 */ 858 #if (ISCSIT_MAX_VERSION > 0) 859 if (ISCSIT_MAX_VERSION >= lh_req->min_version) { 860 lh_resp->active_version = 861 MIN(lh_req->max_version, ISCSIT_MAX_VERSION); 862 } else { 863 ASSERT(ISCSIT_MAX_VERSION <= lh_req->max_version); 864 lh_resp->active_version = ISCSIT_MAX_VERSION; 865 } 866 #endif 867 868 lh_resp->hlength = 0; /* No AHS */ 869 bcopy(lh_req->isid, lh_resp->isid, ISCSI_ISID_LEN); 870 lh_resp->tsid = lh_req->tsid; 871 lh_resp->itt = lh_req->itt; 872 873 /* 874 * StatSn, ExpCmdSn and MaxCmdSn will be set immediately before 875 * transmission 876 */ 877 } 878 879 static void 880 login_sm_send_next_response(iscsit_conn_t *ict, idm_pdu_t *pdu) 881 { 882 iscsi_login_rsp_hdr_t *lh_resp = (iscsi_login_rsp_hdr_t *)pdu->isp_hdr; 883 884 /* Make sure this PDU is part of the login phase */ 885 ASSERT((pdu->isp_flags & IDM_PDU_LOGIN_TX) != 0); 886 887 /* 888 * Fill in header values 889 */ 890 hton24(lh_resp->dlength, pdu->isp_datalen); 891 892 /* 893 * If this is going to be the last PDU of a login response 894 * that moves us to FFP then generate the ILE_LOGIN_FFP event. 895 */ 896 if (lh_resp->status_class == ISCSI_STATUS_CLASS_SUCCESS) { 897 ASSERT(ict->ict_sess != NULL); 898 899 if ((lh_resp->flags & ISCSI_FLAG_LOGIN_TRANSIT) && 900 (ISCSI_LOGIN_NEXT_STAGE(lh_resp->flags) == 901 ISCSI_FULL_FEATURE_PHASE) && 902 !(lh_resp->flags & ISCSI_FLAG_LOGIN_CONTINUE)) { 903 iscsit_login_sm_event(ict, ILE_LOGIN_FFP, NULL); 904 } 905 if (login_sm_is_last_response(pdu) == B_TRUE) { 906 /* 907 * The last of a potentially mult-PDU response finished. 908 */ 909 iscsit_login_sm_event(ict, ILE_LOGIN_RESP_COMPLETE, 910 NULL); 911 } 912 913 iscsit_conn_hold(ict); 914 iscsit_pdu_tx(pdu); 915 } else { 916 /* 917 * If status_class != ISCSI_STATUS_CLASS_SUCCESS then 918 * StatSN is not valid and we can call idm_pdu_tx instead 919 * of iscsit_pdu_tx. This is very good thing since in 920 * some cases of login failure we may not have a session. 921 * Since iscsit_calc_rspsn grabs the session mutex while 922 * it is retrieving values for expcmdsn and maxcmdsn this 923 * would cause a panic. 924 * 925 * Since we still want a value for expcmdsn, fill in an 926 * appropriate value based on the login request before 927 * sending the response. Cmdsn/expcmdsn do not advance during 928 * login phase. 929 */ 930 lh_resp->expcmdsn = htonl(ict->ict_login_sm.icl_cmdsn); 931 lh_resp->maxcmdsn = htonl(ict->ict_login_sm.icl_cmdsn + 1); 932 933 iscsit_conn_hold(ict); 934 idm_pdu_tx(pdu); 935 } 936 937 } 938 939 static void 940 login_sm_process_request(iscsit_conn_t *ict) 941 { 942 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 943 uint8_t error_class = 0; 944 uint8_t error_detail = 0; 945 946 /* 947 * First walk all the PDU's that make up this login request 948 * and compile all the iSCSI key-value pairs into nvlist format. 949 */ 950 951 ASSERT(lsm->icl_request_nvlist == NULL); 952 /* create an nvlist for request key/value pairs */ 953 if (idm_pdu_list_to_nvlist(&lsm->icl_pdu_list, 954 &lsm->icl_request_nvlist, &error_detail) != IDM_STATUS_SUCCESS) { 955 error_class = ISCSI_STATUS_CLASS_TARGET_ERR; 956 SET_LOGIN_ERROR(ict, error_class, error_detail); 957 goto request_fail; 958 } 959 960 /* Allocate a new nvlist for response key/value pairs */ 961 ASSERT(lsm->icl_response_nvlist == NULL); 962 if (nvlist_alloc(&lsm->icl_response_nvlist, NV_UNIQUE_NAME, 963 KM_NOSLEEP) != 0) { 964 error_class = ISCSI_STATUS_CLASS_TARGET_ERR; 965 error_detail = ISCSI_LOGIN_STATUS_NO_RESOURCES; 966 SET_LOGIN_ERROR(ict, error_class, error_detail); 967 goto request_fail; 968 } 969 970 /* 971 * This would be a very good time to make sure we have 972 * negotiated the required values for the login phase. For 973 * example we definitely should have defined InitiatorName, 974 * and Target name regardless of our current login phase. 975 */ 976 if (!ict->ict_op.op_initial_params_set) { 977 if (login_sm_validate_initial_parameters(ict) != 978 IDM_STATUS_SUCCESS) { 979 goto request_fail; 980 } 981 982 /* 983 * Now setup our session association. This includes 984 * create a new session or looking up an existing session, 985 * and if this is not a discovery session then we will 986 * also register this session with STMF. 987 */ 988 if (login_sm_session_bind(ict) != IDM_STATUS_SUCCESS) { 989 goto request_fail; 990 } 991 992 if (login_sm_set_auth(ict) != IDM_STATUS_SUCCESS) { 993 goto request_fail; 994 } 995 996 /* 997 * Prepend TargetAlias and PortalGroupTag 998 */ 999 if (ict->ict_op.op_discovery_session == B_FALSE) { 1000 if ((lsm->icl_auth.ca_tgt_alias[0]) != '\0') { 1001 (void) iscsit_reply_string(ict, 1002 "TargetAlias", 1003 &lsm->icl_auth.ca_tgt_alias[0]); 1004 } 1005 (void) iscsit_reply_numerical(ict, 1006 "TargetPortalGroupTag", 1007 (uint64_t)lsm->icl_tpgt_tag); 1008 if (iscsit_add_declarative_keys(ict) != 1009 IDM_STATUS_SUCCESS) { 1010 goto request_fail; 1011 } 1012 } 1013 1014 ict->ict_op.op_initial_params_set = B_TRUE; 1015 } 1016 1017 if (login_sm_process_nvlist(ict) != IDM_STATUS_SUCCESS) { 1018 goto request_fail; 1019 } 1020 1021 if (login_sm_check_security(ict) != IDM_STATUS_SUCCESS) { 1022 goto request_fail; 1023 } 1024 1025 /* clean up request_nvlist */ 1026 if (lsm->icl_request_nvlist != NULL) { 1027 nvlist_free(lsm->icl_request_nvlist); 1028 lsm->icl_request_nvlist = NULL; 1029 } 1030 1031 /* convert any responses to textbuf form */ 1032 ASSERT(lsm->icl_login_resp_itb == NULL); 1033 if (lsm->icl_response_nvlist) { 1034 lsm->icl_login_resp_itb = idm_nvlist_to_itextbuf( 1035 lsm->icl_response_nvlist); 1036 if (lsm->icl_login_resp_itb == NULL) { 1037 /* Still need to send the resp so continue */ 1038 SET_LOGIN_ERROR(ict, 1039 ISCSI_STATUS_CLASS_TARGET_ERR, 1040 ISCSI_LOGIN_STATUS_NO_RESOURCES); 1041 } 1042 /* clean up response_nvlist */ 1043 nvlist_free(lsm->icl_response_nvlist); 1044 lsm->icl_response_nvlist = NULL; 1045 } 1046 1047 /* tell the state machine to send the textbuf */ 1048 iscsit_login_sm_event(ict, ILE_LOGIN_RESP_READY, NULL); 1049 return; 1050 1051 request_fail: 1052 1053 /* clean up request_nvlist and response_nvlist */ 1054 if (lsm->icl_request_nvlist != NULL) { 1055 nvlist_free(lsm->icl_request_nvlist); 1056 lsm->icl_request_nvlist = NULL; 1057 } 1058 if (lsm->icl_response_nvlist != NULL) { 1059 nvlist_free(lsm->icl_response_nvlist); 1060 lsm->icl_response_nvlist = NULL; 1061 } 1062 } 1063 1064 1065 static void 1066 login_sm_ffp_actions(iscsit_conn_t *ict) 1067 { 1068 iscsit_process_negotiated_values(ict); 1069 } 1070 1071 static idm_status_t 1072 login_sm_validate_initial_parameters(iscsit_conn_t *ict) 1073 { 1074 int nvrc; 1075 char *string_val; 1076 uint8_t error_class = ISCSI_STATUS_CLASS_INITIATOR_ERR; 1077 uint8_t error_detail = ISCSI_LOGIN_STATUS_MISSING_FIELDS; 1078 idm_status_t status = IDM_STATUS_FAIL; 1079 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 1080 1081 /* 1082 * Make sure we received the required information from the initial 1083 * login. Add these declaratives to the negotiated list and 1084 * remove them from the request list as we go. If anything fails, 1085 * the caller will clean-up the nvlists. 1086 */ 1087 1088 /* 1089 * Initiator name 1090 */ 1091 if ((nvrc = nvlist_lookup_string(lsm->icl_request_nvlist, 1092 "InitiatorName", &string_val)) != 0) { 1093 goto initial_params_done; 1094 } 1095 if ((nvrc = nvlist_add_string(lsm->icl_negotiated_values, 1096 "InitiatorName", string_val)) != 0) { 1097 goto initial_params_done; 1098 } 1099 if ((nvrc = nvlist_lookup_string(lsm->icl_negotiated_values, 1100 "InitiatorName", &string_val)) != 0) { 1101 goto initial_params_done; 1102 } 1103 lsm->icl_initiator_name = string_val; 1104 idm_conn_set_initiator_name(ict->ict_ic, lsm->icl_initiator_name); 1105 if ((nvrc = nvlist_remove(lsm->icl_request_nvlist, 1106 "InitiatorName", DATA_TYPE_STRING)) != 0) { 1107 goto initial_params_done; 1108 } 1109 1110 /* 1111 * Session type 1112 */ 1113 ict->ict_op.op_discovery_session = B_FALSE; 1114 nvrc = nvlist_lookup_string(lsm->icl_request_nvlist, 1115 "SessionType", &string_val); 1116 if (nvrc != ENOENT && nvrc != 0) { 1117 goto initial_params_done; 1118 } 1119 if (nvrc == 0) { 1120 if (strcmp(string_val, "Discovery") == 0) { 1121 ict->ict_op.op_discovery_session = B_TRUE; 1122 } else if (strcmp(string_val, "Normal") != 0) { 1123 goto initial_params_done; 1124 } 1125 if ((nvrc = nvlist_add_string(lsm->icl_negotiated_values, 1126 "SessionType", string_val)) != 0) { 1127 goto initial_params_done; 1128 } 1129 if ((nvrc = nvlist_remove(lsm->icl_request_nvlist, 1130 "SessionType", DATA_TYPE_STRING)) != 0) { 1131 goto initial_params_done; 1132 } 1133 } 1134 1135 /* 1136 * Must have either TargetName or SessionType==Discovery 1137 */ 1138 lsm->icl_target_name = NULL; 1139 nvrc = nvlist_lookup_string(lsm->icl_request_nvlist, 1140 "TargetName", &string_val); 1141 if (nvrc != ENOENT && nvrc != 0) { 1142 goto initial_params_done; 1143 } 1144 if (nvrc == 0) { 1145 if ((nvrc = nvlist_add_string(lsm->icl_negotiated_values, 1146 "TargetName", string_val)) != 0) { 1147 goto initial_params_done; 1148 } 1149 if ((nvrc = nvlist_lookup_string(lsm->icl_negotiated_values, 1150 "TargetName", &string_val)) != 0) { 1151 goto initial_params_done; 1152 } 1153 lsm->icl_target_name = string_val; 1154 idm_conn_set_target_name(ict->ict_ic, lsm->icl_target_name); 1155 if ((nvrc = nvlist_remove(lsm->icl_request_nvlist, 1156 "TargetName", DATA_TYPE_STRING)) != 0) { 1157 goto initial_params_done; 1158 } 1159 } else if (ict->ict_op.op_discovery_session == B_FALSE) { 1160 /* 1161 * Missing target name 1162 */ 1163 goto initial_params_done; 1164 } 1165 1166 idm_conn_set_isid(ict->ict_ic, lsm->icl_isid); 1167 (void) snprintf(ict->ict_ic->ic_tsih, ISCSI_MAX_TSIH_LEN + 1, "0x%04x", 1168 lsm->icl_tsih); 1169 1170 IDM_SM_LOG(CE_NOTE, "conn %p: initiator=%s", (void *)ict->ict_ic, 1171 (lsm->icl_initiator_name == NULL) ? "N/A" : 1172 lsm->icl_initiator_name); 1173 IDM_SM_LOG(CE_NOTE, "conn %p: target=%s", (void *)ict->ict_ic, 1174 (lsm->icl_target_name == NULL) ? "N/A" : 1175 lsm->icl_target_name); 1176 IDM_SM_LOG(CE_NOTE, "conn %p: sessiontype=%s", (void *)ict->ict_ic, 1177 ict->ict_op.op_discovery_session ? "Discovery" : "Normal"); 1178 1179 /* Sucess */ 1180 status = IDM_STATUS_SUCCESS; 1181 error_class = ISCSI_STATUS_CLASS_SUCCESS; 1182 error_detail = ISCSI_LOGIN_STATUS_ACCEPT; 1183 1184 initial_params_done: 1185 SET_LOGIN_ERROR(ict, error_class, error_detail); 1186 return (status); 1187 } 1188 1189 1190 /* 1191 * login_sm_session_bind 1192 * 1193 * This function looks at the data from the initial login request 1194 * of a new connection and either looks up and existing session, 1195 * creates a new session, or returns an error. RFC3720 section 5.3.1 1196 * defines these rules: 1197 * 1198 * +------------------------------------------------------------------+ 1199 * |ISID | TSIH | CID | Target action | 1200 * +------------------------------------------------------------------+ 1201 * |new | non-zero | any | fail the login | 1202 * | | | | ("session does not exist") | 1203 * +------------------------------------------------------------------+ 1204 * |new | zero | any | instantiate a new session | 1205 * +------------------------------------------------------------------+ 1206 * |existing | zero | any | do session reinstatement | 1207 * | | | | (see section 5.3.5) | 1208 * +------------------------------------------------------------------+ 1209 * |existing | non-zero | new | add a new connection to | 1210 * | | existing | | the session | 1211 * +------------------------------------------------------------------+ 1212 * |existing | non-zero |existing| do connection reinstatement| 1213 * | | existing | | (see section 5.3.4) | 1214 * +------------------------------------------------------------------+ 1215 * |existing | non-zero | any | fail the login | 1216 * | | new | | ("session does not exist") | 1217 * +------------------------------------------------------------------+ 1218 * 1219 */ 1220 1221 /* 1222 * Map an <ipv6,port> address to an <ipv4,port> address if possible. 1223 * Returns: 1224 * 1 - success 1225 * 0 - address not mapable 1226 */ 1227 1228 static int 1229 iscsit_is_v4_mapped(struct sockaddr_storage *sa, struct sockaddr_storage *v4sa) 1230 { 1231 struct sockaddr_in *sin; 1232 struct in_addr *in; 1233 struct sockaddr_in6 *sin6; 1234 struct in6_addr *in6; 1235 int ret = 0; 1236 1237 sin6 = (struct sockaddr_in6 *)sa; 1238 in6 = &sin6->sin6_addr; 1239 if ((sa->ss_family == AF_INET6) && 1240 (IN6_IS_ADDR_V4MAPPED(in6) || IN6_IS_ADDR_V4COMPAT(in6))) { 1241 sin = (struct sockaddr_in *)v4sa; 1242 in = &sin->sin_addr; 1243 v4sa->ss_family = AF_INET; 1244 sin->sin_port = sin6->sin6_port; 1245 IN6_V4MAPPED_TO_INADDR(in6, in); 1246 ret = 1; 1247 } 1248 return (ret); 1249 } 1250 1251 static idm_status_t 1252 login_sm_session_bind(iscsit_conn_t *ict) 1253 { 1254 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 1255 iscsit_tgt_t *tgt = NULL; 1256 iscsit_tpgt_t *tpgt = NULL; 1257 iscsit_portal_t *portal = NULL; 1258 iscsit_sess_t *existing_sess = NULL; 1259 iscsit_sess_t *new_sess = NULL; 1260 iscsit_conn_t *existing_ict = NULL; 1261 uint8_t error_class; 1262 uint8_t error_detail; 1263 1264 /* 1265 * Look up target and then check if there are sessions or connections 1266 * that match this request (see below). Any holds taken on objects 1267 * must be released at the end of the function (let's keep things 1268 * simple). 1269 * 1270 * If target name is set then we should have a corresponding target 1271 * context configured. 1272 */ 1273 if (lsm->icl_target_name != NULL) { 1274 /* 1275 * iscsit_tgt_lookup implicitly takes a ref on the target 1276 */ 1277 ISCSIT_GLOBAL_LOCK(RW_READER); 1278 tgt = iscsit_tgt_lookup_locked(lsm->icl_target_name); 1279 if (tgt == NULL) { 1280 ISCSIT_GLOBAL_UNLOCK(); 1281 SET_LOGIN_ERROR(ict, ISCSI_STATUS_CLASS_INITIATOR_ERR, 1282 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND); 1283 goto session_bind_error; 1284 } else { 1285 mutex_enter(&tgt->target_mutex); 1286 tpgt = avl_first(&tgt->target_tpgt_list); 1287 1288 if (IS_DEFAULT_TPGT(tpgt)) { 1289 lsm->icl_tpgt_tag = ISCSIT_DEFAULT_TPGT; 1290 } else { 1291 /* 1292 * Find the portal group tag for the 1293 * login response. 1294 */ 1295 struct sockaddr_storage v4sa, *sa; 1296 1297 sa = &ict->ict_ic->ic_laddr; 1298 portal = iscsit_tgt_lookup_portal(tgt, 1299 sa, &tpgt); 1300 if (portal == NULL && 1301 iscsit_is_v4_mapped(sa, &v4sa)) { 1302 /* 1303 * Try again if the local address 1304 * was v6 mappable to v4. 1305 */ 1306 portal = iscsit_tgt_lookup_portal(tgt, 1307 &v4sa, &tpgt); 1308 1309 } 1310 if (portal == NULL) { 1311 /* 1312 * Initiator came in on wrong address 1313 */ 1314 SET_LOGIN_ERROR(ict, 1315 ISCSI_STATUS_CLASS_INITIATOR_ERR, 1316 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND); 1317 mutex_exit(&tgt->target_mutex); 1318 ISCSIT_GLOBAL_UNLOCK(); 1319 goto session_bind_error; 1320 } 1321 1322 /* 1323 * Need to release holds on the portal and 1324 * tpgt after processing is complete. 1325 */ 1326 lsm->icl_tpgt_tag = tpgt->tpgt_tag; 1327 iscsit_portal_rele(portal); 1328 iscsit_tpgt_rele(tpgt); 1329 } 1330 1331 if ((tgt->target_state != TS_STMF_ONLINE) || 1332 ((iscsit_global.global_svc_state != ISE_ENABLED) && 1333 ((iscsit_global.global_svc_state != ISE_BUSY)))) { 1334 SET_LOGIN_ERROR(ict, 1335 ISCSI_STATUS_CLASS_TARGET_ERR, 1336 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1337 mutex_exit(&tgt->target_mutex); 1338 ISCSIT_GLOBAL_UNLOCK(); 1339 goto session_bind_error; 1340 } 1341 mutex_exit(&tgt->target_mutex); 1342 ISCSIT_GLOBAL_UNLOCK(); 1343 } 1344 } 1345 1346 ASSERT((tgt != NULL) || (ict->ict_op.op_discovery_session == B_TRUE)); 1347 1348 /* 1349 * Check if there is an existing session matching this ISID. If 1350 * tgt == NULL then we'll look for the session on the global list 1351 * of discovery session. If we find a session then the ISID 1352 * exists. 1353 */ 1354 existing_sess = iscsit_tgt_lookup_sess(tgt, lsm->icl_initiator_name, 1355 lsm->icl_isid, lsm->icl_tsih, lsm->icl_tpgt_tag); 1356 if (existing_sess != NULL) { 1357 existing_ict = iscsit_sess_lookup_conn(existing_sess, 1358 ict->ict_cid); 1359 } 1360 1361 /* 1362 * If this is a discovery session, make sure it has appropriate 1363 * parameters. 1364 */ 1365 if ((ict->ict_op.op_discovery_session == B_TRUE) && 1366 ((lsm->icl_tsih != ISCSI_UNSPEC_TSIH) || (existing_sess != NULL))) { 1367 /* XXX Do we need to check for existing ISID (sess != NULL)? */ 1368 SET_LOGIN_ERROR(ict, ISCSI_STATUS_CLASS_INITIATOR_ERR, 1369 ISCSI_LOGIN_STATUS_INVALID_REQUEST); 1370 goto session_bind_error; 1371 } 1372 1373 /* 1374 * Check the two error conditions from the table. 1375 * 1376 * ISID=new, TSIH=non-zero 1377 */ 1378 if ((existing_sess == NULL) && (lsm->icl_tsih != ISCSI_UNSPEC_TSIH)) { 1379 /* fail the login */ 1380 SET_LOGIN_ERROR(ict, ISCSI_STATUS_CLASS_INITIATOR_ERR, 1381 ISCSI_LOGIN_STATUS_NO_SESSION); 1382 goto session_bind_error; 1383 } 1384 1385 /* ISID=existing, TSIH=non-zero new */ 1386 if ((existing_sess != NULL) && (lsm->icl_tsih != 0) && 1387 (existing_sess->ist_tsih != lsm->icl_tsih)) { 1388 /* fail the login */ 1389 SET_LOGIN_ERROR(ict, ISCSI_STATUS_CLASS_INITIATOR_ERR, 1390 ISCSI_LOGIN_STATUS_NO_SESSION); 1391 goto session_bind_error; 1392 } 1393 1394 /* 1395 * Handle the remaining table cases in order 1396 */ 1397 if (existing_sess == NULL) { 1398 /* Should have caught this above */ 1399 ASSERT(lsm->icl_tsih == ISCSI_UNSPEC_TSIH); 1400 /* 1401 * ISID=new, TSIH=zero --> instantiate a new session 1402 */ 1403 new_sess = iscsit_sess_create(tgt, ict, lsm->icl_cmdsn, 1404 lsm->icl_isid, lsm->icl_tpgt_tag, lsm->icl_initiator_name, 1405 lsm->icl_target_name, &error_class, &error_detail); 1406 ASSERT(new_sess != NULL); 1407 1408 /* Session create may have failed even if it returned a value */ 1409 if (error_class != ISCSI_STATUS_CLASS_SUCCESS) { 1410 SET_LOGIN_ERROR(ict, error_class, error_detail); 1411 goto session_bind_error; 1412 } 1413 1414 /* 1415 * If we don't already have an STMF session and this is not 1416 * a discovery session then we need to allocate and register 1417 * one. 1418 */ 1419 if (!ict->ict_op.op_discovery_session) { 1420 if (login_sm_session_register(ict) != 1421 IDM_STATUS_SUCCESS) { 1422 /* login_sm_session_register sets error codes */ 1423 goto session_bind_error; 1424 } 1425 } 1426 1427 } else { 1428 if (lsm->icl_tsih == ISCSI_UNSPEC_TSIH) { 1429 /* 1430 * ISID=existing, TSIH=zero --> Session reinstatement 1431 */ 1432 new_sess = iscsit_sess_reinstate(tgt, existing_sess, 1433 ict, &error_class, &error_detail); 1434 ASSERT(new_sess != NULL); 1435 1436 if (error_class != ISCSI_STATUS_CLASS_SUCCESS) { 1437 SET_LOGIN_ERROR(ict, error_class, error_detail); 1438 goto session_bind_error; 1439 } 1440 1441 /* 1442 * If we don't already have an STMF session and this is 1443 * not a discovery session then we need to allocate and 1444 * register one. 1445 */ 1446 if (!ict->ict_op.op_discovery_session) { 1447 if (login_sm_session_register(ict) != 1448 IDM_STATUS_SUCCESS) { 1449 /* 1450 * login_sm_session_register sets 1451 * error codes 1452 */ 1453 goto session_bind_error; 1454 } 1455 } 1456 } else { 1457 /* 1458 * The following code covers these two cases: 1459 * ISID=existing, TSIH=non-zero existing, CID=new 1460 * --> add new connection to MC/S session 1461 * ISID=existing, TSIH=non-zero existing, CID=existing 1462 * --> do connection reinstatement 1463 * 1464 * Session continuation uses this path as well 1465 */ 1466 cmn_err(CE_NOTE, "login_sm_session_bind: add new " 1467 "conn/sess continue"); 1468 if (existing_ict != NULL) { 1469 /* 1470 * ISID=existing, TSIH=non-zero existing, 1471 * CID=existing --> do connection reinstatement 1472 */ 1473 if (iscsit_conn_reinstate(existing_ict, ict) != 1474 IDM_STATUS_SUCCESS) { 1475 /* 1476 * Most likely this means the connection 1477 * the initiator is trying to reinstate 1478 * is not in an acceptable state. 1479 */ 1480 SET_LOGIN_ERROR(ict, 1481 ISCSI_STATUS_CLASS_INITIATOR_ERR, 1482 ISCSI_LOGIN_STATUS_INIT_ERR); 1483 goto session_bind_error; 1484 } 1485 } 1486 1487 iscsit_sess_sm_event(existing_sess, SE_CONN_IN_LOGIN, 1488 ict); 1489 } 1490 } 1491 1492 if (tgt != NULL) 1493 iscsit_tgt_rele(tgt); 1494 if (existing_sess != NULL) 1495 iscsit_sess_rele(existing_sess); 1496 if (existing_ict != NULL) 1497 iscsit_conn_rele(existing_ict); 1498 1499 return (IDM_STATUS_SUCCESS); 1500 1501 session_bind_error: 1502 if (tgt != NULL) 1503 iscsit_tgt_rele(tgt); 1504 if (existing_sess != NULL) 1505 iscsit_sess_rele(existing_sess); 1506 if (existing_ict != NULL) 1507 iscsit_conn_rele(existing_ict); 1508 1509 /* 1510 * If session bind fails we will fail the login but don't destroy 1511 * the session until later. 1512 */ 1513 return (IDM_STATUS_FAIL); 1514 } 1515 1516 1517 static idm_status_t 1518 login_sm_set_auth(iscsit_conn_t *ict) 1519 { 1520 idm_status_t idmrc = IDM_STATUS_SUCCESS; 1521 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 1522 iscsit_ini_t *ini; 1523 iscsit_tgt_t *tgt; 1524 char *auth = ""; 1525 char *radiusserver = ""; 1526 char *radiussecret = ""; 1527 char *chapuser = ""; 1528 char *chapsecret = ""; 1529 char *targetchapuser = ""; 1530 char *targetchapsecret = ""; 1531 char *targetalias = ""; 1532 int i; 1533 1534 ISCSIT_GLOBAL_LOCK(RW_READER); 1535 1536 /* 1537 * Set authentication method to none for discovery session. 1538 */ 1539 if (ict->ict_op.op_discovery_session == B_TRUE) { 1540 lsm->icl_auth.ca_method_valid_list[0] = AM_NONE; 1541 ISCSIT_GLOBAL_UNLOCK(); 1542 return (idmrc); 1543 } 1544 1545 /* 1546 * Get all the authentication parameters we need -- since we hold 1547 * the global config lock we guarantee that the parameters will 1548 * be consistent with each other. 1549 */ 1550 (void) nvlist_lookup_string(iscsit_global.global_props, 1551 PROP_AUTH, &auth); 1552 (void) nvlist_lookup_string(iscsit_global.global_props, 1553 PROP_RADIUS_SERVER, &radiusserver); 1554 (void) nvlist_lookup_string(iscsit_global.global_props, 1555 PROP_RADIUS_SECRET, &radiussecret); 1556 1557 ini = iscsit_ini_lookup_locked(lsm->icl_initiator_name); 1558 if (ini != NULL) { 1559 /* Get Initiator CHAP parameters */ 1560 (void) nvlist_lookup_string(ini->ini_props, PROP_CHAP_USER, 1561 &chapuser); 1562 (void) nvlist_lookup_string(ini->ini_props, PROP_CHAP_SECRET, 1563 &chapsecret); 1564 } 1565 1566 tgt = ict->ict_sess->ist_tgt; 1567 if (tgt != NULL) { 1568 /* See if we have a target-specific authentication setting */ 1569 (void) nvlist_lookup_string(tgt->target_props, PROP_AUTH, 1570 &auth); 1571 /* Get target CHAP parameters */ 1572 (void) nvlist_lookup_string(tgt->target_props, 1573 PROP_TARGET_CHAP_USER, &targetchapuser); 1574 (void) nvlist_lookup_string(tgt->target_props, 1575 PROP_TARGET_CHAP_SECRET, &targetchapsecret); 1576 /* Get alias */ 1577 (void) nvlist_lookup_string(tgt->target_props, 1578 PROP_ALIAS, &targetalias); 1579 } 1580 1581 /* Set authentication method */ 1582 i = 0; 1583 if (strcmp(auth, PA_AUTH_RADIUS) == 0) { 1584 /* CHAP authentication using RADIUS server */ 1585 lsm->icl_auth.ca_method_valid_list[i++] = AM_CHAP; 1586 lsm->icl_auth.ca_use_radius = B_TRUE; 1587 } else if (strcmp(auth, PA_AUTH_CHAP) == 0) { 1588 /* Local CHAP authentication */ 1589 lsm->icl_auth.ca_method_valid_list[i++] = AM_CHAP; 1590 lsm->icl_auth.ca_use_radius = B_FALSE; 1591 } else if ((strcmp(auth, PA_AUTH_NONE) == 0) || 1592 (strcmp(auth, "") == 0)) { 1593 /* No authentication */ 1594 lsm->icl_auth.ca_method_valid_list[i++] = AM_NONE; 1595 } 1596 1597 /* 1598 * If initiator/target CHAP username is not set then use the 1599 * node name. If lsm->icl_target_name == NULL then this is 1600 * a discovery session so we don't need to work about the target. 1601 */ 1602 if (strcmp(chapuser, "") == 0) { 1603 (void) strlcpy(lsm->icl_auth.ca_ini_chapuser, 1604 lsm->icl_initiator_name, 1605 min(iscsitAuthStringMaxLength, MAX_ISCSI_NODENAMELEN)); 1606 } else { 1607 (void) strlcpy(lsm->icl_auth.ca_ini_chapuser, chapuser, 1608 iscsitAuthStringMaxLength); 1609 } 1610 if ((lsm->icl_target_name != NULL) && 1611 (strcmp(targetchapuser, "") == 0)) { 1612 (void) strlcpy(lsm->icl_auth.ca_tgt_chapuser, 1613 lsm->icl_target_name, 1614 min(iscsitAuthStringMaxLength, MAX_ISCSI_NODENAMELEN)); 1615 } else { 1616 (void) strlcpy(lsm->icl_auth.ca_tgt_chapuser, 1617 targetchapuser, iscsitAuthStringMaxLength); 1618 } 1619 1620 /* 1621 * Secrets are stored in base64-encoded format so we need to 1622 * decode them into binary form 1623 */ 1624 if (strcmp(chapsecret, "") == 0) { 1625 lsm->icl_auth.ca_ini_chapsecretlen = 0; 1626 } else { 1627 if (iscsi_base64_str_to_binary(chapsecret, 1628 strnlen(chapsecret, iscsitAuthStringMaxLength), 1629 lsm->icl_auth.ca_ini_chapsecret, iscsitAuthStringMaxLength, 1630 &lsm->icl_auth.ca_ini_chapsecretlen) != 0) { 1631 cmn_err(CE_WARN, "Corrupted CHAP secret" 1632 " for initiator %s", lsm->icl_initiator_name); 1633 lsm->icl_auth.ca_ini_chapsecretlen = 0; 1634 } 1635 } 1636 if (strcmp(targetchapsecret, "") == 0) { 1637 lsm->icl_auth.ca_tgt_chapsecretlen = 0; 1638 } else { 1639 if (iscsi_base64_str_to_binary(targetchapsecret, 1640 strnlen(targetchapsecret, iscsitAuthStringMaxLength), 1641 lsm->icl_auth.ca_tgt_chapsecret, iscsitAuthStringMaxLength, 1642 &lsm->icl_auth.ca_tgt_chapsecretlen) != 0) { 1643 cmn_err(CE_WARN, "Corrupted CHAP secret" 1644 " for target %s", lsm->icl_target_name); 1645 lsm->icl_auth.ca_tgt_chapsecretlen = 0; 1646 } 1647 } 1648 if (strcmp(radiussecret, "") == 0) { 1649 lsm->icl_auth.ca_radius_secretlen = 0; 1650 } else { 1651 if (iscsi_base64_str_to_binary(radiussecret, 1652 strnlen(radiussecret, iscsitAuthStringMaxLength), 1653 lsm->icl_auth.ca_radius_secret, iscsitAuthStringMaxLength, 1654 &lsm->icl_auth.ca_radius_secretlen) != 0) { 1655 cmn_err(CE_WARN, "Corrupted RADIUS secret"); 1656 lsm->icl_auth.ca_radius_secretlen = 0; 1657 } 1658 } 1659 1660 /* 1661 * Set alias 1662 */ 1663 (void) strlcpy(lsm->icl_auth.ca_tgt_alias, targetalias, 1664 MAX_ISCSI_NODENAMELEN); 1665 1666 /* 1667 * Now that authentication parameters are setup, validate the parameters 1668 * against the authentication mode 1669 * Decode RADIUS server value int lsm->icl_auth.ca_radius_server 1670 */ 1671 if ((strcmp(auth, PA_AUTH_RADIUS) == 0) && 1672 ((lsm->icl_auth.ca_radius_secretlen == 0) || 1673 (strcmp(radiusserver, "") == 0) || 1674 it_common_convert_sa(radiusserver, 1675 &lsm->icl_auth.ca_radius_server, 1676 DEFAULT_RADIUS_PORT) == NULL)) { 1677 cmn_err(CE_WARN, "RADIUS authentication selected " 1678 "for target %s but RADIUS parameters are not " 1679 "configured.", lsm->icl_target_name); 1680 SET_LOGIN_ERROR(ict, ISCSI_STATUS_CLASS_TARGET_ERR, 1681 ISCSI_LOGIN_STATUS_TARGET_ERROR); 1682 idmrc = IDM_STATUS_FAIL; 1683 } else if ((strcmp(auth, PA_AUTH_CHAP) == 0) && 1684 (lsm->icl_auth.ca_ini_chapsecretlen == 0)) { 1685 SET_LOGIN_ERROR(ict, ISCSI_STATUS_CLASS_INITIATOR_ERR, 1686 ISCSI_LOGIN_STATUS_AUTH_FAILED); 1687 idmrc = IDM_STATUS_FAIL; 1688 } 1689 1690 ISCSIT_GLOBAL_UNLOCK(); 1691 1692 return (idmrc); 1693 } 1694 1695 1696 static idm_status_t 1697 login_sm_session_register(iscsit_conn_t *ict) 1698 { 1699 iscsit_sess_t *ist = ict->ict_sess; 1700 stmf_scsi_session_t *ss; 1701 1702 /* 1703 * Hold target mutex until we have finished registering with STMF 1704 */ 1705 mutex_enter(&ist->ist_tgt->target_mutex); 1706 if (ist->ist_tgt->target_state != TS_STMF_ONLINE) { 1707 mutex_exit(&ist->ist_tgt->target_mutex); 1708 SET_LOGIN_ERROR(ict, ISCSI_STATUS_CLASS_INITIATOR_ERR, 1709 ISCSI_LOGIN_STATUS_TGT_REMOVED); 1710 return (IDM_STATUS_FAIL); 1711 } 1712 1713 ss = stmf_alloc(STMF_STRUCT_SCSI_SESSION, 0, 1714 0); 1715 if (ss == NULL) { 1716 mutex_exit(&ist->ist_tgt->target_mutex); 1717 SET_LOGIN_ERROR(ict, ISCSI_STATUS_CLASS_TARGET_ERR, 1718 ISCSI_LOGIN_STATUS_NO_RESOURCES); 1719 return (IDM_STATUS_FAIL); 1720 } 1721 1722 ss->ss_rport_id = kmem_zalloc(sizeof (scsi_devid_desc_t) + 1723 strlen(ist->ist_initiator_name) + 1, KM_SLEEP); 1724 (void) strcpy((char *)ss->ss_rport_id->ident, ist->ist_initiator_name); 1725 ss->ss_rport_id->ident_length = strlen(ist->ist_initiator_name); 1726 ss->ss_rport_id->protocol_id = PROTOCOL_iSCSI; 1727 ss->ss_rport_id->piv = 1; 1728 ss->ss_rport_id->code_set = CODE_SET_ASCII; 1729 ss->ss_rport_id->association = ID_IS_TARGET_PORT; 1730 1731 ss->ss_lport = ist->ist_lport; 1732 1733 if (stmf_register_scsi_session(ict->ict_sess->ist_lport, ss) != 1734 STMF_SUCCESS) { 1735 mutex_exit(&ist->ist_tgt->target_mutex); 1736 kmem_free(ss->ss_rport_id, 1737 sizeof (scsi_devid_desc_t) + 1738 strlen(ist->ist_initiator_name) + 1); 1739 stmf_free(ss); 1740 SET_LOGIN_ERROR(ict, ISCSI_STATUS_CLASS_TARGET_ERR, 1741 ISCSI_LOGIN_STATUS_TARGET_ERROR); 1742 return (IDM_STATUS_FAIL); 1743 } 1744 1745 ss->ss_port_private = ict->ict_sess; 1746 ict->ict_sess->ist_stmf_sess = ss; 1747 mutex_exit(&ist->ist_tgt->target_mutex); 1748 1749 return (IDM_STATUS_SUCCESS); 1750 } 1751 1752 1753 static idm_status_t 1754 login_sm_req_pdu_check(iscsit_conn_t *ict, idm_pdu_t *pdu) 1755 { 1756 uint8_t csg_req; 1757 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 1758 iscsi_login_hdr_t *lh = (iscsi_login_hdr_t *)pdu->isp_hdr; 1759 iscsi_login_rsp_hdr_t *lh_resp = lsm->icl_login_resp_tmpl; 1760 1761 /* 1762 * Check CSG 1763 */ 1764 csg_req = ISCSI_LOGIN_CURRENT_STAGE(lh->flags); 1765 switch (csg_req) { 1766 case ISCSI_SECURITY_NEGOTIATION_STAGE: 1767 case ISCSI_OP_PARMS_NEGOTIATION_STAGE: 1768 if ((csg_req != lsm->icl_login_csg) && 1769 (lsm->icl_login_state != ILS_LOGIN_INIT)) { 1770 /* 1771 * Inappropriate CSG change. Initiator can only 1772 * change CSG after we've responded with the 1773 * transit bit set. If we had responded with 1774 * a CSG change previous we would have updated 1775 * our copy of CSG. 1776 * 1777 * The exception is when we are in ILS_LOGIN_INIT 1778 * state since we haven't determined our initial 1779 * CSG value yet. 1780 */ 1781 goto pdu_check_fail; 1782 } 1783 break; 1784 case ISCSI_FULL_FEATURE_PHASE: 1785 default: 1786 goto pdu_check_fail; 1787 } 1788 1789 /* 1790 * If this is the first login PDU for a new connection then 1791 * the session will be NULL. 1792 */ 1793 if (ict->ict_sess != NULL) { 1794 /* 1795 * We've already created a session on a previous PDU. Make 1796 * sure this PDU is consistent with what we've already seen 1797 */ 1798 if ((ict->ict_cid != ntohs(lh->cid)) || 1799 (bcmp(ict->ict_sess->ist_isid, lh->isid, 1800 ISCSI_ISID_LEN) != 0)) { 1801 goto pdu_check_fail; 1802 } 1803 } 1804 1805 /* 1806 * Make sure we are compatible with the version range 1807 */ 1808 #if (ISCSIT_MAX_VERSION > 0) 1809 if ((lh->min_version > ISCSIT_MAX_VERSION) || 1810 (lh->max_version < ISCSIT_MIN_VERSION)) { 1811 goto pdu_check_fail; 1812 } 1813 #endif 1814 1815 /* 1816 * Just in case the initiator changes things up on us along the way 1817 * check against our active_version -- we can't change the active 1818 * version and the initiator is not *supposed* to change its 1819 * min_version and max_version values so this should never happen. 1820 * Of course we only do this if the response header template has 1821 * been built. 1822 */ 1823 if ((lh_resp->opcode == ISCSI_OP_LOGIN_RSP) && /* header valid */ 1824 ((lh->min_version > lh_resp->active_version) || 1825 (lh->max_version < lh_resp->active_version))) { 1826 goto pdu_check_fail; 1827 } 1828 1829 return (IDM_STATUS_SUCCESS); 1830 1831 pdu_check_fail: 1832 return (IDM_STATUS_FAIL); 1833 } 1834 1835 static idm_status_t 1836 login_sm_process_nvlist(iscsit_conn_t *ict) 1837 { 1838 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 1839 char *nvp_name; 1840 nvpair_t *nvp; 1841 nvpair_t *next_nvp; 1842 nvpair_t *negotiated_nvp; 1843 kv_status_t kvrc; 1844 uint8_t error_class; 1845 uint8_t error_detail; 1846 idm_status_t idm_status; 1847 1848 error_class = ISCSI_STATUS_CLASS_SUCCESS; 1849 error_detail = ISCSI_LOGIN_STATUS_ACCEPT; 1850 1851 /* First, request that the transport process the list */ 1852 kvrc = idm_negotiate_key_values(ict->ict_ic, lsm->icl_request_nvlist, 1853 lsm->icl_response_nvlist, lsm->icl_negotiated_values); 1854 idm_kvstat_to_error(kvrc, &error_class, &error_detail); 1855 if (error_class != ISCSI_STATUS_CLASS_SUCCESS) { 1856 SET_LOGIN_ERROR(ict, error_class, error_detail); 1857 idm_status = IDM_STATUS_FAIL; 1858 return (idm_status); 1859 } 1860 1861 /* Ensure we clear transit bit if the transport layer has countered */ 1862 if (kvrc == KV_HANDLED_NO_TRANSIT) { 1863 lsm->icl_login_transit = B_FALSE; 1864 } 1865 1866 /* Now, move on and process the rest of the pairs */ 1867 nvp = nvlist_next_nvpair(lsm->icl_request_nvlist, NULL); 1868 while (nvp != NULL) { 1869 next_nvp = nvlist_next_nvpair(lsm->icl_request_nvlist, nvp); 1870 nvp_name = nvpair_name(nvp); 1871 /* 1872 * If we've already agreed upon a value then make sure this 1873 * is not attempting to change that value. From RFC3270 1874 * section 5.3: 1875 * 1876 * "Neither the initiator nor the target should attempt to 1877 * declare or negotiate a parameter more than once during 1878 * login except for responses to specific keys that 1879 * explicitly allow repeated key declarations (e.g., 1880 * TargetAddress). An attempt to renegotiate/redeclare 1881 * parameters not specifically allowed MUST be detected 1882 * by the initiator and target. If such an attempt is 1883 * detected by the target, the target MUST respond 1884 * with Login reject (initiator error); ..." 1885 */ 1886 if (nvlist_lookup_nvpair(lsm->icl_negotiated_values, 1887 nvp_name, &negotiated_nvp) == 0) { 1888 kvrc = KV_HANDLED; 1889 } else { 1890 kvrc = iscsit_handle_key(ict, nvp, nvp_name); 1891 } 1892 1893 idm_kvstat_to_error(kvrc, &error_class, &error_detail); 1894 if (error_class != ISCSI_STATUS_CLASS_SUCCESS) { 1895 break; 1896 } 1897 1898 nvp = next_nvp; 1899 } 1900 1901 if (error_class == ISCSI_STATUS_CLASS_SUCCESS) { 1902 idm_status = IDM_STATUS_SUCCESS; 1903 } else { 1904 /* supply login class/detail for login errors */ 1905 SET_LOGIN_ERROR(ict, error_class, error_detail); 1906 idm_status = IDM_STATUS_FAIL; 1907 } 1908 1909 return (idm_status); 1910 } 1911 1912 static idm_status_t 1913 login_sm_check_security(iscsit_conn_t *ict) 1914 { 1915 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 1916 conn_auth_t *auth = &lsm->icl_auth; 1917 iscsit_auth_method_t *am_list = &auth->ca_method_valid_list[0]; 1918 kv_status_t kvrc; 1919 uint8_t error_class; 1920 uint8_t error_detail; 1921 idm_status_t idm_status; 1922 1923 error_class = ISCSI_STATUS_CLASS_SUCCESS; 1924 error_detail = ISCSI_LOGIN_STATUS_ACCEPT; 1925 1926 /* Check authentication status. */ 1927 if (lsm->icl_login_csg == ISCSI_SECURITY_NEGOTIATION_STAGE) { 1928 /* 1929 * We should have some authentication key/value pair(s) 1930 * received from initiator and the authentication phase 1931 * has been shifted when the key/value pair(s) are being 1932 * handled in the previous call iscsit_handle_security_key. 1933 * Now it turns to target to check the authentication phase 1934 * and shift it after taking some authentication action. 1935 */ 1936 kvrc = iscsit_reply_security_key(ict); 1937 idm_kvstat_to_error(kvrc, &error_class, &error_detail); 1938 } else if (!ict->ict_login_sm.icl_auth_pass) { 1939 /* 1940 * Check to see if the target allows initiators to bypass the 1941 * security check. If the target is configured to require 1942 * authentication, we reject the connection. 1943 */ 1944 if (am_list[0] == AM_NONE || am_list[0] == 0) { 1945 ict->ict_login_sm.icl_auth_pass = 1; 1946 } else { 1947 error_class = ISCSI_STATUS_CLASS_INITIATOR_ERR; 1948 error_detail = ISCSI_LOGIN_STATUS_AUTH_FAILED; 1949 } 1950 } 1951 1952 if (error_class == ISCSI_STATUS_CLASS_SUCCESS) { 1953 idm_status = IDM_STATUS_SUCCESS; 1954 } else { 1955 /* supply login class/detail for login errors */ 1956 SET_LOGIN_ERROR(ict, error_class, error_detail); 1957 idm_status = IDM_STATUS_FAIL; 1958 } 1959 1960 return (idm_status); 1961 } 1962 1963 static idm_pdu_t * 1964 login_sm_build_login_response(iscsit_conn_t *ict) 1965 { 1966 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 1967 iscsi_login_rsp_hdr_t *lh; 1968 int transit, text_transit = 1; 1969 idm_pdu_t *login_resp; 1970 1971 /* 1972 * Create a response PDU and fill it with as much of 1973 * the response text that will fit. 1974 */ 1975 1976 if (lsm->icl_login_resp_itb) { 1977 /* allocate a pdu with space for text */ 1978 login_resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), 1979 ISCSI_DEFAULT_MAX_RECV_SEG_LEN); 1980 /* copy a chunk of text into the pdu */ 1981 lsm->icl_login_resp_buf = idm_pdu_init_text_data( 1982 login_resp, lsm->icl_login_resp_itb, 1983 ISCSI_DEFAULT_MAX_RECV_SEG_LEN, 1984 lsm->icl_login_resp_buf, &text_transit); 1985 if (text_transit) { 1986 /* text buf has been consumed */ 1987 idm_itextbuf_free(lsm->icl_login_resp_itb); 1988 lsm->icl_login_resp_itb = NULL; 1989 lsm->icl_login_resp_buf = NULL; 1990 } 1991 } else { 1992 /* allocate a pdu for just a header */ 1993 login_resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0); 1994 } 1995 /* finish initializing the pdu */ 1996 idm_pdu_init(login_resp, 1997 ict->ict_ic, ict, login_resp_complete_cb); 1998 login_resp->isp_flags |= IDM_PDU_LOGIN_TX; 1999 2000 /* 2001 * Use the BHS header values from the response template 2002 */ 2003 bcopy(lsm->icl_login_resp_tmpl, 2004 login_resp->isp_hdr, sizeof (iscsi_login_rsp_hdr_t)); 2005 2006 lh = (iscsi_login_rsp_hdr_t *)login_resp->isp_hdr; 2007 2008 /* Set error class/detail */ 2009 lh->status_class = lsm->icl_login_resp_err_class; 2010 lh->status_detail = lsm->icl_login_resp_err_detail; 2011 /* Set CSG, NSG and Transit */ 2012 lh->flags = 0; 2013 lh->flags |= lsm->icl_login_csg << 2; 2014 2015 2016 if (lh->status_class == ISCSI_STATUS_CLASS_SUCCESS) { 2017 if (lsm->icl_login_transit && 2018 lsm->icl_auth_pass != 0) { 2019 transit = 1; 2020 } else { 2021 transit = 0; 2022 } 2023 /* 2024 * inititalize the text data 2025 */ 2026 if (transit == 1 && text_transit == 1) { 2027 lh->flags |= lsm->icl_login_nsg; 2028 lsm->icl_login_csg = lsm->icl_login_nsg; 2029 lh->flags |= ISCSI_FLAG_LOGIN_TRANSIT; 2030 } else { 2031 lh->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT; 2032 } 2033 2034 /* If we are transitioning to FFP then set TSIH */ 2035 if (transit && (lh->flags & ISCSI_FLAG_LOGIN_TRANSIT) && 2036 lsm->icl_login_csg == ISCSI_FULL_FEATURE_PHASE) { 2037 lh->tsid = htons(ict->ict_sess->ist_tsih); 2038 } 2039 } else { 2040 login_resp->isp_data = 0; 2041 login_resp->isp_datalen = 0; 2042 } 2043 return (login_resp); 2044 } 2045 2046 static kv_status_t 2047 iscsit_handle_key(iscsit_conn_t *ict, nvpair_t *nvp, char *nvp_name) 2048 { 2049 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 2050 kv_status_t kvrc; 2051 const idm_kv_xlate_t *ikvx; 2052 2053 ikvx = idm_lookup_kv_xlate(nvp_name, strlen(nvp_name)); 2054 if (ikvx->ik_key_id == KI_MAX_KEY) { 2055 /* 2056 * Any key not understood by the acceptor may be igonred 2057 * by the acceptor without affecting the basic function. 2058 * However, the answer for a key not understood MUST be 2059 * key=NotUnderstood. 2060 */ 2061 kvrc = iscsit_reply_string(ict, nvp_name, 2062 ISCSI_TEXT_NOTUNDERSTOOD); 2063 } else { 2064 kvrc = iscsit_handle_common_key(ict, nvp, ikvx); 2065 if (kvrc == KV_UNHANDLED) { 2066 switch (lsm->icl_login_csg) { 2067 case ISCSI_SECURITY_NEGOTIATION_STAGE: 2068 kvrc = iscsit_handle_security_key( 2069 ict, nvp, ikvx); 2070 break; 2071 case ISCSI_OP_PARMS_NEGOTIATION_STAGE: 2072 kvrc = iscsit_handle_operational_key( 2073 ict, nvp, ikvx); 2074 break; 2075 case ISCSI_FULL_FEATURE_PHASE: 2076 default: 2077 /* What are we doing here? */ 2078 ASSERT(0); 2079 kvrc = KV_UNHANDLED; 2080 } 2081 } 2082 } 2083 2084 return (kvrc); 2085 } 2086 2087 static kv_status_t 2088 iscsit_handle_common_key(iscsit_conn_t *ict, nvpair_t *nvp, 2089 const idm_kv_xlate_t *ikvx) 2090 { 2091 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 2092 kv_status_t kvrc; 2093 char *string_val; 2094 int nvrc; 2095 2096 switch (ikvx->ik_key_id) { 2097 case KI_INITIATOR_NAME: 2098 case KI_INITIATOR_ALIAS: 2099 nvrc = nvlist_add_nvpair(lsm->icl_negotiated_values, nvp); 2100 kvrc = idm_nvstat_to_kvstat(nvrc); 2101 break; 2102 case KI_TARGET_NAME: 2103 /* We'll validate the target during login_sm_session_bind() */ 2104 nvrc = nvpair_value_string(nvp, &string_val); 2105 ASSERT(nvrc == 0); /* We built this nvlist */ 2106 2107 nvrc = nvlist_add_nvpair(lsm->icl_negotiated_values, nvp); 2108 kvrc = idm_nvstat_to_kvstat(nvrc); 2109 break; 2110 case KI_TARGET_ALIAS: 2111 case KI_TARGET_ADDRESS: 2112 case KI_TARGET_PORTAL_GROUP_TAG: 2113 kvrc = KV_TARGET_ONLY; /* Only the target can declare this */ 2114 break; 2115 case KI_SESSION_TYPE: 2116 /* 2117 * If we don't receive this key on the initial login 2118 * we assume this is a normal session. 2119 */ 2120 nvrc = nvlist_add_nvpair(lsm->icl_negotiated_values, nvp); 2121 kvrc = idm_nvstat_to_kvstat(nvrc); 2122 nvrc = nvpair_value_string(nvp, &string_val); 2123 ASSERT(nvrc == 0); /* We built this nvlist */ 2124 ict->ict_op.op_discovery_session = 2125 strcmp(string_val, "Discovery") == 0 ? B_TRUE : B_FALSE; 2126 break; 2127 default: 2128 /* 2129 * This is not really an error but we should 2130 * leave this nvpair on the list since we 2131 * didn't do anything with it. Either 2132 * the security or operational phase 2133 * handling functions should process it. 2134 */ 2135 kvrc = KV_UNHANDLED; 2136 break; 2137 } 2138 2139 return (kvrc); 2140 } 2141 2142 static kv_status_t 2143 iscsit_handle_security_key(iscsit_conn_t *ict, nvpair_t *nvp, 2144 const idm_kv_xlate_t *ikvx) 2145 { 2146 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 2147 iscsit_auth_client_t *client = &lsm->icl_auth_client; 2148 iscsikey_id_t kv_id; 2149 kv_status_t kvrc; 2150 iscsit_auth_handler_t handler; 2151 2152 /* 2153 * After all of security keys are handled, this function will 2154 * be called again to verify current authentication status 2155 * and perform some actual authentication work. At this time, 2156 * the nvp and ikvx will be passed in as NULLs. 2157 */ 2158 if (ikvx != NULL) { 2159 kv_id = ikvx->ik_key_id; 2160 } else { 2161 kv_id = 0; 2162 } 2163 2164 handler = iscsit_auth_get_handler(client, kv_id); 2165 if (handler) { 2166 kvrc = handler(ict, nvp, ikvx); 2167 } else { 2168 kvrc = KV_UNHANDLED; /* invalid request */ 2169 } 2170 2171 return (kvrc); 2172 } 2173 2174 static kv_status_t 2175 iscsit_reply_security_key(iscsit_conn_t *ict) 2176 { 2177 return (iscsit_handle_security_key(ict, NULL, NULL)); 2178 } 2179 2180 static kv_status_t 2181 iscsit_handle_operational_key(iscsit_conn_t *ict, nvpair_t *nvp, 2182 const idm_kv_xlate_t *ikvx) 2183 { 2184 kv_status_t kvrc = KV_UNHANDLED; 2185 boolean_t bool_val; 2186 uint64_t num_val; 2187 int nvrc; 2188 2189 /* 2190 * Retrieve values. All value lookups are expected to succeed 2191 * since we build the nvlist while decoding the text buffer. This 2192 * step is intended to eliminate some duplication of code (for example 2193 * we only need to code the numerical value lookup once). We will 2194 * handle the values (if necessary) below. 2195 */ 2196 switch (ikvx->ik_key_id) { 2197 /* Lists */ 2198 case KI_HEADER_DIGEST: 2199 case KI_DATA_DIGEST: 2200 break; 2201 /* Booleans */ 2202 case KI_INITIAL_R2T: 2203 case KI_IMMEDIATE_DATA: 2204 case KI_DATA_PDU_IN_ORDER: 2205 case KI_DATA_SEQUENCE_IN_ORDER: 2206 case KI_IFMARKER: 2207 case KI_OFMARKER: 2208 nvrc = nvpair_value_boolean_value(nvp, &bool_val); 2209 ASSERT(nvrc == 0); /* We built this nvlist */ 2210 break; 2211 /* Numericals */ 2212 case KI_MAX_CONNECTIONS: 2213 case KI_MAX_RECV_DATA_SEGMENT_LENGTH: 2214 case KI_MAX_BURST_LENGTH: 2215 case KI_FIRST_BURST_LENGTH: 2216 case KI_DEFAULT_TIME_2_WAIT: 2217 case KI_DEFAULT_TIME_2_RETAIN: 2218 case KI_MAX_OUTSTANDING_R2T: 2219 case KI_ERROR_RECOVERY_LEVEL: 2220 nvrc = nvpair_value_uint64(nvp, &num_val); 2221 ASSERT(nvrc == 0); 2222 break; 2223 /* Ranges */ 2224 case KI_OFMARKERINT: 2225 case KI_IFMARKERINT: 2226 break; 2227 default: 2228 break; 2229 } 2230 2231 /* 2232 * Now handle the values according to the key name. Sometimes we 2233 * don't care what the value is -- in that case we just add the nvpair 2234 * to the negotiated values list. 2235 */ 2236 switch (ikvx->ik_key_id) { 2237 case KI_HEADER_DIGEST: 2238 kvrc = iscsit_handle_digest(ict, nvp, ikvx); 2239 break; 2240 case KI_DATA_DIGEST: 2241 kvrc = iscsit_handle_digest(ict, nvp, ikvx); 2242 break; 2243 case KI_INITIAL_R2T: 2244 /* We *require* INITIAL_R2T=yes */ 2245 kvrc = iscsit_handle_boolean(ict, nvp, bool_val, ikvx, 2246 B_TRUE); 2247 break; 2248 case KI_IMMEDIATE_DATA: 2249 kvrc = iscsit_handle_boolean(ict, nvp, bool_val, ikvx, 2250 bool_val); 2251 break; 2252 case KI_DATA_PDU_IN_ORDER: 2253 kvrc = iscsit_handle_boolean(ict, nvp, bool_val, ikvx, 2254 B_TRUE); 2255 break; 2256 case KI_DATA_SEQUENCE_IN_ORDER: 2257 /* We allow any value for DATA_SEQUENCE_IN_ORDER */ 2258 kvrc = iscsit_handle_boolean(ict, nvp, bool_val, ikvx, 2259 bool_val); 2260 break; 2261 case KI_OFMARKER: 2262 case KI_IFMARKER: 2263 /* We don't support markers */ 2264 kvrc = iscsit_handle_boolean(ict, nvp, bool_val, ikvx, 2265 B_FALSE); 2266 break; 2267 case KI_MAX_CONNECTIONS: 2268 kvrc = iscsit_handle_numerical(ict, nvp, num_val, ikvx, 2269 ISCSI_MIN_CONNECTIONS, 2270 ISCSI_MAX_CONNECTIONS, 2271 ISCSIT_MAX_CONNECTIONS); 2272 break; 2273 /* this is a declartive param */ 2274 case KI_MAX_RECV_DATA_SEGMENT_LENGTH: 2275 kvrc = iscsit_handle_numerical(ict, nvp, num_val, ikvx, 2276 ISCSI_MIN_RECV_DATA_SEGMENT_LENGTH, 2277 ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH, 2278 ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH); 2279 break; 2280 case KI_MAX_BURST_LENGTH: 2281 kvrc = iscsit_handle_numerical(ict, nvp, num_val, ikvx, 2282 ISCSI_MIN_MAX_BURST_LENGTH, 2283 ISCSI_MAX_BURST_LENGTH, 2284 ISCSIT_MAX_BURST_LENGTH); 2285 break; 2286 case KI_FIRST_BURST_LENGTH: 2287 kvrc = iscsit_handle_numerical(ict, nvp, num_val, ikvx, 2288 ISCSI_MIN_FIRST_BURST_LENGTH, 2289 ISCSI_MAX_FIRST_BURST_LENGTH, 2290 ISCSIT_MAX_FIRST_BURST_LENGTH); 2291 break; 2292 case KI_DEFAULT_TIME_2_WAIT: 2293 kvrc = iscsit_handle_numerical(ict, nvp, num_val, ikvx, 2294 ISCSI_MIN_TIME2WAIT, 2295 ISCSI_MAX_TIME2WAIT, 2296 ISCSIT_MAX_TIME2WAIT); 2297 break; 2298 case KI_DEFAULT_TIME_2_RETAIN: 2299 kvrc = iscsit_handle_numerical(ict, nvp, num_val, ikvx, 2300 ISCSI_MIN_TIME2RETAIN, 2301 ISCSI_MAX_TIME2RETAIN, 2302 ISCSIT_MAX_TIME2RETAIN); 2303 break; 2304 case KI_MAX_OUTSTANDING_R2T: 2305 kvrc = iscsit_handle_numerical(ict, nvp, num_val, ikvx, 2306 ISCSI_MIN_MAX_OUTSTANDING_R2T, 2307 ISCSI_MAX_OUTSTANDING_R2T, 2308 ISCSIT_MAX_OUTSTANDING_R2T); 2309 break; 2310 case KI_ERROR_RECOVERY_LEVEL: 2311 kvrc = iscsit_handle_numerical(ict, nvp, num_val, ikvx, 2312 ISCSI_MIN_ERROR_RECOVERY_LEVEL, 2313 ISCSI_MAX_ERROR_RECOVERY_LEVEL, 2314 ISCSIT_MAX_ERROR_RECOVERY_LEVEL); 2315 break; 2316 case KI_OFMARKERINT: 2317 case KI_IFMARKERINT: 2318 kvrc = iscsit_reply_string(ict, ikvx->ik_key_name, 2319 ISCSI_TEXT_IRRELEVANT); 2320 break; 2321 default: 2322 kvrc = KV_UNHANDLED; /* invalid request */ 2323 break; 2324 } 2325 2326 return (kvrc); 2327 } 2328 2329 static kv_status_t 2330 iscsit_reply_numerical(iscsit_conn_t *ict, 2331 const char *nvp_name, const uint64_t value) 2332 { 2333 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 2334 kv_status_t kvrc; 2335 int nvrc; 2336 2337 nvrc = nvlist_add_uint64(lsm->icl_response_nvlist, 2338 nvp_name, value); 2339 kvrc = idm_nvstat_to_kvstat(nvrc); 2340 2341 return (kvrc); 2342 } 2343 2344 static kv_status_t 2345 iscsit_reply_string(iscsit_conn_t *ict, 2346 const char *nvp_name, const char *text) 2347 { 2348 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 2349 kv_status_t kvrc; 2350 int nvrc; 2351 2352 nvrc = nvlist_add_string(lsm->icl_response_nvlist, 2353 nvp_name, text); 2354 kvrc = idm_nvstat_to_kvstat(nvrc); 2355 2356 return (kvrc); 2357 } 2358 2359 static kv_status_t 2360 iscsit_handle_digest(iscsit_conn_t *ict, nvpair_t *choices, 2361 const idm_kv_xlate_t *ikvx) 2362 { 2363 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 2364 kv_status_t kvrc = KV_VALUE_ERROR; 2365 int nvrc; 2366 nvpair_t *digest_choice; 2367 char *digest_choice_string; 2368 2369 /* 2370 * Need to add persistent config here if we want users to allow 2371 * disabling of digests on the target side. You could argue that 2372 * this makes things too complicated... just let the initiator state 2373 * what it wants and we'll take it. For now that's exactly what 2374 * we'll do. 2375 * 2376 * Basic digest negotiation happens here at iSCSI level. IDM 2377 * can override this during negotiate_key_values phase to 2378 * decline to set up any digest processing. 2379 */ 2380 digest_choice = idm_get_next_listvalue(choices, NULL); 2381 2382 /* 2383 * Loop through all choices. As soon as we find a choice 2384 * that we support add the value to our negotiated values list 2385 * and respond with that value in the login response. 2386 */ 2387 while (digest_choice != NULL) { 2388 nvrc = nvpair_value_string(digest_choice, 2389 &digest_choice_string); 2390 ASSERT(nvrc == 0); 2391 2392 if ((strcasecmp(digest_choice_string, "crc32c") == 0) || 2393 (strcasecmp(digest_choice_string, "none") == 0)) { 2394 /* Add to negotiated values list */ 2395 nvrc = nvlist_add_string(lsm->icl_negotiated_values, 2396 ikvx->ik_key_name, digest_choice_string); 2397 kvrc = idm_nvstat_to_kvstat(nvrc); 2398 if (nvrc == 0) { 2399 /* Add to login response list */ 2400 nvrc = nvlist_add_string( 2401 lsm->icl_response_nvlist, 2402 ikvx->ik_key_name, digest_choice_string); 2403 kvrc = idm_nvstat_to_kvstat(nvrc); 2404 } 2405 break; 2406 } 2407 digest_choice = idm_get_next_listvalue(choices, 2408 digest_choice); 2409 } 2410 2411 if (digest_choice == NULL) 2412 kvrc = KV_VALUE_ERROR; 2413 2414 return (kvrc); 2415 } 2416 2417 static kv_status_t 2418 iscsit_handle_boolean(iscsit_conn_t *ict, nvpair_t *nvp, boolean_t value, 2419 const idm_kv_xlate_t *ikvx, boolean_t iscsit_value) 2420 { 2421 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 2422 kv_status_t kvrc; 2423 int nvrc; 2424 2425 if (ikvx->ik_declarative) { 2426 nvrc = nvlist_add_nvpair(lsm->icl_negotiated_values, nvp); 2427 } else { 2428 if (value != iscsit_value) { 2429 /* Respond back to initiator with our value */ 2430 value = iscsit_value; 2431 lsm->icl_login_transit = B_FALSE; 2432 nvrc = 0; 2433 } else { 2434 /* Add this to our negotiated values */ 2435 nvrc = nvlist_add_nvpair(lsm->icl_negotiated_values, 2436 nvp); 2437 } 2438 2439 /* Response of Simple-value Negotiation */ 2440 if (nvrc == 0) { 2441 nvrc = nvlist_add_boolean_value( 2442 lsm->icl_response_nvlist, ikvx->ik_key_name, value); 2443 } 2444 } 2445 2446 kvrc = idm_nvstat_to_kvstat(nvrc); 2447 2448 return (kvrc); 2449 } 2450 2451 static kv_status_t 2452 iscsit_handle_numerical(iscsit_conn_t *ict, nvpair_t *nvp, uint64_t value, 2453 const idm_kv_xlate_t *ikvx, 2454 uint64_t iscsi_min_value, uint64_t iscsi_max_value, 2455 uint64_t iscsit_max_value) 2456 { 2457 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 2458 kv_status_t kvrc; 2459 int nvrc; 2460 2461 /* Validate against standard */ 2462 if ((value < iscsi_min_value) || (value > iscsi_max_value)) { 2463 kvrc = KV_VALUE_ERROR; 2464 } else if (ikvx->ik_declarative) { 2465 nvrc = nvlist_add_nvpair(lsm->icl_negotiated_values, nvp); 2466 kvrc = idm_nvstat_to_kvstat(nvrc); 2467 } else { 2468 if (value > iscsit_max_value) { 2469 /* Respond back to initiator with our value */ 2470 value = iscsit_max_value; 2471 lsm->icl_login_transit = B_FALSE; 2472 nvrc = 0; 2473 } else { 2474 /* Add this to our negotiated values */ 2475 nvrc = nvlist_add_nvpair(lsm->icl_negotiated_values, 2476 nvp); 2477 } 2478 2479 /* Response of Simple-value Negotiation */ 2480 if (nvrc == 0) { 2481 nvrc = nvlist_add_uint64(lsm->icl_response_nvlist, 2482 ikvx->ik_key_name, value); 2483 } 2484 kvrc = idm_nvstat_to_kvstat(nvrc); 2485 } 2486 2487 return (kvrc); 2488 } 2489 2490 2491 static void 2492 iscsit_process_negotiated_values(iscsit_conn_t *ict) 2493 { 2494 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 2495 char *string_val; 2496 boolean_t boolean_val; 2497 uint64_t uint64_val; 2498 int nvrc; 2499 2500 /* Let the IDM level activate its parameters first */ 2501 idm_notice_key_values(ict->ict_ic, lsm->icl_negotiated_values); 2502 2503 /* 2504 * Initiator alias and target alias 2505 */ 2506 if ((nvrc = nvlist_lookup_string(lsm->icl_negotiated_values, 2507 "InitiatorAlias", &string_val)) != ENOENT) { 2508 ASSERT(nvrc == 0); 2509 ict->ict_sess->ist_initiator_alias = 2510 kmem_alloc(strlen(string_val) + 1, KM_SLEEP); 2511 (void) strcpy(ict->ict_sess->ist_initiator_alias, string_val); 2512 } 2513 2514 if ((nvrc = nvlist_lookup_string(lsm->icl_negotiated_values, 2515 "TargetAlias", &string_val)) != ENOENT) { 2516 ASSERT(nvrc == 0); 2517 ict->ict_sess->ist_target_alias = 2518 kmem_alloc(strlen(string_val) + 1, KM_SLEEP); 2519 (void) strcpy(ict->ict_sess->ist_target_alias, string_val); 2520 } 2521 2522 /* 2523 * Operational parameters. We process SessionType when it is 2524 * initially received since it is required on the initial login. 2525 */ 2526 if ((nvrc = nvlist_lookup_boolean_value(lsm->icl_negotiated_values, 2527 "InitialR2T", &boolean_val)) != ENOENT) { 2528 ASSERT(nvrc == 0); 2529 ict->ict_op.op_initial_r2t = boolean_val; 2530 } 2531 2532 if ((nvrc = nvlist_lookup_boolean_value(lsm->icl_negotiated_values, 2533 "ImmediateData", &boolean_val)) != ENOENT) { 2534 ASSERT(nvrc == 0); 2535 ict->ict_op.op_immed_data = boolean_val; 2536 } 2537 2538 if ((nvrc = nvlist_lookup_boolean_value(lsm->icl_negotiated_values, 2539 "DataPDUInOrder", &boolean_val)) != ENOENT) { 2540 ASSERT(nvrc == 0); 2541 ict->ict_op.op_data_pdu_in_order = boolean_val; 2542 } 2543 2544 if ((nvrc = nvlist_lookup_boolean_value(lsm->icl_negotiated_values, 2545 "DataSequenceInOrder", &boolean_val)) != ENOENT) { 2546 ASSERT(nvrc == 0); 2547 ict->ict_op.op_data_sequence_in_order = boolean_val; 2548 } 2549 2550 if ((nvrc = nvlist_lookup_uint64(lsm->icl_negotiated_values, 2551 "MaxConnections", &uint64_val)) != ENOENT) { 2552 ASSERT(nvrc == 0); 2553 ict->ict_op.op_max_connections = uint64_val; 2554 } 2555 2556 if ((nvrc = nvlist_lookup_uint64(lsm->icl_negotiated_values, 2557 "MaxRecvDataSegmentLength", &uint64_val)) != ENOENT) { 2558 ASSERT(nvrc == 0); 2559 ict->ict_op.op_max_recv_data_segment_length = uint64_val; 2560 } 2561 2562 if ((nvrc = nvlist_lookup_uint64(lsm->icl_negotiated_values, 2563 "MaxBurstLength", &uint64_val)) != ENOENT) { 2564 ASSERT(nvrc == 0); 2565 ict->ict_op.op_max_burst_length = uint64_val; 2566 } 2567 2568 if ((nvrc = nvlist_lookup_uint64(lsm->icl_negotiated_values, 2569 "FirstBurstLength", &uint64_val)) != ENOENT) { 2570 ASSERT(nvrc == 0); 2571 ict->ict_op.op_first_burst_length = uint64_val; 2572 } 2573 2574 if ((nvrc = nvlist_lookup_uint64(lsm->icl_negotiated_values, 2575 "DefaultTime2Wait", &uint64_val)) != ENOENT) { 2576 ASSERT(nvrc == 0); 2577 ict->ict_op.op_default_time_2_wait = uint64_val; 2578 } 2579 2580 if ((nvrc = nvlist_lookup_uint64(lsm->icl_negotiated_values, 2581 "DefaultTime2Retain", &uint64_val)) != ENOENT) { 2582 ASSERT(nvrc == 0); 2583 ict->ict_op.op_default_time_2_retain = uint64_val; 2584 } 2585 2586 if ((nvrc = nvlist_lookup_uint64(lsm->icl_negotiated_values, 2587 "MaxOutstandingR2T", &uint64_val)) != ENOENT) { 2588 ASSERT(nvrc == 0); 2589 ict->ict_op.op_max_outstanding_r2t = uint64_val; 2590 } 2591 2592 if ((nvrc = nvlist_lookup_uint64(lsm->icl_negotiated_values, 2593 "ErrorRecoveryLevel", &uint64_val)) != ENOENT) { 2594 ASSERT(nvrc == 0); 2595 ict->ict_op.op_error_recovery_level = uint64_val; 2596 } 2597 } 2598 2599 static idm_status_t 2600 iscsit_add_declarative_keys(iscsit_conn_t *ict) 2601 { 2602 nvlist_t *cfg_nv = NULL; 2603 kv_status_t kvrc; 2604 int nvrc; 2605 iscsit_conn_login_t *lsm = &ict->ict_login_sm; 2606 uint8_t error_class; 2607 uint8_t error_detail; 2608 idm_status_t idm_status; 2609 2610 if ((nvrc = nvlist_alloc(&cfg_nv, NV_UNIQUE_NAME, KM_NOSLEEP)) != 0) { 2611 kvrc = idm_nvstat_to_kvstat(nvrc); 2612 goto alloc_fail; 2613 } 2614 if ((nvrc = nvlist_add_uint64(cfg_nv, "MaxRecvDataSegmentLength", 2615 max_dataseglen_target)) != 0) { 2616 kvrc = idm_nvstat_to_kvstat(nvrc); 2617 goto done; 2618 } 2619 if ((nvrc = nvlist_add_uint64(cfg_nv, "MaxOutstandingUnexpectedPDUs", 2620 ISCSIT_MAX_OUTSTANDING_UNEXPECTED_PDUS)) != 0) { 2621 kvrc = idm_nvstat_to_kvstat(nvrc); 2622 goto done; 2623 } 2624 2625 kvrc = idm_declare_key_values(ict->ict_ic, cfg_nv, 2626 lsm->icl_response_nvlist); 2627 done: 2628 nvlist_free(cfg_nv); 2629 alloc_fail: 2630 idm_kvstat_to_error(kvrc, &error_class, &error_detail); 2631 if (error_class == ISCSI_STATUS_CLASS_SUCCESS) { 2632 idm_status = IDM_STATUS_SUCCESS; 2633 } else { 2634 SET_LOGIN_ERROR(ict, error_class, error_detail); 2635 idm_status = IDM_STATUS_FAIL; 2636 } 2637 return (idm_status); 2638 } 2639