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/sched/signal.h>
24 #include <linux/idr.h>
25 #include <linux/tcp.h> /* TCP_NODELAY */
26 #include <net/ipv6.h> /* ipv6_addr_v4mapped() */
27 #include <scsi/iscsi_proto.h>
28 #include <target/target_core_base.h>
29 #include <target/target_core_fabric.h>
31 #include <target/iscsi/iscsi_target_core.h>
32 #include <target/iscsi/iscsi_target_stat.h>
33 #include "iscsi_target_device.h"
34 #include "iscsi_target_nego.h"
35 #include "iscsi_target_erl0.h"
36 #include "iscsi_target_erl2.h"
37 #include "iscsi_target_login.h"
38 #include "iscsi_target_tpg.h"
39 #include "iscsi_target_util.h"
40 #include "iscsi_target.h"
41 #include "iscsi_target_parameters.h"
43 #include <target/iscsi/iscsi_transport.h>
45 static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
47 struct iscsi_login *login;
49 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
51 pr_err("Unable to allocate memory for struct iscsi_login.\n");
56 login->first_request = 1;
58 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
59 if (!login->req_buf) {
60 pr_err("Unable to allocate memory for response buffer.\n");
64 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
65 if (!login->rsp_buf) {
66 pr_err("Unable to allocate memory for request buffer.\n");
70 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
71 if (!conn->conn_ops) {
72 pr_err("Unable to allocate memory for"
73 " struct iscsi_conn_ops.\n");
77 init_waitqueue_head(&conn->queues_wq);
78 INIT_LIST_HEAD(&conn->conn_list);
79 INIT_LIST_HEAD(&conn->conn_cmd_list);
80 INIT_LIST_HEAD(&conn->immed_queue_list);
81 INIT_LIST_HEAD(&conn->response_queue_list);
82 init_completion(&conn->conn_post_wait_comp);
83 init_completion(&conn->conn_wait_comp);
84 init_completion(&conn->conn_wait_rcfr_comp);
85 init_completion(&conn->conn_waiting_on_uc_comp);
86 init_completion(&conn->conn_logout_comp);
87 init_completion(&conn->rx_half_close_comp);
88 init_completion(&conn->tx_half_close_comp);
89 init_completion(&conn->rx_login_comp);
90 spin_lock_init(&conn->cmd_lock);
91 spin_lock_init(&conn->conn_usage_lock);
92 spin_lock_init(&conn->immed_queue_lock);
93 spin_lock_init(&conn->nopin_timer_lock);
94 spin_lock_init(&conn->response_queue_lock);
95 spin_lock_init(&conn->state_lock);
97 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
98 pr_err("Unable to allocate conn->conn_cpumask\n");
101 conn->conn_login = login;
106 kfree(conn->conn_ops);
108 kfree(login->rsp_buf);
110 kfree(login->req_buf);
117 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
118 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
120 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
122 struct crypto_ahash *tfm;
125 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
126 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
127 * to software 1x8 byte slicing from crc32c.ko
129 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
131 pr_err("crypto_alloc_ahash() failed\n");
135 conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
136 if (!conn->conn_rx_hash) {
137 pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
138 crypto_free_ahash(tfm);
141 ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
143 conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
144 if (!conn->conn_tx_hash) {
145 pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
146 ahash_request_free(conn->conn_rx_hash);
147 conn->conn_rx_hash = NULL;
148 crypto_free_ahash(tfm);
151 ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
156 static int iscsi_login_check_initiator_version(
157 struct iscsi_conn *conn,
161 if ((version_max != 0x00) || (version_min != 0x00)) {
162 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
163 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
164 version_min, version_max);
165 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
166 ISCSI_LOGIN_STATUS_NO_VERSION);
173 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
176 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
177 struct iscsi_portal_group *tpg = conn->tpg;
178 struct iscsi_session *sess = NULL, *sess_p = NULL;
179 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
180 struct se_session *se_sess, *se_sess_tmp;
182 initiatorname_param = iscsi_find_param_from_key(
183 INITIATORNAME, conn->param_list);
184 sessiontype_param = iscsi_find_param_from_key(
185 SESSIONTYPE, conn->param_list);
186 if (!initiatorname_param || !sessiontype_param) {
187 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
188 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
192 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
194 spin_lock_bh(&se_tpg->session_lock);
195 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
198 sess_p = se_sess->fabric_sess_ptr;
199 spin_lock(&sess_p->conn_lock);
200 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
201 atomic_read(&sess_p->session_logout) ||
202 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
203 spin_unlock(&sess_p->conn_lock);
206 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
207 (!strcmp(sess_p->sess_ops->InitiatorName,
208 initiatorname_param->value) &&
209 (sess_p->sess_ops->SessionType == sessiontype))) {
210 atomic_set(&sess_p->session_reinstatement, 1);
211 atomic_set(&sess_p->session_fall_back_to_erl0, 1);
212 spin_unlock(&sess_p->conn_lock);
213 iscsit_inc_session_usage_count(sess_p);
214 iscsit_stop_time2retain_timer(sess_p);
218 spin_unlock(&sess_p->conn_lock);
220 spin_unlock_bh(&se_tpg->session_lock);
222 * If the Time2Retain handler has expired, the session is already gone.
227 pr_debug("%s iSCSI Session SID %u is still active for %s,"
228 " performing session reinstatement.\n", (sessiontype) ?
229 "Discovery" : "Normal", sess->sid,
230 sess->sess_ops->InitiatorName);
232 spin_lock_bh(&sess->conn_lock);
233 if (sess->session_state == TARG_SESS_STATE_FAILED) {
234 spin_unlock_bh(&sess->conn_lock);
235 iscsit_dec_session_usage_count(sess);
236 iscsit_close_session(sess);
239 spin_unlock_bh(&sess->conn_lock);
241 iscsit_stop_session(sess, 1, 1);
242 iscsit_dec_session_usage_count(sess);
244 iscsit_close_session(sess);
248 static int iscsi_login_set_conn_values(
249 struct iscsi_session *sess,
250 struct iscsi_conn *conn,
255 conn->cid = be16_to_cpu(cid);
257 * Generate a random Status sequence number (statsn) for the new
260 ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32));
264 mutex_lock(&auth_id_lock);
265 conn->auth_id = iscsit_global->auth_id++;
266 mutex_unlock(&auth_id_lock);
270 __printf(2, 3) int iscsi_change_param_sprintf(
271 struct iscsi_conn *conn,
272 const char *fmt, ...)
275 unsigned char buf[64];
277 memset(buf, 0, sizeof buf);
280 vsnprintf(buf, sizeof buf, fmt, args);
283 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
284 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
285 ISCSI_LOGIN_STATUS_NO_RESOURCES);
291 EXPORT_SYMBOL(iscsi_change_param_sprintf);
294 * This is the leading connection of a new session,
295 * or session reinstatement.
297 static int iscsi_login_zero_tsih_s1(
298 struct iscsi_conn *conn,
301 struct iscsi_session *sess = NULL;
302 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
305 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
307 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
308 ISCSI_LOGIN_STATUS_NO_RESOURCES);
309 pr_err("Could not allocate memory for session\n");
313 ret = iscsi_login_set_conn_values(sess, conn, pdu->cid);
318 sess->init_task_tag = pdu->itt;
319 memcpy(&sess->isid, pdu->isid, 6);
320 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
321 INIT_LIST_HEAD(&sess->sess_conn_list);
322 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
323 INIT_LIST_HEAD(&sess->cr_active_list);
324 INIT_LIST_HEAD(&sess->cr_inactive_list);
325 init_completion(&sess->async_msg_comp);
326 init_completion(&sess->reinstatement_comp);
327 init_completion(&sess->session_wait_comp);
328 init_completion(&sess->session_waiting_on_uc_comp);
329 mutex_init(&sess->cmdsn_mutex);
330 spin_lock_init(&sess->conn_lock);
331 spin_lock_init(&sess->cr_a_lock);
332 spin_lock_init(&sess->cr_i_lock);
333 spin_lock_init(&sess->session_usage_lock);
334 spin_lock_init(&sess->ttt_lock);
336 idr_preload(GFP_KERNEL);
337 spin_lock_bh(&sess_idr_lock);
338 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
340 sess->session_index = ret;
341 spin_unlock_bh(&sess_idr_lock);
345 pr_err("idr_alloc() for sess_idr failed\n");
346 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
347 ISCSI_LOGIN_STATUS_NO_RESOURCES);
352 sess->creation_time = get_jiffies_64();
354 * The FFP CmdSN window values will be allocated from the TPG's
355 * Initiator Node's ACL once the login has been successfully completed.
357 atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
359 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
360 if (!sess->sess_ops) {
361 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
362 ISCSI_LOGIN_STATUS_NO_RESOURCES);
363 pr_err("Unable to allocate memory for"
364 " struct iscsi_sess_ops.\n");
369 sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
370 if (IS_ERR(sess->se_sess)) {
371 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
372 ISCSI_LOGIN_STATUS_NO_RESOURCES);
373 kfree(sess->sess_ops);
381 static int iscsi_login_zero_tsih_s2(
382 struct iscsi_conn *conn)
384 struct iscsi_node_attrib *na;
385 struct iscsi_session *sess = conn->sess;
388 sess->tpg = conn->tpg;
391 * Assign a new TPG Session Handle. Note this is protected with
392 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
394 sess->tsih = ++sess->tpg->ntsih;
396 sess->tsih = ++sess->tpg->ntsih;
399 * Create the default params from user defined values..
401 if (iscsi_copy_param_list(&conn->param_list,
402 conn->tpg->param_list, 1) < 0) {
403 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
404 ISCSI_LOGIN_STATUS_NO_RESOURCES);
408 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
411 iscsi_set_keys_to_negotiate(conn->param_list, iser);
413 if (sess->sess_ops->SessionType)
414 return iscsi_set_keys_irrelevant_for_discovery(
417 na = iscsit_tpg_get_node_attrib(sess);
420 * Need to send TargetPortalGroupTag back in first login response
421 * on any iSCSI connection where the Initiator provides TargetName.
422 * See 5.3.1. Login Phase Start
424 * In our case, we have already located the struct iscsi_tiqn at this point.
426 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
430 * Workaround for Initiators that have broken connection recovery logic.
432 * "We would really like to get rid of this." Linux-iSCSI.org team
434 if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
438 * Set RDMAExtensions=Yes by default for iSER enabled network portals
441 struct iscsi_param *param;
442 unsigned long mrdsl, off;
445 if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
449 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
450 * Immediate Data + Unsolicited Data-OUT if necessary..
452 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
455 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
456 ISCSI_LOGIN_STATUS_NO_RESOURCES);
459 rc = kstrtoul(param->value, 0, &mrdsl);
461 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
462 ISCSI_LOGIN_STATUS_NO_RESOURCES);
465 off = mrdsl % PAGE_SIZE;
469 if (mrdsl < PAGE_SIZE)
474 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
475 " to PAGE_SIZE\n", mrdsl);
477 if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
480 * ISER currently requires that ImmediateData + Unsolicited
481 * Data be disabled when protection / signature MRs are enabled.
484 if (sess->se_sess->sup_prot_ops &
485 (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
486 TARGET_PROT_DOUT_INSERT)) {
488 if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
491 if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
494 pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
495 " T10-PI enabled ISER session\n");
502 static int iscsi_login_non_zero_tsih_s1(
503 struct iscsi_conn *conn,
506 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
508 return iscsi_login_set_conn_values(NULL, conn, pdu->cid);
512 * Add a new connection to an existing session.
514 static int iscsi_login_non_zero_tsih_s2(
515 struct iscsi_conn *conn,
518 struct iscsi_portal_group *tpg = conn->tpg;
519 struct iscsi_session *sess = NULL, *sess_p = NULL;
520 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
521 struct se_session *se_sess, *se_sess_tmp;
522 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
525 spin_lock_bh(&se_tpg->session_lock);
526 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
529 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
530 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
531 atomic_read(&sess_p->session_logout) ||
532 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
534 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
535 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
536 iscsit_inc_session_usage_count(sess_p);
537 iscsit_stop_time2retain_timer(sess_p);
542 spin_unlock_bh(&se_tpg->session_lock);
545 * If the Time2Retain handler has expired, the session is already gone.
548 pr_err("Initiator attempting to add a connection to"
549 " a non-existent session, rejecting iSCSI Login.\n");
550 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
551 ISCSI_LOGIN_STATUS_NO_SESSION);
556 * Stop the Time2Retain timer if this is a failed session, we restart
557 * the timer if the login is not successful.
559 spin_lock_bh(&sess->conn_lock);
560 if (sess->session_state == TARG_SESS_STATE_FAILED)
561 atomic_set(&sess->session_continuation, 1);
562 spin_unlock_bh(&sess->conn_lock);
564 if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 ||
565 iscsi_copy_param_list(&conn->param_list,
566 conn->tpg->param_list, 0) < 0) {
567 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
568 ISCSI_LOGIN_STATUS_NO_RESOURCES);
572 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
575 iscsi_set_keys_to_negotiate(conn->param_list, iser);
577 * Need to send TargetPortalGroupTag back in first login response
578 * on any iSCSI connection where the Initiator provides TargetName.
579 * See 5.3.1. Login Phase Start
581 * In our case, we have already located the struct iscsi_tiqn at this point.
583 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
589 int iscsi_login_post_auth_non_zero_tsih(
590 struct iscsi_conn *conn,
594 struct iscsi_conn *conn_ptr = NULL;
595 struct iscsi_conn_recovery *cr = NULL;
596 struct iscsi_session *sess = conn->sess;
599 * By following item 5 in the login table, if we have found
600 * an existing ISID and a valid/existing TSIH and an existing
601 * CID we do connection reinstatement. Currently we dont not
602 * support it so we send back an non-zero status class to the
603 * initiator and release the new connection.
605 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
607 pr_err("Connection exists with CID %hu for %s,"
608 " performing connection reinstatement.\n",
609 conn_ptr->cid, sess->sess_ops->InitiatorName);
611 iscsit_connection_reinstatement_rcfr(conn_ptr);
612 iscsit_dec_conn_usage_count(conn_ptr);
616 * Check for any connection recovery entires containing CID.
617 * We use the original ExpStatSN sent in the first login request
618 * to acknowledge commands for the failed connection.
620 * Also note that an explict logout may have already been sent,
621 * but the response may not be sent due to additional connection
624 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
625 cr = iscsit_get_inactive_connection_recovery_entry(
628 pr_debug("Performing implicit logout"
629 " for connection recovery on CID: %hu\n",
631 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
636 * Else we follow item 4 from the login table in that we have
637 * found an existing ISID and a valid/existing TSIH and a new
638 * CID we go ahead and continue to add a new connection to the
641 pr_debug("Adding CID %hu to existing session for %s.\n",
642 cid, sess->sess_ops->InitiatorName);
644 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
645 pr_err("Adding additional connection to this session"
646 " would exceed MaxConnections %d, login failed.\n",
647 sess->sess_ops->MaxConnections);
648 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
649 ISCSI_LOGIN_STATUS_ISID_ERROR);
656 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
658 struct iscsi_session *sess = conn->sess;
660 * FIXME: Unsolicited NopIN support for ISER
662 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
665 if (!sess->sess_ops->SessionType)
666 iscsit_start_nopin_timer(conn);
669 int iscsit_start_kthreads(struct iscsi_conn *conn)
673 spin_lock(&iscsit_global->ts_bitmap_lock);
674 conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
675 ISCSIT_BITMAP_BITS, get_order(1));
676 spin_unlock(&iscsit_global->ts_bitmap_lock);
678 if (conn->bitmap_id < 0) {
679 pr_err("bitmap_find_free_region() failed for"
680 " iscsit_start_kthreads()\n");
684 conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
685 "%s", ISCSI_TX_THREAD_NAME);
686 if (IS_ERR(conn->tx_thread)) {
687 pr_err("Unable to start iscsi_target_tx_thread\n");
688 ret = PTR_ERR(conn->tx_thread);
691 conn->tx_thread_active = true;
693 conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
694 "%s", ISCSI_RX_THREAD_NAME);
695 if (IS_ERR(conn->rx_thread)) {
696 pr_err("Unable to start iscsi_target_rx_thread\n");
697 ret = PTR_ERR(conn->rx_thread);
700 conn->rx_thread_active = true;
704 send_sig(SIGINT, conn->tx_thread, 1);
705 kthread_stop(conn->tx_thread);
706 conn->tx_thread_active = false;
708 spin_lock(&iscsit_global->ts_bitmap_lock);
709 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
711 spin_unlock(&iscsit_global->ts_bitmap_lock);
715 void iscsi_post_login_handler(
717 struct iscsi_conn *conn,
721 struct iscsi_session *sess = conn->sess;
722 struct se_session *se_sess = sess->se_sess;
723 struct iscsi_portal_group *tpg = sess->tpg;
724 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
726 iscsit_inc_conn_usage_count(conn);
728 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
729 ISCSI_LOGIN_STATUS_ACCEPT);
731 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
732 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
734 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
736 * SCSI Initiator -> SCSI Target Port Mapping
739 iscsi_set_session_parameters(sess->sess_ops,
740 conn->param_list, 0);
741 iscsi_release_param_list(conn->param_list);
742 conn->param_list = NULL;
744 spin_lock_bh(&sess->conn_lock);
745 atomic_set(&sess->session_continuation, 0);
746 if (sess->session_state == TARG_SESS_STATE_FAILED) {
748 " TARG_SESS_STATE_LOGGED_IN.\n");
749 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
753 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
754 " %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
755 &conn->local_sockaddr, tpg->tpgt);
757 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
758 atomic_inc(&sess->nconn);
759 pr_debug("Incremented iSCSI Connection count to %hu"
760 " from node: %s\n", atomic_read(&sess->nconn),
761 sess->sess_ops->InitiatorName);
762 spin_unlock_bh(&sess->conn_lock);
764 iscsi_post_login_start_timers(conn);
766 * Determine CPU mask to ensure connection's RX and TX kthreads
767 * are scheduled on the same CPU.
769 iscsit_thread_get_cpumask(conn);
770 conn->conn_rx_reset_cpumask = 1;
771 conn->conn_tx_reset_cpumask = 1;
773 * Wakeup the sleeping iscsi_target_rx_thread() now that
774 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
776 complete(&conn->rx_login_comp);
777 iscsit_dec_conn_usage_count(conn);
780 spin_lock_bh(&se_tpg->session_lock);
781 iscsit_stop_time2retain_timer(sess);
782 spin_unlock_bh(&se_tpg->session_lock);
784 iscsit_dec_session_usage_count(sess);
788 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
789 iscsi_release_param_list(conn->param_list);
790 conn->param_list = NULL;
792 iscsit_determine_maxcmdsn(sess);
794 spin_lock_bh(&se_tpg->session_lock);
795 __transport_register_session(&sess->tpg->tpg_se_tpg,
796 se_sess->se_node_acl, se_sess, sess);
797 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
798 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
800 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
801 conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
804 spin_lock_bh(&sess->conn_lock);
805 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
806 atomic_inc(&sess->nconn);
807 pr_debug("Incremented iSCSI Connection count to %hu from node:"
808 " %s\n", atomic_read(&sess->nconn),
809 sess->sess_ops->InitiatorName);
810 spin_unlock_bh(&sess->conn_lock);
812 sess->sid = tpg->sid++;
814 sess->sid = tpg->sid++;
815 pr_debug("Established iSCSI session from node: %s\n",
816 sess->sess_ops->InitiatorName);
820 tpg->tpg_tiqn->tiqn_nsessions++;
822 pr_debug("Incremented number of active iSCSI sessions to %u on"
823 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
824 spin_unlock_bh(&se_tpg->session_lock);
826 iscsi_post_login_start_timers(conn);
828 * Determine CPU mask to ensure connection's RX and TX kthreads
829 * are scheduled on the same CPU.
831 iscsit_thread_get_cpumask(conn);
832 conn->conn_rx_reset_cpumask = 1;
833 conn->conn_tx_reset_cpumask = 1;
835 * Wakeup the sleeping iscsi_target_rx_thread() now that
836 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
838 complete(&conn->rx_login_comp);
839 iscsit_dec_conn_usage_count(conn);
842 static void iscsi_handle_login_thread_timeout(unsigned long data)
844 struct iscsi_np *np = (struct iscsi_np *) data;
846 spin_lock_bh(&np->np_thread_lock);
847 pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
850 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
851 spin_unlock_bh(&np->np_thread_lock);
856 send_sig(SIGINT, np->np_thread, 1);
858 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
859 spin_unlock_bh(&np->np_thread_lock);
862 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
865 * This used the TA_LOGIN_TIMEOUT constant because at this
866 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
868 spin_lock_bh(&np->np_thread_lock);
869 init_timer(&np->np_login_timer);
870 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
871 np->np_login_timer.data = (unsigned long)np;
872 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
873 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
874 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
875 add_timer(&np->np_login_timer);
877 pr_debug("Added timeout timer to iSCSI login request for"
878 " %u seconds.\n", TA_LOGIN_TIMEOUT);
879 spin_unlock_bh(&np->np_thread_lock);
882 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
884 spin_lock_bh(&np->np_thread_lock);
885 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
886 spin_unlock_bh(&np->np_thread_lock);
889 np->np_login_timer_flags |= ISCSI_TF_STOP;
890 spin_unlock_bh(&np->np_thread_lock);
892 del_timer_sync(&np->np_login_timer);
894 spin_lock_bh(&np->np_thread_lock);
895 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
896 spin_unlock_bh(&np->np_thread_lock);
901 struct sockaddr_storage *sockaddr)
903 struct socket *sock = NULL;
904 int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
906 switch (np->np_network_transport) {
908 np->np_ip_proto = IPPROTO_TCP;
909 np->np_sock_type = SOCK_STREAM;
912 np->np_ip_proto = IPPROTO_SCTP;
913 np->np_sock_type = SOCK_STREAM;
916 np->np_ip_proto = IPPROTO_SCTP;
917 np->np_sock_type = SOCK_SEQPACKET;
920 pr_err("Unsupported network_transport: %d\n",
921 np->np_network_transport);
925 np->np_ip_proto = IPPROTO_TCP;
926 np->np_sock_type = SOCK_STREAM;
928 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
929 np->np_ip_proto, &sock);
931 pr_err("sock_create() failed.\n");
934 np->np_socket = sock;
936 * Setup the np->np_sockaddr from the passed sockaddr setup
937 * in iscsi_target_configfs.c code..
939 memcpy(&np->np_sockaddr, sockaddr,
940 sizeof(struct sockaddr_storage));
942 if (sockaddr->ss_family == AF_INET6)
943 len = sizeof(struct sockaddr_in6);
945 len = sizeof(struct sockaddr_in);
947 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
949 /* FIXME: Someone please explain why this is endian-safe */
951 if (np->np_network_transport == ISCSI_TCP) {
952 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
953 (char *)&opt, sizeof(opt));
955 pr_err("kernel_setsockopt() for TCP_NODELAY"
956 " failed: %d\n", ret);
961 /* FIXME: Someone please explain why this is endian-safe */
962 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
963 (char *)&opt, sizeof(opt));
965 pr_err("kernel_setsockopt() for SO_REUSEADDR"
970 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
971 (char *)&opt, sizeof(opt));
973 pr_err("kernel_setsockopt() for IP_FREEBIND"
978 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
980 pr_err("kernel_bind() failed: %d\n", ret);
984 ret = kernel_listen(sock, backlog);
986 pr_err("kernel_listen() failed: %d\n", ret);
992 np->np_socket = NULL;
997 int iscsi_target_setup_login_socket(
999 struct sockaddr_storage *sockaddr)
1001 struct iscsit_transport *t;
1004 t = iscsit_get_transport(np->np_network_transport);
1008 rc = t->iscsit_setup_np(np, sockaddr);
1010 iscsit_put_transport(t);
1014 np->np_transport = t;
1019 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1021 struct socket *new_sock, *sock = np->np_socket;
1022 struct sockaddr_in sock_in;
1023 struct sockaddr_in6 sock_in6;
1026 rc = kernel_accept(sock, &new_sock, 0);
1030 conn->sock = new_sock;
1031 conn->login_family = np->np_sockaddr.ss_family;
1033 if (np->np_sockaddr.ss_family == AF_INET6) {
1034 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1036 rc = conn->sock->ops->getname(conn->sock,
1037 (struct sockaddr *)&sock_in6, &err, 1);
1039 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1040 memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
1042 /* Pretend to be an ipv4 socket */
1043 sock_in.sin_family = AF_INET;
1044 sock_in.sin_port = sock_in6.sin6_port;
1045 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1046 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1050 rc = conn->sock->ops->getname(conn->sock,
1051 (struct sockaddr *)&sock_in6, &err, 0);
1053 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1054 memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
1056 /* Pretend to be an ipv4 socket */
1057 sock_in.sin_family = AF_INET;
1058 sock_in.sin_port = sock_in6.sin6_port;
1059 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1060 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1064 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1066 rc = conn->sock->ops->getname(conn->sock,
1067 (struct sockaddr *)&sock_in, &err, 1);
1069 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1071 rc = conn->sock->ops->getname(conn->sock,
1072 (struct sockaddr *)&sock_in, &err, 0);
1074 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1080 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1082 struct iscsi_login_req *login_req;
1083 u32 padding = 0, payload_length;
1085 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1088 login_req = (struct iscsi_login_req *)login->req;
1089 payload_length = ntoh24(login_req->dlength);
1090 padding = ((-payload_length) & 3);
1092 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1093 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1094 login_req->flags, login_req->itt, login_req->cmdsn,
1095 login_req->exp_statsn, login_req->cid, payload_length);
1097 * Setup the initial iscsi_login values from the leading
1098 * login request PDU.
1100 if (login->first_request) {
1101 login_req = (struct iscsi_login_req *)login->req;
1102 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1103 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1104 login->version_min = login_req->min_version;
1105 login->version_max = login_req->max_version;
1106 memcpy(login->isid, login_req->isid, 6);
1107 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1108 login->init_task_tag = login_req->itt;
1109 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1110 login->cid = be16_to_cpu(login_req->cid);
1111 login->tsih = be16_to_cpu(login_req->tsih);
1114 if (iscsi_target_check_login_request(conn, login) < 0)
1117 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1118 if (iscsi_login_rx_data(conn, login->req_buf,
1119 payload_length + padding) < 0)
1125 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1128 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1135 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1140 conn->conn_transport = t;
1144 rc = try_module_get(t->owner);
1146 pr_err("try_module_get() failed for %s\n", t->name);
1150 conn->conn_transport = t;
1154 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1155 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1160 pr_err("iSCSI Login negotiation failed.\n");
1161 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1162 ISCSI_LOGIN_STATUS_INIT_ERR);
1163 if (!zero_tsih || !conn->sess)
1165 if (conn->sess->se_sess)
1166 transport_free_session(conn->sess->se_sess);
1167 if (conn->sess->session_index != 0) {
1168 spin_lock_bh(&sess_idr_lock);
1169 idr_remove(&sess_idr, conn->sess->session_index);
1170 spin_unlock_bh(&sess_idr_lock);
1172 kfree(conn->sess->sess_ops);
1177 iscsi_stop_login_thread_timer(np);
1179 * If login negotiation fails check if the Time2Retain timer
1180 * needs to be restarted.
1182 if (!zero_tsih && conn->sess) {
1183 spin_lock_bh(&conn->sess->conn_lock);
1184 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1185 struct se_portal_group *se_tpg =
1186 &conn->tpg->tpg_se_tpg;
1188 atomic_set(&conn->sess->session_continuation, 0);
1189 spin_unlock_bh(&conn->sess->conn_lock);
1190 spin_lock_bh(&se_tpg->session_lock);
1191 iscsit_start_time2retain_handler(conn->sess);
1192 spin_unlock_bh(&se_tpg->session_lock);
1194 spin_unlock_bh(&conn->sess->conn_lock);
1195 iscsit_dec_session_usage_count(conn->sess);
1198 ahash_request_free(conn->conn_tx_hash);
1199 if (conn->conn_rx_hash) {
1200 struct crypto_ahash *tfm;
1202 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
1203 ahash_request_free(conn->conn_rx_hash);
1204 crypto_free_ahash(tfm);
1207 free_cpumask_var(conn->conn_cpumask);
1209 kfree(conn->conn_ops);
1211 if (conn->param_list) {
1212 iscsi_release_param_list(conn->param_list);
1213 conn->param_list = NULL;
1215 iscsi_target_nego_release(conn);
1218 sock_release(conn->sock);
1222 if (conn->conn_transport->iscsit_wait_conn)
1223 conn->conn_transport->iscsit_wait_conn(conn);
1225 if (conn->conn_transport->iscsit_free_conn)
1226 conn->conn_transport->iscsit_free_conn(conn);
1228 iscsit_put_transport(conn->conn_transport);
1232 static int __iscsi_target_login_thread(struct iscsi_np *np)
1234 u8 *buffer, zero_tsih = 0;
1236 struct iscsi_conn *conn = NULL;
1237 struct iscsi_login *login;
1238 struct iscsi_portal_group *tpg = NULL;
1239 struct iscsi_login_req *pdu;
1240 struct iscsi_tpg_np *tpg_np;
1241 bool new_sess = false;
1243 flush_signals(current);
1245 spin_lock_bh(&np->np_thread_lock);
1246 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1247 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1248 complete(&np->np_restart_comp);
1249 } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1250 spin_unlock_bh(&np->np_thread_lock);
1253 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1255 spin_unlock_bh(&np->np_thread_lock);
1257 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1259 pr_err("Could not allocate memory for"
1260 " new connection\n");
1261 /* Get another socket */
1264 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1265 conn->conn_state = TARG_CONN_STATE_FREE;
1267 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1272 rc = np->np_transport->iscsit_accept_np(np, conn);
1273 if (rc == -ENOSYS) {
1274 complete(&np->np_restart_comp);
1275 iscsit_put_transport(conn->conn_transport);
1279 } else if (rc < 0) {
1280 spin_lock_bh(&np->np_thread_lock);
1281 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1282 spin_unlock_bh(&np->np_thread_lock);
1283 complete(&np->np_restart_comp);
1284 iscsit_put_transport(conn->conn_transport);
1287 /* Get another socket */
1290 spin_unlock_bh(&np->np_thread_lock);
1291 iscsit_put_transport(conn->conn_transport);
1297 * Perform the remaining iSCSI connection initialization items..
1299 login = iscsi_login_init_conn(conn);
1304 iscsi_start_login_thread_timer(np);
1306 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1307 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1309 * This will process the first login request + payload..
1311 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1317 buffer = &login->req[0];
1318 pdu = (struct iscsi_login_req *)buffer;
1320 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1321 * when Status-Class != 0.
1323 conn->login_itt = pdu->itt;
1325 spin_lock_bh(&np->np_thread_lock);
1326 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1327 spin_unlock_bh(&np->np_thread_lock);
1328 pr_err("iSCSI Network Portal on %pISpc currently not"
1329 " active.\n", &np->np_sockaddr);
1330 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1331 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1334 spin_unlock_bh(&np->np_thread_lock);
1336 conn->network_transport = np->np_network_transport;
1338 pr_debug("Received iSCSI login request from %pISpc on %s Network"
1339 " Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
1340 &conn->local_sockaddr);
1342 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1343 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1345 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1346 pdu->min_version) < 0)
1349 zero_tsih = (pdu->tsih == 0x0000);
1352 * This is the leading connection of a new session.
1353 * We wait until after authentication to check for
1354 * session reinstatement.
1356 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1360 * Add a new connection to an existing session.
1361 * We check for a non-existant session in
1362 * iscsi_login_non_zero_tsih_s2() below based
1363 * on ISID/TSIH, but wait until after authentication
1364 * to check for connection reinstatement, etc.
1366 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1370 * SessionType: Discovery
1372 * Locates Default Portal
1374 * SessionType: Normal
1376 * Locates Target Portal from NP -> Target IQN
1378 rc = iscsi_target_locate_portal(np, conn, login);
1383 login->zero_tsih = zero_tsih;
1386 conn->sess->se_sess->sup_prot_ops =
1387 conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1391 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1396 if (iscsi_login_zero_tsih_s2(conn) < 0)
1399 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1403 if (conn->conn_transport->iscsit_validate_params) {
1404 ret = conn->conn_transport->iscsit_validate_params(conn);
1413 ret = iscsi_target_start_negotiation(login, conn);
1417 iscsi_stop_login_thread_timer(np);
1420 tpg_np = conn->tpg_np;
1422 iscsi_post_login_handler(np, conn, zero_tsih);
1423 iscsit_deaccess_np(np, tpg, tpg_np);
1428 /* Get another socket */
1434 tpg_np = conn->tpg_np;
1435 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1439 iscsit_deaccess_np(np, tpg, tpg_np);
1448 iscsi_stop_login_thread_timer(np);
1449 spin_lock_bh(&np->np_thread_lock);
1450 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1451 spin_unlock_bh(&np->np_thread_lock);
1456 int iscsi_target_login_thread(void *arg)
1458 struct iscsi_np *np = arg;
1461 allow_signal(SIGINT);
1464 ret = __iscsi_target_login_thread(np);
1466 * We break and exit here unless another sock_accept() call
1473 while (!kthread_should_stop()) {