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 <scsi/iscsi_proto.h>
25 #include <target/target_core_base.h>
26 #include <target/target_core_transport.h>
28 #include "iscsi_target_core.h"
29 #include "iscsi_target_tq.h"
30 #include "iscsi_target_device.h"
31 #include "iscsi_target_nego.h"
32 #include "iscsi_target_erl0.h"
33 #include "iscsi_target_erl2.h"
34 #include "iscsi_target_login.h"
35 #include "iscsi_target_stat.h"
36 #include "iscsi_target_tpg.h"
37 #include "iscsi_target_util.h"
38 #include "iscsi_target.h"
39 #include "iscsi_target_parameters.h"
41 extern struct idr sess_idr;
42 extern struct mutex auth_id_lock;
43 extern spinlock_t sess_idr_lock;
45 static int iscsi_login_init_conn(struct iscsi_conn *conn)
47 init_waitqueue_head(&conn->queues_wq);
48 INIT_LIST_HEAD(&conn->conn_list);
49 INIT_LIST_HEAD(&conn->conn_cmd_list);
50 INIT_LIST_HEAD(&conn->immed_queue_list);
51 INIT_LIST_HEAD(&conn->response_queue_list);
52 init_completion(&conn->conn_post_wait_comp);
53 init_completion(&conn->conn_wait_comp);
54 init_completion(&conn->conn_wait_rcfr_comp);
55 init_completion(&conn->conn_waiting_on_uc_comp);
56 init_completion(&conn->conn_logout_comp);
57 init_completion(&conn->rx_half_close_comp);
58 init_completion(&conn->tx_half_close_comp);
59 spin_lock_init(&conn->cmd_lock);
60 spin_lock_init(&conn->conn_usage_lock);
61 spin_lock_init(&conn->immed_queue_lock);
62 spin_lock_init(&conn->nopin_timer_lock);
63 spin_lock_init(&conn->response_queue_lock);
64 spin_lock_init(&conn->state_lock);
66 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
67 pr_err("Unable to allocate conn->conn_cpumask\n");
75 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
76 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
78 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
81 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
82 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
83 * to software 1x8 byte slicing from crc32c.ko
85 conn->conn_rx_hash.flags = 0;
86 conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
88 if (IS_ERR(conn->conn_rx_hash.tfm)) {
89 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
93 conn->conn_tx_hash.flags = 0;
94 conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
96 if (IS_ERR(conn->conn_tx_hash.tfm)) {
97 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
98 crypto_free_hash(conn->conn_rx_hash.tfm);
105 static int iscsi_login_check_initiator_version(
106 struct iscsi_conn *conn,
110 if ((version_max != 0x00) || (version_min != 0x00)) {
111 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
112 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
113 version_min, version_max);
114 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
115 ISCSI_LOGIN_STATUS_NO_VERSION);
122 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
125 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
126 struct iscsi_portal_group *tpg = conn->tpg;
127 struct iscsi_session *sess = NULL, *sess_p = NULL;
128 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
129 struct se_session *se_sess, *se_sess_tmp;
131 initiatorname_param = iscsi_find_param_from_key(
132 INITIATORNAME, conn->param_list);
133 sessiontype_param = iscsi_find_param_from_key(
134 SESSIONTYPE, conn->param_list);
135 if (!initiatorname_param || !sessiontype_param) {
136 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
137 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
141 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
143 spin_lock_bh(&se_tpg->session_lock);
144 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
147 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
148 spin_lock(&sess_p->conn_lock);
149 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
150 atomic_read(&sess_p->session_logout) ||
151 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
152 spin_unlock(&sess_p->conn_lock);
155 if (!memcmp((void *)sess_p->isid, (void *)conn->sess->isid, 6) &&
156 (!strcmp((void *)sess_p->sess_ops->InitiatorName,
157 (void *)initiatorname_param->value) &&
158 (sess_p->sess_ops->SessionType == sessiontype))) {
159 atomic_set(&sess_p->session_reinstatement, 1);
160 spin_unlock(&sess_p->conn_lock);
161 iscsit_inc_session_usage_count(sess_p);
162 iscsit_stop_time2retain_timer(sess_p);
166 spin_unlock(&sess_p->conn_lock);
168 spin_unlock_bh(&se_tpg->session_lock);
170 * If the Time2Retain handler has expired, the session is already gone.
175 pr_debug("%s iSCSI Session SID %u is still active for %s,"
176 " preforming session reinstatement.\n", (sessiontype) ?
177 "Discovery" : "Normal", sess->sid,
178 sess->sess_ops->InitiatorName);
180 spin_lock_bh(&sess->conn_lock);
181 if (sess->session_state == TARG_SESS_STATE_FAILED) {
182 spin_unlock_bh(&sess->conn_lock);
183 iscsit_dec_session_usage_count(sess);
184 return iscsit_close_session(sess);
186 spin_unlock_bh(&sess->conn_lock);
188 iscsit_stop_session(sess, 1, 1);
189 iscsit_dec_session_usage_count(sess);
191 return iscsit_close_session(sess);
194 static void iscsi_login_set_conn_values(
195 struct iscsi_session *sess,
196 struct iscsi_conn *conn,
202 * Generate a random Status sequence number (statsn) for the new
205 get_random_bytes(&conn->stat_sn, sizeof(u32));
207 mutex_lock(&auth_id_lock);
208 conn->auth_id = iscsit_global->auth_id++;
209 mutex_unlock(&auth_id_lock);
213 * This is the leading connection of a new session,
214 * or session reinstatement.
216 static int iscsi_login_zero_tsih_s1(
217 struct iscsi_conn *conn,
220 struct iscsi_session *sess = NULL;
221 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((void *)&sess->isid, (void *)pdu->isid, 6);
234 sess->exp_cmd_sn = 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 if (!idr_pre_get(&sess_idr, GFP_KERNEL)) {
251 pr_err("idr_pre_get() for sess_idr failed\n");
252 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
253 ISCSI_LOGIN_STATUS_NO_RESOURCES);
257 spin_lock(&sess_idr_lock);
258 idr_get_new(&sess_idr, NULL, &sess->session_index);
259 spin_unlock(&sess_idr_lock);
261 sess->creation_time = get_jiffies_64();
262 spin_lock_init(&sess->session_stats_lock);
264 * The FFP CmdSN window values will be allocated from the TPG's
265 * Initiator Node's ACL once the login has been successfully completed.
267 sess->max_cmd_sn = pdu->cmdsn;
269 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
270 if (!sess->sess_ops) {
271 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
272 ISCSI_LOGIN_STATUS_NO_RESOURCES);
273 pr_err("Unable to allocate memory for"
274 " struct iscsi_sess_ops.\n");
279 sess->se_sess = transport_init_session();
280 if (IS_ERR(sess->se_sess)) {
281 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
282 ISCSI_LOGIN_STATUS_NO_RESOURCES);
290 static int iscsi_login_zero_tsih_s2(
291 struct iscsi_conn *conn)
293 struct iscsi_node_attrib *na;
294 struct iscsi_session *sess = conn->sess;
295 unsigned char buf[32];
297 sess->tpg = conn->tpg;
300 * Assign a new TPG Session Handle. Note this is protected with
301 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
303 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
305 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
308 * Create the default params from user defined values..
310 if (iscsi_copy_param_list(&conn->param_list,
311 ISCSI_TPG_C(conn)->param_list, 1) < 0) {
312 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
313 ISCSI_LOGIN_STATUS_NO_RESOURCES);
317 iscsi_set_keys_to_negotiate(0, conn->param_list);
319 if (sess->sess_ops->SessionType)
320 return iscsi_set_keys_irrelevant_for_discovery(
323 na = iscsit_tpg_get_node_attrib(sess);
326 * Need to send TargetPortalGroupTag back in first login response
327 * on any iSCSI connection where the Initiator provides TargetName.
328 * See 5.3.1. Login Phase Start
330 * In our case, we have already located the struct iscsi_tiqn at this point.
333 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
334 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
335 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
336 ISCSI_LOGIN_STATUS_NO_RESOURCES);
341 * Workaround for Initiators that have broken connection recovery logic.
343 * "We would really like to get rid of this." Linux-iSCSI.org team
346 sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
347 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
348 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
349 ISCSI_LOGIN_STATUS_NO_RESOURCES);
353 if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
360 * Remove PSTATE_NEGOTIATE for the four FIM related keys.
361 * The Initiator node will be able to enable FIM by proposing them itself.
363 int iscsi_login_disable_FIM_keys(
364 struct iscsi_param_list *param_list,
365 struct iscsi_conn *conn)
367 struct iscsi_param *param;
369 param = iscsi_find_param_from_key("OFMarker", param_list);
371 pr_err("iscsi_find_param_from_key() for"
372 " OFMarker failed\n");
373 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
374 ISCSI_LOGIN_STATUS_NO_RESOURCES);
377 param->state &= ~PSTATE_NEGOTIATE;
379 param = iscsi_find_param_from_key("OFMarkInt", param_list);
381 pr_err("iscsi_find_param_from_key() for"
382 " IFMarker failed\n");
383 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
384 ISCSI_LOGIN_STATUS_NO_RESOURCES);
387 param->state &= ~PSTATE_NEGOTIATE;
389 param = iscsi_find_param_from_key("IFMarker", param_list);
391 pr_err("iscsi_find_param_from_key() for"
392 " IFMarker failed\n");
393 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
394 ISCSI_LOGIN_STATUS_NO_RESOURCES);
397 param->state &= ~PSTATE_NEGOTIATE;
399 param = iscsi_find_param_from_key("IFMarkInt", param_list);
401 pr_err("iscsi_find_param_from_key() for"
402 " IFMarker failed\n");
403 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
404 ISCSI_LOGIN_STATUS_NO_RESOURCES);
407 param->state &= ~PSTATE_NEGOTIATE;
412 static int iscsi_login_non_zero_tsih_s1(
413 struct iscsi_conn *conn,
416 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
418 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
423 * Add a new connection to an existing session.
425 static int iscsi_login_non_zero_tsih_s2(
426 struct iscsi_conn *conn,
429 struct iscsi_portal_group *tpg = conn->tpg;
430 struct iscsi_session *sess = NULL, *sess_p = NULL;
431 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
432 struct se_session *se_sess, *se_sess_tmp;
433 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
435 spin_lock_bh(&se_tpg->session_lock);
436 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
439 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
440 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
441 atomic_read(&sess_p->session_logout) ||
442 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
444 if (!memcmp((const void *)sess_p->isid,
445 (const void *)pdu->isid, 6) &&
446 (sess_p->tsih == pdu->tsih)) {
447 iscsit_inc_session_usage_count(sess_p);
448 iscsit_stop_time2retain_timer(sess_p);
453 spin_unlock_bh(&se_tpg->session_lock);
456 * If the Time2Retain handler has expired, the session is already gone.
459 pr_err("Initiator attempting to add a connection to"
460 " a non-existent session, rejecting iSCSI Login.\n");
461 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
462 ISCSI_LOGIN_STATUS_NO_SESSION);
467 * Stop the Time2Retain timer if this is a failed session, we restart
468 * the timer if the login is not successful.
470 spin_lock_bh(&sess->conn_lock);
471 if (sess->session_state == TARG_SESS_STATE_FAILED)
472 atomic_set(&sess->session_continuation, 1);
473 spin_unlock_bh(&sess->conn_lock);
475 iscsi_login_set_conn_values(sess, conn, pdu->cid);
477 if (iscsi_copy_param_list(&conn->param_list,
478 ISCSI_TPG_C(conn)->param_list, 0) < 0) {
479 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
480 ISCSI_LOGIN_STATUS_NO_RESOURCES);
484 iscsi_set_keys_to_negotiate(0, conn->param_list);
486 * Need to send TargetPortalGroupTag back in first login response
487 * on any iSCSI connection where the Initiator provides TargetName.
488 * See 5.3.1. Login Phase Start
490 * In our case, we have already located the struct iscsi_tiqn at this point.
493 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
494 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
495 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
496 ISCSI_LOGIN_STATUS_NO_RESOURCES);
500 return iscsi_login_disable_FIM_keys(conn->param_list, conn);
503 int iscsi_login_post_auth_non_zero_tsih(
504 struct iscsi_conn *conn,
508 struct iscsi_conn *conn_ptr = NULL;
509 struct iscsi_conn_recovery *cr = NULL;
510 struct iscsi_session *sess = conn->sess;
513 * By following item 5 in the login table, if we have found
514 * an existing ISID and a valid/existing TSIH and an existing
515 * CID we do connection reinstatement. Currently we dont not
516 * support it so we send back an non-zero status class to the
517 * initiator and release the new connection.
519 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
521 pr_err("Connection exists with CID %hu for %s,"
522 " performing connection reinstatement.\n",
523 conn_ptr->cid, sess->sess_ops->InitiatorName);
525 iscsit_connection_reinstatement_rcfr(conn_ptr);
526 iscsit_dec_conn_usage_count(conn_ptr);
530 * Check for any connection recovery entires containing CID.
531 * We use the original ExpStatSN sent in the first login request
532 * to acknowledge commands for the failed connection.
534 * Also note that an explict logout may have already been sent,
535 * but the response may not be sent due to additional connection
538 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
539 cr = iscsit_get_inactive_connection_recovery_entry(
542 pr_debug("Performing implicit logout"
543 " for connection recovery on CID: %hu\n",
545 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
550 * Else we follow item 4 from the login table in that we have
551 * found an existing ISID and a valid/existing TSIH and a new
552 * CID we go ahead and continue to add a new connection to the
555 pr_debug("Adding CID %hu to existing session for %s.\n",
556 cid, sess->sess_ops->InitiatorName);
558 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
559 pr_err("Adding additional connection to this session"
560 " would exceed MaxConnections %d, login failed.\n",
561 sess->sess_ops->MaxConnections);
562 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
563 ISCSI_LOGIN_STATUS_ISID_ERROR);
570 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
572 struct iscsi_session *sess = conn->sess;
574 if (!sess->sess_ops->SessionType)
575 iscsit_start_nopin_timer(conn);
578 static int iscsi_post_login_handler(
580 struct iscsi_conn *conn,
584 struct iscsi_session *sess = conn->sess;
585 struct se_session *se_sess = sess->se_sess;
586 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
587 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
588 struct iscsi_thread_set *ts;
590 iscsit_inc_conn_usage_count(conn);
592 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
593 ISCSI_LOGIN_STATUS_ACCEPT);
595 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
596 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
598 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
599 iscsit_set_sync_and_steering_values(conn);
601 * SCSI Initiator -> SCSI Target Port Mapping
603 ts = iscsi_get_thread_set();
605 iscsi_set_session_parameters(sess->sess_ops,
606 conn->param_list, 0);
607 iscsi_release_param_list(conn->param_list);
608 conn->param_list = NULL;
610 spin_lock_bh(&sess->conn_lock);
611 atomic_set(&sess->session_continuation, 0);
612 if (sess->session_state == TARG_SESS_STATE_FAILED) {
614 " TARG_SESS_STATE_LOGGED_IN.\n");
615 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
619 pr_debug("iSCSI Login successful on CID: %hu from %s to"
620 " %s:%hu,%hu\n", conn->cid, conn->login_ip,
621 conn->local_ip, conn->local_port, tpg->tpgt);
623 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
624 atomic_inc(&sess->nconn);
625 pr_debug("Incremented iSCSI Connection count to %hu"
626 " from node: %s\n", atomic_read(&sess->nconn),
627 sess->sess_ops->InitiatorName);
628 spin_unlock_bh(&sess->conn_lock);
630 iscsi_post_login_start_timers(conn);
631 iscsi_activate_thread_set(conn, ts);
633 * Determine CPU mask to ensure connection's RX and TX kthreads
634 * are scheduled on the same CPU.
636 iscsit_thread_get_cpumask(conn);
637 conn->conn_rx_reset_cpumask = 1;
638 conn->conn_tx_reset_cpumask = 1;
640 iscsit_dec_conn_usage_count(conn);
642 spin_lock_bh(&se_tpg->session_lock);
643 iscsit_stop_time2retain_timer(sess);
644 spin_unlock_bh(&se_tpg->session_lock);
646 iscsit_dec_session_usage_count(sess);
650 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
651 iscsi_release_param_list(conn->param_list);
652 conn->param_list = NULL;
654 iscsit_determine_maxcmdsn(sess);
656 spin_lock_bh(&se_tpg->session_lock);
657 __transport_register_session(&sess->tpg->tpg_se_tpg,
658 se_sess->se_node_acl, se_sess, (void *)sess);
659 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
660 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
662 pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
663 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
666 spin_lock_bh(&sess->conn_lock);
667 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
668 atomic_inc(&sess->nconn);
669 pr_debug("Incremented iSCSI Connection count to %hu from node:"
670 " %s\n", atomic_read(&sess->nconn),
671 sess->sess_ops->InitiatorName);
672 spin_unlock_bh(&sess->conn_lock);
674 sess->sid = tpg->sid++;
676 sess->sid = tpg->sid++;
677 pr_debug("Established iSCSI session from node: %s\n",
678 sess->sess_ops->InitiatorName);
682 tpg->tpg_tiqn->tiqn_nsessions++;
684 pr_debug("Incremented number of active iSCSI sessions to %u on"
685 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
686 spin_unlock_bh(&se_tpg->session_lock);
688 iscsi_post_login_start_timers(conn);
689 iscsi_activate_thread_set(conn, ts);
691 * Determine CPU mask to ensure connection's RX and TX kthreads
692 * are scheduled on the same CPU.
694 iscsit_thread_get_cpumask(conn);
695 conn->conn_rx_reset_cpumask = 1;
696 conn->conn_tx_reset_cpumask = 1;
698 iscsit_dec_conn_usage_count(conn);
703 static void iscsi_handle_login_thread_timeout(unsigned long data)
705 struct iscsi_np *np = (struct iscsi_np *) data;
707 spin_lock_bh(&np->np_thread_lock);
708 pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
709 np->np_ip, np->np_port);
711 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
712 spin_unlock_bh(&np->np_thread_lock);
717 send_sig(SIGINT, np->np_thread, 1);
719 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
720 spin_unlock_bh(&np->np_thread_lock);
723 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
726 * This used the TA_LOGIN_TIMEOUT constant because at this
727 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
729 spin_lock_bh(&np->np_thread_lock);
730 init_timer(&np->np_login_timer);
731 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
732 np->np_login_timer.data = (unsigned long)np;
733 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
734 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
735 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
736 add_timer(&np->np_login_timer);
738 pr_debug("Added timeout timer to iSCSI login request for"
739 " %u seconds.\n", TA_LOGIN_TIMEOUT);
740 spin_unlock_bh(&np->np_thread_lock);
743 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
745 spin_lock_bh(&np->np_thread_lock);
746 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
747 spin_unlock_bh(&np->np_thread_lock);
750 np->np_login_timer_flags |= ISCSI_TF_STOP;
751 spin_unlock_bh(&np->np_thread_lock);
753 del_timer_sync(&np->np_login_timer);
755 spin_lock_bh(&np->np_thread_lock);
756 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
757 spin_unlock_bh(&np->np_thread_lock);
760 int iscsi_target_setup_login_socket(
762 struct __kernel_sockaddr_storage *sockaddr)
765 int backlog = 5, ret, opt = 0, len;
767 switch (np->np_network_transport) {
769 np->np_ip_proto = IPPROTO_TCP;
770 np->np_sock_type = SOCK_STREAM;
773 np->np_ip_proto = IPPROTO_SCTP;
774 np->np_sock_type = SOCK_STREAM;
777 np->np_ip_proto = IPPROTO_SCTP;
778 np->np_sock_type = SOCK_SEQPACKET;
780 case ISCSI_IWARP_TCP:
781 case ISCSI_IWARP_SCTP:
782 case ISCSI_INFINIBAND:
784 pr_err("Unsupported network_transport: %d\n",
785 np->np_network_transport);
789 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
790 np->np_ip_proto, &sock);
792 pr_err("sock_create() failed.\n");
795 np->np_socket = sock;
797 * Setup the np->np_sockaddr from the passed sockaddr setup
798 * in iscsi_target_configfs.c code..
800 memcpy((void *)&np->np_sockaddr, (void *)sockaddr,
801 sizeof(struct __kernel_sockaddr_storage));
803 if (sockaddr->ss_family == AF_INET6)
804 len = sizeof(struct sockaddr_in6);
806 len = sizeof(struct sockaddr_in);
808 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
811 if (np->np_network_transport == ISCSI_TCP) {
812 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
813 (char *)&opt, sizeof(opt));
815 pr_err("kernel_setsockopt() for TCP_NODELAY"
816 " failed: %d\n", ret);
821 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
822 (char *)&opt, sizeof(opt));
824 pr_err("kernel_setsockopt() for SO_REUSEADDR"
829 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
831 pr_err("kernel_bind() failed: %d\n", ret);
835 ret = kernel_listen(sock, backlog);
837 pr_err("kernel_listen() failed: %d\n", ret);
844 np->np_socket = NULL;
850 static int __iscsi_target_login_thread(struct iscsi_np *np)
852 u8 buffer[ISCSI_HDR_LEN], iscsi_opcode, zero_tsih = 0;
853 int err, ret = 0, ip_proto, sock_type, stop;
854 struct iscsi_conn *conn = NULL;
855 struct iscsi_login *login;
856 struct iscsi_portal_group *tpg = NULL;
857 struct socket *new_sock, *sock;
859 struct iscsi_login_req *pdu;
860 struct sockaddr_in sock_in;
861 struct sockaddr_in6 sock_in6;
863 flush_signals(current);
864 sock = np->np_socket;
865 ip_proto = np->np_ip_proto;
866 sock_type = np->np_sock_type;
868 spin_lock_bh(&np->np_thread_lock);
869 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
870 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
871 complete(&np->np_restart_comp);
873 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
875 spin_unlock_bh(&np->np_thread_lock);
877 if (kernel_accept(sock, &new_sock, 0) < 0) {
878 spin_lock_bh(&np->np_thread_lock);
879 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
880 spin_unlock_bh(&np->np_thread_lock);
881 complete(&np->np_restart_comp);
882 /* Get another socket */
885 spin_unlock_bh(&np->np_thread_lock);
888 iscsi_start_login_thread_timer(np);
890 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
892 pr_err("Could not allocate memory for"
893 " new connection\n");
894 sock_release(new_sock);
895 /* Get another socket */
899 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
900 conn->conn_state = TARG_CONN_STATE_FREE;
901 conn->sock = new_sock;
903 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
904 conn->conn_state = TARG_CONN_STATE_XPT_UP;
907 * Allocate conn->conn_ops early as a failure calling
908 * iscsit_tx_login_rsp() below will call tx_data().
910 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
911 if (!conn->conn_ops) {
912 pr_err("Unable to allocate memory for"
913 " struct iscsi_conn_ops.\n");
917 * Perform the remaining iSCSI connection initialization items..
919 if (iscsi_login_init_conn(conn) < 0)
922 memset(buffer, 0, ISCSI_HDR_LEN);
923 memset(&iov, 0, sizeof(struct kvec));
924 iov.iov_base = buffer;
925 iov.iov_len = ISCSI_HDR_LEN;
927 if (rx_data(conn, &iov, 1, ISCSI_HDR_LEN) <= 0) {
928 pr_err("rx_data() returned an error.\n");
932 iscsi_opcode = (buffer[0] & ISCSI_OPCODE_MASK);
933 if (!(iscsi_opcode & ISCSI_OP_LOGIN)) {
934 pr_err("First opcode is not login request,"
935 " failing login request.\n");
939 pdu = (struct iscsi_login_req *) buffer;
940 pdu->cid = be16_to_cpu(pdu->cid);
941 pdu->tsih = be16_to_cpu(pdu->tsih);
942 pdu->itt = be32_to_cpu(pdu->itt);
943 pdu->cmdsn = be32_to_cpu(pdu->cmdsn);
944 pdu->exp_statsn = be32_to_cpu(pdu->exp_statsn);
946 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
947 * when Status-Class != 0.
949 conn->login_itt = pdu->itt;
951 spin_lock_bh(&np->np_thread_lock);
952 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
953 spin_unlock_bh(&np->np_thread_lock);
954 pr_err("iSCSI Network Portal on %s:%hu currently not"
955 " active.\n", np->np_ip, np->np_port);
956 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
957 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
960 spin_unlock_bh(&np->np_thread_lock);
962 if (np->np_sockaddr.ss_family == AF_INET6) {
963 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
965 if (conn->sock->ops->getname(conn->sock,
966 (struct sockaddr *)&sock_in6, &err, 1) < 0) {
967 pr_err("sock_ops->getname() failed.\n");
968 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
969 ISCSI_LOGIN_STATUS_TARGET_ERROR);
972 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
973 &sock_in6.sin6_addr.in6_u);
974 conn->login_port = ntohs(sock_in6.sin6_port);
976 if (conn->sock->ops->getname(conn->sock,
977 (struct sockaddr *)&sock_in6, &err, 0) < 0) {
978 pr_err("sock_ops->getname() failed.\n");
979 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
980 ISCSI_LOGIN_STATUS_TARGET_ERROR);
983 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
984 &sock_in6.sin6_addr.in6_u);
985 conn->local_port = ntohs(sock_in6.sin6_port);
988 memset(&sock_in, 0, sizeof(struct sockaddr_in));
990 if (conn->sock->ops->getname(conn->sock,
991 (struct sockaddr *)&sock_in, &err, 1) < 0) {
992 pr_err("sock_ops->getname() failed.\n");
993 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
994 ISCSI_LOGIN_STATUS_TARGET_ERROR);
997 sprintf(conn->login_ip, "%pI4", &sock_in.sin_addr.s_addr);
998 conn->login_port = ntohs(sock_in.sin_port);
1000 if (conn->sock->ops->getname(conn->sock,
1001 (struct sockaddr *)&sock_in, &err, 0) < 0) {
1002 pr_err("sock_ops->getname() failed.\n");
1003 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1004 ISCSI_LOGIN_STATUS_TARGET_ERROR);
1007 sprintf(conn->local_ip, "%pI4", &sock_in.sin_addr.s_addr);
1008 conn->local_port = ntohs(sock_in.sin_port);
1011 conn->network_transport = np->np_network_transport;
1013 pr_debug("Received iSCSI login request from %s on %s Network"
1014 " Portal %s:%hu\n", conn->login_ip,
1015 (conn->network_transport == ISCSI_TCP) ? "TCP" : "SCTP",
1016 conn->local_ip, conn->local_port);
1018 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1019 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1021 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1022 pdu->min_version) < 0)
1025 zero_tsih = (pdu->tsih == 0x0000);
1028 * This is the leading connection of a new session.
1029 * We wait until after authentication to check for
1030 * session reinstatement.
1032 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1036 * Add a new connection to an existing session.
1037 * We check for a non-existant session in
1038 * iscsi_login_non_zero_tsih_s2() below based
1039 * on ISID/TSIH, but wait until after authentication
1040 * to check for connection reinstatement, etc.
1042 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1047 * This will process the first login request, and call
1048 * iscsi_target_locate_portal(), and return a valid struct iscsi_login.
1050 login = iscsi_target_init_negotiation(np, conn, buffer);
1058 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1063 if (iscsi_login_zero_tsih_s2(conn) < 0) {
1064 iscsi_target_nego_release(login, conn);
1068 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0) {
1069 iscsi_target_nego_release(login, conn);
1074 if (iscsi_target_start_negotiation(login, conn) < 0)
1078 pr_err("struct iscsi_conn session pointer is NULL!\n");
1082 iscsi_stop_login_thread_timer(np);
1084 if (signal_pending(current))
1087 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1092 iscsit_deaccess_np(np, tpg);
1094 /* Get another socket */
1098 pr_err("iSCSI Login negotiation failed.\n");
1099 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1100 ISCSI_LOGIN_STATUS_INIT_ERR);
1101 if (!zero_tsih || !conn->sess)
1103 if (conn->sess->se_sess)
1104 transport_free_session(conn->sess->se_sess);
1105 if (conn->sess->session_index != 0) {
1106 spin_lock_bh(&sess_idr_lock);
1107 idr_remove(&sess_idr, conn->sess->session_index);
1108 spin_unlock_bh(&sess_idr_lock);
1110 if (conn->sess->sess_ops)
1111 kfree(conn->sess->sess_ops);
1115 iscsi_stop_login_thread_timer(np);
1117 * If login negotiation fails check if the Time2Retain timer
1118 * needs to be restarted.
1120 if (!zero_tsih && conn->sess) {
1121 spin_lock_bh(&conn->sess->conn_lock);
1122 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1123 struct se_portal_group *se_tpg =
1124 &ISCSI_TPG_C(conn)->tpg_se_tpg;
1126 atomic_set(&conn->sess->session_continuation, 0);
1127 spin_unlock_bh(&conn->sess->conn_lock);
1128 spin_lock_bh(&se_tpg->session_lock);
1129 iscsit_start_time2retain_handler(conn->sess);
1130 spin_unlock_bh(&se_tpg->session_lock);
1132 spin_unlock_bh(&conn->sess->conn_lock);
1133 iscsit_dec_session_usage_count(conn->sess);
1136 if (!IS_ERR(conn->conn_rx_hash.tfm))
1137 crypto_free_hash(conn->conn_rx_hash.tfm);
1138 if (!IS_ERR(conn->conn_tx_hash.tfm))
1139 crypto_free_hash(conn->conn_tx_hash.tfm);
1141 if (conn->conn_cpumask)
1142 free_cpumask_var(conn->conn_cpumask);
1144 kfree(conn->conn_ops);
1146 if (conn->param_list) {
1147 iscsi_release_param_list(conn->param_list);
1148 conn->param_list = NULL;
1151 sock_release(conn->sock);
1155 iscsit_deaccess_np(np, tpg);
1160 stop = kthread_should_stop();
1161 if (!stop && signal_pending(current)) {
1162 spin_lock_bh(&np->np_thread_lock);
1163 stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN);
1164 spin_unlock_bh(&np->np_thread_lock);
1166 /* Wait for another socket.. */
1170 iscsi_stop_login_thread_timer(np);
1171 spin_lock_bh(&np->np_thread_lock);
1172 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1173 spin_unlock_bh(&np->np_thread_lock);
1177 int iscsi_target_login_thread(void *arg)
1179 struct iscsi_np *np = (struct iscsi_np *)arg;
1182 allow_signal(SIGINT);
1184 while (!kthread_should_stop()) {
1185 ret = __iscsi_target_login_thread(np);
1187 * We break and exit here unless another sock_accept() call