linux/drivers/target/iscsi/iscsi_target_nego.c
<<
>>
Prefs
   1/*******************************************************************************
   2 * This file contains main functions related to iSCSI Parameter negotiation.
   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/ctype.h>
  20#include <linux/kthread.h>
  21#include <scsi/iscsi_proto.h>
  22#include <target/target_core_base.h>
  23#include <target/target_core_fabric.h>
  24#include <target/iscsi/iscsi_transport.h>
  25
  26#include <target/iscsi/iscsi_target_core.h>
  27#include "iscsi_target_parameters.h"
  28#include "iscsi_target_login.h"
  29#include "iscsi_target_nego.h"
  30#include "iscsi_target_tpg.h"
  31#include "iscsi_target_util.h"
  32#include "iscsi_target.h"
  33#include "iscsi_target_auth.h"
  34
  35#define MAX_LOGIN_PDUS  7
  36#define TEXT_LEN        4096
  37
  38void convert_null_to_semi(char *buf, int len)
  39{
  40        int i;
  41
  42        for (i = 0; i < len; i++)
  43                if (buf[i] == '\0')
  44                        buf[i] = ';';
  45}
  46
  47static int strlen_semi(char *buf)
  48{
  49        int i = 0;
  50
  51        while (buf[i] != '\0') {
  52                if (buf[i] == ';')
  53                        return i;
  54                i++;
  55        }
  56
  57        return -1;
  58}
  59
  60int extract_param(
  61        const char *in_buf,
  62        const char *pattern,
  63        unsigned int max_length,
  64        char *out_buf,
  65        unsigned char *type)
  66{
  67        char *ptr;
  68        int len;
  69
  70        if (!in_buf || !pattern || !out_buf || !type)
  71                return -1;
  72
  73        ptr = strstr(in_buf, pattern);
  74        if (!ptr)
  75                return -1;
  76
  77        ptr = strstr(ptr, "=");
  78        if (!ptr)
  79                return -1;
  80
  81        ptr += 1;
  82        if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
  83                ptr += 2; /* skip 0x */
  84                *type = HEX;
  85        } else
  86                *type = DECIMAL;
  87
  88        len = strlen_semi(ptr);
  89        if (len < 0)
  90                return -1;
  91
  92        if (len >= max_length) {
  93                pr_err("Length of input: %d exceeds max_length:"
  94                        " %d\n", len, max_length);
  95                return -1;
  96        }
  97        memcpy(out_buf, ptr, len);
  98        out_buf[len] = '\0';
  99
 100        return 0;
 101}
 102
 103static u32 iscsi_handle_authentication(
 104        struct iscsi_conn *conn,
 105        char *in_buf,
 106        char *out_buf,
 107        int in_length,
 108        int *out_length,
 109        unsigned char *authtype)
 110{
 111        struct iscsi_session *sess = conn->sess;
 112        struct iscsi_node_auth *auth;
 113        struct iscsi_node_acl *iscsi_nacl;
 114        struct iscsi_portal_group *iscsi_tpg;
 115        struct se_node_acl *se_nacl;
 116
 117        if (!sess->sess_ops->SessionType) {
 118                /*
 119                 * For SessionType=Normal
 120                 */
 121                se_nacl = conn->sess->se_sess->se_node_acl;
 122                if (!se_nacl) {
 123                        pr_err("Unable to locate struct se_node_acl for"
 124                                        " CHAP auth\n");
 125                        return -1;
 126                }
 127                iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
 128                                se_node_acl);
 129                if (!iscsi_nacl) {
 130                        pr_err("Unable to locate struct iscsi_node_acl for"
 131                                        " CHAP auth\n");
 132                        return -1;
 133                }
 134
 135                if (se_nacl->dynamic_node_acl) {
 136                        iscsi_tpg = container_of(se_nacl->se_tpg,
 137                                        struct iscsi_portal_group, tpg_se_tpg);
 138
 139                        auth = &iscsi_tpg->tpg_demo_auth;
 140                } else {
 141                        iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
 142                                                  se_node_acl);
 143
 144                        auth = &iscsi_nacl->node_auth;
 145                }
 146        } else {
 147                /*
 148                 * For SessionType=Discovery
 149                 */
 150                auth = &iscsit_global->discovery_acl.node_auth;
 151        }
 152
 153        if (strstr("CHAP", authtype))
 154                strcpy(conn->sess->auth_type, "CHAP");
 155        else
 156                strcpy(conn->sess->auth_type, NONE);
 157
 158        if (strstr("None", authtype))
 159                return 1;
 160#ifdef CANSRP
 161        else if (strstr("SRP", authtype))
 162                return srp_main_loop(conn, auth, in_buf, out_buf,
 163                                &in_length, out_length);
 164#endif
 165        else if (strstr("CHAP", authtype))
 166                return chap_main_loop(conn, auth, in_buf, out_buf,
 167                                &in_length, out_length);
 168        else if (strstr("SPKM1", authtype))
 169                return 2;
 170        else if (strstr("SPKM2", authtype))
 171                return 2;
 172        else if (strstr("KRB5", authtype))
 173                return 2;
 174        else
 175                return 2;
 176}
 177
 178static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
 179{
 180        kfree(conn->auth_protocol);
 181}
 182
 183int iscsi_target_check_login_request(
 184        struct iscsi_conn *conn,
 185        struct iscsi_login *login)
 186{
 187        int req_csg, req_nsg;
 188        u32 payload_length;
 189        struct iscsi_login_req *login_req;
 190
 191        login_req = (struct iscsi_login_req *) login->req;
 192        payload_length = ntoh24(login_req->dlength);
 193
 194        switch (login_req->opcode & ISCSI_OPCODE_MASK) {
 195        case ISCSI_OP_LOGIN:
 196                break;
 197        default:
 198                pr_err("Received unknown opcode 0x%02x.\n",
 199                                login_req->opcode & ISCSI_OPCODE_MASK);
 200                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 201                                ISCSI_LOGIN_STATUS_INIT_ERR);
 202                return -1;
 203        }
 204
 205        if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
 206            (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
 207                pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
 208                        " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
 209                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 210                                ISCSI_LOGIN_STATUS_INIT_ERR);
 211                return -1;
 212        }
 213
 214        req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
 215        req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
 216
 217        if (req_csg != login->current_stage) {
 218                pr_err("Initiator unexpectedly changed login stage"
 219                        " from %d to %d, login failed.\n", login->current_stage,
 220                        req_csg);
 221                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 222                                ISCSI_LOGIN_STATUS_INIT_ERR);
 223                return -1;
 224        }
 225
 226        if ((req_nsg == 2) || (req_csg >= 2) ||
 227           ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
 228            (req_nsg <= req_csg))) {
 229                pr_err("Illegal login_req->flags Combination, CSG: %d,"
 230                        " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
 231                        req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
 232                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 233                                ISCSI_LOGIN_STATUS_INIT_ERR);
 234                return -1;
 235        }
 236
 237        if ((login_req->max_version != login->version_max) ||
 238            (login_req->min_version != login->version_min)) {
 239                pr_err("Login request changed Version Max/Nin"
 240                        " unexpectedly to 0x%02x/0x%02x, protocol error\n",
 241                        login_req->max_version, login_req->min_version);
 242                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 243                                ISCSI_LOGIN_STATUS_INIT_ERR);
 244                return -1;
 245        }
 246
 247        if (memcmp(login_req->isid, login->isid, 6) != 0) {
 248                pr_err("Login request changed ISID unexpectedly,"
 249                                " protocol error.\n");
 250                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 251                                ISCSI_LOGIN_STATUS_INIT_ERR);
 252                return -1;
 253        }
 254
 255        if (login_req->itt != login->init_task_tag) {
 256                pr_err("Login request changed ITT unexpectedly to"
 257                        " 0x%08x, protocol error.\n", login_req->itt);
 258                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 259                                ISCSI_LOGIN_STATUS_INIT_ERR);
 260                return -1;
 261        }
 262
 263        if (payload_length > MAX_KEY_VALUE_PAIRS) {
 264                pr_err("Login request payload exceeds default"
 265                        " MaxRecvDataSegmentLength: %u, protocol error.\n",
 266                                MAX_KEY_VALUE_PAIRS);
 267                return -1;
 268        }
 269
 270        return 0;
 271}
 272
 273static int iscsi_target_check_first_request(
 274        struct iscsi_conn *conn,
 275        struct iscsi_login *login)
 276{
 277        struct iscsi_param *param = NULL;
 278        struct se_node_acl *se_nacl;
 279
 280        login->first_request = 0;
 281
 282        list_for_each_entry(param, &conn->param_list->param_list, p_list) {
 283                if (!strncmp(param->name, SESSIONTYPE, 11)) {
 284                        if (!IS_PSTATE_ACCEPTOR(param)) {
 285                                pr_err("SessionType key not received"
 286                                        " in first login request.\n");
 287                                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 288                                        ISCSI_LOGIN_STATUS_MISSING_FIELDS);
 289                                return -1;
 290                        }
 291                        if (!strncmp(param->value, DISCOVERY, 9))
 292                                return 0;
 293                }
 294
 295                if (!strncmp(param->name, INITIATORNAME, 13)) {
 296                        if (!IS_PSTATE_ACCEPTOR(param)) {
 297                                if (!login->leading_connection)
 298                                        continue;
 299
 300                                pr_err("InitiatorName key not received"
 301                                        " in first login request.\n");
 302                                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 303                                        ISCSI_LOGIN_STATUS_MISSING_FIELDS);
 304                                return -1;
 305                        }
 306
 307                        /*
 308                         * For non-leading connections, double check that the
 309                         * received InitiatorName matches the existing session's
 310                         * struct iscsi_node_acl.
 311                         */
 312                        if (!login->leading_connection) {
 313                                se_nacl = conn->sess->se_sess->se_node_acl;
 314                                if (!se_nacl) {
 315                                        pr_err("Unable to locate"
 316                                                " struct se_node_acl\n");
 317                                        iscsit_tx_login_rsp(conn,
 318                                                        ISCSI_STATUS_CLS_INITIATOR_ERR,
 319                                                        ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
 320                                        return -1;
 321                                }
 322
 323                                if (strcmp(param->value,
 324                                                se_nacl->initiatorname)) {
 325                                        pr_err("Incorrect"
 326                                                " InitiatorName: %s for this"
 327                                                " iSCSI Initiator Node.\n",
 328                                                param->value);
 329                                        iscsit_tx_login_rsp(conn,
 330                                                        ISCSI_STATUS_CLS_INITIATOR_ERR,
 331                                                        ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
 332                                        return -1;
 333                                }
 334                        }
 335                }
 336        }
 337
 338        return 0;
 339}
 340
 341static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
 342{
 343        u32 padding = 0;
 344        struct iscsi_login_rsp *login_rsp;
 345
 346        login_rsp = (struct iscsi_login_rsp *) login->rsp;
 347
 348        login_rsp->opcode               = ISCSI_OP_LOGIN_RSP;
 349        hton24(login_rsp->dlength, login->rsp_length);
 350        memcpy(login_rsp->isid, login->isid, 6);
 351        login_rsp->tsih                 = cpu_to_be16(login->tsih);
 352        login_rsp->itt                  = login->init_task_tag;
 353        login_rsp->statsn               = cpu_to_be32(conn->stat_sn++);
 354        login_rsp->exp_cmdsn            = cpu_to_be32(conn->sess->exp_cmd_sn);
 355        login_rsp->max_cmdsn            = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
 356
 357        pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
 358                " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
 359                " %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
 360                ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
 361                ntohl(login_rsp->statsn), login->rsp_length);
 362
 363        padding = ((-login->rsp_length) & 3);
 364        /*
 365         * Before sending the last login response containing the transition
 366         * bit for full-feature-phase, go ahead and start up TX/RX threads
 367         * now to avoid potential resource allocation failures after the
 368         * final login response has been sent.
 369         */
 370        if (login->login_complete) {
 371                int rc = iscsit_start_kthreads(conn);
 372                if (rc) {
 373                        iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 374                                            ISCSI_LOGIN_STATUS_NO_RESOURCES);
 375                        return -1;
 376                }
 377        }
 378
 379        if (conn->conn_transport->iscsit_put_login_tx(conn, login,
 380                                        login->rsp_length + padding) < 0)
 381                goto err;
 382
 383        login->rsp_length               = 0;
 384
 385        return 0;
 386
 387err:
 388        if (login->login_complete) {
 389                if (conn->rx_thread && conn->rx_thread_active) {
 390                        send_sig(SIGINT, conn->rx_thread, 1);
 391                        kthread_stop(conn->rx_thread);
 392                }
 393                if (conn->tx_thread && conn->tx_thread_active) {
 394                        send_sig(SIGINT, conn->tx_thread, 1);
 395                        kthread_stop(conn->tx_thread);
 396                }
 397                spin_lock(&iscsit_global->ts_bitmap_lock);
 398                bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
 399                                      get_order(1));
 400                spin_unlock(&iscsit_global->ts_bitmap_lock);
 401        }
 402        return -1;
 403}
 404
 405static void iscsi_target_sk_data_ready(struct sock *sk)
 406{
 407        struct iscsi_conn *conn = sk->sk_user_data;
 408        bool rc;
 409
 410        pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
 411
 412        write_lock_bh(&sk->sk_callback_lock);
 413        if (!sk->sk_user_data) {
 414                write_unlock_bh(&sk->sk_callback_lock);
 415                return;
 416        }
 417        if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
 418                write_unlock_bh(&sk->sk_callback_lock);
 419                pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
 420                return;
 421        }
 422        if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
 423                write_unlock_bh(&sk->sk_callback_lock);
 424                pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
 425                return;
 426        }
 427        if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
 428                write_unlock_bh(&sk->sk_callback_lock);
 429                pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
 430                return;
 431        }
 432
 433        rc = schedule_delayed_work(&conn->login_work, 0);
 434        if (!rc) {
 435                pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
 436                         " got false\n");
 437        }
 438        write_unlock_bh(&sk->sk_callback_lock);
 439}
 440
 441static void iscsi_target_sk_state_change(struct sock *);
 442
 443static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
 444{
 445        struct sock *sk;
 446
 447        if (!conn->sock)
 448                return;
 449
 450        sk = conn->sock->sk;
 451        pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
 452
 453        write_lock_bh(&sk->sk_callback_lock);
 454        sk->sk_user_data = conn;
 455        conn->orig_data_ready = sk->sk_data_ready;
 456        conn->orig_state_change = sk->sk_state_change;
 457        sk->sk_data_ready = iscsi_target_sk_data_ready;
 458        sk->sk_state_change = iscsi_target_sk_state_change;
 459        write_unlock_bh(&sk->sk_callback_lock);
 460
 461        sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
 462        sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
 463}
 464
 465static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
 466{
 467        struct sock *sk;
 468
 469        if (!conn->sock)
 470                return;
 471
 472        sk = conn->sock->sk;
 473        pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
 474
 475        write_lock_bh(&sk->sk_callback_lock);
 476        if (!sk->sk_user_data) {
 477                write_unlock_bh(&sk->sk_callback_lock);
 478                return;
 479        }
 480        sk->sk_user_data = NULL;
 481        sk->sk_data_ready = conn->orig_data_ready;
 482        sk->sk_state_change = conn->orig_state_change;
 483        write_unlock_bh(&sk->sk_callback_lock);
 484
 485        sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
 486        sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
 487}
 488
 489static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
 490
 491static bool iscsi_target_sk_state_check(struct sock *sk)
 492{
 493        if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
 494                pr_debug("iscsi_target_sk_state_check: TCP_CLOSE_WAIT|TCP_CLOSE,"
 495                        "returning FALSE\n");
 496                return false;
 497        }
 498        return true;
 499}
 500
 501static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
 502{
 503        struct iscsi_np *np = login->np;
 504        bool zero_tsih = login->zero_tsih;
 505
 506        iscsi_remove_failed_auth_entry(conn);
 507        iscsi_target_nego_release(conn);
 508        iscsi_target_login_sess_out(conn, np, zero_tsih, true);
 509}
 510
 511static void iscsi_target_login_timeout(unsigned long data)
 512{
 513        struct iscsi_conn *conn = (struct iscsi_conn *)data;
 514
 515        pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
 516
 517        if (conn->login_kworker) {
 518                pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
 519                         conn->login_kworker->comm, conn->login_kworker->pid);
 520                send_sig(SIGINT, conn->login_kworker, 1);
 521        }
 522}
 523
 524static void iscsi_target_do_login_rx(struct work_struct *work)
 525{
 526        struct iscsi_conn *conn = container_of(work,
 527                                struct iscsi_conn, login_work.work);
 528        struct iscsi_login *login = conn->login;
 529        struct iscsi_np *np = login->np;
 530        struct iscsi_portal_group *tpg = conn->tpg;
 531        struct iscsi_tpg_np *tpg_np = conn->tpg_np;
 532        struct timer_list login_timer;
 533        int rc, zero_tsih = login->zero_tsih;
 534        bool state;
 535
 536        pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
 537                        conn, current->comm, current->pid);
 538
 539        spin_lock(&tpg->tpg_state_lock);
 540        state = (tpg->tpg_state == TPG_STATE_ACTIVE);
 541        spin_unlock(&tpg->tpg_state_lock);
 542
 543        if (!state) {
 544                pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
 545                iscsi_target_restore_sock_callbacks(conn);
 546                iscsi_target_login_drop(conn, login);
 547                iscsit_deaccess_np(np, tpg, tpg_np);
 548                return;
 549        }
 550
 551        if (conn->sock) {
 552                struct sock *sk = conn->sock->sk;
 553
 554                read_lock_bh(&sk->sk_callback_lock);
 555                state = iscsi_target_sk_state_check(sk);
 556                read_unlock_bh(&sk->sk_callback_lock);
 557
 558                if (!state) {
 559                        pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
 560                        iscsi_target_restore_sock_callbacks(conn);
 561                        iscsi_target_login_drop(conn, login);
 562                        iscsit_deaccess_np(np, tpg, tpg_np);
 563                        return;
 564                }
 565        }
 566
 567        conn->login_kworker = current;
 568        allow_signal(SIGINT);
 569
 570        init_timer(&login_timer);
 571        login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
 572        login_timer.data = (unsigned long)conn;
 573        login_timer.function = iscsi_target_login_timeout;
 574        add_timer(&login_timer);
 575        pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
 576
 577        rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
 578        del_timer_sync(&login_timer);
 579        flush_signals(current);
 580        conn->login_kworker = NULL;
 581
 582        if (rc < 0) {
 583                iscsi_target_restore_sock_callbacks(conn);
 584                iscsi_target_login_drop(conn, login);
 585                iscsit_deaccess_np(np, tpg, tpg_np);
 586                return;
 587        }
 588
 589        pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
 590                        conn, current->comm, current->pid);
 591
 592        rc = iscsi_target_do_login(conn, login);
 593        if (rc < 0) {
 594                iscsi_target_restore_sock_callbacks(conn);
 595                iscsi_target_login_drop(conn, login);
 596                iscsit_deaccess_np(np, tpg, tpg_np);
 597        } else if (!rc) {
 598                if (conn->sock) {
 599                        struct sock *sk = conn->sock->sk;
 600
 601                        write_lock_bh(&sk->sk_callback_lock);
 602                        clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
 603                        write_unlock_bh(&sk->sk_callback_lock);
 604                }
 605        } else if (rc == 1) {
 606                iscsi_target_nego_release(conn);
 607                iscsi_post_login_handler(np, conn, zero_tsih);
 608                iscsit_deaccess_np(np, tpg, tpg_np);
 609        }
 610}
 611
 612static void iscsi_target_do_cleanup(struct work_struct *work)
 613{
 614        struct iscsi_conn *conn = container_of(work,
 615                                struct iscsi_conn, login_cleanup_work.work);
 616        struct sock *sk = conn->sock->sk;
 617        struct iscsi_login *login = conn->login;
 618        struct iscsi_np *np = login->np;
 619        struct iscsi_portal_group *tpg = conn->tpg;
 620        struct iscsi_tpg_np *tpg_np = conn->tpg_np;
 621
 622        pr_debug("Entering iscsi_target_do_cleanup\n");
 623
 624        cancel_delayed_work_sync(&conn->login_work);
 625        conn->orig_state_change(sk);
 626
 627        iscsi_target_restore_sock_callbacks(conn);
 628        iscsi_target_login_drop(conn, login);
 629        iscsit_deaccess_np(np, tpg, tpg_np);
 630
 631        pr_debug("iscsi_target_do_cleanup done()\n");
 632}
 633
 634static void iscsi_target_sk_state_change(struct sock *sk)
 635{
 636        struct iscsi_conn *conn;
 637        void (*orig_state_change)(struct sock *);
 638        bool state;
 639
 640        pr_debug("Entering iscsi_target_sk_state_change\n");
 641
 642        write_lock_bh(&sk->sk_callback_lock);
 643        conn = sk->sk_user_data;
 644        if (!conn) {
 645                write_unlock_bh(&sk->sk_callback_lock);
 646                return;
 647        }
 648        orig_state_change = conn->orig_state_change;
 649
 650        if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
 651                pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
 652                         conn);
 653                write_unlock_bh(&sk->sk_callback_lock);
 654                orig_state_change(sk);
 655                return;
 656        }
 657        if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
 658                pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
 659                         " conn: %p\n", conn);
 660                write_unlock_bh(&sk->sk_callback_lock);
 661                orig_state_change(sk);
 662                return;
 663        }
 664        if (test_and_set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
 665                pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
 666                         conn);
 667                write_unlock_bh(&sk->sk_callback_lock);
 668                orig_state_change(sk);
 669                return;
 670        }
 671
 672        state = iscsi_target_sk_state_check(sk);
 673        write_unlock_bh(&sk->sk_callback_lock);
 674
 675        pr_debug("iscsi_target_sk_state_change: state: %d\n", state);
 676
 677        if (!state) {
 678                pr_debug("iscsi_target_sk_state_change got failed state\n");
 679                schedule_delayed_work(&conn->login_cleanup_work, 0);
 680                return;
 681        }
 682        orig_state_change(sk);
 683}
 684
 685/*
 686 *      NOTE: We check for existing sessions or connections AFTER the initiator
 687 *      has been successfully authenticated in order to protect against faked
 688 *      ISID/TSIH combinations.
 689 */
 690static int iscsi_target_check_for_existing_instances(
 691        struct iscsi_conn *conn,
 692        struct iscsi_login *login)
 693{
 694        if (login->checked_for_existing)
 695                return 0;
 696
 697        login->checked_for_existing = 1;
 698
 699        if (!login->tsih)
 700                return iscsi_check_for_session_reinstatement(conn);
 701        else
 702                return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
 703                                login->initial_exp_statsn);
 704}
 705
 706static int iscsi_target_do_authentication(
 707        struct iscsi_conn *conn,
 708        struct iscsi_login *login)
 709{
 710        int authret;
 711        u32 payload_length;
 712        struct iscsi_param *param;
 713        struct iscsi_login_req *login_req;
 714        struct iscsi_login_rsp *login_rsp;
 715
 716        login_req = (struct iscsi_login_req *) login->req;
 717        login_rsp = (struct iscsi_login_rsp *) login->rsp;
 718        payload_length = ntoh24(login_req->dlength);
 719
 720        param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
 721        if (!param)
 722                return -1;
 723
 724        authret = iscsi_handle_authentication(
 725                        conn,
 726                        login->req_buf,
 727                        login->rsp_buf,
 728                        payload_length,
 729                        &login->rsp_length,
 730                        param->value);
 731        switch (authret) {
 732        case 0:
 733                pr_debug("Received OK response"
 734                " from LIO Authentication, continuing.\n");
 735                break;
 736        case 1:
 737                pr_debug("iSCSI security negotiation"
 738                        " completed successfully.\n");
 739                login->auth_complete = 1;
 740                if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
 741                    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
 742                        login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
 743                                             ISCSI_FLAG_LOGIN_TRANSIT);
 744                        login->current_stage = 1;
 745                }
 746                return iscsi_target_check_for_existing_instances(
 747                                conn, login);
 748        case 2:
 749                pr_err("Security negotiation"
 750                        " failed.\n");
 751                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 752                                ISCSI_LOGIN_STATUS_AUTH_FAILED);
 753                return -1;
 754        default:
 755                pr_err("Received unknown error %d from LIO"
 756                                " Authentication\n", authret);
 757                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 758                                ISCSI_LOGIN_STATUS_TARGET_ERROR);
 759                return -1;
 760        }
 761
 762        return 0;
 763}
 764
 765static int iscsi_target_handle_csg_zero(
 766        struct iscsi_conn *conn,
 767        struct iscsi_login *login)
 768{
 769        int ret;
 770        u32 payload_length;
 771        struct iscsi_param *param;
 772        struct iscsi_login_req *login_req;
 773        struct iscsi_login_rsp *login_rsp;
 774
 775        login_req = (struct iscsi_login_req *) login->req;
 776        login_rsp = (struct iscsi_login_rsp *) login->rsp;
 777        payload_length = ntoh24(login_req->dlength);
 778
 779        param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
 780        if (!param)
 781                return -1;
 782
 783        ret = iscsi_decode_text_input(
 784                        PHASE_SECURITY|PHASE_DECLARATIVE,
 785                        SENDER_INITIATOR|SENDER_RECEIVER,
 786                        login->req_buf,
 787                        payload_length,
 788                        conn);
 789        if (ret < 0)
 790                return -1;
 791
 792        if (ret > 0) {
 793                if (login->auth_complete) {
 794                        pr_err("Initiator has already been"
 795                                " successfully authenticated, but is still"
 796                                " sending %s keys.\n", param->value);
 797                        iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 798                                        ISCSI_LOGIN_STATUS_INIT_ERR);
 799                        return -1;
 800                }
 801
 802                goto do_auth;
 803        } else if (!payload_length) {
 804                pr_err("Initiator sent zero length security payload,"
 805                       " login failed\n");
 806                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 807                                    ISCSI_LOGIN_STATUS_AUTH_FAILED);
 808                return -1;
 809        }
 810
 811        if (login->first_request)
 812                if (iscsi_target_check_first_request(conn, login) < 0)
 813                        return -1;
 814
 815        ret = iscsi_encode_text_output(
 816                        PHASE_SECURITY|PHASE_DECLARATIVE,
 817                        SENDER_TARGET,
 818                        login->rsp_buf,
 819                        &login->rsp_length,
 820                        conn->param_list);
 821        if (ret < 0)
 822                return -1;
 823
 824        if (!iscsi_check_negotiated_keys(conn->param_list)) {
 825                if (conn->tpg->tpg_attrib.authentication &&
 826                    !strncmp(param->value, NONE, 4)) {
 827                        pr_err("Initiator sent AuthMethod=None but"
 828                                " Target is enforcing iSCSI Authentication,"
 829                                        " login failed.\n");
 830                        iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 831                                        ISCSI_LOGIN_STATUS_AUTH_FAILED);
 832                        return -1;
 833                }
 834
 835                if (conn->tpg->tpg_attrib.authentication &&
 836                    !login->auth_complete)
 837                        return 0;
 838
 839                if (strncmp(param->value, NONE, 4) && !login->auth_complete)
 840                        return 0;
 841
 842                if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
 843                    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
 844                        login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
 845                                            ISCSI_FLAG_LOGIN_TRANSIT;
 846                        login->current_stage = 1;
 847                }
 848        }
 849
 850        return 0;
 851do_auth:
 852        return iscsi_target_do_authentication(conn, login);
 853}
 854
 855static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
 856{
 857        int ret;
 858        u32 payload_length;
 859        struct iscsi_login_req *login_req;
 860        struct iscsi_login_rsp *login_rsp;
 861
 862        login_req = (struct iscsi_login_req *) login->req;
 863        login_rsp = (struct iscsi_login_rsp *) login->rsp;
 864        payload_length = ntoh24(login_req->dlength);
 865
 866        ret = iscsi_decode_text_input(
 867                        PHASE_OPERATIONAL|PHASE_DECLARATIVE,
 868                        SENDER_INITIATOR|SENDER_RECEIVER,
 869                        login->req_buf,
 870                        payload_length,
 871                        conn);
 872        if (ret < 0) {
 873                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 874                                ISCSI_LOGIN_STATUS_INIT_ERR);
 875                return -1;
 876        }
 877
 878        if (login->first_request)
 879                if (iscsi_target_check_first_request(conn, login) < 0)
 880                        return -1;
 881
 882        if (iscsi_target_check_for_existing_instances(conn, login) < 0)
 883                return -1;
 884
 885        ret = iscsi_encode_text_output(
 886                        PHASE_OPERATIONAL|PHASE_DECLARATIVE,
 887                        SENDER_TARGET,
 888                        login->rsp_buf,
 889                        &login->rsp_length,
 890                        conn->param_list);
 891        if (ret < 0) {
 892                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 893                                ISCSI_LOGIN_STATUS_INIT_ERR);
 894                return -1;
 895        }
 896
 897        if (!login->auth_complete &&
 898             conn->tpg->tpg_attrib.authentication) {
 899                pr_err("Initiator is requesting CSG: 1, has not been"
 900                         " successfully authenticated, and the Target is"
 901                        " enforcing iSCSI Authentication, login failed.\n");
 902                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
 903                                ISCSI_LOGIN_STATUS_AUTH_FAILED);
 904                return -1;
 905        }
 906
 907        if (!iscsi_check_negotiated_keys(conn->param_list))
 908                if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
 909                    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
 910                        login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
 911                                            ISCSI_FLAG_LOGIN_TRANSIT;
 912
 913        return 0;
 914}
 915
 916static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
 917{
 918        int pdu_count = 0;
 919        struct iscsi_login_req *login_req;
 920        struct iscsi_login_rsp *login_rsp;
 921
 922        login_req = (struct iscsi_login_req *) login->req;
 923        login_rsp = (struct iscsi_login_rsp *) login->rsp;
 924
 925        while (1) {
 926                if (++pdu_count > MAX_LOGIN_PDUS) {
 927                        pr_err("MAX_LOGIN_PDUS count reached.\n");
 928                        iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
 929                                        ISCSI_LOGIN_STATUS_TARGET_ERROR);
 930                        return -1;
 931                }
 932
 933                switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
 934                case 0:
 935                        login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
 936                        if (iscsi_target_handle_csg_zero(conn, login) < 0)
 937                                return -1;
 938                        break;
 939                case 1:
 940                        login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
 941                        if (iscsi_target_handle_csg_one(conn, login) < 0)
 942                                return -1;
 943                        if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
 944                                login->tsih = conn->sess->tsih;
 945                                login->login_complete = 1;
 946                                iscsi_target_restore_sock_callbacks(conn);
 947                                if (iscsi_target_do_tx_login_io(conn,
 948                                                login) < 0)
 949                                        return -1;
 950                                return 1;
 951                        }
 952                        break;
 953                default:
 954                        pr_err("Illegal CSG: %d received from"
 955                                " Initiator, protocol error.\n",
 956                                ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
 957                        break;
 958                }
 959
 960                if (iscsi_target_do_tx_login_io(conn, login) < 0)
 961                        return -1;
 962
 963                if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
 964                        login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
 965                        login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
 966                }
 967                break;
 968        }
 969
 970        if (conn->sock) {
 971                struct sock *sk = conn->sock->sk;
 972                bool state;
 973
 974                read_lock_bh(&sk->sk_callback_lock);
 975                state = iscsi_target_sk_state_check(sk);
 976                read_unlock_bh(&sk->sk_callback_lock);
 977
 978                if (!state) {
 979                        pr_debug("iscsi_target_do_login() failed state for"
 980                                 " conn: %p\n", conn);
 981                        return -1;
 982                }
 983        }
 984
 985        return 0;
 986}
 987
 988static void iscsi_initiatorname_tolower(
 989        char *param_buf)
 990{
 991        char *c;
 992        u32 iqn_size = strlen(param_buf), i;
 993
 994        for (i = 0; i < iqn_size; i++) {
 995                c = &param_buf[i];
 996                if (!isupper(*c))
 997                        continue;
 998
 999                *c = tolower(*c);
1000        }
1001}
1002
1003/*
1004 * Processes the first Login Request..
1005 */
1006int iscsi_target_locate_portal(
1007        struct iscsi_np *np,
1008        struct iscsi_conn *conn,
1009        struct iscsi_login *login)
1010{
1011        char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1012        char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
1013        struct iscsi_session *sess = conn->sess;
1014        struct iscsi_tiqn *tiqn;
1015        struct iscsi_tpg_np *tpg_np = NULL;
1016        struct iscsi_login_req *login_req;
1017        struct se_node_acl *se_nacl;
1018        u32 payload_length, queue_depth = 0;
1019        int sessiontype = 0, ret = 0, tag_num, tag_size;
1020
1021        INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
1022        INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup);
1023        iscsi_target_set_sock_callbacks(conn);
1024
1025        login->np = np;
1026
1027        login_req = (struct iscsi_login_req *) login->req;
1028        payload_length = ntoh24(login_req->dlength);
1029
1030        tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1031        if (!tmpbuf) {
1032                pr_err("Unable to allocate memory for tmpbuf.\n");
1033                return -1;
1034        }
1035
1036        memcpy(tmpbuf, login->req_buf, payload_length);
1037        tmpbuf[payload_length] = '\0';
1038        start = tmpbuf;
1039        end = (start + payload_length);
1040
1041        /*
1042         * Locate the initial keys expected from the Initiator node in
1043         * the first login request in order to progress with the login phase.
1044         */
1045        while (start < end) {
1046                if (iscsi_extract_key_value(start, &key, &value) < 0) {
1047                        ret = -1;
1048                        goto out;
1049                }
1050
1051                if (!strncmp(key, "InitiatorName", 13))
1052                        i_buf = value;
1053                else if (!strncmp(key, "SessionType", 11))
1054                        s_buf = value;
1055                else if (!strncmp(key, "TargetName", 10))
1056                        t_buf = value;
1057
1058                start += strlen(key) + strlen(value) + 2;
1059        }
1060        /*
1061         * See 5.3.  Login Phase.
1062         */
1063        if (!i_buf) {
1064                pr_err("InitiatorName key not received"
1065                        " in first login request.\n");
1066                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1067                        ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1068                ret = -1;
1069                goto out;
1070        }
1071        /*
1072         * Convert the incoming InitiatorName to lowercase following
1073         * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1074         * are NOT case sensitive.
1075         */
1076        iscsi_initiatorname_tolower(i_buf);
1077
1078        if (!s_buf) {
1079                if (!login->leading_connection)
1080                        goto get_target;
1081
1082                pr_err("SessionType key not received"
1083                        " in first login request.\n");
1084                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1085                        ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1086                ret = -1;
1087                goto out;
1088        }
1089
1090        /*
1091         * Use default portal group for discovery sessions.
1092         */
1093        sessiontype = strncmp(s_buf, DISCOVERY, 9);
1094        if (!sessiontype) {
1095                conn->tpg = iscsit_global->discovery_tpg;
1096                if (!login->leading_connection)
1097                        goto get_target;
1098
1099                sess->sess_ops->SessionType = 1;
1100                /*
1101                 * Setup crc32c modules from libcrypto
1102                 */
1103                if (iscsi_login_setup_crypto(conn) < 0) {
1104                        pr_err("iscsi_login_setup_crypto() failed\n");
1105                        ret = -1;
1106                        goto out;
1107                }
1108                /*
1109                 * Serialize access across the discovery struct iscsi_portal_group to
1110                 * process login attempt.
1111                 */
1112                if (iscsit_access_np(np, conn->tpg) < 0) {
1113                        iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1114                                ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1115                        ret = -1;
1116                        goto out;
1117                }
1118                ret = 0;
1119                goto alloc_tags;
1120        }
1121
1122get_target:
1123        if (!t_buf) {
1124                pr_err("TargetName key not received"
1125                        " in first login request while"
1126                        " SessionType=Normal.\n");
1127                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1128                        ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1129                ret = -1;
1130                goto out;
1131        }
1132
1133        /*
1134         * Locate Target IQN from Storage Node.
1135         */
1136        tiqn = iscsit_get_tiqn_for_login(t_buf);
1137        if (!tiqn) {
1138                pr_err("Unable to locate Target IQN: %s in"
1139                        " Storage Node\n", t_buf);
1140                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1141                                ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1142                ret = -1;
1143                goto out;
1144        }
1145        pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1146
1147        /*
1148         * Locate Target Portal Group from Storage Node.
1149         */
1150        conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1151        if (!conn->tpg) {
1152                pr_err("Unable to locate Target Portal Group"
1153                                " on %s\n", tiqn->tiqn);
1154                iscsit_put_tiqn_for_login(tiqn);
1155                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1156                                ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1157                ret = -1;
1158                goto out;
1159        }
1160        conn->tpg_np = tpg_np;
1161        pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1162        /*
1163         * Setup crc32c modules from libcrypto
1164         */
1165        if (iscsi_login_setup_crypto(conn) < 0) {
1166                pr_err("iscsi_login_setup_crypto() failed\n");
1167                kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1168                iscsit_put_tiqn_for_login(tiqn);
1169                conn->tpg = NULL;
1170                ret = -1;
1171                goto out;
1172        }
1173        /*
1174         * Serialize access across the struct iscsi_portal_group to
1175         * process login attempt.
1176         */
1177        if (iscsit_access_np(np, conn->tpg) < 0) {
1178                kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1179                iscsit_put_tiqn_for_login(tiqn);
1180                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1181                                ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1182                conn->tpg = NULL;
1183                ret = -1;
1184                goto out;
1185        }
1186
1187        /*
1188         * conn->sess->node_acl will be set when the referenced
1189         * struct iscsi_session is located from received ISID+TSIH in
1190         * iscsi_login_non_zero_tsih_s2().
1191         */
1192        if (!login->leading_connection) {
1193                ret = 0;
1194                goto out;
1195        }
1196
1197        /*
1198         * This value is required in iscsi_login_zero_tsih_s2()
1199         */
1200        sess->sess_ops->SessionType = 0;
1201
1202        /*
1203         * Locate incoming Initiator IQN reference from Storage Node.
1204         */
1205        sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1206                        &conn->tpg->tpg_se_tpg, i_buf);
1207        if (!sess->se_sess->se_node_acl) {
1208                pr_err("iSCSI Initiator Node: %s is not authorized to"
1209                        " access iSCSI target portal group: %hu.\n",
1210                                i_buf, conn->tpg->tpgt);
1211                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1212                                ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1213                ret = -1;
1214                goto out;
1215        }
1216        se_nacl = sess->se_sess->se_node_acl;
1217        queue_depth = se_nacl->queue_depth;
1218        /*
1219         * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1220         * depth for non immediate commands, plus extra tags for immediate
1221         * commands.
1222         *
1223         * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1224         * in per-cpu-ida tag allocation logic + small queue_depth.
1225         */
1226alloc_tags:
1227        tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
1228        tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
1229        tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
1230
1231        ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1232        if (ret < 0) {
1233                iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1234                                    ISCSI_LOGIN_STATUS_NO_RESOURCES);
1235                ret = -1;
1236        }
1237out:
1238        kfree(tmpbuf);
1239        return ret;
1240}
1241
1242int iscsi_target_start_negotiation(
1243        struct iscsi_login *login,
1244        struct iscsi_conn *conn)
1245{
1246        int ret;
1247
1248        ret = iscsi_target_do_login(conn, login);
1249        if (!ret) {
1250                if (conn->sock) {
1251                        struct sock *sk = conn->sock->sk;
1252
1253                        write_lock_bh(&sk->sk_callback_lock);
1254                        set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1255                        write_unlock_bh(&sk->sk_callback_lock);
1256                }
1257        } else if (ret < 0) {
1258                cancel_delayed_work_sync(&conn->login_work);
1259                cancel_delayed_work_sync(&conn->login_cleanup_work);
1260                iscsi_target_restore_sock_callbacks(conn);
1261                iscsi_remove_failed_auth_entry(conn);
1262        }
1263        if (ret != 0)
1264                iscsi_target_nego_release(conn);
1265
1266        return ret;
1267}
1268
1269void iscsi_target_nego_release(struct iscsi_conn *conn)
1270{
1271        struct iscsi_login *login = conn->conn_login;
1272
1273        if (!login)
1274                return;
1275
1276        kfree(login->req_buf);
1277        kfree(login->rsp_buf);
1278        kfree(login);
1279
1280        conn->conn_login = NULL;
1281}
1282