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