sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_login.c
blob450f51deb2a2ae18137ede36d4a3e8c188fd7352
1 /*******************************************************************************
2 * This file contains the login functions used by the iSCSI Target driver.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <crypto/hash.h>
20 #include <linux/string.h>
21 #include <linux/kthread.h>
22 #include <linux/idr.h>
23 #include <linux/tcp.h> /* TCP_NODELAY */
24 #include <net/ipv6.h> /* ipv6_addr_v4mapped() */
25 #include <scsi/iscsi_proto.h>
26 #include <target/target_core_base.h>
27 #include <target/target_core_fabric.h>
29 #include <target/iscsi/iscsi_target_core.h>
30 #include <target/iscsi/iscsi_target_stat.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_tpg.h"
37 #include "iscsi_target_util.h"
38 #include "iscsi_target.h"
39 #include "iscsi_target_parameters.h"
41 #include <target/iscsi/iscsi_transport.h>
43 static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
45 struct iscsi_login *login;
47 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
48 if (!login) {
49 pr_err("Unable to allocate memory for struct iscsi_login.\n");
50 return NULL;
52 conn->login = login;
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 init_completion(&conn->rx_login_comp);
88 spin_lock_init(&conn->cmd_lock);
89 spin_lock_init(&conn->conn_usage_lock);
90 spin_lock_init(&conn->immed_queue_lock);
91 spin_lock_init(&conn->nopin_timer_lock);
92 spin_lock_init(&conn->response_queue_lock);
93 spin_lock_init(&conn->state_lock);
95 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
96 pr_err("Unable to allocate conn->conn_cpumask\n");
97 goto out_conn_ops;
99 conn->conn_login = login;
101 return login;
103 out_conn_ops:
104 kfree(conn->conn_ops);
105 out_rsp_buf:
106 kfree(login->rsp_buf);
107 out_req_buf:
108 kfree(login->req_buf);
109 out_login:
110 kfree(login);
111 return NULL;
115 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
116 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
118 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
120 struct crypto_ahash *tfm;
123 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
124 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
125 * to software 1x8 byte slicing from crc32c.ko
127 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
128 if (IS_ERR(tfm)) {
129 pr_err("crypto_alloc_ahash() failed\n");
130 return -ENOMEM;
133 conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
134 if (!conn->conn_rx_hash) {
135 pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
136 crypto_free_ahash(tfm);
137 return -ENOMEM;
139 ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
141 conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
142 if (!conn->conn_tx_hash) {
143 pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
144 ahash_request_free(conn->conn_rx_hash);
145 conn->conn_rx_hash = NULL;
146 crypto_free_ahash(tfm);
147 return -ENOMEM;
149 ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
151 return 0;
154 static int iscsi_login_check_initiator_version(
155 struct iscsi_conn *conn,
156 u8 version_max,
157 u8 version_min)
159 if ((version_max != 0x00) || (version_min != 0x00)) {
160 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
161 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
162 version_min, version_max);
163 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
164 ISCSI_LOGIN_STATUS_NO_VERSION);
165 return -1;
168 return 0;
171 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
173 int sessiontype;
174 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
175 struct iscsi_portal_group *tpg = conn->tpg;
176 struct iscsi_session *sess = NULL, *sess_p = NULL;
177 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
178 struct se_session *se_sess, *se_sess_tmp;
180 initiatorname_param = iscsi_find_param_from_key(
181 INITIATORNAME, conn->param_list);
182 sessiontype_param = iscsi_find_param_from_key(
183 SESSIONTYPE, conn->param_list);
184 if (!initiatorname_param || !sessiontype_param) {
185 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
186 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
187 return -1;
190 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
192 spin_lock_bh(&se_tpg->session_lock);
193 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
194 sess_list) {
196 sess_p = se_sess->fabric_sess_ptr;
197 spin_lock(&sess_p->conn_lock);
198 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
199 atomic_read(&sess_p->session_logout) ||
200 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
201 spin_unlock(&sess_p->conn_lock);
202 continue;
204 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
205 (!strcmp(sess_p->sess_ops->InitiatorName,
206 initiatorname_param->value) &&
207 (sess_p->sess_ops->SessionType == sessiontype))) {
208 atomic_set(&sess_p->session_reinstatement, 1);
209 spin_unlock(&sess_p->conn_lock);
210 iscsit_inc_session_usage_count(sess_p);
211 iscsit_stop_time2retain_timer(sess_p);
212 sess = sess_p;
213 break;
215 spin_unlock(&sess_p->conn_lock);
217 spin_unlock_bh(&se_tpg->session_lock);
219 * If the Time2Retain handler has expired, the session is already gone.
221 if (!sess)
222 return 0;
224 pr_debug("%s iSCSI Session SID %u is still active for %s,"
225 " preforming session reinstatement.\n", (sessiontype) ?
226 "Discovery" : "Normal", sess->sid,
227 sess->sess_ops->InitiatorName);
229 spin_lock_bh(&sess->conn_lock);
230 if (sess->session_state == TARG_SESS_STATE_FAILED) {
231 spin_unlock_bh(&sess->conn_lock);
232 iscsit_dec_session_usage_count(sess);
233 iscsit_close_session(sess);
234 return 0;
236 spin_unlock_bh(&sess->conn_lock);
238 iscsit_stop_session(sess, 1, 1);
239 iscsit_dec_session_usage_count(sess);
241 iscsit_close_session(sess);
242 return 0;
245 static void iscsi_login_set_conn_values(
246 struct iscsi_session *sess,
247 struct iscsi_conn *conn,
248 __be16 cid)
250 conn->sess = sess;
251 conn->cid = be16_to_cpu(cid);
253 * Generate a random Status sequence number (statsn) for the new
254 * iSCSI connection.
256 get_random_bytes(&conn->stat_sn, sizeof(u32));
258 mutex_lock(&auth_id_lock);
259 conn->auth_id = iscsit_global->auth_id++;
260 mutex_unlock(&auth_id_lock);
263 __printf(2, 3) int iscsi_change_param_sprintf(
264 struct iscsi_conn *conn,
265 const char *fmt, ...)
267 va_list args;
268 unsigned char buf[64];
270 memset(buf, 0, sizeof buf);
272 va_start(args, fmt);
273 vsnprintf(buf, sizeof buf, fmt, args);
274 va_end(args);
276 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
277 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
278 ISCSI_LOGIN_STATUS_NO_RESOURCES);
279 return -1;
282 return 0;
284 EXPORT_SYMBOL(iscsi_change_param_sprintf);
287 * This is the leading connection of a new session,
288 * or session reinstatement.
290 static int iscsi_login_zero_tsih_s1(
291 struct iscsi_conn *conn,
292 unsigned char *buf)
294 struct iscsi_session *sess = NULL;
295 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
296 int ret;
298 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
299 if (!sess) {
300 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
301 ISCSI_LOGIN_STATUS_NO_RESOURCES);
302 pr_err("Could not allocate memory for session\n");
303 return -ENOMEM;
306 iscsi_login_set_conn_values(sess, conn, pdu->cid);
307 sess->init_task_tag = pdu->itt;
308 memcpy(&sess->isid, pdu->isid, 6);
309 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
310 INIT_LIST_HEAD(&sess->sess_conn_list);
311 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
312 INIT_LIST_HEAD(&sess->cr_active_list);
313 INIT_LIST_HEAD(&sess->cr_inactive_list);
314 init_completion(&sess->async_msg_comp);
315 init_completion(&sess->reinstatement_comp);
316 init_completion(&sess->session_wait_comp);
317 init_completion(&sess->session_waiting_on_uc_comp);
318 mutex_init(&sess->cmdsn_mutex);
319 spin_lock_init(&sess->conn_lock);
320 spin_lock_init(&sess->cr_a_lock);
321 spin_lock_init(&sess->cr_i_lock);
322 spin_lock_init(&sess->session_usage_lock);
323 spin_lock_init(&sess->ttt_lock);
325 idr_preload(GFP_KERNEL);
326 spin_lock_bh(&sess_idr_lock);
327 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
328 if (ret >= 0)
329 sess->session_index = ret;
330 spin_unlock_bh(&sess_idr_lock);
331 idr_preload_end();
333 if (ret < 0) {
334 pr_err("idr_alloc() for sess_idr failed\n");
335 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
336 ISCSI_LOGIN_STATUS_NO_RESOURCES);
337 kfree(sess);
338 return -ENOMEM;
341 sess->creation_time = get_jiffies_64();
343 * The FFP CmdSN window values will be allocated from the TPG's
344 * Initiator Node's ACL once the login has been successfully completed.
346 atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
348 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
349 if (!sess->sess_ops) {
350 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
351 ISCSI_LOGIN_STATUS_NO_RESOURCES);
352 pr_err("Unable to allocate memory for"
353 " struct iscsi_sess_ops.\n");
354 kfree(sess);
355 return -ENOMEM;
358 sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
359 if (IS_ERR(sess->se_sess)) {
360 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
361 ISCSI_LOGIN_STATUS_NO_RESOURCES);
362 kfree(sess->sess_ops);
363 kfree(sess);
364 return -ENOMEM;
367 return 0;
370 static int iscsi_login_zero_tsih_s2(
371 struct iscsi_conn *conn)
373 struct iscsi_node_attrib *na;
374 struct iscsi_session *sess = conn->sess;
375 bool iser = false;
377 sess->tpg = conn->tpg;
380 * Assign a new TPG Session Handle. Note this is protected with
381 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
383 sess->tsih = ++sess->tpg->ntsih;
384 if (!sess->tsih)
385 sess->tsih = ++sess->tpg->ntsih;
388 * Create the default params from user defined values..
390 if (iscsi_copy_param_list(&conn->param_list,
391 conn->tpg->param_list, 1) < 0) {
392 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
393 ISCSI_LOGIN_STATUS_NO_RESOURCES);
394 return -1;
397 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
398 iser = true;
400 iscsi_set_keys_to_negotiate(conn->param_list, iser);
402 if (sess->sess_ops->SessionType)
403 return iscsi_set_keys_irrelevant_for_discovery(
404 conn->param_list);
406 na = iscsit_tpg_get_node_attrib(sess);
409 * Need to send TargetPortalGroupTag back in first login response
410 * on any iSCSI connection where the Initiator provides TargetName.
411 * See 5.3.1. Login Phase Start
413 * In our case, we have already located the struct iscsi_tiqn at this point.
415 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
416 return -1;
419 * Workaround for Initiators that have broken connection recovery logic.
421 * "We would really like to get rid of this." Linux-iSCSI.org team
423 if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
424 return -1;
427 * Set RDMAExtensions=Yes by default for iSER enabled network portals
429 if (iser) {
430 struct iscsi_param *param;
431 unsigned long mrdsl, off;
432 int rc;
434 if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
435 return -1;
438 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
439 * Immediate Data + Unsolicited Data-OUT if necessary..
441 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
442 conn->param_list);
443 if (!param) {
444 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
445 ISCSI_LOGIN_STATUS_NO_RESOURCES);
446 return -1;
448 rc = kstrtoul(param->value, 0, &mrdsl);
449 if (rc < 0) {
450 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
451 ISCSI_LOGIN_STATUS_NO_RESOURCES);
452 return -1;
454 off = mrdsl % PAGE_SIZE;
455 if (!off)
456 goto check_prot;
458 if (mrdsl < PAGE_SIZE)
459 mrdsl = PAGE_SIZE;
460 else
461 mrdsl -= off;
463 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
464 " to PAGE_SIZE\n", mrdsl);
466 if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
467 return -1;
469 * ISER currently requires that ImmediateData + Unsolicited
470 * Data be disabled when protection / signature MRs are enabled.
472 check_prot:
473 if (sess->se_sess->sup_prot_ops &
474 (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
475 TARGET_PROT_DOUT_INSERT)) {
477 if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
478 return -1;
480 if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
481 return -1;
483 pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
484 " T10-PI enabled ISER session\n");
488 return 0;
491 static int iscsi_login_non_zero_tsih_s1(
492 struct iscsi_conn *conn,
493 unsigned char *buf)
495 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
497 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
498 return 0;
502 * Add a new connection to an existing session.
504 static int iscsi_login_non_zero_tsih_s2(
505 struct iscsi_conn *conn,
506 unsigned char *buf)
508 struct iscsi_portal_group *tpg = conn->tpg;
509 struct iscsi_session *sess = NULL, *sess_p = NULL;
510 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
511 struct se_session *se_sess, *se_sess_tmp;
512 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
513 bool iser = false;
515 spin_lock_bh(&se_tpg->session_lock);
516 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
517 sess_list) {
519 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
520 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
521 atomic_read(&sess_p->session_logout) ||
522 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
523 continue;
524 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
525 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
526 iscsit_inc_session_usage_count(sess_p);
527 iscsit_stop_time2retain_timer(sess_p);
528 sess = sess_p;
529 break;
532 spin_unlock_bh(&se_tpg->session_lock);
535 * If the Time2Retain handler has expired, the session is already gone.
537 if (!sess) {
538 pr_err("Initiator attempting to add a connection to"
539 " a non-existent session, rejecting iSCSI Login.\n");
540 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
541 ISCSI_LOGIN_STATUS_NO_SESSION);
542 return -1;
546 * Stop the Time2Retain timer if this is a failed session, we restart
547 * the timer if the login is not successful.
549 spin_lock_bh(&sess->conn_lock);
550 if (sess->session_state == TARG_SESS_STATE_FAILED)
551 atomic_set(&sess->session_continuation, 1);
552 spin_unlock_bh(&sess->conn_lock);
554 iscsi_login_set_conn_values(sess, conn, pdu->cid);
556 if (iscsi_copy_param_list(&conn->param_list,
557 conn->tpg->param_list, 0) < 0) {
558 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
559 ISCSI_LOGIN_STATUS_NO_RESOURCES);
560 return -1;
563 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
564 iser = true;
566 iscsi_set_keys_to_negotiate(conn->param_list, iser);
568 * Need to send TargetPortalGroupTag back in first login response
569 * on any iSCSI connection where the Initiator provides TargetName.
570 * See 5.3.1. Login Phase Start
572 * In our case, we have already located the struct iscsi_tiqn at this point.
574 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
575 return -1;
577 return 0;
580 int iscsi_login_post_auth_non_zero_tsih(
581 struct iscsi_conn *conn,
582 u16 cid,
583 u32 exp_statsn)
585 struct iscsi_conn *conn_ptr = NULL;
586 struct iscsi_conn_recovery *cr = NULL;
587 struct iscsi_session *sess = conn->sess;
590 * By following item 5 in the login table, if we have found
591 * an existing ISID and a valid/existing TSIH and an existing
592 * CID we do connection reinstatement. Currently we dont not
593 * support it so we send back an non-zero status class to the
594 * initiator and release the new connection.
596 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
597 if (conn_ptr) {
598 pr_err("Connection exists with CID %hu for %s,"
599 " performing connection reinstatement.\n",
600 conn_ptr->cid, sess->sess_ops->InitiatorName);
602 iscsit_connection_reinstatement_rcfr(conn_ptr);
603 iscsit_dec_conn_usage_count(conn_ptr);
607 * Check for any connection recovery entires containing CID.
608 * We use the original ExpStatSN sent in the first login request
609 * to acknowledge commands for the failed connection.
611 * Also note that an explict logout may have already been sent,
612 * but the response may not be sent due to additional connection
613 * loss.
615 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
616 cr = iscsit_get_inactive_connection_recovery_entry(
617 sess, cid);
618 if (cr) {
619 pr_debug("Performing implicit logout"
620 " for connection recovery on CID: %hu\n",
621 conn->cid);
622 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
627 * Else we follow item 4 from the login table in that we have
628 * found an existing ISID and a valid/existing TSIH and a new
629 * CID we go ahead and continue to add a new connection to the
630 * session.
632 pr_debug("Adding CID %hu to existing session for %s.\n",
633 cid, sess->sess_ops->InitiatorName);
635 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
636 pr_err("Adding additional connection to this session"
637 " would exceed MaxConnections %d, login failed.\n",
638 sess->sess_ops->MaxConnections);
639 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
640 ISCSI_LOGIN_STATUS_ISID_ERROR);
641 return -1;
644 return 0;
647 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
649 struct iscsi_session *sess = conn->sess;
651 * FIXME: Unsolicited NopIN support for ISER
653 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
654 return;
656 if (!sess->sess_ops->SessionType)
657 iscsit_start_nopin_timer(conn);
660 int iscsit_start_kthreads(struct iscsi_conn *conn)
662 int ret = 0;
664 spin_lock(&iscsit_global->ts_bitmap_lock);
665 conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
666 ISCSIT_BITMAP_BITS, get_order(1));
667 spin_unlock(&iscsit_global->ts_bitmap_lock);
669 if (conn->bitmap_id < 0) {
670 pr_err("bitmap_find_free_region() failed for"
671 " iscsit_start_kthreads()\n");
672 return -ENOMEM;
675 conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
676 "%s", ISCSI_TX_THREAD_NAME);
677 if (IS_ERR(conn->tx_thread)) {
678 pr_err("Unable to start iscsi_target_tx_thread\n");
679 ret = PTR_ERR(conn->tx_thread);
680 goto out_bitmap;
682 conn->tx_thread_active = true;
684 conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
685 "%s", ISCSI_RX_THREAD_NAME);
686 if (IS_ERR(conn->rx_thread)) {
687 pr_err("Unable to start iscsi_target_rx_thread\n");
688 ret = PTR_ERR(conn->rx_thread);
689 goto out_tx;
691 conn->rx_thread_active = true;
693 return 0;
694 out_tx:
695 send_sig(SIGINT, conn->tx_thread, 1);
696 kthread_stop(conn->tx_thread);
697 conn->tx_thread_active = false;
698 out_bitmap:
699 spin_lock(&iscsit_global->ts_bitmap_lock);
700 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
701 get_order(1));
702 spin_unlock(&iscsit_global->ts_bitmap_lock);
703 return ret;
706 void iscsi_post_login_handler(
707 struct iscsi_np *np,
708 struct iscsi_conn *conn,
709 u8 zero_tsih)
711 int stop_timer = 0;
712 struct iscsi_session *sess = conn->sess;
713 struct se_session *se_sess = sess->se_sess;
714 struct iscsi_portal_group *tpg = sess->tpg;
715 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
717 iscsit_inc_conn_usage_count(conn);
719 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
720 ISCSI_LOGIN_STATUS_ACCEPT);
722 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
723 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
725 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
727 * SCSI Initiator -> SCSI Target Port Mapping
729 if (!zero_tsih) {
730 iscsi_set_session_parameters(sess->sess_ops,
731 conn->param_list, 0);
732 iscsi_release_param_list(conn->param_list);
733 conn->param_list = NULL;
735 spin_lock_bh(&sess->conn_lock);
736 atomic_set(&sess->session_continuation, 0);
737 if (sess->session_state == TARG_SESS_STATE_FAILED) {
738 pr_debug("Moving to"
739 " TARG_SESS_STATE_LOGGED_IN.\n");
740 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
741 stop_timer = 1;
744 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
745 " %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
746 &conn->local_sockaddr, tpg->tpgt);
748 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
749 atomic_inc(&sess->nconn);
750 pr_debug("Incremented iSCSI Connection count to %hu"
751 " from node: %s\n", atomic_read(&sess->nconn),
752 sess->sess_ops->InitiatorName);
753 spin_unlock_bh(&sess->conn_lock);
755 iscsi_post_login_start_timers(conn);
757 * Determine CPU mask to ensure connection's RX and TX kthreads
758 * are scheduled on the same CPU.
760 iscsit_thread_get_cpumask(conn);
761 conn->conn_rx_reset_cpumask = 1;
762 conn->conn_tx_reset_cpumask = 1;
764 * Wakeup the sleeping iscsi_target_rx_thread() now that
765 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
767 complete(&conn->rx_login_comp);
768 iscsit_dec_conn_usage_count(conn);
770 if (stop_timer) {
771 spin_lock_bh(&se_tpg->session_lock);
772 iscsit_stop_time2retain_timer(sess);
773 spin_unlock_bh(&se_tpg->session_lock);
775 iscsit_dec_session_usage_count(sess);
776 return;
779 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
780 iscsi_release_param_list(conn->param_list);
781 conn->param_list = NULL;
783 iscsit_determine_maxcmdsn(sess);
785 spin_lock_bh(&se_tpg->session_lock);
786 __transport_register_session(&sess->tpg->tpg_se_tpg,
787 se_sess->se_node_acl, se_sess, sess);
788 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
789 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
791 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
792 conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
793 tpg->tpgt);
795 spin_lock_bh(&sess->conn_lock);
796 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
797 atomic_inc(&sess->nconn);
798 pr_debug("Incremented iSCSI Connection count to %hu from node:"
799 " %s\n", atomic_read(&sess->nconn),
800 sess->sess_ops->InitiatorName);
801 spin_unlock_bh(&sess->conn_lock);
803 sess->sid = tpg->sid++;
804 if (!sess->sid)
805 sess->sid = tpg->sid++;
806 pr_debug("Established iSCSI session from node: %s\n",
807 sess->sess_ops->InitiatorName);
809 tpg->nsessions++;
810 if (tpg->tpg_tiqn)
811 tpg->tpg_tiqn->tiqn_nsessions++;
813 pr_debug("Incremented number of active iSCSI sessions to %u on"
814 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
815 spin_unlock_bh(&se_tpg->session_lock);
817 iscsi_post_login_start_timers(conn);
819 * Determine CPU mask to ensure connection's RX and TX kthreads
820 * are scheduled on the same CPU.
822 iscsit_thread_get_cpumask(conn);
823 conn->conn_rx_reset_cpumask = 1;
824 conn->conn_tx_reset_cpumask = 1;
826 * Wakeup the sleeping iscsi_target_rx_thread() now that
827 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
829 complete(&conn->rx_login_comp);
830 iscsit_dec_conn_usage_count(conn);
833 static void iscsi_handle_login_thread_timeout(unsigned long data)
835 struct iscsi_np *np = (struct iscsi_np *) data;
837 spin_lock_bh(&np->np_thread_lock);
838 pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
839 &np->np_sockaddr);
841 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
842 spin_unlock_bh(&np->np_thread_lock);
843 return;
846 if (np->np_thread)
847 send_sig(SIGINT, np->np_thread, 1);
849 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
850 spin_unlock_bh(&np->np_thread_lock);
853 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
856 * This used the TA_LOGIN_TIMEOUT constant because at this
857 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
859 spin_lock_bh(&np->np_thread_lock);
860 init_timer(&np->np_login_timer);
861 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
862 np->np_login_timer.data = (unsigned long)np;
863 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
864 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
865 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
866 add_timer(&np->np_login_timer);
868 pr_debug("Added timeout timer to iSCSI login request for"
869 " %u seconds.\n", TA_LOGIN_TIMEOUT);
870 spin_unlock_bh(&np->np_thread_lock);
873 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
875 spin_lock_bh(&np->np_thread_lock);
876 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
877 spin_unlock_bh(&np->np_thread_lock);
878 return;
880 np->np_login_timer_flags |= ISCSI_TF_STOP;
881 spin_unlock_bh(&np->np_thread_lock);
883 del_timer_sync(&np->np_login_timer);
885 spin_lock_bh(&np->np_thread_lock);
886 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
887 spin_unlock_bh(&np->np_thread_lock);
890 int iscsit_setup_np(
891 struct iscsi_np *np,
892 struct sockaddr_storage *sockaddr)
894 struct socket *sock = NULL;
895 int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
897 switch (np->np_network_transport) {
898 case ISCSI_TCP:
899 np->np_ip_proto = IPPROTO_TCP;
900 np->np_sock_type = SOCK_STREAM;
901 break;
902 case ISCSI_SCTP_TCP:
903 np->np_ip_proto = IPPROTO_SCTP;
904 np->np_sock_type = SOCK_STREAM;
905 break;
906 case ISCSI_SCTP_UDP:
907 np->np_ip_proto = IPPROTO_SCTP;
908 np->np_sock_type = SOCK_SEQPACKET;
909 break;
910 default:
911 pr_err("Unsupported network_transport: %d\n",
912 np->np_network_transport);
913 return -EINVAL;
916 np->np_ip_proto = IPPROTO_TCP;
917 np->np_sock_type = SOCK_STREAM;
919 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
920 np->np_ip_proto, &sock);
921 if (ret < 0) {
922 pr_err("sock_create() failed.\n");
923 return ret;
925 np->np_socket = sock;
927 * Setup the np->np_sockaddr from the passed sockaddr setup
928 * in iscsi_target_configfs.c code..
930 memcpy(&np->np_sockaddr, sockaddr,
931 sizeof(struct sockaddr_storage));
933 if (sockaddr->ss_family == AF_INET6)
934 len = sizeof(struct sockaddr_in6);
935 else
936 len = sizeof(struct sockaddr_in);
938 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
940 /* FIXME: Someone please explain why this is endian-safe */
941 opt = 1;
942 if (np->np_network_transport == ISCSI_TCP) {
943 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
944 (char *)&opt, sizeof(opt));
945 if (ret < 0) {
946 pr_err("kernel_setsockopt() for TCP_NODELAY"
947 " failed: %d\n", ret);
948 goto fail;
952 /* FIXME: Someone please explain why this is endian-safe */
953 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
954 (char *)&opt, sizeof(opt));
955 if (ret < 0) {
956 pr_err("kernel_setsockopt() for SO_REUSEADDR"
957 " failed\n");
958 goto fail;
961 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
962 (char *)&opt, sizeof(opt));
963 if (ret < 0) {
964 pr_err("kernel_setsockopt() for IP_FREEBIND"
965 " failed\n");
966 goto fail;
969 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
970 if (ret < 0) {
971 pr_err("kernel_bind() failed: %d\n", ret);
972 goto fail;
975 ret = kernel_listen(sock, backlog);
976 if (ret != 0) {
977 pr_err("kernel_listen() failed: %d\n", ret);
978 goto fail;
981 return 0;
982 fail:
983 np->np_socket = NULL;
984 sock_release(sock);
985 return ret;
988 int iscsi_target_setup_login_socket(
989 struct iscsi_np *np,
990 struct sockaddr_storage *sockaddr)
992 struct iscsit_transport *t;
993 int rc;
995 t = iscsit_get_transport(np->np_network_transport);
996 if (!t)
997 return -EINVAL;
999 rc = t->iscsit_setup_np(np, sockaddr);
1000 if (rc < 0) {
1001 iscsit_put_transport(t);
1002 return rc;
1005 np->np_transport = t;
1006 np->enabled = true;
1007 return 0;
1010 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1012 struct socket *new_sock, *sock = np->np_socket;
1013 struct sockaddr_in sock_in;
1014 struct sockaddr_in6 sock_in6;
1015 int rc, err;
1017 rc = kernel_accept(sock, &new_sock, 0);
1018 if (rc < 0)
1019 return rc;
1021 conn->sock = new_sock;
1022 conn->login_family = np->np_sockaddr.ss_family;
1024 if (np->np_sockaddr.ss_family == AF_INET6) {
1025 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1027 rc = conn->sock->ops->getname(conn->sock,
1028 (struct sockaddr *)&sock_in6, &err, 1);
1029 if (!rc) {
1030 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1031 memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
1032 } else {
1033 /* Pretend to be an ipv4 socket */
1034 sock_in.sin_family = AF_INET;
1035 sock_in.sin_port = sock_in6.sin6_port;
1036 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1037 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1041 rc = conn->sock->ops->getname(conn->sock,
1042 (struct sockaddr *)&sock_in6, &err, 0);
1043 if (!rc) {
1044 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1045 memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
1046 } else {
1047 /* Pretend to be an ipv4 socket */
1048 sock_in.sin_family = AF_INET;
1049 sock_in.sin_port = sock_in6.sin6_port;
1050 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1051 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1054 } else {
1055 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1057 rc = conn->sock->ops->getname(conn->sock,
1058 (struct sockaddr *)&sock_in, &err, 1);
1059 if (!rc)
1060 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1062 rc = conn->sock->ops->getname(conn->sock,
1063 (struct sockaddr *)&sock_in, &err, 0);
1064 if (!rc)
1065 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1068 return 0;
1071 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1073 struct iscsi_login_req *login_req;
1074 u32 padding = 0, payload_length;
1076 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1077 return -1;
1079 login_req = (struct iscsi_login_req *)login->req;
1080 payload_length = ntoh24(login_req->dlength);
1081 padding = ((-payload_length) & 3);
1083 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1084 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1085 login_req->flags, login_req->itt, login_req->cmdsn,
1086 login_req->exp_statsn, login_req->cid, payload_length);
1088 * Setup the initial iscsi_login values from the leading
1089 * login request PDU.
1091 if (login->first_request) {
1092 login_req = (struct iscsi_login_req *)login->req;
1093 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1094 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1095 login->version_min = login_req->min_version;
1096 login->version_max = login_req->max_version;
1097 memcpy(login->isid, login_req->isid, 6);
1098 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1099 login->init_task_tag = login_req->itt;
1100 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1101 login->cid = be16_to_cpu(login_req->cid);
1102 login->tsih = be16_to_cpu(login_req->tsih);
1105 if (iscsi_target_check_login_request(conn, login) < 0)
1106 return -1;
1108 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1109 if (iscsi_login_rx_data(conn, login->req_buf,
1110 payload_length + padding) < 0)
1111 return -1;
1113 return 0;
1116 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1117 u32 length)
1119 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1120 return -1;
1122 return 0;
1125 static int
1126 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1128 int rc;
1130 if (!t->owner) {
1131 conn->conn_transport = t;
1132 return 0;
1135 rc = try_module_get(t->owner);
1136 if (!rc) {
1137 pr_err("try_module_get() failed for %s\n", t->name);
1138 return -EINVAL;
1141 conn->conn_transport = t;
1142 return 0;
1145 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1146 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1148 if (!new_sess)
1149 goto old_sess_out;
1151 pr_err("iSCSI Login negotiation failed.\n");
1152 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1153 ISCSI_LOGIN_STATUS_INIT_ERR);
1154 if (!zero_tsih || !conn->sess)
1155 goto old_sess_out;
1156 if (conn->sess->se_sess)
1157 transport_free_session(conn->sess->se_sess);
1158 if (conn->sess->session_index != 0) {
1159 spin_lock_bh(&sess_idr_lock);
1160 idr_remove(&sess_idr, conn->sess->session_index);
1161 spin_unlock_bh(&sess_idr_lock);
1163 kfree(conn->sess->sess_ops);
1164 kfree(conn->sess);
1165 conn->sess = NULL;
1167 old_sess_out:
1168 iscsi_stop_login_thread_timer(np);
1170 * If login negotiation fails check if the Time2Retain timer
1171 * needs to be restarted.
1173 if (!zero_tsih && conn->sess) {
1174 spin_lock_bh(&conn->sess->conn_lock);
1175 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1176 struct se_portal_group *se_tpg =
1177 &conn->tpg->tpg_se_tpg;
1179 atomic_set(&conn->sess->session_continuation, 0);
1180 spin_unlock_bh(&conn->sess->conn_lock);
1181 spin_lock_bh(&se_tpg->session_lock);
1182 iscsit_start_time2retain_handler(conn->sess);
1183 spin_unlock_bh(&se_tpg->session_lock);
1184 } else
1185 spin_unlock_bh(&conn->sess->conn_lock);
1186 iscsit_dec_session_usage_count(conn->sess);
1189 ahash_request_free(conn->conn_tx_hash);
1190 if (conn->conn_rx_hash) {
1191 struct crypto_ahash *tfm;
1193 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
1194 ahash_request_free(conn->conn_rx_hash);
1195 crypto_free_ahash(tfm);
1198 free_cpumask_var(conn->conn_cpumask);
1200 kfree(conn->conn_ops);
1202 if (conn->param_list) {
1203 iscsi_release_param_list(conn->param_list);
1204 conn->param_list = NULL;
1206 iscsi_target_nego_release(conn);
1208 if (conn->sock) {
1209 sock_release(conn->sock);
1210 conn->sock = NULL;
1213 if (conn->conn_transport->iscsit_wait_conn)
1214 conn->conn_transport->iscsit_wait_conn(conn);
1216 if (conn->conn_transport->iscsit_free_conn)
1217 conn->conn_transport->iscsit_free_conn(conn);
1219 iscsit_put_transport(conn->conn_transport);
1220 kfree(conn);
1223 static int __iscsi_target_login_thread(struct iscsi_np *np)
1225 u8 *buffer, zero_tsih = 0;
1226 int ret = 0, rc;
1227 struct iscsi_conn *conn = NULL;
1228 struct iscsi_login *login;
1229 struct iscsi_portal_group *tpg = NULL;
1230 struct iscsi_login_req *pdu;
1231 struct iscsi_tpg_np *tpg_np;
1232 bool new_sess = false;
1234 flush_signals(current);
1236 spin_lock_bh(&np->np_thread_lock);
1237 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1238 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1239 complete(&np->np_restart_comp);
1240 } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1241 spin_unlock_bh(&np->np_thread_lock);
1242 goto exit;
1243 } else {
1244 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1246 spin_unlock_bh(&np->np_thread_lock);
1248 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1249 if (!conn) {
1250 pr_err("Could not allocate memory for"
1251 " new connection\n");
1252 /* Get another socket */
1253 return 1;
1255 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1256 conn->conn_state = TARG_CONN_STATE_FREE;
1258 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1259 kfree(conn);
1260 return 1;
1263 rc = np->np_transport->iscsit_accept_np(np, conn);
1264 if (rc == -ENOSYS) {
1265 complete(&np->np_restart_comp);
1266 iscsit_put_transport(conn->conn_transport);
1267 kfree(conn);
1268 conn = NULL;
1269 goto exit;
1270 } else if (rc < 0) {
1271 spin_lock_bh(&np->np_thread_lock);
1272 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1273 spin_unlock_bh(&np->np_thread_lock);
1274 complete(&np->np_restart_comp);
1275 iscsit_put_transport(conn->conn_transport);
1276 kfree(conn);
1277 conn = NULL;
1278 /* Get another socket */
1279 return 1;
1281 spin_unlock_bh(&np->np_thread_lock);
1282 iscsit_put_transport(conn->conn_transport);
1283 kfree(conn);
1284 conn = NULL;
1285 goto out;
1288 * Perform the remaining iSCSI connection initialization items..
1290 login = iscsi_login_init_conn(conn);
1291 if (!login) {
1292 goto new_sess_out;
1295 iscsi_start_login_thread_timer(np);
1297 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1298 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1300 * This will process the first login request + payload..
1302 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1303 if (rc == 1)
1304 return 1;
1305 else if (rc < 0)
1306 goto new_sess_out;
1308 buffer = &login->req[0];
1309 pdu = (struct iscsi_login_req *)buffer;
1311 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1312 * when Status-Class != 0.
1314 conn->login_itt = pdu->itt;
1316 spin_lock_bh(&np->np_thread_lock);
1317 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1318 spin_unlock_bh(&np->np_thread_lock);
1319 pr_err("iSCSI Network Portal on %pISpc currently not"
1320 " active.\n", &np->np_sockaddr);
1321 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1322 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1323 goto new_sess_out;
1325 spin_unlock_bh(&np->np_thread_lock);
1327 conn->network_transport = np->np_network_transport;
1329 pr_debug("Received iSCSI login request from %pISpc on %s Network"
1330 " Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
1331 &conn->local_sockaddr);
1333 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1334 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1336 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1337 pdu->min_version) < 0)
1338 goto new_sess_out;
1340 zero_tsih = (pdu->tsih == 0x0000);
1341 if (zero_tsih) {
1343 * This is the leading connection of a new session.
1344 * We wait until after authentication to check for
1345 * session reinstatement.
1347 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1348 goto new_sess_out;
1349 } else {
1351 * Add a new connection to an existing session.
1352 * We check for a non-existant session in
1353 * iscsi_login_non_zero_tsih_s2() below based
1354 * on ISID/TSIH, but wait until after authentication
1355 * to check for connection reinstatement, etc.
1357 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1358 goto new_sess_out;
1361 * SessionType: Discovery
1363 * Locates Default Portal
1365 * SessionType: Normal
1367 * Locates Target Portal from NP -> Target IQN
1369 rc = iscsi_target_locate_portal(np, conn, login);
1370 if (rc < 0) {
1371 tpg = conn->tpg;
1372 goto new_sess_out;
1374 login->zero_tsih = zero_tsih;
1376 if (conn->sess)
1377 conn->sess->se_sess->sup_prot_ops =
1378 conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1380 tpg = conn->tpg;
1381 if (!tpg) {
1382 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1383 goto new_sess_out;
1386 if (zero_tsih) {
1387 if (iscsi_login_zero_tsih_s2(conn) < 0)
1388 goto new_sess_out;
1389 } else {
1390 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1391 goto old_sess_out;
1394 if (conn->conn_transport->iscsit_validate_params) {
1395 ret = conn->conn_transport->iscsit_validate_params(conn);
1396 if (ret < 0) {
1397 if (zero_tsih)
1398 goto new_sess_out;
1399 else
1400 goto old_sess_out;
1404 ret = iscsi_target_start_negotiation(login, conn);
1405 if (ret < 0)
1406 goto new_sess_out;
1408 iscsi_stop_login_thread_timer(np);
1410 if (ret == 1) {
1411 tpg_np = conn->tpg_np;
1413 iscsi_post_login_handler(np, conn, zero_tsih);
1414 iscsit_deaccess_np(np, tpg, tpg_np);
1417 tpg = NULL;
1418 tpg_np = NULL;
1419 /* Get another socket */
1420 return 1;
1422 new_sess_out:
1423 new_sess = true;
1424 old_sess_out:
1425 tpg_np = conn->tpg_np;
1426 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1427 new_sess = false;
1429 if (tpg) {
1430 iscsit_deaccess_np(np, tpg, tpg_np);
1431 tpg = NULL;
1432 tpg_np = NULL;
1435 out:
1436 return 1;
1438 exit:
1439 iscsi_stop_login_thread_timer(np);
1440 spin_lock_bh(&np->np_thread_lock);
1441 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1442 spin_unlock_bh(&np->np_thread_lock);
1444 return 0;
1447 int iscsi_target_login_thread(void *arg)
1449 struct iscsi_np *np = arg;
1450 int ret;
1452 allow_signal(SIGINT);
1454 while (1) {
1455 ret = __iscsi_target_login_thread(np);
1457 * We break and exit here unless another sock_accept() call
1458 * is expected.
1460 if (ret != 1)
1461 break;
1464 return 0;