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 if (!initiatorname_param)
133 sessiontype_param = iscsi_find_param_from_key(
134 SESSIONTYPE, conn->param_list);
135 if (!sessiontype_param)
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 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 ret = idr_get_new(&sess_idr, NULL, &sess->session_index);
259 spin_unlock(&sess_idr_lock);
262 pr_err("idr_get_new() for sess_idr failed\n");
263 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
264 ISCSI_LOGIN_STATUS_NO_RESOURCES);
269 sess->creation_time = get_jiffies_64();
270 spin_lock_init(&sess->session_stats_lock);
272 * The FFP CmdSN window values will be allocated from the TPG's
273 * Initiator Node's ACL once the login has been successfully completed.
275 sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn);
277 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
278 if (!sess->sess_ops) {
279 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
280 ISCSI_LOGIN_STATUS_NO_RESOURCES);
281 pr_err("Unable to allocate memory for"
282 " struct iscsi_sess_ops.\n");
287 sess->se_sess = transport_init_session();
288 if (IS_ERR(sess->se_sess)) {
289 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
290 ISCSI_LOGIN_STATUS_NO_RESOURCES);
298 static int iscsi_login_zero_tsih_s2(
299 struct iscsi_conn *conn)
301 struct iscsi_node_attrib *na;
302 struct iscsi_session *sess = conn->sess;
303 unsigned char buf[32];
305 sess->tpg = conn->tpg;
308 * Assign a new TPG Session Handle. Note this is protected with
309 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
311 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
313 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
316 * Create the default params from user defined values..
318 if (iscsi_copy_param_list(&conn->param_list,
319 ISCSI_TPG_C(conn)->param_list, 1) < 0) {
320 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
321 ISCSI_LOGIN_STATUS_NO_RESOURCES);
325 iscsi_set_keys_to_negotiate(0, conn->param_list);
327 if (sess->sess_ops->SessionType)
328 return iscsi_set_keys_irrelevant_for_discovery(
331 na = iscsit_tpg_get_node_attrib(sess);
334 * Need to send TargetPortalGroupTag back in first login response
335 * on any iSCSI connection where the Initiator provides TargetName.
336 * See 5.3.1. Login Phase Start
338 * In our case, we have already located the struct iscsi_tiqn at this point.
341 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
342 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
343 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
344 ISCSI_LOGIN_STATUS_NO_RESOURCES);
349 * Workaround for Initiators that have broken connection recovery logic.
351 * "We would really like to get rid of this." Linux-iSCSI.org team
354 sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
355 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
356 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
357 ISCSI_LOGIN_STATUS_NO_RESOURCES);
361 if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
368 * Remove PSTATE_NEGOTIATE for the four FIM related keys.
369 * The Initiator node will be able to enable FIM by proposing them itself.
371 int iscsi_login_disable_FIM_keys(
372 struct iscsi_param_list *param_list,
373 struct iscsi_conn *conn)
375 struct iscsi_param *param;
377 param = iscsi_find_param_from_key("OFMarker", param_list);
379 pr_err("iscsi_find_param_from_key() for"
380 " OFMarker failed\n");
381 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
382 ISCSI_LOGIN_STATUS_NO_RESOURCES);
385 param->state &= ~PSTATE_NEGOTIATE;
387 param = iscsi_find_param_from_key("OFMarkInt", param_list);
389 pr_err("iscsi_find_param_from_key() for"
390 " IFMarker failed\n");
391 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
392 ISCSI_LOGIN_STATUS_NO_RESOURCES);
395 param->state &= ~PSTATE_NEGOTIATE;
397 param = iscsi_find_param_from_key("IFMarker", param_list);
399 pr_err("iscsi_find_param_from_key() for"
400 " IFMarker failed\n");
401 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
402 ISCSI_LOGIN_STATUS_NO_RESOURCES);
405 param->state &= ~PSTATE_NEGOTIATE;
407 param = iscsi_find_param_from_key("IFMarkInt", param_list);
409 pr_err("iscsi_find_param_from_key() for"
410 " IFMarker failed\n");
411 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
412 ISCSI_LOGIN_STATUS_NO_RESOURCES);
415 param->state &= ~PSTATE_NEGOTIATE;
420 static int iscsi_login_non_zero_tsih_s1(
421 struct iscsi_conn *conn,
424 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
426 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
431 * Add a new connection to an existing session.
433 static int iscsi_login_non_zero_tsih_s2(
434 struct iscsi_conn *conn,
437 struct iscsi_portal_group *tpg = conn->tpg;
438 struct iscsi_session *sess = NULL, *sess_p = NULL;
439 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
440 struct se_session *se_sess, *se_sess_tmp;
441 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
443 spin_lock_bh(&se_tpg->session_lock);
444 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
447 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
448 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
449 atomic_read(&sess_p->session_logout) ||
450 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
452 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
453 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
454 iscsit_inc_session_usage_count(sess_p);
455 iscsit_stop_time2retain_timer(sess_p);
460 spin_unlock_bh(&se_tpg->session_lock);
463 * If the Time2Retain handler has expired, the session is already gone.
466 pr_err("Initiator attempting to add a connection to"
467 " a non-existent session, rejecting iSCSI Login.\n");
468 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
469 ISCSI_LOGIN_STATUS_NO_SESSION);
474 * Stop the Time2Retain timer if this is a failed session, we restart
475 * the timer if the login is not successful.
477 spin_lock_bh(&sess->conn_lock);
478 if (sess->session_state == TARG_SESS_STATE_FAILED)
479 atomic_set(&sess->session_continuation, 1);
480 spin_unlock_bh(&sess->conn_lock);
482 iscsi_login_set_conn_values(sess, conn, pdu->cid);
484 if (iscsi_copy_param_list(&conn->param_list,
485 ISCSI_TPG_C(conn)->param_list, 0) < 0) {
486 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
487 ISCSI_LOGIN_STATUS_NO_RESOURCES);
491 iscsi_set_keys_to_negotiate(0, conn->param_list);
493 * Need to send TargetPortalGroupTag back in first login response
494 * on any iSCSI connection where the Initiator provides TargetName.
495 * See 5.3.1. Login Phase Start
497 * In our case, we have already located the struct iscsi_tiqn at this point.
500 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
501 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
502 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
503 ISCSI_LOGIN_STATUS_NO_RESOURCES);
507 return iscsi_login_disable_FIM_keys(conn->param_list, conn);
510 int iscsi_login_post_auth_non_zero_tsih(
511 struct iscsi_conn *conn,
515 struct iscsi_conn *conn_ptr = NULL;
516 struct iscsi_conn_recovery *cr = NULL;
517 struct iscsi_session *sess = conn->sess;
520 * By following item 5 in the login table, if we have found
521 * an existing ISID and a valid/existing TSIH and an existing
522 * CID we do connection reinstatement. Currently we dont not
523 * support it so we send back an non-zero status class to the
524 * initiator and release the new connection.
526 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
528 pr_err("Connection exists with CID %hu for %s,"
529 " performing connection reinstatement.\n",
530 conn_ptr->cid, sess->sess_ops->InitiatorName);
532 iscsit_connection_reinstatement_rcfr(conn_ptr);
533 iscsit_dec_conn_usage_count(conn_ptr);
537 * Check for any connection recovery entires containing CID.
538 * We use the original ExpStatSN sent in the first login request
539 * to acknowledge commands for the failed connection.
541 * Also note that an explict logout may have already been sent,
542 * but the response may not be sent due to additional connection
545 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
546 cr = iscsit_get_inactive_connection_recovery_entry(
549 pr_debug("Performing implicit logout"
550 " for connection recovery on CID: %hu\n",
552 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
557 * Else we follow item 4 from the login table in that we have
558 * found an existing ISID and a valid/existing TSIH and a new
559 * CID we go ahead and continue to add a new connection to the
562 pr_debug("Adding CID %hu to existing session for %s.\n",
563 cid, sess->sess_ops->InitiatorName);
565 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
566 pr_err("Adding additional connection to this session"
567 " would exceed MaxConnections %d, login failed.\n",
568 sess->sess_ops->MaxConnections);
569 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
570 ISCSI_LOGIN_STATUS_ISID_ERROR);
577 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
579 struct iscsi_session *sess = conn->sess;
581 if (!sess->sess_ops->SessionType)
582 iscsit_start_nopin_timer(conn);
585 static int iscsi_post_login_handler(
587 struct iscsi_conn *conn,
591 struct iscsi_session *sess = conn->sess;
592 struct se_session *se_sess = sess->se_sess;
593 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
594 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
595 struct iscsi_thread_set *ts;
597 iscsit_inc_conn_usage_count(conn);
599 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
600 ISCSI_LOGIN_STATUS_ACCEPT);
602 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
603 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
605 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
606 iscsit_set_sync_and_steering_values(conn);
608 * SCSI Initiator -> SCSI Target Port Mapping
610 ts = iscsi_get_thread_set();
612 iscsi_set_session_parameters(sess->sess_ops,
613 conn->param_list, 0);
614 iscsi_release_param_list(conn->param_list);
615 conn->param_list = NULL;
617 spin_lock_bh(&sess->conn_lock);
618 atomic_set(&sess->session_continuation, 0);
619 if (sess->session_state == TARG_SESS_STATE_FAILED) {
621 " TARG_SESS_STATE_LOGGED_IN.\n");
622 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
626 pr_debug("iSCSI Login successful on CID: %hu from %s to"
627 " %s:%hu,%hu\n", conn->cid, conn->login_ip,
628 conn->local_ip, conn->local_port, tpg->tpgt);
630 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
631 atomic_inc(&sess->nconn);
632 pr_debug("Incremented iSCSI Connection count to %hu"
633 " from node: %s\n", atomic_read(&sess->nconn),
634 sess->sess_ops->InitiatorName);
635 spin_unlock_bh(&sess->conn_lock);
637 iscsi_post_login_start_timers(conn);
638 iscsi_activate_thread_set(conn, ts);
640 * Determine CPU mask to ensure connection's RX and TX kthreads
641 * are scheduled on the same CPU.
643 iscsit_thread_get_cpumask(conn);
644 conn->conn_rx_reset_cpumask = 1;
645 conn->conn_tx_reset_cpumask = 1;
647 iscsit_dec_conn_usage_count(conn);
649 spin_lock_bh(&se_tpg->session_lock);
650 iscsit_stop_time2retain_timer(sess);
651 spin_unlock_bh(&se_tpg->session_lock);
653 iscsit_dec_session_usage_count(sess);
657 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
658 iscsi_release_param_list(conn->param_list);
659 conn->param_list = NULL;
661 iscsit_determine_maxcmdsn(sess);
663 spin_lock_bh(&se_tpg->session_lock);
664 __transport_register_session(&sess->tpg->tpg_se_tpg,
665 se_sess->se_node_acl, se_sess, sess);
666 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
667 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
669 pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
670 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
673 spin_lock_bh(&sess->conn_lock);
674 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
675 atomic_inc(&sess->nconn);
676 pr_debug("Incremented iSCSI Connection count to %hu from node:"
677 " %s\n", atomic_read(&sess->nconn),
678 sess->sess_ops->InitiatorName);
679 spin_unlock_bh(&sess->conn_lock);
681 sess->sid = tpg->sid++;
683 sess->sid = tpg->sid++;
684 pr_debug("Established iSCSI session from node: %s\n",
685 sess->sess_ops->InitiatorName);
689 tpg->tpg_tiqn->tiqn_nsessions++;
691 pr_debug("Incremented number of active iSCSI sessions to %u on"
692 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
693 spin_unlock_bh(&se_tpg->session_lock);
695 iscsi_post_login_start_timers(conn);
696 iscsi_activate_thread_set(conn, ts);
698 * Determine CPU mask to ensure connection's RX and TX kthreads
699 * are scheduled on the same CPU.
701 iscsit_thread_get_cpumask(conn);
702 conn->conn_rx_reset_cpumask = 1;
703 conn->conn_tx_reset_cpumask = 1;
705 iscsit_dec_conn_usage_count(conn);
710 static void iscsi_handle_login_thread_timeout(unsigned long data)
712 struct iscsi_np *np = (struct iscsi_np *) data;
714 spin_lock_bh(&np->np_thread_lock);
715 pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
716 np->np_ip, np->np_port);
718 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
719 spin_unlock_bh(&np->np_thread_lock);
724 send_sig(SIGINT, np->np_thread, 1);
726 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
727 spin_unlock_bh(&np->np_thread_lock);
730 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
733 * This used the TA_LOGIN_TIMEOUT constant because at this
734 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
736 spin_lock_bh(&np->np_thread_lock);
737 init_timer(&np->np_login_timer);
738 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
739 np->np_login_timer.data = (unsigned long)np;
740 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
741 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
742 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
743 add_timer(&np->np_login_timer);
745 pr_debug("Added timeout timer to iSCSI login request for"
746 " %u seconds.\n", TA_LOGIN_TIMEOUT);
747 spin_unlock_bh(&np->np_thread_lock);
750 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
752 spin_lock_bh(&np->np_thread_lock);
753 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
754 spin_unlock_bh(&np->np_thread_lock);
757 np->np_login_timer_flags |= ISCSI_TF_STOP;
758 spin_unlock_bh(&np->np_thread_lock);
760 del_timer_sync(&np->np_login_timer);
762 spin_lock_bh(&np->np_thread_lock);
763 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
764 spin_unlock_bh(&np->np_thread_lock);
767 int iscsi_target_setup_login_socket(
769 struct __kernel_sockaddr_storage *sockaddr)
772 int backlog = 5, ret, opt = 0, len;
774 switch (np->np_network_transport) {
776 np->np_ip_proto = IPPROTO_TCP;
777 np->np_sock_type = SOCK_STREAM;
780 np->np_ip_proto = IPPROTO_SCTP;
781 np->np_sock_type = SOCK_STREAM;
784 np->np_ip_proto = IPPROTO_SCTP;
785 np->np_sock_type = SOCK_SEQPACKET;
787 case ISCSI_IWARP_TCP:
788 case ISCSI_IWARP_SCTP:
789 case ISCSI_INFINIBAND:
791 pr_err("Unsupported network_transport: %d\n",
792 np->np_network_transport);
796 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
797 np->np_ip_proto, &sock);
799 pr_err("sock_create() failed.\n");
802 np->np_socket = sock;
804 * Setup the np->np_sockaddr from the passed sockaddr setup
805 * in iscsi_target_configfs.c code..
807 memcpy(&np->np_sockaddr, sockaddr,
808 sizeof(struct __kernel_sockaddr_storage));
810 if (sockaddr->ss_family == AF_INET6)
811 len = sizeof(struct sockaddr_in6);
813 len = sizeof(struct sockaddr_in);
815 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
817 /* FIXME: Someone please explain why this is endian-safe */
819 if (np->np_network_transport == ISCSI_TCP) {
820 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
821 (char *)&opt, sizeof(opt));
823 pr_err("kernel_setsockopt() for TCP_NODELAY"
824 " failed: %d\n", ret);
829 /* FIXME: Someone please explain why this is endian-safe */
830 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
831 (char *)&opt, sizeof(opt));
833 pr_err("kernel_setsockopt() for SO_REUSEADDR"
838 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
839 (char *)&opt, sizeof(opt));
841 pr_err("kernel_setsockopt() for IP_FREEBIND"
846 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
848 pr_err("kernel_bind() failed: %d\n", ret);
852 ret = kernel_listen(sock, backlog);
854 pr_err("kernel_listen() failed: %d\n", ret);
861 np->np_socket = NULL;
867 static int __iscsi_target_login_thread(struct iscsi_np *np)
869 u8 buffer[ISCSI_HDR_LEN], iscsi_opcode, zero_tsih = 0;
870 int err, ret = 0, stop;
871 struct iscsi_conn *conn = NULL;
872 struct iscsi_login *login;
873 struct iscsi_portal_group *tpg = NULL;
874 struct socket *new_sock, *sock;
876 struct iscsi_login_req *pdu;
877 struct sockaddr_in sock_in;
878 struct sockaddr_in6 sock_in6;
880 flush_signals(current);
881 sock = np->np_socket;
883 spin_lock_bh(&np->np_thread_lock);
884 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
885 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
886 complete(&np->np_restart_comp);
888 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
890 spin_unlock_bh(&np->np_thread_lock);
892 if (kernel_accept(sock, &new_sock, 0) < 0) {
893 spin_lock_bh(&np->np_thread_lock);
894 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
895 spin_unlock_bh(&np->np_thread_lock);
896 complete(&np->np_restart_comp);
897 /* Get another socket */
900 spin_unlock_bh(&np->np_thread_lock);
903 iscsi_start_login_thread_timer(np);
905 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
907 pr_err("Could not allocate memory for"
908 " new connection\n");
909 sock_release(new_sock);
910 /* Get another socket */
914 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
915 conn->conn_state = TARG_CONN_STATE_FREE;
916 conn->sock = new_sock;
918 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
919 conn->conn_state = TARG_CONN_STATE_XPT_UP;
922 * Allocate conn->conn_ops early as a failure calling
923 * iscsit_tx_login_rsp() below will call tx_data().
925 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
926 if (!conn->conn_ops) {
927 pr_err("Unable to allocate memory for"
928 " struct iscsi_conn_ops.\n");
932 * Perform the remaining iSCSI connection initialization items..
934 if (iscsi_login_init_conn(conn) < 0)
937 memset(buffer, 0, ISCSI_HDR_LEN);
938 memset(&iov, 0, sizeof(struct kvec));
939 iov.iov_base = buffer;
940 iov.iov_len = ISCSI_HDR_LEN;
942 if (rx_data(conn, &iov, 1, ISCSI_HDR_LEN) <= 0) {
943 pr_err("rx_data() returned an error.\n");
947 iscsi_opcode = (buffer[0] & ISCSI_OPCODE_MASK);
948 if (!(iscsi_opcode & ISCSI_OP_LOGIN)) {
949 pr_err("First opcode is not login request,"
950 " failing login request.\n");
954 pdu = (struct iscsi_login_req *) buffer;
957 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
958 * when Status-Class != 0.
960 conn->login_itt = pdu->itt;
962 spin_lock_bh(&np->np_thread_lock);
963 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
964 spin_unlock_bh(&np->np_thread_lock);
965 pr_err("iSCSI Network Portal on %s:%hu currently not"
966 " active.\n", np->np_ip, np->np_port);
967 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
968 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
971 spin_unlock_bh(&np->np_thread_lock);
973 if (np->np_sockaddr.ss_family == AF_INET6) {
974 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
976 if (conn->sock->ops->getname(conn->sock,
977 (struct sockaddr *)&sock_in6, &err, 1) < 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->login_ip, sizeof(conn->login_ip), "%pI6c",
984 &sock_in6.sin6_addr.in6_u);
985 conn->login_port = ntohs(sock_in6.sin6_port);
987 if (conn->sock->ops->getname(conn->sock,
988 (struct sockaddr *)&sock_in6, &err, 0) < 0) {
989 pr_err("sock_ops->getname() failed.\n");
990 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
991 ISCSI_LOGIN_STATUS_TARGET_ERROR);
994 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
995 &sock_in6.sin6_addr.in6_u);
996 conn->local_port = ntohs(sock_in6.sin6_port);
999 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1001 if (conn->sock->ops->getname(conn->sock,
1002 (struct sockaddr *)&sock_in, &err, 1) < 0) {
1003 pr_err("sock_ops->getname() failed.\n");
1004 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1005 ISCSI_LOGIN_STATUS_TARGET_ERROR);
1008 sprintf(conn->login_ip, "%pI4", &sock_in.sin_addr.s_addr);
1009 conn->login_port = ntohs(sock_in.sin_port);
1011 if (conn->sock->ops->getname(conn->sock,
1012 (struct sockaddr *)&sock_in, &err, 0) < 0) {
1013 pr_err("sock_ops->getname() failed.\n");
1014 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1015 ISCSI_LOGIN_STATUS_TARGET_ERROR);
1018 sprintf(conn->local_ip, "%pI4", &sock_in.sin_addr.s_addr);
1019 conn->local_port = ntohs(sock_in.sin_port);
1022 conn->network_transport = np->np_network_transport;
1024 pr_debug("Received iSCSI login request from %s on %s Network"
1025 " Portal %s:%hu\n", conn->login_ip,
1026 (conn->network_transport == ISCSI_TCP) ? "TCP" : "SCTP",
1027 conn->local_ip, conn->local_port);
1029 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1030 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1032 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1033 pdu->min_version) < 0)
1036 zero_tsih = (pdu->tsih == 0x0000);
1039 * This is the leading connection of a new session.
1040 * We wait until after authentication to check for
1041 * session reinstatement.
1043 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1047 * Add a new connection to an existing session.
1048 * We check for a non-existant session in
1049 * iscsi_login_non_zero_tsih_s2() below based
1050 * on ISID/TSIH, but wait until after authentication
1051 * to check for connection reinstatement, etc.
1053 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1058 * This will process the first login request, and call
1059 * iscsi_target_locate_portal(), and return a valid struct iscsi_login.
1061 login = iscsi_target_init_negotiation(np, conn, buffer);
1069 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1074 if (iscsi_login_zero_tsih_s2(conn) < 0) {
1075 iscsi_target_nego_release(login, conn);
1079 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0) {
1080 iscsi_target_nego_release(login, conn);
1085 if (iscsi_target_start_negotiation(login, conn) < 0)
1089 pr_err("struct iscsi_conn session pointer is NULL!\n");
1093 iscsi_stop_login_thread_timer(np);
1095 if (signal_pending(current))
1098 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1103 iscsit_deaccess_np(np, tpg);
1105 /* Get another socket */
1109 pr_err("iSCSI Login negotiation failed.\n");
1110 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1111 ISCSI_LOGIN_STATUS_INIT_ERR);
1112 if (!zero_tsih || !conn->sess)
1114 if (conn->sess->se_sess)
1115 transport_free_session(conn->sess->se_sess);
1116 if (conn->sess->session_index != 0) {
1117 spin_lock_bh(&sess_idr_lock);
1118 idr_remove(&sess_idr, conn->sess->session_index);
1119 spin_unlock_bh(&sess_idr_lock);
1121 if (conn->sess->sess_ops)
1122 kfree(conn->sess->sess_ops);
1126 iscsi_stop_login_thread_timer(np);
1128 * If login negotiation fails check if the Time2Retain timer
1129 * needs to be restarted.
1131 if (!zero_tsih && conn->sess) {
1132 spin_lock_bh(&conn->sess->conn_lock);
1133 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1134 struct se_portal_group *se_tpg =
1135 &ISCSI_TPG_C(conn)->tpg_se_tpg;
1137 atomic_set(&conn->sess->session_continuation, 0);
1138 spin_unlock_bh(&conn->sess->conn_lock);
1139 spin_lock_bh(&se_tpg->session_lock);
1140 iscsit_start_time2retain_handler(conn->sess);
1141 spin_unlock_bh(&se_tpg->session_lock);
1143 spin_unlock_bh(&conn->sess->conn_lock);
1144 iscsit_dec_session_usage_count(conn->sess);
1147 if (!IS_ERR(conn->conn_rx_hash.tfm))
1148 crypto_free_hash(conn->conn_rx_hash.tfm);
1149 if (!IS_ERR(conn->conn_tx_hash.tfm))
1150 crypto_free_hash(conn->conn_tx_hash.tfm);
1152 if (conn->conn_cpumask)
1153 free_cpumask_var(conn->conn_cpumask);
1155 kfree(conn->conn_ops);
1157 if (conn->param_list) {
1158 iscsi_release_param_list(conn->param_list);
1159 conn->param_list = NULL;
1162 sock_release(conn->sock);
1166 iscsit_deaccess_np(np, tpg);
1171 stop = kthread_should_stop();
1172 if (!stop && signal_pending(current)) {
1173 spin_lock_bh(&np->np_thread_lock);
1174 stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN);
1175 spin_unlock_bh(&np->np_thread_lock);
1177 /* Wait for another socket.. */
1181 iscsi_stop_login_thread_timer(np);
1182 spin_lock_bh(&np->np_thread_lock);
1183 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1184 spin_unlock_bh(&np->np_thread_lock);
1188 int iscsi_target_login_thread(void *arg)
1190 struct iscsi_np *np = arg;
1193 allow_signal(SIGINT);
1195 while (!kthread_should_stop()) {
1196 ret = __iscsi_target_login_thread(np);
1198 * We break and exit here unless another sock_accept() call