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/slab.h>
  22
  23#include "iscsi_target_core.h"
  24#include "iscsi_target_util.h"
  25#include "iscsi_target_parameters.h"
  26
  27int iscsi_login_rx_data(
  28	struct iscsi_conn *conn,
  29	char *buf,
  30	int length)
  31{
  32	int rx_got;
  33	struct kvec iov;
  34
  35	memset(&iov, 0, sizeof(struct kvec));
  36	iov.iov_len	= length;
  37	iov.iov_base	= buf;
  38
  39	/*
  40	 * Initial Marker-less Interval.
  41	 * Add the values regardless of IFMarker/OFMarker, considering
  42	 * it may not be negoitated yet.
  43	 */
  44	conn->of_marker += length;
  45
  46	rx_got = rx_data(conn, &iov, 1, length);
  47	if (rx_got != length) {
  48		pr_err("rx_data returned %d, expecting %d.\n",
  49				rx_got, length);
  50		return -1;
  51	}
  52
  53	return 0 ;
  54}
  55
  56int iscsi_login_tx_data(
  57	struct iscsi_conn *conn,
  58	char *pdu_buf,
  59	char *text_buf,
  60	int text_length)
  61{
  62	int length, tx_sent;
  63	struct kvec iov[2];
  64
  65	length = (ISCSI_HDR_LEN + text_length);
  66
  67	memset(&iov[0], 0, 2 * sizeof(struct kvec));
  68	iov[0].iov_len		= ISCSI_HDR_LEN;
  69	iov[0].iov_base		= pdu_buf;
  70	iov[1].iov_len		= text_length;
  71	iov[1].iov_base		= text_buf;
 
 
 
 
  72
  73	/*
  74	 * Initial Marker-less Interval.
  75	 * Add the values regardless of IFMarker/OFMarker, considering
  76	 * it may not be negoitated yet.
  77	 */
  78	conn->if_marker += length;
  79
  80	tx_sent = tx_data(conn, &iov[0], 2, length);
  81	if (tx_sent != length) {
  82		pr_err("tx_data returned %d, expecting %d.\n",
  83				tx_sent, length);
  84		return -1;
  85	}
  86
  87	return 0;
  88}
  89
  90void iscsi_dump_conn_ops(struct iscsi_conn_ops *conn_ops)
  91{
  92	pr_debug("HeaderDigest: %s\n", (conn_ops->HeaderDigest) ?
  93				"CRC32C" : "None");
  94	pr_debug("DataDigest: %s\n", (conn_ops->DataDigest) ?
  95				"CRC32C" : "None");
  96	pr_debug("MaxRecvDataSegmentLength: %u\n",
  97				conn_ops->MaxRecvDataSegmentLength);
  98	pr_debug("OFMarker: %s\n", (conn_ops->OFMarker) ? "Yes" : "No");
  99	pr_debug("IFMarker: %s\n", (conn_ops->IFMarker) ? "Yes" : "No");
 100	if (conn_ops->OFMarker)
 101		pr_debug("OFMarkInt: %u\n", conn_ops->OFMarkInt);
 102	if (conn_ops->IFMarker)
 103		pr_debug("IFMarkInt: %u\n", conn_ops->IFMarkInt);
 104}
 105
 106void iscsi_dump_sess_ops(struct iscsi_sess_ops *sess_ops)
 107{
 108	pr_debug("InitiatorName: %s\n", sess_ops->InitiatorName);
 109	pr_debug("InitiatorAlias: %s\n", sess_ops->InitiatorAlias);
 110	pr_debug("TargetName: %s\n", sess_ops->TargetName);
 111	pr_debug("TargetAlias: %s\n", sess_ops->TargetAlias);
 112	pr_debug("TargetPortalGroupTag: %hu\n",
 113			sess_ops->TargetPortalGroupTag);
 114	pr_debug("MaxConnections: %hu\n", sess_ops->MaxConnections);
 115	pr_debug("InitialR2T: %s\n",
 116			(sess_ops->InitialR2T) ? "Yes" : "No");
 117	pr_debug("ImmediateData: %s\n", (sess_ops->ImmediateData) ?
 118			"Yes" : "No");
 119	pr_debug("MaxBurstLength: %u\n", sess_ops->MaxBurstLength);
 120	pr_debug("FirstBurstLength: %u\n", sess_ops->FirstBurstLength);
 121	pr_debug("DefaultTime2Wait: %hu\n", sess_ops->DefaultTime2Wait);
 122	pr_debug("DefaultTime2Retain: %hu\n",
 123			sess_ops->DefaultTime2Retain);
 124	pr_debug("MaxOutstandingR2T: %hu\n",
 125			sess_ops->MaxOutstandingR2T);
 126	pr_debug("DataPDUInOrder: %s\n",
 127			(sess_ops->DataPDUInOrder) ? "Yes" : "No");
 128	pr_debug("DataSequenceInOrder: %s\n",
 129			(sess_ops->DataSequenceInOrder) ? "Yes" : "No");
 130	pr_debug("ErrorRecoveryLevel: %hu\n",
 131			sess_ops->ErrorRecoveryLevel);
 132	pr_debug("SessionType: %s\n", (sess_ops->SessionType) ?
 133			"Discovery" : "Normal");
 134}
 135
 136void iscsi_print_params(struct iscsi_param_list *param_list)
 137{
 138	struct iscsi_param *param;
 139
 140	list_for_each_entry(param, &param_list->param_list, p_list)
 141		pr_debug("%s: %s\n", param->name, param->value);
 142}
 143
 144static struct iscsi_param *iscsi_set_default_param(struct iscsi_param_list *param_list,
 145		char *name, char *value, u8 phase, u8 scope, u8 sender,
 146		u16 type_range, u8 use)
 147{
 148	struct iscsi_param *param = NULL;
 149
 150	param = kzalloc(sizeof(struct iscsi_param), GFP_KERNEL);
 151	if (!param) {
 152		pr_err("Unable to allocate memory for parameter.\n");
 153		goto out;
 154	}
 155	INIT_LIST_HEAD(&param->p_list);
 156
 157	param->name = kzalloc(strlen(name) + 1, GFP_KERNEL);
 158	if (!param->name) {
 159		pr_err("Unable to allocate memory for parameter name.\n");
 160		goto out;
 161	}
 162
 163	param->value = kzalloc(strlen(value) + 1, GFP_KERNEL);
 164	if (!param->value) {
 165		pr_err("Unable to allocate memory for parameter value.\n");
 166		goto out;
 167	}
 168
 169	memcpy(param->name, name, strlen(name));
 170	param->name[strlen(name)] = '\0';
 171	memcpy(param->value, value, strlen(value));
 172	param->value[strlen(value)] = '\0';
 173	param->phase		= phase;
 174	param->scope		= scope;
 175	param->sender		= sender;
 176	param->use		= use;
 177	param->type_range	= type_range;
 178
 179	switch (param->type_range) {
 180	case TYPERANGE_BOOL_AND:
 181		param->type = TYPE_BOOL_AND;
 182		break;
 183	case TYPERANGE_BOOL_OR:
 184		param->type = TYPE_BOOL_OR;
 185		break;
 186	case TYPERANGE_0_TO_2:
 187	case TYPERANGE_0_TO_3600:
 188	case TYPERANGE_0_TO_32767:
 189	case TYPERANGE_0_TO_65535:
 190	case TYPERANGE_1_TO_65535:
 191	case TYPERANGE_2_TO_3600:
 192	case TYPERANGE_512_TO_16777215:
 193		param->type = TYPE_NUMBER;
 194		break;
 195	case TYPERANGE_AUTH:
 196	case TYPERANGE_DIGEST:
 197		param->type = TYPE_VALUE_LIST | TYPE_STRING;
 198		break;
 199	case TYPERANGE_MARKINT:
 200		param->type = TYPE_NUMBER_RANGE;
 201		param->type_range |= TYPERANGE_1_TO_65535;
 202		break;
 203	case TYPERANGE_ISCSINAME:
 204	case TYPERANGE_SESSIONTYPE:
 205	case TYPERANGE_TARGETADDRESS:
 206	case TYPERANGE_UTF8:
 207		param->type = TYPE_STRING;
 208		break;
 209	default:
 210		pr_err("Unknown type_range 0x%02x\n",
 211				param->type_range);
 212		goto out;
 213	}
 214	list_add_tail(&param->p_list, &param_list->param_list);
 215
 216	return param;
 217out:
 218	if (param) {
 219		kfree(param->value);
 220		kfree(param->name);
 221		kfree(param);
 222	}
 223
 224	return NULL;
 225}
 226
 227/* #warning Add extension keys */
 228int iscsi_create_default_params(struct iscsi_param_list **param_list_ptr)
 229{
 230	struct iscsi_param *param = NULL;
 231	struct iscsi_param_list *pl;
 232
 233	pl = kzalloc(sizeof(struct iscsi_param_list), GFP_KERNEL);
 234	if (!pl) {
 235		pr_err("Unable to allocate memory for"
 236				" struct iscsi_param_list.\n");
 237		return -1 ;
 238	}
 239	INIT_LIST_HEAD(&pl->param_list);
 240	INIT_LIST_HEAD(&pl->extra_response_list);
 241
 242	/*
 243	 * The format for setting the initial parameter definitions are:
 244	 *
 245	 * Parameter name:
 246	 * Initial value:
 247	 * Allowable phase:
 248	 * Scope:
 249	 * Allowable senders:
 250	 * Typerange:
 251	 * Use:
 252	 */
 253	param = iscsi_set_default_param(pl, AUTHMETHOD, INITIAL_AUTHMETHOD,
 254			PHASE_SECURITY, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 255			TYPERANGE_AUTH, USE_INITIAL_ONLY);
 256	if (!param)
 257		goto out;
 258
 259	param = iscsi_set_default_param(pl, HEADERDIGEST, INITIAL_HEADERDIGEST,
 260			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 261			TYPERANGE_DIGEST, USE_INITIAL_ONLY);
 262	if (!param)
 263		goto out;
 264
 265	param = iscsi_set_default_param(pl, DATADIGEST, INITIAL_DATADIGEST,
 266			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 267			TYPERANGE_DIGEST, USE_INITIAL_ONLY);
 268	if (!param)
 269		goto out;
 270
 271	param = iscsi_set_default_param(pl, MAXCONNECTIONS,
 272			INITIAL_MAXCONNECTIONS, PHASE_OPERATIONAL,
 273			SCOPE_SESSION_WIDE, SENDER_BOTH,
 274			TYPERANGE_1_TO_65535, USE_LEADING_ONLY);
 275	if (!param)
 276		goto out;
 277
 278	param = iscsi_set_default_param(pl, SENDTARGETS, INITIAL_SENDTARGETS,
 279			PHASE_FFP0, SCOPE_SESSION_WIDE, SENDER_INITIATOR,
 280			TYPERANGE_UTF8, 0);
 281	if (!param)
 282		goto out;
 283
 284	param = iscsi_set_default_param(pl, TARGETNAME, INITIAL_TARGETNAME,
 285			PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_BOTH,
 286			TYPERANGE_ISCSINAME, USE_ALL);
 287	if (!param)
 288		goto out;
 289
 290	param = iscsi_set_default_param(pl, INITIATORNAME,
 291			INITIAL_INITIATORNAME, PHASE_DECLARATIVE,
 292			SCOPE_SESSION_WIDE, SENDER_INITIATOR,
 293			TYPERANGE_ISCSINAME, USE_INITIAL_ONLY);
 294	if (!param)
 295		goto out;
 296
 297	param = iscsi_set_default_param(pl, TARGETALIAS, INITIAL_TARGETALIAS,
 298			PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_TARGET,
 299			TYPERANGE_UTF8, USE_ALL);
 300	if (!param)
 301		goto out;
 302
 303	param = iscsi_set_default_param(pl, INITIATORALIAS,
 304			INITIAL_INITIATORALIAS, PHASE_DECLARATIVE,
 305			SCOPE_SESSION_WIDE, SENDER_INITIATOR, TYPERANGE_UTF8,
 306			USE_ALL);
 307	if (!param)
 308		goto out;
 309
 310	param = iscsi_set_default_param(pl, TARGETADDRESS,
 311			INITIAL_TARGETADDRESS, PHASE_DECLARATIVE,
 312			SCOPE_SESSION_WIDE, SENDER_TARGET,
 313			TYPERANGE_TARGETADDRESS, USE_ALL);
 314	if (!param)
 315		goto out;
 316
 317	param = iscsi_set_default_param(pl, TARGETPORTALGROUPTAG,
 318			INITIAL_TARGETPORTALGROUPTAG,
 319			PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_TARGET,
 320			TYPERANGE_0_TO_65535, USE_INITIAL_ONLY);
 321	if (!param)
 322		goto out;
 323
 324	param = iscsi_set_default_param(pl, INITIALR2T, INITIAL_INITIALR2T,
 325			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 326			TYPERANGE_BOOL_OR, USE_LEADING_ONLY);
 327	if (!param)
 328		goto out;
 329
 330	param = iscsi_set_default_param(pl, IMMEDIATEDATA,
 331			INITIAL_IMMEDIATEDATA, PHASE_OPERATIONAL,
 332			SCOPE_SESSION_WIDE, SENDER_BOTH, TYPERANGE_BOOL_AND,
 333			USE_LEADING_ONLY);
 334	if (!param)
 335		goto out;
 336
 
 
 
 
 
 
 
 337	param = iscsi_set_default_param(pl, MAXRECVDATASEGMENTLENGTH,
 338			INITIAL_MAXRECVDATASEGMENTLENGTH,
 339			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 340			TYPERANGE_512_TO_16777215, USE_ALL);
 341	if (!param)
 342		goto out;
 343
 344	param = iscsi_set_default_param(pl, MAXBURSTLENGTH,
 345			INITIAL_MAXBURSTLENGTH, PHASE_OPERATIONAL,
 346			SCOPE_SESSION_WIDE, SENDER_BOTH,
 347			TYPERANGE_512_TO_16777215, USE_LEADING_ONLY);
 348	if (!param)
 349		goto out;
 350
 351	param = iscsi_set_default_param(pl, FIRSTBURSTLENGTH,
 352			INITIAL_FIRSTBURSTLENGTH,
 353			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 354			TYPERANGE_512_TO_16777215, USE_LEADING_ONLY);
 355	if (!param)
 356		goto out;
 357
 358	param = iscsi_set_default_param(pl, DEFAULTTIME2WAIT,
 359			INITIAL_DEFAULTTIME2WAIT,
 360			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 361			TYPERANGE_0_TO_3600, USE_LEADING_ONLY);
 362	if (!param)
 363		goto out;
 364
 365	param = iscsi_set_default_param(pl, DEFAULTTIME2RETAIN,
 366			INITIAL_DEFAULTTIME2RETAIN,
 367			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 368			TYPERANGE_0_TO_3600, USE_LEADING_ONLY);
 369	if (!param)
 370		goto out;
 371
 372	param = iscsi_set_default_param(pl, MAXOUTSTANDINGR2T,
 373			INITIAL_MAXOUTSTANDINGR2T,
 374			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 375			TYPERANGE_1_TO_65535, USE_LEADING_ONLY);
 376	if (!param)
 377		goto out;
 378
 379	param = iscsi_set_default_param(pl, DATAPDUINORDER,
 380			INITIAL_DATAPDUINORDER, PHASE_OPERATIONAL,
 381			SCOPE_SESSION_WIDE, SENDER_BOTH, TYPERANGE_BOOL_OR,
 382			USE_LEADING_ONLY);
 383	if (!param)
 384		goto out;
 385
 386	param = iscsi_set_default_param(pl, DATASEQUENCEINORDER,
 387			INITIAL_DATASEQUENCEINORDER,
 388			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 389			TYPERANGE_BOOL_OR, USE_LEADING_ONLY);
 390	if (!param)
 391		goto out;
 392
 393	param = iscsi_set_default_param(pl, ERRORRECOVERYLEVEL,
 394			INITIAL_ERRORRECOVERYLEVEL,
 395			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 396			TYPERANGE_0_TO_2, USE_LEADING_ONLY);
 397	if (!param)
 398		goto out;
 399
 400	param = iscsi_set_default_param(pl, SESSIONTYPE, INITIAL_SESSIONTYPE,
 401			PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_INITIATOR,
 402			TYPERANGE_SESSIONTYPE, USE_LEADING_ONLY);
 403	if (!param)
 404		goto out;
 405
 406	param = iscsi_set_default_param(pl, IFMARKER, INITIAL_IFMARKER,
 407			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 408			TYPERANGE_BOOL_AND, USE_INITIAL_ONLY);
 409	if (!param)
 410		goto out;
 411
 412	param = iscsi_set_default_param(pl, OFMARKER, INITIAL_OFMARKER,
 413			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 414			TYPERANGE_BOOL_AND, USE_INITIAL_ONLY);
 415	if (!param)
 416		goto out;
 417
 418	param = iscsi_set_default_param(pl, IFMARKINT, INITIAL_IFMARKINT,
 419			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 420			TYPERANGE_MARKINT, USE_INITIAL_ONLY);
 421	if (!param)
 422		goto out;
 423
 424	param = iscsi_set_default_param(pl, OFMARKINT, INITIAL_OFMARKINT,
 425			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 426			TYPERANGE_MARKINT, USE_INITIAL_ONLY);
 427	if (!param)
 428		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 429
 430	*param_list_ptr = pl;
 431	return 0;
 432out:
 433	iscsi_release_param_list(pl);
 434	return -1;
 435}
 436
 437int iscsi_set_keys_to_negotiate(
 438	int sessiontype,
 439	struct iscsi_param_list *param_list)
 440{
 441	struct iscsi_param *param;
 442
 
 
 443	list_for_each_entry(param, &param_list->param_list, p_list) {
 444		param->state = 0;
 445		if (!strcmp(param->name, AUTHMETHOD)) {
 446			SET_PSTATE_NEGOTIATE(param);
 447		} else if (!strcmp(param->name, HEADERDIGEST)) {
 448			SET_PSTATE_NEGOTIATE(param);
 
 449		} else if (!strcmp(param->name, DATADIGEST)) {
 450			SET_PSTATE_NEGOTIATE(param);
 
 451		} else if (!strcmp(param->name, MAXCONNECTIONS)) {
 452			SET_PSTATE_NEGOTIATE(param);
 453		} else if (!strcmp(param->name, TARGETNAME)) {
 454			continue;
 455		} else if (!strcmp(param->name, INITIATORNAME)) {
 456			continue;
 457		} else if (!strcmp(param->name, TARGETALIAS)) {
 458			if (param->value)
 459				SET_PSTATE_NEGOTIATE(param);
 460		} else if (!strcmp(param->name, INITIATORALIAS)) {
 461			continue;
 462		} else if (!strcmp(param->name, TARGETPORTALGROUPTAG)) {
 463			SET_PSTATE_NEGOTIATE(param);
 464		} else if (!strcmp(param->name, INITIALR2T)) {
 465			SET_PSTATE_NEGOTIATE(param);
 466		} else if (!strcmp(param->name, IMMEDIATEDATA)) {
 467			SET_PSTATE_NEGOTIATE(param);
 468		} else if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
 469			SET_PSTATE_NEGOTIATE(param);
 
 
 
 470		} else if (!strcmp(param->name, MAXBURSTLENGTH)) {
 471			SET_PSTATE_NEGOTIATE(param);
 472		} else if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
 473			SET_PSTATE_NEGOTIATE(param);
 474		} else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
 475			SET_PSTATE_NEGOTIATE(param);
 476		} else if (!strcmp(param->name, DEFAULTTIME2RETAIN)) {
 477			SET_PSTATE_NEGOTIATE(param);
 478		} else if (!strcmp(param->name, MAXOUTSTANDINGR2T)) {
 479			SET_PSTATE_NEGOTIATE(param);
 480		} else if (!strcmp(param->name, DATAPDUINORDER)) {
 481			SET_PSTATE_NEGOTIATE(param);
 482		} else if (!strcmp(param->name, DATASEQUENCEINORDER)) {
 483			SET_PSTATE_NEGOTIATE(param);
 484		} else if (!strcmp(param->name, ERRORRECOVERYLEVEL)) {
 485			SET_PSTATE_NEGOTIATE(param);
 486		} else if (!strcmp(param->name, SESSIONTYPE)) {
 487			SET_PSTATE_NEGOTIATE(param);
 488		} else if (!strcmp(param->name, IFMARKER)) {
 489			SET_PSTATE_NEGOTIATE(param);
 490		} else if (!strcmp(param->name, OFMARKER)) {
 491			SET_PSTATE_NEGOTIATE(param);
 492		} else if (!strcmp(param->name, IFMARKINT)) {
 493			SET_PSTATE_NEGOTIATE(param);
 494		} else if (!strcmp(param->name, OFMARKINT)) {
 495			SET_PSTATE_NEGOTIATE(param);
 
 
 
 
 
 
 
 
 
 496		}
 497	}
 498
 499	return 0;
 500}
 501
 502int iscsi_set_keys_irrelevant_for_discovery(
 503	struct iscsi_param_list *param_list)
 504{
 505	struct iscsi_param *param;
 506
 507	list_for_each_entry(param, &param_list->param_list, p_list) {
 508		if (!strcmp(param->name, MAXCONNECTIONS))
 509			param->state &= ~PSTATE_NEGOTIATE;
 510		else if (!strcmp(param->name, INITIALR2T))
 511			param->state &= ~PSTATE_NEGOTIATE;
 512		else if (!strcmp(param->name, IMMEDIATEDATA))
 513			param->state &= ~PSTATE_NEGOTIATE;
 514		else if (!strcmp(param->name, MAXBURSTLENGTH))
 515			param->state &= ~PSTATE_NEGOTIATE;
 516		else if (!strcmp(param->name, FIRSTBURSTLENGTH))
 517			param->state &= ~PSTATE_NEGOTIATE;
 518		else if (!strcmp(param->name, MAXOUTSTANDINGR2T))
 519			param->state &= ~PSTATE_NEGOTIATE;
 520		else if (!strcmp(param->name, DATAPDUINORDER))
 521			param->state &= ~PSTATE_NEGOTIATE;
 522		else if (!strcmp(param->name, DATASEQUENCEINORDER))
 523			param->state &= ~PSTATE_NEGOTIATE;
 524		else if (!strcmp(param->name, ERRORRECOVERYLEVEL))
 525			param->state &= ~PSTATE_NEGOTIATE;
 526		else if (!strcmp(param->name, DEFAULTTIME2WAIT))
 527			param->state &= ~PSTATE_NEGOTIATE;
 528		else if (!strcmp(param->name, DEFAULTTIME2RETAIN))
 529			param->state &= ~PSTATE_NEGOTIATE;
 530		else if (!strcmp(param->name, IFMARKER))
 531			param->state &= ~PSTATE_NEGOTIATE;
 532		else if (!strcmp(param->name, OFMARKER))
 533			param->state &= ~PSTATE_NEGOTIATE;
 534		else if (!strcmp(param->name, IFMARKINT))
 535			param->state &= ~PSTATE_NEGOTIATE;
 536		else if (!strcmp(param->name, OFMARKINT))
 537			param->state &= ~PSTATE_NEGOTIATE;
 
 
 
 
 
 
 538	}
 539
 540	return 0;
 541}
 542
 543int iscsi_copy_param_list(
 544	struct iscsi_param_list **dst_param_list,
 545	struct iscsi_param_list *src_param_list,
 546	int leading)
 547{
 548	struct iscsi_param *param = NULL;
 549	struct iscsi_param *new_param = NULL;
 550	struct iscsi_param_list *param_list = NULL;
 551
 552	param_list = kzalloc(sizeof(struct iscsi_param_list), GFP_KERNEL);
 553	if (!param_list) {
 554		pr_err("Unable to allocate memory for struct iscsi_param_list.\n");
 555		goto err_out;
 556	}
 557	INIT_LIST_HEAD(&param_list->param_list);
 558	INIT_LIST_HEAD(&param_list->extra_response_list);
 559
 560	list_for_each_entry(param, &src_param_list->param_list, p_list) {
 561		if (!leading && (param->scope & SCOPE_SESSION_WIDE)) {
 562			if ((strcmp(param->name, "TargetName") != 0) &&
 563			    (strcmp(param->name, "InitiatorName") != 0) &&
 564			    (strcmp(param->name, "TargetPortalGroupTag") != 0))
 565				continue;
 566		}
 567
 568		new_param = kzalloc(sizeof(struct iscsi_param), GFP_KERNEL);
 569		if (!new_param) {
 570			pr_err("Unable to allocate memory for struct iscsi_param.\n");
 571			goto err_out;
 572		}
 573
 574		new_param->name = kstrdup(param->name, GFP_KERNEL);
 575		new_param->value = kstrdup(param->value, GFP_KERNEL);
 576		if (!new_param->value || !new_param->name) {
 577			kfree(new_param->value);
 578			kfree(new_param->name);
 579			kfree(new_param);
 580			pr_err("Unable to allocate memory for parameter name/value.\n");
 581			goto err_out;
 582		}
 583
 584		new_param->set_param = param->set_param;
 585		new_param->phase = param->phase;
 586		new_param->scope = param->scope;
 587		new_param->sender = param->sender;
 588		new_param->type = param->type;
 589		new_param->use = param->use;
 590		new_param->type_range = param->type_range;
 591
 592		list_add_tail(&new_param->p_list, &param_list->param_list);
 593	}
 594
 595	if (!list_empty(&param_list->param_list)) {
 596		*dst_param_list = param_list;
 597	} else {
 598		pr_err("No parameters allocated.\n");
 599		goto err_out;
 600	}
 601
 602	return 0;
 603
 604err_out:
 605	iscsi_release_param_list(param_list);
 606	return -1;
 607}
 608
 609static void iscsi_release_extra_responses(struct iscsi_param_list *param_list)
 610{
 611	struct iscsi_extra_response *er, *er_tmp;
 612
 613	list_for_each_entry_safe(er, er_tmp, &param_list->extra_response_list,
 614			er_list) {
 615		list_del(&er->er_list);
 616		kfree(er);
 617	}
 618}
 619
 620void iscsi_release_param_list(struct iscsi_param_list *param_list)
 621{
 622	struct iscsi_param *param, *param_tmp;
 623
 624	list_for_each_entry_safe(param, param_tmp, &param_list->param_list,
 625			p_list) {
 626		list_del(&param->p_list);
 627
 628		kfree(param->name);
 629		param->name = NULL;
 630		kfree(param->value);
 631		param->value = NULL;
 632		kfree(param);
 633		param = NULL;
 634	}
 635
 636	iscsi_release_extra_responses(param_list);
 637
 638	kfree(param_list);
 639}
 640
 641struct iscsi_param *iscsi_find_param_from_key(
 642	char *key,
 643	struct iscsi_param_list *param_list)
 644{
 645	struct iscsi_param *param;
 646
 647	if (!key || !param_list) {
 648		pr_err("Key or parameter list pointer is NULL.\n");
 649		return NULL;
 650	}
 651
 652	list_for_each_entry(param, &param_list->param_list, p_list) {
 653		if (!strcmp(key, param->name))
 654			return param;
 655	}
 656
 657	pr_err("Unable to locate key \"%s\".\n", key);
 658	return NULL;
 659}
 660
 661int iscsi_extract_key_value(char *textbuf, char **key, char **value)
 662{
 663	*value = strchr(textbuf, '=');
 664	if (!*value) {
 665		pr_err("Unable to locate \"=\" seperator for key,"
 666				" ignoring request.\n");
 667		return -1;
 668	}
 669
 670	*key = textbuf;
 671	**value = '\0';
 672	*value = *value + 1;
 673
 674	return 0;
 675}
 676
 677int iscsi_update_param_value(struct iscsi_param *param, char *value)
 678{
 679	kfree(param->value);
 680
 681	param->value = kzalloc(strlen(value) + 1, GFP_KERNEL);
 682	if (!param->value) {
 683		pr_err("Unable to allocate memory for value.\n");
 684		return -1;
 685	}
 686
 687	memcpy(param->value, value, strlen(value));
 688	param->value[strlen(value)] = '\0';
 689
 690	pr_debug("iSCSI Parameter updated to %s=%s\n",
 691			param->name, param->value);
 692	return 0;
 693}
 694
 695static int iscsi_add_notunderstood_response(
 696	char *key,
 697	char *value,
 698	struct iscsi_param_list *param_list)
 699{
 700	struct iscsi_extra_response *extra_response;
 701
 702	if (strlen(value) > VALUE_MAXLEN) {
 703		pr_err("Value for notunderstood key \"%s\" exceeds %d,"
 704			" protocol error.\n", key, VALUE_MAXLEN);
 705		return -1;
 706	}
 707
 708	extra_response = kzalloc(sizeof(struct iscsi_extra_response), GFP_KERNEL);
 709	if (!extra_response) {
 710		pr_err("Unable to allocate memory for"
 711			" struct iscsi_extra_response.\n");
 712		return -1;
 713	}
 714	INIT_LIST_HEAD(&extra_response->er_list);
 715
 716	strncpy(extra_response->key, key, strlen(key) + 1);
 717	strncpy(extra_response->value, NOTUNDERSTOOD,
 718			strlen(NOTUNDERSTOOD) + 1);
 719
 720	list_add_tail(&extra_response->er_list,
 721			&param_list->extra_response_list);
 722	return 0;
 723}
 724
 725static int iscsi_check_for_auth_key(char *key)
 726{
 727	/*
 728	 * RFC 1994
 729	 */
 730	if (!strcmp(key, "CHAP_A") || !strcmp(key, "CHAP_I") ||
 731	    !strcmp(key, "CHAP_C") || !strcmp(key, "CHAP_N") ||
 732	    !strcmp(key, "CHAP_R"))
 733		return 1;
 734
 735	/*
 736	 * RFC 2945
 737	 */
 738	if (!strcmp(key, "SRP_U") || !strcmp(key, "SRP_N") ||
 739	    !strcmp(key, "SRP_g") || !strcmp(key, "SRP_s") ||
 740	    !strcmp(key, "SRP_A") || !strcmp(key, "SRP_B") ||
 741	    !strcmp(key, "SRP_M") || !strcmp(key, "SRP_HM"))
 742		return 1;
 743
 744	return 0;
 745}
 746
 747static void iscsi_check_proposer_for_optional_reply(struct iscsi_param *param)
 748{
 749	if (IS_TYPE_BOOL_AND(param)) {
 750		if (!strcmp(param->value, NO))
 751			SET_PSTATE_REPLY_OPTIONAL(param);
 752	} else if (IS_TYPE_BOOL_OR(param)) {
 753		if (!strcmp(param->value, YES))
 754			SET_PSTATE_REPLY_OPTIONAL(param);
 755		 /*
 756		  * Required for gPXE iSCSI boot client
 757		  */
 758		if (!strcmp(param->name, IMMEDIATEDATA))
 759			SET_PSTATE_REPLY_OPTIONAL(param);
 760	} else if (IS_TYPE_NUMBER(param)) {
 761		if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH))
 762			SET_PSTATE_REPLY_OPTIONAL(param);
 763		/*
 764		 * The GlobalSAN iSCSI Initiator for MacOSX does
 765		 * not respond to MaxBurstLength, FirstBurstLength,
 766		 * DefaultTime2Wait or DefaultTime2Retain parameter keys.
 767		 * So, we set them to 'reply optional' here, and assume the
 768		 * the defaults from iscsi_parameters.h if the initiator
 769		 * is not RFC compliant and the keys are not negotiated.
 770		 */
 771		if (!strcmp(param->name, MAXBURSTLENGTH))
 772			SET_PSTATE_REPLY_OPTIONAL(param);
 773		if (!strcmp(param->name, FIRSTBURSTLENGTH))
 774			SET_PSTATE_REPLY_OPTIONAL(param);
 775		if (!strcmp(param->name, DEFAULTTIME2WAIT))
 776			SET_PSTATE_REPLY_OPTIONAL(param);
 777		if (!strcmp(param->name, DEFAULTTIME2RETAIN))
 778			SET_PSTATE_REPLY_OPTIONAL(param);
 779		/*
 780		 * Required for gPXE iSCSI boot client
 781		 */
 782		if (!strcmp(param->name, MAXCONNECTIONS))
 783			SET_PSTATE_REPLY_OPTIONAL(param);
 784	} else if (IS_PHASE_DECLARATIVE(param))
 785		SET_PSTATE_REPLY_OPTIONAL(param);
 786}
 787
 788static int iscsi_check_boolean_value(struct iscsi_param *param, char *value)
 789{
 790	if (strcmp(value, YES) && strcmp(value, NO)) {
 791		pr_err("Illegal value for \"%s\", must be either"
 792			" \"%s\" or \"%s\".\n", param->name, YES, NO);
 793		return -1;
 794	}
 795
 796	return 0;
 797}
 798
 799static int iscsi_check_numerical_value(struct iscsi_param *param, char *value_ptr)
 800{
 801	char *tmpptr;
 802	int value = 0;
 803
 804	value = simple_strtoul(value_ptr, &tmpptr, 0);
 805
 806/* #warning FIXME: Fix this */
 807#if 0
 808	if (strspn(endptr, WHITE_SPACE) != strlen(endptr)) {
 809		pr_err("Illegal value \"%s\" for \"%s\".\n",
 810			value, param->name);
 811		return -1;
 812	}
 813#endif
 814	if (IS_TYPERANGE_0_TO_2(param)) {
 815		if ((value < 0) || (value > 2)) {
 816			pr_err("Illegal value for \"%s\", must be"
 817				" between 0 and 2.\n", param->name);
 818			return -1;
 819		}
 820		return 0;
 821	}
 822	if (IS_TYPERANGE_0_TO_3600(param)) {
 823		if ((value < 0) || (value > 3600)) {
 824			pr_err("Illegal value for \"%s\", must be"
 825				" between 0 and 3600.\n", param->name);
 826			return -1;
 827		}
 828		return 0;
 829	}
 830	if (IS_TYPERANGE_0_TO_32767(param)) {
 831		if ((value < 0) || (value > 32767)) {
 832			pr_err("Illegal value for \"%s\", must be"
 833				" between 0 and 32767.\n", param->name);
 834			return -1;
 835		}
 836		return 0;
 837	}
 838	if (IS_TYPERANGE_0_TO_65535(param)) {
 839		if ((value < 0) || (value > 65535)) {
 840			pr_err("Illegal value for \"%s\", must be"
 841				" between 0 and 65535.\n", param->name);
 842			return -1;
 843		}
 844		return 0;
 845	}
 846	if (IS_TYPERANGE_1_TO_65535(param)) {
 847		if ((value < 1) || (value > 65535)) {
 848			pr_err("Illegal value for \"%s\", must be"
 849				" between 1 and 65535.\n", param->name);
 850			return -1;
 851		}
 852		return 0;
 853	}
 854	if (IS_TYPERANGE_2_TO_3600(param)) {
 855		if ((value < 2) || (value > 3600)) {
 856			pr_err("Illegal value for \"%s\", must be"
 857				" between 2 and 3600.\n", param->name);
 858			return -1;
 859		}
 860		return 0;
 861	}
 862	if (IS_TYPERANGE_512_TO_16777215(param)) {
 863		if ((value < 512) || (value > 16777215)) {
 864			pr_err("Illegal value for \"%s\", must be"
 865				" between 512 and 16777215.\n", param->name);
 866			return -1;
 867		}
 868		return 0;
 869	}
 870
 871	return 0;
 872}
 873
 874static int iscsi_check_numerical_range_value(struct iscsi_param *param, char *value)
 875{
 876	char *left_val_ptr = NULL, *right_val_ptr = NULL;
 877	char *tilde_ptr = NULL, *tmp_ptr = NULL;
 878	u32 left_val, right_val, local_left_val, local_right_val;
 879
 880	if (strcmp(param->name, IFMARKINT) &&
 881	    strcmp(param->name, OFMARKINT)) {
 882		pr_err("Only parameters \"%s\" or \"%s\" may contain a"
 883		       " numerical range value.\n", IFMARKINT, OFMARKINT);
 884		return -1;
 885	}
 886
 887	if (IS_PSTATE_PROPOSER(param))
 888		return 0;
 889
 890	tilde_ptr = strchr(value, '~');
 891	if (!tilde_ptr) {
 892		pr_err("Unable to locate numerical range indicator"
 893			" \"~\" for \"%s\".\n", param->name);
 894		return -1;
 895	}
 896	*tilde_ptr = '\0';
 897
 898	left_val_ptr = value;
 899	right_val_ptr = value + strlen(left_val_ptr) + 1;
 900
 901	if (iscsi_check_numerical_value(param, left_val_ptr) < 0)
 902		return -1;
 903	if (iscsi_check_numerical_value(param, right_val_ptr) < 0)
 904		return -1;
 905
 906	left_val = simple_strtoul(left_val_ptr, &tmp_ptr, 0);
 907	right_val = simple_strtoul(right_val_ptr, &tmp_ptr, 0);
 908	*tilde_ptr = '~';
 909
 910	if (right_val < left_val) {
 911		pr_err("Numerical range for parameter \"%s\" contains"
 912			" a right value which is less than the left.\n",
 913				param->name);
 914		return -1;
 915	}
 916
 917	/*
 918	 * For now,  enforce reasonable defaults for [I,O]FMarkInt.
 919	 */
 920	tilde_ptr = strchr(param->value, '~');
 921	if (!tilde_ptr) {
 922		pr_err("Unable to locate numerical range indicator"
 923			" \"~\" for \"%s\".\n", param->name);
 924		return -1;
 925	}
 926	*tilde_ptr = '\0';
 927
 928	left_val_ptr = param->value;
 929	right_val_ptr = param->value + strlen(left_val_ptr) + 1;
 930
 931	local_left_val = simple_strtoul(left_val_ptr, &tmp_ptr, 0);
 932	local_right_val = simple_strtoul(right_val_ptr, &tmp_ptr, 0);
 933	*tilde_ptr = '~';
 934
 935	if (param->set_param) {
 936		if ((left_val < local_left_val) ||
 937		    (right_val < local_left_val)) {
 938			pr_err("Passed value range \"%u~%u\" is below"
 939				" minimum left value \"%u\" for key \"%s\","
 940				" rejecting.\n", left_val, right_val,
 941				local_left_val, param->name);
 942			return -1;
 943		}
 944	} else {
 945		if ((left_val < local_left_val) &&
 946		    (right_val < local_left_val)) {
 947			pr_err("Received value range \"%u~%u\" is"
 948				" below minimum left value \"%u\" for key"
 949				" \"%s\", rejecting.\n", left_val, right_val,
 950				local_left_val, param->name);
 951			SET_PSTATE_REJECT(param);
 952			if (iscsi_update_param_value(param, REJECT) < 0)
 953				return -1;
 954		}
 955	}
 956
 957	return 0;
 958}
 959
 960static int iscsi_check_string_or_list_value(struct iscsi_param *param, char *value)
 961{
 962	if (IS_PSTATE_PROPOSER(param))
 963		return 0;
 964
 965	if (IS_TYPERANGE_AUTH_PARAM(param)) {
 966		if (strcmp(value, KRB5) && strcmp(value, SPKM1) &&
 967		    strcmp(value, SPKM2) && strcmp(value, SRP) &&
 968		    strcmp(value, CHAP) && strcmp(value, NONE)) {
 969			pr_err("Illegal value for \"%s\", must be"
 970				" \"%s\", \"%s\", \"%s\", \"%s\", \"%s\""
 971				" or \"%s\".\n", param->name, KRB5,
 972					SPKM1, SPKM2, SRP, CHAP, NONE);
 973			return -1;
 974		}
 975	}
 976	if (IS_TYPERANGE_DIGEST_PARAM(param)) {
 977		if (strcmp(value, CRC32C) && strcmp(value, NONE)) {
 978			pr_err("Illegal value for \"%s\", must be"
 979				" \"%s\" or \"%s\".\n", param->name,
 980					CRC32C, NONE);
 981			return -1;
 982		}
 983	}
 984	if (IS_TYPERANGE_SESSIONTYPE(param)) {
 985		if (strcmp(value, DISCOVERY) && strcmp(value, NORMAL)) {
 986			pr_err("Illegal value for \"%s\", must be"
 987				" \"%s\" or \"%s\".\n", param->name,
 988					DISCOVERY, NORMAL);
 989			return -1;
 990		}
 991	}
 992
 993	return 0;
 994}
 995
 996/*
 997 *	This function is used to pick a value range number,  currently just
 998 *	returns the lesser of both right values.
 999 */
