Linux Audio

Check our new training course

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