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 */
91 bss->radius_das_time_window = 300;
95 struct hostapd_config * hostapd_config_defaults(void)
97 #define ecw2cw(ecw) ((1 << (ecw)) - 1)
99 struct hostapd_config *conf;
100 struct hostapd_bss_config *bss;
101 const int aCWmin = 4, aCWmax = 10;
102 const struct hostapd_wmm_ac_params ac_bk =
103 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
104 const struct hostapd_wmm_ac_params ac_be =
105 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
106 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
107 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
108 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
109 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
110 const struct hostapd_tx_queue_params txq_bk =
111 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
112 const struct hostapd_tx_queue_params txq_be =
113 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
114 const struct hostapd_tx_queue_params txq_vi =
115 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
116 const struct hostapd_tx_queue_params txq_vo =
117 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
118 (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
122 conf = os_zalloc(sizeof(*conf));
123 bss = os_zalloc(sizeof(*bss));
124 if (conf == NULL || bss == NULL) {
125 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
126 "configuration data.");
132 bss->radius = os_zalloc(sizeof(*bss->radius));
133 if (bss->radius == NULL) {
139 hostapd_config_defaults_bss(bss);
144 conf->beacon_int = 100;
145 conf->rts_threshold = -1; /* use driver default: 2347 */
146 conf->fragm_threshold = -1; /* user driver default: 2346 */
147 conf->send_probe_response = 1;
149 conf->wmm_ac_params[0] = ac_be;
150 conf->wmm_ac_params[1] = ac_bk;
151 conf->wmm_ac_params[2] = ac_vi;
152 conf->wmm_ac_params[3] = ac_vo;
154 conf->tx_queue[0] = txq_vo;
155 conf->tx_queue[1] = txq_vi;
156 conf->tx_queue[2] = txq_be;
157 conf->tx_queue[3] = txq_bk;
159 conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
165 int hostapd_mac_comp(const void *a, const void *b)
167 return os_memcmp(a, b, sizeof(macaddr));
171 int hostapd_mac_comp_empty(const void *a)
173 macaddr empty = { 0 };
174 return os_memcmp(a, empty, sizeof(macaddr));
178 static int hostapd_config_read_wpa_psk(const char *fname,
179 struct hostapd_ssid *ssid)
183 int line = 0, ret = 0, len, ok;
185 struct hostapd_wpa_psk *psk;
190 f = fopen(fname, "r");
192 wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
196 while (fgets(buf, sizeof(buf), f)) {
202 while (*pos != '\0') {
212 if (hwaddr_aton(buf, addr)) {
213 wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
214 "line %d in '%s'", buf, line, fname);
219 psk = os_zalloc(sizeof(*psk));
221 wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
225 if (is_zero_ether_addr(addr))
228 os_memcpy(psk->addr, addr, ETH_ALEN);
232 wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
241 len = os_strlen(pos);
242 if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
244 else if (len >= 8 && len < 64) {
245 pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
246 4096, psk->psk, PMK_LEN);
250 wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
251 "'%s'", pos, line, fname);
257 psk->next = ssid->wpa_psk;
267 static int hostapd_derive_psk(struct hostapd_ssid *ssid)
269 ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
270 if (ssid->wpa_psk == NULL) {
271 wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
274 wpa_hexdump_ascii(MSG_DEBUG, "SSID",
275 (u8 *) ssid->ssid, ssid->ssid_len);
276 wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
277 (u8 *) ssid->wpa_passphrase,
278 os_strlen(ssid->wpa_passphrase));
279 pbkdf2_sha1(ssid->wpa_passphrase,
280 ssid->ssid, ssid->ssid_len,
281 4096, ssid->wpa_psk->psk, PMK_LEN);
282 wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
283 ssid->wpa_psk->psk, PMK_LEN);
288 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
290 struct hostapd_ssid *ssid = &conf->ssid;
292 if (ssid->wpa_passphrase != NULL) {
293 if (ssid->wpa_psk != NULL) {
294 wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
295 "instead of passphrase");
297 wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
299 if (hostapd_derive_psk(ssid) < 0)
302 ssid->wpa_psk->group = 1;
305 if (ssid->wpa_psk_file) {
306 if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
315 int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b)
319 if (a->idx != b->idx || a->default_len != b->default_len)
321 for (i = 0; i < NUM_WEP_KEYS; i++)
322 if (a->len[i] != b->len[i] ||
323 os_memcmp(a->key[i], b->key[i], a->len[i]) != 0)
329 static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
334 for (i = 0; i < num_servers; i++) {
335 os_free(servers[i].shared_secret);
341 struct hostapd_radius_attr *
342 hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
344 for (; attr; attr = attr->next) {
345 if (attr->type == type)
352 static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
354 struct hostapd_radius_attr *prev;
359 wpabuf_free(prev->val);
365 static void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
367 os_free(user->identity);
368 os_free(user->password);
373 static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
376 for (i = 0; i < NUM_WEP_KEYS; i++) {
377 os_free(keys->key[i]);
383 static void hostapd_config_free_bss(struct hostapd_bss_config *conf)
385 struct hostapd_wpa_psk *psk, *prev;
386 struct hostapd_eap_user *user, *prev_user;
391 psk = conf->ssid.wpa_psk;
398 os_free(conf->ssid.wpa_passphrase);
399 os_free(conf->ssid.wpa_psk_file);
400 hostapd_config_free_wep(&conf->ssid.wep);
401 #ifdef CONFIG_FULL_DYNAMIC_VLAN
402 os_free(conf->ssid.vlan_tagged_interface);
403 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
405 user = conf->eap_user;
409 hostapd_config_free_eap_user(prev_user);
412 os_free(conf->dump_log_name);
413 os_free(conf->eap_req_id_text);
414 os_free(conf->accept_mac);
415 os_free(conf->deny_mac);
416 os_free(conf->nas_identifier);
417 hostapd_config_free_radius(conf->radius->auth_servers,
418 conf->radius->num_auth_servers);
419 hostapd_config_free_radius(conf->radius->acct_servers,
420 conf->radius->num_acct_servers);
421 hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
422 hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
423 os_free(conf->rsn_preauth_interfaces);
424 os_free(conf->ctrl_interface);
425 os_free(conf->ca_cert);
426 os_free(conf->server_cert);
427 os_free(conf->private_key);
428 os_free(conf->private_key_passwd);
429 os_free(conf->dh_file);
430 os_free(conf->pac_opaque_encr_key);
431 os_free(conf->eap_fast_a_id);
432 os_free(conf->eap_fast_a_id_info);
433 os_free(conf->eap_sim_db);
434 os_free(conf->radius_server_clients);
435 os_free(conf->test_socket);
436 os_free(conf->radius);
437 os_free(conf->radius_das_shared_secret);
438 hostapd_config_free_vlan(conf);
439 if (conf->ssid.dyn_vlan_keys) {
440 struct hostapd_ssid *ssid = &conf->ssid;
442 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
443 if (ssid->dyn_vlan_keys[i] == NULL)
445 hostapd_config_free_wep(ssid->dyn_vlan_keys[i]);
446 os_free(ssid->dyn_vlan_keys[i]);
448 os_free(ssid->dyn_vlan_keys);
449 ssid->dyn_vlan_keys = NULL;
452 os_free(conf->time_zone);
454 #ifdef CONFIG_IEEE80211R
456 struct ft_remote_r0kh *r0kh, *r0kh_prev;
457 struct ft_remote_r1kh *r1kh, *r1kh_prev;
459 r0kh = conf->r0kh_list;
460 conf->r0kh_list = NULL;
467 r1kh = conf->r1kh_list;
468 conf->r1kh_list = NULL;
475 #endif /* CONFIG_IEEE80211R */
478 os_free(conf->wps_pin_requests);
479 os_free(conf->device_name);
480 os_free(conf->manufacturer);
481 os_free(conf->model_name);
482 os_free(conf->model_number);
483 os_free(conf->serial_number);
484 os_free(conf->config_methods);
485 os_free(conf->ap_pin);
486 os_free(conf->extra_cred);
487 os_free(conf->ap_settings);
488 os_free(conf->upnp_iface);
489 os_free(conf->friendly_name);
490 os_free(conf->manufacturer_url);
491 os_free(conf->model_description);
492 os_free(conf->model_url);
494 wpabuf_free(conf->wps_nfc_dh_pubkey);
495 wpabuf_free(conf->wps_nfc_dh_privkey);
496 wpabuf_free(conf->wps_nfc_dev_pw);
497 #endif /* CONFIG_WPS */
499 os_free(conf->roaming_consortium);
500 os_free(conf->venue_name);
501 os_free(conf->network_auth_type);
502 os_free(conf->anqp_3gpp_cell_net);
503 os_free(conf->domain_name);
505 #ifdef CONFIG_RADIUS_TEST
506 os_free(conf->dump_msk_file);
507 #endif /* CONFIG_RADIUS_TEST */
510 os_free(conf->hs20_oper_friendly_name);
511 os_free(conf->hs20_wan_metrics);
512 os_free(conf->hs20_connection_capability);
513 os_free(conf->hs20_operating_class);
514 #endif /* CONFIG_HS20 */
519 * hostapd_config_free - Free hostapd configuration
520 * @conf: Configuration data from hostapd_config_read().
522 void hostapd_config_free(struct hostapd_config *conf)
529 for (i = 0; i < conf->num_bss; i++)
530 hostapd_config_free_bss(&conf->bss[i]);
532 os_free(conf->supported_rates);
533 os_free(conf->basic_rates);
540 * hostapd_maclist_found - Find a MAC address from a list
541 * @list: MAC address list
542 * @num_entries: Number of addresses in the list
543 * @addr: Address to search for
544 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
545 * Returns: 1 if address is in the list or 0 if not.
547 * Perform a binary search for given MAC address from a pre-sorted list.
549 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
550 const u8 *addr, int *vlan_id)
552 int start, end, middle, res;
555 end = num_entries - 1;
557 while (start <= end) {
558 middle = (start + end) / 2;
559 res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
562 *vlan_id = list[middle].vlan_id;
575 int hostapd_rate_found(int *list, int rate)
582 for (i = 0; list[i] >= 0; i++)
590 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
592 struct hostapd_vlan *v = vlan;
594 if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
602 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
603 const u8 *addr, const u8 *prev_psk)
605 struct hostapd_wpa_psk *psk;
606 int next_ok = prev_psk == NULL;
608 for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
610 (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0))
613 if (psk->psk == prev_psk)
621 const struct hostapd_eap_user *
622 hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
623 size_t identity_len, int phase2)
625 struct hostapd_eap_user *user = conf->eap_user;
628 if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
629 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
630 static struct hostapd_eap_user wsc_enrollee;
631 os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
632 wsc_enrollee.methods[0].method = eap_server_get_type(
633 "WSC", &wsc_enrollee.methods[0].vendor);
634 return &wsc_enrollee;
637 if (conf->wps_state && identity_len == WSC_ID_REGISTRAR_LEN &&
638 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
639 static struct hostapd_eap_user wsc_registrar;
640 os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
641 wsc_registrar.methods[0].method = eap_server_get_type(
642 "WSC", &wsc_registrar.methods[0].vendor);
643 wsc_registrar.password = (u8 *) conf->ap_pin;
644 wsc_registrar.password_len = conf->ap_pin ?
645 os_strlen(conf->ap_pin) : 0;
646 return &wsc_registrar;
648 #endif /* CONFIG_WPS */
651 if (!phase2 && user->identity == NULL) {
656 if (user->phase2 == !!phase2 && user->wildcard_prefix &&
657 identity_len >= user->identity_len &&
658 os_memcmp(user->identity, identity, user->identity_len) ==
660 /* Wildcard prefix match */
664 if (user->phase2 == !!phase2 &&
665 user->identity_len == identity_len &&
666 os_memcmp(user->identity, identity, identity_len) == 0)