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