2 * hostapd / Configuration helper functions
3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
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"
20 #include "ap_config.h"
23 static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
25 struct hostapd_vlan *vlan, *prev;
39 void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
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;
46 bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
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;
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;
61 bss->max_num_sta = MAX_STA_COUNT;
65 bss->radius_server_auth_port = 1812;
66 bss->ap_max_inactivity = AP_MAX_INACTIVITY;
67 bss->eapol_version = EAPOL_VERSION;
69 bss->max_listen_interval = 65535;
71 bss->pwd_group = 19; /* ECC: GF(p=256) */
73 #ifdef CONFIG_IEEE80211W
74 bss->assoc_sa_query_max_timeout = 1000;
75 bss->assoc_sa_query_retry_timeout = 201;
76 #endif /* CONFIG_IEEE80211W */
77 #ifdef EAP_SERVER_FAST
78 /* both anonymous and authenticated provisioning */
79 bss->eap_fast_prov = 3;
80 bss->pac_key_lifetime = 7 * 24 * 60 * 60;
81 bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
82 #endif /* EAP_SERVER_FAST */
84 /* Set to -1 as defaults depends on HT in setup */
85 bss->wmm_enabled = -1;
87 #ifdef CONFIG_IEEE80211R
89 #endif /* CONFIG_IEEE80211R */
93 struct hostapd_config * hostapd_config_defaults(void)
95 #define ecw2cw(ecw) ((1 << (ecw)) - 1)
97 struct hostapd_config *conf;
98 struct hostapd_bss_config *bss;
99 const int aCWmin = 4, aCWmax = 10;
100 const struct hostapd_wmm_ac_params ac_bk =
101 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
102 const struct hostapd_wmm_ac_params ac_be =
103 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
104 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
105 { aCWmin - 1, aCWmin, 2, 3000 / 32, 1 };
106 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
107 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 1 };
108 const struct hostapd_tx_queue_params txq_bk =
109 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
110 const struct hostapd_tx_queue_params txq_be =
111 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
112 const struct hostapd_tx_queue_params txq_vi =
113 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
114 const struct hostapd_tx_queue_params txq_vo =
115 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
116 (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
120 conf = os_zalloc(sizeof(*conf));
121 bss = os_zalloc(sizeof(*bss));
122 if (conf == NULL || bss == NULL) {
123 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
124 "configuration data.");
130 bss->radius = os_zalloc(sizeof(*bss->radius));
131 if (bss->radius == NULL) {
137 hostapd_config_defaults_bss(bss);
142 conf->beacon_int = 100;
143 conf->rts_threshold = -1; /* use driver default: 2347 */
144 conf->fragm_threshold = -1; /* user driver default: 2346 */
145 conf->send_probe_response = 1;
147 conf->wmm_ac_params[0] = ac_be;
148 conf->wmm_ac_params[1] = ac_bk;
149 conf->wmm_ac_params[2] = ac_vi;
150 conf->wmm_ac_params[3] = ac_vo;
152 conf->tx_queue[0] = txq_vo;
153 conf->tx_queue[1] = txq_vi;
154 conf->tx_queue[2] = txq_be;
155 conf->tx_queue[3] = txq_bk;
157 conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
163 int hostapd_mac_comp(const void *a, const void *b)
165 return os_memcmp(a, b, sizeof(macaddr));
169 int hostapd_mac_comp_empty(const void *a)
171 macaddr empty = { 0 };
172 return os_memcmp(a, empty, sizeof(macaddr));
176 static int hostapd_config_read_wpa_psk(const char *fname,
177 struct hostapd_ssid *ssid)
181 int line = 0, ret = 0, len, ok;
183 struct hostapd_wpa_psk *psk;
188 f = fopen(fname, "r");
190 wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
194 while (fgets(buf, sizeof(buf), f)) {
200 while (*pos != '\0') {
210 if (hwaddr_aton(buf, addr)) {
211 wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
212 "line %d in '%s'", buf, line, fname);
217 psk = os_zalloc(sizeof(*psk));
219 wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
223 if (is_zero_ether_addr(addr))
226 os_memcpy(psk->addr, addr, ETH_ALEN);
230 wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
239 len = os_strlen(pos);
240 if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
242 else if (len >= 8 && len < 64) {
243 pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
244 4096, psk->psk, PMK_LEN);
248 wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
249 "'%s'", pos, line, fname);
255 psk->next = ssid->wpa_psk;
265 static int hostapd_derive_psk(struct hostapd_ssid *ssid)
267 ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
268 if (ssid->wpa_psk == NULL) {
269 wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
272 wpa_hexdump_ascii(MSG_DEBUG, "SSID",
273 (u8 *) ssid->ssid, ssid->ssid_len);
274 wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
275 (u8 *) ssid->wpa_passphrase,
276 os_strlen(ssid->wpa_passphrase));
277 pbkdf2_sha1(ssid->wpa_passphrase,
278 ssid->ssid, ssid->ssid_len,
279 4096, ssid->wpa_psk->psk, PMK_LEN);
280 wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
281 ssid->wpa_psk->psk, PMK_LEN);
286 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
288 struct hostapd_ssid *ssid = &conf->ssid;
290 if (ssid->wpa_passphrase != NULL) {
291 if (ssid->wpa_psk != NULL) {
292 wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
293 "instead of passphrase");
295 wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
297 if (hostapd_derive_psk(ssid) < 0)
300 ssid->wpa_psk->group = 1;
303 if (ssid->wpa_psk_file) {
304 if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
313 int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
317 if (a->idx != b->idx || a->default_len != b->default_len)
319 for (i = 0; i < NUM_WEP_KEYS; i++)
320 if (a->len[i] != b->len[i] ||
321 os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
327 static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
332 for (i = 0; i < num_servers; i++) {
333 os_free(servers[i].shared_secret);
339 struct hostapd_radius_attr *
340 hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
342 for (; attr; attr = attr->next) {
343 if (attr->type == type)
350 static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
352 struct hostapd_radius_attr *prev;
357 wpabuf_free(prev->val);
363 static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
365 os_free(user->identity);
366 os_free(user->password);
371 static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
374 for (i = 0; i < NUM_WEP_KEYS; i++) {
375 os_free(keys->key[i]);
381 static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
383 struct hostapd_wpa_psk *psk, *prev;
384 struct hostapd_eap_user *user, *prev_user;
389 psk = conf->ssid.wpa_psk;
396 os_free(conf->ssid.wpa_passphrase);
397 os_free(conf->ssid.wpa_psk_file);
398 hostapd_config_free_wep(&conf->ssid.wep);
399 #ifdef CONFIG_FULL_DYNAMIC_VLAN
400 os_free(conf->ssid.vlan_tagged_interface);
401 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
403 user = conf->eap_user;
407 hostapd_config_free_eap_user(prev_user);
410 os_free(conf->dump_log_name);
411 os_free(conf->eap_req_id_text);
412 os_free(conf->accept_mac);
413 os_free(conf->deny_mac);
414 os_free(conf->nas_identifier);
415 hostapd_config_free_radius(conf->radius->auth_servers,
416 conf->radius->num_auth_servers);
417 hostapd_config_free_radius(conf->radius->acct_servers,
418 conf->radius->num_acct_servers);
419 hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
420 hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
421 os_free(conf->rsn_preauth_interfaces);
422 os_free(conf->ctrl_interface);
423 os_free(conf->ca_cert);
424 os_free(conf->server_cert);
425 os_free(conf->private_key);
426 os_free(conf->private_key_passwd);
427 os_free(conf->dh_file);
428 os_free(conf->pac_opaque_encr_key);
429 os_free(conf->eap_fast_a_id);
430 os_free(conf->eap_fast_a_id_info);
431 os_free(conf->eap_sim_db);
432 os_free(conf->radius_server_clients);
433 os_free(conf->test_socket);
434 os_free(conf->radius);
435 os_free(conf->radius_das_shared_secret);
436 hostapd_config_free_vlan(conf);
437 if (conf->ssid.dyn_vlan_keys) {
438 struct hostapd_ssid *ssid = &conf->ssid;
440 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
441 if (ssid->dyn_vlan_keys[i] == NULL)
443 hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
444 os_free(ssid->dyn_vlan_keys[i]);
446 os_free(ssid->dyn_vlan_keys);
447 ssid->dyn_vlan_keys = NULL;
450 os_free(conf->time_zone);
452 #ifdef CONFIG_IEEE80211R
454 struct ft_remote_r0kh *r0kh, *r0kh_prev;
455 struct ft_remote_r1kh *r1kh, *r1kh_prev;
457 r0kh = conf->r0kh_list;
458 conf->r0kh_list = NULL;
465 r1kh = conf->r1kh_list;
466 conf->r1kh_list = NULL;
473 #endif /* CONFIG_IEEE80211R */
476 os_free(conf->wps_pin_requests);
477 os_free(conf->device_name);
478 os_free(conf->manufacturer);
479 os_free(conf->model_name);
480 os_free(conf->model_number);
481 os_free(conf->serial_number);
482 os_free(conf->config_methods);
483 os_free(conf->ap_pin);
484 os_free(conf->extra_cred);
485 os_free(conf->ap_settings);
486 os_free(conf->upnp_iface);
487 os_free(conf->friendly_name);
488 os_free(conf->manufacturer_url);
489 os_free(conf->model_description);
490 os_free(conf->model_url);
492 #endif /* CONFIG_WPS */
494 os_free(conf->roaming_consortium);
495 os_free(conf->venue_name);
497 #ifdef CONFIG_RADIUS_TEST
498 os_free(conf->dump_msk_file);
499 #endif /* CONFIG_RADIUS_TEST */
504 * hostapd_config_free - Free hostapd configuration
505 * @conf: Configuration data from hostapd_config_read().
507 void hostapd_config_free(struct hostapd_config *conf)
514 for (i = 0; i < conf->num_bss; i++)
515 hostapd_config_free_bss(&conf->bss[i]);
517 os_free(conf->supported_rates);
518 os_free(conf->basic_rates);
525 * hostapd_maclist_found - Find a MAC address from a list
526 * @list: MAC address list
527 * @num_entries: Number of addresses in the list
528 * @addr: Address to search for
529 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
530 * Returns: 1 if address is in the list or 0 if not.
532 * Perform a binary search for given MAC address from a pre-sorted list.
534 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
535 const u8 *addr, int *vlan_id)
537 int start, end, middle, res;
540 end = num_entries - 1;
542 while (start <= end) {
543 middle = (start + end) / 2;
544 res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
547 *vlan_id = list[middle].vlan_id;
560 int hostapd_rate_found(int *list, int rate)
567 for (i = 0; list[i] >= 0; i++)
575 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
577 struct hostapd_vlan *v = vlan;
579 if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
587 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
588 const u8 *addr, const u8 *prev_psk)
590 struct hostapd_wpa_psk *psk;
591 int next_ok = prev_psk == NULL;
593 for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
595 (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
598 if (psk->psk == prev_psk)
606 const struct hostapd_eap_user *
607 hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
608 size_t identity_len, int phase2)
610 struct hostapd_eap_user *user = conf->eap_user;
613 if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
614 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
615 static struct hostapd_eap_user wsc_enrollee;
616 os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
617 wsc_enrollee.methods[0].method = eap_server_get_type(
618 "WSC", &wsc_enrollee.methods[0].vendor);
619 return &wsc_enrollee;
622 if (conf->wps_state && identity_len == WSC_ID_REGISTRAR_LEN &&
623 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
624 static struct hostapd_eap_user wsc_registrar;
625 os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
626 wsc_registrar.methods[0].method = eap_server_get_type(
627 "WSC", &wsc_registrar.methods[0].vendor);
628 wsc_registrar.password = (u8 *) conf->ap_pin;
629 wsc_registrar.password_len = conf->ap_pin ?
630 os_strlen(conf->ap_pin) : 0;
631 return &wsc_registrar;
633 #endif /* CONFIG_WPS */
636 if (!phase2 && user->identity == NULL) {
641 if (user->phase2 == !!phase2 && user->wildcard_prefix &&
642 identity_len >= user->identity_len &&
643 os_memcmp(user->identity, identity, user->identity_len) ==
645 /* Wildcard prefix match */
649 if (user->phase2 == !!phase2 &&
650 user->identity_len == identity_len &&
651 os_memcmp(user->identity, identity, identity_len) == 0)