Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*******************************************************************************
0003  * This file contains the login functions used by the iSCSI Target driver.
0004  *
0005  * (c) Copyright 2007-2013 Datera, Inc.
0006  *
0007  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
0008  *
0009  ******************************************************************************/
0010 
0011 #include <crypto/hash.h>
0012 #include <linux/module.h>
0013 #include <linux/string.h>
0014 #include <linux/kthread.h>
0015 #include <linux/sched/signal.h>
0016 #include <linux/idr.h>
0017 #include <linux/tcp.h>        /* TCP_NODELAY */
0018 #include <net/ip.h>
0019 #include <net/ipv6.h>         /* ipv6_addr_v4mapped() */
0020 #include <scsi/iscsi_proto.h>
0021 #include <target/target_core_base.h>
0022 #include <target/target_core_fabric.h>
0023 
0024 #include <target/iscsi/iscsi_target_core.h>
0025 #include <target/iscsi/iscsi_target_stat.h>
0026 #include "iscsi_target_device.h"
0027 #include "iscsi_target_nego.h"
0028 #include "iscsi_target_erl0.h"
0029 #include "iscsi_target_erl2.h"
0030 #include "iscsi_target_login.h"
0031 #include "iscsi_target_tpg.h"
0032 #include "iscsi_target_util.h"
0033 #include "iscsi_target.h"
0034 #include "iscsi_target_parameters.h"
0035 
0036 #include <target/iscsi/iscsi_transport.h>
0037 
0038 static struct iscsi_login *iscsi_login_init_conn(struct iscsit_conn *conn)
0039 {
0040     struct iscsi_login *login;
0041 
0042     login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
0043     if (!login) {
0044         pr_err("Unable to allocate memory for struct iscsi_login.\n");
0045         return NULL;
0046     }
0047     conn->login = login;
0048     login->conn = conn;
0049     login->first_request = 1;
0050 
0051     login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
0052     if (!login->req_buf) {
0053         pr_err("Unable to allocate memory for response buffer.\n");
0054         goto out_login;
0055     }
0056 
0057     login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
0058     if (!login->rsp_buf) {
0059         pr_err("Unable to allocate memory for request buffer.\n");
0060         goto out_req_buf;
0061     }
0062 
0063     conn->conn_login = login;
0064 
0065     return login;
0066 
0067 out_req_buf:
0068     kfree(login->req_buf);
0069 out_login:
0070     kfree(login);
0071     return NULL;
0072 }
0073 
0074 /*
0075  * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
0076  * per struct iscsit_conn libcrypto contexts for crc32c and crc32-intel
0077  */
0078 int iscsi_login_setup_crypto(struct iscsit_conn *conn)
0079 {
0080     struct crypto_ahash *tfm;
0081 
0082     /*
0083      * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
0084      * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
0085      * to software 1x8 byte slicing from crc32c.ko
0086      */
0087     tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
0088     if (IS_ERR(tfm)) {
0089         pr_err("crypto_alloc_ahash() failed\n");
0090         return -ENOMEM;
0091     }
0092 
0093     conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
0094     if (!conn->conn_rx_hash) {
0095         pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
0096         crypto_free_ahash(tfm);
0097         return -ENOMEM;
0098     }
0099     ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
0100 
0101     conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
0102     if (!conn->conn_tx_hash) {
0103         pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
0104         ahash_request_free(conn->conn_rx_hash);
0105         conn->conn_rx_hash = NULL;
0106         crypto_free_ahash(tfm);
0107         return -ENOMEM;
0108     }
0109     ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
0110 
0111     return 0;
0112 }
0113 
0114 static int iscsi_login_check_initiator_version(
0115     struct iscsit_conn *conn,
0116     u8 version_max,
0117     u8 version_min)
0118 {
0119     if ((version_max != 0x00) || (version_min != 0x00)) {
0120         pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
0121             " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
0122             version_min, version_max);
0123         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
0124                 ISCSI_LOGIN_STATUS_NO_VERSION);
0125         return -1;
0126     }
0127 
0128     return 0;
0129 }
0130 
0131 int iscsi_check_for_session_reinstatement(struct iscsit_conn *conn)
0132 {
0133     int sessiontype;
0134     struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
0135     struct iscsi_portal_group *tpg = conn->tpg;
0136     struct iscsit_session *sess = NULL, *sess_p = NULL;
0137     struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
0138     struct se_session *se_sess, *se_sess_tmp;
0139 
0140     initiatorname_param = iscsi_find_param_from_key(
0141             INITIATORNAME, conn->param_list);
0142     sessiontype_param = iscsi_find_param_from_key(
0143             SESSIONTYPE, conn->param_list);
0144     if (!initiatorname_param || !sessiontype_param) {
0145         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
0146             ISCSI_LOGIN_STATUS_MISSING_FIELDS);
0147         return -1;
0148     }
0149 
0150     sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
0151 
0152     spin_lock_bh(&se_tpg->session_lock);
0153     list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
0154             sess_list) {
0155 
0156         sess_p = se_sess->fabric_sess_ptr;
0157         spin_lock(&sess_p->conn_lock);
0158         if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
0159             atomic_read(&sess_p->session_logout) ||
0160             atomic_read(&sess_p->session_close) ||
0161             (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
0162             spin_unlock(&sess_p->conn_lock);
0163             continue;
0164         }
0165         if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
0166            (!strcmp(sess_p->sess_ops->InitiatorName,
0167                 initiatorname_param->value) &&
0168            (sess_p->sess_ops->SessionType == sessiontype))) {
0169             atomic_set(&sess_p->session_reinstatement, 1);
0170             atomic_set(&sess_p->session_fall_back_to_erl0, 1);
0171             atomic_set(&sess_p->session_close, 1);
0172             spin_unlock(&sess_p->conn_lock);
0173             iscsit_inc_session_usage_count(sess_p);
0174             iscsit_stop_time2retain_timer(sess_p);
0175             sess = sess_p;
0176             break;
0177         }
0178         spin_unlock(&sess_p->conn_lock);
0179     }
0180     spin_unlock_bh(&se_tpg->session_lock);
0181     /*
0182      * If the Time2Retain handler has expired, the session is already gone.
0183      */
0184     if (!sess)
0185         return 0;
0186 
0187     pr_debug("%s iSCSI Session SID %u is still active for %s,"
0188         " performing session reinstatement.\n", (sessiontype) ?
0189         "Discovery" : "Normal", sess->sid,
0190         sess->sess_ops->InitiatorName);
0191 
0192     spin_lock_bh(&sess->conn_lock);
0193     if (sess->session_state == TARG_SESS_STATE_FAILED) {
0194         spin_unlock_bh(&sess->conn_lock);
0195         iscsit_dec_session_usage_count(sess);
0196         return 0;
0197     }
0198     spin_unlock_bh(&sess->conn_lock);
0199 
0200     iscsit_stop_session(sess, 1, 1);
0201     iscsit_dec_session_usage_count(sess);
0202 
0203     return 0;
0204 }
0205 
0206 static int iscsi_login_set_conn_values(
0207     struct iscsit_session *sess,
0208     struct iscsit_conn *conn,
0209     __be16 cid)
0210 {
0211     int ret;
0212     conn->sess      = sess;
0213     conn->cid       = be16_to_cpu(cid);
0214     /*
0215      * Generate a random Status sequence number (statsn) for the new
0216      * iSCSI connection.
0217      */
0218     ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32));
0219     if (unlikely(ret))
0220         return ret;
0221 
0222     mutex_lock(&auth_id_lock);
0223     conn->auth_id       = iscsit_global->auth_id++;
0224     mutex_unlock(&auth_id_lock);
0225     return 0;
0226 }
0227 
0228 __printf(2, 3) int iscsi_change_param_sprintf(
0229     struct iscsit_conn *conn,
0230     const char *fmt, ...)
0231 {
0232     va_list args;
0233     unsigned char buf[64];
0234 
0235     memset(buf, 0, sizeof buf);
0236 
0237     va_start(args, fmt);
0238     vsnprintf(buf, sizeof buf, fmt, args);
0239     va_end(args);
0240 
0241     if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
0242         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
0243                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
0244         return -1;
0245     }
0246 
0247     return 0;
0248 }
0249 EXPORT_SYMBOL(iscsi_change_param_sprintf);
0250 
0251 /*
0252  *  This is the leading connection of a new session,
0253  *  or session reinstatement.
0254  */
0255 static int iscsi_login_zero_tsih_s1(
0256     struct iscsit_conn *conn,
0257     unsigned char *buf)
0258 {
0259     struct iscsit_session *sess = NULL;
0260     struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
0261     int ret;
0262 
0263     sess = kzalloc(sizeof(struct iscsit_session), GFP_KERNEL);
0264     if (!sess) {
0265         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
0266                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
0267         pr_err("Could not allocate memory for session\n");
0268         return -ENOMEM;
0269     }
0270 
0271     if (iscsi_login_set_conn_values(sess, conn, pdu->cid))
0272         goto free_sess;
0273 
0274     sess->init_task_tag = pdu->itt;
0275     memcpy(&sess->isid, pdu->isid, 6);
0276     sess->exp_cmd_sn    = be32_to_cpu(pdu->cmdsn);
0277     INIT_LIST_HEAD(&sess->sess_conn_list);
0278     INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
0279     INIT_LIST_HEAD(&sess->cr_active_list);
0280     INIT_LIST_HEAD(&sess->cr_inactive_list);
0281     init_completion(&sess->async_msg_comp);
0282     init_completion(&sess->reinstatement_comp);
0283     init_completion(&sess->session_wait_comp);
0284     init_completion(&sess->session_waiting_on_uc_comp);
0285     mutex_init(&sess->cmdsn_mutex);
0286     spin_lock_init(&sess->conn_lock);
0287     spin_lock_init(&sess->cr_a_lock);
0288     spin_lock_init(&sess->cr_i_lock);
0289     spin_lock_init(&sess->session_usage_lock);
0290     spin_lock_init(&sess->ttt_lock);
0291 
0292     timer_setup(&sess->time2retain_timer,
0293             iscsit_handle_time2retain_timeout, 0);
0294 
0295     ret = ida_alloc(&sess_ida, GFP_KERNEL);
0296     if (ret < 0) {
0297         pr_err("Session ID allocation failed %d\n", ret);
0298         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
0299                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
0300         goto free_sess;
0301     }
0302 
0303     sess->session_index = ret;
0304     sess->creation_time = get_jiffies_64();
0305     /*
0306      * The FFP CmdSN window values will be allocated from the TPG's
0307      * Initiator Node's ACL once the login has been successfully completed.
0308      */
0309     atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
0310 
0311     sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
0312     if (!sess->sess_ops) {
0313         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
0314                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
0315         pr_err("Unable to allocate memory for"
0316                 " struct iscsi_sess_ops.\n");
0317         goto free_id;
0318     }
0319 
0320     sess->se_sess = transport_alloc_session(TARGET_PROT_NORMAL);
0321     if (IS_ERR(sess->se_sess)) {
0322         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
0323                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
0324         goto free_ops;
0325     }
0326 
0327     return 0;
0328 
0329 free_ops:
0330     kfree(sess->sess_ops);
0331 free_id:
0332     ida_free(&sess_ida, sess->session_index);
0333 free_sess:
0334     kfree(sess);
0335     conn->sess = NULL;
0336     return -ENOMEM;
0337 }
0338 
0339 static int iscsi_login_zero_tsih_s2(
0340     struct iscsit_conn *conn)
0341 {
0342     struct iscsi_node_attrib *na;
0343     struct iscsit_session *sess = conn->sess;
0344     struct iscsi_param *param;
0345     bool iser = false;
0346 
0347     sess->tpg = conn->tpg;
0348 
0349     /*
0350      * Assign a new TPG Session Handle.  Note this is protected with
0351      * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
0352      */
0353     sess->tsih = ++sess->tpg->ntsih;
0354     if (!sess->tsih)
0355         sess->tsih = ++sess->tpg->ntsih;
0356 
0357     /*
0358      * Create the default params from user defined values..
0359      */
0360     if (iscsi_copy_param_list(&conn->param_list,
0361                 conn->tpg->param_list, 1) < 0) {
0362         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
0363                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
0364         return -1;
0365     }
0366 
0367     if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
0368         iser = true;
0369 
0370     iscsi_set_keys_to_negotiate(conn->param_list, iser);
0371 
0372     if (sess->sess_ops->SessionType)
0373         return iscsi_set_keys_irrelevant_for_discovery(
0374                 conn->param_list);
0375 
0376     na = iscsit_tpg_get_node_attrib(sess);
0377 
0378     /*
0379      * If ACL allows non-authorized access in TPG with CHAP,
0380      * then set None to AuthMethod.
0381      */
0382     param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
0383     if (param && !strstr(param->value, NONE)) {
0384         if (!iscsi_conn_auth_required(conn))
0385             if (iscsi_change_param_sprintf(conn, "AuthMethod=%s",
0386                                NONE))
0387                 return -1;
0388     }
0389 
0390     /*
0391      * Need to send TargetPortalGroupTag back in first login response
0392      * on any iSCSI connection where the Initiator provides TargetName.
0393      * See 5.3.1.  Login Phase Start
0394      *
0395      * In our case, we have already located the struct iscsi_tiqn at this point.
0396      */
0397     if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
0398         return -1;
0399 
0400     /*
0401      * Workaround for Initiators that have broken connection recovery logic.
0402      *
0403      * "We would really like to get rid of this." Linux-iSCSI.org team
0404      */
0405     if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
0406         return -1;
0407 
0408     /*
0409      * Set RDMAExtensions=Yes by default for iSER enabled network portals
0410      */
0411     if (iser) {
0412         struct iscsi_param *param;
0413         unsigned long mrdsl, off;
0414         int rc;
0415 
0416         if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
0417             return -1;
0418 
0419         /*
0420          * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
0421          * Immediate Data + Unsolicited Data-OUT if necessary..
0422          */
0423         param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
0424                           conn->param_list);
0425         if (!param) {
0426             iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
0427                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
0428             return -1;
0429         }
0430         rc = kstrtoul(param->value, 0, &mrdsl);
0431         if (rc < 0) {
0432             iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
0433                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
0434             return -1;
0435         }
0436         off = mrdsl % PAGE_SIZE;
0437         if (!off)
0438             goto check_prot;
0439 
0440         if (mrdsl < PAGE_SIZE)
0441             mrdsl = PAGE_SIZE;
0442         else
0443             mrdsl -= off;
0444 
0445         pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
0446             " to PAGE_SIZE\n", mrdsl);
0447 
0448         if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
0449             return -1;
0450         /*
0451          * ISER currently requires that ImmediateData + Unsolicited
0452          * Data be disabled when protection / signature MRs are enabled.
0453          */
0454 check_prot:
0455         if (sess->se_sess->sup_prot_ops &
0456            (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
0457             TARGET_PROT_DOUT_INSERT)) {
0458 
0459             if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
0460                 return -1;
0461 
0462             if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
0463                 return -1;
0464 
0465             pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
0466                  " T10-PI enabled ISER session\n");
0467         }
0468     }
0469 
0470     return 0;
0471 }
0472 
0473 static int iscsi_login_non_zero_tsih_s1(
0474     struct iscsit_conn *conn,
0475     unsigned char *buf)
0476 {
0477     struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
0478 
0479     return iscsi_login_set_conn_values(NULL, conn, pdu->cid);
0480 }
0481 
0482 /*
0483  *  Add a new connection to an existing session.
0484  */
0485 static int iscsi_login_non_zero_tsih_s2(
0486     struct iscsit_conn *conn,
0487     unsigned char *buf)
0488 {
0489     struct iscsi_portal_group *tpg = conn->tpg;
0490     struct iscsit_session *sess = NULL, *sess_p = NULL;
0491     struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
0492     struct se_session *se_sess, *se_sess_tmp;
0493     struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
0494     bool iser = false;
0495 
0496     spin_lock_bh(&se_tpg->session_lock);
0497     list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
0498             sess_list) {
0499 
0500         sess_p = (struct iscsit_session *)se_sess->fabric_sess_ptr;
0501         if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
0502             atomic_read(&sess_p->session_logout) ||
0503             atomic_read(&sess_p->session_close) ||
0504            (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
0505             continue;
0506         if (!memcmp(sess_p->isid, pdu->isid, 6) &&
0507              (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
0508             iscsit_inc_session_usage_count(sess_p);
0509             iscsit_stop_time2retain_timer(sess_p);
0510             sess = sess_p;
0511             break;
0512         }
0513     }
0514     spin_unlock_bh(&se_tpg->session_lock);
0515 
0516     /*
0517      * If the Time2Retain handler has expired, the session is already gone.
0518      */
0519     if (!sess) {
0520         pr_err("Initiator attempting to add a connection to"
0521             " a non-existent session, rejecting iSCSI Login.\n");
0522         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
0523                 ISCSI_LOGIN_STATUS_NO_SESSION);
0524         return -1;
0525     }
0526 
0527     /*
0528      * Stop the Time2Retain timer if this is a failed session, we restart
0529      * the timer if the login is not successful.
0530      */
0531     spin_lock_bh(&sess->conn_lock);
0532     if (sess->session_state == TARG_SESS_STATE_FAILED)
0533         atomic_set(&sess->session_continuation, 1);
0534     spin_unlock_bh(&sess->conn_lock);
0535 
0536     if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 ||
0537         iscsi_copy_param_list(&conn->param_list,
0538             conn->tpg->param_list, 0) < 0) {
0539         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
0540                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
0541         return -1;
0542     }
0543 
0544     if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
0545         iser = true;
0546 
0547     iscsi_set_keys_to_negotiate(conn->param_list, iser);
0548     /*
0549      * Need to send TargetPortalGroupTag back in first login response
0550      * on any iSCSI connection where the Initiator provides TargetName.
0551      * See 5.3.1.  Login Phase Start
0552      *
0553      * In our case, we have already located the struct iscsi_tiqn at this point.
0554      */
0555     if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
0556         return -1;
0557 
0558     return 0;
0559 }
0560 
0561 int iscsi_login_post_auth_non_zero_tsih(
0562     struct iscsit_conn *conn,
0563     u16 cid,
0564     u32 exp_statsn)
0565 {
0566     struct iscsit_conn *conn_ptr = NULL;
0567     struct iscsi_conn_recovery *cr = NULL;
0568     struct iscsit_session *sess = conn->sess;
0569 
0570     /*
0571      * By following item 5 in the login table,  if we have found
0572      * an existing ISID and a valid/existing TSIH and an existing
0573      * CID we do connection reinstatement.  Currently we dont not
0574      * support it so we send back an non-zero status class to the
0575      * initiator and release the new connection.
0576      */
0577     conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
0578     if (conn_ptr) {
0579         pr_err("Connection exists with CID %hu for %s,"
0580             " performing connection reinstatement.\n",
0581             conn_ptr->cid, sess->sess_ops->InitiatorName);
0582 
0583         iscsit_connection_reinstatement_rcfr(conn_ptr);
0584         iscsit_dec_conn_usage_count(conn_ptr);
0585     }
0586 
0587     /*
0588      * Check for any connection recovery entries containing CID.
0589      * We use the original ExpStatSN sent in the first login request
0590      * to acknowledge commands for the failed connection.
0591      *
0592      * Also note that an explict logout may have already been sent,
0593      * but the response may not be sent due to additional connection
0594      * loss.
0595      */
0596     if (sess->sess_ops->ErrorRecoveryLevel == 2) {
0597         cr = iscsit_get_inactive_connection_recovery_entry(
0598                 sess, cid);
0599         if (cr) {
0600             pr_debug("Performing implicit logout"
0601                 " for connection recovery on CID: %hu\n",
0602                     conn->cid);
0603             iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
0604         }
0605     }
0606 
0607     /*
0608      * Else we follow item 4 from the login table in that we have
0609      * found an existing ISID and a valid/existing TSIH and a new
0610      * CID we go ahead and continue to add a new connection to the
0611      * session.
0612      */
0613     pr_debug("Adding CID %hu to existing session for %s.\n",
0614             cid, sess->sess_ops->InitiatorName);
0615 
0616     if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
0617         pr_err("Adding additional connection to this session"
0618             " would exceed MaxConnections %d, login failed.\n",
0619                 sess->sess_ops->MaxConnections);
0620         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
0621                 ISCSI_LOGIN_STATUS_ISID_ERROR);
0622         return -1;
0623     }
0624 
0625     return 0;
0626 }
0627 
0628 static void iscsi_post_login_start_timers(struct iscsit_conn *conn)
0629 {
0630     struct iscsit_session *sess = conn->sess;
0631     /*
0632      * FIXME: Unsolicited NopIN support for ISER
0633      */
0634     if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
0635         return;
0636 
0637     if (!sess->sess_ops->SessionType)
0638         iscsit_start_nopin_timer(conn);
0639 }
0640 
0641 int iscsit_start_kthreads(struct iscsit_conn *conn)
0642 {
0643     int ret = 0;
0644 
0645     spin_lock(&iscsit_global->ts_bitmap_lock);
0646     conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
0647                     ISCSIT_BITMAP_BITS, get_order(1));
0648     spin_unlock(&iscsit_global->ts_bitmap_lock);
0649 
0650     if (conn->bitmap_id < 0) {
0651         pr_err("bitmap_find_free_region() failed for"
0652                " iscsit_start_kthreads()\n");
0653         return -ENOMEM;
0654     }
0655 
0656     conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
0657                       "%s", ISCSI_TX_THREAD_NAME);
0658     if (IS_ERR(conn->tx_thread)) {
0659         pr_err("Unable to start iscsi_target_tx_thread\n");
0660         ret = PTR_ERR(conn->tx_thread);
0661         goto out_bitmap;
0662     }
0663     conn->tx_thread_active = true;
0664 
0665     conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
0666                       "%s", ISCSI_RX_THREAD_NAME);
0667     if (IS_ERR(conn->rx_thread)) {
0668         pr_err("Unable to start iscsi_target_rx_thread\n");
0669         ret = PTR_ERR(conn->rx_thread);
0670         goto out_tx;
0671     }
0672     conn->rx_thread_active = true;
0673 
0674     return 0;
0675 out_tx:
0676     send_sig(SIGINT, conn->tx_thread, 1);
0677     kthread_stop(conn->tx_thread);
0678     conn->tx_thread_active = false;
0679 out_bitmap:
0680     spin_lock(&iscsit_global->ts_bitmap_lock);
0681     bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
0682                   get_order(1));
0683     spin_unlock(&iscsit_global->ts_bitmap_lock);
0684     return ret;
0685 }
0686 
0687 void iscsi_post_login_handler(
0688     struct iscsi_np *np,
0689     struct iscsit_conn *conn,
0690     u8 zero_tsih)
0691 {
0692     int stop_timer = 0;
0693     struct iscsit_session *sess = conn->sess;
0694     struct se_session *se_sess = sess->se_sess;
0695     struct iscsi_portal_group *tpg = sess->tpg;
0696     struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
0697 
0698     iscsit_inc_conn_usage_count(conn);
0699 
0700     iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
0701             ISCSI_LOGIN_STATUS_ACCEPT);
0702 
0703     pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
0704     conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
0705 
0706     iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
0707     /*
0708      * SCSI Initiator -> SCSI Target Port Mapping
0709      */
0710     if (!zero_tsih) {
0711         iscsi_set_session_parameters(sess->sess_ops,
0712                 conn->param_list, 0);
0713         iscsi_release_param_list(conn->param_list);
0714         conn->param_list = NULL;
0715 
0716         spin_lock_bh(&sess->conn_lock);
0717         atomic_set(&sess->session_continuation, 0);
0718         if (sess->session_state == TARG_SESS_STATE_FAILED) {
0719             pr_debug("Moving to"
0720                     " TARG_SESS_STATE_LOGGED_IN.\n");
0721             sess->session_state = TARG_SESS_STATE_LOGGED_IN;
0722             stop_timer = 1;
0723         }
0724 
0725         pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
0726             " %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
0727             &conn->local_sockaddr, tpg->tpgt);
0728 
0729         list_add_tail(&conn->conn_list, &sess->sess_conn_list);
0730         atomic_inc(&sess->nconn);
0731         pr_debug("Incremented iSCSI Connection count to %d"
0732             " from node: %s\n", atomic_read(&sess->nconn),
0733             sess->sess_ops->InitiatorName);
0734         spin_unlock_bh(&sess->conn_lock);
0735 
0736         iscsi_post_login_start_timers(conn);
0737         /*
0738          * Determine CPU mask to ensure connection's RX and TX kthreads
0739          * are scheduled on the same CPU.
0740          */
0741         iscsit_thread_get_cpumask(conn);
0742         conn->conn_rx_reset_cpumask = 1;
0743         conn->conn_tx_reset_cpumask = 1;
0744         /*
0745          * Wakeup the sleeping iscsi_target_rx_thread() now that
0746          * iscsit_conn is in TARG_CONN_STATE_LOGGED_IN state.
0747          */
0748         complete(&conn->rx_login_comp);
0749         iscsit_dec_conn_usage_count(conn);
0750 
0751         if (stop_timer) {
0752             spin_lock_bh(&se_tpg->session_lock);
0753             iscsit_stop_time2retain_timer(sess);
0754             spin_unlock_bh(&se_tpg->session_lock);
0755         }
0756         iscsit_dec_session_usage_count(sess);
0757         return;
0758     }
0759 
0760     iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
0761     iscsi_release_param_list(conn->param_list);
0762     conn->param_list = NULL;
0763 
0764     iscsit_determine_maxcmdsn(sess);
0765 
0766     spin_lock_bh(&se_tpg->session_lock);
0767     __transport_register_session(&sess->tpg->tpg_se_tpg,
0768             se_sess->se_node_acl, se_sess, sess);
0769     pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
0770     sess->session_state = TARG_SESS_STATE_LOGGED_IN;
0771 
0772     pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
0773         conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
0774         tpg->tpgt);
0775 
0776     spin_lock_bh(&sess->conn_lock);
0777     list_add_tail(&conn->conn_list, &sess->sess_conn_list);
0778     atomic_inc(&sess->nconn);
0779     pr_debug("Incremented iSCSI Connection count to %d from node:"
0780         " %s\n", atomic_read(&sess->nconn),
0781         sess->sess_ops->InitiatorName);
0782     spin_unlock_bh(&sess->conn_lock);
0783 
0784     sess->sid = tpg->sid++;
0785     if (!sess->sid)
0786         sess->sid = tpg->sid++;
0787     pr_debug("Established iSCSI session from node: %s\n",
0788             sess->sess_ops->InitiatorName);
0789 
0790     tpg->nsessions++;
0791     if (tpg->tpg_tiqn)
0792         tpg->tpg_tiqn->tiqn_nsessions++;
0793 
0794     pr_debug("Incremented number of active iSCSI sessions to %u on"
0795         " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
0796     spin_unlock_bh(&se_tpg->session_lock);
0797 
0798     iscsi_post_login_start_timers(conn);
0799     /*
0800      * Determine CPU mask to ensure connection's RX and TX kthreads
0801      * are scheduled on the same CPU.
0802      */
0803     iscsit_thread_get_cpumask(conn);
0804     conn->conn_rx_reset_cpumask = 1;
0805     conn->conn_tx_reset_cpumask = 1;
0806     /*
0807      * Wakeup the sleeping iscsi_target_rx_thread() now that
0808      * iscsit_conn is in TARG_CONN_STATE_LOGGED_IN state.
0809      */
0810     complete(&conn->rx_login_comp);
0811     iscsit_dec_conn_usage_count(conn);
0812 }
0813 
0814 void iscsi_handle_login_thread_timeout(struct timer_list *t)
0815 {
0816     struct iscsi_np *np = from_timer(np, t, np_login_timer);
0817 
0818     spin_lock_bh(&np->np_thread_lock);
0819     pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
0820             &np->np_sockaddr);
0821 
0822     if (np->np_login_timer_flags & ISCSI_TF_STOP) {
0823         spin_unlock_bh(&np->np_thread_lock);
0824         return;
0825     }
0826 
0827     if (np->np_thread)
0828         send_sig(SIGINT, np->np_thread, 1);
0829 
0830     np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
0831     spin_unlock_bh(&np->np_thread_lock);
0832 }
0833 
0834 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
0835 {
0836     /*
0837      * This used the TA_LOGIN_TIMEOUT constant because at this
0838      * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
0839      */
0840     spin_lock_bh(&np->np_thread_lock);
0841     np->np_login_timer_flags &= ~ISCSI_TF_STOP;
0842     np->np_login_timer_flags |= ISCSI_TF_RUNNING;
0843     mod_timer(&np->np_login_timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
0844 
0845     pr_debug("Added timeout timer to iSCSI login request for"
0846             " %u seconds.\n", TA_LOGIN_TIMEOUT);
0847     spin_unlock_bh(&np->np_thread_lock);
0848 }
0849 
0850 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
0851 {
0852     spin_lock_bh(&np->np_thread_lock);
0853     if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
0854         spin_unlock_bh(&np->np_thread_lock);
0855         return;
0856     }
0857     np->np_login_timer_flags |= ISCSI_TF_STOP;
0858     spin_unlock_bh(&np->np_thread_lock);
0859 
0860     del_timer_sync(&np->np_login_timer);
0861 
0862     spin_lock_bh(&np->np_thread_lock);
0863     np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
0864     spin_unlock_bh(&np->np_thread_lock);
0865 }
0866 
0867 int iscsit_setup_np(
0868     struct iscsi_np *np,
0869     struct sockaddr_storage *sockaddr)
0870 {
0871     struct socket *sock = NULL;
0872     int backlog = ISCSIT_TCP_BACKLOG, ret, len;
0873 
0874     switch (np->np_network_transport) {
0875     case ISCSI_TCP:
0876         np->np_ip_proto = IPPROTO_TCP;
0877         np->np_sock_type = SOCK_STREAM;
0878         break;
0879     case ISCSI_SCTP_TCP:
0880         np->np_ip_proto = IPPROTO_SCTP;
0881         np->np_sock_type = SOCK_STREAM;
0882         break;
0883     case ISCSI_SCTP_UDP:
0884         np->np_ip_proto = IPPROTO_SCTP;
0885         np->np_sock_type = SOCK_SEQPACKET;
0886         break;
0887     default:
0888         pr_err("Unsupported network_transport: %d\n",
0889                 np->np_network_transport);
0890         return -EINVAL;
0891     }
0892 
0893     ret = sock_create(sockaddr->ss_family, np->np_sock_type,
0894             np->np_ip_proto, &sock);
0895     if (ret < 0) {
0896         pr_err("sock_create() failed.\n");
0897         return ret;
0898     }
0899     np->np_socket = sock;
0900     /*
0901      * Setup the np->np_sockaddr from the passed sockaddr setup
0902      * in iscsi_target_configfs.c code..
0903      */
0904     memcpy(&np->np_sockaddr, sockaddr,
0905             sizeof(struct sockaddr_storage));
0906 
0907     if (sockaddr->ss_family == AF_INET6)
0908         len = sizeof(struct sockaddr_in6);
0909     else
0910         len = sizeof(struct sockaddr_in);
0911     /*
0912      * Set SO_REUSEADDR, and disable Nagle Algorithm with TCP_NODELAY.
0913      */
0914     if (np->np_network_transport == ISCSI_TCP)
0915         tcp_sock_set_nodelay(sock->sk);
0916     sock_set_reuseaddr(sock->sk);
0917     ip_sock_set_freebind(sock->sk);
0918 
0919     ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
0920     if (ret < 0) {
0921         pr_err("kernel_bind() failed: %d\n", ret);
0922         goto fail;
0923     }
0924 
0925     ret = kernel_listen(sock, backlog);
0926     if (ret != 0) {
0927         pr_err("kernel_listen() failed: %d\n", ret);
0928         goto fail;
0929     }
0930 
0931     return 0;
0932 fail:
0933     np->np_socket = NULL;
0934     sock_release(sock);
0935     return ret;
0936 }
0937 
0938 int iscsi_target_setup_login_socket(
0939     struct iscsi_np *np,
0940     struct sockaddr_storage *sockaddr)
0941 {
0942     struct iscsit_transport *t;
0943     int rc;
0944 
0945     t = iscsit_get_transport(np->np_network_transport);
0946     if (!t)
0947         return -EINVAL;
0948 
0949     rc = t->iscsit_setup_np(np, sockaddr);
0950     if (rc < 0) {
0951         iscsit_put_transport(t);
0952         return rc;
0953     }
0954 
0955     np->np_transport = t;
0956     np->enabled = true;
0957     return 0;
0958 }
0959 
0960 int iscsit_accept_np(struct iscsi_np *np, struct iscsit_conn *conn)
0961 {
0962     struct socket *new_sock, *sock = np->np_socket;
0963     struct sockaddr_in sock_in;
0964     struct sockaddr_in6 sock_in6;
0965     int rc;
0966 
0967     rc = kernel_accept(sock, &new_sock, 0);
0968     if (rc < 0)
0969         return rc;
0970 
0971     conn->sock = new_sock;
0972     conn->login_family = np->np_sockaddr.ss_family;
0973 
0974     if (np->np_sockaddr.ss_family == AF_INET6) {
0975         memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
0976 
0977         rc = conn->sock->ops->getname(conn->sock,
0978                 (struct sockaddr *)&sock_in6, 1);
0979         if (rc >= 0) {
0980             if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
0981                 memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
0982             } else {
0983                 /* Pretend to be an ipv4 socket */
0984                 sock_in.sin_family = AF_INET;
0985                 sock_in.sin_port = sock_in6.sin6_port;
0986                 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
0987                 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
0988             }
0989         }
0990 
0991         rc = conn->sock->ops->getname(conn->sock,
0992                 (struct sockaddr *)&sock_in6, 0);
0993         if (rc >= 0) {
0994             if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
0995                 memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
0996             } else {
0997                 /* Pretend to be an ipv4 socket */
0998                 sock_in.sin_family = AF_INET;
0999                 sock_in.sin_port = sock_in6.sin6_port;
1000                 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1001                 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1002             }
1003         }
1004     } else {
1005         memset(&sock_in, 0, sizeof(struct sockaddr_in));
1006 
1007         rc = conn->sock->ops->getname(conn->sock,
1008                 (struct sockaddr *)&sock_in, 1);
1009         if (rc >= 0)
1010             memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1011 
1012         rc = conn->sock->ops->getname(conn->sock,
1013                 (struct sockaddr *)&sock_in, 0);
1014         if (rc >= 0)
1015             memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1016     }
1017 
1018     return 0;
1019 }
1020 
1021 int iscsit_get_login_rx(struct iscsit_conn *conn, struct iscsi_login *login)
1022 {
1023     struct iscsi_login_req *login_req;
1024     u32 padding = 0, payload_length;
1025 
1026     if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1027         return -1;
1028 
1029     login_req = (struct iscsi_login_req *)login->req;
1030     payload_length  = ntoh24(login_req->dlength);
1031     padding = ((-payload_length) & 3);
1032 
1033     pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1034         " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1035         login_req->flags, login_req->itt, login_req->cmdsn,
1036         login_req->exp_statsn, login_req->cid, payload_length);
1037     /*
1038      * Setup the initial iscsi_login values from the leading
1039      * login request PDU.
1040      */
1041     if (login->first_request) {
1042         login_req = (struct iscsi_login_req *)login->req;
1043         login->leading_connection = (!login_req->tsih) ? 1 : 0;
1044         login->current_stage    = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1045         login->version_min  = login_req->min_version;
1046         login->version_max  = login_req->max_version;
1047         memcpy(login->isid, login_req->isid, 6);
1048         login->cmd_sn       = be32_to_cpu(login_req->cmdsn);
1049         login->init_task_tag    = login_req->itt;
1050         login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1051         login->cid      = be16_to_cpu(login_req->cid);
1052         login->tsih     = be16_to_cpu(login_req->tsih);
1053     }
1054 
1055     if (iscsi_target_check_login_request(conn, login) < 0)
1056         return -1;
1057 
1058     memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1059     if (iscsi_login_rx_data(conn, login->req_buf,
1060                 payload_length + padding) < 0)
1061         return -1;
1062 
1063     return 0;
1064 }
1065 
1066 int iscsit_put_login_tx(struct iscsit_conn *conn, struct iscsi_login *login,
1067             u32 length)
1068 {
1069     if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1070         return -1;
1071 
1072     return 0;
1073 }
1074 
1075 static int
1076 iscsit_conn_set_transport(struct iscsit_conn *conn, struct iscsit_transport *t)
1077 {
1078     int rc;
1079 
1080     if (!t->owner) {
1081         conn->conn_transport = t;
1082         return 0;
1083     }
1084 
1085     rc = try_module_get(t->owner);
1086     if (!rc) {
1087         pr_err("try_module_get() failed for %s\n", t->name);
1088         return -EINVAL;
1089     }
1090 
1091     conn->conn_transport = t;
1092     return 0;
1093 }
1094 
1095 static struct iscsit_conn *iscsit_alloc_conn(struct iscsi_np *np)
1096 {
1097     struct iscsit_conn *conn;
1098 
1099     conn = kzalloc(sizeof(struct iscsit_conn), GFP_KERNEL);
1100     if (!conn) {
1101         pr_err("Could not allocate memory for new connection\n");
1102         return NULL;
1103     }
1104     pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1105     conn->conn_state = TARG_CONN_STATE_FREE;
1106 
1107     init_waitqueue_head(&conn->queues_wq);
1108     INIT_LIST_HEAD(&conn->conn_list);
1109     INIT_LIST_HEAD(&conn->conn_cmd_list);
1110     INIT_LIST_HEAD(&conn->immed_queue_list);
1111     INIT_LIST_HEAD(&conn->response_queue_list);
1112     init_completion(&conn->conn_post_wait_comp);
1113     init_completion(&conn->conn_wait_comp);
1114     init_completion(&conn->conn_wait_rcfr_comp);
1115     init_completion(&conn->conn_waiting_on_uc_comp);
1116     init_completion(&conn->conn_logout_comp);
1117     init_completion(&conn->rx_half_close_comp);
1118     init_completion(&conn->tx_half_close_comp);
1119     init_completion(&conn->rx_login_comp);
1120     spin_lock_init(&conn->cmd_lock);
1121     spin_lock_init(&conn->conn_usage_lock);
1122     spin_lock_init(&conn->immed_queue_lock);
1123     spin_lock_init(&conn->nopin_timer_lock);
1124     spin_lock_init(&conn->response_queue_lock);
1125     spin_lock_init(&conn->state_lock);
1126 
1127     timer_setup(&conn->nopin_response_timer,
1128             iscsit_handle_nopin_response_timeout, 0);
1129     timer_setup(&conn->nopin_timer, iscsit_handle_nopin_timeout, 0);
1130 
1131     if (iscsit_conn_set_transport(conn, np->np_transport) < 0)
1132         goto free_conn;
1133 
1134     conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
1135     if (!conn->conn_ops) {
1136         pr_err("Unable to allocate memory for struct iscsi_conn_ops.\n");
1137         goto put_transport;
1138     }
1139 
1140     if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
1141         pr_err("Unable to allocate conn->conn_cpumask\n");
1142         goto free_conn_ops;
1143     }
1144 
1145     if (!zalloc_cpumask_var(&conn->allowed_cpumask, GFP_KERNEL)) {
1146         pr_err("Unable to allocate conn->allowed_cpumask\n");
1147         goto free_conn_cpumask;
1148     }
1149 
1150     return conn;
1151 
1152 free_conn_cpumask:
1153     free_cpumask_var(conn->conn_cpumask);
1154 free_conn_ops:
1155     kfree(conn->conn_ops);
1156 put_transport:
1157     iscsit_put_transport(conn->conn_transport);
1158 free_conn:
1159     kfree(conn);
1160     return NULL;
1161 }
1162 
1163 void iscsit_free_conn(struct iscsit_conn *conn)
1164 {
1165     free_cpumask_var(conn->allowed_cpumask);
1166     free_cpumask_var(conn->conn_cpumask);
1167     kfree(conn->conn_ops);
1168     iscsit_put_transport(conn->conn_transport);
1169     kfree(conn);
1170 }
1171 
1172 void iscsi_target_login_sess_out(struct iscsit_conn *conn,
1173                  bool zero_tsih, bool new_sess)
1174 {
1175     if (!new_sess)
1176         goto old_sess_out;
1177 
1178     pr_err("iSCSI Login negotiation failed.\n");
1179     iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1180                    ISCSI_LOGIN_STATUS_INIT_ERR);
1181     if (!zero_tsih || !conn->sess)
1182         goto old_sess_out;
1183 
1184     transport_free_session(conn->sess->se_sess);
1185     ida_free(&sess_ida, conn->sess->session_index);
1186     kfree(conn->sess->sess_ops);
1187     kfree(conn->sess);
1188     conn->sess = NULL;
1189 
1190 old_sess_out:
1191     /*
1192      * If login negotiation fails check if the Time2Retain timer
1193      * needs to be restarted.
1194      */
1195     if (!zero_tsih && conn->sess) {
1196         spin_lock_bh(&conn->sess->conn_lock);
1197         if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1198             struct se_portal_group *se_tpg =
1199                     &conn->tpg->tpg_se_tpg;
1200 
1201             atomic_set(&conn->sess->session_continuation, 0);
1202             spin_unlock_bh(&conn->sess->conn_lock);
1203             spin_lock_bh(&se_tpg->session_lock);
1204             iscsit_start_time2retain_handler(conn->sess);
1205             spin_unlock_bh(&se_tpg->session_lock);
1206         } else
1207             spin_unlock_bh(&conn->sess->conn_lock);
1208         iscsit_dec_session_usage_count(conn->sess);
1209     }
1210 
1211     ahash_request_free(conn->conn_tx_hash);
1212     if (conn->conn_rx_hash) {
1213         struct crypto_ahash *tfm;
1214 
1215         tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
1216         ahash_request_free(conn->conn_rx_hash);
1217         crypto_free_ahash(tfm);
1218     }
1219 
1220     if (conn->param_list) {
1221         iscsi_release_param_list(conn->param_list);
1222         conn->param_list = NULL;
1223     }
1224     iscsi_target_nego_release(conn);
1225 
1226     if (conn->sock) {
1227         sock_release(conn->sock);
1228         conn->sock = NULL;
1229     }
1230 
1231     if (conn->conn_transport->iscsit_wait_conn)
1232         conn->conn_transport->iscsit_wait_conn(conn);
1233 
1234     if (conn->conn_transport->iscsit_free_conn)
1235         conn->conn_transport->iscsit_free_conn(conn);
1236 
1237     iscsit_free_conn(conn);
1238 }
1239 
1240 static int __iscsi_target_login_thread(struct iscsi_np *np)
1241 {
1242     u8 *buffer, zero_tsih = 0;
1243     int ret = 0, rc;
1244     struct iscsit_conn *conn = NULL;
1245     struct iscsi_login *login;
1246     struct iscsi_portal_group *tpg = NULL;
1247     struct iscsi_login_req *pdu;
1248     struct iscsi_tpg_np *tpg_np;
1249     bool new_sess = false;
1250 
1251     flush_signals(current);
1252 
1253     spin_lock_bh(&np->np_thread_lock);
1254     if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1255         np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1256         spin_unlock_bh(&np->np_thread_lock);
1257         complete(&np->np_restart_comp);
1258         return 1;
1259     } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1260         spin_unlock_bh(&np->np_thread_lock);
1261         goto exit;
1262     } else {
1263         np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1264     }
1265     spin_unlock_bh(&np->np_thread_lock);
1266 
1267     conn = iscsit_alloc_conn(np);
1268     if (!conn) {
1269         /* Get another socket */
1270         return 1;
1271     }
1272 
1273     rc = np->np_transport->iscsit_accept_np(np, conn);
1274     if (rc == -ENOSYS) {
1275         complete(&np->np_restart_comp);
1276         iscsit_free_conn(conn);
1277         goto exit;
1278     } else if (rc < 0) {
1279         spin_lock_bh(&np->np_thread_lock);
1280         if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1281             np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1282             spin_unlock_bh(&np->np_thread_lock);
1283             complete(&np->np_restart_comp);
1284             iscsit_free_conn(conn);
1285             /* Get another socket */
1286             return 1;
1287         }
1288         spin_unlock_bh(&np->np_thread_lock);
1289         iscsit_free_conn(conn);
1290         return 1;
1291     }
1292     /*
1293      * Perform the remaining iSCSI connection initialization items..
1294      */
1295     login = iscsi_login_init_conn(conn);
1296     if (!login) {
1297         goto new_sess_out;
1298     }
1299 
1300     iscsi_start_login_thread_timer(np);
1301 
1302     pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1303     conn->conn_state = TARG_CONN_STATE_XPT_UP;
1304     /*
1305      * This will process the first login request + payload..
1306      */
1307     rc = np->np_transport->iscsit_get_login_rx(conn, login);
1308     if (rc == 1)
1309         return 1;
1310     else if (rc < 0)
1311         goto new_sess_out;
1312 
1313     buffer = &login->req[0];
1314     pdu = (struct iscsi_login_req *)buffer;
1315     /*
1316      * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1317      * when Status-Class != 0.
1318     */
1319     conn->login_itt = pdu->itt;
1320 
1321     spin_lock_bh(&np->np_thread_lock);
1322     if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1323         spin_unlock_bh(&np->np_thread_lock);
1324         pr_err("iSCSI Network Portal on %pISpc currently not"
1325             " active.\n", &np->np_sockaddr);
1326         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1327                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1328         goto new_sess_out;
1329     }
1330     spin_unlock_bh(&np->np_thread_lock);
1331 
1332     conn->network_transport = np->np_network_transport;
1333 
1334     pr_debug("Received iSCSI login request from %pISpc on %s Network"
1335         " Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
1336         &conn->local_sockaddr);
1337 
1338     pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1339     conn->conn_state    = TARG_CONN_STATE_IN_LOGIN;
1340 
1341     if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1342             pdu->min_version) < 0)
1343         goto new_sess_out;
1344 
1345     zero_tsih = (pdu->tsih == 0x0000);
1346     if (zero_tsih) {
1347         /*
1348          * This is the leading connection of a new session.
1349          * We wait until after authentication to check for
1350          * session reinstatement.
1351          */
1352         if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1353             goto new_sess_out;
1354     } else {
1355         /*
1356          * Add a new connection to an existing session.
1357          * We check for a non-existant session in
1358          * iscsi_login_non_zero_tsih_s2() below based
1359          * on ISID/TSIH, but wait until after authentication
1360          * to check for connection reinstatement, etc.
1361          */
1362         if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1363             goto new_sess_out;
1364     }
1365     /*
1366      * SessionType: Discovery
1367      *
1368      *  Locates Default Portal
1369      *
1370      * SessionType: Normal
1371      *
1372      *  Locates Target Portal from NP -> Target IQN
1373      */
1374     rc = iscsi_target_locate_portal(np, conn, login);
1375     if (rc < 0) {
1376         tpg = conn->tpg;
1377         goto new_sess_out;
1378     }
1379     login->zero_tsih = zero_tsih;
1380 
1381     if (conn->sess)
1382         conn->sess->se_sess->sup_prot_ops =
1383             conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1384 
1385     tpg = conn->tpg;
1386     if (!tpg) {
1387         pr_err("Unable to locate struct iscsit_conn->tpg\n");
1388         goto new_sess_out;
1389     }
1390 
1391     if (zero_tsih) {
1392         if (iscsi_login_zero_tsih_s2(conn) < 0)
1393             goto new_sess_out;
1394     } else {
1395         if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1396             goto old_sess_out;
1397     }
1398 
1399     if (conn->conn_transport->iscsit_validate_params) {
1400         ret = conn->conn_transport->iscsit_validate_params(conn);
1401         if (ret < 0) {
1402             if (zero_tsih)
1403                 goto new_sess_out;
1404             else
1405                 goto old_sess_out;
1406         }
1407     }
1408 
1409     ret = iscsi_target_start_negotiation(login, conn);
1410     if (ret < 0)
1411         goto new_sess_out;
1412 
1413     iscsi_stop_login_thread_timer(np);
1414 
1415     if (ret == 1) {
1416         tpg_np = conn->tpg_np;
1417 
1418         iscsi_post_login_handler(np, conn, zero_tsih);
1419         iscsit_deaccess_np(np, tpg, tpg_np);
1420     }
1421 
1422     tpg = NULL;
1423     tpg_np = NULL;
1424     /* Get another socket */
1425     return 1;
1426 
1427 new_sess_out:
1428     new_sess = true;
1429 old_sess_out:
1430     iscsi_stop_login_thread_timer(np);
1431     tpg_np = conn->tpg_np;
1432     iscsi_target_login_sess_out(conn, zero_tsih, new_sess);
1433     new_sess = false;
1434 
1435     if (tpg) {
1436         iscsit_deaccess_np(np, tpg, tpg_np);
1437         tpg = NULL;
1438         tpg_np = NULL;
1439     }
1440 
1441     return 1;
1442 
1443 exit:
1444     iscsi_stop_login_thread_timer(np);
1445     spin_lock_bh(&np->np_thread_lock);
1446     np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1447     spin_unlock_bh(&np->np_thread_lock);
1448 
1449     return 0;
1450 }
1451 
1452 int iscsi_target_login_thread(void *arg)
1453 {
1454     struct iscsi_np *np = arg;
1455     int ret;
1456 
1457     allow_signal(SIGINT);
1458 
1459     while (1) {
1460         ret = __iscsi_target_login_thread(np);
1461         /*
1462          * We break and exit here unless another sock_accept() call
1463          * is expected.
1464          */
1465         if (ret != 1)
1466             break;
1467     }
1468 
1469     while (!kthread_should_stop()) {
1470         msleep(100);
1471     }
1472 
1473     return 0;
1474 }