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 "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 23 static 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 39 void 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; 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 */ 83 84 /* Set to -1 as defaults depends on HT in setup */ 85 bss->wmm_enabled = -1; 86 87 #ifdef CONFIG_IEEE80211R 88 bss->ft_over_ds = 1; 89 #endif /* CONFIG_IEEE80211R */ 90 91 bss->radius_das_time_window = 300; 92 } 93 94 95 struct hostapd_config * hostapd_config_defaults(void) 96 { 97 #define ecw2cw(ecw) ((1 << (ecw)) - 1) 98 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}; 119 120 #undef ecw2cw 121 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."); 127 os_free(conf); 128 os_free(bss); 129 return NULL; 130 } 131 132 bss->radius = os_zalloc(sizeof(*bss->radius)); 133 if (bss->radius == NULL) { 134 os_free(conf); 135 os_free(bss); 136 return NULL; 137 } 138 139 hostapd_config_defaults_bss(bss); 140 141 conf->num_bss = 1; 142 conf->bss = bss; 143 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; 148 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; 153 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; 158 159 conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED; 160 161 conf->ap_table_max_size = 255; 162 conf->ap_table_expiration_time = 60; 163 164 return conf; 165 } 166 167 168 int hostapd_mac_comp(const void *a, const void *b) 169 { 170 return os_memcmp(a, b, sizeof(macaddr)); 171 } 172 173 174 int hostapd_mac_comp_empty(const void *a) 175 { 176 macaddr empty = { 0 }; 177 return os_memcmp(a, empty, sizeof(macaddr)); 178 } 179 180 181 static int hostapd_config_read_wpa_psk(const char *fname, 182 struct hostapd_ssid *ssid) 183 { 184 FILE *f; 185 char buf[128], *pos; 186 int line = 0, ret = 0, len, ok; 187 u8 addr[ETH_ALEN]; 188 struct hostapd_wpa_psk *psk; 189 190 if (!fname) 191 return 0; 192 193 f = fopen(fname, "r"); 194 if (!f) { 195 wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname); 196 return -1; 197 } 198 199 while (fgets(buf, sizeof(buf), f)) { 200 line++; 201 202 if (buf[0] == '#') 203 continue; 204 pos = buf; 205 while (*pos != '\0') { 206 if (*pos == '\n') { 207 *pos = '\0'; 208 break; 209 } 210 pos++; 211 } 212 if (buf[0] == '\0') 213 continue; 214 215 if (hwaddr_aton(buf, addr)) { 216 wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on " 217 "line %d in '%s'", buf, line, fname); 218 ret = -1; 219 break; 220 } 221 222 psk = os_zalloc(sizeof(*psk)); 223 if (psk == NULL) { 224 wpa_printf(MSG_ERROR, "WPA PSK allocation failed"); 225 ret = -1; 226 break; 227 } 228 if (is_zero_ether_addr(addr)) 229 psk->group = 1; 230 else 231 os_memcpy(psk->addr, addr, ETH_ALEN); 232 233 pos = buf + 17; 234 if (*pos == '\0') { 235 wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'", 236 line, fname); 237 os_free(psk); 238 ret = -1; 239 break; 240 } 241 pos++; 242 243 ok = 0; 244 len = os_strlen(pos); 245 if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0) 246 ok = 1; 247 else if (len >= 8 && len < 64) { 248 pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len, 249 4096, psk->psk, PMK_LEN); 250 ok = 1; 251 } 252 if (!ok) { 253 wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in " 254 "'%s'", pos, line, fname); 255 os_free(psk); 256 ret = -1; 257 break; 258 } 259 260 psk->next = ssid->wpa_psk; 261 ssid->wpa_psk = psk; 262 } 263 264 fclose(f); 265 266 return ret; 267 } 268 269 270 static int hostapd_derive_psk(struct hostapd_ssid *ssid) 271 { 272 ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk)); 273 if (ssid->wpa_psk == NULL) { 274 wpa_printf(MSG_ERROR, "Unable to alloc space for PSK"); 275 return -1; 276 } 277 wpa_hexdump_ascii(MSG_DEBUG, "SSID", 278 (u8 *) ssid->ssid, ssid->ssid_len); 279 wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)", 280 (u8 *) ssid->wpa_passphrase, 281 os_strlen(ssid->wpa_passphrase)); 282 pbkdf2_sha1(ssid->wpa_passphrase, 283 ssid->ssid, ssid->ssid_len, 284 4096, ssid->wpa_psk->psk, PMK_LEN); 285 wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)", 286 ssid->wpa_psk->psk, PMK_LEN); 287 return 0; 288 } 289 290 291 int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf) 292 { 293 struct hostapd_ssid *ssid = &conf->ssid; 294 295 if (ssid->wpa_passphrase != NULL) { 296 if (ssid->wpa_psk != NULL) { 297 wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK " 298 "instead of passphrase"); 299 } else { 300 wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on " 301 "passphrase"); 302 if (hostapd_derive_psk(ssid) < 0) 303 return -1; 304 } 305 ssid->wpa_psk->group = 1; 306 } 307 308 if (ssid->wpa_psk_file) { 309 if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file, 310 &conf->ssid)) 311 return -1; 312 } 313 314 return 0; 315 } 316 317 318 int hostapd_wep_key_cmp(struct hostapd_wep_keys *a, struct hostapd_wep_keys *b) 319 { 320 int i; 321 322 if (a->idx != b->idx || a->default_len != b->default_len) 323 return 1; 324 for (i = 0; i < NUM_WEP_KEYS; i++) 325 if (a->len[i] != b->len[i] || 326 os_memcmp(a->key[i], b->key[i], a->len[i]) != 0) 327 return 1; 328 return 0; 329 } 330 331 332 static void hostapd_config_free_radius(struct hostapd_radius_server *servers, 333 int num_servers) 334 { 335 int i; 336 337 for (i = 0; i < num_servers; i++) { 338 os_free(servers[i].shared_secret); 339 } 340 os_free(servers); 341 } 342 343 344 struct hostapd_radius_attr * 345 hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type) 346 { 347 for (; attr; attr = attr->next) { 348 if (attr->type == type) 349 return attr; 350 } 351 return NULL; 352 } 353 354 355 static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr) 356 { 357 struct hostapd_radius_attr *prev; 358 359 while (attr) { 360 prev = attr; 361 attr = attr->next; 362 wpabuf_free(prev->val); 363 os_free(prev); 364 } 365 } 366 367 368 static void hostapd_config_free_eap_user(struct hostapd_eap_user *user) 369 { 370 os_free(user->identity); 371 os_free(user->password); 372 os_free(user); 373 } 374 375 376 static void hostapd_config_free_wep(struct hostapd_wep_keys *keys) 377 { 378 int i; 379 for (i = 0; i < NUM_WEP_KEYS; i++) { 380 os_free(keys->key[i]); 381 keys->key[i] = NULL; 382 } 383 } 384 385 386 static void hostapd_config_free_bss(struct hostapd_bss_config *conf) 387 { 388 struct hostapd_wpa_psk *psk, *prev; 389 struct hostapd_eap_user *user, *prev_user; 390 391 if (conf == NULL) 392 return; 393 394 psk = conf->ssid.wpa_psk; 395 while (psk) { 396 prev = psk; 397 psk = psk->next; 398 os_free(prev); 399 } 400 401 os_free(conf->ssid.wpa_passphrase); 402 os_free(conf->ssid.wpa_psk_file); 403 hostapd_config_free_wep(&conf->ssid.wep); 404 #ifdef CONFIG_FULL_DYNAMIC_VLAN 405 os_free(conf->ssid.vlan_tagged_interface); 406 #endif /* CONFIG_FULL_DYNAMIC_VLAN */ 407 408 user = conf->eap_user; 409 while (user) { 410 prev_user = user; 411 user = user->next; 412 hostapd_config_free_eap_user(prev_user); 413 } 414 os_free(conf->eap_user_sqlite); 415 416 os_free(conf->dump_log_name); 417 os_free(conf->eap_req_id_text); 418 os_free(conf->accept_mac); 419 os_free(conf->deny_mac); 420 os_free(conf->nas_identifier); 421 hostapd_config_free_radius(conf->radius->auth_servers, 422 conf->radius->num_auth_servers); 423 hostapd_config_free_radius(conf->radius->acct_servers, 424 conf->radius->num_acct_servers); 425 hostapd_config_free_radius_attr(conf->radius_auth_req_attr); 426 hostapd_config_free_radius_attr(conf->radius_acct_req_attr); 427 os_free(conf->rsn_preauth_interfaces); 428 os_free(conf->ctrl_interface); 429 os_free(conf->ca_cert); 430 os_free(conf->server_cert); 431 os_free(conf->private_key); 432 os_free(conf->private_key_passwd); 433 os_free(conf->dh_file); 434 os_free(conf->pac_opaque_encr_key); 435 os_free(conf->eap_fast_a_id); 436 os_free(conf->eap_fast_a_id_info); 437 os_free(conf->eap_sim_db); 438 os_free(conf->radius_server_clients); 439 os_free(conf->test_socket); 440 os_free(conf->radius); 441 os_free(conf->radius_das_shared_secret); 442 hostapd_config_free_vlan(conf); 443 if (conf->ssid.dyn_vlan_keys) { 444 struct hostapd_ssid *ssid = &conf->ssid; 445 size_t i; 446 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) { 447 if (ssid->dyn_vlan_keys[i] == NULL) 448 continue; 449 hostapd_config_free_wep(ssid->dyn_vlan_keys[i]); 450 os_free(ssid->dyn_vlan_keys[i]); 451 } 452 os_free(ssid->dyn_vlan_keys); 453 ssid->dyn_vlan_keys = NULL; 454 } 455 456 os_free(conf->time_zone); 457 458 #ifdef CONFIG_IEEE80211R 459 { 460 struct ft_remote_r0kh *r0kh, *r0kh_prev; 461 struct ft_remote_r1kh *r1kh, *r1kh_prev; 462 463 r0kh = conf->r0kh_list; 464 conf->r0kh_list = NULL; 465 while (r0kh) { 466 r0kh_prev = r0kh; 467 r0kh = r0kh->next; 468 os_free(r0kh_prev); 469 } 470 471 r1kh = conf->r1kh_list; 472 conf->r1kh_list = NULL; 473 while (r1kh) { 474 r1kh_prev = r1kh; 475 r1kh = r1kh->next; 476 os_free(r1kh_prev); 477 } 478 } 479 #endif /* CONFIG_IEEE80211R */ 480 481 #ifdef CONFIG_WPS 482 os_free(conf->wps_pin_requests); 483 os_free(conf->device_name); 484 os_free(conf->manufacturer); 485 os_free(conf->model_name); 486 os_free(conf->model_number); 487 os_free(conf->serial_number); 488 os_free(conf->config_methods); 489 os_free(conf->ap_pin); 490 os_free(conf->extra_cred); 491 os_free(conf->ap_settings); 492 os_free(conf->upnp_iface); 493 os_free(conf->friendly_name); 494 os_free(conf->manufacturer_url); 495 os_free(conf->model_description); 496 os_free(conf->model_url); 497 os_free(conf->upc); 498 wpabuf_free(conf->wps_nfc_dh_pubkey); 499 wpabuf_free(conf->wps_nfc_dh_privkey); 500 wpabuf_free(conf->wps_nfc_dev_pw); 501 #endif /* CONFIG_WPS */ 502 503 os_free(conf->roaming_consortium); 504 os_free(conf->venue_name); 505 os_free(conf->nai_realm_data); 506 os_free(conf->network_auth_type); 507 os_free(conf->anqp_3gpp_cell_net); 508 os_free(conf->domain_name); 509 510 #ifdef CONFIG_RADIUS_TEST 511 os_free(conf->dump_msk_file); 512 #endif /* CONFIG_RADIUS_TEST */ 513 514 #ifdef CONFIG_HS20 515 os_free(conf->hs20_oper_friendly_name); 516 os_free(conf->hs20_wan_metrics); 517 os_free(conf->hs20_connection_capability); 518 os_free(conf->hs20_operating_class); 519 #endif /* CONFIG_HS20 */ 520 521 wpabuf_free(conf->vendor_elements); 522 } 523 524 525 /** 526 * hostapd_config_free - Free hostapd configuration 527 * @conf: Configuration data from hostapd_config_read(). 528 */ 529 void hostapd_config_free(struct hostapd_config *conf) 530 { 531 size_t i; 532 533 if (conf == NULL) 534 return; 535 536 for (i = 0; i < conf->num_bss; i++) 537 hostapd_config_free_bss(&conf->bss[i]); 538 os_free(conf->bss); 539 os_free(conf->supported_rates); 540 os_free(conf->basic_rates); 541 542 os_free(conf); 543 } 544 545 546 /** 547 * hostapd_maclist_found - Find a MAC address from a list 548 * @list: MAC address list 549 * @num_entries: Number of addresses in the list 550 * @addr: Address to search for 551 * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed 552 * Returns: 1 if address is in the list or 0 if not. 553 * 554 * Perform a binary search for given MAC address from a pre-sorted list. 555 */ 556 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries, 557 const u8 *addr, int *vlan_id) 558 { 559 int start, end, middle, res; 560 561 start = 0; 562 end = num_entries - 1; 563 564 while (start <= end) { 565 middle = (start + end) / 2; 566 res = os_memcmp(list[middle].addr, addr, ETH_ALEN); 567 if (res == 0) { 568 if (vlan_id) 569 *vlan_id = list[middle].vlan_id; 570 return 1; 571 } 572 if (res < 0) 573 start = middle + 1; 574 else 575 end = middle - 1; 576 } 577 578 return 0; 579 } 580 581 582 int hostapd_rate_found(int *list, int rate) 583 { 584 int i; 585 586 if (list == NULL) 587 return 0; 588 589 for (i = 0; list[i] >= 0; i++) 590 if (list[i] == rate) 591 return 1; 592 593 return 0; 594 } 595 596 597 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id) 598 { 599 struct hostapd_vlan *v = vlan; 600 while (v) { 601 if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD) 602 return v->ifname; 603 v = v->next; 604 } 605 return NULL; 606 } 607 608 609 const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf, 610 const u8 *addr, const u8 *prev_psk) 611 { 612 struct hostapd_wpa_psk *psk; 613 int next_ok = prev_psk == NULL; 614 615 for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) { 616 if (next_ok && 617 (psk->group || os_memcmp(psk->addr, addr, ETH_ALEN) == 0)) 618 return psk->psk; 619 620 if (psk->psk == prev_psk) 621 next_ok = 1; 622 } 623 624 return NULL; 625 } 626