1 /*
2  * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "sdkconfig.h"
8 #include "utils/includes.h"
9 
10 #include "utils/common.h"
11 #include "crypto/sha1.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/eapol_common.h"
14 #include "ap/wpa_auth.h"
15 #include "ap/ap_config.h"
16 #include "utils/wpa_debug.h"
17 #include "ap/hostapd.h"
18 #include "ap/wpa_auth_i.h"
19 #include "esp_wifi_driver.h"
20 #include "esp_wifi_types.h"
21 
hostap_init(void)22 void *hostap_init(void)
23 {
24     struct wifi_ssid *ssid = esp_wifi_ap_get_prof_ap_ssid_internal();
25     struct hostapd_data *hapd = NULL;
26     struct wpa_auth_config *auth_conf;
27     u8 mac[6];
28     u16 spp_attrubute = 0;
29     u8 pairwise_cipher;
30 
31     hapd = (struct hostapd_data *)os_zalloc(sizeof(struct hostapd_data));
32 
33     if (hapd == NULL) {
34         return NULL;
35     }
36 
37     hapd->conf = (struct hostapd_bss_config *)os_zalloc(sizeof(struct hostapd_bss_config));
38 
39     if (hapd->conf == NULL) {
40         os_free(hapd);
41         return NULL;
42     }
43 
44     auth_conf = (struct wpa_auth_config *)os_zalloc(sizeof(struct  wpa_auth_config));
45 
46     if (auth_conf == NULL) {
47         os_free(hapd->conf);
48         os_free(hapd);
49         hapd = NULL;
50         return NULL;
51     }
52     if (esp_wifi_ap_get_prof_authmode_internal() == WIFI_AUTH_WPA_PSK) {
53         auth_conf->wpa = WPA_PROTO_WPA;
54     }
55     if (esp_wifi_ap_get_prof_authmode_internal() == WIFI_AUTH_WPA2_PSK) {
56         auth_conf->wpa = WPA_PROTO_RSN;
57     }
58     if (esp_wifi_ap_get_prof_authmode_internal() == WIFI_AUTH_WPA_WPA2_PSK) {
59         auth_conf->wpa = WPA_PROTO_RSN | WPA_PROTO_WPA;
60     }
61 
62     pairwise_cipher = esp_wifi_ap_get_prof_pairwise_cipher_internal();
63     /* TKIP is compulsory in WPA Mode */
64     if (auth_conf->wpa == WPA_PROTO_WPA && pairwise_cipher == WIFI_CIPHER_TYPE_CCMP) {
65         pairwise_cipher = WIFI_CIPHER_TYPE_TKIP_CCMP;
66     }
67     if (pairwise_cipher == WIFI_CIPHER_TYPE_TKIP) {
68         auth_conf->wpa_group = WPA_CIPHER_TKIP;
69         auth_conf->wpa_pairwise = WPA_CIPHER_TKIP;
70         auth_conf->rsn_pairwise = WPA_CIPHER_TKIP;
71     } else if (pairwise_cipher == WIFI_CIPHER_TYPE_CCMP) {
72         auth_conf->wpa_group = WPA_CIPHER_CCMP;
73         auth_conf->wpa_pairwise = WPA_CIPHER_CCMP;
74         auth_conf->rsn_pairwise = WPA_CIPHER_CCMP;
75     } else {
76         auth_conf->wpa_group = WPA_CIPHER_TKIP;
77         auth_conf->wpa_pairwise = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
78         auth_conf->rsn_pairwise = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
79     }
80 
81     auth_conf->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
82     auth_conf->eapol_version = EAPOL_VERSION;
83 
84     spp_attrubute = esp_wifi_get_spp_attrubute_internal(WIFI_IF_AP);
85     auth_conf->spp_sup.capable = ((spp_attrubute & WPA_CAPABILITY_SPP_CAPABLE) ? SPP_AMSDU_CAP_ENABLE : SPP_AMSDU_CAP_DISABLE);
86     auth_conf->spp_sup.require = ((spp_attrubute & WPA_CAPABILITY_SPP_REQUIRED) ? SPP_AMSDU_REQ_ENABLE : SPP_AMSDU_REQ_DISABLE);
87 
88     memcpy(hapd->conf->ssid.ssid, ssid->ssid, ssid->len);
89     hapd->conf->ssid.ssid_len = ssid->len;
90     hapd->conf->ssid.wpa_passphrase = (char *)os_zalloc(64);
91     if (hapd->conf->ssid.wpa_passphrase == NULL) {
92         os_free(auth_conf);
93         os_free(hapd->conf);
94         os_free(hapd);
95         hapd = NULL;
96         return NULL;
97     }
98     memcpy(hapd->conf->ssid.wpa_passphrase, esp_wifi_ap_get_prof_password_internal(), strlen((char *)esp_wifi_ap_get_prof_password_internal()));
99 
100     hapd->conf->ap_max_inactivity = 5 * 60;
101     hostapd_setup_wpa_psk(hapd->conf);
102 
103     esp_wifi_get_macaddr_internal(WIFI_IF_AP, mac);
104 
105     hapd->wpa_auth = wpa_init(mac, auth_conf, NULL);
106     esp_wifi_set_appie_internal(WIFI_APPIE_WPA, hapd->wpa_auth->wpa_ie, (uint16_t)hapd->wpa_auth->wpa_ie_len, 0);
107     os_free(auth_conf);
108 
109     return (void *)hapd;
110 }
111 
hostap_deinit(void * data)112 bool hostap_deinit(void *data)
113 {
114     struct hostapd_data *hapd = (struct hostapd_data *)data;
115 
116     if (hapd == NULL) {
117         return true;
118     }
119 
120     if (hapd->wpa_auth != NULL) {
121         if (hapd->wpa_auth->wpa_ie != NULL) {
122             os_free(hapd->wpa_auth->wpa_ie);
123         }
124         if (hapd->wpa_auth->group != NULL) {
125             os_free(hapd->wpa_auth->group);
126         }
127         os_free(hapd->wpa_auth);
128     }
129 
130     if (hapd->conf != NULL) {
131         if (hapd->conf->ssid.wpa_psk != NULL) {
132             os_free(hapd->conf->ssid.wpa_psk);
133         }
134         if (hapd->conf->ssid.wpa_passphrase != NULL) {
135             os_free(hapd->conf->ssid.wpa_passphrase);
136         }
137         os_free(hapd->conf);
138     }
139 
140     os_free(hapd);
141     esp_wifi_unset_appie_internal(WIFI_APPIE_WPA);
142 
143     return true;
144 }
145