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