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