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 <crypto/hash.h>
20 #include <linux/module.h>
21 #include <linux/string.h>
22 #include <linux/kthread.h>
23 #include <linux/idr.h>
24 #include <linux/tcp.h> /* TCP_NODELAY */
25 #include <net/ipv6.h> /* ipv6_addr_v4mapped() */
26 #include <scsi/iscsi_proto.h>
27 #include <target/target_core_base.h>
28 #include <target/target_core_fabric.h>
30 #include <target/iscsi/iscsi_target_core.h>
31 #include <target/iscsi/iscsi_target_stat.h>
32 #include "iscsi_target_device.h"
33 #include "iscsi_target_nego.h"
34 #include "iscsi_target_erl0.h"
35 #include "iscsi_target_erl2.h"
36 #include "iscsi_target_login.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 #include <target/iscsi/iscsi_transport.h>
44 static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
46 struct iscsi_login *login;
48 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
50 pr_err("Unable to allocate memory for struct iscsi_login.\n");
55 login->first_request = 1;
57 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
58 if (!login->req_buf) {
59 pr_err("Unable to allocate memory for response buffer.\n");
63 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
64 if (!login->rsp_buf) {
65 pr_err("Unable to allocate memory for request buffer.\n");
69 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
70 if (!conn->conn_ops) {
71 pr_err("Unable to allocate memory for"
72 " struct iscsi_conn_ops.\n");
76 init_waitqueue_head(&conn->queues_wq);
77 INIT_LIST_HEAD(&conn->conn_list);
78 INIT_LIST_HEAD(&conn->conn_cmd_list);
79 INIT_LIST_HEAD(&conn->immed_queue_list);
80 INIT_LIST_HEAD(&conn->response_queue_list);
81 init_completion(&conn->conn_post_wait_comp);
82 init_completion(&conn->conn_wait_comp);
83 init_completion(&conn->conn_wait_rcfr_comp);
84 init_completion(&conn->conn_waiting_on_uc_comp);
85 init_completion(&conn->conn_logout_comp);
86 init_completion(&conn->rx_half_close_comp);
87 init_completion(&conn->tx_half_close_comp);
88 init_completion(&conn->rx_login_comp);
89 spin_lock_init(&conn->cmd_lock);
90 spin_lock_init(&conn->conn_usage_lock);
91 spin_lock_init(&conn->immed_queue_lock);
92 spin_lock_init(&conn->nopin_timer_lock);
93 spin_lock_init(&conn->response_queue_lock);
94 spin_lock_init(&conn->state_lock);
96 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
97 pr_err("Unable to allocate conn->conn_cpumask\n");
100 conn->conn_login = login;
105 kfree(conn->conn_ops);
107 kfree(login->rsp_buf);
109 kfree(login->req_buf);
116 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
117 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
119 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
121 struct crypto_ahash *tfm;
124 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
125 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
126 * to software 1x8 byte slicing from crc32c.ko
128 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
130 pr_err("crypto_alloc_ahash() failed\n");
134 conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
135 if (!conn->conn_rx_hash) {
136 pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
137 crypto_free_ahash(tfm);
140 ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
142 conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
143 if (!conn->conn_tx_hash) {
144 pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
145 ahash_request_free(conn->conn_rx_hash);
146 conn->conn_rx_hash = NULL;
147 crypto_free_ahash(tfm);
150 ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
155 static int iscsi_login_check_initiator_version(
156 struct iscsi_conn *conn,
160 if ((version_max != 0x00) || (version_min != 0x00)) {
161 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
162 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
163 version_min, version_max);
164 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
165 ISCSI_LOGIN_STATUS_NO_VERSION);
172 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
175 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
176 struct iscsi_portal_group *tpg = conn->tpg;
177 struct iscsi_session *sess = NULL, *sess_p = NULL;
178 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
179 struct se_session *se_sess, *se_sess_tmp;
181 initiatorname_param = iscsi_find_param_from_key(
182 INITIATORNAME, conn->param_list);
183 sessiontype_param = iscsi_find_param_from_key(
184 SESSIONTYPE, conn->param_list);
185 if (!initiatorname_param || !sessiontype_param) {
186 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
187 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
191 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
193 spin_lock_bh(&se_tpg->session_lock);
194 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
197 sess_p = se_sess->fabric_sess_ptr;
198 spin_lock(&sess_p->conn_lock);
199 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
200 atomic_read(&sess_p->session_logout) ||
201 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
202 spin_unlock(&sess_p->conn_lock);
205 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
206 (!strcmp(sess_p->sess_ops->InitiatorName,
207 initiatorname_param->value) &&
208 (sess_p->sess_ops->SessionType == sessiontype))) {
209 atomic_set(&sess_p->session_reinstatement, 1);
210 spin_unlock(&sess_p->conn_lock);
211 iscsit_inc_session_usage_count(sess_p);
212 iscsit_stop_time2retain_timer(sess_p);
216 spin_unlock(&sess_p->conn_lock);
218 spin_unlock_bh(&se_tpg->session_lock);
220 * If the Time2Retain handler has expired, the session is already gone.
225 pr_debug("%s iSCSI Session SID %u is still active for %s,"
226 " performing session reinstatement.\n", (sessiontype) ?
227 "Discovery" : "Normal", sess->sid,
228 sess->sess_ops->InitiatorName);
230 spin_lock_bh(&sess->conn_lock);
231 if (sess->session_state == TARG_SESS_STATE_FAILED) {
232 spin_unlock_bh(&sess->conn_lock);
233 iscsit_dec_session_usage_count(sess);
234 iscsit_close_session(sess);
237 spin_unlock_bh(&sess->conn_lock);
239 iscsit_stop_session(sess, 1, 1);
240 iscsit_dec_session_usage_count(sess);
242 iscsit_close_session(sess);
246 static void iscsi_login_set_conn_values(
247 struct iscsi_session *sess,
248 struct iscsi_conn *conn,
252 conn->cid = be16_to_cpu(cid);
254 * Generate a random Status sequence number (statsn) for the new
257 get_random_bytes(&conn->stat_sn, sizeof(u32));
259 mutex_lock(&auth_id_lock);
260 conn->auth_id = iscsit_global->auth_id++;
261 mutex_unlock(&auth_id_lock);
264 __printf(2, 3) int iscsi_change_param_sprintf(
265 struct iscsi_conn *conn,
266 const char *fmt, ...)
269 unsigned char buf[64];
271 memset(buf, 0, sizeof buf);
274 vsnprintf(buf, sizeof buf, fmt, args);
277 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
278 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
279 ISCSI_LOGIN_STATUS_NO_RESOURCES);
285 EXPORT_SYMBOL(iscsi_change_param_sprintf);
288 * This is the leading connection of a new session,
289 * or session reinstatement.
291 static int iscsi_login_zero_tsih_s1(
292 struct iscsi_conn *conn,
295 struct iscsi_session *sess = NULL;
296 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
299 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
301 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
302 ISCSI_LOGIN_STATUS_NO_RESOURCES);
303 pr_err("Could not allocate memory for session\n");
307 iscsi_login_set_conn_values(sess, conn, pdu->cid);
308 sess->init_task_tag = pdu->itt;
309 memcpy(&sess->isid, pdu->isid, 6);
310 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
311 INIT_LIST_HEAD(&sess->sess_conn_list);
312 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
313 INIT_LIST_HEAD(&sess->cr_active_list);
314 INIT_LIST_HEAD(&sess->cr_inactive_list);
315 init_completion(&sess->async_msg_comp);
316 init_completion(&sess->reinstatement_comp);
317 init_completion(&sess->session_wait_comp);
318 init_completion(&sess->session_waiting_on_uc_comp);
319 mutex_init(&sess->cmdsn_mutex);
320 spin_lock_init(&sess->conn_lock);
321 spin_lock_init(&sess->cr_a_lock);
322 spin_lock_init(&sess->cr_i_lock);
323 spin_lock_init(&sess->session_usage_lock);
324 spin_lock_init(&sess->ttt_lock);
326 idr_preload(GFP_KERNEL);
327 spin_lock_bh(&sess_idr_lock);
328 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
330 sess->session_index = ret;
331 spin_unlock_bh(&sess_idr_lock);
335 pr_err("idr_alloc() for sess_idr failed\n");
336 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
337 ISCSI_LOGIN_STATUS_NO_RESOURCES);
342 sess->creation_time = get_jiffies_64();
344 * The FFP CmdSN window values will be allocated from the TPG's
345 * Initiator Node's ACL once the login has been successfully completed.
347 atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
349 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
350 if (!sess->sess_ops) {
351 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
352 ISCSI_LOGIN_STATUS_NO_RESOURCES);
353 pr_err("Unable to allocate memory for"
354 " struct iscsi_sess_ops.\n");
359 sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
360 if (IS_ERR(sess->se_sess)) {
361 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
362 ISCSI_LOGIN_STATUS_NO_RESOURCES);
363 kfree(sess->sess_ops);
371 static int iscsi_login_zero_tsih_s2(
372 struct iscsi_conn *conn)
374 struct iscsi_node_attrib *na;
375 struct iscsi_session *sess = conn->sess;
378 sess->tpg = conn->tpg;
381 * Assign a new TPG Session Handle. Note this is protected with
382 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
384 sess->tsih = ++sess->tpg->ntsih;
386 sess->tsih = ++sess->tpg->ntsih;
389 * Create the default params from user defined values..
391 if (iscsi_copy_param_list(&conn->param_list,
392 conn->tpg->param_list, 1) < 0) {
393 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
394 ISCSI_LOGIN_STATUS_NO_RESOURCES);
398 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
401 iscsi_set_keys_to_negotiate(conn->param_list, iser);
403 if (sess->sess_ops->SessionType)
404 return iscsi_set_keys_irrelevant_for_discovery(
407 na = iscsit_tpg_get_node_attrib(sess);
410 * Need to send TargetPortalGroupTag back in first login response
411 * on any iSCSI connection where the Initiator provides TargetName.
412 * See 5.3.1. Login Phase Start
414 * In our case, we have already located the struct iscsi_tiqn at this point.
416 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
420 * Workaround for Initiators that have broken connection recovery logic.
422 * "We would really like to get rid of this." Linux-iSCSI.org team
424 if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
428 * Set RDMAExtensions=Yes by default for iSER enabled network portals
431 struct iscsi_param *param;
432 unsigned long mrdsl, off;
435 if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
439 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
440 * Immediate Data + Unsolicited Data-OUT if necessary..
442 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
445 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
446 ISCSI_LOGIN_STATUS_NO_RESOURCES);
449 rc = kstrtoul(param->value, 0, &mrdsl);
451 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
452 ISCSI_LOGIN_STATUS_NO_RESOURCES);
455 off = mrdsl % PAGE_SIZE;
459 if (mrdsl < PAGE_SIZE)
464 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
465 " to PAGE_SIZE\n", mrdsl);
467 if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
470 * ISER currently requires that ImmediateData + Unsolicited
471 * Data be disabled when protection / signature MRs are enabled.
474 if (sess->se_sess->sup_prot_ops &
475 (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
476 TARGET_PROT_DOUT_INSERT)) {
478 if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
481 if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
484 pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
485 " T10-PI enabled ISER session\n");
492 static int iscsi_login_non_zero_tsih_s1(
493 struct iscsi_conn *conn,
496 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
498 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
503 * Add a new connection to an existing session.
505 static int iscsi_login_non_zero_tsih_s2(
506 struct iscsi_conn *conn,
509 struct iscsi_portal_group *tpg = conn->tpg;
510 struct iscsi_session *sess = NULL, *sess_p = NULL;
511 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
512 struct se_session *se_sess, *se_sess_tmp;
513 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
516 spin_lock_bh(&se_tpg->session_lock);
517 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
520 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
521 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
522 atomic_read(&sess_p->session_logout) ||
523 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
525 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
526 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
527 iscsit_inc_session_usage_count(sess_p);
528 iscsit_stop_time2retain_timer(sess_p);
533 spin_unlock_bh(&se_tpg->session_lock);
536 * If the Time2Retain handler has expired, the session is already gone.
539 pr_err("Initiator attempting to add a connection to"
540 " a non-existent session, rejecting iSCSI Login.\n");
541 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
542 ISCSI_LOGIN_STATUS_NO_SESSION);
547 * Stop the Time2Retain timer if this is a failed session, we restart
548 * the timer if the login is not successful.
550 spin_lock_bh(&sess->conn_lock);
551 if (sess->session_state == TARG_SESS_STATE_FAILED)
552 atomic_set(&sess->session_continuation, 1);
553 spin_unlock_bh(&sess->conn_lock);
555 iscsi_login_set_conn_values(sess, conn, pdu->cid);
557 if (iscsi_copy_param_list(&conn->param_list,
558 conn->tpg->param_list, 0) < 0) {
559 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
560 ISCSI_LOGIN_STATUS_NO_RESOURCES);
564 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
567 iscsi_set_keys_to_negotiate(conn->param_list, iser);
569 * Need to send TargetPortalGroupTag back in first login response
570 * on any iSCSI connection where the Initiator provides TargetName.
571 * See 5.3.1. Login Phase Start
573 * In our case, we have already located the struct iscsi_tiqn at this point.
575 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
581 int iscsi_login_post_auth_non_zero_tsih(
582 struct iscsi_conn *conn,
586 struct iscsi_conn *conn_ptr = NULL;
587 struct iscsi_conn_recovery *cr = NULL;
588 struct iscsi_session *sess = conn->sess;
591 * By following item 5 in the login table, if we have found
592 * an existing ISID and a valid/existing TSIH and an existing
593 * CID we do connection reinstatement. Currently we dont not
594 * support it so we send back an non-zero status class to the
595 * initiator and release the new connection.
597 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
599 pr_err("Connection exists with CID %hu for %s,"
600 " performing connection reinstatement.\n",
601 conn_ptr->cid, sess->sess_ops->InitiatorName);
603 iscsit_connection_reinstatement_rcfr(conn_ptr);
604 iscsit_dec_conn_usage_count(conn_ptr);
608 * Check for any connection recovery entires containing CID.
609 * We use the original ExpStatSN sent in the first login request
610 * to acknowledge commands for the failed connection.
612 * Also note that an explict logout may have already been sent,
613 * but the response may not be sent due to additional connection
616 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
617 cr = iscsit_get_inactive_connection_recovery_entry(
620 pr_debug("Performing implicit logout"
621 " for connection recovery on CID: %hu\n",
623 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
628 * Else we follow item 4 from the login table in that we have
629 * found an existing ISID and a valid/existing TSIH and a new
630 * CID we go ahead and continue to add a new connection to the
633 pr_debug("Adding CID %hu to existing session for %s.\n",
634 cid, sess->sess_ops->InitiatorName);
636 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
637 pr_err("Adding additional connection to this session"
638 " would exceed MaxConnections %d, login failed.\n",
639 sess->sess_ops->MaxConnections);
640 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
641 ISCSI_LOGIN_STATUS_ISID_ERROR);
648 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
650 struct iscsi_session *sess = conn->sess;
652 * FIXME: Unsolicited NopIN support for ISER
654 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
657 if (!sess->sess_ops->SessionType)
658 iscsit_start_nopin_timer(conn);
661 int iscsit_start_kthreads(struct iscsi_conn *conn)
665 spin_lock(&iscsit_global->ts_bitmap_lock);
666 conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
667 ISCSIT_BITMAP_BITS, get_order(1));
668 spin_unlock(&iscsit_global->ts_bitmap_lock);
670 if (conn->bitmap_id < 0) {
671 pr_err("bitmap_find_free_region() failed for"
672 " iscsit_start_kthreads()\n");
676 conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
677 "%s", ISCSI_TX_THREAD_NAME);
678 if (IS_ERR(conn->tx_thread)) {
679 pr_err("Unable to start iscsi_target_tx_thread\n");
680 ret = PTR_ERR(conn->tx_thread);
683 conn->tx_thread_active = true;
685 conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
686 "%s", ISCSI_RX_THREAD_NAME);
687 if (IS_ERR(conn->rx_thread)) {
688 pr_err("Unable to start iscsi_target_rx_thread\n");
689 ret = PTR_ERR(conn->rx_thread);
692 conn->rx_thread_active = true;
696 send_sig(SIGINT, conn->tx_thread, 1);
697 kthread_stop(conn->tx_thread);
698 conn->tx_thread_active = false;
700 spin_lock(&iscsit_global->ts_bitmap_lock);
701 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
703 spin_unlock(&iscsit_global->ts_bitmap_lock);
707 void iscsi_post_login_handler(
709 struct iscsi_conn *conn,
713 struct iscsi_session *sess = conn->sess;
714 struct se_session *se_sess = sess->se_sess;
715 struct iscsi_portal_group *tpg = sess->tpg;
716 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
718 iscsit_inc_conn_usage_count(conn);
720 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
721 ISCSI_LOGIN_STATUS_ACCEPT);
723 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
724 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
726 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
728 * SCSI Initiator -> SCSI Target Port Mapping
731 iscsi_set_session_parameters(sess->sess_ops,
732 conn->param_list, 0);
733 iscsi_release_param_list(conn->param_list);
734 conn->param_list = NULL;
736 spin_lock_bh(&sess->conn_lock);
737 atomic_set(&sess->session_continuation, 0);
738 if (sess->session_state == TARG_SESS_STATE_FAILED) {
740 " TARG_SESS_STATE_LOGGED_IN.\n");
741 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
745 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
746 " %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
747 &conn->local_sockaddr, tpg->tpgt);
749 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
750 atomic_inc(&sess->nconn);
751 pr_debug("Incremented iSCSI Connection count to %hu"
752 " from node: %s\n", atomic_read(&sess->nconn),
753 sess->sess_ops->InitiatorName);
754 spin_unlock_bh(&sess->conn_lock);
756 iscsi_post_login_start_timers(conn);
758 * Determine CPU mask to ensure connection's RX and TX kthreads
759 * are scheduled on the same CPU.
761 iscsit_thread_get_cpumask(conn);
762 conn->conn_rx_reset_cpumask = 1;
763 conn->conn_tx_reset_cpumask = 1;
765 * Wakeup the sleeping iscsi_target_rx_thread() now that
766 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
768 complete(&conn->rx_login_comp);
769 iscsit_dec_conn_usage_count(conn);
772 spin_lock_bh(&se_tpg->session_lock);
773 iscsit_stop_time2retain_timer(sess);
774 spin_unlock_bh(&se_tpg->session_lock);
776 iscsit_dec_session_usage_count(sess);
780 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
781 iscsi_release_param_list(conn->param_list);
782 conn->param_list = NULL;
784 iscsit_determine_maxcmdsn(sess);
786 spin_lock_bh(&se_tpg->session_lock);
787 __transport_register_session(&sess->tpg->tpg_se_tpg,
788 se_sess->se_node_acl, se_sess, sess);
789 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
790 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
792 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
793 conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
796 spin_lock_bh(&sess->conn_lock);
797 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
798 atomic_inc(&sess->nconn);
799 pr_debug("Incremented iSCSI Connection count to %hu from node:"
800 " %s\n", atomic_read(&sess->nconn),
801 sess->sess_ops->InitiatorName);
802 spin_unlock_bh(&sess->conn_lock);
804 sess->sid = tpg->sid++;
806 sess->sid = tpg->sid++;
807 pr_debug("Established iSCSI session from node: %s\n",
808 sess->sess_ops->InitiatorName);
812 tpg->tpg_tiqn->tiqn_nsessions++;
814 pr_debug("Incremented number of active iSCSI sessions to %u on"
815 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
816 spin_unlock_bh(&se_tpg->session_lock);
818 iscsi_post_login_start_timers(conn);
820 * Determine CPU mask to ensure connection's RX and TX kthreads
821 * are scheduled on the same CPU.
823 iscsit_thread_get_cpumask(conn);
824 conn->conn_rx_reset_cpumask = 1;
825 conn->conn_tx_reset_cpumask = 1;
827 * Wakeup the sleeping iscsi_target_rx_thread() now that
828 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
830 complete(&conn->rx_login_comp);
831 iscsit_dec_conn_usage_count(conn);
834 static void iscsi_handle_login_thread_timeout(unsigned long data)
836 struct iscsi_np *np = (struct iscsi_np *) data;
838 spin_lock_bh(&np->np_thread_lock);
839 pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
842 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
843 spin_unlock_bh(&np->np_thread_lock);
848 send_sig(SIGINT, np->np_thread, 1);
850 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
851 spin_unlock_bh(&np->np_thread_lock);
854 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
857 * This used the TA_LOGIN_TIMEOUT constant because at this
858 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
860 spin_lock_bh(&np->np_thread_lock);
861 init_timer(&np->np_login_timer);
862 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
863 np->np_login_timer.data = (unsigned long)np;
864 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
865 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
866 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
867 add_timer(&np->np_login_timer);
869 pr_debug("Added timeout timer to iSCSI login request for"
870 " %u seconds.\n", TA_LOGIN_TIMEOUT);
871 spin_unlock_bh(&np->np_thread_lock);
874 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
876 spin_lock_bh(&np->np_thread_lock);
877 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
878 spin_unlock_bh(&np->np_thread_lock);
881 np->np_login_timer_flags |= ISCSI_TF_STOP;
882 spin_unlock_bh(&np->np_thread_lock);
884 del_timer_sync(&np->np_login_timer);
886 spin_lock_bh(&np->np_thread_lock);
887 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
888 spin_unlock_bh(&np->np_thread_lock);
893 struct sockaddr_storage *sockaddr)
895 struct socket *sock = NULL;
896 int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
898 switch (np->np_network_transport) {
900 np->np_ip_proto = IPPROTO_TCP;
901 np->np_sock_type = SOCK_STREAM;
904 np->np_ip_proto = IPPROTO_SCTP;
905 np->np_sock_type = SOCK_STREAM;
908 np->np_ip_proto = IPPROTO_SCTP;
909 np->np_sock_type = SOCK_SEQPACKET;
912 pr_err("Unsupported network_transport: %d\n",
913 np->np_network_transport);
917 np->np_ip_proto = IPPROTO_TCP;
918 np->np_sock_type = SOCK_STREAM;
920 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
921 np->np_ip_proto, &sock);
923 pr_err("sock_create() failed.\n");
926 np->np_socket = sock;
928 * Setup the np->np_sockaddr from the passed sockaddr setup
929 * in iscsi_target_configfs.c code..
931 memcpy(&np->np_sockaddr, sockaddr,
932 sizeof(struct sockaddr_storage));
934 if (sockaddr->ss_family == AF_INET6)
935 len = sizeof(struct sockaddr_in6);
937 len = sizeof(struct sockaddr_in);
939 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
941 /* FIXME: Someone please explain why this is endian-safe */
943 if (np->np_network_transport == ISCSI_TCP) {
944 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
945 (char *)&opt, sizeof(opt));
947 pr_err("kernel_setsockopt() for TCP_NODELAY"
948 " failed: %d\n", ret);
953 /* FIXME: Someone please explain why this is endian-safe */
954 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
955 (char *)&opt, sizeof(opt));
957 pr_err("kernel_setsockopt() for SO_REUSEADDR"
962 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
963 (char *)&opt, sizeof(opt));
965 pr_err("kernel_setsockopt() for IP_FREEBIND"
970 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
972 pr_err("kernel_bind() failed: %d\n", ret);
976 ret = kernel_listen(sock, backlog);
978 pr_err("kernel_listen() failed: %d\n", ret);
984 np->np_socket = NULL;
989 int iscsi_target_setup_login_socket(
991 struct sockaddr_storage *sockaddr)
993 struct iscsit_transport *t;
996 t = iscsit_get_transport(np->np_network_transport);
1000 rc = t->iscsit_setup_np(np, sockaddr);
1002 iscsit_put_transport(t);
1006 np->np_transport = t;
1011 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1013 struct socket *new_sock, *sock = np->np_socket;
1014 struct sockaddr_in sock_in;
1015 struct sockaddr_in6 sock_in6;
1018 rc = kernel_accept(sock, &new_sock, 0);
1022 conn->sock = new_sock;
1023 conn->login_family = np->np_sockaddr.ss_family;
1025 if (np->np_sockaddr.ss_family == AF_INET6) {
1026 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1028 rc = conn->sock->ops->getname(conn->sock,
1029 (struct sockaddr *)&sock_in6, &err, 1);
1031 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1032 memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
1034 /* Pretend to be an ipv4 socket */
1035 sock_in.sin_family = AF_INET;
1036 sock_in.sin_port = sock_in6.sin6_port;
1037 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1038 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1042 rc = conn->sock->ops->getname(conn->sock,
1043 (struct sockaddr *)&sock_in6, &err, 0);
1045 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1046 memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
1048 /* Pretend to be an ipv4 socket */
1049 sock_in.sin_family = AF_INET;
1050 sock_in.sin_port = sock_in6.sin6_port;
1051 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1052 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1056 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1058 rc = conn->sock->ops->getname(conn->sock,
1059 (struct sockaddr *)&sock_in, &err, 1);
1061 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1063 rc = conn->sock->ops->getname(conn->sock,
1064 (struct sockaddr *)&sock_in, &err, 0);
1066 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1072 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1074 struct iscsi_login_req *login_req;
1075 u32 padding = 0, payload_length;
1077 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1080 login_req = (struct iscsi_login_req *)login->req;
1081 payload_length = ntoh24(login_req->dlength);
1082 padding = ((-payload_length) & 3);
1084 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1085 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1086 login_req->flags, login_req->itt, login_req->cmdsn,
1087 login_req->exp_statsn, login_req->cid, payload_length);
1089 * Setup the initial iscsi_login values from the leading
1090 * login request PDU.
1092 if (login->first_request) {
1093 login_req = (struct iscsi_login_req *)login->req;
1094 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1095 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1096 login->version_min = login_req->min_version;
1097 login->version_max = login_req->max_version;
1098 memcpy(login->isid, login_req->isid, 6);
1099 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1100 login->init_task_tag = login_req->itt;
1101 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1102 login->cid = be16_to_cpu(login_req->cid);
1103 login->tsih = be16_to_cpu(login_req->tsih);
1106 if (iscsi_target_check_login_request(conn, login) < 0)
1109 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1110 if (iscsi_login_rx_data(conn, login->req_buf,
1111 payload_length + padding) < 0)
1117 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1120 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1127 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1132 conn->conn_transport = t;
1136 rc = try_module_get(t->owner);
1138 pr_err("try_module_get() failed for %s\n", t->name);
1142 conn->conn_transport = t;
1146 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1147 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1152 pr_err("iSCSI Login negotiation failed.\n");
1153 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1154 ISCSI_LOGIN_STATUS_INIT_ERR);
1155 if (!zero_tsih || !conn->sess)
1157 if (conn->sess->se_sess)
1158 transport_free_session(conn->sess->se_sess);
1159 if (conn->sess->session_index != 0) {
1160 spin_lock_bh(&sess_idr_lock);
1161 idr_remove(&sess_idr, conn->sess->session_index);
1162 spin_unlock_bh(&sess_idr_lock);
1164 kfree(conn->sess->sess_ops);
1169 iscsi_stop_login_thread_timer(np);
1171 * If login negotiation fails check if the Time2Retain timer
1172 * needs to be restarted.
1174 if (!zero_tsih && conn->sess) {
1175 spin_lock_bh(&conn->sess->conn_lock);
1176 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1177 struct se_portal_group *se_tpg =
1178 &conn->tpg->tpg_se_tpg;
1180 atomic_set(&conn->sess->session_continuation, 0);
1181 spin_unlock_bh(&conn->sess->conn_lock);
1182 spin_lock_bh(&se_tpg->session_lock);
1183 iscsit_start_time2retain_handler(conn->sess);
1184 spin_unlock_bh(&se_tpg->session_lock);
1186 spin_unlock_bh(&conn->sess->conn_lock);
1187 iscsit_dec_session_usage_count(conn->sess);
1190 ahash_request_free(conn->conn_tx_hash);
1191 if (conn->conn_rx_hash) {
1192 struct crypto_ahash *tfm;
1194 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
1195 ahash_request_free(conn->conn_rx_hash);
1196 crypto_free_ahash(tfm);
1199 free_cpumask_var(conn->conn_cpumask);
1201 kfree(conn->conn_ops);
1203 if (conn->param_list) {
1204 iscsi_release_param_list(conn->param_list);
1205 conn->param_list = NULL;
1207 iscsi_target_nego_release(conn);
1210 sock_release(conn->sock);
1214 if (conn->conn_transport->iscsit_wait_conn)
1215 conn->conn_transport->iscsit_wait_conn(conn);
1217 if (conn->conn_transport->iscsit_free_conn)
1218 conn->conn_transport->iscsit_free_conn(conn);
1220 iscsit_put_transport(conn->conn_transport);
1224 static int __iscsi_target_login_thread(struct iscsi_np *np)
1226 u8 *buffer, zero_tsih = 0;
1228 struct iscsi_conn *conn = NULL;
1229 struct iscsi_login *login;
1230 struct iscsi_portal_group *tpg = NULL;
1231 struct iscsi_login_req *pdu;
1232 struct iscsi_tpg_np *tpg_np;
1233 bool new_sess = false;
1235 flush_signals(current);
1237 spin_lock_bh(&np->np_thread_lock);
1238 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1239 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1240 complete(&np->np_restart_comp);
1241 } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1242 spin_unlock_bh(&np->np_thread_lock);
1245 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1247 spin_unlock_bh(&np->np_thread_lock);
1249 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1251 pr_err("Could not allocate memory for"
1252 " new connection\n");
1253 /* Get another socket */
1256 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1257 conn->conn_state = TARG_CONN_STATE_FREE;
1259 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1264 rc = np->np_transport->iscsit_accept_np(np, conn);
1265 if (rc == -ENOSYS) {
1266 complete(&np->np_restart_comp);
1267 iscsit_put_transport(conn->conn_transport);
1271 } else if (rc < 0) {
1272 spin_lock_bh(&np->np_thread_lock);
1273 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1274 spin_unlock_bh(&np->np_thread_lock);
1275 complete(&np->np_restart_comp);
1276 iscsit_put_transport(conn->conn_transport);
1279 /* Get another socket */
1282 spin_unlock_bh(&np->np_thread_lock);
1283 iscsit_put_transport(conn->conn_transport);
1289 * Perform the remaining iSCSI connection initialization items..
1291 login = iscsi_login_init_conn(conn);
1296 iscsi_start_login_thread_timer(np);
1298 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1299 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1301 * This will process the first login request + payload..
1303 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1309 buffer = &login->req[0];
1310 pdu = (struct iscsi_login_req *)buffer;
1312 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1313 * when Status-Class != 0.
1315 conn->login_itt = pdu->itt;
1317 spin_lock_bh(&np->np_thread_lock);
1318 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1319 spin_unlock_bh(&np->np_thread_lock);
1320 pr_err("iSCSI Network Portal on %pISpc currently not"
1321 " active.\n", &np->np_sockaddr);
1322 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1323 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1326 spin_unlock_bh(&np->np_thread_lock);
1328 conn->network_transport = np->np_network_transport;
1330 pr_debug("Received iSCSI login request from %pISpc on %s Network"
1331 " Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
1332 &conn->local_sockaddr);
1334 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1335 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1337 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1338 pdu->min_version) < 0)
1341 zero_tsih = (pdu->tsih == 0x0000);
1344 * This is the leading connection of a new session.
1345 * We wait until after authentication to check for
1346 * session reinstatement.
1348 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1352 * Add a new connection to an existing session.
1353 * We check for a non-existant session in
1354 * iscsi_login_non_zero_tsih_s2() below based
1355 * on ISID/TSIH, but wait until after authentication
1356 * to check for connection reinstatement, etc.
1358 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1362 * SessionType: Discovery
1364 * Locates Default Portal
1366 * SessionType: Normal
1368 * Locates Target Portal from NP -> Target IQN
1370 rc = iscsi_target_locate_portal(np, conn, login);
1375 login->zero_tsih = zero_tsih;
1378 conn->sess->se_sess->sup_prot_ops =
1379 conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1383 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1388 if (iscsi_login_zero_tsih_s2(conn) < 0)
1391 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1395 if (conn->conn_transport->iscsit_validate_params) {
1396 ret = conn->conn_transport->iscsit_validate_params(conn);
1405 ret = iscsi_target_start_negotiation(login, conn);
1409 iscsi_stop_login_thread_timer(np);
1412 tpg_np = conn->tpg_np;
1414 iscsi_post_login_handler(np, conn, zero_tsih);
1415 iscsit_deaccess_np(np, tpg, tpg_np);
1420 /* Get another socket */
1426 tpg_np = conn->tpg_np;
1427 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1431 iscsit_deaccess_np(np, tpg, tpg_np);
1440 iscsi_stop_login_thread_timer(np);
1441 spin_lock_bh(&np->np_thread_lock);
1442 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1443 spin_unlock_bh(&np->np_thread_lock);
1448 int iscsi_target_login_thread(void *arg)
1450 struct iscsi_np *np = arg;
1453 allow_signal(SIGINT);
1456 ret = __iscsi_target_login_thread(np);
1458 * We break and exit here unless another sock_accept() call