@@ -69,12 +69,56 @@ static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
6969 struct sock * sk ,
7070 gfp_t gfp )
7171{
72+ struct sctp_hmac_algo_param * auth_hmacs = NULL ;
73+ struct sctp_chunks_param * auth_chunks = NULL ;
74+ struct sctp_shared_key * null_key ;
75+ int err ;
76+
7277 memset (ep , 0 , sizeof (struct sctp_endpoint ));
7378
7479 ep -> digest = kzalloc (SCTP_SIGNATURE_SIZE , gfp );
7580 if (!ep -> digest )
7681 return NULL ;
7782
83+ if (sctp_auth_enable ) {
84+ /* Allocate space for HMACS and CHUNKS authentication
85+ * variables. There are arrays that we encode directly
86+ * into parameters to make the rest of the operations easier.
87+ */
88+ auth_hmacs = kzalloc (sizeof (sctp_hmac_algo_param_t ) +
89+ sizeof (__u16 ) * SCTP_AUTH_NUM_HMACS , gfp );
90+ if (!auth_hmacs )
91+ goto nomem ;
92+
93+ auth_chunks = kzalloc (sizeof (sctp_chunks_param_t ) +
94+ SCTP_NUM_CHUNK_TYPES , gfp );
95+ if (!auth_chunks )
96+ goto nomem ;
97+
98+ /* Initialize the HMACS parameter.
99+ * SCTP-AUTH: Section 3.3
100+ * Every endpoint supporting SCTP chunk authentication MUST
101+ * support the HMAC based on the SHA-1 algorithm.
102+ */
103+ auth_hmacs -> param_hdr .type = SCTP_PARAM_HMAC_ALGO ;
104+ auth_hmacs -> param_hdr .length =
105+ htons (sizeof (sctp_paramhdr_t ) + 2 );
106+ auth_hmacs -> hmac_ids [0 ] = htons (SCTP_AUTH_HMAC_ID_SHA1 );
107+
108+ /* Initialize the CHUNKS parameter */
109+ auth_chunks -> param_hdr .type = SCTP_PARAM_CHUNKS ;
110+
111+ /* If the Add-IP functionality is enabled, we must
112+ * authenticate, ASCONF and ASCONF-ACK chunks
113+ */
114+ if (sctp_addip_enable ) {
115+ auth_chunks -> chunks [0 ] = SCTP_CID_ASCONF ;
116+ auth_chunks -> chunks [1 ] = SCTP_CID_ASCONF_ACK ;
117+ auth_chunks -> param_hdr .length =
118+ htons (sizeof (sctp_paramhdr_t ) + 2 );
119+ }
120+ }
121+
78122 /* Initialize the base structure. */
79123 /* What type of endpoint are we? */
80124 ep -> base .type = SCTP_EP_TYPE_SOCKET ;
@@ -114,7 +158,36 @@ static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
114158 ep -> last_key = ep -> current_key = 0 ;
115159 ep -> key_changed_at = jiffies ;
116160
161+ /* SCTP-AUTH extensions*/
162+ INIT_LIST_HEAD (& ep -> endpoint_shared_keys );
163+ null_key = sctp_auth_shkey_create (0 , GFP_KERNEL );
164+ if (!null_key )
165+ goto nomem ;
166+
167+ list_add (& null_key -> key_list , & ep -> endpoint_shared_keys );
168+
169+ /* Allocate and initialize transorms arrays for suported HMACs. */
170+ err = sctp_auth_init_hmacs (ep , gfp );
171+ if (err )
172+ goto nomem_hmacs ;
173+
174+ /* Add the null key to the endpoint shared keys list and
175+ * set the hmcas and chunks pointers.
176+ */
177+ ep -> auth_hmacs_list = auth_hmacs ;
178+ ep -> auth_chunk_list = auth_chunks ;
179+
117180 return ep ;
181+
182+ nomem_hmacs :
183+ sctp_auth_destroy_keys (& ep -> endpoint_shared_keys );
184+ nomem :
185+ /* Free all allocations */
186+ kfree (auth_hmacs );
187+ kfree (auth_chunks );
188+ kfree (ep -> digest );
189+ return NULL ;
190+
118191}
119192
120193/* Create a sctp_endpoint with all that boring stuff initialized.
@@ -187,6 +260,16 @@ static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
187260 /* Free the digest buffer */
188261 kfree (ep -> digest );
189262
263+ /* SCTP-AUTH: Free up AUTH releated data such as shared keys
264+ * chunks and hmacs arrays that were allocated
265+ */
266+ sctp_auth_destroy_keys (& ep -> endpoint_shared_keys );
267+ kfree (ep -> auth_hmacs_list );
268+ kfree (ep -> auth_chunk_list );
269+
270+ /* AUTH - Free any allocated HMAC transform containers */
271+ sctp_auth_destroy_hmacs (ep -> auth_hmacs );
272+
190273 /* Cleanup. */
191274 sctp_inq_free (& ep -> base .inqueue );
192275 sctp_bind_addr_free (& ep -> base .bind_addr );
0 commit comments