Merge branch 'bugfix/init_wpsreg_in_APSTA_mode' into 'master'

esp_wifi:Enable wpsreg to initialize in APSTA mode

Closes WIFI-5436

See merge request espressif/esp-idf!23034
This commit is contained in:
Kapil Gupta 2023-04-21 17:15:00 +08:00
commit a189bdbc7d
6 changed files with 72 additions and 27 deletions

View File

@ -18,6 +18,7 @@
#include "esp_wifi_driver.h" #include "esp_wifi_driver.h"
#include "esp_wifi_types.h" #include "esp_wifi_types.h"
#include "esp_wpa3_i.h" #include "esp_wpa3_i.h"
#include "esp_wps.h"
#define WIFI_PASSWORD_LEN_MAX 65 #define WIFI_PASSWORD_LEN_MAX 65
@ -211,7 +212,12 @@ void hostapd_cleanup(struct hostapd_data *hapd)
} }
#endif /* CONFIG_SAE */ #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); os_free(hapd);
global_hapd = NULL; global_hapd = NULL;

View File

@ -37,8 +37,9 @@ static int wifi_ap_wps_init(const esp_wps_config_t *config)
struct wps_sm *sm = NULL; struct wps_sm *sm = NULL;
uint8_t mac[ETH_ALEN]; uint8_t mac[ETH_ALEN];
struct wps_config cfg = {0}; struct wps_config cfg = {0};
struct hostapd_data *hapd = hostapd_get_hapd_data();
if (gWpsSm) { if (!hapd || gWpsSm) {
goto _out; goto _out;
} }
@ -76,7 +77,7 @@ static int wifi_ap_wps_init(const esp_wps_config_t *config)
goto _err; 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 */ /* Report PIN */
if (wps_get_type() == WPS_TYPE_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 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; 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); 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; return ESP_ERR_WIFI_MODE;
} }
API_MUTEX_TAKE(); API_MUTEX_TAKE();
if (s_wps_enabled) { 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(); API_MUTEX_GIVE();
wpa_printf(MSG_DEBUG, "wps enable: already enabled"); return ret;
return ESP_OK;
} }
ret = wifi_ap_wps_enable_internal(config); 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 esp_wifi_ap_wps_disable(void)
{ {
int ret = 0; int ret = 0;
wifi_mode_t mode = WIFI_MODE_NULL; struct wps_sm *sm = gWpsSm;
ret = esp_wifi_get_mode(&mode); if (sm && os_memcmp(sm->identity, WSC_ID_ENROLLEE, sm->identity_len) == 0) {
if (mode != WIFI_MODE_AP) {
return ESP_ERR_WIFI_MODE; 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; wifi_mode_t mode = WIFI_MODE_NULL;
esp_wifi_get_mode(&mode); esp_wifi_get_mode(&mode);
if (mode != WIFI_MODE_AP) { if (mode != WIFI_MODE_AP && mode != WIFI_MODE_APSTA) {
wpa_printf(MSG_ERROR, "wps start: mode=%d is not AP", mode); wpa_printf(MSG_ERROR, "wps start: mode=%d does not include AP", mode);
return ESP_ERR_WIFI_MODE; return ESP_ERR_WIFI_MODE;
} }

View File

@ -817,6 +817,7 @@ esp_err_t esp_wifi_sta_wpa2_ent_enable(void)
{ {
wifi_wpa2_param_t param; wifi_wpa2_param_t param;
esp_err_t ret; esp_err_t ret;
struct wpa_sm *sm = &gWpaSm;
wpa2_api_lock(); wpa2_api_lock();
@ -833,6 +834,7 @@ esp_err_t esp_wifi_sta_wpa2_ent_enable(void)
if (ESP_OK == ret) { if (ESP_OK == ret) {
wpa2_set_state(WPA2_STATE_ENABLED); wpa2_set_state(WPA2_STATE_ENABLED);
sm->wpa_sm_wpa2_ent_disable = esp_wifi_sta_wpa2_ent_disable;
} else { } else {
wpa_printf(MSG_ERROR, "failed to enable wpa2 ret=%d", ret); 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) 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); wpa_printf(MSG_INFO, "WPA2 ENTERPRISE VERSION: [%s] disable", WPA2_VERSION);
esp_wifi_unregister_wpa2_cb_internal(); 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(); eap_peer_unregister_methods();
#endif #endif
sm->wpa_sm_wpa2_ent_disable = NULL;
return ESP_OK; return ESP_OK;
} }

View File

@ -181,8 +181,15 @@ void wpa_ap_get_peer_spp_msg(void *sm_data, bool *spp_cap, bool *spp_req)
bool wpa_deattach(void) bool wpa_deattach(void)
{ {
struct wpa_sm *sm = &gWpaSm;
esp_wpa3_free_sae_data(); 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(); wpa_sm_deinit();
return true; return true;
} }

View File

@ -1771,7 +1771,6 @@ int wps_post_block(uint32_t sig, void *arg)
int wps_check_wifi_mode(void) int wps_check_wifi_mode(void)
{ {
bool sniffer = false;
wifi_mode_t mode; wifi_mode_t mode;
int ret; int ret;
@ -1781,18 +1780,12 @@ int wps_check_wifi_mode(void)
return ESP_FAIL; 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 ( if (
#ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT #ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
mode == WIFI_MODE_AP || mode == WIFI_MODE_AP ||
#endif #endif
mode == WIFI_MODE_NULL || sniffer == true) { mode == WIFI_MODE_NULL) {
wpa_printf(MSG_ERROR, "wps check wifi mode: wrong wifi mode=%d sniffer=%d", mode, sniffer); wpa_printf(MSG_ERROR, "wps check wifi mode: wrong wifi mode=%d ", mode);
return ESP_ERR_WIFI_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 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()) { if (ESP_OK != wps_check_wifi_mode()) {
return ESP_ERR_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(); API_MUTEX_TAKE();
if (s_wps_enabled) { 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(); API_MUTEX_GIVE();
wpa_printf(MSG_DEBUG, "wps enable: already enabled"); return ret;
return ESP_OK;
} }
#ifdef USE_WPS_TASK #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 wifi_wps_enable_internal(const esp_wps_config_t *config)
{ {
int ret = 0; int ret = 0;
struct wpa_sm *wpa_sm = &gWpaSm;
wpa_printf(MSG_DEBUG, "ESP WPS crypto initialize!"); wpa_printf(MSG_DEBUG, "ESP WPS crypto initialize!");
if (config->wps_type == WPS_TYPE_DISABLE) { 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); wps_set_status(WPS_STATUS_DISABLE);
return ESP_FAIL; return ESP_FAIL;
} }
wpa_sm->wpa_sm_wps_disable = esp_wifi_wps_disable;
return ESP_OK; return ESP_OK;
} }
@ -1882,8 +1887,10 @@ int esp_wifi_wps_disable(void)
{ {
int ret = 0; int ret = 0;
int wps_status; 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; return ESP_ERR_WIFI_MODE;
} }
@ -1922,6 +1929,7 @@ int esp_wifi_wps_disable(void)
wps_task_deinit(); wps_task_deinit();
s_wps_enabled = false; s_wps_enabled = false;
API_MUTEX_GIVE(); API_MUTEX_GIVE();
wpa_sm->wpa_sm_wps_disable = NULL;
return ESP_OK; return ESP_OK;
} }

View File

@ -116,6 +116,8 @@ struct wpa_sm {
u16 owe_group; u16 owe_group;
struct wpabuf *owe_ie; struct wpabuf *owe_ie;
#endif /* CONFIG_OWE_STA */ #endif /* CONFIG_OWE_STA */
int (*wpa_sm_wps_disable)(void);
esp_err_t (*wpa_sm_wpa2_ent_disable)(void);
}; };
/** /**