gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_login.c
blob731ee67fe914b211379fd8acad73053bd60e28b9
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * This file contains the login functions used by the iSCSI Target driver.
5 * (c) Copyright 2007-2013 Datera, Inc.
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9 ******************************************************************************/
11 #include <crypto/hash.h>
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/kthread.h>
15 #include <linux/sched/signal.h>
16 #include <linux/idr.h>
17 #include <linux/tcp.h> /* TCP_NODELAY */
18 #include <net/ipv6.h> /* ipv6_addr_v4mapped() */
19 #include <scsi/iscsi_proto.h>
20 #include <target/target_core_base.h>
21 #include <target/target_core_fabric.h>
23 #include <target/iscsi/iscsi_target_core.h>
24 #include <target/iscsi/iscsi_target_stat.h>
25 #include "iscsi_target_device.h"
26 #include "iscsi_target_nego.h"
27 #include "iscsi_target_erl0.h"
28 #include "iscsi_target_erl2.h"
29 #include "iscsi_target_login.h"
30 #include "iscsi_target_tpg.h"
31 #include "iscsi_target_util.h"
32 #include "iscsi_target.h"
33 #include "iscsi_target_parameters.h"
35 #include <target/iscsi/iscsi_transport.h>
37 static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
39 struct iscsi_login *login;
41 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
42 if (!login) {
43 pr_err("Unable to allocate memory for struct iscsi_login.\n");
44 return NULL;
46 conn->login = login;
47 login->conn = conn;
48 login->first_request = 1;
50 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
51 if (!login->req_buf) {
52 pr_err("Unable to allocate memory for response buffer.\n");
53 goto out_login;
56 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
57 if (!login->rsp_buf) {
58 pr_err("Unable to allocate memory for request buffer.\n");
59 goto out_req_buf;
62 conn->conn_login = login;
64 return login;
66 out_req_buf:
67 kfree(login->req_buf);
68 out_login:
69 kfree(login);
70 return NULL;
74 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
75 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
77 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
79 struct crypto_ahash *tfm;
82 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
83 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
84 * to software 1x8 byte slicing from crc32c.ko
86 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
87 if (IS_ERR(tfm)) {
88 pr_err("crypto_alloc_ahash() failed\n");
89 return -ENOMEM;
92 conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
93 if (!conn->conn_rx_hash) {
94 pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
95 crypto_free_ahash(tfm);
96 return -ENOMEM;
98 ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
100 conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
101 if (!conn->conn_tx_hash) {
102 pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
103 ahash_request_free(conn->conn_rx_hash);
104 conn->conn_rx_hash = NULL;
105 crypto_free_ahash(tfm);
106 return -ENOMEM;
108 ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
110 return 0;
113 static int iscsi_login_check_initiator_version(
114 struct iscsi_conn *conn,
115 u8 version_max,
116 u8 version_min)
118 if ((version_max != 0x00) || (version_min != 0x00)) {
119 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
120 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
121 version_min, version_max);
122 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
123 ISCSI_LOGIN_STATUS_NO_VERSION);
124 return -1;
127 return 0;
130 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
132 int sessiontype;
133 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
134 struct iscsi_portal_group *tpg = conn->tpg;
135 struct iscsi_session *sess = NULL, *sess_p = NULL;
136 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
137 struct se_session *se_sess, *se_sess_tmp;
139 initiatorname_param = iscsi_find_param_from_key(
140 INITIATORNAME, conn->param_list);
141 sessiontype_param = iscsi_find_param_from_key(
142 SESSIONTYPE, conn->param_list);
143 if (!initiatorname_param || !sessiontype_param) {
144 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
145 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
146 return -1;
149 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
151 spin_lock_bh(&se_tpg->session_lock);
152 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
153 sess_list) {
155 sess_p = se_sess->fabric_sess_ptr;
156 spin_lock(&sess_p->conn_lock);
157 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
158 atomic_read(&sess_p->session_logout) ||
159 atomic_read(&sess_p->session_close) ||
160 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
161 spin_unlock(&sess_p->conn_lock);
162 continue;
164 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
165 (!strcmp(sess_p->sess_ops->InitiatorName,
166 initiatorname_param->value) &&
167 (sess_p->sess_ops->SessionType == sessiontype))) {
168 atomic_set(&sess_p->session_reinstatement, 1);
169 atomic_set(&sess_p->session_fall_back_to_erl0, 1);
170 atomic_set(&sess_p->session_close, 1);
171 spin_unlock(&sess_p->conn_lock);
172 iscsit_inc_session_usage_count(sess_p);
173 iscsit_stop_time2retain_timer(sess_p);
174 sess = sess_p;
175 break;
177 spin_unlock(&sess_p->conn_lock);
179 spin_unlock_bh(&se_tpg->session_lock);
181 * If the Time2Retain handler has expired, the session is already gone.
183 if (!sess)
184 return 0;
186 pr_debug("%s iSCSI Session SID %u is still active for %s,"
187 " performing session reinstatement.\n", (sessiontype) ?
188 "Discovery" : "Normal", sess->sid,
189 sess->sess_ops->InitiatorName);
191 spin_lock_bh(&sess->conn_lock);
192 if (sess->session_state == TARG_SESS_STATE_FAILED) {
193 spin_unlock_bh(&sess->conn_lock);
194 iscsit_dec_session_usage_count(sess);
195 return 0;
197 spin_unlock_bh(&sess->conn_lock);
199 iscsit_stop_session(sess, 1, 1);
200 iscsit_dec_session_usage_count(sess);
202 return 0;
205 static int iscsi_login_set_conn_values(
206 struct iscsi_session *sess,
207 struct iscsi_conn *conn,
208 __be16 cid)
210 int ret;
211 conn->sess = sess;
212 conn->cid = be16_to_cpu(cid);
214 * Generate a random Status sequence number (statsn) for the new
215 * iSCSI connection.
217 ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32));
218 if (unlikely(ret))
219 return ret;
221 mutex_lock(&auth_id_lock);
222 conn->auth_id = iscsit_global->auth_id++;
223 mutex_unlock(&auth_id_lock);
224 return 0;
227 __printf(2, 3) int iscsi_change_param_sprintf(
228 struct iscsi_conn *conn,
229 const char *fmt, ...)
231 va_list args;
232 unsigned char buf[64];
234 memset(buf, 0, sizeof buf);
236 va_start(args, fmt);
237 vsnprintf(buf, sizeof buf, fmt, args);
238 va_end(args);
240 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
241 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
242 ISCSI_LOGIN_STATUS_NO_RESOURCES);
243 return -1;
246 return 0;
248 EXPORT_SYMBOL(iscsi_change_param_sprintf);
251 * This is the leading connection of a new session,
252 * or session reinstatement.
254 static int iscsi_login_zero_tsih_s1(
255 struct iscsi_conn *conn,
256 unsigned char *buf)
258 struct iscsi_session *sess = NULL;
259 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
260 int ret;
262 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
263 if (!sess) {
264 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
265 ISCSI_LOGIN_STATUS_NO_RESOURCES);
266 pr_err("Could not allocate memory for session\n");
267 return -ENOMEM;
270 if (iscsi_login_set_conn_values(sess, conn, pdu->cid))
271 goto free_sess;
273 sess->init_task_tag = pdu->itt;
274 memcpy(&sess->isid, pdu->isid, 6);
275 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
276 INIT_LIST_HEAD(&sess->sess_conn_list);
277 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
278 INIT_LIST_HEAD(&sess->cr_active_list);
279 INIT_LIST_HEAD(&sess->cr_inactive_list);
280 init_completion(&sess->async_msg_comp);
281 init_completion(&sess->reinstatement_comp);
282 init_completion(&sess->session_wait_comp);
283 init_completion(&sess->session_waiting_on_uc_comp);
284 mutex_init(&sess->cmdsn_mutex);
285 spin_lock_init(&sess->conn_lock);
286 spin_lock_init(&sess->cr_a_lock);
287 spin_lock_init(&sess->cr_i_lock);
288 spin_lock_init(&sess->session_usage_lock);
289 spin_lock_init(&sess->ttt_lock);
291 timer_setup(&sess->time2retain_timer,
292 iscsit_handle_time2retain_timeout, 0);
294 ret = ida_alloc(&sess_ida, GFP_KERNEL);
295 if (ret < 0) {
296 pr_err("Session ID allocation failed %d\n", ret);
297 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
298 ISCSI_LOGIN_STATUS_NO_RESOURCES);
299 goto free_sess;
302 sess->session_index = ret;
303 sess->creation_time = get_jiffies_64();
305 * The FFP CmdSN window values will be allocated from the TPG's
306 * Initiator Node's ACL once the login has been successfully completed.
308 atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
310 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
311 if (!sess->sess_ops) {
312 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
313 ISCSI_LOGIN_STATUS_NO_RESOURCES);
314 pr_err("Unable to allocate memory for"
315 " struct iscsi_sess_ops.\n");
316 goto free_id;
319 sess->se_sess = transport_alloc_session(TARGET_PROT_NORMAL);
320 if (IS_ERR(sess->se_sess)) {
321 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
322 ISCSI_LOGIN_STATUS_NO_RESOURCES);
323 goto free_ops;
326 return 0;
328 free_ops:
329 kfree(sess->sess_ops);
330 free_id:
331 ida_free(&sess_ida, sess->session_index);
332 free_sess:
333 kfree(sess);
334 conn->sess = NULL;
335 return -ENOMEM;
338 static int iscsi_login_zero_tsih_s2(
339 struct iscsi_conn *conn)
341 struct iscsi_node_attrib *na;
342 struct iscsi_session *sess = conn->sess;
343 bool iser = false;
345 sess->tpg = conn->tpg;
348 * Assign a new TPG Session Handle. Note this is protected with
349 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
351 sess->tsih = ++sess->tpg->ntsih;
352 if (!sess->tsih)
353 sess->tsih = ++sess->tpg->ntsih;
356 * Create the default params from user defined values..
358 if (iscsi_copy_param_list(&conn->param_list,
359 conn->tpg->param_list, 1) < 0) {
360 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
361 ISCSI_LOGIN_STATUS_NO_RESOURCES);
362 return -1;
365 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
366 iser = true;
368 iscsi_set_keys_to_negotiate(conn->param_list, iser);
370 if (sess->sess_ops->SessionType)
371 return iscsi_set_keys_irrelevant_for_discovery(
372 conn->param_list);
374 na = iscsit_tpg_get_node_attrib(sess);
377 * Need to send TargetPortalGroupTag back in first login response
378 * on any iSCSI connection where the Initiator provides TargetName.
379 * See 5.3.1. Login Phase Start
381 * In our case, we have already located the struct iscsi_tiqn at this point.
383 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
384 return -1;
387 * Workaround for Initiators that have broken connection recovery logic.
389 * "We would really like to get rid of this." Linux-iSCSI.org team
391 if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
392 return -1;
395 * Set RDMAExtensions=Yes by default for iSER enabled network portals
397 if (iser) {
398 struct iscsi_param *param;
399 unsigned long mrdsl, off;
400 int rc;
402 if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
403 return -1;
406 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
407 * Immediate Data + Unsolicited Data-OUT if necessary..
409 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
410 conn->param_list);
411 if (!param) {
412 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
413 ISCSI_LOGIN_STATUS_NO_RESOURCES);
414 return -1;
416 rc = kstrtoul(param->value, 0, &mrdsl);
417 if (rc < 0) {
418 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
419 ISCSI_LOGIN_STATUS_NO_RESOURCES);
420 return -1;
422 off = mrdsl % PAGE_SIZE;
423 if (!off)
424 goto check_prot;
426 if (mrdsl < PAGE_SIZE)
427 mrdsl = PAGE_SIZE;
428 else
429 mrdsl -= off;
431 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
432 " to PAGE_SIZE\n", mrdsl);
434 if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
435 return -1;
437 * ISER currently requires that ImmediateData + Unsolicited
438 * Data be disabled when protection / signature MRs are enabled.
440 check_prot:
441 if (sess->se_sess->sup_prot_ops &
442 (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
443 TARGET_PROT_DOUT_INSERT)) {
445 if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
446 return -1;
448 if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
449 return -1;
451 pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
452 " T10-PI enabled ISER session\n");
456 return 0;
459 static int iscsi_login_non_zero_tsih_s1(
460 struct iscsi_conn *conn,
461 unsigned char *buf)
463 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
465 return iscsi_login_set_conn_values(NULL, conn, pdu->cid);
469 * Add a new connection to an existing session.
471 static int iscsi_login_non_zero_tsih_s2(
472 struct iscsi_conn *conn,
473 unsigned char *buf)
475 struct iscsi_portal_group *tpg = conn->tpg;
476 struct iscsi_session *sess = NULL, *sess_p = NULL;
477 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
478 struct se_session *se_sess, *se_sess_tmp;
479 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
480 bool iser = false;
482 spin_lock_bh(&se_tpg->session_lock);
483 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
484 sess_list) {
486 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
487 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
488 atomic_read(&sess_p->session_logout) ||
489 atomic_read(&sess_p->session_close) ||
490 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
491 continue;
492 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
493 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
494 iscsit_inc_session_usage_count(sess_p);
495 iscsit_stop_time2retain_timer(sess_p);
496 sess = sess_p;
497 break;
500 spin_unlock_bh(&se_tpg->session_lock);
503 * If the Time2Retain handler has expired, the session is already gone.
505 if (!sess) {
506 pr_err("Initiator attempting to add a connection to"
507 " a non-existent session, rejecting iSCSI Login.\n");
508 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
509 ISCSI_LOGIN_STATUS_NO_SESSION);
510 return -1;
514 * Stop the Time2Retain timer if this is a failed session, we restart
515 * the timer if the login is not successful.
517 spin_lock_bh(&sess->conn_lock);
518 if (sess->session_state == TARG_SESS_STATE_FAILED)
519 atomic_set(&sess->session_continuation, 1);
520 spin_unlock_bh(&sess->conn_lock);
522 if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 ||
523 iscsi_copy_param_list(&conn->param_list,
524 conn->tpg->param_list, 0) < 0) {
525 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
526 ISCSI_LOGIN_STATUS_NO_RESOURCES);
527 return -1;
530 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
531 iser = true;
533 iscsi_set_keys_to_negotiate(conn->param_list, iser);
535 * Need to send TargetPortalGroupTag back in first login response
536 * on any iSCSI connection where the Initiator provides TargetName.
537 * See 5.3.1. Login Phase Start
539 * In our case, we have already located the struct iscsi_tiqn at this point.
541 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
542 return -1;
544 return 0;
547 int iscsi_login_post_auth_non_zero_tsih(
548 struct iscsi_conn *conn,
549 u16 cid,
550 u32 exp_statsn)
552 struct iscsi_conn *conn_ptr = NULL;
553 struct iscsi_conn_recovery *cr = NULL;
554 struct iscsi_session *sess = conn->sess;
557 * By following item 5 in the login table, if we have found
558 * an existing ISID and a valid/existing TSIH and an existing
559 * CID we do connection reinstatement. Currently we dont not
560 * support it so we send back an non-zero status class to the
561 * initiator and release the new connection.
563 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
564 if (conn_ptr) {
565 pr_err("Connection exists with CID %hu for %s,"
566 " performing connection reinstatement.\n",
567 conn_ptr->cid, sess->sess_ops->InitiatorName);
569 iscsit_connection_reinstatement_rcfr(conn_ptr);
570 iscsit_dec_conn_usage_count(conn_ptr);
574 * Check for any connection recovery entries containing CID.
575 * We use the original ExpStatSN sent in the first login request
576 * to acknowledge commands for the failed connection.
578 * Also note that an explict logout may have already been sent,
579 * but the response may not be sent due to additional connection
580 * loss.
582 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
583 cr = iscsit_get_inactive_connection_recovery_entry(
584 sess, cid);
585 if (cr) {
586 pr_debug("Performing implicit logout"
587 " for connection recovery on CID: %hu\n",
588 conn->cid);
589 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
594 * Else we follow item 4 from the login table in that we have
595 * found an existing ISID and a valid/existing TSIH and a new
596 * CID we go ahead and continue to add a new connection to the
597 * session.
599 pr_debug("Adding CID %hu to existing session for %s.\n",
600 cid, sess->sess_ops->InitiatorName);
602 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
603 pr_err("Adding additional connection to this session"
604 " would exceed MaxConnections %d, login failed.\n",
605 sess->sess_ops->MaxConnections);
606 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
607 ISCSI_LOGIN_STATUS_ISID_ERROR);
608 return -1;
611 return 0;
614 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
616 struct iscsi_session *sess = conn->sess;
618 * FIXME: Unsolicited NopIN support for ISER
620 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
621 return;
623 if (!sess->sess_ops->SessionType)
624 iscsit_start_nopin_timer(conn);
627 int iscsit_start_kthreads(struct iscsi_conn *conn)
629 int ret = 0;
631 spin_lock(&iscsit_global->ts_bitmap_lock);
632 conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
633 ISCSIT_BITMAP_BITS, get_order(1));
634 spin_unlock(&iscsit_global->ts_bitmap_lock);
636 if (conn->bitmap_id < 0) {
637 pr_err("bitmap_find_free_region() failed for"
638 " iscsit_start_kthreads()\n");
639 return -ENOMEM;
642 conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
643 "%s", ISCSI_TX_THREAD_NAME);
644 if (IS_ERR(conn->tx_thread)) {
645 pr_err("Unable to start iscsi_target_tx_thread\n");
646 ret = PTR_ERR(conn->tx_thread);
647 goto out_bitmap;
649 conn->tx_thread_active = true;
651 conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
652 "%s", ISCSI_RX_THREAD_NAME);
653 if (IS_ERR(conn->rx_thread)) {
654 pr_err("Unable to start iscsi_target_rx_thread\n");
655 ret = PTR_ERR(conn->rx_thread);
656 goto out_tx;
658 conn->rx_thread_active = true;
660 return 0;
661 out_tx:
662 send_sig(SIGINT, conn->tx_thread, 1);
663 kthread_stop(conn->tx_thread);
664 conn->tx_thread_active = false;
665 out_bitmap:
666 spin_lock(&iscsit_global->ts_bitmap_lock);
667 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
668 get_order(1));
669 spin_unlock(&iscsit_global->ts_bitmap_lock);
670 return ret;
673 void iscsi_post_login_handler(
674 struct iscsi_np *np,
675 struct iscsi_conn *conn,
676 u8 zero_tsih)
678 int stop_timer = 0;
679 struct iscsi_session *sess = conn->sess;
680 struct se_session *se_sess = sess->se_sess;
681 struct iscsi_portal_group *tpg = sess->tpg;
682 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
684 iscsit_inc_conn_usage_count(conn);
686 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
687 ISCSI_LOGIN_STATUS_ACCEPT);
689 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
690 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
692 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
694 * SCSI Initiator -> SCSI Target Port Mapping
696 if (!zero_tsih) {
697 iscsi_set_session_parameters(sess->sess_ops,
698 conn->param_list, 0);
699 iscsi_release_param_list(conn->param_list);
700 conn->param_list = NULL;
702 spin_lock_bh(&sess->conn_lock);
703 atomic_set(&sess->session_continuation, 0);
704 if (sess->session_state == TARG_SESS_STATE_FAILED) {
705 pr_debug("Moving to"
706 " TARG_SESS_STATE_LOGGED_IN.\n");
707 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
708 stop_timer = 1;
711 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
712 " %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
713 &conn->local_sockaddr, tpg->tpgt);
715 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
716 atomic_inc(&sess->nconn);
717 pr_debug("Incremented iSCSI Connection count to %hu"
718 " from node: %s\n", atomic_read(&sess->nconn),
719 sess->sess_ops->InitiatorName);
720 spin_unlock_bh(&sess->conn_lock);
722 iscsi_post_login_start_timers(conn);
724 * Determine CPU mask to ensure connection's RX and TX kthreads
725 * are scheduled on the same CPU.
727 iscsit_thread_get_cpumask(conn);
728 conn->conn_rx_reset_cpumask = 1;
729 conn->conn_tx_reset_cpumask = 1;
731 * Wakeup the sleeping iscsi_target_rx_thread() now that
732 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
734 complete(&conn->rx_login_comp);
735 iscsit_dec_conn_usage_count(conn);
737 if (stop_timer) {
738 spin_lock_bh(&se_tpg->session_lock);
739 iscsit_stop_time2retain_timer(sess);
740 spin_unlock_bh(&se_tpg->session_lock);
742 iscsit_dec_session_usage_count(sess);
743 return;
746 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
747 iscsi_release_param_list(conn->param_list);
748 conn->param_list = NULL;
750 iscsit_determine_maxcmdsn(sess);
752 spin_lock_bh(&se_tpg->session_lock);
753 __transport_register_session(&sess->tpg->tpg_se_tpg,
754 se_sess->se_node_acl, se_sess, sess);
755 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
756 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
758 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
759 conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
760 tpg->tpgt);
762 spin_lock_bh(&sess->conn_lock);
763 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
764 atomic_inc(&sess->nconn);
765 pr_debug("Incremented iSCSI Connection count to %hu from node:"
766 " %s\n", atomic_read(&sess->nconn),
767 sess->sess_ops->InitiatorName);
768 spin_unlock_bh(&sess->conn_lock);
770 sess->sid = tpg->sid++;
771 if (!sess->sid)
772 sess->sid = tpg->sid++;
773 pr_debug("Established iSCSI session from node: %s\n",
774 sess->sess_ops->InitiatorName);
776 tpg->nsessions++;
777 if (tpg->tpg_tiqn)
778 tpg->tpg_tiqn->tiqn_nsessions++;
780 pr_debug("Incremented number of active iSCSI sessions to %u on"
781 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
782 spin_unlock_bh(&se_tpg->session_lock);
784 iscsi_post_login_start_timers(conn);
786 * Determine CPU mask to ensure connection's RX and TX kthreads
787 * are scheduled on the same CPU.
789 iscsit_thread_get_cpumask(conn);
790 conn->conn_rx_reset_cpumask = 1;
791 conn->conn_tx_reset_cpumask = 1;
793 * Wakeup the sleeping iscsi_target_rx_thread() now that
794 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
796 complete(&conn->rx_login_comp);
797 iscsit_dec_conn_usage_count(conn);
800 void iscsi_handle_login_thread_timeout(struct timer_list *t)
802 struct iscsi_np *np = from_timer(np, t, np_login_timer);
804 spin_lock_bh(&np->np_thread_lock);
805 pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
806 &np->np_sockaddr);
808 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
809 spin_unlock_bh(&np->np_thread_lock);
810 return;
813 if (np->np_thread)
814 send_sig(SIGINT, np->np_thread, 1);
816 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
817 spin_unlock_bh(&np->np_thread_lock);
820 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
823 * This used the TA_LOGIN_TIMEOUT constant because at this
824 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
826 spin_lock_bh(&np->np_thread_lock);
827 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
828 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
829 mod_timer(&np->np_login_timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
831 pr_debug("Added timeout timer to iSCSI login request for"
832 " %u seconds.\n", TA_LOGIN_TIMEOUT);
833 spin_unlock_bh(&np->np_thread_lock);
836 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
838 spin_lock_bh(&np->np_thread_lock);
839 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
840 spin_unlock_bh(&np->np_thread_lock);
841 return;
843 np->np_login_timer_flags |= ISCSI_TF_STOP;
844 spin_unlock_bh(&np->np_thread_lock);
846 del_timer_sync(&np->np_login_timer);
848 spin_lock_bh(&np->np_thread_lock);
849 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
850 spin_unlock_bh(&np->np_thread_lock);
853 int iscsit_setup_np(
854 struct iscsi_np *np,
855 struct sockaddr_storage *sockaddr)
857 struct socket *sock = NULL;
858 int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
860 switch (np->np_network_transport) {
861 case ISCSI_TCP:
862 np->np_ip_proto = IPPROTO_TCP;
863 np->np_sock_type = SOCK_STREAM;
864 break;
865 case ISCSI_SCTP_TCP:
866 np->np_ip_proto = IPPROTO_SCTP;
867 np->np_sock_type = SOCK_STREAM;
868 break;
869 case ISCSI_SCTP_UDP:
870 np->np_ip_proto = IPPROTO_SCTP;
871 np->np_sock_type = SOCK_SEQPACKET;
872 break;
873 default:
874 pr_err("Unsupported network_transport: %d\n",
875 np->np_network_transport);
876 return -EINVAL;
879 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
880 np->np_ip_proto, &sock);
881 if (ret < 0) {
882 pr_err("sock_create() failed.\n");
883 return ret;
885 np->np_socket = sock;
887 * Setup the np->np_sockaddr from the passed sockaddr setup
888 * in iscsi_target_configfs.c code..
890 memcpy(&np->np_sockaddr, sockaddr,
891 sizeof(struct sockaddr_storage));
893 if (sockaddr->ss_family == AF_INET6)
894 len = sizeof(struct sockaddr_in6);
895 else
896 len = sizeof(struct sockaddr_in);
898 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
900 /* FIXME: Someone please explain why this is endian-safe */
901 opt = 1;
902 if (np->np_network_transport == ISCSI_TCP) {
903 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
904 (char *)&opt, sizeof(opt));
905 if (ret < 0) {
906 pr_err("kernel_setsockopt() for TCP_NODELAY"
907 " failed: %d\n", ret);
908 goto fail;
912 /* FIXME: Someone please explain why this is endian-safe */
913 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
914 (char *)&opt, sizeof(opt));
915 if (ret < 0) {
916 pr_err("kernel_setsockopt() for SO_REUSEADDR"
917 " failed\n");
918 goto fail;
921 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
922 (char *)&opt, sizeof(opt));
923 if (ret < 0) {
924 pr_err("kernel_setsockopt() for IP_FREEBIND"
925 " failed\n");
926 goto fail;
929 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
930 if (ret < 0) {
931 pr_err("kernel_bind() failed: %d\n", ret);
932 goto fail;
935 ret = kernel_listen(sock, backlog);
936 if (ret != 0) {
937 pr_err("kernel_listen() failed: %d\n", ret);
938 goto fail;
941 return 0;
942 fail:
943 np->np_socket = NULL;
944 sock_release(sock);
945 return ret;
948 int iscsi_target_setup_login_socket(
949 struct iscsi_np *np,
950 struct sockaddr_storage *sockaddr)
952 struct iscsit_transport *t;
953 int rc;
955 t = iscsit_get_transport(np->np_network_transport);
956 if (!t)
957 return -EINVAL;
959 rc = t->iscsit_setup_np(np, sockaddr);
960 if (rc < 0) {
961 iscsit_put_transport(t);
962 return rc;
965 np->np_transport = t;
966 np->enabled = true;
967 return 0;
970 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
972 struct socket *new_sock, *sock = np->np_socket;
973 struct sockaddr_in sock_in;
974 struct sockaddr_in6 sock_in6;
975 int rc;
977 rc = kernel_accept(sock, &new_sock, 0);
978 if (rc < 0)
979 return rc;
981 conn->sock = new_sock;
982 conn->login_family = np->np_sockaddr.ss_family;
984 if (np->np_sockaddr.ss_family == AF_INET6) {
985 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
987 rc = conn->sock->ops->getname(conn->sock,
988 (struct sockaddr *)&sock_in6, 1);
989 if (rc >= 0) {
990 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
991 memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
992 } else {
993 /* Pretend to be an ipv4 socket */
994 sock_in.sin_family = AF_INET;
995 sock_in.sin_port = sock_in6.sin6_port;
996 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
997 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1001 rc = conn->sock->ops->getname(conn->sock,
1002 (struct sockaddr *)&sock_in6, 0);
1003 if (rc >= 0) {
1004 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1005 memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
1006 } else {
1007 /* Pretend to be an ipv4 socket */
1008 sock_in.sin_family = AF_INET;
1009 sock_in.sin_port = sock_in6.sin6_port;
1010 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1011 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1014 } else {
1015 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1017 rc = conn->sock->ops->getname(conn->sock,
1018 (struct sockaddr *)&sock_in, 1);
1019 if (rc >= 0)
1020 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1022 rc = conn->sock->ops->getname(conn->sock,
1023 (struct sockaddr *)&sock_in, 0);
1024 if (rc >= 0)
1025 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1028 return 0;
1031 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1033 struct iscsi_login_req *login_req;
1034 u32 padding = 0, payload_length;
1036 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1037 return -1;
1039 login_req = (struct iscsi_login_req *)login->req;
1040 payload_length = ntoh24(login_req->dlength);
1041 padding = ((-payload_length) & 3);
1043 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1044 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1045 login_req->flags, login_req->itt, login_req->cmdsn,
1046 login_req->exp_statsn, login_req->cid, payload_length);
1048 * Setup the initial iscsi_login values from the leading
1049 * login request PDU.
1051 if (login->first_request) {
1052 login_req = (struct iscsi_login_req *)login->req;
1053 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1054 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1055 login->version_min = login_req->min_version;
1056 login->version_max = login_req->max_version;
1057 memcpy(login->isid, login_req->isid, 6);
1058 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1059 login->init_task_tag = login_req->itt;
1060 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1061 login->cid = be16_to_cpu(login_req->cid);
1062 login->tsih = be16_to_cpu(login_req->tsih);
1065 if (iscsi_target_check_login_request(conn, login) < 0)
1066 return -1;
1068 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1069 if (iscsi_login_rx_data(conn, login->req_buf,
1070 payload_length + padding) < 0)
1071 return -1;
1073 return 0;
1076 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1077 u32 length)
1079 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1080 return -1;
1082 return 0;
1085 static int
1086 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1088 int rc;
1090 if (!t->owner) {
1091 conn->conn_transport = t;
1092 return 0;
1095 rc = try_module_get(t->owner);
1096 if (!rc) {
1097 pr_err("try_module_get() failed for %s\n", t->name);
1098 return -EINVAL;
1101 conn->conn_transport = t;
1102 return 0;
1105 static struct iscsi_conn *iscsit_alloc_conn(struct iscsi_np *np)
1107 struct iscsi_conn *conn;
1109 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1110 if (!conn) {
1111 pr_err("Could not allocate memory for new connection\n");
1112 return NULL;
1114 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1115 conn->conn_state = TARG_CONN_STATE_FREE;
1117 init_waitqueue_head(&conn->queues_wq);
1118 INIT_LIST_HEAD(&conn->conn_list);
1119 INIT_LIST_HEAD(&conn->conn_cmd_list);
1120 INIT_LIST_HEAD(&conn->immed_queue_list);
1121 INIT_LIST_HEAD(&conn->response_queue_list);
1122 init_completion(&conn->conn_post_wait_comp);
1123 init_completion(&conn->conn_wait_comp);
1124 init_completion(&conn->conn_wait_rcfr_comp);
1125 init_completion(&conn->conn_waiting_on_uc_comp);
1126 init_completion(&conn->conn_logout_comp);
1127 init_completion(&conn->rx_half_close_comp);
1128 init_completion(&conn->tx_half_close_comp);
1129 init_completion(&conn->rx_login_comp);
1130 spin_lock_init(&conn->cmd_lock);
1131 spin_lock_init(&conn->conn_usage_lock);
1132 spin_lock_init(&conn->immed_queue_lock);
1133 spin_lock_init(&conn->nopin_timer_lock);
1134 spin_lock_init(&conn->response_queue_lock);
1135 spin_lock_init(&conn->state_lock);
1137 timer_setup(&conn->nopin_response_timer,
1138 iscsit_handle_nopin_response_timeout, 0);
1139 timer_setup(&conn->nopin_timer, iscsit_handle_nopin_timeout, 0);
1141 if (iscsit_conn_set_transport(conn, np->np_transport) < 0)
1142 goto free_conn;
1144 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
1145 if (!conn->conn_ops) {
1146 pr_err("Unable to allocate memory for struct iscsi_conn_ops.\n");
1147 goto put_transport;
1150 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
1151 pr_err("Unable to allocate conn->conn_cpumask\n");
1152 goto free_conn_ops;
1155 return conn;
1157 free_conn_ops:
1158 kfree(conn->conn_ops);
1159 put_transport:
1160 iscsit_put_transport(conn->conn_transport);
1161 free_conn:
1162 kfree(conn);
1163 return NULL;
1166 void iscsit_free_conn(struct iscsi_conn *conn)
1168 free_cpumask_var(conn->conn_cpumask);
1169 kfree(conn->conn_ops);
1170 iscsit_put_transport(conn->conn_transport);
1171 kfree(conn);
1174 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1175 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1177 if (!new_sess)
1178 goto old_sess_out;
1180 pr_err("iSCSI Login negotiation failed.\n");
1181 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1182 ISCSI_LOGIN_STATUS_INIT_ERR);
1183 if (!zero_tsih || !conn->sess)
1184 goto old_sess_out;
1186 transport_free_session(conn->sess->se_sess);
1187 ida_free(&sess_ida, conn->sess->session_index);
1188 kfree(conn->sess->sess_ops);
1189 kfree(conn->sess);
1190 conn->sess = NULL;
1192 old_sess_out:
1193 iscsi_stop_login_thread_timer(np);
1195 * If login negotiation fails check if the Time2Retain timer
1196 * needs to be restarted.
1198 if (!zero_tsih && conn->sess) {
1199 spin_lock_bh(&conn->sess->conn_lock);
1200 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1201 struct se_portal_group *se_tpg =
1202 &conn->tpg->tpg_se_tpg;
1204 atomic_set(&conn->sess->session_continuation, 0);
1205 spin_unlock_bh(&conn->sess->conn_lock);
1206 spin_lock_bh(&se_tpg->session_lock);
1207 iscsit_start_time2retain_handler(conn->sess);
1208 spin_unlock_bh(&se_tpg->session_lock);
1209 } else
1210 spin_unlock_bh(&conn->sess->conn_lock);
1211 iscsit_dec_session_usage_count(conn->sess);
1214 ahash_request_free(conn->conn_tx_hash);
1215 if (conn->conn_rx_hash) {
1216 struct crypto_ahash *tfm;
1218 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
1219 ahash_request_free(conn->conn_rx_hash);
1220 crypto_free_ahash(tfm);
1223 if (conn->param_list) {
1224 iscsi_release_param_list(conn->param_list);
1225 conn->param_list = NULL;
1227 iscsi_target_nego_release(conn);
1229 if (conn->sock) {
1230 sock_release(conn->sock);
1231 conn->sock = NULL;
1234 if (conn->conn_transport->iscsit_wait_conn)
1235 conn->conn_transport->iscsit_wait_conn(conn);
1237 if (conn->conn_transport->iscsit_free_conn)
1238 conn->conn_transport->iscsit_free_conn(conn);
1240 iscsit_free_conn(conn);
1243 static int __iscsi_target_login_thread(struct iscsi_np *np)
1245 u8 *buffer, zero_tsih = 0;
1246 int ret = 0, rc;
1247 struct iscsi_conn *conn = NULL;
1248 struct iscsi_login *login;
1249 struct iscsi_portal_group *tpg = NULL;
1250 struct iscsi_login_req *pdu;
1251 struct iscsi_tpg_np *tpg_np;
1252 bool new_sess = false;
1254 flush_signals(current);
1256 spin_lock_bh(&np->np_thread_lock);
1257 if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1258 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1259 spin_unlock_bh(&np->np_thread_lock);
1260 complete(&np->np_restart_comp);
1261 return 1;
1262 } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1263 spin_unlock_bh(&np->np_thread_lock);
1264 goto exit;
1265 } else {
1266 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1268 spin_unlock_bh(&np->np_thread_lock);
1270 conn = iscsit_alloc_conn(np);
1271 if (!conn) {
1272 /* Get another socket */
1273 return 1;
1276 rc = np->np_transport->iscsit_accept_np(np, conn);
1277 if (rc == -ENOSYS) {
1278 complete(&np->np_restart_comp);
1279 iscsit_free_conn(conn);
1280 goto exit;
1281 } else if (rc < 0) {
1282 spin_lock_bh(&np->np_thread_lock);
1283 if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1284 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1285 spin_unlock_bh(&np->np_thread_lock);
1286 complete(&np->np_restart_comp);
1287 iscsit_free_conn(conn);
1288 /* Get another socket */
1289 return 1;
1291 spin_unlock_bh(&np->np_thread_lock);
1292 iscsit_free_conn(conn);
1293 return 1;
1296 * Perform the remaining iSCSI connection initialization items..
1298 login = iscsi_login_init_conn(conn);
1299 if (!login) {
1300 goto new_sess_out;
1303 iscsi_start_login_thread_timer(np);
1305 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1306 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1308 * This will process the first login request + payload..
1310 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1311 if (rc == 1)
1312 return 1;
1313 else if (rc < 0)
1314 goto new_sess_out;
1316 buffer = &login->req[0];
1317 pdu = (struct iscsi_login_req *)buffer;
1319 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1320 * when Status-Class != 0.
1322 conn->login_itt = pdu->itt;
1324 spin_lock_bh(&np->np_thread_lock);
1325 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1326 spin_unlock_bh(&np->np_thread_lock);
1327 pr_err("iSCSI Network Portal on %pISpc currently not"
1328 " active.\n", &np->np_sockaddr);
1329 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1330 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1331 goto new_sess_out;
1333 spin_unlock_bh(&np->np_thread_lock);
1335 conn->network_transport = np->np_network_transport;
1337 pr_debug("Received iSCSI login request from %pISpc on %s Network"
1338 " Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
1339 &conn->local_sockaddr);
1341 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1342 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1344 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1345 pdu->min_version) < 0)
1346 goto new_sess_out;
1348 zero_tsih = (pdu->tsih == 0x0000);
1349 if (zero_tsih) {
1351 * This is the leading connection of a new session.
1352 * We wait until after authentication to check for
1353 * session reinstatement.
1355 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1356 goto new_sess_out;
1357 } else {
1359 * Add a new connection to an existing session.
1360 * We check for a non-existant session in
1361 * iscsi_login_non_zero_tsih_s2() below based
1362 * on ISID/TSIH, but wait until after authentication
1363 * to check for connection reinstatement, etc.
1365 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1366 goto new_sess_out;
1369 * SessionType: Discovery
1371 * Locates Default Portal
1373 * SessionType: Normal
1375 * Locates Target Portal from NP -> Target IQN
1377 rc = iscsi_target_locate_portal(np, conn, login);
1378 if (rc < 0) {
1379 tpg = conn->tpg;
1380 goto new_sess_out;
1382 login->zero_tsih = zero_tsih;
1384 if (conn->sess)
1385 conn->sess->se_sess->sup_prot_ops =
1386 conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1388 tpg = conn->tpg;
1389 if (!tpg) {
1390 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1391 goto new_sess_out;
1394 if (zero_tsih) {
1395 if (iscsi_login_zero_tsih_s2(conn) < 0)
1396 goto new_sess_out;
1397 } else {
1398 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1399 goto old_sess_out;
1402 if (conn->conn_transport->iscsit_validate_params) {
1403 ret = conn->conn_transport->iscsit_validate_params(conn);
1404 if (ret < 0) {
1405 if (zero_tsih)
1406 goto new_sess_out;
1407 else
1408 goto old_sess_out;
1412 ret = iscsi_target_start_negotiation(login, conn);
1413 if (ret < 0)
1414 goto new_sess_out;
1416 iscsi_stop_login_thread_timer(np);
1418 if (ret == 1) {
1419 tpg_np = conn->tpg_np;
1421 iscsi_post_login_handler(np, conn, zero_tsih);
1422 iscsit_deaccess_np(np, tpg, tpg_np);
1425 tpg = NULL;
1426 tpg_np = NULL;
1427 /* Get another socket */
1428 return 1;
1430 new_sess_out:
1431 new_sess = true;
1432 old_sess_out:
1433 tpg_np = conn->tpg_np;
1434 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1435 new_sess = false;
1437 if (tpg) {
1438 iscsit_deaccess_np(np, tpg, tpg_np);
1439 tpg = NULL;
1440 tpg_np = NULL;
1443 return 1;
1445 exit:
1446 iscsi_stop_login_thread_timer(np);
1447 spin_lock_bh(&np->np_thread_lock);
1448 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1449 spin_unlock_bh(&np->np_thread_lock);
1451 return 0;
1454 int iscsi_target_login_thread(void *arg)
1456 struct iscsi_np *np = arg;
1457 int ret;
1459 allow_signal(SIGINT);
1461 while (1) {
1462 ret = __iscsi_target_login_thread(np);
1464 * We break and exit here unless another sock_accept() call
1465 * is expected.
1467 if (ret != 1)
1468 break;
1471 while (!kthread_should_stop()) {
1472 msleep(100);
1475 return 0;