1 /*******************************************************************************
2 * This file contains the login functions used by the iSCSI Target driver.
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
21 #include <linux/string.h>
22 #include <linux/kthread.h>
23 #include <linux/crypto.h>
24 #include <linux/idr.h>
25 #include <scsi/iscsi_proto.h>
26 #include <target/target_core_base.h>
27 #include <target/target_core_fabric.h>
29 #include "iscsi_target_core.h"
30 #include "iscsi_target_tq.h"
31 #include "iscsi_target_device.h"
32 #include "iscsi_target_nego.h"
33 #include "iscsi_target_erl0.h"
34 #include "iscsi_target_erl2.h"
35 #include "iscsi_target_login.h"
36 #include "iscsi_target_stat.h"
37 #include "iscsi_target_tpg.h"
38 #include "iscsi_target_util.h"
39 #include "iscsi_target.h"
40 #include "iscsi_target_parameters.h"
42 static int iscsi_login_init_conn(struct iscsi_conn *conn)
44 init_waitqueue_head(&conn->queues_wq);
45 INIT_LIST_HEAD(&conn->conn_list);
46 INIT_LIST_HEAD(&conn->conn_cmd_list);
47 INIT_LIST_HEAD(&conn->immed_queue_list);
48 INIT_LIST_HEAD(&conn->response_queue_list);
49 init_completion(&conn->conn_post_wait_comp);
50 init_completion(&conn->conn_wait_comp);
51 init_completion(&conn->conn_wait_rcfr_comp);
52 init_completion(&conn->conn_waiting_on_uc_comp);
53 init_completion(&conn->conn_logout_comp);
54 init_completion(&conn->rx_half_close_comp);
55 init_completion(&conn->tx_half_close_comp);
56 spin_lock_init(&conn->cmd_lock);
57 spin_lock_init(&conn->conn_usage_lock);
58 spin_lock_init(&conn->immed_queue_lock);
59 spin_lock_init(&conn->nopin_timer_lock);
60 spin_lock_init(&conn->response_queue_lock);
61 spin_lock_init(&conn->state_lock);
63 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
64 pr_err("Unable to allocate conn->conn_cpumask\n");
72 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
73 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
75 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
78 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
79 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
80 * to software 1x8 byte slicing from crc32c.ko
82 conn->conn_rx_hash.flags = 0;
83 conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
85 if (IS_ERR(conn->conn_rx_hash.tfm)) {
86 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
90 conn->conn_tx_hash.flags = 0;
91 conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
93 if (IS_ERR(conn->conn_tx_hash.tfm)) {
94 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
95 crypto_free_hash(conn->conn_rx_hash.tfm);
102 static int iscsi_login_check_initiator_version(
103 struct iscsi_conn *conn,
107 if ((version_max != 0x00) || (version_min != 0x00)) {
108 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
109 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
110 version_min, version_max);
111 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
112 ISCSI_LOGIN_STATUS_NO_VERSION);
119 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
122 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
123 struct iscsi_portal_group *tpg = conn->tpg;
124 struct iscsi_session *sess = NULL, *sess_p = NULL;
125 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
126 struct se_session *se_sess, *se_sess_tmp;
128 initiatorname_param = iscsi_find_param_from_key(
129 INITIATORNAME, conn->param_list);
130 sessiontype_param = iscsi_find_param_from_key(
131 SESSIONTYPE, conn->param_list);
132 if (!initiatorname_param || !sessiontype_param) {
133 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
134 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
138 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
140 spin_lock_bh(&se_tpg->session_lock);
141 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
144 sess_p = se_sess->fabric_sess_ptr;
145 spin_lock(&sess_p->conn_lock);
146 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
147 atomic_read(&sess_p->session_logout) ||
148 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
149 spin_unlock(&sess_p->conn_lock);
152 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
153 (!strcmp(sess_p->sess_ops->InitiatorName,
154 initiatorname_param->value) &&
155 (sess_p->sess_ops->SessionType == sessiontype))) {
156 atomic_set(&sess_p->session_reinstatement, 1);
157 spin_unlock(&sess_p->conn_lock);
158 iscsit_inc_session_usage_count(sess_p);
159 iscsit_stop_time2retain_timer(sess_p);
163 spin_unlock(&sess_p->conn_lock);
165 spin_unlock_bh(&se_tpg->session_lock);
167 * If the Time2Retain handler has expired, the session is already gone.
172 pr_debug("%s iSCSI Session SID %u is still active for %s,"
173 " preforming session reinstatement.\n", (sessiontype) ?
174 "Discovery" : "Normal", sess->sid,
175 sess->sess_ops->InitiatorName);
177 spin_lock_bh(&sess->conn_lock);
178 if (sess->session_state == TARG_SESS_STATE_FAILED) {
179 spin_unlock_bh(&sess->conn_lock);
180 iscsit_dec_session_usage_count(sess);
181 target_put_session(sess->se_sess);
184 spin_unlock_bh(&sess->conn_lock);
186 iscsit_stop_session(sess, 1, 1);
187 iscsit_dec_session_usage_count(sess);
189 target_put_session(sess->se_sess);
193 static void iscsi_login_set_conn_values(
194 struct iscsi_session *sess,
195 struct iscsi_conn *conn,
199 conn->cid = be16_to_cpu(cid);
201 * Generate a random Status sequence number (statsn) for the new
204 get_random_bytes(&conn->stat_sn, sizeof(u32));
206 mutex_lock(&auth_id_lock);
207 conn->auth_id = iscsit_global->auth_id++;
208 mutex_unlock(&auth_id_lock);
212 * This is the leading connection of a new session,
213 * or session reinstatement.
215 static int iscsi_login_zero_tsih_s1(
216 struct iscsi_conn *conn,
219 struct iscsi_session *sess = NULL;
220 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
223 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
225 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
226 ISCSI_LOGIN_STATUS_NO_RESOURCES);
227 pr_err("Could not allocate memory for session\n");
231 iscsi_login_set_conn_values(sess, conn, pdu->cid);
232 sess->init_task_tag = pdu->itt;
233 memcpy(&sess->isid, pdu->isid, 6);
234 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
235 INIT_LIST_HEAD(&sess->sess_conn_list);
236 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
237 INIT_LIST_HEAD(&sess->cr_active_list);
238 INIT_LIST_HEAD(&sess->cr_inactive_list);
239 init_completion(&sess->async_msg_comp);
240 init_completion(&sess->reinstatement_comp);
241 init_completion(&sess->session_wait_comp);
242 init_completion(&sess->session_waiting_on_uc_comp);
243 mutex_init(&sess->cmdsn_mutex);
244 spin_lock_init(&sess->conn_lock);
245 spin_lock_init(&sess->cr_a_lock);
246 spin_lock_init(&sess->cr_i_lock);
247 spin_lock_init(&sess->session_usage_lock);
248 spin_lock_init(&sess->ttt_lock);
250 idr_preload(GFP_KERNEL);
251 spin_lock_bh(&sess_idr_lock);
252 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
254 sess->session_index = ret;
255 spin_unlock_bh(&sess_idr_lock);
259 pr_err("idr_alloc() for sess_idr failed\n");
260 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
261 ISCSI_LOGIN_STATUS_NO_RESOURCES);
266 sess->creation_time = get_jiffies_64();
267 spin_lock_init(&sess->session_stats_lock);
269 * The FFP CmdSN window values will be allocated from the TPG's
270 * Initiator Node's ACL once the login has been successfully completed.
272 sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn);
274 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
275 if (!sess->sess_ops) {
276 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
277 ISCSI_LOGIN_STATUS_NO_RESOURCES);
278 pr_err("Unable to allocate memory for"
279 " struct iscsi_sess_ops.\n");
284 sess->se_sess = transport_init_session();
285 if (IS_ERR(sess->se_sess)) {
286 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
287 ISCSI_LOGIN_STATUS_NO_RESOURCES);
295 static int iscsi_login_zero_tsih_s2(
296 struct iscsi_conn *conn)
298 struct iscsi_node_attrib *na;
299 struct iscsi_session *sess = conn->sess;
300 unsigned char buf[32];
302 sess->tpg = conn->tpg;
305 * Assign a new TPG Session Handle. Note this is protected with
306 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
308 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
310 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
313 * Create the default params from user defined values..
315 if (iscsi_copy_param_list(&conn->param_list,
316 ISCSI_TPG_C(conn)->param_list, 1) < 0) {
317 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
318 ISCSI_LOGIN_STATUS_NO_RESOURCES);
322 iscsi_set_keys_to_negotiate(0, conn->param_list);
324 if (sess->sess_ops->SessionType)
325 return iscsi_set_keys_irrelevant_for_discovery(
328 na = iscsit_tpg_get_node_attrib(sess);
331 * Need to send TargetPortalGroupTag back in first login response
332 * on any iSCSI connection where the Initiator provides TargetName.
333 * See 5.3.1. Login Phase Start
335 * In our case, we have already located the struct iscsi_tiqn at this point.
338 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
339 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
340 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
341 ISCSI_LOGIN_STATUS_NO_RESOURCES);
346 * Workaround for Initiators that have broken connection recovery logic.
348 * "We would really like to get rid of this." Linux-iSCSI.org team
351 sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
352 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
353 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
354 ISCSI_LOGIN_STATUS_NO_RESOURCES);
358 if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
365 * Remove PSTATE_NEGOTIATE for the four FIM related keys.
366 * The Initiator node will be able to enable FIM by proposing them itself.
368 int iscsi_login_disable_FIM_keys(
369 struct iscsi_param_list *param_list,
370 struct iscsi_conn *conn)
372 struct iscsi_param *param;
374 param = iscsi_find_param_from_key("OFMarker", param_list);
376 pr_err("iscsi_find_param_from_key() for"
377 " OFMarker failed\n");
378 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
379 ISCSI_LOGIN_STATUS_NO_RESOURCES);
382 param->state &= ~PSTATE_NEGOTIATE;
384 param = iscsi_find_param_from_key("OFMarkInt", param_list);
386 pr_err("iscsi_find_param_from_key() for"
387 " IFMarker failed\n");
388 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
389 ISCSI_LOGIN_STATUS_NO_RESOURCES);
392 param->state &= ~PSTATE_NEGOTIATE;
394 param = iscsi_find_param_from_key("IFMarker", param_list);
396 pr_err("iscsi_find_param_from_key() for"
397 " IFMarker failed\n");
398 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
399 ISCSI_LOGIN_STATUS_NO_RESOURCES);
402 param->state &= ~PSTATE_NEGOTIATE;
404 param = iscsi_find_param_from_key("IFMarkInt", param_list);
406 pr_err("iscsi_find_param_from_key() for"
407 " IFMarker failed\n");
408 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
409 ISCSI_LOGIN_STATUS_NO_RESOURCES);
412 param->state &= ~PSTATE_NEGOTIATE;
417 static int iscsi_login_non_zero_tsih_s1(
418 struct iscsi_conn *conn,
421 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
423 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
428 * Add a new connection to an existing session.
430 static int iscsi_login_non_zero_tsih_s2(
431 struct iscsi_conn *conn,
434 struct iscsi_portal_group *tpg = conn->tpg;
435 struct iscsi_session *sess = NULL, *sess_p = NULL;
436 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
437 struct se_session *se_sess, *se_sess_tmp;
438 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
440 spin_lock_bh(&se_tpg->session_lock);
441 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
444 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
445 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
446 atomic_read(&sess_p->session_logout) ||
447 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
449 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
450 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
451 iscsit_inc_session_usage_count(sess_p);
452 iscsit_stop_time2retain_timer(sess_p);
457 spin_unlock_bh(&se_tpg->session_lock);
460 * If the Time2Retain handler has expired, the session is already gone.
463 pr_err("Initiator attempting to add a connection to"
464 " a non-existent session, rejecting iSCSI Login.\n");
465 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
466 ISCSI_LOGIN_STATUS_NO_SESSION);
471 * Stop the Time2Retain timer if this is a failed session, we restart
472 * the timer if the login is not successful.
474 spin_lock_bh(&sess->conn_lock);
475 if (sess->session_state == TARG_SESS_STATE_FAILED)
476 atomic_set(&sess->session_continuation, 1);
477 spin_unlock_bh(&sess->conn_lock);
479 iscsi_login_set_conn_values(sess, conn, pdu->cid);
481 if (iscsi_copy_param_list(&conn->param_list,
482 ISCSI_TPG_C(conn)->param_list, 0) < 0) {
483 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
484 ISCSI_LOGIN_STATUS_NO_RESOURCES);
488 iscsi_set_keys_to_negotiate(0, conn->param_list);
490 * Need to send TargetPortalGroupTag back in first login response
491 * on any iSCSI connection where the Initiator provides TargetName.
492 * See 5.3.1. Login Phase Start
494 * In our case, we have already located the struct iscsi_tiqn at this point.
497 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
498 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
499 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
500 ISCSI_LOGIN_STATUS_NO_RESOURCES);
504 return iscsi_login_disable_FIM_keys(conn->param_list, conn);
507 int iscsi_login_post_auth_non_zero_tsih(
508 struct iscsi_conn *conn,
512 struct iscsi_conn *conn_ptr = NULL;
513 struct iscsi_conn_recovery *cr = NULL;
514 struct iscsi_session *sess = conn->sess;
517 * By following item 5 in the login table, if we have found
518 * an existing ISID and a valid/existing TSIH and an existing
519 * CID we do connection reinstatement. Currently we dont not
520 * support it so we send back an non-zero status class to the
521 * initiator and release the new connection.
523 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
525 pr_err("Connection exists with CID %hu for %s,"
526 " performing connection reinstatement.\n",
527 conn_ptr->cid, sess->sess_ops->InitiatorName);
529 iscsit_connection_reinstatement_rcfr(conn_ptr);
530 iscsit_dec_conn_usage_count(conn_ptr);
534 * Check for any connection recovery entires containing CID.
535 * We use the original ExpStatSN sent in the first login request
536 * to acknowledge commands for the failed connection.
538 * Also note that an explict logout may have already been sent,
539 * but the response may not be sent due to additional connection
542 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
543 cr = iscsit_get_inactive_connection_recovery_entry(
546 pr_debug("Performing implicit logout"
547 " for connection recovery on CID: %hu\n",
549 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
554 * Else we follow item 4 from the login table in that we have
555 * found an existing ISID and a valid/existing TSIH and a new
556 * CID we go ahead and continue to add a new connection to the
559 pr_debug("Adding CID %hu to existing session for %s.\n",
560 cid, sess->sess_ops->InitiatorName);
562 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
563 pr_err("Adding additional connection to this session"
564 " would exceed MaxConnections %d, login failed.\n",
565 sess->sess_ops->MaxConnections);
566 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
567 ISCSI_LOGIN_STATUS_ISID_ERROR);
574 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
576 struct iscsi_session *sess = conn->sess;
578 if (!sess->sess_ops->SessionType)
579 iscsit_start_nopin_timer(conn);
582 static int iscsi_post_login_handler(
584 struct iscsi_conn *conn,
588 struct iscsi_session *sess = conn->sess;
589 struct se_session *se_sess = sess->se_sess;
590 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
591 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
592 struct iscsi_thread_set *ts;
594 iscsit_inc_conn_usage_count(conn);
596 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
597 ISCSI_LOGIN_STATUS_ACCEPT);
599 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
600 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
602 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
603 iscsit_set_sync_and_steering_values(conn);
605 * SCSI Initiator -> SCSI Target Port Mapping
607 ts = iscsi_get_thread_set();
609 iscsi_set_session_parameters(sess->sess_ops,
610 conn->param_list, 0);
611 iscsi_release_param_list(conn->param_list);
612 conn->param_list = NULL;
614 spin_lock_bh(&sess->conn_lock);
615 atomic_set(&sess->session_continuation, 0);
616 if (sess->session_state == TARG_SESS_STATE_FAILED) {
618 " TARG_SESS_STATE_LOGGED_IN.\n");
619 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
623 pr_debug("iSCSI Login successful on CID: %hu from %s to"
624 " %s:%hu,%hu\n", conn->cid, conn->login_ip,
625 conn->local_ip, conn->local_port, tpg->tpgt);
627 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
628 atomic_inc(&sess->nconn);
629 pr_debug("Incremented iSCSI Connection count to %hu"
630 " from node: %s\n", atomic_read(&sess->nconn),
631 sess->sess_ops->InitiatorName);
632 spin_unlock_bh(&sess->conn_lock);
634 iscsi_post_login_start_timers(conn);
635 iscsi_activate_thread_set(conn, ts);
637 * Determine CPU mask to ensure connection's RX and TX kthreads
638 * are scheduled on the same CPU.
640 iscsit_thread_get_cpumask(conn);
641 conn->conn_rx_reset_cpumask = 1;
642 conn->conn_tx_reset_cpumask = 1;
644 iscsit_dec_conn_usage_count(conn);
646 spin_lock_bh(&se_tpg->session_lock);
647 iscsit_stop_time2retain_timer(sess);
648 spin_unlock_bh(&se_tpg->session_lock);
650 iscsit_dec_session_usage_count(sess);
654 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
655 iscsi_release_param_list(conn->param_list);
656 conn->param_list = NULL;
658 iscsit_determine_maxcmdsn(sess);
660 spin_lock_bh(&se_tpg->session_lock);
661 __transport_register_session(&sess->tpg->tpg_se_tpg,
662 se_sess->se_node_acl, se_sess, sess);
663 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
664 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
666 pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
667 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
670 spin_lock_bh(&sess->conn_lock);
671 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
672 atomic_inc(&sess->nconn);
673 pr_debug("Incremented iSCSI Connection count to %hu from node:"
674 " %s\n", atomic_read(&sess->nconn),
675 sess->sess_ops->InitiatorName);
676 spin_unlock_bh(&sess->conn_lock);
678 sess->sid = tpg->sid++;
680 sess->sid = tpg->sid++;
681 pr_debug("Established iSCSI session from node: %s\n",
682 sess->sess_ops->InitiatorName);
686 tpg->tpg_tiqn->tiqn_nsessions++;
688 pr_debug("Incremented number of active iSCSI sessions to %u on"
689 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
690 spin_unlock_bh(&se_tpg->session_lock);
692 iscsi_post_login_start_timers(conn);
693 iscsi_activate_thread_set(conn, ts);
695 * Determine CPU mask to ensure connection's RX and TX kthreads
696 * are scheduled on the same CPU.
698 iscsit_thread_get_cpumask(conn);
699 conn->conn_rx_reset_cpumask = 1;
700 conn->conn_tx_reset_cpumask = 1;
702 iscsit_dec_conn_usage_count(conn);
707 static void iscsi_handle_login_thread_timeout(unsigned long data)
709 struct iscsi_np *np = (struct iscsi_np *) data;
711 spin_lock_bh(&np->np_thread_lock);
712 pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
713 np->np_ip, np->np_port);
715 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
716 spin_unlock_bh(&np->np_thread_lock);
721 send_sig(SIGINT, np->np_thread, 1);
723 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
724 spin_unlock_bh(&np->np_thread_lock);
727 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
730 * This used the TA_LOGIN_TIMEOUT constant because at this
731 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
733 spin_lock_bh(&np->np_thread_lock);
734 init_timer(&np->np_login_timer);
735 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
736 np->np_login_timer.data = (unsigned long)np;
737 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
738 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
739 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
740 add_timer(&np->np_login_timer);
742 pr_debug("Added timeout timer to iSCSI login request for"
743 " %u seconds.\n", TA_LOGIN_TIMEOUT);
744 spin_unlock_bh(&np->np_thread_lock);
747 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
749 spin_lock_bh(&np->np_thread_lock);
750 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
751 spin_unlock_bh(&np->np_thread_lock);
754 np->np_login_timer_flags |= ISCSI_TF_STOP;
755 spin_unlock_bh(&np->np_thread_lock);
757 del_timer_sync(&np->np_login_timer);
759 spin_lock_bh(&np->np_thread_lock);
760 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
761 spin_unlock_bh(&np->np_thread_lock);
764 int iscsi_target_setup_login_socket(
766 struct __kernel_sockaddr_storage *sockaddr)
769 int backlog = 5, ret, opt = 0, len;
771 switch (np->np_network_transport) {
773 np->np_ip_proto = IPPROTO_TCP;
774 np->np_sock_type = SOCK_STREAM;
777 np->np_ip_proto = IPPROTO_SCTP;
778 np->np_sock_type = SOCK_STREAM;
781 np->np_ip_proto = IPPROTO_SCTP;
782 np->np_sock_type = SOCK_SEQPACKET;
784 case ISCSI_IWARP_TCP:
785 case ISCSI_IWARP_SCTP:
786 case ISCSI_INFINIBAND:
788 pr_err("Unsupported network_transport: %d\n",
789 np->np_network_transport);
793 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
794 np->np_ip_proto, &sock);
796 pr_err("sock_create() failed.\n");
799 np->np_socket = sock;
801 * Setup the np->np_sockaddr from the passed sockaddr setup
802 * in iscsi_target_configfs.c code..
804 memcpy(&np->np_sockaddr, sockaddr,
805 sizeof(struct __kernel_sockaddr_storage));
807 if (sockaddr->ss_family == AF_INET6)
808 len = sizeof(struct sockaddr_in6);
810 len = sizeof(struct sockaddr_in);
812 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
814 /* FIXME: Someone please explain why this is endian-safe */
816 if (np->np_network_transport == ISCSI_TCP) {
817 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
818 (char *)&opt, sizeof(opt));
820 pr_err("kernel_setsockopt() for TCP_NODELAY"
821 " failed: %d\n", ret);
826 /* FIXME: Someone please explain why this is endian-safe */
827 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
828 (char *)&opt, sizeof(opt));
830 pr_err("kernel_setsockopt() for SO_REUSEADDR"
835 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
836 (char *)&opt, sizeof(opt));
838 pr_err("kernel_setsockopt() for IP_FREEBIND"
843 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
845 pr_err("kernel_bind() failed: %d\n", ret);
849 ret = kernel_listen(sock, backlog);
851 pr_err("kernel_listen() failed: %d\n", ret);
858 np->np_socket = NULL;
864 static int __iscsi_target_login_thread(struct iscsi_np *np)
866 u8 buffer[ISCSI_HDR_LEN], iscsi_opcode, zero_tsih = 0;
867 int err, ret = 0, stop;
868 struct iscsi_conn *conn = NULL;
869 struct iscsi_login *login;
870 struct iscsi_portal_group *tpg = NULL;
871 struct socket *new_sock, *sock;
873 struct iscsi_login_req *pdu;
874 struct sockaddr_in sock_in;
875 struct sockaddr_in6 sock_in6;
877 flush_signals(current);
878 sock = np->np_socket;
880 spin_lock_bh(&np->np_thread_lock);
881 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
882 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
883 complete(&np->np_restart_comp);
885 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
887 spin_unlock_bh(&np->np_thread_lock);
889 if (kernel_accept(sock, &new_sock, 0) < 0) {
890 spin_lock_bh(&np->np_thread_lock);
891 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
892 spin_unlock_bh(&np->np_thread_lock);
893 complete(&np->np_restart_comp);
894 /* Get another socket */
897 spin_unlock_bh(&np->np_thread_lock);
900 iscsi_start_login_thread_timer(np);
902 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
904 pr_err("Could not allocate memory for"
905 " new connection\n");
906 sock_release(new_sock);
907 /* Get another socket */
911 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
912 conn->conn_state = TARG_CONN_STATE_FREE;
913 conn->sock = new_sock;
915 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
916 conn->conn_state = TARG_CONN_STATE_XPT_UP;
919 * Allocate conn->conn_ops early as a failure calling
920 * iscsit_tx_login_rsp() below will call tx_data().
922 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
923 if (!conn->conn_ops) {
924 pr_err("Unable to allocate memory for"
925 " struct iscsi_conn_ops.\n");
929 * Perform the remaining iSCSI connection initialization items..
931 if (iscsi_login_init_conn(conn) < 0)
934 memset(buffer, 0, ISCSI_HDR_LEN);
935 memset(&iov, 0, sizeof(struct kvec));
936 iov.iov_base = buffer;
937 iov.iov_len = ISCSI_HDR_LEN;
939 if (rx_data(conn, &iov, 1, ISCSI_HDR_LEN) <= 0) {
940 pr_err("rx_data() returned an error.\n");
944 iscsi_opcode = (buffer[0] & ISCSI_OPCODE_MASK);
945 if (!(iscsi_opcode & ISCSI_OP_LOGIN)) {
946 pr_err("First opcode is not login request,"
947 " failing login request.\n");
951 pdu = (struct iscsi_login_req *) buffer;
954 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
955 * when Status-Class != 0.
957 conn->login_itt = pdu->itt;
959 spin_lock_bh(&np->np_thread_lock);
960 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
961 spin_unlock_bh(&np->np_thread_lock);
962 pr_err("iSCSI Network Portal on %s:%hu currently not"
963 " active.\n", np->np_ip, np->np_port);
964 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
965 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
968 spin_unlock_bh(&np->np_thread_lock);
970 if (np->np_sockaddr.ss_family == AF_INET6) {
971 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
973 if (conn->sock->ops->getname(conn->sock,
974 (struct sockaddr *)&sock_in6, &err, 1) < 0) {
975 pr_err("sock_ops->getname() failed.\n");
976 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
977 ISCSI_LOGIN_STATUS_TARGET_ERROR);
980 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
981 &sock_in6.sin6_addr.in6_u);
982 conn->login_port = ntohs(sock_in6.sin6_port);
984 if (conn->sock->ops->getname(conn->sock,
985 (struct sockaddr *)&sock_in6, &err, 0) < 0) {
986 pr_err("sock_ops->getname() failed.\n");
987 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
988 ISCSI_LOGIN_STATUS_TARGET_ERROR);
991 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
992 &sock_in6.sin6_addr.in6_u);
993 conn->local_port = ntohs(sock_in6.sin6_port);
996 memset(&sock_in, 0, sizeof(struct sockaddr_in));
998 if (conn->sock->ops->getname(conn->sock,
999 (struct sockaddr *)&sock_in, &err, 1) < 0) {
1000 pr_err("sock_ops->getname() failed.\n");
1001 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1002 ISCSI_LOGIN_STATUS_TARGET_ERROR);
1005 sprintf(conn->login_ip, "%pI4", &sock_in.sin_addr.s_addr);
1006 conn->login_port = ntohs(sock_in.sin_port);
1008 if (conn->sock->ops->getname(conn->sock,
1009 (struct sockaddr *)&sock_in, &err, 0) < 0) {
1010 pr_err("sock_ops->getname() failed.\n");
1011 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1012 ISCSI_LOGIN_STATUS_TARGET_ERROR);
1015 sprintf(conn->local_ip, "%pI4", &sock_in.sin_addr.s_addr);
1016 conn->local_port = ntohs(sock_in.sin_port);
1019 conn->network_transport = np->np_network_transport;
1021 pr_debug("Received iSCSI login request from %s on %s Network"
1022 " Portal %s:%hu\n", conn->login_ip,
1023 (conn->network_transport == ISCSI_TCP) ? "TCP" : "SCTP",
1024 conn->local_ip, conn->local_port);
1026 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1027 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1029 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1030 pdu->min_version) < 0)
1033 zero_tsih = (pdu->tsih == 0x0000);
1036 * This is the leading connection of a new session.
1037 * We wait until after authentication to check for
1038 * session reinstatement.
1040 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1044 * Add a new connection to an existing session.
1045 * We check for a non-existant session in
1046 * iscsi_login_non_zero_tsih_s2() below based
1047 * on ISID/TSIH, but wait until after authentication
1048 * to check for connection reinstatement, etc.
1050 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1055 * This will process the first login request, and call
1056 * iscsi_target_locate_portal(), and return a valid struct iscsi_login.
1058 login = iscsi_target_init_negotiation(np, conn, buffer);
1066 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1071 if (iscsi_login_zero_tsih_s2(conn) < 0) {
1072 iscsi_target_nego_release(login, conn);
1076 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0) {
1077 iscsi_target_nego_release(login, conn);
1082 if (iscsi_target_start_negotiation(login, conn) < 0)
1086 pr_err("struct iscsi_conn session pointer is NULL!\n");
1090 iscsi_stop_login_thread_timer(np);
1092 if (signal_pending(current))
1095 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1100 iscsit_deaccess_np(np, tpg);
1102 /* Get another socket */
1106 pr_err("iSCSI Login negotiation failed.\n");
1107 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1108 ISCSI_LOGIN_STATUS_INIT_ERR);
1109 if (!zero_tsih || !conn->sess)
1111 if (conn->sess->se_sess)
1112 transport_free_session(conn->sess->se_sess);
1113 if (conn->sess->session_index != 0) {
1114 spin_lock_bh(&sess_idr_lock);
1115 idr_remove(&sess_idr, conn->sess->session_index);
1116 spin_unlock_bh(&sess_idr_lock);
1118 kfree(conn->sess->sess_ops);
1121 iscsi_stop_login_thread_timer(np);
1123 * If login negotiation fails check if the Time2Retain timer
1124 * needs to be restarted.
1126 if (!zero_tsih && conn->sess) {
1127 spin_lock_bh(&conn->sess->conn_lock);
1128 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1129 struct se_portal_group *se_tpg =
1130 &ISCSI_TPG_C(conn)->tpg_se_tpg;
1132 atomic_set(&conn->sess->session_continuation, 0);
1133 spin_unlock_bh(&conn->sess->conn_lock);
1134 spin_lock_bh(&se_tpg->session_lock);
1135 iscsit_start_time2retain_handler(conn->sess);
1136 spin_unlock_bh(&se_tpg->session_lock);
1138 spin_unlock_bh(&conn->sess->conn_lock);
1139 iscsit_dec_session_usage_count(conn->sess);
1142 if (!IS_ERR(conn->conn_rx_hash.tfm))
1143 crypto_free_hash(conn->conn_rx_hash.tfm);
1144 if (!IS_ERR(conn->conn_tx_hash.tfm))
1145 crypto_free_hash(conn->conn_tx_hash.tfm);
1147 if (conn->conn_cpumask)
1148 free_cpumask_var(conn->conn_cpumask);
1150 kfree(conn->conn_ops);
1152 if (conn->param_list) {
1153 iscsi_release_param_list(conn->param_list);
1154 conn->param_list = NULL;
1157 sock_release(conn->sock);
1161 iscsit_deaccess_np(np, tpg);
1166 stop = kthread_should_stop();
1167 if (!stop && signal_pending(current)) {
1168 spin_lock_bh(&np->np_thread_lock);
1169 stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN);
1170 spin_unlock_bh(&np->np_thread_lock);
1172 /* Wait for another socket.. */
1176 iscsi_stop_login_thread_timer(np);
1177 spin_lock_bh(&np->np_thread_lock);
1178 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1179 spin_unlock_bh(&np->np_thread_lock);
1183 int iscsi_target_login_thread(void *arg)
1185 struct iscsi_np *np = arg;
1188 allow_signal(SIGINT);
1190 while (!kthread_should_stop()) {
1191 ret = __iscsi_target_login_thread(np);
1193 * We break and exit here unless another sock_accept() call