1000static char *iscsi_get_value_from_number_range(
1001	struct iscsi_param *param,
1002	char *value)
1003{
1004	char *end_ptr, *tilde_ptr1 = NULL, *tilde_ptr2 = NULL;
1005	u32 acceptor_right_value, proposer_right_value;
1006
1007	tilde_ptr1 = strchr(value, '~');
1008	if (!tilde_ptr1)
1009		return NULL;
1010	*tilde_ptr1++ = '\0';
1011	proposer_right_value = simple_strtoul(tilde_ptr1, &end_ptr, 0);
1012
1013	tilde_ptr2 = strchr(param->value, '~');
1014	if (!tilde_ptr2)
1015		return NULL;
1016	*tilde_ptr2++ = '\0';
1017	acceptor_right_value = simple_strtoul(tilde_ptr2, &end_ptr, 0);
1018
1019	return (acceptor_right_value >= proposer_right_value) ?
1020		tilde_ptr1 : tilde_ptr2;
1021}
1022
1023static char *iscsi_check_valuelist_for_support(
1024	struct iscsi_param *param,
1025	char *value)
1026{
1027	char *tmp1 = NULL, *tmp2 = NULL;
1028	char *acceptor_values = NULL, *proposer_values = NULL;
1029
1030	acceptor_values = param->value;
1031	proposer_values = value;
1032
1033	do {
1034		if (!proposer_values)
1035			return NULL;
1036		tmp1 = strchr(proposer_values, ',');
1037		if (tmp1)
1038			*tmp1 = '\0';
1039		acceptor_values = param->value;
1040		do {
1041			if (!acceptor_values) {
1042				if (tmp1)
1043					*tmp1 = ',';
1044				return NULL;
1045			}
1046			tmp2 = strchr(acceptor_values, ',');
1047			if (tmp2)
1048				*tmp2 = '\0';
1049			if (!acceptor_values || !proposer_values) {
1050				if (tmp1)
1051					*tmp1 = ',';
1052				if (tmp2)
1053					*tmp2 = ',';
1054				return NULL;
1055			}
1056			if (!strcmp(acceptor_values, proposer_values)) {
1057				if (tmp2)
1058					*tmp2 = ',';
1059				goto out;
1060			}
1061			if (tmp2)
1062				*tmp2++ = ',';
1063
1064			acceptor_values = tmp2;
1065			if (!acceptor_values)
1066				break;
1067		} while (acceptor_values);
1068		if (tmp1)
1069			*tmp1++ = ',';
1070		proposer_values = tmp1;
1071	} while (proposer_values);
1072
1073out:
1074	return proposer_values;
1075}
1076
1077static int iscsi_check_acceptor_state(struct iscsi_param *param, char *value)
 
