1 /*******************************************************************************
2 * This file contains the login functions used by the iSCSI Target driver.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ******************************************************************************/
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>
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"
39 #include <target/iscsi/iscsi_transport.h>
41 static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
43 struct iscsi_login *login;
45 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
47 pr_err("Unable to allocate memory for struct iscsi_login.\n");
52 login->first_request = 1;
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");
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");
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");
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);
93 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
94 pr_err("Unable to allocate conn->conn_cpumask\n");
97 conn->conn_login = login;
102 kfree(conn->conn_ops);
104 kfree(login->rsp_buf);
106 kfree(login->req_buf);
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
116 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
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
123 conn->conn_rx_hash.flags = 0;
124 conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
126 if (IS_ERR(conn->conn_rx_hash.tfm)) {
127 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
131 conn->conn_tx_hash.flags = 0;
132 conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
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);
143 static int iscsi_login_check_initiator_version(
144 struct iscsi_conn *conn,
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);
160 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
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;
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);
179 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
181 spin_lock_bh(&se_tpg->session_lock);
182 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
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);
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);
204 spin_unlock(&sess_p->conn_lock);
206 spin_unlock_bh(&se_tpg->session_lock);
208 * If the Time2Retain handler has expired, the session is already gone.
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);
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);
225 spin_unlock_bh(&sess->conn_lock);
227 iscsit_stop_session(sess, 1, 1);
228 iscsit_dec_session_usage_count(sess);
230 target_put_session(sess->se_sess);
234 static void iscsi_login_set_conn_values(
235 struct iscsi_session *sess,
236 struct iscsi_conn *conn,
240 conn->cid = be16_to_cpu(cid);
242 * Generate a random Status sequence number (statsn) for the new
245 get_random_bytes(&conn->stat_sn, sizeof(u32));
247 mutex_lock(&auth_id_lock);
248 conn->auth_id = iscsit_global->auth_id++;
249 mutex_unlock(&auth_id_lock);
252 static __printf(2, 3) int iscsi_change_param_sprintf(
253 struct iscsi_conn *conn,
254 const char *fmt, ...)
257 unsigned char buf[64];
259 memset(buf, 0, sizeof buf);
262 vsnprintf(buf, sizeof buf, fmt, args);
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);
275 * This is the leading connection of a new session,
276 * or session reinstatement.
278 static int iscsi_login_zero_tsih_s1(
279 struct iscsi_conn *conn,
282 struct iscsi_session *sess = NULL;
283 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
286 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
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");
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);
313 idr_preload(GFP_KERNEL);
314 spin_lock_bh(&sess_idr_lock);
315 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
317 sess->session_index = ret;
318 spin_unlock_bh(&sess_idr_lock);
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);
329 sess->creation_time = get_jiffies_64();
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.
334 sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn);
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");
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);
358 static int iscsi_login_zero_tsih_s2(
359 struct iscsi_conn *conn)
361 struct iscsi_node_attrib *na;
362 struct iscsi_session *sess = conn->sess;
365 sess->tpg = conn->tpg;
368 * Assign a new TPG Session Handle. Note this is protected with
369 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
371 sess->tsih = ++sess->tpg->ntsih;
373 sess->tsih = ++sess->tpg->ntsih;
376 * Create the default params from user defined values..
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);
385 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
388 iscsi_set_keys_to_negotiate(conn->param_list, iser);
390 if (sess->sess_ops->SessionType)
391 return iscsi_set_keys_irrelevant_for_discovery(
394 na = iscsit_tpg_get_node_attrib(sess);
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
401 * In our case, we have already located the struct iscsi_tiqn at this point.
403 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
407 * Workaround for Initiators that have broken connection recovery logic.
409 * "We would really like to get rid of this." Linux-iSCSI.org team
411 if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
415 * Set RDMAExtensions=Yes by default for iSER enabled network portals
418 struct iscsi_param *param;
419 unsigned long mrdsl, off;
422 if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
426 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
427 * Immediate Data + Unsolicitied Data-OUT if necessary..
429 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
432 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
433 ISCSI_LOGIN_STATUS_NO_RESOURCES);
436 rc = kstrtoul(param->value, 0, &mrdsl);
438 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
439 ISCSI_LOGIN_STATUS_NO_RESOURCES);
442 off = mrdsl % PAGE_SIZE;
446 if (mrdsl < PAGE_SIZE)
451 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
452 " to PAGE_SIZE\n", mrdsl);
454 if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
457 * ISER currently requires that ImmediateData + Unsolicited
458 * Data be disabled when protection / signature MRs are enabled.
461 if (sess->se_sess->sup_prot_ops &
462 (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
463 TARGET_PROT_DOUT_INSERT)) {
465 if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
468 if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
471 pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
472 " T10-PI enabled ISER session\n");
479 static int iscsi_login_non_zero_tsih_s1(
480 struct iscsi_conn *conn,
483 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
485 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
490 * Add a new connection to an existing session.
492 static int iscsi_login_non_zero_tsih_s2(
493 struct iscsi_conn *conn,
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;
503 spin_lock_bh(&se_tpg->session_lock);
504 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
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))
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);
520 spin_unlock_bh(&se_tpg->session_lock);
523 * If the Time2Retain handler has expired, the session is already gone.
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);
534 * Stop the Time2Retain timer if this is a failed session, we restart
535 * the timer if the login is not successful.
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);
542 iscsi_login_set_conn_values(sess, conn, pdu->cid);
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);
551 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
554 iscsi_set_keys_to_negotiate(conn->param_list, iser);
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
560 * In our case, we have already located the struct iscsi_tiqn at this point.
562 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
568 int iscsi_login_post_auth_non_zero_tsih(
569 struct iscsi_conn *conn,
573 struct iscsi_conn *conn_ptr = NULL;
574 struct iscsi_conn_recovery *cr = NULL;
575 struct iscsi_session *sess = conn->sess;
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.
584 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
586 pr_err("Connection exists with CID %hu for %s,"
587 " performing connection reinstatement.\n",
588 conn_ptr->cid, sess->sess_ops->InitiatorName);
590 iscsit_connection_reinstatement_rcfr(conn_ptr);
591 iscsit_dec_conn_usage_count(conn_ptr);
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.
599 * Also note that an explict logout may have already been sent,
600 * but the response may not be sent due to additional connection
603 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
604 cr = iscsit_get_inactive_connection_recovery_entry(
607 pr_debug("Performing implicit logout"
608 " for connection recovery on CID: %hu\n",
610 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
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
620 pr_debug("Adding CID %hu to existing session for %s.\n",
621 cid, sess->sess_ops->InitiatorName);
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);
635 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
637 struct iscsi_session *sess = conn->sess;
639 * FIXME: Unsolicitied NopIN support for ISER
641 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
644 if (!sess->sess_ops->SessionType)
645 iscsit_start_nopin_timer(conn);
648 int iscsit_start_kthreads(struct iscsi_conn *conn)
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);
657 if (conn->bitmap_id < 0) {
658 pr_err("bitmap_find_free_region() failed for"
659 " iscsit_start_kthreads()\n");
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);
670 conn->tx_thread_active = true;
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);
679 conn->rx_thread_active = true;
683 send_sig(SIGINT, conn->tx_thread, 1);
684 kthread_stop(conn->tx_thread);
685 conn->tx_thread_active = false;
687 spin_lock(&iscsit_global->ts_bitmap_lock);
688 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
690 spin_unlock(&iscsit_global->ts_bitmap_lock);
694 void iscsi_post_login_handler(
696 struct iscsi_conn *conn,
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;
705 iscsit_inc_conn_usage_count(conn);
707 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
708 ISCSI_LOGIN_STATUS_ACCEPT);
710 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
711 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
713 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
715 * SCSI Initiator -> SCSI Target Port Mapping
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;
723 spin_lock_bh(&sess->conn_lock);
724 atomic_set(&sess->session_continuation, 0);
725 if (sess->session_state == TARG_SESS_STATE_FAILED) {
727 " TARG_SESS_STATE_LOGGED_IN.\n");
728 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
732 pr_debug("iSCSI Login successful on CID: %hu from %s to"
733 " %s:%hu,%hu\n", conn->cid, conn->login_ip,
734 conn->local_ip, conn->local_port, tpg->tpgt);
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);
743 iscsi_post_login_start_timers(conn);
745 * Determine CPU mask to ensure connection's RX and TX kthreads
746 * are scheduled on the same CPU.
748 iscsit_thread_get_cpumask(conn);
749 conn->conn_rx_reset_cpumask = 1;
750 conn->conn_tx_reset_cpumask = 1;
752 * Wakeup the sleeping iscsi_target_rx_thread() now that
753 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
755 complete(&conn->rx_login_comp);
756 iscsit_dec_conn_usage_count(conn);
759 spin_lock_bh(&se_tpg->session_lock);
760 iscsit_stop_time2retain_timer(sess);
761 spin_unlock_bh(&se_tpg->session_lock);
763 iscsit_dec_session_usage_count(sess);
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;
771 iscsit_determine_maxcmdsn(sess);
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;
779 pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
780 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
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);
791 sess->sid = tpg->sid++;
793 sess->sid = tpg->sid++;
794 pr_debug("Established iSCSI session from node: %s\n",
795 sess->sess_ops->InitiatorName);
799 tpg->tpg_tiqn->tiqn_nsessions++;
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);
805 iscsi_post_login_start_timers(conn);
807 * Determine CPU mask to ensure connection's RX and TX kthreads
808 * are scheduled on the same CPU.
810 iscsit_thread_get_cpumask(conn);
811 conn->conn_rx_reset_cpumask = 1;
812 conn->conn_tx_reset_cpumask = 1;
814 * Wakeup the sleeping iscsi_target_rx_thread() now that
815 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
817 complete(&conn->rx_login_comp);
818 iscsit_dec_conn_usage_count(conn);
821 static void iscsi_handle_login_thread_timeout(unsigned long data)
823 struct iscsi_np *np = (struct iscsi_np *) data;
825 spin_lock_bh(&np->np_thread_lock);
826 pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
827 np->np_ip, np->np_port);
829 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
830 spin_unlock_bh(&np->np_thread_lock);
835 send_sig(SIGINT, np->np_thread, 1);
837 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
838 spin_unlock_bh(&np->np_thread_lock);
841 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
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
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);
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);
861 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
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);
868 np->np_login_timer_flags |= ISCSI_TF_STOP;
869 spin_unlock_bh(&np->np_thread_lock);
871 del_timer_sync(&np->np_login_timer);
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);
880 struct __kernel_sockaddr_storage *sockaddr)
882 struct socket *sock = NULL;
883 int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
885 switch (np->np_network_transport) {
887 np->np_ip_proto = IPPROTO_TCP;
888 np->np_sock_type = SOCK_STREAM;
891 np->np_ip_proto = IPPROTO_SCTP;
892 np->np_sock_type = SOCK_STREAM;
895 np->np_ip_proto = IPPROTO_SCTP;
896 np->np_sock_type = SOCK_SEQPACKET;
899 pr_err("Unsupported network_transport: %d\n",
900 np->np_network_transport);
904 np->np_ip_proto = IPPROTO_TCP;
905 np->np_sock_type = SOCK_STREAM;
907 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
908 np->np_ip_proto, &sock);
910 pr_err("sock_create() failed.\n");
913 np->np_socket = sock;
915 * Setup the np->np_sockaddr from the passed sockaddr setup
916 * in iscsi_target_configfs.c code..
918 memcpy(&np->np_sockaddr, sockaddr,
919 sizeof(struct __kernel_sockaddr_storage));
921 if (sockaddr->ss_family == AF_INET6)
922 len = sizeof(struct sockaddr_in6);
924 len = sizeof(struct sockaddr_in);
926 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
928 /* FIXME: Someone please explain why this is endian-safe */
930 if (np->np_network_transport == ISCSI_TCP) {
931 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
932 (char *)&opt, sizeof(opt));
934 pr_err("kernel_setsockopt() for TCP_NODELAY"
935 " failed: %d\n", ret);
940 /* FIXME: Someone please explain why this is endian-safe */
941 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
942 (char *)&opt, sizeof(opt));
944 pr_err("kernel_setsockopt() for SO_REUSEADDR"
949 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
950 (char *)&opt, sizeof(opt));
952 pr_err("kernel_setsockopt() for IP_FREEBIND"
957 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
959 pr_err("kernel_bind() failed: %d\n", ret);
963 ret = kernel_listen(sock, backlog);
965 pr_err("kernel_listen() failed: %d\n", ret);
971 np->np_socket = NULL;
976 int iscsi_target_setup_login_socket(
978 struct __kernel_sockaddr_storage *sockaddr)
980 struct iscsit_transport *t;
983 t = iscsit_get_transport(np->np_network_transport);
987 rc = t->iscsit_setup_np(np, sockaddr);
989 iscsit_put_transport(t);
993 np->np_transport = t;
998 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1000 struct socket *new_sock, *sock = np->np_socket;
1001 struct sockaddr_in sock_in;
1002 struct sockaddr_in6 sock_in6;
1005 rc = kernel_accept(sock, &new_sock, 0);
1009 conn->sock = new_sock;
1010 conn->login_family = np->np_sockaddr.ss_family;
1012 if (np->np_sockaddr.ss_family == AF_INET6) {
1013 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1015 rc = conn->sock->ops->getname(conn->sock,
1016 (struct sockaddr *)&sock_in6, &err, 1);
1018 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1019 snprintf(conn->login_ip, sizeof(conn->login_ip), "[%pI6c]",
1020 &sock_in6.sin6_addr.in6_u);
1022 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI4",
1023 &sock_in6.sin6_addr.s6_addr32[3]);
1024 conn->login_port = ntohs(sock_in6.sin6_port);
1027 rc = conn->sock->ops->getname(conn->sock,
1028 (struct sockaddr *)&sock_in6, &err, 0);
1030 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1031 snprintf(conn->local_ip, sizeof(conn->local_ip), "[%pI6c]",
1032 &sock_in6.sin6_addr.in6_u);
1034 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI4",
1035 &sock_in6.sin6_addr.s6_addr32[3]);
1036 conn->local_port = ntohs(sock_in6.sin6_port);
1039 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1041 rc = conn->sock->ops->getname(conn->sock,
1042 (struct sockaddr *)&sock_in, &err, 1);
1044 sprintf(conn->login_ip, "%pI4",
1045 &sock_in.sin_addr.s_addr);
1046 conn->login_port = ntohs(sock_in.sin_port);
1049 rc = conn->sock->ops->getname(conn->sock,
1050 (struct sockaddr *)&sock_in, &err, 0);
1052 sprintf(conn->local_ip, "%pI4",
1053 &sock_in.sin_addr.s_addr);
1054 conn->local_port = ntohs(sock_in.sin_port);
1061 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1063 struct iscsi_login_req *login_req;
1064 u32 padding = 0, payload_length;
1066 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1069 login_req = (struct iscsi_login_req *)login->req;
1070 payload_length = ntoh24(login_req->dlength);
1071 padding = ((-payload_length) & 3);
1073 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1074 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1075 login_req->flags, login_req->itt, login_req->cmdsn,
1076 login_req->exp_statsn, login_req->cid, payload_length);
1078 * Setup the initial iscsi_login values from the leading
1079 * login request PDU.
1081 if (login->first_request) {
1082 login_req = (struct iscsi_login_req *)login->req;
1083 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1084 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1085 login->version_min = login_req->min_version;
1086 login->version_max = login_req->max_version;
1087 memcpy(login->isid, login_req->isid, 6);
1088 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1089 login->init_task_tag = login_req->itt;
1090 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1091 login->cid = be16_to_cpu(login_req->cid);
1092 login->tsih = be16_to_cpu(login_req->tsih);
1095 if (iscsi_target_check_login_request(conn, login) < 0)
1098 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1099 if (iscsi_login_rx_data(conn, login->req_buf,
1100 payload_length + padding) < 0)
1106 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1109 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1116 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1121 conn->conn_transport = t;
1125 rc = try_module_get(t->owner);
1127 pr_err("try_module_get() failed for %s\n", t->name);
1131 conn->conn_transport = t;
1135 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1136 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1141 pr_err("iSCSI Login negotiation failed.\n");
1142 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1143 ISCSI_LOGIN_STATUS_INIT_ERR);
1144 if (!zero_tsih || !conn->sess)
1146 if (conn->sess->se_sess)
1147 transport_free_session(conn->sess->se_sess);
1148 if (conn->sess->session_index != 0) {
1149 spin_lock_bh(&sess_idr_lock);
1150 idr_remove(&sess_idr, conn->sess->session_index);
1151 spin_unlock_bh(&sess_idr_lock);
1153 kfree(conn->sess->sess_ops);
1158 iscsi_stop_login_thread_timer(np);
1160 * If login negotiation fails check if the Time2Retain timer
1161 * needs to be restarted.
1163 if (!zero_tsih && conn->sess) {
1164 spin_lock_bh(&conn->sess->conn_lock);
1165 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1166 struct se_portal_group *se_tpg =
1167 &conn->tpg->tpg_se_tpg;
1169 atomic_set(&conn->sess->session_continuation, 0);
1170 spin_unlock_bh(&conn->sess->conn_lock);
1171 spin_lock_bh(&se_tpg->session_lock);
1172 iscsit_start_time2retain_handler(conn->sess);
1173 spin_unlock_bh(&se_tpg->session_lock);
1175 spin_unlock_bh(&conn->sess->conn_lock);
1176 iscsit_dec_session_usage_count(conn->sess);
1179 if (!IS_ERR(conn->conn_rx_hash.tfm))
1180 crypto_free_hash(conn->conn_rx_hash.tfm);
1181 if (!IS_ERR(conn->conn_tx_hash.tfm))
1182 crypto_free_hash(conn->conn_tx_hash.tfm);
1184 free_cpumask_var(conn->conn_cpumask);
1186 kfree(conn->conn_ops);
1188 if (conn->param_list) {
1189 iscsi_release_param_list(conn->param_list);
1190 conn->param_list = NULL;
1192 iscsi_target_nego_release(conn);
1195 sock_release(conn->sock);
1199 if (conn->conn_transport->iscsit_wait_conn)
1200 conn->conn_transport->iscsit_wait_conn(conn);
1202 if (conn->conn_transport->iscsit_free_conn)
1203 conn->conn_transport->iscsit_free_conn(conn);
1205 iscsit_put_transport(conn->conn_transport);
1209 static int __iscsi_target_login_thread(struct iscsi_np *np)
1211 u8 *buffer, zero_tsih = 0;
1213 struct iscsi_conn *conn = NULL;
1214 struct iscsi_login *login;
1215 struct iscsi_portal_group *tpg = NULL;
1216 struct iscsi_login_req *pdu;
1217 struct iscsi_tpg_np *tpg_np;
1218 bool new_sess = false;
1220 flush_signals(current);
1222 spin_lock_bh(&np->np_thread_lock);
1223 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1224 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1225 complete(&np->np_restart_comp);
1226 } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1227 spin_unlock_bh(&np->np_thread_lock);
1230 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1232 spin_unlock_bh(&np->np_thread_lock);
1234 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1236 pr_err("Could not allocate memory for"
1237 " new connection\n");
1238 /* Get another socket */
1241 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1242 conn->conn_state = TARG_CONN_STATE_FREE;
1244 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1249 rc = np->np_transport->iscsit_accept_np(np, conn);
1250 if (rc == -ENOSYS) {
1251 complete(&np->np_restart_comp);
1252 iscsit_put_transport(conn->conn_transport);
1256 } else if (rc < 0) {
1257 spin_lock_bh(&np->np_thread_lock);
1258 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1259 spin_unlock_bh(&np->np_thread_lock);
1260 complete(&np->np_restart_comp);
1261 iscsit_put_transport(conn->conn_transport);
1264 /* Get another socket */
1267 spin_unlock_bh(&np->np_thread_lock);
1268 iscsit_put_transport(conn->conn_transport);
1274 * Perform the remaining iSCSI connection initialization items..
1276 login = iscsi_login_init_conn(conn);
1281 iscsi_start_login_thread_timer(np);
1283 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1284 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1286 * This will process the first login request + payload..
1288 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1294 buffer = &login->req[0];
1295 pdu = (struct iscsi_login_req *)buffer;
1297 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1298 * when Status-Class != 0.
1300 conn->login_itt = pdu->itt;
1302 spin_lock_bh(&np->np_thread_lock);
1303 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1304 spin_unlock_bh(&np->np_thread_lock);
1305 pr_err("iSCSI Network Portal on %s:%hu currently not"
1306 " active.\n", np->np_ip, np->np_port);
1307 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1308 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1311 spin_unlock_bh(&np->np_thread_lock);
1313 conn->network_transport = np->np_network_transport;
1315 pr_debug("Received iSCSI login request from %s on %s Network"
1316 " Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
1317 conn->local_ip, conn->local_port);
1319 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1320 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1322 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1323 pdu->min_version) < 0)
1326 zero_tsih = (pdu->tsih == 0x0000);
1329 * This is the leading connection of a new session.
1330 * We wait until after authentication to check for
1331 * session reinstatement.
1333 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1337 * Add a new connection to an existing session.
1338 * We check for a non-existant session in
1339 * iscsi_login_non_zero_tsih_s2() below based
1340 * on ISID/TSIH, but wait until after authentication
1341 * to check for connection reinstatement, etc.
1343 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1347 * SessionType: Discovery
1349 * Locates Default Portal
1351 * SessionType: Normal
1353 * Locates Target Portal from NP -> Target IQN
1355 rc = iscsi_target_locate_portal(np, conn, login);
1360 login->zero_tsih = zero_tsih;
1362 conn->sess->se_sess->sup_prot_ops =
1363 conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1367 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1372 if (iscsi_login_zero_tsih_s2(conn) < 0)
1375 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1379 ret = iscsi_target_start_negotiation(login, conn);
1383 iscsi_stop_login_thread_timer(np);
1386 tpg_np = conn->tpg_np;
1388 iscsi_post_login_handler(np, conn, zero_tsih);
1389 iscsit_deaccess_np(np, tpg, tpg_np);
1394 /* Get another socket */
1400 tpg_np = conn->tpg_np;
1401 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1405 iscsit_deaccess_np(np, tpg, tpg_np);
1414 iscsi_stop_login_thread_timer(np);
1415 spin_lock_bh(&np->np_thread_lock);
1416 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1417 spin_unlock_bh(&np->np_thread_lock);
1422 int iscsi_target_login_thread(void *arg)
1424 struct iscsi_np *np = arg;
1427 allow_signal(SIGINT);
1430 ret = __iscsi_target_login_thread(np);
1432 * We break and exit here unless another sock_accept() call