1 /*
2  * hostapd / Configuration helper functions
3  * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "crypto/sha1.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/eapol_common.h"
15 #include "common/sae.h"
16 #include "ap/wpa_auth.h"
17 #include "ap/ap_config.h"
18 #include "utils/wpa_debug.h"
19 #include "ap/hostapd.h"
20 #include "ap/wpa_auth_i.h"
21 #include "esp_wifi_driver.h"
22 #include "esp_wifi_types.h"
23 
hostapd_config_defaults_bss(struct hostapd_bss_config * bss)24 void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
25 {
26     bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
27 
28     bss->wep_rekeying_period = 300;
29     /* use key0 in individual key and key1 in broadcast key */
30     bss->broadcast_key_idx_min = 1;
31     bss->broadcast_key_idx_max = 2;
32 
33     bss->wpa_group_rekey = 600;
34     bss->wpa_gmk_rekey = 86400;
35     bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
36     bss->wpa_pairwise = WPA_CIPHER_TKIP;
37     bss->wpa_group = WPA_CIPHER_TKIP;
38     bss->rsn_pairwise = 0;
39 
40     bss->max_num_sta = MAX_STA_COUNT;
41 
42     bss->dtim_period = 2;
43 
44     bss->ap_max_inactivity = 5*60;  //AP_MAX_INACTIVITY;
45     bss->eapol_version = EAPOL_VERSION;
46 
47     bss->max_listen_interval = 65535;
48 
49 #ifdef CONFIG_IEEE80211W
50     bss->assoc_sa_query_max_timeout = 1000;
51     bss->assoc_sa_query_retry_timeout = 201;
52 #endif /* CONFIG_IEEE80211W */
53 #ifdef EAP_SERVER_FAST
54      /* both anonymous and authenticated provisioning */
55     bss->eap_fast_prov = 3;
56     bss->pac_key_lifetime = 7 * 24 * 60 * 60;
57     bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
58 #endif /* EAP_SERVER_FAST */
59 
60     /* Set to -1 as defaults depends on HT in setup */
61     bss->wmm_enabled = -1;
62 
63 #ifdef CONFIG_IEEE80211R
64     bss->ft_over_ds = 1;
65 #endif /* CONFIG_IEEE80211R */
66 
67 }
68 
69 
hostapd_config_defaults(void)70 struct hostapd_config * hostapd_config_defaults(void)
71 {
72 #define ecw2cw(ecw) ((1 << (ecw)) - 1)
73 
74     struct hostapd_config *conf;
75     struct hostapd_bss_config *bss;
76 #undef ecw2cw
77 
78     conf = (struct hostapd_config *)os_zalloc(sizeof(*conf));
79     bss = (struct hostapd_bss_config *)os_zalloc(sizeof(*bss));
80     if (conf == NULL || bss == NULL) {
81     	wpa_printf(MSG_DEBUG, "Failed to allocate memory for "
82                "configuration data.");
83         os_free(conf);
84         os_free(bss);
85         return NULL;
86     }
87 
88     hostapd_config_defaults_bss(bss);
89 
90     conf->num_bss = 1;
91     conf->bss = bss;
92 
93     conf->beacon_int = 100;
94     conf->rts_threshold = -1; /* use driver default: 2347 */
95     conf->fragm_threshold = -1; /* user driver default: 2346 */
96     conf->send_probe_response = 1;
97 
98     conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
99 
100     conf->ap_table_max_size = 255;
101     conf->ap_table_expiration_time = 60;
102 
103     return conf;
104 }
105 
106 
hostapd_mac_comp(const void * a,const void * b)107 int hostapd_mac_comp(const void *a, const void *b)
108 {
109     return memcmp(a, b, sizeof(macaddr));
110 }
111 
112 
hostapd_mac_comp_empty(const void * a)113 int hostapd_mac_comp_empty(const void *a)
114 {
115     u8 empty[ETH_ALEN];
116 
117     os_bzero(empty, ETH_ALEN);
118 
119     return memcmp(a, empty, ETH_ALEN);
120 }
121 
hostapd_derive_psk(struct hostapd_ssid * ssid)122 static int hostapd_derive_psk(struct hostapd_ssid *ssid)
123 {
124     ssid->wpa_psk = (struct hostapd_wpa_psk *)os_zalloc(sizeof(struct hostapd_wpa_psk));
125     if (ssid->wpa_psk == NULL) {
126     	wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
127         return -1;
128     }
129     wpa_hexdump_ascii(MSG_DEBUG, "SSID",
130               (u8 *) ssid->ssid, ssid->ssid_len);
131     wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
132                   (u8 *) ssid->wpa_passphrase,
133                   strlen(ssid->wpa_passphrase));
134 #ifdef ESP_SUPPLICANT
135     memcpy(ssid->wpa_psk->psk, esp_wifi_ap_get_prof_pmk_internal(), PMK_LEN);
136 #else
137     /* It's too SLOW */
138     pbkdf2_sha1(ssid->wpa_passphrase,
139             ssid->ssid, ssid->ssid_len,
140             4096, ssid->wpa_psk->psk, PMK_LEN);
141 #endif
142     wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
143             ssid->wpa_psk->psk, PMK_LEN);
144     return 0;
145 }
146 
147 
hostapd_setup_sae_pt(struct hostapd_bss_config * conf)148 int hostapd_setup_sae_pt(struct hostapd_bss_config *conf)
149 {
150 #ifdef CONFIG_SAE
151     struct hostapd_ssid *ssid = &conf->ssid;
152     if ((conf->sae_pwe == SAE_PWE_HUNT_AND_PECK ||
153         !wpa_key_mgmt_sae(conf->wpa_key_mgmt)))
154         return 0; /* PT not needed */
155 
156     sae_deinit_pt(ssid->pt);
157     ssid->pt = NULL;
158     if (ssid->wpa_passphrase) {
159         ssid->pt = sae_derive_pt(conf->sae_groups, ssid->ssid,
160                      ssid->ssid_len,
161                      (const u8 *) ssid->wpa_passphrase,
162                      os_strlen(ssid->wpa_passphrase),
163                      NULL);
164         if (!ssid->pt)
165             return -1;
166     }
167 
168 #endif /* CONFIG_SAE */
169 
170     return 0;
171 }
172 
173 
hostapd_setup_wpa_psk(struct hostapd_bss_config * conf)174 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
175 {
176     struct hostapd_ssid *ssid = &conf->ssid;
177 
178     if (hostapd_setup_sae_pt(conf) < 0)
179         return -1;
180 
181     if (ssid->wpa_passphrase != NULL) {
182         if (ssid->wpa_psk != NULL) {
183         	wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
184                    "instead of passphrase");
185         } else {
186         	wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
187                    "passphrase\n");
188             if (hostapd_derive_psk(ssid) < 0)
189                 return -1;
190         }
191         ssid->wpa_psk->group = 1;
192     }
193 
194     return 0;
195 }
196 
197 
hostapd_wep_key_cmp(struct hostapd_wep_keys * a,struct hostapd_wep_keys * b)198 int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
199 {
200     int i;
201 
202     if (a->idx != b->idx || a->default_len != b->default_len)
203         return 1;
204     for (i = 0; i < NUM_WEP_KEYS; i++)
205         if (a->len[i] != b->len[i] ||
206         	memcmp(a->key[i], b->key[i], a->len[i]) != 0)
207             return 1;
208     return 0;
209 }
210 
211 /**
212  * hostapd_maclist_found - Find a MAC address from a list
213  * @list: MAC address list
214  * @num_entries: Number of addresses in the list
215  * @addr: Address to search for
216  * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
217  * Returns: 1 if address is in the list or 0 if not.
218  *
219  * Perform a binary search for given MAC address from a pre-sorted list.
220  */
hostapd_maclist_found(struct mac_acl_entry * list,int num_entries,const u8 * addr,int * vlan_id)221 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
222               const u8 *addr, int *vlan_id)
223 {
224     int start, end, middle, res;
225 
226     start = 0;
227     end = num_entries - 1;
228 
229     while (start <= end) {
230         middle = (start + end) / 2;
231         res = memcmp(list[middle].addr, addr, ETH_ALEN);
232         if (res == 0) {
233             if (vlan_id)
234                 *vlan_id = list[middle].vlan_id;
235             return 1;
236         }
237         if (res < 0)
238             start = middle + 1;
239         else
240             end = middle - 1;
241     }
242 
243     return 0;
244 }
245 
246 
hostapd_rate_found(int * list,int rate)247 int hostapd_rate_found(int *list, int rate)
248 {
249     int i;
250 
251     if (list == NULL)
252         return 0;
253 
254     for (i = 0; list[i] >= 0; i++)
255         if (list[i] == rate)
256             return 1;
257 
258     return 0;
259 }
260 
hostapd_get_psk(const struct hostapd_bss_config * conf,const u8 * addr,const u8 * prev_psk)261 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
262 			   const u8 *addr, const u8 *prev_psk)
263 {
264     struct hostapd_wpa_psk *psk;
265     int next_ok = prev_psk == NULL;
266 
267     for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
268         if (next_ok &&
269             (psk->group || memcmp(psk->addr, addr, ETH_ALEN) == 0))
270             return psk->psk;
271 
272         if (psk->psk == prev_psk)
273             next_ok = 1;
274     }
275 
276     return NULL;
277 }
278 
hostapd_config_clear_wpa_psk(struct hostapd_wpa_psk ** l)279 void hostapd_config_clear_wpa_psk(struct hostapd_wpa_psk **l)
280 {
281     struct hostapd_wpa_psk *psk, *tmp;
282 
283     for (psk = *l; psk;) {
284         tmp = psk;
285         psk = psk->next;
286         bin_clear_free(tmp, sizeof(*tmp));
287     }
288     *l = NULL;
289 }
290 
hostapd_config_free_bss(struct hostapd_bss_config * conf)291 void hostapd_config_free_bss(struct hostapd_bss_config *conf)
292 {
293     hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
294 #ifdef CONFIG_SAE
295     sae_deinit_pt(conf->ssid.pt);
296 #endif /* CONFIG_SAE */
297     os_free(conf);
298 }
299