1 /* 2 * hostapd / Initialization and configuration 3 * Copyright (c) 2002-2019, 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 #ifdef CONFIG_SQLITE 11 #include <sqlite3.h> 12 #endif /* CONFIG_SQLITE */ 13 14 #include "utils/common.h" 15 #include "utils/eloop.h" 16 #include "common/ieee802_11_defs.h" 17 #include "common/wpa_ctrl.h" 18 #include "common/hw_features_common.h" 19 #include "radius/radius_client.h" 20 #include "radius/radius_das.h" 21 #include "eap_server/tncs.h" 22 #include "eapol_auth/eapol_auth_sm.h" 23 #include "eapol_auth/eapol_auth_sm_i.h" 24 #include "fst/fst.h" 25 #include "hostapd.h" 26 #include "authsrv.h" 27 #include "sta_info.h" 28 #include "accounting.h" 29 #include "ap_list.h" 30 #include "beacon.h" 31 #include "iapp.h" 32 #include "ieee802_1x.h" 33 #include "ieee802_11_auth.h" 34 #include "vlan_init.h" 35 #include "wpa_auth.h" 36 #include "wps_hostapd.h" 37 #include "dpp_hostapd.h" 38 #include "gas_query_ap.h" 39 #include "hw_features.h" 40 #include "wpa_auth_glue.h" 41 #include "ap_drv_ops.h" 42 #include "ap_config.h" 43 #include "p2p_hostapd.h" 44 #include "gas_serv.h" 45 #include "dfs.h" 46 #include "ieee802_11.h" 47 #include "bss_load.h" 48 #include "x_snoop.h" 49 #include "dhcp_snoop.h" 50 #include "ndisc_snoop.h" 51 #include "neighbor_db.h" 52 #include "rrm.h" 53 #include "fils_hlp.h" 54 #include "acs.h" 55 #include "hs20.h" 56 #include "airtime_policy.h" 57 #include "wpa_auth_kay.h" 58 59 60 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason); 61 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd); 62 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd); 63 static int setup_interface2(struct hostapd_iface *iface); 64 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx); 65 static void hostapd_interface_setup_failure_handler(void *eloop_ctx, 66 void *timeout_ctx); 67 68 69 int hostapd_for_each_interface(struct hapd_interfaces *interfaces, 70 int (*cb)(struct hostapd_iface *iface, 71 void *ctx), void *ctx) 72 { 73 size_t i; 74 int ret; 75 76 for (i = 0; i < interfaces->count; i++) { 77 ret = cb(interfaces->iface[i], ctx); 78 if (ret) 79 return ret; 80 } 81 82 return 0; 83 } 84 85 86 void hostapd_reconfig_encryption(struct hostapd_data *hapd) 87 { 88 if (hapd->wpa_auth) 89 return; 90 91 hostapd_set_privacy(hapd, 0); 92 hostapd_setup_encryption(hapd->conf->iface, hapd); 93 } 94 95 96 static void hostapd_reload_bss(struct hostapd_data *hapd) 97 { 98 struct hostapd_ssid *ssid; 99 100 if (!hapd->started) 101 return; 102 103 if (hapd->conf->wmm_enabled < 0) 104 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n; 105 106 #ifndef CONFIG_NO_RADIUS 107 radius_client_reconfig(hapd->radius, hapd->conf->radius); 108 #endif /* CONFIG_NO_RADIUS */ 109 110 ssid = &hapd->conf->ssid; 111 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next && 112 ssid->wpa_passphrase_set && ssid->wpa_passphrase) { 113 /* 114 * Force PSK to be derived again since SSID or passphrase may 115 * have changed. 116 */ 117 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk); 118 } 119 if (hostapd_setup_wpa_psk(hapd->conf)) { 120 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK " 121 "after reloading configuration"); 122 } 123 124 if (hapd->conf->ieee802_1x || hapd->conf->wpa) 125 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1); 126 else 127 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0); 128 129 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) { 130 hostapd_setup_wpa(hapd); 131 if (hapd->wpa_auth) 132 wpa_init_keys(hapd->wpa_auth); 133 } else if (hapd->conf->wpa) { 134 const u8 *wpa_ie; 135 size_t wpa_ie_len; 136 hostapd_reconfig_wpa(hapd); 137 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len); 138 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) 139 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for " 140 "the kernel driver."); 141 } else if (hapd->wpa_auth) { 142 wpa_deinit(hapd->wpa_auth); 143 hapd->wpa_auth = NULL; 144 hostapd_set_privacy(hapd, 0); 145 hostapd_setup_encryption(hapd->conf->iface, hapd); 146 hostapd_set_generic_elem(hapd, (u8 *) "", 0); 147 } 148 149 ieee802_11_set_beacon(hapd); 150 hostapd_update_wps(hapd); 151 152 if (hapd->conf->ssid.ssid_set && 153 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid, 154 hapd->conf->ssid.ssid_len)) { 155 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver"); 156 /* try to continue */ 157 } 158 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface); 159 } 160 161 162 static void hostapd_clear_old(struct hostapd_iface *iface) 163 { 164 size_t j; 165 166 /* 167 * Deauthenticate all stations since the new configuration may not 168 * allow them to use the BSS anymore. 169 */ 170 for (j = 0; j < iface->num_bss; j++) { 171 hostapd_flush_old_stations(iface->bss[j], 172 WLAN_REASON_PREV_AUTH_NOT_VALID); 173 hostapd_broadcast_wep_clear(iface->bss[j]); 174 175 #ifndef CONFIG_NO_RADIUS 176 /* TODO: update dynamic data based on changed configuration 177 * items (e.g., open/close sockets, etc.) */ 178 radius_client_flush(iface->bss[j]->radius, 0); 179 #endif /* CONFIG_NO_RADIUS */ 180 } 181 } 182 183 184 static int hostapd_iface_conf_changed(struct hostapd_config *newconf, 185 struct hostapd_config *oldconf) 186 { 187 size_t i; 188 189 if (newconf->num_bss != oldconf->num_bss) 190 return 1; 191 192 for (i = 0; i < newconf->num_bss; i++) { 193 if (os_strcmp(newconf->bss[i]->iface, 194 oldconf->bss[i]->iface) != 0) 195 return 1; 196 } 197 198 return 0; 199 } 200 201 202 int hostapd_reload_config(struct hostapd_iface *iface) 203 { 204 struct hapd_interfaces *interfaces = iface->interfaces; 205 struct hostapd_data *hapd = iface->bss[0]; 206 struct hostapd_config *newconf, *oldconf; 207 size_t j; 208 209 if (iface->config_fname == NULL) { 210 /* Only in-memory config in use - assume it has been updated */ 211 hostapd_clear_old(iface); 212 for (j = 0; j < iface->num_bss; j++) 213 hostapd_reload_bss(iface->bss[j]); 214 return 0; 215 } 216 217 if (iface->interfaces == NULL || 218 iface->interfaces->config_read_cb == NULL) 219 return -1; 220 newconf = iface->interfaces->config_read_cb(iface->config_fname); 221 if (newconf == NULL) 222 return -1; 223 224 hostapd_clear_old(iface); 225 226 oldconf = hapd->iconf; 227 if (hostapd_iface_conf_changed(newconf, oldconf)) { 228 char *fname; 229 int res; 230 231 wpa_printf(MSG_DEBUG, 232 "Configuration changes include interface/BSS modification - force full disable+enable sequence"); 233 fname = os_strdup(iface->config_fname); 234 if (!fname) { 235 hostapd_config_free(newconf); 236 return -1; 237 } 238 hostapd_remove_iface(interfaces, hapd->conf->iface); 239 iface = hostapd_init(interfaces, fname); 240 os_free(fname); 241 hostapd_config_free(newconf); 242 if (!iface) { 243 wpa_printf(MSG_ERROR, 244 "Failed to initialize interface on config reload"); 245 return -1; 246 } 247 iface->interfaces = interfaces; 248 interfaces->iface[interfaces->count] = iface; 249 interfaces->count++; 250 res = hostapd_enable_iface(iface); 251 if (res < 0) 252 wpa_printf(MSG_ERROR, 253 "Failed to enable interface on config reload"); 254 return res; 255 } 256 iface->conf = newconf; 257 258 for (j = 0; j < iface->num_bss; j++) { 259 hapd = iface->bss[j]; 260 hapd->iconf = newconf; 261 hapd->iconf->channel = oldconf->channel; 262 hapd->iconf->acs = oldconf->acs; 263 hapd->iconf->secondary_channel = oldconf->secondary_channel; 264 hapd->iconf->ieee80211n = oldconf->ieee80211n; 265 hapd->iconf->ieee80211ac = oldconf->ieee80211ac; 266 hapd->iconf->ht_capab = oldconf->ht_capab; 267 hapd->iconf->vht_capab = oldconf->vht_capab; 268 hostapd_set_oper_chwidth(hapd->iconf, 269 hostapd_get_oper_chwidth(oldconf)); 270 hostapd_set_oper_centr_freq_seg0_idx( 271 hapd->iconf, 272 hostapd_get_oper_centr_freq_seg0_idx(oldconf)); 273 hostapd_set_oper_centr_freq_seg1_idx( 274 hapd->iconf, 275 hostapd_get_oper_centr_freq_seg1_idx(oldconf)); 276 hapd->conf = newconf->bss[j]; 277 hostapd_reload_bss(hapd); 278 } 279 280 hostapd_config_free(oldconf); 281 282 283 return 0; 284 } 285 286 287 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd, 288 const char *ifname) 289 { 290 int i; 291 292 if (!ifname || !hapd->drv_priv) 293 return; 294 for (i = 0; i < NUM_WEP_KEYS; i++) { 295 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i, 296 0, NULL, 0, NULL, 0)) { 297 wpa_printf(MSG_DEBUG, "Failed to clear default " 298 "encryption keys (ifname=%s keyidx=%d)", 299 ifname, i); 300 } 301 } 302 #ifdef CONFIG_IEEE80211W 303 if (hapd->conf->ieee80211w) { 304 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) { 305 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, 306 NULL, i, 0, NULL, 307 0, NULL, 0)) { 308 wpa_printf(MSG_DEBUG, "Failed to clear " 309 "default mgmt encryption keys " 310 "(ifname=%s keyidx=%d)", ifname, i); 311 } 312 } 313 } 314 #endif /* CONFIG_IEEE80211W */ 315 } 316 317 318 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd) 319 { 320 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface); 321 return 0; 322 } 323 324 325 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd) 326 { 327 int errors = 0, idx; 328 struct hostapd_ssid *ssid = &hapd->conf->ssid; 329 330 idx = ssid->wep.idx; 331 if (ssid->wep.default_len && 332 hostapd_drv_set_key(hapd->conf->iface, 333 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx, 334 1, NULL, 0, ssid->wep.key[idx], 335 ssid->wep.len[idx])) { 336 wpa_printf(MSG_WARNING, "Could not set WEP encryption."); 337 errors++; 338 } 339 340 return errors; 341 } 342 343 344 static void hostapd_free_hapd_data(struct hostapd_data *hapd) 345 { 346 os_free(hapd->probereq_cb); 347 hapd->probereq_cb = NULL; 348 hapd->num_probereq_cb = 0; 349 350 #ifdef CONFIG_P2P 351 wpabuf_free(hapd->p2p_beacon_ie); 352 hapd->p2p_beacon_ie = NULL; 353 wpabuf_free(hapd->p2p_probe_resp_ie); 354 hapd->p2p_probe_resp_ie = NULL; 355 #endif /* CONFIG_P2P */ 356 357 if (!hapd->started) { 358 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started", 359 __func__, hapd->conf ? hapd->conf->iface : "N/A"); 360 return; 361 } 362 hapd->started = 0; 363 hapd->beacon_set_done = 0; 364 365 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface); 366 iapp_deinit(hapd->iapp); 367 hapd->iapp = NULL; 368 accounting_deinit(hapd); 369 hostapd_deinit_wpa(hapd); 370 vlan_deinit(hapd); 371 hostapd_acl_deinit(hapd); 372 #ifndef CONFIG_NO_RADIUS 373 radius_client_deinit(hapd->radius); 374 hapd->radius = NULL; 375 radius_das_deinit(hapd->radius_das); 376 hapd->radius_das = NULL; 377 #endif /* CONFIG_NO_RADIUS */ 378 379 hostapd_deinit_wps(hapd); 380 ieee802_1x_dealloc_kay_sm_hapd(hapd); 381 #ifdef CONFIG_DPP 382 hostapd_dpp_deinit(hapd); 383 gas_query_ap_deinit(hapd->gas); 384 #endif /* CONFIG_DPP */ 385 386 authsrv_deinit(hapd); 387 388 if (hapd->interface_added) { 389 hapd->interface_added = 0; 390 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) { 391 wpa_printf(MSG_WARNING, 392 "Failed to remove BSS interface %s", 393 hapd->conf->iface); 394 hapd->interface_added = 1; 395 } else { 396 /* 397 * Since this was a dynamically added interface, the 398 * driver wrapper may have removed its internal instance 399 * and hapd->drv_priv is not valid anymore. 400 */ 401 hapd->drv_priv = NULL; 402 } 403 } 404 405 wpabuf_free(hapd->time_adv); 406 407 #ifdef CONFIG_INTERWORKING 408 gas_serv_deinit(hapd); 409 #endif /* CONFIG_INTERWORKING */ 410 411 bss_load_update_deinit(hapd); 412 ndisc_snoop_deinit(hapd); 413 dhcp_snoop_deinit(hapd); 414 x_snoop_deinit(hapd); 415 416 #ifdef CONFIG_SQLITE 417 bin_clear_free(hapd->tmp_eap_user.identity, 418 hapd->tmp_eap_user.identity_len); 419 bin_clear_free(hapd->tmp_eap_user.password, 420 hapd->tmp_eap_user.password_len); 421 #endif /* CONFIG_SQLITE */ 422 423 #ifdef CONFIG_MESH 424 wpabuf_free(hapd->mesh_pending_auth); 425 hapd->mesh_pending_auth = NULL; 426 #endif /* CONFIG_MESH */ 427 428 hostapd_clean_rrm(hapd); 429 fils_hlp_deinit(hapd); 430 431 #ifdef CONFIG_SAE 432 { 433 struct hostapd_sae_commit_queue *q; 434 435 while ((q = dl_list_first(&hapd->sae_commit_queue, 436 struct hostapd_sae_commit_queue, 437 list))) { 438 dl_list_del(&q->list); 439 os_free(q); 440 } 441 } 442 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL); 443 #endif /* CONFIG_SAE */ 444 } 445 446 447 /** 448 * hostapd_cleanup - Per-BSS cleanup (deinitialization) 449 * @hapd: Pointer to BSS data 450 * 451 * This function is used to free all per-BSS data structures and resources. 452 * Most of the modules that are initialized in hostapd_setup_bss() are 453 * deinitialized here. 454 */ 455 static void hostapd_cleanup(struct hostapd_data *hapd) 456 { 457 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd, 458 hapd->conf ? hapd->conf->iface : "N/A"); 459 if (hapd->iface->interfaces && 460 hapd->iface->interfaces->ctrl_iface_deinit) { 461 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING); 462 hapd->iface->interfaces->ctrl_iface_deinit(hapd); 463 } 464 hostapd_free_hapd_data(hapd); 465 } 466 467 468 static void sta_track_deinit(struct hostapd_iface *iface) 469 { 470 struct hostapd_sta_info *info; 471 472 if (!iface->num_sta_seen) 473 return; 474 475 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info, 476 list))) { 477 dl_list_del(&info->list); 478 iface->num_sta_seen--; 479 sta_track_del(info); 480 } 481 } 482 483 484 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface) 485 { 486 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface); 487 #ifdef CONFIG_IEEE80211N 488 #ifdef NEED_AP_MLME 489 hostapd_stop_setup_timers(iface); 490 #endif /* NEED_AP_MLME */ 491 #endif /* CONFIG_IEEE80211N */ 492 if (iface->current_mode) 493 acs_cleanup(iface); 494 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features); 495 iface->hw_features = NULL; 496 iface->current_mode = NULL; 497 os_free(iface->current_rates); 498 iface->current_rates = NULL; 499 os_free(iface->basic_rates); 500 iface->basic_rates = NULL; 501 ap_list_deinit(iface); 502 sta_track_deinit(iface); 503 airtime_policy_update_deinit(iface); 504 } 505 506 507 /** 508 * hostapd_cleanup_iface - Complete per-interface cleanup 509 * @iface: Pointer to interface data 510 * 511 * This function is called after per-BSS data structures are deinitialized 512 * with hostapd_cleanup(). 513 */ 514 static void hostapd_cleanup_iface(struct hostapd_iface *iface) 515 { 516 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface); 517 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL); 518 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface, 519 NULL); 520 521 hostapd_cleanup_iface_partial(iface); 522 hostapd_config_free(iface->conf); 523 iface->conf = NULL; 524 525 os_free(iface->config_fname); 526 os_free(iface->bss); 527 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface); 528 os_free(iface); 529 } 530 531 532 static void hostapd_clear_wep(struct hostapd_data *hapd) 533 { 534 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) { 535 hostapd_set_privacy(hapd, 0); 536 hostapd_broadcast_wep_clear(hapd); 537 } 538 } 539 540 541 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd) 542 { 543 int i; 544 545 hostapd_broadcast_wep_set(hapd); 546 547 if (hapd->conf->ssid.wep.default_len) { 548 hostapd_set_privacy(hapd, 1); 549 return 0; 550 } 551 552 /* 553 * When IEEE 802.1X is not enabled, the driver may need to know how to 554 * set authentication algorithms for static WEP. 555 */ 556 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs); 557 558 for (i = 0; i < 4; i++) { 559 if (hapd->conf->ssid.wep.key[i] && 560 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i, 561 i == hapd->conf->ssid.wep.idx, NULL, 0, 562 hapd->conf->ssid.wep.key[i], 563 hapd->conf->ssid.wep.len[i])) { 564 wpa_printf(MSG_WARNING, "Could not set WEP " 565 "encryption."); 566 return -1; 567 } 568 if (hapd->conf->ssid.wep.key[i] && 569 i == hapd->conf->ssid.wep.idx) 570 hostapd_set_privacy(hapd, 1); 571 } 572 573 return 0; 574 } 575 576 577 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason) 578 { 579 int ret = 0; 580 u8 addr[ETH_ALEN]; 581 582 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL) 583 return 0; 584 585 if (!hapd->iface->driver_ap_teardown) { 586 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, 587 "Flushing old station entries"); 588 589 if (hostapd_flush(hapd)) { 590 wpa_msg(hapd->msg_ctx, MSG_WARNING, 591 "Could not connect to kernel driver"); 592 ret = -1; 593 } 594 } 595 if (hapd->conf && hapd->conf->broadcast_deauth) { 596 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, 597 "Deauthenticate all stations"); 598 os_memset(addr, 0xff, ETH_ALEN); 599 hostapd_drv_sta_deauth(hapd, addr, reason); 600 } 601 hostapd_free_stas(hapd); 602 603 return ret; 604 } 605 606 607 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd) 608 { 609 hostapd_free_stas(hapd); 610 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING); 611 hostapd_clear_wep(hapd); 612 } 613 614 615 /** 616 * hostapd_validate_bssid_configuration - Validate BSSID configuration 617 * @iface: Pointer to interface data 618 * Returns: 0 on success, -1 on failure 619 * 620 * This function is used to validate that the configured BSSIDs are valid. 621 */ 622 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface) 623 { 624 u8 mask[ETH_ALEN] = { 0 }; 625 struct hostapd_data *hapd = iface->bss[0]; 626 unsigned int i = iface->conf->num_bss, bits = 0, j; 627 int auto_addr = 0; 628 629 if (hostapd_drv_none(hapd)) 630 return 0; 631 632 if (iface->conf->use_driver_iface_addr) 633 return 0; 634 635 /* Generate BSSID mask that is large enough to cover the BSSIDs. */ 636 637 /* Determine the bits necessary to cover the number of BSSIDs. */ 638 for (i--; i; i >>= 1) 639 bits++; 640 641 /* Determine the bits necessary to any configured BSSIDs, 642 if they are higher than the number of BSSIDs. */ 643 for (j = 0; j < iface->conf->num_bss; j++) { 644 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) { 645 if (j) 646 auto_addr++; 647 continue; 648 } 649 650 for (i = 0; i < ETH_ALEN; i++) { 651 mask[i] |= 652 iface->conf->bss[j]->bssid[i] ^ 653 hapd->own_addr[i]; 654 } 655 } 656 657 if (!auto_addr) 658 goto skip_mask_ext; 659 660 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++) 661 ; 662 j = 0; 663 if (i < ETH_ALEN) { 664 j = (5 - i) * 8; 665 666 while (mask[i] != 0) { 667 mask[i] >>= 1; 668 j++; 669 } 670 } 671 672 if (bits < j) 673 bits = j; 674 675 if (bits > 40) { 676 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)", 677 bits); 678 return -1; 679 } 680 681 os_memset(mask, 0xff, ETH_ALEN); 682 j = bits / 8; 683 for (i = 5; i > 5 - j; i--) 684 mask[i] = 0; 685 j = bits % 8; 686 while (j) { 687 j--; 688 mask[i] <<= 1; 689 } 690 691 skip_mask_ext: 692 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)", 693 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits); 694 695 if (!auto_addr) 696 return 0; 697 698 for (i = 0; i < ETH_ALEN; i++) { 699 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) { 700 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR 701 " for start address " MACSTR ".", 702 MAC2STR(mask), MAC2STR(hapd->own_addr)); 703 wpa_printf(MSG_ERROR, "Start address must be the " 704 "first address in the block (i.e., addr " 705 "AND mask == addr)."); 706 return -1; 707 } 708 } 709 710 return 0; 711 } 712 713 714 static int mac_in_conf(struct hostapd_config *conf, const void *a) 715 { 716 size_t i; 717 718 for (i = 0; i < conf->num_bss; i++) { 719 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) { 720 return 1; 721 } 722 } 723 724 return 0; 725 } 726 727 728 #ifndef CONFIG_NO_RADIUS 729 730 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd, 731 struct radius_das_attrs *attr) 732 { 733 if (attr->nas_identifier && 734 (!hapd->conf->nas_identifier || 735 os_strlen(hapd->conf->nas_identifier) != 736 attr->nas_identifier_len || 737 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier, 738 attr->nas_identifier_len) != 0)) { 739 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch"); 740 return 1; 741 } 742 743 if (attr->nas_ip_addr && 744 (hapd->conf->own_ip_addr.af != AF_INET || 745 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) != 746 0)) { 747 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch"); 748 return 1; 749 } 750 751 #ifdef CONFIG_IPV6 752 if (attr->nas_ipv6_addr && 753 (hapd->conf->own_ip_addr.af != AF_INET6 || 754 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16) 755 != 0)) { 756 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch"); 757 return 1; 758 } 759 #endif /* CONFIG_IPV6 */ 760 761 return 0; 762 } 763 764 765 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd, 766 struct radius_das_attrs *attr, 767 int *multi) 768 { 769 struct sta_info *selected, *sta; 770 char buf[128]; 771 int num_attr = 0; 772 int count; 773 774 *multi = 0; 775 776 for (sta = hapd->sta_list; sta; sta = sta->next) 777 sta->radius_das_match = 1; 778 779 if (attr->sta_addr) { 780 num_attr++; 781 sta = ap_get_sta(hapd, attr->sta_addr); 782 if (!sta) { 783 wpa_printf(MSG_DEBUG, 784 "RADIUS DAS: No Calling-Station-Id match"); 785 return NULL; 786 } 787 788 selected = sta; 789 for (sta = hapd->sta_list; sta; sta = sta->next) { 790 if (sta != selected) 791 sta->radius_das_match = 0; 792 } 793 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match"); 794 } 795 796 if (attr->acct_session_id) { 797 num_attr++; 798 if (attr->acct_session_id_len != 16) { 799 wpa_printf(MSG_DEBUG, 800 "RADIUS DAS: Acct-Session-Id cannot match"); 801 return NULL; 802 } 803 count = 0; 804 805 for (sta = hapd->sta_list; sta; sta = sta->next) { 806 if (!sta->radius_das_match) 807 continue; 808 os_snprintf(buf, sizeof(buf), "%016llX", 809 (unsigned long long) sta->acct_session_id); 810 if (os_memcmp(attr->acct_session_id, buf, 16) != 0) 811 sta->radius_das_match = 0; 812 else 813 count++; 814 } 815 816 if (count == 0) { 817 wpa_printf(MSG_DEBUG, 818 "RADIUS DAS: No matches remaining after Acct-Session-Id check"); 819 return NULL; 820 } 821 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match"); 822 } 823 824 if (attr->acct_multi_session_id) { 825 num_attr++; 826 if (attr->acct_multi_session_id_len != 16) { 827 wpa_printf(MSG_DEBUG, 828 "RADIUS DAS: Acct-Multi-Session-Id cannot match"); 829 return NULL; 830 } 831 count = 0; 832 833 for (sta = hapd->sta_list; sta; sta = sta->next) { 834 if (!sta->radius_das_match) 835 continue; 836 if (!sta->eapol_sm || 837 !sta->eapol_sm->acct_multi_session_id) { 838 sta->radius_das_match = 0; 839 continue; 840 } 841 os_snprintf(buf, sizeof(buf), "%016llX", 842 (unsigned long long) 843 sta->eapol_sm->acct_multi_session_id); 844 if (os_memcmp(attr->acct_multi_session_id, buf, 16) != 845 0) 846 sta->radius_das_match = 0; 847 else 848 count++; 849 } 850 851 if (count == 0) { 852 wpa_printf(MSG_DEBUG, 853 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check"); 854 return NULL; 855 } 856 wpa_printf(MSG_DEBUG, 857 "RADIUS DAS: Acct-Multi-Session-Id match"); 858 } 859 860 if (attr->cui) { 861 num_attr++; 862 count = 0; 863 864 for (sta = hapd->sta_list; sta; sta = sta->next) { 865 struct wpabuf *cui; 866 867 if (!sta->radius_das_match) 868 continue; 869 cui = ieee802_1x_get_radius_cui(sta->eapol_sm); 870 if (!cui || wpabuf_len(cui) != attr->cui_len || 871 os_memcmp(wpabuf_head(cui), attr->cui, 872 attr->cui_len) != 0) 873 sta->radius_das_match = 0; 874 else 875 count++; 876 } 877 878 if (count == 0) { 879 wpa_printf(MSG_DEBUG, 880 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check"); 881 return NULL; 882 } 883 wpa_printf(MSG_DEBUG, 884 "RADIUS DAS: Chargeable-User-Identity match"); 885 } 886 887 if (attr->user_name) { 888 num_attr++; 889 count = 0; 890 891 for (sta = hapd->sta_list; sta; sta = sta->next) { 892 u8 *identity; 893 size_t identity_len; 894 895 if (!sta->radius_das_match) 896 continue; 897 identity = ieee802_1x_get_identity(sta->eapol_sm, 898 &identity_len); 899 if (!identity || 900 identity_len != attr->user_name_len || 901 os_memcmp(identity, attr->user_name, identity_len) 902 != 0) 903 sta->radius_das_match = 0; 904 else 905 count++; 906 } 907 908 if (count == 0) { 909 wpa_printf(MSG_DEBUG, 910 "RADIUS DAS: No matches remaining after User-Name check"); 911 return NULL; 912 } 913 wpa_printf(MSG_DEBUG, 914 "RADIUS DAS: User-Name match"); 915 } 916 917 if (num_attr == 0) { 918 /* 919 * In theory, we could match all current associations, but it 920 * seems safer to just reject requests that do not include any 921 * session identification attributes. 922 */ 923 wpa_printf(MSG_DEBUG, 924 "RADIUS DAS: No session identification attributes included"); 925 return NULL; 926 } 927 928 selected = NULL; 929 for (sta = hapd->sta_list; sta; sta = sta->next) { 930 if (sta->radius_das_match) { 931 if (selected) { 932 *multi = 1; 933 return NULL; 934 } 935 selected = sta; 936 } 937 } 938 939 return selected; 940 } 941 942 943 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd, 944 struct radius_das_attrs *attr) 945 { 946 if (!hapd->wpa_auth) 947 return -1; 948 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr); 949 } 950 951 952 static enum radius_das_res 953 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr) 954 { 955 struct hostapd_data *hapd = ctx; 956 struct sta_info *sta; 957 int multi; 958 959 if (hostapd_das_nas_mismatch(hapd, attr)) 960 return RADIUS_DAS_NAS_MISMATCH; 961 962 sta = hostapd_das_find_sta(hapd, attr, &multi); 963 if (sta == NULL) { 964 if (multi) { 965 wpa_printf(MSG_DEBUG, 966 "RADIUS DAS: Multiple sessions match - not supported"); 967 return RADIUS_DAS_MULTI_SESSION_MATCH; 968 } 969 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) { 970 wpa_printf(MSG_DEBUG, 971 "RADIUS DAS: PMKSA cache entry matched"); 972 return RADIUS_DAS_SUCCESS; 973 } 974 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found"); 975 return RADIUS_DAS_SESSION_NOT_FOUND; 976 } 977 978 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR 979 " - disconnecting", MAC2STR(sta->addr)); 980 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr); 981 982 hostapd_drv_sta_deauth(hapd, sta->addr, 983 WLAN_REASON_PREV_AUTH_NOT_VALID); 984 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID); 985 986 return RADIUS_DAS_SUCCESS; 987 } 988 989 990 #ifdef CONFIG_HS20 991 static enum radius_das_res 992 hostapd_das_coa(void *ctx, struct radius_das_attrs *attr) 993 { 994 struct hostapd_data *hapd = ctx; 995 struct sta_info *sta; 996 int multi; 997 998 if (hostapd_das_nas_mismatch(hapd, attr)) 999 return RADIUS_DAS_NAS_MISMATCH; 1000 1001 sta = hostapd_das_find_sta(hapd, attr, &multi); 1002 if (!sta) { 1003 if (multi) { 1004 wpa_printf(MSG_DEBUG, 1005 "RADIUS DAS: Multiple sessions match - not supported"); 1006 return RADIUS_DAS_MULTI_SESSION_MATCH; 1007 } 1008 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found"); 1009 return RADIUS_DAS_SESSION_NOT_FOUND; 1010 } 1011 1012 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR 1013 " - CoA", MAC2STR(sta->addr)); 1014 1015 if (attr->hs20_t_c_filtering) { 1016 if (attr->hs20_t_c_filtering[0] & BIT(0)) { 1017 wpa_printf(MSG_DEBUG, 1018 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request"); 1019 return RADIUS_DAS_COA_FAILED; 1020 } 1021 1022 hs20_t_c_filtering(hapd, sta, 0); 1023 } 1024 1025 return RADIUS_DAS_SUCCESS; 1026 } 1027 #else /* CONFIG_HS20 */ 1028 #define hostapd_das_coa NULL 1029 #endif /* CONFIG_HS20 */ 1030 1031 1032 #ifdef CONFIG_SQLITE 1033 1034 static int db_table_exists(sqlite3 *db, const char *name) 1035 { 1036 char cmd[128]; 1037 1038 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name); 1039 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK; 1040 } 1041 1042 1043 static int db_table_create_radius_attributes(sqlite3 *db) 1044 { 1045 char *err = NULL; 1046 const char *sql = 1047 "CREATE TABLE radius_attributes(" 1048 " id INTEGER PRIMARY KEY," 1049 " sta TEXT," 1050 " reqtype TEXT," 1051 " attr TEXT" 1052 ");" 1053 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);"; 1054 1055 wpa_printf(MSG_DEBUG, 1056 "Adding database table for RADIUS attribute information"); 1057 if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) { 1058 wpa_printf(MSG_ERROR, "SQLite error: %s", err); 1059 sqlite3_free(err); 1060 return -1; 1061 } 1062 1063 return 0; 1064 } 1065 1066 #endif /* CONFIG_SQLITE */ 1067 1068 #endif /* CONFIG_NO_RADIUS */ 1069 1070 1071 /** 1072 * hostapd_setup_bss - Per-BSS setup (initialization) 1073 * @hapd: Pointer to BSS data 1074 * @first: Whether this BSS is the first BSS of an interface; -1 = not first, 1075 * but interface may exist 1076 * 1077 * This function is used to initialize all per-BSS data structures and 1078 * resources. This gets called in a loop for each BSS when an interface is 1079 * initialized. Most of the modules that are initialized here will be 1080 * deinitialized in hostapd_cleanup(). 1081 */ 1082 static int hostapd_setup_bss(struct hostapd_data *hapd, int first) 1083 { 1084 struct hostapd_bss_config *conf = hapd->conf; 1085 u8 ssid[SSID_MAX_LEN + 1]; 1086 int ssid_len, set_ssid; 1087 char force_ifname[IFNAMSIZ]; 1088 u8 if_addr[ETH_ALEN]; 1089 int flush_old_stations = 1; 1090 1091 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)", 1092 __func__, hapd, conf->iface, first); 1093 1094 #ifdef EAP_SERVER_TNC 1095 if (conf->tnc && tncs_global_init() < 0) { 1096 wpa_printf(MSG_ERROR, "Failed to initialize TNCS"); 1097 return -1; 1098 } 1099 #endif /* EAP_SERVER_TNC */ 1100 1101 if (hapd->started) { 1102 wpa_printf(MSG_ERROR, "%s: Interface %s was already started", 1103 __func__, conf->iface); 1104 return -1; 1105 } 1106 hapd->started = 1; 1107 1108 if (!first || first == -1) { 1109 u8 *addr = hapd->own_addr; 1110 1111 if (!is_zero_ether_addr(conf->bssid)) { 1112 /* Allocate the configured BSSID. */ 1113 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN); 1114 1115 if (hostapd_mac_comp(hapd->own_addr, 1116 hapd->iface->bss[0]->own_addr) == 1117 0) { 1118 wpa_printf(MSG_ERROR, "BSS '%s' may not have " 1119 "BSSID set to the MAC address of " 1120 "the radio", conf->iface); 1121 return -1; 1122 } 1123 } else if (hapd->iconf->use_driver_iface_addr) { 1124 addr = NULL; 1125 } else { 1126 /* Allocate the next available BSSID. */ 1127 do { 1128 inc_byte_array(hapd->own_addr, ETH_ALEN); 1129 } while (mac_in_conf(hapd->iconf, hapd->own_addr)); 1130 } 1131 1132 hapd->interface_added = 1; 1133 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS, 1134 conf->iface, addr, hapd, 1135 &hapd->drv_priv, force_ifname, if_addr, 1136 conf->bridge[0] ? conf->bridge : NULL, 1137 first == -1)) { 1138 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID=" 1139 MACSTR ")", MAC2STR(hapd->own_addr)); 1140 hapd->interface_added = 0; 1141 return -1; 1142 } 1143 1144 if (!addr) 1145 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN); 1146 } 1147 1148 if (conf->wmm_enabled < 0) 1149 conf->wmm_enabled = hapd->iconf->ieee80211n; 1150 1151 #ifdef CONFIG_IEEE80211R_AP 1152 if (is_zero_ether_addr(conf->r1_key_holder)) 1153 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN); 1154 #endif /* CONFIG_IEEE80211R_AP */ 1155 1156 #ifdef CONFIG_MESH 1157 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL) 1158 flush_old_stations = 0; 1159 #endif /* CONFIG_MESH */ 1160 1161 if (flush_old_stations) 1162 hostapd_flush_old_stations(hapd, 1163 WLAN_REASON_PREV_AUTH_NOT_VALID); 1164 hostapd_set_privacy(hapd, 0); 1165 1166 hostapd_broadcast_wep_clear(hapd); 1167 if (hostapd_setup_encryption(conf->iface, hapd)) 1168 return -1; 1169 1170 /* 1171 * Fetch the SSID from the system and use it or, 1172 * if one was specified in the config file, verify they 1173 * match. 1174 */ 1175 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid)); 1176 if (ssid_len < 0) { 1177 wpa_printf(MSG_ERROR, "Could not read SSID from system"); 1178 return -1; 1179 } 1180 if (conf->ssid.ssid_set) { 1181 /* 1182 * If SSID is specified in the config file and it differs 1183 * from what is being used then force installation of the 1184 * new SSID. 1185 */ 1186 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len || 1187 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0); 1188 } else { 1189 /* 1190 * No SSID in the config file; just use the one we got 1191 * from the system. 1192 */ 1193 set_ssid = 0; 1194 conf->ssid.ssid_len = ssid_len; 1195 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len); 1196 } 1197 1198 if (!hostapd_drv_none(hapd)) { 1199 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR 1200 " and ssid \"%s\"", 1201 conf->iface, MAC2STR(hapd->own_addr), 1202 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len)); 1203 } 1204 1205 if (hostapd_setup_wpa_psk(conf)) { 1206 wpa_printf(MSG_ERROR, "WPA-PSK setup failed."); 1207 return -1; 1208 } 1209 1210 /* Set SSID for the kernel driver (to be used in beacon and probe 1211 * response frames) */ 1212 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid, 1213 conf->ssid.ssid_len)) { 1214 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver"); 1215 return -1; 1216 } 1217 1218 if (wpa_debug_level <= MSG_MSGDUMP) 1219 conf->radius->msg_dumps = 1; 1220 #ifndef CONFIG_NO_RADIUS 1221 1222 #ifdef CONFIG_SQLITE 1223 if (conf->radius_req_attr_sqlite) { 1224 if (sqlite3_open(conf->radius_req_attr_sqlite, 1225 &hapd->rad_attr_db)) { 1226 wpa_printf(MSG_ERROR, "Could not open SQLite file '%s'", 1227 conf->radius_req_attr_sqlite); 1228 return -1; 1229 } 1230 1231 wpa_printf(MSG_DEBUG, "Opening RADIUS attribute database: %s", 1232 conf->radius_req_attr_sqlite); 1233 if (!db_table_exists(hapd->rad_attr_db, "radius_attributes") && 1234 db_table_create_radius_attributes(hapd->rad_attr_db) < 0) 1235 return -1; 1236 } 1237 #endif /* CONFIG_SQLITE */ 1238 1239 hapd->radius = radius_client_init(hapd, conf->radius); 1240 if (hapd->radius == NULL) { 1241 wpa_printf(MSG_ERROR, "RADIUS client initialization failed."); 1242 return -1; 1243 } 1244 1245 if (conf->radius_das_port) { 1246 struct radius_das_conf das_conf; 1247 os_memset(&das_conf, 0, sizeof(das_conf)); 1248 das_conf.port = conf->radius_das_port; 1249 das_conf.shared_secret = conf->radius_das_shared_secret; 1250 das_conf.shared_secret_len = 1251 conf->radius_das_shared_secret_len; 1252 das_conf.client_addr = &conf->radius_das_client_addr; 1253 das_conf.time_window = conf->radius_das_time_window; 1254 das_conf.require_event_timestamp = 1255 conf->radius_das_require_event_timestamp; 1256 das_conf.require_message_authenticator = 1257 conf->radius_das_require_message_authenticator; 1258 das_conf.ctx = hapd; 1259 das_conf.disconnect = hostapd_das_disconnect; 1260 das_conf.coa = hostapd_das_coa; 1261 hapd->radius_das = radius_das_init(&das_conf); 1262 if (hapd->radius_das == NULL) { 1263 wpa_printf(MSG_ERROR, "RADIUS DAS initialization " 1264 "failed."); 1265 return -1; 1266 } 1267 } 1268 #endif /* CONFIG_NO_RADIUS */ 1269 1270 if (hostapd_acl_init(hapd)) { 1271 wpa_printf(MSG_ERROR, "ACL initialization failed."); 1272 return -1; 1273 } 1274 if (hostapd_init_wps(hapd, conf)) 1275 return -1; 1276 1277 #ifdef CONFIG_DPP 1278 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx); 1279 if (!hapd->gas) 1280 return -1; 1281 if (hostapd_dpp_init(hapd)) 1282 return -1; 1283 #endif /* CONFIG_DPP */ 1284 1285 if (authsrv_init(hapd) < 0) 1286 return -1; 1287 1288 if (ieee802_1x_init(hapd)) { 1289 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed."); 1290 return -1; 1291 } 1292 1293 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd)) 1294 return -1; 1295 1296 if (accounting_init(hapd)) { 1297 wpa_printf(MSG_ERROR, "Accounting initialization failed."); 1298 return -1; 1299 } 1300 1301 if (conf->ieee802_11f && 1302 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) { 1303 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization " 1304 "failed."); 1305 return -1; 1306 } 1307 1308 #ifdef CONFIG_INTERWORKING 1309 if (gas_serv_init(hapd)) { 1310 wpa_printf(MSG_ERROR, "GAS server initialization failed"); 1311 return -1; 1312 } 1313 1314 if (conf->qos_map_set_len && 1315 hostapd_drv_set_qos_map(hapd, conf->qos_map_set, 1316 conf->qos_map_set_len)) { 1317 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map"); 1318 return -1; 1319 } 1320 #endif /* CONFIG_INTERWORKING */ 1321 1322 if (conf->bss_load_update_period && bss_load_update_init(hapd)) { 1323 wpa_printf(MSG_ERROR, "BSS Load initialization failed"); 1324 return -1; 1325 } 1326 1327 if (conf->proxy_arp) { 1328 if (x_snoop_init(hapd)) { 1329 wpa_printf(MSG_ERROR, 1330 "Generic snooping infrastructure initialization failed"); 1331 return -1; 1332 } 1333 1334 if (dhcp_snoop_init(hapd)) { 1335 wpa_printf(MSG_ERROR, 1336 "DHCP snooping initialization failed"); 1337 return -1; 1338 } 1339 1340 if (ndisc_snoop_init(hapd)) { 1341 wpa_printf(MSG_ERROR, 1342 "Neighbor Discovery snooping initialization failed"); 1343 return -1; 1344 } 1345 } 1346 1347 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) { 1348 wpa_printf(MSG_ERROR, "VLAN initialization failed."); 1349 return -1; 1350 } 1351 1352 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0) 1353 return -1; 1354 1355 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0) 1356 return -1; 1357 1358 if (hapd->driver && hapd->driver->set_operstate) 1359 hapd->driver->set_operstate(hapd->drv_priv, 1); 1360 1361 return 0; 1362 } 1363 1364 1365 static void hostapd_tx_queue_params(struct hostapd_iface *iface) 1366 { 1367 struct hostapd_data *hapd = iface->bss[0]; 1368 int i; 1369 struct hostapd_tx_queue_params *p; 1370 1371 #ifdef CONFIG_MESH 1372 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL) 1373 return; 1374 #endif /* CONFIG_MESH */ 1375 1376 for (i = 0; i < NUM_TX_QUEUES; i++) { 1377 p = &iface->conf->tx_queue[i]; 1378 1379 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin, 1380 p->cwmax, p->burst)) { 1381 wpa_printf(MSG_DEBUG, "Failed to set TX queue " 1382 "parameters for queue %d.", i); 1383 /* Continue anyway */ 1384 } 1385 } 1386 } 1387 1388 1389 static int hostapd_set_acl_list(struct hostapd_data *hapd, 1390 struct mac_acl_entry *mac_acl, 1391 int n_entries, u8 accept_acl) 1392 { 1393 struct hostapd_acl_params *acl_params; 1394 int i, err; 1395 1396 acl_params = os_zalloc(sizeof(*acl_params) + 1397 (n_entries * sizeof(acl_params->mac_acl[0]))); 1398 if (!acl_params) 1399 return -ENOMEM; 1400 1401 for (i = 0; i < n_entries; i++) 1402 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr, 1403 ETH_ALEN); 1404 1405 acl_params->acl_policy = accept_acl; 1406 acl_params->num_mac_acl = n_entries; 1407 1408 err = hostapd_drv_set_acl(hapd, acl_params); 1409 1410 os_free(acl_params); 1411 1412 return err; 1413 } 1414 1415 1416 static void hostapd_set_acl(struct hostapd_data *hapd) 1417 { 1418 struct hostapd_config *conf = hapd->iconf; 1419 int err; 1420 u8 accept_acl; 1421 1422 if (hapd->iface->drv_max_acl_mac_addrs == 0) 1423 return; 1424 1425 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) { 1426 accept_acl = 1; 1427 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac, 1428 conf->bss[0]->num_accept_mac, 1429 accept_acl); 1430 if (err) { 1431 wpa_printf(MSG_DEBUG, "Failed to set accept acl"); 1432 return; 1433 } 1434 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) { 1435 accept_acl = 0; 1436 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac, 1437 conf->bss[0]->num_deny_mac, 1438 accept_acl); 1439 if (err) { 1440 wpa_printf(MSG_DEBUG, "Failed to set deny acl"); 1441 return; 1442 } 1443 } 1444 } 1445 1446 1447 static int start_ctrl_iface_bss(struct hostapd_data *hapd) 1448 { 1449 if (!hapd->iface->interfaces || 1450 !hapd->iface->interfaces->ctrl_iface_init) 1451 return 0; 1452 1453 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) { 1454 wpa_printf(MSG_ERROR, 1455 "Failed to setup control interface for %s", 1456 hapd->conf->iface); 1457 return -1; 1458 } 1459 1460 return 0; 1461 } 1462 1463 1464 static int start_ctrl_iface(struct hostapd_iface *iface) 1465 { 1466 size_t i; 1467 1468 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init) 1469 return 0; 1470 1471 for (i = 0; i < iface->num_bss; i++) { 1472 struct hostapd_data *hapd = iface->bss[i]; 1473 if (iface->interfaces->ctrl_iface_init(hapd)) { 1474 wpa_printf(MSG_ERROR, 1475 "Failed to setup control interface for %s", 1476 hapd->conf->iface); 1477 return -1; 1478 } 1479 } 1480 1481 return 0; 1482 } 1483 1484 1485 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx) 1486 { 1487 struct hostapd_iface *iface = eloop_ctx; 1488 1489 if (!iface->wait_channel_update) { 1490 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it"); 1491 return; 1492 } 1493 1494 /* 1495 * It is possible that the existing channel list is acceptable, so try 1496 * to proceed. 1497 */ 1498 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway"); 1499 setup_interface2(iface); 1500 } 1501 1502 1503 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator) 1504 { 1505 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER) 1506 return; 1507 1508 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup"); 1509 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL); 1510 setup_interface2(iface); 1511 } 1512 1513 1514 static int setup_interface(struct hostapd_iface *iface) 1515 { 1516 struct hostapd_data *hapd = iface->bss[0]; 1517 size_t i; 1518 1519 /* 1520 * It is possible that setup_interface() is called after the interface 1521 * was disabled etc., in which case driver_ap_teardown is possibly set 1522 * to 1. Clear it here so any other key/station deletion, which is not 1523 * part of a teardown flow, would also call the relevant driver 1524 * callbacks. 1525 */ 1526 iface->driver_ap_teardown = 0; 1527 1528 if (!iface->phy[0]) { 1529 const char *phy = hostapd_drv_get_radio_name(hapd); 1530 if (phy) { 1531 wpa_printf(MSG_DEBUG, "phy: %s", phy); 1532 os_strlcpy(iface->phy, phy, sizeof(iface->phy)); 1533 } 1534 } 1535 1536 /* 1537 * Make sure that all BSSes get configured with a pointer to the same 1538 * driver interface. 1539 */ 1540 for (i = 1; i < iface->num_bss; i++) { 1541 iface->bss[i]->driver = hapd->driver; 1542 iface->bss[i]->drv_priv = hapd->drv_priv; 1543 } 1544 1545 if (hostapd_validate_bssid_configuration(iface)) 1546 return -1; 1547 1548 /* 1549 * Initialize control interfaces early to allow external monitoring of 1550 * channel setup operations that may take considerable amount of time 1551 * especially for DFS cases. 1552 */ 1553 if (start_ctrl_iface(iface)) 1554 return -1; 1555 1556 if (hapd->iconf->country[0] && hapd->iconf->country[1]) { 1557 char country[4], previous_country[4]; 1558 1559 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE); 1560 if (hostapd_get_country(hapd, previous_country) < 0) 1561 previous_country[0] = '\0'; 1562 1563 os_memcpy(country, hapd->iconf->country, 3); 1564 country[3] = '\0'; 1565 if (hostapd_set_country(hapd, country) < 0) { 1566 wpa_printf(MSG_ERROR, "Failed to set country code"); 1567 return -1; 1568 } 1569 1570 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s", 1571 previous_country, country); 1572 1573 if (os_strncmp(previous_country, country, 2) != 0) { 1574 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update"); 1575 iface->wait_channel_update = 1; 1576 eloop_register_timeout(5, 0, 1577 channel_list_update_timeout, 1578 iface, NULL); 1579 return 0; 1580 } 1581 } 1582 1583 return setup_interface2(iface); 1584 } 1585 1586 1587 static int setup_interface2(struct hostapd_iface *iface) 1588 { 1589 iface->wait_channel_update = 0; 1590 1591 if (hostapd_get_hw_features(iface)) { 1592 /* Not all drivers support this yet, so continue without hw 1593 * feature data. */ 1594 } else { 1595 int ret = hostapd_select_hw_mode(iface); 1596 if (ret < 0) { 1597 wpa_printf(MSG_ERROR, "Could not select hw_mode and " 1598 "channel. (%d)", ret); 1599 goto fail; 1600 } 1601 if (ret == 1) { 1602 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)"); 1603 return 0; 1604 } 1605 ret = hostapd_check_ht_capab(iface); 1606 if (ret < 0) 1607 goto fail; 1608 if (ret == 1) { 1609 wpa_printf(MSG_DEBUG, "Interface initialization will " 1610 "be completed in a callback"); 1611 return 0; 1612 } 1613 1614 if (iface->conf->ieee80211h) 1615 wpa_printf(MSG_DEBUG, "DFS support is enabled"); 1616 } 1617 return hostapd_setup_interface_complete(iface, 0); 1618 1619 fail: 1620 hostapd_set_state(iface, HAPD_IFACE_DISABLED); 1621 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED); 1622 if (iface->interfaces && iface->interfaces->terminate_on_error) 1623 eloop_terminate(); 1624 return -1; 1625 } 1626 1627 1628 #ifdef CONFIG_FST 1629 1630 static const u8 * fst_hostapd_get_bssid_cb(void *ctx) 1631 { 1632 struct hostapd_data *hapd = ctx; 1633 1634 return hapd->own_addr; 1635 } 1636 1637 1638 static void fst_hostapd_get_channel_info_cb(void *ctx, 1639 enum hostapd_hw_mode *hw_mode, 1640 u8 *channel) 1641 { 1642 struct hostapd_data *hapd = ctx; 1643 1644 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel); 1645 } 1646 1647 1648 static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies) 1649 { 1650 struct hostapd_data *hapd = ctx; 1651 1652 if (hapd->iface->fst_ies != fst_ies) { 1653 hapd->iface->fst_ies = fst_ies; 1654 if (ieee802_11_set_beacon(hapd)) 1655 wpa_printf(MSG_WARNING, "FST: Cannot set beacon"); 1656 } 1657 } 1658 1659 1660 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da, 1661 struct wpabuf *buf) 1662 { 1663 struct hostapd_data *hapd = ctx; 1664 1665 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da, 1666 wpabuf_head(buf), wpabuf_len(buf)); 1667 } 1668 1669 1670 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr) 1671 { 1672 struct hostapd_data *hapd = ctx; 1673 struct sta_info *sta = ap_get_sta(hapd, addr); 1674 1675 return sta ? sta->mb_ies : NULL; 1676 } 1677 1678 1679 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr, 1680 const u8 *buf, size_t size) 1681 { 1682 struct hostapd_data *hapd = ctx; 1683 struct sta_info *sta = ap_get_sta(hapd, addr); 1684 1685 if (sta) { 1686 struct mb_ies_info info; 1687 1688 if (!mb_ies_info_by_ies(&info, buf, size)) { 1689 wpabuf_free(sta->mb_ies); 1690 sta->mb_ies = mb_ies_by_info(&info); 1691 } 1692 } 1693 } 1694 1695 1696 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx, 1697 Boolean mb_only) 1698 { 1699 struct sta_info *s = (struct sta_info *) *get_ctx; 1700 1701 if (mb_only) { 1702 for (; s && !s->mb_ies; s = s->next) 1703 ; 1704 } 1705 1706 if (s) { 1707 *get_ctx = (struct fst_get_peer_ctx *) s->next; 1708 1709 return s->addr; 1710 } 1711 1712 *get_ctx = NULL; 1713 return NULL; 1714 } 1715 1716 1717 static const u8 * fst_hostapd_get_peer_first(void *ctx, 1718 struct fst_get_peer_ctx **get_ctx, 1719 Boolean mb_only) 1720 { 1721 struct hostapd_data *hapd = ctx; 1722 1723 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list; 1724 1725 return fst_hostapd_get_sta(get_ctx, mb_only); 1726 } 1727 1728 1729 static const u8 * fst_hostapd_get_peer_next(void *ctx, 1730 struct fst_get_peer_ctx **get_ctx, 1731 Boolean mb_only) 1732 { 1733 return fst_hostapd_get_sta(get_ctx, mb_only); 1734 } 1735 1736 1737 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd, 1738 struct fst_wpa_obj *iface_obj) 1739 { 1740 iface_obj->ctx = hapd; 1741 iface_obj->get_bssid = fst_hostapd_get_bssid_cb; 1742 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb; 1743 iface_obj->set_ies = fst_hostapd_set_ies_cb; 1744 iface_obj->send_action = fst_hostapd_send_action_cb; 1745 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb; 1746 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb; 1747 iface_obj->get_peer_first = fst_hostapd_get_peer_first; 1748 iface_obj->get_peer_next = fst_hostapd_get_peer_next; 1749 } 1750 1751 #endif /* CONFIG_FST */ 1752 1753 #ifdef CONFIG_OWE 1754 1755 static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx) 1756 { 1757 struct hostapd_data *hapd = ctx; 1758 size_t i; 1759 1760 for (i = 0; i < iface->num_bss; i++) { 1761 struct hostapd_data *bss = iface->bss[i]; 1762 1763 if (os_strcmp(hapd->conf->owe_transition_ifname, 1764 bss->conf->iface) != 0) 1765 continue; 1766 1767 wpa_printf(MSG_DEBUG, 1768 "OWE: ifname=%s found transition mode ifname=%s BSSID " 1769 MACSTR " SSID %s", 1770 hapd->conf->iface, bss->conf->iface, 1771 MAC2STR(bss->own_addr), 1772 wpa_ssid_txt(bss->conf->ssid.ssid, 1773 bss->conf->ssid.ssid_len)); 1774 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len || 1775 is_zero_ether_addr(bss->own_addr)) 1776 continue; 1777 1778 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr, 1779 ETH_ALEN); 1780 os_memcpy(hapd->conf->owe_transition_ssid, 1781 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len); 1782 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len; 1783 wpa_printf(MSG_DEBUG, 1784 "OWE: Copied transition mode information"); 1785 return 1; 1786 } 1787 1788 return 0; 1789 } 1790 1791 1792 int hostapd_owe_trans_get_info(struct hostapd_data *hapd) 1793 { 1794 if (hapd->conf->owe_transition_ssid_len > 0 && 1795 !is_zero_ether_addr(hapd->conf->owe_transition_bssid)) 1796 return 0; 1797 1798 /* Find transition mode SSID/BSSID information from a BSS operated by 1799 * this hostapd instance. */ 1800 if (!hapd->iface->interfaces || 1801 !hapd->iface->interfaces->for_each_interface) 1802 return hostapd_owe_iface_iter(hapd->iface, hapd); 1803 else 1804 return hapd->iface->interfaces->for_each_interface( 1805 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd); 1806 } 1807 1808 1809 static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx) 1810 { 1811 size_t i; 1812 1813 for (i = 0; i < iface->num_bss; i++) { 1814 struct hostapd_data *bss = iface->bss[i]; 1815 int res; 1816 1817 if (!bss->conf->owe_transition_ifname[0]) 1818 continue; 1819 res = hostapd_owe_trans_get_info(bss); 1820 if (res == 0) 1821 continue; 1822 wpa_printf(MSG_DEBUG, 1823 "OWE: Matching transition mode interface enabled - update beacon data for %s", 1824 bss->conf->iface); 1825 ieee802_11_set_beacon(bss); 1826 } 1827 1828 return 0; 1829 } 1830 1831 #endif /* CONFIG_OWE */ 1832 1833 1834 static void hostapd_owe_update_trans(struct hostapd_iface *iface) 1835 { 1836 #ifdef CONFIG_OWE 1837 /* Check whether the enabled BSS can complete OWE transition mode 1838 * configuration for any pending interface. */ 1839 if (!iface->interfaces || 1840 !iface->interfaces->for_each_interface) 1841 hostapd_owe_iface_iter2(iface, NULL); 1842 else 1843 iface->interfaces->for_each_interface( 1844 iface->interfaces, hostapd_owe_iface_iter2, NULL); 1845 #endif /* CONFIG_OWE */ 1846 } 1847 1848 1849 static void hostapd_interface_setup_failure_handler(void *eloop_ctx, 1850 void *timeout_ctx) 1851 { 1852 struct hostapd_iface *iface = eloop_ctx; 1853 struct hostapd_data *hapd; 1854 1855 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0]) 1856 return; 1857 hapd = iface->bss[0]; 1858 if (hapd->setup_complete_cb) 1859 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx); 1860 } 1861 1862 1863 static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface, 1864 int err) 1865 { 1866 struct hostapd_data *hapd = iface->bss[0]; 1867 size_t j; 1868 u8 *prev_addr; 1869 int delay_apply_cfg = 0; 1870 int res_dfs_offload = 0; 1871 1872 if (err) 1873 goto fail; 1874 1875 wpa_printf(MSG_DEBUG, "Completing interface initialization"); 1876 if (iface->conf->channel) { 1877 #ifdef NEED_AP_MLME 1878 int res; 1879 #endif /* NEED_AP_MLME */ 1880 1881 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel); 1882 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d " 1883 "Frequency: %d MHz", 1884 hostapd_hw_mode_txt(iface->conf->hw_mode), 1885 iface->conf->channel, iface->freq); 1886 1887 #ifdef NEED_AP_MLME 1888 /* Handle DFS only if it is not offloaded to the driver */ 1889 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) { 1890 /* Check DFS */ 1891 res = hostapd_handle_dfs(iface); 1892 if (res <= 0) { 1893 if (res < 0) 1894 goto fail; 1895 return res; 1896 } 1897 } else { 1898 /* If DFS is offloaded to the driver */ 1899 res_dfs_offload = hostapd_handle_dfs_offload(iface); 1900 if (res_dfs_offload <= 0) { 1901 if (res_dfs_offload < 0) 1902 goto fail; 1903 } else { 1904 wpa_printf(MSG_DEBUG, 1905 "Proceed with AP/channel setup"); 1906 /* 1907 * If this is a DFS channel, move to completing 1908 * AP setup. 1909 */ 1910 if (res_dfs_offload == 1) 1911 goto dfs_offload; 1912 /* Otherwise fall through. */ 1913 } 1914 } 1915 #endif /* NEED_AP_MLME */ 1916 1917 #ifdef CONFIG_MESH 1918 if (iface->mconf != NULL) { 1919 wpa_printf(MSG_DEBUG, 1920 "%s: Mesh configuration will be applied while joining the mesh network", 1921 iface->bss[0]->conf->iface); 1922 delay_apply_cfg = 1; 1923 } 1924 #endif /* CONFIG_MESH */ 1925 1926 if (!delay_apply_cfg && 1927 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq, 1928 hapd->iconf->channel, 1929 hapd->iconf->ieee80211n, 1930 hapd->iconf->ieee80211ac, 1931 hapd->iconf->ieee80211ax, 1932 hapd->iconf->secondary_channel, 1933 hostapd_get_oper_chwidth(hapd->iconf), 1934 hostapd_get_oper_centr_freq_seg0_idx( 1935 hapd->iconf), 1936 hostapd_get_oper_centr_freq_seg1_idx( 1937 hapd->iconf))) { 1938 wpa_printf(MSG_ERROR, "Could not set channel for " 1939 "kernel driver"); 1940 goto fail; 1941 } 1942 } 1943 1944 if (iface->current_mode) { 1945 if (hostapd_prepare_rates(iface, iface->current_mode)) { 1946 wpa_printf(MSG_ERROR, "Failed to prepare rates " 1947 "table."); 1948 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211, 1949 HOSTAPD_LEVEL_WARNING, 1950 "Failed to prepare rates table."); 1951 goto fail; 1952 } 1953 } 1954 1955 if (hapd->iconf->rts_threshold >= -1 && 1956 hostapd_set_rts(hapd, hapd->iconf->rts_threshold) && 1957 hapd->iconf->rts_threshold >= -1) { 1958 wpa_printf(MSG_ERROR, "Could not set RTS threshold for " 1959 "kernel driver"); 1960 goto fail; 1961 } 1962 1963 if (hapd->iconf->fragm_threshold >= -1 && 1964 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold) && 1965 hapd->iconf->fragm_threshold != -1) { 1966 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold " 1967 "for kernel driver"); 1968 goto fail; 1969 } 1970 1971 prev_addr = hapd->own_addr; 1972 1973 for (j = 0; j < iface->num_bss; j++) { 1974 hapd = iface->bss[j]; 1975 if (j) 1976 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN); 1977 if (hostapd_setup_bss(hapd, j == 0)) { 1978 for (;;) { 1979 hapd = iface->bss[j]; 1980 hostapd_bss_deinit_no_free(hapd); 1981 hostapd_free_hapd_data(hapd); 1982 if (j == 0) 1983 break; 1984 j--; 1985 } 1986 goto fail; 1987 } 1988 if (is_zero_ether_addr(hapd->conf->bssid)) 1989 prev_addr = hapd->own_addr; 1990 } 1991 hapd = iface->bss[0]; 1992 1993 hostapd_tx_queue_params(iface); 1994 1995 ap_list_init(iface); 1996 1997 hostapd_set_acl(hapd); 1998 1999 if (hostapd_driver_commit(hapd) < 0) { 2000 wpa_printf(MSG_ERROR, "%s: Failed to commit driver " 2001 "configuration", __func__); 2002 goto fail; 2003 } 2004 2005 /* 2006 * WPS UPnP module can be initialized only when the "upnp_iface" is up. 2007 * If "interface" and "upnp_iface" are the same (e.g., non-bridge 2008 * mode), the interface is up only after driver_commit, so initialize 2009 * WPS after driver_commit. 2010 */ 2011 for (j = 0; j < iface->num_bss; j++) { 2012 if (hostapd_init_wps_complete(iface->bss[j])) 2013 goto fail; 2014 } 2015 2016 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && 2017 !res_dfs_offload) { 2018 /* 2019 * If freq is DFS, and DFS is offloaded to the driver, then wait 2020 * for CAC to complete. 2021 */ 2022 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__); 2023 return res_dfs_offload; 2024 } 2025 2026 #ifdef NEED_AP_MLME 2027 dfs_offload: 2028 #endif /* NEED_AP_MLME */ 2029 2030 #ifdef CONFIG_FST 2031 if (hapd->iconf->fst_cfg.group_id[0]) { 2032 struct fst_wpa_obj iface_obj; 2033 2034 fst_hostapd_fill_iface_obj(hapd, &iface_obj); 2035 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr, 2036 &iface_obj, &hapd->iconf->fst_cfg); 2037 if (!iface->fst) { 2038 wpa_printf(MSG_ERROR, "Could not attach to FST %s", 2039 hapd->iconf->fst_cfg.group_id); 2040 goto fail; 2041 } 2042 } 2043 #endif /* CONFIG_FST */ 2044 2045 hostapd_set_state(iface, HAPD_IFACE_ENABLED); 2046 hostapd_owe_update_trans(iface); 2047 airtime_policy_update_init(iface); 2048 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED); 2049 if (hapd->setup_complete_cb) 2050 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx); 2051 2052 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.", 2053 iface->bss[0]->conf->iface); 2054 if (iface->interfaces && iface->interfaces->terminate_on_error > 0) 2055 iface->interfaces->terminate_on_error--; 2056 2057 for (j = 0; j < iface->num_bss; j++) 2058 hostapd_neighbor_set_own_report(iface->bss[j]); 2059 2060 return 0; 2061 2062 fail: 2063 wpa_printf(MSG_ERROR, "Interface initialization failed"); 2064 hostapd_set_state(iface, HAPD_IFACE_DISABLED); 2065 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED); 2066 #ifdef CONFIG_FST 2067 if (iface->fst) { 2068 fst_detach(iface->fst); 2069 iface->fst = NULL; 2070 } 2071 #endif /* CONFIG_FST */ 2072 2073 if (iface->interfaces && iface->interfaces->terminate_on_error) { 2074 eloop_terminate(); 2075 } else if (hapd->setup_complete_cb) { 2076 /* 2077 * Calling hapd->setup_complete_cb directly may cause iface 2078 * deinitialization which may be accessed later by the caller. 2079 */ 2080 eloop_register_timeout(0, 0, 2081 hostapd_interface_setup_failure_handler, 2082 iface, NULL); 2083 } 2084 2085 return -1; 2086 } 2087 2088 2089 /** 2090 * hostapd_setup_interface_complete - Complete interface setup 2091 * 2092 * This function is called when previous steps in the interface setup has been 2093 * completed. This can also start operations, e.g., DFS, that will require 2094 * additional processing before interface is ready to be enabled. Such 2095 * operations will call this function from eloop callbacks when finished. 2096 */ 2097 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err) 2098 { 2099 struct hapd_interfaces *interfaces = iface->interfaces; 2100 struct hostapd_data *hapd = iface->bss[0]; 2101 unsigned int i; 2102 int not_ready_in_sync_ifaces = 0; 2103 2104 if (!iface->need_to_start_in_sync) 2105 return hostapd_setup_interface_complete_sync(iface, err); 2106 2107 if (err) { 2108 wpa_printf(MSG_ERROR, "Interface initialization failed"); 2109 hostapd_set_state(iface, HAPD_IFACE_DISABLED); 2110 iface->need_to_start_in_sync = 0; 2111 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED); 2112 if (interfaces && interfaces->terminate_on_error) 2113 eloop_terminate(); 2114 return -1; 2115 } 2116 2117 if (iface->ready_to_start_in_sync) { 2118 /* Already in ready and waiting. should never happpen */ 2119 return 0; 2120 } 2121 2122 for (i = 0; i < interfaces->count; i++) { 2123 if (interfaces->iface[i]->need_to_start_in_sync && 2124 !interfaces->iface[i]->ready_to_start_in_sync) 2125 not_ready_in_sync_ifaces++; 2126 } 2127 2128 /* 2129 * Check if this is the last interface, if yes then start all the other 2130 * waiting interfaces. If not, add this interface to the waiting list. 2131 */ 2132 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) { 2133 /* 2134 * If this interface went through CAC, do not synchronize, just 2135 * start immediately. 2136 */ 2137 iface->need_to_start_in_sync = 0; 2138 wpa_printf(MSG_INFO, 2139 "%s: Finished CAC - bypass sync and start interface", 2140 iface->bss[0]->conf->iface); 2141 return hostapd_setup_interface_complete_sync(iface, err); 2142 } 2143 2144 if (not_ready_in_sync_ifaces > 1) { 2145 /* need to wait as there are other interfaces still coming up */ 2146 iface->ready_to_start_in_sync = 1; 2147 wpa_printf(MSG_INFO, 2148 "%s: Interface waiting to sync with other interfaces", 2149 iface->bss[0]->conf->iface); 2150 return 0; 2151 } 2152 2153 wpa_printf(MSG_INFO, 2154 "%s: Last interface to sync - starting all interfaces", 2155 iface->bss[0]->conf->iface); 2156 iface->need_to_start_in_sync = 0; 2157 hostapd_setup_interface_complete_sync(iface, err); 2158 for (i = 0; i < interfaces->count; i++) { 2159 if (interfaces->iface[i]->need_to_start_in_sync && 2160 interfaces->iface[i]->ready_to_start_in_sync) { 2161 hostapd_setup_interface_complete_sync( 2162 interfaces->iface[i], 0); 2163 /* Only once the interfaces are sync started */ 2164 interfaces->iface[i]->need_to_start_in_sync = 0; 2165 } 2166 } 2167 2168 return 0; 2169 } 2170 2171 2172 /** 2173 * hostapd_setup_interface - Setup of an interface 2174 * @iface: Pointer to interface data. 2175 * Returns: 0 on success, -1 on failure 2176 * 2177 * Initializes the driver interface, validates the configuration, 2178 * and sets driver parameters based on the configuration. 2179 * Flushes old stations, sets the channel, encryption, 2180 * beacons, and WDS links based on the configuration. 2181 * 2182 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS, 2183 * or DFS operations, this function returns 0 before such operations have been 2184 * completed. The pending operations are registered into eloop and will be 2185 * completed from eloop callbacks. Those callbacks end up calling 2186 * hostapd_setup_interface_complete() once setup has been completed. 2187 */ 2188 int hostapd_setup_interface(struct hostapd_iface *iface) 2189 { 2190 int ret; 2191 2192 ret = setup_interface(iface); 2193 if (ret) { 2194 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.", 2195 iface->bss[0]->conf->iface); 2196 return -1; 2197 } 2198 2199 return 0; 2200 } 2201 2202 2203 /** 2204 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data 2205 * @hapd_iface: Pointer to interface data 2206 * @conf: Pointer to per-interface configuration 2207 * @bss: Pointer to per-BSS configuration for this BSS 2208 * Returns: Pointer to allocated BSS data 2209 * 2210 * This function is used to allocate per-BSS data structure. This data will be 2211 * freed after hostapd_cleanup() is called for it during interface 2212 * deinitialization. 2213 */ 2214 struct hostapd_data * 2215 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface, 2216 struct hostapd_config *conf, 2217 struct hostapd_bss_config *bss) 2218 { 2219 struct hostapd_data *hapd; 2220 2221 hapd = os_zalloc(sizeof(*hapd)); 2222 if (hapd == NULL) 2223 return NULL; 2224 2225 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta; 2226 hapd->iconf = conf; 2227 hapd->conf = bss; 2228 hapd->iface = hapd_iface; 2229 if (conf) 2230 hapd->driver = conf->driver; 2231 hapd->ctrl_sock = -1; 2232 dl_list_init(&hapd->ctrl_dst); 2233 dl_list_init(&hapd->nr_db); 2234 hapd->dhcp_sock = -1; 2235 #ifdef CONFIG_IEEE80211R_AP 2236 dl_list_init(&hapd->l2_queue); 2237 dl_list_init(&hapd->l2_oui_queue); 2238 #endif /* CONFIG_IEEE80211R_AP */ 2239 #ifdef CONFIG_SAE 2240 dl_list_init(&hapd->sae_commit_queue); 2241 #endif /* CONFIG_SAE */ 2242 2243 return hapd; 2244 } 2245 2246 2247 static void hostapd_bss_deinit(struct hostapd_data *hapd) 2248 { 2249 if (!hapd) 2250 return; 2251 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__, 2252 hapd->conf ? hapd->conf->iface : "N/A"); 2253 hostapd_bss_deinit_no_free(hapd); 2254 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED); 2255 #ifdef CONFIG_SQLITE 2256 if (hapd->rad_attr_db) { 2257 sqlite3_close(hapd->rad_attr_db); 2258 hapd->rad_attr_db = NULL; 2259 } 2260 #endif /* CONFIG_SQLITE */ 2261 hostapd_cleanup(hapd); 2262 } 2263 2264 2265 void hostapd_interface_deinit(struct hostapd_iface *iface) 2266 { 2267 int j; 2268 2269 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface); 2270 if (iface == NULL) 2271 return; 2272 2273 hostapd_set_state(iface, HAPD_IFACE_DISABLED); 2274 2275 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL); 2276 iface->wait_channel_update = 0; 2277 2278 #ifdef CONFIG_FST 2279 if (iface->fst) { 2280 fst_detach(iface->fst); 2281 iface->fst = NULL; 2282 } 2283 #endif /* CONFIG_FST */ 2284 2285 for (j = (int) iface->num_bss - 1; j >= 0; j--) { 2286 if (!iface->bss) 2287 break; 2288 hostapd_bss_deinit(iface->bss[j]); 2289 } 2290 2291 #ifdef CONFIG_IEEE80211N 2292 #ifdef NEED_AP_MLME 2293 hostapd_stop_setup_timers(iface); 2294 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL); 2295 #endif /* NEED_AP_MLME */ 2296 #endif /* CONFIG_IEEE80211N */ 2297 } 2298 2299 2300 void hostapd_interface_free(struct hostapd_iface *iface) 2301 { 2302 size_t j; 2303 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface); 2304 for (j = 0; j < iface->num_bss; j++) { 2305 if (!iface->bss) 2306 break; 2307 wpa_printf(MSG_DEBUG, "%s: free hapd %p", 2308 __func__, iface->bss[j]); 2309 os_free(iface->bss[j]); 2310 } 2311 hostapd_cleanup_iface(iface); 2312 } 2313 2314 2315 struct hostapd_iface * hostapd_alloc_iface(void) 2316 { 2317 struct hostapd_iface *hapd_iface; 2318 2319 hapd_iface = os_zalloc(sizeof(*hapd_iface)); 2320 if (!hapd_iface) 2321 return NULL; 2322 2323 dl_list_init(&hapd_iface->sta_seen); 2324 2325 return hapd_iface; 2326 } 2327 2328 2329 /** 2330 * hostapd_init - Allocate and initialize per-interface data 2331 * @config_file: Path to the configuration file 2332 * Returns: Pointer to the allocated interface data or %NULL on failure 2333 * 2334 * This function is used to allocate main data structures for per-interface 2335 * data. The allocated data buffer will be freed by calling 2336 * hostapd_cleanup_iface(). 2337 */ 2338 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces, 2339 const char *config_file) 2340 { 2341 struct hostapd_iface *hapd_iface = NULL; 2342 struct hostapd_config *conf = NULL; 2343 struct hostapd_data *hapd; 2344 size_t i; 2345 2346 hapd_iface = hostapd_alloc_iface(); 2347 if (hapd_iface == NULL) 2348 goto fail; 2349 2350 hapd_iface->config_fname = os_strdup(config_file); 2351 if (hapd_iface->config_fname == NULL) 2352 goto fail; 2353 2354 conf = interfaces->config_read_cb(hapd_iface->config_fname); 2355 if (conf == NULL) 2356 goto fail; 2357 hapd_iface->conf = conf; 2358 2359 hapd_iface->num_bss = conf->num_bss; 2360 hapd_iface->bss = os_calloc(conf->num_bss, 2361 sizeof(struct hostapd_data *)); 2362 if (hapd_iface->bss == NULL) 2363 goto fail; 2364 2365 for (i = 0; i < conf->num_bss; i++) { 2366 hapd = hapd_iface->bss[i] = 2367 hostapd_alloc_bss_data(hapd_iface, conf, 2368 conf->bss[i]); 2369 if (hapd == NULL) 2370 goto fail; 2371 hapd->msg_ctx = hapd; 2372 } 2373 2374 return hapd_iface; 2375 2376 fail: 2377 wpa_printf(MSG_ERROR, "Failed to set up interface with %s", 2378 config_file); 2379 if (conf) 2380 hostapd_config_free(conf); 2381 if (hapd_iface) { 2382 os_free(hapd_iface->config_fname); 2383 os_free(hapd_iface->bss); 2384 wpa_printf(MSG_DEBUG, "%s: free iface %p", 2385 __func__, hapd_iface); 2386 os_free(hapd_iface); 2387 } 2388 return NULL; 2389 } 2390 2391 2392 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname) 2393 { 2394 size_t i, j; 2395 2396 for (i = 0; i < interfaces->count; i++) { 2397 struct hostapd_iface *iface = interfaces->iface[i]; 2398 for (j = 0; j < iface->num_bss; j++) { 2399 struct hostapd_data *hapd = iface->bss[j]; 2400 if (os_strcmp(ifname, hapd->conf->iface) == 0) 2401 return 1; 2402 } 2403 } 2404 2405 return 0; 2406 } 2407 2408 2409 /** 2410 * hostapd_interface_init_bss - Read configuration file and init BSS data 2411 * 2412 * This function is used to parse configuration file for a BSS. This BSS is 2413 * added to an existing interface sharing the same radio (if any) or a new 2414 * interface is created if this is the first interface on a radio. This 2415 * allocate memory for the BSS. No actual driver operations are started. 2416 * 2417 * This is similar to hostapd_interface_init(), but for a case where the 2418 * configuration is used to add a single BSS instead of all BSSes for a radio. 2419 */ 2420 struct hostapd_iface * 2421 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy, 2422 const char *config_fname, int debug) 2423 { 2424 struct hostapd_iface *new_iface = NULL, *iface = NULL; 2425 struct hostapd_data *hapd; 2426 int k; 2427 size_t i, bss_idx; 2428 2429 if (!phy || !*phy) 2430 return NULL; 2431 2432 for (i = 0; i < interfaces->count; i++) { 2433 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) { 2434 iface = interfaces->iface[i]; 2435 break; 2436 } 2437 } 2438 2439 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s", 2440 config_fname, phy, iface ? "" : " --> new PHY"); 2441 if (iface) { 2442 struct hostapd_config *conf; 2443 struct hostapd_bss_config **tmp_conf; 2444 struct hostapd_data **tmp_bss; 2445 struct hostapd_bss_config *bss; 2446 const char *ifname; 2447 2448 /* Add new BSS to existing iface */ 2449 conf = interfaces->config_read_cb(config_fname); 2450 if (conf == NULL) 2451 return NULL; 2452 if (conf->num_bss > 1) { 2453 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config"); 2454 hostapd_config_free(conf); 2455 return NULL; 2456 } 2457 2458 ifname = conf->bss[0]->iface; 2459 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) { 2460 wpa_printf(MSG_ERROR, 2461 "Interface name %s already in use", ifname); 2462 hostapd_config_free(conf); 2463 return NULL; 2464 } 2465 2466 tmp_conf = os_realloc_array( 2467 iface->conf->bss, iface->conf->num_bss + 1, 2468 sizeof(struct hostapd_bss_config *)); 2469 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1, 2470 sizeof(struct hostapd_data *)); 2471 if (tmp_bss) 2472 iface->bss = tmp_bss; 2473 if (tmp_conf) { 2474 iface->conf->bss = tmp_conf; 2475 iface->conf->last_bss = tmp_conf[0]; 2476 } 2477 if (tmp_bss == NULL || tmp_conf == NULL) { 2478 hostapd_config_free(conf); 2479 return NULL; 2480 } 2481 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0]; 2482 iface->conf->num_bss++; 2483 2484 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss); 2485 if (hapd == NULL) { 2486 iface->conf->num_bss--; 2487 hostapd_config_free(conf); 2488 return NULL; 2489 } 2490 iface->conf->last_bss = bss; 2491 iface->bss[iface->num_bss] = hapd; 2492 hapd->msg_ctx = hapd; 2493 2494 bss_idx = iface->num_bss++; 2495 conf->num_bss--; 2496 conf->bss[0] = NULL; 2497 hostapd_config_free(conf); 2498 } else { 2499 /* Add a new iface with the first BSS */ 2500 new_iface = iface = hostapd_init(interfaces, config_fname); 2501 if (!iface) 2502 return NULL; 2503 os_strlcpy(iface->phy, phy, sizeof(iface->phy)); 2504 iface->interfaces = interfaces; 2505 bss_idx = 0; 2506 } 2507 2508 for (k = 0; k < debug; k++) { 2509 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0) 2510 iface->bss[bss_idx]->conf->logger_stdout_level--; 2511 } 2512 2513 if (iface->conf->bss[bss_idx]->iface[0] == '\0' && 2514 !hostapd_drv_none(iface->bss[bss_idx])) { 2515 wpa_printf(MSG_ERROR, "Interface name not specified in %s", 2516 config_fname); 2517 if (new_iface) 2518 hostapd_interface_deinit_free(new_iface); 2519 return NULL; 2520 } 2521 2522 return iface; 2523 } 2524 2525 2526 void hostapd_interface_deinit_free(struct hostapd_iface *iface) 2527 { 2528 const struct wpa_driver_ops *driver; 2529 void *drv_priv; 2530 2531 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface); 2532 if (iface == NULL) 2533 return; 2534 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u", 2535 __func__, (unsigned int) iface->num_bss, 2536 (unsigned int) iface->conf->num_bss); 2537 driver = iface->bss[0]->driver; 2538 drv_priv = iface->bss[0]->drv_priv; 2539 hostapd_interface_deinit(iface); 2540 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit", 2541 __func__, driver, drv_priv); 2542 if (driver && driver->hapd_deinit && drv_priv) { 2543 driver->hapd_deinit(drv_priv); 2544 iface->bss[0]->drv_priv = NULL; 2545 } 2546 hostapd_interface_free(iface); 2547 } 2548 2549 2550 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver, 2551 void *drv_priv, 2552 struct hostapd_iface *hapd_iface) 2553 { 2554 size_t j; 2555 2556 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit", 2557 __func__, driver, drv_priv); 2558 if (driver && driver->hapd_deinit && drv_priv) { 2559 driver->hapd_deinit(drv_priv); 2560 for (j = 0; j < hapd_iface->num_bss; j++) { 2561 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p", 2562 __func__, (int) j, 2563 hapd_iface->bss[j]->drv_priv); 2564 if (hapd_iface->bss[j]->drv_priv == drv_priv) { 2565 hapd_iface->bss[j]->drv_priv = NULL; 2566 hapd_iface->extended_capa = NULL; 2567 hapd_iface->extended_capa_mask = NULL; 2568 hapd_iface->extended_capa_len = 0; 2569 } 2570 } 2571 } 2572 } 2573 2574 2575 int hostapd_enable_iface(struct hostapd_iface *hapd_iface) 2576 { 2577 size_t j; 2578 2579 if (hapd_iface->bss[0]->drv_priv != NULL) { 2580 wpa_printf(MSG_ERROR, "Interface %s already enabled", 2581 hapd_iface->conf->bss[0]->iface); 2582 return -1; 2583 } 2584 2585 wpa_printf(MSG_DEBUG, "Enable interface %s", 2586 hapd_iface->conf->bss[0]->iface); 2587 2588 for (j = 0; j < hapd_iface->num_bss; j++) 2589 hostapd_set_security_params(hapd_iface->conf->bss[j], 1); 2590 if (hostapd_config_check(hapd_iface->conf, 1) < 0) { 2591 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable"); 2592 return -1; 2593 } 2594 2595 if (hapd_iface->interfaces == NULL || 2596 hapd_iface->interfaces->driver_init == NULL || 2597 hapd_iface->interfaces->driver_init(hapd_iface)) 2598 return -1; 2599 2600 if (hostapd_setup_interface(hapd_iface)) { 2601 hostapd_deinit_driver(hapd_iface->bss[0]->driver, 2602 hapd_iface->bss[0]->drv_priv, 2603 hapd_iface); 2604 return -1; 2605 } 2606 2607 return 0; 2608 } 2609 2610 2611 int hostapd_reload_iface(struct hostapd_iface *hapd_iface) 2612 { 2613 size_t j; 2614 2615 wpa_printf(MSG_DEBUG, "Reload interface %s", 2616 hapd_iface->conf->bss[0]->iface); 2617 for (j = 0; j < hapd_iface->num_bss; j++) 2618 hostapd_set_security_params(hapd_iface->conf->bss[j], 1); 2619 if (hostapd_config_check(hapd_iface->conf, 1) < 0) { 2620 wpa_printf(MSG_ERROR, "Updated configuration is invalid"); 2621 return -1; 2622 } 2623 hostapd_clear_old(hapd_iface); 2624 for (j = 0; j < hapd_iface->num_bss; j++) 2625 hostapd_reload_bss(hapd_iface->bss[j]); 2626 2627 return 0; 2628 } 2629 2630 2631 int hostapd_disable_iface(struct hostapd_iface *hapd_iface) 2632 { 2633 size_t j; 2634 const struct wpa_driver_ops *driver; 2635 void *drv_priv; 2636 2637 if (hapd_iface == NULL) 2638 return -1; 2639 2640 if (hapd_iface->bss[0]->drv_priv == NULL) { 2641 wpa_printf(MSG_INFO, "Interface %s already disabled", 2642 hapd_iface->conf->bss[0]->iface); 2643 return -1; 2644 } 2645 2646 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED); 2647 driver = hapd_iface->bss[0]->driver; 2648 drv_priv = hapd_iface->bss[0]->drv_priv; 2649 2650 hapd_iface->driver_ap_teardown = 2651 !!(hapd_iface->drv_flags & 2652 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT); 2653 2654 #ifdef NEED_AP_MLME 2655 for (j = 0; j < hapd_iface->num_bss; j++) 2656 hostapd_cleanup_cs_params(hapd_iface->bss[j]); 2657 #endif /* NEED_AP_MLME */ 2658 2659 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */ 2660 for (j = 0; j < hapd_iface->num_bss; j++) { 2661 struct hostapd_data *hapd = hapd_iface->bss[j]; 2662 hostapd_bss_deinit_no_free(hapd); 2663 hostapd_free_hapd_data(hapd); 2664 } 2665 2666 hostapd_deinit_driver(driver, drv_priv, hapd_iface); 2667 2668 /* From hostapd_cleanup_iface: These were initialized in 2669 * hostapd_setup_interface and hostapd_setup_interface_complete 2670 */ 2671 hostapd_cleanup_iface_partial(hapd_iface); 2672 2673 wpa_printf(MSG_DEBUG, "Interface %s disabled", 2674 hapd_iface->bss[0]->conf->iface); 2675 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED); 2676 return 0; 2677 } 2678 2679 2680 static struct hostapd_iface * 2681 hostapd_iface_alloc(struct hapd_interfaces *interfaces) 2682 { 2683 struct hostapd_iface **iface, *hapd_iface; 2684 2685 iface = os_realloc_array(interfaces->iface, interfaces->count + 1, 2686 sizeof(struct hostapd_iface *)); 2687 if (iface == NULL) 2688 return NULL; 2689 interfaces->iface = iface; 2690 hapd_iface = interfaces->iface[interfaces->count] = 2691 hostapd_alloc_iface(); 2692 if (hapd_iface == NULL) { 2693 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for " 2694 "the interface", __func__); 2695 return NULL; 2696 } 2697 interfaces->count++; 2698 hapd_iface->interfaces = interfaces; 2699 2700 return hapd_iface; 2701 } 2702 2703 2704 static struct hostapd_config * 2705 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname, 2706 const char *ctrl_iface, const char *driver) 2707 { 2708 struct hostapd_bss_config *bss; 2709 struct hostapd_config *conf; 2710 2711 /* Allocates memory for bss and conf */ 2712 conf = hostapd_config_defaults(); 2713 if (conf == NULL) { 2714 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for " 2715 "configuration", __func__); 2716 return NULL; 2717 } 2718 2719 if (driver) { 2720 int j; 2721 2722 for (j = 0; wpa_drivers[j]; j++) { 2723 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) { 2724 conf->driver = wpa_drivers[j]; 2725 goto skip; 2726 } 2727 } 2728 2729 wpa_printf(MSG_ERROR, 2730 "Invalid/unknown driver '%s' - registering the default driver", 2731 driver); 2732 } 2733 2734 conf->driver = wpa_drivers[0]; 2735 if (conf->driver == NULL) { 2736 wpa_printf(MSG_ERROR, "No driver wrappers registered!"); 2737 hostapd_config_free(conf); 2738 return NULL; 2739 } 2740 2741 skip: 2742 bss = conf->last_bss = conf->bss[0]; 2743 2744 os_strlcpy(bss->iface, ifname, sizeof(bss->iface)); 2745 bss->ctrl_interface = os_strdup(ctrl_iface); 2746 if (bss->ctrl_interface == NULL) { 2747 hostapd_config_free(conf); 2748 return NULL; 2749 } 2750 2751 /* Reading configuration file skipped, will be done in SET! 2752 * From reading the configuration till the end has to be done in 2753 * SET 2754 */ 2755 return conf; 2756 } 2757 2758 2759 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface, 2760 struct hostapd_config *conf) 2761 { 2762 size_t i; 2763 struct hostapd_data *hapd; 2764 2765 hapd_iface->bss = os_calloc(conf->num_bss, 2766 sizeof(struct hostapd_data *)); 2767 if (hapd_iface->bss == NULL) 2768 return -1; 2769 2770 for (i = 0; i < conf->num_bss; i++) { 2771 hapd = hapd_iface->bss[i] = 2772 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]); 2773 if (hapd == NULL) { 2774 while (i > 0) { 2775 i--; 2776 os_free(hapd_iface->bss[i]); 2777 hapd_iface->bss[i] = NULL; 2778 } 2779 os_free(hapd_iface->bss); 2780 hapd_iface->bss = NULL; 2781 return -1; 2782 } 2783 hapd->msg_ctx = hapd; 2784 } 2785 2786 hapd_iface->conf = conf; 2787 hapd_iface->num_bss = conf->num_bss; 2788 2789 return 0; 2790 } 2791 2792 2793 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf) 2794 { 2795 struct hostapd_config *conf = NULL; 2796 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL; 2797 struct hostapd_data *hapd; 2798 char *ptr; 2799 size_t i, j; 2800 const char *conf_file = NULL, *phy_name = NULL; 2801 2802 if (os_strncmp(buf, "bss_config=", 11) == 0) { 2803 char *pos; 2804 phy_name = buf + 11; 2805 pos = os_strchr(phy_name, ':'); 2806 if (!pos) 2807 return -1; 2808 *pos++ = '\0'; 2809 conf_file = pos; 2810 if (!os_strlen(conf_file)) 2811 return -1; 2812 2813 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name, 2814 conf_file, 0); 2815 if (!hapd_iface) 2816 return -1; 2817 for (j = 0; j < interfaces->count; j++) { 2818 if (interfaces->iface[j] == hapd_iface) 2819 break; 2820 } 2821 if (j == interfaces->count) { 2822 struct hostapd_iface **tmp; 2823 tmp = os_realloc_array(interfaces->iface, 2824 interfaces->count + 1, 2825 sizeof(struct hostapd_iface *)); 2826 if (!tmp) { 2827 hostapd_interface_deinit_free(hapd_iface); 2828 return -1; 2829 } 2830 interfaces->iface = tmp; 2831 interfaces->iface[interfaces->count++] = hapd_iface; 2832 new_iface = hapd_iface; 2833 } 2834 2835 if (new_iface) { 2836 if (interfaces->driver_init(hapd_iface)) 2837 goto fail; 2838 2839 if (hostapd_setup_interface(hapd_iface)) { 2840 hostapd_deinit_driver( 2841 hapd_iface->bss[0]->driver, 2842 hapd_iface->bss[0]->drv_priv, 2843 hapd_iface); 2844 goto fail; 2845 } 2846 } else { 2847 /* Assign new BSS with bss[0]'s driver info */ 2848 hapd = hapd_iface->bss[hapd_iface->num_bss - 1]; 2849 hapd->driver = hapd_iface->bss[0]->driver; 2850 hapd->drv_priv = hapd_iface->bss[0]->drv_priv; 2851 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr, 2852 ETH_ALEN); 2853 2854 if (start_ctrl_iface_bss(hapd) < 0 || 2855 (hapd_iface->state == HAPD_IFACE_ENABLED && 2856 hostapd_setup_bss(hapd, -1))) { 2857 hostapd_cleanup(hapd); 2858 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL; 2859 hapd_iface->conf->num_bss--; 2860 hapd_iface->num_bss--; 2861 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s", 2862 __func__, hapd, hapd->conf->iface); 2863 hostapd_config_free_bss(hapd->conf); 2864 hapd->conf = NULL; 2865 os_free(hapd); 2866 return -1; 2867 } 2868 } 2869 hostapd_owe_update_trans(hapd_iface); 2870 return 0; 2871 } 2872 2873 ptr = os_strchr(buf, ' '); 2874 if (ptr == NULL) 2875 return -1; 2876 *ptr++ = '\0'; 2877 2878 if (os_strncmp(ptr, "config=", 7) == 0) 2879 conf_file = ptr + 7; 2880 2881 for (i = 0; i < interfaces->count; i++) { 2882 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface, 2883 buf)) { 2884 wpa_printf(MSG_INFO, "Cannot add interface - it " 2885 "already exists"); 2886 return -1; 2887 } 2888 } 2889 2890 hapd_iface = hostapd_iface_alloc(interfaces); 2891 if (hapd_iface == NULL) { 2892 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory " 2893 "for interface", __func__); 2894 goto fail; 2895 } 2896 new_iface = hapd_iface; 2897 2898 if (conf_file && interfaces->config_read_cb) { 2899 conf = interfaces->config_read_cb(conf_file); 2900 if (conf && conf->bss) 2901 os_strlcpy(conf->bss[0]->iface, buf, 2902 sizeof(conf->bss[0]->iface)); 2903 } else { 2904 char *driver = os_strchr(ptr, ' '); 2905 2906 if (driver) 2907 *driver++ = '\0'; 2908 conf = hostapd_config_alloc(interfaces, buf, ptr, driver); 2909 } 2910 2911 if (conf == NULL || conf->bss == NULL) { 2912 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory " 2913 "for configuration", __func__); 2914 goto fail; 2915 } 2916 2917 if (hostapd_data_alloc(hapd_iface, conf) < 0) { 2918 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory " 2919 "for hostapd", __func__); 2920 goto fail; 2921 } 2922 conf = NULL; 2923 2924 if (start_ctrl_iface(hapd_iface) < 0) 2925 goto fail; 2926 2927 wpa_printf(MSG_INFO, "Add interface '%s'", 2928 hapd_iface->conf->bss[0]->iface); 2929 2930 return 0; 2931 2932 fail: 2933 if (conf) 2934 hostapd_config_free(conf); 2935 if (hapd_iface) { 2936 if (hapd_iface->bss) { 2937 for (i = 0; i < hapd_iface->num_bss; i++) { 2938 hapd = hapd_iface->bss[i]; 2939 if (!hapd) 2940 continue; 2941 if (hapd_iface->interfaces && 2942 hapd_iface->interfaces->ctrl_iface_deinit) 2943 hapd_iface->interfaces-> 2944 ctrl_iface_deinit(hapd); 2945 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)", 2946 __func__, hapd_iface->bss[i], 2947 hapd->conf->iface); 2948 hostapd_cleanup(hapd); 2949 os_free(hapd); 2950 hapd_iface->bss[i] = NULL; 2951 } 2952 os_free(hapd_iface->bss); 2953 hapd_iface->bss = NULL; 2954 } 2955 if (new_iface) { 2956 interfaces->count--; 2957 interfaces->iface[interfaces->count] = NULL; 2958 } 2959 hostapd_cleanup_iface(hapd_iface); 2960 } 2961 return -1; 2962 } 2963 2964 2965 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx) 2966 { 2967 size_t i; 2968 2969 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface); 2970 2971 /* Remove hostapd_data only if it has already been initialized */ 2972 if (idx < iface->num_bss) { 2973 struct hostapd_data *hapd = iface->bss[idx]; 2974 2975 hostapd_bss_deinit(hapd); 2976 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)", 2977 __func__, hapd, hapd->conf->iface); 2978 hostapd_config_free_bss(hapd->conf); 2979 hapd->conf = NULL; 2980 os_free(hapd); 2981 2982 iface->num_bss--; 2983 2984 for (i = idx; i < iface->num_bss; i++) 2985 iface->bss[i] = iface->bss[i + 1]; 2986 } else { 2987 hostapd_config_free_bss(iface->conf->bss[idx]); 2988 iface->conf->bss[idx] = NULL; 2989 } 2990 2991 iface->conf->num_bss--; 2992 for (i = idx; i < iface->conf->num_bss; i++) 2993 iface->conf->bss[i] = iface->conf->bss[i + 1]; 2994 2995 return 0; 2996 } 2997 2998 2999 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf) 3000 { 3001 struct hostapd_iface *hapd_iface; 3002 size_t i, j, k = 0; 3003 3004 for (i = 0; i < interfaces->count; i++) { 3005 hapd_iface = interfaces->iface[i]; 3006 if (hapd_iface == NULL) 3007 return -1; 3008 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) { 3009 wpa_printf(MSG_INFO, "Remove interface '%s'", buf); 3010 hapd_iface->driver_ap_teardown = 3011 !!(hapd_iface->drv_flags & 3012 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT); 3013 3014 hostapd_interface_deinit_free(hapd_iface); 3015 k = i; 3016 while (k < (interfaces->count - 1)) { 3017 interfaces->iface[k] = 3018 interfaces->iface[k + 1]; 3019 k++; 3020 } 3021 interfaces->count--; 3022 return 0; 3023 } 3024 3025 for (j = 0; j < hapd_iface->conf->num_bss; j++) { 3026 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) { 3027 hapd_iface->driver_ap_teardown = 3028 !(hapd_iface->drv_flags & 3029 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT); 3030 return hostapd_remove_bss(hapd_iface, j); 3031 } 3032 } 3033 } 3034 return -1; 3035 } 3036 3037 3038 /** 3039 * hostapd_new_assoc_sta - Notify that a new station associated with the AP 3040 * @hapd: Pointer to BSS data 3041 * @sta: Pointer to the associated STA data 3042 * @reassoc: 1 to indicate this was a re-association; 0 = first association 3043 * 3044 * This function will be called whenever a station associates with the AP. It 3045 * can be called from ieee802_11.c for drivers that export MLME to hostapd and 3046 * from drv_callbacks.c based on driver events for drivers that take care of 3047 * management frames (IEEE 802.11 authentication and association) internally. 3048 */ 3049 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta, 3050 int reassoc) 3051 { 3052 if (hapd->tkip_countermeasures) { 3053 hostapd_drv_sta_deauth(hapd, sta->addr, 3054 WLAN_REASON_MICHAEL_MIC_FAILURE); 3055 return; 3056 } 3057 3058 hostapd_prune_associations(hapd, sta->addr); 3059 ap_sta_clear_disconnect_timeouts(hapd, sta); 3060 3061 /* IEEE 802.11F (IAPP) */ 3062 if (hapd->conf->ieee802_11f) 3063 iapp_new_station(hapd->iapp, sta); 3064 3065 #ifdef CONFIG_P2P 3066 if (sta->p2p_ie == NULL && !sta->no_p2p_set) { 3067 sta->no_p2p_set = 1; 3068 hapd->num_sta_no_p2p++; 3069 if (hapd->num_sta_no_p2p == 1) 3070 hostapd_p2p_non_p2p_sta_connected(hapd); 3071 } 3072 #endif /* CONFIG_P2P */ 3073 3074 airtime_policy_new_sta(hapd, sta); 3075 3076 /* Start accounting here, if IEEE 802.1X and WPA are not used. 3077 * IEEE 802.1X/WPA code will start accounting after the station has 3078 * been authorized. */ 3079 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) { 3080 ap_sta_set_authorized(hapd, sta, 1); 3081 os_get_reltime(&sta->connected_time); 3082 accounting_sta_start(hapd, sta); 3083 } 3084 3085 /* Start IEEE 802.1X authentication process for new stations */ 3086 ieee802_1x_new_station(hapd, sta); 3087 if (reassoc) { 3088 if (sta->auth_alg != WLAN_AUTH_FT && 3089 sta->auth_alg != WLAN_AUTH_FILS_SK && 3090 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS && 3091 sta->auth_alg != WLAN_AUTH_FILS_PK && 3092 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) 3093 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH); 3094 } else 3095 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm); 3096 3097 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) { 3098 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) { 3099 wpa_printf(MSG_DEBUG, 3100 "%s: %s: canceled wired ap_handle_timer timeout for " 3101 MACSTR, 3102 hapd->conf->iface, __func__, 3103 MAC2STR(sta->addr)); 3104 } 3105 } else if (!(hapd->iface->drv_flags & 3106 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) { 3107 wpa_printf(MSG_DEBUG, 3108 "%s: %s: reschedule ap_handle_timer timeout for " 3109 MACSTR " (%d seconds - ap_max_inactivity)", 3110 hapd->conf->iface, __func__, MAC2STR(sta->addr), 3111 hapd->conf->ap_max_inactivity); 3112 eloop_cancel_timeout(ap_handle_timer, hapd, sta); 3113 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0, 3114 ap_handle_timer, hapd, sta); 3115 } 3116 3117 #ifdef CONFIG_MACSEC 3118 if (hapd->conf->wpa_key_mgmt == WPA_KEY_MGMT_NONE && 3119 hapd->conf->mka_psk_set) 3120 ieee802_1x_create_preshared_mka_hapd(hapd, sta); 3121 else 3122 ieee802_1x_alloc_kay_sm_hapd(hapd, sta); 3123 #endif /* CONFIG_MACSEC */ 3124 } 3125 3126 3127 const char * hostapd_state_text(enum hostapd_iface_state s) 3128 { 3129 switch (s) { 3130 case HAPD_IFACE_UNINITIALIZED: 3131 return "UNINITIALIZED"; 3132 case HAPD_IFACE_DISABLED: 3133 return "DISABLED"; 3134 case HAPD_IFACE_COUNTRY_UPDATE: 3135 return "COUNTRY_UPDATE"; 3136 case HAPD_IFACE_ACS: 3137 return "ACS"; 3138 case HAPD_IFACE_HT_SCAN: 3139 return "HT_SCAN"; 3140 case HAPD_IFACE_DFS: 3141 return "DFS"; 3142 case HAPD_IFACE_ENABLED: 3143 return "ENABLED"; 3144 } 3145 3146 return "UNKNOWN"; 3147 } 3148 3149 3150 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s) 3151 { 3152 wpa_printf(MSG_INFO, "%s: interface state %s->%s", 3153 iface->conf ? iface->conf->bss[0]->iface : "N/A", 3154 hostapd_state_text(iface->state), hostapd_state_text(s)); 3155 iface->state = s; 3156 } 3157 3158 3159 int hostapd_csa_in_progress(struct hostapd_iface *iface) 3160 { 3161 unsigned int i; 3162 3163 for (i = 0; i < iface->num_bss; i++) 3164 if (iface->bss[i]->csa_in_progress) 3165 return 1; 3166 return 0; 3167 } 3168 3169 3170 #ifdef NEED_AP_MLME 3171 3172 static void free_beacon_data(struct beacon_data *beacon) 3173 { 3174 os_free(beacon->head); 3175 beacon->head = NULL; 3176 os_free(beacon->tail); 3177 beacon->tail = NULL; 3178 os_free(beacon->probe_resp); 3179 beacon->probe_resp = NULL; 3180 os_free(beacon->beacon_ies); 3181 beacon->beacon_ies = NULL; 3182 os_free(beacon->proberesp_ies); 3183 beacon->proberesp_ies = NULL; 3184 os_free(beacon->assocresp_ies); 3185 beacon->assocresp_ies = NULL; 3186 } 3187 3188 3189 static int hostapd_build_beacon_data(struct hostapd_data *hapd, 3190 struct beacon_data *beacon) 3191 { 3192 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra; 3193 struct wpa_driver_ap_params params; 3194 int ret; 3195 3196 os_memset(beacon, 0, sizeof(*beacon)); 3197 ret = ieee802_11_build_ap_params(hapd, ¶ms); 3198 if (ret < 0) 3199 return ret; 3200 3201 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra, 3202 &proberesp_extra, 3203 &assocresp_extra); 3204 if (ret) 3205 goto free_ap_params; 3206 3207 ret = -1; 3208 beacon->head = os_memdup(params.head, params.head_len); 3209 if (!beacon->head) 3210 goto free_ap_extra_ies; 3211 3212 beacon->head_len = params.head_len; 3213 3214 beacon->tail = os_memdup(params.tail, params.tail_len); 3215 if (!beacon->tail) 3216 goto free_beacon; 3217 3218 beacon->tail_len = params.tail_len; 3219 3220 if (params.proberesp != NULL) { 3221 beacon->probe_resp = os_memdup(params.proberesp, 3222 params.proberesp_len); 3223 if (!beacon->probe_resp) 3224 goto free_beacon; 3225 3226 beacon->probe_resp_len = params.proberesp_len; 3227 } 3228 3229 /* copy the extra ies */ 3230 if (beacon_extra) { 3231 beacon->beacon_ies = os_memdup(beacon_extra->buf, 3232 wpabuf_len(beacon_extra)); 3233 if (!beacon->beacon_ies) 3234 goto free_beacon; 3235 3236 beacon->beacon_ies_len = wpabuf_len(beacon_extra); 3237 } 3238 3239 if (proberesp_extra) { 3240 beacon->proberesp_ies = os_memdup(proberesp_extra->buf, 3241 wpabuf_len(proberesp_extra)); 3242 if (!beacon->proberesp_ies) 3243 goto free_beacon; 3244 3245 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra); 3246 } 3247 3248 if (assocresp_extra) { 3249 beacon->assocresp_ies = os_memdup(assocresp_extra->buf, 3250 wpabuf_len(assocresp_extra)); 3251 if (!beacon->assocresp_ies) 3252 goto free_beacon; 3253 3254 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra); 3255 } 3256 3257 ret = 0; 3258 free_beacon: 3259 /* if the function fails, the caller should not free beacon data */ 3260 if (ret) 3261 free_beacon_data(beacon); 3262 3263 free_ap_extra_ies: 3264 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra, 3265 assocresp_extra); 3266 free_ap_params: 3267 ieee802_11_free_ap_params(¶ms); 3268 return ret; 3269 } 3270 3271 3272 /* 3273 * TODO: This flow currently supports only changing channel and width within 3274 * the same hw_mode. Any other changes to MAC parameters or provided settings 3275 * are not supported. 3276 */ 3277 static int hostapd_change_config_freq(struct hostapd_data *hapd, 3278 struct hostapd_config *conf, 3279 struct hostapd_freq_params *params, 3280 struct hostapd_freq_params *old_params) 3281 { 3282 int channel; 3283 u8 seg0, seg1; 3284 struct hostapd_hw_modes *mode; 3285 3286 if (!params->channel) { 3287 /* check if the new channel is supported by hw */ 3288 params->channel = hostapd_hw_get_channel(hapd, params->freq); 3289 } 3290 3291 channel = params->channel; 3292 if (!channel) 3293 return -1; 3294 3295 mode = hapd->iface->current_mode; 3296 3297 /* if a pointer to old_params is provided we save previous state */ 3298 if (old_params && 3299 hostapd_set_freq_params(old_params, conf->hw_mode, 3300 hostapd_hw_get_freq(hapd, conf->channel), 3301 conf->channel, conf->ieee80211n, 3302 conf->ieee80211ac, conf->ieee80211ax, 3303 conf->secondary_channel, 3304 hostapd_get_oper_chwidth(conf), 3305 hostapd_get_oper_centr_freq_seg0_idx(conf), 3306 hostapd_get_oper_centr_freq_seg1_idx(conf), 3307 conf->vht_capab, 3308 mode ? &mode->he_capab[IEEE80211_MODE_AP] : 3309 NULL)) 3310 return -1; 3311 3312 switch (params->bandwidth) { 3313 case 0: 3314 case 20: 3315 case 40: 3316 hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT); 3317 break; 3318 case 80: 3319 if (params->center_freq2) 3320 hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ); 3321 else 3322 hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ); 3323 break; 3324 case 160: 3325 hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ); 3326 break; 3327 default: 3328 return -1; 3329 } 3330 3331 conf->channel = channel; 3332 conf->ieee80211n = params->ht_enabled; 3333 conf->secondary_channel = params->sec_channel_offset; 3334 ieee80211_freq_to_chan(params->center_freq1, 3335 &seg0); 3336 ieee80211_freq_to_chan(params->center_freq2, 3337 &seg1); 3338 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0); 3339 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1); 3340 3341 /* TODO: maybe call here hostapd_config_check here? */ 3342 3343 return 0; 3344 } 3345 3346 3347 static int hostapd_fill_csa_settings(struct hostapd_data *hapd, 3348 struct csa_settings *settings) 3349 { 3350 struct hostapd_iface *iface = hapd->iface; 3351 struct hostapd_freq_params old_freq; 3352 int ret; 3353 u8 chan, bandwidth; 3354 3355 os_memset(&old_freq, 0, sizeof(old_freq)); 3356 if (!iface || !iface->freq || hapd->csa_in_progress) 3357 return -1; 3358 3359 switch (settings->freq_params.bandwidth) { 3360 case 80: 3361 if (settings->freq_params.center_freq2) 3362 bandwidth = CHANWIDTH_80P80MHZ; 3363 else 3364 bandwidth = CHANWIDTH_80MHZ; 3365 break; 3366 case 160: 3367 bandwidth = CHANWIDTH_160MHZ; 3368 break; 3369 default: 3370 bandwidth = CHANWIDTH_USE_HT; 3371 break; 3372 } 3373 3374 if (ieee80211_freq_to_channel_ext( 3375 settings->freq_params.freq, 3376 settings->freq_params.sec_channel_offset, 3377 bandwidth, 3378 &hapd->iface->cs_oper_class, 3379 &chan) == NUM_HOSTAPD_MODES) { 3380 wpa_printf(MSG_DEBUG, 3381 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d, he_enabled=%d)", 3382 settings->freq_params.freq, 3383 settings->freq_params.sec_channel_offset, 3384 settings->freq_params.vht_enabled, 3385 settings->freq_params.he_enabled); 3386 return -1; 3387 } 3388 3389 settings->freq_params.channel = chan; 3390 3391 ret = hostapd_change_config_freq(iface->bss[0], iface->conf, 3392 &settings->freq_params, 3393 &old_freq); 3394 if (ret) 3395 return ret; 3396 3397 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after); 3398 3399 /* change back the configuration */ 3400 hostapd_change_config_freq(iface->bss[0], iface->conf, 3401 &old_freq, NULL); 3402 3403 if (ret) 3404 return ret; 3405 3406 /* set channel switch parameters for csa ie */ 3407 hapd->cs_freq_params = settings->freq_params; 3408 hapd->cs_count = settings->cs_count; 3409 hapd->cs_block_tx = settings->block_tx; 3410 3411 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa); 3412 if (ret) { 3413 free_beacon_data(&settings->beacon_after); 3414 return ret; 3415 } 3416 3417 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon; 3418 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp; 3419 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon; 3420 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp; 3421 3422 return 0; 3423 } 3424 3425 3426 void hostapd_cleanup_cs_params(struct hostapd_data *hapd) 3427 { 3428 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params)); 3429 hapd->cs_count = 0; 3430 hapd->cs_block_tx = 0; 3431 hapd->cs_c_off_beacon = 0; 3432 hapd->cs_c_off_proberesp = 0; 3433 hapd->csa_in_progress = 0; 3434 hapd->cs_c_off_ecsa_beacon = 0; 3435 hapd->cs_c_off_ecsa_proberesp = 0; 3436 } 3437 3438 3439 void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled) 3440 { 3441 if (vht_enabled) 3442 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED; 3443 else 3444 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED; 3445 3446 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211, 3447 HOSTAPD_LEVEL_INFO, "CHAN_SWITCH VHT CONFIG 0x%x", 3448 hapd->iconf->ch_switch_vht_config); 3449 } 3450 3451 3452 int hostapd_switch_channel(struct hostapd_data *hapd, 3453 struct csa_settings *settings) 3454 { 3455 int ret; 3456 3457 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) { 3458 wpa_printf(MSG_INFO, "CSA is not supported"); 3459 return -1; 3460 } 3461 3462 ret = hostapd_fill_csa_settings(hapd, settings); 3463 if (ret) 3464 return ret; 3465 3466 ret = hostapd_drv_switch_channel(hapd, settings); 3467 free_beacon_data(&settings->beacon_csa); 3468 free_beacon_data(&settings->beacon_after); 3469 3470 if (ret) { 3471 /* if we failed, clean cs parameters */ 3472 hostapd_cleanup_cs_params(hapd); 3473 return ret; 3474 } 3475 3476 hapd->csa_in_progress = 1; 3477 return 0; 3478 } 3479 3480 3481 void 3482 hostapd_switch_channel_fallback(struct hostapd_iface *iface, 3483 const struct hostapd_freq_params *freq_params) 3484 { 3485 int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT; 3486 3487 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes"); 3488 3489 if (freq_params->center_freq1) 3490 seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5; 3491 if (freq_params->center_freq2) 3492 seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5; 3493 3494 switch (freq_params->bandwidth) { 3495 case 0: 3496 case 20: 3497 case 40: 3498 bw = CHANWIDTH_USE_HT; 3499 break; 3500 case 80: 3501 if (freq_params->center_freq2) 3502 bw = CHANWIDTH_80P80MHZ; 3503 else 3504 bw = CHANWIDTH_80MHZ; 3505 break; 3506 case 160: 3507 bw = CHANWIDTH_160MHZ; 3508 break; 3509 default: 3510 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d", 3511 freq_params->bandwidth); 3512 break; 3513 } 3514 3515 iface->freq = freq_params->freq; 3516 iface->conf->channel = freq_params->channel; 3517 iface->conf->secondary_channel = freq_params->sec_channel_offset; 3518 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, seg0_idx); 3519 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, seg1_idx); 3520 hostapd_set_oper_chwidth(iface->conf, bw); 3521 iface->conf->ieee80211n = freq_params->ht_enabled; 3522 iface->conf->ieee80211ac = freq_params->vht_enabled; 3523 iface->conf->ieee80211ax = freq_params->he_enabled; 3524 3525 /* 3526 * cs_params must not be cleared earlier because the freq_params 3527 * argument may actually point to one of these. 3528 * These params will be cleared during interface disable below. 3529 */ 3530 hostapd_disable_iface(iface); 3531 hostapd_enable_iface(iface); 3532 } 3533 3534 #endif /* NEED_AP_MLME */ 3535 3536 3537 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces, 3538 const char *ifname) 3539 { 3540 size_t i, j; 3541 3542 for (i = 0; i < interfaces->count; i++) { 3543 struct hostapd_iface *iface = interfaces->iface[i]; 3544 3545 for (j = 0; j < iface->num_bss; j++) { 3546 struct hostapd_data *hapd = iface->bss[j]; 3547 3548 if (os_strcmp(ifname, hapd->conf->iface) == 0) 3549 return hapd; 3550 } 3551 } 3552 3553 return NULL; 3554 } 3555 3556 3557 void hostapd_periodic_iface(struct hostapd_iface *iface) 3558 { 3559 size_t i; 3560 3561 ap_list_timer(iface); 3562 3563 for (i = 0; i < iface->num_bss; i++) { 3564 struct hostapd_data *hapd = iface->bss[i]; 3565 3566 if (!hapd->started) 3567 continue; 3568 3569 #ifndef CONFIG_NO_RADIUS 3570 hostapd_acl_expire(hapd); 3571 #endif /* CONFIG_NO_RADIUS */ 3572 } 3573 } 3574