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