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 } 272e48354ceSNicholas Bellinger 273e48354ceSNicholas Bellinger static int iscsi_target_check_first_request( 274e48354ceSNicholas Bellinger struct iscsi_conn *conn, 275e48354ceSNicholas Bellinger struct iscsi_login *login) 276e48354ceSNicholas Bellinger { 277e48354ceSNicholas Bellinger struct iscsi_param *param = NULL; 278e48354ceSNicholas Bellinger struct se_node_acl *se_nacl; 279e48354ceSNicholas Bellinger 280e48354ceSNicholas Bellinger login->first_request = 0; 281e48354ceSNicholas Bellinger 282e48354ceSNicholas Bellinger list_for_each_entry(param, &conn->param_list->param_list, p_list) { 283e48354ceSNicholas Bellinger if (!strncmp(param->name, SESSIONTYPE, 11)) { 284e48354ceSNicholas Bellinger if (!IS_PSTATE_ACCEPTOR(param)) { 285e48354ceSNicholas Bellinger pr_err("SessionType key not received" 286e48354ceSNicholas Bellinger " in first login request.\n"); 287e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 288e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_MISSING_FIELDS); 289e48354ceSNicholas Bellinger return -1; 290e48354ceSNicholas Bellinger } 291e48354ceSNicholas Bellinger if (!strncmp(param->value, DISCOVERY, 9)) 292e48354ceSNicholas Bellinger return 0; 293e48354ceSNicholas Bellinger } 294e48354ceSNicholas Bellinger 295e48354ceSNicholas Bellinger if (!strncmp(param->name, INITIATORNAME, 13)) { 296e48354ceSNicholas Bellinger if (!IS_PSTATE_ACCEPTOR(param)) { 297e48354ceSNicholas Bellinger if (!login->leading_connection) 298e48354ceSNicholas Bellinger continue; 299e48354ceSNicholas Bellinger 300e48354ceSNicholas Bellinger pr_err("InitiatorName key not received" 301e48354ceSNicholas Bellinger " in first login request.\n"); 302e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 303e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_MISSING_FIELDS); 304e48354ceSNicholas Bellinger return -1; 305e48354ceSNicholas Bellinger } 306e48354ceSNicholas Bellinger 307e48354ceSNicholas Bellinger /* 308e48354ceSNicholas Bellinger * For non-leading connections, double check that the 309e48354ceSNicholas Bellinger * received InitiatorName matches the existing session's 310e48354ceSNicholas Bellinger * struct iscsi_node_acl. 311e48354ceSNicholas Bellinger */ 312e48354ceSNicholas Bellinger if (!login->leading_connection) { 313e48354ceSNicholas Bellinger se_nacl = conn->sess->se_sess->se_node_acl; 314e48354ceSNicholas Bellinger if (!se_nacl) { 315e48354ceSNicholas Bellinger pr_err("Unable to locate" 316e48354ceSNicholas Bellinger " struct se_node_acl\n"); 317e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, 318e48354ceSNicholas Bellinger ISCSI_STATUS_CLS_INITIATOR_ERR, 319e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_TGT_NOT_FOUND); 320e48354ceSNicholas Bellinger return -1; 321e48354ceSNicholas Bellinger } 322e48354ceSNicholas Bellinger 323e48354ceSNicholas Bellinger if (strcmp(param->value, 324e48354ceSNicholas Bellinger se_nacl->initiatorname)) { 325e48354ceSNicholas Bellinger pr_err("Incorrect" 326e48354ceSNicholas Bellinger " InitiatorName: %s for this" 327e48354ceSNicholas Bellinger " iSCSI Initiator Node.\n", 328e48354ceSNicholas Bellinger param->value); 329e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, 330e48354ceSNicholas Bellinger ISCSI_STATUS_CLS_INITIATOR_ERR, 331e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_TGT_NOT_FOUND); 332e48354ceSNicholas Bellinger return -1; 333e48354ceSNicholas Bellinger } 334e48354ceSNicholas Bellinger } 335e48354ceSNicholas Bellinger } 336e48354ceSNicholas Bellinger } 337e48354ceSNicholas Bellinger 338e48354ceSNicholas Bellinger return 0; 339e48354ceSNicholas Bellinger } 340e48354ceSNicholas Bellinger 341e48354ceSNicholas Bellinger static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login) 342e48354ceSNicholas Bellinger { 343e48354ceSNicholas Bellinger u32 padding = 0; 344e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 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); 356e48354ceSNicholas Bellinger login_rsp->max_cmdsn = cpu_to_be32(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 mutex_lock(&sess->cmdsn_mutex); 38650e5c87dSChristoph Hellwig login_rsp->exp_cmdsn = cpu_to_be32(sess->exp_cmd_sn); 38750e5c87dSChristoph Hellwig login_rsp->max_cmdsn = cpu_to_be32(sess->max_cmd_sn); 388e48354ceSNicholas Bellinger mutex_unlock(&sess->cmdsn_mutex); 389e48354ceSNicholas Bellinger 390e48354ceSNicholas Bellinger return 0; 391e5419865SNicholas Bellinger 392e5419865SNicholas Bellinger err: 393e5419865SNicholas Bellinger if (login->login_complete) { 394e5419865SNicholas Bellinger if (conn->rx_thread && conn->rx_thread_active) { 395e5419865SNicholas Bellinger send_sig(SIGINT, conn->rx_thread, 1); 396e5419865SNicholas Bellinger kthread_stop(conn->rx_thread); 397e5419865SNicholas Bellinger } 398e5419865SNicholas Bellinger if (conn->tx_thread && conn->tx_thread_active) { 399e5419865SNicholas Bellinger send_sig(SIGINT, conn->tx_thread, 1); 400e5419865SNicholas Bellinger kthread_stop(conn->tx_thread); 401e5419865SNicholas Bellinger } 402e5419865SNicholas Bellinger spin_lock(&iscsit_global->ts_bitmap_lock); 403e5419865SNicholas Bellinger bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id, 404e5419865SNicholas Bellinger get_order(1)); 405e5419865SNicholas Bellinger spin_unlock(&iscsit_global->ts_bitmap_lock); 406e5419865SNicholas Bellinger } 407e5419865SNicholas Bellinger return -1; 408e48354ceSNicholas Bellinger } 409e48354ceSNicholas Bellinger 410676d2369SDavid S. Miller static void iscsi_target_sk_data_ready(struct sock *sk) 411d381a801SNicholas Bellinger { 412d381a801SNicholas Bellinger struct iscsi_conn *conn = sk->sk_user_data; 413d381a801SNicholas Bellinger bool rc; 414d381a801SNicholas Bellinger 415d381a801SNicholas Bellinger pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn); 416d381a801SNicholas Bellinger 417d381a801SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 418d381a801SNicholas Bellinger if (!sk->sk_user_data) { 419d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 420d381a801SNicholas Bellinger return; 421d381a801SNicholas Bellinger } 422d381a801SNicholas Bellinger if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) { 423d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 424d381a801SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn); 425d381a801SNicholas Bellinger return; 426d381a801SNicholas Bellinger } 427d381a801SNicholas Bellinger if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) { 428d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 429d381a801SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn); 430d381a801SNicholas Bellinger return; 431d381a801SNicholas Bellinger } 432d381a801SNicholas Bellinger if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) { 433d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 434d381a801SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn); 435d381a801SNicholas Bellinger return; 436d381a801SNicholas Bellinger } 437d381a801SNicholas Bellinger 438d381a801SNicholas Bellinger rc = schedule_delayed_work(&conn->login_work, 0); 4390bcc297eSChristophe Vu-Brugier if (!rc) { 440d381a801SNicholas Bellinger pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work" 441d381a801SNicholas Bellinger " got false\n"); 442d381a801SNicholas Bellinger } 443d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 444d381a801SNicholas Bellinger } 445d381a801SNicholas Bellinger 446bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *); 447bb048357SNicholas Bellinger 448d381a801SNicholas Bellinger static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn) 449d381a801SNicholas Bellinger { 450d381a801SNicholas Bellinger struct sock *sk; 451d381a801SNicholas Bellinger 452d381a801SNicholas Bellinger if (!conn->sock) 453d381a801SNicholas Bellinger return; 454d381a801SNicholas Bellinger 455d381a801SNicholas Bellinger sk = conn->sock->sk; 456d381a801SNicholas Bellinger pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn); 457d381a801SNicholas Bellinger 458d381a801SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 459d381a801SNicholas Bellinger sk->sk_user_data = conn; 460d381a801SNicholas Bellinger conn->orig_data_ready = sk->sk_data_ready; 461bb048357SNicholas Bellinger conn->orig_state_change = sk->sk_state_change; 462d381a801SNicholas Bellinger sk->sk_data_ready = iscsi_target_sk_data_ready; 463bb048357SNicholas Bellinger sk->sk_state_change = iscsi_target_sk_state_change; 464d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 465bb048357SNicholas Bellinger 466bb048357SNicholas Bellinger sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ; 467bb048357SNicholas Bellinger sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ; 468d381a801SNicholas Bellinger } 469d381a801SNicholas Bellinger 470d381a801SNicholas Bellinger static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn) 471d381a801SNicholas Bellinger { 472d381a801SNicholas Bellinger struct sock *sk; 473d381a801SNicholas Bellinger 474d381a801SNicholas Bellinger if (!conn->sock) 475d381a801SNicholas Bellinger return; 476d381a801SNicholas Bellinger 477d381a801SNicholas Bellinger sk = conn->sock->sk; 478d381a801SNicholas Bellinger pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn); 479d381a801SNicholas Bellinger 480d381a801SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 481d381a801SNicholas Bellinger if (!sk->sk_user_data) { 482d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 483d381a801SNicholas Bellinger return; 484d381a801SNicholas Bellinger } 485d381a801SNicholas Bellinger sk->sk_user_data = NULL; 486d381a801SNicholas Bellinger sk->sk_data_ready = conn->orig_data_ready; 487bb048357SNicholas Bellinger sk->sk_state_change = conn->orig_state_change; 488d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 489bb048357SNicholas Bellinger 490bb048357SNicholas Bellinger sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; 491bb048357SNicholas Bellinger sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; 492d381a801SNicholas Bellinger } 493d381a801SNicholas Bellinger 494d381a801SNicholas Bellinger static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *); 495d381a801SNicholas Bellinger 496d381a801SNicholas Bellinger static bool iscsi_target_sk_state_check(struct sock *sk) 497d381a801SNicholas Bellinger { 498d381a801SNicholas Bellinger if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) { 499d381a801SNicholas Bellinger pr_debug("iscsi_target_sk_state_check: TCP_CLOSE_WAIT|TCP_CLOSE," 500d381a801SNicholas Bellinger "returning FALSE\n"); 501d381a801SNicholas Bellinger return false; 502d381a801SNicholas Bellinger } 503d381a801SNicholas Bellinger return true; 504d381a801SNicholas Bellinger } 505d381a801SNicholas Bellinger 506d381a801SNicholas Bellinger static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login) 507d381a801SNicholas Bellinger { 508d381a801SNicholas Bellinger struct iscsi_np *np = login->np; 509d381a801SNicholas Bellinger bool zero_tsih = login->zero_tsih; 510d381a801SNicholas Bellinger 511d381a801SNicholas Bellinger iscsi_remove_failed_auth_entry(conn); 512d381a801SNicholas Bellinger iscsi_target_nego_release(conn); 513d381a801SNicholas Bellinger iscsi_target_login_sess_out(conn, np, zero_tsih, true); 514d381a801SNicholas Bellinger } 515d381a801SNicholas Bellinger 516d381a801SNicholas Bellinger static void iscsi_target_login_timeout(unsigned long data) 517d381a801SNicholas Bellinger { 518d381a801SNicholas Bellinger struct iscsi_conn *conn = (struct iscsi_conn *)data; 519d381a801SNicholas Bellinger 520d381a801SNicholas Bellinger pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n"); 521d381a801SNicholas Bellinger 522d381a801SNicholas Bellinger if (conn->login_kworker) { 523d381a801SNicholas Bellinger pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n", 524d381a801SNicholas Bellinger conn->login_kworker->comm, conn->login_kworker->pid); 525d381a801SNicholas Bellinger send_sig(SIGINT, conn->login_kworker, 1); 526d381a801SNicholas Bellinger } 527d381a801SNicholas Bellinger } 528d381a801SNicholas Bellinger 529d381a801SNicholas Bellinger static void iscsi_target_do_login_rx(struct work_struct *work) 530d381a801SNicholas Bellinger { 531d381a801SNicholas Bellinger struct iscsi_conn *conn = container_of(work, 532d381a801SNicholas Bellinger struct iscsi_conn, login_work.work); 533d381a801SNicholas Bellinger struct iscsi_login *login = conn->login; 534d381a801SNicholas Bellinger struct iscsi_np *np = login->np; 535d381a801SNicholas Bellinger struct iscsi_portal_group *tpg = conn->tpg; 536d381a801SNicholas Bellinger struct iscsi_tpg_np *tpg_np = conn->tpg_np; 537d381a801SNicholas Bellinger struct timer_list login_timer; 538d381a801SNicholas Bellinger int rc, zero_tsih = login->zero_tsih; 539d381a801SNicholas Bellinger bool state; 540d381a801SNicholas Bellinger 541d381a801SNicholas Bellinger pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n", 542d381a801SNicholas Bellinger conn, current->comm, current->pid); 543d381a801SNicholas Bellinger 544d381a801SNicholas Bellinger spin_lock(&tpg->tpg_state_lock); 545d381a801SNicholas Bellinger state = (tpg->tpg_state == TPG_STATE_ACTIVE); 546d381a801SNicholas Bellinger spin_unlock(&tpg->tpg_state_lock); 547d381a801SNicholas Bellinger 5480bcc297eSChristophe Vu-Brugier if (!state) { 549d381a801SNicholas Bellinger pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n"); 550d381a801SNicholas Bellinger iscsi_target_restore_sock_callbacks(conn); 551d381a801SNicholas Bellinger iscsi_target_login_drop(conn, login); 552d381a801SNicholas Bellinger iscsit_deaccess_np(np, tpg, tpg_np); 553d381a801SNicholas Bellinger return; 554d381a801SNicholas Bellinger } 555d381a801SNicholas Bellinger 556d381a801SNicholas Bellinger if (conn->sock) { 557d381a801SNicholas Bellinger struct sock *sk = conn->sock->sk; 558d381a801SNicholas Bellinger 559d381a801SNicholas Bellinger read_lock_bh(&sk->sk_callback_lock); 560d381a801SNicholas Bellinger state = iscsi_target_sk_state_check(sk); 561d381a801SNicholas Bellinger read_unlock_bh(&sk->sk_callback_lock); 562d381a801SNicholas Bellinger 5630bcc297eSChristophe Vu-Brugier if (!state) { 564d381a801SNicholas Bellinger pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n"); 565d381a801SNicholas Bellinger iscsi_target_restore_sock_callbacks(conn); 566d381a801SNicholas Bellinger iscsi_target_login_drop(conn, login); 567d381a801SNicholas Bellinger iscsit_deaccess_np(np, tpg, tpg_np); 568d381a801SNicholas Bellinger return; 569d381a801SNicholas Bellinger } 570d381a801SNicholas Bellinger } 571d381a801SNicholas Bellinger 572d381a801SNicholas Bellinger conn->login_kworker = current; 573d381a801SNicholas Bellinger allow_signal(SIGINT); 574d381a801SNicholas Bellinger 575d381a801SNicholas Bellinger init_timer(&login_timer); 576d381a801SNicholas Bellinger login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ); 577d381a801SNicholas Bellinger login_timer.data = (unsigned long)conn; 578d381a801SNicholas Bellinger login_timer.function = iscsi_target_login_timeout; 579d381a801SNicholas Bellinger add_timer(&login_timer); 580d381a801SNicholas Bellinger pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid); 581d381a801SNicholas Bellinger 582d381a801SNicholas Bellinger rc = conn->conn_transport->iscsit_get_login_rx(conn, login); 583d381a801SNicholas Bellinger del_timer_sync(&login_timer); 584d381a801SNicholas Bellinger flush_signals(current); 585d381a801SNicholas Bellinger conn->login_kworker = NULL; 586d381a801SNicholas Bellinger 587d381a801SNicholas Bellinger if (rc < 0) { 588d381a801SNicholas Bellinger iscsi_target_restore_sock_callbacks(conn); 589d381a801SNicholas Bellinger iscsi_target_login_drop(conn, login); 590d381a801SNicholas Bellinger iscsit_deaccess_np(np, tpg, tpg_np); 591d381a801SNicholas Bellinger return; 592d381a801SNicholas Bellinger } 593d381a801SNicholas Bellinger 594d381a801SNicholas Bellinger pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n", 595d381a801SNicholas Bellinger conn, current->comm, current->pid); 596d381a801SNicholas Bellinger 597d381a801SNicholas Bellinger rc = iscsi_target_do_login(conn, login); 598d381a801SNicholas Bellinger if (rc < 0) { 599d381a801SNicholas Bellinger iscsi_target_restore_sock_callbacks(conn); 600d381a801SNicholas Bellinger iscsi_target_login_drop(conn, login); 601d381a801SNicholas Bellinger iscsit_deaccess_np(np, tpg, tpg_np); 602d381a801SNicholas Bellinger } else if (!rc) { 603d381a801SNicholas Bellinger if (conn->sock) { 604d381a801SNicholas Bellinger struct sock *sk = conn->sock->sk; 605d381a801SNicholas Bellinger 606d381a801SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 607d381a801SNicholas Bellinger clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags); 608d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 609d381a801SNicholas Bellinger } 610d381a801SNicholas Bellinger } else if (rc == 1) { 611d381a801SNicholas Bellinger iscsi_target_nego_release(conn); 612d381a801SNicholas Bellinger iscsi_post_login_handler(np, conn, zero_tsih); 613d381a801SNicholas Bellinger iscsit_deaccess_np(np, tpg, tpg_np); 614d381a801SNicholas Bellinger } 615d381a801SNicholas Bellinger } 616d381a801SNicholas Bellinger 617bb048357SNicholas Bellinger static void iscsi_target_do_cleanup(struct work_struct *work) 618bb048357SNicholas Bellinger { 619bb048357SNicholas Bellinger struct iscsi_conn *conn = container_of(work, 620bb048357SNicholas Bellinger struct iscsi_conn, login_cleanup_work.work); 621bb048357SNicholas Bellinger struct sock *sk = conn->sock->sk; 622bb048357SNicholas Bellinger struct iscsi_login *login = conn->login; 623bb048357SNicholas Bellinger struct iscsi_np *np = login->np; 624bb048357SNicholas Bellinger struct iscsi_portal_group *tpg = conn->tpg; 625bb048357SNicholas Bellinger struct iscsi_tpg_np *tpg_np = conn->tpg_np; 626bb048357SNicholas Bellinger 627bb048357SNicholas Bellinger pr_debug("Entering iscsi_target_do_cleanup\n"); 628bb048357SNicholas Bellinger 629bb048357SNicholas Bellinger cancel_delayed_work_sync(&conn->login_work); 630bb048357SNicholas Bellinger conn->orig_state_change(sk); 631bb048357SNicholas Bellinger 632bb048357SNicholas Bellinger iscsi_target_restore_sock_callbacks(conn); 633bb048357SNicholas Bellinger iscsi_target_login_drop(conn, login); 634bb048357SNicholas Bellinger iscsit_deaccess_np(np, tpg, tpg_np); 635bb048357SNicholas Bellinger 636bb048357SNicholas Bellinger pr_debug("iscsi_target_do_cleanup done()\n"); 637bb048357SNicholas Bellinger } 638bb048357SNicholas Bellinger 639bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *sk) 640bb048357SNicholas Bellinger { 641bb048357SNicholas Bellinger struct iscsi_conn *conn; 642bb048357SNicholas Bellinger void (*orig_state_change)(struct sock *); 643bb048357SNicholas Bellinger bool state; 644bb048357SNicholas Bellinger 645bb048357SNicholas Bellinger pr_debug("Entering iscsi_target_sk_state_change\n"); 646bb048357SNicholas Bellinger 647bb048357SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 648bb048357SNicholas Bellinger conn = sk->sk_user_data; 649bb048357SNicholas Bellinger if (!conn) { 650bb048357SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 651bb048357SNicholas Bellinger return; 652bb048357SNicholas Bellinger } 653bb048357SNicholas Bellinger orig_state_change = conn->orig_state_change; 654bb048357SNicholas Bellinger 655bb048357SNicholas Bellinger if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) { 656bb048357SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n", 657bb048357SNicholas Bellinger conn); 658bb048357SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 659bb048357SNicholas Bellinger orig_state_change(sk); 660bb048357SNicholas Bellinger return; 661bb048357SNicholas Bellinger } 662bb048357SNicholas Bellinger if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) { 663bb048357SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change" 664bb048357SNicholas Bellinger " conn: %p\n", conn); 665bb048357SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 666bb048357SNicholas Bellinger orig_state_change(sk); 667bb048357SNicholas Bellinger return; 668bb048357SNicholas Bellinger } 669bb048357SNicholas Bellinger if (test_and_set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) { 670bb048357SNicholas Bellinger pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n", 671bb048357SNicholas Bellinger conn); 672bb048357SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 673bb048357SNicholas Bellinger orig_state_change(sk); 674bb048357SNicholas Bellinger return; 675bb048357SNicholas Bellinger } 676bb048357SNicholas Bellinger 677bb048357SNicholas Bellinger state = iscsi_target_sk_state_check(sk); 678bb048357SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 679bb048357SNicholas Bellinger 680bb048357SNicholas Bellinger pr_debug("iscsi_target_sk_state_change: state: %d\n", state); 681bb048357SNicholas Bellinger 682bb048357SNicholas Bellinger if (!state) { 683bb048357SNicholas Bellinger pr_debug("iscsi_target_sk_state_change got failed state\n"); 684bb048357SNicholas Bellinger schedule_delayed_work(&conn->login_cleanup_work, 0); 685bb048357SNicholas Bellinger return; 686bb048357SNicholas Bellinger } 687bb048357SNicholas Bellinger orig_state_change(sk); 688bb048357SNicholas Bellinger } 689bb048357SNicholas Bellinger 690e48354ceSNicholas Bellinger /* 691e48354ceSNicholas Bellinger * NOTE: We check for existing sessions or connections AFTER the initiator 692e48354ceSNicholas Bellinger * has been successfully authenticated in order to protect against faked 693e48354ceSNicholas Bellinger * ISID/TSIH combinations. 694e48354ceSNicholas Bellinger */ 695e48354ceSNicholas Bellinger static int iscsi_target_check_for_existing_instances( 696e48354ceSNicholas Bellinger struct iscsi_conn *conn, 697e48354ceSNicholas Bellinger struct iscsi_login *login) 698e48354ceSNicholas Bellinger { 699e48354ceSNicholas Bellinger if (login->checked_for_existing) 700e48354ceSNicholas Bellinger return 0; 701e48354ceSNicholas Bellinger 702e48354ceSNicholas Bellinger login->checked_for_existing = 1; 703e48354ceSNicholas Bellinger 704e48354ceSNicholas Bellinger if (!login->tsih) 705e48354ceSNicholas Bellinger return iscsi_check_for_session_reinstatement(conn); 706e48354ceSNicholas Bellinger else 707e48354ceSNicholas Bellinger return iscsi_login_post_auth_non_zero_tsih(conn, login->cid, 708e48354ceSNicholas Bellinger login->initial_exp_statsn); 709e48354ceSNicholas Bellinger } 710e48354ceSNicholas Bellinger 711e48354ceSNicholas Bellinger static int iscsi_target_do_authentication( 712e48354ceSNicholas Bellinger struct iscsi_conn *conn, 713e48354ceSNicholas Bellinger struct iscsi_login *login) 714e48354ceSNicholas Bellinger { 715e48354ceSNicholas Bellinger int authret; 716e48354ceSNicholas Bellinger u32 payload_length; 717e48354ceSNicholas Bellinger struct iscsi_param *param; 718e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 719e48354ceSNicholas Bellinger struct iscsi_login_rsp *login_rsp; 720e48354ceSNicholas Bellinger 721e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 722e48354ceSNicholas Bellinger login_rsp = (struct iscsi_login_rsp *) login->rsp; 723e48354ceSNicholas Bellinger payload_length = ntoh24(login_req->dlength); 724e48354ceSNicholas Bellinger 725e48354ceSNicholas Bellinger param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list); 726e48354ceSNicholas Bellinger if (!param) 727e48354ceSNicholas Bellinger return -1; 728e48354ceSNicholas Bellinger 729e48354ceSNicholas Bellinger authret = iscsi_handle_authentication( 730e48354ceSNicholas Bellinger conn, 731e48354ceSNicholas Bellinger login->req_buf, 732e48354ceSNicholas Bellinger login->rsp_buf, 733e48354ceSNicholas Bellinger payload_length, 734e48354ceSNicholas Bellinger &login->rsp_length, 735e48354ceSNicholas Bellinger param->value); 736e48354ceSNicholas Bellinger switch (authret) { 737e48354ceSNicholas Bellinger case 0: 738e48354ceSNicholas Bellinger pr_debug("Received OK response" 739e48354ceSNicholas Bellinger " from LIO Authentication, continuing.\n"); 740e48354ceSNicholas Bellinger break; 741e48354ceSNicholas Bellinger case 1: 742e48354ceSNicholas Bellinger pr_debug("iSCSI security negotiation" 743bfb9035cSJoe Perches " completed successfully.\n"); 744e48354ceSNicholas Bellinger login->auth_complete = 1; 745e48354ceSNicholas Bellinger if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) && 746e48354ceSNicholas Bellinger (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) { 747e48354ceSNicholas Bellinger login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 | 748e48354ceSNicholas Bellinger ISCSI_FLAG_LOGIN_TRANSIT); 749e48354ceSNicholas Bellinger login->current_stage = 1; 750e48354ceSNicholas Bellinger } 751e48354ceSNicholas Bellinger return iscsi_target_check_for_existing_instances( 752e48354ceSNicholas Bellinger conn, login); 753e48354ceSNicholas Bellinger case 2: 754e48354ceSNicholas Bellinger pr_err("Security negotiation" 755e48354ceSNicholas Bellinger " failed.\n"); 756e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 757e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_AUTH_FAILED); 758e48354ceSNicholas Bellinger return -1; 759e48354ceSNicholas Bellinger default: 760e48354ceSNicholas Bellinger pr_err("Received unknown error %d from LIO" 761e48354ceSNicholas Bellinger " Authentication\n", authret); 762e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 763e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_TARGET_ERROR); 764e48354ceSNicholas Bellinger return -1; 765e48354ceSNicholas Bellinger } 766e48354ceSNicholas Bellinger 767e48354ceSNicholas Bellinger return 0; 768e48354ceSNicholas Bellinger } 769e48354ceSNicholas Bellinger 770e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_zero( 771e48354ceSNicholas Bellinger struct iscsi_conn *conn, 772e48354ceSNicholas Bellinger struct iscsi_login *login) 773e48354ceSNicholas Bellinger { 774e48354ceSNicholas Bellinger int ret; 775e48354ceSNicholas Bellinger u32 payload_length; 776e48354ceSNicholas Bellinger struct iscsi_param *param; 777e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 778e48354ceSNicholas Bellinger struct iscsi_login_rsp *login_rsp; 779e48354ceSNicholas Bellinger 780e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 781e48354ceSNicholas Bellinger login_rsp = (struct iscsi_login_rsp *) login->rsp; 782e48354ceSNicholas Bellinger payload_length = ntoh24(login_req->dlength); 783e48354ceSNicholas Bellinger 784e48354ceSNicholas Bellinger param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list); 785e48354ceSNicholas Bellinger if (!param) 786e48354ceSNicholas Bellinger return -1; 787e48354ceSNicholas Bellinger 788e48354ceSNicholas Bellinger ret = iscsi_decode_text_input( 789e48354ceSNicholas Bellinger PHASE_SECURITY|PHASE_DECLARATIVE, 790e48354ceSNicholas Bellinger SENDER_INITIATOR|SENDER_RECEIVER, 791e48354ceSNicholas Bellinger login->req_buf, 792e48354ceSNicholas Bellinger payload_length, 7939977bb18SNicholas Bellinger conn); 794e48354ceSNicholas Bellinger if (ret < 0) 795e48354ceSNicholas Bellinger return -1; 796e48354ceSNicholas Bellinger 797e48354ceSNicholas Bellinger if (ret > 0) { 798e48354ceSNicholas Bellinger if (login->auth_complete) { 799e48354ceSNicholas Bellinger pr_err("Initiator has already been" 800e48354ceSNicholas Bellinger " successfully authenticated, but is still" 801e48354ceSNicholas Bellinger " sending %s keys.\n", param->value); 802e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 803e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_INIT_ERR); 804e48354ceSNicholas Bellinger return -1; 805e48354ceSNicholas Bellinger } 806e48354ceSNicholas Bellinger 807e48354ceSNicholas Bellinger goto do_auth; 80891f0abfdSNicholas Bellinger } else if (!payload_length) { 80991f0abfdSNicholas Bellinger pr_err("Initiator sent zero length security payload," 81091f0abfdSNicholas Bellinger " login failed\n"); 81191f0abfdSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 81291f0abfdSNicholas Bellinger ISCSI_LOGIN_STATUS_AUTH_FAILED); 81391f0abfdSNicholas Bellinger return -1; 814e48354ceSNicholas Bellinger } 815e48354ceSNicholas Bellinger 816e48354ceSNicholas Bellinger if (login->first_request) 817e48354ceSNicholas Bellinger if (iscsi_target_check_first_request(conn, login) < 0) 818e48354ceSNicholas Bellinger return -1; 819e48354ceSNicholas Bellinger 820e48354ceSNicholas Bellinger ret = iscsi_encode_text_output( 821e48354ceSNicholas Bellinger PHASE_SECURITY|PHASE_DECLARATIVE, 822e48354ceSNicholas Bellinger SENDER_TARGET, 823e48354ceSNicholas Bellinger login->rsp_buf, 824e48354ceSNicholas Bellinger &login->rsp_length, 825e48354ceSNicholas Bellinger conn->param_list); 826e48354ceSNicholas Bellinger if (ret < 0) 827e48354ceSNicholas Bellinger return -1; 828e48354ceSNicholas Bellinger 829e48354ceSNicholas Bellinger if (!iscsi_check_negotiated_keys(conn->param_list)) { 83060bfcf8eSAndy Grover if (conn->tpg->tpg_attrib.authentication && 831e48354ceSNicholas Bellinger !strncmp(param->value, NONE, 4)) { 832e48354ceSNicholas Bellinger pr_err("Initiator sent AuthMethod=None but" 833e48354ceSNicholas Bellinger " Target is enforcing iSCSI Authentication," 834e48354ceSNicholas Bellinger " login failed.\n"); 835e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 836e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_AUTH_FAILED); 837e48354ceSNicholas Bellinger return -1; 838e48354ceSNicholas Bellinger } 839e48354ceSNicholas Bellinger 84060bfcf8eSAndy Grover if (conn->tpg->tpg_attrib.authentication && 841e48354ceSNicholas Bellinger !login->auth_complete) 842e48354ceSNicholas Bellinger return 0; 843e48354ceSNicholas Bellinger 844e48354ceSNicholas Bellinger if (strncmp(param->value, NONE, 4) && !login->auth_complete) 845e48354ceSNicholas Bellinger return 0; 846e48354ceSNicholas Bellinger 847e48354ceSNicholas Bellinger if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) && 848e48354ceSNicholas Bellinger (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) { 849e48354ceSNicholas Bellinger login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 | 850e48354ceSNicholas Bellinger ISCSI_FLAG_LOGIN_TRANSIT; 851e48354ceSNicholas Bellinger login->current_stage = 1; 852e48354ceSNicholas Bellinger } 853e48354ceSNicholas Bellinger } 854e48354ceSNicholas Bellinger 855e48354ceSNicholas Bellinger return 0; 856e48354ceSNicholas Bellinger do_auth: 857e48354ceSNicholas Bellinger return iscsi_target_do_authentication(conn, login); 858e48354ceSNicholas Bellinger } 859e48354ceSNicholas Bellinger 860e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login) 861e48354ceSNicholas Bellinger { 862e48354ceSNicholas Bellinger int ret; 863e48354ceSNicholas Bellinger u32 payload_length; 864e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 865e48354ceSNicholas Bellinger struct iscsi_login_rsp *login_rsp; 866e48354ceSNicholas Bellinger 867e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 868e48354ceSNicholas Bellinger login_rsp = (struct iscsi_login_rsp *) login->rsp; 869e48354ceSNicholas Bellinger payload_length = ntoh24(login_req->dlength); 870e48354ceSNicholas Bellinger 871e48354ceSNicholas Bellinger ret = iscsi_decode_text_input( 872e48354ceSNicholas Bellinger PHASE_OPERATIONAL|PHASE_DECLARATIVE, 873e48354ceSNicholas Bellinger SENDER_INITIATOR|SENDER_RECEIVER, 874e48354ceSNicholas Bellinger login->req_buf, 875e48354ceSNicholas Bellinger payload_length, 8769977bb18SNicholas Bellinger conn); 8771c5c12c6SRoland Dreier if (ret < 0) { 8781c5c12c6SRoland Dreier iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 8791c5c12c6SRoland Dreier ISCSI_LOGIN_STATUS_INIT_ERR); 880e48354ceSNicholas Bellinger return -1; 8811c5c12c6SRoland Dreier } 882e48354ceSNicholas Bellinger 883e48354ceSNicholas Bellinger if (login->first_request) 884e48354ceSNicholas Bellinger if (iscsi_target_check_first_request(conn, login) < 0) 885e48354ceSNicholas Bellinger return -1; 886e48354ceSNicholas Bellinger 887e48354ceSNicholas Bellinger if (iscsi_target_check_for_existing_instances(conn, login) < 0) 888e48354ceSNicholas Bellinger return -1; 889e48354ceSNicholas Bellinger 890e48354ceSNicholas Bellinger ret = iscsi_encode_text_output( 891e48354ceSNicholas Bellinger PHASE_OPERATIONAL|PHASE_DECLARATIVE, 892e48354ceSNicholas Bellinger SENDER_TARGET, 893e48354ceSNicholas Bellinger login->rsp_buf, 894e48354ceSNicholas Bellinger &login->rsp_length, 895e48354ceSNicholas Bellinger conn->param_list); 8961c5c12c6SRoland Dreier if (ret < 0) { 8971c5c12c6SRoland Dreier iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 8981c5c12c6SRoland Dreier ISCSI_LOGIN_STATUS_INIT_ERR); 899e48354ceSNicholas Bellinger return -1; 9001c5c12c6SRoland Dreier } 901e48354ceSNicholas Bellinger 902e48354ceSNicholas Bellinger if (!login->auth_complete && 90360bfcf8eSAndy Grover conn->tpg->tpg_attrib.authentication) { 904e48354ceSNicholas Bellinger pr_err("Initiator is requesting CSG: 1, has not been" 905e48354ceSNicholas Bellinger " successfully authenticated, and the Target is" 906e48354ceSNicholas Bellinger " enforcing iSCSI Authentication, login failed.\n"); 907e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 908e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_AUTH_FAILED); 909e48354ceSNicholas Bellinger return -1; 910e48354ceSNicholas Bellinger } 911e48354ceSNicholas Bellinger 912e48354ceSNicholas Bellinger if (!iscsi_check_negotiated_keys(conn->param_list)) 913e48354ceSNicholas Bellinger if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) && 914e48354ceSNicholas Bellinger (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) 915e48354ceSNicholas Bellinger login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 | 916e48354ceSNicholas Bellinger ISCSI_FLAG_LOGIN_TRANSIT; 917e48354ceSNicholas Bellinger 918e48354ceSNicholas Bellinger return 0; 919e48354ceSNicholas Bellinger } 920e48354ceSNicholas Bellinger 921e48354ceSNicholas Bellinger static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login) 922e48354ceSNicholas Bellinger { 923e48354ceSNicholas Bellinger int pdu_count = 0; 924e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 925e48354ceSNicholas Bellinger struct iscsi_login_rsp *login_rsp; 926e48354ceSNicholas Bellinger 927e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 928e48354ceSNicholas Bellinger login_rsp = (struct iscsi_login_rsp *) login->rsp; 929e48354ceSNicholas Bellinger 930e48354ceSNicholas Bellinger while (1) { 931e48354ceSNicholas Bellinger if (++pdu_count > MAX_LOGIN_PDUS) { 932e48354ceSNicholas Bellinger pr_err("MAX_LOGIN_PDUS count reached.\n"); 933e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 934e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_TARGET_ERROR); 935e48354ceSNicholas Bellinger return -1; 936e48354ceSNicholas Bellinger } 937e48354ceSNicholas Bellinger 9385d358065SAndy Grover switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) { 939e48354ceSNicholas Bellinger case 0: 9405d358065SAndy Grover login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK; 941e48354ceSNicholas Bellinger if (iscsi_target_handle_csg_zero(conn, login) < 0) 942e48354ceSNicholas Bellinger return -1; 943e48354ceSNicholas Bellinger break; 944e48354ceSNicholas Bellinger case 1: 945e48354ceSNicholas Bellinger login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1; 946e48354ceSNicholas Bellinger if (iscsi_target_handle_csg_one(conn, login) < 0) 947e48354ceSNicholas Bellinger return -1; 948e48354ceSNicholas Bellinger if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) { 949e48354ceSNicholas Bellinger login->tsih = conn->sess->tsih; 950baa4d64bSNicholas Bellinger login->login_complete = 1; 951d381a801SNicholas Bellinger iscsi_target_restore_sock_callbacks(conn); 952e48354ceSNicholas Bellinger if (iscsi_target_do_tx_login_io(conn, 953e48354ceSNicholas Bellinger login) < 0) 954e48354ceSNicholas Bellinger return -1; 955d381a801SNicholas Bellinger return 1; 956e48354ceSNicholas Bellinger } 957e48354ceSNicholas Bellinger break; 958e48354ceSNicholas Bellinger default: 959e48354ceSNicholas Bellinger pr_err("Illegal CSG: %d received from" 960e48354ceSNicholas Bellinger " Initiator, protocol error.\n", 9615d358065SAndy Grover ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)); 962e48354ceSNicholas Bellinger break; 963e48354ceSNicholas Bellinger } 964e48354ceSNicholas Bellinger 965ea3a179aSNicholas Bellinger if (iscsi_target_do_tx_login_io(conn, login) < 0) 966e48354ceSNicholas Bellinger return -1; 967e48354ceSNicholas Bellinger 968e48354ceSNicholas Bellinger if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) { 969e48354ceSNicholas Bellinger login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT; 970e48354ceSNicholas Bellinger login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK; 971e48354ceSNicholas Bellinger } 972d381a801SNicholas Bellinger break; 973e48354ceSNicholas Bellinger } 974e48354ceSNicholas Bellinger 975bb048357SNicholas Bellinger if (conn->sock) { 976bb048357SNicholas Bellinger struct sock *sk = conn->sock->sk; 977bb048357SNicholas Bellinger bool state; 978bb048357SNicholas Bellinger 979bb048357SNicholas Bellinger read_lock_bh(&sk->sk_callback_lock); 980bb048357SNicholas Bellinger state = iscsi_target_sk_state_check(sk); 981bb048357SNicholas Bellinger read_unlock_bh(&sk->sk_callback_lock); 982bb048357SNicholas Bellinger 983bb048357SNicholas Bellinger if (!state) { 984bb048357SNicholas Bellinger pr_debug("iscsi_target_do_login() failed state for" 985bb048357SNicholas Bellinger " conn: %p\n", conn); 986bb048357SNicholas Bellinger return -1; 987bb048357SNicholas Bellinger } 988bb048357SNicholas Bellinger } 989bb048357SNicholas Bellinger 990e48354ceSNicholas Bellinger return 0; 991e48354ceSNicholas Bellinger } 992e48354ceSNicholas Bellinger 993e48354ceSNicholas Bellinger static void iscsi_initiatorname_tolower( 994e48354ceSNicholas Bellinger char *param_buf) 995e48354ceSNicholas Bellinger { 996e48354ceSNicholas Bellinger char *c; 997e48354ceSNicholas Bellinger u32 iqn_size = strlen(param_buf), i; 998e48354ceSNicholas Bellinger 999e48354ceSNicholas Bellinger for (i = 0; i < iqn_size; i++) { 10008359cf43SJörn Engel c = ¶m_buf[i]; 1001e48354ceSNicholas Bellinger if (!isupper(*c)) 1002e48354ceSNicholas Bellinger continue; 1003e48354ceSNicholas Bellinger 1004e48354ceSNicholas Bellinger *c = tolower(*c); 1005e48354ceSNicholas Bellinger } 1006e48354ceSNicholas Bellinger } 1007e48354ceSNicholas Bellinger 1008e48354ceSNicholas Bellinger /* 1009e48354ceSNicholas Bellinger * Processes the first Login Request.. 1010e48354ceSNicholas Bellinger */ 1011baa4d64bSNicholas Bellinger int iscsi_target_locate_portal( 1012e48354ceSNicholas Bellinger struct iscsi_np *np, 1013e48354ceSNicholas Bellinger struct iscsi_conn *conn, 1014e48354ceSNicholas Bellinger struct iscsi_login *login) 1015e48354ceSNicholas Bellinger { 1016e48354ceSNicholas Bellinger char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL; 1017e48354ceSNicholas Bellinger char *tmpbuf, *start = NULL, *end = NULL, *key, *value; 1018e48354ceSNicholas Bellinger struct iscsi_session *sess = conn->sess; 1019e48354ceSNicholas Bellinger struct iscsi_tiqn *tiqn; 1020d381a801SNicholas Bellinger struct iscsi_tpg_np *tpg_np = NULL; 1021e48354ceSNicholas Bellinger struct iscsi_login_req *login_req; 1022988e3a85SNicholas Bellinger struct se_node_acl *se_nacl; 1023988e3a85SNicholas Bellinger u32 payload_length, queue_depth = 0; 1024988e3a85SNicholas Bellinger int sessiontype = 0, ret = 0, tag_num, tag_size; 1025e48354ceSNicholas Bellinger 1026d381a801SNicholas Bellinger INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx); 1027bb048357SNicholas Bellinger INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup); 1028d381a801SNicholas Bellinger iscsi_target_set_sock_callbacks(conn); 1029d381a801SNicholas Bellinger 1030d381a801SNicholas Bellinger login->np = np; 1031d381a801SNicholas Bellinger 1032e48354ceSNicholas Bellinger login_req = (struct iscsi_login_req *) login->req; 1033e48354ceSNicholas Bellinger payload_length = ntoh24(login_req->dlength); 1034e48354ceSNicholas Bellinger 1035e48354ceSNicholas Bellinger tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL); 1036e48354ceSNicholas Bellinger if (!tmpbuf) { 1037e48354ceSNicholas Bellinger pr_err("Unable to allocate memory for tmpbuf.\n"); 1038e48354ceSNicholas Bellinger return -1; 1039e48354ceSNicholas Bellinger } 1040e48354ceSNicholas Bellinger 1041e48354ceSNicholas Bellinger memcpy(tmpbuf, login->req_buf, payload_length); 1042e48354ceSNicholas Bellinger tmpbuf[payload_length] = '\0'; 1043e48354ceSNicholas Bellinger start = tmpbuf; 1044e48354ceSNicholas Bellinger end = (start + payload_length); 1045e48354ceSNicholas Bellinger 1046e48354ceSNicholas Bellinger /* 1047e48354ceSNicholas Bellinger * Locate the initial keys expected from the Initiator node in 1048e48354ceSNicholas Bellinger * the first login request in order to progress with the login phase. 1049e48354ceSNicholas Bellinger */ 1050e48354ceSNicholas Bellinger while (start < end) { 1051e48354ceSNicholas Bellinger if (iscsi_extract_key_value(start, &key, &value) < 0) { 1052e48354ceSNicholas Bellinger ret = -1; 1053e48354ceSNicholas Bellinger goto out; 1054e48354ceSNicholas Bellinger } 1055e48354ceSNicholas Bellinger 1056e48354ceSNicholas Bellinger if (!strncmp(key, "InitiatorName", 13)) 1057e48354ceSNicholas Bellinger i_buf = value; 1058e48354ceSNicholas Bellinger else if (!strncmp(key, "SessionType", 11)) 1059e48354ceSNicholas Bellinger s_buf = value; 1060e48354ceSNicholas Bellinger else if (!strncmp(key, "TargetName", 10)) 1061e48354ceSNicholas Bellinger t_buf = value; 1062e48354ceSNicholas Bellinger 1063e48354ceSNicholas Bellinger start += strlen(key) + strlen(value) + 2; 1064e48354ceSNicholas Bellinger } 1065e48354ceSNicholas Bellinger /* 1066e48354ceSNicholas Bellinger * See 5.3. Login Phase. 1067e48354ceSNicholas Bellinger */ 1068e48354ceSNicholas Bellinger if (!i_buf) { 1069e48354ceSNicholas Bellinger pr_err("InitiatorName key not received" 1070e48354ceSNicholas Bellinger " in first login request.\n"); 1071e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1072e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_MISSING_FIELDS); 1073e48354ceSNicholas Bellinger ret = -1; 1074e48354ceSNicholas Bellinger goto out; 1075e48354ceSNicholas Bellinger } 1076e48354ceSNicholas Bellinger /* 1077e48354ceSNicholas Bellinger * Convert the incoming InitiatorName to lowercase following 1078e48354ceSNicholas Bellinger * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs 1079e48354ceSNicholas Bellinger * are NOT case sensitive. 1080e48354ceSNicholas Bellinger */ 1081e48354ceSNicholas Bellinger iscsi_initiatorname_tolower(i_buf); 1082e48354ceSNicholas Bellinger 1083e48354ceSNicholas Bellinger if (!s_buf) { 1084e48354ceSNicholas Bellinger if (!login->leading_connection) 1085e48354ceSNicholas Bellinger goto get_target; 1086e48354ceSNicholas Bellinger 1087e48354ceSNicholas Bellinger pr_err("SessionType key not received" 1088e48354ceSNicholas Bellinger " in first login request.\n"); 1089e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1090e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_MISSING_FIELDS); 1091e48354ceSNicholas Bellinger ret = -1; 1092e48354ceSNicholas Bellinger goto out; 1093e48354ceSNicholas Bellinger } 1094e48354ceSNicholas Bellinger 1095e48354ceSNicholas Bellinger /* 1096e48354ceSNicholas Bellinger * Use default portal group for discovery sessions. 1097e48354ceSNicholas Bellinger */ 1098e48354ceSNicholas Bellinger sessiontype = strncmp(s_buf, DISCOVERY, 9); 1099e48354ceSNicholas Bellinger if (!sessiontype) { 1100e48354ceSNicholas Bellinger conn->tpg = iscsit_global->discovery_tpg; 1101e48354ceSNicholas Bellinger if (!login->leading_connection) 1102e48354ceSNicholas Bellinger goto get_target; 1103e48354ceSNicholas Bellinger 1104e48354ceSNicholas Bellinger sess->sess_ops->SessionType = 1; 1105e48354ceSNicholas Bellinger /* 1106e48354ceSNicholas Bellinger * Setup crc32c modules from libcrypto 1107e48354ceSNicholas Bellinger */ 1108e48354ceSNicholas Bellinger if (iscsi_login_setup_crypto(conn) < 0) { 1109e48354ceSNicholas Bellinger pr_err("iscsi_login_setup_crypto() failed\n"); 1110e48354ceSNicholas Bellinger ret = -1; 1111e48354ceSNicholas Bellinger goto out; 1112e48354ceSNicholas Bellinger } 1113e48354ceSNicholas Bellinger /* 1114e48354ceSNicholas Bellinger * Serialize access across the discovery struct iscsi_portal_group to 1115e48354ceSNicholas Bellinger * process login attempt. 1116e48354ceSNicholas Bellinger */ 1117e48354ceSNicholas Bellinger if (iscsit_access_np(np, conn->tpg) < 0) { 1118e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1119e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1120e48354ceSNicholas Bellinger ret = -1; 1121e48354ceSNicholas Bellinger goto out; 1122e48354ceSNicholas Bellinger } 1123e48354ceSNicholas Bellinger ret = 0; 1124988e3a85SNicholas Bellinger goto alloc_tags; 1125e48354ceSNicholas Bellinger } 1126e48354ceSNicholas Bellinger 1127e48354ceSNicholas Bellinger get_target: 1128e48354ceSNicholas Bellinger if (!t_buf) { 1129e48354ceSNicholas Bellinger pr_err("TargetName key not received" 1130e48354ceSNicholas Bellinger " in first login request while" 1131e48354ceSNicholas Bellinger " SessionType=Normal.\n"); 1132e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1133e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_MISSING_FIELDS); 1134e48354ceSNicholas Bellinger ret = -1; 1135e48354ceSNicholas Bellinger goto out; 1136e48354ceSNicholas Bellinger } 1137e48354ceSNicholas Bellinger 1138e48354ceSNicholas Bellinger /* 1139e48354ceSNicholas Bellinger * Locate Target IQN from Storage Node. 1140e48354ceSNicholas Bellinger */ 1141e48354ceSNicholas Bellinger tiqn = iscsit_get_tiqn_for_login(t_buf); 1142e48354ceSNicholas Bellinger if (!tiqn) { 1143e48354ceSNicholas Bellinger pr_err("Unable to locate Target IQN: %s in" 1144e48354ceSNicholas Bellinger " Storage Node\n", t_buf); 1145e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1146e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1147e48354ceSNicholas Bellinger ret = -1; 1148e48354ceSNicholas Bellinger goto out; 1149e48354ceSNicholas Bellinger } 1150e48354ceSNicholas Bellinger pr_debug("Located Storage Object: %s\n", tiqn->tiqn); 1151e48354ceSNicholas Bellinger 1152e48354ceSNicholas Bellinger /* 1153e48354ceSNicholas Bellinger * Locate Target Portal Group from Storage Node. 1154e48354ceSNicholas Bellinger */ 1155d381a801SNicholas Bellinger conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np); 1156e48354ceSNicholas Bellinger if (!conn->tpg) { 1157e48354ceSNicholas Bellinger pr_err("Unable to locate Target Portal Group" 1158e48354ceSNicholas Bellinger " on %s\n", tiqn->tiqn); 1159e48354ceSNicholas Bellinger iscsit_put_tiqn_for_login(tiqn); 1160e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1161e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1162e48354ceSNicholas Bellinger ret = -1; 1163e48354ceSNicholas Bellinger goto out; 1164e48354ceSNicholas Bellinger } 1165d381a801SNicholas Bellinger conn->tpg_np = tpg_np; 1166e48354ceSNicholas Bellinger pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt); 1167e48354ceSNicholas Bellinger /* 1168e48354ceSNicholas Bellinger * Setup crc32c modules from libcrypto 1169e48354ceSNicholas Bellinger */ 1170e48354ceSNicholas Bellinger if (iscsi_login_setup_crypto(conn) < 0) { 1171e48354ceSNicholas Bellinger pr_err("iscsi_login_setup_crypto() failed\n"); 1172d381a801SNicholas Bellinger kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); 1173d381a801SNicholas Bellinger iscsit_put_tiqn_for_login(tiqn); 1174d381a801SNicholas Bellinger conn->tpg = NULL; 1175e48354ceSNicholas Bellinger ret = -1; 1176e48354ceSNicholas Bellinger goto out; 1177e48354ceSNicholas Bellinger } 1178e48354ceSNicholas Bellinger /* 1179e48354ceSNicholas Bellinger * Serialize access across the struct iscsi_portal_group to 1180e48354ceSNicholas Bellinger * process login attempt. 1181e48354ceSNicholas Bellinger */ 1182e48354ceSNicholas Bellinger if (iscsit_access_np(np, conn->tpg) < 0) { 1183d381a801SNicholas Bellinger kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); 1184e48354ceSNicholas Bellinger iscsit_put_tiqn_for_login(tiqn); 1185e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1186e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1187e48354ceSNicholas Bellinger conn->tpg = NULL; 1188d381a801SNicholas Bellinger ret = -1; 1189e48354ceSNicholas Bellinger goto out; 1190e48354ceSNicholas Bellinger } 1191e48354ceSNicholas Bellinger 1192e48354ceSNicholas Bellinger /* 1193e48354ceSNicholas Bellinger * conn->sess->node_acl will be set when the referenced 1194e48354ceSNicholas Bellinger * struct iscsi_session is located from received ISID+TSIH in 1195e48354ceSNicholas Bellinger * iscsi_login_non_zero_tsih_s2(). 1196e48354ceSNicholas Bellinger */ 1197e48354ceSNicholas Bellinger if (!login->leading_connection) { 1198e48354ceSNicholas Bellinger ret = 0; 1199e48354ceSNicholas Bellinger goto out; 1200e48354ceSNicholas Bellinger } 1201e48354ceSNicholas Bellinger 1202e48354ceSNicholas Bellinger /* 1203e48354ceSNicholas Bellinger * This value is required in iscsi_login_zero_tsih_s2() 1204e48354ceSNicholas Bellinger */ 1205e48354ceSNicholas Bellinger sess->sess_ops->SessionType = 0; 1206e48354ceSNicholas Bellinger 1207e48354ceSNicholas Bellinger /* 1208e48354ceSNicholas Bellinger * Locate incoming Initiator IQN reference from Storage Node. 1209e48354ceSNicholas Bellinger */ 1210e48354ceSNicholas Bellinger sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl( 1211e48354ceSNicholas Bellinger &conn->tpg->tpg_se_tpg, i_buf); 1212e48354ceSNicholas Bellinger if (!sess->se_sess->se_node_acl) { 1213e48354ceSNicholas Bellinger pr_err("iSCSI Initiator Node: %s is not authorized to" 1214e48354ceSNicholas Bellinger " access iSCSI target portal group: %hu.\n", 1215e48354ceSNicholas Bellinger i_buf, conn->tpg->tpgt); 1216e48354ceSNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1217e48354ceSNicholas Bellinger ISCSI_LOGIN_STATUS_TGT_FORBIDDEN); 1218e48354ceSNicholas Bellinger ret = -1; 1219e48354ceSNicholas Bellinger goto out; 1220e48354ceSNicholas Bellinger } 1221988e3a85SNicholas Bellinger se_nacl = sess->se_sess->se_node_acl; 1222988e3a85SNicholas Bellinger queue_depth = se_nacl->queue_depth; 1223988e3a85SNicholas Bellinger /* 1224988e3a85SNicholas Bellinger * Setup pre-allocated tags based upon allowed per NodeACL CmdSN 1225988e3a85SNicholas Bellinger * depth for non immediate commands, plus extra tags for immediate 1226988e3a85SNicholas Bellinger * commands. 1227988e3a85SNicholas Bellinger * 1228988e3a85SNicholas Bellinger * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention 1229988e3a85SNicholas Bellinger * in per-cpu-ida tag allocation logic + small queue_depth. 1230988e3a85SNicholas Bellinger */ 1231988e3a85SNicholas Bellinger alloc_tags: 1232988e3a85SNicholas Bellinger tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth); 12334a4caa29SNicholas Bellinger tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS; 1234988e3a85SNicholas Bellinger tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size; 1235e48354ceSNicholas Bellinger 1236988e3a85SNicholas Bellinger ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size); 1237988e3a85SNicholas Bellinger if (ret < 0) { 1238988e3a85SNicholas Bellinger iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1239988e3a85SNicholas Bellinger ISCSI_LOGIN_STATUS_NO_RESOURCES); 1240988e3a85SNicholas Bellinger ret = -1; 1241988e3a85SNicholas Bellinger } 1242e48354ceSNicholas Bellinger out: 1243e48354ceSNicholas Bellinger kfree(tmpbuf); 1244e48354ceSNicholas Bellinger return ret; 1245e48354ceSNicholas Bellinger } 1246e48354ceSNicholas Bellinger 1247e48354ceSNicholas Bellinger int iscsi_target_start_negotiation( 1248e48354ceSNicholas Bellinger struct iscsi_login *login, 1249e48354ceSNicholas Bellinger struct iscsi_conn *conn) 1250e48354ceSNicholas Bellinger { 1251baa4d64bSNicholas Bellinger int ret; 1252e48354ceSNicholas Bellinger 1253e48354ceSNicholas Bellinger ret = iscsi_target_do_login(conn, login); 1254d381a801SNicholas Bellinger if (!ret) { 1255d381a801SNicholas Bellinger if (conn->sock) { 1256d381a801SNicholas Bellinger struct sock *sk = conn->sock->sk; 1257e48354ceSNicholas Bellinger 1258d381a801SNicholas Bellinger write_lock_bh(&sk->sk_callback_lock); 1259d381a801SNicholas Bellinger set_bit(LOGIN_FLAGS_READY, &conn->login_flags); 1260d381a801SNicholas Bellinger write_unlock_bh(&sk->sk_callback_lock); 1261d381a801SNicholas Bellinger } 1262d381a801SNicholas Bellinger } else if (ret < 0) { 1263d381a801SNicholas Bellinger cancel_delayed_work_sync(&conn->login_work); 1264bb048357SNicholas Bellinger cancel_delayed_work_sync(&conn->login_cleanup_work); 1265d381a801SNicholas Bellinger iscsi_target_restore_sock_callbacks(conn); 1266d381a801SNicholas Bellinger iscsi_remove_failed_auth_entry(conn); 1267d381a801SNicholas Bellinger } 1268d381a801SNicholas Bellinger if (ret != 0) 1269baa4d64bSNicholas Bellinger iscsi_target_nego_release(conn); 1270d381a801SNicholas Bellinger 1271e48354ceSNicholas Bellinger return ret; 1272e48354ceSNicholas Bellinger } 1273e48354ceSNicholas Bellinger 1274baa4d64bSNicholas Bellinger void iscsi_target_nego_release(struct iscsi_conn *conn) 1275e48354ceSNicholas Bellinger { 1276baa4d64bSNicholas Bellinger struct iscsi_login *login = conn->conn_login; 1277baa4d64bSNicholas Bellinger 1278baa4d64bSNicholas Bellinger if (!login) 1279baa4d64bSNicholas Bellinger return; 1280baa4d64bSNicholas Bellinger 1281e48354ceSNicholas Bellinger kfree(login->req_buf); 1282e48354ceSNicholas Bellinger kfree(login->rsp_buf); 1283e48354ceSNicholas Bellinger kfree(login); 1284baa4d64bSNicholas Bellinger 1285baa4d64bSNicholas Bellinger conn->conn_login = NULL; 1286e48354ceSNicholas Bellinger } 1287