2021-04-07 03:04:51 -04:00
|
|
|
/*
|
2022-01-04 23:52:04 -05:00
|
|
|
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
|
2021-04-07 03:04:51 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-08-13 04:37:56 -04:00
|
|
|
|
|
|
|
#include "utils/includes.h"
|
|
|
|
|
|
|
|
#include "utils/common.h"
|
|
|
|
#include "crypto/sha1.h"
|
|
|
|
#include "common/ieee802_11_defs.h"
|
|
|
|
#include "common/eapol_common.h"
|
|
|
|
#include "ap/wpa_auth.h"
|
|
|
|
#include "ap/ap_config.h"
|
|
|
|
#include "utils/wpa_debug.h"
|
|
|
|
#include "ap/hostapd.h"
|
|
|
|
#include "ap/wpa_auth_i.h"
|
|
|
|
#include "esp_wifi_driver.h"
|
|
|
|
#include "esp_wifi_types.h"
|
|
|
|
|
2022-05-13 04:02:24 -04:00
|
|
|
struct hostapd_data *global_hapd;
|
|
|
|
|
|
|
|
struct hostapd_data *hostapd_get_hapd_data(void)
|
|
|
|
{
|
|
|
|
return global_hapd;
|
|
|
|
}
|
|
|
|
|
2018-08-13 04:37:56 -04:00
|
|
|
void *hostap_init(void)
|
|
|
|
{
|
|
|
|
struct wifi_ssid *ssid = esp_wifi_ap_get_prof_ap_ssid_internal();
|
|
|
|
struct hostapd_data *hapd = NULL;
|
|
|
|
struct wpa_auth_config *auth_conf;
|
2021-01-15 04:00:15 -05:00
|
|
|
u16 spp_attrubute = 0;
|
2020-10-15 05:05:32 -04:00
|
|
|
u8 pairwise_cipher;
|
2022-01-04 23:52:04 -05:00
|
|
|
wifi_pmf_config_t pmf_cfg;
|
2018-08-13 04:37:56 -04:00
|
|
|
|
|
|
|
hapd = (struct hostapd_data *)os_zalloc(sizeof(struct hostapd_data));
|
|
|
|
|
|
|
|
if (hapd == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hapd->conf = (struct hostapd_bss_config *)os_zalloc(sizeof(struct hostapd_bss_config));
|
|
|
|
|
|
|
|
if (hapd->conf == NULL) {
|
|
|
|
os_free(hapd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-05-13 04:02:24 -04:00
|
|
|
hapd->conf->max_num_sta = MAX_STA_COUNT;
|
2018-08-13 04:37:56 -04:00
|
|
|
|
|
|
|
auth_conf = (struct wpa_auth_config *)os_zalloc(sizeof(struct wpa_auth_config));
|
|
|
|
|
|
|
|
if (auth_conf == NULL) {
|
|
|
|
os_free(hapd->conf);
|
|
|
|
os_free(hapd);
|
|
|
|
hapd = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (esp_wifi_ap_get_prof_authmode_internal() == WIFI_AUTH_WPA_PSK) {
|
|
|
|
auth_conf->wpa = WPA_PROTO_WPA;
|
|
|
|
}
|
|
|
|
if (esp_wifi_ap_get_prof_authmode_internal() == WIFI_AUTH_WPA2_PSK) {
|
|
|
|
auth_conf->wpa = WPA_PROTO_RSN;
|
|
|
|
}
|
|
|
|
if (esp_wifi_ap_get_prof_authmode_internal() == WIFI_AUTH_WPA_WPA2_PSK) {
|
|
|
|
auth_conf->wpa = WPA_PROTO_RSN | WPA_PROTO_WPA;
|
|
|
|
}
|
|
|
|
|
2020-10-15 05:05:32 -04:00
|
|
|
pairwise_cipher = esp_wifi_ap_get_prof_pairwise_cipher_internal();
|
2022-01-04 23:52:04 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_IEEE80211W
|
|
|
|
|
|
|
|
esp_wifi_get_pmf_config_internal(&pmf_cfg, WIFI_IF_AP);
|
|
|
|
|
|
|
|
if (pmf_cfg.required) {
|
|
|
|
pairwise_cipher = WIFI_CIPHER_TYPE_CCMP;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_IEEE80211W */
|
|
|
|
|
2020-10-15 05:05:32 -04:00
|
|
|
/* TKIP is compulsory in WPA Mode */
|
|
|
|
if (auth_conf->wpa == WPA_PROTO_WPA && pairwise_cipher == WIFI_CIPHER_TYPE_CCMP) {
|
|
|
|
pairwise_cipher = WIFI_CIPHER_TYPE_TKIP_CCMP;
|
|
|
|
}
|
|
|
|
if (pairwise_cipher == WIFI_CIPHER_TYPE_TKIP) {
|
|
|
|
auth_conf->wpa_group = WPA_CIPHER_TKIP;
|
|
|
|
auth_conf->wpa_pairwise = WPA_CIPHER_TKIP;
|
|
|
|
auth_conf->rsn_pairwise = WPA_CIPHER_TKIP;
|
|
|
|
} else if (pairwise_cipher == WIFI_CIPHER_TYPE_CCMP) {
|
|
|
|
auth_conf->wpa_group = WPA_CIPHER_CCMP;
|
|
|
|
auth_conf->wpa_pairwise = WPA_CIPHER_CCMP;
|
|
|
|
auth_conf->rsn_pairwise = WPA_CIPHER_CCMP;
|
|
|
|
} else {
|
|
|
|
auth_conf->wpa_group = WPA_CIPHER_TKIP;
|
|
|
|
auth_conf->wpa_pairwise = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
|
|
|
|
auth_conf->rsn_pairwise = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
|
|
|
|
}
|
|
|
|
|
2018-08-13 04:37:56 -04:00
|
|
|
auth_conf->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
|
|
|
|
auth_conf->eapol_version = EAPOL_VERSION;
|
|
|
|
|
2022-01-04 23:52:04 -05:00
|
|
|
#ifdef CONFIG_IEEE80211W
|
|
|
|
if (pmf_cfg.required && pmf_cfg.capable) {
|
|
|
|
auth_conf->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
|
|
|
|
auth_conf->wpa_key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
|
|
|
|
wpa_printf(MSG_DEBUG, "%s :pmf required", __func__);
|
|
|
|
} else if (pmf_cfg.capable && !pmf_cfg.required) {
|
|
|
|
auth_conf->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
|
|
|
|
auth_conf->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
|
|
|
|
wpa_printf(MSG_DEBUG, "%s : pmf optional", __func__);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_IEEE80211W */
|
|
|
|
|
2021-01-15 04:00:15 -05:00
|
|
|
spp_attrubute = esp_wifi_get_spp_attrubute_internal(WIFI_IF_AP);
|
|
|
|
auth_conf->spp_sup.capable = ((spp_attrubute & WPA_CAPABILITY_SPP_CAPABLE) ? SPP_AMSDU_CAP_ENABLE : SPP_AMSDU_CAP_DISABLE);
|
2021-08-04 02:03:12 -04:00
|
|
|
auth_conf->spp_sup.require = ((spp_attrubute & WPA_CAPABILITY_SPP_REQUIRED) ? SPP_AMSDU_REQ_ENABLE : SPP_AMSDU_REQ_DISABLE);
|
2021-01-15 04:00:15 -05:00
|
|
|
|
2018-08-13 04:37:56 -04:00
|
|
|
memcpy(hapd->conf->ssid.ssid, ssid->ssid, ssid->len);
|
|
|
|
hapd->conf->ssid.ssid_len = ssid->len;
|
|
|
|
hapd->conf->ssid.wpa_passphrase = (char *)os_zalloc(64);
|
|
|
|
if (hapd->conf->ssid.wpa_passphrase == NULL) {
|
|
|
|
os_free(auth_conf);
|
|
|
|
os_free(hapd->conf);
|
|
|
|
os_free(hapd);
|
|
|
|
hapd = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(hapd->conf->ssid.wpa_passphrase, esp_wifi_ap_get_prof_password_internal(), strlen((char *)esp_wifi_ap_get_prof_password_internal()));
|
|
|
|
|
|
|
|
hapd->conf->ap_max_inactivity = 5 * 60;
|
|
|
|
hostapd_setup_wpa_psk(hapd->conf);
|
|
|
|
|
2022-05-13 04:02:24 -04:00
|
|
|
esp_wifi_get_macaddr_internal(WIFI_IF_AP, hapd->own_addr);
|
2018-08-13 04:37:56 -04:00
|
|
|
|
2022-05-13 04:02:24 -04:00
|
|
|
hapd->wpa_auth = wpa_init(hapd->own_addr, auth_conf, NULL);
|
2020-07-13 23:59:39 -04:00
|
|
|
esp_wifi_set_appie_internal(WIFI_APPIE_WPA, hapd->wpa_auth->wpa_ie, (uint16_t)hapd->wpa_auth->wpa_ie_len, 0);
|
2018-08-13 04:37:56 -04:00
|
|
|
os_free(auth_conf);
|
2022-05-13 04:02:24 -04:00
|
|
|
global_hapd = hapd;
|
2018-08-13 04:37:56 -04:00
|
|
|
|
|
|
|
return (void *)hapd;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hostap_deinit(void *data)
|
|
|
|
{
|
|
|
|
struct hostapd_data *hapd = (struct hostapd_data *)data;
|
|
|
|
|
|
|
|
if (hapd == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hapd->wpa_auth != NULL) {
|
2021-11-10 01:18:00 -05:00
|
|
|
if (hapd->wpa_auth->wpa_ie != NULL) {
|
|
|
|
os_free(hapd->wpa_auth->wpa_ie);
|
|
|
|
}
|
|
|
|
if (hapd->wpa_auth->group != NULL) {
|
|
|
|
os_free(hapd->wpa_auth->group);
|
|
|
|
}
|
2018-08-13 04:37:56 -04:00
|
|
|
os_free(hapd->wpa_auth);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hapd->conf != NULL) {
|
2021-11-10 01:18:00 -05:00
|
|
|
if (hapd->conf->ssid.wpa_psk != NULL) {
|
|
|
|
os_free(hapd->conf->ssid.wpa_psk);
|
|
|
|
}
|
|
|
|
if (hapd->conf->ssid.wpa_passphrase != NULL) {
|
|
|
|
os_free(hapd->conf->ssid.wpa_passphrase);
|
|
|
|
}
|
2018-08-13 04:37:56 -04:00
|
|
|
os_free(hapd->conf);
|
|
|
|
}
|
|
|
|
|
2021-11-10 01:18:00 -05:00
|
|
|
os_free(hapd);
|
2020-07-13 23:59:39 -04:00
|
|
|
esp_wifi_unset_appie_internal(WIFI_APPIE_WPA);
|
2022-05-13 04:02:24 -04:00
|
|
|
global_hapd = NULL;
|
2020-07-13 23:59:39 -04:00
|
|
|
|
2018-08-13 04:37:56 -04:00
|
|
|
return true;
|
|
|
|
}
|