1 /* 2 * hostapd / Configuration helper functions 3 * Copyright (c) 2003-2009, Jouni Malinen <j (at) w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #include "utils/includes.h" 16 17 #include "utils/common.h" 18 #include "crypto/sha1.h" 19 #include "radius/radius_client.h" 20 #include "common/ieee802_11_defs.h" 21 #include "common/eapol_common.h" 22 #include "eap_common/eap_wsc_common.h" 23 #include "eap_server/eap.h" 24 #include "wpa_auth.h" 25 #include "sta_info.h" 26 #include "ap_config.h" 27 28 29 static void hostapd_config_free_vlan(struct hostapd_bss_config *bss) 30 { 31 struct hostapd_vlan *vlan, *prev; 32 33 vlan = bss->vlan; 34 prev = NULL; 35 while (vlan) { 36 prev = vlan; 37 vlan = vlan->next; 38 os_free(prev); 39 } 40 41 bss->vlan = NULL; 42 } 43 44 45 void hostapd_config_defaults_bss(struct hostapd_bss_config *bss) 46 { 47 bss->logger_syslog_level = HOSTAPD_LEVEL_INFO; 48 bss->logger_stdout_level = HOSTAPD_LEVEL_INFO; 49 bss->logger_syslog = (unsigned int) -1; 50 bss->logger_stdout = (unsigned int) -1; 51 52 bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED; 53 54 bss->wep_rekeying_period = 300; 55 /* use key0 in individual key and key1 in broadcast key */ 56 bss->broadcast_key_idx_min = 1; 57 bss->broadcast_key_idx_max = 2; 58 bss->eap_reauth_period = 3600; 59 60 bss->wpa_group_rekey = 600; 61 bss->wpa_gmk_rekey = 86400; 62 bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK; 63 bss->wpa_pairwise = WPA_CIPHER_TKIP; 64 bss->wpa_group = WPA_CIPHER_TKIP; 65 bss->rsn_pairwise = 0; 66 67 bss->max_num_sta = MAX_STA_COUNT; 68 69 bss->dtim_period = 2; 70 71 bss->radius_server_auth_port = 1812; 72 bss->ap_max_inactivity = AP_MAX_INACTIVITY; 73 bss->eapol_version = EAPOL_VERSION; 74 75 bss->max_listen_interval = 65535; 76 77 bss->pwd_group = 19; /* ECC: GF(p=256) */ 78 79 #ifdef CONFIG_IEEE80211W 80 bss->assoc_sa_query_max_timeout = 1000; 81 bss->assoc_sa_query_retry_timeout = 201; 82 #endif /* CONFIG_IEEE80211W */ 83 #ifdef EAP_SERVER_FAST 84 /* both anonymous and authenticated provisioning */ 85 bss->eap_fast_prov = 3; 86 bss->pac_key_lifetime = 7 * 24 * 60 * 60; 87 bss->pac_key_refresh_time = 1 * 24 * 60 * 60; 88 #endif /* EAP_SERVER_FAST */ 89 90 /* Set to -1 as defaults depends on HT in setup */ 91 bss->wmm_enabled = -1; 92 93 #ifdef CONFIG_IEEE80211R 94 bss->ft_over_ds = 1; 95 #endif /* CONFIG_IEEE80211R */ 96 } 97 98 99 struct hostapd_config * hostapd_config_defaults(void) 100 { 101 #define ecw2cw(ecw) ((1 << (ecw)) - 1) 102 103 struct hostapd_config *conf; 104 struct hostapd_bss_config *bss; 105 const int aCWmin = 4, aCWmax = 10; 106 const struct hostapd_wmm_ac_params ac_bk = 107 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */ 108 const struct hostapd_wmm_ac_params ac_be = 109 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */ 110 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */ 111 { aCWmin - 1, aCWmin, 2, 3000 / 32, 1 }; 112 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */ 113 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 1 }; 114 const struct hostapd_tx_queue_params txq_bk = 115 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 }; 116 const struct hostapd_tx_queue_params txq_be = 117 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0}; 118 const struct hostapd_tx_queue_params txq_vi = 119 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30}; 120 const struct hostapd_tx_queue_params txq_vo = 121 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1, 122 (ecw2cw(aCWmin) + 1) / 2 - 1, 15}; 123 124 #undef ecw2cw 125 126 conf = os_zalloc(sizeof(*conf)); 127 bss = os_zalloc(sizeof(*bss)); 128 if (conf == NULL || bss == NULL) { 129 wpa_printf(MSG_ERROR, "Failed to allocate memory for " 130 "configuration data."); 131 os_free(conf); 132 os_free(bss); 133 return NULL; 134 } 135 136 bss->radius = os_zalloc(sizeof(*bss->radius)); 137 if (bss->radius == NULL) { 138 os_free(conf); 139 os_free(bss); 140 return NULL; 141 } 142 143 hostapd_config_defaults_bss(bss); 144 145 conf->num_bss = 1; 146 conf->bss = bss; 147 148 conf->beacon_int = 100; 149 conf->rts_threshold = -1; /* use driver default: 2347 */ 150 conf->fragm_threshold = -1; /* user driver default: 2346 */ 151 conf->send_probe_response = 1; 152 153 conf->wmm_ac_params[0] = ac_be; 154 conf->wmm_ac_params[1] = ac_bk; 155 conf->wmm_ac_params[2] = ac_vi; 156 conf->wmm_ac_params[3] = ac_vo; 157 158 conf->tx_queue[0] = txq_vo; 159 conf->tx_queue[1] = txq_vi; 160 conf->tx_queue[2] = txq_be; 161 conf->tx_queue[3] = txq_bk; 162 163 conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED; 164 165 return conf; 166 } 167 168 169 int hostapd_mac_comp(const void *a, const void *b) 170 { 171 return os_memcmp(a, b, sizeof(macaddr)); 172 } 173 174 175 int hostapd_mac_comp_empty(const void *a) 176 { 177 macaddr empty = { 0 }; 178 return os_memcmp(a, empty, sizeof(macaddr)); 179 } 180 181 182 static int hostapd_config_read_wpa_psk(const char *fname, 183 struct hostapd_ssid *ssid) 184 { 185 FILE *f; 186 char buf[128], *pos; 187 int line = 0, ret = 0, len, ok; 188 u8 addr[ETH_ALEN]; 189 struct hostapd_wpa_psk *psk; 190 191 if (!fname) 192 return 0; 193 194 f = fopen(fname, "r"); 195 if (!f) { 196 wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname); 197 return -1; 198 } 199 200 while (fgets(buf, sizeof(buf), f)) { 201 line++; 202 203 if (buf[0] == '#') 204 continue; 205 pos = buf; 206 while (*pos != '\0') { 207 if (*pos == '\n') { 208 *pos = '\0'; 209 break; 210 } 211 pos++; 212 } 213 if (buf[0] == '\0') 214 continue; 215 216 if (hwaddr_aton(buf, addr)) { 217 wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on " 218 "line %d in '%s'", buf, line, fname); 219 ret = -1; 220 break; 221 } 222 223 psk = os_zalloc(sizeof(*psk)); 224 if (psk == NULL) { 225 wpa_printf(MSG_ERROR, "WPA PSK allocation failed"); 226 ret = -1; 227 break; 228 } 229 if (is_zero_ether_addr(addr)) 230 psk->group = 1; 231 else 232 os_memcpy(psk->addr, addr, ETH_ALEN); 233 234 pos = buf + 17; 235 if (*pos == '\0') { 236 wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'", 237 line, fname); 238 os_free(psk); 239 ret = -1; 240 break; 241 } 242 pos++; 243 244 ok = 0; 245 len = os_strlen(pos); 246 if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0) 247 ok = 1; 248 else if (len >= 8 && len < 64) { 249 pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len, 250 4096, psk->psk, PMK_LEN); 251 ok = 1; 252 } 253 if (!ok) { 254 wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in " 255 "'%s'", pos, line, fname); 256 os_free(psk); 257 ret = -1; 258 break; 259 } 260 261 psk->next = ssid->wpa_psk; 262 ssid->wpa_psk = psk; 263 } 264 265 fclose(f); 266 267 return ret; 268 } 269 270 271 static int hostapd_derive_psk(struct hostapd_ssid *ssid) 272 { 273 ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk)); 274 if (ssid->wpa_psk == NULL) { 275 wpa_printf(MSG_ERROR, "Unable to alloc space for PSK"); 276 return -1; 277 } 278 wpa_hexdump_ascii(MSG_DEBUG, "SSID", 279 (u8 *) ssid->ssid, ssid->ssid_len); 280 wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)", 281 (u8 *) ssid->wpa_passphrase, 282 os_strlen(ssid->wpa_passphrase)); 283 pbkdf2_sha1(ssid->wpa_passphrase, 284 ssid->ssid, ssid->ssid_len, 285 4096, ssid->wpa_psk->psk, PMK_LEN); 286 wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)", 287 ssid->wpa_psk->psk, PMK_LEN); 288 return 0; 289 } 290 291 292 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf) 293 { 294 struct hostapd_ssid *ssid = &conf->ssid; 295 296 if (ssid->wpa_passphrase != NULL) { 297 if (ssid->wpa_psk != NULL) { 298 wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK " 299 "instead of passphrase"); 300 } else { 301 wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on " 302 "passphrase"); 303 if (hostapd_derive_psk(ssid) < 0) 304 return -1; 305 } 306 ssid->wpa_psk->group = 1; 307 } 308 309 if (ssid->wpa_psk_file) { 310 if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file, 311 &conf->ssid)) 312 return -1; 313 } 314 315 return 0; 316 } 317 318 319 int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b) 320 { 321 int i; 322 323 if (a->idx != b->idx || a->default_len != b->default_len) 324 return 1; 325 for (i = 0; i < NUM_WEP_KEYS; i++) 326 if (a->len[i] != b->len[i] || 327 os_memcmp(a->key[i], b->key[i], a->len[i]) != 0) 328 return 1; 329 return 0; 330 } 331 332 333 static void hostapd_config_free_radius(struct hostapd_radius_server *servers, 334 int num_servers) 335 { 336 int i; 337 338 for (i = 0; i < num_servers; i++) { 339 os_free(servers[i].shared_secret); 340 } 341 os_free(servers); 342 } 343 344 345 static void hostapd_config_free_eap_user(struct hostapd_eap_user *user) 346 { 347 os_free(user->identity); 348 os_free(user->password); 349 os_free(user); 350 } 351 352 353 static void hostapd_config_free_wep(struct hostapd_wep_keys *keys) 354 { 355 int i; 356 for (i = 0; i < NUM_WEP_KEYS; i++) { 357 os_free(keys->key[i]); 358 keys->key[i] = NULL; 359 } 360 } 361 362 363 static void hostapd_config_free_bss(struct hostapd_bss_config *conf) 364 { 365 struct hostapd_wpa_psk *psk, *prev; 366 struct hostapd_eap_user *user, *prev_user; 367 368 if (conf == NULL) 369 return; 370 371 psk = conf->ssid.wpa_psk; 372 while (psk) { 373 prev = psk; 374 psk = psk->next; 375 os_free(prev); 376 } 377 378 os_free(conf->ssid.wpa_passphrase); 379 os_free(conf->ssid.wpa_psk_file); 380 hostapd_config_free_wep(&conf->ssid.wep); 381 #ifdef CONFIG_FULL_DYNAMIC_VLAN 382 os_free(conf->ssid.vlan_tagged_interface); 383 #endif /* CONFIG_FULL_DYNAMIC_VLAN */ 384 385 user = conf->eap_user; 386 while (user) { 387 prev_user = user; 388 user = user->next; 389 hostapd_config_free_eap_user(prev_user); 390 } 391 392 os_free(conf->dump_log_name); 393 os_free(conf->eap_req_id_text); 394 os_free(conf->accept_mac); 395 os_free(conf->deny_mac); 396 os_free(conf->nas_identifier); 397 hostapd_config_free_radius(conf->radius->auth_servers, 398 conf->radius->num_auth_servers); 399 hostapd_config_free_radius(conf->radius->acct_servers, 400 conf->radius->num_acct_servers); 401 os_free(conf->rsn_preauth_interfaces); 402 os_free(conf->ctrl_interface); 403 os_free(conf->ca_cert); 404 os_free(conf->server_cert); 405 os_free(conf->private_key); 406 os_free(conf->private_key_passwd); 407 os_free(conf->dh_file); 408 os_free(conf->pac_opaque_encr_key); 409 os_free(conf->eap_fast_a_id); 410 os_free(conf->eap_fast_a_id_info); 411 os_free(conf->eap_sim_db); 412 os_free(conf->radius_server_clients); 413 os_free(conf->test_socket); 414 os_free(conf->radius); 415 hostapd_config_free_vlan(conf); 416 if (conf->ssid.dyn_vlan_keys) { 417 struct hostapd_ssid *ssid = &conf->ssid; 418 size_t i; 419 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) { 420 if (ssid->dyn_vlan_keys[i] == NULL) 421 continue; 422 hostapd_config_free_wep(ssid->dyn_vlan_keys[i]); 423 os_free(ssid->dyn_vlan_keys[i]); 424 } 425 os_free(ssid->dyn_vlan_keys); 426 ssid->dyn_vlan_keys = NULL; 427 } 428 429 #ifdef CONFIG_IEEE80211R 430 { 431 struct ft_remote_r0kh *r0kh, *r0kh_prev; 432 struct ft_remote_r1kh *r1kh, *r1kh_prev; 433 434 r0kh = conf->r0kh_list; 435 conf->r0kh_list = NULL; 436 while (r0kh) { 437 r0kh_prev = r0kh; 438 r0kh = r0kh->next; 439 os_free(r0kh_prev); 440 } 441 442 r1kh = conf->r1kh_list; 443 conf->r1kh_list = NULL; 444 while (r1kh) { 445 r1kh_prev = r1kh; 446 r1kh = r1kh->next; 447 os_free(r1kh_prev); 448 } 449 } 450 #endif /* CONFIG_IEEE80211R */ 451 452 #ifdef CONFIG_WPS 453 os_free(conf->wps_pin_requests); 454 os_free(conf->device_name); 455 os_free(conf->manufacturer); 456 os_free(conf->model_name); 457 os_free(conf->model_number); 458 os_free(conf->serial_number); 459 os_free(conf->config_methods); 460 os_free(conf->ap_pin); 461 os_free(conf->extra_cred); 462 os_free(conf->ap_settings); 463 os_free(conf->upnp_iface); 464 os_free(conf->friendly_name); 465 os_free(conf->manufacturer_url); 466 os_free(conf->model_description); 467 os_free(conf->model_url); 468 os_free(conf->upc); 469 #endif /* CONFIG_WPS */ 470 } 471 472 473 /** 474 * hostapd_config_free - Free hostapd configuration 475 * @conf: Configuration data from hostapd_config_read(). 476 */ 477 void hostapd_config_free(struct hostapd_config *conf) 478 { 479 size_t i; 480 481 if (conf == NULL) 482 return; 483 484 for (i = 0; i < conf->num_bss; i++) 485 hostapd_config_free_bss(&conf->bss[i]); 486 os_free(conf->bss); 487 os_free(conf->supported_rates); 488 os_free(conf->basic_rates); 489 490 os_free(conf); 491 } 492 493 494 /** 495 * hostapd_maclist_found - Find a MAC address from a list 496 * @list: MAC address list 497 * @num_entries: Number of addresses in the list 498 * @addr: Address to search for 499 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed 500 * Returns: 1 if address is in the list or 0 if not. 501 * 502 * Perform a binary search for given MAC address from a pre-sorted list. 503 */ 504 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries, 505 const u8 *addr, int *vlan_id) 506 { 507 int start, end, middle, res; 508 509 start = 0; 510 end = num_entries - 1; 511 512 while (start <= end) { 513 middle = (start + end) / 2; 514 res = os_memcmp(list[middle].addr, addr, ETH_ALEN); 515 if (res == 0) { 516 if (vlan_id) 517 *vlan_id = list[middle].vlan_id; 518 return 1; 519 } 520 if (res < 0) 521 start = middle + 1; 522 else 523 end = middle - 1; 524 } 525 526 return 0; 527 } 528 529 530 int hostapd_rate_found(int *list, int rate) 531 { 532 int i; 533 534 if (list == NULL) 535 return 0; 536 537 for (i = 0; list[i] >= 0; i++) 538 if (list[i] == rate) 539 return 1; 540 541 return 0; 542 } 543 544 545 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id) 546 { 547 struct hostapd_vlan *v = vlan; 548 while (v) { 549 if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD) 550 return v->ifname; 551 v = v->next; 552 } 553 return NULL; 554 } 555 556 557 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf, 558 const u8 *addr, const u8 *prev_psk) 559 { 560 struct hostapd_wpa_psk *psk; 561 int next_ok = prev_psk == NULL; 562 563 for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) { 564 if (next_ok && 565 (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0)) 566 return psk->psk; 567 568 if (psk->psk == prev_psk) 569 next_ok = 1; 570 } 571 572 return NULL; 573 } 574 575 576 const struct hostapd_eap_user * 577 hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity, 578 size_t identity_len, int phase2) 579 { 580 struct hostapd_eap_user *user = conf->eap_user; 581 582 #ifdef CONFIG_WPS 583 if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN && 584 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) { 585 static struct hostapd_eap_user wsc_enrollee; 586 os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee)); 587 wsc_enrollee.methods[0].method = eap_server_get_type( 588 "WSC", &wsc_enrollee.methods[0].vendor); 589 return &wsc_enrollee; 590 } 591 592 if (conf->wps_state && identity_len == WSC_ID_REGISTRAR_LEN && 593 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) { 594 static struct hostapd_eap_user wsc_registrar; 595 os_memset(&wsc_registrar, 0, sizeof(wsc_registrar)); 596 wsc_registrar.methods[0].method = eap_server_get_type( 597 "WSC", &wsc_registrar.methods[0].vendor); 598 wsc_registrar.password = (u8 *) conf->ap_pin; 599 wsc_registrar.password_len = conf->ap_pin ? 600 os_strlen(conf->ap_pin) : 0; 601 return &wsc_registrar; 602 } 603 #endif /* CONFIG_WPS */ 604 605 while (user) { 606 if (!phase2 && user->identity == NULL) { 607 /* Wildcard match */ 608 break; 609 } 610 611 if (user->phase2 == !!phase2 && user->wildcard_prefix && 612 identity_len >= user->identity_len && 613 os_memcmp(user->identity, identity, user->identity_len) == 614 0) { 615 /* Wildcard prefix match */ 616 break; 617 } 618 619 if (user->phase2 == !!phase2 && 620 user->identity_len == identity_len && 621 os_memcmp(user->identity, identity, identity_len) == 0) 622 break; 623 user = user->next; 624 } 625 626 return user; 627 } 628