2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2014, 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"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/hw_features_common.h"
16 #include "radius/radius_client.h"
17 #include "radius/radius_das.h"
18 #include "eap_server/tncs.h"
19 #include "eapol_auth/eapol_auth_sm.h"
20 #include "eapol_auth/eapol_auth_sm_i.h"
25 #include "accounting.h"
29 #include "ieee802_1x.h"
30 #include "ieee802_11_auth.h"
31 #include "vlan_init.h"
33 #include "wps_hostapd.h"
34 #include "dpp_hostapd.h"
35 #include "gas_query_ap.h"
36 #include "hw_features.h"
37 #include "wpa_auth_glue.h"
38 #include "ap_drv_ops.h"
39 #include "ap_config.h"
40 #include "p2p_hostapd.h"
43 #include "ieee802_11.h"
46 #include "dhcp_snoop.h"
47 #include "ndisc_snoop.h"
48 #include "neighbor_db.h"
55 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
56 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
57 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
58 static int setup_interface2(struct hostapd_iface *iface);
59 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
60 static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
64 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
65 int (*cb)(struct hostapd_iface *iface,
66 void *ctx), void *ctx)
71 for (i = 0; i < interfaces->count; i++) {
72 ret = cb(interfaces->iface[i], ctx);
81 void hostapd_reconfig_encryption(struct hostapd_data *hapd)
86 hostapd_set_privacy(hapd, 0);
87 hostapd_setup_encryption(hapd->conf->iface, hapd);
91 static void hostapd_reload_bss(struct hostapd_data *hapd)
93 struct hostapd_ssid *ssid;
98 if (hapd->conf->wmm_enabled < 0)
99 hapd->conf->wmm_enabled = hapd->iconf->ieee80211n;
101 #ifndef CONFIG_NO_RADIUS
102 radius_client_reconfig(hapd->radius, hapd->conf->radius);
103 #endif /* CONFIG_NO_RADIUS */
105 ssid = &hapd->conf->ssid;
106 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
107 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
109 * Force PSK to be derived again since SSID or passphrase may
112 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
114 if (hostapd_setup_wpa_psk(hapd->conf)) {
115 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
116 "after reloading configuration");
119 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
120 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
122 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
124 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
125 hostapd_setup_wpa(hapd);
127 wpa_init_keys(hapd->wpa_auth);
128 } else if (hapd->conf->wpa) {
131 hostapd_reconfig_wpa(hapd);
132 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
133 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
134 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
135 "the kernel driver.");
136 } else if (hapd->wpa_auth) {
137 wpa_deinit(hapd->wpa_auth);
138 hapd->wpa_auth = NULL;
139 hostapd_set_privacy(hapd, 0);
140 hostapd_setup_encryption(hapd->conf->iface, hapd);
141 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
144 ieee802_11_set_beacon(hapd);
145 hostapd_update_wps(hapd);
147 if (hapd->conf->ssid.ssid_set &&
148 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
149 hapd->conf->ssid.ssid_len)) {
150 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
151 /* try to continue */
153 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
157 static void hostapd_clear_old(struct hostapd_iface *iface)
162 * Deauthenticate all stations since the new configuration may not
163 * allow them to use the BSS anymore.
165 for (j = 0; j < iface->num_bss; j++) {
166 hostapd_flush_old_stations(iface->bss[j],
167 WLAN_REASON_PREV_AUTH_NOT_VALID);
168 hostapd_broadcast_wep_clear(iface->bss[j]);
170 #ifndef CONFIG_NO_RADIUS
171 /* TODO: update dynamic data based on changed configuration
172 * items (e.g., open/close sockets, etc.) */
173 radius_client_flush(iface->bss[j]->radius, 0);
174 #endif /* CONFIG_NO_RADIUS */
179 static int hostapd_iface_conf_changed(struct hostapd_config *newconf,
180 struct hostapd_config *oldconf)
184 if (newconf->num_bss != oldconf->num_bss)
187 for (i = 0; i < newconf->num_bss; i++) {
188 if (os_strcmp(newconf->bss[i]->iface,
189 oldconf->bss[i]->iface) != 0)
197 int hostapd_reload_config(struct hostapd_iface *iface)
199 struct hapd_interfaces *interfaces = iface->interfaces;
200 struct hostapd_data *hapd = iface->bss[0];
201 struct hostapd_config *newconf, *oldconf;
204 if (iface->config_fname == NULL) {
205 /* Only in-memory config in use - assume it has been updated */
206 hostapd_clear_old(iface);
207 for (j = 0; j < iface->num_bss; j++)
208 hostapd_reload_bss(iface->bss[j]);
212 if (iface->interfaces == NULL ||
213 iface->interfaces->config_read_cb == NULL)
215 newconf = iface->interfaces->config_read_cb(iface->config_fname);
219 hostapd_clear_old(iface);
221 oldconf = hapd->iconf;
222 if (hostapd_iface_conf_changed(newconf, oldconf)) {
226 wpa_printf(MSG_DEBUG,
227 "Configuration changes include interface/BSS modification - force full disable+enable sequence");
228 fname = os_strdup(iface->config_fname);
230 hostapd_config_free(newconf);
233 hostapd_remove_iface(interfaces, hapd->conf->iface);
234 iface = hostapd_init(interfaces, fname);
236 hostapd_config_free(newconf);
238 wpa_printf(MSG_ERROR,
239 "Failed to initialize interface on config reload");
242 iface->interfaces = interfaces;
243 interfaces->iface[interfaces->count] = iface;
245 res = hostapd_enable_iface(iface);
247 wpa_printf(MSG_ERROR,
248 "Failed to enable interface on config reload");
251 iface->conf = newconf;
253 for (j = 0; j < iface->num_bss; j++) {
254 hapd = iface->bss[j];
255 hapd->iconf = newconf;
256 hapd->iconf->channel = oldconf->channel;
257 hapd->iconf->acs = oldconf->acs;
258 hapd->iconf->secondary_channel = oldconf->secondary_channel;
259 hapd->iconf->ieee80211n = oldconf->ieee80211n;
260 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
261 hapd->iconf->ht_capab = oldconf->ht_capab;
262 hapd->iconf->vht_capab = oldconf->vht_capab;
263 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
264 hapd->iconf->vht_oper_centr_freq_seg0_idx =
265 oldconf->vht_oper_centr_freq_seg0_idx;
266 hapd->iconf->vht_oper_centr_freq_seg1_idx =
267 oldconf->vht_oper_centr_freq_seg1_idx;
268 hapd->conf = newconf->bss[j];
269 hostapd_reload_bss(hapd);
272 hostapd_config_free(oldconf);
279 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
284 if (!ifname || !hapd->drv_priv)
286 for (i = 0; i < NUM_WEP_KEYS; i++) {
287 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
288 0, NULL, 0, NULL, 0)) {
289 wpa_printf(MSG_DEBUG, "Failed to clear default "
290 "encryption keys (ifname=%s keyidx=%d)",
294 #ifdef CONFIG_IEEE80211W
295 if (hapd->conf->ieee80211w) {
296 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
297 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
300 wpa_printf(MSG_DEBUG, "Failed to clear "
301 "default mgmt encryption keys "
302 "(ifname=%s keyidx=%d)", ifname, i);
306 #endif /* CONFIG_IEEE80211W */
310 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
312 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
317 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
320 struct hostapd_ssid *ssid = &hapd->conf->ssid;
323 if (ssid->wep.default_len &&
324 hostapd_drv_set_key(hapd->conf->iface,
325 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
326 1, NULL, 0, ssid->wep.key[idx],
327 ssid->wep.len[idx])) {
328 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
336 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
338 os_free(hapd->probereq_cb);
339 hapd->probereq_cb = NULL;
340 hapd->num_probereq_cb = 0;
343 wpabuf_free(hapd->p2p_beacon_ie);
344 hapd->p2p_beacon_ie = NULL;
345 wpabuf_free(hapd->p2p_probe_resp_ie);
346 hapd->p2p_probe_resp_ie = NULL;
347 #endif /* CONFIG_P2P */
349 if (!hapd->started) {
350 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
351 __func__, hapd->conf->iface);
356 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
357 iapp_deinit(hapd->iapp);
359 accounting_deinit(hapd);
360 hostapd_deinit_wpa(hapd);
362 hostapd_acl_deinit(hapd);
363 #ifndef CONFIG_NO_RADIUS
364 radius_client_deinit(hapd->radius);
366 radius_das_deinit(hapd->radius_das);
367 hapd->radius_das = NULL;
368 #endif /* CONFIG_NO_RADIUS */
370 hostapd_deinit_wps(hapd);
372 hostapd_dpp_deinit(hapd);
373 gas_query_ap_deinit(hapd->gas);
374 #endif /* CONFIG_DPP */
376 authsrv_deinit(hapd);
378 if (hapd->interface_added) {
379 hapd->interface_added = 0;
380 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
381 wpa_printf(MSG_WARNING,
382 "Failed to remove BSS interface %s",
384 hapd->interface_added = 1;
387 * Since this was a dynamically added interface, the
388 * driver wrapper may have removed its internal instance
389 * and hapd->drv_priv is not valid anymore.
391 hapd->drv_priv = NULL;
395 wpabuf_free(hapd->time_adv);
397 #ifdef CONFIG_INTERWORKING
398 gas_serv_deinit(hapd);
399 #endif /* CONFIG_INTERWORKING */
401 bss_load_update_deinit(hapd);
402 ndisc_snoop_deinit(hapd);
403 dhcp_snoop_deinit(hapd);
404 x_snoop_deinit(hapd);
407 bin_clear_free(hapd->tmp_eap_user.identity,
408 hapd->tmp_eap_user.identity_len);
409 bin_clear_free(hapd->tmp_eap_user.password,
410 hapd->tmp_eap_user.password_len);
411 #endif /* CONFIG_SQLITE */
414 wpabuf_free(hapd->mesh_pending_auth);
415 hapd->mesh_pending_auth = NULL;
416 #endif /* CONFIG_MESH */
418 hostapd_clean_rrm(hapd);
419 fils_hlp_deinit(hapd);
424 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
425 * @hapd: Pointer to BSS data
427 * This function is used to free all per-BSS data structures and resources.
428 * Most of the modules that are initialized in hostapd_setup_bss() are
429 * deinitialized here.
431 static void hostapd_cleanup(struct hostapd_data *hapd)
433 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
435 if (hapd->iface->interfaces &&
436 hapd->iface->interfaces->ctrl_iface_deinit) {
437 wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_TERMINATING);
438 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
440 hostapd_free_hapd_data(hapd);
444 static void sta_track_deinit(struct hostapd_iface *iface)
446 struct hostapd_sta_info *info;
448 if (!iface->num_sta_seen)
451 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
453 dl_list_del(&info->list);
454 iface->num_sta_seen--;
460 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
462 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
463 #ifdef CONFIG_IEEE80211N
465 hostapd_stop_setup_timers(iface);
466 #endif /* NEED_AP_MLME */
467 #endif /* CONFIG_IEEE80211N */
468 if (iface->current_mode)
470 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
471 iface->hw_features = NULL;
472 iface->current_mode = NULL;
473 os_free(iface->current_rates);
474 iface->current_rates = NULL;
475 os_free(iface->basic_rates);
476 iface->basic_rates = NULL;
477 ap_list_deinit(iface);
478 sta_track_deinit(iface);
483 * hostapd_cleanup_iface - Complete per-interface cleanup
484 * @iface: Pointer to interface data
486 * This function is called after per-BSS data structures are deinitialized
487 * with hostapd_cleanup().
489 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
491 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
492 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
493 eloop_cancel_timeout(hostapd_interface_setup_failure_handler, iface,
496 hostapd_cleanup_iface_partial(iface);
497 hostapd_config_free(iface->conf);
500 os_free(iface->config_fname);
502 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
507 static void hostapd_clear_wep(struct hostapd_data *hapd)
509 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
510 hostapd_set_privacy(hapd, 0);
511 hostapd_broadcast_wep_clear(hapd);
516 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
520 hostapd_broadcast_wep_set(hapd);
522 if (hapd->conf->ssid.wep.default_len) {
523 hostapd_set_privacy(hapd, 1);
528 * When IEEE 802.1X is not enabled, the driver may need to know how to
529 * set authentication algorithms for static WEP.
531 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
533 for (i = 0; i < 4; i++) {
534 if (hapd->conf->ssid.wep.key[i] &&
535 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
536 i == hapd->conf->ssid.wep.idx, NULL, 0,
537 hapd->conf->ssid.wep.key[i],
538 hapd->conf->ssid.wep.len[i])) {
539 wpa_printf(MSG_WARNING, "Could not set WEP "
543 if (hapd->conf->ssid.wep.key[i] &&
544 i == hapd->conf->ssid.wep.idx)
545 hostapd_set_privacy(hapd, 1);
552 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
557 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
560 if (!hapd->iface->driver_ap_teardown) {
561 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
562 "Flushing old station entries");
564 if (hostapd_flush(hapd)) {
565 wpa_msg(hapd->msg_ctx, MSG_WARNING,
566 "Could not connect to kernel driver");
570 if (hapd->conf && hapd->conf->broadcast_deauth) {
571 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
572 "Deauthenticate all stations");
573 os_memset(addr, 0xff, ETH_ALEN);
574 hostapd_drv_sta_deauth(hapd, addr, reason);
576 hostapd_free_stas(hapd);
582 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
584 hostapd_free_stas(hapd);
585 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
586 hostapd_clear_wep(hapd);
591 * hostapd_validate_bssid_configuration - Validate BSSID configuration
592 * @iface: Pointer to interface data
593 * Returns: 0 on success, -1 on failure
595 * This function is used to validate that the configured BSSIDs are valid.
597 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
599 u8 mask[ETH_ALEN] = { 0 };
600 struct hostapd_data *hapd = iface->bss[0];
601 unsigned int i = iface->conf->num_bss, bits = 0, j;
604 if (hostapd_drv_none(hapd))
607 if (iface->conf->use_driver_iface_addr)
610 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
612 /* Determine the bits necessary to cover the number of BSSIDs. */
613 for (i--; i; i >>= 1)
616 /* Determine the bits necessary to any configured BSSIDs,
617 if they are higher than the number of BSSIDs. */
618 for (j = 0; j < iface->conf->num_bss; j++) {
619 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
625 for (i = 0; i < ETH_ALEN; i++) {
627 iface->conf->bss[j]->bssid[i] ^
635 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
641 while (mask[i] != 0) {
651 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
656 os_memset(mask, 0xff, ETH_ALEN);
658 for (i = 5; i > 5 - j; i--)
665 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
666 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
671 for (i = 0; i < ETH_ALEN; i++) {
672 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
673 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
674 " for start address " MACSTR ".",
675 MAC2STR(mask), MAC2STR(hapd->own_addr));
676 wpa_printf(MSG_ERROR, "Start address must be the "
677 "first address in the block (i.e., addr "
678 "AND mask == addr).");
687 static int mac_in_conf(struct hostapd_config *conf, const void *a)
691 for (i = 0; i < conf->num_bss; i++) {
692 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
701 #ifndef CONFIG_NO_RADIUS
703 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
704 struct radius_das_attrs *attr)
706 if (attr->nas_identifier &&
707 (!hapd->conf->nas_identifier ||
708 os_strlen(hapd->conf->nas_identifier) !=
709 attr->nas_identifier_len ||
710 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
711 attr->nas_identifier_len) != 0)) {
712 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
716 if (attr->nas_ip_addr &&
717 (hapd->conf->own_ip_addr.af != AF_INET ||
718 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
720 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
725 if (attr->nas_ipv6_addr &&
726 (hapd->conf->own_ip_addr.af != AF_INET6 ||
727 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
729 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
732 #endif /* CONFIG_IPV6 */
738 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
739 struct radius_das_attrs *attr,
742 struct sta_info *selected, *sta;
749 for (sta = hapd->sta_list; sta; sta = sta->next)
750 sta->radius_das_match = 1;
752 if (attr->sta_addr) {
754 sta = ap_get_sta(hapd, attr->sta_addr);
756 wpa_printf(MSG_DEBUG,
757 "RADIUS DAS: No Calling-Station-Id match");
762 for (sta = hapd->sta_list; sta; sta = sta->next) {
764 sta->radius_das_match = 0;
766 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
769 if (attr->acct_session_id) {
771 if (attr->acct_session_id_len != 16) {
772 wpa_printf(MSG_DEBUG,
773 "RADIUS DAS: Acct-Session-Id cannot match");
778 for (sta = hapd->sta_list; sta; sta = sta->next) {
779 if (!sta->radius_das_match)
781 os_snprintf(buf, sizeof(buf), "%016llX",
782 (unsigned long long) sta->acct_session_id);
783 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
784 sta->radius_das_match = 0;
790 wpa_printf(MSG_DEBUG,
791 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
794 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
797 if (attr->acct_multi_session_id) {
799 if (attr->acct_multi_session_id_len != 16) {
800 wpa_printf(MSG_DEBUG,
801 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
806 for (sta = hapd->sta_list; sta; sta = sta->next) {
807 if (!sta->radius_das_match)
809 if (!sta->eapol_sm ||
810 !sta->eapol_sm->acct_multi_session_id) {
811 sta->radius_das_match = 0;
814 os_snprintf(buf, sizeof(buf), "%016llX",
816 sta->eapol_sm->acct_multi_session_id);
817 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
819 sta->radius_das_match = 0;
825 wpa_printf(MSG_DEBUG,
826 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
829 wpa_printf(MSG_DEBUG,
830 "RADIUS DAS: Acct-Multi-Session-Id match");
837 for (sta = hapd->sta_list; sta; sta = sta->next) {
840 if (!sta->radius_das_match)
842 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
843 if (!cui || wpabuf_len(cui) != attr->cui_len ||
844 os_memcmp(wpabuf_head(cui), attr->cui,
846 sta->radius_das_match = 0;
852 wpa_printf(MSG_DEBUG,
853 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
856 wpa_printf(MSG_DEBUG,
857 "RADIUS DAS: Chargeable-User-Identity match");
860 if (attr->user_name) {
864 for (sta = hapd->sta_list; sta; sta = sta->next) {
868 if (!sta->radius_das_match)
870 identity = ieee802_1x_get_identity(sta->eapol_sm,
873 identity_len != attr->user_name_len ||
874 os_memcmp(identity, attr->user_name, identity_len)
876 sta->radius_das_match = 0;
882 wpa_printf(MSG_DEBUG,
883 "RADIUS DAS: No matches remaining after User-Name check");
886 wpa_printf(MSG_DEBUG,
887 "RADIUS DAS: User-Name match");
892 * In theory, we could match all current associations, but it
893 * seems safer to just reject requests that do not include any
894 * session identification attributes.
896 wpa_printf(MSG_DEBUG,
897 "RADIUS DAS: No session identification attributes included");
902 for (sta = hapd->sta_list; sta; sta = sta->next) {
903 if (sta->radius_das_match) {
916 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
917 struct radius_das_attrs *attr)
921 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
925 static enum radius_das_res
926 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
928 struct hostapd_data *hapd = ctx;
929 struct sta_info *sta;
932 if (hostapd_das_nas_mismatch(hapd, attr))
933 return RADIUS_DAS_NAS_MISMATCH;
935 sta = hostapd_das_find_sta(hapd, attr, &multi);
938 wpa_printf(MSG_DEBUG,
939 "RADIUS DAS: Multiple sessions match - not supported");
940 return RADIUS_DAS_MULTI_SESSION_MATCH;
942 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
943 wpa_printf(MSG_DEBUG,
944 "RADIUS DAS: PMKSA cache entry matched");
945 return RADIUS_DAS_SUCCESS;
947 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
948 return RADIUS_DAS_SESSION_NOT_FOUND;
951 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
952 " - disconnecting", MAC2STR(sta->addr));
953 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
955 hostapd_drv_sta_deauth(hapd, sta->addr,
956 WLAN_REASON_PREV_AUTH_NOT_VALID);
957 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
959 return RADIUS_DAS_SUCCESS;
964 static enum radius_das_res
965 hostapd_das_coa(void *ctx, struct radius_das_attrs *attr)
967 struct hostapd_data *hapd = ctx;
968 struct sta_info *sta;
971 if (hostapd_das_nas_mismatch(hapd, attr))
972 return RADIUS_DAS_NAS_MISMATCH;
974 sta = hostapd_das_find_sta(hapd, attr, &multi);
977 wpa_printf(MSG_DEBUG,
978 "RADIUS DAS: Multiple sessions match - not supported");
979 return RADIUS_DAS_MULTI_SESSION_MATCH;
981 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
982 return RADIUS_DAS_SESSION_NOT_FOUND;
985 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
986 " - CoA", MAC2STR(sta->addr));
988 if (attr->hs20_t_c_filtering) {
989 if (attr->hs20_t_c_filtering[0] & BIT(0)) {
990 wpa_printf(MSG_DEBUG,
991 "HS 2.0: Unexpected Terms and Conditions filtering required in CoA-Request");
992 return RADIUS_DAS_COA_FAILED;
995 hs20_t_c_filtering(hapd, sta, 0);
998 return RADIUS_DAS_SUCCESS;
1000 #else /* CONFIG_HS20 */
1001 #define hostapd_das_coa NULL
1002 #endif /* CONFIG_HS20 */
1004 #endif /* CONFIG_NO_RADIUS */
1008 * hostapd_setup_bss - Per-BSS setup (initialization)
1009 * @hapd: Pointer to BSS data
1010 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
1011 * but interface may exist
1013 * This function is used to initialize all per-BSS data structures and
1014 * resources. This gets called in a loop for each BSS when an interface is
1015 * initialized. Most of the modules that are initialized here will be
1016 * deinitialized in hostapd_cleanup().
1018 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1020 struct hostapd_bss_config *conf = hapd->conf;
1021 u8 ssid[SSID_MAX_LEN + 1];
1022 int ssid_len, set_ssid;
1023 char force_ifname[IFNAMSIZ];
1024 u8 if_addr[ETH_ALEN];
1025 int flush_old_stations = 1;
1027 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
1028 __func__, hapd, conf->iface, first);
1030 #ifdef EAP_SERVER_TNC
1031 if (conf->tnc && tncs_global_init() < 0) {
1032 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1035 #endif /* EAP_SERVER_TNC */
1037 if (hapd->started) {
1038 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
1039 __func__, conf->iface);
1044 if (!first || first == -1) {
1045 u8 *addr = hapd->own_addr;
1047 if (!is_zero_ether_addr(conf->bssid)) {
1048 /* Allocate the configured BSSID. */
1049 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
1051 if (hostapd_mac_comp(hapd->own_addr,
1052 hapd->iface->bss[0]->own_addr) ==
1054 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1055 "BSSID set to the MAC address of "
1056 "the radio", conf->iface);
1059 } else if (hapd->iconf->use_driver_iface_addr) {
1062 /* Allocate the next available BSSID. */
1064 inc_byte_array(hapd->own_addr, ETH_ALEN);
1065 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1068 hapd->interface_added = 1;
1069 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
1070 conf->iface, addr, hapd,
1071 &hapd->drv_priv, force_ifname, if_addr,
1072 conf->bridge[0] ? conf->bridge : NULL,
1074 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1075 MACSTR ")", MAC2STR(hapd->own_addr));
1076 hapd->interface_added = 0;
1081 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
1084 if (conf->wmm_enabled < 0)
1085 conf->wmm_enabled = hapd->iconf->ieee80211n;
1087 #ifdef CONFIG_IEEE80211R_AP
1088 if (is_zero_ether_addr(conf->r1_key_holder))
1089 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
1090 #endif /* CONFIG_IEEE80211R_AP */
1093 if ((hapd->conf->mesh & MESH_ENABLED) && hapd->iface->mconf == NULL)
1094 flush_old_stations = 0;
1095 #endif /* CONFIG_MESH */
1097 if (flush_old_stations)
1098 hostapd_flush_old_stations(hapd,
1099 WLAN_REASON_PREV_AUTH_NOT_VALID);
1100 hostapd_set_privacy(hapd, 0);
1102 hostapd_broadcast_wep_clear(hapd);
1103 if (hostapd_setup_encryption(conf->iface, hapd))
1107 * Fetch the SSID from the system and use it or,
1108 * if one was specified in the config file, verify they
1111 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1113 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1116 if (conf->ssid.ssid_set) {
1118 * If SSID is specified in the config file and it differs
1119 * from what is being used then force installation of the
1122 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1123 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1126 * No SSID in the config file; just use the one we got
1130 conf->ssid.ssid_len = ssid_len;
1131 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1134 if (!hostapd_drv_none(hapd)) {
1135 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1137 conf->iface, MAC2STR(hapd->own_addr),
1138 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
1141 if (hostapd_setup_wpa_psk(conf)) {
1142 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1146 /* Set SSID for the kernel driver (to be used in beacon and probe
1147 * response frames) */
1148 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
1149 conf->ssid.ssid_len)) {
1150 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1154 if (wpa_debug_level <= MSG_MSGDUMP)
1155 conf->radius->msg_dumps = 1;
1156 #ifndef CONFIG_NO_RADIUS
1157 hapd->radius = radius_client_init(hapd, conf->radius);
1158 if (hapd->radius == NULL) {
1159 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1163 if (conf->radius_das_port) {
1164 struct radius_das_conf das_conf;
1165 os_memset(&das_conf, 0, sizeof(das_conf));
1166 das_conf.port = conf->radius_das_port;
1167 das_conf.shared_secret = conf->radius_das_shared_secret;
1168 das_conf.shared_secret_len =
1169 conf->radius_das_shared_secret_len;
1170 das_conf.client_addr = &conf->radius_das_client_addr;
1171 das_conf.time_window = conf->radius_das_time_window;
1172 das_conf.require_event_timestamp =
1173 conf->radius_das_require_event_timestamp;
1174 das_conf.require_message_authenticator =
1175 conf->radius_das_require_message_authenticator;
1176 das_conf.ctx = hapd;
1177 das_conf.disconnect = hostapd_das_disconnect;
1178 das_conf.coa = hostapd_das_coa;
1179 hapd->radius_das = radius_das_init(&das_conf);
1180 if (hapd->radius_das == NULL) {
1181 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1186 #endif /* CONFIG_NO_RADIUS */
1188 if (hostapd_acl_init(hapd)) {
1189 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1192 if (hostapd_init_wps(hapd, conf))
1196 hapd->gas = gas_query_ap_init(hapd, hapd->msg_ctx);
1199 if (hostapd_dpp_init(hapd))
1201 #endif /* CONFIG_DPP */
1203 if (authsrv_init(hapd) < 0)
1206 if (ieee802_1x_init(hapd)) {
1207 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1211 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1214 if (accounting_init(hapd)) {
1215 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1219 if (conf->ieee802_11f &&
1220 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1221 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1226 #ifdef CONFIG_INTERWORKING
1227 if (gas_serv_init(hapd)) {
1228 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1232 if (conf->qos_map_set_len &&
1233 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1234 conf->qos_map_set_len)) {
1235 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1238 #endif /* CONFIG_INTERWORKING */
1240 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1241 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1245 if (conf->proxy_arp) {
1246 if (x_snoop_init(hapd)) {
1247 wpa_printf(MSG_ERROR,
1248 "Generic snooping infrastructure initialization failed");
1252 if (dhcp_snoop_init(hapd)) {
1253 wpa_printf(MSG_ERROR,
1254 "DHCP snooping initialization failed");
1258 if (ndisc_snoop_init(hapd)) {
1259 wpa_printf(MSG_ERROR,
1260 "Neighbor Discovery snooping initialization failed");
1265 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1266 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1270 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1273 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1276 if (hapd->driver && hapd->driver->set_operstate)
1277 hapd->driver->set_operstate(hapd->drv_priv, 1);
1283 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1285 struct hostapd_data *hapd = iface->bss[0];
1287 struct hostapd_tx_queue_params *p;
1290 if ((hapd->conf->mesh & MESH_ENABLED) && iface->mconf == NULL)
1292 #endif /* CONFIG_MESH */
1294 for (i = 0; i < NUM_TX_QUEUES; i++) {
1295 p = &iface->conf->tx_queue[i];
1297 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1298 p->cwmax, p->burst)) {
1299 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1300 "parameters for queue %d.", i);
1301 /* Continue anyway */
1307 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1308 struct mac_acl_entry *mac_acl,
1309 int n_entries, u8 accept_acl)
1311 struct hostapd_acl_params *acl_params;
1314 acl_params = os_zalloc(sizeof(*acl_params) +
1315 (n_entries * sizeof(acl_params->mac_acl[0])));
1319 for (i = 0; i < n_entries; i++)
1320 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1323 acl_params->acl_policy = accept_acl;
1324 acl_params->num_mac_acl = n_entries;
1326 err = hostapd_drv_set_acl(hapd, acl_params);
1328 os_free(acl_params);
1334 static void hostapd_set_acl(struct hostapd_data *hapd)
1336 struct hostapd_config *conf = hapd->iconf;
1340 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1343 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1345 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1346 conf->bss[0]->num_accept_mac,
1349 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1352 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1354 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1355 conf->bss[0]->num_deny_mac,
1358 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1365 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1367 if (!hapd->iface->interfaces ||
1368 !hapd->iface->interfaces->ctrl_iface_init)
1371 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1372 wpa_printf(MSG_ERROR,
1373 "Failed to setup control interface for %s",
1382 static int start_ctrl_iface(struct hostapd_iface *iface)
1386 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1389 for (i = 0; i < iface->num_bss; i++) {
1390 struct hostapd_data *hapd = iface->bss[i];
1391 if (iface->interfaces->ctrl_iface_init(hapd)) {
1392 wpa_printf(MSG_ERROR,
1393 "Failed to setup control interface for %s",
1403 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1405 struct hostapd_iface *iface = eloop_ctx;
1407 if (!iface->wait_channel_update) {
1408 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1413 * It is possible that the existing channel list is acceptable, so try
1416 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1417 setup_interface2(iface);
1421 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1423 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1426 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1427 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1428 setup_interface2(iface);
1432 static int setup_interface(struct hostapd_iface *iface)
1434 struct hostapd_data *hapd = iface->bss[0];
1438 * It is possible that setup_interface() is called after the interface
1439 * was disabled etc., in which case driver_ap_teardown is possibly set
1440 * to 1. Clear it here so any other key/station deletion, which is not
1441 * part of a teardown flow, would also call the relevant driver
1444 iface->driver_ap_teardown = 0;
1446 if (!iface->phy[0]) {
1447 const char *phy = hostapd_drv_get_radio_name(hapd);
1449 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1450 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1455 * Make sure that all BSSes get configured with a pointer to the same
1458 for (i = 1; i < iface->num_bss; i++) {
1459 iface->bss[i]->driver = hapd->driver;
1460 iface->bss[i]->drv_priv = hapd->drv_priv;
1463 if (hostapd_validate_bssid_configuration(iface))
1467 * Initialize control interfaces early to allow external monitoring of
1468 * channel setup operations that may take considerable amount of time
1469 * especially for DFS cases.
1471 if (start_ctrl_iface(iface))
1474 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1475 char country[4], previous_country[4];
1477 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1478 if (hostapd_get_country(hapd, previous_country) < 0)
1479 previous_country[0] = '\0';
1481 os_memcpy(country, hapd->iconf->country, 3);
1483 if (hostapd_set_country(hapd, country) < 0) {
1484 wpa_printf(MSG_ERROR, "Failed to set country code");
1488 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1489 previous_country, country);
1491 if (os_strncmp(previous_country, country, 2) != 0) {
1492 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1493 iface->wait_channel_update = 1;
1494 eloop_register_timeout(5, 0,
1495 channel_list_update_timeout,
1501 return setup_interface2(iface);
1505 static int setup_interface2(struct hostapd_iface *iface)
1507 iface->wait_channel_update = 0;
1509 if (hostapd_get_hw_features(iface)) {
1510 /* Not all drivers support this yet, so continue without hw
1513 int ret = hostapd_select_hw_mode(iface);
1515 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1516 "channel. (%d)", ret);
1520 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1523 ret = hostapd_check_ht_capab(iface);
1527 wpa_printf(MSG_DEBUG, "Interface initialization will "
1528 "be completed in a callback");
1532 if (iface->conf->ieee80211h)
1533 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1535 return hostapd_setup_interface_complete(iface, 0);
1538 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1539 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1540 if (iface->interfaces && iface->interfaces->terminate_on_error)
1548 static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1550 struct hostapd_data *hapd = ctx;
1552 return hapd->own_addr;
1556 static void fst_hostapd_get_channel_info_cb(void *ctx,
1557 enum hostapd_hw_mode *hw_mode,
1560 struct hostapd_data *hapd = ctx;
1562 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1566 static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1568 struct hostapd_data *hapd = ctx;
1570 if (hapd->iface->fst_ies != fst_ies) {
1571 hapd->iface->fst_ies = fst_ies;
1572 if (ieee802_11_set_beacon(hapd))
1573 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1578 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1581 struct hostapd_data *hapd = ctx;
1583 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1584 wpabuf_head(buf), wpabuf_len(buf));
1588 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1590 struct hostapd_data *hapd = ctx;
1591 struct sta_info *sta = ap_get_sta(hapd, addr);
1593 return sta ? sta->mb_ies : NULL;
1597 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1598 const u8 *buf, size_t size)
1600 struct hostapd_data *hapd = ctx;
1601 struct sta_info *sta = ap_get_sta(hapd, addr);
1604 struct mb_ies_info info;
1606 if (!mb_ies_info_by_ies(&info, buf, size)) {
1607 wpabuf_free(sta->mb_ies);
1608 sta->mb_ies = mb_ies_by_info(&info);
1614 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1617 struct sta_info *s = (struct sta_info *) *get_ctx;
1620 for (; s && !s->mb_ies; s = s->next)
1625 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1635 static const u8 * fst_hostapd_get_peer_first(void *ctx,
1636 struct fst_get_peer_ctx **get_ctx,
1639 struct hostapd_data *hapd = ctx;
1641 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1643 return fst_hostapd_get_sta(get_ctx, mb_only);
1647 static const u8 * fst_hostapd_get_peer_next(void *ctx,
1648 struct fst_get_peer_ctx **get_ctx,
1651 return fst_hostapd_get_sta(get_ctx, mb_only);
1655 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1656 struct fst_wpa_obj *iface_obj)
1658 iface_obj->ctx = hapd;
1659 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1660 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1661 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1662 iface_obj->send_action = fst_hostapd_send_action_cb;
1663 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1664 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1665 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1666 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1669 #endif /* CONFIG_FST */
1673 static enum nr_chan_width hostapd_get_nr_chan_width(struct hostapd_data *hapd,
1677 return NR_CHAN_WIDTH_20;
1678 if (!hapd->iconf->secondary_channel)
1679 return NR_CHAN_WIDTH_20;
1680 if (!vht || hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_USE_HT)
1681 return NR_CHAN_WIDTH_40;
1682 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80MHZ)
1683 return NR_CHAN_WIDTH_80;
1684 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_160MHZ)
1685 return NR_CHAN_WIDTH_160;
1686 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80P80MHZ)
1687 return NR_CHAN_WIDTH_80P80;
1688 return NR_CHAN_WIDTH_20;
1690 #endif /* NEED_AP_MLME */
1693 static void hostapd_set_own_neighbor_report(struct hostapd_data *hapd)
1696 u16 capab = hostapd_own_capab_info(hapd);
1697 int ht = hapd->iconf->ieee80211n && !hapd->conf->disable_11n;
1698 int vht = hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac;
1699 struct wpa_ssid_value ssid;
1700 u8 channel, op_class;
1701 u8 center_freq1_idx = 0, center_freq2_idx = 0;
1702 enum nr_chan_width width;
1706 if (!(hapd->conf->radio_measurements[0] &
1707 WLAN_RRM_CAPS_NEIGHBOR_REPORT))
1710 bssid_info = 3; /* AP is reachable */
1711 bssid_info |= NEI_REP_BSSID_INFO_SECURITY; /* "same as the AP" */
1712 bssid_info |= NEI_REP_BSSID_INFO_KEY_SCOPE; /* "same as the AP" */
1714 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT)
1715 bssid_info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
1717 bssid_info |= NEI_REP_BSSID_INFO_RM; /* RRM is supported */
1719 if (hapd->conf->wmm_enabled) {
1720 bssid_info |= NEI_REP_BSSID_INFO_QOS;
1722 if (hapd->conf->wmm_uapsd &&
1723 (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
1724 bssid_info |= NEI_REP_BSSID_INFO_APSD;
1728 bssid_info |= NEI_REP_BSSID_INFO_HT |
1729 NEI_REP_BSSID_INFO_DELAYED_BA;
1731 /* VHT bit added in IEEE P802.11-REVmc/D4.3 */
1733 bssid_info |= NEI_REP_BSSID_INFO_VHT;
1736 /* TODO: Set NEI_REP_BSSID_INFO_MOBILITY_DOMAIN if MDE is set */
1738 if (ieee80211_freq_to_channel_ext(hapd->iface->freq,
1739 hapd->iconf->secondary_channel,
1740 hapd->iconf->vht_oper_chwidth,
1741 &op_class, &channel) ==
1744 width = hostapd_get_nr_chan_width(hapd, ht, vht);
1746 center_freq1_idx = hapd->iconf->vht_oper_centr_freq_seg0_idx;
1747 if (width == NR_CHAN_WIDTH_80P80)
1749 hapd->iconf->vht_oper_centr_freq_seg1_idx;
1751 ieee80211_freq_to_chan(hapd->iface->freq +
1752 10 * hapd->iconf->secondary_channel,
1756 ssid.ssid_len = hapd->conf->ssid.ssid_len;
1757 os_memcpy(ssid.ssid, hapd->conf->ssid.ssid, ssid.ssid_len);
1760 * Neighbor Report element size = BSSID + BSSID info + op_class + chan +
1761 * phy type + wide bandwidth channel subelement.
1763 nr = wpabuf_alloc(ETH_ALEN + 4 + 1 + 1 + 1 + 5);
1767 wpabuf_put_data(nr, hapd->own_addr, ETH_ALEN);
1768 wpabuf_put_le32(nr, bssid_info);
1769 wpabuf_put_u8(nr, op_class);
1770 wpabuf_put_u8(nr, channel);
1771 wpabuf_put_u8(nr, ieee80211_get_phy_type(hapd->iface->freq, ht, vht));
1774 * Wide Bandwidth Channel subelement may be needed to allow the
1775 * receiving STA to send packets to the AP. See IEEE P802.11-REVmc/D5.0
1778 wpabuf_put_u8(nr, WNM_NEIGHBOR_WIDE_BW_CHAN);
1779 wpabuf_put_u8(nr, 3);
1780 wpabuf_put_u8(nr, width);
1781 wpabuf_put_u8(nr, center_freq1_idx);
1782 wpabuf_put_u8(nr, center_freq2_idx);
1784 hostapd_neighbor_set(hapd, hapd->own_addr, &ssid, nr, hapd->iconf->lci,
1785 hapd->iconf->civic, hapd->iconf->stationary_ap);
1788 #endif /* NEED_AP_MLME */
1794 static int hostapd_owe_iface_iter(struct hostapd_iface *iface, void *ctx)
1796 struct hostapd_data *hapd = ctx;
1799 for (i = 0; i < iface->num_bss; i++) {
1800 struct hostapd_data *bss = iface->bss[i];
1802 if (os_strcmp(hapd->conf->owe_transition_ifname,
1803 bss->conf->iface) != 0)
1806 wpa_printf(MSG_DEBUG,
1807 "OWE: ifname=%s found transition mode ifname=%s BSSID "
1809 hapd->conf->iface, bss->conf->iface,
1810 MAC2STR(bss->own_addr),
1811 wpa_ssid_txt(bss->conf->ssid.ssid,
1812 bss->conf->ssid.ssid_len));
1813 if (!bss->conf->ssid.ssid_set || !bss->conf->ssid.ssid_len ||
1814 is_zero_ether_addr(bss->own_addr))
1817 os_memcpy(hapd->conf->owe_transition_bssid, bss->own_addr,
1819 os_memcpy(hapd->conf->owe_transition_ssid,
1820 bss->conf->ssid.ssid, bss->conf->ssid.ssid_len);
1821 hapd->conf->owe_transition_ssid_len = bss->conf->ssid.ssid_len;
1822 wpa_printf(MSG_DEBUG,
1823 "OWE: Copied transition mode information");
1831 int hostapd_owe_trans_get_info(struct hostapd_data *hapd)
1833 if (hapd->conf->owe_transition_ssid_len > 0 &&
1834 !is_zero_ether_addr(hapd->conf->owe_transition_bssid))
1837 /* Find transition mode SSID/BSSID information from a BSS operated by
1838 * this hostapd instance. */
1839 if (!hapd->iface->interfaces ||
1840 !hapd->iface->interfaces->for_each_interface)
1841 return hostapd_owe_iface_iter(hapd->iface, hapd);
1843 return hapd->iface->interfaces->for_each_interface(
1844 hapd->iface->interfaces, hostapd_owe_iface_iter, hapd);
1848 static int hostapd_owe_iface_iter2(struct hostapd_iface *iface, void *ctx)
1852 for (i = 0; i < iface->num_bss; i++) {
1853 struct hostapd_data *bss = iface->bss[i];
1856 if (!bss->conf->owe_transition_ifname[0])
1858 res = hostapd_owe_trans_get_info(bss);
1861 wpa_printf(MSG_DEBUG,
1862 "OWE: Matching transition mode interface enabled - update beacon data for %s",
1864 ieee802_11_set_beacon(bss);
1870 #endif /* CONFIG_OWE */
1873 static void hostapd_owe_update_trans(struct hostapd_iface *iface)
1876 /* Check whether the enabled BSS can complete OWE transition mode
1877 * configuration for any pending interface. */
1878 if (!iface->interfaces ||
1879 !iface->interfaces->for_each_interface)
1880 hostapd_owe_iface_iter2(iface, NULL);
1882 iface->interfaces->for_each_interface(
1883 iface->interfaces, hostapd_owe_iface_iter2, NULL);
1884 #endif /* CONFIG_OWE */
1888 static void hostapd_interface_setup_failure_handler(void *eloop_ctx,
1891 struct hostapd_iface *iface = eloop_ctx;
1892 struct hostapd_data *hapd;
1894 if (iface->num_bss < 1 || !iface->bss || !iface->bss[0])
1896 hapd = iface->bss[0];
1897 if (hapd->setup_complete_cb)
1898 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1902 static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1905 struct hostapd_data *hapd = iface->bss[0];
1908 int delay_apply_cfg = 0;
1909 int res_dfs_offload = 0;
1914 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1915 if (iface->conf->channel) {
1918 #endif /* NEED_AP_MLME */
1920 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1921 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1922 "Frequency: %d MHz",
1923 hostapd_hw_mode_txt(iface->conf->hw_mode),
1924 iface->conf->channel, iface->freq);
1927 /* Handle DFS only if it is not offloaded to the driver */
1928 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1930 res = hostapd_handle_dfs(iface);
1937 /* If DFS is offloaded to the driver */
1938 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1939 if (res_dfs_offload <= 0) {
1940 if (res_dfs_offload < 0)
1943 wpa_printf(MSG_DEBUG,
1944 "Proceed with AP/channel setup");
1946 * If this is a DFS channel, move to completing
1949 if (res_dfs_offload == 1)
1951 /* Otherwise fall through. */
1954 #endif /* NEED_AP_MLME */
1957 if (iface->mconf != NULL) {
1958 wpa_printf(MSG_DEBUG,
1959 "%s: Mesh configuration will be applied while joining the mesh network",
1960 iface->bss[0]->conf->iface);
1961 delay_apply_cfg = 1;
1963 #endif /* CONFIG_MESH */
1965 if (!delay_apply_cfg &&
1966 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1967 hapd->iconf->channel,
1968 hapd->iconf->ieee80211n,
1969 hapd->iconf->ieee80211ac,
1970 hapd->iconf->secondary_channel,
1971 hapd->iconf->vht_oper_chwidth,
1972 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1973 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1974 wpa_printf(MSG_ERROR, "Could not set channel for "
1980 if (iface->current_mode) {
1981 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1982 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1984 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1985 HOSTAPD_LEVEL_WARNING,
1986 "Failed to prepare rates table.");
1991 if (hapd->iconf->rts_threshold > -1 &&
1992 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1993 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1998 if (hapd->iconf->fragm_threshold > -1 &&
1999 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
2000 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
2001 "for kernel driver");
2005 prev_addr = hapd->own_addr;
2007 for (j = 0; j < iface->num_bss; j++) {
2008 hapd = iface->bss[j];
2010 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
2011 if (hostapd_setup_bss(hapd, j == 0)) {
2013 hapd = iface->bss[j];
2014 hostapd_bss_deinit_no_free(hapd);
2015 hostapd_free_hapd_data(hapd);
2019 if (is_zero_ether_addr(hapd->conf->bssid))
2020 prev_addr = hapd->own_addr;
2022 hapd = iface->bss[0];
2024 hostapd_tx_queue_params(iface);
2026 ap_list_init(iface);
2028 hostapd_set_acl(hapd);
2030 if (hostapd_driver_commit(hapd) < 0) {
2031 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
2032 "configuration", __func__);
2037 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
2038 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
2039 * mode), the interface is up only after driver_commit, so initialize
2040 * WPS after driver_commit.
2042 for (j = 0; j < iface->num_bss; j++) {
2043 if (hostapd_init_wps_complete(iface->bss[j]))
2047 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
2050 * If freq is DFS, and DFS is offloaded to the driver, then wait
2051 * for CAC to complete.
2053 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
2054 return res_dfs_offload;
2059 #endif /* NEED_AP_MLME */
2062 if (hapd->iconf->fst_cfg.group_id[0]) {
2063 struct fst_wpa_obj iface_obj;
2065 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2066 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
2067 &iface_obj, &hapd->iconf->fst_cfg);
2069 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
2070 hapd->iconf->fst_cfg.group_id);
2074 #endif /* CONFIG_FST */
2076 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
2077 hostapd_owe_update_trans(iface);
2078 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
2079 if (hapd->setup_complete_cb)
2080 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
2082 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
2083 iface->bss[0]->conf->iface);
2084 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
2085 iface->interfaces->terminate_on_error--;
2087 for (j = 0; j < iface->num_bss; j++)
2088 hostapd_set_own_neighbor_report(iface->bss[j]);
2093 wpa_printf(MSG_ERROR, "Interface initialization failed");
2094 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2095 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2098 fst_detach(iface->fst);
2101 #endif /* CONFIG_FST */
2103 if (iface->interfaces && iface->interfaces->terminate_on_error) {
2105 } else if (hapd->setup_complete_cb) {
2107 * Calling hapd->setup_complete_cb directly may cause iface
2108 * deinitialization which may be accessed later by the caller.
2110 eloop_register_timeout(0, 0,
2111 hostapd_interface_setup_failure_handler,
2120 * hostapd_setup_interface_complete - Complete interface setup
2122 * This function is called when previous steps in the interface setup has been
2123 * completed. This can also start operations, e.g., DFS, that will require
2124 * additional processing before interface is ready to be enabled. Such
2125 * operations will call this function from eloop callbacks when finished.
2127 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
2129 struct hapd_interfaces *interfaces = iface->interfaces;
2130 struct hostapd_data *hapd = iface->bss[0];
2132 int not_ready_in_sync_ifaces = 0;
2134 if (!iface->need_to_start_in_sync)
2135 return hostapd_setup_interface_complete_sync(iface, err);
2138 wpa_printf(MSG_ERROR, "Interface initialization failed");
2139 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2140 iface->need_to_start_in_sync = 0;
2141 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2142 if (interfaces && interfaces->terminate_on_error)
2147 if (iface->ready_to_start_in_sync) {
2148 /* Already in ready and waiting. should never happpen */
2152 for (i = 0; i < interfaces->count; i++) {
2153 if (interfaces->iface[i]->need_to_start_in_sync &&
2154 !interfaces->iface[i]->ready_to_start_in_sync)
2155 not_ready_in_sync_ifaces++;
2159 * Check if this is the last interface, if yes then start all the other
2160 * waiting interfaces. If not, add this interface to the waiting list.
2162 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
2164 * If this interface went through CAC, do not synchronize, just
2165 * start immediately.
2167 iface->need_to_start_in_sync = 0;
2168 wpa_printf(MSG_INFO,
2169 "%s: Finished CAC - bypass sync and start interface",
2170 iface->bss[0]->conf->iface);
2171 return hostapd_setup_interface_complete_sync(iface, err);
2174 if (not_ready_in_sync_ifaces > 1) {
2175 /* need to wait as there are other interfaces still coming up */
2176 iface->ready_to_start_in_sync = 1;
2177 wpa_printf(MSG_INFO,
2178 "%s: Interface waiting to sync with other interfaces",
2179 iface->bss[0]->conf->iface);
2183 wpa_printf(MSG_INFO,
2184 "%s: Last interface to sync - starting all interfaces",
2185 iface->bss[0]->conf->iface);
2186 iface->need_to_start_in_sync = 0;
2187 hostapd_setup_interface_complete_sync(iface, err);
2188 for (i = 0; i < interfaces->count; i++) {
2189 if (interfaces->iface[i]->need_to_start_in_sync &&
2190 interfaces->iface[i]->ready_to_start_in_sync) {
2191 hostapd_setup_interface_complete_sync(
2192 interfaces->iface[i], 0);
2193 /* Only once the interfaces are sync started */
2194 interfaces->iface[i]->need_to_start_in_sync = 0;
2203 * hostapd_setup_interface - Setup of an interface
2204 * @iface: Pointer to interface data.
2205 * Returns: 0 on success, -1 on failure
2207 * Initializes the driver interface, validates the configuration,
2208 * and sets driver parameters based on the configuration.
2209 * Flushes old stations, sets the channel, encryption,
2210 * beacons, and WDS links based on the configuration.
2212 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
2213 * or DFS operations, this function returns 0 before such operations have been
2214 * completed. The pending operations are registered into eloop and will be
2215 * completed from eloop callbacks. Those callbacks end up calling
2216 * hostapd_setup_interface_complete() once setup has been completed.
2218 int hostapd_setup_interface(struct hostapd_iface *iface)
2222 ret = setup_interface(iface);
2224 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
2225 iface->bss[0]->conf->iface);
2234 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
2235 * @hapd_iface: Pointer to interface data
2236 * @conf: Pointer to per-interface configuration
2237 * @bss: Pointer to per-BSS configuration for this BSS
2238 * Returns: Pointer to allocated BSS data
2240 * This function is used to allocate per-BSS data structure. This data will be
2241 * freed after hostapd_cleanup() is called for it during interface
2244 struct hostapd_data *
2245 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
2246 struct hostapd_config *conf,
2247 struct hostapd_bss_config *bss)
2249 struct hostapd_data *hapd;
2251 hapd = os_zalloc(sizeof(*hapd));
2255 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
2258 hapd->iface = hapd_iface;
2260 hapd->driver = conf->driver;
2261 hapd->ctrl_sock = -1;
2262 dl_list_init(&hapd->ctrl_dst);
2263 dl_list_init(&hapd->nr_db);
2264 hapd->dhcp_sock = -1;
2265 #ifdef CONFIG_IEEE80211R_AP
2266 dl_list_init(&hapd->l2_queue);
2267 dl_list_init(&hapd->l2_oui_queue);
2268 #endif /* CONFIG_IEEE80211R_AP */
2274 static void hostapd_bss_deinit(struct hostapd_data *hapd)
2278 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
2280 hostapd_bss_deinit_no_free(hapd);
2281 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2282 hostapd_cleanup(hapd);
2286 void hostapd_interface_deinit(struct hostapd_iface *iface)
2290 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2294 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2296 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2297 iface->wait_channel_update = 0;
2301 fst_detach(iface->fst);
2304 #endif /* CONFIG_FST */
2306 for (j = iface->num_bss - 1; j >= 0; j--) {
2309 hostapd_bss_deinit(iface->bss[j]);
2312 #ifdef CONFIG_IEEE80211N
2314 hostapd_stop_setup_timers(iface);
2315 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2316 #endif /* NEED_AP_MLME */
2317 #endif /* CONFIG_IEEE80211N */
2321 void hostapd_interface_free(struct hostapd_iface *iface)
2324 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2325 for (j = 0; j < iface->num_bss; j++) {
2328 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2329 __func__, iface->bss[j]);
2330 os_free(iface->bss[j]);
2332 hostapd_cleanup_iface(iface);
2336 struct hostapd_iface * hostapd_alloc_iface(void)
2338 struct hostapd_iface *hapd_iface;
2340 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2344 dl_list_init(&hapd_iface->sta_seen);
2351 * hostapd_init - Allocate and initialize per-interface data
2352 * @config_file: Path to the configuration file
2353 * Returns: Pointer to the allocated interface data or %NULL on failure
2355 * This function is used to allocate main data structures for per-interface
2356 * data. The allocated data buffer will be freed by calling
2357 * hostapd_cleanup_iface().
2359 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2360 const char *config_file)
2362 struct hostapd_iface *hapd_iface = NULL;
2363 struct hostapd_config *conf = NULL;
2364 struct hostapd_data *hapd;
2367 hapd_iface = hostapd_alloc_iface();
2368 if (hapd_iface == NULL)
2371 hapd_iface->config_fname = os_strdup(config_file);
2372 if (hapd_iface->config_fname == NULL)
2375 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2378 hapd_iface->conf = conf;
2380 hapd_iface->num_bss = conf->num_bss;
2381 hapd_iface->bss = os_calloc(conf->num_bss,
2382 sizeof(struct hostapd_data *));
2383 if (hapd_iface->bss == NULL)
2386 for (i = 0; i < conf->num_bss; i++) {
2387 hapd = hapd_iface->bss[i] =
2388 hostapd_alloc_bss_data(hapd_iface, conf,
2392 hapd->msg_ctx = hapd;
2398 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2401 hostapd_config_free(conf);
2403 os_free(hapd_iface->config_fname);
2404 os_free(hapd_iface->bss);
2405 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2406 __func__, hapd_iface);
2407 os_free(hapd_iface);
2413 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2417 for (i = 0; i < interfaces->count; i++) {
2418 struct hostapd_iface *iface = interfaces->iface[i];
2419 for (j = 0; j < iface->num_bss; j++) {
2420 struct hostapd_data *hapd = iface->bss[j];
2421 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2431 * hostapd_interface_init_bss - Read configuration file and init BSS data
2433 * This function is used to parse configuration file for a BSS. This BSS is
2434 * added to an existing interface sharing the same radio (if any) or a new
2435 * interface is created if this is the first interface on a radio. This
2436 * allocate memory for the BSS. No actual driver operations are started.
2438 * This is similar to hostapd_interface_init(), but for a case where the
2439 * configuration is used to add a single BSS instead of all BSSes for a radio.
2441 struct hostapd_iface *
2442 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2443 const char *config_fname, int debug)
2445 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2446 struct hostapd_data *hapd;
2453 for (i = 0; i < interfaces->count; i++) {
2454 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2455 iface = interfaces->iface[i];
2460 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2461 config_fname, phy, iface ? "" : " --> new PHY");
2463 struct hostapd_config *conf;
2464 struct hostapd_bss_config **tmp_conf;
2465 struct hostapd_data **tmp_bss;
2466 struct hostapd_bss_config *bss;
2469 /* Add new BSS to existing iface */
2470 conf = interfaces->config_read_cb(config_fname);
2473 if (conf->num_bss > 1) {
2474 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2475 hostapd_config_free(conf);
2479 ifname = conf->bss[0]->iface;
2480 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2481 wpa_printf(MSG_ERROR,
2482 "Interface name %s already in use", ifname);
2483 hostapd_config_free(conf);
2487 tmp_conf = os_realloc_array(
2488 iface->conf->bss, iface->conf->num_bss + 1,
2489 sizeof(struct hostapd_bss_config *));
2490 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2491 sizeof(struct hostapd_data *));
2493 iface->bss = tmp_bss;
2495 iface->conf->bss = tmp_conf;
2496 iface->conf->last_bss = tmp_conf[0];
2498 if (tmp_bss == NULL || tmp_conf == NULL) {
2499 hostapd_config_free(conf);
2502 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2503 iface->conf->num_bss++;
2505 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2507 iface->conf->num_bss--;
2508 hostapd_config_free(conf);
2511 iface->conf->last_bss = bss;
2512 iface->bss[iface->num_bss] = hapd;
2513 hapd->msg_ctx = hapd;
2515 bss_idx = iface->num_bss++;
2517 conf->bss[0] = NULL;
2518 hostapd_config_free(conf);
2520 /* Add a new iface with the first BSS */
2521 new_iface = iface = hostapd_init(interfaces, config_fname);
2524 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2525 iface->interfaces = interfaces;
2529 for (k = 0; k < debug; k++) {
2530 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2531 iface->bss[bss_idx]->conf->logger_stdout_level--;
2534 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2535 !hostapd_drv_none(iface->bss[bss_idx])) {
2536 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2539 hostapd_interface_deinit_free(new_iface);
2547 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2549 const struct wpa_driver_ops *driver;
2552 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2555 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2556 __func__, (unsigned int) iface->num_bss,
2557 (unsigned int) iface->conf->num_bss);
2558 driver = iface->bss[0]->driver;
2559 drv_priv = iface->bss[0]->drv_priv;
2560 hostapd_interface_deinit(iface);
2561 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2562 __func__, driver, drv_priv);
2563 if (driver && driver->hapd_deinit && drv_priv) {
2564 driver->hapd_deinit(drv_priv);
2565 iface->bss[0]->drv_priv = NULL;
2567 hostapd_interface_free(iface);
2571 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2573 struct hostapd_iface *hapd_iface)
2577 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2578 __func__, driver, drv_priv);
2579 if (driver && driver->hapd_deinit && drv_priv) {
2580 driver->hapd_deinit(drv_priv);
2581 for (j = 0; j < hapd_iface->num_bss; j++) {
2582 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2584 hapd_iface->bss[j]->drv_priv);
2585 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2586 hapd_iface->bss[j]->drv_priv = NULL;
2592 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2596 if (hapd_iface->bss[0]->drv_priv != NULL) {
2597 wpa_printf(MSG_ERROR, "Interface %s already enabled",
2598 hapd_iface->conf->bss[0]->iface);
2602 wpa_printf(MSG_DEBUG, "Enable interface %s",
2603 hapd_iface->conf->bss[0]->iface);
2605 for (j = 0; j < hapd_iface->num_bss; j++)
2606 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2607 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2608 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2612 if (hapd_iface->interfaces == NULL ||
2613 hapd_iface->interfaces->driver_init == NULL ||
2614 hapd_iface->interfaces->driver_init(hapd_iface))
2617 if (hostapd_setup_interface(hapd_iface)) {
2618 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2619 hapd_iface->bss[0]->drv_priv,
2628 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2632 wpa_printf(MSG_DEBUG, "Reload interface %s",
2633 hapd_iface->conf->bss[0]->iface);
2634 for (j = 0; j < hapd_iface->num_bss; j++)
2635 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2636 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2637 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2640 hostapd_clear_old(hapd_iface);
2641 for (j = 0; j < hapd_iface->num_bss; j++)
2642 hostapd_reload_bss(hapd_iface->bss[j]);
2648 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2651 const struct wpa_driver_ops *driver;
2654 if (hapd_iface == NULL)
2657 if (hapd_iface->bss[0]->drv_priv == NULL) {
2658 wpa_printf(MSG_INFO, "Interface %s already disabled",
2659 hapd_iface->conf->bss[0]->iface);
2663 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2664 driver = hapd_iface->bss[0]->driver;
2665 drv_priv = hapd_iface->bss[0]->drv_priv;
2667 hapd_iface->driver_ap_teardown =
2668 !!(hapd_iface->drv_flags &
2669 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2672 for (j = 0; j < hapd_iface->num_bss; j++)
2673 hostapd_cleanup_cs_params(hapd_iface->bss[j]);
2674 #endif /* NEED_AP_MLME */
2676 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
2677 for (j = 0; j < hapd_iface->num_bss; j++) {
2678 struct hostapd_data *hapd = hapd_iface->bss[j];
2679 hostapd_bss_deinit_no_free(hapd);
2680 hostapd_free_hapd_data(hapd);
2683 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
2685 /* From hostapd_cleanup_iface: These were initialized in
2686 * hostapd_setup_interface and hostapd_setup_interface_complete
2688 hostapd_cleanup_iface_partial(hapd_iface);
2690 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2691 hapd_iface->bss[0]->conf->iface);
2692 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2697 static struct hostapd_iface *
2698 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2700 struct hostapd_iface **iface, *hapd_iface;
2702 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2703 sizeof(struct hostapd_iface *));
2706 interfaces->iface = iface;
2707 hapd_iface = interfaces->iface[interfaces->count] =
2708 hostapd_alloc_iface();
2709 if (hapd_iface == NULL) {
2710 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2711 "the interface", __func__);
2714 interfaces->count++;
2715 hapd_iface->interfaces = interfaces;
2721 static struct hostapd_config *
2722 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2723 const char *ctrl_iface, const char *driver)
2725 struct hostapd_bss_config *bss;
2726 struct hostapd_config *conf;
2728 /* Allocates memory for bss and conf */
2729 conf = hostapd_config_defaults();
2731 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2732 "configuration", __func__);
2739 for (j = 0; wpa_drivers[j]; j++) {
2740 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2741 conf->driver = wpa_drivers[j];
2746 wpa_printf(MSG_ERROR,
2747 "Invalid/unknown driver '%s' - registering the default driver",
2751 conf->driver = wpa_drivers[0];
2752 if (conf->driver == NULL) {
2753 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2754 hostapd_config_free(conf);
2759 bss = conf->last_bss = conf->bss[0];
2761 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2762 bss->ctrl_interface = os_strdup(ctrl_iface);
2763 if (bss->ctrl_interface == NULL) {
2764 hostapd_config_free(conf);
2768 /* Reading configuration file skipped, will be done in SET!
2769 * From reading the configuration till the end has to be done in
2776 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2777 struct hostapd_config *conf)
2780 struct hostapd_data *hapd;
2782 hapd_iface->bss = os_calloc(conf->num_bss,
2783 sizeof(struct hostapd_data *));
2784 if (hapd_iface->bss == NULL)
2787 for (i = 0; i < conf->num_bss; i++) {
2788 hapd = hapd_iface->bss[i] =
2789 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2793 os_free(hapd_iface->bss[i]);
2794 hapd_iface->bss[i] = NULL;
2796 os_free(hapd_iface->bss);
2797 hapd_iface->bss = NULL;
2800 hapd->msg_ctx = hapd;
2803 hapd_iface->conf = conf;
2804 hapd_iface->num_bss = conf->num_bss;
2810 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2812 struct hostapd_config *conf = NULL;
2813 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2814 struct hostapd_data *hapd;
2817 const char *conf_file = NULL, *phy_name = NULL;
2819 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2821 phy_name = buf + 11;
2822 pos = os_strchr(phy_name, ':');
2827 if (!os_strlen(conf_file))
2830 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2834 for (j = 0; j < interfaces->count; j++) {
2835 if (interfaces->iface[j] == hapd_iface)
2838 if (j == interfaces->count) {
2839 struct hostapd_iface **tmp;
2840 tmp = os_realloc_array(interfaces->iface,
2841 interfaces->count + 1,
2842 sizeof(struct hostapd_iface *));
2844 hostapd_interface_deinit_free(hapd_iface);
2847 interfaces->iface = tmp;
2848 interfaces->iface[interfaces->count++] = hapd_iface;
2849 new_iface = hapd_iface;
2853 if (interfaces->driver_init(hapd_iface))
2856 if (hostapd_setup_interface(hapd_iface)) {
2857 hostapd_deinit_driver(
2858 hapd_iface->bss[0]->driver,
2859 hapd_iface->bss[0]->drv_priv,
2864 /* Assign new BSS with bss[0]'s driver info */
2865 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2866 hapd->driver = hapd_iface->bss[0]->driver;
2867 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2868 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2871 if (start_ctrl_iface_bss(hapd) < 0 ||
2872 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2873 hostapd_setup_bss(hapd, -1))) {
2874 hostapd_cleanup(hapd);
2875 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2876 hapd_iface->conf->num_bss--;
2877 hapd_iface->num_bss--;
2878 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2879 __func__, hapd, hapd->conf->iface);
2880 hostapd_config_free_bss(hapd->conf);
2886 hostapd_owe_update_trans(hapd_iface);
2890 ptr = os_strchr(buf, ' ');
2895 if (os_strncmp(ptr, "config=", 7) == 0)
2896 conf_file = ptr + 7;
2898 for (i = 0; i < interfaces->count; i++) {
2899 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2901 wpa_printf(MSG_INFO, "Cannot add interface - it "
2907 hapd_iface = hostapd_iface_alloc(interfaces);
2908 if (hapd_iface == NULL) {
2909 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2910 "for interface", __func__);
2913 new_iface = hapd_iface;
2915 if (conf_file && interfaces->config_read_cb) {
2916 conf = interfaces->config_read_cb(conf_file);
2917 if (conf && conf->bss)
2918 os_strlcpy(conf->bss[0]->iface, buf,
2919 sizeof(conf->bss[0]->iface));
2921 char *driver = os_strchr(ptr, ' ');
2925 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2928 if (conf == NULL || conf->bss == NULL) {
2929 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2930 "for configuration", __func__);
2934 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2935 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2936 "for hostapd", __func__);
2941 if (start_ctrl_iface(hapd_iface) < 0)
2944 wpa_printf(MSG_INFO, "Add interface '%s'",
2945 hapd_iface->conf->bss[0]->iface);
2951 hostapd_config_free(conf);
2953 if (hapd_iface->bss) {
2954 for (i = 0; i < hapd_iface->num_bss; i++) {
2955 hapd = hapd_iface->bss[i];
2958 if (hapd_iface->interfaces &&
2959 hapd_iface->interfaces->ctrl_iface_deinit)
2960 hapd_iface->interfaces->
2961 ctrl_iface_deinit(hapd);
2962 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2963 __func__, hapd_iface->bss[i],
2965 hostapd_cleanup(hapd);
2967 hapd_iface->bss[i] = NULL;
2969 os_free(hapd_iface->bss);
2970 hapd_iface->bss = NULL;
2973 interfaces->count--;
2974 interfaces->iface[interfaces->count] = NULL;
2976 hostapd_cleanup_iface(hapd_iface);
2982 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2986 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2988 /* Remove hostapd_data only if it has already been initialized */
2989 if (idx < iface->num_bss) {
2990 struct hostapd_data *hapd = iface->bss[idx];
2992 hostapd_bss_deinit(hapd);
2993 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2994 __func__, hapd, hapd->conf->iface);
2995 hostapd_config_free_bss(hapd->conf);
3001 for (i = idx; i < iface->num_bss; i++)
3002 iface->bss[i] = iface->bss[i + 1];
3004 hostapd_config_free_bss(iface->conf->bss[idx]);
3005 iface->conf->bss[idx] = NULL;
3008 iface->conf->num_bss--;
3009 for (i = idx; i < iface->conf->num_bss; i++)
3010 iface->conf->bss[i] = iface->conf->bss[i + 1];
3016 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
3018 struct hostapd_iface *hapd_iface;
3021 for (i = 0; i < interfaces->count; i++) {
3022 hapd_iface = interfaces->iface[i];
3023 if (hapd_iface == NULL)
3025 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
3026 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
3027 hapd_iface->driver_ap_teardown =
3028 !!(hapd_iface->drv_flags &
3029 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3031 hostapd_interface_deinit_free(hapd_iface);
3033 while (k < (interfaces->count - 1)) {
3034 interfaces->iface[k] =
3035 interfaces->iface[k + 1];
3038 interfaces->count--;
3042 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
3043 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
3044 hapd_iface->driver_ap_teardown =
3045 !(hapd_iface->drv_flags &
3046 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
3047 return hostapd_remove_bss(hapd_iface, j);
3056 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
3057 * @hapd: Pointer to BSS data
3058 * @sta: Pointer to the associated STA data
3059 * @reassoc: 1 to indicate this was a re-association; 0 = first association
3061 * This function will be called whenever a station associates with the AP. It
3062 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
3063 * from drv_callbacks.c based on driver events for drivers that take care of
3064 * management frames (IEEE 802.11 authentication and association) internally.
3066 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
3069 if (hapd->tkip_countermeasures) {
3070 hostapd_drv_sta_deauth(hapd, sta->addr,
3071 WLAN_REASON_MICHAEL_MIC_FAILURE);
3075 hostapd_prune_associations(hapd, sta->addr);
3076 ap_sta_clear_disconnect_timeouts(hapd, sta);
3078 /* IEEE 802.11F (IAPP) */
3079 if (hapd->conf->ieee802_11f)
3080 iapp_new_station(hapd->iapp, sta);
3083 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
3084 sta->no_p2p_set = 1;
3085 hapd->num_sta_no_p2p++;
3086 if (hapd->num_sta_no_p2p == 1)
3087 hostapd_p2p_non_p2p_sta_connected(hapd);
3089 #endif /* CONFIG_P2P */
3091 /* Start accounting here, if IEEE 802.1X and WPA are not used.
3092 * IEEE 802.1X/WPA code will start accounting after the station has
3093 * been authorized. */
3094 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
3095 ap_sta_set_authorized(hapd, sta, 1);
3096 os_get_reltime(&sta->connected_time);
3097 accounting_sta_start(hapd, sta);
3100 /* Start IEEE 802.1X authentication process for new stations */
3101 ieee802_1x_new_station(hapd, sta);
3103 if (sta->auth_alg != WLAN_AUTH_FT &&
3104 sta->auth_alg != WLAN_AUTH_FILS_SK &&
3105 sta->auth_alg != WLAN_AUTH_FILS_SK_PFS &&
3106 sta->auth_alg != WLAN_AUTH_FILS_PK &&
3107 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
3108 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
3110 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
3112 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) {
3113 if (eloop_cancel_timeout(ap_handle_timer, hapd, sta) > 0) {
3114 wpa_printf(MSG_DEBUG,
3115 "%s: %s: canceled wired ap_handle_timer timeout for "
3117 hapd->conf->iface, __func__,
3118 MAC2STR(sta->addr));
3120 } else if (!(hapd->iface->drv_flags &
3121 WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
3122 wpa_printf(MSG_DEBUG,
3123 "%s: %s: reschedule ap_handle_timer timeout for "
3124 MACSTR " (%d seconds - ap_max_inactivity)",
3125 hapd->conf->iface, __func__, MAC2STR(sta->addr),
3126 hapd->conf->ap_max_inactivity);
3127 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
3128 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
3129 ap_handle_timer, hapd, sta);
3134 const char * hostapd_state_text(enum hostapd_iface_state s)
3137 case HAPD_IFACE_UNINITIALIZED:
3138 return "UNINITIALIZED";
3139 case HAPD_IFACE_DISABLED:
3141 case HAPD_IFACE_COUNTRY_UPDATE:
3142 return "COUNTRY_UPDATE";
3143 case HAPD_IFACE_ACS:
3145 case HAPD_IFACE_HT_SCAN:
3147 case HAPD_IFACE_DFS:
3149 case HAPD_IFACE_ENABLED:
3157 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
3159 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
3160 iface->conf ? iface->conf->bss[0]->iface : "N/A",
3161 hostapd_state_text(iface->state), hostapd_state_text(s));
3166 int hostapd_csa_in_progress(struct hostapd_iface *iface)
3170 for (i = 0; i < iface->num_bss; i++)
3171 if (iface->bss[i]->csa_in_progress)
3179 static void free_beacon_data(struct beacon_data *beacon)
3181 os_free(beacon->head);
3182 beacon->head = NULL;
3183 os_free(beacon->tail);
3184 beacon->tail = NULL;
3185 os_free(beacon->probe_resp);
3186 beacon->probe_resp = NULL;
3187 os_free(beacon->beacon_ies);
3188 beacon->beacon_ies = NULL;
3189 os_free(beacon->proberesp_ies);
3190 beacon->proberesp_ies = NULL;
3191 os_free(beacon->assocresp_ies);
3192 beacon->assocresp_ies = NULL;
3196 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
3197 struct beacon_data *beacon)
3199 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
3200 struct wpa_driver_ap_params params;
3203 os_memset(beacon, 0, sizeof(*beacon));
3204 ret = ieee802_11_build_ap_params(hapd, ¶ms);
3208 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
3212 goto free_ap_params;
3215 beacon->head = os_memdup(params.head, params.head_len);
3217 goto free_ap_extra_ies;
3219 beacon->head_len = params.head_len;
3221 beacon->tail = os_memdup(params.tail, params.tail_len);
3225 beacon->tail_len = params.tail_len;
3227 if (params.proberesp != NULL) {
3228 beacon->probe_resp = os_memdup(params.proberesp,
3229 params.proberesp_len);
3230 if (!beacon->probe_resp)
3233 beacon->probe_resp_len = params.proberesp_len;
3236 /* copy the extra ies */
3238 beacon->beacon_ies = os_memdup(beacon_extra->buf,
3239 wpabuf_len(beacon_extra));
3240 if (!beacon->beacon_ies)
3243 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
3246 if (proberesp_extra) {
3247 beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
3248 wpabuf_len(proberesp_extra));
3249 if (!beacon->proberesp_ies)
3252 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
3255 if (assocresp_extra) {
3256 beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
3257 wpabuf_len(assocresp_extra));
3258 if (!beacon->assocresp_ies)
3261 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
3266 /* if the function fails, the caller should not free beacon data */
3268 free_beacon_data(beacon);
3271 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
3274 ieee802_11_free_ap_params(¶ms);
3280 * TODO: This flow currently supports only changing channel and width within
3281 * the same hw_mode. Any other changes to MAC parameters or provided settings
3282 * are not supported.
3284 static int hostapd_change_config_freq(struct hostapd_data *hapd,
3285 struct hostapd_config *conf,
3286 struct hostapd_freq_params *params,
3287 struct hostapd_freq_params *old_params)
3291 if (!params->channel) {
3292 /* check if the new channel is supported by hw */
3293 params->channel = hostapd_hw_get_channel(hapd, params->freq);
3296 channel = params->channel;
3300 /* if a pointer to old_params is provided we save previous state */
3302 hostapd_set_freq_params(old_params, conf->hw_mode,
3303 hostapd_hw_get_freq(hapd, conf->channel),
3304 conf->channel, conf->ieee80211n,
3306 conf->secondary_channel,
3307 conf->vht_oper_chwidth,
3308 conf->vht_oper_centr_freq_seg0_idx,
3309 conf->vht_oper_centr_freq_seg1_idx,
3313 switch (params->bandwidth) {
3317 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
3320 if (params->center_freq2)
3321 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
3323 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
3326 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
3332 conf->channel = channel;
3333 conf->ieee80211n = params->ht_enabled;
3334 conf->secondary_channel = params->sec_channel_offset;
3335 ieee80211_freq_to_chan(params->center_freq1,
3336 &conf->vht_oper_centr_freq_seg0_idx);
3337 ieee80211_freq_to_chan(params->center_freq2,
3338 &conf->vht_oper_centr_freq_seg1_idx);
3340 /* TODO: maybe call here hostapd_config_check here? */
3346 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
3347 struct csa_settings *settings)
3349 struct hostapd_iface *iface = hapd->iface;
3350 struct hostapd_freq_params old_freq;
3352 u8 chan, vht_bandwidth;
3354 os_memset(&old_freq, 0, sizeof(old_freq));
3355 if (!iface || !iface->freq || hapd->csa_in_progress)
3358 switch (settings->freq_params.bandwidth) {
3360 if (settings->freq_params.center_freq2)
3361 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
3363 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
3366 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
3369 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
3373 if (ieee80211_freq_to_channel_ext(
3374 settings->freq_params.freq,
3375 settings->freq_params.sec_channel_offset,
3377 &hapd->iface->cs_oper_class,
3378 &chan) == NUM_HOSTAPD_MODES) {
3379 wpa_printf(MSG_DEBUG,
3380 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
3381 settings->freq_params.freq,
3382 settings->freq_params.sec_channel_offset,
3383 settings->freq_params.vht_enabled);
3387 settings->freq_params.channel = chan;
3389 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3390 &settings->freq_params,
3395 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
3397 /* change back the configuration */
3398 hostapd_change_config_freq(iface->bss[0], iface->conf,
3404 /* set channel switch parameters for csa ie */
3405 hapd->cs_freq_params = settings->freq_params;
3406 hapd->cs_count = settings->cs_count;
3407 hapd->cs_block_tx = settings->block_tx;
3409 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
3411 free_beacon_data(&settings->beacon_after);
3415 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3416 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3417 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3418 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
3424 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3426 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3428 hapd->cs_block_tx = 0;
3429 hapd->cs_c_off_beacon = 0;
3430 hapd->cs_c_off_proberesp = 0;
3431 hapd->csa_in_progress = 0;
3432 hapd->cs_c_off_ecsa_beacon = 0;
3433 hapd->cs_c_off_ecsa_proberesp = 0;
3437 void hostapd_chan_switch_vht_config(struct hostapd_data *hapd, int vht_enabled)
3440 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_ENABLED;
3442 hapd->iconf->ch_switch_vht_config |= CH_SWITCH_VHT_DISABLED;
3444 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
3445 HOSTAPD_LEVEL_INFO, "CHAN_SWITCH VHT CONFIG 0x%x",
3446 hapd->iconf->ch_switch_vht_config);
3450 int hostapd_switch_channel(struct hostapd_data *hapd,
3451 struct csa_settings *settings)
3455 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3456 wpa_printf(MSG_INFO, "CSA is not supported");
3460 ret = hostapd_fill_csa_settings(hapd, settings);
3464 ret = hostapd_drv_switch_channel(hapd, settings);
3465 free_beacon_data(&settings->beacon_csa);
3466 free_beacon_data(&settings->beacon_after);
3469 /* if we failed, clean cs parameters */
3470 hostapd_cleanup_cs_params(hapd);
3474 hapd->csa_in_progress = 1;
3480 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3481 const struct hostapd_freq_params *freq_params)
3483 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3485 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3487 if (freq_params->center_freq1)
3488 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3489 if (freq_params->center_freq2)
3490 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3492 switch (freq_params->bandwidth) {
3496 vht_bw = VHT_CHANWIDTH_USE_HT;
3499 if (freq_params->center_freq2)
3500 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3502 vht_bw = VHT_CHANWIDTH_80MHZ;
3505 vht_bw = VHT_CHANWIDTH_160MHZ;
3508 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3509 freq_params->bandwidth);
3513 iface->freq = freq_params->freq;
3514 iface->conf->channel = freq_params->channel;
3515 iface->conf->secondary_channel = freq_params->sec_channel_offset;
3516 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3517 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3518 iface->conf->vht_oper_chwidth = vht_bw;
3519 iface->conf->ieee80211n = freq_params->ht_enabled;
3520 iface->conf->ieee80211ac = freq_params->vht_enabled;
3523 * cs_params must not be cleared earlier because the freq_params
3524 * argument may actually point to one of these.
3525 * These params will be cleared during interface disable below.
3527 hostapd_disable_iface(iface);
3528 hostapd_enable_iface(iface);
3531 #endif /* NEED_AP_MLME */
3534 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3539 for (i = 0; i < interfaces->count; i++) {
3540 struct hostapd_iface *iface = interfaces->iface[i];
3542 for (j = 0; j < iface->num_bss; j++) {
3543 struct hostapd_data *hapd = iface->bss[j];
3545 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3554 void hostapd_periodic_iface(struct hostapd_iface *iface)
3558 ap_list_timer(iface);
3560 for (i = 0; i < iface->num_bss; i++) {
3561 struct hostapd_data *hapd = iface->bss[i];
3566 #ifndef CONFIG_NO_RADIUS
3567 hostapd_acl_expire(hapd);
3568 #endif /* CONFIG_NO_RADIUS */