1078{
1079	u8 acceptor_boolean_value = 0, proposer_boolean_value = 0;
1080	char *negoitated_value = NULL;
1081
1082	if (IS_PSTATE_ACCEPTOR(param)) {
1083		pr_err("Received key \"%s\" twice, protocol error.\n",
1084				param->name);
1085		return -1;
1086	}
1087
1088	if (IS_PSTATE_REJECT(param))
1089		return 0;
1090
1091	if (IS_TYPE_BOOL_AND(param)) {
1092		if (!strcmp(value, YES))
1093			proposer_boolean_value = 1;
1094		if (!strcmp(param->value, YES))
1095			acceptor_boolean_value = 1;
1096		if (acceptor_boolean_value && proposer_boolean_value)
1097			do {} while (0);
1098		else {
1099			if (iscsi_update_param_value(param, NO) < 0)
1100				return -1;
1101			if (!proposer_boolean_value)
1102				SET_PSTATE_REPLY_OPTIONAL(param);
1103		}
1104	} else if (IS_TYPE_BOOL_OR(param)) {
1105		if (!strcmp(value, YES))
1106			proposer_boolean_value = 1;
1107		if (!strcmp(param->value, YES))
1108			acceptor_boolean_value = 1;
1109		if (acceptor_boolean_value || proposer_boolean_value) {
1110			if (iscsi_update_param_value(param, YES) < 0)
1111				return -1;
1112			if (proposer_boolean_value)
1113				SET_PSTATE_REPLY_OPTIONAL(param);
1114		}
1115	} else if (IS_TYPE_NUMBER(param)) {
1116		char *tmpptr, buf[10];
1117		u32 acceptor_value = simple_strtoul(param->value, &tmpptr, 0);
1118		u32 proposer_value = simple_strtoul(value, &tmpptr, 0);
1119
1120		memset(buf, 0, 10);
1121
1122		if (!strcmp(param->name, MAXCONNECTIONS) ||
1123		    !strcmp(param->name, MAXBURSTLENGTH) ||
1124		    !strcmp(param->name, FIRSTBURSTLENGTH) ||
1125		    !strcmp(param->name, MAXOUTSTANDINGR2T) ||
1126		    !strcmp(param->name, DEFAULTTIME2RETAIN) ||
1127		    !strcmp(param->name, ERRORRECOVERYLEVEL)) {
1128			if (proposer_value > acceptor_value) {
1129				sprintf(buf, "%u", acceptor_value);
1130				if (iscsi_update_param_value(param,
1131						&buf[0]) < 0)
1132					return -1;
1133			} else {
1134				if (iscsi_update_param_value(param, value) < 0)
1135					return -1;
1136			}
1137		} else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
1138			if (acceptor_value > proposer_value) {
1139				sprintf(buf, "%u", acceptor_value);
1140				if (iscsi_update_param_value(param,
1141						&buf[0]) < 0)
1142					return -1;
1143			} else {
1144				if (iscsi_update_param_value(param, value) < 0)
1145					return -1;
1146			}
1147		} else {
1148			if (iscsi_update_param_value(param, value) < 0)
1149				return -1;
1150		}
1151
1152		if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH))
1153			SET_PSTATE_REPLY_OPTIONAL(param);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1154	} else if (IS_TYPE_NUMBER_RANGE(param)) {
1155		negoitated_value = iscsi_get_value_from_number_range(
1156					param, value);
1157		if (!negoitated_value)
1158			return -1;
1159		if (iscsi_update_param_value(param, negoitated_value) < 0)
1160			return -1;
1161	} else if (IS_TYPE_VALUE_LIST(param)) {
1162		negoitated_value = iscsi_check_valuelist_for_support(
1163					param, value);
1164		if (!negoitated_value) {
1165			pr_err("Proposer's value list \"%s\" contains"
1166				" no valid values from Acceptor's value list"
1167				" \"%s\".\n", value, param->value);
1168			return -1;
1169		}
1170		if (iscsi_update_param_value(param, negoitated_value) < 0)
1171			return -1;
1172	} else if (IS_PHASE_DECLARATIVE(param)) {
1173		if (iscsi_update_param_value(param, value) < 0)
1174			return -1;
1175		SET_PSTATE_REPLY_OPTIONAL(param);
1176	}
1177
1178	return 0;
1179}
1180
1181static int iscsi_check_proposer_state(struct iscsi_param *param, char *value)
1182{
1183	if (IS_PSTATE_RESPONSE_GOT(param)) {
1184		pr_err("Received key \"%s\" twice, protocol error.\n",
1185				param->name);
1186		return -1;
1187	}
1188
1189	if (IS_TYPE_NUMBER_RANGE(param)) {
1190		u32 left_val = 0, right_val = 0, recieved_value = 0;
1191		char *left_val_ptr = NULL, *right_val_ptr = NULL;
1192		char *tilde_ptr = NULL, *tmp_ptr = NULL;
1193
1194		if (!strcmp(value, IRRELEVANT) || !strcmp(value, REJECT)) {
1195			if (iscsi_update_param_value(param, value) < 0)
1196				return -1;
1197			return 0;
1198		}
1199
1200		tilde_ptr = strchr(value, '~');
1201		if (tilde_ptr) {
1202			pr_err("Illegal \"~\" in response for \"%s\".\n",
1203					param->name);
1204			return -1;
1205		}
1206		tilde_ptr = strchr(param->value, '~');
1207		if (!tilde_ptr) {
1208			pr_err("Unable to locate numerical range"
1209				" indicator \"~\" for \"%s\".\n", param->name);
1210			return -1;
1211		}
1212		*tilde_ptr = '\0';
1213
1214		left_val_ptr = param->value;
1215		right_val_ptr = param->value + strlen(left_val_ptr) + 1;
1216		left_val = simple_strtoul(left_val_ptr, &tmp_ptr, 0);
1217		right_val = simple_strtoul(right_val_ptr, &tmp_ptr, 0);
1218		recieved_value = simple_strtoul(value, &tmp_ptr, 0);
1219
1220		*tilde_ptr = '~';
1221
1222		if ((recieved_value < left_val) ||
1223		    (recieved_value > right_val)) {
1224			pr_err("Illegal response \"%s=%u\", value must"
1225				" be between %u and %u.\n", param->name,
1226				recieved_value, left_val, right_val);
1227			return -1;
1228		}
1229	} else if (IS_TYPE_VALUE_LIST(param)) {
1230		char *comma_ptr = NULL, *tmp_ptr = NULL;
1231
1232		comma_ptr = strchr(value, ',');
1233		if (comma_ptr) {
1234			pr_err("Illegal \",\" in response for \"%s\".\n",
1235					param->name);
1236			return -1;
1237		}
1238
1239		tmp_ptr = iscsi_check_valuelist_for_support(param, value);
1240		if (!tmp_ptr)
1241			return -1;
1242	}
1243
1244	if (iscsi_update_param_value(param, value) < 0)
1245		return -1;
1246
1247	return 0;
1248}
1249
1250static int iscsi_check_value(struct iscsi_param *param, char *value)
1251{
1252	char *comma_ptr = NULL;
1253
1254	if (!strcmp(value, REJECT)) {
1255		if (!strcmp(param->name, IFMARKINT) ||
1256		    !strcmp(param->name, OFMARKINT)) {
1257			/*
1258			 * Reject is not fatal for [I,O]FMarkInt,  and causes
1259			 * [I,O]FMarker to be reset to No. (See iSCSI v20 A.3.2)
1260			 */
1261			SET_PSTATE_REJECT(param);
1262			return 0;
1263		}
1264		pr_err("Received %s=%s\n", param->name, value);
1265		return -1;
1266	}
1267	if (!strcmp(value, IRRELEVANT)) {
1268		pr_debug("Received %s=%s\n", param->name, value);
1269		SET_PSTATE_IRRELEVANT(param);
1270		return 0;
1271	}
1272	if (!strcmp(value, NOTUNDERSTOOD)) {
1273		if (!IS_PSTATE_PROPOSER(param)) {
1274			pr_err("Received illegal offer %s=%s\n",
1275				param->name, value);
1276			return -1;
1277		}
1278
1279/* #warning FIXME: Add check for X-ExtensionKey here */
1280		pr_err("Standard iSCSI key \"%s\" cannot be answered"
1281			" with \"%s\", protocol error.\n", param->name, value);
1282		return -1;
1283	}
1284
1285	do {
1286		comma_ptr = NULL;
1287		comma_ptr = strchr(value, ',');
1288
1289		if (comma_ptr && !IS_TYPE_VALUE_LIST(param)) {
1290			pr_err("Detected value seperator \",\", but"
1291				" key \"%s\" does not allow a value list,"
1292				" protocol error.\n", param->name);
1293			return -1;
1294		}
1295		if (comma_ptr)
1296			*comma_ptr = '\0';
1297
1298		if (strlen(value) > VALUE_MAXLEN) {
1299			pr_err("Value for key \"%s\" exceeds %d,"
1300				" protocol error.\n", param->name,
1301				VALUE_MAXLEN);
1302			return -1;
1303		}
1304
1305		if (IS_TYPE_BOOL_AND(param) || IS_TYPE_BOOL_OR(param)) {
1306			if (iscsi_check_boolean_value(param, value) < 0)
1307				return -1;
1308		} else if (IS_TYPE_NUMBER(param)) {
1309			if (iscsi_check_numerical_value(param, value) < 0)
1310				return -1;
1311		} else if (IS_TYPE_NUMBER_RANGE(param)) {
1312			if (iscsi_check_numerical_range_value(param, value) < 0)
1313				return -1;
1314		} else if (IS_TYPE_STRING(param) || IS_TYPE_VALUE_LIST(param)) {
1315			if (iscsi_check_string_or_list_value(param, value) < 0)
1316				return -1;
1317		} else {
1318			pr_err("Huh? 0x%02x\n", param->type);
1319			return -1;
1320		}
1321
1322		if (comma_ptr)
1323			*comma_ptr++ = ',';
1324
1325		value = comma_ptr;
1326	} while (value);
1327
1328	return 0;
1329}
1330
1331static struct iscsi_param *__iscsi_check_key(
1332	char *key,
1333	int sender,
1334	struct iscsi_param_list *param_list)
1335{
1336	struct iscsi_param *param;
1337
1338	if (strlen(key) > KEY_MAXLEN) {
1339		pr_err("Length of key name \"%s\" exceeds %d.\n",
1340			key, KEY_MAXLEN);
1341		return NULL;
1342	}
1343
1344	param = iscsi_find_param_from_key(key, param_list);
1345	if (!param)
1346		return NULL;
1347
1348	if ((sender & SENDER_INITIATOR) && !IS_SENDER_INITIATOR(param)) {
1349		pr_err("Key \"%s\" may not be sent to %s,"
1350			" protocol error.\n", param->name,
1351			(sender & SENDER_RECEIVER) ? "target" : "initiator");
1352		return NULL;
1353	}
1354
1355	if ((sender & SENDER_TARGET) && !IS_SENDER_TARGET(param)) {
1356		pr_err("Key \"%s\" may not be sent to %s,"
1357			" protocol error.\n", param->name,
1358			(sender & SENDER_RECEIVER) ? "initiator" : "target");
1359		return NULL;
1360	}
1361
1362	return param;
1363}
1364
1365static struct iscsi_param *iscsi_check_key(
1366	char *key,
1367	int phase,
1368	int sender,
1369	struct iscsi_param_list *param_list)
1370{
1371	struct iscsi_param *param;
1372	/*
1373	 * Key name length must not exceed 63 bytes. (See iSCSI v20 5.1)
1374	 */
1375	if (strlen(key) > KEY_MAXLEN) {
1376		pr_err("Length of key name \"%s\" exceeds %d.\n",
1377			key, KEY_MAXLEN);
1378		return NULL;
1379	}
1380
1381	param = iscsi_find_param_from_key(key, param_list);
1382	if (!param)
1383		return NULL;
1384
1385	if ((sender & SENDER_INITIATOR) && !IS_SENDER_INITIATOR(param)) {
1386		pr_err("Key \"%s\" may not be sent to %s,"
1387			" protocol error.\n", param->name,
1388			(sender & SENDER_RECEIVER) ? "target" : "initiator");
1389		return NULL;
1390	}
1391	if ((sender & SENDER_TARGET) && !IS_SENDER_TARGET(param)) {
1392		pr_err("Key \"%s\" may not be sent to %s,"
1393				" protocol error.\n", param->name,
1394			(sender & SENDER_RECEIVER) ? "initiator" : "target");
1395		return NULL;
1396	}
1397
1398	if (IS_PSTATE_ACCEPTOR(param)) {
1399		pr_err("Key \"%s\" received twice, protocol error.\n",
1400				key);
1401		return NULL;
1402	}
1403
1404	if (!phase)
1405		return param;
1406
1407	if (!(param->phase & phase)) {
1408		pr_err("Key \"%s\" may not be negotiated during ",
1409				param->name);
1410		switch (phase) {
1411		case PHASE_SECURITY:
1412			pr_debug("Security phase.\n");
1413			break;
1414		case PHASE_OPERATIONAL:
1415			pr_debug("Operational phase.\n");
 
1416		default:
1417			pr_debug("Unknown phase.\n");
1418		}
1419		return NULL;
1420	}
1421
1422	return param;
1423}
1424
1425static int iscsi_enforce_integrity_rules(
1426	u8 phase,
1427	struct iscsi_param_list *param_list)
1428{
1429	char *tmpptr;
1430	u8 DataSequenceInOrder = 0;
1431	u8 ErrorRecoveryLevel = 0, SessionType = 0;
1432	u8 IFMarker = 0, OFMarker = 0;
1433	u8 IFMarkInt_Reject = 1, OFMarkInt_Reject = 1;
1434	u32 FirstBurstLength = 0, MaxBurstLength = 0;
1435	struct iscsi_param *param = NULL;
1436
1437	list_for_each_entry(param, &param_list->param_list, p_list) {
1438		if (!(param->phase & phase))
1439			continue;
1440		if (!strcmp(param->name, SESSIONTYPE))
1441			if (!strcmp(param->value, NORMAL))
1442				SessionType = 1;
1443		if (!strcmp(param->name, ERRORRECOVERYLEVEL))
1444			ErrorRecoveryLevel = simple_strtoul(param->value,
1445					&tmpptr, 0);
1446		if (!strcmp(param->name, DATASEQUENCEINORDER))
1447			if (!strcmp(param->value, YES))
1448				DataSequenceInOrder = 1;
1449		if (!strcmp(param->name, MAXBURSTLENGTH))
1450			MaxBurstLength = simple_strtoul(param->value,
1451					&tmpptr, 0);
1452		if (!strcmp(param->name, IFMARKER))
1453			if (!strcmp(param->value, YES))
1454				IFMarker = 1;
1455		if (!strcmp(param->name, OFMARKER))
1456			if (!strcmp(param->value, YES))
1457				OFMarker = 1;
1458		if (!strcmp(param->name, IFMARKINT))
1459			if (!strcmp(param->value, REJECT))
1460				IFMarkInt_Reject = 1;
1461		if (!strcmp(param->name, OFMARKINT))
1462			if (!strcmp(param->value, REJECT))
1463				OFMarkInt_Reject = 1;
1464	}
1465
1466	list_for_each_entry(param, &param_list->param_list, p_list) {
1467		if (!(param->phase & phase))
1468			continue;
1469		if (!SessionType && (!IS_PSTATE_ACCEPTOR(param) &&
1470		     (strcmp(param->name, IFMARKER) &&
1471		      strcmp(param->name, OFMARKER) &&
1472		      strcmp(param->name, IFMARKINT) &&
1473		      strcmp(param->name, OFMARKINT))))
1474			continue;
1475		if (!strcmp(param->name, MAXOUTSTANDINGR2T) &&
1476		    DataSequenceInOrder && (ErrorRecoveryLevel > 0)) {
1477			if (strcmp(param->value, "1")) {
1478				if (iscsi_update_param_value(param, "1") < 0)
1479					return -1;
1480				pr_debug("Reset \"%s\" to \"%s\".\n",
1481					param->name, param->value);
1482			}
1483		}
1484		if (!strcmp(param->name, MAXCONNECTIONS) && !SessionType) {
1485			if (strcmp(param->value, "1")) {
1486				if (iscsi_update_param_value(param, "1") < 0)
1487					return -1;
1488				pr_debug("Reset \"%s\" to \"%s\".\n",
1489					param->name, param->value);
1490			}
1491		}
1492		if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
1493			FirstBurstLength = simple_strtoul(param->value,
1494					&tmpptr, 0);
1495			if (FirstBurstLength > MaxBurstLength) {
1496				char tmpbuf[10];
1497				memset(tmpbuf, 0, 10);
1498				sprintf(tmpbuf, "%u", MaxBurstLength);
1499				if (iscsi_update_param_value(param, tmpbuf))
1500					return -1;
1501				pr_debug("Reset \"%s\" to \"%s\".\n",
1502					param->name, param->value);
1503			}
1504		}
1505		if (!strcmp(param->name, IFMARKER) && IFMarkInt_Reject) {
1506			if (iscsi_update_param_value(param, NO) < 0)
1507				return -1;
1508			IFMarker = 0;
1509			pr_debug("Reset \"%s\" to \"%s\".\n",
1510					param->name, param->value);
1511		}
1512		if (!strcmp(param->name, OFMARKER) && OFMarkInt_Reject) {
1513			if (iscsi_update_param_value(param, NO) < 0)
1514				return -1;
1515			OFMarker = 0;
1516			pr_debug("Reset \"%s\" to \"%s\".\n",
1517					 param->name, param->value);
1518		}
1519		if (!strcmp(param->name, IFMARKINT) && !IFMarker) {
1520			if (!strcmp(param->value, REJECT))
1521				continue;
1522			param->state &= ~PSTATE_NEGOTIATE;
1523			if (iscsi_update_param_value(param, IRRELEVANT) < 0)
1524				return -1;
1525			pr_debug("Reset \"%s\" to \"%s\".\n",
1526					param->name, param->value);
1527		}
1528		if (!strcmp(param->name, OFMARKINT) && !OFMarker) {
1529			if (!strcmp(param->value, REJECT))
1530				continue;
1531			param->state &= ~PSTATE_NEGOTIATE;
1532			if (iscsi_update_param_value(param, IRRELEVANT) < 0)
1533				return -1;
1534			pr_debug("Reset \"%s\" to \"%s\".\n",
1535					param->name, param->value);
1536		}
1537	}
1538
1539	return 0;
1540}
1541
1542int iscsi_decode_text_input(
1543	u8 phase,
1544	u8 sender,
1545	char *textbuf,
1546	u32 length,
1547	struct iscsi_param_list *param_list)
1548{
 
1549	char *tmpbuf, *start = NULL, *end = NULL;
1550
1551	tmpbuf = kzalloc(length + 1, GFP_KERNEL);
1552	if (!tmpbuf) {
1553		pr_err("Unable to allocate memory for tmpbuf.\n");
1554		return -1;
1555	}
1556
1557	memcpy(tmpbuf, textbuf, length);
1558	tmpbuf[length] = '\0';
1559	start = tmpbuf;
1560	end = (start + length);
1561
1562	while (start < end) {
1563		char *key, *value;
1564		struct iscsi_param *param;
1565
1566		if (iscsi_extract_key_value(start, &key, &value) < 0) {
1567			kfree(tmpbuf);
1568			return -1;
1569		}
1570
1571		pr_debug("Got key: %s=%s\n", key, value);
1572
1573		if (phase & PHASE_SECURITY) {
1574			if (iscsi_check_for_auth_key(key) > 0) {
1575				char *tmpptr = key + strlen(key);
1576				*tmpptr = '=';
1577				kfree(tmpbuf);
1578				return 1;
1579			}
1580		}
1581
1582		param = iscsi_check_key(key, phase, sender, param_list);
1583		if (!param) {
1584			if (iscsi_add_notunderstood_response(key,
1585					value, param_list) < 0) {
1586				kfree(tmpbuf);
1587				return -1;
1588			}
1589			start += strlen(key) + strlen(value) + 2;
1590			continue;
1591		}
1592		if (iscsi_check_value(param, value) < 0) {
1593			kfree(tmpbuf);
1594			return -1;
1595		}
1596
1597		start += strlen(key) + strlen(value) + 2;
1598
1599		if (IS_PSTATE_PROPOSER(param)) {
1600			if (iscsi_check_proposer_state(param, value) < 0) {
1601				kfree(tmpbuf);
1602				return -1;
1603			}
1604			SET_PSTATE_RESPONSE_GOT(param);
1605		} else {
1606			if (iscsi_check_acceptor_state(param, value) < 0) {
1607				kfree(tmpbuf);
1608				return -1;
1609			}
1610			SET_PSTATE_ACCEPTOR(param);
1611		}
1612	}
1613
1614	kfree(tmpbuf);
1615	return 0;
1616}
1617
1618int iscsi_encode_text_output(
1619	u8 phase,
1620	u8 sender,
1621	char *textbuf,
1622	u32 *length,
1623	struct iscsi_param_list *param_list)
1624{
1625	char *output_buf = NULL;
1626	struct iscsi_extra_response *er;
1627	struct iscsi_param *param;
1628
1629	output_buf = textbuf + *length;
1630
1631	if (iscsi_enforce_integrity_rules(phase, param_list) < 0)
1632		return -1;
1633
1634	list_for_each_entry(param, &param_list->param_list, p_list) {
1635		if (!(param->sender & sender))
1636			continue;
1637		if (IS_PSTATE_ACCEPTOR(param) &&
1638		    !IS_PSTATE_RESPONSE_SENT(param) &&
1639		    !IS_PSTATE_REPLY_OPTIONAL(param) &&
1640		    (param->phase & phase)) {
1641			*length += sprintf(output_buf, "%s=%s",
1642				param->name, param->value);
1643			*length += 1;
1644			output_buf = textbuf + *length;
1645			SET_PSTATE_RESPONSE_SENT(param);
1646			pr_debug("Sending key: %s=%s\n",
1647				param->name, param->value);
1648			continue;
1649		}
1650		if (IS_PSTATE_NEGOTIATE(param) &&
1651		    !IS_PSTATE_ACCEPTOR(param) &&
1652		    !IS_PSTATE_PROPOSER(param) &&
1653		    (param->phase & phase)) {
1654			*length += sprintf(output_buf, "%s=%s",
1655				param->name, param->value);
1656			*length += 1;
1657			output_buf = textbuf + *length;
1658			SET_PSTATE_PROPOSER(param);
1659			iscsi_check_proposer_for_optional_reply(param);
1660			pr_debug("Sending key: %s=%s\n",
1661				param->name, param->value);
1662		}
1663	}
1664
1665	list_for_each_entry(er, &param_list->extra_response_list, er_list) {
1666		*length += sprintf(output_buf, "%s=%s", er->key, er->value);
1667		*length += 1;
1668		output_buf = textbuf + *length;
1669		pr_debug("Sending key: %s=%s\n", er->key, er->value);
1670	}
1671	iscsi_release_extra_responses(param_list);
1672
1673	return 0;
1674}
1675
1676int iscsi_check_negotiated_keys(struct iscsi_param_list *param_list)
1677{
1678	int ret = 0;
1679	struct iscsi_param *param;
1680
1681	list_for_each_entry(param, &param_list->param_list, p_list) {
1682		if (IS_PSTATE_NEGOTIATE(param) &&
1683		    IS_PSTATE_PROPOSER(param) &&
1684		    !IS_PSTATE_RESPONSE_GOT(param) &&
1685		    !IS_PSTATE_REPLY_OPTIONAL(param) &&
1686		    !IS_PHASE_DECLARATIVE(param)) {
1687			pr_err("No response for proposed key \"%s\".\n",
1688					param->name);
1689			ret = -1;
1690		}
1691	}
1692
1693	return ret;
1694}
1695
1696int iscsi_change_param_value(
1697	char *keyvalue,
1698	struct iscsi_param_list *param_list,
1699	int check_key)
1700{
1701	char *key = NULL, *value = NULL;
1702	struct iscsi_param *param;
1703	int sender = 0;
1704
1705	if (iscsi_extract_key_value(keyvalue, &key, &value) < 0)
1706		return -1;
1707
1708	if (!check_key) {
1709		param = __iscsi_check_key(keyvalue, sender, param_list);
1710		if (!param)
1711			return -1;
1712	} else {
1713		param = iscsi_check_key(keyvalue, 0, sender, param_list);
1714		if (!param)
1715			return -1;
1716
1717		param->set_param = 1;
1718		if (iscsi_check_value(param, value) < 0) {
1719			param->set_param = 0;
1720			return -1;
1721		}
1722		param->set_param = 0;
1723	}
1724
1725	if (iscsi_update_param_value(param, value) < 0)
1726		return -1;
1727
1728	return 0;
1729}
1730
1731void iscsi_set_connection_parameters(
1732	struct iscsi_conn_ops *ops,
1733	struct iscsi_param_list *param_list)
1734{
1735	char *tmpptr;
1736	struct iscsi_param *param;
1737
1738	pr_debug("---------------------------------------------------"
1739			"---------------\n");
1740	list_for_each_entry(param, &param_list->param_list, p_list) {
 
 
 
 
 
 
 
 
 
 
 
 
1741		if (!IS_PSTATE_ACCEPTOR(param) && !IS_PSTATE_PROPOSER(param))
1742			continue;
1743		if (!strcmp(param->name, AUTHMETHOD)) {
1744			pr_debug("AuthMethod:                   %s\n",
1745				param->value);
1746		} else if (!strcmp(param->name, HEADERDIGEST)) {
1747			ops->HeaderDigest = !strcmp(param->value, CRC32C);
1748			pr_debug("HeaderDigest:                 %s\n",
1749				param->value);
1750		} else if (!strcmp(param->name, DATADIGEST)) {
1751			ops->DataDigest = !strcmp(param->value, CRC32C);
1752			pr_debug("DataDigest:                   %s\n",
1753				param->value);
1754		} else if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
1755			ops->MaxRecvDataSegmentLength =
1756				simple_strtoul(param->value, &tmpptr, 0);
1757			pr_debug("MaxRecvDataSegmentLength:     %s\n",
1758				param->value);
 
 
 
1759		} else if (!strcmp(param->name, OFMARKER)) {
1760			ops->OFMarker = !strcmp(param->value, YES);
1761			pr_debug("OFMarker:                     %s\n",
1762				param->value);
1763		} else if (!strcmp(param->name, IFMARKER)) {
1764			ops->IFMarker = !strcmp(param->value, YES);
1765			pr_debug("IFMarker:                     %s\n",
1766				param->value);
1767		} else if (!strcmp(param->name, OFMARKINT)) {
1768			ops->OFMarkInt =
1769				simple_strtoul(param->value, &tmpptr, 0);
1770			pr_debug("OFMarkInt:                    %s\n",
1771				param->value);
1772		} else if (!strcmp(param->name, IFMARKINT)) {
1773			ops->IFMarkInt =
1774				simple_strtoul(param->value, &tmpptr, 0);
1775			pr_debug("IFMarkInt:                    %s\n",
1776				param->value);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1777		}
1778	}
1779	pr_debug("----------------------------------------------------"
1780			"--------------\n");
1781}
1782
1783void iscsi_set_session_parameters(
1784	struct iscsi_sess_ops *ops,
1785	struct iscsi_param_list *param_list,
1786	int leading)
1787{
1788	char *tmpptr;
1789	struct iscsi_param *param;
1790
1791	pr_debug("----------------------------------------------------"
1792			"--------------\n");
1793	list_for_each_entry(param, &param_list->param_list, p_list) {
1794		if (!IS_PSTATE_ACCEPTOR(param) && !IS_PSTATE_PROPOSER(param))
1795			continue;
1796		if (!strcmp(param->name, INITIATORNAME)) {
1797			if (!param->value)
1798				continue;
1799			if (leading)
1800				snprintf(ops->InitiatorName,
1801						sizeof(ops->InitiatorName),
1802						"%s", param->value);
1803			pr_debug("InitiatorName:                %s\n",
1804				param->value);
1805		} else if (!strcmp(param->name, INITIATORALIAS)) {
1806			if (!param->value)
1807				continue;
1808			snprintf(ops->InitiatorAlias,
1809						sizeof(ops->InitiatorAlias),
1810						"%s", param->value);
1811			pr_debug("InitiatorAlias:               %s\n",
1812				param->value);
1813		} else if (!strcmp(param->name, TARGETNAME)) {
1814			if (!param->value)
1815				continue;
1816			if (leading)
1817				snprintf(ops->TargetName,
1818						sizeof(ops->TargetName),
1819						"%s", param->value);
1820			pr_debug("TargetName:                   %s\n",
1821				param->value);
1822		} else if (!strcmp(param->name, TARGETALIAS)) {
1823			if (!param->value)
1824				continue;
1825			snprintf(ops->TargetAlias, sizeof(ops->TargetAlias),
1826					"%s", param->value);
1827			pr_debug("TargetAlias:                  %s\n",
1828				param->value);
1829		} else if (!strcmp(param->name, TARGETPORTALGROUPTAG)) {
1830			ops->TargetPortalGroupTag =
1831				simple_strtoul(param->value, &tmpptr, 0);
1832			pr_debug("TargetPortalGroupTag:         %s\n",
1833				param->value);
1834		} else if (!strcmp(param->name, MAXCONNECTIONS)) {
1835			ops->MaxConnections =
1836				simple_strtoul(param->value, &tmpptr, 0);
1837			pr_debug("MaxConnections:               %s\n",
1838				param->value);
1839		} else if (!strcmp(param->name, INITIALR2T)) {
1840			ops->InitialR2T = !strcmp(param->value, YES);
1841			 pr_debug("InitialR2T:                   %s\n",
1842				param->value);
1843		} else if (!strcmp(param->name, IMMEDIATEDATA)) {
1844			ops->ImmediateData = !strcmp(param->value, YES);
1845			pr_debug("ImmediateData:                %s\n",
1846				param->value);
1847		} else if (!strcmp(param->name, MAXBURSTLENGTH)) {
1848			ops->MaxBurstLength =
1849				simple_strtoul(param->value, &tmpptr, 0);
1850			pr_debug("MaxBurstLength:               %s\n",
1851				param->value);
1852		} else if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
1853			ops->FirstBurstLength =
1854				simple_strtoul(param->value, &tmpptr, 0);
1855			pr_debug("FirstBurstLength:             %s\n",
1856				param->value);
1857		} else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
1858			ops->DefaultTime2Wait =
1859				simple_strtoul(param->value, &tmpptr, 0);
1860			pr_debug("DefaultTime2Wait:             %s\n",
1861				param->value);
1862		} else if (!strcmp(param->name, DEFAULTTIME2RETAIN)) {
1863			ops->DefaultTime2Retain =
1864				simple_strtoul(param->value, &tmpptr, 0);
1865			pr_debug("DefaultTime2Retain:           %s\n",
1866				param->value);
1867		} else if (!strcmp(param->name, MAXOUTSTANDINGR2T)) {
1868			ops->MaxOutstandingR2T =
1869				simple_strtoul(param->value, &tmpptr, 0);
1870			pr_debug("MaxOutstandingR2T:            %s\n",
1871				param->value);
1872		} else if (!strcmp(param->name, DATAPDUINORDER)) {
1873			ops->DataPDUInOrder = !strcmp(param->value, YES);
1874			pr_debug("DataPDUInOrder:               %s\n",
1875				param->value);
1876		} else if (!strcmp(param->name, DATASEQUENCEINORDER)) {
1877			ops->DataSequenceInOrder = !strcmp(param->value, YES);
1878			pr_debug("DataSequenceInOrder:          %s\n",
1879				param->value);
1880		} else if (!strcmp(param->name, ERRORRECOVERYLEVEL)) {
1881			ops->ErrorRecoveryLevel =
1882				simple_strtoul(param->value, &tmpptr, 0);
1883			pr_debug("ErrorRecoveryLevel:           %s\n",
1884				param->value);
1885		} else if (!strcmp(param->name, SESSIONTYPE)) {
1886			ops->SessionType = !strcmp(param->value, DISCOVERY);
1887			pr_debug("SessionType:                  %s\n",
 
 
 
 
1888				param->value);
1889		}
1890	}
1891	pr_debug("----------------------------------------------------"
1892			"--------------\n");
1893
1894}
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/slab.h>
  20
  21#include "iscsi_target_core.h"
  22#include "iscsi_target_util.h"
  23#include "iscsi_target_parameters.h"
  24
  25int iscsi_login_rx_data(
  26	struct iscsi_conn *conn,
  27	char *buf,
  28	int length)
  29{
  30	int rx_got;
  31	struct kvec iov;
  32
  33	memset(&iov, 0, sizeof(struct kvec));
  34	iov.iov_len	= length;
  35	iov.iov_base	= buf;
  36
  37	/*
  38	 * Initial Marker-less Interval.
  39	 * Add the values regardless of IFMarker/OFMarker, considering
  40	 * it may not be negoitated yet.
  41	 */
  42	conn->of_marker += length;
  43
  44	rx_got = rx_data(conn, &iov, 1, length);
  45	if (rx_got != length) {
  46		pr_err("rx_data returned %d, expecting %d.\n",
  47				rx_got, length);
  48		return -1;
  49	}
  50
  51	return 0 ;
  52}
  53
  54int iscsi_login_tx_data(
  55	struct iscsi_conn *conn,
  56	char *pdu_buf,
  57	char *text_buf,
  58	int text_length)
  59{
  60	int length, tx_sent, iov_cnt = 1;
  61	struct kvec iov[2];
  62
  63	length = (ISCSI_HDR_LEN + text_length);
  64
  65	memset(&iov[0], 0, 2 * sizeof(struct kvec));
  66	iov[0].iov_len		= ISCSI_HDR_LEN;
  67	iov[0].iov_base		= pdu_buf;
  68
  69	if (text_buf && text_length) {
  70		iov[1].iov_len	= text_length;
  71		iov[1].iov_base	= text_buf;
  72		iov_cnt++;
  73	}
  74
  75	/*
  76	 * Initial Marker-less Interval.
  77	 * Add the values regardless of IFMarker/OFMarker, considering
  78	 * it may not be negoitated yet.
  79	 */
  80	conn->if_marker += length;
  81
  82	tx_sent = tx_data(conn, &iov[0], iov_cnt, length);
  83	if (tx_sent != length) {
  84		pr_err("tx_data returned %d, expecting %d.\n",
  85				tx_sent, length);
  86		return -1;
  87	}
  88
  89	return 0;
  90}
  91
  92void iscsi_dump_conn_ops(struct iscsi_conn_ops *conn_ops)
  93{
  94	pr_debug("HeaderDigest: %s\n", (conn_ops->HeaderDigest) ?
  95				"CRC32C" : "None");
  96	pr_debug("DataDigest: %s\n", (conn_ops->DataDigest) ?
  97				"CRC32C" : "None");
  98	pr_debug("MaxRecvDataSegmentLength: %u\n",
  99				conn_ops->MaxRecvDataSegmentLength);
 100	pr_debug("OFMarker: %s\n", (conn_ops->OFMarker) ? "Yes" : "No");
 101	pr_debug("IFMarker: %s\n", (conn_ops->IFMarker) ? "Yes" : "No");
 102	if (conn_ops->OFMarker)
 103		pr_debug("OFMarkInt: %u\n", conn_ops->OFMarkInt);
 104	if (conn_ops->IFMarker)
 105		pr_debug("IFMarkInt: %u\n", conn_ops->IFMarkInt);
 106}
 107
 108void iscsi_dump_sess_ops(struct iscsi_sess_ops *sess_ops)
 109{
 110	pr_debug("InitiatorName: %s\n", sess_ops->InitiatorName);
 111	pr_debug("InitiatorAlias: %s\n", sess_ops->InitiatorAlias);
 112	pr_debug("TargetName: %s\n", sess_ops->TargetName);
 113	pr_debug("TargetAlias: %s\n", sess_ops->TargetAlias);
 114	pr_debug("TargetPortalGroupTag: %hu\n",
 115			sess_ops->TargetPortalGroupTag);
 116	pr_debug("MaxConnections: %hu\n", sess_ops->MaxConnections);
 117	pr_debug("InitialR2T: %s\n",
 118			(sess_ops->InitialR2T) ? "Yes" : "No");
 119	pr_debug("ImmediateData: %s\n", (sess_ops->ImmediateData) ?
 120			"Yes" : "No");
 121	pr_debug("MaxBurstLength: %u\n", sess_ops->MaxBurstLength);
 122	pr_debug("FirstBurstLength: %u\n", sess_ops->FirstBurstLength);
 123	pr_debug("DefaultTime2Wait: %hu\n", sess_ops->DefaultTime2Wait);
 124	pr_debug("DefaultTime2Retain: %hu\n",
 125			sess_ops->DefaultTime2Retain);
 126	pr_debug("MaxOutstandingR2T: %hu\n",
 127			sess_ops->MaxOutstandingR2T);
 128	pr_debug("DataPDUInOrder: %s\n",
 129			(sess_ops->DataPDUInOrder) ? "Yes" : "No");
 130	pr_debug("DataSequenceInOrder: %s\n",
 131			(sess_ops->DataSequenceInOrder) ? "Yes" : "No");
 132	pr_debug("ErrorRecoveryLevel: %hu\n",
 133			sess_ops->ErrorRecoveryLevel);
 134	pr_debug("SessionType: %s\n", (sess_ops->SessionType) ?
 135			"Discovery" : "Normal");
 136}
 137
 138void iscsi_print_params(struct iscsi_param_list *param_list)
 139{
 140	struct iscsi_param *param;
 141
 142	list_for_each_entry(param, &param_list->param_list, p_list)
 143		pr_debug("%s: %s\n", param->name, param->value);
 144}
 145
 146static struct iscsi_param *iscsi_set_default_param(struct iscsi_param_list *param_list,
 147		char *name, char *value, u8 phase, u8 scope, u8 sender,
 148		u16 type_range, u8 use)
 149{
 150	struct iscsi_param *param = NULL;
 151
 152	param = kzalloc(sizeof(struct iscsi_param), GFP_KERNEL);
 153	if (!param) {
 154		pr_err("Unable to allocate memory for parameter.\n");
 155		goto out;
 156	}
 157	INIT_LIST_HEAD(&param->p_list);
 158
 159	param->name = kstrdup(name, GFP_KERNEL);
 160	if (!param->name) {
 161		pr_err("Unable to allocate memory for parameter name.\n");
 162		goto out;
 163	}
 164
 165	param->value = kstrdup(value, GFP_KERNEL);
 166	if (!param->value) {
 167		pr_err("Unable to allocate memory for parameter value.\n");
 168		goto out;
 169	}
 170
 
 
 
 
 171	param->phase		= phase;
 172	param->scope		= scope;
 173	param->sender		= sender;
 174	param->use		= use;
 175	param->type_range	= type_range;
 176
 177	switch (param->type_range) {
 178	case TYPERANGE_BOOL_AND:
 179		param->type = TYPE_BOOL_AND;
 180		break;
 181	case TYPERANGE_BOOL_OR:
 182		param->type = TYPE_BOOL_OR;
 183		break;
 184	case TYPERANGE_0_TO_2:
 185	case TYPERANGE_0_TO_3600:
 186	case TYPERANGE_0_TO_32767:
 187	case TYPERANGE_0_TO_65535:
 188	case TYPERANGE_1_TO_65535:
 189	case TYPERANGE_2_TO_3600:
 190	case TYPERANGE_512_TO_16777215:
 191		param->type = TYPE_NUMBER;
 192		break;
 193	case TYPERANGE_AUTH:
 194	case TYPERANGE_DIGEST:
 195		param->type = TYPE_VALUE_LIST | TYPE_STRING;
 196		break;
 197	case TYPERANGE_MARKINT:
 198		param->type = TYPE_NUMBER_RANGE;
 199		param->type_range |= TYPERANGE_1_TO_65535;
 200		break;
 201	case TYPERANGE_ISCSINAME:
 202	case TYPERANGE_SESSIONTYPE:
 203	case TYPERANGE_TARGETADDRESS:
 204	case TYPERANGE_UTF8:
 205		param->type = TYPE_STRING;
 206		break;
 207	default:
 208		pr_err("Unknown type_range 0x%02x\n",
 209				param->type_range);
 210		goto out;
 211	}
 212	list_add_tail(&param->p_list, &param_list->param_list);
 213
 214	return param;
 215out:
 216	if (param) {
 217		kfree(param->value);
 218		kfree(param->name);
 219		kfree(param);
 220	}
 221
 222	return NULL;
 223}
 224
 225/* #warning Add extension keys */
 226int iscsi_create_default_params(struct iscsi_param_list **param_list_ptr)
 227{
 228	struct iscsi_param *param = NULL;
 229	struct iscsi_param_list *pl;
 230
 231	pl = kzalloc(sizeof(struct iscsi_param_list), GFP_KERNEL);
 232	if (!pl) {
 233		pr_err("Unable to allocate memory for"
 234				" struct iscsi_param_list.\n");
 235		return -1 ;
 236	}
 237	INIT_LIST_HEAD(&pl->param_list);
 238	INIT_LIST_HEAD(&pl->extra_response_list);
 239
 240	/*
 241	 * The format for setting the initial parameter definitions are:
 242	 *
 243	 * Parameter name:
 244	 * Initial value:
 245	 * Allowable phase:
 246	 * Scope:
 247	 * Allowable senders:
 248	 * Typerange:
 249	 * Use:
 250	 */
 251	param = iscsi_set_default_param(pl, AUTHMETHOD, INITIAL_AUTHMETHOD,
 252			PHASE_SECURITY, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 253			TYPERANGE_AUTH, USE_INITIAL_ONLY);
 254	if (!param)
 255		goto out;
 256
 257	param = iscsi_set_default_param(pl, HEADERDIGEST, INITIAL_HEADERDIGEST,
 258			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 259			TYPERANGE_DIGEST, USE_INITIAL_ONLY);
 260	if (!param)
 261		goto out;
 262
 263	param = iscsi_set_default_param(pl, DATADIGEST, INITIAL_DATADIGEST,
 264			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 265			TYPERANGE_DIGEST, USE_INITIAL_ONLY);
 266	if (!param)
 267		goto out;
 268
 269	param = iscsi_set_default_param(pl, MAXCONNECTIONS,
 270			INITIAL_MAXCONNECTIONS, PHASE_OPERATIONAL,
 271			SCOPE_SESSION_WIDE, SENDER_BOTH,
 272			TYPERANGE_1_TO_65535, USE_LEADING_ONLY);
 273	if (!param)
 274		goto out;
 275
 276	param = iscsi_set_default_param(pl, SENDTARGETS, INITIAL_SENDTARGETS,
 277			PHASE_FFP0, SCOPE_SESSION_WIDE, SENDER_INITIATOR,
 278			TYPERANGE_UTF8, 0);
 279	if (!param)
 280		goto out;
 281
 282	param = iscsi_set_default_param(pl, TARGETNAME, INITIAL_TARGETNAME,
 283			PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_BOTH,
 284			TYPERANGE_ISCSINAME, USE_ALL);
 285	if (!param)
 286		goto out;
 287
 288	param = iscsi_set_default_param(pl, INITIATORNAME,
 289			INITIAL_INITIATORNAME, PHASE_DECLARATIVE,
 290			SCOPE_SESSION_WIDE, SENDER_INITIATOR,
 291			TYPERANGE_ISCSINAME, USE_INITIAL_ONLY);
 292	if (!param)
 293		goto out;
 294
 295	param = iscsi_set_default_param(pl, TARGETALIAS, INITIAL_TARGETALIAS,
 296			PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_TARGET,
 297			TYPERANGE_UTF8, USE_ALL);
 298	if (!param)
 299		goto out;
 300
 301	param = iscsi_set_default_param(pl, INITIATORALIAS,
 302			INITIAL_INITIATORALIAS, PHASE_DECLARATIVE,
 303			SCOPE_SESSION_WIDE, SENDER_INITIATOR, TYPERANGE_UTF8,
 304			USE_ALL);
 305	if (!param)
 306		goto out;
 307
 308	param = iscsi_set_default_param(pl, TARGETADDRESS,
 309			INITIAL_TARGETADDRESS, PHASE_DECLARATIVE,
 310			SCOPE_SESSION_WIDE, SENDER_TARGET,
 311			TYPERANGE_TARGETADDRESS, USE_ALL);
 312	if (!param)
 313		goto out;
 314
 315	param = iscsi_set_default_param(pl, TARGETPORTALGROUPTAG,
 316			INITIAL_TARGETPORTALGROUPTAG,
 317			PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_TARGET,
 318			TYPERANGE_0_TO_65535, USE_INITIAL_ONLY);
 319	if (!param)
 320		goto out;
 321
 322	param = iscsi_set_default_param(pl, INITIALR2T, INITIAL_INITIALR2T,
 323			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 324			TYPERANGE_BOOL_OR, USE_LEADING_ONLY);
 325	if (!param)
 326		goto out;
 327
 328	param = iscsi_set_default_param(pl, IMMEDIATEDATA,
 329			INITIAL_IMMEDIATEDATA, PHASE_OPERATIONAL,
 330			SCOPE_SESSION_WIDE, SENDER_BOTH, TYPERANGE_BOOL_AND,
 331			USE_LEADING_ONLY);
 332	if (!param)
 333		goto out;
 334
 335	param = iscsi_set_default_param(pl, MAXXMITDATASEGMENTLENGTH,
 336			INITIAL_MAXXMITDATASEGMENTLENGTH,
 337			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 338			TYPERANGE_512_TO_16777215, USE_ALL);
 339	if (!param)
 340		goto out;
 341
 342	param = iscsi_set_default_param(pl, MAXRECVDATASEGMENTLENGTH,
 343			INITIAL_MAXRECVDATASEGMENTLENGTH,
 344			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 345			TYPERANGE_512_TO_16777215, USE_ALL);
 346	if (!param)
 347		goto out;
 348
 349	param = iscsi_set_default_param(pl, MAXBURSTLENGTH,
 350			INITIAL_MAXBURSTLENGTH, PHASE_OPERATIONAL,
 351			SCOPE_SESSION_WIDE, SENDER_BOTH,
 352			TYPERANGE_512_TO_16777215, USE_LEADING_ONLY);
 353	if (!param)
 354		goto out;
 355
 356	param = iscsi_set_default_param(pl, FIRSTBURSTLENGTH,
 357			INITIAL_FIRSTBURSTLENGTH,
 358			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 359			TYPERANGE_512_TO_16777215, USE_LEADING_ONLY);
 360	if (!param)
 361		goto out;
 362
 363	param = iscsi_set_default_param(pl, DEFAULTTIME2WAIT,
 364			INITIAL_DEFAULTTIME2WAIT,
 365			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 366			TYPERANGE_0_TO_3600, USE_LEADING_ONLY);
 367	if (!param)
 368		goto out;
 369
 370	param = iscsi_set_default_param(pl, DEFAULTTIME2RETAIN,
 371			INITIAL_DEFAULTTIME2RETAIN,
 372			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 373			TYPERANGE_0_TO_3600, USE_LEADING_ONLY);
 374	if (!param)
 375		goto out;
 376
 377	param = iscsi_set_default_param(pl, MAXOUTSTANDINGR2T,
 378			INITIAL_MAXOUTSTANDINGR2T,
 379			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 380			TYPERANGE_1_TO_65535, USE_LEADING_ONLY);
 381	if (!param)
 382		goto out;
 383
 384	param = iscsi_set_default_param(pl, DATAPDUINORDER,
 385			INITIAL_DATAPDUINORDER, PHASE_OPERATIONAL,
 386			SCOPE_SESSION_WIDE, SENDER_BOTH, TYPERANGE_BOOL_OR,
 387			USE_LEADING_ONLY);
 388	if (!param)
 389		goto out;
 390
 391	param = iscsi_set_default_param(pl, DATASEQUENCEINORDER,
 392			INITIAL_DATASEQUENCEINORDER,
 393			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 394			TYPERANGE_BOOL_OR, USE_LEADING_ONLY);
 395	if (!param)
 396		goto out;
 397
 398	param = iscsi_set_default_param(pl, ERRORRECOVERYLEVEL,
 399			INITIAL_ERRORRECOVERYLEVEL,
 400			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 401			TYPERANGE_0_TO_2, USE_LEADING_ONLY);
 402	if (!param)
 403		goto out;
 404
 405	param = iscsi_set_default_param(pl, SESSIONTYPE, INITIAL_SESSIONTYPE,
 406			PHASE_DECLARATIVE, SCOPE_SESSION_WIDE, SENDER_INITIATOR,
 407			TYPERANGE_SESSIONTYPE, USE_LEADING_ONLY);
 408	if (!param)
 409		goto out;
 410
 411	param = iscsi_set_default_param(pl, IFMARKER, INITIAL_IFMARKER,
 412			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 413			TYPERANGE_BOOL_AND, USE_INITIAL_ONLY);
 414	if (!param)
 415		goto out;
 416
 417	param = iscsi_set_default_param(pl, OFMARKER, INITIAL_OFMARKER,
 418			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 419			TYPERANGE_BOOL_AND, USE_INITIAL_ONLY);
 420	if (!param)
 421		goto out;
 422
 423	param = iscsi_set_default_param(pl, IFMARKINT, INITIAL_IFMARKINT,
 424			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 425			TYPERANGE_MARKINT, USE_INITIAL_ONLY);
 426	if (!param)
 427		goto out;
 428
 429	param = iscsi_set_default_param(pl, OFMARKINT, INITIAL_OFMARKINT,
 430			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 431			TYPERANGE_MARKINT, USE_INITIAL_ONLY);
 432	if (!param)
 433		goto out;
 434	/*
 435	 * Extra parameters for ISER from RFC-5046
 436	 */
 437	param = iscsi_set_default_param(pl, RDMAEXTENSIONS, INITIAL_RDMAEXTENSIONS,
 438			PHASE_OPERATIONAL, SCOPE_SESSION_WIDE, SENDER_BOTH,
 439			TYPERANGE_BOOL_AND, USE_LEADING_ONLY);
 440	if (!param)
 441		goto out;
 442
 443	param = iscsi_set_default_param(pl, INITIATORRECVDATASEGMENTLENGTH,
 444			INITIAL_INITIATORRECVDATASEGMENTLENGTH,
 445			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 446			TYPERANGE_512_TO_16777215, USE_ALL);
 447	if (!param)
 448		goto out;
 449
 450	param = iscsi_set_default_param(pl, TARGETRECVDATASEGMENTLENGTH,
 451			INITIAL_TARGETRECVDATASEGMENTLENGTH,
 452			PHASE_OPERATIONAL, SCOPE_CONNECTION_ONLY, SENDER_BOTH,
 453			TYPERANGE_512_TO_16777215, USE_ALL);
 454	if (!param)
 455		goto out;
 456
 457	*param_list_ptr = pl;
 458	return 0;
 459out:
 460	iscsi_release_param_list(pl);
 461	return -1;
 462}
 463
 464int iscsi_set_keys_to_negotiate(
 465	struct iscsi_param_list *param_list,
 466	bool iser)
 467{
 468	struct iscsi_param *param;
 469
 470	param_list->iser = iser;
 471
 472	list_for_each_entry(param, &param_list->param_list, p_list) {
 473		param->state = 0;
 474		if (!strcmp(param->name, AUTHMETHOD)) {
 475			SET_PSTATE_NEGOTIATE(param);
 476		} else if (!strcmp(param->name, HEADERDIGEST)) {
 477			if (iser == false)
 478				SET_PSTATE_NEGOTIATE(param);
 479		} else if (!strcmp(param->name, DATADIGEST)) {
 480			if (iser == false)
 481				SET_PSTATE_NEGOTIATE(param);
 482		} else if (!strcmp(param->name, MAXCONNECTIONS)) {
 483			SET_PSTATE_NEGOTIATE(param);
 484		} else if (!strcmp(param->name, TARGETNAME)) {
 485			continue;
 486		} else if (!strcmp(param->name, INITIATORNAME)) {
 487			continue;
 488		} else if (!strcmp(param->name, TARGETALIAS)) {
 489			if (param->value)
 490				SET_PSTATE_NEGOTIATE(param);
 491		} else if (!strcmp(param->name, INITIATORALIAS)) {
 492			continue;
 493		} else if (!strcmp(param->name, TARGETPORTALGROUPTAG)) {
 494			SET_PSTATE_NEGOTIATE(param);
 495		} else if (!strcmp(param->name, INITIALR2T)) {
 496			SET_PSTATE_NEGOTIATE(param);
 497		} else if (!strcmp(param->name, IMMEDIATEDATA)) {
 498			SET_PSTATE_NEGOTIATE(param);
 499		} else if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
 500			if (iser == false)
 501				SET_PSTATE_NEGOTIATE(param);
 502		} else if (!strcmp(param->name, MAXXMITDATASEGMENTLENGTH)) {
 503			continue;
 504		} else if (!strcmp(param->name, MAXBURSTLENGTH)) {
 505			SET_PSTATE_NEGOTIATE(param);
 506		} else if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
 507			SET_PSTATE_NEGOTIATE(param);
 508		} else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
 509			SET_PSTATE_NEGOTIATE(param);
 510		} else if (!strcmp(param->name, DEFAULTTIME2RETAIN)) {
 511			SET_PSTATE_NEGOTIATE(param);
 512		} else if (!strcmp(param->name, MAXOUTSTANDINGR2T)) {
 513			SET_PSTATE_NEGOTIATE(param);
 514		} else if (!strcmp(param->name, DATAPDUINORDER)) {
 515			SET_PSTATE_NEGOTIATE(param);
 516		} else if (!strcmp(param->name, DATASEQUENCEINORDER)) {
 517			SET_PSTATE_NEGOTIATE(param);
 518		} else if (!strcmp(param->name, ERRORRECOVERYLEVEL)) {
 519			SET_PSTATE_NEGOTIATE(param);
 520		} else if (!strcmp(param->name, SESSIONTYPE)) {
 521			SET_PSTATE_NEGOTIATE(param);
 522		} else if (!strcmp(param->name, IFMARKER)) {
 523			SET_PSTATE_NEGOTIATE(param);
 524		} else if (!strcmp(param->name, OFMARKER)) {
 525			SET_PSTATE_NEGOTIATE(param);
 526		} else if (!strcmp(param->name, IFMARKINT)) {
 527			SET_PSTATE_NEGOTIATE(param);
 528		} else if (!strcmp(param->name, OFMARKINT)) {
 529			SET_PSTATE_NEGOTIATE(param);
 530		} else if (!strcmp(param->name, RDMAEXTENSIONS)) {
 531			if (iser == true)
 532				SET_PSTATE_NEGOTIATE(param);
 533		} else if (!strcmp(param->name, INITIATORRECVDATASEGMENTLENGTH)) {
 534			if (iser == true)
 535				SET_PSTATE_NEGOTIATE(param);
 536		} else if (!strcmp(param->name, TARGETRECVDATASEGMENTLENGTH)) {
 537			if (iser == true)
 538				SET_PSTATE_NEGOTIATE(param);
 539		}
 540	}
 541
 542	return 0;
 543}
 544
 545int iscsi_set_keys_irrelevant_for_discovery(
 546	struct iscsi_param_list *param_list)
 547{
 548	struct iscsi_param *param;
 549
 550	list_for_each_entry(param, &param_list->param_list, p_list) {
 551		if (!strcmp(param->name, MAXCONNECTIONS))
 552			param->state &= ~PSTATE_NEGOTIATE;
 553		else if (!strcmp(param->name, INITIALR2T))
 554			param->state &= ~PSTATE_NEGOTIATE;
 555		else if (!strcmp(param->name, IMMEDIATEDATA))
 556			param->state &= ~PSTATE_NEGOTIATE;
 557		else if (!strcmp(param->name, MAXBURSTLENGTH))
 558			param->state &= ~PSTATE_NEGOTIATE;
 559		else if (!strcmp(param->name, FIRSTBURSTLENGTH))
 560			param->state &= ~PSTATE_NEGOTIATE;
 561		else if (!strcmp(param->name, MAXOUTSTANDINGR2T))
 562			param->state &= ~PSTATE_NEGOTIATE;
 563		else if (!strcmp(param->name, DATAPDUINORDER))
 564			param->state &= ~PSTATE_NEGOTIATE;
 565		else if (!strcmp(param->name, DATASEQUENCEINORDER))
 566			param->state &= ~PSTATE_NEGOTIATE;
 567		else if (!strcmp(param->name, ERRORRECOVERYLEVEL))
 568			param->state &= ~PSTATE_NEGOTIATE;
 569		else if (!strcmp(param->name, DEFAULTTIME2WAIT))
 570			param->state &= ~PSTATE_NEGOTIATE;
 571		else if (!strcmp(param->name, DEFAULTTIME2RETAIN))
 572			param->state &= ~PSTATE_NEGOTIATE;
 573		else if (!strcmp(param->name, IFMARKER))
 574			param->state &= ~PSTATE_NEGOTIATE;
 575		else if (!strcmp(param->name, OFMARKER))
 576			param->state &= ~PSTATE_NEGOTIATE;
 577		else if (!strcmp(param->name, IFMARKINT))
 578			param->state &= ~PSTATE_NEGOTIATE;
 579		else if (!strcmp(param->name, OFMARKINT))
 580			param->state &= ~PSTATE_NEGOTIATE;
 581		else if (!strcmp(param->name, RDMAEXTENSIONS))
 582			param->state &= ~PSTATE_NEGOTIATE;
 583		else if (!strcmp(param->name, INITIATORRECVDATASEGMENTLENGTH))
 584			param->state &= ~PSTATE_NEGOTIATE;
 585		else if (!strcmp(param->name, TARGETRECVDATASEGMENTLENGTH))
 586			param->state &= ~PSTATE_NEGOTIATE;
 587	}
 588
 589	return 0;
 590}
 591
 592int iscsi_copy_param_list(
 593	struct iscsi_param_list **dst_param_list,
 594	struct iscsi_param_list *src_param_list,
 595	int leading)
 596{
 597	struct iscsi_param *param = NULL;
 598	struct iscsi_param *new_param = NULL;
 599	struct iscsi_param_list *param_list = NULL;
 600
 601	param_list = kzalloc(sizeof(struct iscsi_param_list), GFP_KERNEL);
 602	if (!param_list) {
 603		pr_err("Unable to allocate memory for struct iscsi_param_list.\n");
 604		goto err_out;
 605	}
 606	INIT_LIST_HEAD(&param_list->param_list);
 607	INIT_LIST_HEAD(&param_list->extra_response_list);
 608
 609	list_for_each_entry(param, &src_param_list->param_list, p_list) {
 610		if (!leading && (param->scope & SCOPE_SESSION_WIDE)) {
 611			if ((strcmp(param->name, "TargetName") != 0) &&
 612			    (strcmp(param->name, "InitiatorName") != 0) &&
 613			    (strcmp(param->name, "TargetPortalGroupTag") != 0))
 614				continue;
 615		}
 616
 617		new_param = kzalloc(sizeof(struct iscsi_param), GFP_KERNEL);
 618		if (!new_param) {
 619			pr_err("Unable to allocate memory for struct iscsi_param.\n");
 620			goto err_out;
 621		}
 622
 623		new_param->name = kstrdup(param->name, GFP_KERNEL);
 624		new_param->value = kstrdup(param->value, GFP_KERNEL);
 625		if (!new_param->value || !new_param->name) {
 626			kfree(new_param->value);
 627			kfree(new_param->name);
 628			kfree(new_param);
 629			pr_err("Unable to allocate memory for parameter name/value.\n");
 630			goto err_out;
 631		}
 632
 633		new_param->set_param = param->set_param;
 634		new_param->phase = param->phase;
 635		new_param->scope = param->scope;
 636		new_param->sender = param->sender;
 637		new_param->type = param->type;
 638		new_param->use = param->use;
 639		new_param->type_range = param->type_range;
 640
 641		list_add_tail(&new_param->p_list, &param_list->param_list);
 642	}
 643
 644	if (!list_empty(&param_list->param_list)) {
 645		*dst_param_list = param_list;
 646	} else {
 647		pr_err("No parameters allocated.\n");
 648		goto err_out;
 649	}
 650
 651	return 0;
 652
 653err_out:
 654	iscsi_release_param_list(param_list);
 655	return -1;
 656}
 657
 658static void iscsi_release_extra_responses(struct iscsi_param_list *param_list)
 659{
 660	struct iscsi_extra_response *er, *er_tmp;
 661
 662	list_for_each_entry_safe(er, er_tmp, &param_list->extra_response_list,
 663			er_list) {
 664		list_del(&er->er_list);
 665		kfree(er);
 666	}
 667}
 668
 669void iscsi_release_param_list(struct iscsi_param_list *param_list)
 670{
 671	struct iscsi_param *param, *param_tmp;
 672
 673	list_for_each_entry_safe(param, param_tmp, &param_list->param_list,
 674			p_list) {
 675		list_del(&param->p_list);
 676
 677		kfree(param->name);
 
 678		kfree(param->value);
 
 679		kfree(param);
 
 680	}
 681
 682	iscsi_release_extra_responses(param_list);
 683
 684	kfree(param_list);
 685}
 686
 687struct iscsi_param *iscsi_find_param_from_key(
 688	char *key,
 689	struct iscsi_param_list *param_list)
 690{
 691	struct iscsi_param *param;
 692
 693	if (!key || !param_list) {
 694		pr_err("Key or parameter list pointer is NULL.\n");
 695		return NULL;
 696	}
 697
 698	list_for_each_entry(param, &param_list->param_list, p_list) {
 699		if (!strcmp(key, param->name))
 700			return param;
 701	}
 702
 703	pr_err("Unable to locate key \"%s\".\n", key);
 704	return NULL;
 705}
 706
 707int iscsi_extract_key_value(char *textbuf, char **key, char **value)
 708{
 709	*value = strchr(textbuf, '=');
 710	if (!*value) {
 711		pr_err("Unable to locate \"=\" separator for key,"
 712				" ignoring request.\n");
 713		return -1;
 714	}
 715
 716	*key = textbuf;
 717	**value = '\0';
 718	*value = *value + 1;
 719
 720	return 0;
 721}
 722
 723int iscsi_update_param_value(struct iscsi_param *param, char *value)
 724{
 725	kfree(param->value);
 726
 727	param->value = kstrdup(value, GFP_KERNEL);
 728	if (!param->value) {
 729		pr_err("Unable to allocate memory for value.\n");
 730		return -ENOMEM;
 731	}
 732
 
 
 
 733	pr_debug("iSCSI Parameter updated to %s=%s\n",
 734			param->name, param->value);
 735	return 0;
 736}
 737
 738static int iscsi_add_notunderstood_response(
 739	char *key,
 740	char *value,
 741	struct iscsi_param_list *param_list)
 742{
 743	struct iscsi_extra_response *extra_response;
 744
 745	if (strlen(value) > VALUE_MAXLEN) {
 746		pr_err("Value for notunderstood key \"%s\" exceeds %d,"
 747			" protocol error.\n", key, VALUE_MAXLEN);
 748		return -1;
 749	}
 750
 751	extra_response = kzalloc(sizeof(struct iscsi_extra_response), GFP_KERNEL);
 752	if (!extra_response) {
 753		pr_err("Unable to allocate memory for"
 754			" struct iscsi_extra_response.\n");
 755		return -1;
 756	}
 757	INIT_LIST_HEAD(&extra_response->er_list);
 758
 759	strlcpy(extra_response->key, key, sizeof(extra_response->key));
 760	strlcpy(extra_response->value, NOTUNDERSTOOD,
 761		sizeof(extra_response->value));
 762
 763	list_add_tail(&extra_response->er_list,
 764			&param_list->extra_response_list);
 765	return 0;
 766}
 767
 768static int iscsi_check_for_auth_key(char *key)
 769{
 770	/*
 771	 * RFC 1994
 772	 */
 773	if (!strcmp(key, "CHAP_A") || !strcmp(key, "CHAP_I") ||
 774	    !strcmp(key, "CHAP_C") || !strcmp(key, "CHAP_N") ||
 775	    !strcmp(key, "CHAP_R"))
 776		return 1;
 777
 778	/*
 779	 * RFC 2945
 780	 */
 781	if (!strcmp(key, "SRP_U") || !strcmp(key, "SRP_N") ||
 782	    !strcmp(key, "SRP_g") || !strcmp(key, "SRP_s") ||
 783	    !strcmp(key, "SRP_A") || !strcmp(key, "SRP_B") ||
 784	    !strcmp(key, "SRP_M") || !strcmp(key, "SRP_HM"))
 785		return 1;
 786
 787	return 0;
 788}
 789
 790static void iscsi_check_proposer_for_optional_reply(struct iscsi_param *param)
 791{
 792	if (IS_TYPE_BOOL_AND(param)) {
 793		if (!strcmp(param->value, NO))
 794			SET_PSTATE_REPLY_OPTIONAL(param);
 795	} else if (IS_TYPE_BOOL_OR(param)) {
 796		if (!strcmp(param->value, YES))
 797			SET_PSTATE_REPLY_OPTIONAL(param);
 798		 /*
 799		  * Required for gPXE iSCSI boot client
 800		  */
 801		if (!strcmp(param->name, IMMEDIATEDATA))
 802			SET_PSTATE_REPLY_OPTIONAL(param);
 803	} else if (IS_TYPE_NUMBER(param)) {
 804		if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH))
 805			SET_PSTATE_REPLY_OPTIONAL(param);
 806		/*
 807		 * The GlobalSAN iSCSI Initiator for MacOSX does
 808		 * not respond to MaxBurstLength, FirstBurstLength,
 809		 * DefaultTime2Wait or DefaultTime2Retain parameter keys.
 810		 * So, we set them to 'reply optional' here, and assume the
 811		 * the defaults from iscsi_parameters.h if the initiator
 812		 * is not RFC compliant and the keys are not negotiated.
 813		 */
 814		if (!strcmp(param->name, MAXBURSTLENGTH))
 815			SET_PSTATE_REPLY_OPTIONAL(param);
 816		if (!strcmp(param->name, FIRSTBURSTLENGTH))
 817			SET_PSTATE_REPLY_OPTIONAL(param);
 818		if (!strcmp(param->name, DEFAULTTIME2WAIT))
 819			SET_PSTATE_REPLY_OPTIONAL(param);
 820		if (!strcmp(param->name, DEFAULTTIME2RETAIN))
 821			SET_PSTATE_REPLY_OPTIONAL(param);
 822		/*
 823		 * Required for gPXE iSCSI boot client
 824		 */
 825		if (!strcmp(param->name, MAXCONNECTIONS))
 826			SET_PSTATE_REPLY_OPTIONAL(param);
 827	} else if (IS_PHASE_DECLARATIVE(param))
 828		SET_PSTATE_REPLY_OPTIONAL(param);
 829}
 830
 831static int iscsi_check_boolean_value(struct iscsi_param *param, char *value)
 832{
 833	if (strcmp(value, YES) && strcmp(value, NO)) {
 834		pr_err("Illegal value for \"%s\", must be either"
 835			" \"%s\" or \"%s\".\n", param->name, YES, NO);
 836		return -1;
 837	}
 838
 839	return 0;
 840}
 841
 842static int iscsi_check_numerical_value(struct iscsi_param *param, char *value_ptr)
 843{
 844	char *tmpptr;
 845	int value = 0;
 846
 847	value = simple_strtoul(value_ptr, &tmpptr, 0);
 848
 
 
 
 
 
 
 
 
 849	if (IS_TYPERANGE_0_TO_2(param)) {
 850		if ((value < 0) || (value > 2)) {
 851			pr_err("Illegal value for \"%s\", must be"
 852				" between 0 and 2.\n", param->name);
 853			return -1;
 854		}
 855		return 0;
 856	}
 857	if (IS_TYPERANGE_0_TO_3600(param)) {
 858		if ((value < 0) || (value > 3600)) {
 859			pr_err("Illegal value for \"%s\", must be"
 860				" between 0 and 3600.\n", param->name);
 861			return -1;
 862		}
 863		return 0;
 864	}
 865	if (IS_TYPERANGE_0_TO_32767(param)) {
 866		if ((value < 0) || (value > 32767)) {
 867			pr_err("Illegal value for \"%s\", must be"
 868				" between 0 and 32767.\n", param->name);
 869			return -1;
 870		}
 871		return 0;
 872	}
 873	if (IS_TYPERANGE_0_TO_65535(param)) {
 874		if ((value < 0) || (value > 65535)) {
 875			pr_err("Illegal value for \"%s\", must be"
 876				" between 0 and 65535.\n", param->name);
 877			return -1;
 878		}
 879		return 0;
 880	}
 881	if (IS_TYPERANGE_1_TO_65535(param)) {
 882		if ((value < 1) || (value > 65535)) {
 883			pr_err("Illegal value for \"%s\", must be"
 884				" between 1 and 65535.\n", param->name);
 885			return -1;
 886		}
 887		return 0;
 888	}
 889	if (IS_TYPERANGE_2_TO_3600(param)) {
 890		if ((value < 2) || (value > 3600)) {
 891			pr_err("Illegal value for \"%s\", must be"
 892				" between 2 and 3600.\n", param->name);
 893			return -1;
 894		}
 895		return 0;
 896	}
 897	if (IS_TYPERANGE_512_TO_16777215(param)) {
 898		if ((value < 512) || (value > 16777215)) {
 899			pr_err("Illegal value for \"%s\", must be"
 900				" between 512 and 16777215.\n", param->name);
 901			return -1;
 902		}
 903		return 0;
 904	}
 905
 906	return 0;
 907}
 908
 909static int iscsi_check_numerical_range_value(struct iscsi_param *param, char *value)
 910{
 911	char *left_val_ptr = NULL, *right_val_ptr = NULL;
 912	char *tilde_ptr = NULL;
 913	u32 left_val, right_val, local_left_val;
 914
 915	if (strcmp(param->name, IFMARKINT) &&
 916	    strcmp(param->name, OFMARKINT)) {
 917		pr_err("Only parameters \"%s\" or \"%s\" may contain a"
 918		       " numerical range value.\n", IFMARKINT, OFMARKINT);
 919		return -1;
 920	}
 921
 922	if (IS_PSTATE_PROPOSER(param))
 923		return 0;
 924
 925	tilde_ptr = strchr(value, '~');
 926	if (!tilde_ptr) {
 927		pr_err("Unable to locate numerical range indicator"
 928			" \"~\" for \"%s\".\n", param->name);
 929		return -1;
 930	}
 931	*tilde_ptr = '\0';
 932
 933	left_val_ptr = value;
 934	right_val_ptr = value + strlen(left_val_ptr) + 1;
 935
 936	if (iscsi_check_numerical_value(param, left_val_ptr) < 0)
 937		return -1;
 938	if (iscsi_check_numerical_value(param, right_val_ptr) < 0)
 939		return -1;
 940
 941	left_val = simple_strtoul(left_val_ptr, NULL, 0);
 942	right_val = simple_strtoul(right_val_ptr, NULL, 0);
 943	*tilde_ptr = '~';
 944
 945	if (right_val < left_val) {
 946		pr_err("Numerical range for parameter \"%s\" contains"
 947			" a right value which is less than the left.\n",
 948				param->name);
 949		return -1;
 950	}
 951
 952	/*
 953	 * For now,  enforce reasonable defaults for [I,O]FMarkInt.
 954	 */
 955	tilde_ptr = strchr(param->value, '~');
 956	if (!tilde_ptr) {
 957		pr_err("Unable to locate numerical range indicator"
 958			" \"~\" for \"%s\".\n", param->name);
 959		return -1;
 960	}
 961	*tilde_ptr = '\0';
 962
 963	left_val_ptr = param->value;
 964	right_val_ptr = param->value + strlen(left_val_ptr) + 1;
 965
 966	local_left_val = simple_strtoul(left_val_ptr, NULL, 0);
 
 967	*tilde_ptr = '~';
 968
 969	if (param->set_param) {
 970		if ((left_val < local_left_val) ||
 971		    (right_val < local_left_val)) {
 972			pr_err("Passed value range \"%u~%u\" is below"
 973				" minimum left value \"%u\" for key \"%s\","
 974				" rejecting.\n", left_val, right_val,
 975				local_left_val, param->name);
 976			return -1;
 977		}
 978	} else {
 979		if ((left_val < local_left_val) &&
 980		    (right_val < local_left_val)) {
 981			pr_err("Received value range \"%u~%u\" is"
 982				" below minimum left value \"%u\" for key"
 983				" \"%s\", rejecting.\n", left_val, right_val,
 984				local_left_val, param->name);
 985			SET_PSTATE_REJECT(param);
 986			if (iscsi_update_param_value(param, REJECT) < 0)
 987				return -1;
 988		}
 989	}
 990
 991	return 0;
 992}
 993
 994static int iscsi_check_string_or_list_value(struct iscsi_param *param, char *value)
 995{
 996	if (IS_PSTATE_PROPOSER(param))
 997		return 0;
 998
 999	if (IS_TYPERANGE_AUTH_PARAM(param)) {
1000		if (strcmp(value, KRB5) && strcmp(value, SPKM1) &&
1001		    strcmp(value, SPKM2) && strcmp(value, SRP) &&
1002		    strcmp(value, CHAP) && strcmp(value, NONE)) {
1003			pr_err("Illegal value for \"%s\", must be"
1004				" \"%s\", \"%s\", \"%s\", \"%s\", \"%s\""
1005				" or \"%s\".\n", param->name, KRB5,
1006					SPKM1, SPKM2, SRP, CHAP, NONE);
1007			return -1;
1008		}
1009	}
1010	if (IS_TYPERANGE_DIGEST_PARAM(param)) {
1011		if (strcmp(value, CRC32C) && strcmp(value, NONE)) {
1012			pr_err("Illegal value for \"%s\", must be"
1013				" \"%s\" or \"%s\".\n", param->name,
1014					CRC32C, NONE);
1015			return -1;
1016		}
1017	}
1018	if (IS_TYPERANGE_SESSIONTYPE(param)) {
1019		if (strcmp(value, DISCOVERY) && strcmp(value, NORMAL)) {
1020			pr_err("Illegal value for \"%s\", must be"
1021				" \"%s\" or \"%s\".\n", param->name,
1022					DISCOVERY, NORMAL);
1023			return -1;
1024		}
1025	}
1026
1027	return 0;
1028}
1029
1030/*
1031 *	This function is used to pick a value range number,  currently just
1032 *	returns the lesser of both right values.
1033 */
1034static char *iscsi_get_value_from_number_range(
1035	struct iscsi_param *param,
1036	char *value)
1037{
1038	char *end_ptr, *tilde_ptr1 = NULL, *tilde_ptr2 = NULL;
1039	u32 acceptor_right_value, proposer_right_value;
1040
1041	tilde_ptr1 = strchr(value, '~');
1042	if (!tilde_ptr1)
1043		return NULL;
1044	*tilde_ptr1++ = '\0';
1045	proposer_right_value = simple_strtoul(tilde_ptr1, &end_ptr, 0);
1046
1047	tilde_ptr2 = strchr(param->value, '~');
1048	if (!tilde_ptr2)
1049		return NULL;
1050	*tilde_ptr2++ = '\0';
1051	acceptor_right_value = simple_strtoul(tilde_ptr2, &end_ptr, 0);
1052
1053	return (acceptor_right_value >= proposer_right_value) ?
1054		tilde_ptr1 : tilde_ptr2;
1055}
1056
1057static char *iscsi_check_valuelist_for_support(
1058	struct iscsi_param *param,
1059	char *value)
1060{
1061	char *tmp1 = NULL, *tmp2 = NULL;
1062	char *acceptor_values = NULL, *proposer_values = NULL;
1063
1064	acceptor_values = param->value;
1065	proposer_values = value;
1066
1067	do {
1068		if (!proposer_values)
1069			return NULL;
1070		tmp1 = strchr(proposer_values, ',');
1071		if (tmp1)
1072			*tmp1 = '\0';
1073		acceptor_values = param->value;
1074		do {
1075			if (!acceptor_values) {
1076				if (tmp1)
1077					*tmp1 = ',';
1078				return NULL;
1079			}
1080			tmp2 = strchr(acceptor_values, ',');
1081			if (tmp2)
1082				*tmp2 = '\0';
 
 
 
 
 
 
 
1083			if (!strcmp(acceptor_values, proposer_values)) {
1084				if (tmp2)
1085					*tmp2 = ',';
1086				goto out;
1087			}
1088			if (tmp2)
1089				*tmp2++ = ',';
1090
1091			acceptor_values = tmp2;
 
 
1092		} while (acceptor_values);
1093		if (tmp1)
1094			*tmp1++ = ',';
1095		proposer_values = tmp1;
1096	} while (proposer_values);
1097
1098out:
1099	return proposer_values;
1100}
1101
1102static int iscsi_check_acceptor_state(struct iscsi_param *param, char *value,
1103				struct iscsi_conn *conn)
1104{
1105	u8 acceptor_boolean_value = 0, proposer_boolean_value = 0;
1106	char *negoitated_value = NULL;
1107
1108	if (IS_PSTATE_ACCEPTOR(param)) {
1109		pr_err("Received key \"%s\" twice, protocol error.\n",
1110				param->name);
1111		return -1;
1112	}
1113
1114	if (IS_PSTATE_REJECT(param))
1115		return 0;
1116
1117	if (IS_TYPE_BOOL_AND(param)) {
1118		if (!strcmp(value, YES))
1119			proposer_boolean_value = 1;
1120		if (!strcmp(param->value, YES))
1121			acceptor_boolean_value = 1;
1122		if (acceptor_boolean_value && proposer_boolean_value)
1123			do {} while (0);
1124		else {
1125			if (iscsi_update_param_value(param, NO) < 0)
1126				return -1;
1127			if (!proposer_boolean_value)
1128				SET_PSTATE_REPLY_OPTIONAL(param);
1129		}
1130	} else if (IS_TYPE_BOOL_OR(param)) {
1131		if (!strcmp(value, YES))
1132			proposer_boolean_value = 1;
1133		if (!strcmp(param->value, YES))
1134			acceptor_boolean_value = 1;
1135		if (acceptor_boolean_value || proposer_boolean_value) {
1136			if (iscsi_update_param_value(param, YES) < 0)
1137				return -1;
1138			if (proposer_boolean_value)
1139				SET_PSTATE_REPLY_OPTIONAL(param);
1140		}
1141	} else if (IS_TYPE_NUMBER(param)) {
1142		char *tmpptr, buf[11];
1143		u32 acceptor_value = simple_strtoul(param->value, &tmpptr, 0);
1144		u32 proposer_value = simple_strtoul(value, &tmpptr, 0);
1145
1146		memset(buf, 0, sizeof(buf));
1147
1148		if (!strcmp(param->name, MAXCONNECTIONS) ||
1149		    !strcmp(param->name, MAXBURSTLENGTH) ||
1150		    !strcmp(param->name, FIRSTBURSTLENGTH) ||
1151		    !strcmp(param->name, MAXOUTSTANDINGR2T) ||
1152		    !strcmp(param->name, DEFAULTTIME2RETAIN) ||
1153		    !strcmp(param->name, ERRORRECOVERYLEVEL)) {
1154			if (proposer_value > acceptor_value) {
1155				sprintf(buf, "%u", acceptor_value);
1156				if (iscsi_update_param_value(param,
1157						&buf[0]) < 0)
1158					return -1;
1159			} else {
1160				if (iscsi_update_param_value(param, value) < 0)
1161					return -1;
1162			}
1163		} else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
1164			if (acceptor_value > proposer_value) {
1165				sprintf(buf, "%u", acceptor_value);
1166				if (iscsi_update_param_value(param,
1167						&buf[0]) < 0)
1168					return -1;
1169			} else {
1170				if (iscsi_update_param_value(param, value) < 0)
1171					return -1;
1172			}
1173		} else {
1174			if (iscsi_update_param_value(param, value) < 0)
1175				return -1;
1176		}
1177
1178		if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
1179			struct iscsi_param *param_mxdsl;
1180			unsigned long long tmp;
1181			int rc;
1182
1183			rc = kstrtoull(param->value, 0, &tmp);
1184			if (rc < 0)
1185				return -1;
1186
1187			conn->conn_ops->MaxRecvDataSegmentLength = tmp;
1188			pr_debug("Saving op->MaxRecvDataSegmentLength from"
1189				" original initiator received value: %u\n",
1190				conn->conn_ops->MaxRecvDataSegmentLength);
1191
1192			param_mxdsl = iscsi_find_param_from_key(
1193						MAXXMITDATASEGMENTLENGTH,
1194						conn->param_list);
1195			if (!param_mxdsl)
1196				return -1;
1197
1198			rc = iscsi_update_param_value(param,
1199						param_mxdsl->value);
1200			if (rc < 0)
1201				return -1;
1202
1203			pr_debug("Updated %s to target MXDSL value: %s\n",
1204					param->name, param->value);
1205		}
1206
1207	} else if (IS_TYPE_NUMBER_RANGE(param)) {
1208		negoitated_value = iscsi_get_value_from_number_range(
1209					param, value);
1210		if (!negoitated_value)
1211			return -1;
1212		if (iscsi_update_param_value(param, negoitated_value) < 0)
1213			return -1;
1214	} else if (IS_TYPE_VALUE_LIST(param)) {
1215		negoitated_value = iscsi_check_valuelist_for_support(
1216					param, value);
1217		if (!negoitated_value) {
1218			pr_err("Proposer's value list \"%s\" contains"
1219				" no valid values from Acceptor's value list"
1220				" \"%s\".\n", value, param->value);
1221			return -1;
1222		}
1223		if (iscsi_update_param_value(param, negoitated_value) < 0)
1224			return -1;
1225	} else if (IS_PHASE_DECLARATIVE(param)) {
1226		if (iscsi_update_param_value(param, value) < 0)
1227			return -1;
1228		SET_PSTATE_REPLY_OPTIONAL(param);
1229	}
1230
1231	return 0;
1232}
1233
1234static int iscsi_check_proposer_state(struct iscsi_param *param, char *value)
1235{
1236	if (IS_PSTATE_RESPONSE_GOT(param)) {
1237		pr_err("Received key \"%s\" twice, protocol error.\n",
1238				param->name);
1239		return -1;
1240	}
1241
1242	if (IS_TYPE_NUMBER_RANGE(param)) {
1243		u32 left_val = 0, right_val = 0, recieved_value = 0;
1244		char *left_val_ptr = NULL, *right_val_ptr = NULL;
1245		char *tilde_ptr = NULL;
1246
1247		if (!strcmp(value, IRRELEVANT) || !strcmp(value, REJECT)) {
1248			if (iscsi_update_param_value(param, value) < 0)
1249				return -1;
1250			return 0;
1251		}
1252
1253		tilde_ptr = strchr(value, '~');
1254		if (tilde_ptr) {
1255			pr_err("Illegal \"~\" in response for \"%s\".\n",
1256					param->name);
1257			return -1;
1258		}
1259		tilde_ptr = strchr(param->value, '~');
1260		if (!tilde_ptr) {
1261			pr_err("Unable to locate numerical range"
1262				" indicator \"~\" for \"%s\".\n", param->name);
1263			return -1;
1264		}
1265		*tilde_ptr = '\0';
1266
1267		left_val_ptr = param->value;
1268		right_val_ptr = param->value + strlen(left_val_ptr) + 1;
1269		left_val = simple_strtoul(left_val_ptr, NULL, 0);
1270		right_val = simple_strtoul(right_val_ptr, NULL, 0);
1271		recieved_value = simple_strtoul(value, NULL, 0);
1272
1273		*tilde_ptr = '~';
1274
1275		if ((recieved_value < left_val) ||
1276		    (recieved_value > right_val)) {
1277			pr_err("Illegal response \"%s=%u\", value must"
1278				" be between %u and %u.\n", param->name,
1279				recieved_value, left_val, right_val);
1280			return -1;
1281		}
1282	} else if (IS_TYPE_VALUE_LIST(param)) {
1283		char *comma_ptr = NULL, *tmp_ptr = NULL;
1284
1285		comma_ptr = strchr(value, ',');
1286		if (comma_ptr) {
1287			pr_err("Illegal \",\" in response for \"%s\".\n",
1288					param->name);
1289			return -1;
1290		}
1291
1292		tmp_ptr = iscsi_check_valuelist_for_support(param, value);
1293		if (!tmp_ptr)
1294			return -1;
1295	}
1296
1297	if (iscsi_update_param_value(param, value) < 0)
1298		return -1;
1299
1300	return 0;
1301}
1302
1303static int iscsi_check_value(struct iscsi_param *param, char *value)
1304{
1305	char *comma_ptr = NULL;
1306
1307	if (!strcmp(value, REJECT)) {
1308		if (!strcmp(param->name, IFMARKINT) ||
1309		    !strcmp(param->name, OFMARKINT)) {
1310			/*
1311			 * Reject is not fatal for [I,O]FMarkInt,  and causes
1312			 * [I,O]FMarker to be reset to No. (See iSCSI v20 A.3.2)
1313			 */
1314			SET_PSTATE_REJECT(param);
1315			return 0;
1316		}
1317		pr_err("Received %s=%s\n", param->name, value);
1318		return -1;
1319	}
1320	if (!strcmp(value, IRRELEVANT)) {
1321		pr_debug("Received %s=%s\n", param->name, value);
1322		SET_PSTATE_IRRELEVANT(param);
1323		return 0;
1324	}
1325	if (!strcmp(value, NOTUNDERSTOOD)) {
1326		if (!IS_PSTATE_PROPOSER(param)) {
1327			pr_err("Received illegal offer %s=%s\n",
1328				param->name, value);
1329			return -1;
1330		}
1331
1332/* #warning FIXME: Add check for X-ExtensionKey here */
1333		pr_err("Standard iSCSI key \"%s\" cannot be answered"
1334			" with \"%s\", protocol error.\n", param->name, value);
1335		return -1;
1336	}
1337
1338	do {
1339		comma_ptr = NULL;
1340		comma_ptr = strchr(value, ',');
1341
1342		if (comma_ptr && !IS_TYPE_VALUE_LIST(param)) {
1343			pr_err("Detected value separator \",\", but"
1344				" key \"%s\" does not allow a value list,"
1345				" protocol error.\n", param->name);
1346			return -1;
1347		}
1348		if (comma_ptr)
1349			*comma_ptr = '\0';
1350
1351		if (strlen(value) > VALUE_MAXLEN) {
1352			pr_err("Value for key \"%s\" exceeds %d,"
1353				" protocol error.\n", param->name,
1354				VALUE_MAXLEN);
1355			return -1;
1356		}
1357
1358		if (IS_TYPE_BOOL_AND(param) || IS_TYPE_BOOL_OR(param)) {
1359			if (iscsi_check_boolean_value(param, value) < 0)
1360				return -1;
1361		} else if (IS_TYPE_NUMBER(param)) {
1362			if (iscsi_check_numerical_value(param, value) < 0)
1363				return -1;
1364		} else if (IS_TYPE_NUMBER_RANGE(param)) {
1365			if (iscsi_check_numerical_range_value(param, value) < 0)
1366				return -1;
1367		} else if (IS_TYPE_STRING(param) || IS_TYPE_VALUE_LIST(param)) {
1368			if (iscsi_check_string_or_list_value(param, value) < 0)
1369				return -1;
1370		} else {
1371			pr_err("Huh? 0x%02x\n", param->type);
1372			return -1;
1373		}
1374
1375		if (comma_ptr)
1376			*comma_ptr++ = ',';
1377
1378		value = comma_ptr;
1379	} while (value);
1380
1381	return 0;
1382}
1383
1384static struct iscsi_param *__iscsi_check_key(
1385	char *key,
1386	int sender,
1387	struct iscsi_param_list *param_list)
1388{
1389	struct iscsi_param *param;
1390
1391	if (strlen(key) > KEY_MAXLEN) {
1392		pr_err("Length of key name \"%s\" exceeds %d.\n",
1393			key, KEY_MAXLEN);
1394		return NULL;
1395	}
1396
1397	param = iscsi_find_param_from_key(key, param_list);
1398	if (!param)
1399		return NULL;
1400
1401	if ((sender & SENDER_INITIATOR) && !IS_SENDER_INITIATOR(param)) {
1402		pr_err("Key \"%s\" may not be sent to %s,"
1403			" protocol error.\n", param->name,
1404			(sender & SENDER_RECEIVER) ? "target" : "initiator");
1405		return NULL;
1406	}
1407
1408	if ((sender & SENDER_TARGET) && !IS_SENDER_TARGET(param)) {
1409		pr_err("Key \"%s\" may not be sent to %s,"
1410			" protocol error.\n", param->name,
1411			(sender & SENDER_RECEIVER) ? "initiator" : "target");
1412		return NULL;
1413	}
1414
1415	return param;
1416}
1417
1418static struct iscsi_param *iscsi_check_key(
1419	char *key,
1420	int phase,
1421	int sender,
1422	struct iscsi_param_list *param_list)
1423{
1424	struct iscsi_param *param;
1425	/*
1426	 * Key name length must not exceed 63 bytes. (See iSCSI v20 5.1)
1427	 */
1428	if (strlen(key) > KEY_MAXLEN) {
1429		pr_err("Length of key name \"%s\" exceeds %d.\n",
1430			key, KEY_MAXLEN);
1431		return NULL;
1432	}
1433
1434	param = iscsi_find_param_from_key(key, param_list);
1435	if (!param)
1436		return NULL;
1437
1438	if ((sender & SENDER_INITIATOR) && !IS_SENDER_INITIATOR(param)) {
1439		pr_err("Key \"%s\" may not be sent to %s,"
1440			" protocol error.\n", param->name,
1441			(sender & SENDER_RECEIVER) ? "target" : "initiator");
1442		return NULL;
1443	}
1444	if ((sender & SENDER_TARGET) && !IS_SENDER_TARGET(param)) {
1445		pr_err("Key \"%s\" may not be sent to %s,"
1446				" protocol error.\n", param->name,
1447			(sender & SENDER_RECEIVER) ? "initiator" : "target");
1448		return NULL;
1449	}
1450
1451	if (IS_PSTATE_ACCEPTOR(param)) {
1452		pr_err("Key \"%s\" received twice, protocol error.\n",
1453				key);
1454		return NULL;
1455	}
1456
1457	if (!phase)
1458		return param;
1459
1460	if (!(param->phase & phase)) {
1461		pr_err("Key \"%s\" may not be negotiated during ",
1462				param->name);
1463		switch (phase) {
1464		case PHASE_SECURITY:
1465			pr_debug("Security phase.\n");
1466			break;
1467		case PHASE_OPERATIONAL:
1468			pr_debug("Operational phase.\n");
1469			break;
1470		default:
1471			pr_debug("Unknown phase.\n");
1472		}
1473		return NULL;
1474	}
1475
1476	return param;
1477}
1478
1479static int iscsi_enforce_integrity_rules(
1480	u8 phase,
1481	struct iscsi_param_list *param_list)
1482{
1483	char *tmpptr;
1484	u8 DataSequenceInOrder = 0;
1485	u8 ErrorRecoveryLevel = 0, SessionType = 0;
1486	u8 IFMarker = 0, OFMarker = 0;
1487	u8 IFMarkInt_Reject = 1, OFMarkInt_Reject = 1;
1488	u32 FirstBurstLength = 0, MaxBurstLength = 0;
1489	struct iscsi_param *param = NULL;
1490
1491	list_for_each_entry(param, &param_list->param_list, p_list) {
1492		if (!(param->phase & phase))
1493			continue;
1494		if (!strcmp(param->name, SESSIONTYPE))
1495			if (!strcmp(param->value, NORMAL))
1496				SessionType = 1;
1497		if (!strcmp(param->name, ERRORRECOVERYLEVEL))
1498			ErrorRecoveryLevel = simple_strtoul(param->value,
1499					&tmpptr, 0);
1500		if (!strcmp(param->name, DATASEQUENCEINORDER))
1501			if (!strcmp(param->value, YES))
1502				DataSequenceInOrder = 1;
1503		if (!strcmp(param->name, MAXBURSTLENGTH))
1504			MaxBurstLength = simple_strtoul(param->value,
1505					&tmpptr, 0);
1506		if (!strcmp(param->name, IFMARKER))
1507			if (!strcmp(param->value, YES))
1508				IFMarker = 1;
1509		if (!strcmp(param->name, OFMARKER))
1510			if (!strcmp(param->value, YES))
1511				OFMarker = 1;
1512		if (!strcmp(param->name, IFMARKINT))
1513			if (!strcmp(param->value, REJECT))
1514				IFMarkInt_Reject = 1;
1515		if (!strcmp(param->name, OFMARKINT))
1516			if (!strcmp(param->value, REJECT))
1517				OFMarkInt_Reject = 1;
1518	}
1519
1520	list_for_each_entry(param, &param_list->param_list, p_list) {
1521		if (!(param->phase & phase))
1522			continue;
1523		if (!SessionType && (!IS_PSTATE_ACCEPTOR(param) &&
1524		     (strcmp(param->name, IFMARKER) &&
1525		      strcmp(param->name, OFMARKER) &&
1526		      strcmp(param->name, IFMARKINT) &&
1527		      strcmp(param->name, OFMARKINT))))
1528			continue;
1529		if (!strcmp(param->name, MAXOUTSTANDINGR2T) &&
1530		    DataSequenceInOrder && (ErrorRecoveryLevel > 0)) {
1531			if (strcmp(param->value, "1")) {
1532				if (iscsi_update_param_value(param, "1") < 0)
1533					return -1;
1534				pr_debug("Reset \"%s\" to \"%s\".\n",
1535					param->name, param->value);
1536			}
1537		}
1538		if (!strcmp(param->name, MAXCONNECTIONS) && !SessionType) {
1539			if (strcmp(param->value, "1")) {
1540				if (iscsi_update_param_value(param, "1") < 0)
1541					return -1;
1542				pr_debug("Reset \"%s\" to \"%s\".\n",
1543					param->name, param->value);
1544			}
1545		}
1546		if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
1547			FirstBurstLength = simple_strtoul(param->value,
1548					&tmpptr, 0);
1549			if (FirstBurstLength > MaxBurstLength) {
1550				char tmpbuf[11];
1551				memset(tmpbuf, 0, sizeof(tmpbuf));
1552				sprintf(tmpbuf, "%u", MaxBurstLength);
1553				if (iscsi_update_param_value(param, tmpbuf))
1554					return -1;
1555				pr_debug("Reset \"%s\" to \"%s\".\n",
1556					param->name, param->value);
1557			}
1558		}
1559		if (!strcmp(param->name, IFMARKER) && IFMarkInt_Reject) {
1560			if (iscsi_update_param_value(param, NO) < 0)
1561				return -1;
1562			IFMarker = 0;
1563			pr_debug("Reset \"%s\" to \"%s\".\n",
1564					param->name, param->value);
1565		}
1566		if (!strcmp(param->name, OFMARKER) && OFMarkInt_Reject) {
1567			if (iscsi_update_param_value(param, NO) < 0)
1568				return -1;
1569			OFMarker = 0;
1570			pr_debug("Reset \"%s\" to \"%s\".\n",
1571					 param->name, param->value);
1572		}
1573		if (!strcmp(param->name, IFMARKINT) && !IFMarker) {
1574			if (!strcmp(param->value, REJECT))
1575				continue;
1576			param->state &= ~PSTATE_NEGOTIATE;
1577			if (iscsi_update_param_value(param, IRRELEVANT) < 0)
1578				return -1;
1579			pr_debug("Reset \"%s\" to \"%s\".\n",
1580					param->name, param->value);
1581		}
1582		if (!strcmp(param->name, OFMARKINT) && !OFMarker) {
1583			if (!strcmp(param->value, REJECT))
1584				continue;
1585			param->state &= ~PSTATE_NEGOTIATE;
1586			if (iscsi_update_param_value(param, IRRELEVANT) < 0)
1587				return -1;
1588			pr_debug("Reset \"%s\" to \"%s\".\n",
1589					param->name, param->value);
1590		}
1591	}
1592
1593	return 0;
1594}
1595
1596int iscsi_decode_text_input(
1597	u8 phase,
1598	u8 sender,
1599	char *textbuf,
1600	u32 length,
1601	struct iscsi_conn *conn)
1602{
1603	struct iscsi_param_list *param_list = conn->param_list;
1604	char *tmpbuf, *start = NULL, *end = NULL;
1605
1606	tmpbuf = kzalloc(length + 1, GFP_KERNEL);
1607	if (!tmpbuf) {
1608		pr_err("Unable to allocate memory for tmpbuf.\n");
1609		return -1;
1610	}
1611
1612	memcpy(tmpbuf, textbuf, length);
1613	tmpbuf[length] = '\0';
1614	start = tmpbuf;
1615	end = (start + length);
1616
1617	while (start < end) {
1618		char *key, *value;
1619		struct iscsi_param *param;
1620
1621		if (iscsi_extract_key_value(start, &key, &value) < 0) {
1622			kfree(tmpbuf);
1623			return -1;
1624		}
1625
1626		pr_debug("Got key: %s=%s\n", key, value);
1627
1628		if (phase & PHASE_SECURITY) {
1629			if (iscsi_check_for_auth_key(key) > 0) {
 
 
1630				kfree(tmpbuf);
1631				return 1;
1632			}
1633		}
1634
1635		param = iscsi_check_key(key, phase, sender, param_list);
1636		if (!param) {
1637			if (iscsi_add_notunderstood_response(key,
1638					value, param_list) < 0) {
1639				kfree(tmpbuf);
1640				return -1;
1641			}
1642			start += strlen(key) + strlen(value) + 2;
1643			continue;
1644		}
1645		if (iscsi_check_value(param, value) < 0) {
1646			kfree(tmpbuf);
1647			return -1;
1648		}
1649
1650		start += strlen(key) + strlen(value) + 2;
1651
1652		if (IS_PSTATE_PROPOSER(param)) {
1653			if (iscsi_check_proposer_state(param, value) < 0) {
1654				kfree(tmpbuf);
1655				return -1;
1656			}
1657			SET_PSTATE_RESPONSE_GOT(param);
1658		} else {
1659			if (iscsi_check_acceptor_state(param, value, conn) < 0) {
1660				kfree(tmpbuf);
1661				return -1;
1662			}
1663			SET_PSTATE_ACCEPTOR(param);
1664		}
1665	}
1666
1667	kfree(tmpbuf);
1668	return 0;
1669}
1670
1671int iscsi_encode_text_output(
1672	u8 phase,
1673	u8 sender,
1674	char *textbuf,
1675	u32 *length,
1676	struct iscsi_param_list *param_list)
1677{
1678	char *output_buf = NULL;
1679	struct iscsi_extra_response *er;
1680	struct iscsi_param *param;
1681
1682	output_buf = textbuf + *length;
1683
1684	if (iscsi_enforce_integrity_rules(phase, param_list) < 0)
1685		return -1;
1686
1687	list_for_each_entry(param, &param_list->param_list, p_list) {
1688		if (!(param->sender & sender))
1689			continue;
1690		if (IS_PSTATE_ACCEPTOR(param) &&
1691		    !IS_PSTATE_RESPONSE_SENT(param) &&
1692		    !IS_PSTATE_REPLY_OPTIONAL(param) &&
1693		    (param->phase & phase)) {
1694			*length += sprintf(output_buf, "%s=%s",
1695				param->name, param->value);
1696			*length += 1;
1697			output_buf = textbuf + *length;
1698			SET_PSTATE_RESPONSE_SENT(param);
1699			pr_debug("Sending key: %s=%s\n",
1700				param->name, param->value);
1701			continue;
1702		}
1703		if (IS_PSTATE_NEGOTIATE(param) &&
1704		    !IS_PSTATE_ACCEPTOR(param) &&
1705		    !IS_PSTATE_PROPOSER(param) &&
1706		    (param->phase & phase)) {
1707			*length += sprintf(output_buf, "%s=%s",
1708				param->name, param->value);
1709			*length += 1;
1710			output_buf = textbuf + *length;
1711			SET_PSTATE_PROPOSER(param);
1712			iscsi_check_proposer_for_optional_reply(param);
1713			pr_debug("Sending key: %s=%s\n",
1714				param->name, param->value);
1715		}
1716	}
1717
1718	list_for_each_entry(er, &param_list->extra_response_list, er_list) {
1719		*length += sprintf(output_buf, "%s=%s", er->key, er->value);
1720		*length += 1;
1721		output_buf = textbuf + *length;
1722		pr_debug("Sending key: %s=%s\n", er->key, er->value);
1723	}
1724	iscsi_release_extra_responses(param_list);
1725
1726	return 0;
1727}
1728
1729int iscsi_check_negotiated_keys(struct iscsi_param_list *param_list)
1730{
1731	int ret = 0;
1732	struct iscsi_param *param;
1733
1734	list_for_each_entry(param, &param_list->param_list, p_list) {
1735		if (IS_PSTATE_NEGOTIATE(param) &&
1736		    IS_PSTATE_PROPOSER(param) &&
1737		    !IS_PSTATE_RESPONSE_GOT(param) &&
1738		    !IS_PSTATE_REPLY_OPTIONAL(param) &&
1739		    !IS_PHASE_DECLARATIVE(param)) {
1740			pr_err("No response for proposed key \"%s\".\n",
1741					param->name);
1742			ret = -1;
1743		}
1744	}
1745
1746	return ret;
1747}
1748
1749int iscsi_change_param_value(
1750	char *keyvalue,
1751	struct iscsi_param_list *param_list,
1752	int check_key)
1753{
1754	char *key = NULL, *value = NULL;
1755	struct iscsi_param *param;
1756	int sender = 0;
1757
1758	if (iscsi_extract_key_value(keyvalue, &key, &value) < 0)
1759		return -1;
1760
1761	if (!check_key) {
1762		param = __iscsi_check_key(keyvalue, sender, param_list);
1763		if (!param)
1764			return -1;
1765	} else {
1766		param = iscsi_check_key(keyvalue, 0, sender, param_list);
1767		if (!param)
1768			return -1;
1769
1770		param->set_param = 1;
1771		if (iscsi_check_value(param, value) < 0) {
1772			param->set_param = 0;
1773			return -1;
1774		}
1775		param->set_param = 0;
1776	}
1777
1778	if (iscsi_update_param_value(param, value) < 0)
1779		return -1;
1780
1781	return 0;
1782}
1783
1784void iscsi_set_connection_parameters(
1785	struct iscsi_conn_ops *ops,
1786	struct iscsi_param_list *param_list)
1787{
1788	char *tmpptr;
1789	struct iscsi_param *param;
1790
1791	pr_debug("---------------------------------------------------"
1792			"---------------\n");
1793	list_for_each_entry(param, &param_list->param_list, p_list) {
1794		/*
1795		 * Special case to set MAXXMITDATASEGMENTLENGTH from the
1796		 * target requested MaxRecvDataSegmentLength, even though
1797		 * this key is not sent over the wire.
1798		 */
1799		if (!strcmp(param->name, MAXXMITDATASEGMENTLENGTH)) {
1800			ops->MaxXmitDataSegmentLength =
1801				simple_strtoul(param->value, &tmpptr, 0);
1802			pr_debug("MaxXmitDataSegmentLength:     %s\n",
1803				param->value);
1804		}
1805
1806		if (!IS_PSTATE_ACCEPTOR(param) && !IS_PSTATE_PROPOSER(param))
1807			continue;
1808		if (!strcmp(param->name, AUTHMETHOD)) {
1809			pr_debug("AuthMethod:                   %s\n",
1810				param->value);
1811		} else if (!strcmp(param->name, HEADERDIGEST)) {
1812			ops->HeaderDigest = !strcmp(param->value, CRC32C);
1813			pr_debug("HeaderDigest:                 %s\n",
1814				param->value);
1815		} else if (!strcmp(param->name, DATADIGEST)) {
1816			ops->DataDigest = !strcmp(param->value, CRC32C);
1817			pr_debug("DataDigest:                   %s\n",
1818				param->value);
1819		} else if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
1820			/*
1821			 * At this point iscsi_check_acceptor_state() will have
1822			 * set ops->MaxRecvDataSegmentLength from the original
1823			 * initiator provided value.
1824			 */
1825			pr_debug("MaxRecvDataSegmentLength:     %u\n",
1826				ops->MaxRecvDataSegmentLength);
1827		} else if (!strcmp(param->name, OFMARKER)) {
1828			ops->OFMarker = !strcmp(param->value, YES);
1829			pr_debug("OFMarker:                     %s\n",
1830				param->value);
1831		} else if (!strcmp(param->name, IFMARKER)) {
1832			ops->IFMarker = !strcmp(param->value, YES);
1833			pr_debug("IFMarker:                     %s\n",
1834				param->value);
1835		} else if (!strcmp(param->name, OFMARKINT)) {
1836			ops->OFMarkInt =
1837				simple_strtoul(param->value, &tmpptr, 0);
1838			pr_debug("OFMarkInt:                    %s\n",
1839				param->value);
1840		} else if (!strcmp(param->name, IFMARKINT)) {
1841			ops->IFMarkInt =
1842				simple_strtoul(param->value, &tmpptr, 0);
1843			pr_debug("IFMarkInt:                    %s\n",
1844				param->value);
1845		} else if (!strcmp(param->name, INITIATORRECVDATASEGMENTLENGTH)) {
1846			ops->InitiatorRecvDataSegmentLength =
1847				simple_strtoul(param->value, &tmpptr, 0);
1848			pr_debug("InitiatorRecvDataSegmentLength: %s\n",
1849				param->value);
1850			ops->MaxRecvDataSegmentLength =
1851					ops->InitiatorRecvDataSegmentLength;
1852			pr_debug("Set MRDSL from InitiatorRecvDataSegmentLength\n");
1853		} else if (!strcmp(param->name, TARGETRECVDATASEGMENTLENGTH)) {
1854			ops->TargetRecvDataSegmentLength =
1855				simple_strtoul(param->value, &tmpptr, 0);
1856			pr_debug("TargetRecvDataSegmentLength:  %s\n",
1857				param->value);
1858			ops->MaxXmitDataSegmentLength =
1859					ops->TargetRecvDataSegmentLength;
1860			pr_debug("Set MXDSL from TargetRecvDataSegmentLength\n");
1861		}
1862	}
1863	pr_debug("----------------------------------------------------"
1864			"--------------\n");
1865}
1866
1867void iscsi_set_session_parameters(
1868	struct iscsi_sess_ops *ops,
1869	struct iscsi_param_list *param_list,
1870	int leading)
1871{
1872	char *tmpptr;
1873	struct iscsi_param *param;
1874
1875	pr_debug("----------------------------------------------------"
1876			"--------------\n");
1877	list_for_each_entry(param, &param_list->param_list, p_list) {
1878		if (!IS_PSTATE_ACCEPTOR(param) && !IS_PSTATE_PROPOSER(param))
1879			continue;
1880		if (!strcmp(param->name, INITIATORNAME)) {
1881			if (!param->value)
1882				continue;
1883			if (leading)
1884				snprintf(ops->InitiatorName,
1885						sizeof(ops->InitiatorName),
1886						"%s", param->value);
1887			pr_debug("InitiatorName:                %s\n",
1888				param->value);
1889		} else if (!strcmp(param->name, INITIATORALIAS)) {
1890			if (!param->value)
1891				continue;
1892			snprintf(ops->InitiatorAlias,
1893						sizeof(ops->InitiatorAlias),
1894						"%s", param->value);
1895			pr_debug("InitiatorAlias:               %s\n",
1896				param->value);
1897		} else if (!strcmp(param->name, TARGETNAME)) {
1898			if (!param->value)
1899				continue;
1900			if (leading)
1901				snprintf(ops->TargetName,
1902						sizeof(ops->TargetName),
1903						"%s", param->value);
1904			pr_debug("TargetName:                   %s\n",
1905				param->value);
1906		} else if (!strcmp(param->name, TARGETALIAS)) {
1907			if (!param->value)
1908				continue;
1909			snprintf(ops->TargetAlias, sizeof(ops->TargetAlias),
1910					"%s", param->value);
1911			pr_debug("TargetAlias:                  %s\n",
1912				param->value);
1913		} else if (!strcmp(param->name, TARGETPORTALGROUPTAG)) {
1914			ops->TargetPortalGroupTag =
1915				simple_strtoul(param->value, &tmpptr, 0);
1916			pr_debug("TargetPortalGroupTag:         %s\n",
1917				param->value);
1918		} else if (!strcmp(param->name, MAXCONNECTIONS)) {
1919			ops->MaxConnections =
1920				simple_strtoul(param->value, &tmpptr, 0);
1921			pr_debug("MaxConnections:               %s\n",
1922				param->value);
1923		} else if (!strcmp(param->name, INITIALR2T)) {
1924			ops->InitialR2T = !strcmp(param->value, YES);
1925			 pr_debug("InitialR2T:                   %s\n",
1926				param->value);
1927		} else if (!strcmp(param->name, IMMEDIATEDATA)) {
1928			ops->ImmediateData = !strcmp(param->value, YES);
1929			pr_debug("ImmediateData:                %s\n",
1930				param->value);
1931		} else if (!strcmp(param->name, MAXBURSTLENGTH)) {
1932			ops->MaxBurstLength =
1933				simple_strtoul(param->value, &tmpptr, 0);
1934			pr_debug("MaxBurstLength:               %s\n",
1935				param->value);
1936		} else if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
1937			ops->FirstBurstLength =
1938				simple_strtoul(param->value, &tmpptr, 0);
1939			pr_debug("FirstBurstLength:             %s\n",
1940				param->value);
1941		} else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
1942			ops->DefaultTime2Wait =
1943				simple_strtoul(param->value, &tmpptr, 0);
1944			pr_debug("DefaultTime2Wait:             %s\n",
1945				param->value);
1946		} else if (!strcmp(param->name, DEFAULTTIME2RETAIN)) {
1947			ops->DefaultTime2Retain =
1948				simple_strtoul(param->value, &tmpptr, 0);
1949			pr_debug("DefaultTime2Retain:           %s\n",
1950				param->value);
1951		} else if (!strcmp(param->name, MAXOUTSTANDINGR2T)) {
1952			ops->MaxOutstandingR2T =
1953				simple_strtoul(param->value, &tmpptr, 0);
1954			pr_debug("MaxOutstandingR2T:            %s\n",
1955				param->value);
1956		} else if (!strcmp(param->name, DATAPDUINORDER)) {
1957			ops->DataPDUInOrder = !strcmp(param->value, YES);
1958			pr_debug("DataPDUInOrder:               %s\n",
1959				param->value);
1960		} else if (!strcmp(param->name, DATASEQUENCEINORDER)) {
1961			ops->DataSequenceInOrder = !strcmp(param->value, YES);
1962			pr_debug("DataSequenceInOrder:          %s\n",
1963				param->value);
1964		} else if (!strcmp(param->name, ERRORRECOVERYLEVEL)) {
1965			ops->ErrorRecoveryLevel =
1966				simple_strtoul(param->value, &tmpptr, 0);
1967			pr_debug("ErrorRecoveryLevel:           %s\n",
1968				param->value);
1969		} else if (!strcmp(param->name, SESSIONTYPE)) {
1970			ops->SessionType = !strcmp(param->value, DISCOVERY);
1971			pr_debug("SessionType:                  %s\n",
1972				param->value);
1973		} else if (!strcmp(param->name, RDMAEXTENSIONS)) {
1974			ops->RDMAExtensions = !strcmp(param->value, YES);
1975			pr_debug("RDMAExtensions:               %s\n",
1976				param->value);
1977		}
1978	}
1979	pr_debug("----------------------------------------------------"
1980			"--------------\n");
1981
1982}