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 <nab@linux-iscsi.org> 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/Nin" 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 struct conn_timeout { 539 struct timer_list timer; 540 struct iscsit_conn *conn; 541 }; 542 543 static void iscsi_target_login_timeout(struct timer_list *t) 544 { 545 struct conn_timeout *timeout = from_timer(timeout, t, timer); 546 struct iscsit_conn *conn = timeout->conn; 547 548 pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n"); 549 550 if (conn->login_kworker) { 551 pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n", 552 conn->login_kworker->comm, conn->login_kworker->pid); 553 send_sig(SIGINT, conn->login_kworker, 1); 554 } 555 } 556 557 static void iscsi_target_do_login_rx(struct work_struct *work) 558 { 559 struct iscsit_conn *conn = container_of(work, 560 struct iscsit_conn, login_work.work); 561 struct iscsi_login *login = conn->login; 562 struct iscsi_np *np = login->np; 563 struct iscsi_portal_group *tpg = conn->tpg; 564 struct iscsi_tpg_np *tpg_np = conn->tpg_np; 565 struct conn_timeout timeout; 566 int rc, zero_tsih = login->zero_tsih; 567 bool state; 568 569 pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n", 570 conn, current->comm, current->pid); 571 /* 572 * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready() 573 * before initial PDU processing in iscsi_target_start_negotiation() 574 * has completed, go ahead and retry until it's cleared. 575 * 576 * Otherwise if the TCP connection drops while this is occuring, 577 * iscsi_target_start_negotiation() will detect the failure, call 578 * cancel_delayed_work_sync(&conn->login_work), and cleanup the 579 * remaining iscsi connection resources from iscsi_np process context. 580 */ 581 if (iscsi_target_sk_check_flag(conn, LOGIN_FLAGS_INITIAL_PDU)) { 582 schedule_delayed_work(&conn->login_work, msecs_to_jiffies(10)); 583 return; 584 } 585 586 spin_lock(&tpg->tpg_state_lock); 587 state = (tpg->tpg_state == TPG_STATE_ACTIVE); 588 spin_unlock(&tpg->tpg_state_lock); 589 590 if (!state) { 591 pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n"); 592 goto err; 593 } 594 595 if (iscsi_target_sk_check_close(conn)) { 596 pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n"); 597 goto err; 598 } 599 600 conn->login_kworker = current; 601 allow_signal(SIGINT); 602 603 timeout.conn = conn; 604 timer_setup_on_stack(&timeout.timer, iscsi_target_login_timeout, 0); 605 mod_timer(&timeout.timer, jiffies + TA_LOGIN_TIMEOUT * HZ); 606 pr_debug("Starting login timer for %s/%d\n", current->comm, current->pid); 607 608 rc = conn->conn_transport->iscsit_get_login_rx(conn, login); 609 del_timer_sync(&timeout.timer); 610 destroy_timer_on_stack(&timeout.timer); 611 flush_signals(current); 612 conn->login_kworker = NULL; 613 614 if (rc < 0) 615 goto err; 616 617 pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n", 618 conn, current->comm, current->pid); 619 620 /* 621 * LOGIN_FLAGS_READ_ACTIVE is cleared so that sk_data_ready 622 * could be triggered again after this. 623 * 624 * LOGIN_FLAGS_WRITE_ACTIVE is cleared after we successfully 625 * process a login PDU, so that sk_state_chage can do login 626 * cleanup as needed if the socket is closed. If a delayed work is 627 * ongoing (LOGIN_FLAGS_WRITE_ACTIVE or LOGIN_FLAGS_READ_ACTIVE), 628 * sk_state_change will leave the cleanup to the delayed work or 629 * it will schedule a delayed work to do cleanup. 630 */ 631 if (conn->sock) { 632 struct sock *sk = conn->sock->sk; 633 634 write_lock_bh(&sk->sk_callback_lock); 635 if (!test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags)) { 636 clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags); 637 set_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags); 638 } 639 write_unlock_bh(&sk->sk_callback_lock); 640 } 641 642 rc = iscsi_target_do_login(conn, login); 643 if (rc < 0) { 644 goto err; 645 } else if (!rc) { 646 if (iscsi_target_sk_check_and_clear(conn, 647 LOGIN_FLAGS_WRITE_ACTIVE)) 648 goto err; 649 } else if (rc == 1) { 650 cancel_delayed_work(&conn->login_work); 651 iscsi_target_nego_release(conn); 652 iscsi_post_login_handler(np, conn, zero_tsih); 653 iscsit_deaccess_np(np, tpg, tpg_np); 654 } 655 return; 656 657 err: 658 iscsi_target_restore_sock_callbacks(conn); 659 cancel_delayed_work(&conn->login_work); 660 iscsi_target_login_drop(conn, login); 661 iscsit_deaccess_np(np, tpg, tpg_np); 662 } 663 664 static void iscsi_target_sk_state_change(struct sock *sk) 665 { 666 struct iscsit_conn *conn; 667 void (*orig_state_change)(struct sock *); 668 bool state; 669 670 pr_debug("Entering iscsi_target_sk_state_change\n"); 671 672 write_lock_bh(&sk->sk_callback_lock); 673 conn = sk->sk_user_data; 674 if (!conn) { 675 write_unlock_bh(&sk->sk_callback_lock); 676 return; 677 } 678 orig_state_change = conn->orig_state_change; 679 680 if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) { 681 pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n", 682 conn); 683 write_unlock_bh(&sk->sk_callback_lock); 684 orig_state_change(sk); 685 return; 686 } 687 state = __iscsi_target_sk_check_close(sk); 688 pr_debug("__iscsi_target_sk_close_change: state: %d\n", state); 689 690 if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags) || 691 test_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags)) { 692 pr_debug("Got LOGIN_FLAGS_{READ|WRITE}_ACTIVE=1" 693 " sk_state_change conn: %p\n", conn); 694 if (state) 695 set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags); 696 write_unlock_bh(&sk->sk_callback_lock); 697 orig_state_change(sk); 698 return; 699 } 700 if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) { 701 pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n", 702 conn); 703 write_unlock_bh(&sk->sk_callback_lock); 704 orig_state_change(sk); 705 return; 706 } 707 /* 708 * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED, 709 * but only queue conn->login_work -> iscsi_target_do_login_rx() 710 * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared. 711 * 712 * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close() 713 * will detect the dropped TCP connection from delayed workqueue context. 714 * 715 * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial 716 * iscsi_target_start_negotiation() is running, iscsi_target_do_login() 717 * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation() 718 * via iscsi_target_sk_check_and_clear() is responsible for detecting the 719 * dropped TCP connection in iscsi_np process context, and cleaning up 720 * the remaining iscsi connection resources. 721 */ 722 if (state) { 723 pr_debug("iscsi_target_sk_state_change got failed state\n"); 724 set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags); 725 state = test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags); 726 write_unlock_bh(&sk->sk_callback_lock); 727 728 orig_state_change(sk); 729 730 if (!state) 731 schedule_delayed_work(&conn->login_work, 0); 732 return; 733 } 734 write_unlock_bh(&sk->sk_callback_lock); 735 736 orig_state_change(sk); 737 } 738 739 /* 740 * NOTE: We check for existing sessions or connections AFTER the initiator 741 * has been successfully authenticated in order to protect against faked 742 * ISID/TSIH combinations. 743 */ 744 static int iscsi_target_check_for_existing_instances( 745 struct iscsit_conn *conn, 746 struct iscsi_login *login) 747 { 748 if (login->checked_for_existing) 749 return 0; 750 751 login->checked_for_existing = 1; 752 753 if (!login->tsih) 754 return iscsi_check_for_session_reinstatement(conn); 755 else 756 return iscsi_login_post_auth_non_zero_tsih(conn, login->cid, 757 login->initial_exp_statsn); 758 } 759 760 static int iscsi_target_do_authentication( 761 struct iscsit_conn *conn, 762 struct iscsi_login *login) 763 { 764 int authret; 765 u32 payload_length; 766 struct iscsi_param *param; 767 struct iscsi_login_req *login_req; 768 struct iscsi_login_rsp *login_rsp; 769 770 login_req = (struct iscsi_login_req *) login->req; 771 login_rsp = (struct iscsi_login_rsp *) login->rsp; 772 payload_length = ntoh24(login_req->dlength); 773 774 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list); 775 if (!param) 776 return -1; 777 778 authret = iscsi_handle_authentication( 779 conn, 780 login->req_buf, 781 login->rsp_buf, 782 payload_length, 783 &login->rsp_length, 784 param->value); 785 switch (authret) { 786 case 0: 787 pr_debug("Received OK response" 788 " from LIO Authentication, continuing.\n"); 789 break; 790 case 1: 791 pr_debug("iSCSI security negotiation" 792 " completed successfully.\n"); 793 login->auth_complete = 1; 794 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) && 795 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) { 796 login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 | 797 ISCSI_FLAG_LOGIN_TRANSIT); 798 login->current_stage = 1; 799 } 800 return iscsi_target_check_for_existing_instances( 801 conn, login); 802 case 2: 803 pr_err("Security negotiation" 804 " failed.\n"); 805 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 806 ISCSI_LOGIN_STATUS_AUTH_FAILED); 807 return -1; 808 default: 809 pr_err("Received unknown error %d from LIO" 810 " Authentication\n", authret); 811 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 812 ISCSI_LOGIN_STATUS_TARGET_ERROR); 813 return -1; 814 } 815 816 return 0; 817 } 818 819 bool iscsi_conn_auth_required(struct iscsit_conn *conn) 820 { 821 struct iscsi_node_acl *nacl; 822 struct se_node_acl *se_nacl; 823 824 if (conn->sess->sess_ops->SessionType) { 825 /* 826 * For SessionType=Discovery 827 */ 828 return conn->tpg->tpg_attrib.authentication; 829 } 830 /* 831 * For SessionType=Normal 832 */ 833 se_nacl = conn->sess->se_sess->se_node_acl; 834 if (!se_nacl) { 835 pr_debug("Unknown ACL is trying to connect\n"); 836 return true; 837 } 838 839 if (se_nacl->dynamic_node_acl) { 840 pr_debug("Dynamic ACL %s is trying to connect\n", 841 se_nacl->initiatorname); 842 return conn->tpg->tpg_attrib.authentication; 843 } 844 845 pr_debug("Known ACL %s is trying to connect\n", 846 se_nacl->initiatorname); 847 848 nacl = to_iscsi_nacl(se_nacl); 849 if (nacl->node_attrib.authentication == NA_AUTHENTICATION_INHERITED) 850 return conn->tpg->tpg_attrib.authentication; 851 852 return nacl->node_attrib.authentication; 853 } 854 855 static int iscsi_target_handle_csg_zero( 856 struct iscsit_conn *conn, 857 struct iscsi_login *login) 858 { 859 int ret; 860 u32 payload_length; 861 struct iscsi_param *param; 862 struct iscsi_login_req *login_req; 863 struct iscsi_login_rsp *login_rsp; 864 865 login_req = (struct iscsi_login_req *) login->req; 866 login_rsp = (struct iscsi_login_rsp *) login->rsp; 867 payload_length = ntoh24(login_req->dlength); 868 869 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list); 870 if (!param) 871 return -1; 872 873 ret = iscsi_decode_text_input( 874 PHASE_SECURITY|PHASE_DECLARATIVE, 875 SENDER_INITIATOR|SENDER_RECEIVER, 876 login->req_buf, 877 payload_length, 878 conn); 879 if (ret < 0) 880 return -1; 881 882 if (ret > 0) { 883 if (login->auth_complete) { 884 pr_err("Initiator has already been" 885 " successfully authenticated, but is still" 886 " sending %s keys.\n", param->value); 887 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 888 ISCSI_LOGIN_STATUS_INIT_ERR); 889 return -1; 890 } 891 892 goto do_auth; 893 } else if (!payload_length) { 894 pr_err("Initiator sent zero length security payload," 895 " login failed\n"); 896 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 897 ISCSI_LOGIN_STATUS_AUTH_FAILED); 898 return -1; 899 } 900 901 if (login->first_request) 902 if (iscsi_target_check_first_request(conn, login) < 0) 903 return -1; 904 905 ret = iscsi_encode_text_output( 906 PHASE_SECURITY|PHASE_DECLARATIVE, 907 SENDER_TARGET, 908 login->rsp_buf, 909 &login->rsp_length, 910 conn->param_list, 911 conn->tpg->tpg_attrib.login_keys_workaround); 912 if (ret < 0) 913 return -1; 914 915 if (!iscsi_check_negotiated_keys(conn->param_list)) { 916 bool auth_required = iscsi_conn_auth_required(conn); 917 918 if (auth_required) { 919 if (!strncmp(param->value, NONE, 4)) { 920 pr_err("Initiator sent AuthMethod=None but" 921 " Target is enforcing iSCSI Authentication," 922 " login failed.\n"); 923 iscsit_tx_login_rsp(conn, 924 ISCSI_STATUS_CLS_INITIATOR_ERR, 925 ISCSI_LOGIN_STATUS_AUTH_FAILED); 926 return -1; 927 } 928 929 if (!login->auth_complete) 930 return 0; 931 932 if (strncmp(param->value, NONE, 4) && 933 !login->auth_complete) 934 return 0; 935 } 936 937 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) && 938 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) { 939 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 | 940 ISCSI_FLAG_LOGIN_TRANSIT; 941 login->current_stage = 1; 942 } 943 } 944 945 return 0; 946 do_auth: 947 return iscsi_target_do_authentication(conn, login); 948 } 949 950 static bool iscsi_conn_authenticated(struct iscsit_conn *conn, 951 struct iscsi_login *login) 952 { 953 if (!iscsi_conn_auth_required(conn)) 954 return true; 955 956 if (login->auth_complete) 957 return true; 958 959 return false; 960 } 961 962 static int iscsi_target_handle_csg_one(struct iscsit_conn *conn, struct iscsi_login *login) 963 { 964 int ret; 965 u32 payload_length; 966 struct iscsi_login_req *login_req; 967 struct iscsi_login_rsp *login_rsp; 968 969 login_req = (struct iscsi_login_req *) login->req; 970 login_rsp = (struct iscsi_login_rsp *) login->rsp; 971 payload_length = ntoh24(login_req->dlength); 972 973 ret = iscsi_decode_text_input( 974 PHASE_OPERATIONAL|PHASE_DECLARATIVE, 975 SENDER_INITIATOR|SENDER_RECEIVER, 976 login->req_buf, 977 payload_length, 978 conn); 979 if (ret < 0) { 980 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 981 ISCSI_LOGIN_STATUS_INIT_ERR); 982 return -1; 983 } 984 985 if (login->first_request) 986 if (iscsi_target_check_first_request(conn, login) < 0) 987 return -1; 988 989 if (iscsi_target_check_for_existing_instances(conn, login) < 0) 990 return -1; 991 992 ret = iscsi_encode_text_output( 993 PHASE_OPERATIONAL|PHASE_DECLARATIVE, 994 SENDER_TARGET, 995 login->rsp_buf, 996 &login->rsp_length, 997 conn->param_list, 998 conn->tpg->tpg_attrib.login_keys_workaround); 999 if (ret < 0) { 1000 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1001 ISCSI_LOGIN_STATUS_INIT_ERR); 1002 return -1; 1003 } 1004 1005 if (!iscsi_conn_authenticated(conn, login)) { 1006 pr_err("Initiator is requesting CSG: 1, has not been" 1007 " successfully authenticated, and the Target is" 1008 " enforcing iSCSI Authentication, login failed.\n"); 1009 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1010 ISCSI_LOGIN_STATUS_AUTH_FAILED); 1011 return -1; 1012 } 1013 1014 if (!iscsi_check_negotiated_keys(conn->param_list)) 1015 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) && 1016 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) 1017 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 | 1018 ISCSI_FLAG_LOGIN_TRANSIT; 1019 1020 return 0; 1021 } 1022 1023 /* 1024 * RETURN VALUE: 1025 * 1026 * 1 = Login successful 1027 * -1 = Login failed 1028 * 0 = More PDU exchanges required 1029 */ 1030 static int iscsi_target_do_login(struct iscsit_conn *conn, struct iscsi_login *login) 1031 { 1032 int pdu_count = 0; 1033 struct iscsi_login_req *login_req; 1034 struct iscsi_login_rsp *login_rsp; 1035 1036 login_req = (struct iscsi_login_req *) login->req; 1037 login_rsp = (struct iscsi_login_rsp *) login->rsp; 1038 1039 while (1) { 1040 if (++pdu_count > MAX_LOGIN_PDUS) { 1041 pr_err("MAX_LOGIN_PDUS count reached.\n"); 1042 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1043 ISCSI_LOGIN_STATUS_TARGET_ERROR); 1044 return -1; 1045 } 1046 1047 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) { 1048 case 0: 1049 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK; 1050 if (iscsi_target_handle_csg_zero(conn, login) < 0) 1051 return -1; 1052 break; 1053 case 1: 1054 login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1; 1055 if (iscsi_target_handle_csg_one(conn, login) < 0) 1056 return -1; 1057 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) { 1058 /* 1059 * Check to make sure the TCP connection has not 1060 * dropped asynchronously while session reinstatement 1061 * was occuring in this kthread context, before 1062 * transitioning to full feature phase operation. 1063 */ 1064 if (iscsi_target_sk_check_close(conn)) 1065 return -1; 1066 1067 login->tsih = conn->sess->tsih; 1068 login->login_complete = 1; 1069 iscsi_target_restore_sock_callbacks(conn); 1070 if (iscsi_target_do_tx_login_io(conn, 1071 login) < 0) 1072 return -1; 1073 return 1; 1074 } 1075 break; 1076 default: 1077 pr_err("Illegal CSG: %d received from" 1078 " Initiator, protocol error.\n", 1079 ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)); 1080 break; 1081 } 1082 1083 if (iscsi_target_do_tx_login_io(conn, login) < 0) 1084 return -1; 1085 1086 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) { 1087 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT; 1088 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK; 1089 } 1090 break; 1091 } 1092 1093 return 0; 1094 } 1095 1096 static void iscsi_initiatorname_tolower( 1097 char *param_buf) 1098 { 1099 char *c; 1100 u32 iqn_size = strlen(param_buf), i; 1101 1102 for (i = 0; i < iqn_size; i++) { 1103 c = ¶m_buf[i]; 1104 if (!isupper(*c)) 1105 continue; 1106 1107 *c = tolower(*c); 1108 } 1109 } 1110 1111 /* 1112 * Processes the first Login Request.. 1113 */ 1114 int iscsi_target_locate_portal( 1115 struct iscsi_np *np, 1116 struct iscsit_conn *conn, 1117 struct iscsi_login *login) 1118 { 1119 char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL; 1120 char *tmpbuf, *start = NULL, *end = NULL, *key, *value; 1121 struct iscsit_session *sess = conn->sess; 1122 struct iscsi_tiqn *tiqn; 1123 struct iscsi_tpg_np *tpg_np = NULL; 1124 struct iscsi_login_req *login_req; 1125 struct se_node_acl *se_nacl; 1126 u32 payload_length, queue_depth = 0; 1127 int sessiontype = 0, ret = 0, tag_num, tag_size; 1128 1129 INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx); 1130 iscsi_target_set_sock_callbacks(conn); 1131 1132 login->np = np; 1133 1134 login_req = (struct iscsi_login_req *) login->req; 1135 payload_length = ntoh24(login_req->dlength); 1136 1137 tmpbuf = kmemdup_nul(login->req_buf, payload_length, GFP_KERNEL); 1138 if (!tmpbuf) { 1139 pr_err("Unable to allocate memory for tmpbuf.\n"); 1140 return -1; 1141 } 1142 1143 start = tmpbuf; 1144 end = (start + payload_length); 1145 1146 /* 1147 * Locate the initial keys expected from the Initiator node in 1148 * the first login request in order to progress with the login phase. 1149 */ 1150 while (start < end) { 1151 if (iscsi_extract_key_value(start, &key, &value) < 0) { 1152 ret = -1; 1153 goto out; 1154 } 1155 1156 if (!strncmp(key, "InitiatorName", 13)) 1157 i_buf = value; 1158 else if (!strncmp(key, "SessionType", 11)) 1159 s_buf = value; 1160 else if (!strncmp(key, "TargetName", 10)) 1161 t_buf = value; 1162 1163 start += strlen(key) + strlen(value) + 2; 1164 } 1165 /* 1166 * See 5.3. Login Phase. 1167 */ 1168 if (!i_buf) { 1169 pr_err("InitiatorName key not received" 1170 " in first login request.\n"); 1171 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1172 ISCSI_LOGIN_STATUS_MISSING_FIELDS); 1173 ret = -1; 1174 goto out; 1175 } 1176 /* 1177 * Convert the incoming InitiatorName to lowercase following 1178 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs 1179 * are NOT case sensitive. 1180 */ 1181 iscsi_initiatorname_tolower(i_buf); 1182 1183 if (!s_buf) { 1184 if (!login->leading_connection) 1185 goto get_target; 1186 1187 pr_err("SessionType key not received" 1188 " in first login request.\n"); 1189 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1190 ISCSI_LOGIN_STATUS_MISSING_FIELDS); 1191 ret = -1; 1192 goto out; 1193 } 1194 1195 /* 1196 * Use default portal group for discovery sessions. 1197 */ 1198 sessiontype = strncmp(s_buf, DISCOVERY, 9); 1199 if (!sessiontype) { 1200 conn->tpg = iscsit_global->discovery_tpg; 1201 if (!login->leading_connection) 1202 goto get_target; 1203 1204 sess->sess_ops->SessionType = 1; 1205 /* 1206 * Setup crc32c modules from libcrypto 1207 */ 1208 if (iscsi_login_setup_crypto(conn) < 0) { 1209 pr_err("iscsi_login_setup_crypto() failed\n"); 1210 ret = -1; 1211 goto out; 1212 } 1213 /* 1214 * Serialize access across the discovery struct iscsi_portal_group to 1215 * process login attempt. 1216 */ 1217 if (iscsit_access_np(np, conn->tpg) < 0) { 1218 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1219 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1220 ret = -1; 1221 goto out; 1222 } 1223 ret = 0; 1224 goto alloc_tags; 1225 } 1226 1227 get_target: 1228 if (!t_buf) { 1229 pr_err("TargetName key not received" 1230 " in first login request while" 1231 " SessionType=Normal.\n"); 1232 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1233 ISCSI_LOGIN_STATUS_MISSING_FIELDS); 1234 ret = -1; 1235 goto out; 1236 } 1237 1238 /* 1239 * Locate Target IQN from Storage Node. 1240 */ 1241 tiqn = iscsit_get_tiqn_for_login(t_buf); 1242 if (!tiqn) { 1243 pr_err("Unable to locate Target IQN: %s in" 1244 " Storage Node\n", t_buf); 1245 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1246 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1247 ret = -1; 1248 goto out; 1249 } 1250 pr_debug("Located Storage Object: %s\n", tiqn->tiqn); 1251 1252 /* 1253 * Locate Target Portal Group from Storage Node. 1254 */ 1255 conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np); 1256 if (!conn->tpg) { 1257 pr_err("Unable to locate Target Portal Group" 1258 " on %s\n", tiqn->tiqn); 1259 iscsit_put_tiqn_for_login(tiqn); 1260 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1261 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1262 ret = -1; 1263 goto out; 1264 } 1265 conn->tpg_np = tpg_np; 1266 pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt); 1267 /* 1268 * Setup crc32c modules from libcrypto 1269 */ 1270 if (iscsi_login_setup_crypto(conn) < 0) { 1271 pr_err("iscsi_login_setup_crypto() failed\n"); 1272 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); 1273 iscsit_put_tiqn_for_login(tiqn); 1274 conn->tpg = NULL; 1275 ret = -1; 1276 goto out; 1277 } 1278 /* 1279 * Serialize access across the struct iscsi_portal_group to 1280 * process login attempt. 1281 */ 1282 if (iscsit_access_np(np, conn->tpg) < 0) { 1283 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); 1284 iscsit_put_tiqn_for_login(tiqn); 1285 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1286 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 1287 conn->tpg = NULL; 1288 ret = -1; 1289 goto out; 1290 } 1291 1292 /* 1293 * conn->sess->node_acl will be set when the referenced 1294 * struct iscsit_session is located from received ISID+TSIH in 1295 * iscsi_login_non_zero_tsih_s2(). 1296 */ 1297 if (!login->leading_connection) { 1298 ret = 0; 1299 goto out; 1300 } 1301 1302 /* 1303 * This value is required in iscsi_login_zero_tsih_s2() 1304 */ 1305 sess->sess_ops->SessionType = 0; 1306 1307 /* 1308 * Locate incoming Initiator IQN reference from Storage Node. 1309 */ 1310 sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl( 1311 &conn->tpg->tpg_se_tpg, i_buf); 1312 if (!sess->se_sess->se_node_acl) { 1313 pr_err("iSCSI Initiator Node: %s is not authorized to" 1314 " access iSCSI target portal group: %hu.\n", 1315 i_buf, conn->tpg->tpgt); 1316 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 1317 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN); 1318 ret = -1; 1319 goto out; 1320 } 1321 se_nacl = sess->se_sess->se_node_acl; 1322 queue_depth = se_nacl->queue_depth; 1323 /* 1324 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN 1325 * depth for non immediate commands, plus extra tags for immediate 1326 * commands. 1327 * 1328 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention 1329 * in per-cpu-ida tag allocation logic + small queue_depth. 1330 */ 1331 alloc_tags: 1332 tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth); 1333 tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS; 1334 tag_size = sizeof(struct iscsit_cmd) + conn->conn_transport->priv_size; 1335 1336 ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size); 1337 if (ret < 0) { 1338 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 1339 ISCSI_LOGIN_STATUS_NO_RESOURCES); 1340 ret = -1; 1341 } 1342 out: 1343 kfree(tmpbuf); 1344 return ret; 1345 } 1346 1347 int iscsi_target_start_negotiation( 1348 struct iscsi_login *login, 1349 struct iscsit_conn *conn) 1350 { 1351 int ret; 1352 1353 if (conn->sock) { 1354 struct sock *sk = conn->sock->sk; 1355 1356 write_lock_bh(&sk->sk_callback_lock); 1357 set_bit(LOGIN_FLAGS_READY, &conn->login_flags); 1358 set_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags); 1359 write_unlock_bh(&sk->sk_callback_lock); 1360 } 1361 /* 1362 * If iscsi_target_do_login returns zero to signal more PDU 1363 * exchanges are required to complete the login, go ahead and 1364 * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection 1365 * is still active. 1366 * 1367 * Otherwise if TCP connection dropped asynchronously, go ahead 1368 * and perform connection cleanup now. 1369 */ 1370 ret = iscsi_target_do_login(conn, login); 1371 if (!ret && iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_INITIAL_PDU)) 1372 ret = -1; 1373 1374 if (ret < 0) { 1375 iscsi_target_restore_sock_callbacks(conn); 1376 iscsi_remove_failed_auth_entry(conn); 1377 } 1378 if (ret != 0) { 1379 cancel_delayed_work_sync(&conn->login_work); 1380 iscsi_target_nego_release(conn); 1381 } 1382 1383 return ret; 1384 } 1385 1386 void iscsi_target_nego_release(struct iscsit_conn *conn) 1387 { 1388 struct iscsi_login *login = conn->conn_login; 1389 1390 if (!login) 1391 return; 1392 1393 kfree(login->req_buf); 1394 kfree(login->rsp_buf); 1395 kfree(login); 1396 1397 conn->conn_login = NULL; 1398 } 1399