@@ -389,7 +389,7 @@ int sctp_auth_asoc_init_active_key(struct sctp_association *asoc, gfp_t gfp)
389389 /* If we don't support AUTH, or peer is not capable
390390 * we don't need to do anything.
391391 */
392- if (!asoc -> ep -> auth_enable || ! asoc -> peer .auth_capable )
392+ if (!asoc -> peer .auth_capable )
393393 return 0 ;
394394
395395 /* If the key_id is non-zero and we couldn't find an
@@ -675,7 +675,7 @@ int sctp_auth_send_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
675675 if (!asoc )
676676 return 0 ;
677677
678- if (!asoc -> ep -> auth_enable || ! asoc -> peer .auth_capable )
678+ if (!asoc -> peer .auth_capable )
679679 return 0 ;
680680
681681 return __sctp_auth_cid (chunk , asoc -> peer .peer_chunks );
@@ -687,7 +687,7 @@ int sctp_auth_recv_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
687687 if (!asoc )
688688 return 0 ;
689689
690- if (!asoc -> ep -> auth_enable )
690+ if (!asoc -> peer . auth_capable )
691691 return 0 ;
692692
693693 return __sctp_auth_cid (chunk ,
@@ -831,10 +831,15 @@ int sctp_auth_set_key(struct sctp_endpoint *ep,
831831 /* Try to find the given key id to see if
832832 * we are doing a replace, or adding a new key
833833 */
834- if (asoc )
834+ if (asoc ) {
835+ if (!asoc -> peer .auth_capable )
836+ return - EACCES ;
835837 sh_keys = & asoc -> endpoint_shared_keys ;
836- else
838+ } else {
839+ if (!ep -> auth_enable )
840+ return - EACCES ;
837841 sh_keys = & ep -> endpoint_shared_keys ;
842+ }
838843
839844 key_for_each (shkey , sh_keys ) {
840845 if (shkey -> key_id == auth_key -> sca_keynumber ) {
@@ -875,10 +880,15 @@ int sctp_auth_set_active_key(struct sctp_endpoint *ep,
875880 int found = 0 ;
876881
877882 /* The key identifier MUST correst to an existing key */
878- if (asoc )
883+ if (asoc ) {
884+ if (!asoc -> peer .auth_capable )
885+ return - EACCES ;
879886 sh_keys = & asoc -> endpoint_shared_keys ;
880- else
887+ } else {
888+ if (!ep -> auth_enable )
889+ return - EACCES ;
881890 sh_keys = & ep -> endpoint_shared_keys ;
891+ }
882892
883893 key_for_each (key , sh_keys ) {
884894 if (key -> key_id == key_id ) {
@@ -911,11 +921,15 @@ int sctp_auth_del_key_id(struct sctp_endpoint *ep,
911921 * The key identifier MUST correst to an existing key
912922 */
913923 if (asoc ) {
924+ if (!asoc -> peer .auth_capable )
925+ return - EACCES ;
914926 if (asoc -> active_key_id == key_id )
915927 return - EINVAL ;
916928
917929 sh_keys = & asoc -> endpoint_shared_keys ;
918930 } else {
931+ if (!ep -> auth_enable )
932+ return - EACCES ;
919933 if (ep -> active_key_id == key_id )
920934 return - EINVAL ;
921935
@@ -950,11 +964,15 @@ int sctp_auth_deact_key_id(struct sctp_endpoint *ep,
950964 * The key identifier MUST correst to an existing key
951965 */
952966 if (asoc ) {
967+ if (!asoc -> peer .auth_capable )
968+ return - EACCES ;
953969 if (asoc -> active_key_id == key_id )
954970 return - EINVAL ;
955971
956972 sh_keys = & asoc -> endpoint_shared_keys ;
957973 } else {
974+ if (!ep -> auth_enable )
975+ return - EACCES ;
958976 if (ep -> active_key_id == key_id )
959977 return - EINVAL ;
960978
@@ -989,3 +1007,72 @@ int sctp_auth_deact_key_id(struct sctp_endpoint *ep,
9891007
9901008 return 0 ;
9911009}
1010+
1011+ int sctp_auth_init (struct sctp_endpoint * ep , gfp_t gfp )
1012+ {
1013+ int err = - ENOMEM ;
1014+
1015+ /* Allocate space for HMACS and CHUNKS authentication
1016+ * variables. There are arrays that we encode directly
1017+ * into parameters to make the rest of the operations easier.
1018+ */
1019+ if (!ep -> auth_hmacs_list ) {
1020+ struct sctp_hmac_algo_param * auth_hmacs ;
1021+
1022+ auth_hmacs = kzalloc (struct_size (auth_hmacs , hmac_ids ,
1023+ SCTP_AUTH_NUM_HMACS ), gfp );
1024+ if (!auth_hmacs )
1025+ goto nomem ;
1026+ /* Initialize the HMACS parameter.
1027+ * SCTP-AUTH: Section 3.3
1028+ * Every endpoint supporting SCTP chunk authentication MUST
1029+ * support the HMAC based on the SHA-1 algorithm.
1030+ */
1031+ auth_hmacs -> param_hdr .type = SCTP_PARAM_HMAC_ALGO ;
1032+ auth_hmacs -> param_hdr .length =
1033+ htons (sizeof (struct sctp_paramhdr ) + 2 );
1034+ auth_hmacs -> hmac_ids [0 ] = htons (SCTP_AUTH_HMAC_ID_SHA1 );
1035+ ep -> auth_hmacs_list = auth_hmacs ;
1036+ }
1037+
1038+ if (!ep -> auth_chunk_list ) {
1039+ struct sctp_chunks_param * auth_chunks ;
1040+
1041+ auth_chunks = kzalloc (sizeof (* auth_chunks ) +
1042+ SCTP_NUM_CHUNK_TYPES , gfp );
1043+ if (!auth_chunks )
1044+ goto nomem ;
1045+ /* Initialize the CHUNKS parameter */
1046+ auth_chunks -> param_hdr .type = SCTP_PARAM_CHUNKS ;
1047+ auth_chunks -> param_hdr .length =
1048+ htons (sizeof (struct sctp_paramhdr ));
1049+ ep -> auth_chunk_list = auth_chunks ;
1050+ }
1051+
1052+ /* Allocate and initialize transorms arrays for supported
1053+ * HMACs.
1054+ */
1055+ err = sctp_auth_init_hmacs (ep , gfp );
1056+ if (err )
1057+ goto nomem ;
1058+
1059+ return 0 ;
1060+
1061+ nomem :
1062+ /* Free all allocations */
1063+ kfree (ep -> auth_hmacs_list );
1064+ kfree (ep -> auth_chunk_list );
1065+ ep -> auth_hmacs_list = NULL ;
1066+ ep -> auth_chunk_list = NULL ;
1067+ return err ;
1068+ }
1069+
1070+ void sctp_auth_free (struct sctp_endpoint * ep )
1071+ {
1072+ kfree (ep -> auth_hmacs_list );
1073+ kfree (ep -> auth_chunk_list );
1074+ ep -> auth_hmacs_list = NULL ;
1075+ ep -> auth_chunk_list = NULL ;
1076+ sctp_auth_destroy_hmacs (ep -> auth_hmacs );
1077+ ep -> auth_hmacs = NULL ;
1078+ }
0 commit comments