linux/drivers/target/iscsi/iscsi_target_login.c
<<
>>
Prefs
   1/*******************************************************************************
   2 * This file contains the login functions used by the iSCSI Target driver.
   3 *
   4 * (c) Copyright 2007-2013 Datera, Inc.
   5 *
   6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
   7 *
   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.
  12 *
  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 ******************************************************************************/
  18
  19#include <linux/string.h>
  20#include <linux/kthread.h>
  21#include <linux/crypto.h>
  22#include <linux/idr.h>
  23#include <scsi/iscsi_proto.h>
  24#include <target/target_core_base.h>
  25#include <target/target_core_fabric.h>
  26
  27#include "iscsi_target_core.h"
  28#include "iscsi_target_tq.h"
  29#include "iscsi_target_device.h"
  30#include "iscsi_target_nego.h"
  31#include "iscsi_target_erl0.h"
  32#include "iscsi_target_erl2.h"
  33#include "iscsi_target_login.h"
  34#include "iscsi_target_stat.h"
  35#include "iscsi_target_tpg.h"
  36#include "iscsi_target_util.h"
  37#include "iscsi_target.h"
  38#include "iscsi_target_parameters.h"
  39
  40#include <target/iscsi/iscsi_transport.h>
  41
  42static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
  43{
  44        struct iscsi_login *login;
  45
  46        login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
  47        if (!login) {
  48                pr_err("Unable to allocate memory for struct iscsi_login.\n");
  49                return NULL;
  50        }
  51        conn->login = login;
  52        login->conn = conn;
  53        login->first_request = 1;
  54
  55        login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  56        if (!login->req_buf) {
  57                pr_err("Unable to allocate memory for response buffer.\n");
  58                goto out_login;
  59        }
  60
  61        login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
  62        if (!login->rsp_buf) {
  63                pr_err("Unable to allocate memory for request buffer.\n");
  64                goto out_req_buf;
  65        }
  66
  67        conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
  68        if (!conn->conn_ops) {
  69                pr_err("Unable to allocate memory for"
  70                        " struct iscsi_conn_ops.\n");
  71                goto out_rsp_buf;
  72        }
  73
  74        init_waitqueue_head(&conn->queues_wq);
  75        INIT_LIST_HEAD(&conn->conn_list);
  76        INIT_LIST_HEAD(&conn->conn_cmd_list);
  77        INIT_LIST_HEAD(&conn->immed_queue_list);
  78        INIT_LIST_HEAD(&conn->response_queue_list);
  79        init_completion(&conn->conn_post_wait_comp);
  80        init_completion(&conn->conn_wait_comp);
  81        init_completion(&conn->conn_wait_rcfr_comp);
  82        init_completion(&conn->conn_waiting_on_uc_comp);
  83        init_completion(&conn->conn_logout_comp);
  84        init_completion(&conn->rx_half_close_comp);
  85        init_completion(&conn->tx_half_close_comp);
  86        spin_lock_init(&conn->cmd_lock);
  87        spin_lock_init(&conn->conn_usage_lock);
  88        spin_lock_init(&conn->immed_queue_lock);
  89        spin_lock_init(&conn->nopin_timer_lock);
  90        spin_lock_init(&conn->response_queue_lock);
  91        spin_lock_init(&conn->state_lock);
  92
  93        if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
  94                pr_err("Unable to allocate conn->conn_cpumask\n");
  95                goto out_conn_ops;
  96        }
  97        conn->conn_login = login;
  98
  99        return login;
 100
 101out_conn_ops:
 102        kfree(conn->conn_ops);
 103out_rsp_buf:
 104        kfree(login->rsp_buf);
 105out_req_buf:
 106        kfree(login->req_buf);
 107out_login:
 108        kfree(login);
 109        return NULL;
 110}
 111
 112/*
 113 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
 114 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
 115 */
 116int iscsi_login_setup_crypto(struct iscsi_conn *conn)
 117{
 118        /*
 119         * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
 120         * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
 121         * to software 1x8 byte slicing from crc32c.ko
 122         */
 123        conn->conn_rx_hash.flags = 0;
 124        conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
 125                                                CRYPTO_ALG_ASYNC);
 126        if (IS_ERR(conn->conn_rx_hash.tfm)) {
 127                pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
 128                return -ENOMEM;
 129        }
 130
 131        conn->conn_tx_hash.flags = 0;
 132        conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
 133                                                CRYPTO_ALG_ASYNC);
 134        if (IS_ERR(conn->conn_tx_hash.tfm)) {
 135                pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
 136                crypto_free_hash(conn->conn_rx_hash.tfm);
 137                return -ENOMEM;
 138        }
 139
 140        return 0;
 141}
 142
 143static int iscsi_login_check_initiator_version(
 144        struct iscsi_conn *conn,
 145        u8 version_max,
 146        u8 version_min)
 147{
 148        if ((version_max != 0x00) || (version_min != 0x00)) {
 149                pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
 150                        " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
 151                        version_min, version_max);
 152                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 153                                ISCSI_LOGIN_STATUS_NO_VERSION);
 154                return -1;
 155        }
 156
 157        return 0;
 158}
 159
 160int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
 161{
 162        int sessiontype;
 163        struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
 164        struct iscsi_portal_group *tpg = conn->tpg;
 165        struct iscsi_session *sess = NULL, *sess_p = NULL;
 166        struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
 167        struct se_session *se_sess, *se_sess_tmp;
 168
 169        initiatorname_param = iscsi_find_param_from_key(
 170                        INITIATORNAME, conn->param_list);
 171        sessiontype_param = iscsi_find_param_from_key(
 172                        SESSIONTYPE, conn->param_list);
 173        if (!initiatorname_param || !sessiontype_param) {
 174                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 175                        ISCSI_LOGIN_STATUS_MISSING_FIELDS);
 176                return -1;
 177        }
 178
 179        sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
 180
 181        spin_lock_bh(&se_tpg->session_lock);
 182        list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
 183                        sess_list) {
 184
 185                sess_p = se_sess->fabric_sess_ptr;
 186                spin_lock(&sess_p->conn_lock);
 187                if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
 188                    atomic_read(&sess_p->session_logout) ||
 189                    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
 190                        spin_unlock(&sess_p->conn_lock);
 191                        continue;
 192                }
 193                if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
 194                   (!strcmp(sess_p->sess_ops->InitiatorName,
 195                            initiatorname_param->value) &&
 196                   (sess_p->sess_ops->SessionType == sessiontype))) {
 197                        atomic_set(&sess_p->session_reinstatement, 1);
 198                        spin_unlock(&sess_p->conn_lock);
 199                        iscsit_inc_session_usage_count(sess_p);
 200                        iscsit_stop_time2retain_timer(sess_p);
 201                        sess = sess_p;
 202                        break;
 203                }
 204                spin_unlock(&sess_p->conn_lock);
 205        }
 206        spin_unlock_bh(&se_tpg->session_lock);
 207        /*
 208         * If the Time2Retain handler has expired, the session is already gone.
 209         */
 210        if (!sess)
 211                return 0;
 212
 213        pr_debug("%s iSCSI Session SID %u is still active for %s,"
 214                " preforming session reinstatement.\n", (sessiontype) ?
 215                "Discovery" : "Normal", sess->sid,
 216                sess->sess_ops->InitiatorName);
 217
 218        spin_lock_bh(&sess->conn_lock);
 219        if (sess->session_state == TARG_SESS_STATE_FAILED) {
 220                spin_unlock_bh(&sess->conn_lock);
 221                iscsit_dec_session_usage_count(sess);
 222                target_put_session(sess->se_sess);
 223                return 0;
 224        }
 225        spin_unlock_bh(&sess->conn_lock);
 226
 227        iscsit_stop_session(sess, 1, 1);
 228        iscsit_dec_session_usage_count(sess);
 229
 230        target_put_session(sess->se_sess);
 231        return 0;
 232}
 233
 234static void iscsi_login_set_conn_values(
 235        struct iscsi_session *sess,
 236        struct iscsi_conn *conn,
 237        __be16 cid)
 238{
 239        conn->sess              = sess;
 240        conn->cid               = be16_to_cpu(cid);
 241        /*
 242         * Generate a random Status sequence number (statsn) for the new
 243         * iSCSI connection.
 244         */
 245        get_random_bytes(&conn->stat_sn, sizeof(u32));
 246
 247        mutex_lock(&auth_id_lock);
 248        conn->auth_id           = iscsit_global->auth_id++;
 249        mutex_unlock(&auth_id_lock);
 250}
 251
 252static __printf(2, 3) int iscsi_change_param_sprintf(
 253        struct iscsi_conn *conn,
 254        const char *fmt, ...)
 255{
 256        va_list args;
 257        unsigned char buf[64];
 258
 259        memset(buf, 0, sizeof buf);
 260
 261        va_start(args, fmt);
 262        vsnprintf(buf, sizeof buf, fmt, args);
 263        va_end(args);
 264
 265        if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
 266                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 267                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 268                return -1;
 269        }
 270
 271        return 0;
 272}
 273
 274/*
 275 *      This is the leading connection of a new session,
 276 *      or session reinstatement.
 277 */
 278static int iscsi_login_zero_tsih_s1(
 279        struct iscsi_conn *conn,
 280        unsigned char *buf)
 281{
 282        struct iscsi_session *sess = NULL;
 283        struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
 284        int ret;
 285
 286        sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
 287        if (!sess) {
 288                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 289                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 290                pr_err("Could not allocate memory for session\n");
 291                return -ENOMEM;
 292        }
 293
 294        iscsi_login_set_conn_values(sess, conn, pdu->cid);
 295        sess->init_task_tag     = pdu->itt;
 296        memcpy(&sess->isid, pdu->isid, 6);
 297        sess->exp_cmd_sn        = be32_to_cpu(pdu->cmdsn);
 298        INIT_LIST_HEAD(&sess->sess_conn_list);
 299        INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
 300        INIT_LIST_HEAD(&sess->cr_active_list);
 301        INIT_LIST_HEAD(&sess->cr_inactive_list);
 302        init_completion(&sess->async_msg_comp);
 303        init_completion(&sess->reinstatement_comp);
 304        init_completion(&sess->session_wait_comp);
 305        init_completion(&sess->session_waiting_on_uc_comp);
 306        mutex_init(&sess->cmdsn_mutex);
 307        spin_lock_init(&sess->conn_lock);
 308        spin_lock_init(&sess->cr_a_lock);
 309        spin_lock_init(&sess->cr_i_lock);
 310        spin_lock_init(&sess->session_usage_lock);
 311        spin_lock_init(&sess->ttt_lock);
 312
 313        idr_preload(GFP_KERNEL);
 314        spin_lock_bh(&sess_idr_lock);
 315        ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
 316        if (ret >= 0)
 317                sess->session_index = ret;
 318        spin_unlock_bh(&sess_idr_lock);
 319        idr_preload_end();
 320
 321        if (ret < 0) {
 322                pr_err("idr_alloc() for sess_idr failed\n");
 323                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 324                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 325                kfree(sess);
 326                return -ENOMEM;
 327        }
 328
 329        sess->creation_time = get_jiffies_64();
 330        /*
 331         * The FFP CmdSN window values will be allocated from the TPG's
 332         * Initiator Node's ACL once the login has been successfully completed.
 333         */
 334        sess->max_cmd_sn        = be32_to_cpu(pdu->cmdsn);
 335
 336        sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
 337        if (!sess->sess_ops) {
 338                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 339                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 340                pr_err("Unable to allocate memory for"
 341                                " struct iscsi_sess_ops.\n");
 342                kfree(sess);
 343                return -ENOMEM;
 344        }
 345
 346        sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
 347        if (IS_ERR(sess->se_sess)) {
 348                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 349                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 350                kfree(sess);
 351                return -ENOMEM;
 352        }
 353
 354        return 0;
 355}
 356
 357static int iscsi_login_zero_tsih_s2(
 358        struct iscsi_conn *conn)
 359{
 360        struct iscsi_node_attrib *na;
 361        struct iscsi_session *sess = conn->sess;
 362        bool iser = false;
 363
 364        sess->tpg = conn->tpg;
 365
 366        /*
 367         * Assign a new TPG Session Handle.  Note this is protected with
 368         * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
 369         */
 370        sess->tsih = ++sess->tpg->ntsih;
 371        if (!sess->tsih)
 372                sess->tsih = ++sess->tpg->ntsih;
 373
 374        /*
 375         * Create the default params from user defined values..
 376         */
 377        if (iscsi_copy_param_list(&conn->param_list,
 378                                conn->tpg->param_list, 1) < 0) {
 379                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 380                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 381                return -1;
 382        }
 383
 384        if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
 385                iser = true;
 386
 387        iscsi_set_keys_to_negotiate(conn->param_list, iser);
 388
 389        if (sess->sess_ops->SessionType)
 390                return iscsi_set_keys_irrelevant_for_discovery(
 391                                conn->param_list);
 392
 393        na = iscsit_tpg_get_node_attrib(sess);
 394
 395        /*
 396         * Need to send TargetPortalGroupTag back in first login response
 397         * on any iSCSI connection where the Initiator provides TargetName.
 398         * See 5.3.1.  Login Phase Start
 399         *
 400         * In our case, we have already located the struct iscsi_tiqn at this point.
 401         */
 402        if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
 403                return -1;
 404
 405        /*
 406         * Workaround for Initiators that have broken connection recovery logic.
 407         *
 408         * "We would really like to get rid of this." Linux-iSCSI.org team
 409         */
 410        if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
 411                return -1;
 412
 413        if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
 414                return -1;
 415        /*
 416         * Set RDMAExtensions=Yes by default for iSER enabled network portals
 417         */
 418        if (iser) {
 419                struct iscsi_param *param;
 420                unsigned long mrdsl, off;
 421                int rc;
 422
 423                if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
 424                        return -1;
 425
 426                /*
 427                 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
 428                 * Immediate Data + Unsolicitied Data-OUT if necessary..
 429                 */
 430                param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
 431                                                  conn->param_list);
 432                if (!param) {
 433                        iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 434                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 435                        return -1;
 436                }
 437                rc = kstrtoul(param->value, 0, &mrdsl);
 438                if (rc < 0) {
 439                        iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 440                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 441                        return -1;
 442                }
 443                off = mrdsl % PAGE_SIZE;
 444                if (!off)
 445                        goto check_prot;
 446
 447                if (mrdsl < PAGE_SIZE)
 448                        mrdsl = PAGE_SIZE;
 449                else
 450                        mrdsl -= off;
 451
 452                pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
 453                        " to PAGE_SIZE\n", mrdsl);
 454
 455                if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
 456                        return -1;
 457                /*
 458                 * ISER currently requires that ImmediateData + Unsolicited
 459                 * Data be disabled when protection / signature MRs are enabled.
 460                 */
 461check_prot:
 462                if (sess->se_sess->sup_prot_ops &
 463                   (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
 464                    TARGET_PROT_DOUT_INSERT)) {
 465
 466                        if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
 467                                return -1;
 468
 469                        if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
 470                                return -1;
 471
 472                        pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
 473                                 " T10-PI enabled ISER session\n");
 474                }
 475        }
 476
 477        return 0;
 478}
 479
 480/*
 481 * Remove PSTATE_NEGOTIATE for the four FIM related keys.
 482 * The Initiator node will be able to enable FIM by proposing them itself.
 483 */
 484int iscsi_login_disable_FIM_keys(
 485        struct iscsi_param_list *param_list,
 486        struct iscsi_conn *conn)
 487{
 488        struct iscsi_param *param;
 489
 490        param = iscsi_find_param_from_key("OFMarker", param_list);
 491        if (!param) {
 492                pr_err("iscsi_find_param_from_key() for"
 493                                " OFMarker failed\n");
 494                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 495                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 496                return -1;
 497        }
 498        param->state &= ~PSTATE_NEGOTIATE;
 499
 500        param = iscsi_find_param_from_key("OFMarkInt", param_list);
 501        if (!param) {
 502                pr_err("iscsi_find_param_from_key() for"
 503                                " IFMarker failed\n");
 504                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 505                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 506                return -1;
 507        }
 508        param->state &= ~PSTATE_NEGOTIATE;
 509
 510        param = iscsi_find_param_from_key("IFMarker", param_list);
 511        if (!param) {
 512                pr_err("iscsi_find_param_from_key() for"
 513                                " IFMarker failed\n");
 514                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 515                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 516                return -1;
 517        }
 518        param->state &= ~PSTATE_NEGOTIATE;
 519
 520        param = iscsi_find_param_from_key("IFMarkInt", param_list);
 521        if (!param) {
 522                pr_err("iscsi_find_param_from_key() for"
 523                                " IFMarker failed\n");
 524                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 525                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 526                return -1;
 527        }
 528        param->state &= ~PSTATE_NEGOTIATE;
 529
 530        return 0;
 531}
 532
 533static int iscsi_login_non_zero_tsih_s1(
 534        struct iscsi_conn *conn,
 535        unsigned char *buf)
 536{
 537        struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
 538
 539        iscsi_login_set_conn_values(NULL, conn, pdu->cid);
 540        return 0;
 541}
 542
 543/*
 544 *      Add a new connection to an existing session.
 545 */
 546static int iscsi_login_non_zero_tsih_s2(
 547        struct iscsi_conn *conn,
 548        unsigned char *buf)
 549{
 550        struct iscsi_portal_group *tpg = conn->tpg;
 551        struct iscsi_session *sess = NULL, *sess_p = NULL;
 552        struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
 553        struct se_session *se_sess, *se_sess_tmp;
 554        struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
 555        bool iser = false;
 556
 557        spin_lock_bh(&se_tpg->session_lock);
 558        list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
 559                        sess_list) {
 560
 561                sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
 562                if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
 563                    atomic_read(&sess_p->session_logout) ||
 564                   (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
 565                        continue;
 566                if (!memcmp(sess_p->isid, pdu->isid, 6) &&
 567                     (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
 568                        iscsit_inc_session_usage_count(sess_p);
 569                        iscsit_stop_time2retain_timer(sess_p);
 570                        sess = sess_p;
 571                        break;
 572                }
 573        }
 574        spin_unlock_bh(&se_tpg->session_lock);
 575
 576        /*
 577         * If the Time2Retain handler has expired, the session is already gone.
 578         */
 579        if (!sess) {
 580                pr_err("Initiator attempting to add a connection to"
 581                        " a non-existent session, rejecting iSCSI Login.\n");
 582                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 583                                ISCSI_LOGIN_STATUS_NO_SESSION);
 584                return -1;
 585        }
 586
 587        /*
 588         * Stop the Time2Retain timer if this is a failed session, we restart
 589         * the timer if the login is not successful.
 590         */
 591        spin_lock_bh(&sess->conn_lock);
 592        if (sess->session_state == TARG_SESS_STATE_FAILED)
 593                atomic_set(&sess->session_continuation, 1);
 594        spin_unlock_bh(&sess->conn_lock);
 595
 596        iscsi_login_set_conn_values(sess, conn, pdu->cid);
 597
 598        if (iscsi_copy_param_list(&conn->param_list,
 599                        conn->tpg->param_list, 0) < 0) {
 600                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 601                                ISCSI_LOGIN_STATUS_NO_RESOURCES);
 602                return -1;
 603        }
 604
 605        if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
 606                iser = true;
 607
 608        iscsi_set_keys_to_negotiate(conn->param_list, iser);
 609        /*
 610         * Need to send TargetPortalGroupTag back in first login response
 611         * on any iSCSI connection where the Initiator provides TargetName.
 612         * See 5.3.1.  Login Phase Start
 613         *
 614         * In our case, we have already located the struct iscsi_tiqn at this point.
 615         */
 616        if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
 617                return -1;
 618
 619        return iscsi_login_disable_FIM_keys(conn->param_list, conn);
 620}
 621
 622int iscsi_login_post_auth_non_zero_tsih(
 623        struct iscsi_conn *conn,
 624        u16 cid,
 625        u32 exp_statsn)
 626{
 627        struct iscsi_conn *conn_ptr = NULL;
 628        struct iscsi_conn_recovery *cr = NULL;
 629        struct iscsi_session *sess = conn->sess;
 630
 631        /*
 632         * By following item 5 in the login table,  if we have found
 633         * an existing ISID and a valid/existing TSIH and an existing
 634         * CID we do connection reinstatement.  Currently we dont not
 635         * support it so we send back an non-zero status class to the
 636         * initiator and release the new connection.
 637         */
 638        conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
 639        if (conn_ptr) {
 640                pr_err("Connection exists with CID %hu for %s,"
 641                        " performing connection reinstatement.\n",
 642                        conn_ptr->cid, sess->sess_ops->InitiatorName);
 643
 644                iscsit_connection_reinstatement_rcfr(conn_ptr);
 645                iscsit_dec_conn_usage_count(conn_ptr);
 646        }
 647
 648        /*
 649         * Check for any connection recovery entires containing CID.
 650         * We use the original ExpStatSN sent in the first login request
 651         * to acknowledge commands for the failed connection.
 652         *
 653         * Also note that an explict logout may have already been sent,
 654         * but the response may not be sent due to additional connection
 655         * loss.
 656         */
 657        if (sess->sess_ops->ErrorRecoveryLevel == 2) {
 658                cr = iscsit_get_inactive_connection_recovery_entry(
 659                                sess, cid);
 660                if (cr) {
 661                        pr_debug("Performing implicit logout"
 662                                " for connection recovery on CID: %hu\n",
 663                                        conn->cid);
 664                        iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
 665                }
 666        }
 667
 668        /*
 669         * Else we follow item 4 from the login table in that we have
 670         * found an existing ISID and a valid/existing TSIH and a new
 671         * CID we go ahead and continue to add a new connection to the
 672         * session.
 673         */
 674        pr_debug("Adding CID %hu to existing session for %s.\n",
 675                        cid, sess->sess_ops->InitiatorName);
 676
 677        if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
 678                pr_err("Adding additional connection to this session"
 679                        " would exceed MaxConnections %d, login failed.\n",
 680                                sess->sess_ops->MaxConnections);
 681                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 682                                ISCSI_LOGIN_STATUS_ISID_ERROR);
 683                return -1;
 684        }
 685
 686        return 0;
 687}
 688
 689static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
 690{
 691        struct iscsi_session *sess = conn->sess;
 692        /*
 693         * FIXME: Unsolicitied NopIN support for ISER
 694         */
 695        if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
 696                return;
 697
 698        if (!sess->sess_ops->SessionType)
 699                iscsit_start_nopin_timer(conn);
 700}
 701
 702int iscsi_post_login_handler(
 703        struct iscsi_np *np,
 704        struct iscsi_conn *conn,
 705        u8 zero_tsih)
 706{
 707        int stop_timer = 0;
 708        struct iscsi_session *sess = conn->sess;
 709        struct se_session *se_sess = sess->se_sess;
 710        struct iscsi_portal_group *tpg = sess->tpg;
 711        struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
 712        struct iscsi_thread_set *ts;
 713
 714        iscsit_inc_conn_usage_count(conn);
 715
 716        iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
 717                        ISCSI_LOGIN_STATUS_ACCEPT);
 718
 719        pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
 720        conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
 721
 722        iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
 723        iscsit_set_sync_and_steering_values(conn);
 724        /*
 725         * SCSI Initiator -> SCSI Target Port Mapping
 726         */
 727        ts = iscsi_get_thread_set();
 728        if (!zero_tsih) {
 729                iscsi_set_session_parameters(sess->sess_ops,
 730                                conn->param_list, 0);
 731                iscsi_release_param_list(conn->param_list);
 732                conn->param_list = NULL;
 733
 734                spin_lock_bh(&sess->conn_lock);
 735                atomic_set(&sess->session_continuation, 0);
 736                if (sess->session_state == TARG_SESS_STATE_FAILED) {
 737                        pr_debug("Moving to"
 738                                        " TARG_SESS_STATE_LOGGED_IN.\n");
 739                        sess->session_state = TARG_SESS_STATE_LOGGED_IN;
 740                        stop_timer = 1;
 741                }
 742
 743                pr_debug("iSCSI Login successful on CID: %hu from %s to"
 744                        " %s:%hu,%hu\n", conn->cid, conn->login_ip,
 745                        conn->local_ip, conn->local_port, tpg->tpgt);
 746
 747                list_add_tail(&conn->conn_list, &sess->sess_conn_list);
 748                atomic_inc(&sess->nconn);
 749                pr_debug("Incremented iSCSI Connection count to %hu"
 750                        " from node: %s\n", atomic_read(&sess->nconn),
 751                        sess->sess_ops->InitiatorName);
 752                spin_unlock_bh(&sess->conn_lock);
 753
 754                iscsi_post_login_start_timers(conn);
 755
 756                iscsi_activate_thread_set(conn, ts);
 757                /*
 758                 * Determine CPU mask to ensure connection's RX and TX kthreads
 759                 * are scheduled on the same CPU.
 760                 */
 761                iscsit_thread_get_cpumask(conn);
 762                conn->conn_rx_reset_cpumask = 1;
 763                conn->conn_tx_reset_cpumask = 1;
 764
 765                iscsit_dec_conn_usage_count(conn);
 766                if (stop_timer) {
 767                        spin_lock_bh(&se_tpg->session_lock);
 768                        iscsit_stop_time2retain_timer(sess);
 769                        spin_unlock_bh(&se_tpg->session_lock);
 770                }
 771                iscsit_dec_session_usage_count(sess);
 772                return 0;
 773        }
 774
 775        iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
 776        iscsi_release_param_list(conn->param_list);
 777        conn->param_list = NULL;
 778
 779        iscsit_determine_maxcmdsn(sess);
 780
 781        spin_lock_bh(&se_tpg->session_lock);
 782        __transport_register_session(&sess->tpg->tpg_se_tpg,
 783                        se_sess->se_node_acl, se_sess, sess);
 784        pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
 785        sess->session_state = TARG_SESS_STATE_LOGGED_IN;
 786
 787        pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
 788                conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
 789                tpg->tpgt);
 790
 791        spin_lock_bh(&sess->conn_lock);
 792        list_add_tail(&conn->conn_list, &sess->sess_conn_list);
 793        atomic_inc(&sess->nconn);
 794        pr_debug("Incremented iSCSI Connection count to %hu from node:"
 795                " %s\n", atomic_read(&sess->nconn),
 796                sess->sess_ops->InitiatorName);
 797        spin_unlock_bh(&sess->conn_lock);
 798
 799        sess->sid = tpg->sid++;
 800        if (!sess->sid)
 801                sess->sid = tpg->sid++;
 802        pr_debug("Established iSCSI session from node: %s\n",
 803                        sess->sess_ops->InitiatorName);
 804
 805        tpg->nsessions++;
 806        if (tpg->tpg_tiqn)
 807                tpg->tpg_tiqn->tiqn_nsessions++;
 808
 809        pr_debug("Incremented number of active iSCSI sessions to %u on"
 810                " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
 811        spin_unlock_bh(&se_tpg->session_lock);
 812
 813        iscsi_post_login_start_timers(conn);
 814        iscsi_activate_thread_set(conn, ts);
 815        /*
 816         * Determine CPU mask to ensure connection's RX and TX kthreads
 817         * are scheduled on the same CPU.
 818         */
 819        iscsit_thread_get_cpumask(conn);
 820        conn->conn_rx_reset_cpumask = 1;
 821        conn->conn_tx_reset_cpumask = 1;
 822
 823        iscsit_dec_conn_usage_count(conn);
 824
 825        return 0;
 826}
 827
 828static void iscsi_handle_login_thread_timeout(unsigned long data)
 829{
 830        struct iscsi_np *np = (struct iscsi_np *) data;
 831
 832        spin_lock_bh(&np->np_thread_lock);
 833        pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
 834                        np->np_ip, np->np_port);
 835
 836        if (np->np_login_timer_flags & ISCSI_TF_STOP) {
 837                spin_unlock_bh(&np->np_thread_lock);
 838                return;
 839        }
 840
 841        if (np->np_thread)
 842                send_sig(SIGINT, np->np_thread, 1);
 843
 844        np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
 845        spin_unlock_bh(&np->np_thread_lock);
 846}
 847
 848static void iscsi_start_login_thread_timer(struct iscsi_np *np)
 849{
 850        /*
 851         * This used the TA_LOGIN_TIMEOUT constant because at this
 852         * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
 853         */
 854        spin_lock_bh(&np->np_thread_lock);
 855        init_timer(&np->np_login_timer);
 856        np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
 857        np->np_login_timer.data = (unsigned long)np;
 858        np->np_login_timer.function = iscsi_handle_login_thread_timeout;
 859        np->np_login_timer_flags &= ~ISCSI_TF_STOP;
 860        np->np_login_timer_flags |= ISCSI_TF_RUNNING;
 861        add_timer(&np->np_login_timer);
 862
 863        pr_debug("Added timeout timer to iSCSI login request for"
 864                        " %u seconds.\n", TA_LOGIN_TIMEOUT);
 865        spin_unlock_bh(&np->np_thread_lock);
 866}
 867
 868static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
 869{
 870        spin_lock_bh(&np->np_thread_lock);
 871        if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
 872                spin_unlock_bh(&np->np_thread_lock);
 873                return;
 874        }
 875        np->np_login_timer_flags |= ISCSI_TF_STOP;
 876        spin_unlock_bh(&np->np_thread_lock);
 877
 878        del_timer_sync(&np->np_login_timer);
 879
 880        spin_lock_bh(&np->np_thread_lock);
 881        np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
 882        spin_unlock_bh(&np->np_thread_lock);
 883}
 884
 885int iscsit_setup_np(
 886        struct iscsi_np *np,
 887        struct __kernel_sockaddr_storage *sockaddr)
 888{
 889        struct socket *sock = NULL;
 890        int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
 891
 892        switch (np->np_network_transport) {
 893        case ISCSI_TCP:
 894                np->np_ip_proto = IPPROTO_TCP;
 895                np->np_sock_type = SOCK_STREAM;
 896                break;
 897        case ISCSI_SCTP_TCP:
 898                np->np_ip_proto = IPPROTO_SCTP;
 899                np->np_sock_type = SOCK_STREAM;
 900                break;
 901        case ISCSI_SCTP_UDP:
 902                np->np_ip_proto = IPPROTO_SCTP;
 903                np->np_sock_type = SOCK_SEQPACKET;
 904                break;
 905        default:
 906                pr_err("Unsupported network_transport: %d\n",
 907                                np->np_network_transport);
 908                return -EINVAL;
 909        }
 910
 911        np->np_ip_proto = IPPROTO_TCP;
 912        np->np_sock_type = SOCK_STREAM;
 913
 914        ret = sock_create(sockaddr->ss_family, np->np_sock_type,
 915                        np->np_ip_proto, &sock);
 916        if (ret < 0) {
 917                pr_err("sock_create() failed.\n");
 918                return ret;
 919        }
 920        np->np_socket = sock;
 921        /*
 922         * Setup the np->np_sockaddr from the passed sockaddr setup
 923         * in iscsi_target_configfs.c code..
 924         */
 925        memcpy(&np->np_sockaddr, sockaddr,
 926                        sizeof(struct __kernel_sockaddr_storage));
 927
 928        if (sockaddr->ss_family == AF_INET6)
 929                len = sizeof(struct sockaddr_in6);
 930        else
 931                len = sizeof(struct sockaddr_in);
 932        /*
 933         * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
 934         */
 935        /* FIXME: Someone please explain why this is endian-safe */
 936        opt = 1;
 937        if (np->np_network_transport == ISCSI_TCP) {
 938                ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
 939                                (char *)&opt, sizeof(opt));
 940                if (ret < 0) {
 941                        pr_err("kernel_setsockopt() for TCP_NODELAY"
 942                                " failed: %d\n", ret);
 943                        goto fail;
 944                }
 945        }
 946
 947        /* FIXME: Someone please explain why this is endian-safe */
 948        ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
 949                        (char *)&opt, sizeof(opt));
 950        if (ret < 0) {
 951                pr_err("kernel_setsockopt() for SO_REUSEADDR"
 952                        " failed\n");
 953                goto fail;
 954        }
 955
 956        ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
 957                        (char *)&opt, sizeof(opt));
 958        if (ret < 0) {
 959                pr_err("kernel_setsockopt() for IP_FREEBIND"
 960                        " failed\n");
 961                goto fail;
 962        }
 963
 964        ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
 965        if (ret < 0) {
 966                pr_err("kernel_bind() failed: %d\n", ret);
 967                goto fail;
 968        }
 969
 970        ret = kernel_listen(sock, backlog);
 971        if (ret != 0) {
 972                pr_err("kernel_listen() failed: %d\n", ret);
 973                goto fail;
 974        }
 975
 976        return 0;
 977fail:
 978        np->np_socket = NULL;
 979        sock_release(sock);
 980        return ret;
 981}
 982
 983int iscsi_target_setup_login_socket(
 984        struct iscsi_np *np,
 985        struct __kernel_sockaddr_storage *sockaddr)
 986{
 987        struct iscsit_transport *t;
 988        int rc;
 989
 990        t = iscsit_get_transport(np->np_network_transport);
 991        if (!t)
 992                return -EINVAL;
 993
 994        rc = t->iscsit_setup_np(np, sockaddr);
 995        if (rc < 0) {
 996                iscsit_put_transport(t);
 997                return rc;
 998        }
 999
1000        np->np_transport = t;
1001        np->enabled = true;
1002        return 0;
1003}
1004
1005int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1006{
1007        struct socket *new_sock, *sock = np->np_socket;
1008        struct sockaddr_in sock_in;
1009        struct sockaddr_in6 sock_in6;
1010        int rc, err;
1011
1012        rc = kernel_accept(sock, &new_sock, 0);
1013        if (rc < 0)
1014                return rc;
1015
1016        conn->sock = new_sock;
1017        conn->login_family = np->np_sockaddr.ss_family;
1018
1019        if (np->np_sockaddr.ss_family == AF_INET6) {
1020                memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1021
1022                rc = conn->sock->ops->getname(conn->sock,
1023                                (struct sockaddr *)&sock_in6, &err, 1);
1024                if (!rc) {
1025                        if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1026                                snprintf(conn->login_ip, sizeof(conn->login_ip), "[%pI6c]",
1027                                        &sock_in6.sin6_addr.in6_u);
1028                        else
1029                                snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI4",
1030                                        &sock_in6.sin6_addr.s6_addr32[3]);
1031                        conn->login_port = ntohs(sock_in6.sin6_port);
1032                }
1033
1034                rc = conn->sock->ops->getname(conn->sock,
1035                                (struct sockaddr *)&sock_in6, &err, 0);
1036                if (!rc) {
1037                        if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1038                                snprintf(conn->local_ip, sizeof(conn->local_ip), "[%pI6c]",
1039                                        &sock_in6.sin6_addr.in6_u);
1040                        else
1041                                snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI4",
1042                                        &sock_in6.sin6_addr.s6_addr32[3]);
1043                        conn->local_port = ntohs(sock_in6.sin6_port);
1044                }
1045        } else {
1046                memset(&sock_in, 0, sizeof(struct sockaddr_in));
1047
1048                rc = conn->sock->ops->getname(conn->sock,
1049                                (struct sockaddr *)&sock_in, &err, 1);
1050                if (!rc) {
1051                        sprintf(conn->login_ip, "%pI4",
1052                                        &sock_in.sin_addr.s_addr);
1053                        conn->login_port = ntohs(sock_in.sin_port);
1054                }
1055
1056                rc = conn->sock->ops->getname(conn->sock,
1057                                (struct sockaddr *)&sock_in, &err, 0);
1058                if (!rc) {
1059                        sprintf(conn->local_ip, "%pI4",
1060                                        &sock_in.sin_addr.s_addr);
1061                        conn->local_port = ntohs(sock_in.sin_port);
1062                }
1063        }
1064
1065        return 0;
1066}
1067
1068int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1069{
1070        struct iscsi_login_req *login_req;
1071        u32 padding = 0, payload_length;
1072
1073        if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1074                return -1;
1075
1076        login_req = (struct iscsi_login_req *)login->req;
1077        payload_length  = ntoh24(login_req->dlength);
1078        padding = ((-payload_length) & 3);
1079
1080        pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1081                " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1082                login_req->flags, login_req->itt, login_req->cmdsn,
1083                login_req->exp_statsn, login_req->cid, payload_length);
1084        /*
1085         * Setup the initial iscsi_login values from the leading
1086         * login request PDU.
1087         */
1088        if (login->first_request) {
1089                login_req = (struct iscsi_login_req *)login->req;
1090                login->leading_connection = (!login_req->tsih) ? 1 : 0;
1091                login->current_stage    = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1092                login->version_min      = login_req->min_version;
1093                login->version_max      = login_req->max_version;
1094                memcpy(login->isid, login_req->isid, 6);
1095                login->cmd_sn           = be32_to_cpu(login_req->cmdsn);
1096                login->init_task_tag    = login_req->itt;
1097                login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1098                login->cid              = be16_to_cpu(login_req->cid);
1099                login->tsih             = be16_to_cpu(login_req->tsih);
1100        }
1101
1102        if (iscsi_target_check_login_request(conn, login) < 0)
1103                return -1;
1104
1105        memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1106        if (iscsi_login_rx_data(conn, login->req_buf,
1107                                payload_length + padding) < 0)
1108                return -1;
1109
1110        return 0;
1111}
1112
1113int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1114                        u32 length)
1115{
1116        if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1117                return -1;
1118
1119        return 0;
1120}
1121
1122static int
1123iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1124{
1125        int rc;
1126
1127        if (!t->owner) {
1128                conn->conn_transport = t;
1129                return 0;
1130        }
1131
1132        rc = try_module_get(t->owner);
1133        if (!rc) {
1134                pr_err("try_module_get() failed for %s\n", t->name);
1135                return -EINVAL;
1136        }
1137
1138        conn->conn_transport = t;
1139        return 0;
1140}
1141
1142void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1143                struct iscsi_np *np, bool zero_tsih, bool new_sess)
1144{
1145        if (!new_sess)
1146                goto old_sess_out;
1147
1148        pr_err("iSCSI Login negotiation failed.\n");
1149        iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1150                                   ISCSI_LOGIN_STATUS_INIT_ERR);
1151        if (!zero_tsih || !conn->sess)
1152                goto old_sess_out;
1153        if (conn->sess->se_sess)
1154                transport_free_session(conn->sess->se_sess);
1155        if (conn->sess->session_index != 0) {
1156                spin_lock_bh(&sess_idr_lock);
1157                idr_remove(&sess_idr, conn->sess->session_index);
1158                spin_unlock_bh(&sess_idr_lock);
1159        }
1160        kfree(conn->sess->sess_ops);
1161        kfree(conn->sess);
1162        conn->sess = NULL;
1163
1164old_sess_out:
1165        iscsi_stop_login_thread_timer(np);
1166        /*
1167         * If login negotiation fails check if the Time2Retain timer
1168         * needs to be restarted.
1169         */
1170        if (!zero_tsih && conn->sess) {
1171                spin_lock_bh(&conn->sess->conn_lock);
1172                if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1173                        struct se_portal_group *se_tpg =
1174                                        &conn->tpg->tpg_se_tpg;
1175
1176                        atomic_set(&conn->sess->session_continuation, 0);
1177                        spin_unlock_bh(&conn->sess->conn_lock);
1178                        spin_lock_bh(&se_tpg->session_lock);
1179                        iscsit_start_time2retain_handler(conn->sess);
1180                        spin_unlock_bh(&se_tpg->session_lock);
1181                } else
1182                        spin_unlock_bh(&conn->sess->conn_lock);
1183                iscsit_dec_session_usage_count(conn->sess);
1184        }
1185
1186        if (!IS_ERR(conn->conn_rx_hash.tfm))
1187                crypto_free_hash(conn->conn_rx_hash.tfm);
1188        if (!IS_ERR(conn->conn_tx_hash.tfm))
1189                crypto_free_hash(conn->conn_tx_hash.tfm);
1190
1191        free_cpumask_var(conn->conn_cpumask);
1192
1193        kfree(conn->conn_ops);
1194
1195        if (conn->param_list) {
1196                iscsi_release_param_list(conn->param_list);
1197                conn->param_list = NULL;
1198        }
1199        iscsi_target_nego_release(conn);
1200
1201        if (conn->sock) {
1202                sock_release(conn->sock);
1203                conn->sock = NULL;
1204        }
1205
1206        if (conn->conn_transport->iscsit_wait_conn)
1207                conn->conn_transport->iscsit_wait_conn(conn);
1208
1209        if (conn->conn_transport->iscsit_free_conn)
1210                conn->conn_transport->iscsit_free_conn(conn);
1211
1212        iscsit_put_transport(conn->conn_transport);
1213        kfree(conn);
1214}
1215
1216static int __iscsi_target_login_thread(struct iscsi_np *np)
1217{
1218        u8 *buffer, zero_tsih = 0;
1219        int ret = 0, rc;
1220        struct iscsi_conn *conn = NULL;
1221        struct iscsi_login *login;
1222        struct iscsi_portal_group *tpg = NULL;
1223        struct iscsi_login_req *pdu;
1224        struct iscsi_tpg_np *tpg_np;
1225        bool new_sess = false;
1226
1227        flush_signals(current);
1228
1229        spin_lock_bh(&np->np_thread_lock);
1230        if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1231                np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1232                complete(&np->np_restart_comp);
1233        } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1234                spin_unlock_bh(&np->np_thread_lock);
1235                goto exit;
1236        } else {
1237                np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1238        }
1239        spin_unlock_bh(&np->np_thread_lock);
1240
1241        conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1242        if (!conn) {
1243                pr_err("Could not allocate memory for"
1244                        " new connection\n");
1245                /* Get another socket */
1246                return 1;
1247        }
1248        pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1249        conn->conn_state = TARG_CONN_STATE_FREE;
1250
1251        if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1252                kfree(conn);
1253                return 1;
1254        }
1255
1256        rc = np->np_transport->iscsit_accept_np(np, conn);
1257        if (rc == -ENOSYS) {
1258                complete(&np->np_restart_comp);
1259                iscsit_put_transport(conn->conn_transport);
1260                kfree(conn);
1261                conn = NULL;
1262                goto exit;
1263        } else if (rc < 0) {
1264                spin_lock_bh(&np->np_thread_lock);
1265                if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1266                        spin_unlock_bh(&np->np_thread_lock);
1267                        complete(&np->np_restart_comp);
1268                        iscsit_put_transport(conn->conn_transport);
1269                        kfree(conn);
1270                        conn = NULL;
1271                        /* Get another socket */
1272                        return 1;
1273                }
1274                spin_unlock_bh(&np->np_thread_lock);
1275                iscsit_put_transport(conn->conn_transport);
1276                kfree(conn);
1277                conn = NULL;
1278                goto out;
1279        }
1280        /*
1281         * Perform the remaining iSCSI connection initialization items..
1282         */
1283        login = iscsi_login_init_conn(conn);
1284        if (!login) {
1285                goto new_sess_out;
1286        }
1287
1288        iscsi_start_login_thread_timer(np);
1289
1290        pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1291        conn->conn_state = TARG_CONN_STATE_XPT_UP;
1292        /*
1293         * This will process the first login request + payload..
1294         */
1295        rc = np->np_transport->iscsit_get_login_rx(conn, login);
1296        if (rc == 1)
1297                return 1;
1298        else if (rc < 0)
1299                goto new_sess_out;
1300
1301        buffer = &login->req[0];
1302        pdu = (struct iscsi_login_req *)buffer;
1303        /*
1304         * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1305         * when Status-Class != 0.
1306        */
1307        conn->login_itt = pdu->itt;
1308
1309        spin_lock_bh(&np->np_thread_lock);
1310        if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1311                spin_unlock_bh(&np->np_thread_lock);
1312                pr_err("iSCSI Network Portal on %s:%hu currently not"
1313                        " active.\n", np->np_ip, np->np_port);
1314                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1315                                ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1316                goto new_sess_out;
1317        }
1318        spin_unlock_bh(&np->np_thread_lock);
1319
1320        conn->network_transport = np->np_network_transport;
1321
1322        pr_debug("Received iSCSI login request from %s on %s Network"
1323                " Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
1324                conn->local_ip, conn->local_port);
1325
1326        pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1327        conn->conn_state        = TARG_CONN_STATE_IN_LOGIN;
1328
1329        if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1330                        pdu->min_version) < 0)
1331                goto new_sess_out;
1332
1333        zero_tsih = (pdu->tsih == 0x0000);
1334        if (zero_tsih) {
1335                /*
1336                 * This is the leading connection of a new session.
1337                 * We wait until after authentication to check for
1338                 * session reinstatement.
1339                 */
1340                if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1341                        goto new_sess_out;
1342        } else {
1343                /*
1344                 * Add a new connection to an existing session.
1345                 * We check for a non-existant session in
1346                 * iscsi_login_non_zero_tsih_s2() below based
1347                 * on ISID/TSIH, but wait until after authentication
1348                 * to check for connection reinstatement, etc.
1349                 */
1350                if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1351                        goto new_sess_out;
1352        }
1353        /*
1354         * SessionType: Discovery
1355         *
1356         *      Locates Default Portal
1357         *
1358         * SessionType: Normal
1359         *
1360         *      Locates Target Portal from NP -> Target IQN
1361         */
1362        rc = iscsi_target_locate_portal(np, conn, login);
1363        if (rc < 0) {
1364                tpg = conn->tpg;
1365                goto new_sess_out;
1366        }
1367        login->zero_tsih = zero_tsih;
1368
1369        conn->sess->se_sess->sup_prot_ops =
1370                conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1371
1372        tpg = conn->tpg;
1373        if (!tpg) {
1374                pr_err("Unable to locate struct iscsi_conn->tpg\n");
1375                goto new_sess_out;
1376        }
1377
1378        if (zero_tsih) {
1379                if (iscsi_login_zero_tsih_s2(conn) < 0)
1380                        goto new_sess_out;
1381        } else {
1382                if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1383                        goto old_sess_out;
1384        }
1385
1386        ret = iscsi_target_start_negotiation(login, conn);
1387        if (ret < 0)
1388                goto new_sess_out;
1389
1390        if (!conn->sess) {
1391                pr_err("struct iscsi_conn session pointer is NULL!\n");
1392                goto new_sess_out;
1393        }
1394
1395        iscsi_stop_login_thread_timer(np);
1396
1397        if (signal_pending(current))
1398                goto new_sess_out;
1399
1400        if (ret == 1) {
1401                tpg_np = conn->tpg_np;
1402
1403                ret = iscsi_post_login_handler(np, conn, zero_tsih);
1404                if (ret < 0)
1405                        goto new_sess_out;
1406
1407                iscsit_deaccess_np(np, tpg, tpg_np);
1408        }
1409
1410        tpg = NULL;
1411        tpg_np = NULL;
1412        /* Get another socket */
1413        return 1;
1414
1415new_sess_out:
1416        new_sess = true;
1417old_sess_out:
1418        tpg_np = conn->tpg_np;
1419        iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1420        new_sess = false;
1421
1422        if (tpg) {
1423                iscsit_deaccess_np(np, tpg, tpg_np);
1424                tpg = NULL;
1425                tpg_np = NULL;
1426        }
1427
1428out:
1429        return 1;
1430
1431exit:
1432        iscsi_stop_login_thread_timer(np);
1433        spin_lock_bh(&np->np_thread_lock);
1434        np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1435        spin_unlock_bh(&np->np_thread_lock);
1436
1437        return 0;
1438}
1439
1440int iscsi_target_login_thread(void *arg)
1441{
1442        struct iscsi_np *np = arg;
1443        int ret;
1444
1445        allow_signal(SIGINT);
1446
1447        while (1) {
1448                ret = __iscsi_target_login_thread(np);
1449                /*
1450                 * We break and exit here unless another sock_accept() call
1451                 * is expected.
1452                 */
1453                if (ret != 1)
1454                        break;
1455        }
1456
1457        return 0;
1458}
1459