From 5524aad43f6487cb18050862388dc64533a4cc89 Mon Sep 17 00:00:00 2001 From: Shreyas Sheth Date: Mon, 3 Apr 2023 03:51:14 +0530 Subject: [PATCH] esp_wifi:Enable wpsreg to initialize in APSTA mode --- .../esp_supplicant/src/esp_hostap.c | 8 +++- .../esp_supplicant/src/esp_hostpad_wps.c | 40 ++++++++++++++----- .../esp_supplicant/src/esp_wpa2.c | 4 ++ .../esp_supplicant/src/esp_wpa_main.c | 9 ++++- .../esp_supplicant/src/esp_wps.c | 36 ++++++++++------- .../wpa_supplicant/src/rsn_supp/wpa_i.h | 2 + 6 files changed, 72 insertions(+), 27 deletions(-) diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_hostap.c b/components/wpa_supplicant/esp_supplicant/src/esp_hostap.c index 4bc5716763..d160b7c336 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_hostap.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_hostap.c @@ -18,6 +18,7 @@ #include "esp_wifi_driver.h" #include "esp_wifi_types.h" #include "esp_wpa3_i.h" +#include "esp_wps.h" #define WIFI_PASSWORD_LEN_MAX 65 @@ -211,7 +212,12 @@ void hostapd_cleanup(struct hostapd_data *hapd) } #endif /* CONFIG_SAE */ - +#ifdef CONFIG_WPS_REGISTRAR + if (esp_wifi_get_wps_type_internal () != WPS_TYPE_DISABLE || + esp_wifi_get_wps_status_internal() != WPS_STATUS_DISABLE) { + esp_wifi_ap_wps_disable(); + } +#endif /* CONFIG_WPS_REGISTRAR */ os_free(hapd); global_hapd = NULL; diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_hostpad_wps.c b/components/wpa_supplicant/esp_supplicant/src/esp_hostpad_wps.c index b436b810e1..84a8177dc6 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_hostpad_wps.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_hostpad_wps.c @@ -37,8 +37,9 @@ static int wifi_ap_wps_init(const esp_wps_config_t *config) struct wps_sm *sm = NULL; uint8_t mac[ETH_ALEN]; struct wps_config cfg = {0}; + struct hostapd_data *hapd = hostapd_get_hapd_data(); - if (gWpsSm) { + if (!hapd || gWpsSm) { goto _out; } @@ -76,7 +77,7 @@ static int wifi_ap_wps_init(const esp_wps_config_t *config) goto _err; } - hostapd_init_wps(hostapd_get_hapd_data(), sm->wps, sm->wps_ctx); + hostapd_init_wps(hapd, sm->wps, sm->wps_ctx); /* Report PIN */ if (wps_get_type() == WPS_TYPE_PIN) { @@ -171,19 +172,37 @@ int wifi_ap_wps_enable_internal(const esp_wps_config_t *config) int esp_wifi_ap_wps_enable(const esp_wps_config_t *config) { - int ret; + int ret = ESP_OK; + struct wps_sm *sm = gWpsSm; wifi_mode_t mode = WIFI_MODE_NULL; + if (esp_wifi_get_user_init_flag_internal() == 0) { + wpa_printf(MSG_ERROR, "wps enable: wifi not started cannot enable wpsreg"); + return ESP_ERR_WIFI_STATE; + } + ret = esp_wifi_get_mode(&mode); - if (mode != WIFI_MODE_AP) { + if (mode != WIFI_MODE_AP && mode != WIFI_MODE_APSTA) { + wpa_printf(MSG_ERROR, "wps enable: mode=%d does not include AP", mode); + return ESP_ERR_WIFI_MODE; + } + + if (esp_wifi_ap_get_prof_authmode_internal() == WIFI_AUTH_OPEN) { + wpa_printf(MSG_ERROR, "wps enable: wpsreg not supported when authmode is open"); return ESP_ERR_WIFI_MODE; } API_MUTEX_TAKE(); if (s_wps_enabled) { + if (sm && os_memcmp(sm->identity, WSC_ID_ENROLLEE, sm->identity_len) == 0) { + wpa_printf(MSG_ERROR, "wps enable: wps enrollee already enabled cannot enable wpsreg"); + ret = ESP_ERR_WIFI_MODE; + } else { + wpa_printf(MSG_DEBUG, "wps enable: already enabled"); + ret = ESP_OK; + } API_MUTEX_GIVE(); - wpa_printf(MSG_DEBUG, "wps enable: already enabled"); - return ESP_OK; + return ret; } ret = wifi_ap_wps_enable_internal(config); @@ -195,10 +214,9 @@ int esp_wifi_ap_wps_enable(const esp_wps_config_t *config) int esp_wifi_ap_wps_disable(void) { int ret = 0; - wifi_mode_t mode = WIFI_MODE_NULL; + struct wps_sm *sm = gWpsSm; - ret = esp_wifi_get_mode(&mode); - if (mode != WIFI_MODE_AP) { + if (sm && os_memcmp(sm->identity, WSC_ID_ENROLLEE, sm->identity_len) == 0) { return ESP_ERR_WIFI_MODE; } @@ -230,8 +248,8 @@ int esp_wifi_ap_wps_start(const unsigned char *pin) wifi_mode_t mode = WIFI_MODE_NULL; esp_wifi_get_mode(&mode); - if (mode != WIFI_MODE_AP) { - wpa_printf(MSG_ERROR, "wps start: mode=%d is not AP", mode); + if (mode != WIFI_MODE_AP && mode != WIFI_MODE_APSTA) { + wpa_printf(MSG_ERROR, "wps start: mode=%d does not include AP", mode); return ESP_ERR_WIFI_MODE; } diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wpa2.c b/components/wpa_supplicant/esp_supplicant/src/esp_wpa2.c index 18353777c5..290c4fa6cc 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wpa2.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wpa2.c @@ -817,6 +817,7 @@ esp_err_t esp_wifi_sta_wpa2_ent_enable(void) { wifi_wpa2_param_t param; esp_err_t ret; + struct wpa_sm *sm = &gWpaSm; wpa2_api_lock(); @@ -833,6 +834,7 @@ esp_err_t esp_wifi_sta_wpa2_ent_enable(void) if (ESP_OK == ret) { wpa2_set_state(WPA2_STATE_ENABLED); + sm->wpa_sm_wpa2_ent_disable = esp_wifi_sta_wpa2_ent_disable; } else { wpa_printf(MSG_ERROR, "failed to enable wpa2 ret=%d", ret); } @@ -844,6 +846,7 @@ esp_err_t esp_wifi_sta_wpa2_ent_enable(void) esp_err_t esp_wifi_sta_wpa2_ent_disable_fn(void *param) { + struct wpa_sm *sm = &gWpaSm; wpa_printf(MSG_INFO, "WPA2 ENTERPRISE VERSION: [%s] disable", WPA2_VERSION); esp_wifi_unregister_wpa2_cb_internal(); @@ -855,6 +858,7 @@ esp_err_t esp_wifi_sta_wpa2_ent_disable_fn(void *param) eap_peer_unregister_methods(); #endif + sm->wpa_sm_wpa2_ent_disable = NULL; return ESP_OK; } diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c b/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c index 43d7f9c1ae..889cb2ef0b 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c @@ -181,8 +181,15 @@ void wpa_ap_get_peer_spp_msg(void *sm_data, bool *spp_cap, bool *spp_req) bool wpa_deattach(void) { + struct wpa_sm *sm = &gWpaSm; esp_wpa3_free_sae_data(); - esp_wifi_sta_wpa2_ent_disable(); + if (sm->wpa_sm_wpa2_ent_disable) { + sm->wpa_sm_wpa2_ent_disable(); + } + if (sm->wpa_sm_wps_disable) { + sm->wpa_sm_wps_disable(); + } + wpa_sm_deinit(); return true; } diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wps.c b/components/wpa_supplicant/esp_supplicant/src/esp_wps.c index 1dd0451e9c..98c7efe821 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wps.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wps.c @@ -1771,7 +1771,6 @@ int wps_post_block(uint32_t sig, void *arg) int wps_check_wifi_mode(void) { - bool sniffer = false; wifi_mode_t mode; int ret; @@ -1781,18 +1780,12 @@ int wps_check_wifi_mode(void) return ESP_FAIL; } - ret = esp_wifi_get_promiscuous(&sniffer); - if (ESP_OK != ret) { - wpa_printf(MSG_ERROR, "wps check wifi mode: failed to get sniffer mode ret=%d", ret); - return ESP_FAIL; - } - if ( #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT mode == WIFI_MODE_AP || #endif - mode == WIFI_MODE_NULL || sniffer == true) { - wpa_printf(MSG_ERROR, "wps check wifi mode: wrong wifi mode=%d sniffer=%d", mode, sniffer); + mode == WIFI_MODE_NULL) { + wpa_printf(MSG_ERROR, "wps check wifi mode: wrong wifi mode=%d ", mode); return ESP_ERR_WIFI_MODE; } @@ -1801,7 +1794,13 @@ int wps_check_wifi_mode(void) int esp_wifi_wps_enable(const esp_wps_config_t *config) { - int ret; + int ret = ESP_OK; + struct wps_sm *sm = gWpsSm; + + if (esp_wifi_get_user_init_flag_internal() == 0) { + wpa_printf(MSG_ERROR, "wps enable: wifi not started cannot disable wpsreg"); + return ESP_ERR_WIFI_STATE; + } if (ESP_OK != wps_check_wifi_mode()) { return ESP_ERR_WIFI_MODE; @@ -1809,9 +1808,14 @@ int esp_wifi_wps_enable(const esp_wps_config_t *config) API_MUTEX_TAKE(); if (s_wps_enabled) { + if (sm && os_memcmp(sm->identity, WSC_ID_REGISTRAR, sm->identity_len) == 0) { + wpa_printf(MSG_ERROR, "wps enable: wpsreg already enabled cannot enable wps enrollee"); + ret = ESP_ERR_WIFI_MODE; + } else { + wpa_printf(MSG_DEBUG, "wps enable: already enabled"); + } API_MUTEX_GIVE(); - wpa_printf(MSG_DEBUG, "wps enable: already enabled"); - return ESP_OK; + return ret; } #ifdef USE_WPS_TASK @@ -1842,6 +1846,7 @@ int esp_wifi_wps_enable(const esp_wps_config_t *config) int wifi_wps_enable_internal(const esp_wps_config_t *config) { int ret = 0; + struct wpa_sm *wpa_sm = &gWpaSm; wpa_printf(MSG_DEBUG, "ESP WPS crypto initialize!"); if (config->wps_type == WPS_TYPE_DISABLE) { @@ -1867,7 +1872,7 @@ int wifi_wps_enable_internal(const esp_wps_config_t *config) wps_set_status(WPS_STATUS_DISABLE); return ESP_FAIL; } - + wpa_sm->wpa_sm_wps_disable = esp_wifi_wps_disable; return ESP_OK; } @@ -1882,8 +1887,10 @@ int esp_wifi_wps_disable(void) { int ret = 0; int wps_status; + struct wps_sm *wps_sm = gWpsSm; + struct wpa_sm *wpa_sm = &gWpaSm; - if (ESP_OK != wps_check_wifi_mode()) { + if (wps_sm && os_memcmp(wps_sm->identity, WSC_ID_REGISTRAR, wps_sm->identity_len) == 0) { return ESP_ERR_WIFI_MODE; } @@ -1922,6 +1929,7 @@ int esp_wifi_wps_disable(void) wps_task_deinit(); s_wps_enabled = false; API_MUTEX_GIVE(); + wpa_sm->wpa_sm_wps_disable = NULL; return ESP_OK; } diff --git a/components/wpa_supplicant/src/rsn_supp/wpa_i.h b/components/wpa_supplicant/src/rsn_supp/wpa_i.h index 0728146cae..b4b0ce75d9 100644 --- a/components/wpa_supplicant/src/rsn_supp/wpa_i.h +++ b/components/wpa_supplicant/src/rsn_supp/wpa_i.h @@ -116,6 +116,8 @@ struct wpa_sm { u16 owe_group; struct wpabuf *owe_ie; #endif /* CONFIG_OWE_STA */ + int (*wpa_sm_wps_disable)(void); + esp_err_t (*wpa_sm_wpa2_ent_disable)(void); }; /**