From a08e4afd07fdc2933818a2da14b19bef9f6b7b08 Mon Sep 17 00:00:00 2001 From: jgujarathi Date: Tue, 3 Oct 2023 13:53:00 +0530 Subject: [PATCH] fix(wpa_supplicant/dpp): Ensure dpp follows init->bootstrap->listen path - esp_supp_dpp_init : Ensures that the mode is set to station before the API call. - Ensures that dpp follows the path of init(esp_supp_dpp_init) -> bootstrap(esp_supp_dpp_bootstrap_gen) -> listen(esp_supp_dpp_start_listen) by returning errors if any of them is invoked out of order. --- .../esp_supplicant/src/esp_dpp.c | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c b/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c index bd43dbdb45..72de734c8b 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c @@ -22,7 +22,7 @@ static void *s_dpp_api_lock = NULL; static bool s_dpp_stop_listening; static int s_dpp_auth_retries; -struct esp_dpp_context_t s_dpp_ctx; +static struct esp_dpp_context_t s_dpp_ctx; static wifi_action_rx_cb_t s_action_rx_cb = esp_supp_rx_action; #define DPP_API_LOCK() os_mutex_lock(s_dpp_api_lock) @@ -518,6 +518,10 @@ esp_err_t esp_supp_dpp_bootstrap_gen(const char *chan_list, enum dpp_bootstrap_type type, const char *key, const char *uri_info) { + if (!s_dpp_ctx.dpp_global) { + wpa_printf(MSG_ERROR, "DPP: failed to bootstrap as dpp not initialized."); + return ESP_FAIL; + } struct dpp_bootstrap_params_t *params = &s_dpp_ctx.bootstrap_params; char *uri_chan_list = esp_dpp_parse_chan_list(chan_list); char *command = os_zalloc(1200); @@ -581,6 +585,11 @@ fail: esp_err_t esp_supp_dpp_start_listen(void) { + if (!s_dpp_ctx.dpp_global || s_dpp_ctx.id < 1) { + wpa_printf(MSG_ERROR, "DPP: failed to start listen as dpp not initialized or bootstrapped."); + return ESP_FAIL; + } + if (esp_wifi_get_user_init_flag_internal() == 0) { wpa_printf(MSG_ERROR, "DPP: ROC not possible before wifi is started"); return ESP_ERR_INVALID_STATE; @@ -598,6 +607,15 @@ void esp_supp_dpp_stop_listen(void) esp_err_t esp_supp_dpp_init(esp_supp_dpp_event_cb_t cb) { + wifi_mode_t mode = 0; + if (esp_wifi_get_mode(&mode) || ((mode != WIFI_MODE_STA) && (mode != WIFI_MODE_APSTA))) { + wpa_printf(MSG_ERROR, "DPP: failed to init as not in station mode."); + return ESP_FAIL; + } + if (s_dpp_ctx.dpp_global) { + wpa_printf(MSG_ERROR, "DPP: failed to init as init already done."); + return ESP_FAIL; + } struct dpp_global_config cfg = {0}; int ret; @@ -636,7 +654,6 @@ esp_err_t esp_supp_dpp_init(esp_supp_dpp_event_cb_t cb) void esp_supp_dpp_deinit(void) { struct dpp_bootstrap_params_t *params = &s_dpp_ctx.bootstrap_params; - if (params->info) { os_free(params->info); params->info = NULL; @@ -647,7 +664,10 @@ void esp_supp_dpp_deinit(void) esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_ROC_DONE, &offchan_event_handler); s_dpp_auth_retries = 0; - dpp_global_deinit(s_dpp_ctx.dpp_global); - esp_dpp_post_evt(SIG_DPP_DEL_TASK, 0); + if (s_dpp_ctx.dpp_global) { + dpp_global_deinit(s_dpp_ctx.dpp_global); + s_dpp_ctx.dpp_global = NULL; + esp_dpp_post_evt(SIG_DPP_DEL_TASK, 0); + } } #endif