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