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