1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /******************************************************************************* 3 * This file contains the login functions used by the iSCSI Target driver. 4 * 5 * (c) Copyright 2007-2013 Datera, Inc. 6 * 7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org> 8 * 9 ******************************************************************************/ 10 11 #include <crypto/hash.h> 12 #include <linux/module.h> 13 #include <linux/string.h> 14 #include <linux/kthread.h> 15 #include <linux/sched/signal.h> 16 #include <linux/idr.h> 17 #include <linux/tcp.h> /* TCP_NODELAY */ 18 #include <net/ip.h> 19 #include <net/ipv6.h> /* ipv6_addr_v4mapped() */ 20 #include <scsi/iscsi_proto.h> 21 #include <target/target_core_base.h> 22 #include <target/target_core_fabric.h> 23 24 #include <target/iscsi/iscsi_target_core.h> 25 #include <target/iscsi/iscsi_target_stat.h> 26 #include "iscsi_target_device.h" 27 #include "iscsi_target_nego.h" 28 #include "iscsi_target_erl0.h" 29 #include "iscsi_target_erl2.h" 30 #include "iscsi_target_login.h" 31 #include "iscsi_target_tpg.h" 32 #include "iscsi_target_util.h" 33 #include "iscsi_target.h" 34 #include "iscsi_target_parameters.h" 35 36 #include <target/iscsi/iscsi_transport.h> 37 38 static struct iscsi_login *iscsi_login_init_conn(struct iscsit_conn *conn) 39 { 40 struct iscsi_login *login; 41 42 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL); 43 if (!login) { 44 pr_err("Unable to allocate memory for struct iscsi_login.\n"); 45 return NULL; 46 } 47 conn->login = login; 48 login->conn = conn; 49 login->first_request = 1; 50 51 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL); 52 if (!login->req_buf) { 53 pr_err("Unable to allocate memory for response buffer.\n"); 54 goto out_login; 55 } 56 57 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL); 58 if (!login->rsp_buf) { 59 pr_err("Unable to allocate memory for request buffer.\n"); 60 goto out_req_buf; 61 } 62 63 conn->conn_login = login; 64 65 return login; 66 67 out_req_buf: 68 kfree(login->req_buf); 69 out_login: 70 kfree(login); 71 return NULL; 72 } 73 74 /* 75 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup 76 * per struct iscsit_conn libcrypto contexts for crc32c and crc32-intel 77 */ 78 int iscsi_login_setup_crypto(struct iscsit_conn *conn) 79 { 80 struct crypto_ahash *tfm; 81 82 /* 83 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts 84 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback 85 * to software 1x8 byte slicing from crc32c.ko 86 */ 87 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC); 88 if (IS_ERR(tfm)) { 89 pr_err("crypto_alloc_ahash() failed\n"); 90 return -ENOMEM; 91 } 92 93 conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL); 94 if (!conn->conn_rx_hash) { 95 pr_err("ahash_request_alloc() failed for conn_rx_hash\n"); 96 crypto_free_ahash(tfm); 97 return -ENOMEM; 98 } 99 ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL); 100 101 conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL); 102 if (!conn->conn_tx_hash) { 103 pr_err("ahash_request_alloc() failed for conn_tx_hash\n"); 104 ahash_request_free(conn->conn_rx_hash); 105 conn->conn_rx_hash = NULL; 106 crypto_free_ahash(tfm); 107 return -ENOMEM; 108 } 109 ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL); 110 111 return 0; 112 } 113 114 static int iscsi_login_check_initiator_version( 115 struct iscsit_conn *conn, 116 u8 version_max, 117 u8 version_min) 118 { 119 if ((version_max != 0x00) || (version_min != 0x00)) { 120 pr_err("Unsupported iSCSI IETF Pre-RFC Revision," 121 " version Min/Max 0x%02x/0x%02x, rejecting login.\n", 122 version_min, version_max); 123 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 124 ISCSI_LOGIN_STATUS_NO_VERSION); 125 return -1; 126 } 127 128 return 0; 129 } 130 131 int iscsi_check_for_session_reinstatement(struct iscsit_conn *conn) 132 { 133 int sessiontype; 134 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL; 135 struct iscsi_portal_group *tpg = conn->tpg; 136 struct iscsit_session *sess = NULL, *sess_p = NULL; 137 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; 138 struct se_session *se_sess, *se_sess_tmp; 139 140 initiatorname_param = iscsi_find_param_from_key( 141 INITIATORNAME, conn->param_list); 142 sessiontype_param = iscsi_find_param_from_key( 143 SESSIONTYPE, conn->param_list); 144 if (!initiatorname_param || !sessiontype_param) { 145 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 146 ISCSI_LOGIN_STATUS_MISSING_FIELDS); 147 return -1; 148 } 149 150 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0; 151 152 spin_lock_bh(&se_tpg->session_lock); 153 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list, 154 sess_list) { 155 156 sess_p = se_sess->fabric_sess_ptr; 157 spin_lock(&sess_p->conn_lock); 158 if (atomic_read(&sess_p->session_fall_back_to_erl0) || 159 atomic_read(&sess_p->session_logout) || 160 atomic_read(&sess_p->session_close) || 161 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) { 162 spin_unlock(&sess_p->conn_lock); 163 continue; 164 } 165 if (!memcmp(sess_p->isid, conn->sess->isid, 6) && 166 (!strcmp(sess_p->sess_ops->InitiatorName, 167 initiatorname_param->value) && 168 (sess_p->sess_ops->SessionType == sessiontype))) { 169 atomic_set(&sess_p->session_reinstatement, 1); 170 atomic_set(&sess_p->session_fall_back_to_erl0, 1); 171 atomic_set(&sess_p->session_close, 1); 172 spin_unlock(&sess_p->conn_lock); 173 iscsit_inc_session_usage_count(sess_p); 174 iscsit_stop_time2retain_timer(sess_p); 175 sess = sess_p; 176 break; 177 } 178 spin_unlock(&sess_p->conn_lock); 179 } 180 spin_unlock_bh(&se_tpg->session_lock); 181 /* 182 * If the Time2Retain handler has expired, the session is already gone. 183 */ 184 if (!sess) 185 return 0; 186 187 pr_debug("%s iSCSI Session SID %u is still active for %s," 188 " performing session reinstatement.\n", (sessiontype) ? 189 "Discovery" : "Normal", sess->sid, 190 sess->sess_ops->InitiatorName); 191 192 spin_lock_bh(&sess->conn_lock); 193 if (sess->session_state == TARG_SESS_STATE_FAILED) { 194 spin_unlock_bh(&sess->conn_lock); 195 iscsit_dec_session_usage_count(sess); 196 return 0; 197 } 198 spin_unlock_bh(&sess->conn_lock); 199 200 iscsit_stop_session(sess, 1, 1); 201 iscsit_dec_session_usage_count(sess); 202 203 return 0; 204 } 205 206 static int iscsi_login_set_conn_values( 207 struct iscsit_session *sess, 208 struct iscsit_conn *conn, 209 __be16 cid) 210 { 211 int ret; 212 conn->sess = sess; 213 conn->cid = be16_to_cpu(cid); 214 /* 215 * Generate a random Status sequence number (statsn) for the new 216 * iSCSI connection. 217 */ 218 ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32)); 219 if (unlikely(ret)) 220 return ret; 221 222 mutex_lock(&auth_id_lock); 223 conn->auth_id = iscsit_global->auth_id++; 224 mutex_unlock(&auth_id_lock); 225 return 0; 226 } 227 228 __printf(2, 3) int iscsi_change_param_sprintf( 229 struct iscsit_conn *conn, 230 const char *fmt, ...) 231 { 232 va_list args; 233 unsigned char buf[64]; 234 235 memset(buf, 0, sizeof buf); 236 237 va_start(args, fmt); 238 vsnprintf(buf, sizeof buf, fmt, args); 239 va_end(args); 240 241 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) { 242 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 243 ISCSI_LOGIN_STATUS_NO_RESOURCES); 244 return -1; 245 } 246 247 return 0; 248 } 249 EXPORT_SYMBOL(iscsi_change_param_sprintf); 250 251 /* 252 * This is the leading connection of a new session, 253 * or session reinstatement. 254 */ 255 static int iscsi_login_zero_tsih_s1( 256 struct iscsit_conn *conn, 257 unsigned char *buf) 258 { 259 struct iscsit_session *sess = NULL; 260 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf; 261 int ret; 262 263 sess = kzalloc(sizeof(struct iscsit_session), GFP_KERNEL); 264 if (!sess) { 265 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 266 ISCSI_LOGIN_STATUS_NO_RESOURCES); 267 pr_err("Could not allocate memory for session\n"); 268 return -ENOMEM; 269 } 270 271 if (iscsi_login_set_conn_values(sess, conn, pdu->cid)) 272 goto free_sess; 273 274 sess->init_task_tag = pdu->itt; 275 memcpy(&sess->isid, pdu->isid, 6); 276 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn); 277 INIT_LIST_HEAD(&sess->sess_conn_list); 278 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list); 279 INIT_LIST_HEAD(&sess->cr_active_list); 280 INIT_LIST_HEAD(&sess->cr_inactive_list); 281 init_completion(&sess->async_msg_comp); 282 init_completion(&sess->reinstatement_comp); 283 init_completion(&sess->session_wait_comp); 284 init_completion(&sess->session_waiting_on_uc_comp); 285 mutex_init(&sess->cmdsn_mutex); 286 spin_lock_init(&sess->conn_lock); 287 spin_lock_init(&sess->cr_a_lock); 288 spin_lock_init(&sess->cr_i_lock); 289 spin_lock_init(&sess->session_usage_lock); 290 spin_lock_init(&sess->ttt_lock); 291 292 timer_setup(&sess->time2retain_timer, 293 iscsit_handle_time2retain_timeout, 0); 294 295 ret = ida_alloc(&sess_ida, GFP_KERNEL); 296 if (ret < 0) { 297 pr_err("Session ID allocation failed %d\n", ret); 298 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 299 ISCSI_LOGIN_STATUS_NO_RESOURCES); 300 goto free_sess; 301 } 302 303 sess->session_index = ret; 304 sess->creation_time = get_jiffies_64(); 305 /* 306 * The FFP CmdSN window values will be allocated from the TPG's 307 * Initiator Node's ACL once the login has been successfully completed. 308 */ 309 atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn)); 310 311 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL); 312 if (!sess->sess_ops) { 313 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 314 ISCSI_LOGIN_STATUS_NO_RESOURCES); 315 pr_err("Unable to allocate memory for" 316 " struct iscsi_sess_ops.\n"); 317 goto free_id; 318 } 319 320 sess->se_sess = transport_alloc_session(TARGET_PROT_NORMAL); 321 if (IS_ERR(sess->se_sess)) { 322 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 323 ISCSI_LOGIN_STATUS_NO_RESOURCES); 324 goto free_ops; 325 } 326 327 return 0; 328 329 free_ops: 330 kfree(sess->sess_ops); 331 free_id: 332 ida_free(&sess_ida, sess->session_index); 333 free_sess: 334 kfree(sess); 335 conn->sess = NULL; 336 return -ENOMEM; 337 } 338 339 static int iscsi_login_zero_tsih_s2( 340 struct iscsit_conn *conn) 341 { 342 struct iscsi_node_attrib *na; 343 struct iscsit_session *sess = conn->sess; 344 struct iscsi_param *param; 345 bool iser = false; 346 347 sess->tpg = conn->tpg; 348 349 /* 350 * Assign a new TPG Session Handle. Note this is protected with 351 * struct iscsi_portal_group->np_login_sem from iscsit_access_np(). 352 */ 353 sess->tsih = ++sess->tpg->ntsih; 354 if (!sess->tsih) 355 sess->tsih = ++sess->tpg->ntsih; 356 357 /* 358 * Create the default params from user defined values.. 359 */ 360 if (iscsi_copy_param_list(&conn->param_list, 361 conn->tpg->param_list, 1) < 0) { 362 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 363 ISCSI_LOGIN_STATUS_NO_RESOURCES); 364 return -1; 365 } 366 367 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) 368 iser = true; 369 370 iscsi_set_keys_to_negotiate(conn->param_list, iser); 371 372 if (sess->sess_ops->SessionType) 373 return iscsi_set_keys_irrelevant_for_discovery( 374 conn->param_list); 375 376 na = iscsit_tpg_get_node_attrib(sess); 377 378 /* 379 * If ACL allows non-authorized access in TPG with CHAP, 380 * then set None to AuthMethod. 381 */ 382 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list); 383 if (param && !strstr(param->value, NONE)) { 384 if (!iscsi_conn_auth_required(conn)) 385 if (iscsi_change_param_sprintf(conn, "AuthMethod=%s", 386 NONE)) 387 return -1; 388 } 389 390 /* 391 * Need to send TargetPortalGroupTag back in first login response 392 * on any iSCSI connection where the Initiator provides TargetName. 393 * See 5.3.1. Login Phase Start 394 * 395 * In our case, we have already located the struct iscsi_tiqn at this point. 396 */ 397 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt)) 398 return -1; 399 400 /* 401 * Workaround for Initiators that have broken connection recovery logic. 402 * 403 * "We would really like to get rid of this." Linux-iSCSI.org team 404 */ 405 if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl)) 406 return -1; 407 408 /* 409 * Set RDMAExtensions=Yes by default for iSER enabled network portals 410 */ 411 if (iser) { 412 struct iscsi_param *param; 413 unsigned long mrdsl, off; 414 int rc; 415 416 if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes")) 417 return -1; 418 419 /* 420 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for 421 * Immediate Data + Unsolicited Data-OUT if necessary.. 422 */ 423 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength", 424 conn->param_list); 425 if (!param) { 426 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 427 ISCSI_LOGIN_STATUS_NO_RESOURCES); 428 return -1; 429 } 430 rc = kstrtoul(param->value, 0, &mrdsl); 431 if (rc < 0) { 432 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 433 ISCSI_LOGIN_STATUS_NO_RESOURCES); 434 return -1; 435 } 436 off = mrdsl % PAGE_SIZE; 437 if (!off) 438 goto check_prot; 439 440 if (mrdsl < PAGE_SIZE) 441 mrdsl = PAGE_SIZE; 442 else 443 mrdsl -= off; 444 445 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down" 446 " to PAGE_SIZE\n", mrdsl); 447 448 if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl)) 449 return -1; 450 /* 451 * ISER currently requires that ImmediateData + Unsolicited 452 * Data be disabled when protection / signature MRs are enabled. 453 */ 454 check_prot: 455 if (sess->se_sess->sup_prot_ops & 456 (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS | 457 TARGET_PROT_DOUT_INSERT)) { 458 459 if (iscsi_change_param_sprintf(conn, "ImmediateData=No")) 460 return -1; 461 462 if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes")) 463 return -1; 464 465 pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for" 466 " T10-PI enabled ISER session\n"); 467 } 468 } 469 470 return 0; 471 } 472 473 static int iscsi_login_non_zero_tsih_s1( 474 struct iscsit_conn *conn, 475 unsigned char *buf) 476 { 477 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf; 478 479 return iscsi_login_set_conn_values(NULL, conn, pdu->cid); 480 } 481 482 /* 483 * Add a new connection to an existing session. 484 */ 485 static int iscsi_login_non_zero_tsih_s2( 486 struct iscsit_conn *conn, 487 unsigned char *buf) 488 { 489 struct iscsi_portal_group *tpg = conn->tpg; 490 struct iscsit_session *sess = NULL, *sess_p = NULL; 491 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; 492 struct se_session *se_sess, *se_sess_tmp; 493 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf; 494 bool iser = false; 495 496 spin_lock_bh(&se_tpg->session_lock); 497 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list, 498 sess_list) { 499 500 sess_p = (struct iscsit_session *)se_sess->fabric_sess_ptr; 501 if (atomic_read(&sess_p->session_fall_back_to_erl0) || 502 atomic_read(&sess_p->session_logout) || 503 atomic_read(&sess_p->session_close) || 504 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) 505 continue; 506 if (!memcmp(sess_p->isid, pdu->isid, 6) && 507 (sess_p->tsih == be16_to_cpu(pdu->tsih))) { 508 iscsit_inc_session_usage_count(sess_p); 509 iscsit_stop_time2retain_timer(sess_p); 510 sess = sess_p; 511 break; 512 } 513 } 514 spin_unlock_bh(&se_tpg->session_lock); 515 516 /* 517 * If the Time2Retain handler has expired, the session is already gone. 518 */ 519 if (!sess) { 520 pr_err("Initiator attempting to add a connection to" 521 " a non-existent session, rejecting iSCSI Login.\n"); 522 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 523 ISCSI_LOGIN_STATUS_NO_SESSION); 524 return -1; 525 } 526 527 /* 528 * Stop the Time2Retain timer if this is a failed session, we restart 529 * the timer if the login is not successful. 530 */ 531 spin_lock_bh(&sess->conn_lock); 532 if (sess->session_state == TARG_SESS_STATE_FAILED) 533 atomic_set(&sess->session_continuation, 1); 534 spin_unlock_bh(&sess->conn_lock); 535 536 if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 || 537 iscsi_copy_param_list(&conn->param_list, 538 conn->tpg->param_list, 0) < 0) { 539 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 540 ISCSI_LOGIN_STATUS_NO_RESOURCES); 541 return -1; 542 } 543 544 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) 545 iser = true; 546 547 iscsi_set_keys_to_negotiate(conn->param_list, iser); 548 /* 549 * Need to send TargetPortalGroupTag back in first login response 550 * on any iSCSI connection where the Initiator provides TargetName. 551 * See 5.3.1. Login Phase Start 552 * 553 * In our case, we have already located the struct iscsi_tiqn at this point. 554 */ 555 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt)) 556 return -1; 557 558 return 0; 559 } 560 561 int iscsi_login_post_auth_non_zero_tsih( 562 struct iscsit_conn *conn, 563 u16 cid, 564 u32 exp_statsn) 565 { 566 struct iscsit_conn *conn_ptr = NULL; 567 struct iscsi_conn_recovery *cr = NULL; 568 struct iscsit_session *sess = conn->sess; 569 570 /* 571 * By following item 5 in the login table, if we have found 572 * an existing ISID and a valid/existing TSIH and an existing 573 * CID we do connection reinstatement. Currently we dont not 574 * support it so we send back an non-zero status class to the 575 * initiator and release the new connection. 576 */ 577 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid); 578 if (conn_ptr) { 579 pr_err("Connection exists with CID %hu for %s," 580 " performing connection reinstatement.\n", 581 conn_ptr->cid, sess->sess_ops->InitiatorName); 582 583 iscsit_connection_reinstatement_rcfr(conn_ptr); 584 iscsit_dec_conn_usage_count(conn_ptr); 585 } 586 587 /* 588 * Check for any connection recovery entries containing CID. 589 * We use the original ExpStatSN sent in the first login request 590 * to acknowledge commands for the failed connection. 591 * 592 * Also note that an explict logout may have already been sent, 593 * but the response may not be sent due to additional connection 594 * loss. 595 */ 596 if (sess->sess_ops->ErrorRecoveryLevel == 2) { 597 cr = iscsit_get_inactive_connection_recovery_entry( 598 sess, cid); 599 if (cr) { 600 pr_debug("Performing implicit logout" 601 " for connection recovery on CID: %hu\n", 602 conn->cid); 603 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn); 604 } 605 } 606 607 /* 608 * Else we follow item 4 from the login table in that we have 609 * found an existing ISID and a valid/existing TSIH and a new 610 * CID we go ahead and continue to add a new connection to the 611 * session. 612 */ 613 pr_debug("Adding CID %hu to existing session for %s.\n", 614 cid, sess->sess_ops->InitiatorName); 615 616 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) { 617 pr_err("Adding additional connection to this session" 618 " would exceed MaxConnections %d, login failed.\n", 619 sess->sess_ops->MaxConnections); 620 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 621 ISCSI_LOGIN_STATUS_ISID_ERROR); 622 return -1; 623 } 624 625 return 0; 626 } 627 628 static void iscsi_post_login_start_timers(struct iscsit_conn *conn) 629 { 630 struct iscsit_session *sess = conn->sess; 631 /* 632 * FIXME: Unsolicited NopIN support for ISER 633 */ 634 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) 635 return; 636 637 if (!sess->sess_ops->SessionType) 638 iscsit_start_nopin_timer(conn); 639 } 640 641 int iscsit_start_kthreads(struct iscsit_conn *conn) 642 { 643 int ret = 0; 644 645 spin_lock(&iscsit_global->ts_bitmap_lock); 646 conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap, 647 ISCSIT_BITMAP_BITS, get_order(1)); 648 spin_unlock(&iscsit_global->ts_bitmap_lock); 649 650 if (conn->bitmap_id < 0) { 651 pr_err("bitmap_find_free_region() failed for" 652 " iscsit_start_kthreads()\n"); 653 return -ENOMEM; 654 } 655 656 conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn, 657 "%s", ISCSI_TX_THREAD_NAME); 658 if (IS_ERR(conn->tx_thread)) { 659 pr_err("Unable to start iscsi_target_tx_thread\n"); 660 ret = PTR_ERR(conn->tx_thread); 661 goto out_bitmap; 662 } 663 conn->tx_thread_active = true; 664 665 conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn, 666 "%s", ISCSI_RX_THREAD_NAME); 667 if (IS_ERR(conn->rx_thread)) { 668 pr_err("Unable to start iscsi_target_rx_thread\n"); 669 ret = PTR_ERR(conn->rx_thread); 670 goto out_tx; 671 } 672 conn->rx_thread_active = true; 673 674 return 0; 675 out_tx: 676 send_sig(SIGINT, conn->tx_thread, 1); 677 kthread_stop(conn->tx_thread); 678 conn->tx_thread_active = false; 679 out_bitmap: 680 spin_lock(&iscsit_global->ts_bitmap_lock); 681 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id, 682 get_order(1)); 683 spin_unlock(&iscsit_global->ts_bitmap_lock); 684 return ret; 685 } 686 687 void iscsi_post_login_handler( 688 struct iscsi_np *np, 689 struct iscsit_conn *conn, 690 u8 zero_tsih) 691 { 692 int stop_timer = 0; 693 struct iscsit_session *sess = conn->sess; 694 struct se_session *se_sess = sess->se_sess; 695 struct iscsi_portal_group *tpg = sess->tpg; 696 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; 697 698 iscsit_inc_conn_usage_count(conn); 699 700 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS, 701 ISCSI_LOGIN_STATUS_ACCEPT); 702 703 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n"); 704 conn->conn_state = TARG_CONN_STATE_LOGGED_IN; 705 706 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list); 707 /* 708 * SCSI Initiator -> SCSI Target Port Mapping 709 */ 710 if (!zero_tsih) { 711 iscsi_set_session_parameters(sess->sess_ops, 712 conn->param_list, 0); 713 iscsi_release_param_list(conn->param_list); 714 conn->param_list = NULL; 715 716 spin_lock_bh(&sess->conn_lock); 717 atomic_set(&sess->session_continuation, 0); 718 if (sess->session_state == TARG_SESS_STATE_FAILED) { 719 pr_debug("Moving to" 720 " TARG_SESS_STATE_LOGGED_IN.\n"); 721 sess->session_state = TARG_SESS_STATE_LOGGED_IN; 722 stop_timer = 1; 723 } 724 725 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to" 726 " %pISpc,%hu\n", conn->cid, &conn->login_sockaddr, 727 &conn->local_sockaddr, tpg->tpgt); 728 729 list_add_tail(&conn->conn_list, &sess->sess_conn_list); 730 atomic_inc(&sess->nconn); 731 pr_debug("Incremented iSCSI Connection count to %d" 732 " from node: %s\n", atomic_read(&sess->nconn), 733 sess->sess_ops->InitiatorName); 734 spin_unlock_bh(&sess->conn_lock); 735 736 iscsi_post_login_start_timers(conn); 737 /* 738 * Determine CPU mask to ensure connection's RX and TX kthreads 739 * are scheduled on the same CPU. 740 */ 741 iscsit_thread_get_cpumask(conn); 742 conn->conn_rx_reset_cpumask = 1; 743 conn->conn_tx_reset_cpumask = 1; 744 /* 745 * Wakeup the sleeping iscsi_target_rx_thread() now that 746 * iscsit_conn is in TARG_CONN_STATE_LOGGED_IN state. 747 */ 748 complete(&conn->rx_login_comp); 749 iscsit_dec_conn_usage_count(conn); 750 751 if (stop_timer) { 752 spin_lock_bh(&se_tpg->session_lock); 753 iscsit_stop_time2retain_timer(sess); 754 spin_unlock_bh(&se_tpg->session_lock); 755 } 756 iscsit_dec_session_usage_count(sess); 757 return; 758 } 759 760 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1); 761 iscsi_release_param_list(conn->param_list); 762 conn->param_list = NULL; 763 764 iscsit_determine_maxcmdsn(sess); 765 766 spin_lock_bh(&se_tpg->session_lock); 767 __transport_register_session(&sess->tpg->tpg_se_tpg, 768 se_sess->se_node_acl, se_sess, sess); 769 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n"); 770 sess->session_state = TARG_SESS_STATE_LOGGED_IN; 771 772 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n", 773 conn->cid, &conn->login_sockaddr, &conn->local_sockaddr, 774 tpg->tpgt); 775 776 spin_lock_bh(&sess->conn_lock); 777 list_add_tail(&conn->conn_list, &sess->sess_conn_list); 778 atomic_inc(&sess->nconn); 779 pr_debug("Incremented iSCSI Connection count to %d from node:" 780 " %s\n", atomic_read(&sess->nconn), 781 sess->sess_ops->InitiatorName); 782 spin_unlock_bh(&sess->conn_lock); 783 784 sess->sid = tpg->sid++; 785 if (!sess->sid) 786 sess->sid = tpg->sid++; 787 pr_debug("Established iSCSI session from node: %s\n", 788 sess->sess_ops->InitiatorName); 789 790 tpg->nsessions++; 791 if (tpg->tpg_tiqn) 792 tpg->tpg_tiqn->tiqn_nsessions++; 793 794 pr_debug("Incremented number of active iSCSI sessions to %u on" 795 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt); 796 spin_unlock_bh(&se_tpg->session_lock); 797 798 iscsi_post_login_start_timers(conn); 799 /* 800 * Determine CPU mask to ensure connection's RX and TX kthreads 801 * are scheduled on the same CPU. 802 */ 803 iscsit_thread_get_cpumask(conn); 804 conn->conn_rx_reset_cpumask = 1; 805 conn->conn_tx_reset_cpumask = 1; 806 /* 807 * Wakeup the sleeping iscsi_target_rx_thread() now that 808 * iscsit_conn is in TARG_CONN_STATE_LOGGED_IN state. 809 */ 810 complete(&conn->rx_login_comp); 811 iscsit_dec_conn_usage_count(conn); 812 } 813 814 void iscsi_handle_login_thread_timeout(struct timer_list *t) 815 { 816 struct iscsi_np *np = from_timer(np, t, np_login_timer); 817 818 spin_lock_bh(&np->np_thread_lock); 819 pr_err("iSCSI Login timeout on Network Portal %pISpc\n", 820 &np->np_sockaddr); 821 822 if (np->np_login_timer_flags & ISCSI_TF_STOP) { 823 spin_unlock_bh(&np->np_thread_lock); 824 return; 825 } 826 827 if (np->np_thread) 828 send_sig(SIGINT, np->np_thread, 1); 829 830 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING; 831 spin_unlock_bh(&np->np_thread_lock); 832 } 833 834 static void iscsi_start_login_thread_timer(struct iscsi_np *np) 835 { 836 /* 837 * This used the TA_LOGIN_TIMEOUT constant because at this 838 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout 839 */ 840 spin_lock_bh(&np->np_thread_lock); 841 np->np_login_timer_flags &= ~ISCSI_TF_STOP; 842 np->np_login_timer_flags |= ISCSI_TF_RUNNING; 843 mod_timer(&np->np_login_timer, jiffies + TA_LOGIN_TIMEOUT * HZ); 844 845 pr_debug("Added timeout timer to iSCSI login request for" 846 " %u seconds.\n", TA_LOGIN_TIMEOUT); 847 spin_unlock_bh(&np->np_thread_lock); 848 } 849 850 static void iscsi_stop_login_thread_timer(struct iscsi_np *np) 851 { 852 spin_lock_bh(&np->np_thread_lock); 853 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) { 854 spin_unlock_bh(&np->np_thread_lock); 855 return; 856 } 857 np->np_login_timer_flags |= ISCSI_TF_STOP; 858 spin_unlock_bh(&np->np_thread_lock); 859 860 del_timer_sync(&np->np_login_timer); 861 862 spin_lock_bh(&np->np_thread_lock); 863 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING; 864 spin_unlock_bh(&np->np_thread_lock); 865 } 866 867 int iscsit_setup_np( 868 struct iscsi_np *np, 869 struct sockaddr_storage *sockaddr) 870 { 871 struct socket *sock = NULL; 872 int backlog = ISCSIT_TCP_BACKLOG, ret, len; 873 874 switch (np->np_network_transport) { 875 case ISCSI_TCP: 876 np->np_ip_proto = IPPROTO_TCP; 877 np->np_sock_type = SOCK_STREAM; 878 break; 879 case ISCSI_SCTP_TCP: 880 np->np_ip_proto = IPPROTO_SCTP; 881 np->np_sock_type = SOCK_STREAM; 882 break; 883 case ISCSI_SCTP_UDP: 884 np->np_ip_proto = IPPROTO_SCTP; 885 np->np_sock_type = SOCK_SEQPACKET; 886 break; 887 default: 888 pr_err("Unsupported network_transport: %d\n", 889 np->np_network_transport); 890 return -EINVAL; 891 } 892 893 ret = sock_create(sockaddr->ss_family, np->np_sock_type, 894 np->np_ip_proto, &sock); 895 if (ret < 0) { 896 pr_err("sock_create() failed.\n"); 897 return ret; 898 } 899 np->np_socket = sock; 900 /* 901 * Setup the np->np_sockaddr from the passed sockaddr setup 902 * in iscsi_target_configfs.c code.. 903 */ 904 memcpy(&np->np_sockaddr, sockaddr, 905 sizeof(struct sockaddr_storage)); 906 907 if (sockaddr->ss_family == AF_INET6) 908 len = sizeof(struct sockaddr_in6); 909 else 910 len = sizeof(struct sockaddr_in); 911 /* 912 * Set SO_REUSEADDR, and disable Nagle Algorithm with TCP_NODELAY. 913 */ 914 if (np->np_network_transport == ISCSI_TCP) 915 tcp_sock_set_nodelay(sock->sk); 916 sock_set_reuseaddr(sock->sk); 917 ip_sock_set_freebind(sock->sk); 918 919 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len); 920 if (ret < 0) { 921 pr_err("kernel_bind() failed: %d\n", ret); 922 goto fail; 923 } 924 925 ret = kernel_listen(sock, backlog); 926 if (ret != 0) { 927 pr_err("kernel_listen() failed: %d\n", ret); 928 goto fail; 929 } 930 931 return 0; 932 fail: 933 np->np_socket = NULL; 934 sock_release(sock); 935 return ret; 936 } 937 938 int iscsi_target_setup_login_socket( 939 struct iscsi_np *np, 940 struct sockaddr_storage *sockaddr) 941 { 942 struct iscsit_transport *t; 943 int rc; 944 945 t = iscsit_get_transport(np->np_network_transport); 946 if (!t) 947 return -EINVAL; 948 949 rc = t->iscsit_setup_np(np, sockaddr); 950 if (rc < 0) { 951 iscsit_put_transport(t); 952 return rc; 953 } 954 955 np->np_transport = t; 956 np->enabled = true; 957 return 0; 958 } 959 960 int iscsit_accept_np(struct iscsi_np *np, struct iscsit_conn *conn) 961 { 962 struct socket *new_sock, *sock = np->np_socket; 963 struct sockaddr_in sock_in; 964 struct sockaddr_in6 sock_in6; 965 int rc; 966 967 rc = kernel_accept(sock, &new_sock, 0); 968 if (rc < 0) 969 return rc; 970 971 conn->sock = new_sock; 972 conn->login_family = np->np_sockaddr.ss_family; 973 974 if (np->np_sockaddr.ss_family == AF_INET6) { 975 memset(&sock_in6, 0, sizeof(struct sockaddr_in6)); 976 977 rc = conn->sock->ops->getname(conn->sock, 978 (struct sockaddr *)&sock_in6, 1); 979 if (rc >= 0) { 980 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) { 981 memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6)); 982 } else { 983 /* Pretend to be an ipv4 socket */ 984 sock_in.sin_family = AF_INET; 985 sock_in.sin_port = sock_in6.sin6_port; 986 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4); 987 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in)); 988 } 989 } 990 991 rc = conn->sock->ops->getname(conn->sock, 992 (struct sockaddr *)&sock_in6, 0); 993 if (rc >= 0) { 994 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) { 995 memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6)); 996 } else { 997 /* Pretend to be an ipv4 socket */ 998 sock_in.sin_family = AF_INET; 999 sock_in.sin_port = sock_in6.sin6_port; 1000 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4); 1001 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in)); 1002 } 1003 } 1004 } else { 1005 memset(&sock_in, 0, sizeof(struct sockaddr_in)); 1006 1007 rc = conn->sock->ops->getname(conn->sock, 1008 (struct sockaddr *)&sock_in, 1); 1009 if (rc >= 0) 1010 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in)); 1011 1012 rc = conn->sock->ops->getname(conn->sock, 1013 (struct sockaddr *)&sock_in, 0); 1014 if (rc >= 0) 1015 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in)); 1016 } 1017 1018 return 0; 1019 } 1020 1021 int iscsit_get_login_rx(struct iscsit_conn *conn, struct iscsi_login *login) 1022 { 1023 struct iscsi_login_req *login_req; 1024 u32 padding = 0, payload_length; 1025 1026 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0) 1027 return -1; 1028 1029 login_req = (struct iscsi_login_req *)login->req; 1030 payload_length = ntoh24(login_req->dlength); 1031 padding = ((-payload_length) & 3); 1032 1033 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x," 1034 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n", 1035 login_req->flags, login_req->itt, login_req->cmdsn, 1036 login_req->exp_statsn, login_req->cid, payload_length); 1037 /* 1038 * Setup the initial iscsi_login values from the leading 1039 * login request PDU. 1040 */ 1041 if (login->first_request) { 1042 login_req = (struct iscsi_login_req *)login->req; 1043 login->leading_connection = (!login_req->tsih) ? 1 : 0; 1044 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags); 1045 login->version_min = login_req->min_version; 1046 login->version_max = login_req->max_version; 1047 memcpy(login->isid, login_req->isid, 6); 1048 login->cmd_sn = be32_to_cpu(login_req->cmdsn); 1049 login->init_task_tag = login_req->itt; 1050 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn); 1051 login->cid = be16_to_cpu(login_req->cid); 1052 login->tsih = be16_to_cpu(login_req->tsih); 1053 } 1054 1055 if (iscsi_target_check_login_request(conn, login) < 0) 1056 return -1; 1057 1058 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS); 1059 if (iscsi_login_rx_data(conn, login->req_buf, 1060 payload_length + padding) < 0) 1061 return -1; 1062 1063 return 0; 1064 } 1065 1066 int iscsit_put_login_tx(struct iscsit_conn *conn, struct iscsi_login *login, 1067 u32 length) 1068 { 1069 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0) 1070 return -1; 1071 1072 return 0; 1073 } 1074 1075 static int 1076 iscsit_conn_set_transport(struct iscsit_conn *conn, struct iscsit_transport *t) 1077 { 1078 int rc; 1079 1080 if (!t->owner) { 1081 conn->conn_transport = t; 1082 return 0; 1083 } 1084 1085 rc = try_module_get(t->owner); 1086 if (!rc) { 1087 pr_err("try_module_get() failed for %s\n", t->name); 1088 return -EINVAL; 1089 } 1090 1091 conn->conn_transport = t; 1092 return 0; 1093 } 1094 1095 static struct iscsit_conn *iscsit_alloc_conn(struct iscsi_np *np) 1096 { 1097 struct iscsit_conn *conn; 1098 1099 conn = kzalloc(sizeof(struct iscsit_conn), GFP_KERNEL); 1100 if (!conn) { 1101 pr_err("Could not allocate memory for new connection\n"); 1102 return NULL; 1103 } 1104 pr_debug("Moving to TARG_CONN_STATE_FREE.\n"); 1105 conn->conn_state = TARG_CONN_STATE_FREE; 1106 1107 init_waitqueue_head(&conn->queues_wq); 1108 INIT_LIST_HEAD(&conn->conn_list); 1109 INIT_LIST_HEAD(&conn->conn_cmd_list); 1110 INIT_LIST_HEAD(&conn->immed_queue_list); 1111 INIT_LIST_HEAD(&conn->response_queue_list); 1112 init_completion(&conn->conn_post_wait_comp); 1113 init_completion(&conn->conn_wait_comp); 1114 init_completion(&conn->conn_wait_rcfr_comp); 1115 init_completion(&conn->conn_waiting_on_uc_comp); 1116 init_completion(&conn->conn_logout_comp); 1117 init_completion(&conn->rx_half_close_comp); 1118 init_completion(&conn->tx_half_close_comp); 1119 init_completion(&conn->rx_login_comp); 1120 spin_lock_init(&conn->cmd_lock); 1121 spin_lock_init(&conn->conn_usage_lock); 1122 spin_lock_init(&conn->immed_queue_lock); 1123 spin_lock_init(&conn->nopin_timer_lock); 1124 spin_lock_init(&conn->response_queue_lock); 1125 spin_lock_init(&conn->state_lock); 1126 1127 timer_setup(&conn->nopin_response_timer, 1128 iscsit_handle_nopin_response_timeout, 0); 1129 timer_setup(&conn->nopin_timer, iscsit_handle_nopin_timeout, 0); 1130 1131 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) 1132 goto free_conn; 1133 1134 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL); 1135 if (!conn->conn_ops) { 1136 pr_err("Unable to allocate memory for struct iscsi_conn_ops.\n"); 1137 goto put_transport; 1138 } 1139 1140 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) { 1141 pr_err("Unable to allocate conn->conn_cpumask\n"); 1142 goto free_conn_ops; 1143 } 1144 1145 if (!zalloc_cpumask_var(&conn->allowed_cpumask, GFP_KERNEL)) { 1146 pr_err("Unable to allocate conn->allowed_cpumask\n"); 1147 goto free_conn_cpumask; 1148 } 1149 1150 return conn; 1151 1152 free_conn_cpumask: 1153 free_cpumask_var(conn->conn_cpumask); 1154 free_conn_ops: 1155 kfree(conn->conn_ops); 1156 put_transport: 1157 iscsit_put_transport(conn->conn_transport); 1158 free_conn: 1159 kfree(conn); 1160 return NULL; 1161 } 1162 1163 void iscsit_free_conn(struct iscsit_conn *conn) 1164 { 1165 free_cpumask_var(conn->allowed_cpumask); 1166 free_cpumask_var(conn->conn_cpumask); 1167 kfree(conn->conn_ops); 1168 iscsit_put_transport(conn->conn_transport); 1169 kfree(conn); 1170 } 1171 1172 void iscsi_target_login_sess_out(struct iscsit_conn *conn, 1173 bool zero_tsih, bool new_sess) 1174 { 1175 if (!new_sess) 1176 goto old_sess_out; 1177 1178 pr_err("iSCSI Login negotiation failed.\n"); 1179 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1180 ISCSI_LOGIN_STATUS_INIT_ERR); 1181 if (!zero_tsih || !conn->sess) 1182 goto old_sess_out; 1183 1184 transport_free_session(conn->sess->se_sess); 1185 ida_free(&sess_ida, conn->sess->session_index); 1186 kfree(conn->sess->sess_ops); 1187 kfree(conn->sess); 1188 conn->sess = NULL; 1189 1190 old_sess_out: 1191 /* 1192 * If login negotiation fails check if the Time2Retain timer 1193 * needs to be restarted. 1194 */ 1195 if (!zero_tsih && conn->sess) { 1196 spin_lock_bh(&conn->sess->conn_lock); 1197 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) { 1198 struct se_portal_group *se_tpg = 1199 &conn->tpg->tpg_se_tpg; 1200 1201 atomic_set(&conn->sess->session_continuation, 0); 1202 spin_unlock_bh(&conn->sess->conn_lock); 1203 spin_lock_bh(&se_tpg->session_lock); 1204 iscsit_start_time2retain_handler(conn->sess); 1205 spin_unlock_bh(&se_tpg->session_lock); 1206 } else 1207 spin_unlock_bh(&conn->sess->conn_lock); 1208 iscsit_dec_session_usage_count(conn->sess); 1209 } 1210 1211 ahash_request_free(conn->conn_tx_hash); 1212 if (conn->conn_rx_hash) { 1213 struct crypto_ahash *tfm; 1214 1215 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash); 1216 ahash_request_free(conn->conn_rx_hash); 1217 crypto_free_ahash(tfm); 1218 } 1219 1220 if (conn->param_list) { 1221 iscsi_release_param_list(conn->param_list); 1222 conn->param_list = NULL; 1223 } 1224 iscsi_target_nego_release(conn); 1225 1226 if (conn->sock) { 1227 sock_release(conn->sock); 1228 conn->sock = NULL; 1229 } 1230 1231 if (conn->conn_transport->iscsit_wait_conn) 1232 conn->conn_transport->iscsit_wait_conn(conn); 1233 1234 if (conn->conn_transport->iscsit_free_conn) 1235 conn->conn_transport->iscsit_free_conn(conn); 1236 1237 iscsit_free_conn(conn); 1238 } 1239 1240 static int __iscsi_target_login_thread(struct iscsi_np *np) 1241 { 1242 u8 *buffer, zero_tsih = 0; 1243 int ret = 0, rc; 1244 struct iscsit_conn *conn = NULL; 1245 struct iscsi_login *login; 1246 struct iscsi_portal_group *tpg = NULL; 1247 struct iscsi_login_req *pdu; 1248 struct iscsi_tpg_np *tpg_np; 1249 bool new_sess = false; 1250 1251 flush_signals(current); 1252 1253 spin_lock_bh(&np->np_thread_lock); 1254 if (atomic_dec_if_positive(&np->np_reset_count) >= 0) { 1255 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE; 1256 spin_unlock_bh(&np->np_thread_lock); 1257 complete(&np->np_restart_comp); 1258 return 1; 1259 } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) { 1260 spin_unlock_bh(&np->np_thread_lock); 1261 goto exit; 1262 } else { 1263 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE; 1264 } 1265 spin_unlock_bh(&np->np_thread_lock); 1266 1267 conn = iscsit_alloc_conn(np); 1268 if (!conn) { 1269 /* Get another socket */ 1270 return 1; 1271 } 1272 1273 rc = np->np_transport->iscsit_accept_np(np, conn); 1274 if (rc == -ENOSYS) { 1275 complete(&np->np_restart_comp); 1276 iscsit_free_conn(conn); 1277 goto exit; 1278 } else if (rc < 0) { 1279 spin_lock_bh(&np->np_thread_lock); 1280 if (atomic_dec_if_positive(&np->np_reset_count) >= 0) { 1281 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE; 1282 spin_unlock_bh(&np->np_thread_lock); 1283 complete(&np->np_restart_comp); 1284 iscsit_free_conn(conn); 1285 /* Get another socket */ 1286 return 1; 1287 } 1288 spin_unlock_bh(&np->np_thread_lock); 1289 iscsit_free_conn(conn); 1290 return 1; 1291 } 1292 /* 1293 * Perform the remaining iSCSI connection initialization items.. 1294 */ 1295 login = iscsi_login_init_conn(conn); 1296 if (!login) { 1297 goto new_sess_out; 1298 } 1299 1300 iscsi_start_login_thread_timer(np); 1301 1302 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n"); 1303 conn->conn_state = TARG_CONN_STATE_XPT_UP; 1304 /* 1305 * This will process the first login request + payload.. 1306 */ 1307 rc = np->np_transport->iscsit_get_login_rx(conn, login); 1308 if (rc == 1) 1309 return 1; 1310 else if (rc < 0) 1311 goto new_sess_out; 1312 1313 buffer = &login->req[0]; 1314 pdu = (struct iscsi_login_req *)buffer; 1315 /* 1316 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs 1317 * when Status-Class != 0. 1318 */ 1319 conn->login_itt = pdu->itt; 1320 1321 spin_lock_bh(&np->np_thread_lock); 1322 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { 1323 spin_unlock_bh(&np->np_thread_lock); 1324 pr_err("iSCSI Network Portal on %pISpc currently not" 1325 " active.\n", &np->np_sockaddr); 1326 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1327 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1328 goto new_sess_out; 1329 } 1330 spin_unlock_bh(&np->np_thread_lock); 1331 1332 conn->network_transport = np->np_network_transport; 1333 1334 pr_debug("Received iSCSI login request from %pISpc on %s Network" 1335 " Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name, 1336 &conn->local_sockaddr); 1337 1338 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n"); 1339 conn->conn_state = TARG_CONN_STATE_IN_LOGIN; 1340 1341 if (iscsi_login_check_initiator_version(conn, pdu->max_version, 1342 pdu->min_version) < 0) 1343 goto new_sess_out; 1344 1345 zero_tsih = (pdu->tsih == 0x0000); 1346 if (zero_tsih) { 1347 /* 1348 * This is the leading connection of a new session. 1349 * We wait until after authentication to check for 1350 * session reinstatement. 1351 */ 1352 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0) 1353 goto new_sess_out; 1354 } else { 1355 /* 1356 * Add a new connection to an existing session. 1357 * We check for a non-existant session in 1358 * iscsi_login_non_zero_tsih_s2() below based 1359 * on ISID/TSIH, but wait until after authentication 1360 * to check for connection reinstatement, etc. 1361 */ 1362 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0) 1363 goto new_sess_out; 1364 } 1365 /* 1366 * SessionType: Discovery 1367 * 1368 * Locates Default Portal 1369 * 1370 * SessionType: Normal 1371 * 1372 * Locates Target Portal from NP -> Target IQN 1373 */ 1374 rc = iscsi_target_locate_portal(np, conn, login); 1375 if (rc < 0) { 1376 tpg = conn->tpg; 1377 goto new_sess_out; 1378 } 1379 login->zero_tsih = zero_tsih; 1380 1381 if (conn->sess) 1382 conn->sess->se_sess->sup_prot_ops = 1383 conn->conn_transport->iscsit_get_sup_prot_ops(conn); 1384 1385 tpg = conn->tpg; 1386 if (!tpg) { 1387 pr_err("Unable to locate struct iscsit_conn->tpg\n"); 1388 goto new_sess_out; 1389 } 1390 1391 if (zero_tsih) { 1392 if (iscsi_login_zero_tsih_s2(conn) < 0) 1393 goto new_sess_out; 1394 } else { 1395 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0) 1396 goto old_sess_out; 1397 } 1398 1399 if (conn->conn_transport->iscsit_validate_params) { 1400 ret = conn->conn_transport->iscsit_validate_params(conn); 1401 if (ret < 0) { 1402 if (zero_tsih) 1403 goto new_sess_out; 1404 else 1405 goto old_sess_out; 1406 } 1407 } 1408 1409 ret = iscsi_target_start_negotiation(login, conn); 1410 if (ret < 0) 1411 goto new_sess_out; 1412 1413 iscsi_stop_login_thread_timer(np); 1414 1415 if (ret == 1) { 1416 tpg_np = conn->tpg_np; 1417 1418 iscsi_post_login_handler(np, conn, zero_tsih); 1419 iscsit_deaccess_np(np, tpg, tpg_np); 1420 } 1421 1422 tpg = NULL; 1423 tpg_np = NULL; 1424 /* Get another socket */ 1425 return 1; 1426 1427 new_sess_out: 1428 new_sess = true; 1429 old_sess_out: 1430 iscsi_stop_login_thread_timer(np); 1431 tpg_np = conn->tpg_np; 1432 iscsi_target_login_sess_out(conn, zero_tsih, new_sess); 1433 new_sess = false; 1434 1435 if (tpg) { 1436 iscsit_deaccess_np(np, tpg, tpg_np); 1437 tpg = NULL; 1438 tpg_np = NULL; 1439 } 1440 1441 return 1; 1442 1443 exit: 1444 iscsi_stop_login_thread_timer(np); 1445 spin_lock_bh(&np->np_thread_lock); 1446 np->np_thread_state = ISCSI_NP_THREAD_EXIT; 1447 spin_unlock_bh(&np->np_thread_lock); 1448 1449 return 0; 1450 } 1451 1452 int iscsi_target_login_thread(void *arg) 1453 { 1454 struct iscsi_np *np = arg; 1455 int ret; 1456 1457 allow_signal(SIGINT); 1458 1459 while (1) { 1460 ret = __iscsi_target_login_thread(np); 1461 /* 1462 * We break and exit here unless another sock_accept() call 1463 * is expected. 1464 */ 1465 if (ret != 1) 1466 break; 1467 } 1468 1469 while (!kthread_should_stop()) { 1470 msleep(100); 1471 } 1472 1473 return 0; 1474 } 1475