net: qmi_wwan: add Olivetti Olicard 500
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_login.c
blob8739b98f6f93539b8c6eb95f27d7fde3601b40d7
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 enum target_prot_op sup_pro_ops;
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();
310 * The FFP CmdSN window values will be allocated from the TPG's
311 * Initiator Node's ACL once the login has been successfully completed.
313 sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn);
315 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
316 if (!sess->sess_ops) {
317 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
318 ISCSI_LOGIN_STATUS_NO_RESOURCES);
319 pr_err("Unable to allocate memory for"
320 " struct iscsi_sess_ops.\n");
321 kfree(sess);
322 return -ENOMEM;
324 sup_pro_ops = conn->conn_transport->iscsit_get_sup_prot_ops(conn);
326 sess->se_sess = transport_init_session(sup_pro_ops);
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 = ++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 memset(buf, 0, 32);
384 sprintf(buf, "TargetPortalGroupTag=%hu", sess->tpg->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 = kstrtoul(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 conn->tpg->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", sess->tpg->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 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 = sess->tpg;
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 = ISCSIT_TCP_BACKLOG, 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 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1011 snprintf(conn->login_ip, sizeof(conn->login_ip), "[%pI6c]",
1012 &sock_in6.sin6_addr.in6_u);
1013 else
1014 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI4",
1015 &sock_in6.sin6_addr.s6_addr32[3]);
1016 conn->login_port = ntohs(sock_in6.sin6_port);
1019 rc = conn->sock->ops->getname(conn->sock,
1020 (struct sockaddr *)&sock_in6, &err, 0);
1021 if (!rc) {
1022 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1023 snprintf(conn->local_ip, sizeof(conn->local_ip), "[%pI6c]",
1024 &sock_in6.sin6_addr.in6_u);
1025 else
1026 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI4",
1027 &sock_in6.sin6_addr.s6_addr32[3]);
1028 conn->local_port = ntohs(sock_in6.sin6_port);
1030 } else {
1031 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1033 rc = conn->sock->ops->getname(conn->sock,
1034 (struct sockaddr *)&sock_in, &err, 1);
1035 if (!rc) {
1036 sprintf(conn->login_ip, "%pI4",
1037 &sock_in.sin_addr.s_addr);
1038 conn->login_port = ntohs(sock_in.sin_port);
1041 rc = conn->sock->ops->getname(conn->sock,
1042 (struct sockaddr *)&sock_in, &err, 0);
1043 if (!rc) {
1044 sprintf(conn->local_ip, "%pI4",
1045 &sock_in.sin_addr.s_addr);
1046 conn->local_port = ntohs(sock_in.sin_port);
1050 return 0;
1053 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1055 struct iscsi_login_req *login_req;
1056 u32 padding = 0, payload_length;
1058 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1059 return -1;
1061 login_req = (struct iscsi_login_req *)login->req;
1062 payload_length = ntoh24(login_req->dlength);
1063 padding = ((-payload_length) & 3);
1065 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1066 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1067 login_req->flags, login_req->itt, login_req->cmdsn,
1068 login_req->exp_statsn, login_req->cid, payload_length);
1070 * Setup the initial iscsi_login values from the leading
1071 * login request PDU.
1073 if (login->first_request) {
1074 login_req = (struct iscsi_login_req *)login->req;
1075 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1076 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1077 login->version_min = login_req->min_version;
1078 login->version_max = login_req->max_version;
1079 memcpy(login->isid, login_req->isid, 6);
1080 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1081 login->init_task_tag = login_req->itt;
1082 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1083 login->cid = be16_to_cpu(login_req->cid);
1084 login->tsih = be16_to_cpu(login_req->tsih);
1087 if (iscsi_target_check_login_request(conn, login) < 0)
1088 return -1;
1090 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1091 if (iscsi_login_rx_data(conn, login->req_buf,
1092 payload_length + padding) < 0)
1093 return -1;
1095 return 0;
1098 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1099 u32 length)
1101 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1102 return -1;
1104 return 0;
1107 static int
1108 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1110 int rc;
1112 if (!t->owner) {
1113 conn->conn_transport = t;
1114 return 0;
1117 rc = try_module_get(t->owner);
1118 if (!rc) {
1119 pr_err("try_module_get() failed for %s\n", t->name);
1120 return -EINVAL;
1123 conn->conn_transport = t;
1124 return 0;
1127 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1128 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1130 if (new_sess == false)
1131 goto old_sess_out;
1133 pr_err("iSCSI Login negotiation failed.\n");
1134 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1135 ISCSI_LOGIN_STATUS_INIT_ERR);
1136 if (!zero_tsih || !conn->sess)
1137 goto old_sess_out;
1138 if (conn->sess->se_sess)
1139 transport_free_session(conn->sess->se_sess);
1140 if (conn->sess->session_index != 0) {
1141 spin_lock_bh(&sess_idr_lock);
1142 idr_remove(&sess_idr, conn->sess->session_index);
1143 spin_unlock_bh(&sess_idr_lock);
1145 kfree(conn->sess->sess_ops);
1146 kfree(conn->sess);
1148 old_sess_out:
1149 iscsi_stop_login_thread_timer(np);
1151 * If login negotiation fails check if the Time2Retain timer
1152 * needs to be restarted.
1154 if (!zero_tsih && conn->sess) {
1155 spin_lock_bh(&conn->sess->conn_lock);
1156 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1157 struct se_portal_group *se_tpg =
1158 &conn->tpg->tpg_se_tpg;
1160 atomic_set(&conn->sess->session_continuation, 0);
1161 spin_unlock_bh(&conn->sess->conn_lock);
1162 spin_lock_bh(&se_tpg->session_lock);
1163 iscsit_start_time2retain_handler(conn->sess);
1164 spin_unlock_bh(&se_tpg->session_lock);
1165 } else
1166 spin_unlock_bh(&conn->sess->conn_lock);
1167 iscsit_dec_session_usage_count(conn->sess);
1170 if (!IS_ERR(conn->conn_rx_hash.tfm))
1171 crypto_free_hash(conn->conn_rx_hash.tfm);
1172 if (!IS_ERR(conn->conn_tx_hash.tfm))
1173 crypto_free_hash(conn->conn_tx_hash.tfm);
1175 if (conn->conn_cpumask)
1176 free_cpumask_var(conn->conn_cpumask);
1178 kfree(conn->conn_ops);
1180 if (conn->param_list) {
1181 iscsi_release_param_list(conn->param_list);
1182 conn->param_list = NULL;
1184 iscsi_target_nego_release(conn);
1186 if (conn->sock) {
1187 sock_release(conn->sock);
1188 conn->sock = NULL;
1191 if (conn->conn_transport->iscsit_free_conn)
1192 conn->conn_transport->iscsit_free_conn(conn);
1194 iscsit_put_transport(conn->conn_transport);
1195 kfree(conn);
1198 static int __iscsi_target_login_thread(struct iscsi_np *np)
1200 u8 *buffer, zero_tsih = 0;
1201 int ret = 0, rc, stop;
1202 struct iscsi_conn *conn = NULL;
1203 struct iscsi_login *login;
1204 struct iscsi_portal_group *tpg = NULL;
1205 struct iscsi_login_req *pdu;
1206 struct iscsi_tpg_np *tpg_np;
1207 bool new_sess = false;
1209 flush_signals(current);
1211 spin_lock_bh(&np->np_thread_lock);
1212 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1213 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1214 complete(&np->np_restart_comp);
1215 } else {
1216 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1218 spin_unlock_bh(&np->np_thread_lock);
1220 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1221 if (!conn) {
1222 pr_err("Could not allocate memory for"
1223 " new connection\n");
1224 /* Get another socket */
1225 return 1;
1227 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1228 conn->conn_state = TARG_CONN_STATE_FREE;
1230 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1231 kfree(conn);
1232 return 1;
1235 rc = np->np_transport->iscsit_accept_np(np, conn);
1236 if (rc == -ENOSYS) {
1237 complete(&np->np_restart_comp);
1238 iscsit_put_transport(conn->conn_transport);
1239 kfree(conn);
1240 conn = NULL;
1241 goto exit;
1242 } else if (rc < 0) {
1243 spin_lock_bh(&np->np_thread_lock);
1244 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1245 spin_unlock_bh(&np->np_thread_lock);
1246 complete(&np->np_restart_comp);
1247 iscsit_put_transport(conn->conn_transport);
1248 kfree(conn);
1249 conn = NULL;
1250 if (ret == -ENODEV)
1251 goto out;
1252 /* Get another socket */
1253 return 1;
1255 spin_unlock_bh(&np->np_thread_lock);
1256 iscsit_put_transport(conn->conn_transport);
1257 kfree(conn);
1258 conn = NULL;
1259 goto out;
1262 * Perform the remaining iSCSI connection initialization items..
1264 login = iscsi_login_init_conn(conn);
1265 if (!login) {
1266 goto new_sess_out;
1269 iscsi_start_login_thread_timer(np);
1271 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1272 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1274 * This will process the first login request + payload..
1276 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1277 if (rc == 1)
1278 return 1;
1279 else if (rc < 0)
1280 goto new_sess_out;
1282 buffer = &login->req[0];
1283 pdu = (struct iscsi_login_req *)buffer;
1285 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1286 * when Status-Class != 0.
1288 conn->login_itt = pdu->itt;
1290 spin_lock_bh(&np->np_thread_lock);
1291 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1292 spin_unlock_bh(&np->np_thread_lock);
1293 pr_err("iSCSI Network Portal on %s:%hu currently not"
1294 " active.\n", np->np_ip, np->np_port);
1295 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1296 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1297 goto new_sess_out;
1299 spin_unlock_bh(&np->np_thread_lock);
1301 conn->network_transport = np->np_network_transport;
1303 pr_debug("Received iSCSI login request from %s on %s Network"
1304 " Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
1305 conn->local_ip, conn->local_port);
1307 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1308 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1310 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1311 pdu->min_version) < 0)
1312 goto new_sess_out;
1314 zero_tsih = (pdu->tsih == 0x0000);
1315 if (zero_tsih) {
1317 * This is the leading connection of a new session.
1318 * We wait until after authentication to check for
1319 * session reinstatement.
1321 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1322 goto new_sess_out;
1323 } else {
1325 * Add a new connection to an existing session.
1326 * We check for a non-existant session in
1327 * iscsi_login_non_zero_tsih_s2() below based
1328 * on ISID/TSIH, but wait until after authentication
1329 * to check for connection reinstatement, etc.
1331 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1332 goto new_sess_out;
1335 * SessionType: Discovery
1337 * Locates Default Portal
1339 * SessionType: Normal
1341 * Locates Target Portal from NP -> Target IQN
1343 rc = iscsi_target_locate_portal(np, conn, login);
1344 if (rc < 0) {
1345 tpg = conn->tpg;
1346 goto new_sess_out;
1348 login->zero_tsih = zero_tsih;
1350 tpg = conn->tpg;
1351 if (!tpg) {
1352 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1353 goto new_sess_out;
1356 if (zero_tsih) {
1357 if (iscsi_login_zero_tsih_s2(conn) < 0)
1358 goto new_sess_out;
1359 } else {
1360 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1361 goto old_sess_out;
1364 ret = iscsi_target_start_negotiation(login, conn);
1365 if (ret < 0)
1366 goto new_sess_out;
1368 if (!conn->sess) {
1369 pr_err("struct iscsi_conn session pointer is NULL!\n");
1370 goto new_sess_out;
1373 iscsi_stop_login_thread_timer(np);
1375 if (signal_pending(current))
1376 goto new_sess_out;
1378 if (ret == 1) {
1379 tpg_np = conn->tpg_np;
1381 ret = iscsi_post_login_handler(np, conn, zero_tsih);
1382 if (ret < 0)
1383 goto new_sess_out;
1385 iscsit_deaccess_np(np, tpg, tpg_np);
1388 tpg = NULL;
1389 tpg_np = NULL;
1390 /* Get another socket */
1391 return 1;
1393 new_sess_out:
1394 new_sess = true;
1395 old_sess_out:
1396 tpg_np = conn->tpg_np;
1397 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1398 new_sess = false;
1400 if (tpg) {
1401 iscsit_deaccess_np(np, tpg, tpg_np);
1402 tpg = NULL;
1403 tpg_np = NULL;
1406 out:
1407 stop = kthread_should_stop();
1408 /* Wait for another socket.. */
1409 if (!stop)
1410 return 1;
1411 exit:
1412 iscsi_stop_login_thread_timer(np);
1413 spin_lock_bh(&np->np_thread_lock);
1414 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1415 spin_unlock_bh(&np->np_thread_lock);
1417 return 0;
1420 int iscsi_target_login_thread(void *arg)
1422 struct iscsi_np *np = arg;
1423 int ret;
1425 allow_signal(SIGINT);
1427 while (!kthread_should_stop()) {
1428 ret = __iscsi_target_login_thread(np);
1430 * We break and exit here unless another sock_accept() call
1431 * is expected.
1433 if (ret != 1)
1434 break;
1437 return 0;