2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
12 #endif /* CONFIG_SQLITE */
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"
28 #include "accounting.h"
32 #include "ieee802_1x.h"
33 #include "ieee802_11_auth.h"
34 #include "vlan_init.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"
46 #include "ieee802_11.h"
49 #include "dhcp_snoop.h"
50 #include "ndisc_snoop.h"
51 #include "neighbor_db.h"
56 #include "airtime_policy.h"
57 #include "wpa_auth_kay.h"
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,
69 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
70 int (*cb)(struct hostapd_iface *iface,
71 void *ctx), void *ctx)
76 for (i = 0; i < interfaces->count; i++) {
77 ret = cb(interfaces->iface[i], ctx);
86 void hostapd_reconfig_encryption(struct hostapd_data *hapd)
91 hostapd_set_privacy(hapd, 0);
92 hostapd_setup_encryption(hapd->conf->iface, hapd);
96 static void hostapd_reload_bss(struct hostapd_data *hapd)
98 struct hostapd_ssid *ssid;
103 if (hapd->conf->wmm_enabled < 0)
104 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n;
106 #ifndef CONFIG_NO_RADIUS
107 radius_client_reconfig(hapd->radius, hapd->conf->radius);
108 #endif /* CONFIG_NO_RADIUS */
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) {
114 * Force PSK to be derived again since SSID or passphrase may
117 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
119 if (hostapd_setup_wpa_psk(hapd->conf)) {
120 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
121 "after reloading configuration");
124 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
125 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
127 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
129 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
130 hostapd_setup_wpa(hapd);
132 wpa_init_keys(hapd->wpa_auth);
133 } else if (hapd->conf->wpa) {
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);
149 ieee802_11_set_beacon(hapd);
150 hostapd_update_wps(hapd);
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 */
158 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
162 static void hostapd_clear_old(struct hostapd_iface *iface)
167 * Deauthenticate all stations since the new configuration may not
168 * allow them to use the BSS anymore.
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]);
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 */
184 static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
185 struct hostapd_config *oldconf)
189 if (newconf->num_bss != oldconf->num_bss)
192 for (i = 0; i < newconf->num_bss; i++) {
193 if (os_strcmp(newconf->bss[i]->iface,
194 oldconf->bss[i]->iface) != 0)
202 int hostapd_reload_config(struct hostapd_iface *iface)
204 struct hapd_interfaces *interfaces = iface->interfaces;
205 struct hostapd_data *hapd = iface->bss[0];
206 struct hostapd_config *newconf, *oldconf;
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]);
217 if (iface->interfaces == NULL ||
218 iface->interfaces->config_read_cb == NULL)
220 newconf = iface->interfaces->config_read_cb(iface->config_fname);
224 hostapd_clear_old(iface);
226 oldconf = hapd->iconf;
227 if (hostapd_iface_conf_changed(newconf, oldconf)) {
231 wpa_printf(MSG_DEBUG,
232 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
233 fname = os_strdup(iface->config_fname);
235 hostapd_config_free(newconf);
238 hostapd_remove_iface(interfaces, hapd->conf->iface);
239 iface = hostapd_init(interfaces, fname);
241 hostapd_config_free(newconf);
243 wpa_printf(MSG_ERROR,
244 "Failed to initialize interface on config reload");
247 iface->interfaces = interfaces;
248 interfaces->iface[interfaces->count] = iface;
250 res = hostapd_enable_iface(iface);
252 wpa_printf(MSG_ERROR,
253 "Failed to enable interface on config reload");
256 iface->conf = newconf;
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(
272 hostapd_get_oper_centr_freq_seg0_idx(oldconf));
273 hostapd_set_oper_centr_freq_seg1_idx(
275 hostapd_get_oper_centr_freq_seg1_idx(oldconf));
276 hapd->conf = newconf->bss[j];
277 hostapd_reload_bss(hapd);
280 hostapd_config_free(oldconf);
287 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
292 if (!ifname || !hapd->drv_priv)
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)",
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,
308 wpa_printf(MSG_DEBUG, "Failed to clear "
309 "default mgmt encryption keys "
310 "(ifname=%s keyidx=%d)", ifname, i);
314 #endif /* CONFIG_IEEE80211W */
318 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
320 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
325 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
328 struct hostapd_ssid *ssid = &hapd->conf->ssid;
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.");
344 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
346 os_free(hapd->probereq_cb);
347 hapd->probereq_cb = NULL;
348 hapd->num_probereq_cb = 0;
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 */
357 if (!hapd->started) {
358 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
359 __func__, hapd->conf ? hapd->conf->iface : "N/A");
363 hapd->beacon_set_done = 0;
365 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
366 iapp_deinit(hapd->iapp);
368 accounting_deinit(hapd);
369 hostapd_deinit_wpa(hapd);
371 hostapd_acl_deinit(hapd);
372 #ifndef CONFIG_NO_RADIUS
373 radius_client_deinit(hapd->radius);
375 radius_das_deinit(hapd->radius_das);
376 hapd->radius_das = NULL;
377 #endif /* CONFIG_NO_RADIUS */
379 hostapd_deinit_wps(hapd);
380 ieee802_1x_dealloc_kay_sm_hapd(hapd);
382 hostapd_dpp_deinit(hapd);
383 gas_query_ap_deinit(hapd->gas);
384 #endif /* CONFIG_DPP */
386 authsrv_deinit(hapd);
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",
394 hapd->interface_added = 1;
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.
401 hapd->drv_priv = NULL;
405 wpabuf_free(hapd->time_adv);
407 #ifdef CONFIG_INTERWORKING
408 gas_serv_deinit(hapd);
409 #endif /* CONFIG_INTERWORKING */
411 bss_load_update_deinit(hapd);
412 ndisc_snoop_deinit(hapd);
413 dhcp_snoop_deinit(hapd);
414 x_snoop_deinit(hapd);
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 */
424 wpabuf_free(hapd->mesh_pending_auth);
425 hapd->mesh_pending_auth = NULL;
426 #endif /* CONFIG_MESH */
428 hostapd_clean_rrm(hapd);
429 fils_hlp_deinit(hapd);
433 struct hostapd_sae_commit_queue *q;
435 while ((q = dl_list_first(&hapd->sae_commit_queue,
436 struct hostapd_sae_commit_queue,
438 dl_list_del(&q->list);
442 eloop_cancel_timeout(auth_sae_process_commit, hapd, NULL);
443 #endif /* CONFIG_SAE */
448 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
449 * @hapd: Pointer to BSS data
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.
455 static void hostapd_cleanup(struct hostapd_data *hapd)
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);
464 hostapd_free_hapd_data(hapd);
468 static void sta_track_deinit(struct hostapd_iface *iface)
470 struct hostapd_sta_info *info;
472 if (!iface->num_sta_seen)
475 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
477 dl_list_del(&info->list);
478 iface->num_sta_seen--;
484 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
486 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
487 #ifdef CONFIG_IEEE80211N
489 hostapd_stop_setup_timers(iface);
490 #endif /* NEED_AP_MLME */
491 #endif /* CONFIG_IEEE80211N */
492 if (iface->current_mode)
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);
508 * hostapd_cleanup_iface - Complete per-interface cleanup
509 * @iface: Pointer to interface data
511 * This function is called after per-BSS data structures are deinitialized
512 * with hostapd_cleanup().
514 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
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,
521 hostapd_cleanup_iface_partial(iface);
522 hostapd_config_free(iface->conf);
525 os_free(iface->config_fname);
527 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
532 static void hostapd_clear_wep(struct hostapd_data *hapd)
534 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown && hapd->conf) {
535 hostapd_set_privacy(hapd, 0);
536 hostapd_broadcast_wep_clear(hapd);
541 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
545 hostapd_broadcast_wep_set(hapd);
547 if (hapd->conf->ssid.wep.default_len) {
548 hostapd_set_privacy(hapd, 1);
553 * When IEEE 802.1X is not enabled, the driver may need to know how to
554 * set authentication algorithms for static WEP.
556 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
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 "
568 if (hapd->conf->ssid.wep.key[i] &&
569 i == hapd->conf->ssid.wep.idx)
570 hostapd_set_privacy(hapd, 1);
577 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
582 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
585 if (!hapd->iface->driver_ap_teardown) {
586 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
587 "Flushing old station entries");
589 if (hostapd_flush(hapd)) {
590 wpa_msg(hapd->msg_ctx, MSG_WARNING,
591 "Could not connect to kernel driver");
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);
601 hostapd_free_stas(hapd);
607 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
609 hostapd_free_stas(hapd);
610 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
611 hostapd_clear_wep(hapd);
616 * hostapd_validate_bssid_configuration - Validate BSSID configuration
617 * @iface: Pointer to interface data
618 * Returns: 0 on success, -1 on failure
620 * This function is used to validate that the configured BSSIDs are valid.
622 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
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;
629 if (hostapd_drv_none(hapd))
632 if (iface->conf->use_driver_iface_addr)
635 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
637 /* Determine the bits necessary to cover the number of BSSIDs. */
638 for (i--; i; i >>= 1)
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)) {
650 for (i = 0; i < ETH_ALEN; i++) {
652 iface->conf->bss[j]->bssid[i] ^
660 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
666 while (mask[i] != 0) {
676 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
681 os_memset(mask, 0xff, ETH_ALEN);
683 for (i = 5; i > 5 - j; i--)
692 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
693 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
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).");
714 static int mac_in_conf(struct hostapd_config *conf, const void *a)
718 for (i = 0; i < conf->num_bss; i++) {
719 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
728 #ifndef CONFIG_NO_RADIUS
730 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
731 struct radius_das_attrs *attr)
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");
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) !=
747 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
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)
756 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
759 #endif /* CONFIG_IPV6 */
765 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
766 struct radius_das_attrs *attr,
769 struct sta_info *selected, *sta;
776 for (sta = hapd->sta_list; sta; sta = sta->next)
777 sta->radius_das_match = 1;
779 if (attr->sta_addr) {
781 sta = ap_get_sta(hapd, attr->sta_addr);
783 wpa_printf(MSG_DEBUG,
784 "RADIUS DAS: No Calling-Station-Id match");
789 for (sta = hapd->sta_list; sta; sta = sta->next) {
791 sta->radius_das_match = 0;
793 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
796 if (attr->acct_session_id) {
798 if (attr->acct_session_id_len != 16) {
799 wpa_printf(MSG_DEBUG,
800 "RADIUS DAS: Acct-Session-Id cannot match");
805 for (sta = hapd->sta_list; sta; sta = sta->next) {
806 if (!sta->radius_das_match)
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;
817 wpa_printf(MSG_DEBUG,
818 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
821 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
824 if (attr->acct_multi_session_id) {
826 if (attr->acct_multi_session_id_len != 16) {
827 wpa_printf(MSG_DEBUG,
828 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
833 for (sta = hapd->sta_list; sta; sta = sta->next) {
834 if (!sta->radius_das_match)
836 if (!sta->eapol_sm ||
837 !sta->eapol_sm->acct_multi_session_id) {
838 sta->radius_das_match = 0;
841 os_snprintf(buf, sizeof(buf), "%016llX",
843 sta->eapol_sm->acct_multi_session_id);
844 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
846 sta->radius_das_match = 0;
852 wpa_printf(MSG_DEBUG,
853 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
856 wpa_printf(MSG_DEBUG,
857 "RADIUS DAS: Acct-Multi-Session-Id match");
864 for (sta = hapd->sta_list; sta; sta = sta->next) {
867 if (!sta->radius_das_match)
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,
873 sta->radius_das_match = 0;
879 wpa_printf(MSG_DEBUG,
880 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
883 wpa_printf(MSG_DEBUG,
884 "RADIUS DAS: Chargeable-User-Identity match");
887 if (attr->user_name) {
891 for (sta = hapd->sta_list; sta; sta = sta->next) {
895 if (!sta->radius_das_match)
897 identity = ieee802_1x_get_identity(sta->eapol_sm,
900 identity_len != attr->user_name_len ||
901 os_memcmp(identity, attr->user_name, identity_len)
903 sta->radius_das_match = 0;
909 wpa_printf(MSG_DEBUG,
910 "RADIUS DAS: No matches remaining after User-Name check");
913 wpa_printf(MSG_DEBUG,
914 "RADIUS DAS: User-Name match");
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.
923 wpa_printf(MSG_DEBUG,
924 "RADIUS DAS: No session identification attributes included");
929 for (sta = hapd->sta_list; sta; sta = sta->next) {
930 if (sta->radius_das_match) {
943 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
944 struct radius_das_attrs *attr)
948 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
952 static enum radius_das_res
953 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
955 struct hostapd_data *hapd = ctx;
956 struct sta_info *sta;
959 if (hostapd_das_nas_mismatch(hapd, attr))
960 return RADIUS_DAS_NAS_MISMATCH;
962 sta = hostapd_das_find_sta(hapd, attr, &multi);
965 wpa_printf(MSG_DEBUG,
966 "RADIUS DAS: Multiple sessions match - not supported");
967 return RADIUS_DAS_MULTI_SESSION_MATCH;
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;
974 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
975 return RADIUS_DAS_SESSION_NOT_FOUND;
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);
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);
986 return RADIUS_DAS_SUCCESS;
991 static enum radius_das_res
992 hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
994 struct hostapd_data *hapd = ctx;
995 struct sta_info *sta;
998 if (hostapd_das_nas_mismatch(hapd, attr))
999 return RADIUS_DAS_NAS_MISMATCH;
1001 sta = hostapd_das_find_sta(hapd, attr, &multi);
1004 wpa_printf(MSG_DEBUG,
1005 "RADIUS DAS: Multiple sessions match - not supported");
1006 return RADIUS_DAS_MULTI_SESSION_MATCH;
1008 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
1009 return RADIUS_DAS_SESSION_NOT_FOUND;
1012 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
1013 " - CoA", MAC2STR(sta->addr));
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;
1022 hs20_t_c_filtering(hapd, sta, 0);
1025 return RADIUS_DAS_SUCCESS;
1027 #else /* CONFIG_HS20 */
1028 #define hostapd_das_coa NULL
1029 #endif /* CONFIG_HS20 */
1032 #ifdef CONFIG_SQLITE
1034 static int db_table_exists(sqlite3 *db, const char *name)
1038 os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
1039 return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
1043 static int db_table_create_radius_attributes(sqlite3 *db)
1047 "CREATE TABLE radius_attributes("
1048 " id INTEGER PRIMARY KEY,"
1053 "CREATE INDEX idx_sta_reqtype ON radius_attributes(sta,reqtype);";
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);
1066 #endif /* CONFIG_SQLITE */
1068 #endif /* CONFIG_NO_RADIUS */
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
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().
1082 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
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;
1091 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
1092 __func__, hapd, conf->iface, first);
1094 #ifdef EAP_SERVER_TNC
1095 if (conf->tnc && tncs_global_init() < 0) {
1096 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1099 #endif /* EAP_SERVER_TNC */
1101 if (hapd->started) {
1102 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
1103 __func__, conf->iface);
1108 if (!first || first == -1) {
1109 u8 *addr = hapd->own_addr;
1111 if (!is_zero_ether_addr(conf->bssid)) {
1112 /* Allocate the configured BSSID. */
1113 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
1115 if (hostapd_mac_comp(hapd->own_addr,
1116 hapd->iface->bss[0]->own_addr) ==
1118 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1119 "BSSID set to the MAC address of "
1120 "the radio", conf->iface);
1123 } else if (hapd->iconf->use_driver_iface_addr) {
1126 /* Allocate the next available BSSID. */
1128 inc_byte_array(hapd->own_addr, ETH_ALEN);
1129 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
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,
1138 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1139 MACSTR ")", MAC2STR(hapd->own_addr));
1140 hapd->interface_added = 0;
1145 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
1148 if (conf->wmm_enabled < 0)
1149 conf->wmm_enabled = hapd->iconf->ieee80211n;
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 */
1157 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
1158 flush_old_stations = 0;
1159 #endif /* CONFIG_MESH */
1161 if (flush_old_stations)
1162 hostapd_flush_old_stations(hapd,
1163 WLAN_REASON_PREV_AUTH_NOT_VALID);
1164 hostapd_set_privacy(hapd, 0);
1166 hostapd_broadcast_wep_clear(hapd);
1167 if (hostapd_setup_encryption(conf->iface, hapd))
1171 * Fetch the SSID from the system and use it or,
1172 * if one was specified in the config file, verify they
1175 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1177 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1180 if (conf->ssid.ssid_set) {
1182 * If SSID is specified in the config file and it differs
1183 * from what is being used then force installation of the
1186 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1187 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1190 * No SSID in the config file; just use the one we got
1194 conf->ssid.ssid_len = ssid_len;
1195 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1198 if (!hostapd_drv_none(hapd)) {
1199 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1201 conf->iface, MAC2STR(hapd->own_addr),
1202 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
1205 if (hostapd_setup_wpa_psk(conf)) {
1206 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
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");
1218 if (wpa_debug_level <= MSG_MSGDUMP)
1219 conf->radius->msg_dumps = 1;
1220 #ifndef CONFIG_NO_RADIUS
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);
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)
1237 #endif /* CONFIG_SQLITE */
1239 hapd->radius = radius_client_init(hapd, conf->radius);
1240 if (hapd->radius == NULL) {
1241 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
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 "
1268 #endif /* CONFIG_NO_RADIUS */
1270 if (hostapd_acl_init(hapd)) {
1271 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1274 if (hostapd_init_wps(hapd, conf))
1278 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1281 if (hostapd_dpp_init(hapd))
1283 #endif /* CONFIG_DPP */
1285 if (authsrv_init(hapd) < 0)
1288 if (ieee802_1x_init(hapd)) {
1289 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1293 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1296 if (accounting_init(hapd)) {
1297 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
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 "
1308 #ifdef CONFIG_INTERWORKING
1309 if (gas_serv_init(hapd)) {
1310 wpa_printf(MSG_ERROR, "GAS server initialization failed");
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");
1320 #endif /* CONFIG_INTERWORKING */
1322 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1323 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1327 if (conf->proxy_arp) {
1328 if (x_snoop_init(hapd)) {
1329 wpa_printf(MSG_ERROR,
1330 "Generic snooping infrastructure initialization failed");
1334 if (dhcp_snoop_init(hapd)) {
1335 wpa_printf(MSG_ERROR,
1336 "DHCP snooping initialization failed");
1340 if (ndisc_snoop_init(hapd)) {
1341 wpa_printf(MSG_ERROR,
1342 "Neighbor Discovery snooping initialization failed");
1347 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1348 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1352 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1355 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1358 if (hapd->driver && hapd->driver->set_operstate)
1359 hapd->driver->set_operstate(hapd->drv_priv, 1);
1365 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1367 struct hostapd_data *hapd = iface->bss[0];
1369 struct hostapd_tx_queue_params *p;
1372 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
1374 #endif /* CONFIG_MESH */
1376 for (i = 0; i < NUM_TX_QUEUES; i++) {
1377 p = &iface->conf->tx_queue[i];
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 */
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)
1393 struct hostapd_acl_params *acl_params;
1396 acl_params = os_zalloc(sizeof(*acl_params) +
1397 (n_entries * sizeof(acl_params->mac_acl[0])));
1401 for (i = 0; i < n_entries; i++)
1402 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1405 acl_params->acl_policy = accept_acl;
1406 acl_params->num_mac_acl = n_entries;
1408 err = hostapd_drv_set_acl(hapd, acl_params);
1410 os_free(acl_params);
1416 static void hostapd_set_acl(struct hostapd_data *hapd)
1418 struct hostapd_config *conf = hapd->iconf;
1422 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1425 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1427 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1428 conf->bss[0]->num_accept_mac,
1431 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1434 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1436 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1437 conf->bss[0]->num_deny_mac,
1440 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1447 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1449 if (!hapd->iface->interfaces ||
1450 !hapd->iface->interfaces->ctrl_iface_init)
1453 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1454 wpa_printf(MSG_ERROR,
1455 "Failed to setup control interface for %s",
1464 static int start_ctrl_iface(struct hostapd_iface *iface)
1468 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
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",
1485 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1487 struct hostapd_iface *iface = eloop_ctx;
1489 if (!iface->wait_channel_update) {
1490 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1495 * It is possible that the existing channel list is acceptable, so try
1498 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1499 setup_interface2(iface);
1503 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1505 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1508 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1509 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1510 setup_interface2(iface);
1514 static int setup_interface(struct hostapd_iface *iface)
1516 struct hostapd_data *hapd = iface->bss[0];
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
1526 iface->driver_ap_teardown = 0;
1528 if (!iface->phy[0]) {
1529 const char *phy = hostapd_drv_get_radio_name(hapd);
1531 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1532 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1537 * Make sure that all BSSes get configured with a pointer to the same
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;
1545 if (hostapd_validate_bssid_configuration(iface))
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.
1553 if (start_ctrl_iface(iface))
1556 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1557 char country[4], previous_country[4];
1559 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1560 if (hostapd_get_country(hapd, previous_country) < 0)
1561 previous_country[0] = '\0';
1563 os_memcpy(country, hapd->iconf->country, 3);
1565 if (hostapd_set_country(hapd, country) < 0) {
1566 wpa_printf(MSG_ERROR, "Failed to set country code");
1570 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1571 previous_country, country);
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,
1583 return setup_interface2(iface);
1587 static int setup_interface2(struct hostapd_iface *iface)
1589 iface->wait_channel_update = 0;
1591 if (hostapd_get_hw_features(iface)) {
1592 /* Not all drivers support this yet, so continue without hw
1595 int ret = hostapd_select_hw_mode(iface);
1597 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1598 "channel. (%d)", ret);
1602 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1605 ret = hostapd_check_ht_capab(iface);
1609 wpa_printf(MSG_DEBUG, "Interface initialization will "
1610 "be completed in a callback");
1614 if (iface->conf->ieee80211h)
1615 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1617 return hostapd_setup_interface_complete(iface, 0);
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)
1630 static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1632 struct hostapd_data *hapd = ctx;
1634 return hapd->own_addr;
1638 static void fst_hostapd_get_channel_info_cb(void *ctx,
1639 enum hostapd_hw_mode *hw_mode,
1642 struct hostapd_data *hapd = ctx;
1644 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1648 static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1650 struct hostapd_data *hapd = ctx;
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");
1660 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1663 struct hostapd_data *hapd = ctx;
1665 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1666 wpabuf_head(buf), wpabuf_len(buf));
1670 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1672 struct hostapd_data *hapd = ctx;
1673 struct sta_info *sta = ap_get_sta(hapd, addr);
1675 return sta ? sta->mb_ies : NULL;
1679 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1680 const u8 *buf, size_t size)
1682 struct hostapd_data *hapd = ctx;
1683 struct sta_info *sta = ap_get_sta(hapd, addr);
1686 struct mb_ies_info info;
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);
1696 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1699 struct sta_info *s = (struct sta_info *) *get_ctx;
1702 for (; s && !s->mb_ies; s = s->next)
1707 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1717 static const u8 * fst_hostapd_get_peer_first(void *ctx,
1718 struct fst_get_peer_ctx **get_ctx,
1721 struct hostapd_data *hapd = ctx;
1723 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1725 return fst_hostapd_get_sta(get_ctx, mb_only);
1729 static const u8 * fst_hostapd_get_peer_next(void *ctx,
1730 struct fst_get_peer_ctx **get_ctx,
1733 return fst_hostapd_get_sta(get_ctx, mb_only);
1737 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1738 struct fst_wpa_obj *iface_obj)
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;
1751 #endif /* CONFIG_FST */
1755 static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1757 struct hostapd_data *hapd = ctx;
1760 for (i = 0; i < iface->num_bss; i++) {
1761 struct hostapd_data *bss = iface->bss[i];
1763 if (os_strcmp(hapd->conf->owe_transition_ifname,
1764 bss->conf->iface) != 0)
1767 wpa_printf(MSG_DEBUG,
1768 "OWE: ifname=%s found transition mode ifname=%s BSSID "
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))
1778 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
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");
1792 int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1794 if (hapd->conf->owe_transition_ssid_len > 0 &&
1795 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
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);
1804 return hapd->iface->interfaces->for_each_interface(
1805 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1809 static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1813 for (i = 0; i < iface->num_bss; i++) {
1814 struct hostapd_data *bss = iface->bss[i];
1817 if (!bss->conf->owe_transition_ifname[0])
1819 res = hostapd_owe_trans_get_info(bss);
1822 wpa_printf(MSG_DEBUG,
1823 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1825 ieee802_11_set_beacon(bss);
1831 #endif /* CONFIG_OWE */
1834 static void hostapd_owe_update_trans(struct hostapd_iface *iface)
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);
1843 iface->interfaces->for_each_interface(
1844 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1845 #endif /* CONFIG_OWE */
1849 static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1852 struct hostapd_iface *iface = eloop_ctx;
1853 struct hostapd_data *hapd;
1855 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1857 hapd = iface->bss[0];
1858 if (hapd->setup_complete_cb)
1859 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1863 static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1866 struct hostapd_data *hapd = iface->bss[0];
1869 int delay_apply_cfg = 0;
1870 int res_dfs_offload = 0;
1875 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1876 if (iface->conf->channel) {
1879 #endif /* NEED_AP_MLME */
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);
1888 /* Handle DFS only if it is not offloaded to the driver */
1889 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1891 res = hostapd_handle_dfs(iface);
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)
1904 wpa_printf(MSG_DEBUG,
1905 "Proceed with AP/channel setup");
1907 * If this is a DFS channel, move to completing
1910 if (res_dfs_offload == 1)
1912 /* Otherwise fall through. */
1915 #endif /* NEED_AP_MLME */
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;
1924 #endif /* CONFIG_MESH */
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(
1936 hostapd_get_oper_centr_freq_seg1_idx(
1938 wpa_printf(MSG_ERROR, "Could not set channel for "
1944 if (iface->current_mode) {
1945 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1946 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1948 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1949 HOSTAPD_LEVEL_WARNING,
1950 "Failed to prepare rates table.");
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 "
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");
1971 prev_addr = hapd->own_addr;
1973 for (j = 0; j < iface->num_bss; j++) {
1974 hapd = iface->bss[j];
1976 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1977 if (hostapd_setup_bss(hapd, j == 0)) {
1979 hapd = iface->bss[j];
1980 hostapd_bss_deinit_no_free(hapd);
1981 hostapd_free_hapd_data(hapd);
1988 if (is_zero_ether_addr(hapd->conf->bssid))
1989 prev_addr = hapd->own_addr;
1991 hapd = iface->bss[0];
1993 hostapd_tx_queue_params(iface);
1995 ap_list_init(iface);
1997 hostapd_set_acl(hapd);
1999 if (hostapd_driver_commit(hapd) < 0) {
2000 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2001 "configuration", __func__);
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.
2011 for (j = 0; j < iface->num_bss; j++) {
2012 if (hostapd_init_wps_complete(iface->bss[j]))
2016 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2019 * If freq is DFS, and DFS is offloaded to the driver, then wait
2020 * for CAC to complete.
2022 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2023 return res_dfs_offload;
2028 #endif /* NEED_AP_MLME */
2031 if (hapd->iconf->fst_cfg.group_id[0]) {
2032 struct fst_wpa_obj iface_obj;
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);
2038 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2039 hapd->iconf->fst_cfg.group_id);
2043 #endif /* CONFIG_FST */
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);
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--;
2057 for (j = 0; j < iface->num_bss; j++)
2058 hostapd_neighbor_set_own_report(iface->bss[j]);
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);
2068 fst_detach(iface->fst);
2071 #endif /* CONFIG_FST */
2073 if (iface->interfaces && iface->interfaces->terminate_on_error) {
2075 } else if (hapd->setup_complete_cb) {
2077 * Calling hapd->setup_complete_cb directly may cause iface
2078 * deinitialization which may be accessed later by the caller.
2080 eloop_register_timeout(0, 0,
2081 hostapd_interface_setup_failure_handler,
2090 * hostapd_setup_interface_complete - Complete interface setup
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.
2097 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2099 struct hapd_interfaces *interfaces = iface->interfaces;
2100 struct hostapd_data *hapd = iface->bss[0];
2102 int not_ready_in_sync_ifaces = 0;
2104 if (!iface->need_to_start_in_sync)
2105 return hostapd_setup_interface_complete_sync(iface, 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)
2117 if (iface->ready_to_start_in_sync) {
2118 /* Already in ready and waiting. should never happpen */
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++;
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.
2132 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2134 * If this interface went through CAC, do not synchronize, just
2135 * start immediately.
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);
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);
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;
2173 * hostapd_setup_interface - Setup of an interface
2174 * @iface: Pointer to interface data.
2175 * Returns: 0 on success, -1 on failure
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.
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.
2188 int hostapd_setup_interface(struct hostapd_iface *iface)
2192 ret = setup_interface(iface);
2194 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2195 iface->bss[0]->conf->iface);
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
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
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)
2219 struct hostapd_data *hapd;
2221 hapd = os_zalloc(sizeof(*hapd));
2225 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2228 hapd->iface = hapd_iface;
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 */
2240 dl_list_init(&hapd->sae_commit_queue);
2241 #endif /* CONFIG_SAE */
2247 static void hostapd_bss_deinit(struct hostapd_data *hapd)
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;
2260 #endif /* CONFIG_SQLITE */
2261 hostapd_cleanup(hapd);
2265 void hostapd_interface_deinit(struct hostapd_iface *iface)
2269 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2273 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2275 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2276 iface->wait_channel_update = 0;
2280 fst_detach(iface->fst);
2283 #endif /* CONFIG_FST */
2285 for (j = (int) iface->num_bss - 1; j >= 0; j--) {
2288 hostapd_bss_deinit(iface->bss[j]);
2291 #ifdef CONFIG_IEEE80211N
2293 hostapd_stop_setup_timers(iface);
2294 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2295 #endif /* NEED_AP_MLME */
2296 #endif /* CONFIG_IEEE80211N */
2300 void hostapd_interface_free(struct hostapd_iface *iface)
2303 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2304 for (j = 0; j < iface->num_bss; j++) {
2307 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2308 __func__, iface->bss[j]);
2309 os_free(iface->bss[j]);
2311 hostapd_cleanup_iface(iface);
2315 struct hostapd_iface * hostapd_alloc_iface(void)
2317 struct hostapd_iface *hapd_iface;
2319 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2323 dl_list_init(&hapd_iface->sta_seen);
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
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().
2338 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2339 const char *config_file)
2341 struct hostapd_iface *hapd_iface = NULL;
2342 struct hostapd_config *conf = NULL;
2343 struct hostapd_data *hapd;
2346 hapd_iface = hostapd_alloc_iface();
2347 if (hapd_iface == NULL)
2350 hapd_iface->config_fname = os_strdup(config_file);
2351 if (hapd_iface->config_fname == NULL)
2354 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2357 hapd_iface->conf = conf;
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)
2365 for (i = 0; i < conf->num_bss; i++) {
2366 hapd = hapd_iface->bss[i] =
2367 hostapd_alloc_bss_data(hapd_iface, conf,
2371 hapd->msg_ctx = hapd;
2377 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2380 hostapd_config_free(conf);
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);
2392 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
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)
2410 * hostapd_interface_init_bss - Read configuration file and init BSS data
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.
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.
2420 struct hostapd_iface *
2421 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2422 const char *config_fname, int debug)
2424 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2425 struct hostapd_data *hapd;
2432 for (i = 0; i < interfaces->count; i++) {
2433 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2434 iface = interfaces->iface[i];
2439 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2440 config_fname, phy, iface ? "" : " --> new PHY");
2442 struct hostapd_config *conf;
2443 struct hostapd_bss_config **tmp_conf;
2444 struct hostapd_data **tmp_bss;
2445 struct hostapd_bss_config *bss;
2448 /* Add new BSS to existing iface */
2449 conf = interfaces->config_read_cb(config_fname);
2452 if (conf->num_bss > 1) {
2453 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2454 hostapd_config_free(conf);
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);
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 *));
2472 iface->bss = tmp_bss;
2474 iface->conf->bss = tmp_conf;
2475 iface->conf->last_bss = tmp_conf[0];
2477 if (tmp_bss == NULL || tmp_conf == NULL) {
2478 hostapd_config_free(conf);
2481 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2482 iface->conf->num_bss++;
2484 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2486 iface->conf->num_bss--;
2487 hostapd_config_free(conf);
2490 iface->conf->last_bss = bss;
2491 iface->bss[iface->num_bss] = hapd;
2492 hapd->msg_ctx = hapd;
2494 bss_idx = iface->num_bss++;
2496 conf->bss[0] = NULL;
2497 hostapd_config_free(conf);
2499 /* Add a new iface with the first BSS */
2500 new_iface = iface = hostapd_init(interfaces, config_fname);
2503 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2504 iface->interfaces = interfaces;
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--;
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",
2518 hostapd_interface_deinit_free(new_iface);
2526 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2528 const struct wpa_driver_ops *driver;
2531 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
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;
2546 hostapd_interface_free(iface);
2550 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2552 struct hostapd_iface *hapd_iface)
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",
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;
2575 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
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);
2585 wpa_printf(MSG_DEBUG, "Enable interface %s",
2586 hapd_iface->conf->bss[0]->iface);
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");
2595 if (hapd_iface->interfaces == NULL ||
2596 hapd_iface->interfaces->driver_init == NULL ||
2597 hapd_iface->interfaces->driver_init(hapd_iface))
2600 if (hostapd_setup_interface(hapd_iface)) {
2601 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2602 hapd_iface->bss[0]->drv_priv,
2611 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
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");
2623 hostapd_clear_old(hapd_iface);
2624 for (j = 0; j < hapd_iface->num_bss; j++)
2625 hostapd_reload_bss(hapd_iface->bss[j]);
2631 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2634 const struct wpa_driver_ops *driver;
2637 if (hapd_iface == NULL)
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);
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;
2650 hapd_iface->driver_ap_teardown =
2651 !!(hapd_iface->drv_flags &
2652 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2655 for (j = 0; j < hapd_iface->num_bss; j++)
2656 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2657 #endif /* NEED_AP_MLME */
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);
2666 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
2668 /* From hostapd_cleanup_iface: These were initialized in
2669 * hostapd_setup_interface and hostapd_setup_interface_complete
2671 hostapd_cleanup_iface_partial(hapd_iface);
2673 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2674 hapd_iface->bss[0]->conf->iface);
2675 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2680 static struct hostapd_iface *
2681 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2683 struct hostapd_iface **iface, *hapd_iface;
2685 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2686 sizeof(struct hostapd_iface *));
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__);
2697 interfaces->count++;
2698 hapd_iface->interfaces = interfaces;
2704 static struct hostapd_config *
2705 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2706 const char *ctrl_iface, const char *driver)
2708 struct hostapd_bss_config *bss;
2709 struct hostapd_config *conf;
2711 /* Allocates memory for bss and conf */
2712 conf = hostapd_config_defaults();
2714 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2715 "configuration", __func__);
2722 for (j = 0; wpa_drivers[j]; j++) {
2723 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2724 conf->driver = wpa_drivers[j];
2729 wpa_printf(MSG_ERROR,
2730 "Invalid/unknown driver '%s' - registering the default driver",
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);
2742 bss = conf->last_bss = conf->bss[0];
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);
2751 /* Reading configuration file skipped, will be done in SET!
2752 * From reading the configuration till the end has to be done in
2759 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2760 struct hostapd_config *conf)
2763 struct hostapd_data *hapd;
2765 hapd_iface->bss = os_calloc(conf->num_bss,
2766 sizeof(struct hostapd_data *));
2767 if (hapd_iface->bss == NULL)
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]);
2776 os_free(hapd_iface->bss[i]);
2777 hapd_iface->bss[i] = NULL;
2779 os_free(hapd_iface->bss);
2780 hapd_iface->bss = NULL;
2783 hapd->msg_ctx = hapd;
2786 hapd_iface->conf = conf;
2787 hapd_iface->num_bss = conf->num_bss;
2793 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2795 struct hostapd_config *conf = NULL;
2796 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2797 struct hostapd_data *hapd;
2800 const char *conf_file = NULL, *phy_name = NULL;
2802 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2804 phy_name = buf + 11;
2805 pos = os_strchr(phy_name, ':');
2810 if (!os_strlen(conf_file))
2813 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2817 for (j = 0; j < interfaces->count; j++) {
2818 if (interfaces->iface[j] == hapd_iface)
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 *));
2827 hostapd_interface_deinit_free(hapd_iface);
2830 interfaces->iface = tmp;
2831 interfaces->iface[interfaces->count++] = hapd_iface;
2832 new_iface = hapd_iface;
2836 if (interfaces->driver_init(hapd_iface))
2839 if (hostapd_setup_interface(hapd_iface)) {
2840 hostapd_deinit_driver(
2841 hapd_iface->bss[0]->driver,
2842 hapd_iface->bss[0]->drv_priv,
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,
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);
2869 hostapd_owe_update_trans(hapd_iface);
2873 ptr = os_strchr(buf, ' ');
2878 if (os_strncmp(ptr, "config=", 7) == 0)
2879 conf_file = ptr + 7;
2881 for (i = 0; i < interfaces->count; i++) {
2882 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2884 wpa_printf(MSG_INFO, "Cannot add interface - it "
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__);
2896 new_iface = hapd_iface;
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));
2904 char *driver = os_strchr(ptr, ' ');
2908 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2911 if (conf == NULL || conf->bss == NULL) {
2912 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2913 "for configuration", __func__);
2917 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2918 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2919 "for hostapd", __func__);
2924 if (start_ctrl_iface(hapd_iface) < 0)
2927 wpa_printf(MSG_INFO, "Add interface '%s'",
2928 hapd_iface->conf->bss[0]->iface);
2934 hostapd_config_free(conf);
2936 if (hapd_iface->bss) {
2937 for (i = 0; i < hapd_iface->num_bss; i++) {
2938 hapd = hapd_iface->bss[i];
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],
2948 hostapd_cleanup(hapd);
2950 hapd_iface->bss[i] = NULL;
2952 os_free(hapd_iface->bss);
2953 hapd_iface->bss = NULL;
2956 interfaces->count--;
2957 interfaces->iface[interfaces->count] = NULL;
2959 hostapd_cleanup_iface(hapd_iface);
2965 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2969 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
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];
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);
2984 for (i = idx; i < iface->num_bss; i++)
2985 iface->bss[i] = iface->bss[i + 1];
2987 hostapd_config_free_bss(iface->conf->bss[idx]);
2988 iface->conf->bss[idx] = NULL;
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];
2999 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3001 struct hostapd_iface *hapd_iface;
3004 for (i = 0; i < interfaces->count; i++) {
3005 hapd_iface = interfaces->iface[i];
3006 if (hapd_iface == NULL)
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);
3014 hostapd_interface_deinit_free(hapd_iface);
3016 while (k < (interfaces->count - 1)) {
3017 interfaces->iface[k] =
3018 interfaces->iface[k + 1];
3021 interfaces->count--;
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);
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
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.
3049 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3052 if (hapd->tkip_countermeasures) {
3053 hostapd_drv_sta_deauth(hapd, sta->addr,
3054 WLAN_REASON_MICHAEL_MIC_FAILURE);
3058 hostapd_prune_associations(hapd, sta->addr);
3059 ap_sta_clear_disconnect_timeouts(hapd, sta);
3061 /* IEEE 802.11F (IAPP) */
3062 if (hapd->conf->ieee802_11f)
3063 iapp_new_station(hapd->iapp, sta);
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);
3072 #endif /* CONFIG_P2P */
3074 airtime_policy_new_sta(hapd, sta);
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);
3085 /* Start IEEE 802.1X authentication process for new stations */
3086 ieee802_1x_new_station(hapd, sta);
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);
3095 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
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 "
3102 hapd->conf->iface, __func__,
3103 MAC2STR(sta->addr));
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);
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);
3122 ieee802_1x_alloc_kay_sm_hapd(hapd, sta);
3123 #endif /* CONFIG_MACSEC */
3127 const char * hostapd_state_text(enum hostapd_iface_state s)
3130 case HAPD_IFACE_UNINITIALIZED:
3131 return "UNINITIALIZED";
3132 case HAPD_IFACE_DISABLED:
3134 case HAPD_IFACE_COUNTRY_UPDATE:
3135 return "COUNTRY_UPDATE";
3136 case HAPD_IFACE_ACS:
3138 case HAPD_IFACE_HT_SCAN:
3140 case HAPD_IFACE_DFS:
3142 case HAPD_IFACE_ENABLED:
3150 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
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));
3159 int hostapd_csa_in_progress(struct hostapd_iface *iface)
3163 for (i = 0; i < iface->num_bss; i++)
3164 if (iface->bss[i]->csa_in_progress)
3172 static void free_beacon_data(struct beacon_data *beacon)
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;
3189 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
3190 struct beacon_data *beacon)
3192 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3193 struct wpa_driver_ap_params params;
3196 os_memset(beacon, 0, sizeof(*beacon));
3197 ret = ieee802_11_build_ap_params(hapd, ¶ms);
3201 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3205 goto free_ap_params;
3208 beacon->head = os_memdup(params.head, params.head_len);
3210 goto free_ap_extra_ies;
3212 beacon->head_len = params.head_len;
3214 beacon->tail = os_memdup(params.tail, params.tail_len);
3218 beacon->tail_len = params.tail_len;
3220 if (params.proberesp != NULL) {
3221 beacon->probe_resp = os_memdup(params.proberesp,
3222 params.proberesp_len);
3223 if (!beacon->probe_resp)
3226 beacon->probe_resp_len = params.proberesp_len;
3229 /* copy the extra ies */
3231 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3232 wpabuf_len(beacon_extra));
3233 if (!beacon->beacon_ies)
3236 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3239 if (proberesp_extra) {
3240 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3241 wpabuf_len(proberesp_extra));
3242 if (!beacon->proberesp_ies)
3245 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3248 if (assocresp_extra) {
3249 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3250 wpabuf_len(assocresp_extra));
3251 if (!beacon->assocresp_ies)
3254 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3259 /* if the function fails, the caller should not free beacon data */
3261 free_beacon_data(beacon);
3264 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3267 ieee802_11_free_ap_params(¶ms);
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.
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)
3284 struct hostapd_hw_modes *mode;
3286 if (!params->channel) {
3287 /* check if the new channel is supported by hw */
3288 params->channel = hostapd_hw_get_channel(hapd, params->freq);
3291 channel = params->channel;
3295 mode = hapd->iface->current_mode;
3297 /* if a pointer to old_params is provided we save previous state */
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),
3308 mode ? &mode->he_capab[IEEE80211_MODE_AP] :
3312 switch (params->bandwidth) {
3316 hostapd_set_oper_chwidth(conf, CHANWIDTH_USE_HT);
3319 if (params->center_freq2)
3320 hostapd_set_oper_chwidth(conf, CHANWIDTH_80P80MHZ);
3322 hostapd_set_oper_chwidth(conf, CHANWIDTH_80MHZ);
3325 hostapd_set_oper_chwidth(conf, CHANWIDTH_160MHZ);
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,
3336 ieee80211_freq_to_chan(params->center_freq2,
3338 hostapd_set_oper_centr_freq_seg0_idx(conf, seg0);
3339 hostapd_set_oper_centr_freq_seg1_idx(conf, seg1);
3341 /* TODO: maybe call here hostapd_config_check here? */
3347 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
3348 struct csa_settings *settings)
3350 struct hostapd_iface *iface = hapd->iface;
3351 struct hostapd_freq_params old_freq;
3355 os_memset(&old_freq, 0, sizeof(old_freq));
3356 if (!iface || !iface->freq || hapd->csa_in_progress)
3359 switch (settings->freq_params.bandwidth) {
3361 if (settings->freq_params.center_freq2)
3362 bandwidth = CHANWIDTH_80P80MHZ;
3364 bandwidth = CHANWIDTH_80MHZ;
3367 bandwidth = CHANWIDTH_160MHZ;
3370 bandwidth = CHANWIDTH_USE_HT;
3374 if (ieee80211_freq_to_channel_ext(
3375 settings->freq_params.freq,
3376 settings->freq_params.sec_channel_offset,
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);
3389 settings->freq_params.channel = chan;
3391 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3392 &settings->freq_params,
3397 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
3399 /* change back the configuration */
3400 hostapd_change_config_freq(iface->bss[0], iface->conf,
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;
3411 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
3413 free_beacon_data(&settings->beacon_after);
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;
3426 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3428 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
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;
3439 void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled)
3442 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3444 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
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);
3452 int hostapd_switch_channel(struct hostapd_data *hapd,
3453 struct csa_settings *settings)
3457 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3458 wpa_printf(MSG_INFO, "CSA is not supported");
3462 ret = hostapd_fill_csa_settings(hapd, settings);
3466 ret = hostapd_drv_switch_channel(hapd, settings);
3467 free_beacon_data(&settings->beacon_csa);
3468 free_beacon_data(&settings->beacon_after);
3471 /* if we failed, clean cs parameters */
3472 hostapd_cleanup_cs_params(hapd);
3476 hapd->csa_in_progress = 1;
3482 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3483 const struct hostapd_freq_params *freq_params)
3485 int seg0_idx = 0, seg1_idx = 0, bw = CHANWIDTH_USE_HT;
3487 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
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;
3494 switch (freq_params->bandwidth) {
3498 bw = CHANWIDTH_USE_HT;
3501 if (freq_params->center_freq2)
3502 bw = CHANWIDTH_80P80MHZ;
3504 bw = CHANWIDTH_80MHZ;
3507 bw = CHANWIDTH_160MHZ;
3510 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3511 freq_params->bandwidth);
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;
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.
3530 hostapd_disable_iface(iface);
3531 hostapd_enable_iface(iface);
3534 #endif /* NEED_AP_MLME */
3537 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3542 for (i = 0; i < interfaces->count; i++) {
3543 struct hostapd_iface *iface = interfaces->iface[i];
3545 for (j = 0; j < iface->num_bss; j++) {
3546 struct hostapd_data *hapd = iface->bss[j];
3548 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3557 void hostapd_periodic_iface(struct hostapd_iface *iface)
3561 ap_list_timer(iface);
3563 for (i = 0; i < iface->num_bss; i++) {
3564 struct hostapd_data *hapd = iface->bss[i];
3569 #ifndef CONFIG_NO_RADIUS
3570 hostapd_acl_expire(hapd);
3571 #endif /* CONFIG_NO_RADIUS */