1 /******************************************************************************* 2 * This file contains main functions related to iSCSI Parameter negotiation. 3 * 4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC. 5 * 6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2. 7 * 8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org> 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 ******************************************************************************/ 20 21 #include <linux/ctype.h> 22 #include <scsi/iscsi_proto.h> 23 #include <target/target_core_base.h> 24 #include <target/target_core_fabric.h> 25 #include <target/iscsi/iscsi_transport.h> 26 27 #include "iscsi_target_core.h" 28 #include "iscsi_target_parameters.h" 29 #include "iscsi_target_login.h" 30 #include "iscsi_target_nego.h" 31 #include "iscsi_target_tpg.h" 32 #include "iscsi_target_util.h" 33 #include "iscsi_target.h" 34 #include "iscsi_target_auth.h" 35 36 #define MAX_LOGIN_PDUS 7 37 #define TEXT_LEN 4096 38 39 void convert_null_to_semi(char *buf, int len) 40 { 41 int i; 42 43 for (i = 0; i < len; i++) 44 if (buf[i] == '\0') 45 buf[i] = ';'; 46 } 47 48 static int strlen_semi(char *buf) 49 { 50 int i = 0; 51 52 while (buf[i] != '\0') { 53 if (buf[i] == ';') 54 return i; 55 i++; 56 } 57 58 return -1; 59 } 60 61 int extract_param( 62 const char *in_buf, 63 const char *pattern, 64 unsigned int max_length, 65 char *out_buf, 66 unsigned char *type) 67 { 68 char *ptr; 69 int len; 70 71 if (!in_buf || !pattern || !out_buf || !type) 72 return -1; 73 74 ptr = strstr(in_buf, pattern); 75 if (!ptr) 76 return -1; 77 78 ptr = strstr(ptr, "="); 79 if (!ptr) 80 return -1; 81 82 ptr += 1; 83 if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) { 84 ptr += 2; /* skip 0x */ 85 *type = HEX; 86 } else 87 *type = DECIMAL; 88 89 len = strlen_semi(ptr); 90 if (len < 0) 91 return -1; 92 93 if (len > max_length) { 94 pr_err("Length of input: %d exceeds max_length:" 95 " %d\n", len, max_length); 96 return -1; 97 } 98 memcpy(out_buf, ptr, len); 99 out_buf[len] = '\0'; 100 101 return 0; 102 } 103 104 static u32 iscsi_handle_authentication( 105 struct iscsi_conn *conn, 106 char *in_buf, 107 char *out_buf, 108 int in_length, 109 int *out_length, 110 unsigned char *authtype) 111 { 112 struct iscsi_session *sess = conn->sess; 113 struct iscsi_node_auth *auth; 114 struct iscsi_node_acl *iscsi_nacl; 115 struct se_node_acl *se_nacl; 116 117 if (!sess->sess_ops->SessionType) { 118 /* 119 * For SessionType=Normal 120 */ 121 se_nacl = conn->sess->se_sess->se_node_acl; 122 if (!se_nacl) { 123 pr_err("Unable to locate struct se_node_acl for" 124 " CHAP auth\n"); 125 return -1; 126 } 127 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl, 128 se_node_acl); 129 if (!iscsi_nacl) { 130 pr_err("Unable to locate struct iscsi_node_acl for" 131 " CHAP auth\n"); 132 return -1; 133 } 134 135 auth = ISCSI_NODE_AUTH(iscsi_nacl); 136 } else { 137 /* 138 * For SessionType=Discovery 139 */ 140 auth = &iscsit_global->discovery_acl.node_auth; 141 } 142 143 if (strstr("CHAP", authtype)) 144 strcpy(conn->sess->auth_type, "CHAP"); 145 else 146 strcpy(conn->sess->auth_type, NONE); 147 148 if (strstr("None", authtype)) 149 return 1; 150 #ifdef CANSRP 151 else if (strstr("SRP", authtype)) 152 return srp_main_loop(conn, auth, in_buf, out_buf, 153 &in_length, out_length); 154 #endif 155 else if (strstr("CHAP", authtype)) 156 return chap_main_loop(conn, auth, in_buf, out_buf, 157 &in_length, out_length); 158 else if (strstr("SPKM1", authtype)) 159 return 2; 160 else if (strstr("SPKM2", authtype)) 161 return 2; 162 else if (strstr("KRB5", authtype)) 163 return 2; 164 else 165 return 2; 166 } 167 168 static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn) 169 { 170 kfree(conn->auth_protocol); 171 } 172 173 int iscsi_target_check_login_request( 174 struct iscsi_conn *conn, 175 struct iscsi_login *login) 176 { 177 int req_csg, req_nsg; 178 u32 payload_length; 179 struct iscsi_login_req *login_req; 180 181 login_req = (struct iscsi_login_req *) login->req; 182 payload_length = ntoh24(login_req->dlength); 183 184 switch (login_req->opcode & ISCSI_OPCODE_MASK) { 185 case ISCSI_OP_LOGIN: 186 break; 187 default: 188 pr_err("Received unknown opcode 0x%02x.\n", 189 login_req->opcode & ISCSI_OPCODE_MASK); 190 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 191 ISCSI_LOGIN_STATUS_INIT_ERR); 192 return -1; 193 } 194 195 if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) && 196 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) { 197 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE" 198 " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n"); 199 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 200 ISCSI_LOGIN_STATUS_INIT_ERR); 201 return -1; 202 } 203 204 req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags); 205 req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags); 206 207 if (req_csg != login->current_stage) { 208 pr_err("Initiator unexpectedly changed login stage" 209 " from %d to %d, login failed.\n", login->current_stage, 210 req_csg); 211 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 212 ISCSI_LOGIN_STATUS_INIT_ERR); 213 return -1; 214 } 215 216 if ((req_nsg == 2) || (req_csg >= 2) || 217 ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) && 218 (req_nsg <= req_csg))) { 219 pr_err("Illegal login_req->flags Combination, CSG: %d," 220 " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg, 221 req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)); 222 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 223 ISCSI_LOGIN_STATUS_INIT_ERR); 224 return -1; 225 } 226 227 if ((login_req->max_version != login->version_max) || 228 (login_req->min_version != login->version_min)) { 229 pr_err("Login request changed Version Max/Nin" 230 " unexpectedly to 0x%02x/0x%02x, protocol error\n", 231 login_req->max_version, login_req->min_version); 232 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 233 ISCSI_LOGIN_STATUS_INIT_ERR); 234 return -1; 235 } 236 237 if (memcmp(login_req->isid, login->isid, 6) != 0) { 238 pr_err("Login request changed ISID unexpectedly," 239 " protocol error.\n"); 240 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 241 ISCSI_LOGIN_STATUS_INIT_ERR); 242 return -1; 243 } 244 245 if (login_req->itt != login->init_task_tag) { 246 pr_err("Login request changed ITT unexpectedly to" 247 " 0x%08x, protocol error.\n", login_req->itt); 248 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 249 ISCSI_LOGIN_STATUS_INIT_ERR); 250 return -1; 251 } 252 253 if (payload_length > MAX_KEY_VALUE_PAIRS) { 254 pr_err("Login request payload exceeds default" 255 " MaxRecvDataSegmentLength: %u, protocol error.\n", 256 MAX_KEY_VALUE_PAIRS); 257 return -1; 258 } 259 260 return 0; 261 } 262 263 static int iscsi_target_check_first_request( 264 struct iscsi_conn *conn, 265 struct iscsi_login *login) 266 { 267 struct iscsi_param *param = NULL; 268 struct se_node_acl *se_nacl; 269 270 login->first_request = 0; 271 272 list_for_each_entry(param, &conn->param_list->param_list, p_list) { 273 if (!strncmp(param->name, SESSIONTYPE, 11)) { 274 if (!IS_PSTATE_ACCEPTOR(param)) { 275 pr_err("SessionType key not received" 276 " in first login request.\n"); 277 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 278 ISCSI_LOGIN_STATUS_MISSING_FIELDS); 279 return -1; 280 } 281 if (!strncmp(param->value, DISCOVERY, 9)) 282 return 0; 283 } 284 285 if (!strncmp(param->name, INITIATORNAME, 13)) { 286 if (!IS_PSTATE_ACCEPTOR(param)) { 287 if (!login->leading_connection) 288 continue; 289 290 pr_err("InitiatorName key not received" 291 " in first login request.\n"); 292 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 293 ISCSI_LOGIN_STATUS_MISSING_FIELDS); 294 return -1; 295 } 296 297 /* 298 * For non-leading connections, double check that the 299 * received InitiatorName matches the existing session's 300 * struct iscsi_node_acl. 301 */ 302 if (!login->leading_connection) { 303 se_nacl = conn->sess->se_sess->se_node_acl; 304 if (!se_nacl) { 305 pr_err("Unable to locate" 306 " struct se_node_acl\n"); 307 iscsit_tx_login_rsp(conn, 308 ISCSI_STATUS_CLS_INITIATOR_ERR, 309 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND); 310 return -1; 311 } 312 313 if (strcmp(param->value, 314 se_nacl->initiatorname)) { 315 pr_err("Incorrect" 316 " InitiatorName: %s for this" 317 " iSCSI Initiator Node.\n", 318 param->value); 319 iscsit_tx_login_rsp(conn, 320 ISCSI_STATUS_CLS_INITIATOR_ERR, 321 ISCSI_LOGIN_STATUS_TGT_NOT_FOUND); 322 return -1; 323 } 324 } 325 } 326 } 327 328 return 0; 329 } 330 331 static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login) 332 { 333 u32 padding = 0; 334 struct iscsi_session *sess = conn->sess; 335 struct iscsi_login_rsp *login_rsp; 336 337 login_rsp = (struct iscsi_login_rsp *) login->rsp; 338 339 login_rsp->opcode = ISCSI_OP_LOGIN_RSP; 340 hton24(login_rsp->dlength, login->rsp_length); 341 memcpy(login_rsp->isid, login->isid, 6); 342 login_rsp->tsih = cpu_to_be16(login->tsih); 343 login_rsp->itt = login->init_task_tag; 344 login_rsp->statsn = cpu_to_be32(conn->stat_sn++); 345 login_rsp->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); 346 login_rsp->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); 347 348 pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x," 349 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:" 350 " %u\n", login_rsp->flags, (__force u32)login_rsp->itt, 351 ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn), 352 ntohl(login_rsp->statsn), login->rsp_length); 353 354 padding = ((-login->rsp_length) & 3); 355 356 if (conn->conn_transport->iscsit_put_login_tx(conn, login, 357 login->rsp_length + padding) < 0) 358 return -1; 359 360 login->rsp_length = 0; 361 mutex_lock(&sess->cmdsn_mutex); 362 login_rsp->exp_cmdsn = cpu_to_be32(sess->exp_cmd_sn); 363 login_rsp->max_cmdsn = cpu_to_be32(sess->max_cmd_sn); 364 mutex_unlock(&sess->cmdsn_mutex); 365 366 return 0; 367 } 368 369 static int iscsi_target_do_login_io(struct iscsi_conn *conn, struct iscsi_login *login) 370 { 371 if (iscsi_target_do_tx_login_io(conn, login) < 0) 372 return -1; 373 374 if (conn->conn_transport->iscsit_get_login_rx(conn, login) < 0) 375 return -1; 376 377 return 0; 378 } 379 380 /* 381 * NOTE: We check for existing sessions or connections AFTER the initiator 382 * has been successfully authenticated in order to protect against faked 383 * ISID/TSIH combinations. 384 */ 385 static int iscsi_target_check_for_existing_instances( 386 struct iscsi_conn *conn, 387 struct iscsi_login *login) 388 { 389 if (login->checked_for_existing) 390 return 0; 391 392 login->checked_for_existing = 1; 393 394 if (!login->tsih) 395 return iscsi_check_for_session_reinstatement(conn); 396 else 397 return iscsi_login_post_auth_non_zero_tsih(conn, login->cid, 398 login->initial_exp_statsn); 399 } 400 401 static int iscsi_target_do_authentication( 402 struct iscsi_conn *conn, 403 struct iscsi_login *login) 404 { 405 int authret; 406 u32 payload_length; 407 struct iscsi_param *param; 408 struct iscsi_login_req *login_req; 409 struct iscsi_login_rsp *login_rsp; 410 411 login_req = (struct iscsi_login_req *) login->req; 412 login_rsp = (struct iscsi_login_rsp *) login->rsp; 413 payload_length = ntoh24(login_req->dlength); 414 415 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list); 416 if (!param) 417 return -1; 418 419 authret = iscsi_handle_authentication( 420 conn, 421 login->req_buf, 422 login->rsp_buf, 423 payload_length, 424 &login->rsp_length, 425 param->value); 426 switch (authret) { 427 case 0: 428 pr_debug("Received OK response" 429 " from LIO Authentication, continuing.\n"); 430 break; 431 case 1: 432 pr_debug("iSCSI security negotiation" 433 " completed successfully.\n"); 434 login->auth_complete = 1; 435 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) && 436 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) { 437 login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 | 438 ISCSI_FLAG_LOGIN_TRANSIT); 439 login->current_stage = 1; 440 } 441 return iscsi_target_check_for_existing_instances( 442 conn, login); 443 case 2: 444 pr_err("Security negotiation" 445 " failed.\n"); 446 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 447 ISCSI_LOGIN_STATUS_AUTH_FAILED); 448 return -1; 449 default: 450 pr_err("Received unknown error %d from LIO" 451 " Authentication\n", authret); 452 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 453 ISCSI_LOGIN_STATUS_TARGET_ERROR); 454 return -1; 455 } 456 457 return 0; 458 } 459 460 static int iscsi_target_handle_csg_zero( 461 struct iscsi_conn *conn, 462 struct iscsi_login *login) 463 { 464 int ret; 465 u32 payload_length; 466 struct iscsi_param *param; 467 struct iscsi_login_req *login_req; 468 struct iscsi_login_rsp *login_rsp; 469 470 login_req = (struct iscsi_login_req *) login->req; 471 login_rsp = (struct iscsi_login_rsp *) login->rsp; 472 payload_length = ntoh24(login_req->dlength); 473 474 param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list); 475 if (!param) 476 return -1; 477 478 ret = iscsi_decode_text_input( 479 PHASE_SECURITY|PHASE_DECLARATIVE, 480 SENDER_INITIATOR|SENDER_RECEIVER, 481 login->req_buf, 482 payload_length, 483 conn); 484 if (ret < 0) 485 return -1; 486 487 if (ret > 0) { 488 if (login->auth_complete) { 489 pr_err("Initiator has already been" 490 " successfully authenticated, but is still" 491 " sending %s keys.\n", param->value); 492 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 493 ISCSI_LOGIN_STATUS_INIT_ERR); 494 return -1; 495 } 496 497 goto do_auth; 498 } 499 500 if (login->first_request) 501 if (iscsi_target_check_first_request(conn, login) < 0) 502 return -1; 503 504 ret = iscsi_encode_text_output( 505 PHASE_SECURITY|PHASE_DECLARATIVE, 506 SENDER_TARGET, 507 login->rsp_buf, 508 &login->rsp_length, 509 conn->param_list); 510 if (ret < 0) 511 return -1; 512 513 if (!iscsi_check_negotiated_keys(conn->param_list)) { 514 if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication && 515 !strncmp(param->value, NONE, 4)) { 516 pr_err("Initiator sent AuthMethod=None but" 517 " Target is enforcing iSCSI Authentication," 518 " login failed.\n"); 519 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 520 ISCSI_LOGIN_STATUS_AUTH_FAILED); 521 return -1; 522 } 523 524 if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication && 525 !login->auth_complete) 526 return 0; 527 528 if (strncmp(param->value, NONE, 4) && !login->auth_complete) 529 return 0; 530 531 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) && 532 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) { 533 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 | 534 ISCSI_FLAG_LOGIN_TRANSIT; 535 login->current_stage = 1; 536 } 537 } 538 539 return 0; 540 do_auth: 541 return iscsi_target_do_authentication(conn, login); 542 } 543 544 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login) 545 { 546 int ret; 547 u32 payload_length; 548 struct iscsi_login_req *login_req; 549 struct iscsi_login_rsp *login_rsp; 550 551 login_req = (struct iscsi_login_req *) login->req; 552 login_rsp = (struct iscsi_login_rsp *) login->rsp; 553 payload_length = ntoh24(login_req->dlength); 554 555 ret = iscsi_decode_text_input( 556 PHASE_OPERATIONAL|PHASE_DECLARATIVE, 557 SENDER_INITIATOR|SENDER_RECEIVER, 558 login->req_buf, 559 payload_length, 560 conn); 561 if (ret < 0) { 562 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 563 ISCSI_LOGIN_STATUS_INIT_ERR); 564 return -1; 565 } 566 567 if (login->first_request) 568 if (iscsi_target_check_first_request(conn, login) < 0) 569 return -1; 570 571 if (iscsi_target_check_for_existing_instances(conn, login) < 0) 572 return -1; 573 574 ret = iscsi_encode_text_output( 575 PHASE_OPERATIONAL|PHASE_DECLARATIVE, 576 SENDER_TARGET, 577 login->rsp_buf, 578 &login->rsp_length, 579 conn->param_list); 580 if (ret < 0) { 581 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 582 ISCSI_LOGIN_STATUS_INIT_ERR); 583 return -1; 584 } 585 586 if (!login->auth_complete && 587 ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication) { 588 pr_err("Initiator is requesting CSG: 1, has not been" 589 " successfully authenticated, and the Target is" 590 " enforcing iSCSI Authentication, login failed.\n"); 591 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 592 ISCSI_LOGIN_STATUS_AUTH_FAILED); 593 return -1; 594 } 595 596 if (!iscsi_check_negotiated_keys(conn->param_list)) 597 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) && 598 (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) 599 login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 | 600 ISCSI_FLAG_LOGIN_TRANSIT; 601 602 return 0; 603 } 604 605 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login) 606 { 607 int pdu_count = 0; 608 struct iscsi_login_req *login_req; 609 struct iscsi_login_rsp *login_rsp; 610 611 login_req = (struct iscsi_login_req *) login->req; 612 login_rsp = (struct iscsi_login_rsp *) login->rsp; 613 614 while (1) { 615 if (++pdu_count > MAX_LOGIN_PDUS) { 616 pr_err("MAX_LOGIN_PDUS count reached.\n"); 617 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 618 ISCSI_LOGIN_STATUS_TARGET_ERROR); 619 return -1; 620 } 621 622 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) { 623 case 0: 624 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK; 625 if (iscsi_target_handle_csg_zero(conn, login) < 0) 626 return -1; 627 break; 628 case 1: 629 login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1; 630 if (iscsi_target_handle_csg_one(conn, login) < 0) 631 return -1; 632 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) { 633 login->tsih = conn->sess->tsih; 634 login->login_complete = 1; 635 if (iscsi_target_do_tx_login_io(conn, 636 login) < 0) 637 return -1; 638 return 0; 639 } 640 break; 641 default: 642 pr_err("Illegal CSG: %d received from" 643 " Initiator, protocol error.\n", 644 ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)); 645 break; 646 } 647 648 if (iscsi_target_do_login_io(conn, login) < 0) 649 return -1; 650 651 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) { 652 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT; 653 login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK; 654 } 655 } 656 657 return 0; 658 } 659 660 static void iscsi_initiatorname_tolower( 661 char *param_buf) 662 { 663 char *c; 664 u32 iqn_size = strlen(param_buf), i; 665 666 for (i = 0; i < iqn_size; i++) { 667 c = ¶m_buf[i]; 668 if (!isupper(*c)) 669 continue; 670 671 *c = tolower(*c); 672 } 673 } 674 675 /* 676 * Processes the first Login Request.. 677 */ 678 int iscsi_target_locate_portal( 679 struct iscsi_np *np, 680 struct iscsi_conn *conn, 681 struct iscsi_login *login) 682 { 683 char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL; 684 char *tmpbuf, *start = NULL, *end = NULL, *key, *value; 685 struct iscsi_session *sess = conn->sess; 686 struct iscsi_tiqn *tiqn; 687 struct iscsi_login_req *login_req; 688 u32 payload_length; 689 int sessiontype = 0, ret = 0; 690 691 login_req = (struct iscsi_login_req *) login->req; 692 payload_length = ntoh24(login_req->dlength); 693 694 tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL); 695 if (!tmpbuf) { 696 pr_err("Unable to allocate memory for tmpbuf.\n"); 697 return -1; 698 } 699 700 memcpy(tmpbuf, login->req_buf, payload_length); 701 tmpbuf[payload_length] = '\0'; 702 start = tmpbuf; 703 end = (start + payload_length); 704 705 /* 706 * Locate the initial keys expected from the Initiator node in 707 * the first login request in order to progress with the login phase. 708 */ 709 while (start < end) { 710 if (iscsi_extract_key_value(start, &key, &value) < 0) { 711 ret = -1; 712 goto out; 713 } 714 715 if (!strncmp(key, "InitiatorName", 13)) 716 i_buf = value; 717 else if (!strncmp(key, "SessionType", 11)) 718 s_buf = value; 719 else if (!strncmp(key, "TargetName", 10)) 720 t_buf = value; 721 722 start += strlen(key) + strlen(value) + 2; 723 } 724 725 printk("i_buf: %s, s_buf: %s, t_buf: %s\n", i_buf, s_buf, t_buf); 726 727 /* 728 * See 5.3. Login Phase. 729 */ 730 if (!i_buf) { 731 pr_err("InitiatorName key not received" 732 " in first login request.\n"); 733 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 734 ISCSI_LOGIN_STATUS_MISSING_FIELDS); 735 ret = -1; 736 goto out; 737 } 738 /* 739 * Convert the incoming InitiatorName to lowercase following 740 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs 741 * are NOT case sensitive. 742 */ 743 iscsi_initiatorname_tolower(i_buf); 744 745 if (!s_buf) { 746 if (!login->leading_connection) 747 goto get_target; 748 749 pr_err("SessionType key not received" 750 " in first login request.\n"); 751 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 752 ISCSI_LOGIN_STATUS_MISSING_FIELDS); 753 ret = -1; 754 goto out; 755 } 756 757 /* 758 * Use default portal group for discovery sessions. 759 */ 760 sessiontype = strncmp(s_buf, DISCOVERY, 9); 761 if (!sessiontype) { 762 conn->tpg = iscsit_global->discovery_tpg; 763 if (!login->leading_connection) 764 goto get_target; 765 766 sess->sess_ops->SessionType = 1; 767 /* 768 * Setup crc32c modules from libcrypto 769 */ 770 if (iscsi_login_setup_crypto(conn) < 0) { 771 pr_err("iscsi_login_setup_crypto() failed\n"); 772 ret = -1; 773 goto out; 774 } 775 /* 776 * Serialize access across the discovery struct iscsi_portal_group to 777 * process login attempt. 778 */ 779 if (iscsit_access_np(np, conn->tpg) < 0) { 780 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 781 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 782 ret = -1; 783 goto out; 784 } 785 ret = 0; 786 goto out; 787 } 788 789 get_target: 790 if (!t_buf) { 791 pr_err("TargetName key not received" 792 " in first login request while" 793 " SessionType=Normal.\n"); 794 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 795 ISCSI_LOGIN_STATUS_MISSING_FIELDS); 796 ret = -1; 797 goto out; 798 } 799 800 /* 801 * Locate Target IQN from Storage Node. 802 */ 803 tiqn = iscsit_get_tiqn_for_login(t_buf); 804 if (!tiqn) { 805 pr_err("Unable to locate Target IQN: %s in" 806 " Storage Node\n", t_buf); 807 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 808 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 809 ret = -1; 810 goto out; 811 } 812 pr_debug("Located Storage Object: %s\n", tiqn->tiqn); 813 814 /* 815 * Locate Target Portal Group from Storage Node. 816 */ 817 conn->tpg = iscsit_get_tpg_from_np(tiqn, np); 818 if (!conn->tpg) { 819 pr_err("Unable to locate Target Portal Group" 820 " on %s\n", tiqn->tiqn); 821 iscsit_put_tiqn_for_login(tiqn); 822 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 823 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 824 ret = -1; 825 goto out; 826 } 827 pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt); 828 /* 829 * Setup crc32c modules from libcrypto 830 */ 831 if (iscsi_login_setup_crypto(conn) < 0) { 832 pr_err("iscsi_login_setup_crypto() failed\n"); 833 ret = -1; 834 goto out; 835 } 836 /* 837 * Serialize access across the struct iscsi_portal_group to 838 * process login attempt. 839 */ 840 if (iscsit_access_np(np, conn->tpg) < 0) { 841 iscsit_put_tiqn_for_login(tiqn); 842 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, 843 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); 844 ret = -1; 845 conn->tpg = NULL; 846 goto out; 847 } 848 849 /* 850 * conn->sess->node_acl will be set when the referenced 851 * struct iscsi_session is located from received ISID+TSIH in 852 * iscsi_login_non_zero_tsih_s2(). 853 */ 854 if (!login->leading_connection) { 855 ret = 0; 856 goto out; 857 } 858 859 /* 860 * This value is required in iscsi_login_zero_tsih_s2() 861 */ 862 sess->sess_ops->SessionType = 0; 863 864 /* 865 * Locate incoming Initiator IQN reference from Storage Node. 866 */ 867 sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl( 868 &conn->tpg->tpg_se_tpg, i_buf); 869 if (!sess->se_sess->se_node_acl) { 870 pr_err("iSCSI Initiator Node: %s is not authorized to" 871 " access iSCSI target portal group: %hu.\n", 872 i_buf, conn->tpg->tpgt); 873 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, 874 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN); 875 ret = -1; 876 goto out; 877 } 878 879 ret = 0; 880 out: 881 kfree(tmpbuf); 882 return ret; 883 } 884 885 int iscsi_target_start_negotiation( 886 struct iscsi_login *login, 887 struct iscsi_conn *conn) 888 { 889 int ret; 890 891 ret = iscsi_target_do_login(conn, login); 892 if (ret != 0) 893 iscsi_remove_failed_auth_entry(conn); 894 895 iscsi_target_nego_release(conn); 896 return ret; 897 } 898 899 void iscsi_target_nego_release(struct iscsi_conn *conn) 900 { 901 struct iscsi_login *login = conn->conn_login; 902 903 if (!login) 904 return; 905 906 kfree(login->req_buf); 907 kfree(login->rsp_buf); 908 kfree(login); 909 910 conn->conn_login = NULL; 911 } 912