Browse Source

[iscsi] Rewrite unrelicensable portions of iscsi.c

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 years ago
parent
commit
c1ac466838
1 changed files with 28 additions and 36 deletions
  1. 28
    36
      src/net/tcp/iscsi.c

+ 28
- 36
src/net/tcp/iscsi.c View File

15
  * along with this program; if not, write to the Free Software
15
  * along with this program; if not, write to the Free Software
16
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
  * 02110-1301, USA.
17
  * 02110-1301, USA.
18
+ *
19
+ * You can also choose to distribute this program under the terms of
20
+ * the Unmodified Binary Distribution Licence (as given in the file
21
+ * COPYING.UBDL), provided that you have satisfied its requirements.
18
  */
22
  */
19
 
23
 
20
-FILE_LICENCE ( GPL2_OR_LATER );
24
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
21
 
25
 
22
 #include <stddef.h>
26
 #include <stddef.h>
23
 #include <string.h>
27
 #include <string.h>
24
 #include <stdlib.h>
28
 #include <stdlib.h>
25
 #include <stdio.h>
29
 #include <stdio.h>
30
+#include <ctype.h>
26
 #include <errno.h>
31
 #include <errno.h>
27
 #include <assert.h>
32
 #include <assert.h>
28
 #include <byteswap.h>
33
 #include <byteswap.h>
127
 #define EPROTO_INVALID_LARGE_BINARY \
132
 #define EPROTO_INVALID_LARGE_BINARY \
128
 	__einfo_error ( EINFO_EPROTO_INVALID_LARGE_BINARY )
133
 	__einfo_error ( EINFO_EPROTO_INVALID_LARGE_BINARY )
129
 #define EINFO_EPROTO_INVALID_LARGE_BINARY \
134
 #define EINFO_EPROTO_INVALID_LARGE_BINARY \
130
-	__einfo_uniqify ( EINFO_EPROTO, 0x03, "Invalid large binary" )
135
+	__einfo_uniqify ( EINFO_EPROTO, 0x03, "Invalid large binary value" )
131
 #define EPROTO_INVALID_CHAP_RESPONSE \
136
 #define EPROTO_INVALID_CHAP_RESPONSE \
132
 	__einfo_error ( EINFO_EPROTO_INVALID_CHAP_RESPONSE )
137
 	__einfo_error ( EINFO_EPROTO_INVALID_CHAP_RESPONSE )
133
 #define EINFO_EPROTO_INVALID_CHAP_RESPONSE \
138
 #define EINFO_EPROTO_INVALID_CHAP_RESPONSE \
823
 	return xfer_deliver_iob ( &iscsi->socket, iobuf );
828
 	return xfer_deliver_iob ( &iscsi->socket, iobuf );
824
 }
829
 }
825
 
830
 
826
-/**
827
- * Calculate maximum length of decoded large binary value
828
- *
829
- * @v encoded		Encoded large binary value
830
- * @v max_raw_len	Maximum length of raw data
831
- */
832
-static inline size_t
833
-iscsi_large_binary_decoded_max_len ( const char *encoded ) {
834
-	return ( strlen ( encoded ) ); /* Decoding never expands data */
835
-}
836
-
837
 /**
831
 /**
838
  * Decode large binary value
832
  * Decode large binary value
839
  *
833
  *
843
  */
837
  */
844
 static int iscsi_large_binary_decode ( const char *encoded, uint8_t *raw ) {
838
 static int iscsi_large_binary_decode ( const char *encoded, uint8_t *raw ) {
845
 
839
 
846
-	if ( encoded[0] != '0' )
847
-		return -EPROTO_INVALID_LARGE_BINARY;
848
-
849
-	switch ( encoded[1] ) {
850
-	case 'x' :
851
-	case 'X' :
852
-		return base16_decode ( ( encoded + 2 ), raw );
853
-	case 'b' :
854
-	case 'B' :
855
-		return base64_decode ( ( encoded + 2 ), raw );
856
-	default:
857
-		return -EPROTO_INVALID_LARGE_BINARY;
840
+	/* Check for initial '0x' or '0b' and decode as appropriate */
841
+	if ( *(encoded++) == '0' ) {
842
+		switch ( tolower ( *(encoded++) ) ) {
843
+		case 'x' :
844
+			return base16_decode ( encoded, raw );
845
+		case 'b' :
846
+			return base64_decode ( encoded, raw );
847
+		}
858
 	}
848
 	}
849
+
850
+	return -EPROTO_INVALID_LARGE_BINARY;
859
 }
851
 }
