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