From 3e5559f893b21db9eacbca1dfff5236b4e0ec4af Mon Sep 17 00:00:00 2001 From: Tom L <98499638+expressvpn-tom-l@users.noreply.github.com> Date: Mon, 9 Oct 2023 14:49:00 +0800 Subject: [PATCH] fixup: LIT-134 implementing pmtud state machines --- src/he/conn.c | 19 ++++----- src/he/he_internal.h | 7 +++- src/he/pmtud.c | 80 +++++++++++++++++++++++++++++++++++++ src/he/pmtud.h | 9 +++++ test/he/test_conn.c | 6 +-- test/he/test_conn_connect.c | 1 + 6 files changed, 105 insertions(+), 17 deletions(-) diff --git a/src/he/conn.c b/src/he/conn.c index 7110f854..a43cf3c6 100644 --- a/src/he/conn.c +++ b/src/he/conn.c @@ -16,14 +16,6 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -#include "conn.h" -#include "conn_internal.h" -#include "core.h" -#include "config.h" -#include "ssl_ctx.h" -#include "pmtud.h" - #ifndef WOLFSSL_USER_SETTINGS #include #endif @@ -31,6 +23,12 @@ #include #include +#include "conn.h" +#include "conn_internal.h" +#include "core.h" +#include "config.h" +#include "ssl_ctx.h" +#include "pmtud.h" #include "memory.h" bool he_conn_is_error_fatal(he_conn_t *conn, he_return_code_t error_msg) { @@ -1189,11 +1187,8 @@ he_return_code_t he_conn_start_pmtu_discovery(he_conn_t *conn) { return HE_ERR_INVALID_CONN_STATE; } - // Generate event - he_internal_generate_event(conn, HE_EVENT_PMTU_DISCOVERY_STARTED); - // Enter Base state - conn->pmtud_state = HE_PMTUD_STATE_BASE; + he_internal_change_pmtud_state(conn, HE_PMTUD_STATE_BASE); return HE_SUCCESS; } diff --git a/src/he/he_internal.h b/src/he/he_internal.h index 8d65a718..d5ff5681 100644 --- a/src/he/he_internal.h +++ b/src/he/he_internal.h @@ -227,14 +227,17 @@ struct he_conn { /// Identifier of the ping message pending reply uint16_t ping_pending_id; - /// Path MTU Discovery + /// Path MTU Discovery state he_pmtud_state_t pmtud_state; /// Current effective PMTU uint16_t effective_pmtu; /// PMTUD internal data - uint16_t base_pmtu; + uint16_t pmtud_base; + uint8_t pmtud_probe_count; + uint16_t pmtud_probing_size; + bool pmtud_is_using_big_step; }; struct he_plugin_chain { diff --git a/src/he/pmtud.c b/src/he/pmtud.c index dcf6c666..761c1099 100644 --- a/src/he/pmtud.c +++ b/src/he/pmtud.c @@ -44,6 +44,86 @@ he_return_code_t he_internal_pmtud_send_probe(he_conn_t *conn, uint16_t probe_mt void he_internal_pmtud_pong_received(he_conn_t *conn) { } +void he_internal_change_pmtud_state(he_conn_t *conn, he_pmtud_state_t state) { + if(conn == NULL || conn->pmtud_state == state) { + return; + } + + switch(conn->state) { + case HE_PMTUD_STATE_DISABLED: + switch(state) { + case HE_PMTUD_STATE_BASE: + // DISABLED -> BASE + he_internal_generate_event(conn, HE_EVENT_PMTU_DISCOVERY_STARTED); + + // Initialize PMTU internal data + conn->pmtud_base = HE_MAX_MTU; + conn->pmtud_is_using_big_step = true; + conn->pmtud_probe_count = 0; + + // TODO: start timers? + break; + default: + // Invalid state transition, do nothing + return; + } + break; + case HE_PMTUD_STATE_BASE: + switch(state) { + case HE_PMTUD_STATE_SEARCHING: + // TODO: enter searching state + break; + case HE_PMTUD_STATE_ERROR: + // TODO: enter error state + break; + case HE_PMTUD_STATE_SEARCH_COMPLETE: + // TODO: enter search complete state + break; + default: + // Invalid state transition, do nothing + return; + } + break; + case HE_PMTUD_STATE_SEARCHING: + switch(state) { + case HE_PMTUD_STATE_BASE: + // TODO: Return to Base when blackhole is detected + break; + case HE_PMTUD_STATE_SEARCH_COMPLETE: + // TODO: Probe acked + break; + default: + // Invalid state transition, do nothing + return; + } + break; + case HE_PMTUD_STATE_ERROR: + switch(state) { + case HE_PMTUD_STATE_SEARCHING: + // TODO: Enter Searching state when probe succeeds + break; + default: + // Invalid state transition, do nothing + return; + } + break; + case HE_PMTUD_STATE_SEARCH_COMPLETE: + switch(state) { + case HE_PMTUD_STATE_BASE: + // TODO: enter Base state if MAX_PROBES successive PLPMTUD-sized probes fail to be + // acknowledged + break; + + default: + break; + } + break; + } + + // State changed + conn->state = state; +} + void he_internal_pmtud_update(he_conn_t *conn) { if(conn == NULL || conn->pmtud_state == HE_PMTUD_STATE_DISABLED) { // Invalid state, do nothing diff --git a/src/he/pmtud.h b/src/he/pmtud.h index 31c4f818..89ef9738 100644 --- a/src/he/pmtud.h +++ b/src/he/pmtud.h @@ -26,6 +26,10 @@ #ifndef PMTUD_H #define PMTUD_H +/// The maximum value of the PROBE_COUNT counter. MAX_PROBES represents the limit for the number of +/// consecutive probe attempts of any size. +#define MAX_PROBES 3 + /// The smallest PMTU the discovery process will attempt to use #define MIN_PLPMTU 512 @@ -69,6 +73,11 @@ typedef enum he_pmtud_state { // Internal functions for PMTUD +/** + * @brief Change PMTUD state + */ +void he_internal_change_pmtud_state(he_conn_t *conn, he_pmtud_state_t state); + /** * @brief Update PMTUD state machine. */ diff --git a/test/he/test_conn.c b/test/he/test_conn.c index 6b90e62d..815ed1d2 100644 --- a/test/he/test_conn.c +++ b/test/he/test_conn.c @@ -34,6 +34,7 @@ // Internal Mocks #include "mock_fake_dispatch.h" #include "mock_wolf.h" +#include "mock_pmtud.h" // External Mocks #include "mock_ssl.h" @@ -1619,11 +1620,10 @@ void test_he_conn_get_curve_name(void) { void test_he_conn_start_pmtu_discovery(void) { conn.state = HE_STATE_ONLINE; - conn.event_cb = event_cb; + + he_internal_change_pmtud_state_Expect(&conn, HE_PMTUD_STATE_BASE); TEST_ASSERT_EQUAL(HE_SUCCESS, he_conn_start_pmtu_discovery(&conn)); - TEST_ASSERT_EQUAL(HE_PMTUD_STATE_BASE, conn.pmtud_state); - TEST_ASSERT_EQUAL(1, call_counter); } void test_he_conn_start_pmtu_discovery_null(void) { diff --git a/test/he/test_conn_connect.c b/test/he/test_conn_connect.c index fe251e69..85395527 100644 --- a/test/he/test_conn_connect.c +++ b/test/he/test_conn_connect.c @@ -38,6 +38,7 @@ #include "mock_wolf.h" #include "mock_ssl_ctx.h" #include "mock_fake_dispatch.h" +#include "mock_pmtud.h" // External Mocks #include "mock_ssl.h"