blob: 1c0ed7aa938f0453ce944ca9dae5926667dddb1b [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Configuration helper functions
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003 * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "crypto/sha1.h"
13#include "radius/radius_client.h"
14#include "common/ieee802_11_defs.h"
15#include "common/eapol_common.h"
16#include "eap_common/eap_wsc_common.h"
17#include "eap_server/eap.h"
18#include "wpa_auth.h"
19#include "sta_info.h"
20#include "ap_config.h"
21
22
23static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
24{
25 struct hostapd_vlan *vlan, *prev;
26
27 vlan = bss->vlan;
28 prev = NULL;
29 while (vlan) {
30 prev = vlan;
31 vlan = vlan->next;
32 os_free(prev);
33 }
34
35 bss->vlan = NULL;
36}
37
38
39void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
40{
41 bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
42 bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
43 bss->logger_syslog = (unsigned int) -1;
44 bss->logger_stdout = (unsigned int) -1;
45
46 bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
47
48 bss->wep_rekeying_period = 300;
49 /* use key0 in individual key and key1 in broadcast key */
50 bss->broadcast_key_idx_min = 1;
51 bss->broadcast_key_idx_max = 2;
52 bss->eap_reauth_period = 3600;
53
54 bss->wpa_group_rekey = 600;
55 bss->wpa_gmk_rekey = 86400;
56 bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
57 bss->wpa_pairwise = WPA_CIPHER_TKIP;
58 bss->wpa_group = WPA_CIPHER_TKIP;
59 bss->rsn_pairwise = 0;
60
61 bss->max_num_sta = MAX_STA_COUNT;
62
63 bss->dtim_period = 2;
64
65 bss->radius_server_auth_port = 1812;
66 bss->ap_max_inactivity = AP_MAX_INACTIVITY;
67 bss->eapol_version = EAPOL_VERSION;
68
69 bss->max_listen_interval = 65535;
70
71 bss->pwd_group = 19; /* ECC: GF(p=256) */
72
73#ifdef CONFIG_IEEE80211W
74 bss->assoc_sa_query_max_timeout = 1000;
75 bss->assoc_sa_query_retry_timeout = 201;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -070076 bss->group_mgmt_cipher = WPA_CIPHER_AES_128_CMAC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077#endif /* CONFIG_IEEE80211W */
78#ifdef EAP_SERVER_FAST
79 /* both anonymous and authenticated provisioning */
80 bss->eap_fast_prov = 3;
81 bss->pac_key_lifetime = 7 * 24 * 60 * 60;
82 bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
83#endif /* EAP_SERVER_FAST */
84
85 /* Set to -1 as defaults depends on HT in setup */
86 bss->wmm_enabled = -1;
87
88#ifdef CONFIG_IEEE80211R
89 bss->ft_over_ds = 1;
90#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt04949592012-07-19 12:16:46 -070091
92 bss->radius_das_time_window = 300;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080093
94 bss->sae_anti_clogging_threshold = 5;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095}
96
97
98struct hostapd_config * hostapd_config_defaults(void)
99{
100#define ecw2cw(ecw) ((1 << (ecw)) - 1)
101
102 struct hostapd_config *conf;
103 struct hostapd_bss_config *bss;
104 const int aCWmin = 4, aCWmax = 10;
105 const struct hostapd_wmm_ac_params ac_bk =
106 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
107 const struct hostapd_wmm_ac_params ac_be =
108 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
109 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700110 { aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700112 { aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 const struct hostapd_tx_queue_params txq_bk =
114 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
115 const struct hostapd_tx_queue_params txq_be =
116 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
117 const struct hostapd_tx_queue_params txq_vi =
118 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
119 const struct hostapd_tx_queue_params txq_vo =
120 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
121 (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
122
123#undef ecw2cw
124
125 conf = os_zalloc(sizeof(*conf));
126 bss = os_zalloc(sizeof(*bss));
127 if (conf == NULL || bss == NULL) {
128 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
129 "configuration data.");
130 os_free(conf);
131 os_free(bss);
132 return NULL;
133 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800134 conf->bss = os_calloc(1, sizeof(struct hostapd_bss_config *));
135 if (conf->bss == NULL) {
136 os_free(conf);
137 os_free(bss);
138 return NULL;
139 }
140 conf->bss[0] = bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141
142 bss->radius = os_zalloc(sizeof(*bss->radius));
143 if (bss->radius == NULL) {
Dmitry Shmidt97672262014-02-03 13:02:54 -0800144 os_free(conf->bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145 os_free(conf);
146 os_free(bss);
147 return NULL;
148 }
149
150 hostapd_config_defaults_bss(bss);
151
152 conf->num_bss = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153
154 conf->beacon_int = 100;
155 conf->rts_threshold = -1; /* use driver default: 2347 */
156 conf->fragm_threshold = -1; /* user driver default: 2346 */
157 conf->send_probe_response = 1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800158 /* Set to invalid value means do not add Power Constraint IE */
159 conf->local_pwr_constraint = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160
161 conf->wmm_ac_params[0] = ac_be;
162 conf->wmm_ac_params[1] = ac_bk;
163 conf->wmm_ac_params[2] = ac_vi;
164 conf->wmm_ac_params[3] = ac_vo;
165
166 conf->tx_queue[0] = txq_vo;
167 conf->tx_queue[1] = txq_vi;
168 conf->tx_queue[2] = txq_be;
169 conf->tx_queue[3] = txq_bk;
170
171 conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
172
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800173 conf->ap_table_max_size = 255;
174 conf->ap_table_expiration_time = 60;
175
Dmitry Shmidt8da800a2013-04-24 12:57:01 -0700176#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700177 conf->ignore_probe_probability = 0.0;
178 conf->ignore_auth_probability = 0.0;
179 conf->ignore_assoc_probability = 0.0;
180 conf->ignore_reassoc_probability = 0.0;
181 conf->corrupt_gtk_rekey_mic_probability = 0.0;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -0700182#endif /* CONFIG_TESTING_OPTIONS */
183
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700184#ifdef CONFIG_ACS
185 conf->acs_num_scans = 5;
186#endif /* CONFIG_ACS */
187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188 return conf;
189}
190
191
192int hostapd_mac_comp(const void *a, const void *b)
193{
194 return os_memcmp(a, b, sizeof(macaddr));
195}
196
197
198int hostapd_mac_comp_empty(const void *a)
199{
200 macaddr empty = { 0 };
201 return os_memcmp(a, empty, sizeof(macaddr));
202}
203
204
205static int hostapd_config_read_wpa_psk(const char *fname,
206 struct hostapd_ssid *ssid)
207{
208 FILE *f;
209 char buf[128], *pos;
210 int line = 0, ret = 0, len, ok;
211 u8 addr[ETH_ALEN];
212 struct hostapd_wpa_psk *psk;
213
214 if (!fname)
215 return 0;
216
217 f = fopen(fname, "r");
218 if (!f) {
219 wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
220 return -1;
221 }
222
223 while (fgets(buf, sizeof(buf), f)) {
224 line++;
225
226 if (buf[0] == '#')
227 continue;
228 pos = buf;
229 while (*pos != '\0') {
230 if (*pos == '\n') {
231 *pos = '\0';
232 break;
233 }
234 pos++;
235 }
236 if (buf[0] == '\0')
237 continue;
238
239 if (hwaddr_aton(buf, addr)) {
240 wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
241 "line %d in '%s'", buf, line, fname);
242 ret = -1;
243 break;
244 }
245
246 psk = os_zalloc(sizeof(*psk));
247 if (psk == NULL) {
248 wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
249 ret = -1;
250 break;
251 }
252 if (is_zero_ether_addr(addr))
253 psk->group = 1;
254 else
255 os_memcpy(psk->addr, addr, ETH_ALEN);
256
257 pos = buf + 17;
258 if (*pos == '\0') {
259 wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
260 line, fname);
261 os_free(psk);
262 ret = -1;
263 break;
264 }
265 pos++;
266
267 ok = 0;
268 len = os_strlen(pos);
269 if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
270 ok = 1;
271 else if (len >= 8 && len < 64) {
272 pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
273 4096, psk->psk, PMK_LEN);
274 ok = 1;
275 }
276 if (!ok) {
277 wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
278 "'%s'", pos, line, fname);
279 os_free(psk);
280 ret = -1;
281 break;
282 }
283
284 psk->next = ssid->wpa_psk;
285 ssid->wpa_psk = psk;
286 }
287
288 fclose(f);
289
290 return ret;
291}
292
293
294static int hostapd_derive_psk(struct hostapd_ssid *ssid)
295{
296 ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
297 if (ssid->wpa_psk == NULL) {
298 wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
299 return -1;
300 }
301 wpa_hexdump_ascii(MSG_DEBUG, "SSID",
302 (u8 *) ssid->ssid, ssid->ssid_len);
303 wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
304 (u8 *) ssid->wpa_passphrase,
305 os_strlen(ssid->wpa_passphrase));
306 pbkdf2_sha1(ssid->wpa_passphrase,
307 ssid->ssid, ssid->ssid_len,
308 4096, ssid->wpa_psk->psk, PMK_LEN);
309 wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
310 ssid->wpa_psk->psk, PMK_LEN);
311 return 0;
312}
313
314
315int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
316{
317 struct hostapd_ssid *ssid = &conf->ssid;
318
319 if (ssid->wpa_passphrase != NULL) {
320 if (ssid->wpa_psk != NULL) {
321 wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
322 "instead of passphrase");
323 } else {
324 wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
325 "passphrase");
326 if (hostapd_derive_psk(ssid) < 0)
327 return -1;
328 }
329 ssid->wpa_psk->group = 1;
330 }
331
332 if (ssid->wpa_psk_file) {
333 if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
334 &conf->ssid))
335 return -1;
336 }
337
338 return 0;
339}
340
341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
343 int num_servers)
344{
345 int i;
346
347 for (i = 0; i < num_servers; i++) {
348 os_free(servers[i].shared_secret);
349 }
350 os_free(servers);
351}
352
353
Dmitry Shmidt04949592012-07-19 12:16:46 -0700354struct hostapd_radius_attr *
355hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
356{
357 for (; attr; attr = attr->next) {
358 if (attr->type == type)
359 return attr;
360 }
361 return NULL;
362}
363
364
365static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
366{
367 struct hostapd_radius_attr *prev;
368
369 while (attr) {
370 prev = attr;
371 attr = attr->next;
372 wpabuf_free(prev->val);
373 os_free(prev);
374 }
375}
376
377
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700378void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379{
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700380 hostapd_config_free_radius_attr(user->accept_attr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 os_free(user->identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700382 bin_clear_free(user->password, user->password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 os_free(user);
384}
385
386
387static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
388{
389 int i;
390 for (i = 0; i < NUM_WEP_KEYS; i++) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700391 bin_clear_free(keys->key[i], keys->len[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 keys->key[i] = NULL;
393 }
394}
395
396
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800397void hostapd_config_free_bss(struct hostapd_bss_config *conf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398{
399 struct hostapd_wpa_psk *psk, *prev;
400 struct hostapd_eap_user *user, *prev_user;
401
402 if (conf == NULL)
403 return;
404
405 psk = conf->ssid.wpa_psk;
406 while (psk) {
407 prev = psk;
408 psk = psk->next;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700409 bin_clear_free(prev, sizeof(*prev));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410 }
411
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700412 str_clear_free(conf->ssid.wpa_passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 os_free(conf->ssid.wpa_psk_file);
414 hostapd_config_free_wep(&conf->ssid.wep);
415#ifdef CONFIG_FULL_DYNAMIC_VLAN
416 os_free(conf->ssid.vlan_tagged_interface);
417#endif /* CONFIG_FULL_DYNAMIC_VLAN */
418
419 user = conf->eap_user;
420 while (user) {
421 prev_user = user;
422 user = user->next;
423 hostapd_config_free_eap_user(prev_user);
424 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800425 os_free(conf->eap_user_sqlite);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 os_free(conf->eap_req_id_text);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800428 os_free(conf->erp_domain);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 os_free(conf->accept_mac);
430 os_free(conf->deny_mac);
431 os_free(conf->nas_identifier);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800432 if (conf->radius) {
433 hostapd_config_free_radius(conf->radius->auth_servers,
434 conf->radius->num_auth_servers);
435 hostapd_config_free_radius(conf->radius->acct_servers,
436 conf->radius->num_acct_servers);
437 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700438 hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
439 hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 os_free(conf->rsn_preauth_interfaces);
441 os_free(conf->ctrl_interface);
442 os_free(conf->ca_cert);
443 os_free(conf->server_cert);
444 os_free(conf->private_key);
445 os_free(conf->private_key_passwd);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700446 os_free(conf->ocsp_stapling_response);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 os_free(conf->dh_file);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800448 os_free(conf->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449 os_free(conf->pac_opaque_encr_key);
450 os_free(conf->eap_fast_a_id);
451 os_free(conf->eap_fast_a_id_info);
452 os_free(conf->eap_sim_db);
453 os_free(conf->radius_server_clients);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454 os_free(conf->radius);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700455 os_free(conf->radius_das_shared_secret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456 hostapd_config_free_vlan(conf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800457 os_free(conf->time_zone);
458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459#ifdef CONFIG_IEEE80211R
460 {
461 struct ft_remote_r0kh *r0kh, *r0kh_prev;
462 struct ft_remote_r1kh *r1kh, *r1kh_prev;
463
464 r0kh = conf->r0kh_list;
465 conf->r0kh_list = NULL;
466 while (r0kh) {
467 r0kh_prev = r0kh;
468 r0kh = r0kh->next;
469 os_free(r0kh_prev);
470 }
471
472 r1kh = conf->r1kh_list;
473 conf->r1kh_list = NULL;
474 while (r1kh) {
475 r1kh_prev = r1kh;
476 r1kh = r1kh->next;
477 os_free(r1kh_prev);
478 }
479 }
480#endif /* CONFIG_IEEE80211R */
481
482#ifdef CONFIG_WPS
483 os_free(conf->wps_pin_requests);
484 os_free(conf->device_name);
485 os_free(conf->manufacturer);
486 os_free(conf->model_name);
487 os_free(conf->model_number);
488 os_free(conf->serial_number);
489 os_free(conf->config_methods);
490 os_free(conf->ap_pin);
491 os_free(conf->extra_cred);
492 os_free(conf->ap_settings);
493 os_free(conf->upnp_iface);
494 os_free(conf->friendly_name);
495 os_free(conf->manufacturer_url);
496 os_free(conf->model_description);
497 os_free(conf->model_url);
498 os_free(conf->upc);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800499 {
500 unsigned int i;
501
502 for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++)
503 wpabuf_free(conf->wps_vendor_ext[i]);
504 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700505 wpabuf_free(conf->wps_nfc_dh_pubkey);
506 wpabuf_free(conf->wps_nfc_dh_privkey);
507 wpabuf_free(conf->wps_nfc_dev_pw);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800509
510 os_free(conf->roaming_consortium);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700511 os_free(conf->venue_name);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700512 os_free(conf->nai_realm_data);
513 os_free(conf->network_auth_type);
514 os_free(conf->anqp_3gpp_cell_net);
515 os_free(conf->domain_name);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800516
517#ifdef CONFIG_RADIUS_TEST
518 os_free(conf->dump_msk_file);
519#endif /* CONFIG_RADIUS_TEST */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700520
521#ifdef CONFIG_HS20
522 os_free(conf->hs20_oper_friendly_name);
523 os_free(conf->hs20_wan_metrics);
524 os_free(conf->hs20_connection_capability);
525 os_free(conf->hs20_operating_class);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800526 os_free(conf->hs20_icons);
527 if (conf->hs20_osu_providers) {
528 size_t i;
529 for (i = 0; i < conf->hs20_osu_providers_count; i++) {
530 struct hs20_osu_provider *p;
531 size_t j;
532 p = &conf->hs20_osu_providers[i];
533 os_free(p->friendly_name);
534 os_free(p->server_uri);
535 os_free(p->method_list);
536 for (j = 0; j < p->icons_count; j++)
537 os_free(p->icons[j]);
538 os_free(p->icons);
539 os_free(p->osu_nai);
540 os_free(p->service_desc);
541 }
542 os_free(conf->hs20_osu_providers);
543 }
544 os_free(conf->subscr_remediation_url);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700545#endif /* CONFIG_HS20 */
546
547 wpabuf_free(conf->vendor_elements);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800548
549 os_free(conf->sae_groups);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700550
Dmitry Shmidt0207e232014-09-03 14:58:37 -0700551 os_free(conf->wowlan_triggers);
552
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700553 os_free(conf->server_id);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800554
555 os_free(conf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556}
557
558
559/**
560 * hostapd_config_free - Free hostapd configuration
561 * @conf: Configuration data from hostapd_config_read().
562 */
563void hostapd_config_free(struct hostapd_config *conf)
564{
565 size_t i;
566
567 if (conf == NULL)
568 return;
569
570 for (i = 0; i < conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800571 hostapd_config_free_bss(conf->bss[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 os_free(conf->bss);
573 os_free(conf->supported_rates);
574 os_free(conf->basic_rates);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700575 os_free(conf->chanlist);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800576 os_free(conf->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577
578 os_free(conf);
579}
580
581
582/**
583 * hostapd_maclist_found - Find a MAC address from a list
584 * @list: MAC address list
585 * @num_entries: Number of addresses in the list
586 * @addr: Address to search for
587 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
588 * Returns: 1 if address is in the list or 0 if not.
589 *
590 * Perform a binary search for given MAC address from a pre-sorted list.
591 */
592int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
593 const u8 *addr, int *vlan_id)
594{
595 int start, end, middle, res;
596
597 start = 0;
598 end = num_entries - 1;
599
600 while (start <= end) {
601 middle = (start + end) / 2;
602 res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
603 if (res == 0) {
604 if (vlan_id)
605 *vlan_id = list[middle].vlan_id;
606 return 1;
607 }
608 if (res < 0)
609 start = middle + 1;
610 else
611 end = middle - 1;
612 }
613
614 return 0;
615}
616
617
618int hostapd_rate_found(int *list, int rate)
619{
620 int i;
621
622 if (list == NULL)
623 return 0;
624
625 for (i = 0; list[i] >= 0; i++)
626 if (list[i] == rate)
627 return 1;
628
629 return 0;
630}
631
632
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700633int hostapd_vlan_id_valid(struct hostapd_vlan *vlan, int vlan_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634{
635 struct hostapd_vlan *v = vlan;
636 while (v) {
637 if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700638 return 1;
639 v = v->next;
640 }
641 return 0;
642}
643
644
645const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
646{
647 struct hostapd_vlan *v = vlan;
648 while (v) {
649 if (v->vlan_id == vlan_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 return v->ifname;
651 v = v->next;
652 }
653 return NULL;
654}
655
656
657const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700658 const u8 *addr, const u8 *p2p_dev_addr,
659 const u8 *prev_psk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660{
661 struct hostapd_wpa_psk *psk;
662 int next_ok = prev_psk == NULL;
663
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700664 if (p2p_dev_addr && !is_zero_ether_addr(p2p_dev_addr)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700665 wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
666 " p2p_dev_addr=" MACSTR " prev_psk=%p",
667 MAC2STR(addr), MAC2STR(p2p_dev_addr), prev_psk);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700668 addr = NULL; /* Use P2P Device Address for matching */
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700669 } else {
670 wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
671 " prev_psk=%p",
672 MAC2STR(addr), prev_psk);
673 }
674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
676 if (next_ok &&
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700677 (psk->group ||
678 (addr && os_memcmp(psk->addr, addr, ETH_ALEN) == 0) ||
679 (!addr && p2p_dev_addr &&
680 os_memcmp(psk->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
681 0)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 return psk->psk;
683
684 if (psk->psk == prev_psk)
685 next_ok = 1;
686 }
687
688 return NULL;
689}
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800690
691
692static int hostapd_config_check_bss(struct hostapd_bss_config *bss,
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800693 struct hostapd_config *conf,
694 int full_config)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800695{
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800696 if (full_config && bss->ieee802_1x && !bss->eap_server &&
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800697 !bss->radius->auth_servers) {
698 wpa_printf(MSG_ERROR, "Invalid IEEE 802.1X configuration (no "
699 "EAP authenticator configured).");
700 return -1;
701 }
702
703 if (bss->wpa) {
704 int wep, i;
705
706 wep = bss->default_wep_key_len > 0 ||
707 bss->individual_wep_key_len > 0;
708 for (i = 0; i < NUM_WEP_KEYS; i++) {
709 if (bss->ssid.wep.keys_set) {
710 wep = 1;
711 break;
712 }
713 }
714
715 if (wep) {
716 wpa_printf(MSG_ERROR, "WEP configuration in a WPA network is not supported");
717 return -1;
718 }
719 }
720
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800721 if (full_config && bss->wpa &&
722 bss->wpa_psk_radius != PSK_RADIUS_IGNORED &&
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800723 bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
724 wpa_printf(MSG_ERROR, "WPA-PSK using RADIUS enabled, but no "
725 "RADIUS checking (macaddr_acl=2) enabled.");
726 return -1;
727 }
728
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800729 if (full_config && bss->wpa && (bss->wpa_key_mgmt & WPA_KEY_MGMT_PSK) &&
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800730 bss->ssid.wpa_psk == NULL && bss->ssid.wpa_passphrase == NULL &&
731 bss->ssid.wpa_psk_file == NULL &&
732 (bss->wpa_psk_radius != PSK_RADIUS_REQUIRED ||
733 bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH)) {
734 wpa_printf(MSG_ERROR, "WPA-PSK enabled, but PSK or passphrase "
735 "is not configured.");
736 return -1;
737 }
738
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800739 if (full_config && hostapd_mac_comp_empty(bss->bssid) != 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800740 size_t i;
741
742 for (i = 0; i < conf->num_bss; i++) {
743 if (conf->bss[i] != bss &&
744 (hostapd_mac_comp(conf->bss[i]->bssid,
745 bss->bssid) == 0)) {
746 wpa_printf(MSG_ERROR, "Duplicate BSSID " MACSTR
747 " on interface '%s' and '%s'.",
748 MAC2STR(bss->bssid),
749 conf->bss[i]->iface, bss->iface);
750 return -1;
751 }
752 }
753 }
754
755#ifdef CONFIG_IEEE80211R
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800756 if (full_config && wpa_key_mgmt_ft(bss->wpa_key_mgmt) &&
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800757 (bss->nas_identifier == NULL ||
758 os_strlen(bss->nas_identifier) < 1 ||
759 os_strlen(bss->nas_identifier) > FT_R0KH_ID_MAX_LEN)) {
760 wpa_printf(MSG_ERROR, "FT (IEEE 802.11r) requires "
761 "nas_identifier to be configured as a 1..48 octet "
762 "string");
763 return -1;
764 }
765#endif /* CONFIG_IEEE80211R */
766
767#ifdef CONFIG_IEEE80211N
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800768 if (full_config && conf->ieee80211n &&
769 conf->hw_mode == HOSTAPD_MODE_IEEE80211B) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800770 bss->disable_11n = 1;
771 wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) in 11b mode is not "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700772 "allowed, disabling HT capabilities");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800773 }
774
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800775 if (full_config && conf->ieee80211n &&
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800776 bss->ssid.security_policy == SECURITY_STATIC_WEP) {
777 bss->disable_11n = 1;
778 wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WEP is not "
779 "allowed, disabling HT capabilities");
780 }
781
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800782 if (full_config && conf->ieee80211n && bss->wpa &&
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800783 !(bss->wpa_pairwise & WPA_CIPHER_CCMP) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800784 !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
785 WPA_CIPHER_CCMP_256 | WPA_CIPHER_GCMP_256)))
786 {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800787 bss->disable_11n = 1;
788 wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WPA/WPA2 "
789 "requires CCMP/GCMP to be enabled, disabling HT "
790 "capabilities");
791 }
792#endif /* CONFIG_IEEE80211N */
793
Dmitry Shmidt15907092014-03-25 10:42:57 -0700794#ifdef CONFIG_WPS
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800795 if (full_config && bss->wps_state && bss->ignore_broadcast_ssid) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800796 wpa_printf(MSG_INFO, "WPS: ignore_broadcast_ssid "
797 "configuration forced WPS to be disabled");
798 bss->wps_state = 0;
799 }
800
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800801 if (full_config && bss->wps_state &&
802 bss->ssid.wep.keys_set && bss->wpa == 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800803 wpa_printf(MSG_INFO, "WPS: WEP configuration forced WPS to be "
804 "disabled");
805 bss->wps_state = 0;
806 }
807
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800808 if (full_config && bss->wps_state && bss->wpa &&
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800809 (!(bss->wpa & 2) ||
810 !(bss->rsn_pairwise & WPA_CIPHER_CCMP))) {
811 wpa_printf(MSG_INFO, "WPS: WPA/TKIP configuration without "
812 "WPA2/CCMP forced WPS to be disabled");
813 bss->wps_state = 0;
814 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700815#endif /* CONFIG_WPS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800816
817#ifdef CONFIG_HS20
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800818 if (full_config && bss->hs20 &&
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800819 (!(bss->wpa & 2) ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800820 !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
821 WPA_CIPHER_CCMP_256 |
822 WPA_CIPHER_GCMP_256)))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800823 wpa_printf(MSG_ERROR, "HS 2.0: WPA2-Enterprise/CCMP "
824 "configuration is required for Hotspot 2.0 "
825 "functionality");
826 return -1;
827 }
828#endif /* CONFIG_HS20 */
829
830 return 0;
831}
832
833
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800834int hostapd_config_check(struct hostapd_config *conf, int full_config)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800835{
836 size_t i;
837
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800838 if (full_config && conf->ieee80211d &&
839 (!conf->country[0] || !conf->country[1])) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800840 wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11d without "
841 "setting the country_code");
842 return -1;
843 }
844
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800845 if (full_config && conf->ieee80211h && !conf->ieee80211d) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800846 wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11h without "
847 "IEEE 802.11d enabled");
848 return -1;
849 }
850
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800851 if (full_config && conf->local_pwr_constraint != -1 &&
852 !conf->ieee80211d) {
853 wpa_printf(MSG_ERROR, "Cannot add Power Constraint element without Country element");
854 return -1;
855 }
856
857 if (full_config && conf->spectrum_mgmt_required &&
858 conf->local_pwr_constraint == -1) {
859 wpa_printf(MSG_ERROR, "Cannot set Spectrum Management bit without Country and Power Constraint elements");
860 return -1;
861 }
862
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800863 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800864 if (hostapd_config_check_bss(conf->bss[i], conf, full_config))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800865 return -1;
866 }
867
868 return 0;
869}
870
871
Dmitry Shmidt71757432014-06-02 13:50:35 -0700872void hostapd_set_security_params(struct hostapd_bss_config *bss,
873 int full_config)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800874{
875 if (bss->individual_wep_key_len == 0) {
876 /* individual keys are not use; can use key idx0 for
877 * broadcast keys */
878 bss->broadcast_key_idx_min = 0;
879 }
880
881 if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
882 bss->rsn_pairwise = bss->wpa_pairwise;
883 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
884 bss->rsn_pairwise);
885
Dmitry Shmidt71757432014-06-02 13:50:35 -0700886 if (full_config) {
887 bss->radius->auth_server = bss->radius->auth_servers;
888 bss->radius->acct_server = bss->radius->acct_servers;
889 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800890
891 if (bss->wpa && bss->ieee802_1x) {
892 bss->ssid.security_policy = SECURITY_WPA;
893 } else if (bss->wpa) {
894 bss->ssid.security_policy = SECURITY_WPA_PSK;
895 } else if (bss->ieee802_1x) {
896 int cipher = WPA_CIPHER_NONE;
897 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
898 bss->ssid.wep.default_len = bss->default_wep_key_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800899 if (full_config && bss->default_wep_key_len) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800900 cipher = bss->default_wep_key_len >= 13 ?
901 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800902 } else if (full_config && bss->ssid.wep.keys_set) {
903 if (bss->ssid.wep.len[0] >= 13)
904 cipher = WPA_CIPHER_WEP104;
905 else
906 cipher = WPA_CIPHER_WEP40;
907 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800908 bss->wpa_group = cipher;
909 bss->wpa_pairwise = cipher;
910 bss->rsn_pairwise = cipher;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800911 if (full_config)
912 bss->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800913 } else if (bss->ssid.wep.keys_set) {
914 int cipher = WPA_CIPHER_WEP40;
915 if (bss->ssid.wep.len[0] >= 13)
916 cipher = WPA_CIPHER_WEP104;
917 bss->ssid.security_policy = SECURITY_STATIC_WEP;
918 bss->wpa_group = cipher;
919 bss->wpa_pairwise = cipher;
920 bss->rsn_pairwise = cipher;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800921 if (full_config)
922 bss->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800923 } else if (bss->osen) {
924 bss->ssid.security_policy = SECURITY_OSEN;
925 bss->wpa_group = WPA_CIPHER_CCMP;
926 bss->wpa_pairwise = 0;
927 bss->rsn_pairwise = WPA_CIPHER_CCMP;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800928 } else {
929 bss->ssid.security_policy = SECURITY_PLAINTEXT;
930 bss->wpa_group = WPA_CIPHER_NONE;
931 bss->wpa_pairwise = WPA_CIPHER_NONE;
932 bss->rsn_pairwise = WPA_CIPHER_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800933 if (full_config)
934 bss->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800935 }
936}