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