hostapd.c revision e0e48dc666fb14a7bb60264ca87463ba7bc1fe0b
1/* 2 * hostapd / Initialization and configuration 3 * Copyright (c) 2002-2013, 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 "utils/eloop.h" 13#include "common/ieee802_11_defs.h" 14#include "common/wpa_ctrl.h" 15#include "radius/radius_client.h" 16#include "radius/radius_das.h" 17#include "drivers/driver.h" 18#include "hostapd.h" 19#include "authsrv.h" 20#include "sta_info.h" 21#include "accounting.h" 22#include "ap_list.h" 23#include "beacon.h" 24#include "iapp.h" 25#include "ieee802_1x.h" 26#include "ieee802_11_auth.h" 27#include "vlan_init.h" 28#include "wpa_auth.h" 29#include "wps_hostapd.h" 30#include "hw_features.h" 31#include "wpa_auth_glue.h" 32#include "ap_drv_ops.h" 33#include "ap_config.h" 34#include "p2p_hostapd.h" 35#include "gas_serv.h" 36#include "dfs.h" 37 38 39static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason); 40static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd); 41static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd); 42static int setup_interface2(struct hostapd_iface *iface); 43static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx); 44 45extern int wpa_debug_level; 46extern struct wpa_driver_ops *wpa_drivers[]; 47 48 49int hostapd_for_each_interface(struct hapd_interfaces *interfaces, 50 int (*cb)(struct hostapd_iface *iface, 51 void *ctx), void *ctx) 52{ 53 size_t i; 54 int ret; 55 56 for (i = 0; i < interfaces->count; i++) { 57 ret = cb(interfaces->iface[i], ctx); 58 if (ret) 59 return ret; 60 } 61 62 return 0; 63} 64 65 66static void hostapd_reload_bss(struct hostapd_data *hapd) 67{ 68 struct hostapd_ssid *ssid; 69 70#ifndef CONFIG_NO_RADIUS 71 radius_client_reconfig(hapd->radius, hapd->conf->radius); 72#endif /* CONFIG_NO_RADIUS */ 73 74 ssid = &hapd->conf->ssid; 75 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next && 76 ssid->wpa_passphrase_set && ssid->wpa_passphrase) { 77 /* 78 * Force PSK to be derived again since SSID or passphrase may 79 * have changed. 80 */ 81 os_free(ssid->wpa_psk); 82 ssid->wpa_psk = NULL; 83 } 84 if (hostapd_setup_wpa_psk(hapd->conf)) { 85 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK " 86 "after reloading configuration"); 87 } 88 89 if (hapd->conf->ieee802_1x || hapd->conf->wpa) 90 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1); 91 else 92 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0); 93 94 if (hapd->conf->wpa && hapd->wpa_auth == NULL) { 95 hostapd_setup_wpa(hapd); 96 if (hapd->wpa_auth) 97 wpa_init_keys(hapd->wpa_auth); 98 } else if (hapd->conf->wpa) { 99 const u8 *wpa_ie; 100 size_t wpa_ie_len; 101 hostapd_reconfig_wpa(hapd); 102 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len); 103 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) 104 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for " 105 "the kernel driver."); 106 } else if (hapd->wpa_auth) { 107 wpa_deinit(hapd->wpa_auth); 108 hapd->wpa_auth = NULL; 109 hostapd_set_privacy(hapd, 0); 110 hostapd_setup_encryption(hapd->conf->iface, hapd); 111 hostapd_set_generic_elem(hapd, (u8 *) "", 0); 112 } 113 114 ieee802_11_set_beacon(hapd); 115 hostapd_update_wps(hapd); 116 117 if (hapd->conf->ssid.ssid_set && 118 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid, 119 hapd->conf->ssid.ssid_len)) { 120 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver"); 121 /* try to continue */ 122 } 123 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface); 124} 125 126 127static void hostapd_clear_old(struct hostapd_iface *iface) 128{ 129 size_t j; 130 131 /* 132 * Deauthenticate all stations since the new configuration may not 133 * allow them to use the BSS anymore. 134 */ 135 for (j = 0; j < iface->num_bss; j++) { 136 hostapd_flush_old_stations(iface->bss[j], 137 WLAN_REASON_PREV_AUTH_NOT_VALID); 138 hostapd_broadcast_wep_clear(iface->bss[j]); 139 140#ifndef CONFIG_NO_RADIUS 141 /* TODO: update dynamic data based on changed configuration 142 * items (e.g., open/close sockets, etc.) */ 143 radius_client_flush(iface->bss[j]->radius, 0); 144#endif /* CONFIG_NO_RADIUS */ 145 } 146} 147 148 149int hostapd_reload_config(struct hostapd_iface *iface) 150{ 151 struct hostapd_data *hapd = iface->bss[0]; 152 struct hostapd_config *newconf, *oldconf; 153 size_t j; 154 155 if (iface->config_fname == NULL) { 156 /* Only in-memory config in use - assume it has been updated */ 157 hostapd_clear_old(iface); 158 for (j = 0; j < iface->num_bss; j++) 159 hostapd_reload_bss(iface->bss[j]); 160 return 0; 161 } 162 163 if (iface->interfaces == NULL || 164 iface->interfaces->config_read_cb == NULL) 165 return -1; 166 newconf = iface->interfaces->config_read_cb(iface->config_fname); 167 if (newconf == NULL) 168 return -1; 169 170 hostapd_clear_old(iface); 171 172 oldconf = hapd->iconf; 173 iface->conf = newconf; 174 175 for (j = 0; j < iface->num_bss; j++) { 176 hapd = iface->bss[j]; 177 hapd->iconf = newconf; 178 hapd->conf = newconf->bss[j]; 179 hostapd_reload_bss(hapd); 180 } 181 182 hostapd_config_free(oldconf); 183 184 185 return 0; 186} 187 188 189static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd, 190 char *ifname) 191{ 192 int i; 193 194 for (i = 0; i < NUM_WEP_KEYS; i++) { 195 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 196 0, NULL, 0, NULL, 0)) { 197 wpa_printf(MSG_DEBUG, "Failed to clear default " 198 "encryption keys (ifname=%s keyidx=%d)", 199 ifname, i); 200 } 201 } 202#ifdef CONFIG_IEEE80211W 203 if (hapd->conf->ieee80211w) { 204 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) { 205 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, 206 NULL, i, 0, NULL, 207 0, NULL, 0)) { 208 wpa_printf(MSG_DEBUG, "Failed to clear " 209 "default mgmt encryption keys " 210 "(ifname=%s keyidx=%d)", ifname, i); 211 } 212 } 213 } 214#endif /* CONFIG_IEEE80211W */ 215} 216 217 218static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd) 219{ 220 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface); 221 return 0; 222} 223 224 225static int hostapd_broadcast_wep_set(struct hostapd_data *hapd) 226{ 227 int errors = 0, idx; 228 struct hostapd_ssid *ssid = &hapd->conf->ssid; 229 230 idx = ssid->wep.idx; 231 if (ssid->wep.default_len && 232 hostapd_drv_set_key(hapd->conf->iface, 233 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 234 1, NULL, 0, ssid->wep.key[idx], 235 ssid->wep.len[idx])) { 236 wpa_printf(MSG_WARNING, "Could not set WEP encryption."); 237 errors++; 238 } 239 240 return errors; 241} 242 243 244static void hostapd_free_hapd_data(struct hostapd_data *hapd) 245{ 246 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface); 247 iapp_deinit(hapd->iapp); 248 hapd->iapp = NULL; 249 accounting_deinit(hapd); 250 hostapd_deinit_wpa(hapd); 251 vlan_deinit(hapd); 252 hostapd_acl_deinit(hapd); 253#ifndef CONFIG_NO_RADIUS 254 radius_client_deinit(hapd->radius); 255 hapd->radius = NULL; 256 radius_das_deinit(hapd->radius_das); 257 hapd->radius_das = NULL; 258#endif /* CONFIG_NO_RADIUS */ 259 260 hostapd_deinit_wps(hapd); 261 262 authsrv_deinit(hapd); 263 264 if (hapd->interface_added && 265 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) { 266 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s", 267 hapd->conf->iface); 268 } 269 270 os_free(hapd->probereq_cb); 271 hapd->probereq_cb = NULL; 272 273#ifdef CONFIG_P2P 274 wpabuf_free(hapd->p2p_beacon_ie); 275 hapd->p2p_beacon_ie = NULL; 276 wpabuf_free(hapd->p2p_probe_resp_ie); 277 hapd->p2p_probe_resp_ie = NULL; 278#endif /* CONFIG_P2P */ 279 280 wpabuf_free(hapd->time_adv); 281 282#ifdef CONFIG_INTERWORKING 283 gas_serv_deinit(hapd); 284#endif /* CONFIG_INTERWORKING */ 285 286#ifdef CONFIG_SQLITE 287 os_free(hapd->tmp_eap_user.identity); 288 os_free(hapd->tmp_eap_user.password); 289#endif /* CONFIG_SQLITE */ 290} 291 292 293/** 294 * hostapd_cleanup - Per-BSS cleanup (deinitialization) 295 * @hapd: Pointer to BSS data 296 * 297 * This function is used to free all per-BSS data structures and resources. 298 * Most of the modules that are initialized in hostapd_setup_bss() are 299 * deinitialized here. 300 */ 301static void hostapd_cleanup(struct hostapd_data *hapd) 302{ 303 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd, 304 hapd->conf->iface); 305 if (hapd->iface->interfaces && 306 hapd->iface->interfaces->ctrl_iface_deinit) 307 hapd->iface->interfaces->ctrl_iface_deinit(hapd); 308 hostapd_free_hapd_data(hapd); 309 hapd->started = 0; 310} 311 312 313static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface) 314{ 315 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface); 316 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features); 317 iface->hw_features = NULL; 318 os_free(iface->current_rates); 319 iface->current_rates = NULL; 320 os_free(iface->basic_rates); 321 iface->basic_rates = NULL; 322 ap_list_deinit(iface); 323} 324 325 326/** 327 * hostapd_cleanup_iface - Complete per-interface cleanup 328 * @iface: Pointer to interface data 329 * 330 * This function is called after per-BSS data structures are deinitialized 331 * with hostapd_cleanup(). 332 */ 333static void hostapd_cleanup_iface(struct hostapd_iface *iface) 334{ 335 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface); 336 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL); 337 338 hostapd_cleanup_iface_partial(iface); 339 hostapd_config_free(iface->conf); 340 iface->conf = NULL; 341 342 os_free(iface->config_fname); 343 os_free(iface->bss); 344 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface); 345 os_free(iface); 346} 347 348 349static void hostapd_clear_wep(struct hostapd_data *hapd) 350{ 351 if (hapd->drv_priv) { 352 hostapd_set_privacy(hapd, 0); 353 hostapd_broadcast_wep_clear(hapd); 354 } 355} 356 357 358static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd) 359{ 360 int i; 361 362 hostapd_broadcast_wep_set(hapd); 363 364 if (hapd->conf->ssid.wep.default_len) { 365 hostapd_set_privacy(hapd, 1); 366 return 0; 367 } 368 369 /* 370 * When IEEE 802.1X is not enabled, the driver may need to know how to 371 * set authentication algorithms for static WEP. 372 */ 373 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs); 374 375 for (i = 0; i < 4; i++) { 376 if (hapd->conf->ssid.wep.key[i] && 377 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 378 i == hapd->conf->ssid.wep.idx, NULL, 0, 379 hapd->conf->ssid.wep.key[i], 380 hapd->conf->ssid.wep.len[i])) { 381 wpa_printf(MSG_WARNING, "Could not set WEP " 382 "encryption."); 383 return -1; 384 } 385 if (hapd->conf->ssid.wep.key[i] && 386 i == hapd->conf->ssid.wep.idx) 387 hostapd_set_privacy(hapd, 1); 388 } 389 390 return 0; 391} 392 393 394static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason) 395{ 396 int ret = 0; 397 u8 addr[ETH_ALEN]; 398 399 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL) 400 return 0; 401 402 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries"); 403 if (hostapd_flush(hapd)) { 404 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to " 405 "kernel driver"); 406 ret = -1; 407 } 408 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations"); 409 os_memset(addr, 0xff, ETH_ALEN); 410 hostapd_drv_sta_deauth(hapd, addr, reason); 411 hostapd_free_stas(hapd); 412 413 return ret; 414} 415 416 417/** 418 * hostapd_validate_bssid_configuration - Validate BSSID configuration 419 * @iface: Pointer to interface data 420 * Returns: 0 on success, -1 on failure 421 * 422 * This function is used to validate that the configured BSSIDs are valid. 423 */ 424static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface) 425{ 426 u8 mask[ETH_ALEN] = { 0 }; 427 struct hostapd_data *hapd = iface->bss[0]; 428 unsigned int i = iface->conf->num_bss, bits = 0, j; 429 int auto_addr = 0; 430 431 if (hostapd_drv_none(hapd)) 432 return 0; 433 434 /* Generate BSSID mask that is large enough to cover the BSSIDs. */ 435 436 /* Determine the bits necessary to cover the number of BSSIDs. */ 437 for (i--; i; i >>= 1) 438 bits++; 439 440 /* Determine the bits necessary to any configured BSSIDs, 441 if they are higher than the number of BSSIDs. */ 442 for (j = 0; j < iface->conf->num_bss; j++) { 443 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) { 444 if (j) 445 auto_addr++; 446 continue; 447 } 448 449 for (i = 0; i < ETH_ALEN; i++) { 450 mask[i] |= 451 iface->conf->bss[j]->bssid[i] ^ 452 hapd->own_addr[i]; 453 } 454 } 455 456 if (!auto_addr) 457 goto skip_mask_ext; 458 459 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++) 460 ; 461 j = 0; 462 if (i < ETH_ALEN) { 463 j = (5 - i) * 8; 464 465 while (mask[i] != 0) { 466 mask[i] >>= 1; 467 j++; 468 } 469 } 470 471 if (bits < j) 472 bits = j; 473 474 if (bits > 40) { 475 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)", 476 bits); 477 return -1; 478 } 479 480 os_memset(mask, 0xff, ETH_ALEN); 481 j = bits / 8; 482 for (i = 5; i > 5 - j; i--) 483 mask[i] = 0; 484 j = bits % 8; 485 while (j--) 486 mask[i] <<= 1; 487 488skip_mask_ext: 489 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)", 490 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits); 491 492 if (!auto_addr) 493 return 0; 494 495 for (i = 0; i < ETH_ALEN; i++) { 496 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) { 497 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR 498 " for start address " MACSTR ".", 499 MAC2STR(mask), MAC2STR(hapd->own_addr)); 500 wpa_printf(MSG_ERROR, "Start address must be the " 501 "first address in the block (i.e., addr " 502 "AND mask == addr)."); 503 return -1; 504 } 505 } 506 507 return 0; 508} 509 510 511static int mac_in_conf(struct hostapd_config *conf, const void *a) 512{ 513 size_t i; 514 515 for (i = 0; i < conf->num_bss; i++) { 516 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) { 517 return 1; 518 } 519 } 520 521 return 0; 522} 523 524 525#ifndef CONFIG_NO_RADIUS 526 527static int hostapd_das_nas_mismatch(struct hostapd_data *hapd, 528 struct radius_das_attrs *attr) 529{ 530 /* TODO */ 531 return 0; 532} 533 534 535static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd, 536 struct radius_das_attrs *attr) 537{ 538 struct sta_info *sta = NULL; 539 char buf[128]; 540 541 if (attr->sta_addr) 542 sta = ap_get_sta(hapd, attr->sta_addr); 543 544 if (sta == NULL && attr->acct_session_id && 545 attr->acct_session_id_len == 17) { 546 for (sta = hapd->sta_list; sta; sta = sta->next) { 547 os_snprintf(buf, sizeof(buf), "%08X-%08X", 548 sta->acct_session_id_hi, 549 sta->acct_session_id_lo); 550 if (os_memcmp(attr->acct_session_id, buf, 17) == 0) 551 break; 552 } 553 } 554 555 if (sta == NULL && attr->cui) { 556 for (sta = hapd->sta_list; sta; sta = sta->next) { 557 struct wpabuf *cui; 558 cui = ieee802_1x_get_radius_cui(sta->eapol_sm); 559 if (cui && wpabuf_len(cui) == attr->cui_len && 560 os_memcmp(wpabuf_head(cui), attr->cui, 561 attr->cui_len) == 0) 562 break; 563 } 564 } 565 566 if (sta == NULL && attr->user_name) { 567 for (sta = hapd->sta_list; sta; sta = sta->next) { 568 u8 *identity; 569 size_t identity_len; 570 identity = ieee802_1x_get_identity(sta->eapol_sm, 571 &identity_len); 572 if (identity && 573 identity_len == attr->user_name_len && 574 os_memcmp(identity, attr->user_name, identity_len) 575 == 0) 576 break; 577 } 578 } 579 580 return sta; 581} 582 583 584static enum radius_das_res 585hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr) 586{ 587 struct hostapd_data *hapd = ctx; 588 struct sta_info *sta; 589 590 if (hostapd_das_nas_mismatch(hapd, attr)) 591 return RADIUS_DAS_NAS_MISMATCH; 592 593 sta = hostapd_das_find_sta(hapd, attr); 594 if (sta == NULL) 595 return RADIUS_DAS_SESSION_NOT_FOUND; 596 597 hostapd_drv_sta_deauth(hapd, sta->addr, 598 WLAN_REASON_PREV_AUTH_NOT_VALID); 599 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID); 600 601 return RADIUS_DAS_SUCCESS; 602} 603 604#endif /* CONFIG_NO_RADIUS */ 605 606 607/** 608 * hostapd_setup_bss - Per-BSS setup (initialization) 609 * @hapd: Pointer to BSS data 610 * @first: Whether this BSS is the first BSS of an interface; -1 = not first, 611 * but interface may exist 612 * 613 * This function is used to initialize all per-BSS data structures and 614 * resources. This gets called in a loop for each BSS when an interface is 615 * initialized. Most of the modules that are initialized here will be 616 * deinitialized in hostapd_cleanup(). 617 */ 618static int hostapd_setup_bss(struct hostapd_data *hapd, int first) 619{ 620 struct hostapd_bss_config *conf = hapd->conf; 621 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1]; 622 int ssid_len, set_ssid; 623 char force_ifname[IFNAMSIZ]; 624 u8 if_addr[ETH_ALEN]; 625 626 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)", 627 __func__, hapd, hapd->conf->iface, first); 628 629 if (hapd->started) { 630 wpa_printf(MSG_ERROR, "%s: Interface %s was already started", 631 __func__, hapd->conf->iface); 632 return -1; 633 } 634 hapd->started = 1; 635 636 if (!first || first == -1) { 637 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) { 638 /* Allocate the next available BSSID. */ 639 do { 640 inc_byte_array(hapd->own_addr, ETH_ALEN); 641 } while (mac_in_conf(hapd->iconf, hapd->own_addr)); 642 } else { 643 /* Allocate the configured BSSID. */ 644 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN); 645 646 if (hostapd_mac_comp(hapd->own_addr, 647 hapd->iface->bss[0]->own_addr) == 648 0) { 649 wpa_printf(MSG_ERROR, "BSS '%s' may not have " 650 "BSSID set to the MAC address of " 651 "the radio", hapd->conf->iface); 652 return -1; 653 } 654 } 655 656 hapd->interface_added = 1; 657 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS, 658 hapd->conf->iface, hapd->own_addr, hapd, 659 &hapd->drv_priv, force_ifname, if_addr, 660 hapd->conf->bridge[0] ? hapd->conf->bridge : 661 NULL, first == -1)) { 662 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID=" 663 MACSTR ")", MAC2STR(hapd->own_addr)); 664 return -1; 665 } 666 } 667 668 if (conf->wmm_enabled < 0) 669 conf->wmm_enabled = hapd->iconf->ieee80211n; 670 671 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID); 672 hostapd_set_privacy(hapd, 0); 673 674 hostapd_broadcast_wep_clear(hapd); 675 if (hostapd_setup_encryption(hapd->conf->iface, hapd)) 676 return -1; 677 678 /* 679 * Fetch the SSID from the system and use it or, 680 * if one was specified in the config file, verify they 681 * match. 682 */ 683 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid)); 684 if (ssid_len < 0) { 685 wpa_printf(MSG_ERROR, "Could not read SSID from system"); 686 return -1; 687 } 688 if (conf->ssid.ssid_set) { 689 /* 690 * If SSID is specified in the config file and it differs 691 * from what is being used then force installation of the 692 * new SSID. 693 */ 694 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len || 695 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0); 696 } else { 697 /* 698 * No SSID in the config file; just use the one we got 699 * from the system. 700 */ 701 set_ssid = 0; 702 conf->ssid.ssid_len = ssid_len; 703 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len); 704 } 705 706 if (!hostapd_drv_none(hapd)) { 707 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR 708 " and ssid \"%s\"", 709 hapd->conf->iface, MAC2STR(hapd->own_addr), 710 wpa_ssid_txt(hapd->conf->ssid.ssid, 711 hapd->conf->ssid.ssid_len)); 712 } 713 714 if (hostapd_setup_wpa_psk(conf)) { 715 wpa_printf(MSG_ERROR, "WPA-PSK setup failed."); 716 return -1; 717 } 718 719 /* Set SSID for the kernel driver (to be used in beacon and probe 720 * response frames) */ 721 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid, 722 conf->ssid.ssid_len)) { 723 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver"); 724 return -1; 725 } 726 727 if (wpa_debug_level == MSG_MSGDUMP) 728 conf->radius->msg_dumps = 1; 729#ifndef CONFIG_NO_RADIUS 730 hapd->radius = radius_client_init(hapd, conf->radius); 731 if (hapd->radius == NULL) { 732 wpa_printf(MSG_ERROR, "RADIUS client initialization failed."); 733 return -1; 734 } 735 736 if (hapd->conf->radius_das_port) { 737 struct radius_das_conf das_conf; 738 os_memset(&das_conf, 0, sizeof(das_conf)); 739 das_conf.port = hapd->conf->radius_das_port; 740 das_conf.shared_secret = hapd->conf->radius_das_shared_secret; 741 das_conf.shared_secret_len = 742 hapd->conf->radius_das_shared_secret_len; 743 das_conf.client_addr = &hapd->conf->radius_das_client_addr; 744 das_conf.time_window = hapd->conf->radius_das_time_window; 745 das_conf.require_event_timestamp = 746 hapd->conf->radius_das_require_event_timestamp; 747 das_conf.ctx = hapd; 748 das_conf.disconnect = hostapd_das_disconnect; 749 hapd->radius_das = radius_das_init(&das_conf); 750 if (hapd->radius_das == NULL) { 751 wpa_printf(MSG_ERROR, "RADIUS DAS initialization " 752 "failed."); 753 return -1; 754 } 755 } 756#endif /* CONFIG_NO_RADIUS */ 757 758 if (hostapd_acl_init(hapd)) { 759 wpa_printf(MSG_ERROR, "ACL initialization failed."); 760 return -1; 761 } 762 if (hostapd_init_wps(hapd, conf)) 763 return -1; 764 765 if (authsrv_init(hapd) < 0) 766 return -1; 767 768 if (ieee802_1x_init(hapd)) { 769 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed."); 770 return -1; 771 } 772 773 if (hapd->conf->wpa && hostapd_setup_wpa(hapd)) 774 return -1; 775 776 if (accounting_init(hapd)) { 777 wpa_printf(MSG_ERROR, "Accounting initialization failed."); 778 return -1; 779 } 780 781 if (hapd->conf->ieee802_11f && 782 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) { 783 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization " 784 "failed."); 785 return -1; 786 } 787 788#ifdef CONFIG_INTERWORKING 789 if (gas_serv_init(hapd)) { 790 wpa_printf(MSG_ERROR, "GAS server initialization failed"); 791 return -1; 792 } 793 794 if (conf->qos_map_set_len && 795 hostapd_drv_set_qos_map(hapd, conf->qos_map_set, 796 conf->qos_map_set_len)) { 797 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map"); 798 return -1; 799 } 800#endif /* CONFIG_INTERWORKING */ 801 802 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) { 803 wpa_printf(MSG_ERROR, "VLAN initialization failed."); 804 return -1; 805 } 806 807 if (!hapd->conf->start_disabled) 808 ieee802_11_set_beacon(hapd); 809 810 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0) 811 return -1; 812 813 if (hapd->driver && hapd->driver->set_operstate) 814 hapd->driver->set_operstate(hapd->drv_priv, 1); 815 816 return 0; 817} 818 819 820static void hostapd_tx_queue_params(struct hostapd_iface *iface) 821{ 822 struct hostapd_data *hapd = iface->bss[0]; 823 int i; 824 struct hostapd_tx_queue_params *p; 825 826 for (i = 0; i < NUM_TX_QUEUES; i++) { 827 p = &iface->conf->tx_queue[i]; 828 829 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin, 830 p->cwmax, p->burst)) { 831 wpa_printf(MSG_DEBUG, "Failed to set TX queue " 832 "parameters for queue %d.", i); 833 /* Continue anyway */ 834 } 835 } 836} 837 838 839static int hostapd_set_acl_list(struct hostapd_data *hapd, 840 struct mac_acl_entry *mac_acl, 841 int n_entries, u8 accept_acl) 842{ 843 struct hostapd_acl_params *acl_params; 844 int i, err; 845 846 acl_params = os_zalloc(sizeof(*acl_params) + 847 (n_entries * sizeof(acl_params->mac_acl[0]))); 848 if (!acl_params) 849 return -ENOMEM; 850 851 for (i = 0; i < n_entries; i++) 852 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr, 853 ETH_ALEN); 854 855 acl_params->acl_policy = accept_acl; 856 acl_params->num_mac_acl = n_entries; 857 858 err = hostapd_drv_set_acl(hapd, acl_params); 859 860 os_free(acl_params); 861 862 return err; 863} 864 865 866static void hostapd_set_acl(struct hostapd_data *hapd) 867{ 868 struct hostapd_config *conf = hapd->iconf; 869 int err; 870 u8 accept_acl; 871 872 if (hapd->iface->drv_max_acl_mac_addrs == 0) 873 return; 874 if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac)) 875 return; 876 877 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) { 878 if (conf->bss[0]->num_accept_mac) { 879 accept_acl = 1; 880 err = hostapd_set_acl_list(hapd, 881 conf->bss[0]->accept_mac, 882 conf->bss[0]->num_accept_mac, 883 accept_acl); 884 if (err) { 885 wpa_printf(MSG_DEBUG, "Failed to set accept acl"); 886 return; 887 } 888 } else { 889 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file"); 890 } 891 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) { 892 if (conf->bss[0]->num_deny_mac) { 893 accept_acl = 0; 894 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac, 895 conf->bss[0]->num_deny_mac, 896 accept_acl); 897 if (err) { 898 wpa_printf(MSG_DEBUG, "Failed to set deny acl"); 899 return; 900 } 901 } else { 902 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file"); 903 } 904 } 905} 906 907 908static int start_ctrl_iface_bss(struct hostapd_data *hapd) 909{ 910 if (!hapd->iface->interfaces || 911 !hapd->iface->interfaces->ctrl_iface_init) 912 return 0; 913 914 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) { 915 wpa_printf(MSG_ERROR, 916 "Failed to setup control interface for %s", 917 hapd->conf->iface); 918 return -1; 919 } 920 921 return 0; 922} 923 924 925static int start_ctrl_iface(struct hostapd_iface *iface) 926{ 927 size_t i; 928 929 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init) 930 return 0; 931 932 for (i = 0; i < iface->num_bss; i++) { 933 struct hostapd_data *hapd = iface->bss[i]; 934 if (iface->interfaces->ctrl_iface_init(hapd)) { 935 wpa_printf(MSG_ERROR, 936 "Failed to setup control interface for %s", 937 hapd->conf->iface); 938 return -1; 939 } 940 } 941 942 return 0; 943} 944 945 946static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx) 947{ 948 struct hostapd_iface *iface = eloop_ctx; 949 950 if (!iface->wait_channel_update) { 951 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it"); 952 return; 953 } 954 955 /* 956 * It is possible that the existing channel list is acceptable, so try 957 * to proceed. 958 */ 959 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway"); 960 setup_interface2(iface); 961} 962 963 964void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator) 965{ 966 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER) 967 return; 968 969 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup"); 970 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL); 971 setup_interface2(iface); 972} 973 974 975static int setup_interface(struct hostapd_iface *iface) 976{ 977 struct hostapd_data *hapd = iface->bss[0]; 978 size_t i; 979 980 if (!iface->phy[0]) { 981 const char *phy = hostapd_drv_get_radio_name(hapd); 982 if (phy) { 983 wpa_printf(MSG_DEBUG, "phy: %s", phy); 984 os_strlcpy(iface->phy, phy, sizeof(iface->phy)); 985 } 986 } 987 988 /* 989 * Make sure that all BSSes get configured with a pointer to the same 990 * driver interface. 991 */ 992 for (i = 1; i < iface->num_bss; i++) { 993 iface->bss[i]->driver = hapd->driver; 994 iface->bss[i]->drv_priv = hapd->drv_priv; 995 } 996 997 if (hostapd_validate_bssid_configuration(iface)) 998 return -1; 999 1000 /* 1001 * Initialize control interfaces early to allow external monitoring of 1002 * channel setup operations that may take considerable amount of time 1003 * especially for DFS cases. 1004 */ 1005 if (start_ctrl_iface(iface)) 1006 return -1; 1007 1008 if (hapd->iconf->country[0] && hapd->iconf->country[1]) { 1009 char country[4], previous_country[4]; 1010 1011 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE); 1012 if (hostapd_get_country(hapd, previous_country) < 0) 1013 previous_country[0] = '\0'; 1014 1015 os_memcpy(country, hapd->iconf->country, 3); 1016 country[3] = '\0'; 1017 if (hostapd_set_country(hapd, country) < 0) { 1018 wpa_printf(MSG_ERROR, "Failed to set country code"); 1019 return -1; 1020 } 1021 1022 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s", 1023 previous_country, country); 1024 1025 if (os_strncmp(previous_country, country, 2) != 0) { 1026 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update"); 1027 iface->wait_channel_update = 1; 1028 eloop_register_timeout(1, 0, 1029 channel_list_update_timeout, 1030 iface, NULL); 1031 return 0; 1032 } 1033 } 1034 1035 return setup_interface2(iface); 1036} 1037 1038 1039static int setup_interface2(struct hostapd_iface *iface) 1040{ 1041 iface->wait_channel_update = 0; 1042 1043 if (hostapd_get_hw_features(iface)) { 1044 /* Not all drivers support this yet, so continue without hw 1045 * feature data. */ 1046 } else { 1047 int ret = hostapd_select_hw_mode(iface); 1048 if (ret < 0) { 1049 wpa_printf(MSG_ERROR, "Could not select hw_mode and " 1050 "channel. (%d)", ret); 1051 return -1; 1052 } 1053 if (ret == 1) { 1054 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)"); 1055 return 0; 1056 } 1057 ret = hostapd_check_ht_capab(iface); 1058 if (ret < 0) 1059 return -1; 1060 if (ret == 1) { 1061 wpa_printf(MSG_DEBUG, "Interface initialization will " 1062 "be completed in a callback"); 1063 return 0; 1064 } 1065 1066 if (iface->conf->ieee80211h) 1067 wpa_printf(MSG_DEBUG, "DFS support is enabled"); 1068 } 1069 return hostapd_setup_interface_complete(iface, 0); 1070} 1071 1072 1073/** 1074 * hostapd_setup_interface_complete - Complete interface setup 1075 * 1076 * This function is called when previous steps in the interface setup has been 1077 * completed. This can also start operations, e.g., DFS, that will require 1078 * additional processing before interface is ready to be enabled. Such 1079 * operations will call this function from eloop callbacks when finished. 1080 */ 1081int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err) 1082{ 1083 struct hostapd_data *hapd = iface->bss[0]; 1084 size_t j; 1085 u8 *prev_addr; 1086 1087 if (err) { 1088 wpa_printf(MSG_ERROR, "Interface initialization failed"); 1089 hostapd_set_state(iface, HAPD_IFACE_DISABLED); 1090 if (iface->interfaces && iface->interfaces->terminate_on_error) 1091 eloop_terminate(); 1092 return -1; 1093 } 1094 1095 wpa_printf(MSG_DEBUG, "Completing interface initialization"); 1096 if (iface->conf->channel) { 1097#ifdef NEED_AP_MLME 1098 int res; 1099#endif /* NEED_AP_MLME */ 1100 1101 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel); 1102 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d " 1103 "Frequency: %d MHz", 1104 hostapd_hw_mode_txt(iface->conf->hw_mode), 1105 iface->conf->channel, iface->freq); 1106 1107#ifdef NEED_AP_MLME 1108 /* Check DFS */ 1109 res = hostapd_handle_dfs(iface); 1110 if (res <= 0) 1111 return res; 1112#endif /* NEED_AP_MLME */ 1113 1114 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq, 1115 hapd->iconf->channel, 1116 hapd->iconf->ieee80211n, 1117 hapd->iconf->ieee80211ac, 1118 hapd->iconf->secondary_channel, 1119 hapd->iconf->vht_oper_chwidth, 1120 hapd->iconf->vht_oper_centr_freq_seg0_idx, 1121 hapd->iconf->vht_oper_centr_freq_seg1_idx)) { 1122 wpa_printf(MSG_ERROR, "Could not set channel for " 1123 "kernel driver"); 1124 return -1; 1125 } 1126 } 1127 1128 if (iface->current_mode) { 1129 if (hostapd_prepare_rates(iface, iface->current_mode)) { 1130 wpa_printf(MSG_ERROR, "Failed to prepare rates " 1131 "table."); 1132 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211, 1133 HOSTAPD_LEVEL_WARNING, 1134 "Failed to prepare rates table."); 1135 return -1; 1136 } 1137 } 1138 1139 if (hapd->iconf->rts_threshold > -1 && 1140 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) { 1141 wpa_printf(MSG_ERROR, "Could not set RTS threshold for " 1142 "kernel driver"); 1143 return -1; 1144 } 1145 1146 if (hapd->iconf->fragm_threshold > -1 && 1147 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) { 1148 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold " 1149 "for kernel driver"); 1150 return -1; 1151 } 1152 1153 prev_addr = hapd->own_addr; 1154 1155 for (j = 0; j < iface->num_bss; j++) { 1156 hapd = iface->bss[j]; 1157 if (j) 1158 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN); 1159 if (hostapd_setup_bss(hapd, j == 0)) 1160 return -1; 1161 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) 1162 prev_addr = hapd->own_addr; 1163 } 1164 hapd = iface->bss[0]; 1165 1166 hostapd_tx_queue_params(iface); 1167 1168 ap_list_init(iface); 1169 1170 hostapd_set_acl(hapd); 1171 1172 if (hostapd_driver_commit(hapd) < 0) { 1173 wpa_printf(MSG_ERROR, "%s: Failed to commit driver " 1174 "configuration", __func__); 1175 return -1; 1176 } 1177 1178 /* 1179 * WPS UPnP module can be initialized only when the "upnp_iface" is up. 1180 * If "interface" and "upnp_iface" are the same (e.g., non-bridge 1181 * mode), the interface is up only after driver_commit, so initialize 1182 * WPS after driver_commit. 1183 */ 1184 for (j = 0; j < iface->num_bss; j++) { 1185 if (hostapd_init_wps_complete(iface->bss[j])) 1186 return -1; 1187 } 1188 1189 hostapd_set_state(iface, HAPD_IFACE_ENABLED); 1190 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED); 1191 if (hapd->setup_complete_cb) 1192 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx); 1193 1194 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.", 1195 iface->bss[0]->conf->iface); 1196 if (iface->interfaces && iface->interfaces->terminate_on_error > 0) 1197 iface->interfaces->terminate_on_error--; 1198 1199 return 0; 1200} 1201 1202 1203/** 1204 * hostapd_setup_interface - Setup of an interface 1205 * @iface: Pointer to interface data. 1206 * Returns: 0 on success, -1 on failure 1207 * 1208 * Initializes the driver interface, validates the configuration, 1209 * and sets driver parameters based on the configuration. 1210 * Flushes old stations, sets the channel, encryption, 1211 * beacons, and WDS links based on the configuration. 1212 * 1213 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS, 1214 * or DFS operations, this function returns 0 before such operations have been 1215 * completed. The pending operations are registered into eloop and will be 1216 * completed from eloop callbacks. Those callbacks end up calling 1217 * hostapd_setup_interface_complete() once setup has been completed. 1218 */ 1219int hostapd_setup_interface(struct hostapd_iface *iface) 1220{ 1221 int ret; 1222 1223 ret = setup_interface(iface); 1224 if (ret) { 1225 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.", 1226 iface->bss[0]->conf->iface); 1227 return -1; 1228 } 1229 1230 return 0; 1231} 1232 1233 1234/** 1235 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data 1236 * @hapd_iface: Pointer to interface data 1237 * @conf: Pointer to per-interface configuration 1238 * @bss: Pointer to per-BSS configuration for this BSS 1239 * Returns: Pointer to allocated BSS data 1240 * 1241 * This function is used to allocate per-BSS data structure. This data will be 1242 * freed after hostapd_cleanup() is called for it during interface 1243 * deinitialization. 1244 */ 1245struct hostapd_data * 1246hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface, 1247 struct hostapd_config *conf, 1248 struct hostapd_bss_config *bss) 1249{ 1250 struct hostapd_data *hapd; 1251 1252 hapd = os_zalloc(sizeof(*hapd)); 1253 if (hapd == NULL) 1254 return NULL; 1255 1256 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta; 1257 hapd->iconf = conf; 1258 hapd->conf = bss; 1259 hapd->iface = hapd_iface; 1260 hapd->driver = hapd->iconf->driver; 1261 hapd->ctrl_sock = -1; 1262 1263 return hapd; 1264} 1265 1266 1267static void hostapd_bss_deinit(struct hostapd_data *hapd) 1268{ 1269 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__, 1270 hapd->conf->iface); 1271 hostapd_free_stas(hapd); 1272 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING); 1273 hostapd_clear_wep(hapd); 1274 hostapd_cleanup(hapd); 1275} 1276 1277 1278void hostapd_interface_deinit(struct hostapd_iface *iface) 1279{ 1280 int j; 1281 1282 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface); 1283 if (iface == NULL) 1284 return; 1285 1286 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL); 1287 iface->wait_channel_update = 0; 1288 1289 for (j = iface->num_bss - 1; j >= 0; j--) 1290 hostapd_bss_deinit(iface->bss[j]); 1291} 1292 1293 1294void hostapd_interface_free(struct hostapd_iface *iface) 1295{ 1296 size_t j; 1297 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface); 1298 for (j = 0; j < iface->num_bss; j++) { 1299 wpa_printf(MSG_DEBUG, "%s: free hapd %p", 1300 __func__, iface->bss[j]); 1301 os_free(iface->bss[j]); 1302 } 1303 hostapd_cleanup_iface(iface); 1304} 1305 1306 1307/** 1308 * hostapd_init - Allocate and initialize per-interface data 1309 * @config_file: Path to the configuration file 1310 * Returns: Pointer to the allocated interface data or %NULL on failure 1311 * 1312 * This function is used to allocate main data structures for per-interface 1313 * data. The allocated data buffer will be freed by calling 1314 * hostapd_cleanup_iface(). 1315 */ 1316struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces, 1317 const char *config_file) 1318{ 1319 struct hostapd_iface *hapd_iface = NULL; 1320 struct hostapd_config *conf = NULL; 1321 struct hostapd_data *hapd; 1322 size_t i; 1323 1324 hapd_iface = os_zalloc(sizeof(*hapd_iface)); 1325 if (hapd_iface == NULL) 1326 goto fail; 1327 1328 hapd_iface->config_fname = os_strdup(config_file); 1329 if (hapd_iface->config_fname == NULL) 1330 goto fail; 1331 1332 conf = interfaces->config_read_cb(hapd_iface->config_fname); 1333 if (conf == NULL) 1334 goto fail; 1335 hapd_iface->conf = conf; 1336 1337 hapd_iface->num_bss = conf->num_bss; 1338 hapd_iface->bss = os_calloc(conf->num_bss, 1339 sizeof(struct hostapd_data *)); 1340 if (hapd_iface->bss == NULL) 1341 goto fail; 1342 1343 for (i = 0; i < conf->num_bss; i++) { 1344 hapd = hapd_iface->bss[i] = 1345 hostapd_alloc_bss_data(hapd_iface, conf, 1346 conf->bss[i]); 1347 if (hapd == NULL) 1348 goto fail; 1349 hapd->msg_ctx = hapd; 1350 } 1351 1352 return hapd_iface; 1353 1354fail: 1355 wpa_printf(MSG_ERROR, "Failed to set up interface with %s", 1356 config_file); 1357 if (conf) 1358 hostapd_config_free(conf); 1359 if (hapd_iface) { 1360 os_free(hapd_iface->config_fname); 1361 os_free(hapd_iface->bss); 1362 wpa_printf(MSG_DEBUG, "%s: free iface %p", 1363 __func__, hapd_iface); 1364 os_free(hapd_iface); 1365 } 1366 return NULL; 1367} 1368 1369 1370static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname) 1371{ 1372 size_t i, j; 1373 1374 for (i = 0; i < interfaces->count; i++) { 1375 struct hostapd_iface *iface = interfaces->iface[i]; 1376 for (j = 0; j < iface->num_bss; j++) { 1377 struct hostapd_data *hapd = iface->bss[j]; 1378 if (os_strcmp(ifname, hapd->conf->iface) == 0) 1379 return 1; 1380 } 1381 } 1382 1383 return 0; 1384} 1385 1386 1387/** 1388 * hostapd_interface_init_bss - Read configuration file and init BSS data 1389 * 1390 * This function is used to parse configuration file for a BSS. This BSS is 1391 * added to an existing interface sharing the same radio (if any) or a new 1392 * interface is created if this is the first interface on a radio. This 1393 * allocate memory for the BSS. No actual driver operations are started. 1394 * 1395 * This is similar to hostapd_interface_init(), but for a case where the 1396 * configuration is used to add a single BSS instead of all BSSes for a radio. 1397 */ 1398struct hostapd_iface * 1399hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy, 1400 const char *config_fname, int debug) 1401{ 1402 struct hostapd_iface *new_iface = NULL, *iface = NULL; 1403 struct hostapd_data *hapd; 1404 int k; 1405 size_t i, bss_idx; 1406 1407 if (!phy || !*phy) 1408 return NULL; 1409 1410 for (i = 0; i < interfaces->count; i++) { 1411 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) { 1412 iface = interfaces->iface[i]; 1413 break; 1414 } 1415 } 1416 1417 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s", 1418 config_fname, phy, iface ? "" : " --> new PHY"); 1419 if (iface) { 1420 struct hostapd_config *conf; 1421 struct hostapd_bss_config **tmp_conf; 1422 struct hostapd_data **tmp_bss; 1423 struct hostapd_bss_config *bss; 1424 const char *ifname; 1425 1426 /* Add new BSS to existing iface */ 1427 conf = interfaces->config_read_cb(config_fname); 1428 if (conf == NULL) 1429 return NULL; 1430 if (conf->num_bss > 1) { 1431 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config"); 1432 hostapd_config_free(conf); 1433 return NULL; 1434 } 1435 1436 ifname = conf->bss[0]->iface; 1437 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) { 1438 wpa_printf(MSG_ERROR, 1439 "Interface name %s already in use", ifname); 1440 hostapd_config_free(conf); 1441 return NULL; 1442 } 1443 1444 tmp_conf = os_realloc_array( 1445 iface->conf->bss, iface->conf->num_bss + 1, 1446 sizeof(struct hostapd_bss_config *)); 1447 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1, 1448 sizeof(struct hostapd_data *)); 1449 if (tmp_bss) 1450 iface->bss = tmp_bss; 1451 if (tmp_conf) { 1452 iface->conf->bss = tmp_conf; 1453 iface->conf->last_bss = tmp_conf[0]; 1454 } 1455 if (tmp_bss == NULL || tmp_conf == NULL) { 1456 hostapd_config_free(conf); 1457 return NULL; 1458 } 1459 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0]; 1460 iface->conf->num_bss++; 1461 1462 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss); 1463 if (hapd == NULL) { 1464 iface->conf->num_bss--; 1465 hostapd_config_free(conf); 1466 return NULL; 1467 } 1468 iface->conf->last_bss = bss; 1469 iface->bss[iface->num_bss] = hapd; 1470 hapd->msg_ctx = hapd; 1471 1472 bss_idx = iface->num_bss++; 1473 conf->num_bss--; 1474 conf->bss[0] = NULL; 1475 hostapd_config_free(conf); 1476 } else { 1477 /* Add a new iface with the first BSS */ 1478 new_iface = iface = hostapd_init(interfaces, config_fname); 1479 if (!iface) 1480 return NULL; 1481 os_strlcpy(iface->phy, phy, sizeof(iface->phy)); 1482 iface->interfaces = interfaces; 1483 bss_idx = 0; 1484 } 1485 1486 for (k = 0; k < debug; k++) { 1487 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0) 1488 iface->bss[bss_idx]->conf->logger_stdout_level--; 1489 } 1490 1491 if (iface->conf->bss[bss_idx]->iface[0] == '\0' && 1492 !hostapd_drv_none(iface->bss[bss_idx])) { 1493 wpa_printf(MSG_ERROR, "Interface name not specified in %s", 1494 config_fname); 1495 if (new_iface) 1496 hostapd_interface_deinit_free(new_iface); 1497 return NULL; 1498 } 1499 1500 return iface; 1501} 1502 1503 1504void hostapd_interface_deinit_free(struct hostapd_iface *iface) 1505{ 1506 const struct wpa_driver_ops *driver; 1507 void *drv_priv; 1508 1509 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface); 1510 if (iface == NULL) 1511 return; 1512 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u", 1513 __func__, (unsigned int) iface->num_bss, 1514 (unsigned int) iface->conf->num_bss); 1515 driver = iface->bss[0]->driver; 1516 drv_priv = iface->bss[0]->drv_priv; 1517 hostapd_interface_deinit(iface); 1518 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit", 1519 __func__, driver, drv_priv); 1520 if (driver && driver->hapd_deinit && drv_priv) 1521 driver->hapd_deinit(drv_priv); 1522 hostapd_interface_free(iface); 1523} 1524 1525 1526int hostapd_enable_iface(struct hostapd_iface *hapd_iface) 1527{ 1528 if (hapd_iface->bss[0]->drv_priv != NULL) { 1529 wpa_printf(MSG_ERROR, "Interface %s already enabled", 1530 hapd_iface->conf->bss[0]->iface); 1531 return -1; 1532 } 1533 1534 wpa_printf(MSG_DEBUG, "Enable interface %s", 1535 hapd_iface->conf->bss[0]->iface); 1536 1537 if (hapd_iface->interfaces == NULL || 1538 hapd_iface->interfaces->driver_init == NULL || 1539 hapd_iface->interfaces->driver_init(hapd_iface)) 1540 return -1; 1541 1542 if (hostapd_setup_interface(hapd_iface)) { 1543 const struct wpa_driver_ops *driver; 1544 void *drv_priv; 1545 1546 driver = hapd_iface->bss[0]->driver; 1547 drv_priv = hapd_iface->bss[0]->drv_priv; 1548 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit", 1549 __func__, driver, drv_priv); 1550 if (driver && driver->hapd_deinit && drv_priv) { 1551 driver->hapd_deinit(drv_priv); 1552 hapd_iface->bss[0]->drv_priv = NULL; 1553 } 1554 return -1; 1555 } 1556 1557 return 0; 1558} 1559 1560 1561int hostapd_reload_iface(struct hostapd_iface *hapd_iface) 1562{ 1563 size_t j; 1564 1565 wpa_printf(MSG_DEBUG, "Reload interface %s", 1566 hapd_iface->conf->bss[0]->iface); 1567 for (j = 0; j < hapd_iface->num_bss; j++) 1568 hostapd_set_security_params(hapd_iface->conf->bss[j]); 1569 if (hostapd_config_check(hapd_iface->conf) < 0) { 1570 wpa_printf(MSG_ERROR, "Updated configuration is invalid"); 1571 return -1; 1572 } 1573 hostapd_clear_old(hapd_iface); 1574 for (j = 0; j < hapd_iface->num_bss; j++) 1575 hostapd_reload_bss(hapd_iface->bss[j]); 1576 1577 return 0; 1578} 1579 1580 1581int hostapd_disable_iface(struct hostapd_iface *hapd_iface) 1582{ 1583 size_t j; 1584 const struct wpa_driver_ops *driver; 1585 void *drv_priv; 1586 1587 if (hapd_iface == NULL) 1588 return -1; 1589 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED); 1590 driver = hapd_iface->bss[0]->driver; 1591 drv_priv = hapd_iface->bss[0]->drv_priv; 1592 1593 /* whatever hostapd_interface_deinit does */ 1594 for (j = 0; j < hapd_iface->num_bss; j++) { 1595 struct hostapd_data *hapd = hapd_iface->bss[j]; 1596 hostapd_free_stas(hapd); 1597 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING); 1598 hostapd_clear_wep(hapd); 1599 hostapd_free_hapd_data(hapd); 1600 } 1601 1602 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit", 1603 __func__, driver, drv_priv); 1604 if (driver && driver->hapd_deinit && drv_priv) { 1605 driver->hapd_deinit(drv_priv); 1606 hapd_iface->bss[0]->drv_priv = NULL; 1607 } 1608 1609 /* From hostapd_cleanup_iface: These were initialized in 1610 * hostapd_setup_interface and hostapd_setup_interface_complete 1611 */ 1612 hostapd_cleanup_iface_partial(hapd_iface); 1613 1614 wpa_printf(MSG_DEBUG, "Interface %s disabled", 1615 hapd_iface->bss[0]->conf->iface); 1616 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED); 1617 return 0; 1618} 1619 1620 1621static struct hostapd_iface * 1622hostapd_iface_alloc(struct hapd_interfaces *interfaces) 1623{ 1624 struct hostapd_iface **iface, *hapd_iface; 1625 1626 iface = os_realloc_array(interfaces->iface, interfaces->count + 1, 1627 sizeof(struct hostapd_iface *)); 1628 if (iface == NULL) 1629 return NULL; 1630 interfaces->iface = iface; 1631 hapd_iface = interfaces->iface[interfaces->count] = 1632 os_zalloc(sizeof(*hapd_iface)); 1633 if (hapd_iface == NULL) { 1634 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for " 1635 "the interface", __func__); 1636 return NULL; 1637 } 1638 interfaces->count++; 1639 hapd_iface->interfaces = interfaces; 1640 1641 return hapd_iface; 1642} 1643 1644 1645static struct hostapd_config * 1646hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname, 1647 const char *ctrl_iface) 1648{ 1649 struct hostapd_bss_config *bss; 1650 struct hostapd_config *conf; 1651 1652 /* Allocates memory for bss and conf */ 1653 conf = hostapd_config_defaults(); 1654 if (conf == NULL) { 1655 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for " 1656 "configuration", __func__); 1657 return NULL; 1658 } 1659 1660 conf->driver = wpa_drivers[0]; 1661 if (conf->driver == NULL) { 1662 wpa_printf(MSG_ERROR, "No driver wrappers registered!"); 1663 hostapd_config_free(conf); 1664 return NULL; 1665 } 1666 1667 bss = conf->last_bss = conf->bss[0]; 1668 1669 os_strlcpy(bss->iface, ifname, sizeof(bss->iface)); 1670 bss->ctrl_interface = os_strdup(ctrl_iface); 1671 if (bss->ctrl_interface == NULL) { 1672 hostapd_config_free(conf); 1673 return NULL; 1674 } 1675 1676 /* Reading configuration file skipped, will be done in SET! 1677 * From reading the configuration till the end has to be done in 1678 * SET 1679 */ 1680 return conf; 1681} 1682 1683 1684static struct hostapd_iface * hostapd_data_alloc( 1685 struct hapd_interfaces *interfaces, struct hostapd_config *conf) 1686{ 1687 size_t i; 1688 struct hostapd_iface *hapd_iface = 1689 interfaces->iface[interfaces->count - 1]; 1690 struct hostapd_data *hapd; 1691 1692 hapd_iface->conf = conf; 1693 hapd_iface->num_bss = conf->num_bss; 1694 1695 hapd_iface->bss = os_zalloc(conf->num_bss * 1696 sizeof(struct hostapd_data *)); 1697 if (hapd_iface->bss == NULL) 1698 return NULL; 1699 1700 for (i = 0; i < conf->num_bss; i++) { 1701 hapd = hapd_iface->bss[i] = 1702 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]); 1703 if (hapd == NULL) 1704 return NULL; 1705 hapd->msg_ctx = hapd; 1706 } 1707 1708 hapd_iface->interfaces = interfaces; 1709 1710 return hapd_iface; 1711} 1712 1713 1714int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf) 1715{ 1716 struct hostapd_config *conf = NULL; 1717 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL; 1718 struct hostapd_data *hapd; 1719 char *ptr; 1720 size_t i, j; 1721 const char *conf_file = NULL, *phy_name = NULL; 1722 1723 if (os_strncmp(buf, "bss_config=", 11) == 0) { 1724 char *pos; 1725 phy_name = buf + 11; 1726 pos = os_strchr(phy_name, ':'); 1727 if (!pos) 1728 return -1; 1729 *pos++ = '\0'; 1730 conf_file = pos; 1731 if (!os_strlen(conf_file)) 1732 return -1; 1733 1734 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name, 1735 conf_file, 0); 1736 if (!hapd_iface) 1737 return -1; 1738 for (j = 0; j < interfaces->count; j++) { 1739 if (interfaces->iface[j] == hapd_iface) 1740 break; 1741 } 1742 if (j == interfaces->count) { 1743 struct hostapd_iface **tmp; 1744 tmp = os_realloc_array(interfaces->iface, 1745 interfaces->count + 1, 1746 sizeof(struct hostapd_iface *)); 1747 if (!tmp) { 1748 hostapd_interface_deinit_free(hapd_iface); 1749 return -1; 1750 } 1751 interfaces->iface = tmp; 1752 interfaces->iface[interfaces->count++] = hapd_iface; 1753 new_iface = hapd_iface; 1754 } 1755 1756 if (new_iface) { 1757 if (interfaces->driver_init(hapd_iface) || 1758 hostapd_setup_interface(hapd_iface)) { 1759 interfaces->count--; 1760 goto fail; 1761 } 1762 } else { 1763 /* Assign new BSS with bss[0]'s driver info */ 1764 hapd = hapd_iface->bss[hapd_iface->num_bss - 1]; 1765 hapd->driver = hapd_iface->bss[0]->driver; 1766 hapd->drv_priv = hapd_iface->bss[0]->drv_priv; 1767 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr, 1768 ETH_ALEN); 1769 1770 if (start_ctrl_iface_bss(hapd) < 0 || 1771 (hapd_iface->state == HAPD_IFACE_ENABLED && 1772 hostapd_setup_bss(hapd, -1))) { 1773 hapd_iface->conf->num_bss--; 1774 hapd_iface->num_bss--; 1775 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s", 1776 __func__, hapd, hapd->conf->iface); 1777 os_free(hapd); 1778 return -1; 1779 } 1780 } 1781 return 0; 1782 } 1783 1784 ptr = os_strchr(buf, ' '); 1785 if (ptr == NULL) 1786 return -1; 1787 *ptr++ = '\0'; 1788 1789 if (os_strncmp(ptr, "config=", 7) == 0) 1790 conf_file = ptr + 7; 1791 1792 for (i = 0; i < interfaces->count; i++) { 1793 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface, 1794 buf)) { 1795 wpa_printf(MSG_INFO, "Cannot add interface - it " 1796 "already exists"); 1797 return -1; 1798 } 1799 } 1800 1801 hapd_iface = hostapd_iface_alloc(interfaces); 1802 if (hapd_iface == NULL) { 1803 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory " 1804 "for interface", __func__); 1805 goto fail; 1806 } 1807 1808 if (conf_file && interfaces->config_read_cb) { 1809 conf = interfaces->config_read_cb(conf_file); 1810 if (conf && conf->bss) 1811 os_strlcpy(conf->bss[0]->iface, buf, 1812 sizeof(conf->bss[0]->iface)); 1813 } else 1814 conf = hostapd_config_alloc(interfaces, buf, ptr); 1815 if (conf == NULL || conf->bss == NULL) { 1816 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory " 1817 "for configuration", __func__); 1818 goto fail; 1819 } 1820 1821 hapd_iface = hostapd_data_alloc(interfaces, conf); 1822 if (hapd_iface == NULL) { 1823 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory " 1824 "for hostapd", __func__); 1825 goto fail; 1826 } 1827 1828 if (start_ctrl_iface(hapd_iface) < 0) 1829 goto fail; 1830 1831 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface); 1832 1833 return 0; 1834 1835fail: 1836 if (conf) 1837 hostapd_config_free(conf); 1838 if (hapd_iface) { 1839 if (hapd_iface->bss) { 1840 for (i = 0; i < hapd_iface->num_bss; i++) { 1841 hapd = hapd_iface->bss[i]; 1842 if (hapd && hapd_iface->interfaces && 1843 hapd_iface->interfaces->ctrl_iface_deinit) 1844 hapd_iface->interfaces-> 1845 ctrl_iface_deinit(hapd); 1846 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)", 1847 __func__, hapd_iface->bss[i], 1848 hapd_iface->bss[i]->conf->iface); 1849 os_free(hapd_iface->bss[i]); 1850 } 1851 os_free(hapd_iface->bss); 1852 } 1853 wpa_printf(MSG_DEBUG, "%s: free iface %p", 1854 __func__, hapd_iface); 1855 os_free(hapd_iface); 1856 } 1857 return -1; 1858} 1859 1860 1861static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx) 1862{ 1863 size_t i; 1864 1865 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface); 1866 1867 /* Remove hostapd_data only if it has already been initialized */ 1868 if (idx < iface->num_bss) { 1869 struct hostapd_data *hapd = iface->bss[idx]; 1870 1871 hostapd_bss_deinit(hapd); 1872 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)", 1873 __func__, hapd, hapd->conf->iface); 1874 hostapd_config_free_bss(hapd->conf); 1875 os_free(hapd); 1876 1877 iface->num_bss--; 1878 1879 for (i = idx; i < iface->num_bss; i++) 1880 iface->bss[i] = iface->bss[i + 1]; 1881 } else { 1882 hostapd_config_free_bss(iface->conf->bss[idx]); 1883 iface->conf->bss[idx] = NULL; 1884 } 1885 1886 iface->conf->num_bss--; 1887 for (i = idx; i < iface->conf->num_bss; i++) 1888 iface->conf->bss[i] = iface->conf->bss[i + 1]; 1889 1890 return 0; 1891} 1892 1893 1894int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf) 1895{ 1896 struct hostapd_iface *hapd_iface; 1897 size_t i, j, k = 0; 1898 1899 for (i = 0; i < interfaces->count; i++) { 1900 hapd_iface = interfaces->iface[i]; 1901 if (hapd_iface == NULL) 1902 return -1; 1903 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) { 1904 wpa_printf(MSG_INFO, "Remove interface '%s'", buf); 1905 hostapd_interface_deinit_free(hapd_iface); 1906 k = i; 1907 while (k < (interfaces->count - 1)) { 1908 interfaces->iface[k] = 1909 interfaces->iface[k + 1]; 1910 k++; 1911 } 1912 interfaces->count--; 1913 return 0; 1914 } 1915 1916 for (j = 0; j < hapd_iface->conf->num_bss; j++) { 1917 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) 1918 return hostapd_remove_bss(hapd_iface, j); 1919 } 1920 } 1921 return -1; 1922} 1923 1924 1925/** 1926 * hostapd_new_assoc_sta - Notify that a new station associated with the AP 1927 * @hapd: Pointer to BSS data 1928 * @sta: Pointer to the associated STA data 1929 * @reassoc: 1 to indicate this was a re-association; 0 = first association 1930 * 1931 * This function will be called whenever a station associates with the AP. It 1932 * can be called from ieee802_11.c for drivers that export MLME to hostapd and 1933 * from drv_callbacks.c based on driver events for drivers that take care of 1934 * management frames (IEEE 802.11 authentication and association) internally. 1935 */ 1936void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta, 1937 int reassoc) 1938{ 1939 if (hapd->tkip_countermeasures) { 1940 hostapd_drv_sta_deauth(hapd, sta->addr, 1941 WLAN_REASON_MICHAEL_MIC_FAILURE); 1942 return; 1943 } 1944 1945 hostapd_prune_associations(hapd, sta->addr); 1946 1947 /* IEEE 802.11F (IAPP) */ 1948 if (hapd->conf->ieee802_11f) 1949 iapp_new_station(hapd->iapp, sta); 1950 1951#ifdef CONFIG_P2P 1952 if (sta->p2p_ie == NULL && !sta->no_p2p_set) { 1953 sta->no_p2p_set = 1; 1954 hapd->num_sta_no_p2p++; 1955 if (hapd->num_sta_no_p2p == 1) 1956 hostapd_p2p_non_p2p_sta_connected(hapd); 1957 } 1958#endif /* CONFIG_P2P */ 1959 1960 /* Start accounting here, if IEEE 802.1X and WPA are not used. 1961 * IEEE 802.1X/WPA code will start accounting after the station has 1962 * been authorized. */ 1963 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) { 1964 os_get_time(&sta->connected_time); 1965 accounting_sta_start(hapd, sta); 1966 } 1967 1968 /* Start IEEE 802.1X authentication process for new stations */ 1969 ieee802_1x_new_station(hapd, sta); 1970 if (reassoc) { 1971 if (sta->auth_alg != WLAN_AUTH_FT && 1972 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) 1973 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH); 1974 } else 1975 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm); 1976 1977 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout " 1978 "for " MACSTR " (%d seconds - ap_max_inactivity)", 1979 __func__, MAC2STR(sta->addr), 1980 hapd->conf->ap_max_inactivity); 1981 eloop_cancel_timeout(ap_handle_timer, hapd, sta); 1982 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0, 1983 ap_handle_timer, hapd, sta); 1984} 1985 1986 1987const char * hostapd_state_text(enum hostapd_iface_state s) 1988{ 1989 switch (s) { 1990 case HAPD_IFACE_UNINITIALIZED: 1991 return "UNINITIALIZED"; 1992 case HAPD_IFACE_DISABLED: 1993 return "DISABLED"; 1994 case HAPD_IFACE_COUNTRY_UPDATE: 1995 return "COUNTRY_UPDATE"; 1996 case HAPD_IFACE_ACS: 1997 return "ACS"; 1998 case HAPD_IFACE_HT_SCAN: 1999 return "HT_SCAN"; 2000 case HAPD_IFACE_DFS: 2001 return "DFS"; 2002 case HAPD_IFACE_ENABLED: 2003 return "ENABLED"; 2004 } 2005 2006 return "UNKNOWN"; 2007} 2008 2009 2010void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s) 2011{ 2012 wpa_printf(MSG_INFO, "%s: interface state %s->%s", 2013 iface->conf->bss[0]->iface, hostapd_state_text(iface->state), 2014 hostapd_state_text(s)); 2015 iface->state = s; 2016} 2017 2018 2019#ifdef NEED_AP_MLME 2020 2021static void free_beacon_data(struct beacon_data *beacon) 2022{ 2023 os_free(beacon->head); 2024 beacon->head = NULL; 2025 os_free(beacon->tail); 2026 beacon->tail = NULL; 2027 os_free(beacon->probe_resp); 2028 beacon->probe_resp = NULL; 2029 os_free(beacon->beacon_ies); 2030 beacon->beacon_ies = NULL; 2031 os_free(beacon->proberesp_ies); 2032 beacon->proberesp_ies = NULL; 2033 os_free(beacon->assocresp_ies); 2034 beacon->assocresp_ies = NULL; 2035} 2036 2037 2038static int hostapd_build_beacon_data(struct hostapd_iface *iface, 2039 struct beacon_data *beacon) 2040{ 2041 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra; 2042 struct wpa_driver_ap_params params; 2043 int ret; 2044 struct hostapd_data *hapd = iface->bss[0]; 2045 2046 ret = ieee802_11_build_ap_params(hapd, ¶ms); 2047 if (ret < 0) 2048 return ret; 2049 2050 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra, 2051 &proberesp_extra, 2052 &assocresp_extra); 2053 if (ret) 2054 goto free_ap_params; 2055 2056 ret = -1; 2057 beacon->head = os_malloc(params.head_len); 2058 if (!beacon->head) 2059 goto free_ap_extra_ies; 2060 2061 os_memcpy(beacon->head, params.head, params.head_len); 2062 beacon->head_len = params.head_len; 2063 2064 beacon->tail = os_malloc(params.tail_len); 2065 if (!beacon->tail) 2066 goto free_beacon; 2067 2068 os_memcpy(beacon->tail, params.tail, params.tail_len); 2069 beacon->tail_len = params.tail_len; 2070 2071 if (params.proberesp != NULL) { 2072 beacon->probe_resp = os_malloc(params.proberesp_len); 2073 if (!beacon->probe_resp) 2074 goto free_beacon; 2075 2076 os_memcpy(beacon->probe_resp, params.proberesp, 2077 params.proberesp_len); 2078 beacon->probe_resp_len = params.proberesp_len; 2079 } 2080 2081 /* copy the extra ies */ 2082 if (beacon_extra) { 2083 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra)); 2084 if (!beacon->beacon_ies) 2085 goto free_beacon; 2086 2087 os_memcpy(beacon->beacon_ies, 2088 beacon_extra->buf, wpabuf_len(beacon_extra)); 2089 beacon->beacon_ies_len = wpabuf_len(beacon_extra); 2090 } 2091 2092 if (proberesp_extra) { 2093 beacon->proberesp_ies = 2094 os_malloc(wpabuf_len(proberesp_extra)); 2095 if (!beacon->proberesp_ies) 2096 goto free_beacon; 2097 2098 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf, 2099 wpabuf_len(proberesp_extra)); 2100 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra); 2101 } 2102 2103 if (assocresp_extra) { 2104 beacon->assocresp_ies = 2105 os_malloc(wpabuf_len(assocresp_extra)); 2106 if (!beacon->assocresp_ies) 2107 goto free_beacon; 2108 2109 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf, 2110 wpabuf_len(assocresp_extra)); 2111 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra); 2112 } 2113 2114 ret = 0; 2115free_beacon: 2116 /* if the function fails, the caller should not free beacon data */ 2117 if (ret) 2118 free_beacon_data(beacon); 2119 2120free_ap_extra_ies: 2121 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra, 2122 assocresp_extra); 2123free_ap_params: 2124 ieee802_11_free_ap_params(¶ms); 2125 return ret; 2126} 2127 2128 2129/* 2130 * TODO: This flow currently supports only changing frequency within the 2131 * same hw_mode. Any other changes to MAC parameters or provided settings (even 2132 * width) are not supported. 2133 */ 2134static int hostapd_change_config_freq(struct hostapd_data *hapd, 2135 struct hostapd_config *conf, 2136 struct hostapd_freq_params *params, 2137 struct hostapd_freq_params *old_params) 2138{ 2139 int channel; 2140 2141 if (!params->channel) { 2142 /* check if the new channel is supported by hw */ 2143 channel = hostapd_hw_get_channel(hapd, params->freq); 2144 if (!channel) 2145 return -1; 2146 } else { 2147 channel = params->channel; 2148 } 2149 2150 /* if a pointer to old_params is provided we save previous state */ 2151 if (old_params) { 2152 old_params->channel = conf->channel; 2153 old_params->ht_enabled = conf->ieee80211n; 2154 old_params->sec_channel_offset = conf->secondary_channel; 2155 } 2156 2157 conf->channel = channel; 2158 conf->ieee80211n = params->ht_enabled; 2159 conf->secondary_channel = params->sec_channel_offset; 2160 2161 /* TODO: maybe call here hostapd_config_check here? */ 2162 2163 return 0; 2164} 2165 2166 2167static int hostapd_fill_csa_settings(struct hostapd_iface *iface, 2168 struct csa_settings *settings) 2169{ 2170 struct hostapd_freq_params old_freq; 2171 int ret; 2172 2173 os_memset(&old_freq, 0, sizeof(old_freq)); 2174 if (!iface || !iface->freq || iface->csa_in_progress) 2175 return -1; 2176 2177 ret = hostapd_change_config_freq(iface->bss[0], iface->conf, 2178 &settings->freq_params, 2179 &old_freq); 2180 if (ret) 2181 return ret; 2182 2183 ret = hostapd_build_beacon_data(iface, &settings->beacon_after); 2184 2185 /* change back the configuration */ 2186 hostapd_change_config_freq(iface->bss[0], iface->conf, 2187 &old_freq, NULL); 2188 2189 if (ret) 2190 return ret; 2191 2192 /* set channel switch parameters for csa ie */ 2193 iface->cs_freq = settings->freq_params.freq; 2194 iface->cs_count = settings->cs_count; 2195 iface->cs_block_tx = settings->block_tx; 2196 2197 ret = hostapd_build_beacon_data(iface, &settings->beacon_csa); 2198 if (ret) { 2199 free_beacon_data(&settings->beacon_after); 2200 return ret; 2201 } 2202 2203 settings->counter_offset_beacon = iface->cs_c_off_beacon; 2204 settings->counter_offset_presp = iface->cs_c_off_proberesp; 2205 2206 return 0; 2207} 2208 2209 2210void hostapd_cleanup_cs_params(struct hostapd_data *hapd) 2211{ 2212 hapd->iface->cs_freq = 0; 2213 hapd->iface->cs_count = 0; 2214 hapd->iface->cs_block_tx = 0; 2215 hapd->iface->cs_c_off_beacon = 0; 2216 hapd->iface->cs_c_off_proberesp = 0; 2217 hapd->iface->csa_in_progress = 0; 2218} 2219 2220 2221int hostapd_switch_channel(struct hostapd_data *hapd, 2222 struct csa_settings *settings) 2223{ 2224 int ret; 2225 ret = hostapd_fill_csa_settings(hapd->iface, settings); 2226 if (ret) 2227 return ret; 2228 2229 ret = hostapd_drv_switch_channel(hapd, settings); 2230 free_beacon_data(&settings->beacon_csa); 2231 free_beacon_data(&settings->beacon_after); 2232 2233 if (ret) { 2234 /* if we failed, clean cs parameters */ 2235 hostapd_cleanup_cs_params(hapd); 2236 return ret; 2237 } 2238 2239 hapd->iface->csa_in_progress = 1; 2240 return 0; 2241} 2242 2243#endif /* NEED_AP_MLME */ 2244