860
 
852
 
861
 /**
853
 /**
982
  */
974
  */
983
 static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
975
 static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
984
 				       const char *value ) {
976
 				       const char *value ) {
985
-	uint8_t buf[ iscsi_large_binary_decoded_max_len ( value ) ];
977
+	uint8_t buf[ strlen ( value ) ]; /* Decoding never expands data */
986
 	unsigned int i;
978
 	unsigned int i;
987
-	size_t len;
979
+	int len;
988
 	int rc;
980
 	int rc;
989
 
981
 
990
 	/* Process challenge */
982
 	/* Process challenge */
991
-	rc = iscsi_large_binary_decode ( value, buf );
992
-	if ( rc < 0 ) {
983
+	len = iscsi_large_binary_decode ( value, buf );
984
+	if ( len < 0 ) {
985
+		rc = len;
993
 		DBGC ( iscsi, "iSCSI %p invalid CHAP challenge \"%s\": %s\n",
986
 		DBGC ( iscsi, "iSCSI %p invalid CHAP challenge \"%s\": %s\n",
994
 		       iscsi, value, strerror ( rc ) );
987
 		       iscsi, value, strerror ( rc ) );
995
 		return rc;
988
 		return rc;
996
 	}
989
 	}
997
-	len = rc;
998
 	chap_update ( &iscsi->chap, buf, len );
990
 	chap_update ( &iscsi->chap, buf, len );
999
 
991
 
1000
 	/* Build CHAP response */
992
 	/* Build CHAP response */
1052
  */
1044
  */
1053
 static int iscsi_handle_chap_r_value ( struct iscsi_session *iscsi,
1045
 static int iscsi_handle_chap_r_value ( struct iscsi_session *iscsi,
1054
 				       const char *value ) {
1046
 				       const char *value ) {
1055
-	uint8_t buf[ iscsi_large_binary_decoded_max_len ( value ) ];
1056
-	size_t len;
1047
+	uint8_t buf[ strlen ( value ) ]; /* Decoding never expands data */
1048
+	int len;
1057
 	int rc;
1049
 	int rc;
1058
 
1050
 
1059
 	/* Generate CHAP response for verification */
1051
 	/* Generate CHAP response for verification */
1073
 	chap_respond ( &iscsi->chap );
1065
 	chap_respond ( &iscsi->chap );
1074
 
1066
 
1075
 	/* Process response */
1067
 	/* Process response */
1076
-	rc = iscsi_large_binary_decode ( value, buf );
1077
-	if ( rc < 0 ) {
1068
+	len = iscsi_large_binary_decode ( value, buf );
1069
+	if ( len < 0 ) {
1070
+		rc = len;
1078
 		DBGC ( iscsi, "iSCSI %p invalid CHAP response \"%s\": %s\n",
1071
 		DBGC ( iscsi, "iSCSI %p invalid CHAP response \"%s\": %s\n",
1079
 		       iscsi, value, strerror ( rc ) );
1072
 		       iscsi, value, strerror ( rc ) );
1080
 		return rc;
1073
 		return rc;
1081
 	}
1074
 	}
1082
-	len = rc;
1083
 
1075
 
1084
 	/* Check CHAP response */
1076
 	/* Check CHAP response */
1085
-	if ( len != iscsi->chap.response_len ) {
1077
+	if ( len != ( int ) iscsi->chap.response_len ) {
1086
 		DBGC ( iscsi, "iSCSI %p invalid CHAP response length\n",
1078
 		DBGC ( iscsi, "iSCSI %p invalid CHAP response length\n",
1087
 		       iscsi );
1079
 		       iscsi );
1088
 		return -EPROTO_INVALID_CHAP_RESPONSE;
1080
 		return -EPROTO_INVALID_CHAP_RESPONSE;

Loading…
Cancel
Save