Home | History | Annotate | Line # | Download | only in iscsi
iscsi_text.c revision 1.12
      1  1.12   mlelstv /*	$NetBSD: iscsi_text.c,v 1.12 2019/04/21 11:26:46 mlelstv Exp $	*/
      2   1.1       agc 
      3   1.1       agc /*-
      4   1.1       agc  * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc.
      5   1.1       agc  * All rights reserved.
      6   1.1       agc  *
      7   1.1       agc  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1       agc  * by Wasabi Systems, Inc.
      9   1.1       agc  *
     10   1.1       agc  * Redistribution and use in source and binary forms, with or without
     11   1.1       agc  * modification, are permitted provided that the following conditions
     12   1.1       agc  * are met:
     13   1.1       agc  * 1. Redistributions of source code must retain the above copyright
     14   1.1       agc  *    notice, this list of conditions and the following disclaimer.
     15   1.1       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       agc  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       agc  *    documentation and/or other materials provided with the distribution.
     18   1.1       agc  *
     19   1.1       agc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1       agc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1       agc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1       agc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1       agc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1       agc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1       agc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1       agc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1       agc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1       agc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1       agc  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       agc  */
     31   1.1       agc 
     32   1.1       agc #include "iscsi_globals.h"
     33   1.1       agc #include "base64.h"
     34   1.1       agc #include <sys/md5.h>
     35   1.2       tls #include <sys/cprng.h>
     36   1.1       agc 
     37   1.1       agc #define isdigit(x) ((x) >= '0' && (x) <= '9')
     38   1.1       agc #define toupper(x) ((x) & ~0x20)
     39   1.1       agc 
     40   1.1       agc /*****************************************************************************/
     41   1.1       agc 
     42   1.1       agc #define MAX_STRING   255	/* Maximum length of parameter value */
     43   1.1       agc #define MAX_LIST     4		/* Maximum number of list elements we'll ever send */
     44   1.1       agc 
     45   1.1       agc /* Maximum number of negotiation parameters in the operational negotiation phase */
     46   1.1       agc /* 48 should be more than enough even with the target defining its own keys */
     47   1.1       agc #define MAX_NEG      48
     48   1.1       agc 
     49   1.1       agc #define CHAP_CHALLENGE_LEN    32	/* Number of bytes to send in challenge */
     50   1.1       agc #define CHAP_MD5_SIZE         16	/* Number of bytes in MD5 hash */
     51   1.1       agc 
     52   1.1       agc /*****************************************************************************/
     53   1.1       agc 
     54   1.1       agc /* authentication states */
     55   1.1       agc 
     56   1.1       agc typedef enum
     57   1.1       agc {
     58   1.1       agc 	AUTH_INITIAL,				/* sending choice of algorithms */
     59   1.1       agc 	AUTH_METHOD_SELECTED,		/* received choice, sending first parameter */
     60   1.1       agc 	/* from here it's alg dependent */
     61   1.1       agc 	AUTH_CHAP_ALG_SENT,			/* CHAP: Algorithm selected */
     62   1.1       agc 	AUTH_CHAP_RSP_SENT,			/* CHAP: Response sent */
     63   1.1       agc 	/* for all algorithms */
     64   1.1       agc 	AUTH_DONE					/* in parameter negotiation stage */
     65   1.1       agc } auth_state_t;
     66   1.1       agc 
     67   1.1       agc 
     68   1.1       agc /* enumeration of all the keys we know, and a place for the ones we don't */
     69   1.1       agc 
     70   1.1       agc typedef enum
     71   1.1       agc {
     72   1.1       agc 	K_AuthMethod,
     73   1.1       agc 	K_Auth_CHAP_Algorithm,
     74   1.1       agc 	K_Auth_CHAP_Challenge,
     75   1.1       agc 	K_Auth_CHAP_Identifier,
     76   1.1       agc 	K_Auth_CHAP_Name,
     77   1.1       agc 	K_Auth_CHAP_Response,
     78   1.1       agc 	K_DataDigest,
     79   1.1       agc 	K_DataPDUInOrder,
     80   1.1       agc 	K_DataSequenceInOrder,
     81   1.1       agc 	K_DefaultTime2Retain,
     82   1.1       agc 	K_DefaultTime2Wait,
     83   1.1       agc 	K_ErrorRecoveryLevel,
     84   1.1       agc 	K_FirstBurstLength,
     85   1.1       agc 	K_HeaderDigest,
     86   1.1       agc 	K_IFMarker,
     87   1.1       agc 	K_IFMarkInt,
     88   1.1       agc 	K_ImmediateData,
     89   1.1       agc 	K_InitialR2T,
     90   1.1       agc 	K_InitiatorAlias,
     91   1.1       agc 	K_InitiatorName,
     92   1.1       agc 	K_MaxBurstLength,
     93   1.1       agc 	K_MaxConnections,
     94   1.1       agc 	K_MaxOutstandingR2T,
     95   1.1       agc 	K_MaxRecvDataSegmentLength,
     96   1.1       agc 	K_OFMarker,
     97   1.1       agc 	K_OFMarkInt,
     98   1.1       agc 	K_SendTargets,
     99   1.1       agc 	K_SessionType,
    100   1.1       agc 	K_TargetAddress,
    101   1.1       agc 	K_TargetAlias,
    102   1.1       agc 	K_TargetName,
    103   1.1       agc 	K_TargetPortalGroupTag,
    104   1.1       agc 	K_NotUnderstood
    105   1.1       agc } text_key_t;
    106   1.1       agc 
    107   1.1       agc /* maximum known key */
    108   1.1       agc #define MAX_KEY   K_TargetPortalGroupTag
    109   1.1       agc 
    110   1.1       agc /* value types */
    111   1.1       agc typedef enum
    112   1.1       agc {						/* Value is... */
    113   1.1       agc 	T_NUM,					/* numeric */
    114   1.1       agc 	T_BIGNUM,				/* large numeric */
    115   1.1       agc 	T_STRING,				/* string */
    116   1.1       agc 	T_YESNO,				/* boolean (Yes or No) */
    117   1.1       agc 	T_AUTH,					/* authentication type (CHAP or None for now) */
    118   1.1       agc 	T_DIGEST,				/* digest (None or CRC32C) */
    119   1.1       agc 	T_RANGE,				/* numeric range */
    120   1.1       agc 	T_SENDT,				/* send target options (ALL, target-name, empty) */
    121   1.1       agc 	T_SESS					/* session type (Discovery or Normal) */
    122   1.1       agc } val_kind_t;
    123   1.1       agc 
    124   1.1       agc 
    125   1.1       agc /* table of negotiation key strings with value type and default */
    126   1.1       agc 
    127   1.1       agc typedef struct
    128   1.1       agc {
    129   1.1       agc 	const uint8_t *name;				/* the key name */
    130   1.1       agc 	val_kind_t val;				/* the value type */
    131   1.1       agc 	uint32_t defval;			/* default value */
    132   1.1       agc } key_entry_t;
    133   1.1       agc 
    134   1.1       agc STATIC key_entry_t entries[] = {
    135   1.1       agc 	{"AuthMethod", T_AUTH, 0},
    136   1.1       agc 	{"CHAP_A", T_NUM, 5},
    137   1.1       agc 	{"CHAP_C", T_BIGNUM, 0},
    138   1.1       agc 	{"CHAP_I", T_NUM, 0},
    139   1.1       agc 	{"CHAP_N", T_STRING, 0},
    140   1.1       agc 	{"CHAP_R", T_BIGNUM, 0},
    141   1.1       agc 	{"DataDigest", T_DIGEST, 0},
    142   1.1       agc 	{"DataPDUInOrder", T_YESNO, 1},
    143   1.1       agc 	{"DataSequenceInOrder", T_YESNO, 1},
    144   1.1       agc 	{"DefaultTime2Retain", T_NUM, 20},
    145   1.1       agc 	{"DefaultTime2Wait", T_NUM, 2},
    146   1.1       agc 	{"ErrorRecoveryLevel", T_NUM, 0},
    147   1.1       agc 	{"FirstBurstLength", T_NUM, 64 * 1024},
    148   1.1       agc 	{"HeaderDigest", T_DIGEST, 0},
    149   1.1       agc 	{"IFMarker", T_YESNO, 0},
    150   1.1       agc 	{"IFMarkInt", T_RANGE, 2048},
    151   1.1       agc 	{"ImmediateData", T_YESNO, 1},
    152   1.1       agc 	{"InitialR2T", T_YESNO, 1},
    153   1.1       agc 	{"InitiatorAlias", T_STRING, 0},
    154   1.1       agc 	{"InitiatorName", T_STRING, 0},
    155   1.1       agc 	{"MaxBurstLength", T_NUM, 256 * 1024},
    156   1.1       agc 	{"MaxConnections", T_NUM, 1},
    157   1.1       agc 	{"MaxOutstandingR2T", T_NUM, 1},
    158   1.1       agc 	{"MaxRecvDataSegmentLength", T_NUM, 8192},
    159   1.1       agc 	{"OFMarker", T_YESNO, 0},
    160   1.1       agc 	{"OFMarkInt", T_RANGE, 2048},
    161   1.1       agc 	{"SendTargets", T_SENDT, 0},
    162   1.1       agc 	{"SessionType", T_SESS, 0},
    163   1.1       agc 	{"TargetAddress", T_STRING, 0},
    164   1.1       agc 	{"TargetAlias", T_STRING, 0},
    165   1.1       agc 	{"TargetName", T_STRING, 0},
    166   1.1       agc 	{"TargetPortalGroupTag", T_NUM, 0},
    167   1.1       agc 	{NULL, T_STRING, 0}
    168   1.1       agc };
    169   1.1       agc 
    170   1.1       agc /* a negotiation parameter: key and values (there may be more than 1 for lists) */
    171   1.1       agc typedef struct
    172   1.1       agc {
    173   1.1       agc 	text_key_t key;				/* the key */
    174   1.1       agc 	int list_num;				/* number of elements in list, doubles as */
    175  1.12   mlelstv 	bool hex_bignums;			/* wether to encode in hex or base64 */
    176   1.1       agc 	/* data size for large numeric values */
    177   1.1       agc 	union
    178   1.1       agc 	{
    179   1.1       agc 		uint32_t nval[MAX_LIST];	/* numeric or enumeration values */
    180   1.1       agc 		uint8_t *sval;				/* string or data pointer */
    181   1.1       agc 	} val;
    182   1.1       agc } negotiation_parameter_t;
    183   1.1       agc 
    184   1.1       agc 
    185   1.1       agc /* Negotiation state flags */
    186   1.1       agc #define NS_SENT      0x01		/* key was sent to target */
    187   1.1       agc #define NS_RECEIVED  0x02		/* key was received from target */
    188   1.1       agc 
    189   1.1       agc typedef struct
    190   1.1       agc {
    191   1.1       agc 	negotiation_parameter_t pars[MAX_NEG];	/* the parameters to send */
    192   1.1       agc 	negotiation_parameter_t *cpar;			/* the last parameter set */
    193   1.1       agc 	uint16_t num_pars;						/* number of parameters to send */
    194   1.1       agc 	auth_state_t auth_state;				/* authentication state */
    195   1.1       agc 	iscsi_auth_types_t auth_alg;			/* authentication algorithm */
    196   1.1       agc 	uint8_t kflags[MAX_KEY + 2];			/* negotiation flags for each key */
    197   1.1       agc 	uint8_t password[MAX_STRING + 1];		/* authentication secret */
    198   1.1       agc 	uint8_t target_password[MAX_STRING + 1];	/* target authentication secret */
    199   1.1       agc 	uint8_t user_name[MAX_STRING + 1];		/* authentication user ID */
    200   1.1       agc 	uint8_t temp_buf[MAX_STRING + 1];		/* scratch buffer */
    201   1.1       agc 
    202   1.1       agc 	bool HeaderDigest;
    203   1.1       agc 	bool DataDigest;
    204   1.1       agc 	bool InitialR2T;
    205   1.1       agc 	bool ImmediateData;
    206   1.1       agc 	uint32_t ErrorRecoveryLevel;
    207   1.1       agc 	uint32_t MaxRecvDataSegmentLength;
    208   1.1       agc 	uint32_t MaxConnections;
    209   1.1       agc 	uint32_t DefaultTime2Wait;
    210   1.1       agc 	uint32_t DefaultTime2Retain;
    211   1.1       agc 	uint32_t MaxBurstLength;
    212   1.1       agc 	uint32_t FirstBurstLength;
    213   1.1       agc 	uint32_t MaxOutstandingR2T;
    214   1.1       agc 
    215   1.1       agc } negotiation_state_t;
    216   1.1       agc 
    217   1.1       agc 
    218   1.1       agc #define TX(state, key) (state->kflags [key] & NS_SENT)
    219   1.1       agc #define RX(state, key) (state->kflags [key] & NS_RECEIVED)
    220   1.1       agc 
    221   1.1       agc /*****************************************************************************/
    222   1.1       agc 
    223   1.1       agc 
    224   1.1       agc STATIC void
    225   1.1       agc chap_md5_response(uint8_t *buffer, uint8_t identifier, uint8_t *secret,
    226   1.1       agc 				  uint8_t *challenge, int challenge_size)
    227   1.1       agc {
    228   1.1       agc 	MD5_CTX md5;
    229   1.1       agc 
    230   1.1       agc 	MD5Init(&md5);
    231   1.1       agc 	MD5Update(&md5, &identifier, 1);
    232   1.1       agc 	MD5Update(&md5, secret, strlen(secret));
    233   1.1       agc 	MD5Update(&md5, challenge, challenge_size);
    234   1.1       agc 	MD5Final(buffer, &md5);
    235   1.1       agc }
    236   1.1       agc 
    237   1.1       agc 
    238   1.1       agc /*****************************************************************************/
    239   1.1       agc 
    240   1.1       agc /*
    241   1.1       agc  * hexdig:
    242   1.1       agc  *    Return value of hex digit.
    243   1.1       agc  *    Note: a null character is acceptable, and returns 0.
    244   1.1       agc  *
    245   1.1       agc  *    Parameter:
    246   1.1       agc  *          c     The character
    247   1.1       agc  *
    248   1.1       agc  *    Returns:    The value, -1 on error.
    249   1.1       agc  */
    250   1.1       agc 
    251   1.1       agc static __inline int
    252   1.1       agc hexdig(uint8_t c)
    253   1.1       agc {
    254   1.1       agc 
    255   1.1       agc 	if (!c) {
    256   1.1       agc 		return 0;
    257   1.1       agc 	}
    258   1.1       agc 	if (isdigit(c)) {
    259   1.1       agc 		return c - '0';
    260   1.1       agc 	}
    261   1.1       agc 	c = toupper(c);
    262   1.1       agc 	if (c >= 'A' && c <= 'F') {
    263   1.1       agc 		return c - 'A' + 10;
    264   1.1       agc 	}
    265   1.1       agc 	return -1;
    266   1.1       agc }
    267   1.1       agc 
    268   1.1       agc /*
    269   1.1       agc  * skiptozero:
    270   1.1       agc  *    Skip to next zero character in buffer.
    271   1.1       agc  *
    272   1.1       agc  *    Parameter:
    273   1.1       agc  *          buf      The buffer pointer
    274   1.1       agc  *
    275   1.1       agc  *    Returns:    The pointer to the character after the zero character.
    276   1.1       agc  */
    277   1.1       agc 
    278   1.1       agc static __inline uint8_t *
    279   1.1       agc skiptozero(uint8_t *buf)
    280   1.1       agc {
    281   1.1       agc 
    282   1.1       agc 	while (*buf) {
    283   1.1       agc 		buf++;
    284   1.1       agc 	}
    285   1.1       agc 	return buf + 1;
    286   1.1       agc }
    287   1.1       agc 
    288   1.1       agc 
    289   1.1       agc /*
    290   1.1       agc  * get_bignumval:
    291   1.1       agc  *    Get a large numeric value.
    292   1.1       agc  *    NOTE: Overwrites source string.
    293   1.1       agc  *
    294   1.1       agc  *    Parameter:
    295   1.1       agc  *          buf      The buffer pointer
    296   1.1       agc  *          par      The parameter
    297   1.1       agc  *
    298   1.1       agc  *    Returns:    The pointer to the next parameter, NULL on error.
    299   1.1       agc  */
    300   1.1       agc 
    301   1.1       agc STATIC uint8_t *
    302   1.1       agc get_bignumval(uint8_t *buf, negotiation_parameter_t *par)
    303   1.1       agc {
    304   1.1       agc 	int val;
    305   1.1       agc 	char c;
    306   1.1       agc 	uint8_t *dp = buf;
    307   1.1       agc 
    308   1.1       agc 	par->val.sval = buf;
    309   1.1       agc 
    310   1.1       agc 	if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
    311   1.1       agc 		buf += 2;
    312   1.1       agc 		while ((c = *buf) != 0x0) {
    313   1.1       agc 			buf++;
    314   1.1       agc 			val = (hexdig(c) << 4) | hexdig(*buf);
    315   1.1       agc 			if (val < 0) {
    316   1.1       agc 				return NULL;
    317   1.1       agc 			}
    318   1.1       agc 			*dp++ = (uint8_t) val;
    319   1.1       agc 			if (*buf) {
    320   1.1       agc 				buf++;
    321   1.1       agc 			}
    322   1.1       agc 		}
    323   1.1       agc 		buf++;
    324   1.1       agc 		par->list_num = dp - par->val.sval;
    325   1.1       agc 	} else if (buf[0] == '0' && (buf[1] == 'b' || buf[1] == 'B')) {
    326   1.1       agc 		buf = base64_decode(&buf[2], par->val.sval, &par->list_num);
    327   1.1       agc 	} else {
    328   1.1       agc 		DEBOUT(("Ill-formatted large number <%s>\n", buf));
    329   1.1       agc 		return NULL;
    330   1.1       agc 	}
    331   1.1       agc 
    332   1.1       agc 	return buf;
    333   1.1       agc }
    334   1.1       agc 
    335   1.1       agc 
    336   1.1       agc /*
    337   1.1       agc  * get_numval:
    338   1.1       agc  *    Get a numeric value.
    339   1.1       agc  *
    340   1.1       agc  *    Parameter:
    341   1.1       agc  *          buf      The buffer pointer
    342   1.1       agc  *          pval     The pointer to the result.
    343   1.1       agc  *
    344   1.1       agc  *    Returns:    The pointer to the next parameter, NULL on error.
    345   1.1       agc  */
    346   1.1       agc 
    347   1.1       agc STATIC uint8_t *
    348   1.1       agc get_numval(uint8_t *buf, uint32_t *pval)
    349   1.1       agc {
    350   1.1       agc 	uint32_t val = 0;
    351   1.1       agc 	char c;
    352   1.1       agc 
    353   1.1       agc 	if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
    354   1.1       agc 		buf += 2;
    355   1.1       agc 		while (*buf && *buf != '~') {
    356   1.1       agc 			int n;
    357   1.1       agc 
    358   1.1       agc 			if ((n = hexdig(*buf++)) < 0)
    359   1.1       agc 				return NULL;
    360   1.1       agc 			val = (val << 4) | n;
    361   1.1       agc 		}
    362   1.1       agc 	} else
    363   1.1       agc 		while (*buf && *buf != '~') {
    364   1.1       agc 			c = *buf++;
    365   1.1       agc 			if (!isdigit(c))
    366   1.1       agc 				return NULL;
    367   1.1       agc 			val = val * 10 + (c - '0');
    368   1.1       agc 		}
    369   1.1       agc 
    370   1.1       agc 	*pval = val;
    371   1.1       agc 
    372   1.1       agc 	return buf + 1;
    373   1.1       agc }
    374   1.1       agc 
    375   1.1       agc 
    376   1.1       agc /*
    377   1.1       agc  * get_range:
    378   1.1       agc  *    Get a numeric range.
    379   1.1       agc  *
    380   1.1       agc  *    Parameter:
    381   1.1       agc  *          buf      The buffer pointer
    382   1.1       agc  *          pval1    The pointer to the first result.
    383   1.1       agc  *          pval2    The pointer to the second result.
    384   1.1       agc  *
    385   1.1       agc  *    Returns:    The pointer to the next parameter, NULL on error.
    386   1.1       agc  */
    387   1.1       agc 
    388   1.1       agc STATIC uint8_t *
    389   1.1       agc get_range(uint8_t *buf, uint32_t *pval1, uint32_t *pval2)
    390   1.1       agc {
    391   1.1       agc 
    392   1.1       agc 	if ((buf = get_numval(buf, pval1)) == NULL)
    393   1.1       agc 		return NULL;
    394   1.1       agc 	if (!*buf)
    395   1.1       agc 		return NULL;
    396   1.1       agc 	if ((buf = get_numval(buf, pval2)) == NULL)
    397   1.1       agc 		return NULL;
    398   1.1       agc 	return buf;
    399   1.1       agc }
    400   1.1       agc 
    401   1.1       agc 
    402   1.1       agc /*
    403   1.1       agc  * get_ynval:
    404   1.1       agc  *    Get a yes/no selection.
    405   1.1       agc  *
    406   1.1       agc  *    Parameter:
    407   1.1       agc  *          buf      The buffer pointer
    408   1.1       agc  *          pval     The pointer to the result.
    409   1.1       agc  *
    410   1.1       agc  *    Returns:    The pointer to the next parameter, NULL on error.
    411   1.1       agc  */
    412   1.1       agc 
    413   1.1       agc STATIC uint8_t *
    414   1.1       agc get_ynval(uint8_t *buf, uint32_t *pval)
    415   1.1       agc {
    416   1.1       agc 
    417   1.1       agc 	if (strcmp(buf, "Yes") == 0)
    418   1.1       agc 		*pval = 1;
    419   1.1       agc 	else if (strcmp(buf, "No") == 0)
    420   1.1       agc 		*pval = 0;
    421   1.1       agc 	else
    422   1.1       agc 		return NULL;
    423   1.1       agc 
    424   1.1       agc 	return skiptozero(buf);
    425   1.1       agc }
    426   1.1       agc 
    427   1.1       agc 
    428   1.1       agc /*
    429   1.1       agc  * get_digestval:
    430   1.1       agc  *    Get a digest selection.
    431   1.1       agc  *
    432   1.1       agc  *    Parameter:
    433   1.1       agc  *          buf      The buffer pointer
    434   1.1       agc  *          pval     The pointer to the result.
    435   1.1       agc  *
    436   1.1       agc  *    Returns:    The pointer to the next parameter, NULL on error.
    437   1.1       agc  */
    438   1.1       agc 
    439   1.1       agc STATIC uint8_t *
    440   1.1       agc get_digestval(uint8_t *buf, uint32_t *pval)
    441   1.1       agc {
    442   1.1       agc 
    443   1.1       agc 	if (strcmp(buf, "CRC32C") == 0)
    444   1.1       agc 		*pval = 1;
    445   1.1       agc 	else if (strcmp(buf, "None") == 0)
    446   1.1       agc 		*pval = 0;
    447   1.1       agc 	else
    448   1.1       agc 		return NULL;
    449   1.1       agc 
    450   1.1       agc 	return skiptozero(buf);
    451   1.1       agc }
    452   1.1       agc 
    453   1.1       agc 
    454   1.1       agc /*
    455   1.1       agc  * get_authval:
    456   1.1       agc  *    Get an authentication method.
    457   1.1       agc  *
    458   1.1       agc  *    Parameter:
    459   1.1       agc  *          buf      The buffer pointer
    460   1.1       agc  *          pval     The pointer to the result.
    461   1.1       agc  *
    462   1.1       agc  *    Returns:    The pointer to the next parameter, NULL on error.
    463   1.1       agc  */
    464   1.1       agc 
    465   1.1       agc STATIC uint8_t *
    466   1.1       agc get_authval(uint8_t *buf, uint32_t *pval)
    467   1.1       agc {
    468   1.1       agc 
    469   1.1       agc 	if (strcmp(buf, "None") == 0)
    470   1.1       agc 		*pval = ISCSI_AUTH_None;
    471   1.1       agc 	else if (strcmp(buf, "CHAP") == 0)
    472   1.1       agc 		*pval = ISCSI_AUTH_CHAP;
    473   1.1       agc 	else if (strcmp(buf, "KRB5") == 0)
    474   1.1       agc 		*pval = ISCSI_AUTH_KRB5;
    475   1.1       agc 	else if (strcmp(buf, "SRP") == 0)
    476   1.1       agc 		*pval = ISCSI_AUTH_SRP;
    477   1.1       agc 	else
    478   1.1       agc 		return NULL;
    479   1.1       agc 
    480   1.1       agc 	return skiptozero(buf);
    481   1.1       agc }
    482   1.1       agc 
    483   1.1       agc 
    484   1.1       agc /*
    485   1.1       agc  * get_strval:
    486   1.1       agc  *    Get a string value (returns pointer to original buffer, not a copy).
    487   1.1       agc  *
    488   1.1       agc  *    Parameter:
    489   1.1       agc  *          buf      The buffer pointer
    490   1.1       agc  *          pval     The pointer to the result pointer.
    491   1.1       agc  *
    492   1.1       agc  *    Returns:    The pointer to the next parameter, NULL on error.
    493   1.1       agc  */
    494   1.1       agc 
    495   1.1       agc STATIC uint8_t *
    496   1.1       agc get_strval(uint8_t *buf, uint8_t **pval)
    497   1.1       agc {
    498   1.1       agc 
    499   1.1       agc 	if (strlen(buf) > MAX_STRING)
    500   1.1       agc 		return NULL;
    501   1.1       agc 
    502   1.1       agc 	*pval = buf;
    503   1.1       agc 
    504   1.1       agc 	return skiptozero(buf);
    505   1.1       agc }
    506   1.1       agc 
    507   1.1       agc 
    508   1.1       agc /*
    509   1.1       agc  * get_parameter:
    510   1.1       agc  *    Analyze a key=value string.
    511   1.1       agc  *    NOTE: The string is modified in the process.
    512   1.1       agc  *
    513   1.1       agc  *    Parameter:
    514   1.1       agc  *          buf      The buffer pointer
    515   1.1       agc  *          par      The parameter descriptor to be filled in
    516   1.1       agc  *
    517   1.1       agc  *    Returns:    The pointer to the next parameter, NULL on error.
    518   1.1       agc  */
    519   1.1       agc 
    520   1.1       agc STATIC uint8_t *
    521   1.1       agc get_parameter(uint8_t *buf, negotiation_parameter_t *par)
    522   1.1       agc {
    523   1.1       agc 	uint8_t *bp = buf;
    524   1.1       agc 	int i;
    525   1.1       agc 
    526   1.1       agc 	while (*bp && *bp != '=') {
    527   1.1       agc 		bp++;
    528   1.1       agc 	}
    529   1.1       agc 	if (!*bp) {
    530   1.1       agc 		DEBOUT(("get_parameter: Premature end of parameter\n"));
    531   1.1       agc 		return NULL;
    532   1.1       agc 	}
    533   1.1       agc 
    534   1.1       agc 	*bp++ = 0;
    535   1.1       agc 
    536   1.1       agc 	for (i = 0; i <= MAX_KEY; i++)
    537   1.1       agc 		if (!strcmp(buf, entries[i].name))
    538   1.1       agc 			break;
    539   1.1       agc 
    540   1.1       agc 	par->key = i;
    541   1.1       agc 	par->list_num = 1;
    542   1.1       agc 
    543   1.1       agc 	if (i > MAX_KEY) {
    544   1.1       agc 		DEBOUT(("get_parameter: unrecognized key <%s>\n", buf));
    545   1.1       agc 		if (strlen(buf) > MAX_STRING) {
    546   1.1       agc 			DEBOUT(("get_parameter: key name > MAX_STRING\n"));
    547   1.1       agc 			return NULL;
    548   1.1       agc 		}
    549   1.1       agc 		par->val.sval = buf;
    550   1.1       agc 		return skiptozero(bp);
    551   1.1       agc 	}
    552   1.1       agc 
    553   1.5   mlelstv 	DEB(10, ("get_par: key <%s>=%d, val=%d, ret %p\n",
    554   1.5   mlelstv 			buf, i, entries[i].val, bp));
    555   1.5   mlelstv 	DEB(10, ("get_par: value '%s'\n",bp));
    556   1.5   mlelstv 
    557   1.1       agc 	switch (entries[i].val) {
    558   1.1       agc 	case T_NUM:
    559   1.1       agc 		bp = get_numval(bp, &par->val.nval[0]);
    560   1.1       agc 		break;
    561   1.1       agc 
    562   1.1       agc 	case T_BIGNUM:
    563   1.1       agc 		bp = get_bignumval(bp, par);
    564   1.1       agc 		break;
    565   1.1       agc 
    566   1.1       agc 	case T_STRING:
    567   1.1       agc 		bp = get_strval(bp, &par->val.sval);
    568   1.1       agc 		break;
    569   1.1       agc 
    570   1.1       agc 	case T_YESNO:
    571   1.1       agc 		bp = get_ynval(bp, &par->val.nval[0]);
    572   1.1       agc 		break;
    573   1.1       agc 
    574   1.1       agc 	case T_AUTH:
    575   1.1       agc 		bp = get_authval(bp, &par->val.nval[0]);
    576   1.1       agc 		break;
    577   1.1       agc 
    578   1.1       agc 	case T_DIGEST:
    579   1.1       agc 		bp = get_digestval(bp, &par->val.nval[0]);
    580   1.1       agc 		break;
    581   1.1       agc 
    582   1.1       agc 	case T_RANGE:
    583   1.1       agc 		bp = get_range(bp, &par->val.nval[0], &par->val.nval[1]);
    584   1.1       agc 		break;
    585   1.1       agc 
    586   1.1       agc 	default:
    587   1.1       agc 		/* Target sending any other types is wrong */
    588   1.1       agc 		bp = NULL;
    589   1.1       agc 		break;
    590   1.1       agc 	}
    591   1.1       agc 	return bp;
    592   1.1       agc }
    593   1.1       agc 
    594   1.1       agc /*****************************************************************************/
    595   1.1       agc 
    596   1.1       agc /*
    597   1.1       agc  * my_strcpy:
    598   1.1       agc  *    Replacement for strcpy that returns the end of the result string
    599   1.1       agc  *
    600   1.1       agc  *    Parameter:
    601   1.1       agc  *          dest     The destination buffer pointer
    602   1.1       agc  *          src      The source string
    603   1.1       agc  *
    604   1.1       agc  *    Returns:    A pointer to the terminating zero of the result.
    605   1.1       agc  */
    606   1.1       agc 
    607   1.1       agc static __inline unsigned
    608   1.1       agc my_strcpy(uint8_t *dest, const uint8_t *src)
    609   1.1       agc {
    610   1.1       agc 	unsigned	cc;
    611   1.1       agc 
    612   1.1       agc 	for (cc = 0 ; (*dest = *src) != 0x0 ; cc++) {
    613   1.1       agc 		dest++;
    614   1.1       agc 		src++;
    615   1.1       agc 	}
    616   1.1       agc 	return cc;
    617   1.1       agc }
    618   1.1       agc 
    619   1.5   mlelstv /*
    620   1.5   mlelstv  * put_bignumval:
    621   1.5   mlelstv  *    Write a large numeric value.
    622   1.5   mlelstv  *    NOTE: Overwrites source string.
    623   1.5   mlelstv  *
    624   1.5   mlelstv  *    Parameter:
    625   1.5   mlelstv  *          buf      The buffer pointer
    626   1.5   mlelstv  *          par      The parameter
    627   1.5   mlelstv  *
    628   1.5   mlelstv  *    Returns:    The pointer to the next parameter, NULL on error.
    629   1.5   mlelstv  */
    630   1.5   mlelstv 
    631   1.5   mlelstv STATIC unsigned
    632   1.5   mlelstv put_bignumval(negotiation_parameter_t *par, uint8_t *buf)
    633   1.5   mlelstv {
    634  1.10   mlelstv 	int k, c;
    635  1.10   mlelstv 
    636  1.12   mlelstv 	if (par->hex_bignums) {
    637  1.12   mlelstv 		my_strcpy(buf, "0x");
    638  1.12   mlelstv 		for (k=0; k<par->list_num; ++k) {
    639  1.12   mlelstv 			c = par->val.sval[k] >> 4;
    640  1.12   mlelstv 			buf[2+2*k] = c < 10 ? '0' + c : 'a' + (c-10);
    641  1.12   mlelstv 			c = par->val.sval[k] & 0xf;
    642  1.12   mlelstv 			buf[2+2*k+1] = c < 10 ? '0' + c : 'a' + (c-10);
    643  1.12   mlelstv 		}
    644  1.12   mlelstv 		buf[2+2*k] = '\0';
    645  1.12   mlelstv 
    646  1.12   mlelstv 		return 2+2*par->list_num;
    647  1.10   mlelstv 	}
    648   1.5   mlelstv 	return base64_encode(par->val.sval, par->list_num, buf);
    649   1.5   mlelstv }
    650   1.1       agc 
    651   1.1       agc /*
    652   1.1       agc  * put_parameter:
    653   1.1       agc  *    Create a key=value string.
    654   1.1       agc  *
    655   1.1       agc  *    Parameter:
    656   1.1       agc  *          buf      The buffer pointer
    657   1.1       agc  *          par      The parameter descriptor
    658   1.1       agc  *
    659   1.1       agc  *    Returns:    The pointer to the next free buffer space, NULL on error.
    660   1.1       agc  */
    661   1.1       agc 
    662   1.1       agc STATIC unsigned
    663   1.1       agc put_parameter(uint8_t *buf, unsigned len, negotiation_parameter_t *par)
    664   1.1       agc {
    665   1.1       agc 	int i;
    666   1.5   mlelstv 	unsigned	cc, cl;
    667   1.1       agc 	const uint8_t *sp;
    668   1.1       agc 
    669   1.5   mlelstv 	DEB(10, ("put_par: key <%s>=%d, val=%d\n",
    670   1.5   mlelstv 		entries[par->key].name, par->key, entries[par->key].val));
    671   1.5   mlelstv 
    672   1.1       agc 	if (par->key > MAX_KEY) {
    673   1.1       agc 		return snprintf(buf, len, "%s=NotUnderstood", par->val.sval);
    674   1.1       agc 	}
    675   1.1       agc 
    676   1.1       agc 	cc = snprintf(buf, len, "%s=", entries[par->key].name);
    677   1.7  christos 	if (cc >= len)
    678   1.7  christos 		return len;
    679   1.1       agc 
    680   1.1       agc 	for (i = 0; i < par->list_num; i++) {
    681   1.1       agc 		switch (entries[par->key].val) {
    682   1.1       agc 		case T_NUM:
    683   1.5   mlelstv 			cl = snprintf(&buf[cc], len - cc, "%d",
    684   1.5   mlelstv 			               par->val.nval[i]);
    685   1.1       agc 			break;
    686   1.1       agc 
    687   1.1       agc 		case T_BIGNUM:
    688   1.5   mlelstv 			cl = put_bignumval(par, &buf[cc]);
    689   1.1       agc 			i = par->list_num;
    690   1.1       agc 			break;
    691   1.1       agc 
    692   1.1       agc 		case T_STRING:
    693   1.5   mlelstv 			cl =  my_strcpy(&buf[cc], par->val.sval);
    694   1.1       agc 			break;
    695   1.1       agc 
    696   1.1       agc 		case T_YESNO:
    697   1.5   mlelstv 			cl = my_strcpy(&buf[cc],
    698   1.1       agc 				(par->val.nval[i]) ? "Yes" : "No");
    699   1.1       agc 			break;
    700   1.1       agc 
    701   1.1       agc 		case T_AUTH:
    702   1.1       agc 			switch (par->val.nval[i]) {
    703   1.1       agc 			case ISCSI_AUTH_CHAP:
    704   1.1       agc 				sp = "CHAP";
    705   1.1       agc 				break;
    706   1.1       agc 			case ISCSI_AUTH_KRB5:
    707   1.1       agc 				sp = "KRB5";
    708   1.1       agc 				break;
    709   1.1       agc 			case ISCSI_AUTH_SRP:
    710   1.1       agc 				sp = "SRP";
    711   1.1       agc 				break;
    712   1.1       agc 			default:
    713   1.1       agc 				sp = "None";
    714   1.1       agc 				break;
    715   1.1       agc 			}
    716   1.5   mlelstv 			cl = my_strcpy(&buf[cc], sp);
    717   1.1       agc 			break;
    718   1.1       agc 
    719   1.1       agc 		case T_DIGEST:
    720   1.5   mlelstv 			cl = my_strcpy(&buf[cc],
    721   1.5   mlelstv 				(par->val.nval[i]) ? "CRC32C" : "None");
    722   1.1       agc 			break;
    723   1.1       agc 
    724   1.1       agc 		case T_RANGE:
    725   1.1       agc 			if ((i + 1) >= par->list_num) {
    726   1.5   mlelstv 				cl = my_strcpy(&buf[cc], "Reject");
    727   1.1       agc 			} else {
    728   1.5   mlelstv 				cl = snprintf(&buf[cc], len - cc,
    729   1.1       agc 						"%d~%d", par->val.nval[i],
    730   1.1       agc 						par->val.nval[i + 1]);
    731   1.1       agc 				i++;
    732   1.1       agc 			}
    733   1.1       agc 			break;
    734   1.1       agc 
    735   1.1       agc 		case T_SENDT:
    736   1.5   mlelstv 			cl = my_strcpy(&buf[cc], par->val.sval);
    737   1.1       agc 			break;
    738   1.1       agc 
    739   1.1       agc 		case T_SESS:
    740   1.5   mlelstv 			cl = my_strcpy(&buf[cc],
    741   1.1       agc 				(par->val.nval[i]) ? "Normal" : "Discovery");
    742   1.1       agc 			break;
    743   1.1       agc 
    744   1.1       agc 		default:
    745   1.5   mlelstv 			cl = 0;
    746   1.1       agc 			/* We should't be here... */
    747   1.1       agc 			DEBOUT(("Invalid type %d in put_parameter!\n",
    748   1.1       agc 					entries[par->key].val));
    749   1.1       agc 			break;
    750   1.1       agc 		}
    751   1.5   mlelstv 
    752   1.5   mlelstv 		DEB(10, ("put_par: value '%s'\n",&buf[cc]));
    753   1.5   mlelstv 
    754   1.5   mlelstv 		cc += cl;
    755   1.7  christos 		if (cc >= len)
    756   1.7  christos 			return len;
    757   1.1       agc 		if ((i + 1) < par->list_num) {
    758   1.7  christos 			if (cc >= len)
    759   1.7  christos 				return len;
    760   1.1       agc 			buf[cc++] = ',';
    761   1.1       agc 		}
    762   1.1       agc 	}
    763   1.1       agc 
    764   1.7  christos 	if (cc >= len)
    765   1.7  christos 		return len;
    766   1.5   mlelstv 	buf[cc] = 0x0;				/* make sure it's terminated */
    767   1.1       agc 	return cc + 1;				/* return next place in list */
    768   1.1       agc }
    769   1.1       agc 
    770   1.1       agc 
    771   1.1       agc /*
    772   1.1       agc  * put_par_block:
    773   1.1       agc  *    Fill a parameter block
    774   1.1       agc  *
    775   1.1       agc  *    Parameter:
    776   1.1       agc  *          buf      The buffer pointer
    777   1.1       agc  *          pars     The parameter descriptor array
    778   1.1       agc  *          n        The number of elements
    779   1.1       agc  *
    780   1.1       agc  *    Returns:    result from put_parameter (ptr to buffer, NULL on error)
    781   1.1       agc  */
    782   1.1       agc 
    783   1.1       agc static __inline unsigned
    784   1.1       agc put_par_block(uint8_t *buf, unsigned len, negotiation_parameter_t *pars, int n)
    785   1.1       agc {
    786   1.1       agc 	unsigned	cc;
    787   1.1       agc 	int i;
    788   1.1       agc 
    789   1.1       agc 	for (cc = 0, i = 0; i < n; i++) {
    790   1.1       agc 		cc += put_parameter(&buf[cc], len - cc, pars++);
    791   1.1       agc 		if (cc >= len) {
    792   1.1       agc 			break;
    793   1.1       agc 		}
    794   1.1       agc 	}
    795   1.1       agc 	return cc;
    796   1.1       agc }
    797   1.1       agc 
    798   1.1       agc /*
    799   1.1       agc  * parameter_size:
    800   1.1       agc  *    Determine the size of a key=value string.
    801   1.1       agc  *
    802   1.1       agc  *    Parameter:
    803   1.1       agc  *          par      The parameter descriptor
    804   1.1       agc  *
    805   1.1       agc  *    Returns:    The size of the resulting string.
    806   1.1       agc  */
    807   1.1       agc 
    808   1.1       agc STATIC int
    809   1.1       agc parameter_size(negotiation_parameter_t *par)
    810   1.1       agc {
    811   1.1       agc 	int i, size;
    812   1.1       agc 	char buf[24];	/* max. 2 10-digit numbers + sep. */
    813   1.1       agc 
    814   1.1       agc 	if (par->key > MAX_KEY) {
    815   1.1       agc 		return strlen(par->val.sval) + 15;
    816   1.1       agc 	}
    817   1.1       agc 	/* count '=' and terminal zero */
    818   1.1       agc 	size = strlen(entries[par->key].name) + 2;
    819   1.1       agc 
    820   1.1       agc 	for (i = 0; i < par->list_num; i++) {
    821   1.1       agc 		switch (entries[par->key].val) {
    822   1.1       agc 		case T_NUM:
    823   1.1       agc 			size += snprintf(buf, sizeof(buf), "%d",
    824   1.1       agc 					par->val.nval[i]);
    825   1.1       agc 			break;
    826   1.1       agc 
    827   1.1       agc 		case T_BIGNUM:
    828   1.1       agc 			/* list_num holds value size */
    829  1.12   mlelstv 			if (par->hex_bignums)
    830  1.12   mlelstv 				size += 2 + 2*par->list_num;
    831  1.12   mlelstv 			else
    832  1.12   mlelstv 				size += base64_enclen(par->list_num);
    833   1.1       agc 			i = par->list_num;
    834   1.1       agc 			break;
    835   1.1       agc 
    836   1.1       agc 		case T_STRING:
    837   1.1       agc 		case T_SENDT:
    838   1.1       agc 			size += strlen(par->val.sval);
    839   1.1       agc 			break;
    840   1.1       agc 
    841   1.1       agc 		case T_YESNO:
    842   1.1       agc 			size += (par->val.nval[i]) ? 3 : 2;
    843   1.1       agc 			break;
    844   1.1       agc 
    845   1.1       agc 		case T_AUTH:
    846   1.1       agc 			size += (par->val.nval[i] == ISCSI_AUTH_SRP) ? 3 : 4;
    847   1.1       agc 			break;
    848   1.1       agc 
    849   1.1       agc 		case T_DIGEST:
    850   1.1       agc 			size += (par->val.nval[i]) ? 6 : 4;
    851   1.1       agc 			break;
    852   1.1       agc 
    853   1.1       agc 		case T_RANGE:
    854  1.10   mlelstv 			if (i+1 < par->list_num) {
    855  1.10   mlelstv 				size += snprintf(buf, sizeof(buf), "%d~%d",
    856  1.10   mlelstv 					par->val.nval[i],
    857  1.10   mlelstv 					par->val.nval[i + 1]);
    858  1.10   mlelstv 				i++;
    859  1.10   mlelstv 			} else
    860  1.10   mlelstv 				DEBOUT(("Incomplete range parameter\n"));
    861   1.1       agc 			break;
    862   1.1       agc 
    863   1.1       agc 		case T_SESS:
    864   1.1       agc 			size += (par->val.nval[i]) ? 6 : 9;
    865   1.1       agc 			break;
    866   1.1       agc 
    867   1.1       agc 		default:
    868   1.1       agc 			/* We should't be here... */
    869   1.1       agc 			DEBOUT(("Invalid type %d in parameter_size!\n",
    870   1.1       agc 					entries[par->key].val));
    871   1.1       agc 			break;
    872   1.1       agc 		}
    873   1.1       agc 		if ((i + 1) < par->list_num) {
    874   1.1       agc 			size++;
    875   1.1       agc 		}
    876   1.1       agc 	}
    877   1.1       agc 
    878   1.1       agc 	return size;
    879   1.1       agc }
    880   1.1       agc 
    881   1.1       agc 
    882   1.1       agc /*
    883   1.1       agc  * total_size:
    884   1.1       agc  *    Determine the size of a negotiation data block
    885   1.1       agc  *
    886   1.1       agc  *    Parameter:
    887   1.1       agc  *          pars     The parameter descriptor array
    888   1.1       agc  *          n        The number of elements
    889   1.1       agc  *
    890   1.1       agc  *    Returns:    The size of the block
    891   1.1       agc  */
    892   1.1       agc 
    893   1.1       agc static __inline int
    894   1.1       agc total_size(negotiation_parameter_t *pars, int n)
    895   1.1       agc {
    896   1.1       agc 	int i, size;
    897   1.1       agc 
    898   1.1       agc 	for (i = 0, size = 0; i < n; i++) {
    899   1.1       agc 		size += parameter_size(pars++);
    900   1.1       agc 	}
    901   1.1       agc 	return size;
    902   1.1       agc }
    903   1.1       agc 
    904   1.1       agc /*****************************************************************************/
    905   1.1       agc 
    906   1.1       agc 
    907   1.1       agc /*
    908   1.1       agc  * complete_pars:
    909   1.1       agc  *    Allocate space for text parameters, translate parameter values into
    910   1.1       agc  *    text.
    911   1.1       agc  *
    912   1.1       agc  *    Parameter:
    913   1.1       agc  *          state    Negotiation state
    914   1.1       agc  *          pdu      The transmit PDU
    915   1.1       agc  *
    916   1.1       agc  *    Returns:    0     On success
    917   1.1       agc  *                > 0   (an ISCSI error code) if an error occurred.
    918   1.1       agc  */
    919   1.1       agc 
    920   1.1       agc STATIC int
    921   1.1       agc complete_pars(negotiation_state_t *state, pdu_t *pdu)
    922   1.1       agc {
    923   1.1       agc 	int len;
    924   1.1       agc 	uint8_t *bp;
    925   1.1       agc 
    926   1.1       agc 	len = total_size(state->pars, state->num_pars);
    927   1.1       agc 
    928   1.1       agc 	DEB(10, ("complete_pars: n=%d, len=%d\n", state->num_pars, len));
    929   1.1       agc 
    930   1.1       agc 	if ((bp = malloc(len, M_TEMP, M_WAITOK)) == NULL) {
    931   1.1       agc 		DEBOUT(("*** Out of memory in complete_pars\n"));
    932   1.1       agc 		return ISCSI_STATUS_NO_RESOURCES;
    933   1.1       agc 	}
    934  1.11  christos 	pdu->pdu_temp_data = bp;
    935   1.1       agc 
    936  1.11  christos 	if (put_par_block(pdu->pdu_temp_data, len, state->pars,
    937   1.1       agc 			state->num_pars) == 0) {
    938   1.1       agc 		DEBOUT(("Bad parameter in complete_pars\n"));
    939   1.1       agc 		return ISCSI_STATUS_PARAMETER_INVALID;
    940   1.1       agc 	}
    941   1.1       agc 
    942  1.11  christos 	pdu->pdu_temp_data_len = len;
    943   1.1       agc 	return 0;
    944   1.1       agc }
    945   1.1       agc 
    946   1.1       agc 
    947   1.1       agc /*
    948   1.1       agc  * set_key_n:
    949   1.1       agc  *    Initialize a key and its numeric value.
    950   1.1       agc  *
    951   1.1       agc  *    Parameter:
    952   1.1       agc  *          state    Negotiation state
    953   1.1       agc  *          key      The key
    954   1.1       agc  *          val      The value
    955   1.1       agc  */
    956   1.1       agc 
    957   1.1       agc STATIC negotiation_parameter_t *
    958   1.1       agc set_key_n(negotiation_state_t *state, text_key_t key, uint32_t val)
    959   1.1       agc {
    960   1.1       agc 	negotiation_parameter_t *par;
    961   1.1       agc 
    962   1.1       agc 	if (state->num_pars >= MAX_NEG) {
    963   1.1       agc 		DEBOUT(("set_key_n: num_pars (%d) >= MAX_NEG (%d)\n",
    964   1.1       agc 				state->num_pars, MAX_NEG));
    965   1.1       agc 		return NULL;
    966   1.1       agc 	}
    967   1.1       agc 	par = &state->pars[state->num_pars];
    968   1.1       agc 	par->key = key;
    969   1.1       agc 	par->list_num = 1;
    970   1.1       agc 	par->val.nval[0] = val;
    971   1.1       agc 	state->num_pars++;
    972   1.1       agc 	state->kflags[key] |= NS_SENT;
    973   1.1       agc 
    974   1.1       agc 	return par;
    975   1.1       agc }
    976   1.1       agc 
    977   1.1       agc /*
    978   1.1       agc  * set_key_s:
    979   1.1       agc  *    Initialize a key and its string value.
    980   1.1       agc  *
    981   1.1       agc  *    Parameter:
    982   1.1       agc  *          state    Negotiation state
    983   1.1       agc  *          key      The key
    984   1.1       agc  *          val      The value
    985   1.1       agc  */
    986   1.1       agc 
    987   1.1       agc STATIC negotiation_parameter_t *
    988   1.1       agc set_key_s(negotiation_state_t *state, text_key_t key, uint8_t *val)
    989   1.1       agc {
    990   1.1       agc 	negotiation_parameter_t *par;
    991   1.1       agc 
    992   1.1       agc 	if (state->num_pars >= MAX_NEG) {
    993   1.1       agc 		DEBOUT(("set_key_s: num_pars (%d) >= MAX_NEG (%d)\n",
    994   1.1       agc 				state->num_pars, MAX_NEG));
    995   1.1       agc 		return NULL;
    996   1.1       agc 	}
    997   1.1       agc 	par = &state->pars[state->num_pars];
    998   1.1       agc 	par->key = key;
    999   1.1       agc 	par->list_num = 1;
   1000   1.1       agc 	par->val.sval = val;
   1001  1.12   mlelstv 	par->hex_bignums = iscsi_hex_bignums;
   1002   1.1       agc 	state->num_pars++;
   1003   1.1       agc 	state->kflags[key] |= NS_SENT;
   1004   1.1       agc 
   1005   1.1       agc 	return par;
   1006   1.1       agc }
   1007   1.1       agc 
   1008   1.1       agc 
   1009   1.1       agc /*****************************************************************************/
   1010   1.1       agc 
   1011   1.1       agc /*
   1012   1.1       agc  * eval_parameter:
   1013   1.1       agc  *    Evaluate a received negotiation value.
   1014   1.1       agc  *
   1015   1.1       agc  *    Parameter:
   1016   1.1       agc  *          conn     The connection
   1017   1.1       agc  *          state    The negotiation state
   1018   1.1       agc  *          par      The parameter
   1019   1.1       agc  *
   1020   1.1       agc  *    Returns:    0 on success, else an ISCSI status value.
   1021   1.1       agc  */
   1022   1.1       agc 
   1023   1.1       agc STATIC int
   1024   1.1       agc eval_parameter(connection_t *conn, negotiation_state_t *state,
   1025   1.1       agc 			   negotiation_parameter_t *par)
   1026   1.1       agc {
   1027   1.1       agc 	uint32_t n = par->val.nval[0];
   1028   1.1       agc 	size_t sz;
   1029   1.1       agc 	text_key_t key = par->key;
   1030   1.1       agc 	bool sent = (state->kflags[key] & NS_SENT) != 0;
   1031   1.1       agc 
   1032   1.1       agc 	state->kflags[key] |= NS_RECEIVED;
   1033   1.1       agc 
   1034   1.1       agc 	switch (key) {
   1035   1.1       agc 		/*
   1036   1.1       agc 		 *  keys connected to security negotiation
   1037   1.1       agc 		 */
   1038   1.1       agc 	case K_AuthMethod:
   1039   1.1       agc 		if (n) {
   1040   1.1       agc 			DEBOUT(("eval_par: AuthMethod nonzero (%d)\n", n));
   1041   1.1       agc 			return ISCSI_STATUS_NEGOTIATION_ERROR;
   1042   1.1       agc 		}
   1043   1.1       agc 		break;
   1044   1.1       agc 
   1045   1.1       agc 	case K_Auth_CHAP_Algorithm:
   1046   1.1       agc 	case K_Auth_CHAP_Challenge:
   1047   1.1       agc 	case K_Auth_CHAP_Identifier:
   1048   1.1       agc 	case K_Auth_CHAP_Name:
   1049   1.1       agc 	case K_Auth_CHAP_Response:
   1050   1.1       agc 		DEBOUT(("eval_par: Authorization Key in Operational Phase\n"));
   1051   1.1       agc 		return ISCSI_STATUS_NEGOTIATION_ERROR;
   1052   1.1       agc 
   1053   1.1       agc 		/*
   1054   1.1       agc 		 * keys we always send
   1055   1.1       agc 		 */
   1056   1.1       agc 	case K_DataDigest:
   1057   1.1       agc 		state->DataDigest = n;
   1058   1.1       agc 		if (!sent)
   1059   1.1       agc 			set_key_n(state, key, n);
   1060   1.1       agc 		break;
   1061   1.1       agc 
   1062   1.1       agc 	case K_HeaderDigest:
   1063   1.1       agc 		state->HeaderDigest = n;
   1064   1.1       agc 		if (!sent)
   1065   1.1       agc 			set_key_n(state, key, n);
   1066   1.1       agc 		break;
   1067   1.1       agc 
   1068   1.1       agc 	case K_ErrorRecoveryLevel:
   1069   1.1       agc 		state->ErrorRecoveryLevel = n;
   1070   1.1       agc 		if (!sent)
   1071   1.1       agc 			set_key_n(state, key, n);
   1072   1.1       agc 		break;
   1073   1.1       agc 
   1074   1.1       agc 	case K_ImmediateData:
   1075   1.1       agc 		state->ImmediateData = n;
   1076   1.1       agc 		if (!sent)
   1077   1.1       agc 			set_key_n(state, key, n);
   1078   1.1       agc 		break;
   1079   1.1       agc 
   1080   1.1       agc 	case K_InitialR2T:
   1081   1.1       agc 		state->InitialR2T = n;
   1082   1.1       agc 		if (!sent)
   1083   1.1       agc 			set_key_n(state, key, n);
   1084   1.1       agc 		break;
   1085   1.1       agc 
   1086   1.1       agc 	case K_MaxRecvDataSegmentLength:
   1087   1.1       agc 		state->MaxRecvDataSegmentLength = n;
   1088   1.1       agc 		/* this is basically declarative, not negotiated */
   1089   1.1       agc 		/* (each side has its own value) */
   1090   1.1       agc 		break;
   1091   1.1       agc 
   1092   1.1       agc 		/*
   1093   1.1       agc 		 * keys we don't always send, so we may have to reflect the value
   1094   1.1       agc 		 */
   1095   1.1       agc 	case K_DefaultTime2Retain:
   1096   1.1       agc 		state->DefaultTime2Retain = n = min(state->DefaultTime2Retain, n);
   1097   1.1       agc 		if (!sent)
   1098   1.1       agc 			set_key_n(state, key, n);
   1099   1.1       agc 		break;
   1100   1.1       agc 
   1101   1.1       agc 	case K_DefaultTime2Wait:
   1102   1.1       agc 		state->DefaultTime2Wait = n = min(state->DefaultTime2Wait, n);
   1103   1.1       agc 		if (!sent)
   1104   1.1       agc 			set_key_n(state, key, n);
   1105   1.1       agc 		break;
   1106   1.1       agc 
   1107   1.1       agc 	case K_MaxConnections:
   1108   1.1       agc 		if (state->MaxConnections)
   1109   1.1       agc 			state->MaxConnections = n = min(state->MaxConnections, n);
   1110   1.1       agc 		else
   1111   1.1       agc 			state->MaxConnections = n;
   1112   1.1       agc 
   1113   1.1       agc 		if (!sent)
   1114   1.1       agc 			set_key_n(state, key, n);
   1115   1.1       agc 		break;
   1116   1.1       agc 
   1117   1.1       agc 	case K_MaxOutstandingR2T:
   1118   1.1       agc 		state->MaxOutstandingR2T = n;
   1119   1.1       agc 		if (!sent)
   1120   1.1       agc 			set_key_n(state, key, n);
   1121   1.1       agc 		break;
   1122   1.1       agc 
   1123   1.1       agc 	case K_FirstBurstLength:
   1124   1.1       agc 		state->FirstBurstLength = n;
   1125   1.1       agc 		if (!sent)
   1126   1.1       agc 			set_key_n(state, key, n);
   1127   1.1       agc 		break;
   1128   1.1       agc 
   1129   1.1       agc 	case K_MaxBurstLength:
   1130   1.1       agc 		state->MaxBurstLength = n;
   1131   1.1       agc 		if (!sent)
   1132   1.1       agc 			set_key_n(state, key, n);
   1133   1.1       agc 		break;
   1134   1.1       agc 
   1135   1.1       agc 	case K_IFMarker:
   1136   1.1       agc 	case K_OFMarker:
   1137   1.1       agc 		/* not (yet) supported */
   1138   1.1       agc 		if (!sent)
   1139   1.1       agc 			set_key_n(state, key, 0);
   1140   1.1       agc 		break;
   1141   1.1       agc 
   1142   1.1       agc 	case K_IFMarkInt:
   1143   1.1       agc 	case K_OFMarkInt:
   1144   1.1       agc 		/* it's a range, and list_num will be 1, so this will reply "Reject" */
   1145   1.1       agc 		if (!sent)
   1146   1.1       agc 			set_key_n(state, key, 0);
   1147   1.1       agc 		break;
   1148   1.1       agc 
   1149   1.1       agc 	case K_DataPDUInOrder:
   1150   1.1       agc 	case K_DataSequenceInOrder:
   1151   1.1       agc 		/* values are don't care */
   1152   1.1       agc 		if (!sent)
   1153   1.1       agc 			set_key_n(state, key, n);
   1154   1.1       agc 		break;
   1155   1.1       agc 
   1156   1.1       agc 	case K_NotUnderstood:
   1157   1.1       agc 		/* return "NotUnderstood" */
   1158   1.1       agc 		set_key_s(state, key, par->val.sval);
   1159   1.1       agc 		break;
   1160   1.1       agc 
   1161   1.1       agc 		/*
   1162   1.1       agc 		 * Declarative keys (no response required)
   1163   1.1       agc 		 */
   1164   1.1       agc 	case K_TargetAddress:
   1165   1.1       agc 		/* ignore for now... */
   1166   1.1       agc 		break;
   1167   1.1       agc 
   1168   1.1       agc 	case K_TargetAlias:
   1169  1.11  christos 		if (conn->c_login_par->is_present.TargetAlias) {
   1170  1.11  christos 			copyoutstr(par->val.sval, conn->c_login_par->TargetAlias,
   1171   1.1       agc 				ISCSI_STRING_LENGTH - 1, &sz);
   1172   1.1       agc 			/* do anything with return code?? */
   1173   1.1       agc 		}
   1174   1.1       agc 		break;
   1175   1.1       agc 
   1176   1.1       agc 	case K_TargetPortalGroupTag:
   1177   1.1       agc 		/* ignore for now... */
   1178   1.1       agc 		break;
   1179   1.1       agc 
   1180   1.1       agc 	default:
   1181   1.1       agc 		DEBOUT(("eval_par: Invalid parameter type %d\n", par->key));
   1182   1.1       agc 		return ISCSI_STATUS_NEGOTIATION_ERROR;
   1183   1.1       agc 	}
   1184   1.1       agc 	return 0;
   1185   1.1       agc }
   1186   1.1       agc 
   1187   1.1       agc /*****************************************************************************/
   1188   1.1       agc 
   1189   1.1       agc 
   1190   1.1       agc /*
   1191   1.1       agc  * init_session_parameters:
   1192   1.1       agc  *    Initialize session-related negotiation parameters from existing session
   1193   1.1       agc  *
   1194   1.1       agc  *    Parameter:
   1195   1.1       agc  *          sess     The session
   1196   1.1       agc  *          state    The negotiation state
   1197   1.1       agc  */
   1198   1.1       agc 
   1199   1.1       agc STATIC void
   1200   1.1       agc init_session_parameters(session_t *sess, negotiation_state_t *state)
   1201   1.1       agc {
   1202   1.1       agc 
   1203  1.11  christos 	state->ErrorRecoveryLevel = sess->s_ErrorRecoveryLevel;
   1204  1.11  christos 	state->InitialR2T = sess->s_InitialR2T;
   1205  1.11  christos 	state->ImmediateData = sess->s_ImmediateData;
   1206  1.11  christos 	state->MaxConnections = sess->s_MaxConnections;
   1207  1.11  christos 	state->DefaultTime2Wait = sess->s_DefaultTime2Wait;
   1208  1.11  christos 	state->DefaultTime2Retain = sess->s_DefaultTime2Retain;
   1209  1.11  christos 	state->MaxBurstLength = sess->s_MaxBurstLength;
   1210  1.11  christos 	state->FirstBurstLength = sess->s_FirstBurstLength;
   1211  1.11  christos 	state->MaxOutstandingR2T = sess->s_MaxOutstandingR2T;
   1212   1.1       agc }
   1213   1.1       agc 
   1214   1.1       agc 
   1215   1.1       agc 
   1216   1.1       agc /*
   1217   1.1       agc  * assemble_login_parameters:
   1218   1.1       agc  *    Assemble the initial login negotiation parameters.
   1219   1.1       agc  *
   1220   1.1       agc  *    Parameter:
   1221   1.1       agc  *          conn     The connection
   1222   1.1       agc  *          ccb      The CCB for the login exchange
   1223   1.1       agc  *          pdu      The PDU to use for sending
   1224   1.1       agc  *
   1225   1.1       agc  *    Returns:    < 0   if more security negotiation is required
   1226   1.1       agc  *                0     if this is the last security negotiation block
   1227   1.1       agc  *                > 0   (an ISCSI error code) if an error occurred.
   1228   1.1       agc  */
   1229   1.1       agc 
   1230   1.1       agc int
   1231   1.1       agc assemble_login_parameters(connection_t *conn, ccb_t *ccb, pdu_t *pdu)
   1232   1.1       agc {
   1233  1.11  christos 	iscsi_login_parameters_t *par = conn->c_login_par;
   1234   1.1       agc 	size_t sz;
   1235   1.1       agc 	int rc, i, next;
   1236   1.1       agc 	negotiation_state_t *state;
   1237   1.1       agc 	negotiation_parameter_t *cpar;
   1238   1.1       agc 
   1239   1.1       agc 	state = malloc(sizeof(*state), M_TEMP, M_WAITOK | M_ZERO);
   1240   1.1       agc 	if (state == NULL) {
   1241   1.1       agc 		DEBOUT(("*** Out of memory in assemble_login_params\n"));
   1242   1.1       agc 		return ISCSI_STATUS_NO_RESOURCES;
   1243   1.1       agc 	}
   1244  1.11  christos 	ccb->ccb_temp_data = state;
   1245   1.1       agc 
   1246   1.6   mlelstv 	if (!iscsi_InitiatorName[0]) {
   1247   1.1       agc 		DEBOUT(("No InitiatorName\n"));
   1248   1.1       agc 		return ISCSI_STATUS_PARAMETER_MISSING;
   1249   1.1       agc 	}
   1250   1.6   mlelstv 	set_key_s(state, K_InitiatorName, iscsi_InitiatorName);
   1251   1.1       agc 
   1252   1.6   mlelstv 	if (iscsi_InitiatorAlias[0])
   1253   1.6   mlelstv 		set_key_s(state, K_InitiatorAlias, iscsi_InitiatorAlias);
   1254   1.1       agc 
   1255  1.11  christos 	conn->c_Our_MaxRecvDataSegmentLength =
   1256   1.1       agc 		(par->is_present.MaxRecvDataSegmentLength)
   1257   1.1       agc 		? par->MaxRecvDataSegmentLength : DEFAULT_MaxRecvDataSegmentLength;
   1258   1.1       agc 
   1259   1.1       agc 	/* setup some values for authentication */
   1260   1.1       agc 	if (par->is_present.password)
   1261   1.1       agc 		copyinstr(par->password, state->password, MAX_STRING, &sz);
   1262   1.1       agc 	if (par->is_present.target_password)
   1263   1.1       agc 		copyinstr(par->target_password, state->target_password,
   1264   1.1       agc 			MAX_STRING, &sz);
   1265   1.1       agc 	if (par->is_present.user_name)
   1266   1.1       agc 		copyinstr(par->user_name, state->user_name, MAX_STRING, &sz);
   1267   1.1       agc 	else
   1268   1.6   mlelstv 		strlcpy(state->user_name, iscsi_InitiatorName,
   1269   1.1       agc 			sizeof(state->user_name));
   1270   1.1       agc 
   1271   1.1       agc 	next = TRUE;
   1272   1.1       agc 
   1273   1.1       agc 	set_key_n(state, K_SessionType,
   1274   1.1       agc 			  par->login_type > ISCSI_LOGINTYPE_DISCOVERY);
   1275   1.1       agc 
   1276   1.1       agc 	cpar = set_key_n(state, K_AuthMethod, ISCSI_AUTH_None);
   1277   1.1       agc 
   1278   1.1       agc 	if (cpar != NULL && par->is_present.auth_info &&
   1279   1.1       agc 		par->auth_info.auth_number > 0) {
   1280   1.1       agc 		if (par->auth_info.auth_number > ISCSI_AUTH_OPTIONS) {
   1281   1.1       agc 			DEBOUT(("Auth number too big in asm_login\n"));
   1282   1.1       agc 			return ISCSI_STATUS_PARAMETER_INVALID;
   1283   1.1       agc 		}
   1284   1.1       agc 		cpar->list_num = par->auth_info.auth_number;
   1285   1.1       agc 		for (i = 0; i < cpar->list_num; i++) {
   1286   1.1       agc 			cpar->val.nval[i] = par->auth_info.auth_type[i];
   1287   1.1       agc 			if (par->auth_info.auth_type[i])
   1288   1.1       agc 				next = FALSE;
   1289   1.1       agc 		}
   1290   1.1       agc 	}
   1291   1.1       agc 
   1292   1.1       agc 	if (par->is_present.TargetName)
   1293   1.1       agc 		copyinstr(par->TargetName, state->temp_buf, ISCSI_STRING_LENGTH - 1,
   1294   1.1       agc 				  &sz);
   1295   1.1       agc 	else {
   1296   1.1       agc 		state->temp_buf[0] = 0;
   1297   1.1       agc 		sz = 0;
   1298   1.1       agc 	}
   1299   1.1       agc 
   1300   1.1       agc 	if ((!sz || !state->temp_buf[0]) &&
   1301   1.1       agc 		par->login_type != ISCSI_LOGINTYPE_DISCOVERY) {
   1302   1.1       agc 		DEBOUT(("No TargetName\n"));
   1303   1.1       agc 		return ISCSI_STATUS_PARAMETER_MISSING;
   1304   1.1       agc 	}
   1305   1.1       agc 
   1306   1.1       agc 	if (state->temp_buf[0]) {
   1307   1.1       agc 		set_key_s(state, K_TargetName, state->temp_buf);
   1308   1.1       agc 	}
   1309   1.1       agc 
   1310   1.1       agc 	if ((rc = complete_pars(state, pdu)) != 0)
   1311   1.1       agc 		return rc;
   1312   1.1       agc 
   1313   1.1       agc 	return (next) ? 0 : -1;
   1314   1.1       agc }
   1315   1.1       agc 
   1316   1.1       agc 
   1317   1.1       agc /*
   1318   1.1       agc  * assemble_security_parameters:
   1319   1.1       agc  *    Assemble the security negotiation parameters.
   1320   1.1       agc  *
   1321   1.1       agc  *    Parameter:
   1322   1.1       agc  *          conn     The connection
   1323   1.1       agc  *          rx_pdu   The received login response PDU
   1324   1.1       agc  *          tx_pdu   The transmit PDU
   1325   1.1       agc  *
   1326   1.1       agc  *    Returns:    < 0   if more security negotiation is required
   1327   1.1       agc  *                0     if this is the last security negotiation block
   1328   1.1       agc  *                > 0   (an ISCSI error code) if an error occurred.
   1329   1.1       agc  */
   1330   1.1       agc 
   1331   1.1       agc int
   1332   1.1       agc assemble_security_parameters(connection_t *conn, ccb_t *ccb, pdu_t *rx_pdu,
   1333   1.1       agc 							 pdu_t *tx_pdu)
   1334   1.1       agc {
   1335  1.11  christos 	negotiation_state_t *state = (negotiation_state_t *) ccb->ccb_temp_data;
   1336  1.11  christos 	iscsi_login_parameters_t *par = conn->c_login_par;
   1337   1.1       agc 	negotiation_parameter_t rxp, *cpar;
   1338   1.1       agc 	uint8_t *rxpars;
   1339   1.1       agc 	int rc, next;
   1340   1.1       agc 	uint8_t identifier = 0;
   1341   1.1       agc 	uint8_t *challenge = NULL;
   1342   1.1       agc 	int challenge_size = 0;
   1343   1.1       agc 	uint8_t *response = NULL;
   1344   1.1       agc 	int response_size = 0;
   1345   1.1       agc 
   1346   1.1       agc 	state->num_pars = 0;
   1347   1.1       agc 	next = 0;
   1348   1.1       agc 
   1349  1.11  christos 	rxpars = (uint8_t *) rx_pdu->pdu_temp_data;
   1350   1.1       agc 	if (rxpars == NULL) {
   1351   1.1       agc 		DEBOUT(("No received parameters!\n"));
   1352   1.1       agc 		return ISCSI_STATUS_NEGOTIATION_ERROR;
   1353   1.1       agc 	}
   1354   1.1       agc 	/* Note: There are always at least 2 extra bytes past temp_data_len */
   1355  1.11  christos 	rxpars[rx_pdu->pdu_temp_data_len] = '\0';
   1356  1.11  christos 	rxpars[rx_pdu->pdu_temp_data_len + 1] = '\0';
   1357   1.1       agc 
   1358   1.1       agc 	while (*rxpars) {
   1359   1.1       agc 		if ((rxpars = get_parameter(rxpars, &rxp)) == NULL) {
   1360   1.1       agc 			DEBOUT(("get_parameter returned error\n"));
   1361   1.1       agc 			return ISCSI_STATUS_NEGOTIATION_ERROR;
   1362   1.1       agc 		}
   1363   1.1       agc 
   1364   1.1       agc 		state->kflags[rxp.key] |= NS_RECEIVED;
   1365   1.1       agc 
   1366   1.1       agc 		switch (rxp.key) {
   1367   1.1       agc 		case K_AuthMethod:
   1368   1.1       agc 			if (state->auth_state != AUTH_INITIAL) {
   1369   1.1       agc 				DEBOUT(("AuthMethod received, auth_state = %d\n",
   1370   1.1       agc 						state->auth_state));
   1371   1.1       agc 				return ISCSI_STATUS_NEGOTIATION_ERROR;
   1372   1.1       agc 			}
   1373   1.1       agc 
   1374   1.1       agc 			/* Note: if the selection is None, we shouldn't be here,
   1375   1.1       agc 			 * the target should have transited the state to op-neg.
   1376   1.1       agc 			 */
   1377   1.1       agc 			if (rxp.val.nval[0] != ISCSI_AUTH_CHAP) {
   1378   1.1       agc 				DEBOUT(("AuthMethod isn't CHAP (%d)\n", rxp.val.nval[0]));
   1379   1.1       agc 				return ISCSI_STATUS_NEGOTIATION_ERROR;
   1380   1.1       agc 			}
   1381   1.1       agc 
   1382   1.1       agc 			state->auth_state = AUTH_METHOD_SELECTED;
   1383   1.1       agc 			state->auth_alg = rxp.val.nval[0];
   1384   1.1       agc 			break;
   1385   1.1       agc 
   1386   1.1       agc 		case K_Auth_CHAP_Algorithm:
   1387   1.1       agc 			if (state->auth_state != AUTH_CHAP_ALG_SENT ||
   1388   1.1       agc 				rxp.val.nval[0] != 5) {
   1389   1.1       agc 				DEBOUT(("Bad algorithm, auth_state = %d, alg %d\n",
   1390   1.1       agc 						state->auth_state, rxp.val.nval[0]));
   1391   1.1       agc 				return ISCSI_STATUS_NEGOTIATION_ERROR;
   1392   1.1       agc 			}
   1393   1.1       agc 			break;
   1394   1.1       agc 
   1395   1.1       agc 		case K_Auth_CHAP_Challenge:
   1396   1.1       agc 			if (state->auth_state != AUTH_CHAP_ALG_SENT || !rxp.list_num) {
   1397   1.1       agc 				DEBOUT(("Bad Challenge, auth_state = %d, len %d\n",
   1398   1.1       agc 						state->auth_state, rxp.list_num));
   1399   1.1       agc 				return ISCSI_STATUS_NEGOTIATION_ERROR;
   1400   1.1       agc 			}
   1401   1.1       agc 			challenge = rxp.val.sval;
   1402   1.1       agc 			challenge_size = rxp.list_num;
   1403   1.1       agc 			break;
   1404   1.1       agc 
   1405   1.1       agc 		case K_Auth_CHAP_Identifier:
   1406   1.1       agc 			if (state->auth_state != AUTH_CHAP_ALG_SENT) {
   1407   1.1       agc 				DEBOUT(("Bad ID, auth_state = %d, id %d\n",
   1408   1.1       agc 						state->auth_state, rxp.val.nval[0]));
   1409   1.1       agc 				return ISCSI_STATUS_NEGOTIATION_ERROR;
   1410   1.1       agc 			}
   1411   1.1       agc 			identifier = (uint8_t) rxp.val.nval[0];
   1412   1.1       agc 			break;
   1413   1.1       agc 
   1414   1.1       agc 		case K_Auth_CHAP_Name:
   1415   1.1       agc 			if (state->auth_state != AUTH_CHAP_RSP_SENT) {
   1416   1.1       agc 				DEBOUT(("Bad Name, auth_state = %d, name <%s>\n",
   1417   1.1       agc 						state->auth_state, rxp.val.sval));
   1418   1.1       agc 				return ISCSI_STATUS_NEGOTIATION_ERROR;
   1419   1.1       agc 			}
   1420   1.1       agc 			/* what do we do with the name?? */
   1421   1.1       agc 			break;
   1422   1.1       agc 
   1423   1.1       agc 		case K_Auth_CHAP_Response:
   1424   1.1       agc 			if (state->auth_state != AUTH_CHAP_RSP_SENT) {
   1425   1.1       agc 				DEBOUT(("Bad Response, auth_state = %d, size %d\n",
   1426   1.1       agc 						state->auth_state, rxp.list_num));
   1427   1.1       agc 				return ISCSI_STATUS_NEGOTIATION_ERROR;
   1428   1.1       agc 			}
   1429   1.1       agc 			response = rxp.val.sval;
   1430   1.1       agc 			response_size = rxp.list_num;
   1431   1.1       agc 			if (response_size != CHAP_MD5_SIZE)
   1432   1.1       agc 				return ISCSI_STATUS_NEGOTIATION_ERROR;
   1433   1.1       agc 			break;
   1434   1.1       agc 
   1435   1.1       agc 		default:
   1436   1.1       agc 			rc = eval_parameter(conn, state, &rxp);
   1437   1.1       agc 			if (rc)
   1438   1.1       agc 				return rc;
   1439   1.1       agc 			break;
   1440   1.1       agc 		}
   1441   1.1       agc 	}
   1442   1.1       agc 
   1443   1.1       agc 	switch (state->auth_state) {
   1444   1.1       agc 	case AUTH_INITIAL:
   1445   1.1       agc 		DEBOUT(("Didn't receive Method\n"));
   1446   1.1       agc 		return ISCSI_STATUS_NEGOTIATION_ERROR;
   1447   1.1       agc 
   1448   1.1       agc 	case AUTH_METHOD_SELECTED:
   1449   1.1       agc 		set_key_n(state, K_Auth_CHAP_Algorithm, 5);
   1450   1.1       agc 		state->auth_state = AUTH_CHAP_ALG_SENT;
   1451   1.1       agc 		next = -1;
   1452   1.1       agc 		break;
   1453   1.1       agc 
   1454   1.1       agc 	case AUTH_CHAP_ALG_SENT:
   1455   1.1       agc 		if (!RX(state, K_Auth_CHAP_Algorithm) ||
   1456   1.1       agc 			!RX(state, K_Auth_CHAP_Identifier) ||
   1457   1.1       agc 			!RX(state, K_Auth_CHAP_Challenge)) {
   1458   1.1       agc 			DEBOUT(("Didn't receive all parameters\n"));
   1459   1.1       agc 			return ISCSI_STATUS_NEGOTIATION_ERROR;
   1460   1.1       agc 		}
   1461   1.1       agc 
   1462   1.1       agc 		set_key_s(state, K_Auth_CHAP_Name, state->user_name);
   1463   1.1       agc 
   1464   1.1       agc 		chap_md5_response(state->temp_buf, identifier, state->password,
   1465   1.1       agc 						  challenge, challenge_size);
   1466   1.1       agc 
   1467   1.1       agc 		cpar = set_key_s(state, K_Auth_CHAP_Response, state->temp_buf);
   1468   1.1       agc 		if (cpar != NULL)
   1469   1.1       agc 			cpar->list_num = CHAP_MD5_SIZE;
   1470   1.1       agc 
   1471   1.1       agc 		if (par->auth_info.mutual_auth) {
   1472   1.1       agc 			if (!state->target_password[0]) {
   1473   1.1       agc 				DEBOUT(("No target password with mutual authentication!\n"));
   1474   1.1       agc 				return ISCSI_STATUS_PARAMETER_MISSING;
   1475   1.1       agc 			}
   1476   1.1       agc 
   1477   1.2       tls 			cprng_strong(kern_cprng,
   1478   1.2       tls 				     &state->temp_buf[CHAP_MD5_SIZE],
   1479   1.3       tls 				     CHAP_CHALLENGE_LEN + 1, 0);
   1480   1.1       agc 			set_key_n(state, K_Auth_CHAP_Identifier,
   1481   1.1       agc 					  state->temp_buf[CHAP_MD5_SIZE]);
   1482   1.1       agc 			cpar = set_key_s(state, K_Auth_CHAP_Challenge,
   1483   1.1       agc 							 &state->temp_buf[CHAP_MD5_SIZE + 1]);
   1484   1.1       agc 			if (cpar != NULL)
   1485   1.1       agc 				cpar->list_num = CHAP_CHALLENGE_LEN;
   1486   1.1       agc 			next = -1;
   1487   1.1       agc 		}
   1488   1.1       agc 		state->auth_state = AUTH_CHAP_RSP_SENT;
   1489   1.1       agc 		break;
   1490   1.1       agc 
   1491   1.1       agc 	case AUTH_CHAP_RSP_SENT:
   1492   1.1       agc 		/* we can only be here for mutual authentication */
   1493   1.1       agc 		if (!par->auth_info.mutual_auth || response == NULL) {
   1494   1.1       agc 			DEBOUT(("Mutual authentication not requested\n"));
   1495   1.1       agc 			return ISCSI_STATUS_NEGOTIATION_ERROR;
   1496   1.1       agc 		}
   1497   1.1       agc 
   1498   1.1       agc 		chap_md5_response(state->temp_buf,
   1499   1.1       agc 				state->temp_buf[CHAP_MD5_SIZE],
   1500   1.1       agc 				state->password,
   1501   1.1       agc 				&state->temp_buf[CHAP_MD5_SIZE + 1],
   1502   1.1       agc 				CHAP_CHALLENGE_LEN);
   1503   1.1       agc 
   1504   1.1       agc 		if (memcmp(state->temp_buf, response, response_size)) {
   1505   1.1       agc 			DEBOUT(("Mutual authentication mismatch\n"));
   1506   1.1       agc 			return ISCSI_STATUS_AUTHENTICATION_FAILED;
   1507   1.1       agc 		}
   1508   1.1       agc 		break;
   1509   1.1       agc 
   1510   1.1       agc 	default:
   1511   1.1       agc 		break;
   1512   1.1       agc 	}
   1513   1.1       agc 
   1514   1.1       agc 	complete_pars(state, tx_pdu);
   1515   1.1       agc 
   1516   1.1       agc 	return next;
   1517   1.1       agc }
   1518   1.1       agc 
   1519   1.1       agc 
   1520   1.1       agc /*
   1521   1.1       agc  * set_first_opnegs:
   1522   1.1       agc  *    Set the operational negotiation parameters we want to negotiate in
   1523   1.1       agc  *    the first login request in op_neg phase.
   1524   1.1       agc  *
   1525   1.1       agc  *    Parameter:
   1526   1.1       agc  *          conn     The connection
   1527   1.1       agc  *          state    Negotiation state
   1528   1.1       agc  */
   1529   1.1       agc 
   1530   1.1       agc STATIC void
   1531   1.1       agc set_first_opnegs(connection_t *conn, negotiation_state_t *state)
   1532   1.1       agc {
   1533  1.11  christos 	iscsi_login_parameters_t *lpar = conn->c_login_par;
   1534   1.1       agc 	negotiation_parameter_t *cpar;
   1535   1.1       agc 
   1536   1.1       agc     /* Digests - suggest None,CRC32C unless the user forces a value */
   1537   1.1       agc 	cpar = set_key_n(state, K_HeaderDigest,
   1538   1.1       agc 					 (lpar->is_present.HeaderDigest) ? lpar->HeaderDigest : 0);
   1539   1.1       agc 	if (cpar != NULL && !lpar->is_present.HeaderDigest) {
   1540   1.1       agc 		cpar->list_num = 2;
   1541   1.1       agc 		cpar->val.nval[1] = 1;
   1542   1.1       agc 	}
   1543   1.1       agc 
   1544   1.1       agc 	cpar = set_key_n(state, K_DataDigest, (lpar->is_present.DataDigest)
   1545   1.1       agc 		? lpar->DataDigest : 0);
   1546   1.1       agc 	if (cpar != NULL && !lpar->is_present.DataDigest) {
   1547   1.1       agc 		cpar->list_num = 2;
   1548   1.1       agc 		cpar->val.nval[1] = 1;
   1549   1.1       agc 	}
   1550   1.1       agc 
   1551   1.1       agc 	set_key_n(state, K_MaxRecvDataSegmentLength,
   1552  1.11  christos 		conn->c_Our_MaxRecvDataSegmentLength);
   1553   1.1       agc 	/* This is direction-specific, we may have a different default */
   1554   1.1       agc 	state->MaxRecvDataSegmentLength =
   1555   1.1       agc 		entries[K_MaxRecvDataSegmentLength].defval;
   1556   1.1       agc 
   1557   1.1       agc 	/* First connection only */
   1558  1.11  christos 	if (!conn->c_session->s_TSIH) {
   1559   1.1       agc 		state->ErrorRecoveryLevel =
   1560   1.1       agc 			(lpar->is_present.ErrorRecoveryLevel) ? lpar->ErrorRecoveryLevel
   1561   1.1       agc 												  : 2;
   1562   1.1       agc 		/*
   1563   1.1       agc 		   Negotiate InitialR2T to FALSE and ImmediateData to TRUE, should
   1564   1.1       agc 		   be slightly more efficient than the default InitialR2T=TRUE.
   1565   1.1       agc 		 */
   1566   1.1       agc 		state->InitialR2T = FALSE;
   1567   1.1       agc 		state->ImmediateData = TRUE;
   1568   1.1       agc 
   1569   1.1       agc 		/* We don't really care about this, so don't negotiate by default */
   1570   1.1       agc 		state->MaxBurstLength = entries[K_MaxBurstLength].defval;
   1571   1.1       agc 		state->FirstBurstLength = entries[K_FirstBurstLength].defval;
   1572   1.1       agc 		state->MaxOutstandingR2T = entries[K_MaxOutstandingR2T].defval;
   1573   1.1       agc 
   1574   1.1       agc 		set_key_n(state, K_ErrorRecoveryLevel, state->ErrorRecoveryLevel);
   1575   1.1       agc 		set_key_n(state, K_InitialR2T, state->InitialR2T);
   1576   1.1       agc 		set_key_n(state, K_ImmediateData, state->ImmediateData);
   1577   1.1       agc 
   1578   1.1       agc 		if (lpar->is_present.MaxConnections) {
   1579   1.1       agc 			state->MaxConnections = lpar->MaxConnections;
   1580   1.1       agc 			set_key_n(state, K_MaxConnections, lpar->MaxConnections);
   1581   1.1       agc 		}
   1582   1.1       agc 
   1583   1.1       agc 		if (lpar->is_present.DefaultTime2Wait)
   1584   1.1       agc 			set_key_n(state, K_DefaultTime2Wait, lpar->DefaultTime2Wait);
   1585   1.1       agc 		else
   1586   1.1       agc 			state->DefaultTime2Wait = entries[K_DefaultTime2Wait].defval;
   1587   1.1       agc 
   1588   1.1       agc 		if (lpar->is_present.DefaultTime2Retain)
   1589   1.1       agc 			set_key_n(state, K_DefaultTime2Retain, lpar->DefaultTime2Retain);
   1590   1.1       agc 		else
   1591   1.1       agc 			state->DefaultTime2Retain = entries[K_DefaultTime2Retain].defval;
   1592   1.1       agc 	} else
   1593  1.11  christos 		init_session_parameters(conn->c_session, state);
   1594   1.1       agc 
   1595   1.1       agc 	DEBC(conn, 10, ("SetFirstOpnegs: recover=%d, MRDSL=%d\n",
   1596  1.11  christos 		conn->c_recover, state->MaxRecvDataSegmentLength));
   1597   1.1       agc }
   1598   1.1       agc 
   1599   1.1       agc 
   1600   1.1       agc /*
   1601   1.1       agc  * assemble_negotiation_parameters:
   1602   1.1       agc  *    Assemble any negotiation parameters requested by the other side.
   1603   1.1       agc  *
   1604   1.1       agc  *    Parameter:
   1605   1.1       agc  *          conn     The connection
   1606   1.1       agc  *          ccb      The login ccb
   1607   1.1       agc  *          rx_pdu   The received login response PDU
   1608   1.1       agc  *          tx_pdu   The transmit PDU
   1609   1.1       agc  *
   1610   1.1       agc  *    Returns:    0     On success
   1611   1.1       agc  *                > 0   (an ISCSI error code) if an error occurred.
   1612   1.1       agc  */
   1613   1.1       agc 
   1614   1.1       agc int
   1615   1.1       agc assemble_negotiation_parameters(connection_t *conn, ccb_t *ccb, pdu_t *rx_pdu,
   1616   1.1       agc 							    pdu_t *tx_pdu)
   1617   1.1       agc {
   1618  1.11  christos 	negotiation_state_t *state = (negotiation_state_t *) ccb->ccb_temp_data;
   1619   1.1       agc 	negotiation_parameter_t rxp;
   1620   1.1       agc 	uint8_t *rxpars;
   1621   1.1       agc 	int rc;
   1622   1.1       agc 
   1623   1.1       agc 	state->num_pars = 0;
   1624   1.1       agc 
   1625   1.1       agc 	DEBC(conn, 10, ("AsmNegParams: connState=%d, MRDSL=%d\n",
   1626  1.11  christos 		conn->c_state, state->MaxRecvDataSegmentLength));
   1627   1.1       agc 
   1628  1.11  christos 	if (conn->c_state == ST_SEC_NEG) {
   1629  1.11  christos 		conn->c_state = ST_OP_NEG;
   1630   1.1       agc 		set_first_opnegs(conn, state);
   1631   1.1       agc 	}
   1632   1.1       agc 
   1633  1.11  christos 	rxpars = (uint8_t *) rx_pdu->pdu_temp_data;
   1634   1.1       agc 	if (rxpars != NULL) {
   1635   1.1       agc 		/* Note: There are always at least 2 extra bytes past temp_data_len */
   1636  1.11  christos 		rxpars[rx_pdu->pdu_temp_data_len] = '\0';
   1637  1.11  christos 		rxpars[rx_pdu->pdu_temp_data_len + 1] = '\0';
   1638   1.1       agc 
   1639   1.1       agc 		while (*rxpars) {
   1640   1.1       agc 			if ((rxpars = get_parameter(rxpars, &rxp)) == NULL)
   1641   1.1       agc 				return ISCSI_STATUS_NEGOTIATION_ERROR;
   1642   1.1       agc 
   1643   1.1       agc 			rc = eval_parameter(conn, state, &rxp);
   1644   1.1       agc 			if (rc)
   1645   1.1       agc 				return rc;
   1646   1.1       agc 		}
   1647   1.1       agc 	}
   1648   1.1       agc 
   1649   1.1       agc 	if (tx_pdu == NULL)
   1650   1.1       agc 		return 0;
   1651   1.1       agc 
   1652   1.1       agc 	complete_pars(state, tx_pdu);
   1653   1.1       agc 
   1654   1.1       agc 	return 0;
   1655   1.1       agc }
   1656   1.1       agc 
   1657   1.1       agc /*
   1658   1.1       agc  * init_text_parameters:
   1659   1.1       agc  *    Initialize text negotiation.
   1660   1.1       agc  *
   1661   1.1       agc  *    Parameter:
   1662   1.1       agc  *          conn     The connection
   1663   1.1       agc  *          tx_pdu   The transmit PDU
   1664   1.1       agc  *
   1665   1.1       agc  *    Returns:    0     On success
   1666   1.1       agc  *                > 0   (an ISCSI error code) if an error occurred.
   1667   1.1       agc  */
   1668   1.1       agc 
   1669   1.1       agc int
   1670   1.1       agc init_text_parameters(connection_t *conn, ccb_t *ccb)
   1671   1.1       agc {
   1672   1.1       agc 	negotiation_state_t *state;
   1673   1.1       agc 
   1674   1.1       agc 	state = malloc(sizeof(*state), M_TEMP, M_WAITOK | M_ZERO);
   1675   1.1       agc 	if (state == NULL) {
   1676   1.1       agc 		DEBOUT(("*** Out of memory in init_text_params\n"));
   1677   1.1       agc 		return ISCSI_STATUS_NO_RESOURCES;
   1678   1.1       agc 	}
   1679  1.11  christos 	ccb->ccb_temp_data = state;
   1680   1.1       agc 
   1681  1.11  christos 	state->HeaderDigest = conn->c_HeaderDigest;
   1682  1.11  christos 	state->DataDigest = conn->c_DataDigest;
   1683  1.11  christos 	state->MaxRecvDataSegmentLength = conn->c_MaxRecvDataSegmentLength;
   1684  1.11  christos 	init_session_parameters(conn->c_session, state);
   1685   1.1       agc 
   1686   1.1       agc 	return 0;
   1687   1.1       agc }
   1688   1.1       agc 
   1689   1.1       agc 
   1690   1.1       agc /*
   1691   1.1       agc  * assemble_send_targets:
   1692   1.1       agc  *    Assemble send targets request
   1693   1.1       agc  *
   1694   1.1       agc  *    Parameter:
   1695   1.1       agc  *          pdu      The transmit PDU
   1696   1.1       agc  *          val      The SendTargets key value
   1697   1.1       agc  *
   1698   1.1       agc  *    Returns:    0     On success
   1699   1.1       agc  *                > 0   (an ISCSI error code) if an error occurred.
   1700   1.1       agc  */
   1701   1.1       agc 
   1702   1.1       agc int
   1703   1.1       agc assemble_send_targets(pdu_t *pdu, uint8_t *val)
   1704   1.1       agc {
   1705   1.1       agc 	negotiation_parameter_t par;
   1706   1.1       agc 	uint8_t *buf;
   1707   1.1       agc 	int len;
   1708   1.1       agc 
   1709   1.1       agc 	par.key = K_SendTargets;
   1710   1.1       agc 	par.list_num = 1;
   1711   1.1       agc 	par.val.sval = val;
   1712   1.1       agc 
   1713   1.1       agc 	len = parameter_size(&par);
   1714   1.1       agc 
   1715   1.1       agc 	if ((buf = malloc(len, M_TEMP, M_WAITOK)) == NULL) {
   1716   1.1       agc 		DEBOUT(("*** Out of memory in assemble_send_targets\n"));
   1717   1.1       agc 		return ISCSI_STATUS_NO_RESOURCES;
   1718   1.1       agc 	}
   1719  1.11  christos 	pdu->pdu_temp_data = buf;
   1720  1.11  christos 	pdu->pdu_temp_data_len = len;
   1721   1.1       agc 
   1722   1.1       agc 	if (put_parameter(buf, len, &par) == 0)
   1723   1.1       agc 		return ISCSI_STATUS_PARAMETER_INVALID;
   1724   1.1       agc 
   1725   1.1       agc 	return 0;
   1726   1.1       agc }
   1727   1.1       agc 
   1728   1.1       agc 
   1729   1.1       agc /*
   1730   1.1       agc  * set_negotiated_parameters:
   1731   1.1       agc  *    Copy the negotiated parameters into the connection and session structure.
   1732   1.1       agc  *
   1733   1.1       agc  *    Parameter:
   1734   1.1       agc  *          ccb      The ccb containing the state information
   1735   1.1       agc  */
   1736   1.1       agc 
   1737   1.1       agc void
   1738   1.1       agc set_negotiated_parameters(ccb_t *ccb)
   1739   1.1       agc {
   1740  1.11  christos 	negotiation_state_t *state = (negotiation_state_t *) ccb->ccb_temp_data;
   1741  1.11  christos 	connection_t *conn = ccb->ccb_connection;
   1742  1.11  christos 	session_t *sess = ccb->ccb_session;
   1743  1.11  christos 
   1744  1.11  christos 	conn->c_HeaderDigest = state->HeaderDigest;
   1745  1.11  christos 	conn->c_DataDigest = state->DataDigest;
   1746  1.11  christos 	sess->s_ErrorRecoveryLevel = state->ErrorRecoveryLevel;
   1747  1.11  christos 	sess->s_InitialR2T = state->InitialR2T;
   1748  1.11  christos 	sess->s_ImmediateData = state->ImmediateData;
   1749  1.11  christos 	conn->c_MaxRecvDataSegmentLength = state->MaxRecvDataSegmentLength;
   1750  1.11  christos 	sess->s_MaxConnections = state->MaxConnections;
   1751  1.11  christos 	sess->s_DefaultTime2Wait = conn->c_Time2Wait = state->DefaultTime2Wait;
   1752  1.11  christos 	sess->s_DefaultTime2Retain = conn->c_Time2Retain =
   1753   1.1       agc 		state->DefaultTime2Retain;
   1754   1.1       agc 
   1755   1.1       agc 	/* set idle connection timeout to half the Time2Retain window so we */
   1756   1.1       agc 	/* don't miss it, unless Time2Retain is ridiculously small. */
   1757  1.11  christos 	conn->c_idle_timeout_val = (conn->c_Time2Retain >= 10) ?
   1758  1.11  christos 		(conn->c_Time2Retain / 2) * hz : CONNECTION_IDLE_TIMEOUT;
   1759   1.1       agc 
   1760  1.11  christos 	sess->s_MaxBurstLength = state->MaxBurstLength;
   1761  1.11  christos 	sess->s_FirstBurstLength = state->FirstBurstLength;
   1762  1.11  christos 	sess->s_MaxOutstandingR2T = state->MaxOutstandingR2T;
   1763   1.1       agc 
   1764   1.1       agc 	DEBC(conn, 10,("SetNegPar: MRDSL=%d, MBL=%d, FBL=%d, IR2T=%d, ImD=%d\n",
   1765   1.1       agc 		state->MaxRecvDataSegmentLength, state->MaxBurstLength,
   1766   1.1       agc 		state->FirstBurstLength, state->InitialR2T,
   1767   1.1       agc 		state->ImmediateData));
   1768   1.1       agc 
   1769  1.11  christos 	conn->c_max_transfer = min(sess->s_MaxBurstLength, conn->c_MaxRecvDataSegmentLength);
   1770   1.1       agc 
   1771  1.11  christos 	conn->c_max_firstimmed = (!sess->s_ImmediateData) ? 0 :
   1772  1.11  christos 				min(sess->s_FirstBurstLength, conn->c_max_transfer);
   1773   1.1       agc 
   1774  1.11  christos 	conn->c_max_firstdata = (sess->s_InitialR2T || sess->s_FirstBurstLength < conn->c_max_firstimmed) ? 0 :
   1775  1.11  christos 				min(sess->s_FirstBurstLength - conn->c_max_firstimmed, conn->c_max_transfer);
   1776   1.4   mlelstv 
   1777   1.1       agc }
   1778