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