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