1 /*******************************************************************************
2 * This file contains the login functions used by the iSCSI Target driver.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <linux/string.h>
20 #include <linux/kthread.h>
21 #include <linux/crypto.h>
22 #include <linux/idr.h>
23 #include <scsi/iscsi_proto.h>
24 #include <target/target_core_base.h>
25 #include <target/target_core_fabric.h>
27 #include <target/iscsi/iscsi_target_core.h>
28 #include <target/iscsi/iscsi_target_stat.h>
29 #include "iscsi_target_device.h"
30 #include "iscsi_target_nego.h"
31 #include "iscsi_target_erl0.h"
32 #include "iscsi_target_erl2.h"
33 #include "iscsi_target_login.h"
34 #include "iscsi_target_tpg.h"
35 #include "iscsi_target_util.h"
36 #include "iscsi_target.h"
37 #include "iscsi_target_parameters.h"
39 #include <target/iscsi/iscsi_transport.h>
41 static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
43 struct iscsi_login *login;
45 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
47 pr_err("Unable to allocate memory for struct iscsi_login.\n");
52 login->first_request = 1;
54 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
55 if (!login->req_buf) {
56 pr_err("Unable to allocate memory for response buffer.\n");
60 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
61 if (!login->rsp_buf) {
62 pr_err("Unable to allocate memory for request buffer.\n");
66 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
67 if (!conn->conn_ops) {
68 pr_err("Unable to allocate memory for"
69 " struct iscsi_conn_ops.\n");
73 init_waitqueue_head(&conn->queues_wq);
74 INIT_LIST_HEAD(&conn->conn_list);
75 INIT_LIST_HEAD(&conn->conn_cmd_list);
76 INIT_LIST_HEAD(&conn->immed_queue_list);
77 INIT_LIST_HEAD(&conn->response_queue_list);
78 init_completion(&conn->conn_post_wait_comp);
79 init_completion(&conn->conn_wait_comp);
80 init_completion(&conn->conn_wait_rcfr_comp);
81 init_completion(&conn->conn_waiting_on_uc_comp);
82 init_completion(&conn->conn_logout_comp);
83 init_completion(&conn->rx_half_close_comp);
84 init_completion(&conn->tx_half_close_comp);
85 spin_lock_init(&conn->cmd_lock);
86 spin_lock_init(&conn->conn_usage_lock);
87 spin_lock_init(&conn->immed_queue_lock);
88 spin_lock_init(&conn->nopin_timer_lock);
89 spin_lock_init(&conn->response_queue_lock);
90 spin_lock_init(&conn->state_lock);
92 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
93 pr_err("Unable to allocate conn->conn_cpumask\n");
96 conn->conn_login = login;
101 kfree(conn->conn_ops);
103 kfree(login->rsp_buf);
105 kfree(login->req_buf);
112 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
113 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
115 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
118 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
119 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
120 * to software 1x8 byte slicing from crc32c.ko
122 conn->conn_rx_hash.flags = 0;
123 conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
125 if (IS_ERR(conn->conn_rx_hash.tfm)) {
126 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
130 conn->conn_tx_hash.flags = 0;
131 conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
133 if (IS_ERR(conn->conn_tx_hash.tfm)) {
134 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
135 crypto_free_hash(conn->conn_rx_hash.tfm);
142 static int iscsi_login_check_initiator_version(
143 struct iscsi_conn *conn,
147 if ((version_max != 0x00) || (version_min != 0x00)) {
148 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
149 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
150 version_min, version_max);
151 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
152 ISCSI_LOGIN_STATUS_NO_VERSION);
159 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
162 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
163 struct iscsi_portal_group *tpg = conn->tpg;
164 struct iscsi_session *sess = NULL, *sess_p = NULL;
165 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
166 struct se_session *se_sess, *se_sess_tmp;
168 initiatorname_param = iscsi_find_param_from_key(
169 INITIATORNAME, conn->param_list);
170 sessiontype_param = iscsi_find_param_from_key(
171 SESSIONTYPE, conn->param_list);
172 if (!initiatorname_param || !sessiontype_param) {
173 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
174 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
178 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
180 spin_lock_bh(&se_tpg->session_lock);
181 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
184 sess_p = se_sess->fabric_sess_ptr;
185 spin_lock(&sess_p->conn_lock);
186 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
187 atomic_read(&sess_p->session_logout) ||
188 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
189 spin_unlock(&sess_p->conn_lock);
192 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
193 (!strcmp(sess_p->sess_ops->InitiatorName,
194 initiatorname_param->value) &&
195 (sess_p->sess_ops->SessionType == sessiontype))) {
196 atomic_set(&sess_p->session_reinstatement, 1);
197 spin_unlock(&sess_p->conn_lock);
198 iscsit_inc_session_usage_count(sess_p);
199 iscsit_stop_time2retain_timer(sess_p);
203 spin_unlock(&sess_p->conn_lock);
205 spin_unlock_bh(&se_tpg->session_lock);
207 * If the Time2Retain handler has expired, the session is already gone.
212 pr_debug("%s iSCSI Session SID %u is still active for %s,"
213 " preforming session reinstatement.\n", (sessiontype) ?
214 "Discovery" : "Normal", sess->sid,
215 sess->sess_ops->InitiatorName);
217 spin_lock_bh(&sess->conn_lock);
218 if (sess->session_state == TARG_SESS_STATE_FAILED) {
219 spin_unlock_bh(&sess->conn_lock);
220 iscsit_dec_session_usage_count(sess);
221 target_put_session(sess->se_sess);
224 spin_unlock_bh(&sess->conn_lock);
226 iscsit_stop_session(sess, 1, 1);
227 iscsit_dec_session_usage_count(sess);
229 target_put_session(sess->se_sess);
233 static void iscsi_login_set_conn_values(
234 struct iscsi_session *sess,
235 struct iscsi_conn *conn,
239 conn->cid = be16_to_cpu(cid);
241 * Generate a random Status sequence number (statsn) for the new
244 get_random_bytes(&conn->stat_sn, sizeof(u32));
246 mutex_lock(&auth_id_lock);
247 conn->auth_id = iscsit_global->auth_id++;
248 mutex_unlock(&auth_id_lock);
251 static __printf(2, 3) int iscsi_change_param_sprintf(
252 struct iscsi_conn *conn,
253 const char *fmt, ...)
256 unsigned char buf[64];
258 memset(buf, 0, sizeof buf);
261 vsnprintf(buf, sizeof buf, fmt, args);
264 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
265 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
266 ISCSI_LOGIN_STATUS_NO_RESOURCES);
274 * This is the leading connection of a new session,
275 * or session reinstatement.
277 static int iscsi_login_zero_tsih_s1(
278 struct iscsi_conn *conn,
281 struct iscsi_session *sess = NULL;
282 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
285 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
287 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
288 ISCSI_LOGIN_STATUS_NO_RESOURCES);
289 pr_err("Could not allocate memory for session\n");
293 iscsi_login_set_conn_values(sess, conn, pdu->cid);
294 sess->init_task_tag = pdu->itt;
295 memcpy(&sess->isid, pdu->isid, 6);
296 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
297 INIT_LIST_HEAD(&sess->sess_conn_list);
298 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
299 INIT_LIST_HEAD(&sess->cr_active_list);
300 INIT_LIST_HEAD(&sess->cr_inactive_list);
301 init_completion(&sess->async_msg_comp);
302 init_completion(&sess->reinstatement_comp);
303 init_completion(&sess->session_wait_comp);
304 init_completion(&sess->session_waiting_on_uc_comp);
305 mutex_init(&sess->cmdsn_mutex);
306 spin_lock_init(&sess->conn_lock);
307 spin_lock_init(&sess->cr_a_lock);
308 spin_lock_init(&sess->cr_i_lock);
309 spin_lock_init(&sess->session_usage_lock);
310 spin_lock_init(&sess->ttt_lock);
312 idr_preload(GFP_KERNEL);
313 spin_lock_bh(&sess_idr_lock);
314 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
316 sess->session_index = ret;
317 spin_unlock_bh(&sess_idr_lock);
321 pr_err("idr_alloc() for sess_idr failed\n");
322 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
323 ISCSI_LOGIN_STATUS_NO_RESOURCES);
328 sess->creation_time = get_jiffies_64();
330 * The FFP CmdSN window values will be allocated from the TPG's
331 * Initiator Node's ACL once the login has been successfully completed.
333 atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
335 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
336 if (!sess->sess_ops) {
337 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
338 ISCSI_LOGIN_STATUS_NO_RESOURCES);
339 pr_err("Unable to allocate memory for"
340 " struct iscsi_sess_ops.\n");
345 sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
346 if (IS_ERR(sess->se_sess)) {
347 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
348 ISCSI_LOGIN_STATUS_NO_RESOURCES);
349 kfree(sess->sess_ops);
357 static int iscsi_login_zero_tsih_s2(
358 struct iscsi_conn *conn)
360 struct iscsi_node_attrib *na;
361 struct iscsi_session *sess = conn->sess;
364 sess->tpg = conn->tpg;
367 * Assign a new TPG Session Handle. Note this is protected with
368 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
370 sess->tsih = ++sess->tpg->ntsih;
372 sess->tsih = ++sess->tpg->ntsih;
375 * Create the default params from user defined values..
377 if (iscsi_copy_param_list(&conn->param_list,
378 conn->tpg->param_list, 1) < 0) {
379 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
380 ISCSI_LOGIN_STATUS_NO_RESOURCES);
384 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
387 iscsi_set_keys_to_negotiate(conn->param_list, iser);
389 if (sess->sess_ops->SessionType)
390 return iscsi_set_keys_irrelevant_for_discovery(
393 na = iscsit_tpg_get_node_attrib(sess);
396 * Need to send TargetPortalGroupTag back in first login response
397 * on any iSCSI connection where the Initiator provides TargetName.
398 * See 5.3.1. Login Phase Start
400 * In our case, we have already located the struct iscsi_tiqn at this point.
402 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
406 * Workaround for Initiators that have broken connection recovery logic.
408 * "We would really like to get rid of this." Linux-iSCSI.org team
410 if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
414 * Set RDMAExtensions=Yes by default for iSER enabled network portals
417 struct iscsi_param *param;
418 unsigned long mrdsl, off;
421 if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
425 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
426 * Immediate Data + Unsolicitied Data-OUT if necessary..
428 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
431 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
432 ISCSI_LOGIN_STATUS_NO_RESOURCES);
435 rc = kstrtoul(param->value, 0, &mrdsl);
437 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
438 ISCSI_LOGIN_STATUS_NO_RESOURCES);
441 off = mrdsl % PAGE_SIZE;
445 if (mrdsl < PAGE_SIZE)
450 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
451 " to PAGE_SIZE\n", mrdsl);
453 if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
456 * ISER currently requires that ImmediateData + Unsolicited
457 * Data be disabled when protection / signature MRs are enabled.
460 if (sess->se_sess->sup_prot_ops &
461 (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
462 TARGET_PROT_DOUT_INSERT)) {
464 if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
467 if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
470 pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
471 " T10-PI enabled ISER session\n");
478 static int iscsi_login_non_zero_tsih_s1(
479 struct iscsi_conn *conn,
482 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
484 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
489 * Add a new connection to an existing session.
491 static int iscsi_login_non_zero_tsih_s2(
492 struct iscsi_conn *conn,
495 struct iscsi_portal_group *tpg = conn->tpg;
496 struct iscsi_session *sess = NULL, *sess_p = NULL;
497 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
498 struct se_session *se_sess, *se_sess_tmp;
499 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
502 spin_lock_bh(&se_tpg->session_lock);
503 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
506 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
507 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
508 atomic_read(&sess_p->session_logout) ||
509 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
511 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
512 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
513 iscsit_inc_session_usage_count(sess_p);
514 iscsit_stop_time2retain_timer(sess_p);
519 spin_unlock_bh(&se_tpg->session_lock);
522 * If the Time2Retain handler has expired, the session is already gone.
525 pr_err("Initiator attempting to add a connection to"
526 " a non-existent session, rejecting iSCSI Login.\n");
527 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
528 ISCSI_LOGIN_STATUS_NO_SESSION);
533 * Stop the Time2Retain timer if this is a failed session, we restart
534 * the timer if the login is not successful.
536 spin_lock_bh(&sess->conn_lock);
537 if (sess->session_state == TARG_SESS_STATE_FAILED)
538 atomic_set(&sess->session_continuation, 1);
539 spin_unlock_bh(&sess->conn_lock);
541 iscsi_login_set_conn_values(sess, conn, pdu->cid);
543 if (iscsi_copy_param_list(&conn->param_list,
544 conn->tpg->param_list, 0) < 0) {
545 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
546 ISCSI_LOGIN_STATUS_NO_RESOURCES);
550 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
553 iscsi_set_keys_to_negotiate(conn->param_list, iser);
555 * Need to send TargetPortalGroupTag back in first login response
556 * on any iSCSI connection where the Initiator provides TargetName.
557 * See 5.3.1. Login Phase Start
559 * In our case, we have already located the struct iscsi_tiqn at this point.
561 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
567 int iscsi_login_post_auth_non_zero_tsih(
568 struct iscsi_conn *conn,
572 struct iscsi_conn *conn_ptr = NULL;
573 struct iscsi_conn_recovery *cr = NULL;
574 struct iscsi_session *sess = conn->sess;
577 * By following item 5 in the login table, if we have found
578 * an existing ISID and a valid/existing TSIH and an existing
579 * CID we do connection reinstatement. Currently we dont not
580 * support it so we send back an non-zero status class to the
581 * initiator and release the new connection.
583 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
585 pr_err("Connection exists with CID %hu for %s,"
586 " performing connection reinstatement.\n",
587 conn_ptr->cid, sess->sess_ops->InitiatorName);
589 iscsit_connection_reinstatement_rcfr(conn_ptr);
590 iscsit_dec_conn_usage_count(conn_ptr);
594 * Check for any connection recovery entires containing CID.
595 * We use the original ExpStatSN sent in the first login request
596 * to acknowledge commands for the failed connection.
598 * Also note that an explict logout may have already been sent,
599 * but the response may not be sent due to additional connection
602 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
603 cr = iscsit_get_inactive_connection_recovery_entry(
606 pr_debug("Performing implicit logout"
607 " for connection recovery on CID: %hu\n",
609 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
614 * Else we follow item 4 from the login table in that we have
615 * found an existing ISID and a valid/existing TSIH and a new
616 * CID we go ahead and continue to add a new connection to the
619 pr_debug("Adding CID %hu to existing session for %s.\n",
620 cid, sess->sess_ops->InitiatorName);
622 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
623 pr_err("Adding additional connection to this session"
624 " would exceed MaxConnections %d, login failed.\n",
625 sess->sess_ops->MaxConnections);
626 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
627 ISCSI_LOGIN_STATUS_ISID_ERROR);
634 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
636 struct iscsi_session *sess = conn->sess;
638 * FIXME: Unsolicitied NopIN support for ISER
640 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
643 if (!sess->sess_ops->SessionType)
644 iscsit_start_nopin_timer(conn);
647 static int iscsit_start_kthreads(struct iscsi_conn *conn)
651 spin_lock(&iscsit_global->ts_bitmap_lock);
652 conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
653 ISCSIT_BITMAP_BITS, get_order(1));
654 spin_unlock(&iscsit_global->ts_bitmap_lock);
656 if (conn->bitmap_id < 0) {
657 pr_err("bitmap_find_free_region() failed for"
658 " iscsit_start_kthreads()\n");
662 conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
663 "%s", ISCSI_TX_THREAD_NAME);
664 if (IS_ERR(conn->tx_thread)) {
665 pr_err("Unable to start iscsi_target_tx_thread\n");
666 ret = PTR_ERR(conn->tx_thread);
669 conn->tx_thread_active = true;
671 conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
672 "%s", ISCSI_RX_THREAD_NAME);
673 if (IS_ERR(conn->rx_thread)) {
674 pr_err("Unable to start iscsi_target_rx_thread\n");
675 ret = PTR_ERR(conn->rx_thread);
678 conn->rx_thread_active = true;
682 kthread_stop(conn->tx_thread);
683 conn->tx_thread_active = false;
685 spin_lock(&iscsit_global->ts_bitmap_lock);
686 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
688 spin_unlock(&iscsit_global->ts_bitmap_lock);
692 int iscsi_post_login_handler(
694 struct iscsi_conn *conn,
698 struct iscsi_session *sess = conn->sess;
699 struct se_session *se_sess = sess->se_sess;
700 struct iscsi_portal_group *tpg = sess->tpg;
701 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
704 iscsit_inc_conn_usage_count(conn);
706 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
707 ISCSI_LOGIN_STATUS_ACCEPT);
709 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
710 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
712 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
714 * SCSI Initiator -> SCSI Target Port Mapping
717 iscsi_set_session_parameters(sess->sess_ops,
718 conn->param_list, 0);
719 iscsi_release_param_list(conn->param_list);
720 conn->param_list = NULL;
722 spin_lock_bh(&sess->conn_lock);
723 atomic_set(&sess->session_continuation, 0);
724 if (sess->session_state == TARG_SESS_STATE_FAILED) {
726 " TARG_SESS_STATE_LOGGED_IN.\n");
727 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
731 pr_debug("iSCSI Login successful on CID: %hu from %s to"
732 " %pISpc,%hu\n", conn->cid, conn->login_ip,
733 &conn->local_sockaddr, tpg->tpgt);
735 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
736 atomic_inc(&sess->nconn);
737 pr_debug("Incremented iSCSI Connection count to %hu"
738 " from node: %s\n", atomic_read(&sess->nconn),
739 sess->sess_ops->InitiatorName);
740 spin_unlock_bh(&sess->conn_lock);
742 rc = iscsit_start_kthreads(conn);
746 iscsi_post_login_start_timers(conn);
748 * Determine CPU mask to ensure connection's RX and TX kthreads
749 * are scheduled on the same CPU.
751 iscsit_thread_get_cpumask(conn);
752 conn->conn_rx_reset_cpumask = 1;
753 conn->conn_tx_reset_cpumask = 1;
755 iscsit_dec_conn_usage_count(conn);
757 spin_lock_bh(&se_tpg->session_lock);
758 iscsit_stop_time2retain_timer(sess);
759 spin_unlock_bh(&se_tpg->session_lock);
761 iscsit_dec_session_usage_count(sess);
765 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
766 iscsi_release_param_list(conn->param_list);
767 conn->param_list = NULL;
769 iscsit_determine_maxcmdsn(sess);
771 spin_lock_bh(&se_tpg->session_lock);
772 __transport_register_session(&sess->tpg->tpg_se_tpg,
773 se_sess->se_node_acl, se_sess, sess);
774 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
775 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
777 pr_debug("iSCSI Login successful on CID: %hu from %s to %pISpc,%hu\n",
778 conn->cid, conn->login_ip, &conn->local_sockaddr,
781 spin_lock_bh(&sess->conn_lock);
782 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
783 atomic_inc(&sess->nconn);
784 pr_debug("Incremented iSCSI Connection count to %hu from node:"
785 " %s\n", atomic_read(&sess->nconn),
786 sess->sess_ops->InitiatorName);
787 spin_unlock_bh(&sess->conn_lock);
789 sess->sid = tpg->sid++;
791 sess->sid = tpg->sid++;
792 pr_debug("Established iSCSI session from node: %s\n",
793 sess->sess_ops->InitiatorName);
797 tpg->tpg_tiqn->tiqn_nsessions++;
799 pr_debug("Incremented number of active iSCSI sessions to %u on"
800 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
801 spin_unlock_bh(&se_tpg->session_lock);
803 rc = iscsit_start_kthreads(conn);
807 iscsi_post_login_start_timers(conn);
809 * Determine CPU mask to ensure connection's RX and TX kthreads
810 * are scheduled on the same CPU.
812 iscsit_thread_get_cpumask(conn);
813 conn->conn_rx_reset_cpumask = 1;
814 conn->conn_tx_reset_cpumask = 1;
816 iscsit_dec_conn_usage_count(conn);
821 static void iscsi_handle_login_thread_timeout(unsigned long data)
823 struct iscsi_np *np = (struct iscsi_np *) data;
825 spin_lock_bh(&np->np_thread_lock);
826 pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
829 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
830 spin_unlock_bh(&np->np_thread_lock);
835 send_sig(SIGINT, np->np_thread, 1);
837 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
838 spin_unlock_bh(&np->np_thread_lock);
841 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
844 * This used the TA_LOGIN_TIMEOUT constant because at this
845 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
847 spin_lock_bh(&np->np_thread_lock);
848 init_timer(&np->np_login_timer);
849 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
850 np->np_login_timer.data = (unsigned long)np;
851 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
852 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
853 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
854 add_timer(&np->np_login_timer);
856 pr_debug("Added timeout timer to iSCSI login request for"
857 " %u seconds.\n", TA_LOGIN_TIMEOUT);
858 spin_unlock_bh(&np->np_thread_lock);
861 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
863 spin_lock_bh(&np->np_thread_lock);
864 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
865 spin_unlock_bh(&np->np_thread_lock);
868 np->np_login_timer_flags |= ISCSI_TF_STOP;
869 spin_unlock_bh(&np->np_thread_lock);
871 del_timer_sync(&np->np_login_timer);
873 spin_lock_bh(&np->np_thread_lock);
874 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
875 spin_unlock_bh(&np->np_thread_lock);
880 struct __kernel_sockaddr_storage *sockaddr)
882 struct socket *sock = NULL;
883 int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
885 switch (np->np_network_transport) {
887 np->np_ip_proto = IPPROTO_TCP;
888 np->np_sock_type = SOCK_STREAM;
891 np->np_ip_proto = IPPROTO_SCTP;
892 np->np_sock_type = SOCK_STREAM;
895 np->np_ip_proto = IPPROTO_SCTP;
896 np->np_sock_type = SOCK_SEQPACKET;
899 pr_err("Unsupported network_transport: %d\n",
900 np->np_network_transport);
904 np->np_ip_proto = IPPROTO_TCP;
905 np->np_sock_type = SOCK_STREAM;
907 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
908 np->np_ip_proto, &sock);
910 pr_err("sock_create() failed.\n");
913 np->np_socket = sock;
915 * Setup the np->np_sockaddr from the passed sockaddr setup
916 * in iscsi_target_configfs.c code..
918 memcpy(&np->np_sockaddr, sockaddr,
919 sizeof(struct __kernel_sockaddr_storage));
921 if (sockaddr->ss_family == AF_INET6)
922 len = sizeof(struct sockaddr_in6);
924 len = sizeof(struct sockaddr_in);
926 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
928 /* FIXME: Someone please explain why this is endian-safe */
930 if (np->np_network_transport == ISCSI_TCP) {
931 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
932 (char *)&opt, sizeof(opt));
934 pr_err("kernel_setsockopt() for TCP_NODELAY"
935 " failed: %d\n", ret);
940 /* FIXME: Someone please explain why this is endian-safe */
941 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
942 (char *)&opt, sizeof(opt));
944 pr_err("kernel_setsockopt() for SO_REUSEADDR"
949 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
950 (char *)&opt, sizeof(opt));
952 pr_err("kernel_setsockopt() for IP_FREEBIND"
957 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
959 pr_err("kernel_bind() failed: %d\n", ret);
963 ret = kernel_listen(sock, backlog);
965 pr_err("kernel_listen() failed: %d\n", ret);
971 np->np_socket = NULL;
976 int iscsi_target_setup_login_socket(
978 struct __kernel_sockaddr_storage *sockaddr)
980 struct iscsit_transport *t;
983 t = iscsit_get_transport(np->np_network_transport);
987 rc = t->iscsit_setup_np(np, sockaddr);
989 iscsit_put_transport(t);
993 np->np_transport = t;
998 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1000 struct socket *new_sock, *sock = np->np_socket;
1001 struct sockaddr_in sock_in;
1002 struct sockaddr_in6 sock_in6;
1005 rc = kernel_accept(sock, &new_sock, 0);
1009 conn->sock = new_sock;
1010 conn->login_family = np->np_sockaddr.ss_family;
1012 if (np->np_sockaddr.ss_family == AF_INET6) {
1013 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1015 rc = conn->sock->ops->getname(conn->sock,
1016 (struct sockaddr *)&sock_in6, &err, 1);
1018 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1019 snprintf(conn->login_ip, sizeof(conn->login_ip), "[%pI6c]",
1020 &sock_in6.sin6_addr.in6_u);
1022 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI4",
1023 &sock_in6.sin6_addr.s6_addr32[3]);
1024 conn->login_port = ntohs(sock_in6.sin6_port);
1027 rc = conn->sock->ops->getname(conn->sock,
1028 (struct sockaddr *)&sock_in6, &err, 0);
1030 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1031 memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
1033 /* Pretend to be an ipv4 socket */
1034 sock_in.sin_family = AF_INET;
1035 sock_in.sin_port = sock_in6.sin6_port;
1036 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1037 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1041 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1043 rc = conn->sock->ops->getname(conn->sock,
1044 (struct sockaddr *)&sock_in, &err, 1);
1046 sprintf(conn->login_ip, "%pI4",
1047 &sock_in.sin_addr.s_addr);
1048 conn->login_port = ntohs(sock_in.sin_port);
1051 rc = conn->sock->ops->getname(conn->sock,
1052 (struct sockaddr *)&sock_in, &err, 0);
1054 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1060 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1062 struct iscsi_login_req *login_req;
1063 u32 padding = 0, payload_length;
1065 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1068 login_req = (struct iscsi_login_req *)login->req;
1069 payload_length = ntoh24(login_req->dlength);
1070 padding = ((-payload_length) & 3);
1072 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1073 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1074 login_req->flags, login_req->itt, login_req->cmdsn,
1075 login_req->exp_statsn, login_req->cid, payload_length);
1077 * Setup the initial iscsi_login values from the leading
1078 * login request PDU.
1080 if (login->first_request) {
1081 login_req = (struct iscsi_login_req *)login->req;
1082 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1083 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1084 login->version_min = login_req->min_version;
1085 login->version_max = login_req->max_version;
1086 memcpy(login->isid, login_req->isid, 6);
1087 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1088 login->init_task_tag = login_req->itt;
1089 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1090 login->cid = be16_to_cpu(login_req->cid);
1091 login->tsih = be16_to_cpu(login_req->tsih);
1094 if (iscsi_target_check_login_request(conn, login) < 0)
1097 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1098 if (iscsi_login_rx_data(conn, login->req_buf,
1099 payload_length + padding) < 0)
1105 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1108 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1115 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1120 conn->conn_transport = t;
1124 rc = try_module_get(t->owner);
1126 pr_err("try_module_get() failed for %s\n", t->name);
1130 conn->conn_transport = t;
1134 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1135 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1140 pr_err("iSCSI Login negotiation failed.\n");
1141 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1142 ISCSI_LOGIN_STATUS_INIT_ERR);
1143 if (!zero_tsih || !conn->sess)
1145 if (conn->sess->se_sess)
1146 transport_free_session(conn->sess->se_sess);
1147 if (conn->sess->session_index != 0) {
1148 spin_lock_bh(&sess_idr_lock);
1149 idr_remove(&sess_idr, conn->sess->session_index);
1150 spin_unlock_bh(&sess_idr_lock);
1152 kfree(conn->sess->sess_ops);
1157 iscsi_stop_login_thread_timer(np);
1159 * If login negotiation fails check if the Time2Retain timer
1160 * needs to be restarted.
1162 if (!zero_tsih && conn->sess) {
1163 spin_lock_bh(&conn->sess->conn_lock);
1164 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1165 struct se_portal_group *se_tpg =
1166 &conn->tpg->tpg_se_tpg;
1168 atomic_set(&conn->sess->session_continuation, 0);
1169 spin_unlock_bh(&conn->sess->conn_lock);
1170 spin_lock_bh(&se_tpg->session_lock);
1171 iscsit_start_time2retain_handler(conn->sess);
1172 spin_unlock_bh(&se_tpg->session_lock);
1174 spin_unlock_bh(&conn->sess->conn_lock);
1175 iscsit_dec_session_usage_count(conn->sess);
1178 if (!IS_ERR(conn->conn_rx_hash.tfm))
1179 crypto_free_hash(conn->conn_rx_hash.tfm);
1180 if (!IS_ERR(conn->conn_tx_hash.tfm))
1181 crypto_free_hash(conn->conn_tx_hash.tfm);
1183 free_cpumask_var(conn->conn_cpumask);
1185 kfree(conn->conn_ops);
1187 if (conn->param_list) {
1188 iscsi_release_param_list(conn->param_list);
1189 conn->param_list = NULL;
1191 iscsi_target_nego_release(conn);
1194 sock_release(conn->sock);
1198 if (conn->conn_transport->iscsit_wait_conn)
1199 conn->conn_transport->iscsit_wait_conn(conn);
1201 if (conn->conn_transport->iscsit_free_conn)
1202 conn->conn_transport->iscsit_free_conn(conn);
1204 iscsit_put_transport(conn->conn_transport);
1208 static int __iscsi_target_login_thread(struct iscsi_np *np)
1210 u8 *buffer, zero_tsih = 0;
1212 struct iscsi_conn *conn = NULL;
1213 struct iscsi_login *login;
1214 struct iscsi_portal_group *tpg = NULL;
1215 struct iscsi_login_req *pdu;
1216 struct iscsi_tpg_np *tpg_np;
1217 bool new_sess = false;
1219 flush_signals(current);
1221 spin_lock_bh(&np->np_thread_lock);
1222 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1223 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1224 complete(&np->np_restart_comp);
1225 } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1226 spin_unlock_bh(&np->np_thread_lock);
1229 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1231 spin_unlock_bh(&np->np_thread_lock);
1233 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1235 pr_err("Could not allocate memory for"
1236 " new connection\n");
1237 /* Get another socket */
1240 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1241 conn->conn_state = TARG_CONN_STATE_FREE;
1243 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1248 rc = np->np_transport->iscsit_accept_np(np, conn);
1249 if (rc == -ENOSYS) {
1250 complete(&np->np_restart_comp);
1251 iscsit_put_transport(conn->conn_transport);
1255 } else if (rc < 0) {
1256 spin_lock_bh(&np->np_thread_lock);
1257 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1258 spin_unlock_bh(&np->np_thread_lock);
1259 complete(&np->np_restart_comp);
1260 iscsit_put_transport(conn->conn_transport);
1263 /* Get another socket */
1266 spin_unlock_bh(&np->np_thread_lock);
1267 iscsit_put_transport(conn->conn_transport);
1273 * Perform the remaining iSCSI connection initialization items..
1275 login = iscsi_login_init_conn(conn);
1280 iscsi_start_login_thread_timer(np);
1282 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1283 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1285 * This will process the first login request + payload..
1287 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1293 buffer = &login->req[0];
1294 pdu = (struct iscsi_login_req *)buffer;
1296 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1297 * when Status-Class != 0.
1299 conn->login_itt = pdu->itt;
1301 spin_lock_bh(&np->np_thread_lock);
1302 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1303 spin_unlock_bh(&np->np_thread_lock);
1304 pr_err("iSCSI Network Portal on %pISpc currently not"
1305 " active.\n", &np->np_sockaddr);
1306 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1307 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1310 spin_unlock_bh(&np->np_thread_lock);
1312 conn->network_transport = np->np_network_transport;
1314 pr_debug("Received iSCSI login request from %s on %s Network"
1315 " Portal %pISpc\n", conn->login_ip, np->np_transport->name,
1316 &conn->local_sockaddr);
1318 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1319 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1321 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1322 pdu->min_version) < 0)
1325 zero_tsih = (pdu->tsih == 0x0000);
1328 * This is the leading connection of a new session.
1329 * We wait until after authentication to check for
1330 * session reinstatement.
1332 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1336 * Add a new connection to an existing session.
1337 * We check for a non-existant session in
1338 * iscsi_login_non_zero_tsih_s2() below based
1339 * on ISID/TSIH, but wait until after authentication
1340 * to check for connection reinstatement, etc.
1342 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1346 * SessionType: Discovery
1348 * Locates Default Portal
1350 * SessionType: Normal
1352 * Locates Target Portal from NP -> Target IQN
1354 rc = iscsi_target_locate_portal(np, conn, login);
1359 login->zero_tsih = zero_tsih;
1361 conn->sess->se_sess->sup_prot_ops =
1362 conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1366 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1371 if (iscsi_login_zero_tsih_s2(conn) < 0)
1374 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1378 ret = iscsi_target_start_negotiation(login, conn);
1383 pr_err("struct iscsi_conn session pointer is NULL!\n");
1387 iscsi_stop_login_thread_timer(np);
1389 if (signal_pending(current))
1393 tpg_np = conn->tpg_np;
1395 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1399 iscsit_deaccess_np(np, tpg, tpg_np);
1404 /* Get another socket */
1410 tpg_np = conn->tpg_np;
1411 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1415 iscsit_deaccess_np(np, tpg, tpg_np);
1424 iscsi_stop_login_thread_timer(np);
1425 spin_lock_bh(&np->np_thread_lock);
1426 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1427 spin_unlock_bh(&np->np_thread_lock);
1432 int iscsi_target_login_thread(void *arg)
1434 struct iscsi_np *np = arg;
1437 allow_signal(SIGINT);
1440 ret = __iscsi_target_login_thread(np);
1442 * We break and exit here unless another sock_accept() call