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 extern struct idr sess_idr
;
43 extern struct mutex auth_id_lock
;
44 extern spinlock_t sess_idr_lock
;
46 static int iscsi_login_init_conn(struct iscsi_conn
*conn
)
48 init_waitqueue_head(&conn
->queues_wq
);
49 INIT_LIST_HEAD(&conn
->conn_list
);
50 INIT_LIST_HEAD(&conn
->conn_cmd_list
);
51 INIT_LIST_HEAD(&conn
->immed_queue_list
);
52 INIT_LIST_HEAD(&conn
->response_queue_list
);
53 init_completion(&conn
->conn_post_wait_comp
);
54 init_completion(&conn
->conn_wait_comp
);
55 init_completion(&conn
->conn_wait_rcfr_comp
);
56 init_completion(&conn
->conn_waiting_on_uc_comp
);
57 init_completion(&conn
->conn_logout_comp
);
58 init_completion(&conn
->rx_half_close_comp
);
59 init_completion(&conn
->tx_half_close_comp
);
60 spin_lock_init(&conn
->cmd_lock
);
61 spin_lock_init(&conn
->conn_usage_lock
);
62 spin_lock_init(&conn
->immed_queue_lock
);
63 spin_lock_init(&conn
->nopin_timer_lock
);
64 spin_lock_init(&conn
->response_queue_lock
);
65 spin_lock_init(&conn
->state_lock
);
67 if (!zalloc_cpumask_var(&conn
->conn_cpumask
, GFP_KERNEL
)) {
68 pr_err("Unable to allocate conn->conn_cpumask\n");
76 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
77 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
79 int iscsi_login_setup_crypto(struct iscsi_conn
*conn
)
82 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
83 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
84 * to software 1x8 byte slicing from crc32c.ko
86 conn
->conn_rx_hash
.flags
= 0;
87 conn
->conn_rx_hash
.tfm
= crypto_alloc_hash("crc32c", 0,
89 if (IS_ERR(conn
->conn_rx_hash
.tfm
)) {
90 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
94 conn
->conn_tx_hash
.flags
= 0;
95 conn
->conn_tx_hash
.tfm
= crypto_alloc_hash("crc32c", 0,
97 if (IS_ERR(conn
->conn_tx_hash
.tfm
)) {
98 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
99 crypto_free_hash(conn
->conn_rx_hash
.tfm
);
106 static int iscsi_login_check_initiator_version(
107 struct iscsi_conn
*conn
,
111 if ((version_max
!= 0x00) || (version_min
!= 0x00)) {
112 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
113 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
114 version_min
, version_max
);
115 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
116 ISCSI_LOGIN_STATUS_NO_VERSION
);
123 int iscsi_check_for_session_reinstatement(struct iscsi_conn
*conn
)
126 struct iscsi_param
*initiatorname_param
= NULL
, *sessiontype_param
= NULL
;
127 struct iscsi_portal_group
*tpg
= conn
->tpg
;
128 struct iscsi_session
*sess
= NULL
, *sess_p
= NULL
;
129 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
130 struct se_session
*se_sess
, *se_sess_tmp
;
132 initiatorname_param
= iscsi_find_param_from_key(
133 INITIATORNAME
, conn
->param_list
);
134 if (!initiatorname_param
)
137 sessiontype_param
= iscsi_find_param_from_key(
138 SESSIONTYPE
, conn
->param_list
);
139 if (!sessiontype_param
)
142 sessiontype
= (strncmp(sessiontype_param
->value
, NORMAL
, 6)) ? 1 : 0;
144 spin_lock_bh(&se_tpg
->session_lock
);
145 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
148 sess_p
= se_sess
->fabric_sess_ptr
;
149 spin_lock(&sess_p
->conn_lock
);
150 if (atomic_read(&sess_p
->session_fall_back_to_erl0
) ||
151 atomic_read(&sess_p
->session_logout
) ||
152 (sess_p
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
)) {
153 spin_unlock(&sess_p
->conn_lock
);
156 if (!memcmp(sess_p
->isid
, conn
->sess
->isid
, 6) &&
157 (!strcmp(sess_p
->sess_ops
->InitiatorName
,
158 initiatorname_param
->value
) &&
159 (sess_p
->sess_ops
->SessionType
== sessiontype
))) {
160 atomic_set(&sess_p
->session_reinstatement
, 1);
161 spin_unlock(&sess_p
->conn_lock
);
162 iscsit_inc_session_usage_count(sess_p
);
163 iscsit_stop_time2retain_timer(sess_p
);
167 spin_unlock(&sess_p
->conn_lock
);
169 spin_unlock_bh(&se_tpg
->session_lock
);
171 * If the Time2Retain handler has expired, the session is already gone.
176 pr_debug("%s iSCSI Session SID %u is still active for %s,"
177 " preforming session reinstatement.\n", (sessiontype
) ?
178 "Discovery" : "Normal", sess
->sid
,
179 sess
->sess_ops
->InitiatorName
);
181 spin_lock_bh(&sess
->conn_lock
);
182 if (sess
->session_state
== TARG_SESS_STATE_FAILED
) {
183 spin_unlock_bh(&sess
->conn_lock
);
184 iscsit_dec_session_usage_count(sess
);
185 target_put_session(sess
->se_sess
);
188 spin_unlock_bh(&sess
->conn_lock
);
190 iscsit_stop_session(sess
, 1, 1);
191 iscsit_dec_session_usage_count(sess
);
193 target_put_session(sess
->se_sess
);
197 static void iscsi_login_set_conn_values(
198 struct iscsi_session
*sess
,
199 struct iscsi_conn
*conn
,
205 * Generate a random Status sequence number (statsn) for the new
208 get_random_bytes(&conn
->stat_sn
, sizeof(u32
));
210 mutex_lock(&auth_id_lock
);
211 conn
->auth_id
= iscsit_global
->auth_id
++;
212 mutex_unlock(&auth_id_lock
);
216 * This is the leading connection of a new session,
217 * or session reinstatement.
219 static int iscsi_login_zero_tsih_s1(
220 struct iscsi_conn
*conn
,
223 struct iscsi_session
*sess
= NULL
;
224 struct iscsi_login_req
*pdu
= (struct iscsi_login_req
*)buf
;
227 sess
= kzalloc(sizeof(struct iscsi_session
), GFP_KERNEL
);
229 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
230 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
231 pr_err("Could not allocate memory for session\n");
235 iscsi_login_set_conn_values(sess
, conn
, pdu
->cid
);
236 sess
->init_task_tag
= pdu
->itt
;
237 memcpy(&sess
->isid
, pdu
->isid
, 6);
238 sess
->exp_cmd_sn
= pdu
->cmdsn
;
239 INIT_LIST_HEAD(&sess
->sess_conn_list
);
240 INIT_LIST_HEAD(&sess
->sess_ooo_cmdsn_list
);
241 INIT_LIST_HEAD(&sess
->cr_active_list
);
242 INIT_LIST_HEAD(&sess
->cr_inactive_list
);
243 init_completion(&sess
->async_msg_comp
);
244 init_completion(&sess
->reinstatement_comp
);
245 init_completion(&sess
->session_wait_comp
);
246 init_completion(&sess
->session_waiting_on_uc_comp
);
247 mutex_init(&sess
->cmdsn_mutex
);
248 spin_lock_init(&sess
->conn_lock
);
249 spin_lock_init(&sess
->cr_a_lock
);
250 spin_lock_init(&sess
->cr_i_lock
);
251 spin_lock_init(&sess
->session_usage_lock
);
252 spin_lock_init(&sess
->ttt_lock
);
254 if (!idr_pre_get(&sess_idr
, GFP_KERNEL
)) {
255 pr_err("idr_pre_get() for sess_idr failed\n");
256 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
257 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
261 spin_lock(&sess_idr_lock
);
262 ret
= idr_get_new(&sess_idr
, NULL
, &sess
->session_index
);
263 spin_unlock(&sess_idr_lock
);
266 pr_err("idr_get_new() for sess_idr failed\n");
267 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
268 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
273 sess
->creation_time
= get_jiffies_64();
274 spin_lock_init(&sess
->session_stats_lock
);
276 * The FFP CmdSN window values will be allocated from the TPG's
277 * Initiator Node's ACL once the login has been successfully completed.
279 sess
->max_cmd_sn
= pdu
->cmdsn
;
281 sess
->sess_ops
= kzalloc(sizeof(struct iscsi_sess_ops
), GFP_KERNEL
);
282 if (!sess
->sess_ops
) {
283 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
284 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
285 pr_err("Unable to allocate memory for"
286 " struct iscsi_sess_ops.\n");
291 sess
->se_sess
= transport_init_session();
292 if (IS_ERR(sess
->se_sess
)) {
293 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
294 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
302 static int iscsi_login_zero_tsih_s2(
303 struct iscsi_conn
*conn
)
305 struct iscsi_node_attrib
*na
;
306 struct iscsi_session
*sess
= conn
->sess
;
307 unsigned char buf
[32];
309 sess
->tpg
= conn
->tpg
;
312 * Assign a new TPG Session Handle. Note this is protected with
313 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
315 sess
->tsih
= ++ISCSI_TPG_S(sess
)->ntsih
;
317 sess
->tsih
= ++ISCSI_TPG_S(sess
)->ntsih
;
320 * Create the default params from user defined values..
322 if (iscsi_copy_param_list(&conn
->param_list
,
323 ISCSI_TPG_C(conn
)->param_list
, 1) < 0) {
324 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
325 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
329 iscsi_set_keys_to_negotiate(0, conn
->param_list
);
331 if (sess
->sess_ops
->SessionType
)
332 return iscsi_set_keys_irrelevant_for_discovery(
335 na
= iscsit_tpg_get_node_attrib(sess
);
338 * Need to send TargetPortalGroupTag back in first login response
339 * on any iSCSI connection where the Initiator provides TargetName.
340 * See 5.3.1. Login Phase Start
342 * In our case, we have already located the struct iscsi_tiqn at this point.
345 sprintf(buf
, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess
)->tpgt
);
346 if (iscsi_change_param_value(buf
, conn
->param_list
, 0) < 0) {
347 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
348 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
353 * Workaround for Initiators that have broken connection recovery logic.
355 * "We would really like to get rid of this." Linux-iSCSI.org team
358 sprintf(buf
, "ErrorRecoveryLevel=%d", na
->default_erl
);
359 if (iscsi_change_param_value(buf
, conn
->param_list
, 0) < 0) {
360 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
361 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
365 if (iscsi_login_disable_FIM_keys(conn
->param_list
, conn
) < 0)
372 * Remove PSTATE_NEGOTIATE for the four FIM related keys.
373 * The Initiator node will be able to enable FIM by proposing them itself.
375 int iscsi_login_disable_FIM_keys(
376 struct iscsi_param_list
*param_list
,
377 struct iscsi_conn
*conn
)
379 struct iscsi_param
*param
;
381 param
= iscsi_find_param_from_key("OFMarker", param_list
);
383 pr_err("iscsi_find_param_from_key() for"
384 " OFMarker failed\n");
385 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
386 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
389 param
->state
&= ~PSTATE_NEGOTIATE
;
391 param
= iscsi_find_param_from_key("OFMarkInt", param_list
);
393 pr_err("iscsi_find_param_from_key() for"
394 " IFMarker failed\n");
395 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
396 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
399 param
->state
&= ~PSTATE_NEGOTIATE
;
401 param
= iscsi_find_param_from_key("IFMarker", param_list
);
403 pr_err("iscsi_find_param_from_key() for"
404 " IFMarker failed\n");
405 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
406 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
409 param
->state
&= ~PSTATE_NEGOTIATE
;
411 param
= iscsi_find_param_from_key("IFMarkInt", param_list
);
413 pr_err("iscsi_find_param_from_key() for"
414 " IFMarker failed\n");
415 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
416 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
419 param
->state
&= ~PSTATE_NEGOTIATE
;
424 static int iscsi_login_non_zero_tsih_s1(
425 struct iscsi_conn
*conn
,
428 struct iscsi_login_req
*pdu
= (struct iscsi_login_req
*)buf
;
430 iscsi_login_set_conn_values(NULL
, conn
, pdu
->cid
);
435 * Add a new connection to an existing session.
437 static int iscsi_login_non_zero_tsih_s2(
438 struct iscsi_conn
*conn
,
441 struct iscsi_portal_group
*tpg
= conn
->tpg
;
442 struct iscsi_session
*sess
= NULL
, *sess_p
= NULL
;
443 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
444 struct se_session
*se_sess
, *se_sess_tmp
;
445 struct iscsi_login_req
*pdu
= (struct iscsi_login_req
*)buf
;
447 spin_lock_bh(&se_tpg
->session_lock
);
448 list_for_each_entry_safe(se_sess
, se_sess_tmp
, &se_tpg
->tpg_sess_list
,
451 sess_p
= (struct iscsi_session
*)se_sess
->fabric_sess_ptr
;
452 if (atomic_read(&sess_p
->session_fall_back_to_erl0
) ||
453 atomic_read(&sess_p
->session_logout
) ||
454 (sess_p
->time2retain_timer_flags
& ISCSI_TF_EXPIRED
))
456 if (!memcmp(sess_p
->isid
, pdu
->isid
, 6) &&
457 (sess_p
->tsih
== pdu
->tsih
)) {
458 iscsit_inc_session_usage_count(sess_p
);
459 iscsit_stop_time2retain_timer(sess_p
);
464 spin_unlock_bh(&se_tpg
->session_lock
);
467 * If the Time2Retain handler has expired, the session is already gone.
470 pr_err("Initiator attempting to add a connection to"
471 " a non-existent session, rejecting iSCSI Login.\n");
472 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
473 ISCSI_LOGIN_STATUS_NO_SESSION
);
478 * Stop the Time2Retain timer if this is a failed session, we restart
479 * the timer if the login is not successful.
481 spin_lock_bh(&sess
->conn_lock
);
482 if (sess
->session_state
== TARG_SESS_STATE_FAILED
)
483 atomic_set(&sess
->session_continuation
, 1);
484 spin_unlock_bh(&sess
->conn_lock
);
486 iscsi_login_set_conn_values(sess
, conn
, pdu
->cid
);
488 if (iscsi_copy_param_list(&conn
->param_list
,
489 ISCSI_TPG_C(conn
)->param_list
, 0) < 0) {
490 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
491 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
495 iscsi_set_keys_to_negotiate(0, conn
->param_list
);
497 * Need to send TargetPortalGroupTag back in first login response
498 * on any iSCSI connection where the Initiator provides TargetName.
499 * See 5.3.1. Login Phase Start
501 * In our case, we have already located the struct iscsi_tiqn at this point.
504 sprintf(buf
, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess
)->tpgt
);
505 if (iscsi_change_param_value(buf
, conn
->param_list
, 0) < 0) {
506 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
507 ISCSI_LOGIN_STATUS_NO_RESOURCES
);
511 return iscsi_login_disable_FIM_keys(conn
->param_list
, conn
);
514 int iscsi_login_post_auth_non_zero_tsih(
515 struct iscsi_conn
*conn
,
519 struct iscsi_conn
*conn_ptr
= NULL
;
520 struct iscsi_conn_recovery
*cr
= NULL
;
521 struct iscsi_session
*sess
= conn
->sess
;
524 * By following item 5 in the login table, if we have found
525 * an existing ISID and a valid/existing TSIH and an existing
526 * CID we do connection reinstatement. Currently we dont not
527 * support it so we send back an non-zero status class to the
528 * initiator and release the new connection.
530 conn_ptr
= iscsit_get_conn_from_cid_rcfr(sess
, cid
);
532 pr_err("Connection exists with CID %hu for %s,"
533 " performing connection reinstatement.\n",
534 conn_ptr
->cid
, sess
->sess_ops
->InitiatorName
);
536 iscsit_connection_reinstatement_rcfr(conn_ptr
);
537 iscsit_dec_conn_usage_count(conn_ptr
);
541 * Check for any connection recovery entires containing CID.
542 * We use the original ExpStatSN sent in the first login request
543 * to acknowledge commands for the failed connection.
545 * Also note that an explict logout may have already been sent,
546 * but the response may not be sent due to additional connection
549 if (sess
->sess_ops
->ErrorRecoveryLevel
== 2) {
550 cr
= iscsit_get_inactive_connection_recovery_entry(
553 pr_debug("Performing implicit logout"
554 " for connection recovery on CID: %hu\n",
556 iscsit_discard_cr_cmds_by_expstatsn(cr
, exp_statsn
);
561 * Else we follow item 4 from the login table in that we have
562 * found an existing ISID and a valid/existing TSIH and a new
563 * CID we go ahead and continue to add a new connection to the
566 pr_debug("Adding CID %hu to existing session for %s.\n",
567 cid
, sess
->sess_ops
->InitiatorName
);
569 if ((atomic_read(&sess
->nconn
) + 1) > sess
->sess_ops
->MaxConnections
) {
570 pr_err("Adding additional connection to this session"
571 " would exceed MaxConnections %d, login failed.\n",
572 sess
->sess_ops
->MaxConnections
);
573 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
574 ISCSI_LOGIN_STATUS_ISID_ERROR
);
581 static void iscsi_post_login_start_timers(struct iscsi_conn
*conn
)
583 struct iscsi_session
*sess
= conn
->sess
;
585 if (!sess
->sess_ops
->SessionType
)
586 iscsit_start_nopin_timer(conn
);
589 static int iscsi_post_login_handler(
591 struct iscsi_conn
*conn
,
595 struct iscsi_session
*sess
= conn
->sess
;
596 struct se_session
*se_sess
= sess
->se_sess
;
597 struct iscsi_portal_group
*tpg
= ISCSI_TPG_S(sess
);
598 struct se_portal_group
*se_tpg
= &tpg
->tpg_se_tpg
;
599 struct iscsi_thread_set
*ts
;
601 iscsit_inc_conn_usage_count(conn
);
603 iscsit_collect_login_stats(conn
, ISCSI_STATUS_CLS_SUCCESS
,
604 ISCSI_LOGIN_STATUS_ACCEPT
);
606 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
607 conn
->conn_state
= TARG_CONN_STATE_LOGGED_IN
;
609 iscsi_set_connection_parameters(conn
->conn_ops
, conn
->param_list
);
610 iscsit_set_sync_and_steering_values(conn
);
612 * SCSI Initiator -> SCSI Target Port Mapping
614 ts
= iscsi_get_thread_set();
616 iscsi_set_session_parameters(sess
->sess_ops
,
617 conn
->param_list
, 0);
618 iscsi_release_param_list(conn
->param_list
);
619 conn
->param_list
= NULL
;
621 spin_lock_bh(&sess
->conn_lock
);
622 atomic_set(&sess
->session_continuation
, 0);
623 if (sess
->session_state
== TARG_SESS_STATE_FAILED
) {
625 " TARG_SESS_STATE_LOGGED_IN.\n");
626 sess
->session_state
= TARG_SESS_STATE_LOGGED_IN
;
630 pr_debug("iSCSI Login successful on CID: %hu from %s to"
631 " %s:%hu,%hu\n", conn
->cid
, conn
->login_ip
,
632 conn
->local_ip
, conn
->local_port
, tpg
->tpgt
);
634 list_add_tail(&conn
->conn_list
, &sess
->sess_conn_list
);
635 atomic_inc(&sess
->nconn
);
636 pr_debug("Incremented iSCSI Connection count to %hu"
637 " from node: %s\n", atomic_read(&sess
->nconn
),
638 sess
->sess_ops
->InitiatorName
);
639 spin_unlock_bh(&sess
->conn_lock
);
641 iscsi_post_login_start_timers(conn
);
642 iscsi_activate_thread_set(conn
, ts
);
644 * Determine CPU mask to ensure connection's RX and TX kthreads
645 * are scheduled on the same CPU.
647 iscsit_thread_get_cpumask(conn
);
648 conn
->conn_rx_reset_cpumask
= 1;
649 conn
->conn_tx_reset_cpumask
= 1;
651 iscsit_dec_conn_usage_count(conn
);
653 spin_lock_bh(&se_tpg
->session_lock
);
654 iscsit_stop_time2retain_timer(sess
);
655 spin_unlock_bh(&se_tpg
->session_lock
);
657 iscsit_dec_session_usage_count(sess
);
661 iscsi_set_session_parameters(sess
->sess_ops
, conn
->param_list
, 1);
662 iscsi_release_param_list(conn
->param_list
);
663 conn
->param_list
= NULL
;
665 iscsit_determine_maxcmdsn(sess
);
667 spin_lock_bh(&se_tpg
->session_lock
);
668 __transport_register_session(&sess
->tpg
->tpg_se_tpg
,
669 se_sess
->se_node_acl
, se_sess
, sess
);
670 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
671 sess
->session_state
= TARG_SESS_STATE_LOGGED_IN
;
673 pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
674 conn
->cid
, conn
->login_ip
, conn
->local_ip
, conn
->local_port
,
677 spin_lock_bh(&sess
->conn_lock
);
678 list_add_tail(&conn
->conn_list
, &sess
->sess_conn_list
);
679 atomic_inc(&sess
->nconn
);
680 pr_debug("Incremented iSCSI Connection count to %hu from node:"
681 " %s\n", atomic_read(&sess
->nconn
),
682 sess
->sess_ops
->InitiatorName
);
683 spin_unlock_bh(&sess
->conn_lock
);
685 sess
->sid
= tpg
->sid
++;
687 sess
->sid
= tpg
->sid
++;
688 pr_debug("Established iSCSI session from node: %s\n",
689 sess
->sess_ops
->InitiatorName
);
693 tpg
->tpg_tiqn
->tiqn_nsessions
++;
695 pr_debug("Incremented number of active iSCSI sessions to %u on"
696 " iSCSI Target Portal Group: %hu\n", tpg
->nsessions
, tpg
->tpgt
);
697 spin_unlock_bh(&se_tpg
->session_lock
);
699 iscsi_post_login_start_timers(conn
);
700 iscsi_activate_thread_set(conn
, ts
);
702 * Determine CPU mask to ensure connection's RX and TX kthreads
703 * are scheduled on the same CPU.
705 iscsit_thread_get_cpumask(conn
);
706 conn
->conn_rx_reset_cpumask
= 1;
707 conn
->conn_tx_reset_cpumask
= 1;
709 iscsit_dec_conn_usage_count(conn
);
714 static void iscsi_handle_login_thread_timeout(unsigned long data
)
716 struct iscsi_np
*np
= (struct iscsi_np
*) data
;
718 spin_lock_bh(&np
->np_thread_lock
);
719 pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
720 np
->np_ip
, np
->np_port
);
722 if (np
->np_login_timer_flags
& ISCSI_TF_STOP
) {
723 spin_unlock_bh(&np
->np_thread_lock
);
728 send_sig(SIGINT
, np
->np_thread
, 1);
730 np
->np_login_timer_flags
&= ~ISCSI_TF_RUNNING
;
731 spin_unlock_bh(&np
->np_thread_lock
);
734 static void iscsi_start_login_thread_timer(struct iscsi_np
*np
)
737 * This used the TA_LOGIN_TIMEOUT constant because at this
738 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
740 spin_lock_bh(&np
->np_thread_lock
);
741 init_timer(&np
->np_login_timer
);
742 np
->np_login_timer
.expires
= (get_jiffies_64() + TA_LOGIN_TIMEOUT
* HZ
);
743 np
->np_login_timer
.data
= (unsigned long)np
;
744 np
->np_login_timer
.function
= iscsi_handle_login_thread_timeout
;
745 np
->np_login_timer_flags
&= ~ISCSI_TF_STOP
;
746 np
->np_login_timer_flags
|= ISCSI_TF_RUNNING
;
747 add_timer(&np
->np_login_timer
);
749 pr_debug("Added timeout timer to iSCSI login request for"
750 " %u seconds.\n", TA_LOGIN_TIMEOUT
);
751 spin_unlock_bh(&np
->np_thread_lock
);
754 static void iscsi_stop_login_thread_timer(struct iscsi_np
*np
)
756 spin_lock_bh(&np
->np_thread_lock
);
757 if (!(np
->np_login_timer_flags
& ISCSI_TF_RUNNING
)) {
758 spin_unlock_bh(&np
->np_thread_lock
);
761 np
->np_login_timer_flags
|= ISCSI_TF_STOP
;
762 spin_unlock_bh(&np
->np_thread_lock
);
764 del_timer_sync(&np
->np_login_timer
);
766 spin_lock_bh(&np
->np_thread_lock
);
767 np
->np_login_timer_flags
&= ~ISCSI_TF_RUNNING
;
768 spin_unlock_bh(&np
->np_thread_lock
);
771 int iscsi_target_setup_login_socket(
773 struct __kernel_sockaddr_storage
*sockaddr
)
776 int backlog
= 5, ret
, opt
= 0, len
;
778 switch (np
->np_network_transport
) {
780 np
->np_ip_proto
= IPPROTO_TCP
;
781 np
->np_sock_type
= SOCK_STREAM
;
784 np
->np_ip_proto
= IPPROTO_SCTP
;
785 np
->np_sock_type
= SOCK_STREAM
;
788 np
->np_ip_proto
= IPPROTO_SCTP
;
789 np
->np_sock_type
= SOCK_SEQPACKET
;
791 case ISCSI_IWARP_TCP
:
792 case ISCSI_IWARP_SCTP
:
793 case ISCSI_INFINIBAND
:
795 pr_err("Unsupported network_transport: %d\n",
796 np
->np_network_transport
);
800 ret
= sock_create(sockaddr
->ss_family
, np
->np_sock_type
,
801 np
->np_ip_proto
, &sock
);
803 pr_err("sock_create() failed.\n");
806 np
->np_socket
= sock
;
808 * Setup the np->np_sockaddr from the passed sockaddr setup
809 * in iscsi_target_configfs.c code..
811 memcpy(&np
->np_sockaddr
, sockaddr
,
812 sizeof(struct __kernel_sockaddr_storage
));
814 if (sockaddr
->ss_family
== AF_INET6
)
815 len
= sizeof(struct sockaddr_in6
);
817 len
= sizeof(struct sockaddr_in
);
819 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
821 /* FIXME: Someone please explain why this is endian-safe */
823 if (np
->np_network_transport
== ISCSI_TCP
) {
824 ret
= kernel_setsockopt(sock
, IPPROTO_TCP
, TCP_NODELAY
,
825 (char *)&opt
, sizeof(opt
));
827 pr_err("kernel_setsockopt() for TCP_NODELAY"
828 " failed: %d\n", ret
);
833 /* FIXME: Someone please explain why this is endian-safe */
834 ret
= kernel_setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
,
835 (char *)&opt
, sizeof(opt
));
837 pr_err("kernel_setsockopt() for SO_REUSEADDR"
842 ret
= kernel_setsockopt(sock
, IPPROTO_IP
, IP_FREEBIND
,
843 (char *)&opt
, sizeof(opt
));
845 pr_err("kernel_setsockopt() for IP_FREEBIND"
850 ret
= kernel_bind(sock
, (struct sockaddr
*)&np
->np_sockaddr
, len
);
852 pr_err("kernel_bind() failed: %d\n", ret
);
856 ret
= kernel_listen(sock
, backlog
);
858 pr_err("kernel_listen() failed: %d\n", ret
);
865 np
->np_socket
= NULL
;
871 static int __iscsi_target_login_thread(struct iscsi_np
*np
)
873 u8 buffer
[ISCSI_HDR_LEN
], iscsi_opcode
, zero_tsih
= 0;
874 int err
, ret
= 0, stop
;
875 struct iscsi_conn
*conn
= NULL
;
876 struct iscsi_login
*login
;
877 struct iscsi_portal_group
*tpg
= NULL
;
878 struct socket
*new_sock
, *sock
;
880 struct iscsi_login_req
*pdu
;
881 struct sockaddr_in sock_in
;
882 struct sockaddr_in6 sock_in6
;
884 flush_signals(current
);
885 sock
= np
->np_socket
;
887 spin_lock_bh(&np
->np_thread_lock
);
888 if (np
->np_thread_state
== ISCSI_NP_THREAD_RESET
) {
889 np
->np_thread_state
= ISCSI_NP_THREAD_ACTIVE
;
890 complete(&np
->np_restart_comp
);
892 np
->np_thread_state
= ISCSI_NP_THREAD_ACTIVE
;
894 spin_unlock_bh(&np
->np_thread_lock
);
896 if (kernel_accept(sock
, &new_sock
, 0) < 0) {
897 spin_lock_bh(&np
->np_thread_lock
);
898 if (np
->np_thread_state
== ISCSI_NP_THREAD_RESET
) {
899 spin_unlock_bh(&np
->np_thread_lock
);
900 complete(&np
->np_restart_comp
);
901 /* Get another socket */
904 spin_unlock_bh(&np
->np_thread_lock
);
907 iscsi_start_login_thread_timer(np
);
909 conn
= kzalloc(sizeof(struct iscsi_conn
), GFP_KERNEL
);
911 pr_err("Could not allocate memory for"
912 " new connection\n");
913 sock_release(new_sock
);
914 /* Get another socket */
918 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
919 conn
->conn_state
= TARG_CONN_STATE_FREE
;
920 conn
->sock
= new_sock
;
922 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
923 conn
->conn_state
= TARG_CONN_STATE_XPT_UP
;
926 * Allocate conn->conn_ops early as a failure calling
927 * iscsit_tx_login_rsp() below will call tx_data().
929 conn
->conn_ops
= kzalloc(sizeof(struct iscsi_conn_ops
), GFP_KERNEL
);
930 if (!conn
->conn_ops
) {
931 pr_err("Unable to allocate memory for"
932 " struct iscsi_conn_ops.\n");
936 * Perform the remaining iSCSI connection initialization items..
938 if (iscsi_login_init_conn(conn
) < 0)
941 memset(buffer
, 0, ISCSI_HDR_LEN
);
942 memset(&iov
, 0, sizeof(struct kvec
));
943 iov
.iov_base
= buffer
;
944 iov
.iov_len
= ISCSI_HDR_LEN
;
946 if (rx_data(conn
, &iov
, 1, ISCSI_HDR_LEN
) <= 0) {
947 pr_err("rx_data() returned an error.\n");
951 iscsi_opcode
= (buffer
[0] & ISCSI_OPCODE_MASK
);
952 if (!(iscsi_opcode
& ISCSI_OP_LOGIN
)) {
953 pr_err("First opcode is not login request,"
954 " failing login request.\n");
958 pdu
= (struct iscsi_login_req
*) buffer
;
959 pdu
->cid
= be16_to_cpu(pdu
->cid
);
960 pdu
->tsih
= be16_to_cpu(pdu
->tsih
);
961 pdu
->itt
= be32_to_cpu(pdu
->itt
);
962 pdu
->cmdsn
= be32_to_cpu(pdu
->cmdsn
);
963 pdu
->exp_statsn
= be32_to_cpu(pdu
->exp_statsn
);
965 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
966 * when Status-Class != 0.
968 conn
->login_itt
= pdu
->itt
;
970 spin_lock_bh(&np
->np_thread_lock
);
971 if (np
->np_thread_state
!= ISCSI_NP_THREAD_ACTIVE
) {
972 spin_unlock_bh(&np
->np_thread_lock
);
973 pr_err("iSCSI Network Portal on %s:%hu currently not"
974 " active.\n", np
->np_ip
, np
->np_port
);
975 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
976 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE
);
979 spin_unlock_bh(&np
->np_thread_lock
);
981 if (np
->np_sockaddr
.ss_family
== AF_INET6
) {
982 memset(&sock_in6
, 0, sizeof(struct sockaddr_in6
));
984 if (conn
->sock
->ops
->getname(conn
->sock
,
985 (struct sockaddr
*)&sock_in6
, &err
, 1) < 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
->login_ip
, sizeof(conn
->login_ip
), "%pI6c",
992 &sock_in6
.sin6_addr
.in6_u
);
993 conn
->login_port
= ntohs(sock_in6
.sin6_port
);
995 if (conn
->sock
->ops
->getname(conn
->sock
,
996 (struct sockaddr
*)&sock_in6
, &err
, 0) < 0) {
997 pr_err("sock_ops->getname() failed.\n");
998 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
999 ISCSI_LOGIN_STATUS_TARGET_ERROR
);
1002 snprintf(conn
->local_ip
, sizeof(conn
->local_ip
), "%pI6c",
1003 &sock_in6
.sin6_addr
.in6_u
);
1004 conn
->local_port
= ntohs(sock_in6
.sin6_port
);
1007 memset(&sock_in
, 0, sizeof(struct sockaddr_in
));
1009 if (conn
->sock
->ops
->getname(conn
->sock
,
1010 (struct sockaddr
*)&sock_in
, &err
, 1) < 0) {
1011 pr_err("sock_ops->getname() failed.\n");
1012 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
1013 ISCSI_LOGIN_STATUS_TARGET_ERROR
);
1016 sprintf(conn
->login_ip
, "%pI4", &sock_in
.sin_addr
.s_addr
);
1017 conn
->login_port
= ntohs(sock_in
.sin_port
);
1019 if (conn
->sock
->ops
->getname(conn
->sock
,
1020 (struct sockaddr
*)&sock_in
, &err
, 0) < 0) {
1021 pr_err("sock_ops->getname() failed.\n");
1022 iscsit_tx_login_rsp(conn
, ISCSI_STATUS_CLS_TARGET_ERR
,
1023 ISCSI_LOGIN_STATUS_TARGET_ERROR
);
1026 sprintf(conn
->local_ip
, "%pI4", &sock_in
.sin_addr
.s_addr
);
1027 conn
->local_port
= ntohs(sock_in
.sin_port
);
1030 conn
->network_transport
= np
->np_network_transport
;
1032 pr_debug("Received iSCSI login request from %s on %s Network"
1033 " Portal %s:%hu\n", conn
->login_ip
,
1034 (conn
->network_transport
== ISCSI_TCP
) ? "TCP" : "SCTP",
1035 conn
->local_ip
, conn
->local_port
);
1037 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1038 conn
->conn_state
= TARG_CONN_STATE_IN_LOGIN
;
1040 if (iscsi_login_check_initiator_version(conn
, pdu
->max_version
,
1041 pdu
->min_version
) < 0)
1044 zero_tsih
= (pdu
->tsih
== 0x0000);
1047 * This is the leading connection of a new session.
1048 * We wait until after authentication to check for
1049 * session reinstatement.
1051 if (iscsi_login_zero_tsih_s1(conn
, buffer
) < 0)
1055 * Add a new connection to an existing session.
1056 * We check for a non-existant session in
1057 * iscsi_login_non_zero_tsih_s2() below based
1058 * on ISID/TSIH, but wait until after authentication
1059 * to check for connection reinstatement, etc.
1061 if (iscsi_login_non_zero_tsih_s1(conn
, buffer
) < 0)
1066 * This will process the first login request, and call
1067 * iscsi_target_locate_portal(), and return a valid struct iscsi_login.
1069 login
= iscsi_target_init_negotiation(np
, conn
, buffer
);
1077 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1082 if (iscsi_login_zero_tsih_s2(conn
) < 0) {
1083 iscsi_target_nego_release(login
, conn
);
1087 if (iscsi_login_non_zero_tsih_s2(conn
, buffer
) < 0) {
1088 iscsi_target_nego_release(login
, conn
);
1093 if (iscsi_target_start_negotiation(login
, conn
) < 0)
1097 pr_err("struct iscsi_conn session pointer is NULL!\n");
1101 iscsi_stop_login_thread_timer(np
);
1103 if (signal_pending(current
))
1106 ret
= iscsi_post_login_handler(np
, conn
, zero_tsih
);
1111 iscsit_deaccess_np(np
, tpg
);
1113 /* Get another socket */
1117 pr_err("iSCSI Login negotiation failed.\n");
1118 iscsit_collect_login_stats(conn
, ISCSI_STATUS_CLS_INITIATOR_ERR
,
1119 ISCSI_LOGIN_STATUS_INIT_ERR
);
1120 if (!zero_tsih
|| !conn
->sess
)
1122 if (conn
->sess
->se_sess
)
1123 transport_free_session(conn
->sess
->se_sess
);
1124 if (conn
->sess
->session_index
!= 0) {
1125 spin_lock_bh(&sess_idr_lock
);
1126 idr_remove(&sess_idr
, conn
->sess
->session_index
);
1127 spin_unlock_bh(&sess_idr_lock
);
1129 if (conn
->sess
->sess_ops
)
1130 kfree(conn
->sess
->sess_ops
);
1134 iscsi_stop_login_thread_timer(np
);
1136 * If login negotiation fails check if the Time2Retain timer
1137 * needs to be restarted.
1139 if (!zero_tsih
&& conn
->sess
) {
1140 spin_lock_bh(&conn
->sess
->conn_lock
);
1141 if (conn
->sess
->session_state
== TARG_SESS_STATE_FAILED
) {
1142 struct se_portal_group
*se_tpg
=
1143 &ISCSI_TPG_C(conn
)->tpg_se_tpg
;
1145 atomic_set(&conn
->sess
->session_continuation
, 0);
1146 spin_unlock_bh(&conn
->sess
->conn_lock
);
1147 spin_lock_bh(&se_tpg
->session_lock
);
1148 iscsit_start_time2retain_handler(conn
->sess
);
1149 spin_unlock_bh(&se_tpg
->session_lock
);
1151 spin_unlock_bh(&conn
->sess
->conn_lock
);
1152 iscsit_dec_session_usage_count(conn
->sess
);
1155 if (!IS_ERR(conn
->conn_rx_hash
.tfm
))
1156 crypto_free_hash(conn
->conn_rx_hash
.tfm
);
1157 if (!IS_ERR(conn
->conn_tx_hash
.tfm
))
1158 crypto_free_hash(conn
->conn_tx_hash
.tfm
);
1160 if (conn
->conn_cpumask
)
1161 free_cpumask_var(conn
->conn_cpumask
);
1163 kfree(conn
->conn_ops
);
1165 if (conn
->param_list
) {
1166 iscsi_release_param_list(conn
->param_list
);
1167 conn
->param_list
= NULL
;
1170 sock_release(conn
->sock
);
1174 iscsit_deaccess_np(np
, tpg
);
1179 stop
= kthread_should_stop();
1180 if (!stop
&& signal_pending(current
)) {
1181 spin_lock_bh(&np
->np_thread_lock
);
1182 stop
= (np
->np_thread_state
== ISCSI_NP_THREAD_SHUTDOWN
);
1183 spin_unlock_bh(&np
->np_thread_lock
);
1185 /* Wait for another socket.. */
1189 iscsi_stop_login_thread_timer(np
);
1190 spin_lock_bh(&np
->np_thread_lock
);
1191 np
->np_thread_state
= ISCSI_NP_THREAD_EXIT
;
1192 spin_unlock_bh(&np
->np_thread_lock
);
1196 int iscsi_target_login_thread(void *arg
)
1198 struct iscsi_np
*np
= arg
;
1201 allow_signal(SIGINT
);
1203 while (!kthread_should_stop()) {
1204 ret
= __iscsi_target_login_thread(np
);
1206 * We break and exit here unless another sock_accept() call