Linux 3.11-rc3
[cris-mirror.git] / drivers / target / iscsi / iscsi_target_login.c
blob3402241be87cd401b500da10a398cb734e62bf8b
1 /*******************************************************************************
2 * This file contains the login functions used by the iSCSI Target driver.
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
21 #include <linux/string.h>
22 #include <linux/kthread.h>
23 #include <linux/crypto.h>
24 #include <linux/idr.h>
25 #include <scsi/iscsi_proto.h>
26 #include <target/target_core_base.h>
27 #include <target/target_core_fabric.h>
29 #include "iscsi_target_core.h"
30 #include "iscsi_target_tq.h"
31 #include "iscsi_target_device.h"
32 #include "iscsi_target_nego.h"
33 #include "iscsi_target_erl0.h"
34 #include "iscsi_target_erl2.h"
35 #include "iscsi_target_login.h"
36 #include "iscsi_target_stat.h"
37 #include "iscsi_target_tpg.h"
38 #include "iscsi_target_util.h"
39 #include "iscsi_target.h"
40 #include "iscsi_target_parameters.h"
42 #include <target/iscsi/iscsi_transport.h>
44 static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
46 struct iscsi_login *login;
48 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
49 if (!login) {
50 pr_err("Unable to allocate memory for struct iscsi_login.\n");
51 return NULL;
53 login->conn = conn;
54 login->first_request = 1;
56 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
57 if (!login->req_buf) {
58 pr_err("Unable to allocate memory for response buffer.\n");
59 goto out_login;
62 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
63 if (!login->rsp_buf) {
64 pr_err("Unable to allocate memory for request buffer.\n");
65 goto out_req_buf;
68 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
69 if (!conn->conn_ops) {
70 pr_err("Unable to allocate memory for"
71 " struct iscsi_conn_ops.\n");
72 goto out_rsp_buf;
75 init_waitqueue_head(&conn->queues_wq);
76 INIT_LIST_HEAD(&conn->conn_list);
77 INIT_LIST_HEAD(&conn->conn_cmd_list);
78 INIT_LIST_HEAD(&conn->immed_queue_list);
79 INIT_LIST_HEAD(&conn->response_queue_list);
80 init_completion(&conn->conn_post_wait_comp);
81 init_completion(&conn->conn_wait_comp);
82 init_completion(&conn->conn_wait_rcfr_comp);
83 init_completion(&conn->conn_waiting_on_uc_comp);
84 init_completion(&conn->conn_logout_comp);
85 init_completion(&conn->rx_half_close_comp);
86 init_completion(&conn->tx_half_close_comp);
87 spin_lock_init(&conn->cmd_lock);
88 spin_lock_init(&conn->conn_usage_lock);
89 spin_lock_init(&conn->immed_queue_lock);
90 spin_lock_init(&conn->nopin_timer_lock);
91 spin_lock_init(&conn->response_queue_lock);
92 spin_lock_init(&conn->state_lock);
94 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
95 pr_err("Unable to allocate conn->conn_cpumask\n");
96 goto out_conn_ops;
98 conn->conn_login = login;
100 return login;
102 out_conn_ops:
103 kfree(conn->conn_ops);
104 out_rsp_buf:
105 kfree(login->rsp_buf);
106 out_req_buf:
107 kfree(login->req_buf);
108 out_login:
109 kfree(login);
110 return NULL;
114 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
115 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
117 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
120 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
121 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
122 * to software 1x8 byte slicing from crc32c.ko
124 conn->conn_rx_hash.flags = 0;
125 conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
126 CRYPTO_ALG_ASYNC);
127 if (IS_ERR(conn->conn_rx_hash.tfm)) {
128 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
129 return -ENOMEM;
132 conn->conn_tx_hash.flags = 0;
133 conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
134 CRYPTO_ALG_ASYNC);
135 if (IS_ERR(conn->conn_tx_hash.tfm)) {
136 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
137 crypto_free_hash(conn->conn_rx_hash.tfm);
138 return -ENOMEM;
141 return 0;
144 static int iscsi_login_check_initiator_version(
145 struct iscsi_conn *conn,
146 u8 version_max,
147 u8 version_min)
149 if ((version_max != 0x00) || (version_min != 0x00)) {
150 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
151 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
152 version_min, version_max);
153 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
154 ISCSI_LOGIN_STATUS_NO_VERSION);
155 return -1;
158 return 0;
161 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
163 int sessiontype;
164 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
165 struct iscsi_portal_group *tpg = conn->tpg;
166 struct iscsi_session *sess = NULL, *sess_p = NULL;
167 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
168 struct se_session *se_sess, *se_sess_tmp;
170 initiatorname_param = iscsi_find_param_from_key(
171 INITIATORNAME, conn->param_list);
172 sessiontype_param = iscsi_find_param_from_key(
173 SESSIONTYPE, conn->param_list);
174 if (!initiatorname_param || !sessiontype_param) {
175 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
176 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
177 return -1;
180 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
182 spin_lock_bh(&se_tpg->session_lock);
183 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
184 sess_list) {
186 sess_p = se_sess->fabric_sess_ptr;
187 spin_lock(&sess_p->conn_lock);
188 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
189 atomic_read(&sess_p->session_logout) ||
190 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
191 spin_unlock(&sess_p->conn_lock);
192 continue;
194 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
195 (!strcmp(sess_p->sess_ops->InitiatorName,
196 initiatorname_param->value) &&
197 (sess_p->sess_ops->SessionType == sessiontype))) {
198 atomic_set(&sess_p->session_reinstatement, 1);
199 spin_unlock(&sess_p->conn_lock);
200 iscsit_inc_session_usage_count(sess_p);
201 iscsit_stop_time2retain_timer(sess_p);
202 sess = sess_p;
203 break;
205 spin_unlock(&sess_p->conn_lock);
207 spin_unlock_bh(&se_tpg->session_lock);
209 * If the Time2Retain handler has expired, the session is already gone.
211 if (!sess)
212 return 0;
214 pr_debug("%s iSCSI Session SID %u is still active for %s,"
215 " preforming session reinstatement.\n", (sessiontype) ?
216 "Discovery" : "Normal", sess->sid,
217 sess->sess_ops->InitiatorName);
219 spin_lock_bh(&sess->conn_lock);
220 if (sess->session_state == TARG_SESS_STATE_FAILED) {
221 spin_unlock_bh(&sess->conn_lock);
222 iscsit_dec_session_usage_count(sess);
223 target_put_session(sess->se_sess);
224 return 0;
226 spin_unlock_bh(&sess->conn_lock);
228 iscsit_stop_session(sess, 1, 1);
229 iscsit_dec_session_usage_count(sess);
231 target_put_session(sess->se_sess);
232 return 0;
235 static void iscsi_login_set_conn_values(
236 struct iscsi_session *sess,
237 struct iscsi_conn *conn,
238 __be16 cid)
240 conn->sess = sess;
241 conn->cid = be16_to_cpu(cid);
243 * Generate a random Status sequence number (statsn) for the new
244 * iSCSI connection.
246 get_random_bytes(&conn->stat_sn, sizeof(u32));
248 mutex_lock(&auth_id_lock);
249 conn->auth_id = iscsit_global->auth_id++;
250 mutex_unlock(&auth_id_lock);
254 * This is the leading connection of a new session,
255 * or session reinstatement.
257 static int iscsi_login_zero_tsih_s1(
258 struct iscsi_conn *conn,
259 unsigned char *buf)
261 struct iscsi_session *sess = NULL;
262 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
263 int ret;
265 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
266 if (!sess) {
267 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
268 ISCSI_LOGIN_STATUS_NO_RESOURCES);
269 pr_err("Could not allocate memory for session\n");
270 return -ENOMEM;
273 iscsi_login_set_conn_values(sess, conn, pdu->cid);
274 sess->init_task_tag = pdu->itt;
275 memcpy(&sess->isid, pdu->isid, 6);
276 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
277 INIT_LIST_HEAD(&sess->sess_conn_list);
278 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
279 INIT_LIST_HEAD(&sess->cr_active_list);
280 INIT_LIST_HEAD(&sess->cr_inactive_list);
281 init_completion(&sess->async_msg_comp);
282 init_completion(&sess->reinstatement_comp);
283 init_completion(&sess->session_wait_comp);
284 init_completion(&sess->session_waiting_on_uc_comp);
285 mutex_init(&sess->cmdsn_mutex);
286 spin_lock_init(&sess->conn_lock);
287 spin_lock_init(&sess->cr_a_lock);
288 spin_lock_init(&sess->cr_i_lock);
289 spin_lock_init(&sess->session_usage_lock);
290 spin_lock_init(&sess->ttt_lock);
292 idr_preload(GFP_KERNEL);
293 spin_lock_bh(&sess_idr_lock);
294 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
295 if (ret >= 0)
296 sess->session_index = ret;
297 spin_unlock_bh(&sess_idr_lock);
298 idr_preload_end();
300 if (ret < 0) {
301 pr_err("idr_alloc() for sess_idr failed\n");
302 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
303 ISCSI_LOGIN_STATUS_NO_RESOURCES);
304 kfree(sess);
305 return -ENOMEM;
308 sess->creation_time = get_jiffies_64();
309 spin_lock_init(&sess->session_stats_lock);
311 * The FFP CmdSN window values will be allocated from the TPG's
312 * Initiator Node's ACL once the login has been successfully completed.
314 sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn);
316 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
317 if (!sess->sess_ops) {
318 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
319 ISCSI_LOGIN_STATUS_NO_RESOURCES);
320 pr_err("Unable to allocate memory for"
321 " struct iscsi_sess_ops.\n");
322 kfree(sess);
323 return -ENOMEM;
326 sess->se_sess = transport_init_session();
327 if (IS_ERR(sess->se_sess)) {
328 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
329 ISCSI_LOGIN_STATUS_NO_RESOURCES);
330 kfree(sess);
331 return -ENOMEM;
334 return 0;
337 static int iscsi_login_zero_tsih_s2(
338 struct iscsi_conn *conn)
340 struct iscsi_node_attrib *na;
341 struct iscsi_session *sess = conn->sess;
342 unsigned char buf[32];
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 = ++ISCSI_TPG_S(sess)->ntsih;
352 if (!sess->tsih)
353 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
356 * Create the default params from user defined values..
358 if (iscsi_copy_param_list(&conn->param_list,
359 ISCSI_TPG_C(conn)->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 memset(buf, 0, 32);
384 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
385 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
386 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
387 ISCSI_LOGIN_STATUS_NO_RESOURCES);
388 return -1;
392 * Workaround for Initiators that have broken connection recovery logic.
394 * "We would really like to get rid of this." Linux-iSCSI.org team
396 memset(buf, 0, 32);
397 sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
398 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
399 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
400 ISCSI_LOGIN_STATUS_NO_RESOURCES);
401 return -1;
404 if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
405 return -1;
407 * Set RDMAExtensions=Yes by default for iSER enabled network portals
409 if (iser) {
410 struct iscsi_param *param;
411 unsigned long mrdsl, off;
412 int rc;
414 sprintf(buf, "RDMAExtensions=Yes");
415 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
416 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
417 ISCSI_LOGIN_STATUS_NO_RESOURCES);
418 return -1;
421 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
422 * Immediate Data + Unsolicitied Data-OUT if necessary..
424 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
425 conn->param_list);
426 if (!param) {
427 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
428 ISCSI_LOGIN_STATUS_NO_RESOURCES);
429 return -1;
431 rc = strict_strtoul(param->value, 0, &mrdsl);
432 if (rc < 0) {
433 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
434 ISCSI_LOGIN_STATUS_NO_RESOURCES);
435 return -1;
437 off = mrdsl % PAGE_SIZE;
438 if (!off)
439 return 0;
441 if (mrdsl < PAGE_SIZE)
442 mrdsl = PAGE_SIZE;
443 else
444 mrdsl -= off;
446 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
447 " to PAGE_SIZE\n", mrdsl);
449 sprintf(buf, "MaxRecvDataSegmentLength=%lu\n", mrdsl);
450 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
451 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
452 ISCSI_LOGIN_STATUS_NO_RESOURCES);
453 return -1;
457 return 0;
461 * Remove PSTATE_NEGOTIATE for the four FIM related keys.
462 * The Initiator node will be able to enable FIM by proposing them itself.
464 int iscsi_login_disable_FIM_keys(
465 struct iscsi_param_list *param_list,
466 struct iscsi_conn *conn)
468 struct iscsi_param *param;
470 param = iscsi_find_param_from_key("OFMarker", param_list);
471 if (!param) {
472 pr_err("iscsi_find_param_from_key() for"
473 " OFMarker failed\n");
474 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
475 ISCSI_LOGIN_STATUS_NO_RESOURCES);
476 return -1;
478 param->state &= ~PSTATE_NEGOTIATE;
480 param = iscsi_find_param_from_key("OFMarkInt", param_list);
481 if (!param) {
482 pr_err("iscsi_find_param_from_key() for"
483 " IFMarker failed\n");
484 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
485 ISCSI_LOGIN_STATUS_NO_RESOURCES);
486 return -1;
488 param->state &= ~PSTATE_NEGOTIATE;
490 param = iscsi_find_param_from_key("IFMarker", param_list);
491 if (!param) {
492 pr_err("iscsi_find_param_from_key() for"
493 " IFMarker failed\n");
494 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
495 ISCSI_LOGIN_STATUS_NO_RESOURCES);
496 return -1;
498 param->state &= ~PSTATE_NEGOTIATE;
500 param = iscsi_find_param_from_key("IFMarkInt", param_list);
501 if (!param) {
502 pr_err("iscsi_find_param_from_key() for"
503 " IFMarker failed\n");
504 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
505 ISCSI_LOGIN_STATUS_NO_RESOURCES);
506 return -1;
508 param->state &= ~PSTATE_NEGOTIATE;
510 return 0;
513 static int iscsi_login_non_zero_tsih_s1(
514 struct iscsi_conn *conn,
515 unsigned char *buf)
517 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
519 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
520 return 0;
524 * Add a new connection to an existing session.
526 static int iscsi_login_non_zero_tsih_s2(
527 struct iscsi_conn *conn,
528 unsigned char *buf)
530 struct iscsi_portal_group *tpg = conn->tpg;
531 struct iscsi_session *sess = NULL, *sess_p = NULL;
532 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
533 struct se_session *se_sess, *se_sess_tmp;
534 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
535 bool iser = false;
537 spin_lock_bh(&se_tpg->session_lock);
538 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
539 sess_list) {
541 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
542 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
543 atomic_read(&sess_p->session_logout) ||
544 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
545 continue;
546 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
547 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
548 iscsit_inc_session_usage_count(sess_p);
549 iscsit_stop_time2retain_timer(sess_p);
550 sess = sess_p;
551 break;
554 spin_unlock_bh(&se_tpg->session_lock);
557 * If the Time2Retain handler has expired, the session is already gone.
559 if (!sess) {
560 pr_err("Initiator attempting to add a connection to"
561 " a non-existent session, rejecting iSCSI Login.\n");
562 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
563 ISCSI_LOGIN_STATUS_NO_SESSION);
564 return -1;
568 * Stop the Time2Retain timer if this is a failed session, we restart
569 * the timer if the login is not successful.
571 spin_lock_bh(&sess->conn_lock);
572 if (sess->session_state == TARG_SESS_STATE_FAILED)
573 atomic_set(&sess->session_continuation, 1);
574 spin_unlock_bh(&sess->conn_lock);
576 iscsi_login_set_conn_values(sess, conn, pdu->cid);
578 if (iscsi_copy_param_list(&conn->param_list,
579 ISCSI_TPG_C(conn)->param_list, 0) < 0) {
580 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
581 ISCSI_LOGIN_STATUS_NO_RESOURCES);
582 return -1;
585 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
586 iser = true;
588 iscsi_set_keys_to_negotiate(conn->param_list, iser);
590 * Need to send TargetPortalGroupTag back in first login response
591 * on any iSCSI connection where the Initiator provides TargetName.
592 * See 5.3.1. Login Phase Start
594 * In our case, we have already located the struct iscsi_tiqn at this point.
596 memset(buf, 0, 32);
597 sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
598 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
599 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
600 ISCSI_LOGIN_STATUS_NO_RESOURCES);
601 return -1;
604 return iscsi_login_disable_FIM_keys(conn->param_list, conn);
607 int iscsi_login_post_auth_non_zero_tsih(
608 struct iscsi_conn *conn,
609 u16 cid,
610 u32 exp_statsn)
612 struct iscsi_conn *conn_ptr = NULL;
613 struct iscsi_conn_recovery *cr = NULL;
614 struct iscsi_session *sess = conn->sess;
617 * By following item 5 in the login table, if we have found
618 * an existing ISID and a valid/existing TSIH and an existing
619 * CID we do connection reinstatement. Currently we dont not
620 * support it so we send back an non-zero status class to the
621 * initiator and release the new connection.
623 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
624 if (conn_ptr) {
625 pr_err("Connection exists with CID %hu for %s,"
626 " performing connection reinstatement.\n",
627 conn_ptr->cid, sess->sess_ops->InitiatorName);
629 iscsit_connection_reinstatement_rcfr(conn_ptr);
630 iscsit_dec_conn_usage_count(conn_ptr);
634 * Check for any connection recovery entires containing CID.
635 * We use the original ExpStatSN sent in the first login request
636 * to acknowledge commands for the failed connection.
638 * Also note that an explict logout may have already been sent,
639 * but the response may not be sent due to additional connection
640 * loss.
642 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
643 cr = iscsit_get_inactive_connection_recovery_entry(
644 sess, cid);
645 if (cr) {
646 pr_debug("Performing implicit logout"
647 " for connection recovery on CID: %hu\n",
648 conn->cid);
649 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
654 * Else we follow item 4 from the login table in that we have
655 * found an existing ISID and a valid/existing TSIH and a new
656 * CID we go ahead and continue to add a new connection to the
657 * session.
659 pr_debug("Adding CID %hu to existing session for %s.\n",
660 cid, sess->sess_ops->InitiatorName);
662 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
663 pr_err("Adding additional connection to this session"
664 " would exceed MaxConnections %d, login failed.\n",
665 sess->sess_ops->MaxConnections);
666 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
667 ISCSI_LOGIN_STATUS_ISID_ERROR);
668 return -1;
671 return 0;
674 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
676 struct iscsi_session *sess = conn->sess;
678 * FIXME: Unsolicitied NopIN support for ISER
680 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
681 return;
683 if (!sess->sess_ops->SessionType)
684 iscsit_start_nopin_timer(conn);
687 static int iscsi_post_login_handler(
688 struct iscsi_np *np,
689 struct iscsi_conn *conn,
690 u8 zero_tsih)
692 int stop_timer = 0;
693 struct iscsi_session *sess = conn->sess;
694 struct se_session *se_sess = sess->se_sess;
695 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
696 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
697 struct iscsi_thread_set *ts;
699 iscsit_inc_conn_usage_count(conn);
701 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
702 ISCSI_LOGIN_STATUS_ACCEPT);
704 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
705 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
707 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
708 iscsit_set_sync_and_steering_values(conn);
710 * SCSI Initiator -> SCSI Target Port Mapping
712 ts = iscsi_get_thread_set();
713 if (!zero_tsih) {
714 iscsi_set_session_parameters(sess->sess_ops,
715 conn->param_list, 0);
716 iscsi_release_param_list(conn->param_list);
717 conn->param_list = NULL;
719 spin_lock_bh(&sess->conn_lock);
720 atomic_set(&sess->session_continuation, 0);
721 if (sess->session_state == TARG_SESS_STATE_FAILED) {
722 pr_debug("Moving to"
723 " TARG_SESS_STATE_LOGGED_IN.\n");
724 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
725 stop_timer = 1;
728 pr_debug("iSCSI Login successful on CID: %hu from %s to"
729 " %s:%hu,%hu\n", conn->cid, conn->login_ip,
730 conn->local_ip, conn->local_port, tpg->tpgt);
732 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
733 atomic_inc(&sess->nconn);
734 pr_debug("Incremented iSCSI Connection count to %hu"
735 " from node: %s\n", atomic_read(&sess->nconn),
736 sess->sess_ops->InitiatorName);
737 spin_unlock_bh(&sess->conn_lock);
739 iscsi_post_login_start_timers(conn);
741 iscsi_activate_thread_set(conn, ts);
743 * Determine CPU mask to ensure connection's RX and TX kthreads
744 * are scheduled on the same CPU.
746 iscsit_thread_get_cpumask(conn);
747 conn->conn_rx_reset_cpumask = 1;
748 conn->conn_tx_reset_cpumask = 1;
750 iscsit_dec_conn_usage_count(conn);
751 if (stop_timer) {
752 spin_lock_bh(&se_tpg->session_lock);
753 iscsit_stop_time2retain_timer(sess);
754 spin_unlock_bh(&se_tpg->session_lock);
756 iscsit_dec_session_usage_count(sess);
757 return 0;
760 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
761 iscsi_release_param_list(conn->param_list);
762 conn->param_list = NULL;
764 iscsit_determine_maxcmdsn(sess);
766 spin_lock_bh(&se_tpg->session_lock);
767 __transport_register_session(&sess->tpg->tpg_se_tpg,
768 se_sess->se_node_acl, se_sess, sess);
769 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
770 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
772 pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
773 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
774 tpg->tpgt);
776 spin_lock_bh(&sess->conn_lock);
777 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
778 atomic_inc(&sess->nconn);
779 pr_debug("Incremented iSCSI Connection count to %hu from node:"
780 " %s\n", atomic_read(&sess->nconn),
781 sess->sess_ops->InitiatorName);
782 spin_unlock_bh(&sess->conn_lock);
784 sess->sid = tpg->sid++;
785 if (!sess->sid)
786 sess->sid = tpg->sid++;
787 pr_debug("Established iSCSI session from node: %s\n",
788 sess->sess_ops->InitiatorName);
790 tpg->nsessions++;
791 if (tpg->tpg_tiqn)
792 tpg->tpg_tiqn->tiqn_nsessions++;
794 pr_debug("Incremented number of active iSCSI sessions to %u on"
795 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
796 spin_unlock_bh(&se_tpg->session_lock);
798 iscsi_post_login_start_timers(conn);
799 iscsi_activate_thread_set(conn, ts);
801 * Determine CPU mask to ensure connection's RX and TX kthreads
802 * are scheduled on the same CPU.
804 iscsit_thread_get_cpumask(conn);
805 conn->conn_rx_reset_cpumask = 1;
806 conn->conn_tx_reset_cpumask = 1;
808 iscsit_dec_conn_usage_count(conn);
810 return 0;
813 static void iscsi_handle_login_thread_timeout(unsigned long data)
815 struct iscsi_np *np = (struct iscsi_np *) data;
817 spin_lock_bh(&np->np_thread_lock);
818 pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
819 np->np_ip, np->np_port);
821 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
822 spin_unlock_bh(&np->np_thread_lock);
823 return;
826 if (np->np_thread)
827 send_sig(SIGINT, np->np_thread, 1);
829 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
830 spin_unlock_bh(&np->np_thread_lock);
833 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
836 * This used the TA_LOGIN_TIMEOUT constant because at this
837 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
839 spin_lock_bh(&np->np_thread_lock);
840 init_timer(&np->np_login_timer);
841 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
842 np->np_login_timer.data = (unsigned long)np;
843 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
844 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
845 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
846 add_timer(&np->np_login_timer);
848 pr_debug("Added timeout timer to iSCSI login request for"
849 " %u seconds.\n", TA_LOGIN_TIMEOUT);
850 spin_unlock_bh(&np->np_thread_lock);
853 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
855 spin_lock_bh(&np->np_thread_lock);
856 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
857 spin_unlock_bh(&np->np_thread_lock);
858 return;
860 np->np_login_timer_flags |= ISCSI_TF_STOP;
861 spin_unlock_bh(&np->np_thread_lock);
863 del_timer_sync(&np->np_login_timer);
865 spin_lock_bh(&np->np_thread_lock);
866 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
867 spin_unlock_bh(&np->np_thread_lock);
870 int iscsit_setup_np(
871 struct iscsi_np *np,
872 struct __kernel_sockaddr_storage *sockaddr)
874 struct socket *sock = NULL;
875 int backlog = 5, ret, opt = 0, len;
877 switch (np->np_network_transport) {
878 case ISCSI_TCP:
879 np->np_ip_proto = IPPROTO_TCP;
880 np->np_sock_type = SOCK_STREAM;
881 break;
882 case ISCSI_SCTP_TCP:
883 np->np_ip_proto = IPPROTO_SCTP;
884 np->np_sock_type = SOCK_STREAM;
885 break;
886 case ISCSI_SCTP_UDP:
887 np->np_ip_proto = IPPROTO_SCTP;
888 np->np_sock_type = SOCK_SEQPACKET;
889 break;
890 default:
891 pr_err("Unsupported network_transport: %d\n",
892 np->np_network_transport);
893 return -EINVAL;
896 np->np_ip_proto = IPPROTO_TCP;
897 np->np_sock_type = SOCK_STREAM;
899 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
900 np->np_ip_proto, &sock);
901 if (ret < 0) {
902 pr_err("sock_create() failed.\n");
903 return ret;
905 np->np_socket = sock;
907 * Setup the np->np_sockaddr from the passed sockaddr setup
908 * in iscsi_target_configfs.c code..
910 memcpy(&np->np_sockaddr, sockaddr,
911 sizeof(struct __kernel_sockaddr_storage));
913 if (sockaddr->ss_family == AF_INET6)
914 len = sizeof(struct sockaddr_in6);
915 else
916 len = sizeof(struct sockaddr_in);
918 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
920 /* FIXME: Someone please explain why this is endian-safe */
921 opt = 1;
922 if (np->np_network_transport == ISCSI_TCP) {
923 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
924 (char *)&opt, sizeof(opt));
925 if (ret < 0) {
926 pr_err("kernel_setsockopt() for TCP_NODELAY"
927 " failed: %d\n", ret);
928 goto fail;
932 /* FIXME: Someone please explain why this is endian-safe */
933 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
934 (char *)&opt, sizeof(opt));
935 if (ret < 0) {
936 pr_err("kernel_setsockopt() for SO_REUSEADDR"
937 " failed\n");
938 goto fail;
941 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
942 (char *)&opt, sizeof(opt));
943 if (ret < 0) {
944 pr_err("kernel_setsockopt() for IP_FREEBIND"
945 " failed\n");
946 goto fail;
949 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
950 if (ret < 0) {
951 pr_err("kernel_bind() failed: %d\n", ret);
952 goto fail;
955 ret = kernel_listen(sock, backlog);
956 if (ret != 0) {
957 pr_err("kernel_listen() failed: %d\n", ret);
958 goto fail;
961 return 0;
962 fail:
963 np->np_socket = NULL;
964 if (sock)
965 sock_release(sock);
966 return ret;
969 int iscsi_target_setup_login_socket(
970 struct iscsi_np *np,
971 struct __kernel_sockaddr_storage *sockaddr)
973 struct iscsit_transport *t;
974 int rc;
976 t = iscsit_get_transport(np->np_network_transport);
977 if (!t)
978 return -EINVAL;
980 rc = t->iscsit_setup_np(np, sockaddr);
981 if (rc < 0) {
982 iscsit_put_transport(t);
983 return rc;
986 np->np_transport = t;
987 return 0;
990 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
992 struct socket *new_sock, *sock = np->np_socket;
993 struct sockaddr_in sock_in;
994 struct sockaddr_in6 sock_in6;
995 int rc, err;
997 rc = kernel_accept(sock, &new_sock, 0);
998 if (rc < 0)
999 return rc;
1001 conn->sock = new_sock;
1002 conn->login_family = np->np_sockaddr.ss_family;
1004 if (np->np_sockaddr.ss_family == AF_INET6) {
1005 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1007 rc = conn->sock->ops->getname(conn->sock,
1008 (struct sockaddr *)&sock_in6, &err, 1);
1009 if (!rc) {
1010 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
1011 &sock_in6.sin6_addr.in6_u);
1012 conn->login_port = ntohs(sock_in6.sin6_port);
1015 rc = conn->sock->ops->getname(conn->sock,
1016 (struct sockaddr *)&sock_in6, &err, 0);
1017 if (!rc) {
1018 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
1019 &sock_in6.sin6_addr.in6_u);
1020 conn->local_port = ntohs(sock_in6.sin6_port);
1022 } else {
1023 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1025 rc = conn->sock->ops->getname(conn->sock,
1026 (struct sockaddr *)&sock_in, &err, 1);
1027 if (!rc) {
1028 sprintf(conn->login_ip, "%pI4",
1029 &sock_in.sin_addr.s_addr);
1030 conn->login_port = ntohs(sock_in.sin_port);
1033 rc = conn->sock->ops->getname(conn->sock,
1034 (struct sockaddr *)&sock_in, &err, 0);
1035 if (!rc) {
1036 sprintf(conn->local_ip, "%pI4",
1037 &sock_in.sin_addr.s_addr);
1038 conn->local_port = ntohs(sock_in.sin_port);
1042 return 0;
1045 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1047 struct iscsi_login_req *login_req;
1048 u32 padding = 0, payload_length;
1050 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1051 return -1;
1053 login_req = (struct iscsi_login_req *)login->req;
1054 payload_length = ntoh24(login_req->dlength);
1055 padding = ((-payload_length) & 3);
1057 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1058 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1059 login_req->flags, login_req->itt, login_req->cmdsn,
1060 login_req->exp_statsn, login_req->cid, payload_length);
1062 * Setup the initial iscsi_login values from the leading
1063 * login request PDU.
1065 if (login->first_request) {
1066 login_req = (struct iscsi_login_req *)login->req;
1067 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1068 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1069 login->version_min = login_req->min_version;
1070 login->version_max = login_req->max_version;
1071 memcpy(login->isid, login_req->isid, 6);
1072 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1073 login->init_task_tag = login_req->itt;
1074 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1075 login->cid = be16_to_cpu(login_req->cid);
1076 login->tsih = be16_to_cpu(login_req->tsih);
1079 if (iscsi_target_check_login_request(conn, login) < 0)
1080 return -1;
1082 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1083 if (iscsi_login_rx_data(conn, login->req_buf,
1084 payload_length + padding) < 0)
1085 return -1;
1087 return 0;
1090 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1091 u32 length)
1093 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1094 return -1;
1096 return 0;
1099 static int
1100 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1102 int rc;
1104 if (!t->owner) {
1105 conn->conn_transport = t;
1106 return 0;
1109 rc = try_module_get(t->owner);
1110 if (!rc) {
1111 pr_err("try_module_get() failed for %s\n", t->name);
1112 return -EINVAL;
1115 conn->conn_transport = t;
1116 return 0;
1119 static int __iscsi_target_login_thread(struct iscsi_np *np)
1121 u8 *buffer, zero_tsih = 0;
1122 int ret = 0, rc, stop;
1123 struct iscsi_conn *conn = NULL;
1124 struct iscsi_login *login;
1125 struct iscsi_portal_group *tpg = NULL;
1126 struct iscsi_login_req *pdu;
1128 flush_signals(current);
1130 spin_lock_bh(&np->np_thread_lock);
1131 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1132 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1133 complete(&np->np_restart_comp);
1134 } else {
1135 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1137 spin_unlock_bh(&np->np_thread_lock);
1139 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1140 if (!conn) {
1141 pr_err("Could not allocate memory for"
1142 " new connection\n");
1143 /* Get another socket */
1144 return 1;
1146 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1147 conn->conn_state = TARG_CONN_STATE_FREE;
1149 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1150 kfree(conn);
1151 return 1;
1154 rc = np->np_transport->iscsit_accept_np(np, conn);
1155 if (rc == -ENOSYS) {
1156 complete(&np->np_restart_comp);
1157 iscsit_put_transport(conn->conn_transport);
1158 kfree(conn);
1159 conn = NULL;
1160 goto exit;
1161 } else if (rc < 0) {
1162 spin_lock_bh(&np->np_thread_lock);
1163 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1164 spin_unlock_bh(&np->np_thread_lock);
1165 complete(&np->np_restart_comp);
1166 if (ret == -ENODEV) {
1167 iscsit_put_transport(conn->conn_transport);
1168 kfree(conn);
1169 conn = NULL;
1170 goto out;
1172 /* Get another socket */
1173 return 1;
1175 spin_unlock_bh(&np->np_thread_lock);
1176 iscsit_put_transport(conn->conn_transport);
1177 kfree(conn);
1178 conn = NULL;
1179 goto out;
1182 * Perform the remaining iSCSI connection initialization items..
1184 login = iscsi_login_init_conn(conn);
1185 if (!login) {
1186 goto new_sess_out;
1189 iscsi_start_login_thread_timer(np);
1191 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1192 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1194 * This will process the first login request + payload..
1196 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1197 if (rc == 1)
1198 return 1;
1199 else if (rc < 0)
1200 goto new_sess_out;
1202 buffer = &login->req[0];
1203 pdu = (struct iscsi_login_req *)buffer;
1205 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1206 * when Status-Class != 0.
1208 conn->login_itt = pdu->itt;
1210 spin_lock_bh(&np->np_thread_lock);
1211 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1212 spin_unlock_bh(&np->np_thread_lock);
1213 pr_err("iSCSI Network Portal on %s:%hu currently not"
1214 " active.\n", np->np_ip, np->np_port);
1215 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1216 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1217 goto new_sess_out;
1219 spin_unlock_bh(&np->np_thread_lock);
1221 conn->network_transport = np->np_network_transport;
1223 pr_debug("Received iSCSI login request from %s on %s Network"
1224 " Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
1225 conn->local_ip, conn->local_port);
1227 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1228 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1230 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1231 pdu->min_version) < 0)
1232 goto new_sess_out;
1234 zero_tsih = (pdu->tsih == 0x0000);
1235 if (zero_tsih) {
1237 * This is the leading connection of a new session.
1238 * We wait until after authentication to check for
1239 * session reinstatement.
1241 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1242 goto new_sess_out;
1243 } else {
1245 * Add a new connection to an existing session.
1246 * We check for a non-existant session in
1247 * iscsi_login_non_zero_tsih_s2() below based
1248 * on ISID/TSIH, but wait until after authentication
1249 * to check for connection reinstatement, etc.
1251 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1252 goto new_sess_out;
1255 * SessionType: Discovery
1257 * Locates Default Portal
1259 * SessionType: Normal
1261 * Locates Target Portal from NP -> Target IQN
1263 rc = iscsi_target_locate_portal(np, conn, login);
1264 if (rc < 0) {
1265 tpg = conn->tpg;
1266 goto new_sess_out;
1269 tpg = conn->tpg;
1270 if (!tpg) {
1271 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1272 goto new_sess_out;
1275 if (zero_tsih) {
1276 if (iscsi_login_zero_tsih_s2(conn) < 0)
1277 goto new_sess_out;
1278 } else {
1279 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1280 goto old_sess_out;
1283 if (iscsi_target_start_negotiation(login, conn) < 0)
1284 goto new_sess_out;
1286 if (!conn->sess) {
1287 pr_err("struct iscsi_conn session pointer is NULL!\n");
1288 goto new_sess_out;
1291 iscsi_stop_login_thread_timer(np);
1293 if (signal_pending(current))
1294 goto new_sess_out;
1296 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1298 if (ret < 0)
1299 goto new_sess_out;
1301 iscsit_deaccess_np(np, tpg);
1302 tpg = NULL;
1303 /* Get another socket */
1304 return 1;
1306 new_sess_out:
1307 pr_err("iSCSI Login negotiation failed.\n");
1308 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1309 ISCSI_LOGIN_STATUS_INIT_ERR);
1310 if (!zero_tsih || !conn->sess)
1311 goto old_sess_out;
1312 if (conn->sess->se_sess)
1313 transport_free_session(conn->sess->se_sess);
1314 if (conn->sess->session_index != 0) {
1315 spin_lock_bh(&sess_idr_lock);
1316 idr_remove(&sess_idr, conn->sess->session_index);
1317 spin_unlock_bh(&sess_idr_lock);
1319 kfree(conn->sess->sess_ops);
1320 kfree(conn->sess);
1321 old_sess_out:
1322 iscsi_stop_login_thread_timer(np);
1324 * If login negotiation fails check if the Time2Retain timer
1325 * needs to be restarted.
1327 if (!zero_tsih && conn->sess) {
1328 spin_lock_bh(&conn->sess->conn_lock);
1329 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1330 struct se_portal_group *se_tpg =
1331 &ISCSI_TPG_C(conn)->tpg_se_tpg;
1333 atomic_set(&conn->sess->session_continuation, 0);
1334 spin_unlock_bh(&conn->sess->conn_lock);
1335 spin_lock_bh(&se_tpg->session_lock);
1336 iscsit_start_time2retain_handler(conn->sess);
1337 spin_unlock_bh(&se_tpg->session_lock);
1338 } else
1339 spin_unlock_bh(&conn->sess->conn_lock);
1340 iscsit_dec_session_usage_count(conn->sess);
1343 if (!IS_ERR(conn->conn_rx_hash.tfm))
1344 crypto_free_hash(conn->conn_rx_hash.tfm);
1345 if (!IS_ERR(conn->conn_tx_hash.tfm))
1346 crypto_free_hash(conn->conn_tx_hash.tfm);
1348 if (conn->conn_cpumask)
1349 free_cpumask_var(conn->conn_cpumask);
1351 kfree(conn->conn_ops);
1353 if (conn->param_list) {
1354 iscsi_release_param_list(conn->param_list);
1355 conn->param_list = NULL;
1357 iscsi_target_nego_release(conn);
1359 if (conn->sock) {
1360 sock_release(conn->sock);
1361 conn->sock = NULL;
1364 if (conn->conn_transport->iscsit_free_conn)
1365 conn->conn_transport->iscsit_free_conn(conn);
1367 iscsit_put_transport(conn->conn_transport);
1369 kfree(conn);
1371 if (tpg) {
1372 iscsit_deaccess_np(np, tpg);
1373 tpg = NULL;
1376 out:
1377 stop = kthread_should_stop();
1378 if (!stop && signal_pending(current)) {
1379 spin_lock_bh(&np->np_thread_lock);
1380 stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN);
1381 spin_unlock_bh(&np->np_thread_lock);
1383 /* Wait for another socket.. */
1384 if (!stop)
1385 return 1;
1386 exit:
1387 iscsi_stop_login_thread_timer(np);
1388 spin_lock_bh(&np->np_thread_lock);
1389 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1390 np->np_thread = NULL;
1391 spin_unlock_bh(&np->np_thread_lock);
1393 return 0;
1396 int iscsi_target_login_thread(void *arg)
1398 struct iscsi_np *np = arg;
1399 int ret;
1401 allow_signal(SIGINT);
1403 while (!kthread_should_stop()) {
1404 ret = __iscsi_target_login_thread(np);
1406 * We break and exit here unless another sock_accept() call
1407 * is expected.
1409 if (ret != 1)
1410 break;
1413 return 0;