1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2e48354ceSNicholas Bellinger /******************************************************************************* 3e48354ceSNicholas Bellinger * This file contains main functions related to iSCSI Parameter negotiation. 4e48354ceSNicholas Bellinger * 54c76251eSNicholas Bellinger * (c) Copyright 2007-2013 Datera, Inc. 6e48354ceSNicholas Bellinger * 7e48354ceSNicholas Bellinger * Author: Nicholas A. Bellinger <nab@linux-iscsi.org> 8e48354ceSNicholas Bellinger * 9e48354ceSNicholas Bellinger ******************************************************************************/ 10e48354ceSNicholas Bellinger 11e48354ceSNicholas Bellinger #include <linux/ctype.h> 12e5419865SNicholas Bellinger #include <linux/kthread.h> 138dcf07beSBart Van Assche #include <linux/slab.h> 143f07c014SIngo Molnar #include <linux/sched/signal.h> 158dcf07beSBart Van Assche #include <net/sock.h> 16e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h> 17e48354ceSNicholas Bellinger #include <target/target_core_base.h> 18c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h> 19baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h> 20e48354ceSNicholas Bellinger 2167f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h> 22e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h" 23e48354ceSNicholas Bellinger #include "iscsi_target_login.h" 24e48354ceSNicholas Bellinger #include "iscsi_target_nego.h" 25e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h" 26e48354ceSNicholas Bellinger #include "iscsi_target_util.h" 27e48354ceSNicholas Bellinger #include "iscsi_target.h" 28e48354ceSNicholas Bellinger #include "iscsi_target_auth.h" 29e48354ceSNicholas Bellinger 30e48354ceSNicholas Bellinger #define MAX_LOGIN_PDUS 7 31e48354ceSNicholas Bellinger #define TEXT_LEN 4096 32e48354ceSNicholas Bellinger 33e48354ceSNicholas Bellinger void convert_null_to_semi(char *buf, int len) 34e48354ceSNicholas Bellinger { 35e48354ceSNicholas Bellinger int i; 36e48354ceSNicholas Bellinger 37e48354ceSNicholas Bellinger for (i = 0; i < len; i++) 38e48354ceSNicholas Bellinger if (buf[i] == '\0') 39e48354ceSNicholas Bellinger buf[i] = ';'; 40e48354ceSNicholas Bellinger } 41e48354ceSNicholas Bellinger 42fceb5bc7SChristoph Hellwig static int strlen_semi(char *buf) 43e48354ceSNicholas Bellinger { 44e48354ceSNicholas Bellinger int i = 0; 45e48354ceSNicholas Bellinger 46e48354ceSNicholas Bellinger while (buf[i] != '\0') { 47e48354ceSNicholas Bellinger if (buf[i] == ';') 48e48354ceSNicholas Bellinger return i; 49e48354ceSNicholas Bellinger i++; 50e48354ceSNicholas Bellinger } 51e48354ceSNicholas Bellinger 52e48354ceSNicholas Bellinger return -1; 53e48354ceSNicholas Bellinger } 54e48354ceSNicholas Bellinger 55e48354ceSNicholas Bellinger int extract_param( 56e48354ceSNicholas Bellinger const char *in_buf, 57e48354ceSNicholas Bellinger const char *pattern, 58e48354ceSNicholas Bellinger unsigned int max_length, 59e48354ceSNicholas Bellinger char *out_buf, 60e48354ceSNicholas Bellinger unsigned char *type) 61e48354ceSNicholas Bellinger { 62e48354ceSNicholas Bellinger char *ptr; 63e48354ceSNicholas Bellinger int len; 64e48354ceSNicholas Bellinger 65e48354ceSNicholas Bellinger if (!in_buf || !pattern || !out_buf || !type) 66e48354ceSNicholas Bellinger return -1; 67e48354ceSNicholas Bellinger 68e48354ceSNicholas Bellinger ptr = strstr(in_buf, pattern); 69e48354ceSNicholas Bellinger if (!ptr) 70e48354ceSNicholas Bellinger return -1; 71e48354ceSNicholas Bellinger 72e48354ceSNicholas Bellinger ptr = strstr(ptr, "="); 73e48354ceSNicholas Bellinger if (!ptr) 74e48354ceSNicholas Bellinger return -1; 75e48354ceSNicholas Bellinger 76e48354ceSNicholas Bellinger ptr += 1; 77e48354ceSNicholas Bellinger if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) { 78e48354ceSNicholas Bellinger ptr += 2; /* skip 0x */ 79e48354ceSNicholas Bellinger *type = HEX; 80e48354ceSNicholas Bellinger } else 81e48354ceSNicholas Bellinger *type = DECIMAL; 82e48354ceSNicholas Bellinger 83e48354ceSNicholas Bellinger len = strlen_semi(ptr); 84e48354ceSNicholas Bellinger if (len < 0) 85e48354ceSNicholas Bellinger return -1; 86e48354ceSNicholas Bellinger 87369653e4SEric Seppanen if (len >= max_length) { 885e58b029SMasanari Iida pr_err("Length of input: %d exceeds max_length:" 89e48354ceSNicholas Bellinger " %d\n", len, max_length); 90e48354ceSNicholas Bellinger return -1; 91e48354ceSNicholas Bellinger } 92e48354ceSNicholas Bellinger memcpy(out_buf, ptr, len); 93e48354ceSNicholas Bellinger out_buf[len] = '\0'; 94e48354ceSNicholas Bellinger 95e48354ceSNicholas Bellinger return 0; 96e48354ceSNicholas Bellinger } 97e48354ceSNicholas Bellinger 98e48354ceSNicholas Bellinger static u32 iscsi_handle_authentication( 99e48354ceSNicholas Bellinger struct iscsi_conn *conn, 100e48354ceSNicholas Bellinger char *in_buf, 101e48354ceSNicholas Bellinger char *out_buf, 102e48354ceSNicholas Bellinger int in_length, 103e48354ceSNicholas Bellinger int *out_length, 104e48354ceSNicholas Bellinger unsigned char *authtype) 105e48354ceSNicholas Bellinger { 106e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 107e48354ceSNicholas Bellinger struct iscsi_node_auth *auth; 108e48354ceSNicholas Bellinger struct iscsi_node_acl *iscsi_nacl; 109c3e51442SNicholas Bellinger struct iscsi_portal_group *iscsi_tpg; 110e48354ceSNicholas Bellinger struct se_node_acl *se_nacl; 111e48354ceSNicholas Bellinger 112e48354ceSNicholas Bellinger if (!sess->sess_ops->SessionType) { 113e48354ceSNicholas Bellinger /* 114e48354ceSNicholas Bellinger * For SessionType=Normal 115e48354ceSNicholas Bellinger */ 116e48354ceSNicholas Bellinger se_nacl = conn->sess->se_sess->se_node_acl; 117e48354ceSNicholas Bellinger if (!se_nacl) { 118e48354ceSNicholas Bellinger pr_err("Unable to locate struct se_node_acl for" 119e48354ceSNicholas Bellinger " CHAP auth\n"); 120e48354ceSNicholas Bellinger return -1; 121e48354ceSNicholas Bellinger } 122e48354ceSNicholas Bellinger iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl, 123e48354ceSNicholas Bellinger se_node_acl); 124e48354ceSNicholas Bellinger if (!iscsi_nacl) { 125e48354ceSNicholas Bellinger pr_err("Unable to locate struct iscsi_node_acl for" 126e48354ceSNicholas Bellinger " CHAP auth\n"); 127e48354ceSNicholas Bellinger return -1; 128e48354ceSNicholas Bellinger } 129e48354ceSNicholas Bellinger 130c3e51442SNicholas Bellinger if (se_nacl->dynamic_node_acl) { 131c3e51442SNicholas Bellinger iscsi_tpg = container_of(se_nacl->se_tpg, 132c3e51442SNicholas Bellinger struct iscsi_portal_group, tpg_se_tpg); 133c3e51442SNicholas Bellinger 134c3e51442SNicholas Bellinger auth = &iscsi_tpg->tpg_demo_auth; 135c3e51442SNicholas Bellinger } else { 136c3e51442SNicholas Bellinger iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl, 137c3e51442SNicholas Bellinger se_node_acl); 138c3e51442SNicholas Bellinger 139b7eec2cdSAndy Grover auth = &iscsi_nacl->node_auth; 140c3e51442SNicholas Bellinger } 141e48354ceSNicholas Bellinger } else { 142e48354ceSNicholas Bellinger /* 143e48354ceSNicholas Bellinger * For SessionType=Discovery 144e48354ceSNicholas Bellinger */ 145e48354ceSNicholas Bellinger auth = &iscsit_global->discovery_acl.node_auth; 146e48354ceSNicholas Bellinger } 147e48354ceSNicholas Bellinger 148e48354ceSNicholas Bellinger if (strstr("CHAP", authtype)) 149e48354ceSNicholas Bellinger strcpy(conn->sess->auth_type, "CHAP"); 150e48354ceSNicholas Bellinger else 151e48354ceSNicholas Bellinger strcpy(conn->sess->auth_type, NONE); 152e48354ceSNicholas Bellinger 153e48354ceSNicholas Bellinger if (strstr("None", authtype)) 154e48354ceSNicholas Bellinger return 1; 155e48354ceSNicholas Bellinger #ifdef CANSRP 156e48354ceSNicholas Bellinger else if (strstr("SRP", authtype)) 157e48354ceSNicholas Bellinger return srp_main_loop(conn, auth, in_buf, out_buf, 158e48354ceSNicholas Bellinger &in_length, out_length); 159e48354ceSNicholas Bellinger #endif 160e48354ceSNicholas Bellinger else if (strstr("CHAP", authtype)) 161e48354ceSNicholas Bellinger return chap_main_loop(conn, auth, in_buf, out_buf, 162e48354ceSNicholas Bellinger &in_length, out_length); 163e48354ceSNicholas Bellinger else if (strstr("SPKM1", authtype)) 164e48354ceSNicholas Bellinger return 2; 165e48354ceSNicholas Bellinger else if (strstr("SPKM2", authtype)) 166e48354ceSNicholas Bellinger return 2; 167e48354ceSNicholas Bellinger else if (strstr("KRB5", authtype)) 168e48354ceSNicholas Bellinger return 2; 169e48354ceSNicholas Bellinger else 170e48354ceSNicholas Bellinger return 2; 171e48354ceSNicholas Bellinger } 172e48354ceSNicholas Bellinger 173e48354ceSNicholas Bellinger static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn) 174e48354ceSNicholas Bellinger { 175e48354ceSNicholas Bellinger kfree(conn->auth_protocol); 176e48354ceSNicholas Bellinger } 177e48354ceSNicholas Bellinger 178baa4d64bSNicholas Bellinger int iscsi_target_check_login_request( 179e48354ceSNicholas Bellinger struct iscsi_conn *conn, 180e48354ceSNicholas Bellinger struct iscsi_login *login) 181e48354ceSNicholas Bellinger { 18228168905SJörn Engel int req_csg, req_nsg; 183e48354ceSNicholas Bellinger u32 payload_length; 184e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 185e48354ceSNicholas Bellinger 186e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 187e48354ceSNicholas Bellinger payload_length = ntoh24(login_req->dlength); 188e48354ceSNicholas Bellinger 189e48354ceSNicholas Bellinger switch (login_req->opcode & ISCSI_OPCODE_MASK) { 190e48354ceSNicholas Bellinger case ISCSI_OP_LOGIN: 191e48354ceSNicholas Bellinger break; 192e48354ceSNicholas Bellinger default: 193e48354ceSNicholas Bellinger pr_err("Received unknown opcode 0x%02x.\n", 194e48354ceSNicholas Bellinger login_req->opcode & ISCSI_OPCODE_MASK); 195e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 196e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_INIT_ERR); 197e48354ceSNicholas Bellinger return -1; 198e48354ceSNicholas Bellinger } 199e48354ceSNicholas Bellinger 200e48354ceSNicholas Bellinger if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) && 201e48354ceSNicholas Bellinger (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) { 202e48354ceSNicholas Bellinger pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE" 203e48354ceSNicholas Bellinger " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n"); 204e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 205e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_INIT_ERR); 206e48354ceSNicholas Bellinger return -1; 207e48354ceSNicholas Bellinger } 208e48354ceSNicholas Bellinger 2095d358065SAndy Grover req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags); 2105d358065SAndy Grover req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags); 211e48354ceSNicholas Bellinger 212e48354ceSNicholas Bellinger if (req_csg != login->current_stage) { 213e48354ceSNicholas Bellinger pr_err("Initiator unexpectedly changed login stage" 214e48354ceSNicholas Bellinger " from %d to %d, login failed.\n", login->current_stage, 215e48354ceSNicholas Bellinger req_csg); 216e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 217e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_INIT_ERR); 218e48354ceSNicholas Bellinger return -1; 219e48354ceSNicholas Bellinger } 220e48354ceSNicholas Bellinger 221e48354ceSNicholas Bellinger if ((req_nsg == 2) || (req_csg >= 2) || 222e48354ceSNicholas Bellinger ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) && 223e48354ceSNicholas Bellinger (req_nsg <= req_csg))) { 224e48354ceSNicholas Bellinger pr_err("Illegal login_req->flags Combination, CSG: %d," 225e48354ceSNicholas Bellinger " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg, 226e48354ceSNicholas Bellinger req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)); 227e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 228e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_INIT_ERR); 229e48354ceSNicholas Bellinger return -1; 230e48354ceSNicholas Bellinger } 231e48354ceSNicholas Bellinger 232e48354ceSNicholas Bellinger if ((login_req->max_version != login->version_max) || 233e48354ceSNicholas Bellinger (login_req->min_version != login->version_min)) { 234e48354ceSNicholas Bellinger pr_err("Login request changed Version Max/Nin" 235e48354ceSNicholas Bellinger " unexpectedly to 0x%02x/0x%02x, protocol error\n", 236e48354ceSNicholas Bellinger login_req->max_version, login_req->min_version); 237e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 238e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_INIT_ERR); 239e48354ceSNicholas Bellinger return -1; 240e48354ceSNicholas Bellinger } 241e48354ceSNicholas Bellinger 242e48354ceSNicholas Bellinger if (memcmp(login_req->isid, login->isid, 6) != 0) { 243e48354ceSNicholas Bellinger pr_err("Login request changed ISID unexpectedly," 244e48354ceSNicholas Bellinger " protocol error.\n"); 245e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 246e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_INIT_ERR); 247e48354ceSNicholas Bellinger return -1; 248e48354ceSNicholas Bellinger } 249e48354ceSNicholas Bellinger 250e48354ceSNicholas Bellinger if (login_req->itt != login->init_task_tag) { 251e48354ceSNicholas Bellinger pr_err("Login request changed ITT unexpectedly to" 252e48354ceSNicholas Bellinger " 0x%08x, protocol error.\n", login_req->itt); 253e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 254e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_INIT_ERR); 255e48354ceSNicholas Bellinger return -1; 256e48354ceSNicholas Bellinger } 257e48354ceSNicholas Bellinger 258e48354ceSNicholas Bellinger if (payload_length > MAX_KEY_VALUE_PAIRS) { 259e48354ceSNicholas Bellinger pr_err("Login request payload exceeds default" 260e48354ceSNicholas Bellinger " MaxRecvDataSegmentLength: %u, protocol error.\n", 261e48354ceSNicholas Bellinger MAX_KEY_VALUE_PAIRS); 262e48354ceSNicholas Bellinger return -1; 263e48354ceSNicholas Bellinger } 264e48354ceSNicholas Bellinger 265e48354ceSNicholas Bellinger return 0; 266e48354ceSNicholas Bellinger } 267d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsi_target_check_login_request); 268e48354ceSNicholas Bellinger 269e48354ceSNicholas Bellinger static int iscsi_target_check_first_request( 270e48354ceSNicholas Bellinger struct iscsi_conn *conn, 271e48354ceSNicholas Bellinger struct iscsi_login *login) 272e48354ceSNicholas Bellinger { 273e48354ceSNicholas Bellinger struct iscsi_param *param = NULL; 274e48354ceSNicholas Bellinger struct se_node_acl *se_nacl; 275e48354ceSNicholas Bellinger 276e48354ceSNicholas Bellinger login->first_request = 0; 277e48354ceSNicholas Bellinger 278e48354ceSNicholas Bellinger list_for_each_entry(param, &conn->param_list->param_list, p_list) { 279e48354ceSNicholas Bellinger if (!strncmp(param->name, SESSIONTYPE, 11)) { 280e48354ceSNicholas Bellinger if (!IS_PSTATE_ACCEPTOR(param)) { 281e48354ceSNicholas Bellinger pr_err("SessionType key not received" 282e48354ceSNicholas Bellinger " in first login request.\n"); 283e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 284e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_MISSING_FIELDS); 285e48354ceSNicholas Bellinger return -1; 286e48354ceSNicholas Bellinger } 287e48354ceSNicholas Bellinger if (!strncmp(param->value, DISCOVERY, 9)) 288e48354ceSNicholas Bellinger return 0; 289e48354ceSNicholas Bellinger } 290e48354ceSNicholas Bellinger 291e48354ceSNicholas Bellinger if (!strncmp(param->name, INITIATORNAME, 13)) { 292e48354ceSNicholas Bellinger if (!IS_PSTATE_ACCEPTOR(param)) { 293e48354ceSNicholas Bellinger if (!login->leading_connection) 294e48354ceSNicholas Bellinger continue; 295e48354ceSNicholas Bellinger 296e48354ceSNicholas Bellinger pr_err("InitiatorName key not received" 297e48354ceSNicholas Bellinger " in first login request.\n"); 298e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 299e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_MISSING_FIELDS); 300e48354ceSNicholas Bellinger return -1; 301e48354ceSNicholas Bellinger } 302e48354ceSNicholas Bellinger 303e48354ceSNicholas Bellinger /* 304e48354ceSNicholas Bellinger * For non-leading connections, double check that the 305e48354ceSNicholas Bellinger * received InitiatorName matches the existing session's 306e48354ceSNicholas Bellinger * struct iscsi_node_acl. 307e48354ceSNicholas Bellinger */ 308e48354ceSNicholas Bellinger if (!login->leading_connection) { 309e48354ceSNicholas Bellinger se_nacl = conn->sess->se_sess->se_node_acl; 310e48354ceSNicholas Bellinger if (!se_nacl) { 311e48354ceSNicholas Bellinger pr_err("Unable to locate" 312e48354ceSNicholas Bellinger " struct se_node_acl\n"); 313e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, 314e48354ceSNicholas Bellinger ISCSI_STATUS_CLS_INITIATOR_ERR, 315e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_TGT_NOT_FOUND); 316e48354ceSNicholas Bellinger return -1; 317e48354ceSNicholas Bellinger } 318e48354ceSNicholas Bellinger 319e48354ceSNicholas Bellinger if (strcmp(param->value, 320e48354ceSNicholas Bellinger se_nacl->initiatorname)) { 321e48354ceSNicholas Bellinger pr_err("Incorrect" 322e48354ceSNicholas Bellinger " InitiatorName: %s for this" 323e48354ceSNicholas Bellinger " iSCSI Initiator Node.\n", 324e48354ceSNicholas Bellinger param->value); 325e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, 326e48354ceSNicholas Bellinger ISCSI_STATUS_CLS_INITIATOR_ERR, 327e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_TGT_NOT_FOUND); 328e48354ceSNicholas Bellinger return -1; 329e48354ceSNicholas Bellinger } 330e48354ceSNicholas Bellinger } 331e48354ceSNicholas Bellinger } 332e48354ceSNicholas Bellinger } 333e48354ceSNicholas Bellinger 334e48354ceSNicholas Bellinger return 0; 335e48354ceSNicholas Bellinger } 336e48354ceSNicholas Bellinger 337e48354ceSNicholas Bellinger static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login) 338e48354ceSNicholas Bellinger { 339e48354ceSNicholas Bellinger u32 padding = 0; 340e48354ceSNicholas Bellinger struct iscsi_login_rsp *login_rsp; 341e48354ceSNicholas Bellinger 342e48354ceSNicholas Bellinger login_rsp = (struct iscsi_login_rsp *) login->rsp; 343e48354ceSNicholas Bellinger 344e48354ceSNicholas Bellinger login_rsp->opcode = ISCSI_OP_LOGIN_RSP; 345e48354ceSNicholas Bellinger hton24(login_rsp->dlength, login->rsp_length); 346e48354ceSNicholas Bellinger memcpy(login_rsp->isid, login->isid, 6); 347e48354ceSNicholas Bellinger login_rsp->tsih = cpu_to_be16(login->tsih); 34866c7db68SChristoph Hellwig login_rsp->itt = login->init_task_tag; 349e48354ceSNicholas Bellinger login_rsp->statsn = cpu_to_be32(conn->stat_sn++); 350e48354ceSNicholas Bellinger login_rsp->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 351109e2381SRoland Dreier login_rsp->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn)); 352e48354ceSNicholas Bellinger 353e48354ceSNicholas Bellinger pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x," 354e48354ceSNicholas Bellinger " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:" 35566c7db68SChristoph Hellwig " %u\n", login_rsp->flags, (__force u32)login_rsp->itt, 356e48354ceSNicholas Bellinger ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn), 357e48354ceSNicholas Bellinger ntohl(login_rsp->statsn), login->rsp_length); 358e48354ceSNicholas Bellinger 359e48354ceSNicholas Bellinger padding = ((-login->rsp_length) & 3); 360e5419865SNicholas Bellinger /* 361e5419865SNicholas Bellinger * Before sending the last login response containing the transition 362e5419865SNicholas Bellinger * bit for full-feature-phase, go ahead and start up TX/RX threads 363e5419865SNicholas Bellinger * now to avoid potential resource allocation failures after the 364e5419865SNicholas Bellinger * final login response has been sent. 365e5419865SNicholas Bellinger */ 366e5419865SNicholas Bellinger if (login->login_complete) { 367e5419865SNicholas Bellinger int rc = iscsit_start_kthreads(conn); 368e5419865SNicholas Bellinger if (rc) { 369e5419865SNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 370e5419865SNicholas Bellinger ISCSI_LOGIN_STATUS_NO_RESOURCES); 371e5419865SNicholas Bellinger return -1; 372e5419865SNicholas Bellinger } 373e5419865SNicholas Bellinger } 374e48354ceSNicholas Bellinger 375baa4d64bSNicholas Bellinger if (conn->conn_transport->iscsit_put_login_tx(conn, login, 376e48354ceSNicholas Bellinger login->rsp_length + padding) < 0) 377e5419865SNicholas Bellinger goto err; 378e48354ceSNicholas Bellinger 379e48354ceSNicholas Bellinger login->rsp_length = 0; 380e48354ceSNicholas Bellinger 381e48354ceSNicholas Bellinger return 0; 382e5419865SNicholas Bellinger 383e5419865SNicholas Bellinger err: 384e5419865SNicholas Bellinger if (login->login_complete) { 385e5419865SNicholas Bellinger if (conn->rx_thread && conn->rx_thread_active) { 386e5419865SNicholas Bellinger send_sig(SIGINT, conn->rx_thread, 1); 387ca82c2bdSNicholas Bellinger complete(&conn->rx_login_comp); 388e5419865SNicholas Bellinger kthread_stop(conn->rx_thread); 389e5419865SNicholas Bellinger } 390e5419865SNicholas Bellinger if (conn->tx_thread && conn->tx_thread_active) { 391e5419865SNicholas Bellinger send_sig(SIGINT, conn->tx_thread, 1); 392e5419865SNicholas Bellinger kthread_stop(conn->tx_thread); 393e5419865SNicholas Bellinger } 394e5419865SNicholas Bellinger spin_lock(&iscsit_global->ts_bitmap_lock); 395e5419865SNicholas Bellinger bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id, 396e5419865SNicholas Bellinger get_order(1)); 397e5419865SNicholas Bellinger spin_unlock(&iscsit_global->ts_bitmap_lock); 398e5419865SNicholas Bellinger } 399e5419865SNicholas Bellinger return -1; 400e48354ceSNicholas Bellinger } 401e48354ceSNicholas Bellinger 402676d2369SDavid S. Miller static void iscsi_target_sk_data_ready(struct sock *sk) 403d381a801SNicholas Bellinger { 404d381a801SNicholas Bellinger struct iscsi_conn *conn = sk->sk_user_data; 405d381a801SNicholas Bellinger bool rc; 406d381a801SNicholas Bellinger 407d381a801SNicholas Bellinger pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn); 408d381a801SNicholas Bellinger 409d381a801SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 410d381a801SNicholas Bellinger if (!sk->sk_user_data) { 411d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 412d381a801SNicholas Bellinger return; 413d381a801SNicholas Bellinger } 414d381a801SNicholas Bellinger if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) { 415d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 416d381a801SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn); 417d381a801SNicholas Bellinger return; 418d381a801SNicholas Bellinger } 419d381a801SNicholas Bellinger if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) { 420d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 421d381a801SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn); 422d381a801SNicholas Bellinger return; 423d381a801SNicholas Bellinger } 424d381a801SNicholas Bellinger if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) { 425d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 426d381a801SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn); 4271c130ae0SFlorian Westphal if (iscsi_target_sk_data_ready == conn->orig_data_ready) 4281c130ae0SFlorian Westphal return; 4291c130ae0SFlorian Westphal conn->orig_data_ready(sk); 430d381a801SNicholas Bellinger return; 431d381a801SNicholas Bellinger } 432d381a801SNicholas Bellinger 433d381a801SNicholas Bellinger rc = schedule_delayed_work(&conn->login_work, 0); 4340bcc297eSChristophe Vu-Brugier if (!rc) { 435d381a801SNicholas Bellinger pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work" 436d381a801SNicholas Bellinger " got false\n"); 437d381a801SNicholas Bellinger } 438d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 439d381a801SNicholas Bellinger } 440d381a801SNicholas Bellinger 441bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *); 442bb048357SNicholas Bellinger 443d381a801SNicholas Bellinger static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn) 444d381a801SNicholas Bellinger { 445d381a801SNicholas Bellinger struct sock *sk; 446d381a801SNicholas Bellinger 447d381a801SNicholas Bellinger if (!conn->sock) 448d381a801SNicholas Bellinger return; 449d381a801SNicholas Bellinger 450d381a801SNicholas Bellinger sk = conn->sock->sk; 451d381a801SNicholas Bellinger pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn); 452d381a801SNicholas Bellinger 453d381a801SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 454d381a801SNicholas Bellinger sk->sk_user_data = conn; 455d381a801SNicholas Bellinger conn->orig_data_ready = sk->sk_data_ready; 456bb048357SNicholas Bellinger conn->orig_state_change = sk->sk_state_change; 457d381a801SNicholas Bellinger sk->sk_data_ready = iscsi_target_sk_data_ready; 458bb048357SNicholas Bellinger sk->sk_state_change = iscsi_target_sk_state_change; 459d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 460bb048357SNicholas Bellinger 461bb048357SNicholas Bellinger sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ; 462bb048357SNicholas Bellinger sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ; 463d381a801SNicholas Bellinger } 464d381a801SNicholas Bellinger 465d381a801SNicholas Bellinger static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn) 466d381a801SNicholas Bellinger { 467d381a801SNicholas Bellinger struct sock *sk; 468d381a801SNicholas Bellinger 469d381a801SNicholas Bellinger if (!conn->sock) 470d381a801SNicholas Bellinger return; 471d381a801SNicholas Bellinger 472d381a801SNicholas Bellinger sk = conn->sock->sk; 473d381a801SNicholas Bellinger pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn); 474d381a801SNicholas Bellinger 475d381a801SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 476d381a801SNicholas Bellinger if (!sk->sk_user_data) { 477d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 478d381a801SNicholas Bellinger return; 479d381a801SNicholas Bellinger } 480d381a801SNicholas Bellinger sk->sk_user_data = NULL; 481d381a801SNicholas Bellinger sk->sk_data_ready = conn->orig_data_ready; 482bb048357SNicholas Bellinger sk->sk_state_change = conn->orig_state_change; 483d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 484bb048357SNicholas Bellinger 485bb048357SNicholas Bellinger sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; 486bb048357SNicholas Bellinger sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; 487d381a801SNicholas Bellinger } 488d381a801SNicholas Bellinger 489d381a801SNicholas Bellinger static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *); 490d381a801SNicholas Bellinger 49125cdda95SNicholas Bellinger static bool __iscsi_target_sk_check_close(struct sock *sk) 492d381a801SNicholas Bellinger { 493d381a801SNicholas Bellinger if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) { 49425cdda95SNicholas Bellinger pr_debug("__iscsi_target_sk_check_close: TCP_CLOSE_WAIT|TCP_CLOSE," 495d381a801SNicholas Bellinger "returning FALSE\n"); 49625cdda95SNicholas Bellinger return true; 49725cdda95SNicholas Bellinger } 498d381a801SNicholas Bellinger return false; 499d381a801SNicholas Bellinger } 50025cdda95SNicholas Bellinger 50125cdda95SNicholas Bellinger static bool iscsi_target_sk_check_close(struct iscsi_conn *conn) 50225cdda95SNicholas Bellinger { 50325cdda95SNicholas Bellinger bool state = false; 50425cdda95SNicholas Bellinger 50525cdda95SNicholas Bellinger if (conn->sock) { 50625cdda95SNicholas Bellinger struct sock *sk = conn->sock->sk; 50725cdda95SNicholas Bellinger 50825cdda95SNicholas Bellinger read_lock_bh(&sk->sk_callback_lock); 50925cdda95SNicholas Bellinger state = (__iscsi_target_sk_check_close(sk) || 51025cdda95SNicholas Bellinger test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)); 51125cdda95SNicholas Bellinger read_unlock_bh(&sk->sk_callback_lock); 51225cdda95SNicholas Bellinger } 51325cdda95SNicholas Bellinger return state; 51425cdda95SNicholas Bellinger } 51525cdda95SNicholas Bellinger 51625cdda95SNicholas Bellinger static bool iscsi_target_sk_check_flag(struct iscsi_conn *conn, unsigned int flag) 51725cdda95SNicholas Bellinger { 51825cdda95SNicholas Bellinger bool state = false; 51925cdda95SNicholas Bellinger 52025cdda95SNicholas Bellinger if (conn->sock) { 52125cdda95SNicholas Bellinger struct sock *sk = conn->sock->sk; 52225cdda95SNicholas Bellinger 52325cdda95SNicholas Bellinger read_lock_bh(&sk->sk_callback_lock); 52425cdda95SNicholas Bellinger state = test_bit(flag, &conn->login_flags); 52525cdda95SNicholas Bellinger read_unlock_bh(&sk->sk_callback_lock); 52625cdda95SNicholas Bellinger } 52725cdda95SNicholas Bellinger return state; 52825cdda95SNicholas Bellinger } 52925cdda95SNicholas Bellinger 53025cdda95SNicholas Bellinger static bool iscsi_target_sk_check_and_clear(struct iscsi_conn *conn, unsigned int flag) 53125cdda95SNicholas Bellinger { 53225cdda95SNicholas Bellinger bool state = false; 53325cdda95SNicholas Bellinger 53425cdda95SNicholas Bellinger if (conn->sock) { 53525cdda95SNicholas Bellinger struct sock *sk = conn->sock->sk; 53625cdda95SNicholas Bellinger 53725cdda95SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 53825cdda95SNicholas Bellinger state = (__iscsi_target_sk_check_close(sk) || 53925cdda95SNicholas Bellinger test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)); 54025cdda95SNicholas Bellinger if (!state) 54125cdda95SNicholas Bellinger clear_bit(flag, &conn->login_flags); 54225cdda95SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 54325cdda95SNicholas Bellinger } 54425cdda95SNicholas Bellinger return state; 545d381a801SNicholas Bellinger } 546d381a801SNicholas Bellinger 547d381a801SNicholas Bellinger static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login) 548d381a801SNicholas Bellinger { 549d381a801SNicholas Bellinger struct iscsi_np *np = login->np; 550d381a801SNicholas Bellinger bool zero_tsih = login->zero_tsih; 551d381a801SNicholas Bellinger 552d381a801SNicholas Bellinger iscsi_remove_failed_auth_entry(conn); 553d381a801SNicholas Bellinger iscsi_target_nego_release(conn); 554d381a801SNicholas Bellinger iscsi_target_login_sess_out(conn, np, zero_tsih, true); 555d381a801SNicholas Bellinger } 556d381a801SNicholas Bellinger 557f7c9564aSKees Cook struct conn_timeout { 558f7c9564aSKees Cook struct timer_list timer; 559f7c9564aSKees Cook struct iscsi_conn *conn; 560f7c9564aSKees Cook }; 561f7c9564aSKees Cook 562f7c9564aSKees Cook static void iscsi_target_login_timeout(struct timer_list *t) 563d381a801SNicholas Bellinger { 564f7c9564aSKees Cook struct conn_timeout *timeout = from_timer(timeout, t, timer); 565f7c9564aSKees Cook struct iscsi_conn *conn = timeout->conn; 566d381a801SNicholas Bellinger 567d381a801SNicholas Bellinger pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n"); 568d381a801SNicholas Bellinger 569d381a801SNicholas Bellinger if (conn->login_kworker) { 570d381a801SNicholas Bellinger pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n", 571d381a801SNicholas Bellinger conn->login_kworker->comm, conn->login_kworker->pid); 572d381a801SNicholas Bellinger send_sig(SIGINT, conn->login_kworker, 1); 573d381a801SNicholas Bellinger } 574d381a801SNicholas Bellinger } 575d381a801SNicholas Bellinger 576d381a801SNicholas Bellinger static void iscsi_target_do_login_rx(struct work_struct *work) 577d381a801SNicholas Bellinger { 578d381a801SNicholas Bellinger struct iscsi_conn *conn = container_of(work, 579d381a801SNicholas Bellinger struct iscsi_conn, login_work.work); 580d381a801SNicholas Bellinger struct iscsi_login *login = conn->login; 581d381a801SNicholas Bellinger struct iscsi_np *np = login->np; 582d381a801SNicholas Bellinger struct iscsi_portal_group *tpg = conn->tpg; 583d381a801SNicholas Bellinger struct iscsi_tpg_np *tpg_np = conn->tpg_np; 584f7c9564aSKees Cook struct conn_timeout timeout; 585d381a801SNicholas Bellinger int rc, zero_tsih = login->zero_tsih; 586d381a801SNicholas Bellinger bool state; 587d381a801SNicholas Bellinger 588d381a801SNicholas Bellinger pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n", 589d381a801SNicholas Bellinger conn, current->comm, current->pid); 59025cdda95SNicholas Bellinger /* 59125cdda95SNicholas Bellinger * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready() 59225cdda95SNicholas Bellinger * before initial PDU processing in iscsi_target_start_negotiation() 59325cdda95SNicholas Bellinger * has completed, go ahead and retry until it's cleared. 59425cdda95SNicholas Bellinger * 59525cdda95SNicholas Bellinger * Otherwise if the TCP connection drops while this is occuring, 59625cdda95SNicholas Bellinger * iscsi_target_start_negotiation() will detect the failure, call 59725cdda95SNicholas Bellinger * cancel_delayed_work_sync(&conn->login_work), and cleanup the 59825cdda95SNicholas Bellinger * remaining iscsi connection resources from iscsi_np process context. 59925cdda95SNicholas Bellinger */ 60025cdda95SNicholas Bellinger if (iscsi_target_sk_check_flag(conn, LOGIN_FLAGS_INITIAL_PDU)) { 60125cdda95SNicholas Bellinger schedule_delayed_work(&conn->login_work, msecs_to_jiffies(10)); 60225cdda95SNicholas Bellinger return; 60325cdda95SNicholas Bellinger } 604d381a801SNicholas Bellinger 605d381a801SNicholas Bellinger spin_lock(&tpg->tpg_state_lock); 606d381a801SNicholas Bellinger state = (tpg->tpg_state == TPG_STATE_ACTIVE); 607d381a801SNicholas Bellinger spin_unlock(&tpg->tpg_state_lock); 608d381a801SNicholas Bellinger 6090bcc297eSChristophe Vu-Brugier if (!state) { 610d381a801SNicholas Bellinger pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n"); 61125cdda95SNicholas Bellinger goto err; 612d381a801SNicholas Bellinger } 613d381a801SNicholas Bellinger 61425cdda95SNicholas Bellinger if (iscsi_target_sk_check_close(conn)) { 615d381a801SNicholas Bellinger pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n"); 61625cdda95SNicholas Bellinger goto err; 617d381a801SNicholas Bellinger } 618d381a801SNicholas Bellinger 619d381a801SNicholas Bellinger conn->login_kworker = current; 620d381a801SNicholas Bellinger allow_signal(SIGINT); 621d381a801SNicholas Bellinger 622f7c9564aSKees Cook timeout.conn = conn; 623f7c9564aSKees Cook timer_setup_on_stack(&timeout.timer, iscsi_target_login_timeout, 0); 624f7c9564aSKees Cook mod_timer(&timeout.timer, jiffies + TA_LOGIN_TIMEOUT * HZ); 625f7c9564aSKees Cook pr_debug("Starting login timer for %s/%d\n", current->comm, current->pid); 626d381a801SNicholas Bellinger 627d381a801SNicholas Bellinger rc = conn->conn_transport->iscsit_get_login_rx(conn, login); 628f7c9564aSKees Cook del_timer_sync(&timeout.timer); 629f7c9564aSKees Cook destroy_timer_on_stack(&timeout.timer); 630d381a801SNicholas Bellinger flush_signals(current); 631d381a801SNicholas Bellinger conn->login_kworker = NULL; 632d381a801SNicholas Bellinger 63325cdda95SNicholas Bellinger if (rc < 0) 63425cdda95SNicholas Bellinger goto err; 635d381a801SNicholas Bellinger 636d381a801SNicholas Bellinger pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n", 637d381a801SNicholas Bellinger conn, current->comm, current->pid); 638d381a801SNicholas Bellinger 639d381a801SNicholas Bellinger rc = iscsi_target_do_login(conn, login); 640d381a801SNicholas Bellinger if (rc < 0) { 64125cdda95SNicholas Bellinger goto err; 642d381a801SNicholas Bellinger } else if (!rc) { 64325cdda95SNicholas Bellinger if (iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_READ_ACTIVE)) 64425cdda95SNicholas Bellinger goto err; 645d381a801SNicholas Bellinger } else if (rc == 1) { 646d381a801SNicholas Bellinger iscsi_target_nego_release(conn); 647d381a801SNicholas Bellinger iscsi_post_login_handler(np, conn, zero_tsih); 648d381a801SNicholas Bellinger iscsit_deaccess_np(np, tpg, tpg_np); 649d381a801SNicholas Bellinger } 65025cdda95SNicholas Bellinger return; 65125cdda95SNicholas Bellinger 65225cdda95SNicholas Bellinger err: 65325cdda95SNicholas Bellinger iscsi_target_restore_sock_callbacks(conn); 65425cdda95SNicholas Bellinger iscsi_target_login_drop(conn, login); 65525cdda95SNicholas Bellinger iscsit_deaccess_np(np, tpg, tpg_np); 656d381a801SNicholas Bellinger } 657d381a801SNicholas Bellinger 658bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *sk) 659bb048357SNicholas Bellinger { 660bb048357SNicholas Bellinger struct iscsi_conn *conn; 661bb048357SNicholas Bellinger void (*orig_state_change)(struct sock *); 662bb048357SNicholas Bellinger bool state; 663bb048357SNicholas Bellinger 664bb048357SNicholas Bellinger pr_debug("Entering iscsi_target_sk_state_change\n"); 665bb048357SNicholas Bellinger 666bb048357SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 667bb048357SNicholas Bellinger conn = sk->sk_user_data; 668bb048357SNicholas Bellinger if (!conn) { 669bb048357SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 670bb048357SNicholas Bellinger return; 671bb048357SNicholas Bellinger } 672bb048357SNicholas Bellinger orig_state_change = conn->orig_state_change; 673bb048357SNicholas Bellinger 674bb048357SNicholas Bellinger if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) { 675bb048357SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n", 676bb048357SNicholas Bellinger conn); 677bb048357SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 678bb048357SNicholas Bellinger orig_state_change(sk); 679bb048357SNicholas Bellinger return; 680bb048357SNicholas Bellinger } 68125cdda95SNicholas Bellinger state = __iscsi_target_sk_check_close(sk); 68225cdda95SNicholas Bellinger pr_debug("__iscsi_target_sk_close_change: state: %d\n", state); 68325cdda95SNicholas Bellinger 684bb048357SNicholas Bellinger if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) { 685bb048357SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change" 686bb048357SNicholas Bellinger " conn: %p\n", conn); 68725cdda95SNicholas Bellinger if (state) 68825cdda95SNicholas Bellinger set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags); 689bb048357SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 690bb048357SNicholas Bellinger orig_state_change(sk); 691bb048357SNicholas Bellinger return; 692bb048357SNicholas Bellinger } 69325cdda95SNicholas Bellinger if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) { 694bb048357SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n", 695bb048357SNicholas Bellinger conn); 696bb048357SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 697bb048357SNicholas Bellinger orig_state_change(sk); 698bb048357SNicholas Bellinger return; 699bb048357SNicholas Bellinger } 70025cdda95SNicholas Bellinger /* 70125cdda95SNicholas Bellinger * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED, 70225cdda95SNicholas Bellinger * but only queue conn->login_work -> iscsi_target_do_login_rx() 70325cdda95SNicholas Bellinger * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared. 70425cdda95SNicholas Bellinger * 70525cdda95SNicholas Bellinger * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close() 70625cdda95SNicholas Bellinger * will detect the dropped TCP connection from delayed workqueue context. 70725cdda95SNicholas Bellinger * 70825cdda95SNicholas Bellinger * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial 70925cdda95SNicholas Bellinger * iscsi_target_start_negotiation() is running, iscsi_target_do_login() 71025cdda95SNicholas Bellinger * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation() 71125cdda95SNicholas Bellinger * via iscsi_target_sk_check_and_clear() is responsible for detecting the 71225cdda95SNicholas Bellinger * dropped TCP connection in iscsi_np process context, and cleaning up 71325cdda95SNicholas Bellinger * the remaining iscsi connection resources. 71425cdda95SNicholas Bellinger */ 71525cdda95SNicholas Bellinger if (state) { 71625cdda95SNicholas Bellinger pr_debug("iscsi_target_sk_state_change got failed state\n"); 71725cdda95SNicholas Bellinger set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags); 71825cdda95SNicholas Bellinger state = test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags); 719bb048357SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 720bb048357SNicholas Bellinger 72125cdda95SNicholas Bellinger orig_state_change(sk); 722bb048357SNicholas Bellinger 72325cdda95SNicholas Bellinger if (!state) 72425cdda95SNicholas Bellinger schedule_delayed_work(&conn->login_work, 0); 725bb048357SNicholas Bellinger return; 726bb048357SNicholas Bellinger } 72725cdda95SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 72825cdda95SNicholas Bellinger 729bb048357SNicholas Bellinger orig_state_change(sk); 730bb048357SNicholas Bellinger } 731bb048357SNicholas Bellinger 732e48354ceSNicholas Bellinger /* 733e48354ceSNicholas Bellinger * NOTE: We check for existing sessions or connections AFTER the initiator 734e48354ceSNicholas Bellinger * has been successfully authenticated in order to protect against faked 735e48354ceSNicholas Bellinger * ISID/TSIH combinations. 736e48354ceSNicholas Bellinger */ 737e48354ceSNicholas Bellinger static int iscsi_target_check_for_existing_instances( 738e48354ceSNicholas Bellinger struct iscsi_conn *conn, 739e48354ceSNicholas Bellinger struct iscsi_login *login) 740e48354ceSNicholas Bellinger { 741e48354ceSNicholas Bellinger if (login->checked_for_existing) 742e48354ceSNicholas Bellinger return 0; 743e48354ceSNicholas Bellinger 744e48354ceSNicholas Bellinger login->checked_for_existing = 1; 745e48354ceSNicholas Bellinger 746e48354ceSNicholas Bellinger if (!login->tsih) 747e48354ceSNicholas Bellinger return iscsi_check_for_session_reinstatement(conn); 748e48354ceSNicholas Bellinger else 749e48354ceSNicholas Bellinger return iscsi_login_post_auth_non_zero_tsih(conn, login->cid, 750e48354ceSNicholas Bellinger login->initial_exp_statsn); 751e48354ceSNicholas Bellinger } 752e48354ceSNicholas Bellinger 753e48354ceSNicholas Bellinger static int iscsi_target_do_authentication( 754e48354ceSNicholas Bellinger struct iscsi_conn *conn, 755e48354ceSNicholas Bellinger struct iscsi_login *login) 756e48354ceSNicholas Bellinger { 757e48354ceSNicholas Bellinger int authret; 758e48354ceSNicholas Bellinger u32 payload_length; 759e48354ceSNicholas Bellinger struct iscsi_param *param; 760e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 761e48354ceSNicholas Bellinger struct iscsi_login_rsp *login_rsp; 762e48354ceSNicholas Bellinger 763e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 764e48354ceSNicholas Bellinger login_rsp = (struct iscsi_login_rsp *) login->rsp; 765e48354ceSNicholas Bellinger payload_length = ntoh24(login_req->dlength); 766e48354ceSNicholas Bellinger 767e48354ceSNicholas Bellinger param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list); 768e48354ceSNicholas Bellinger if (!param) 769e48354ceSNicholas Bellinger return -1; 770e48354ceSNicholas Bellinger 771e48354ceSNicholas Bellinger authret = iscsi_handle_authentication( 772e48354ceSNicholas Bellinger conn, 773e48354ceSNicholas Bellinger login->req_buf, 774e48354ceSNicholas Bellinger login->rsp_buf, 775e48354ceSNicholas Bellinger payload_length, 776e48354ceSNicholas Bellinger &login->rsp_length, 777e48354ceSNicholas Bellinger param->value); 778e48354ceSNicholas Bellinger switch (authret) { 779e48354ceSNicholas Bellinger case 0: 780e48354ceSNicholas Bellinger pr_debug("Received OK response" 781e48354ceSNicholas Bellinger " from LIO Authentication, continuing.\n"); 782e48354ceSNicholas Bellinger break; 783e48354ceSNicholas Bellinger case 1: 784e48354ceSNicholas Bellinger pr_debug("iSCSI security negotiation" 785bfb9035cSJoe Perches " completed successfully.\n"); 786e48354ceSNicholas Bellinger login->auth_complete = 1; 787e48354ceSNicholas Bellinger if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) && 788e48354ceSNicholas Bellinger (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) { 789e48354ceSNicholas Bellinger login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 | 790e48354ceSNicholas Bellinger ISCSI_FLAG_LOGIN_TRANSIT); 791e48354ceSNicholas Bellinger login->current_stage = 1; 792e48354ceSNicholas Bellinger } 793e48354ceSNicholas Bellinger return iscsi_target_check_for_existing_instances( 794e48354ceSNicholas Bellinger conn, login); 795e48354ceSNicholas Bellinger case 2: 796e48354ceSNicholas Bellinger pr_err("Security negotiation" 797e48354ceSNicholas Bellinger " failed.\n"); 798e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 799e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_AUTH_FAILED); 800e48354ceSNicholas Bellinger return -1; 801e48354ceSNicholas Bellinger default: 802e48354ceSNicholas Bellinger pr_err("Received unknown error %d from LIO" 803e48354ceSNicholas Bellinger " Authentication\n", authret); 804e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 805e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_TARGET_ERROR); 806e48354ceSNicholas Bellinger return -1; 807e48354ceSNicholas Bellinger } 808e48354ceSNicholas Bellinger 809e48354ceSNicholas Bellinger return 0; 810e48354ceSNicholas Bellinger } 811e48354ceSNicholas Bellinger 812e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_zero( 813e48354ceSNicholas Bellinger struct iscsi_conn *conn, 814e48354ceSNicholas Bellinger struct iscsi_login *login) 815e48354ceSNicholas Bellinger { 816e48354ceSNicholas Bellinger int ret; 817e48354ceSNicholas Bellinger u32 payload_length; 818e48354ceSNicholas Bellinger struct iscsi_param *param; 819e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 820e48354ceSNicholas Bellinger struct iscsi_login_rsp *login_rsp; 821e48354ceSNicholas Bellinger 822e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 823e48354ceSNicholas Bellinger login_rsp = (struct iscsi_login_rsp *) login->rsp; 824e48354ceSNicholas Bellinger payload_length = ntoh24(login_req->dlength); 825e48354ceSNicholas Bellinger 826e48354ceSNicholas Bellinger param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list); 827e48354ceSNicholas Bellinger if (!param) 828e48354ceSNicholas Bellinger return -1; 829e48354ceSNicholas Bellinger 830e48354ceSNicholas Bellinger ret = iscsi_decode_text_input( 831e48354ceSNicholas Bellinger PHASE_SECURITY|PHASE_DECLARATIVE, 832e48354ceSNicholas Bellinger SENDER_INITIATOR|SENDER_RECEIVER, 833e48354ceSNicholas Bellinger login->req_buf, 834e48354ceSNicholas Bellinger payload_length, 8359977bb18SNicholas Bellinger conn); 836e48354ceSNicholas Bellinger if (ret < 0) 837e48354ceSNicholas Bellinger return -1; 838e48354ceSNicholas Bellinger 839e48354ceSNicholas Bellinger if (ret > 0) { 840e48354ceSNicholas Bellinger if (login->auth_complete) { 841e48354ceSNicholas Bellinger pr_err("Initiator has already been" 842e48354ceSNicholas Bellinger " successfully authenticated, but is still" 843e48354ceSNicholas Bellinger " sending %s keys.\n", param->value); 844e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 845e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_INIT_ERR); 846e48354ceSNicholas Bellinger return -1; 847e48354ceSNicholas Bellinger } 848e48354ceSNicholas Bellinger 849e48354ceSNicholas Bellinger goto do_auth; 85091f0abfdSNicholas Bellinger } else if (!payload_length) { 85191f0abfdSNicholas Bellinger pr_err("Initiator sent zero length security payload," 85291f0abfdSNicholas Bellinger " login failed\n"); 85391f0abfdSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 85491f0abfdSNicholas Bellinger ISCSI_LOGIN_STATUS_AUTH_FAILED); 85591f0abfdSNicholas Bellinger return -1; 856e48354ceSNicholas Bellinger } 857e48354ceSNicholas Bellinger 858e48354ceSNicholas Bellinger if (login->first_request) 859e48354ceSNicholas Bellinger if (iscsi_target_check_first_request(conn, login) < 0) 860e48354ceSNicholas Bellinger return -1; 861e48354ceSNicholas Bellinger 862e48354ceSNicholas Bellinger ret = iscsi_encode_text_output( 863e48354ceSNicholas Bellinger PHASE_SECURITY|PHASE_DECLARATIVE, 864e48354ceSNicholas Bellinger SENDER_TARGET, 865e48354ceSNicholas Bellinger login->rsp_buf, 866e48354ceSNicholas Bellinger &login->rsp_length, 867138d351eSNicholas Bellinger conn->param_list, 868138d351eSNicholas Bellinger conn->tpg->tpg_attrib.login_keys_workaround); 869e48354ceSNicholas Bellinger if (ret < 0) 870e48354ceSNicholas Bellinger return -1; 871e48354ceSNicholas Bellinger 872e48354ceSNicholas Bellinger if (!iscsi_check_negotiated_keys(conn->param_list)) { 87360bfcf8eSAndy Grover if (conn->tpg->tpg_attrib.authentication && 874e48354ceSNicholas Bellinger !strncmp(param->value, NONE, 4)) { 875e48354ceSNicholas Bellinger pr_err("Initiator sent AuthMethod=None but" 876e48354ceSNicholas Bellinger " Target is enforcing iSCSI Authentication," 877e48354ceSNicholas Bellinger " login failed.\n"); 878e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 879e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_AUTH_FAILED); 880e48354ceSNicholas Bellinger return -1; 881e48354ceSNicholas Bellinger } 882e48354ceSNicholas Bellinger 88360bfcf8eSAndy Grover if (conn->tpg->tpg_attrib.authentication && 884e48354ceSNicholas Bellinger !login->auth_complete) 885e48354ceSNicholas Bellinger return 0; 886e48354ceSNicholas Bellinger 887e48354ceSNicholas Bellinger if (strncmp(param->value, NONE, 4) && !login->auth_complete) 888e48354ceSNicholas Bellinger return 0; 889e48354ceSNicholas Bellinger 890e48354ceSNicholas Bellinger if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) && 891e48354ceSNicholas Bellinger (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) { 892e48354ceSNicholas Bellinger login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 | 893e48354ceSNicholas Bellinger ISCSI_FLAG_LOGIN_TRANSIT; 894e48354ceSNicholas Bellinger login->current_stage = 1; 895e48354ceSNicholas Bellinger } 896e48354ceSNicholas Bellinger } 897e48354ceSNicholas Bellinger 898e48354ceSNicholas Bellinger return 0; 899e48354ceSNicholas Bellinger do_auth: 900e48354ceSNicholas Bellinger return iscsi_target_do_authentication(conn, login); 901e48354ceSNicholas Bellinger } 902e48354ceSNicholas Bellinger 903e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login) 904e48354ceSNicholas Bellinger { 905e48354ceSNicholas Bellinger int ret; 906e48354ceSNicholas Bellinger u32 payload_length; 907e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 908e48354ceSNicholas Bellinger struct iscsi_login_rsp *login_rsp; 909e48354ceSNicholas Bellinger 910e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 911e48354ceSNicholas Bellinger login_rsp = (struct iscsi_login_rsp *) login->rsp; 912e48354ceSNicholas Bellinger payload_length = ntoh24(login_req->dlength); 913e48354ceSNicholas Bellinger 914e48354ceSNicholas Bellinger ret = iscsi_decode_text_input( 915e48354ceSNicholas Bellinger PHASE_OPERATIONAL|PHASE_DECLARATIVE, 916e48354ceSNicholas Bellinger SENDER_INITIATOR|SENDER_RECEIVER, 917e48354ceSNicholas Bellinger login->req_buf, 918e48354ceSNicholas Bellinger payload_length, 9199977bb18SNicholas Bellinger conn); 9201c5c12c6SRoland Dreier if (ret < 0) { 9211c5c12c6SRoland Dreier iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 9221c5c12c6SRoland Dreier ISCSI_LOGIN_STATUS_INIT_ERR); 923e48354ceSNicholas Bellinger return -1; 9241c5c12c6SRoland Dreier } 925e48354ceSNicholas Bellinger 926e48354ceSNicholas Bellinger if (login->first_request) 927e48354ceSNicholas Bellinger if (iscsi_target_check_first_request(conn, login) < 0) 928e48354ceSNicholas Bellinger return -1; 929e48354ceSNicholas Bellinger 930e48354ceSNicholas Bellinger if (iscsi_target_check_for_existing_instances(conn, login) < 0) 931e48354ceSNicholas Bellinger return -1; 932e48354ceSNicholas Bellinger 933e48354ceSNicholas Bellinger ret = iscsi_encode_text_output( 934e48354ceSNicholas Bellinger PHASE_OPERATIONAL|PHASE_DECLARATIVE, 935e48354ceSNicholas Bellinger SENDER_TARGET, 936e48354ceSNicholas Bellinger login->rsp_buf, 937e48354ceSNicholas Bellinger &login->rsp_length, 938138d351eSNicholas Bellinger conn->param_list, 939138d351eSNicholas Bellinger conn->tpg->tpg_attrib.login_keys_workaround); 9401c5c12c6SRoland Dreier if (ret < 0) { 9411c5c12c6SRoland Dreier iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 9421c5c12c6SRoland Dreier ISCSI_LOGIN_STATUS_INIT_ERR); 943e48354ceSNicholas Bellinger return -1; 9441c5c12c6SRoland Dreier } 945e48354ceSNicholas Bellinger 946e48354ceSNicholas Bellinger if (!login->auth_complete && 94760bfcf8eSAndy Grover conn->tpg->tpg_attrib.authentication) { 948e48354ceSNicholas Bellinger pr_err("Initiator is requesting CSG: 1, has not been" 949e48354ceSNicholas Bellinger " successfully authenticated, and the Target is" 950e48354ceSNicholas Bellinger " enforcing iSCSI Authentication, login failed.\n"); 951e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 952e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_AUTH_FAILED); 953e48354ceSNicholas Bellinger return -1; 954e48354ceSNicholas Bellinger } 955e48354ceSNicholas Bellinger 956e48354ceSNicholas Bellinger if (!iscsi_check_negotiated_keys(conn->param_list)) 957e48354ceSNicholas Bellinger if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) && 958e48354ceSNicholas Bellinger (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) 959e48354ceSNicholas Bellinger login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 | 960e48354ceSNicholas Bellinger ISCSI_FLAG_LOGIN_TRANSIT; 961e48354ceSNicholas Bellinger 962e48354ceSNicholas Bellinger return 0; 963e48354ceSNicholas Bellinger } 964e48354ceSNicholas Bellinger 965e48354ceSNicholas Bellinger static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login) 966e48354ceSNicholas Bellinger { 967e48354ceSNicholas Bellinger int pdu_count = 0; 968e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 969e48354ceSNicholas Bellinger struct iscsi_login_rsp *login_rsp; 970e48354ceSNicholas Bellinger 971e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 972e48354ceSNicholas Bellinger login_rsp = (struct iscsi_login_rsp *) login->rsp; 973e48354ceSNicholas Bellinger 974e48354ceSNicholas Bellinger while (1) { 975e48354ceSNicholas Bellinger if (++pdu_count > MAX_LOGIN_PDUS) { 976e48354ceSNicholas Bellinger pr_err("MAX_LOGIN_PDUS count reached.\n"); 977e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 978e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_TARGET_ERROR); 979e48354ceSNicholas Bellinger return -1; 980e48354ceSNicholas Bellinger } 981e48354ceSNicholas Bellinger 9825d358065SAndy Grover switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) { 983e48354ceSNicholas Bellinger case 0: 9845d358065SAndy Grover login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK; 985e48354ceSNicholas Bellinger if (iscsi_target_handle_csg_zero(conn, login) < 0) 986e48354ceSNicholas Bellinger return -1; 987e48354ceSNicholas Bellinger break; 988e48354ceSNicholas Bellinger case 1: 989e48354ceSNicholas Bellinger login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1; 990e48354ceSNicholas Bellinger if (iscsi_target_handle_csg_one(conn, login) < 0) 991e48354ceSNicholas Bellinger return -1; 992e48354ceSNicholas Bellinger if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) { 99325cdda95SNicholas Bellinger /* 99425cdda95SNicholas Bellinger * Check to make sure the TCP connection has not 99525cdda95SNicholas Bellinger * dropped asynchronously while session reinstatement 99625cdda95SNicholas Bellinger * was occuring in this kthread context, before 99725cdda95SNicholas Bellinger * transitioning to full feature phase operation. 99825cdda95SNicholas Bellinger */ 99925cdda95SNicholas Bellinger if (iscsi_target_sk_check_close(conn)) 100025cdda95SNicholas Bellinger return -1; 100125cdda95SNicholas Bellinger 1002e48354ceSNicholas Bellinger login->tsih = conn->sess->tsih; 1003baa4d64bSNicholas Bellinger login->login_complete = 1; 1004d381a801SNicholas Bellinger iscsi_target_restore_sock_callbacks(conn); 1005e48354ceSNicholas Bellinger if (iscsi_target_do_tx_login_io(conn, 1006e48354ceSNicholas Bellinger login) < 0) 1007e48354ceSNicholas Bellinger return -1; 1008d381a801SNicholas Bellinger return 1; 1009e48354ceSNicholas Bellinger } 1010e48354ceSNicholas Bellinger break; 1011e48354ceSNicholas Bellinger default: 1012e48354ceSNicholas Bellinger pr_err("Illegal CSG: %d received from" 1013e48354ceSNicholas Bellinger " Initiator, protocol error.\n", 10145d358065SAndy Grover ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)); 1015e48354ceSNicholas Bellinger break; 1016e48354ceSNicholas Bellinger } 1017e48354ceSNicholas Bellinger 1018ea3a179aSNicholas Bellinger if (iscsi_target_do_tx_login_io(conn, login) < 0) 1019e48354ceSNicholas Bellinger return -1; 1020e48354ceSNicholas Bellinger 1021e48354ceSNicholas Bellinger if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) { 1022e48354ceSNicholas Bellinger login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT; 1023e48354ceSNicholas Bellinger login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK; 1024e48354ceSNicholas Bellinger } 1025d381a801SNicholas Bellinger break; 1026e48354ceSNicholas Bellinger } 1027e48354ceSNicholas Bellinger 1028e48354ceSNicholas Bellinger return 0; 1029e48354ceSNicholas Bellinger } 1030e48354ceSNicholas Bellinger 1031e48354ceSNicholas Bellinger static void iscsi_initiatorname_tolower( 1032e48354ceSNicholas Bellinger char *param_buf) 1033e48354ceSNicholas Bellinger { 1034e48354ceSNicholas Bellinger char *c; 1035e48354ceSNicholas Bellinger u32 iqn_size = strlen(param_buf), i; 1036e48354ceSNicholas Bellinger 1037e48354ceSNicholas Bellinger for (i = 0; i < iqn_size; i++) { 10388359cf43SJörn Engel c = ¶m_buf[i]; 1039e48354ceSNicholas Bellinger if (!isupper(*c)) 1040e48354ceSNicholas Bellinger continue; 1041e48354ceSNicholas Bellinger 1042e48354ceSNicholas Bellinger *c = tolower(*c); 1043e48354ceSNicholas Bellinger } 1044e48354ceSNicholas Bellinger } 1045e48354ceSNicholas Bellinger 1046e48354ceSNicholas Bellinger /* 1047e48354ceSNicholas Bellinger * Processes the first Login Request.. 1048e48354ceSNicholas Bellinger */ 1049baa4d64bSNicholas Bellinger int iscsi_target_locate_portal( 1050e48354ceSNicholas Bellinger struct iscsi_np *np, 1051e48354ceSNicholas Bellinger struct iscsi_conn *conn, 1052e48354ceSNicholas Bellinger struct iscsi_login *login) 1053e48354ceSNicholas Bellinger { 1054e48354ceSNicholas Bellinger char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL; 1055e48354ceSNicholas Bellinger char *tmpbuf, *start = NULL, *end = NULL, *key, *value; 1056e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 1057e48354ceSNicholas Bellinger struct iscsi_tiqn *tiqn; 1058d381a801SNicholas Bellinger struct iscsi_tpg_np *tpg_np = NULL; 1059e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 1060988e3a85SNicholas Bellinger struct se_node_acl *se_nacl; 1061988e3a85SNicholas Bellinger u32 payload_length, queue_depth = 0; 1062988e3a85SNicholas Bellinger int sessiontype = 0, ret = 0, tag_num, tag_size; 1063e48354ceSNicholas Bellinger 1064d381a801SNicholas Bellinger INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx); 1065d381a801SNicholas Bellinger iscsi_target_set_sock_callbacks(conn); 1066d381a801SNicholas Bellinger 1067d381a801SNicholas Bellinger login->np = np; 1068d381a801SNicholas Bellinger 1069e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 1070e48354ceSNicholas Bellinger payload_length = ntoh24(login_req->dlength); 1071e48354ceSNicholas Bellinger 1072e48354ceSNicholas Bellinger tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL); 1073e48354ceSNicholas Bellinger if (!tmpbuf) { 1074e48354ceSNicholas Bellinger pr_err("Unable to allocate memory for tmpbuf.\n"); 1075e48354ceSNicholas Bellinger return -1; 1076e48354ceSNicholas Bellinger } 1077e48354ceSNicholas Bellinger 1078e48354ceSNicholas Bellinger memcpy(tmpbuf, login->req_buf, payload_length); 1079e48354ceSNicholas Bellinger tmpbuf[payload_length] = '\0'; 1080e48354ceSNicholas Bellinger start = tmpbuf; 1081e48354ceSNicholas Bellinger end = (start + payload_length); 1082e48354ceSNicholas Bellinger 1083e48354ceSNicholas Bellinger /* 1084e48354ceSNicholas Bellinger * Locate the initial keys expected from the Initiator node in 1085e48354ceSNicholas Bellinger * the first login request in order to progress with the login phase. 1086e48354ceSNicholas Bellinger */ 1087e48354ceSNicholas Bellinger while (start < end) { 1088e48354ceSNicholas Bellinger if (iscsi_extract_key_value(start, &key, &value) < 0) { 1089e48354ceSNicholas Bellinger ret = -1; 1090e48354ceSNicholas Bellinger goto out; 1091e48354ceSNicholas Bellinger } 1092e48354ceSNicholas Bellinger 1093e48354ceSNicholas Bellinger if (!strncmp(key, "InitiatorName", 13)) 1094e48354ceSNicholas Bellinger i_buf = value; 1095e48354ceSNicholas Bellinger else if (!strncmp(key, "SessionType", 11)) 1096e48354ceSNicholas Bellinger s_buf = value; 1097e48354ceSNicholas Bellinger else if (!strncmp(key, "TargetName", 10)) 1098e48354ceSNicholas Bellinger t_buf = value; 1099e48354ceSNicholas Bellinger 1100e48354ceSNicholas Bellinger start += strlen(key) + strlen(value) + 2; 1101e48354ceSNicholas Bellinger } 1102e48354ceSNicholas Bellinger /* 1103e48354ceSNicholas Bellinger * See 5.3. Login Phase. 1104e48354ceSNicholas Bellinger */ 1105e48354ceSNicholas Bellinger if (!i_buf) { 1106e48354ceSNicholas Bellinger pr_err("InitiatorName key not received" 1107e48354ceSNicholas Bellinger " in first login request.\n"); 1108e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1109e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_MISSING_FIELDS); 1110e48354ceSNicholas Bellinger ret = -1; 1111e48354ceSNicholas Bellinger goto out; 1112e48354ceSNicholas Bellinger } 1113e48354ceSNicholas Bellinger /* 1114e48354ceSNicholas Bellinger * Convert the incoming InitiatorName to lowercase following 1115e48354ceSNicholas Bellinger * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs 1116e48354ceSNicholas Bellinger * are NOT case sensitive. 1117e48354ceSNicholas Bellinger */ 1118e48354ceSNicholas Bellinger iscsi_initiatorname_tolower(i_buf); 1119e48354ceSNicholas Bellinger 1120e48354ceSNicholas Bellinger if (!s_buf) { 1121e48354ceSNicholas Bellinger if (!login->leading_connection) 1122e48354ceSNicholas Bellinger goto get_target; 1123e48354ceSNicholas Bellinger 1124e48354ceSNicholas Bellinger pr_err("SessionType key not received" 1125e48354ceSNicholas Bellinger " in first login request.\n"); 1126e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1127e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_MISSING_FIELDS); 1128e48354ceSNicholas Bellinger ret = -1; 1129e48354ceSNicholas Bellinger goto out; 1130e48354ceSNicholas Bellinger } 1131e48354ceSNicholas Bellinger 1132e48354ceSNicholas Bellinger /* 1133e48354ceSNicholas Bellinger * Use default portal group for discovery sessions. 1134e48354ceSNicholas Bellinger */ 1135e48354ceSNicholas Bellinger sessiontype = strncmp(s_buf, DISCOVERY, 9); 1136e48354ceSNicholas Bellinger if (!sessiontype) { 1137e48354ceSNicholas Bellinger conn->tpg = iscsit_global->discovery_tpg; 1138e48354ceSNicholas Bellinger if (!login->leading_connection) 1139e48354ceSNicholas Bellinger goto get_target; 1140e48354ceSNicholas Bellinger 1141e48354ceSNicholas Bellinger sess->sess_ops->SessionType = 1; 1142e48354ceSNicholas Bellinger /* 1143e48354ceSNicholas Bellinger * Setup crc32c modules from libcrypto 1144e48354ceSNicholas Bellinger */ 1145e48354ceSNicholas Bellinger if (iscsi_login_setup_crypto(conn) < 0) { 1146e48354ceSNicholas Bellinger pr_err("iscsi_login_setup_crypto() failed\n"); 1147e48354ceSNicholas Bellinger ret = -1; 1148e48354ceSNicholas Bellinger goto out; 1149e48354ceSNicholas Bellinger } 1150e48354ceSNicholas Bellinger /* 1151e48354ceSNicholas Bellinger * Serialize access across the discovery struct iscsi_portal_group to 1152e48354ceSNicholas Bellinger * process login attempt. 1153e48354ceSNicholas Bellinger */ 1154e48354ceSNicholas Bellinger if (iscsit_access_np(np, conn->tpg) < 0) { 1155e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1156e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1157e48354ceSNicholas Bellinger ret = -1; 1158e48354ceSNicholas Bellinger goto out; 1159e48354ceSNicholas Bellinger } 1160e48354ceSNicholas Bellinger ret = 0; 1161988e3a85SNicholas Bellinger goto alloc_tags; 1162e48354ceSNicholas Bellinger } 1163e48354ceSNicholas Bellinger 1164e48354ceSNicholas Bellinger get_target: 1165e48354ceSNicholas Bellinger if (!t_buf) { 1166e48354ceSNicholas Bellinger pr_err("TargetName key not received" 1167e48354ceSNicholas Bellinger " in first login request while" 1168e48354ceSNicholas Bellinger " SessionType=Normal.\n"); 1169e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1170e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_MISSING_FIELDS); 1171e48354ceSNicholas Bellinger ret = -1; 1172e48354ceSNicholas Bellinger goto out; 1173e48354ceSNicholas Bellinger } 1174e48354ceSNicholas Bellinger 1175e48354ceSNicholas Bellinger /* 1176e48354ceSNicholas Bellinger * Locate Target IQN from Storage Node. 1177e48354ceSNicholas Bellinger */ 1178e48354ceSNicholas Bellinger tiqn = iscsit_get_tiqn_for_login(t_buf); 1179e48354ceSNicholas Bellinger if (!tiqn) { 1180e48354ceSNicholas Bellinger pr_err("Unable to locate Target IQN: %s in" 1181e48354ceSNicholas Bellinger " Storage Node\n", t_buf); 1182e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1183e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1184e48354ceSNicholas Bellinger ret = -1; 1185e48354ceSNicholas Bellinger goto out; 1186e48354ceSNicholas Bellinger } 1187e48354ceSNicholas Bellinger pr_debug("Located Storage Object: %s\n", tiqn->tiqn); 1188e48354ceSNicholas Bellinger 1189e48354ceSNicholas Bellinger /* 1190e48354ceSNicholas Bellinger * Locate Target Portal Group from Storage Node. 1191e48354ceSNicholas Bellinger */ 1192d381a801SNicholas Bellinger conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np); 1193e48354ceSNicholas Bellinger if (!conn->tpg) { 1194e48354ceSNicholas Bellinger pr_err("Unable to locate Target Portal Group" 1195e48354ceSNicholas Bellinger " on %s\n", tiqn->tiqn); 1196e48354ceSNicholas Bellinger iscsit_put_tiqn_for_login(tiqn); 1197e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1198e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1199e48354ceSNicholas Bellinger ret = -1; 1200e48354ceSNicholas Bellinger goto out; 1201e48354ceSNicholas Bellinger } 1202d381a801SNicholas Bellinger conn->tpg_np = tpg_np; 1203e48354ceSNicholas Bellinger pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt); 1204e48354ceSNicholas Bellinger /* 1205e48354ceSNicholas Bellinger * Setup crc32c modules from libcrypto 1206e48354ceSNicholas Bellinger */ 1207e48354ceSNicholas Bellinger if (iscsi_login_setup_crypto(conn) < 0) { 1208e48354ceSNicholas Bellinger pr_err("iscsi_login_setup_crypto() failed\n"); 1209d381a801SNicholas Bellinger kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); 1210d381a801SNicholas Bellinger iscsit_put_tiqn_for_login(tiqn); 1211d381a801SNicholas Bellinger conn->tpg = NULL; 1212e48354ceSNicholas Bellinger ret = -1; 1213e48354ceSNicholas Bellinger goto out; 1214e48354ceSNicholas Bellinger } 1215e48354ceSNicholas Bellinger /* 1216e48354ceSNicholas Bellinger * Serialize access across the struct iscsi_portal_group to 1217e48354ceSNicholas Bellinger * process login attempt. 1218e48354ceSNicholas Bellinger */ 1219e48354ceSNicholas Bellinger if (iscsit_access_np(np, conn->tpg) < 0) { 1220d381a801SNicholas Bellinger kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); 1221e48354ceSNicholas Bellinger iscsit_put_tiqn_for_login(tiqn); 1222e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1223e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1224e48354ceSNicholas Bellinger conn->tpg = NULL; 1225d381a801SNicholas Bellinger ret = -1; 1226e48354ceSNicholas Bellinger goto out; 1227e48354ceSNicholas Bellinger } 1228e48354ceSNicholas Bellinger 1229e48354ceSNicholas Bellinger /* 1230e48354ceSNicholas Bellinger * conn->sess->node_acl will be set when the referenced 1231e48354ceSNicholas Bellinger * struct iscsi_session is located from received ISID+TSIH in 1232e48354ceSNicholas Bellinger * iscsi_login_non_zero_tsih_s2(). 1233e48354ceSNicholas Bellinger */ 1234e48354ceSNicholas Bellinger if (!login->leading_connection) { 1235e48354ceSNicholas Bellinger ret = 0; 1236e48354ceSNicholas Bellinger goto out; 1237e48354ceSNicholas Bellinger } 1238e48354ceSNicholas Bellinger 1239e48354ceSNicholas Bellinger /* 1240e48354ceSNicholas Bellinger * This value is required in iscsi_login_zero_tsih_s2() 1241e48354ceSNicholas Bellinger */ 1242e48354ceSNicholas Bellinger sess->sess_ops->SessionType = 0; 1243e48354ceSNicholas Bellinger 1244e48354ceSNicholas Bellinger /* 1245e48354ceSNicholas Bellinger * Locate incoming Initiator IQN reference from Storage Node. 1246e48354ceSNicholas Bellinger */ 1247e48354ceSNicholas Bellinger sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl( 1248e48354ceSNicholas Bellinger &conn->tpg->tpg_se_tpg, i_buf); 1249e48354ceSNicholas Bellinger if (!sess->se_sess->se_node_acl) { 1250e48354ceSNicholas Bellinger pr_err("iSCSI Initiator Node: %s is not authorized to" 1251e48354ceSNicholas Bellinger " access iSCSI target portal group: %hu.\n", 1252e48354ceSNicholas Bellinger i_buf, conn->tpg->tpgt); 1253e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1254e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_TGT_FORBIDDEN); 1255e48354ceSNicholas Bellinger ret = -1; 1256e48354ceSNicholas Bellinger goto out; 1257e48354ceSNicholas Bellinger } 1258988e3a85SNicholas Bellinger se_nacl = sess->se_sess->se_node_acl; 1259988e3a85SNicholas Bellinger queue_depth = se_nacl->queue_depth; 1260988e3a85SNicholas Bellinger /* 1261988e3a85SNicholas Bellinger * Setup pre-allocated tags based upon allowed per NodeACL CmdSN 1262988e3a85SNicholas Bellinger * depth for non immediate commands, plus extra tags for immediate 1263988e3a85SNicholas Bellinger * commands. 1264988e3a85SNicholas Bellinger * 1265988e3a85SNicholas Bellinger * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention 1266988e3a85SNicholas Bellinger * in per-cpu-ida tag allocation logic + small queue_depth. 1267988e3a85SNicholas Bellinger */ 1268988e3a85SNicholas Bellinger alloc_tags: 1269988e3a85SNicholas Bellinger tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth); 12704a4caa29SNicholas Bellinger tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS; 1271988e3a85SNicholas Bellinger tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size; 1272e48354ceSNicholas Bellinger 1273988e3a85SNicholas Bellinger ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size); 1274988e3a85SNicholas Bellinger if (ret < 0) { 1275988e3a85SNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1276988e3a85SNicholas Bellinger ISCSI_LOGIN_STATUS_NO_RESOURCES); 1277988e3a85SNicholas Bellinger ret = -1; 1278988e3a85SNicholas Bellinger } 1279e48354ceSNicholas Bellinger out: 1280e48354ceSNicholas Bellinger kfree(tmpbuf); 1281e48354ceSNicholas Bellinger return ret; 1282e48354ceSNicholas Bellinger } 1283e48354ceSNicholas Bellinger 1284e48354ceSNicholas Bellinger int iscsi_target_start_negotiation( 1285e48354ceSNicholas Bellinger struct iscsi_login *login, 1286e48354ceSNicholas Bellinger struct iscsi_conn *conn) 1287e48354ceSNicholas Bellinger { 1288baa4d64bSNicholas Bellinger int ret; 1289e48354ceSNicholas Bellinger 1290d381a801SNicholas Bellinger if (conn->sock) { 1291d381a801SNicholas Bellinger struct sock *sk = conn->sock->sk; 1292e48354ceSNicholas Bellinger 1293d381a801SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 1294d381a801SNicholas Bellinger set_bit(LOGIN_FLAGS_READY, &conn->login_flags); 129525cdda95SNicholas Bellinger set_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags); 1296d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 1297d381a801SNicholas Bellinger } 129825cdda95SNicholas Bellinger /* 129925cdda95SNicholas Bellinger * If iscsi_target_do_login returns zero to signal more PDU 130025cdda95SNicholas Bellinger * exchanges are required to complete the login, go ahead and 130125cdda95SNicholas Bellinger * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection 130225cdda95SNicholas Bellinger * is still active. 130325cdda95SNicholas Bellinger * 130425cdda95SNicholas Bellinger * Otherwise if TCP connection dropped asynchronously, go ahead 130525cdda95SNicholas Bellinger * and perform connection cleanup now. 130625cdda95SNicholas Bellinger */ 13078f0dfb3dSNicholas Bellinger ret = iscsi_target_do_login(conn, login); 130825cdda95SNicholas Bellinger if (!ret && iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_INITIAL_PDU)) 130925cdda95SNicholas Bellinger ret = -1; 131025cdda95SNicholas Bellinger 13118f0dfb3dSNicholas Bellinger if (ret < 0) { 1312d381a801SNicholas Bellinger cancel_delayed_work_sync(&conn->login_work); 1313d381a801SNicholas Bellinger iscsi_target_restore_sock_callbacks(conn); 1314d381a801SNicholas Bellinger iscsi_remove_failed_auth_entry(conn); 1315d381a801SNicholas Bellinger } 1316d381a801SNicholas Bellinger if (ret != 0) 1317baa4d64bSNicholas Bellinger iscsi_target_nego_release(conn); 1318d381a801SNicholas Bellinger 1319e48354ceSNicholas Bellinger return ret; 1320e48354ceSNicholas Bellinger } 1321e48354ceSNicholas Bellinger 1322baa4d64bSNicholas Bellinger void iscsi_target_nego_release(struct iscsi_conn *conn) 1323e48354ceSNicholas Bellinger { 1324baa4d64bSNicholas Bellinger struct iscsi_login *login = conn->conn_login; 1325baa4d64bSNicholas Bellinger 1326baa4d64bSNicholas Bellinger if (!login) 1327baa4d64bSNicholas Bellinger return; 1328baa4d64bSNicholas Bellinger 1329e48354ceSNicholas Bellinger kfree(login->req_buf); 1330e48354ceSNicholas Bellinger kfree(login->rsp_buf); 1331e48354ceSNicholas Bellinger kfree(login); 1332baa4d64bSNicholas Bellinger 1333baa4d64bSNicholas Bellinger conn->conn_login = NULL; 1334e48354ceSNicholas Bellinger } 1335