Merge tag 'chrome-platform-for-linus-4.13' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_parameters.c
blobfce627628200cf9917a8212da7a17ccff3d6136c
1 /*******************************************************************************
2 * This file contains main functions related to iSCSI Parameter negotiation.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ******************************************************************************/
19 #include <linux/slab.h>
20 #include <linux/uio.h> /* struct kvec */
21 #include <target/iscsi/iscsi_target_core.h>
22 #include "iscsi_target_util.h"
23 #include "iscsi_target_parameters.h"
25 int iscsi_login_rx_data(
26 struct iscsi_conn *conn,
27 char *buf,
28 int length)
30 int rx_got;
31 struct kvec iov;
33 memset(&iov, 0, sizeof(struct kvec));
34 iov.iov_len = length;
35 iov.iov_base = buf;
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;
44 return 0 ;
47 int iscsi_login_tx_data(
48 struct iscsi_conn *conn,
49 char *pdu_buf,
50 char *text_buf,
51 int text_length)
53 int length, tx_sent, iov_cnt = 1;
54 struct kvec iov[2];
56 length = (ISCSI_HDR_LEN + text_length);
58 memset(&iov[0], 0, 2 * sizeof(struct kvec));
59 iov[0].iov_len = ISCSI_HDR_LEN;
60 iov[0].iov_base = pdu_buf;
62 if (text_buf && text_length) {
63 iov[1].iov_len = text_length;
64 iov[1].iov_base = text_buf;
65 iov_cnt++;
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;
75 return 0;
78 void iscsi_dump_conn_ops(struct iscsi_conn_ops *conn_ops)
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);
88 void iscsi_dump_sess_ops(struct iscsi_sess_ops *sess_ops)
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");
118 void iscsi_print_params(struct iscsi_param_list *param_list)
120 struct iscsi_param *param;
122 list_for_each_entry(param, &param_list->param_list, p_list)
123 pr_debug("%s: %s\n", param->name, param->value);
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)
130 struct iscsi_param *param = NULL;
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;
137 INIT_LIST_HEAD(&param->p_list);
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;
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;
151 param->phase = phase;
152 param->scope = scope;
153 param->sender = sender;
154 param->use = use;
155 param->type_range = type_range;
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;
188 list_add_tail(&param->p_list, &param_list->param_list);
190 return param;
191 out:
192 if (param) {
193 kfree(param->value);
194 kfree(param->name);
195 kfree(param);
198 return NULL;
201 /* #warning Add extension keys */
202 int iscsi_create_default_params(struct iscsi_param_list **param_list_ptr)
204 struct iscsi_param *param = NULL;
205 struct iscsi_param_list *pl;
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 -ENOMEM;
213 INIT_LIST_HEAD(&pl->param_list);
214 INIT_LIST_HEAD(&pl->extra_response_list);
217 * The format for setting the initial parameter definitions are:
219 * Parameter name:
220 * Initial value:
221 * Allowable phase:
222 * Scope:
223 * Allowable senders:
224 * Typerange:
225 * Use:
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
412 * Extra parameters for ISER from RFC-5046
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;
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;
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;
434 *param_list_ptr = pl;
435 return 0;
436 out:
437 iscsi_release_param_list(pl);
438 return -1;
441 int iscsi_set_keys_to_negotiate(
442 struct iscsi_param_list *param_list,
443 bool iser)
445 struct iscsi_param *param;
447 param_list->iser = iser;
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);
519 return 0;
522 int iscsi_set_keys_irrelevant_for_discovery(
523 struct iscsi_param_list *param_list)
525 struct iscsi_param *param;
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;
566 return 0;
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)
574 struct iscsi_param *param = NULL;
575 struct iscsi_param *new_param = NULL;
576 struct iscsi_param_list *param_list = NULL;
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 -ENOMEM;
583 INIT_LIST_HEAD(&param_list->param_list);
584 INIT_LIST_HEAD(&param_list->extra_response_list);
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;
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;
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;
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;
618 list_add_tail(&new_param->p_list, &param_list->param_list);
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;
628 return 0;
630 err_out:
631 iscsi_release_param_list(param_list);
632 return -ENOMEM;
635 static void iscsi_release_extra_responses(struct iscsi_param_list *param_list)
637 struct iscsi_extra_response *er, *er_tmp;
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);
646 void iscsi_release_param_list(struct iscsi_param_list *param_list)
648 struct iscsi_param *param, *param_tmp;
650 list_for_each_entry_safe(param, param_tmp, &param_list->param_list,
651 p_list) {
652 list_del(&param->p_list);
654 kfree(param->name);
655 kfree(param->value);
656 kfree(param);
659 iscsi_release_extra_responses(param_list);
661 kfree(param_list);
664 struct iscsi_param *iscsi_find_param_from_key(
665 char *key,
666 struct iscsi_param_list *param_list)
668 struct iscsi_param *param;
670 if (!key || !param_list) {
671 pr_err("Key or parameter list pointer is NULL.\n");
672 return NULL;
675 list_for_each_entry(param, &param_list->param_list, p_list) {
676 if (!strcmp(key, param->name))
677 return param;
680 pr_err("Unable to locate key \"%s\".\n", key);
681 return NULL;
683 EXPORT_SYMBOL(iscsi_find_param_from_key);
685 int iscsi_extract_key_value(char *textbuf, char **key, char **value)
687 *value = strchr(textbuf, '=');
688 if (!*value) {
689 pr_err("Unable to locate \"=\" separator for key,"
690 " ignoring request.\n");
691 return -1;
694 *key = textbuf;
695 **value = '\0';
696 *value = *value + 1;
698 return 0;
701 int iscsi_update_param_value(struct iscsi_param *param, char *value)
703 kfree(param->value);
705 param->value = kstrdup(value, GFP_KERNEL);
706 if (!param->value) {
707 pr_err("Unable to allocate memory for value.\n");
708 return -ENOMEM;
711 pr_debug("iSCSI Parameter updated to %s=%s\n",
712 param->name, param->value);
713 return 0;
716 static int iscsi_add_notunderstood_response(
717 char *key,
718 char *value,
719 struct iscsi_param_list *param_list)
721 struct iscsi_extra_response *extra_response;
723 if (strlen(value) > VALUE_MAXLEN) {
724 pr_err("Value for notunderstood key \"%s\" exceeds %d,"
725 " protocol error.\n", key, VALUE_MAXLEN);
726 return -1;
729 extra_response = kzalloc(sizeof(struct iscsi_extra_response), GFP_KERNEL);
730 if (!extra_response) {
731 pr_err("Unable to allocate memory for"
732 " struct iscsi_extra_response.\n");
733 return -ENOMEM;
735 INIT_LIST_HEAD(&extra_response->er_list);
737 strlcpy(extra_response->key, key, sizeof(extra_response->key));
738 strlcpy(extra_response->value, NOTUNDERSTOOD,
739 sizeof(extra_response->value));
741 list_add_tail(&extra_response->er_list,
742 &param_list->extra_response_list);
743 return 0;
746 static int iscsi_check_for_auth_key(char *key)
749 * RFC 1994
751 if (!strcmp(key, "CHAP_A") || !strcmp(key, "CHAP_I") ||
752 !strcmp(key, "CHAP_C") || !strcmp(key, "CHAP_N") ||
753 !strcmp(key, "CHAP_R"))
754 return 1;
757 * RFC 2945
759 if (!strcmp(key, "SRP_U") || !strcmp(key, "SRP_N") ||
760 !strcmp(key, "SRP_g") || !strcmp(key, "SRP_s") ||
761 !strcmp(key, "SRP_A") || !strcmp(key, "SRP_B") ||
762 !strcmp(key, "SRP_M") || !strcmp(key, "SRP_HM"))
763 return 1;
765 return 0;
768 static void iscsi_check_proposer_for_optional_reply(struct iscsi_param *param)
770 if (IS_TYPE_BOOL_AND(param)) {
771 if (!strcmp(param->value, NO))
772 SET_PSTATE_REPLY_OPTIONAL(param);
773 } else if (IS_TYPE_BOOL_OR(param)) {
774 if (!strcmp(param->value, YES))
775 SET_PSTATE_REPLY_OPTIONAL(param);
777 * Required for gPXE iSCSI boot client
779 if (!strcmp(param->name, IMMEDIATEDATA))
780 SET_PSTATE_REPLY_OPTIONAL(param);
781 } else if (IS_TYPE_NUMBER(param)) {
782 if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH))
783 SET_PSTATE_REPLY_OPTIONAL(param);
785 * Required for gPXE iSCSI boot client
787 if (!strcmp(param->name, MAXCONNECTIONS))
788 SET_PSTATE_REPLY_OPTIONAL(param);
789 } else if (IS_PHASE_DECLARATIVE(param))
790 SET_PSTATE_REPLY_OPTIONAL(param);
793 static int iscsi_check_boolean_value(struct iscsi_param *param, char *value)
795 if (strcmp(value, YES) && strcmp(value, NO)) {
796 pr_err("Illegal value for \"%s\", must be either"
797 " \"%s\" or \"%s\".\n", param->name, YES, NO);
798 return -1;
801 return 0;
804 static int iscsi_check_numerical_value(struct iscsi_param *param, char *value_ptr)
806 char *tmpptr;
807 int value = 0;
809 value = simple_strtoul(value_ptr, &tmpptr, 0);
811 if (IS_TYPERANGE_0_TO_2(param)) {
812 if ((value < 0) || (value > 2)) {
813 pr_err("Illegal value for \"%s\", must be"
814 " between 0 and 2.\n", param->name);
815 return -1;
817 return 0;
819 if (IS_TYPERANGE_0_TO_3600(param)) {
820 if ((value < 0) || (value > 3600)) {
821 pr_err("Illegal value for \"%s\", must be"
822 " between 0 and 3600.\n", param->name);
823 return -1;
825 return 0;
827 if (IS_TYPERANGE_0_TO_32767(param)) {
828 if ((value < 0) || (value > 32767)) {
829 pr_err("Illegal value for \"%s\", must be"
830 " between 0 and 32767.\n", param->name);
831 return -1;
833 return 0;
835 if (IS_TYPERANGE_0_TO_65535(param)) {
836 if ((value < 0) || (value > 65535)) {
837 pr_err("Illegal value for \"%s\", must be"
838 " between 0 and 65535.\n", param->name);
839 return -1;
841 return 0;
843 if (IS_TYPERANGE_1_TO_65535(param)) {
844 if ((value < 1) || (value > 65535)) {
845 pr_err("Illegal value for \"%s\", must be"
846 " between 1 and 65535.\n", param->name);
847 return -1;
849 return 0;
851 if (IS_TYPERANGE_2_TO_3600(param)) {
852 if ((value < 2) || (value > 3600)) {
853 pr_err("Illegal value for \"%s\", must be"
854 " between 2 and 3600.\n", param->name);
855 return -1;
857 return 0;
859 if (IS_TYPERANGE_512_TO_16777215(param)) {
860 if ((value < 512) || (value > 16777215)) {
861 pr_err("Illegal value for \"%s\", must be"
862 " between 512 and 16777215.\n", param->name);
863 return -1;
865 return 0;
868 return 0;
871 static int iscsi_check_string_or_list_value(struct iscsi_param *param, char *value)
873 if (IS_PSTATE_PROPOSER(param))
874 return 0;
876 if (IS_TYPERANGE_AUTH_PARAM(param)) {
877 if (strcmp(value, KRB5) && strcmp(value, SPKM1) &&
878 strcmp(value, SPKM2) && strcmp(value, SRP) &&
879 strcmp(value, CHAP) && strcmp(value, NONE)) {
880 pr_err("Illegal value for \"%s\", must be"
881 " \"%s\", \"%s\", \"%s\", \"%s\", \"%s\""
882 " or \"%s\".\n", param->name, KRB5,
883 SPKM1, SPKM2, SRP, CHAP, NONE);
884 return -1;
887 if (IS_TYPERANGE_DIGEST_PARAM(param)) {
888 if (strcmp(value, CRC32C) && strcmp(value, NONE)) {
889 pr_err("Illegal value for \"%s\", must be"
890 " \"%s\" or \"%s\".\n", param->name,
891 CRC32C, NONE);
892 return -1;
895 if (IS_TYPERANGE_SESSIONTYPE(param)) {
896 if (strcmp(value, DISCOVERY) && strcmp(value, NORMAL)) {
897 pr_err("Illegal value for \"%s\", must be"
898 " \"%s\" or \"%s\".\n", param->name,
899 DISCOVERY, NORMAL);
900 return -1;
904 return 0;
907 static char *iscsi_check_valuelist_for_support(
908 struct iscsi_param *param,
909 char *value)
911 char *tmp1 = NULL, *tmp2 = NULL;
912 char *acceptor_values = NULL, *proposer_values = NULL;
914 acceptor_values = param->value;
915 proposer_values = value;
917 do {
918 if (!proposer_values)
919 return NULL;
920 tmp1 = strchr(proposer_values, ',');
921 if (tmp1)
922 *tmp1 = '\0';
923 acceptor_values = param->value;
924 do {
925 if (!acceptor_values) {
926 if (tmp1)
927 *tmp1 = ',';
928 return NULL;
930 tmp2 = strchr(acceptor_values, ',');
931 if (tmp2)
932 *tmp2 = '\0';
933 if (!strcmp(acceptor_values, proposer_values)) {
934 if (tmp2)
935 *tmp2 = ',';
936 goto out;
938 if (tmp2)
939 *tmp2++ = ',';
941 acceptor_values = tmp2;
942 } while (acceptor_values);
943 if (tmp1)
944 *tmp1++ = ',';
945 proposer_values = tmp1;
946 } while (proposer_values);
948 out:
949 return proposer_values;
952 static int iscsi_check_acceptor_state(struct iscsi_param *param, char *value,
953 struct iscsi_conn *conn)
955 u8 acceptor_boolean_value = 0, proposer_boolean_value = 0;
956 char *negotiated_value = NULL;
958 if (IS_PSTATE_ACCEPTOR(param)) {
959 pr_err("Received key \"%s\" twice, protocol error.\n",
960 param->name);
961 return -1;
964 if (IS_PSTATE_REJECT(param))
965 return 0;
967 if (IS_TYPE_BOOL_AND(param)) {
968 if (!strcmp(value, YES))
969 proposer_boolean_value = 1;
970 if (!strcmp(param->value, YES))
971 acceptor_boolean_value = 1;
972 if (acceptor_boolean_value && proposer_boolean_value)
973 do {} while (0);
974 else {
975 if (iscsi_update_param_value(param, NO) < 0)
976 return -1;
977 if (!proposer_boolean_value)
978 SET_PSTATE_REPLY_OPTIONAL(param);
980 } else if (IS_TYPE_BOOL_OR(param)) {
981 if (!strcmp(value, YES))
982 proposer_boolean_value = 1;
983 if (!strcmp(param->value, YES))
984 acceptor_boolean_value = 1;
985 if (acceptor_boolean_value || proposer_boolean_value) {
986 if (iscsi_update_param_value(param, YES) < 0)
987 return -1;
988 if (proposer_boolean_value)
989 SET_PSTATE_REPLY_OPTIONAL(param);
991 } else if (IS_TYPE_NUMBER(param)) {
992 char *tmpptr, buf[11];
993 u32 acceptor_value = simple_strtoul(param->value, &tmpptr, 0);
994 u32 proposer_value = simple_strtoul(value, &tmpptr, 0);
996 memset(buf, 0, sizeof(buf));
998 if (!strcmp(param->name, MAXCONNECTIONS) ||
999 !strcmp(param->name, MAXBURSTLENGTH) ||
1000 !strcmp(param->name, FIRSTBURSTLENGTH) ||
1001 !strcmp(param->name, MAXOUTSTANDINGR2T) ||
1002 !strcmp(param->name, DEFAULTTIME2RETAIN) ||
1003 !strcmp(param->name, ERRORRECOVERYLEVEL)) {
1004 if (proposer_value > acceptor_value) {
1005 sprintf(buf, "%u", acceptor_value);
1006 if (iscsi_update_param_value(param,
1007 &buf[0]) < 0)
1008 return -1;
1009 } else {
1010 if (iscsi_update_param_value(param, value) < 0)
1011 return -1;
1013 } else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
1014 if (acceptor_value > proposer_value) {
1015 sprintf(buf, "%u", acceptor_value);
1016 if (iscsi_update_param_value(param,
1017 &buf[0]) < 0)
1018 return -1;
1019 } else {
1020 if (iscsi_update_param_value(param, value) < 0)
1021 return -1;
1023 } else {
1024 if (iscsi_update_param_value(param, value) < 0)
1025 return -1;
1028 if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
1029 struct iscsi_param *param_mxdsl;
1030 unsigned long long tmp;
1031 int rc;
1033 rc = kstrtoull(param->value, 0, &tmp);
1034 if (rc < 0)
1035 return -1;
1037 conn->conn_ops->MaxRecvDataSegmentLength = tmp;
1038 pr_debug("Saving op->MaxRecvDataSegmentLength from"
1039 " original initiator received value: %u\n",
1040 conn->conn_ops->MaxRecvDataSegmentLength);
1042 param_mxdsl = iscsi_find_param_from_key(
1043 MAXXMITDATASEGMENTLENGTH,
1044 conn->param_list);
1045 if (!param_mxdsl)
1046 return -1;
1048 rc = iscsi_update_param_value(param,
1049 param_mxdsl->value);
1050 if (rc < 0)
1051 return -1;
1053 pr_debug("Updated %s to target MXDSL value: %s\n",
1054 param->name, param->value);
1056 } else if (IS_TYPE_VALUE_LIST(param)) {
1057 negotiated_value = iscsi_check_valuelist_for_support(
1058 param, value);
1059 if (!negotiated_value) {
1060 pr_err("Proposer's value list \"%s\" contains"
1061 " no valid values from Acceptor's value list"
1062 " \"%s\".\n", value, param->value);
1063 return -1;
1065 if (iscsi_update_param_value(param, negotiated_value) < 0)
1066 return -1;
1067 } else if (IS_PHASE_DECLARATIVE(param)) {
1068 if (iscsi_update_param_value(param, value) < 0)
1069 return -1;
1070 SET_PSTATE_REPLY_OPTIONAL(param);
1073 return 0;
1076 static int iscsi_check_proposer_state(struct iscsi_param *param, char *value)
1078 if (IS_PSTATE_RESPONSE_GOT(param)) {
1079 pr_err("Received key \"%s\" twice, protocol error.\n",
1080 param->name);
1081 return -1;
1084 if (IS_TYPE_VALUE_LIST(param)) {
1085 char *comma_ptr = NULL, *tmp_ptr = NULL;
1087 comma_ptr = strchr(value, ',');
1088 if (comma_ptr) {
1089 pr_err("Illegal \",\" in response for \"%s\".\n",
1090 param->name);
1091 return -1;
1094 tmp_ptr = iscsi_check_valuelist_for_support(param, value);
1095 if (!tmp_ptr)
1096 return -1;
1099 if (iscsi_update_param_value(param, value) < 0)
1100 return -1;
1102 return 0;
1105 static int iscsi_check_value(struct iscsi_param *param, char *value)
1107 char *comma_ptr = NULL;
1109 if (!strcmp(value, REJECT)) {
1110 if (!strcmp(param->name, IFMARKINT) ||
1111 !strcmp(param->name, OFMARKINT)) {
1113 * Reject is not fatal for [I,O]FMarkInt, and causes
1114 * [I,O]FMarker to be reset to No. (See iSCSI v20 A.3.2)
1116 SET_PSTATE_REJECT(param);
1117 return 0;
1119 pr_err("Received %s=%s\n", param->name, value);
1120 return -1;
1122 if (!strcmp(value, IRRELEVANT)) {
1123 pr_debug("Received %s=%s\n", param->name, value);
1124 SET_PSTATE_IRRELEVANT(param);
1125 return 0;
1127 if (!strcmp(value, NOTUNDERSTOOD)) {
1128 if (!IS_PSTATE_PROPOSER(param)) {
1129 pr_err("Received illegal offer %s=%s\n",
1130 param->name, value);
1131 return -1;
1134 /* #warning FIXME: Add check for X-ExtensionKey here */
1135 pr_err("Standard iSCSI key \"%s\" cannot be answered"
1136 " with \"%s\", protocol error.\n", param->name, value);
1137 return -1;
1140 do {
1141 comma_ptr = NULL;
1142 comma_ptr = strchr(value, ',');
1144 if (comma_ptr && !IS_TYPE_VALUE_LIST(param)) {
1145 pr_err("Detected value separator \",\", but"
1146 " key \"%s\" does not allow a value list,"
1147 " protocol error.\n", param->name);
1148 return -1;
1150 if (comma_ptr)
1151 *comma_ptr = '\0';
1153 if (strlen(value) > VALUE_MAXLEN) {
1154 pr_err("Value for key \"%s\" exceeds %d,"
1155 " protocol error.\n", param->name,
1156 VALUE_MAXLEN);
1157 return -1;
1160 if (IS_TYPE_BOOL_AND(param) || IS_TYPE_BOOL_OR(param)) {
1161 if (iscsi_check_boolean_value(param, value) < 0)
1162 return -1;
1163 } else if (IS_TYPE_NUMBER(param)) {
1164 if (iscsi_check_numerical_value(param, value) < 0)
1165 return -1;
1166 } else if (IS_TYPE_STRING(param) || IS_TYPE_VALUE_LIST(param)) {
1167 if (iscsi_check_string_or_list_value(param, value) < 0)
1168 return -1;
1169 } else {
1170 pr_err("Huh? 0x%02x\n", param->type);
1171 return -1;
1174 if (comma_ptr)
1175 *comma_ptr++ = ',';
1177 value = comma_ptr;
1178 } while (value);
1180 return 0;
1183 static struct iscsi_param *__iscsi_check_key(
1184 char *key,
1185 int sender,
1186 struct iscsi_param_list *param_list)
1188 struct iscsi_param *param;
1190 if (strlen(key) > KEY_MAXLEN) {
1191 pr_err("Length of key name \"%s\" exceeds %d.\n",
1192 key, KEY_MAXLEN);
1193 return NULL;
1196 param = iscsi_find_param_from_key(key, param_list);
1197 if (!param)
1198 return NULL;
1200 if ((sender & SENDER_INITIATOR) && !IS_SENDER_INITIATOR(param)) {
1201 pr_err("Key \"%s\" may not be sent to %s,"
1202 " protocol error.\n", param->name,
1203 (sender & SENDER_RECEIVER) ? "target" : "initiator");
1204 return NULL;
1207 if ((sender & SENDER_TARGET) && !IS_SENDER_TARGET(param)) {
1208 pr_err("Key \"%s\" may not be sent to %s,"
1209 " protocol error.\n", param->name,
1210 (sender & SENDER_RECEIVER) ? "initiator" : "target");
1211 return NULL;
1214 return param;
1217 static struct iscsi_param *iscsi_check_key(
1218 char *key,
1219 int phase,
1220 int sender,
1221 struct iscsi_param_list *param_list)
1223 struct iscsi_param *param;
1225 * Key name length must not exceed 63 bytes. (See iSCSI v20 5.1)
1227 if (strlen(key) > KEY_MAXLEN) {
1228 pr_err("Length of key name \"%s\" exceeds %d.\n",
1229 key, KEY_MAXLEN);
1230 return NULL;
1233 param = iscsi_find_param_from_key(key, param_list);
1234 if (!param)
1235 return NULL;
1237 if ((sender & SENDER_INITIATOR) && !IS_SENDER_INITIATOR(param)) {
1238 pr_err("Key \"%s\" may not be sent to %s,"
1239 " protocol error.\n", param->name,
1240 (sender & SENDER_RECEIVER) ? "target" : "initiator");
1241 return NULL;
1243 if ((sender & SENDER_TARGET) && !IS_SENDER_TARGET(param)) {
1244 pr_err("Key \"%s\" may not be sent to %s,"
1245 " protocol error.\n", param->name,
1246 (sender & SENDER_RECEIVER) ? "initiator" : "target");
1247 return NULL;
1250 if (IS_PSTATE_ACCEPTOR(param)) {
1251 pr_err("Key \"%s\" received twice, protocol error.\n",
1252 key);
1253 return NULL;
1256 if (!phase)
1257 return param;
1259 if (!(param->phase & phase)) {
1260 pr_err("Key \"%s\" may not be negotiated during ",
1261 param->name);
1262 switch (phase) {
1263 case PHASE_SECURITY:
1264 pr_debug("Security phase.\n");
1265 break;
1266 case PHASE_OPERATIONAL:
1267 pr_debug("Operational phase.\n");
1268 break;
1269 default:
1270 pr_debug("Unknown phase.\n");
1272 return NULL;
1275 return param;
1278 static int iscsi_enforce_integrity_rules(
1279 u8 phase,
1280 struct iscsi_param_list *param_list)
1282 char *tmpptr;
1283 u8 DataSequenceInOrder = 0;
1284 u8 ErrorRecoveryLevel = 0, SessionType = 0;
1285 u32 FirstBurstLength = 0, MaxBurstLength = 0;
1286 struct iscsi_param *param = NULL;
1288 list_for_each_entry(param, &param_list->param_list, p_list) {
1289 if (!(param->phase & phase))
1290 continue;
1291 if (!strcmp(param->name, SESSIONTYPE))
1292 if (!strcmp(param->value, NORMAL))
1293 SessionType = 1;
1294 if (!strcmp(param->name, ERRORRECOVERYLEVEL))
1295 ErrorRecoveryLevel = simple_strtoul(param->value,
1296 &tmpptr, 0);
1297 if (!strcmp(param->name, DATASEQUENCEINORDER))
1298 if (!strcmp(param->value, YES))
1299 DataSequenceInOrder = 1;
1300 if (!strcmp(param->name, MAXBURSTLENGTH))
1301 MaxBurstLength = simple_strtoul(param->value,
1302 &tmpptr, 0);
1305 list_for_each_entry(param, &param_list->param_list, p_list) {
1306 if (!(param->phase & phase))
1307 continue;
1308 if (!SessionType && !IS_PSTATE_ACCEPTOR(param))
1309 continue;
1310 if (!strcmp(param->name, MAXOUTSTANDINGR2T) &&
1311 DataSequenceInOrder && (ErrorRecoveryLevel > 0)) {
1312 if (strcmp(param->value, "1")) {
1313 if (iscsi_update_param_value(param, "1") < 0)
1314 return -1;
1315 pr_debug("Reset \"%s\" to \"%s\".\n",
1316 param->name, param->value);
1319 if (!strcmp(param->name, MAXCONNECTIONS) && !SessionType) {
1320 if (strcmp(param->value, "1")) {
1321 if (iscsi_update_param_value(param, "1") < 0)
1322 return -1;
1323 pr_debug("Reset \"%s\" to \"%s\".\n",
1324 param->name, param->value);
1327 if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
1328 FirstBurstLength = simple_strtoul(param->value,
1329 &tmpptr, 0);
1330 if (FirstBurstLength > MaxBurstLength) {
1331 char tmpbuf[11];
1332 memset(tmpbuf, 0, sizeof(tmpbuf));
1333 sprintf(tmpbuf, "%u", MaxBurstLength);
1334 if (iscsi_update_param_value(param, tmpbuf))
1335 return -1;
1336 pr_debug("Reset \"%s\" to \"%s\".\n",
1337 param->name, param->value);
1342 return 0;
1345 int iscsi_decode_text_input(
1346 u8 phase,
1347 u8 sender,
1348 char *textbuf,
1349 u32 length,
1350 struct iscsi_conn *conn)
1352 struct iscsi_param_list *param_list = conn->param_list;
1353 char *tmpbuf, *start = NULL, *end = NULL;
1355 tmpbuf = kzalloc(length + 1, GFP_KERNEL);
1356 if (!tmpbuf) {
1357 pr_err("Unable to allocate %u + 1 bytes for tmpbuf.\n", length);
1358 return -ENOMEM;
1361 memcpy(tmpbuf, textbuf, length);
1362 tmpbuf[length] = '\0';
1363 start = tmpbuf;
1364 end = (start + length);
1366 while (start < end) {
1367 char *key, *value;
1368 struct iscsi_param *param;
1370 if (iscsi_extract_key_value(start, &key, &value) < 0) {
1371 kfree(tmpbuf);
1372 return -1;
1375 pr_debug("Got key: %s=%s\n", key, value);
1377 if (phase & PHASE_SECURITY) {
1378 if (iscsi_check_for_auth_key(key) > 0) {
1379 kfree(tmpbuf);
1380 return 1;
1384 param = iscsi_check_key(key, phase, sender, param_list);
1385 if (!param) {
1386 if (iscsi_add_notunderstood_response(key,
1387 value, param_list) < 0) {
1388 kfree(tmpbuf);
1389 return -1;
1391 start += strlen(key) + strlen(value) + 2;
1392 continue;
1394 if (iscsi_check_value(param, value) < 0) {
1395 kfree(tmpbuf);
1396 return -1;
1399 start += strlen(key) + strlen(value) + 2;
1401 if (IS_PSTATE_PROPOSER(param)) {
1402 if (iscsi_check_proposer_state(param, value) < 0) {
1403 kfree(tmpbuf);
1404 return -1;
1406 SET_PSTATE_RESPONSE_GOT(param);
1407 } else {
1408 if (iscsi_check_acceptor_state(param, value, conn) < 0) {
1409 kfree(tmpbuf);
1410 return -1;
1412 SET_PSTATE_ACCEPTOR(param);
1416 kfree(tmpbuf);
1417 return 0;
1420 int iscsi_encode_text_output(
1421 u8 phase,
1422 u8 sender,
1423 char *textbuf,
1424 u32 *length,
1425 struct iscsi_param_list *param_list)
1427 char *output_buf = NULL;
1428 struct iscsi_extra_response *er;
1429 struct iscsi_param *param;
1431 output_buf = textbuf + *length;
1433 if (iscsi_enforce_integrity_rules(phase, param_list) < 0)
1434 return -1;
1436 list_for_each_entry(param, &param_list->param_list, p_list) {
1437 if (!(param->sender & sender))
1438 continue;
1439 if (IS_PSTATE_ACCEPTOR(param) &&
1440 !IS_PSTATE_RESPONSE_SENT(param) &&
1441 !IS_PSTATE_REPLY_OPTIONAL(param) &&
1442 (param->phase & phase)) {
1443 *length += sprintf(output_buf, "%s=%s",
1444 param->name, param->value);
1445 *length += 1;
1446 output_buf = textbuf + *length;
1447 SET_PSTATE_RESPONSE_SENT(param);
1448 pr_debug("Sending key: %s=%s\n",
1449 param->name, param->value);
1450 continue;
1452 if (IS_PSTATE_NEGOTIATE(param) &&
1453 !IS_PSTATE_ACCEPTOR(param) &&
1454 !IS_PSTATE_PROPOSER(param) &&
1455 (param->phase & phase)) {
1456 *length += sprintf(output_buf, "%s=%s",
1457 param->name, param->value);
1458 *length += 1;
1459 output_buf = textbuf + *length;
1460 SET_PSTATE_PROPOSER(param);
1461 iscsi_check_proposer_for_optional_reply(param);
1462 pr_debug("Sending key: %s=%s\n",
1463 param->name, param->value);
1467 list_for_each_entry(er, &param_list->extra_response_list, er_list) {
1468 *length += sprintf(output_buf, "%s=%s", er->key, er->value);
1469 *length += 1;
1470 output_buf = textbuf + *length;
1471 pr_debug("Sending key: %s=%s\n", er->key, er->value);
1473 iscsi_release_extra_responses(param_list);
1475 return 0;
1478 int iscsi_check_negotiated_keys(struct iscsi_param_list *param_list)
1480 int ret = 0;
1481 struct iscsi_param *param;
1483 list_for_each_entry(param, &param_list->param_list, p_list) {
1484 if (IS_PSTATE_NEGOTIATE(param) &&
1485 IS_PSTATE_PROPOSER(param) &&
1486 !IS_PSTATE_RESPONSE_GOT(param) &&
1487 !IS_PSTATE_REPLY_OPTIONAL(param) &&
1488 !IS_PHASE_DECLARATIVE(param)) {
1489 pr_err("No response for proposed key \"%s\".\n",
1490 param->name);
1491 ret = -1;
1495 return ret;
1498 int iscsi_change_param_value(
1499 char *keyvalue,
1500 struct iscsi_param_list *param_list,
1501 int check_key)
1503 char *key = NULL, *value = NULL;
1504 struct iscsi_param *param;
1505 int sender = 0;
1507 if (iscsi_extract_key_value(keyvalue, &key, &value) < 0)
1508 return -1;
1510 if (!check_key) {
1511 param = __iscsi_check_key(keyvalue, sender, param_list);
1512 if (!param)
1513 return -1;
1514 } else {
1515 param = iscsi_check_key(keyvalue, 0, sender, param_list);
1516 if (!param)
1517 return -1;
1519 param->set_param = 1;
1520 if (iscsi_check_value(param, value) < 0) {
1521 param->set_param = 0;
1522 return -1;
1524 param->set_param = 0;
1527 if (iscsi_update_param_value(param, value) < 0)
1528 return -1;
1530 return 0;
1533 void iscsi_set_connection_parameters(
1534 struct iscsi_conn_ops *ops,
1535 struct iscsi_param_list *param_list)
1537 char *tmpptr;
1538 struct iscsi_param *param;
1540 pr_debug("---------------------------------------------------"
1541 "---------------\n");
1542 list_for_each_entry(param, &param_list->param_list, p_list) {
1544 * Special case to set MAXXMITDATASEGMENTLENGTH from the
1545 * target requested MaxRecvDataSegmentLength, even though
1546 * this key is not sent over the wire.
1548 if (!strcmp(param->name, MAXXMITDATASEGMENTLENGTH)) {
1549 ops->MaxXmitDataSegmentLength =
1550 simple_strtoul(param->value, &tmpptr, 0);
1551 pr_debug("MaxXmitDataSegmentLength: %s\n",
1552 param->value);
1555 if (!IS_PSTATE_ACCEPTOR(param) && !IS_PSTATE_PROPOSER(param))
1556 continue;
1557 if (!strcmp(param->name, AUTHMETHOD)) {
1558 pr_debug("AuthMethod: %s\n",
1559 param->value);
1560 } else if (!strcmp(param->name, HEADERDIGEST)) {
1561 ops->HeaderDigest = !strcmp(param->value, CRC32C);
1562 pr_debug("HeaderDigest: %s\n",
1563 param->value);
1564 } else if (!strcmp(param->name, DATADIGEST)) {
1565 ops->DataDigest = !strcmp(param->value, CRC32C);
1566 pr_debug("DataDigest: %s\n",
1567 param->value);
1568 } else if (!strcmp(param->name, MAXRECVDATASEGMENTLENGTH)) {
1570 * At this point iscsi_check_acceptor_state() will have
1571 * set ops->MaxRecvDataSegmentLength from the original
1572 * initiator provided value.
1574 pr_debug("MaxRecvDataSegmentLength: %u\n",
1575 ops->MaxRecvDataSegmentLength);
1576 } else if (!strcmp(param->name, INITIATORRECVDATASEGMENTLENGTH)) {
1577 ops->InitiatorRecvDataSegmentLength =
1578 simple_strtoul(param->value, &tmpptr, 0);
1579 pr_debug("InitiatorRecvDataSegmentLength: %s\n",
1580 param->value);
1581 ops->MaxRecvDataSegmentLength =
1582 ops->InitiatorRecvDataSegmentLength;
1583 pr_debug("Set MRDSL from InitiatorRecvDataSegmentLength\n");
1584 } else if (!strcmp(param->name, TARGETRECVDATASEGMENTLENGTH)) {
1585 ops->TargetRecvDataSegmentLength =
1586 simple_strtoul(param->value, &tmpptr, 0);
1587 pr_debug("TargetRecvDataSegmentLength: %s\n",
1588 param->value);
1589 ops->MaxXmitDataSegmentLength =
1590 ops->TargetRecvDataSegmentLength;
1591 pr_debug("Set MXDSL from TargetRecvDataSegmentLength\n");
1594 pr_debug("----------------------------------------------------"
1595 "--------------\n");
1598 void iscsi_set_session_parameters(
1599 struct iscsi_sess_ops *ops,
1600 struct iscsi_param_list *param_list,
1601 int leading)
1603 char *tmpptr;
1604 struct iscsi_param *param;
1606 pr_debug("----------------------------------------------------"
1607 "--------------\n");
1608 list_for_each_entry(param, &param_list->param_list, p_list) {
1609 if (!IS_PSTATE_ACCEPTOR(param) && !IS_PSTATE_PROPOSER(param))
1610 continue;
1611 if (!strcmp(param->name, INITIATORNAME)) {
1612 if (!param->value)
1613 continue;
1614 if (leading)
1615 snprintf(ops->InitiatorName,
1616 sizeof(ops->InitiatorName),
1617 "%s", param->value);
1618 pr_debug("InitiatorName: %s\n",
1619 param->value);
1620 } else if (!strcmp(param->name, INITIATORALIAS)) {
1621 if (!param->value)
1622 continue;
1623 snprintf(ops->InitiatorAlias,
1624 sizeof(ops->InitiatorAlias),
1625 "%s", param->value);
1626 pr_debug("InitiatorAlias: %s\n",
1627 param->value);
1628 } else if (!strcmp(param->name, TARGETNAME)) {
1629 if (!param->value)
1630 continue;
1631 if (leading)
1632 snprintf(ops->TargetName,
1633 sizeof(ops->TargetName),
1634 "%s", param->value);
1635 pr_debug("TargetName: %s\n",
1636 param->value);
1637 } else if (!strcmp(param->name, TARGETALIAS)) {
1638 if (!param->value)
1639 continue;
1640 snprintf(ops->TargetAlias, sizeof(ops->TargetAlias),
1641 "%s", param->value);
1642 pr_debug("TargetAlias: %s\n",
1643 param->value);
1644 } else if (!strcmp(param->name, TARGETPORTALGROUPTAG)) {
1645 ops->TargetPortalGroupTag =
1646 simple_strtoul(param->value, &tmpptr, 0);
1647 pr_debug("TargetPortalGroupTag: %s\n",
1648 param->value);
1649 } else if (!strcmp(param->name, MAXCONNECTIONS)) {
1650 ops->MaxConnections =
1651 simple_strtoul(param->value, &tmpptr, 0);
1652 pr_debug("MaxConnections: %s\n",
1653 param->value);
1654 } else if (!strcmp(param->name, INITIALR2T)) {
1655 ops->InitialR2T = !strcmp(param->value, YES);
1656 pr_debug("InitialR2T: %s\n",
1657 param->value);
1658 } else if (!strcmp(param->name, IMMEDIATEDATA)) {
1659 ops->ImmediateData = !strcmp(param->value, YES);
1660 pr_debug("ImmediateData: %s\n",
1661 param->value);
1662 } else if (!strcmp(param->name, MAXBURSTLENGTH)) {
1663 ops->MaxBurstLength =
1664 simple_strtoul(param->value, &tmpptr, 0);
1665 pr_debug("MaxBurstLength: %s\n",
1666 param->value);
1667 } else if (!strcmp(param->name, FIRSTBURSTLENGTH)) {
1668 ops->FirstBurstLength =
1669 simple_strtoul(param->value, &tmpptr, 0);
1670 pr_debug("FirstBurstLength: %s\n",
1671 param->value);
1672 } else if (!strcmp(param->name, DEFAULTTIME2WAIT)) {
1673 ops->DefaultTime2Wait =
1674 simple_strtoul(param->value, &tmpptr, 0);
1675 pr_debug("DefaultTime2Wait: %s\n",
1676 param->value);
1677 } else if (!strcmp(param->name, DEFAULTTIME2RETAIN)) {
1678 ops->DefaultTime2Retain =
1679 simple_strtoul(param->value, &tmpptr, 0);
1680 pr_debug("DefaultTime2Retain: %s\n",
1681 param->value);
1682 } else if (!strcmp(param->name, MAXOUTSTANDINGR2T)) {
1683 ops->MaxOutstandingR2T =
1684 simple_strtoul(param->value, &tmpptr, 0);
1685 pr_debug("MaxOutstandingR2T: %s\n",
1686 param->value);
1687 } else if (!strcmp(param->name, DATAPDUINORDER)) {
1688 ops->DataPDUInOrder = !strcmp(param->value, YES);
1689 pr_debug("DataPDUInOrder: %s\n",
1690 param->value);
1691 } else if (!strcmp(param->name, DATASEQUENCEINORDER)) {
1692 ops->DataSequenceInOrder = !strcmp(param->value, YES);
1693 pr_debug("DataSequenceInOrder: %s\n",
1694 param->value);
1695 } else if (!strcmp(param->name, ERRORRECOVERYLEVEL)) {
1696 ops->ErrorRecoveryLevel =
1697 simple_strtoul(param->value, &tmpptr, 0);
1698 pr_debug("ErrorRecoveryLevel: %s\n",
1699 param->value);
1700 } else if (!strcmp(param->name, SESSIONTYPE)) {
1701 ops->SessionType = !strcmp(param->value, DISCOVERY);
1702 pr_debug("SessionType: %s\n",
1703 param->value);
1704 } else if (!strcmp(param->name, RDMAEXTENSIONS)) {
1705 ops->RDMAExtensions = !strcmp(param->value, YES);
1706 pr_debug("RDMAExtensions: %s\n",
1707 param->value);
1710 pr_debug("----------------------------------------------------"
1711 "--------------\n");