fix a kmap leak in virtio_console
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_login.c
blobe29279e6b577dd564e8271f95c171838ead5ca39
1 /*******************************************************************************
2 * This file contains the login functions used by the iSCSI Target driver.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <linux/string.h>
20 #include <linux/kthread.h>
21 #include <linux/crypto.h>
22 #include <linux/idr.h>
23 #include <scsi/iscsi_proto.h>
24 #include <target/target_core_base.h>
25 #include <target/target_core_fabric.h>
27 #include "iscsi_target_core.h"
28 #include "iscsi_target_tq.h"
29 #include "iscsi_target_device.h"
30 #include "iscsi_target_nego.h"
31 #include "iscsi_target_erl0.h"
32 #include "iscsi_target_erl2.h"
33 #include "iscsi_target_login.h"
34 #include "iscsi_target_stat.h"
35 #include "iscsi_target_tpg.h"
36 #include "iscsi_target_util.h"
37 #include "iscsi_target.h"
38 #include "iscsi_target_parameters.h"
40 #include <target/iscsi/iscsi_transport.h>
42 static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
44 struct iscsi_login *login;
46 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
47 if (!login) {
48 pr_err("Unable to allocate memory for struct iscsi_login.\n");
49 return NULL;
51 conn->login = login;
52 login->conn = conn;
53 login->first_request = 1;
55 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
56 if (!login->req_buf) {
57 pr_err("Unable to allocate memory for response buffer.\n");
58 goto out_login;
61 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
62 if (!login->rsp_buf) {
63 pr_err("Unable to allocate memory for request buffer.\n");
64 goto out_req_buf;
67 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
68 if (!conn->conn_ops) {
69 pr_err("Unable to allocate memory for"
70 " struct iscsi_conn_ops.\n");
71 goto out_rsp_buf;
74 init_waitqueue_head(&conn->queues_wq);
75 INIT_LIST_HEAD(&conn->conn_list);
76 INIT_LIST_HEAD(&conn->conn_cmd_list);
77 INIT_LIST_HEAD(&conn->immed_queue_list);
78 INIT_LIST_HEAD(&conn->response_queue_list);
79 init_completion(&conn->conn_post_wait_comp);
80 init_completion(&conn->conn_wait_comp);
81 init_completion(&conn->conn_wait_rcfr_comp);
82 init_completion(&conn->conn_waiting_on_uc_comp);
83 init_completion(&conn->conn_logout_comp);
84 init_completion(&conn->rx_half_close_comp);
85 init_completion(&conn->tx_half_close_comp);
86 spin_lock_init(&conn->cmd_lock);
87 spin_lock_init(&conn->conn_usage_lock);
88 spin_lock_init(&conn->immed_queue_lock);
89 spin_lock_init(&conn->nopin_timer_lock);
90 spin_lock_init(&conn->response_queue_lock);
91 spin_lock_init(&conn->state_lock);
93 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
94 pr_err("Unable to allocate conn->conn_cpumask\n");
95 goto out_conn_ops;
97 conn->conn_login = login;
99 return login;
101 out_conn_ops:
102 kfree(conn->conn_ops);
103 out_rsp_buf:
104 kfree(login->rsp_buf);
105 out_req_buf:
106 kfree(login->req_buf);
107 out_login:
108 kfree(login);
109 return NULL;
113 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
114 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
116 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
119 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
120 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
121 * to software 1x8 byte slicing from crc32c.ko
123 conn->conn_rx_hash.flags = 0;
124 conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
125 CRYPTO_ALG_ASYNC);
126 if (IS_ERR(conn->conn_rx_hash.tfm)) {
127 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
128 return -ENOMEM;
131 conn->conn_tx_hash.flags = 0;
132 conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
133 CRYPTO_ALG_ASYNC);
134 if (IS_ERR(conn->conn_tx_hash.tfm)) {
135 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
136 crypto_free_hash(conn->conn_rx_hash.tfm);
137 return -ENOMEM;
140 return 0;
143 static int iscsi_login_check_initiator_version(
144 struct iscsi_conn *conn,
145 u8 version_max,
146 u8 version_min)
148 if ((version_max != 0x00) || (version_min != 0x00)) {
149 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
150 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
151 version_min, version_max);
152 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
153 ISCSI_LOGIN_STATUS_NO_VERSION);
154 return -1;
157 return 0;
160 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
162 int sessiontype;
163 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
164 struct iscsi_portal_group *tpg = conn->tpg;
165 struct iscsi_session *sess = NULL, *sess_p = NULL;
166 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
167 struct se_session *se_sess, *se_sess_tmp;
169 initiatorname_param = iscsi_find_param_from_key(
170 INITIATORNAME, conn->param_list);
171 sessiontype_param = iscsi_find_param_from_key(
172 SESSIONTYPE, conn->param_list);
173 if (!initiatorname_param || !sessiontype_param) {
174 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
175 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
176 return -1;
179 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
181 spin_lock_bh(&se_tpg->session_lock);
182 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
183 sess_list) {
185 sess_p = se_sess->fabric_sess_ptr;
186 spin_lock(&sess_p->conn_lock);
187 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
188 atomic_read(&sess_p->session_logout) ||
189 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
190 spin_unlock(&sess_p->conn_lock);
191 continue;
193 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
194 (!strcmp(sess_p->sess_ops->InitiatorName,
195 initiatorname_param->value) &&
196 (sess_p->sess_ops->SessionType == sessiontype))) {
197 atomic_set(&sess_p->session_reinstatement, 1);
198 spin_unlock(&sess_p->conn_lock);
199 iscsit_inc_session_usage_count(sess_p);
200 iscsit_stop_time2retain_timer(sess_p);
201 sess = sess_p;
202 break;
204 spin_unlock(&sess_p->conn_lock);
206 spin_unlock_bh(&se_tpg->session_lock);
208 * If the Time2Retain handler has expired, the session is already gone.
210 if (!sess)
211 return 0;
213 pr_debug("%s iSCSI Session SID %u is still active for %s,"
214 " preforming session reinstatement.\n", (sessiontype) ?
215 "Discovery" : "Normal", sess->sid,
216 sess->sess_ops->InitiatorName);
218 spin_lock_bh(&sess->conn_lock);
219 if (sess->session_state == TARG_SESS_STATE_FAILED) {
220 spin_unlock_bh(&sess->conn_lock);
221 iscsit_dec_session_usage_count(sess);
222 target_put_session(sess->se_sess);
223 return 0;
225 spin_unlock_bh(&sess->conn_lock);
227 iscsit_stop_session(sess, 1, 1);
228 iscsit_dec_session_usage_count(sess);
230 target_put_session(sess->se_sess);
231 return 0;
234 static void iscsi_login_set_conn_values(
235 struct iscsi_session *sess,
236 struct iscsi_conn *conn,
237 __be16 cid)
239 conn->sess = sess;
240 conn->cid = be16_to_cpu(cid);
242 * Generate a random Status sequence number (statsn) for the new
243 * iSCSI connection.
245 get_random_bytes(&conn->stat_sn, sizeof(u32));
247 mutex_lock(&auth_id_lock);
248 conn->auth_id = iscsit_global->auth_id++;
249 mutex_unlock(&auth_id_lock);
253 * This is the leading connection of a new session,
254 * or session reinstatement.
256 static int iscsi_login_zero_tsih_s1(
257 struct iscsi_conn *conn,
258 unsigned char *buf)
260 struct iscsi_session *sess = NULL;
261 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
262 int ret;
264 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
265 if (!sess) {
266 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
267 ISCSI_LOGIN_STATUS_NO_RESOURCES);
268 pr_err("Could not allocate memory for session\n");
269 return -ENOMEM;
272 iscsi_login_set_conn_values(sess, conn, pdu->cid);
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 idr_preload(GFP_KERNEL);
292 spin_lock_bh(&sess_idr_lock);
293 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
294 if (ret >= 0)
295 sess->session_index = ret;
296 spin_unlock_bh(&sess_idr_lock);
297 idr_preload_end();
299 if (ret < 0) {
300 pr_err("idr_alloc() for sess_idr failed\n");
301 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
302 ISCSI_LOGIN_STATUS_NO_RESOURCES);
303 kfree(sess);
304 return -ENOMEM;
307 sess->creation_time = get_jiffies_64();
309 * The FFP CmdSN window values will be allocated from the TPG's
310 * Initiator Node's ACL once the login has been successfully completed.
312 sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn);
314 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
315 if (!sess->sess_ops) {
316 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
317 ISCSI_LOGIN_STATUS_NO_RESOURCES);
318 pr_err("Unable to allocate memory for"
319 " struct iscsi_sess_ops.\n");
320 kfree(sess);
321 return -ENOMEM;
324 sess->se_sess = transport_init_session();
325 if (IS_ERR(sess->se_sess)) {
326 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
327 ISCSI_LOGIN_STATUS_NO_RESOURCES);
328 kfree(sess);
329 return -ENOMEM;
332 return 0;
335 static int iscsi_login_zero_tsih_s2(
336 struct iscsi_conn *conn)
338 struct iscsi_node_attrib *na;
339 struct iscsi_session *sess = conn->sess;
340 unsigned char buf[32];
341 bool iser = false;
343 sess->tpg = conn->tpg;
346 * Assign a new TPG Session Handle. Note this is protected with
347 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
349 sess->tsih = ++sess->tpg->ntsih;
350 if (!sess->tsih)
351 sess->tsih = ++sess->tpg->ntsih;
354 * Create the default params from user defined values..
356 if (iscsi_copy_param_list(&conn->param_list,
357 conn->tpg->param_list, 1) < 0) {
358 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
359 ISCSI_LOGIN_STATUS_NO_RESOURCES);
360 return -1;
363 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
364 iser = true;
366 iscsi_set_keys_to_negotiate(conn->param_list, iser);
368 if (sess->sess_ops->SessionType)
369 return iscsi_set_keys_irrelevant_for_discovery(
370 conn->param_list);
372 na = iscsit_tpg_get_node_attrib(sess);
375 * Need to send TargetPortalGroupTag back in first login response
376 * on any iSCSI connection where the Initiator provides TargetName.
377 * See 5.3.1. Login Phase Start
379 * In our case, we have already located the struct iscsi_tiqn at this point.
381 memset(buf, 0, 32);
382 sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->tpgt);
383 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
384 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
385 ISCSI_LOGIN_STATUS_NO_RESOURCES);
386 return -1;
390 * Workaround for Initiators that have broken connection recovery logic.
392 * "We would really like to get rid of this." Linux-iSCSI.org team
394 memset(buf, 0, 32);
395 sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
396 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
397 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
398 ISCSI_LOGIN_STATUS_NO_RESOURCES);
399 return -1;
402 if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
403 return -1;
405 * Set RDMAExtensions=Yes by default for iSER enabled network portals
407 if (iser) {
408 struct iscsi_param *param;
409 unsigned long mrdsl, off;
410 int rc;
412 sprintf(buf, "RDMAExtensions=Yes");
413 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
414 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
415 ISCSI_LOGIN_STATUS_NO_RESOURCES);
416 return -1;
419 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
420 * Immediate Data + Unsolicitied Data-OUT if necessary..
422 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
423 conn->param_list);
424 if (!param) {
425 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
426 ISCSI_LOGIN_STATUS_NO_RESOURCES);
427 return -1;
429 rc = kstrtoul(param->value, 0, &mrdsl);
430 if (rc < 0) {
431 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
432 ISCSI_LOGIN_STATUS_NO_RESOURCES);
433 return -1;
435 off = mrdsl % PAGE_SIZE;
436 if (!off)
437 return 0;
439 if (mrdsl < PAGE_SIZE)
440 mrdsl = PAGE_SIZE;
441 else
442 mrdsl -= off;
444 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
445 " to PAGE_SIZE\n", mrdsl);
447 sprintf(buf, "MaxRecvDataSegmentLength=%lu\n", mrdsl);
448 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
449 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
450 ISCSI_LOGIN_STATUS_NO_RESOURCES);
451 return -1;
455 return 0;
459 * Remove PSTATE_NEGOTIATE for the four FIM related keys.
460 * The Initiator node will be able to enable FIM by proposing them itself.
462 int iscsi_login_disable_FIM_keys(
463 struct iscsi_param_list *param_list,
464 struct iscsi_conn *conn)
466 struct iscsi_param *param;
468 param = iscsi_find_param_from_key("OFMarker", param_list);
469 if (!param) {
470 pr_err("iscsi_find_param_from_key() for"
471 " OFMarker failed\n");
472 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
473 ISCSI_LOGIN_STATUS_NO_RESOURCES);
474 return -1;
476 param->state &= ~PSTATE_NEGOTIATE;
478 param = iscsi_find_param_from_key("OFMarkInt", param_list);
479 if (!param) {
480 pr_err("iscsi_find_param_from_key() for"
481 " IFMarker failed\n");
482 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
483 ISCSI_LOGIN_STATUS_NO_RESOURCES);
484 return -1;
486 param->state &= ~PSTATE_NEGOTIATE;
488 param = iscsi_find_param_from_key("IFMarker", param_list);
489 if (!param) {
490 pr_err("iscsi_find_param_from_key() for"
491 " IFMarker failed\n");
492 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
493 ISCSI_LOGIN_STATUS_NO_RESOURCES);
494 return -1;
496 param->state &= ~PSTATE_NEGOTIATE;
498 param = iscsi_find_param_from_key("IFMarkInt", param_list);
499 if (!param) {
500 pr_err("iscsi_find_param_from_key() for"
501 " IFMarker failed\n");
502 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
503 ISCSI_LOGIN_STATUS_NO_RESOURCES);
504 return -1;
506 param->state &= ~PSTATE_NEGOTIATE;
508 return 0;
511 static int iscsi_login_non_zero_tsih_s1(
512 struct iscsi_conn *conn,
513 unsigned char *buf)
515 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
517 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
518 return 0;
522 * Add a new connection to an existing session.
524 static int iscsi_login_non_zero_tsih_s2(
525 struct iscsi_conn *conn,
526 unsigned char *buf)
528 struct iscsi_portal_group *tpg = conn->tpg;
529 struct iscsi_session *sess = NULL, *sess_p = NULL;
530 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
531 struct se_session *se_sess, *se_sess_tmp;
532 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
533 bool iser = false;
535 spin_lock_bh(&se_tpg->session_lock);
536 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
537 sess_list) {
539 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
540 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
541 atomic_read(&sess_p->session_logout) ||
542 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
543 continue;
544 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
545 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
546 iscsit_inc_session_usage_count(sess_p);
547 iscsit_stop_time2retain_timer(sess_p);
548 sess = sess_p;
549 break;
552 spin_unlock_bh(&se_tpg->session_lock);
555 * If the Time2Retain handler has expired, the session is already gone.
557 if (!sess) {
558 pr_err("Initiator attempting to add a connection to"
559 " a non-existent session, rejecting iSCSI Login.\n");
560 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
561 ISCSI_LOGIN_STATUS_NO_SESSION);
562 return -1;
566 * Stop the Time2Retain timer if this is a failed session, we restart
567 * the timer if the login is not successful.
569 spin_lock_bh(&sess->conn_lock);
570 if (sess->session_state == TARG_SESS_STATE_FAILED)
571 atomic_set(&sess->session_continuation, 1);
572 spin_unlock_bh(&sess->conn_lock);
574 iscsi_login_set_conn_values(sess, conn, pdu->cid);
576 if (iscsi_copy_param_list(&conn->param_list,
577 conn->tpg->param_list, 0) < 0) {
578 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
579 ISCSI_LOGIN_STATUS_NO_RESOURCES);
580 return -1;
583 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
584 iser = true;
586 iscsi_set_keys_to_negotiate(conn->param_list, iser);
588 * Need to send TargetPortalGroupTag back in first login response
589 * on any iSCSI connection where the Initiator provides TargetName.
590 * See 5.3.1. Login Phase Start
592 * In our case, we have already located the struct iscsi_tiqn at this point.
594 memset(buf, 0, 32);
595 sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->tpgt);
596 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
597 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
598 ISCSI_LOGIN_STATUS_NO_RESOURCES);
599 return -1;
602 return iscsi_login_disable_FIM_keys(conn->param_list, conn);
605 int iscsi_login_post_auth_non_zero_tsih(
606 struct iscsi_conn *conn,
607 u16 cid,
608 u32 exp_statsn)
610 struct iscsi_conn *conn_ptr = NULL;
611 struct iscsi_conn_recovery *cr = NULL;
612 struct iscsi_session *sess = conn->sess;
615 * By following item 5 in the login table, if we have found
616 * an existing ISID and a valid/existing TSIH and an existing
617 * CID we do connection reinstatement. Currently we dont not
618 * support it so we send back an non-zero status class to the
619 * initiator and release the new connection.
621 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
622 if (conn_ptr) {
623 pr_err("Connection exists with CID %hu for %s,"
624 " performing connection reinstatement.\n",
625 conn_ptr->cid, sess->sess_ops->InitiatorName);
627 iscsit_connection_reinstatement_rcfr(conn_ptr);
628 iscsit_dec_conn_usage_count(conn_ptr);
632 * Check for any connection recovery entires containing CID.
633 * We use the original ExpStatSN sent in the first login request
634 * to acknowledge commands for the failed connection.
636 * Also note that an explict logout may have already been sent,
637 * but the response may not be sent due to additional connection
638 * loss.
640 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
641 cr = iscsit_get_inactive_connection_recovery_entry(
642 sess, cid);
643 if (cr) {
644 pr_debug("Performing implicit logout"
645 " for connection recovery on CID: %hu\n",
646 conn->cid);
647 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
652 * Else we follow item 4 from the login table in that we have
653 * found an existing ISID and a valid/existing TSIH and a new
654 * CID we go ahead and continue to add a new connection to the
655 * session.
657 pr_debug("Adding CID %hu to existing session for %s.\n",
658 cid, sess->sess_ops->InitiatorName);
660 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
661 pr_err("Adding additional connection to this session"
662 " would exceed MaxConnections %d, login failed.\n",
663 sess->sess_ops->MaxConnections);
664 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
665 ISCSI_LOGIN_STATUS_ISID_ERROR);
666 return -1;
669 return 0;
672 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
674 struct iscsi_session *sess = conn->sess;
676 * FIXME: Unsolicitied NopIN support for ISER
678 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
679 return;
681 if (!sess->sess_ops->SessionType)
682 iscsit_start_nopin_timer(conn);
685 int iscsi_post_login_handler(
686 struct iscsi_np *np,
687 struct iscsi_conn *conn,
688 u8 zero_tsih)
690 int stop_timer = 0;
691 struct iscsi_session *sess = conn->sess;
692 struct se_session *se_sess = sess->se_sess;
693 struct iscsi_portal_group *tpg = sess->tpg;
694 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
695 struct iscsi_thread_set *ts;
697 iscsit_inc_conn_usage_count(conn);
699 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
700 ISCSI_LOGIN_STATUS_ACCEPT);
702 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
703 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
705 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
706 iscsit_set_sync_and_steering_values(conn);
708 * SCSI Initiator -> SCSI Target Port Mapping
710 ts = iscsi_get_thread_set();
711 if (!zero_tsih) {
712 iscsi_set_session_parameters(sess->sess_ops,
713 conn->param_list, 0);
714 iscsi_release_param_list(conn->param_list);
715 conn->param_list = NULL;
717 spin_lock_bh(&sess->conn_lock);
718 atomic_set(&sess->session_continuation, 0);
719 if (sess->session_state == TARG_SESS_STATE_FAILED) {
720 pr_debug("Moving to"
721 " TARG_SESS_STATE_LOGGED_IN.\n");
722 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
723 stop_timer = 1;
726 pr_debug("iSCSI Login successful on CID: %hu from %s to"
727 " %s:%hu,%hu\n", conn->cid, conn->login_ip,
728 conn->local_ip, conn->local_port, tpg->tpgt);
730 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
731 atomic_inc(&sess->nconn);
732 pr_debug("Incremented iSCSI Connection count to %hu"
733 " from node: %s\n", atomic_read(&sess->nconn),
734 sess->sess_ops->InitiatorName);
735 spin_unlock_bh(&sess->conn_lock);
737 iscsi_post_login_start_timers(conn);
739 iscsi_activate_thread_set(conn, ts);
741 * Determine CPU mask to ensure connection's RX and TX kthreads
742 * are scheduled on the same CPU.
744 iscsit_thread_get_cpumask(conn);
745 conn->conn_rx_reset_cpumask = 1;
746 conn->conn_tx_reset_cpumask = 1;
748 iscsit_dec_conn_usage_count(conn);
749 if (stop_timer) {
750 spin_lock_bh(&se_tpg->session_lock);
751 iscsit_stop_time2retain_timer(sess);
752 spin_unlock_bh(&se_tpg->session_lock);
754 iscsit_dec_session_usage_count(sess);
755 return 0;
758 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
759 iscsi_release_param_list(conn->param_list);
760 conn->param_list = NULL;
762 iscsit_determine_maxcmdsn(sess);
764 spin_lock_bh(&se_tpg->session_lock);
765 __transport_register_session(&sess->tpg->tpg_se_tpg,
766 se_sess->se_node_acl, se_sess, sess);
767 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
768 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
770 pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
771 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
772 tpg->tpgt);
774 spin_lock_bh(&sess->conn_lock);
775 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
776 atomic_inc(&sess->nconn);
777 pr_debug("Incremented iSCSI Connection count to %hu from node:"
778 " %s\n", atomic_read(&sess->nconn),
779 sess->sess_ops->InitiatorName);
780 spin_unlock_bh(&sess->conn_lock);
782 sess->sid = tpg->sid++;
783 if (!sess->sid)
784 sess->sid = tpg->sid++;
785 pr_debug("Established iSCSI session from node: %s\n",
786 sess->sess_ops->InitiatorName);
788 tpg->nsessions++;
789 if (tpg->tpg_tiqn)
790 tpg->tpg_tiqn->tiqn_nsessions++;
792 pr_debug("Incremented number of active iSCSI sessions to %u on"
793 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
794 spin_unlock_bh(&se_tpg->session_lock);
796 iscsi_post_login_start_timers(conn);
797 iscsi_activate_thread_set(conn, ts);
799 * Determine CPU mask to ensure connection's RX and TX kthreads
800 * are scheduled on the same CPU.
802 iscsit_thread_get_cpumask(conn);
803 conn->conn_rx_reset_cpumask = 1;
804 conn->conn_tx_reset_cpumask = 1;
806 iscsit_dec_conn_usage_count(conn);
808 return 0;
811 static void iscsi_handle_login_thread_timeout(unsigned long data)
813 struct iscsi_np *np = (struct iscsi_np *) data;
815 spin_lock_bh(&np->np_thread_lock);
816 pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
817 np->np_ip, np->np_port);
819 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
820 spin_unlock_bh(&np->np_thread_lock);
821 return;
824 if (np->np_thread)
825 send_sig(SIGINT, np->np_thread, 1);
827 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
828 spin_unlock_bh(&np->np_thread_lock);
831 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
834 * This used the TA_LOGIN_TIMEOUT constant because at this
835 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
837 spin_lock_bh(&np->np_thread_lock);
838 init_timer(&np->np_login_timer);
839 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
840 np->np_login_timer.data = (unsigned long)np;
841 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
842 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
843 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
844 add_timer(&np->np_login_timer);
846 pr_debug("Added timeout timer to iSCSI login request for"
847 " %u seconds.\n", TA_LOGIN_TIMEOUT);
848 spin_unlock_bh(&np->np_thread_lock);
851 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
853 spin_lock_bh(&np->np_thread_lock);
854 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
855 spin_unlock_bh(&np->np_thread_lock);
856 return;
858 np->np_login_timer_flags |= ISCSI_TF_STOP;
859 spin_unlock_bh(&np->np_thread_lock);
861 del_timer_sync(&np->np_login_timer);
863 spin_lock_bh(&np->np_thread_lock);
864 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
865 spin_unlock_bh(&np->np_thread_lock);
868 int iscsit_setup_np(
869 struct iscsi_np *np,
870 struct __kernel_sockaddr_storage *sockaddr)
872 struct socket *sock = NULL;
873 int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
875 switch (np->np_network_transport) {
876 case ISCSI_TCP:
877 np->np_ip_proto = IPPROTO_TCP;
878 np->np_sock_type = SOCK_STREAM;
879 break;
880 case ISCSI_SCTP_TCP:
881 np->np_ip_proto = IPPROTO_SCTP;
882 np->np_sock_type = SOCK_STREAM;
883 break;
884 case ISCSI_SCTP_UDP:
885 np->np_ip_proto = IPPROTO_SCTP;
886 np->np_sock_type = SOCK_SEQPACKET;
887 break;
888 default:
889 pr_err("Unsupported network_transport: %d\n",
890 np->np_network_transport);
891 return -EINVAL;
894 np->np_ip_proto = IPPROTO_TCP;
895 np->np_sock_type = SOCK_STREAM;
897 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
898 np->np_ip_proto, &sock);
899 if (ret < 0) {
900 pr_err("sock_create() failed.\n");
901 return ret;
903 np->np_socket = sock;
905 * Setup the np->np_sockaddr from the passed sockaddr setup
906 * in iscsi_target_configfs.c code..
908 memcpy(&np->np_sockaddr, sockaddr,
909 sizeof(struct __kernel_sockaddr_storage));
911 if (sockaddr->ss_family == AF_INET6)
912 len = sizeof(struct sockaddr_in6);
913 else
914 len = sizeof(struct sockaddr_in);
916 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
918 /* FIXME: Someone please explain why this is endian-safe */
919 opt = 1;
920 if (np->np_network_transport == ISCSI_TCP) {
921 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
922 (char *)&opt, sizeof(opt));
923 if (ret < 0) {
924 pr_err("kernel_setsockopt() for TCP_NODELAY"
925 " failed: %d\n", ret);
926 goto fail;
930 /* FIXME: Someone please explain why this is endian-safe */
931 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
932 (char *)&opt, sizeof(opt));
933 if (ret < 0) {
934 pr_err("kernel_setsockopt() for SO_REUSEADDR"
935 " failed\n");
936 goto fail;
939 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
940 (char *)&opt, sizeof(opt));
941 if (ret < 0) {
942 pr_err("kernel_setsockopt() for IP_FREEBIND"
943 " failed\n");
944 goto fail;
947 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
948 if (ret < 0) {
949 pr_err("kernel_bind() failed: %d\n", ret);
950 goto fail;
953 ret = kernel_listen(sock, backlog);
954 if (ret != 0) {
955 pr_err("kernel_listen() failed: %d\n", ret);
956 goto fail;
959 return 0;
960 fail:
961 np->np_socket = NULL;
962 if (sock)
963 sock_release(sock);
964 return ret;
967 int iscsi_target_setup_login_socket(
968 struct iscsi_np *np,
969 struct __kernel_sockaddr_storage *sockaddr)
971 struct iscsit_transport *t;
972 int rc;
974 t = iscsit_get_transport(np->np_network_transport);
975 if (!t)
976 return -EINVAL;
978 rc = t->iscsit_setup_np(np, sockaddr);
979 if (rc < 0) {
980 iscsit_put_transport(t);
981 return rc;
984 np->np_transport = t;
985 return 0;
988 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
990 struct socket *new_sock, *sock = np->np_socket;
991 struct sockaddr_in sock_in;
992 struct sockaddr_in6 sock_in6;
993 int rc, err;
995 rc = kernel_accept(sock, &new_sock, 0);
996 if (rc < 0)
997 return rc;
999 conn->sock = new_sock;
1000 conn->login_family = np->np_sockaddr.ss_family;
1002 if (np->np_sockaddr.ss_family == AF_INET6) {
1003 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1005 rc = conn->sock->ops->getname(conn->sock,
1006 (struct sockaddr *)&sock_in6, &err, 1);
1007 if (!rc) {
1008 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1009 snprintf(conn->login_ip, sizeof(conn->login_ip), "[%pI6c]",
1010 &sock_in6.sin6_addr.in6_u);
1011 else
1012 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI4",
1013 &sock_in6.sin6_addr.s6_addr32[3]);
1014 conn->login_port = ntohs(sock_in6.sin6_port);
1017 rc = conn->sock->ops->getname(conn->sock,
1018 (struct sockaddr *)&sock_in6, &err, 0);
1019 if (!rc) {
1020 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1021 snprintf(conn->local_ip, sizeof(conn->local_ip), "[%pI6c]",
1022 &sock_in6.sin6_addr.in6_u);
1023 else
1024 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI4",
1025 &sock_in6.sin6_addr.s6_addr32[3]);
1026 conn->local_port = ntohs(sock_in6.sin6_port);
1028 } else {
1029 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1031 rc = conn->sock->ops->getname(conn->sock,
1032 (struct sockaddr *)&sock_in, &err, 1);
1033 if (!rc) {
1034 sprintf(conn->login_ip, "%pI4",
1035 &sock_in.sin_addr.s_addr);
1036 conn->login_port = ntohs(sock_in.sin_port);
1039 rc = conn->sock->ops->getname(conn->sock,
1040 (struct sockaddr *)&sock_in, &err, 0);
1041 if (!rc) {
1042 sprintf(conn->local_ip, "%pI4",
1043 &sock_in.sin_addr.s_addr);
1044 conn->local_port = ntohs(sock_in.sin_port);
1048 return 0;
1051 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1053 struct iscsi_login_req *login_req;
1054 u32 padding = 0, payload_length;
1056 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1057 return -1;
1059 login_req = (struct iscsi_login_req *)login->req;
1060 payload_length = ntoh24(login_req->dlength);
1061 padding = ((-payload_length) & 3);
1063 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1064 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1065 login_req->flags, login_req->itt, login_req->cmdsn,
1066 login_req->exp_statsn, login_req->cid, payload_length);
1068 * Setup the initial iscsi_login values from the leading
1069 * login request PDU.
1071 if (login->first_request) {
1072 login_req = (struct iscsi_login_req *)login->req;
1073 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1074 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1075 login->version_min = login_req->min_version;
1076 login->version_max = login_req->max_version;
1077 memcpy(login->isid, login_req->isid, 6);
1078 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1079 login->init_task_tag = login_req->itt;
1080 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1081 login->cid = be16_to_cpu(login_req->cid);
1082 login->tsih = be16_to_cpu(login_req->tsih);
1085 if (iscsi_target_check_login_request(conn, login) < 0)
1086 return -1;
1088 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1089 if (iscsi_login_rx_data(conn, login->req_buf,
1090 payload_length + padding) < 0)
1091 return -1;
1093 return 0;
1096 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1097 u32 length)
1099 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1100 return -1;
1102 return 0;
1105 static int
1106 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1108 int rc;
1110 if (!t->owner) {
1111 conn->conn_transport = t;
1112 return 0;
1115 rc = try_module_get(t->owner);
1116 if (!rc) {
1117 pr_err("try_module_get() failed for %s\n", t->name);
1118 return -EINVAL;
1121 conn->conn_transport = t;
1122 return 0;
1125 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1126 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1128 if (new_sess == false)
1129 goto old_sess_out;
1131 pr_err("iSCSI Login negotiation failed.\n");
1132 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1133 ISCSI_LOGIN_STATUS_INIT_ERR);
1134 if (!zero_tsih || !conn->sess)
1135 goto old_sess_out;
1136 if (conn->sess->se_sess)
1137 transport_free_session(conn->sess->se_sess);
1138 if (conn->sess->session_index != 0) {
1139 spin_lock_bh(&sess_idr_lock);
1140 idr_remove(&sess_idr, conn->sess->session_index);
1141 spin_unlock_bh(&sess_idr_lock);
1143 kfree(conn->sess->sess_ops);
1144 kfree(conn->sess);
1146 old_sess_out:
1147 iscsi_stop_login_thread_timer(np);
1149 * If login negotiation fails check if the Time2Retain timer
1150 * needs to be restarted.
1152 if (!zero_tsih && conn->sess) {
1153 spin_lock_bh(&conn->sess->conn_lock);
1154 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1155 struct se_portal_group *se_tpg =
1156 &conn->tpg->tpg_se_tpg;
1158 atomic_set(&conn->sess->session_continuation, 0);
1159 spin_unlock_bh(&conn->sess->conn_lock);
1160 spin_lock_bh(&se_tpg->session_lock);
1161 iscsit_start_time2retain_handler(conn->sess);
1162 spin_unlock_bh(&se_tpg->session_lock);
1163 } else
1164 spin_unlock_bh(&conn->sess->conn_lock);
1165 iscsit_dec_session_usage_count(conn->sess);
1168 if (!IS_ERR(conn->conn_rx_hash.tfm))
1169 crypto_free_hash(conn->conn_rx_hash.tfm);
1170 if (!IS_ERR(conn->conn_tx_hash.tfm))
1171 crypto_free_hash(conn->conn_tx_hash.tfm);
1173 if (conn->conn_cpumask)
1174 free_cpumask_var(conn->conn_cpumask);
1176 kfree(conn->conn_ops);
1178 if (conn->param_list) {
1179 iscsi_release_param_list(conn->param_list);
1180 conn->param_list = NULL;
1182 iscsi_target_nego_release(conn);
1184 if (conn->sock) {
1185 sock_release(conn->sock);
1186 conn->sock = NULL;
1189 if (conn->conn_transport->iscsit_free_conn)
1190 conn->conn_transport->iscsit_free_conn(conn);
1192 iscsit_put_transport(conn->conn_transport);
1193 kfree(conn);
1196 static int __iscsi_target_login_thread(struct iscsi_np *np)
1198 u8 *buffer, zero_tsih = 0;
1199 int ret = 0, rc, stop;
1200 struct iscsi_conn *conn = NULL;
1201 struct iscsi_login *login;
1202 struct iscsi_portal_group *tpg = NULL;
1203 struct iscsi_login_req *pdu;
1204 struct iscsi_tpg_np *tpg_np;
1205 bool new_sess = false;
1207 flush_signals(current);
1209 spin_lock_bh(&np->np_thread_lock);
1210 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1211 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1212 complete(&np->np_restart_comp);
1213 } else {
1214 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1216 spin_unlock_bh(&np->np_thread_lock);
1218 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1219 if (!conn) {
1220 pr_err("Could not allocate memory for"
1221 " new connection\n");
1222 /* Get another socket */
1223 return 1;
1225 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1226 conn->conn_state = TARG_CONN_STATE_FREE;
1228 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1229 kfree(conn);
1230 return 1;
1233 rc = np->np_transport->iscsit_accept_np(np, conn);
1234 if (rc == -ENOSYS) {
1235 complete(&np->np_restart_comp);
1236 iscsit_put_transport(conn->conn_transport);
1237 kfree(conn);
1238 conn = NULL;
1239 goto exit;
1240 } else if (rc < 0) {
1241 spin_lock_bh(&np->np_thread_lock);
1242 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1243 spin_unlock_bh(&np->np_thread_lock);
1244 complete(&np->np_restart_comp);
1245 iscsit_put_transport(conn->conn_transport);
1246 kfree(conn);
1247 conn = NULL;
1248 if (ret == -ENODEV)
1249 goto out;
1250 /* Get another socket */
1251 return 1;
1253 spin_unlock_bh(&np->np_thread_lock);
1254 iscsit_put_transport(conn->conn_transport);
1255 kfree(conn);
1256 conn = NULL;
1257 goto out;
1260 * Perform the remaining iSCSI connection initialization items..
1262 login = iscsi_login_init_conn(conn);
1263 if (!login) {
1264 goto new_sess_out;
1267 iscsi_start_login_thread_timer(np);
1269 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1270 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1272 * This will process the first login request + payload..
1274 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1275 if (rc == 1)
1276 return 1;
1277 else if (rc < 0)
1278 goto new_sess_out;
1280 buffer = &login->req[0];
1281 pdu = (struct iscsi_login_req *)buffer;
1283 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1284 * when Status-Class != 0.
1286 conn->login_itt = pdu->itt;
1288 spin_lock_bh(&np->np_thread_lock);
1289 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1290 spin_unlock_bh(&np->np_thread_lock);
1291 pr_err("iSCSI Network Portal on %s:%hu currently not"
1292 " active.\n", np->np_ip, np->np_port);
1293 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1294 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1295 goto new_sess_out;
1297 spin_unlock_bh(&np->np_thread_lock);
1299 conn->network_transport = np->np_network_transport;
1301 pr_debug("Received iSCSI login request from %s on %s Network"
1302 " Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
1303 conn->local_ip, conn->local_port);
1305 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1306 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1308 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1309 pdu->min_version) < 0)
1310 goto new_sess_out;
1312 zero_tsih = (pdu->tsih == 0x0000);
1313 if (zero_tsih) {
1315 * This is the leading connection of a new session.
1316 * We wait until after authentication to check for
1317 * session reinstatement.
1319 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1320 goto new_sess_out;
1321 } else {
1323 * Add a new connection to an existing session.
1324 * We check for a non-existant session in
1325 * iscsi_login_non_zero_tsih_s2() below based
1326 * on ISID/TSIH, but wait until after authentication
1327 * to check for connection reinstatement, etc.
1329 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1330 goto new_sess_out;
1333 * SessionType: Discovery
1335 * Locates Default Portal
1337 * SessionType: Normal
1339 * Locates Target Portal from NP -> Target IQN
1341 rc = iscsi_target_locate_portal(np, conn, login);
1342 if (rc < 0) {
1343 tpg = conn->tpg;
1344 goto new_sess_out;
1346 login->zero_tsih = zero_tsih;
1348 tpg = conn->tpg;
1349 if (!tpg) {
1350 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1351 goto new_sess_out;
1354 if (zero_tsih) {
1355 if (iscsi_login_zero_tsih_s2(conn) < 0)
1356 goto new_sess_out;
1357 } else {
1358 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1359 goto old_sess_out;
1362 ret = iscsi_target_start_negotiation(login, conn);
1363 if (ret < 0)
1364 goto new_sess_out;
1366 if (!conn->sess) {
1367 pr_err("struct iscsi_conn session pointer is NULL!\n");
1368 goto new_sess_out;
1371 iscsi_stop_login_thread_timer(np);
1373 if (signal_pending(current))
1374 goto new_sess_out;
1376 if (ret == 1) {
1377 tpg_np = conn->tpg_np;
1379 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1380 if (ret < 0)
1381 goto new_sess_out;
1383 iscsit_deaccess_np(np, tpg, tpg_np);
1386 tpg = NULL;
1387 tpg_np = NULL;
1388 /* Get another socket */
1389 return 1;
1391 new_sess_out:
1392 new_sess = true;
1393 old_sess_out:
1394 tpg_np = conn->tpg_np;
1395 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1396 new_sess = false;
1398 if (tpg) {
1399 iscsit_deaccess_np(np, tpg, tpg_np);
1400 tpg = NULL;
1401 tpg_np = NULL;
1404 out:
1405 stop = kthread_should_stop();
1406 /* Wait for another socket.. */
1407 if (!stop)
1408 return 1;
1409 exit:
1410 iscsi_stop_login_thread_timer(np);
1411 spin_lock_bh(&np->np_thread_lock);
1412 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1413 spin_unlock_bh(&np->np_thread_lock);
1415 return 0;
1418 int iscsi_target_login_thread(void *arg)
1420 struct iscsi_np *np = arg;
1421 int ret;
1423 allow_signal(SIGINT);
1425 while (!kthread_should_stop()) {
1426 ret = __iscsi_target_login_thread(np);
1428 * We break and exit here unless another sock_accept() call
1429 * is expected.
1431 if (ret != 1)
1432 break;
1435 return 0;