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