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