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