35
35
#include <wolfssl/wolfcrypt/settings.h>
36
36
37
37
bool he_internal_flow_should_fragment (he_conn_t * conn , uint16_t effective_pmtu , uint16_t length ) {
38
- return conn -> connection_type == HE_CONNECTION_TYPE_DATAGRAM && length > effective_pmtu ;
38
+ return conn -> connection_type == HE_CONNECTION_TYPE_DATAGRAM &&
39
+ conn -> pmtud_state == HE_PMTUD_STATE_SEARCH_COMPLETE && length > effective_pmtu ;
39
40
}
40
41
41
42
he_return_code_t he_conn_inside_packet_received (he_conn_t * conn , uint8_t * packet , size_t length ) {
@@ -69,9 +70,11 @@ he_return_code_t he_conn_inside_packet_received(he_conn_t *conn, uint8_t *packet
69
70
// Clamp the MSS if PMTU has been fixed
70
71
he_return_code_t ret = HE_SUCCESS ;
71
72
uint16_t effective_pmtu = he_conn_get_effective_pmtu (conn );
72
- ret = he_internal_clamp_mss (packet , length , effective_pmtu - HE_MSS_OVERHEAD );
73
- if (ret != HE_SUCCESS ) {
74
- return ret ;
73
+ if (conn -> pmtud_state == HE_PMTUD_STATE_SEARCH_COMPLETE ) {
74
+ ret = he_internal_clamp_mss (packet , length , effective_pmtu - HE_MSS_OVERHEAD );
75
+ if (ret != HE_SUCCESS ) {
76
+ return ret ;
77
+ }
75
78
}
76
79
77
80
// Process the packet with plugins.
@@ -141,24 +144,26 @@ he_return_code_t he_conn_inside_packet_received(he_conn_t *conn, uint8_t *packet
141
144
return ret ;
142
145
}
143
146
144
- he_return_code_t he_internal_flow_process_message (he_conn_t * conn , he_packet_buffer_t * read_packet ) {
147
+ he_return_code_t he_internal_flow_process_message (he_conn_t * conn ) {
145
148
// Return if conn is null
146
- if (!conn || ! read_packet ) {
149
+ if (!conn ) {
147
150
return HE_ERR_NULL_POINTER ;
148
151
}
149
152
150
153
// If the packet is too small then either the client is sending corrupted data or something is
151
154
// very wrong with the SSL connection
152
- if (read_packet -> packet_size < sizeof (he_msg_hdr_t )) {
153
- read_packet -> has_packet = false;
154
- he_conn_set_ssl_error ( conn , 0 ) ;
155
+ if (conn -> read_packet . packet_size < sizeof (he_msg_hdr_t )) {
156
+ conn -> read_packet . has_packet = false;
157
+ conn -> wolf_error = 0 ;
155
158
return HE_ERR_SSL_ERROR ;
156
159
}
157
160
161
+ he_packet_buffer_t * pkt_buff = & conn -> read_packet ;
162
+
158
163
// Cast the header
159
- he_msg_hdr_t * msg_hdr = (he_msg_hdr_t * )& read_packet -> packet ;
160
- uint8_t * buf = read_packet -> packet ;
161
- int buf_len = read_packet -> packet_size ;
164
+ he_msg_hdr_t * msg_hdr = (he_msg_hdr_t * )& pkt_buff -> packet ;
165
+ uint8_t * buf = pkt_buff -> packet ;
166
+ int buf_len = pkt_buff -> packet_size ;
162
167
163
168
switch (msg_hdr -> msgid ) {
164
169
case HE_MSGID_NOOP :
@@ -213,19 +218,19 @@ he_return_code_t he_internal_flow_process_message(he_conn_t *conn, he_packet_buf
213
218
return HE_SUCCESS ;
214
219
}
215
220
216
- he_return_code_t he_internal_flow_fetch_message (he_conn_t * conn , he_packet_buffer_t * read_packet ) {
221
+ he_return_code_t he_internal_flow_fetch_message (he_conn_t * conn ) {
217
222
// Return if conn is null
218
- if (!conn || ! read_packet ) {
223
+ if (!conn ) {
219
224
return HE_ERR_NULL_POINTER ;
220
225
}
221
226
222
227
// Try to read out a packet
223
228
int res =
224
- wolfSSL_read (conn -> wolf_ssl , read_packet -> packet , sizeof (read_packet -> packet ));
229
+ wolfSSL_read (conn -> wolf_ssl , conn -> read_packet . packet , sizeof (conn -> read_packet . packet ));
225
230
226
231
if (res <= 0 ) {
227
- read_packet -> has_packet = false;
228
- read_packet -> packet_size = 0 ;
232
+ conn -> read_packet . has_packet = false;
233
+ conn -> read_packet . packet_size = 0 ;
229
234
230
235
int error = wolfSSL_get_error (conn -> wolf_ssl , res );
231
236
switch (error ) {
@@ -234,7 +239,7 @@ he_return_code_t he_internal_flow_fetch_message(he_conn_t *conn, he_packet_buffe
234
239
return HE_SUCCESS ;
235
240
236
241
case APP_DATA_READY :
237
- return he_internal_flow_fetch_message (conn , read_packet );
242
+ return he_internal_flow_fetch_message (conn );
238
243
239
244
case SSL_ERROR_WANT_READ :
240
245
case SSL_ERROR_WANT_WRITE :
@@ -246,7 +251,7 @@ he_return_code_t he_internal_flow_fetch_message(he_conn_t *conn, he_packet_buffe
246
251
if (res == 0 ) {
247
252
return HE_ERR_CONNECTION_WAS_CLOSED ;
248
253
} else {
249
- he_conn_set_ssl_error ( conn , error ) ;
254
+ conn -> wolf_error = error ;
250
255
// if this is TCP then any SSL error is fatal (stream corruption).
251
256
// If this is D/TLS we can actually ignore corrupted packets.
252
257
if (conn -> connection_type == HE_CONNECTION_TYPE_STREAM ) {
@@ -257,8 +262,8 @@ he_return_code_t he_internal_flow_fetch_message(he_conn_t *conn, he_packet_buffe
257
262
}
258
263
}
259
264
} else {
260
- read_packet -> has_packet = true;
261
- read_packet -> packet_size = res ;
265
+ conn -> read_packet . has_packet = true;
266
+ conn -> read_packet . packet_size = res ;
262
267
}
263
268
264
269
return HE_SUCCESS ;
@@ -335,12 +340,6 @@ he_return_code_t he_conn_outside_data_received(he_conn_t *conn, uint8_t *buffer,
335
340
}
336
341
}
337
342
338
- #ifdef HE_ENABLE_MULTITHREADED
339
- HE_THREAD_LOCAL uint8_t * cur_packet = NULL ;
340
- HE_THREAD_LOCAL size_t cur_packet_length = 0 ;
341
- HE_THREAD_LOCAL bool packet_seen = false;
342
- #endif
343
-
344
343
he_return_code_t he_internal_flow_outside_packet_received (he_conn_t * conn , uint8_t * packet ,
345
344
size_t length ) {
346
345
// Return if packet or conn is null
@@ -381,10 +380,11 @@ he_return_code_t he_internal_flow_outside_packet_received(he_conn_t *conn, uint8
381
380
382
381
// Update pointer and length in our connection state
383
382
// We need to pull the wire header off first
384
- he_internal_set_packet (conn , packet + sizeof (he_wire_hdr_t ), length - sizeof (he_wire_hdr_t ));
383
+ conn -> incoming_data_length = length - sizeof (he_wire_hdr_t );
384
+ conn -> incoming_data = packet + sizeof (he_wire_hdr_t );
385
385
386
386
// Make sure that this packet is marked as unseen
387
- he_internal_set_packet_seen ( conn , false) ;
387
+ conn -> packet_seen = false;
388
388
389
389
return HE_DISPATCH (he_internal_flow_outside_data_verify_connection , conn );
390
390
}
@@ -410,13 +410,8 @@ he_return_code_t he_internal_flow_outside_data_verify_connection(he_conn_t *conn
410
410
}
411
411
412
412
// Check to see if this is our first message and trigger an event change if it is
413
- #ifdef HE_ENABLE_MULTITHREADED
414
- bool expected = false;
415
- if (atomic_compare_exchange_strong (& conn -> first_message_received , & expected , true)) {
416
- #else
417
- if (!conn -> first_message_received ) {
413
+ if (!conn -> first_message_received ) {
418
414
conn -> first_message_received = true;
419
- #endif
420
415
he_internal_generate_event (conn , HE_EVENT_FIRST_MESSAGE_RECEIVED );
421
416
}
422
417
@@ -439,7 +434,7 @@ he_return_code_t he_internal_flow_outside_data_verify_connection(he_conn_t *conn
439
434
}
440
435
441
436
// We can't recover from any other errors
442
- he_conn_set_ssl_error ( conn , error ) ;
437
+ conn -> wolf_error = error ;
443
438
return HE_ERR_SSL_ERROR ;
444
439
}
445
440
@@ -461,8 +456,6 @@ he_return_code_t he_internal_flow_outside_data_verify_connection(he_conn_t *conn
461
456
}
462
457
463
458
he_return_code_t he_internal_flow_outside_data_handle_messages (he_conn_t * conn ) {
464
- he_packet_buffer_t read_packet = { 0 };
465
-
466
459
// Return if conn is null
467
460
if (!conn ) {
468
461
return HE_ERR_NULL_POINTER ;
@@ -471,18 +464,18 @@ he_return_code_t he_internal_flow_outside_data_handle_messages(he_conn_t *conn)
471
464
// Handle messages
472
465
while (true) {
473
466
// Do we have a message?
474
- he_return_code_t ret = he_internal_flow_fetch_message (conn , & read_packet );
467
+ he_return_code_t ret = he_internal_flow_fetch_message (conn );
475
468
476
469
if (ret != HE_SUCCESS ) {
477
470
return ret ;
478
471
}
479
472
480
- if (!read_packet .has_packet ) {
473
+ if (!conn -> read_packet .has_packet ) {
481
474
break ;
482
475
}
483
476
484
477
// Process the message
485
- ret = he_internal_flow_process_message (conn , & read_packet );
478
+ ret = he_internal_flow_process_message (conn );
486
479
487
480
if (ret != HE_SUCCESS ) return ret ;
488
481
}
@@ -505,7 +498,7 @@ he_return_code_t he_internal_flow_outside_data_handle_messages(he_conn_t *conn)
505
498
// D/TLS 1.3
506
499
int wolf_rc = 0 ;
507
500
if ((wolf_rc = wolfSSL_key_update_response (conn -> wolf_ssl , & resp_pending )) != 0 ) {
508
- he_conn_set_ssl_error ( conn , wolfSSL_get_error (conn -> wolf_ssl , wolf_rc ) );
501
+ conn -> wolf_error = wolfSSL_get_error (conn -> wolf_ssl , wolf_rc );
509
502
return HE_ERR_SSL_ERROR ;
510
503
}
511
504
}
@@ -520,6 +513,10 @@ he_return_code_t he_internal_flow_outside_data_handle_messages(he_conn_t *conn)
520
513
he_internal_update_timeout (conn );
521
514
}
522
515
516
+ // Zero out the packet, ensures that if the connection is unused
517
+ // for extended periods the old outdated data is cleared from memory
518
+ memset (& conn -> read_packet , 0 , sizeof (conn -> read_packet ));
519
+
523
520
// All went well
524
521
return HE_SUCCESS ;
525
522
}
0 commit comments