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