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 "hw_features.h"
35 #include "wpa_auth_glue.h"
36 #include "ap_drv_ops.h"
37 #include "ap_config.h"
38 #include "p2p_hostapd.h"
41 #include "ieee802_11.h"
44 #include "dhcp_snoop.h"
45 #include "ndisc_snoop.h"
48 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
49 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
50 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
51 static int setup_interface2(struct hostapd_iface *iface);
52 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
55 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
56 int (*cb)(struct hostapd_iface *iface,
57 void *ctx), void *ctx)
62 for (i = 0; i < interfaces->count; i++) {
63 ret = cb(interfaces->iface[i], ctx);
72 static void hostapd_reload_bss(struct hostapd_data *hapd)
74 struct hostapd_ssid *ssid;
76 #ifndef CONFIG_NO_RADIUS
77 radius_client_reconfig(hapd->radius, hapd->conf->radius);
78 #endif /* CONFIG_NO_RADIUS */
80 ssid = &hapd->conf->ssid;
81 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
82 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
84 * Force PSK to be derived again since SSID or passphrase may
87 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
89 if (hostapd_setup_wpa_psk(hapd->conf)) {
90 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
91 "after reloading configuration");
94 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
95 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
97 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
99 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
100 hostapd_setup_wpa(hapd);
102 wpa_init_keys(hapd->wpa_auth);
103 } else if (hapd->conf->wpa) {
106 hostapd_reconfig_wpa(hapd);
107 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
108 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
109 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
110 "the kernel driver.");
111 } else if (hapd->wpa_auth) {
112 wpa_deinit(hapd->wpa_auth);
113 hapd->wpa_auth = NULL;
114 hostapd_set_privacy(hapd, 0);
115 hostapd_setup_encryption(hapd->conf->iface, hapd);
116 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
119 ieee802_11_set_beacon(hapd);
120 hostapd_update_wps(hapd);
122 if (hapd->conf->ssid.ssid_set &&
123 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
124 hapd->conf->ssid.ssid_len)) {
125 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
126 /* try to continue */
128 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
132 static void hostapd_clear_old(struct hostapd_iface *iface)
137 * Deauthenticate all stations since the new configuration may not
138 * allow them to use the BSS anymore.
140 for (j = 0; j < iface->num_bss; j++) {
141 hostapd_flush_old_stations(iface->bss[j],
142 WLAN_REASON_PREV_AUTH_NOT_VALID);
143 hostapd_broadcast_wep_clear(iface->bss[j]);
145 #ifndef CONFIG_NO_RADIUS
146 /* TODO: update dynamic data based on changed configuration
147 * items (e.g., open/close sockets, etc.) */
148 radius_client_flush(iface->bss[j]->radius, 0);
149 #endif /* CONFIG_NO_RADIUS */
154 int hostapd_reload_config(struct hostapd_iface *iface)
156 struct hostapd_data *hapd = iface->bss[0];
157 struct hostapd_config *newconf, *oldconf;
160 if (iface->config_fname == NULL) {
161 /* Only in-memory config in use - assume it has been updated */
162 hostapd_clear_old(iface);
163 for (j = 0; j < iface->num_bss; j++)
164 hostapd_reload_bss(iface->bss[j]);
168 if (iface->interfaces == NULL ||
169 iface->interfaces->config_read_cb == NULL)
171 newconf = iface->interfaces->config_read_cb(iface->config_fname);
175 hostapd_clear_old(iface);
177 oldconf = hapd->iconf;
178 iface->conf = newconf;
180 for (j = 0; j < iface->num_bss; j++) {
181 hapd = iface->bss[j];
182 hapd->iconf = newconf;
183 hapd->iconf->channel = oldconf->channel;
184 hapd->iconf->acs = oldconf->acs;
185 hapd->iconf->secondary_channel = oldconf->secondary_channel;
186 hapd->iconf->ieee80211n = oldconf->ieee80211n;
187 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
188 hapd->iconf->ht_capab = oldconf->ht_capab;
189 hapd->iconf->vht_capab = oldconf->vht_capab;
190 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
191 hapd->iconf->vht_oper_centr_freq_seg0_idx =
192 oldconf->vht_oper_centr_freq_seg0_idx;
193 hapd->iconf->vht_oper_centr_freq_seg1_idx =
194 oldconf->vht_oper_centr_freq_seg1_idx;
195 hapd->conf = newconf->bss[j];
196 hostapd_reload_bss(hapd);
199 hostapd_config_free(oldconf);
206 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
211 for (i = 0; i < NUM_WEP_KEYS; i++) {
212 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
213 0, NULL, 0, NULL, 0)) {
214 wpa_printf(MSG_DEBUG, "Failed to clear default "
215 "encryption keys (ifname=%s keyidx=%d)",
219 #ifdef CONFIG_IEEE80211W
220 if (hapd->conf->ieee80211w) {
221 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
222 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
225 wpa_printf(MSG_DEBUG, "Failed to clear "
226 "default mgmt encryption keys "
227 "(ifname=%s keyidx=%d)", ifname, i);
231 #endif /* CONFIG_IEEE80211W */
235 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
237 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
242 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
245 struct hostapd_ssid *ssid = &hapd->conf->ssid;
248 if (ssid->wep.default_len &&
249 hostapd_drv_set_key(hapd->conf->iface,
250 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
251 1, NULL, 0, ssid->wep.key[idx],
252 ssid->wep.len[idx])) {
253 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
261 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
263 os_free(hapd->probereq_cb);
264 hapd->probereq_cb = NULL;
265 hapd->num_probereq_cb = 0;
268 wpabuf_free(hapd->p2p_beacon_ie);
269 hapd->p2p_beacon_ie = NULL;
270 wpabuf_free(hapd->p2p_probe_resp_ie);
271 hapd->p2p_probe_resp_ie = NULL;
272 #endif /* CONFIG_P2P */
274 if (!hapd->started) {
275 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
276 __func__, hapd->conf->iface);
281 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
282 iapp_deinit(hapd->iapp);
284 accounting_deinit(hapd);
285 hostapd_deinit_wpa(hapd);
287 hostapd_acl_deinit(hapd);
288 #ifndef CONFIG_NO_RADIUS
289 radius_client_deinit(hapd->radius);
291 radius_das_deinit(hapd->radius_das);
292 hapd->radius_das = NULL;
293 #endif /* CONFIG_NO_RADIUS */
295 hostapd_deinit_wps(hapd);
297 authsrv_deinit(hapd);
299 if (hapd->interface_added) {
300 hapd->interface_added = 0;
301 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
302 wpa_printf(MSG_WARNING,
303 "Failed to remove BSS interface %s",
305 hapd->interface_added = 1;
308 * Since this was a dynamically added interface, the
309 * driver wrapper may have removed its internal instance
310 * and hapd->drv_priv is not valid anymore.
312 hapd->drv_priv = NULL;
316 wpabuf_free(hapd->time_adv);
318 #ifdef CONFIG_INTERWORKING
319 gas_serv_deinit(hapd);
320 #endif /* CONFIG_INTERWORKING */
322 bss_load_update_deinit(hapd);
323 ndisc_snoop_deinit(hapd);
324 dhcp_snoop_deinit(hapd);
325 x_snoop_deinit(hapd);
328 bin_clear_free(hapd->tmp_eap_user.identity,
329 hapd->tmp_eap_user.identity_len);
330 bin_clear_free(hapd->tmp_eap_user.password,
331 hapd->tmp_eap_user.password_len);
332 #endif /* CONFIG_SQLITE */
335 wpabuf_free(hapd->mesh_pending_auth);
336 hapd->mesh_pending_auth = NULL;
337 #endif /* CONFIG_MESH */
342 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
343 * @hapd: Pointer to BSS data
345 * This function is used to free all per-BSS data structures and resources.
346 * Most of the modules that are initialized in hostapd_setup_bss() are
347 * deinitialized here.
349 static void hostapd_cleanup(struct hostapd_data *hapd)
351 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
353 if (hapd->iface->interfaces &&
354 hapd->iface->interfaces->ctrl_iface_deinit)
355 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
356 hostapd_free_hapd_data(hapd);
360 static void sta_track_deinit(struct hostapd_iface *iface)
362 struct hostapd_sta_info *info;
364 if (!iface->num_sta_seen)
367 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
369 dl_list_del(&info->list);
370 iface->num_sta_seen--;
376 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
378 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
379 #ifdef CONFIG_IEEE80211N
381 hostapd_stop_setup_timers(iface);
382 #endif /* NEED_AP_MLME */
383 #endif /* CONFIG_IEEE80211N */
384 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
385 iface->hw_features = NULL;
386 os_free(iface->current_rates);
387 iface->current_rates = NULL;
388 os_free(iface->basic_rates);
389 iface->basic_rates = NULL;
390 ap_list_deinit(iface);
391 sta_track_deinit(iface);
396 * hostapd_cleanup_iface - Complete per-interface cleanup
397 * @iface: Pointer to interface data
399 * This function is called after per-BSS data structures are deinitialized
400 * with hostapd_cleanup().
402 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
404 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
405 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
407 hostapd_cleanup_iface_partial(iface);
408 hostapd_config_free(iface->conf);
411 os_free(iface->config_fname);
413 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
418 static void hostapd_clear_wep(struct hostapd_data *hapd)
420 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
421 hostapd_set_privacy(hapd, 0);
422 hostapd_broadcast_wep_clear(hapd);
427 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
431 hostapd_broadcast_wep_set(hapd);
433 if (hapd->conf->ssid.wep.default_len) {
434 hostapd_set_privacy(hapd, 1);
439 * When IEEE 802.1X is not enabled, the driver may need to know how to
440 * set authentication algorithms for static WEP.
442 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
444 for (i = 0; i < 4; i++) {
445 if (hapd->conf->ssid.wep.key[i] &&
446 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
447 i == hapd->conf->ssid.wep.idx, NULL, 0,
448 hapd->conf->ssid.wep.key[i],
449 hapd->conf->ssid.wep.len[i])) {
450 wpa_printf(MSG_WARNING, "Could not set WEP "
454 if (hapd->conf->ssid.wep.key[i] &&
455 i == hapd->conf->ssid.wep.idx)
456 hostapd_set_privacy(hapd, 1);
463 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
468 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
471 if (!hapd->iface->driver_ap_teardown) {
472 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
473 "Flushing old station entries");
475 if (hostapd_flush(hapd)) {
476 wpa_msg(hapd->msg_ctx, MSG_WARNING,
477 "Could not connect to kernel driver");
481 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
482 os_memset(addr, 0xff, ETH_ALEN);
483 hostapd_drv_sta_deauth(hapd, addr, reason);
484 hostapd_free_stas(hapd);
490 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
492 hostapd_free_stas(hapd);
493 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
494 hostapd_clear_wep(hapd);
499 * hostapd_validate_bssid_configuration - Validate BSSID configuration
500 * @iface: Pointer to interface data
501 * Returns: 0 on success, -1 on failure
503 * This function is used to validate that the configured BSSIDs are valid.
505 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
507 u8 mask[ETH_ALEN] = { 0 };
508 struct hostapd_data *hapd = iface->bss[0];
509 unsigned int i = iface->conf->num_bss, bits = 0, j;
512 if (hostapd_drv_none(hapd))
515 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
517 /* Determine the bits necessary to cover the number of BSSIDs. */
518 for (i--; i; i >>= 1)
521 /* Determine the bits necessary to any configured BSSIDs,
522 if they are higher than the number of BSSIDs. */
523 for (j = 0; j < iface->conf->num_bss; j++) {
524 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
530 for (i = 0; i < ETH_ALEN; i++) {
532 iface->conf->bss[j]->bssid[i] ^
540 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
546 while (mask[i] != 0) {
556 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
561 os_memset(mask, 0xff, ETH_ALEN);
563 for (i = 5; i > 5 - j; i--)
570 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
571 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
576 for (i = 0; i < ETH_ALEN; i++) {
577 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
578 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
579 " for start address " MACSTR ".",
580 MAC2STR(mask), MAC2STR(hapd->own_addr));
581 wpa_printf(MSG_ERROR, "Start address must be the "
582 "first address in the block (i.e., addr "
583 "AND mask == addr).");
592 static int mac_in_conf(struct hostapd_config *conf, const void *a)
596 for (i = 0; i < conf->num_bss; i++) {
597 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
606 #ifndef CONFIG_NO_RADIUS
608 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
609 struct radius_das_attrs *attr)
611 if (attr->nas_identifier &&
612 (!hapd->conf->nas_identifier ||
613 os_strlen(hapd->conf->nas_identifier) !=
614 attr->nas_identifier_len ||
615 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
616 attr->nas_identifier_len) != 0)) {
617 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
621 if (attr->nas_ip_addr &&
622 (hapd->conf->own_ip_addr.af != AF_INET ||
623 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
625 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
630 if (attr->nas_ipv6_addr &&
631 (hapd->conf->own_ip_addr.af != AF_INET6 ||
632 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
634 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
637 #endif /* CONFIG_IPV6 */
643 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
644 struct radius_das_attrs *attr,
647 struct sta_info *selected, *sta;
654 for (sta = hapd->sta_list; sta; sta = sta->next)
655 sta->radius_das_match = 1;
657 if (attr->sta_addr) {
659 sta = ap_get_sta(hapd, attr->sta_addr);
661 wpa_printf(MSG_DEBUG,
662 "RADIUS DAS: No Calling-Station-Id match");
667 for (sta = hapd->sta_list; sta; sta = sta->next) {
669 sta->radius_das_match = 0;
671 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
674 if (attr->acct_session_id) {
676 if (attr->acct_session_id_len != 16) {
677 wpa_printf(MSG_DEBUG,
678 "RADIUS DAS: Acct-Session-Id cannot match");
683 for (sta = hapd->sta_list; sta; sta = sta->next) {
684 if (!sta->radius_das_match)
686 os_snprintf(buf, sizeof(buf), "%016llX",
687 (unsigned long long) sta->acct_session_id);
688 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
689 sta->radius_das_match = 0;
695 wpa_printf(MSG_DEBUG,
696 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
699 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
702 if (attr->acct_multi_session_id) {
704 if (attr->acct_multi_session_id_len != 16) {
705 wpa_printf(MSG_DEBUG,
706 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
711 for (sta = hapd->sta_list; sta; sta = sta->next) {
712 if (!sta->radius_das_match)
714 if (!sta->eapol_sm ||
715 !sta->eapol_sm->acct_multi_session_id) {
716 sta->radius_das_match = 0;
719 os_snprintf(buf, sizeof(buf), "%016llX",
721 sta->eapol_sm->acct_multi_session_id);
722 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
724 sta->radius_das_match = 0;
730 wpa_printf(MSG_DEBUG,
731 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
734 wpa_printf(MSG_DEBUG,
735 "RADIUS DAS: Acct-Multi-Session-Id match");
742 for (sta = hapd->sta_list; sta; sta = sta->next) {
745 if (!sta->radius_das_match)
747 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
748 if (!cui || wpabuf_len(cui) != attr->cui_len ||
749 os_memcmp(wpabuf_head(cui), attr->cui,
751 sta->radius_das_match = 0;
757 wpa_printf(MSG_DEBUG,
758 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
761 wpa_printf(MSG_DEBUG,
762 "RADIUS DAS: Chargeable-User-Identity match");
765 if (attr->user_name) {
769 for (sta = hapd->sta_list; sta; sta = sta->next) {
773 if (!sta->radius_das_match)
775 identity = ieee802_1x_get_identity(sta->eapol_sm,
778 identity_len != attr->user_name_len ||
779 os_memcmp(identity, attr->user_name, identity_len)
781 sta->radius_das_match = 0;
787 wpa_printf(MSG_DEBUG,
788 "RADIUS DAS: No matches remaining after User-Name check");
791 wpa_printf(MSG_DEBUG,
792 "RADIUS DAS: User-Name match");
797 * In theory, we could match all current associations, but it
798 * seems safer to just reject requests that do not include any
799 * session identification attributes.
801 wpa_printf(MSG_DEBUG,
802 "RADIUS DAS: No session identification attributes included");
807 for (sta = hapd->sta_list; sta; sta = sta->next) {
808 if (sta->radius_das_match) {
821 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
822 struct radius_das_attrs *attr)
826 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
830 static enum radius_das_res
831 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
833 struct hostapd_data *hapd = ctx;
834 struct sta_info *sta;
837 if (hostapd_das_nas_mismatch(hapd, attr))
838 return RADIUS_DAS_NAS_MISMATCH;
840 sta = hostapd_das_find_sta(hapd, attr, &multi);
843 wpa_printf(MSG_DEBUG,
844 "RADIUS DAS: Multiple sessions match - not supported");
845 return RADIUS_DAS_MULTI_SESSION_MATCH;
847 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
848 wpa_printf(MSG_DEBUG,
849 "RADIUS DAS: PMKSA cache entry matched");
850 return RADIUS_DAS_SUCCESS;
852 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
853 return RADIUS_DAS_SESSION_NOT_FOUND;
856 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
857 " - disconnecting", MAC2STR(sta->addr));
858 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
860 hostapd_drv_sta_deauth(hapd, sta->addr,
861 WLAN_REASON_PREV_AUTH_NOT_VALID);
862 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
864 return RADIUS_DAS_SUCCESS;
867 #endif /* CONFIG_NO_RADIUS */
871 * hostapd_setup_bss - Per-BSS setup (initialization)
872 * @hapd: Pointer to BSS data
873 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
874 * but interface may exist
876 * This function is used to initialize all per-BSS data structures and
877 * resources. This gets called in a loop for each BSS when an interface is
878 * initialized. Most of the modules that are initialized here will be
879 * deinitialized in hostapd_cleanup().
881 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
883 struct hostapd_bss_config *conf = hapd->conf;
884 u8 ssid[SSID_MAX_LEN + 1];
885 int ssid_len, set_ssid;
886 char force_ifname[IFNAMSIZ];
887 u8 if_addr[ETH_ALEN];
888 int flush_old_stations = 1;
890 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
891 __func__, hapd, conf->iface, first);
893 #ifdef EAP_SERVER_TNC
894 if (conf->tnc && tncs_global_init() < 0) {
895 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
898 #endif /* EAP_SERVER_TNC */
901 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
902 __func__, conf->iface);
907 if (!first || first == -1) {
908 if (is_zero_ether_addr(conf->bssid)) {
909 /* Allocate the next available BSSID. */
911 inc_byte_array(hapd->own_addr, ETH_ALEN);
912 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
914 /* Allocate the configured BSSID. */
915 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
917 if (hostapd_mac_comp(hapd->own_addr,
918 hapd->iface->bss[0]->own_addr) ==
920 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
921 "BSSID set to the MAC address of "
922 "the radio", conf->iface);
927 hapd->interface_added = 1;
928 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
929 conf->iface, hapd->own_addr, hapd,
930 &hapd->drv_priv, force_ifname, if_addr,
931 conf->bridge[0] ? conf->bridge : NULL,
933 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
934 MACSTR ")", MAC2STR(hapd->own_addr));
935 hapd->interface_added = 0;
940 if (conf->wmm_enabled < 0)
941 conf->wmm_enabled = hapd->iconf->ieee80211n;
943 #ifdef CONFIG_IEEE80211R
944 if (is_zero_ether_addr(conf->r1_key_holder))
945 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
946 #endif /* CONFIG_IEEE80211R */
949 if (hapd->iface->mconf == NULL)
950 flush_old_stations = 0;
951 #endif /* CONFIG_MESH */
953 if (flush_old_stations)
954 hostapd_flush_old_stations(hapd,
955 WLAN_REASON_PREV_AUTH_NOT_VALID);
956 hostapd_set_privacy(hapd, 0);
958 hostapd_broadcast_wep_clear(hapd);
959 if (hostapd_setup_encryption(conf->iface, hapd))
963 * Fetch the SSID from the system and use it or,
964 * if one was specified in the config file, verify they
967 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
969 wpa_printf(MSG_ERROR, "Could not read SSID from system");
972 if (conf->ssid.ssid_set) {
974 * If SSID is specified in the config file and it differs
975 * from what is being used then force installation of the
978 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
979 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
982 * No SSID in the config file; just use the one we got
986 conf->ssid.ssid_len = ssid_len;
987 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
990 if (!hostapd_drv_none(hapd)) {
991 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
993 conf->iface, MAC2STR(hapd->own_addr),
994 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
997 if (hostapd_setup_wpa_psk(conf)) {
998 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1002 /* Set SSID for the kernel driver (to be used in beacon and probe
1003 * response frames) */
1004 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
1005 conf->ssid.ssid_len)) {
1006 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1010 if (wpa_debug_level <= MSG_MSGDUMP)
1011 conf->radius->msg_dumps = 1;
1012 #ifndef CONFIG_NO_RADIUS
1013 hapd->radius = radius_client_init(hapd, conf->radius);
1014 if (hapd->radius == NULL) {
1015 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1019 if (conf->radius_das_port) {
1020 struct radius_das_conf das_conf;
1021 os_memset(&das_conf, 0, sizeof(das_conf));
1022 das_conf.port = conf->radius_das_port;
1023 das_conf.shared_secret = conf->radius_das_shared_secret;
1024 das_conf.shared_secret_len =
1025 conf->radius_das_shared_secret_len;
1026 das_conf.client_addr = &conf->radius_das_client_addr;
1027 das_conf.time_window = conf->radius_das_time_window;
1028 das_conf.require_event_timestamp =
1029 conf->radius_das_require_event_timestamp;
1030 das_conf.ctx = hapd;
1031 das_conf.disconnect = hostapd_das_disconnect;
1032 hapd->radius_das = radius_das_init(&das_conf);
1033 if (hapd->radius_das == NULL) {
1034 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1039 #endif /* CONFIG_NO_RADIUS */
1041 if (hostapd_acl_init(hapd)) {
1042 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1045 if (hostapd_init_wps(hapd, conf))
1048 if (authsrv_init(hapd) < 0)
1051 if (ieee802_1x_init(hapd)) {
1052 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1056 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1059 if (accounting_init(hapd)) {
1060 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1064 if (conf->ieee802_11f &&
1065 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1066 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1071 #ifdef CONFIG_INTERWORKING
1072 if (gas_serv_init(hapd)) {
1073 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1077 if (conf->qos_map_set_len &&
1078 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1079 conf->qos_map_set_len)) {
1080 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1083 #endif /* CONFIG_INTERWORKING */
1085 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1086 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1090 if (conf->proxy_arp) {
1091 if (x_snoop_init(hapd)) {
1092 wpa_printf(MSG_ERROR,
1093 "Generic snooping infrastructure initialization failed");
1097 if (dhcp_snoop_init(hapd)) {
1098 wpa_printf(MSG_ERROR,
1099 "DHCP snooping initialization failed");
1103 if (ndisc_snoop_init(hapd)) {
1104 wpa_printf(MSG_ERROR,
1105 "Neighbor Discovery snooping initialization failed");
1110 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1111 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1115 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1118 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1121 if (hapd->driver && hapd->driver->set_operstate)
1122 hapd->driver->set_operstate(hapd->drv_priv, 1);
1128 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1130 struct hostapd_data *hapd = iface->bss[0];
1132 struct hostapd_tx_queue_params *p;
1135 if (iface->mconf == NULL)
1137 #endif /* CONFIG_MESH */
1139 for (i = 0; i < NUM_TX_QUEUES; i++) {
1140 p = &iface->conf->tx_queue[i];
1142 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1143 p->cwmax, p->burst)) {
1144 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1145 "parameters for queue %d.", i);
1146 /* Continue anyway */
1152 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1153 struct mac_acl_entry *mac_acl,
1154 int n_entries, u8 accept_acl)
1156 struct hostapd_acl_params *acl_params;
1159 acl_params = os_zalloc(sizeof(*acl_params) +
1160 (n_entries * sizeof(acl_params->mac_acl[0])));
1164 for (i = 0; i < n_entries; i++)
1165 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1168 acl_params->acl_policy = accept_acl;
1169 acl_params->num_mac_acl = n_entries;
1171 err = hostapd_drv_set_acl(hapd, acl_params);
1173 os_free(acl_params);
1179 static void hostapd_set_acl(struct hostapd_data *hapd)
1181 struct hostapd_config *conf = hapd->iconf;
1185 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1188 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1190 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1191 conf->bss[0]->num_accept_mac,
1194 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1197 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1199 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1200 conf->bss[0]->num_deny_mac,
1203 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1210 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1212 if (!hapd->iface->interfaces ||
1213 !hapd->iface->interfaces->ctrl_iface_init)
1216 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1217 wpa_printf(MSG_ERROR,
1218 "Failed to setup control interface for %s",
1227 static int start_ctrl_iface(struct hostapd_iface *iface)
1231 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1234 for (i = 0; i < iface->num_bss; i++) {
1235 struct hostapd_data *hapd = iface->bss[i];
1236 if (iface->interfaces->ctrl_iface_init(hapd)) {
1237 wpa_printf(MSG_ERROR,
1238 "Failed to setup control interface for %s",
1248 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1250 struct hostapd_iface *iface = eloop_ctx;
1252 if (!iface->wait_channel_update) {
1253 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1258 * It is possible that the existing channel list is acceptable, so try
1261 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1262 setup_interface2(iface);
1266 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1268 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1271 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1272 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1273 setup_interface2(iface);
1277 static int setup_interface(struct hostapd_iface *iface)
1279 struct hostapd_data *hapd = iface->bss[0];
1283 * It is possible that setup_interface() is called after the interface
1284 * was disabled etc., in which case driver_ap_teardown is possibly set
1285 * to 1. Clear it here so any other key/station deletion, which is not
1286 * part of a teardown flow, would also call the relevant driver
1289 iface->driver_ap_teardown = 0;
1291 if (!iface->phy[0]) {
1292 const char *phy = hostapd_drv_get_radio_name(hapd);
1294 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1295 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1300 * Make sure that all BSSes get configured with a pointer to the same
1303 for (i = 1; i < iface->num_bss; i++) {
1304 iface->bss[i]->driver = hapd->driver;
1305 iface->bss[i]->drv_priv = hapd->drv_priv;
1308 if (hostapd_validate_bssid_configuration(iface))
1312 * Initialize control interfaces early to allow external monitoring of
1313 * channel setup operations that may take considerable amount of time
1314 * especially for DFS cases.
1316 if (start_ctrl_iface(iface))
1319 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1320 char country[4], previous_country[4];
1322 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1323 if (hostapd_get_country(hapd, previous_country) < 0)
1324 previous_country[0] = '\0';
1326 os_memcpy(country, hapd->iconf->country, 3);
1328 if (hostapd_set_country(hapd, country) < 0) {
1329 wpa_printf(MSG_ERROR, "Failed to set country code");
1333 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1334 previous_country, country);
1336 if (os_strncmp(previous_country, country, 2) != 0) {
1337 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1338 iface->wait_channel_update = 1;
1339 eloop_register_timeout(5, 0,
1340 channel_list_update_timeout,
1346 return setup_interface2(iface);
1350 static int setup_interface2(struct hostapd_iface *iface)
1352 iface->wait_channel_update = 0;
1354 if (hostapd_get_hw_features(iface)) {
1355 /* Not all drivers support this yet, so continue without hw
1358 int ret = hostapd_select_hw_mode(iface);
1360 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1361 "channel. (%d)", ret);
1365 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1368 ret = hostapd_check_ht_capab(iface);
1372 wpa_printf(MSG_DEBUG, "Interface initialization will "
1373 "be completed in a callback");
1377 if (iface->conf->ieee80211h)
1378 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1380 return hostapd_setup_interface_complete(iface, 0);
1383 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1384 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1385 if (iface->interfaces && iface->interfaces->terminate_on_error)
1393 static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1395 struct hostapd_data *hapd = ctx;
1397 return hapd->own_addr;
1401 static void fst_hostapd_get_channel_info_cb(void *ctx,
1402 enum hostapd_hw_mode *hw_mode,
1405 struct hostapd_data *hapd = ctx;
1407 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1411 static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1413 struct hostapd_data *hapd = ctx;
1415 if (hapd->iface->fst_ies != fst_ies) {
1416 hapd->iface->fst_ies = fst_ies;
1417 if (ieee802_11_set_beacon(hapd))
1418 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1423 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1426 struct hostapd_data *hapd = ctx;
1428 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1429 wpabuf_head(buf), wpabuf_len(buf));
1433 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1435 struct hostapd_data *hapd = ctx;
1436 struct sta_info *sta = ap_get_sta(hapd, addr);
1438 return sta ? sta->mb_ies : NULL;
1442 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1443 const u8 *buf, size_t size)
1445 struct hostapd_data *hapd = ctx;
1446 struct sta_info *sta = ap_get_sta(hapd, addr);
1449 struct mb_ies_info info;
1451 if (!mb_ies_info_by_ies(&info, buf, size)) {
1452 wpabuf_free(sta->mb_ies);
1453 sta->mb_ies = mb_ies_by_info(&info);
1459 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1462 struct sta_info *s = (struct sta_info *) *get_ctx;
1465 for (; s && !s->mb_ies; s = s->next)
1470 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1480 static const u8 * fst_hostapd_get_peer_first(void *ctx,
1481 struct fst_get_peer_ctx **get_ctx,
1484 struct hostapd_data *hapd = ctx;
1486 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1488 return fst_hostapd_get_sta(get_ctx, mb_only);
1492 static const u8 * fst_hostapd_get_peer_next(void *ctx,
1493 struct fst_get_peer_ctx **get_ctx,
1496 return fst_hostapd_get_sta(get_ctx, mb_only);
1500 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1501 struct fst_wpa_obj *iface_obj)
1503 iface_obj->ctx = hapd;
1504 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1505 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1506 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1507 iface_obj->send_action = fst_hostapd_send_action_cb;
1508 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1509 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1510 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1511 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1514 #endif /* CONFIG_FST */
1517 static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1520 struct hostapd_data *hapd = iface->bss[0];
1523 int delay_apply_cfg = 0;
1524 int res_dfs_offload = 0;
1529 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1530 if (iface->conf->channel) {
1533 #endif /* NEED_AP_MLME */
1535 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1536 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1537 "Frequency: %d MHz",
1538 hostapd_hw_mode_txt(iface->conf->hw_mode),
1539 iface->conf->channel, iface->freq);
1542 /* Handle DFS only if it is not offloaded to the driver */
1543 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1545 res = hostapd_handle_dfs(iface);
1552 /* If DFS is offloaded to the driver */
1553 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1554 if (res_dfs_offload <= 0) {
1555 if (res_dfs_offload < 0)
1558 wpa_printf(MSG_DEBUG,
1559 "Proceed with AP/channel setup");
1561 * If this is a DFS channel, move to completing
1564 if (res_dfs_offload == 1)
1566 /* Otherwise fall through. */
1569 #endif /* NEED_AP_MLME */
1572 if (iface->mconf != NULL) {
1573 wpa_printf(MSG_DEBUG,
1574 "%s: Mesh configuration will be applied while joining the mesh network",
1575 iface->bss[0]->conf->iface);
1576 delay_apply_cfg = 1;
1578 #endif /* CONFIG_MESH */
1580 if (!delay_apply_cfg &&
1581 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1582 hapd->iconf->channel,
1583 hapd->iconf->ieee80211n,
1584 hapd->iconf->ieee80211ac,
1585 hapd->iconf->secondary_channel,
1586 hapd->iconf->vht_oper_chwidth,
1587 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1588 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1589 wpa_printf(MSG_ERROR, "Could not set channel for "
1595 if (iface->current_mode) {
1596 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1597 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1599 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1600 HOSTAPD_LEVEL_WARNING,
1601 "Failed to prepare rates table.");
1606 if (hapd->iconf->rts_threshold > -1 &&
1607 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1608 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1613 if (hapd->iconf->fragm_threshold > -1 &&
1614 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1615 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1616 "for kernel driver");
1620 prev_addr = hapd->own_addr;
1622 for (j = 0; j < iface->num_bss; j++) {
1623 hapd = iface->bss[j];
1625 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1626 if (hostapd_setup_bss(hapd, j == 0)) {
1628 hapd = iface->bss[j];
1629 hostapd_bss_deinit_no_free(hapd);
1630 hostapd_free_hapd_data(hapd);
1634 if (is_zero_ether_addr(hapd->conf->bssid))
1635 prev_addr = hapd->own_addr;
1637 hapd = iface->bss[0];
1639 hostapd_tx_queue_params(iface);
1641 ap_list_init(iface);
1642 dl_list_init(&iface->sta_seen);
1644 hostapd_set_acl(hapd);
1646 if (hostapd_driver_commit(hapd) < 0) {
1647 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1648 "configuration", __func__);
1653 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1654 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1655 * mode), the interface is up only after driver_commit, so initialize
1656 * WPS after driver_commit.
1658 for (j = 0; j < iface->num_bss; j++) {
1659 if (hostapd_init_wps_complete(iface->bss[j]))
1663 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1666 * If freq is DFS, and DFS is offloaded to the driver, then wait
1667 * for CAC to complete.
1669 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1670 return res_dfs_offload;
1675 #endif /* NEED_AP_MLME */
1678 if (hapd->iconf->fst_cfg.group_id[0]) {
1679 struct fst_wpa_obj iface_obj;
1681 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1682 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1683 &iface_obj, &hapd->iconf->fst_cfg);
1685 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1686 hapd->iconf->fst_cfg.group_id);
1690 #endif /* CONFIG_FST */
1692 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1693 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1694 if (hapd->setup_complete_cb)
1695 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1697 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1698 iface->bss[0]->conf->iface);
1699 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1700 iface->interfaces->terminate_on_error--;
1705 wpa_printf(MSG_ERROR, "Interface initialization failed");
1706 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1707 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1710 fst_detach(iface->fst);
1713 #endif /* CONFIG_FST */
1714 if (iface->interfaces && iface->interfaces->terminate_on_error)
1721 * hostapd_setup_interface_complete - Complete interface setup
1723 * This function is called when previous steps in the interface setup has been
1724 * completed. This can also start operations, e.g., DFS, that will require
1725 * additional processing before interface is ready to be enabled. Such
1726 * operations will call this function from eloop callbacks when finished.
1728 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1730 struct hapd_interfaces *interfaces = iface->interfaces;
1731 struct hostapd_data *hapd = iface->bss[0];
1733 int not_ready_in_sync_ifaces = 0;
1735 if (!iface->need_to_start_in_sync)
1736 return hostapd_setup_interface_complete_sync(iface, err);
1739 wpa_printf(MSG_ERROR, "Interface initialization failed");
1740 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1741 iface->need_to_start_in_sync = 0;
1742 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1743 if (interfaces && interfaces->terminate_on_error)
1748 if (iface->ready_to_start_in_sync) {
1749 /* Already in ready and waiting. should never happpen */
1753 for (i = 0; i < interfaces->count; i++) {
1754 if (interfaces->iface[i]->need_to_start_in_sync &&
1755 !interfaces->iface[i]->ready_to_start_in_sync)
1756 not_ready_in_sync_ifaces++;
1760 * Check if this is the last interface, if yes then start all the other
1761 * waiting interfaces. If not, add this interface to the waiting list.
1763 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
1765 * If this interface went through CAC, do not synchronize, just
1766 * start immediately.
1768 iface->need_to_start_in_sync = 0;
1769 wpa_printf(MSG_INFO,
1770 "%s: Finished CAC - bypass sync and start interface",
1771 iface->bss[0]->conf->iface);
1772 return hostapd_setup_interface_complete_sync(iface, err);
1775 if (not_ready_in_sync_ifaces > 1) {
1776 /* need to wait as there are other interfaces still coming up */
1777 iface->ready_to_start_in_sync = 1;
1778 wpa_printf(MSG_INFO,
1779 "%s: Interface waiting to sync with other interfaces",
1780 iface->bss[0]->conf->iface);
1784 wpa_printf(MSG_INFO,
1785 "%s: Last interface to sync - starting all interfaces",
1786 iface->bss[0]->conf->iface);
1787 iface->need_to_start_in_sync = 0;
1788 hostapd_setup_interface_complete_sync(iface, err);
1789 for (i = 0; i < interfaces->count; i++) {
1790 if (interfaces->iface[i]->need_to_start_in_sync &&
1791 interfaces->iface[i]->ready_to_start_in_sync) {
1792 hostapd_setup_interface_complete_sync(
1793 interfaces->iface[i], 0);
1794 /* Only once the interfaces are sync started */
1795 interfaces->iface[i]->need_to_start_in_sync = 0;
1804 * hostapd_setup_interface - Setup of an interface
1805 * @iface: Pointer to interface data.
1806 * Returns: 0 on success, -1 on failure
1808 * Initializes the driver interface, validates the configuration,
1809 * and sets driver parameters based on the configuration.
1810 * Flushes old stations, sets the channel, encryption,
1811 * beacons, and WDS links based on the configuration.
1813 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1814 * or DFS operations, this function returns 0 before such operations have been
1815 * completed. The pending operations are registered into eloop and will be
1816 * completed from eloop callbacks. Those callbacks end up calling
1817 * hostapd_setup_interface_complete() once setup has been completed.
1819 int hostapd_setup_interface(struct hostapd_iface *iface)
1823 ret = setup_interface(iface);
1825 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1826 iface->bss[0]->conf->iface);
1835 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1836 * @hapd_iface: Pointer to interface data
1837 * @conf: Pointer to per-interface configuration
1838 * @bss: Pointer to per-BSS configuration for this BSS
1839 * Returns: Pointer to allocated BSS data
1841 * This function is used to allocate per-BSS data structure. This data will be
1842 * freed after hostapd_cleanup() is called for it during interface
1845 struct hostapd_data *
1846 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1847 struct hostapd_config *conf,
1848 struct hostapd_bss_config *bss)
1850 struct hostapd_data *hapd;
1852 hapd = os_zalloc(sizeof(*hapd));
1856 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1859 hapd->iface = hapd_iface;
1860 hapd->driver = hapd->iconf->driver;
1861 hapd->ctrl_sock = -1;
1867 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1869 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1871 hostapd_bss_deinit_no_free(hapd);
1872 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1873 hostapd_cleanup(hapd);
1877 void hostapd_interface_deinit(struct hostapd_iface *iface)
1881 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1885 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1887 #ifdef CONFIG_IEEE80211N
1889 hostapd_stop_setup_timers(iface);
1890 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1891 #endif /* NEED_AP_MLME */
1892 #endif /* CONFIG_IEEE80211N */
1893 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1894 iface->wait_channel_update = 0;
1898 fst_detach(iface->fst);
1901 #endif /* CONFIG_FST */
1903 for (j = iface->num_bss - 1; j >= 0; j--)
1904 hostapd_bss_deinit(iface->bss[j]);
1908 void hostapd_interface_free(struct hostapd_iface *iface)
1911 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1912 for (j = 0; j < iface->num_bss; j++) {
1913 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1914 __func__, iface->bss[j]);
1915 os_free(iface->bss[j]);
1917 hostapd_cleanup_iface(iface);
1922 * hostapd_init - Allocate and initialize per-interface data
1923 * @config_file: Path to the configuration file
1924 * Returns: Pointer to the allocated interface data or %NULL on failure
1926 * This function is used to allocate main data structures for per-interface
1927 * data. The allocated data buffer will be freed by calling
1928 * hostapd_cleanup_iface().
1930 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1931 const char *config_file)
1933 struct hostapd_iface *hapd_iface = NULL;
1934 struct hostapd_config *conf = NULL;
1935 struct hostapd_data *hapd;
1938 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1939 if (hapd_iface == NULL)
1942 hapd_iface->config_fname = os_strdup(config_file);
1943 if (hapd_iface->config_fname == NULL)
1946 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1949 hapd_iface->conf = conf;
1951 hapd_iface->num_bss = conf->num_bss;
1952 hapd_iface->bss = os_calloc(conf->num_bss,
1953 sizeof(struct hostapd_data *));
1954 if (hapd_iface->bss == NULL)
1957 for (i = 0; i < conf->num_bss; i++) {
1958 hapd = hapd_iface->bss[i] =
1959 hostapd_alloc_bss_data(hapd_iface, conf,
1963 hapd->msg_ctx = hapd;
1969 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1972 hostapd_config_free(conf);
1974 os_free(hapd_iface->config_fname);
1975 os_free(hapd_iface->bss);
1976 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1977 __func__, hapd_iface);
1978 os_free(hapd_iface);
1984 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1988 for (i = 0; i < interfaces->count; i++) {
1989 struct hostapd_iface *iface = interfaces->iface[i];
1990 for (j = 0; j < iface->num_bss; j++) {
1991 struct hostapd_data *hapd = iface->bss[j];
1992 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2002 * hostapd_interface_init_bss - Read configuration file and init BSS data
2004 * This function is used to parse configuration file for a BSS. This BSS is
2005 * added to an existing interface sharing the same radio (if any) or a new
2006 * interface is created if this is the first interface on a radio. This
2007 * allocate memory for the BSS. No actual driver operations are started.
2009 * This is similar to hostapd_interface_init(), but for a case where the
2010 * configuration is used to add a single BSS instead of all BSSes for a radio.
2012 struct hostapd_iface *
2013 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2014 const char *config_fname, int debug)
2016 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2017 struct hostapd_data *hapd;
2024 for (i = 0; i < interfaces->count; i++) {
2025 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2026 iface = interfaces->iface[i];
2031 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2032 config_fname, phy, iface ? "" : " --> new PHY");
2034 struct hostapd_config *conf;
2035 struct hostapd_bss_config **tmp_conf;
2036 struct hostapd_data **tmp_bss;
2037 struct hostapd_bss_config *bss;
2040 /* Add new BSS to existing iface */
2041 conf = interfaces->config_read_cb(config_fname);
2044 if (conf->num_bss > 1) {
2045 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2046 hostapd_config_free(conf);
2050 ifname = conf->bss[0]->iface;
2051 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2052 wpa_printf(MSG_ERROR,
2053 "Interface name %s already in use", ifname);
2054 hostapd_config_free(conf);
2058 tmp_conf = os_realloc_array(
2059 iface->conf->bss, iface->conf->num_bss + 1,
2060 sizeof(struct hostapd_bss_config *));
2061 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2062 sizeof(struct hostapd_data *));
2064 iface->bss = tmp_bss;
2066 iface->conf->bss = tmp_conf;
2067 iface->conf->last_bss = tmp_conf[0];
2069 if (tmp_bss == NULL || tmp_conf == NULL) {
2070 hostapd_config_free(conf);
2073 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2074 iface->conf->num_bss++;
2076 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2078 iface->conf->num_bss--;
2079 hostapd_config_free(conf);
2082 iface->conf->last_bss = bss;
2083 iface->bss[iface->num_bss] = hapd;
2084 hapd->msg_ctx = hapd;
2086 bss_idx = iface->num_bss++;
2088 conf->bss[0] = NULL;
2089 hostapd_config_free(conf);
2091 /* Add a new iface with the first BSS */
2092 new_iface = iface = hostapd_init(interfaces, config_fname);
2095 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2096 iface->interfaces = interfaces;
2100 for (k = 0; k < debug; k++) {
2101 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2102 iface->bss[bss_idx]->conf->logger_stdout_level--;
2105 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2106 !hostapd_drv_none(iface->bss[bss_idx])) {
2107 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2110 hostapd_interface_deinit_free(new_iface);
2118 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2120 const struct wpa_driver_ops *driver;
2123 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2126 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2127 __func__, (unsigned int) iface->num_bss,
2128 (unsigned int) iface->conf->num_bss);
2129 driver = iface->bss[0]->driver;
2130 drv_priv = iface->bss[0]->drv_priv;
2131 hostapd_interface_deinit(iface);
2132 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2133 __func__, driver, drv_priv);
2134 if (driver && driver->hapd_deinit && drv_priv) {
2135 driver->hapd_deinit(drv_priv);
2136 iface->bss[0]->drv_priv = NULL;
2138 hostapd_interface_free(iface);
2142 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2144 struct hostapd_iface *hapd_iface)
2148 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2149 __func__, driver, drv_priv);
2150 if (driver && driver->hapd_deinit && drv_priv) {
2151 driver->hapd_deinit(drv_priv);
2152 for (j = 0; j < hapd_iface->num_bss; j++) {
2153 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2155 hapd_iface->bss[j]->drv_priv);
2156 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2157 hapd_iface->bss[j]->drv_priv = NULL;
2163 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2167 if (hapd_iface->bss[0]->drv_priv != NULL) {
2168 wpa_printf(MSG_ERROR, "Interface %s already enabled",
2169 hapd_iface->conf->bss[0]->iface);
2173 wpa_printf(MSG_DEBUG, "Enable interface %s",
2174 hapd_iface->conf->bss[0]->iface);
2176 for (j = 0; j < hapd_iface->num_bss; j++)
2177 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2178 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2179 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2183 if (hapd_iface->interfaces == NULL ||
2184 hapd_iface->interfaces->driver_init == NULL ||
2185 hapd_iface->interfaces->driver_init(hapd_iface))
2188 if (hostapd_setup_interface(hapd_iface)) {
2189 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2190 hapd_iface->bss[0]->drv_priv,
2199 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2203 wpa_printf(MSG_DEBUG, "Reload interface %s",
2204 hapd_iface->conf->bss[0]->iface);
2205 for (j = 0; j < hapd_iface->num_bss; j++)
2206 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2207 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2208 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2211 hostapd_clear_old(hapd_iface);
2212 for (j = 0; j < hapd_iface->num_bss; j++)
2213 hostapd_reload_bss(hapd_iface->bss[j]);
2219 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2222 const struct wpa_driver_ops *driver;
2225 if (hapd_iface == NULL)
2228 if (hapd_iface->bss[0]->drv_priv == NULL) {
2229 wpa_printf(MSG_INFO, "Interface %s already disabled",
2230 hapd_iface->conf->bss[0]->iface);
2234 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2235 driver = hapd_iface->bss[0]->driver;
2236 drv_priv = hapd_iface->bss[0]->drv_priv;
2238 hapd_iface->driver_ap_teardown =
2239 !!(hapd_iface->drv_flags &
2240 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2242 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
2243 for (j = 0; j < hapd_iface->num_bss; j++) {
2244 struct hostapd_data *hapd = hapd_iface->bss[j];
2245 hostapd_bss_deinit_no_free(hapd);
2246 hostapd_free_hapd_data(hapd);
2249 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
2251 /* From hostapd_cleanup_iface: These were initialized in
2252 * hostapd_setup_interface and hostapd_setup_interface_complete
2254 hostapd_cleanup_iface_partial(hapd_iface);
2256 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2257 hapd_iface->bss[0]->conf->iface);
2258 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2263 static struct hostapd_iface *
2264 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2266 struct hostapd_iface **iface, *hapd_iface;
2268 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2269 sizeof(struct hostapd_iface *));
2272 interfaces->iface = iface;
2273 hapd_iface = interfaces->iface[interfaces->count] =
2274 os_zalloc(sizeof(*hapd_iface));
2275 if (hapd_iface == NULL) {
2276 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2277 "the interface", __func__);
2280 interfaces->count++;
2281 hapd_iface->interfaces = interfaces;
2287 static struct hostapd_config *
2288 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2289 const char *ctrl_iface, const char *driver)
2291 struct hostapd_bss_config *bss;
2292 struct hostapd_config *conf;
2294 /* Allocates memory for bss and conf */
2295 conf = hostapd_config_defaults();
2297 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2298 "configuration", __func__);
2305 for (j = 0; wpa_drivers[j]; j++) {
2306 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2307 conf->driver = wpa_drivers[j];
2312 wpa_printf(MSG_ERROR,
2313 "Invalid/unknown driver '%s' - registering the default driver",
2317 conf->driver = wpa_drivers[0];
2318 if (conf->driver == NULL) {
2319 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2320 hostapd_config_free(conf);
2325 bss = conf->last_bss = conf->bss[0];
2327 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2328 bss->ctrl_interface = os_strdup(ctrl_iface);
2329 if (bss->ctrl_interface == NULL) {
2330 hostapd_config_free(conf);
2334 /* Reading configuration file skipped, will be done in SET!
2335 * From reading the configuration till the end has to be done in
2342 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2343 struct hostapd_config *conf)
2346 struct hostapd_data *hapd;
2348 hapd_iface->bss = os_calloc(conf->num_bss,
2349 sizeof(struct hostapd_data *));
2350 if (hapd_iface->bss == NULL)
2353 for (i = 0; i < conf->num_bss; i++) {
2354 hapd = hapd_iface->bss[i] =
2355 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2359 os_free(hapd_iface->bss[i]);
2360 hapd_iface->bss[i] = NULL;
2362 os_free(hapd_iface->bss);
2363 hapd_iface->bss = NULL;
2366 hapd->msg_ctx = hapd;
2369 hapd_iface->conf = conf;
2370 hapd_iface->num_bss = conf->num_bss;
2376 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2378 struct hostapd_config *conf = NULL;
2379 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2380 struct hostapd_data *hapd;
2383 const char *conf_file = NULL, *phy_name = NULL;
2385 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2387 phy_name = buf + 11;
2388 pos = os_strchr(phy_name, ':');
2393 if (!os_strlen(conf_file))
2396 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2400 for (j = 0; j < interfaces->count; j++) {
2401 if (interfaces->iface[j] == hapd_iface)
2404 if (j == interfaces->count) {
2405 struct hostapd_iface **tmp;
2406 tmp = os_realloc_array(interfaces->iface,
2407 interfaces->count + 1,
2408 sizeof(struct hostapd_iface *));
2410 hostapd_interface_deinit_free(hapd_iface);
2413 interfaces->iface = tmp;
2414 interfaces->iface[interfaces->count++] = hapd_iface;
2415 new_iface = hapd_iface;
2419 if (interfaces->driver_init(hapd_iface))
2422 if (hostapd_setup_interface(hapd_iface)) {
2423 hostapd_deinit_driver(
2424 hapd_iface->bss[0]->driver,
2425 hapd_iface->bss[0]->drv_priv,
2430 /* Assign new BSS with bss[0]'s driver info */
2431 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2432 hapd->driver = hapd_iface->bss[0]->driver;
2433 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2434 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2437 if (start_ctrl_iface_bss(hapd) < 0 ||
2438 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2439 hostapd_setup_bss(hapd, -1))) {
2440 hostapd_cleanup(hapd);
2441 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2442 hapd_iface->conf->num_bss--;
2443 hapd_iface->num_bss--;
2444 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2445 __func__, hapd, hapd->conf->iface);
2446 hostapd_config_free_bss(hapd->conf);
2455 ptr = os_strchr(buf, ' ');
2460 if (os_strncmp(ptr, "config=", 7) == 0)
2461 conf_file = ptr + 7;
2463 for (i = 0; i < interfaces->count; i++) {
2464 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2466 wpa_printf(MSG_INFO, "Cannot add interface - it "
2472 hapd_iface = hostapd_iface_alloc(interfaces);
2473 if (hapd_iface == NULL) {
2474 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2475 "for interface", __func__);
2478 new_iface = hapd_iface;
2480 if (conf_file && interfaces->config_read_cb) {
2481 conf = interfaces->config_read_cb(conf_file);
2482 if (conf && conf->bss)
2483 os_strlcpy(conf->bss[0]->iface, buf,
2484 sizeof(conf->bss[0]->iface));
2486 char *driver = os_strchr(ptr, ' ');
2490 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2493 if (conf == NULL || conf->bss == NULL) {
2494 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2495 "for configuration", __func__);
2499 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2500 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2501 "for hostapd", __func__);
2506 if (start_ctrl_iface(hapd_iface) < 0)
2509 wpa_printf(MSG_INFO, "Add interface '%s'",
2510 hapd_iface->conf->bss[0]->iface);
2516 hostapd_config_free(conf);
2518 if (hapd_iface->bss) {
2519 for (i = 0; i < hapd_iface->num_bss; i++) {
2520 hapd = hapd_iface->bss[i];
2523 if (hapd_iface->interfaces &&
2524 hapd_iface->interfaces->ctrl_iface_deinit)
2525 hapd_iface->interfaces->
2526 ctrl_iface_deinit(hapd);
2527 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2528 __func__, hapd_iface->bss[i],
2530 hostapd_cleanup(hapd);
2532 hapd_iface->bss[i] = NULL;
2534 os_free(hapd_iface->bss);
2535 hapd_iface->bss = NULL;
2538 interfaces->count--;
2539 interfaces->iface[interfaces->count] = NULL;
2541 hostapd_cleanup_iface(hapd_iface);
2547 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2551 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2553 /* Remove hostapd_data only if it has already been initialized */
2554 if (idx < iface->num_bss) {
2555 struct hostapd_data *hapd = iface->bss[idx];
2557 hostapd_bss_deinit(hapd);
2558 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2559 __func__, hapd, hapd->conf->iface);
2560 hostapd_config_free_bss(hapd->conf);
2566 for (i = idx; i < iface->num_bss; i++)
2567 iface->bss[i] = iface->bss[i + 1];
2569 hostapd_config_free_bss(iface->conf->bss[idx]);
2570 iface->conf->bss[idx] = NULL;
2573 iface->conf->num_bss--;
2574 for (i = idx; i < iface->conf->num_bss; i++)
2575 iface->conf->bss[i] = iface->conf->bss[i + 1];
2581 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2583 struct hostapd_iface *hapd_iface;
2586 for (i = 0; i < interfaces->count; i++) {
2587 hapd_iface = interfaces->iface[i];
2588 if (hapd_iface == NULL)
2590 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2591 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2592 hapd_iface->driver_ap_teardown =
2593 !!(hapd_iface->drv_flags &
2594 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2596 hostapd_interface_deinit_free(hapd_iface);
2598 while (k < (interfaces->count - 1)) {
2599 interfaces->iface[k] =
2600 interfaces->iface[k + 1];
2603 interfaces->count--;
2607 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2608 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2609 hapd_iface->driver_ap_teardown =
2610 !(hapd_iface->drv_flags &
2611 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2612 return hostapd_remove_bss(hapd_iface, j);
2621 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2622 * @hapd: Pointer to BSS data
2623 * @sta: Pointer to the associated STA data
2624 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2626 * This function will be called whenever a station associates with the AP. It
2627 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2628 * from drv_callbacks.c based on driver events for drivers that take care of
2629 * management frames (IEEE 802.11 authentication and association) internally.
2631 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2634 if (hapd->tkip_countermeasures) {
2635 hostapd_drv_sta_deauth(hapd, sta->addr,
2636 WLAN_REASON_MICHAEL_MIC_FAILURE);
2640 hostapd_prune_associations(hapd, sta->addr);
2641 ap_sta_clear_disconnect_timeouts(hapd, sta);
2643 /* IEEE 802.11F (IAPP) */
2644 if (hapd->conf->ieee802_11f)
2645 iapp_new_station(hapd->iapp, sta);
2648 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2649 sta->no_p2p_set = 1;
2650 hapd->num_sta_no_p2p++;
2651 if (hapd->num_sta_no_p2p == 1)
2652 hostapd_p2p_non_p2p_sta_connected(hapd);
2654 #endif /* CONFIG_P2P */
2656 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2657 * IEEE 802.1X/WPA code will start accounting after the station has
2658 * been authorized. */
2659 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2660 ap_sta_set_authorized(hapd, sta, 1);
2661 os_get_reltime(&sta->connected_time);
2662 accounting_sta_start(hapd, sta);
2665 /* Start IEEE 802.1X authentication process for new stations */
2666 ieee802_1x_new_station(hapd, sta);
2668 if (sta->auth_alg != WLAN_AUTH_FT &&
2669 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2670 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2672 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2674 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2675 wpa_printf(MSG_DEBUG,
2676 "%s: %s: reschedule ap_handle_timer timeout for "
2677 MACSTR " (%d seconds - ap_max_inactivity)",
2678 hapd->conf->iface, __func__, MAC2STR(sta->addr),
2679 hapd->conf->ap_max_inactivity);
2680 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2681 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2682 ap_handle_timer, hapd, sta);
2687 const char * hostapd_state_text(enum hostapd_iface_state s)
2690 case HAPD_IFACE_UNINITIALIZED:
2691 return "UNINITIALIZED";
2692 case HAPD_IFACE_DISABLED:
2694 case HAPD_IFACE_COUNTRY_UPDATE:
2695 return "COUNTRY_UPDATE";
2696 case HAPD_IFACE_ACS:
2698 case HAPD_IFACE_HT_SCAN:
2700 case HAPD_IFACE_DFS:
2702 case HAPD_IFACE_ENABLED:
2710 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2712 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2713 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2714 hostapd_state_text(s));
2719 int hostapd_csa_in_progress(struct hostapd_iface *iface)
2723 for (i = 0; i < iface->num_bss; i++)
2724 if (iface->bss[i]->csa_in_progress)
2732 static void free_beacon_data(struct beacon_data *beacon)
2734 os_free(beacon->head);
2735 beacon->head = NULL;
2736 os_free(beacon->tail);
2737 beacon->tail = NULL;
2738 os_free(beacon->probe_resp);
2739 beacon->probe_resp = NULL;
2740 os_free(beacon->beacon_ies);
2741 beacon->beacon_ies = NULL;
2742 os_free(beacon->proberesp_ies);
2743 beacon->proberesp_ies = NULL;
2744 os_free(beacon->assocresp_ies);
2745 beacon->assocresp_ies = NULL;
2749 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2750 struct beacon_data *beacon)
2752 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2753 struct wpa_driver_ap_params params;
2756 os_memset(beacon, 0, sizeof(*beacon));
2757 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2761 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2765 goto free_ap_params;
2768 beacon->head = os_malloc(params.head_len);
2770 goto free_ap_extra_ies;
2772 os_memcpy(beacon->head, params.head, params.head_len);
2773 beacon->head_len = params.head_len;
2775 beacon->tail = os_malloc(params.tail_len);
2779 os_memcpy(beacon->tail, params.tail, params.tail_len);
2780 beacon->tail_len = params.tail_len;
2782 if (params.proberesp != NULL) {
2783 beacon->probe_resp = os_malloc(params.proberesp_len);
2784 if (!beacon->probe_resp)
2787 os_memcpy(beacon->probe_resp, params.proberesp,
2788 params.proberesp_len);
2789 beacon->probe_resp_len = params.proberesp_len;
2792 /* copy the extra ies */
2794 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2795 if (!beacon->beacon_ies)
2798 os_memcpy(beacon->beacon_ies,
2799 beacon_extra->buf, wpabuf_len(beacon_extra));
2800 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2803 if (proberesp_extra) {
2804 beacon->proberesp_ies =
2805 os_malloc(wpabuf_len(proberesp_extra));
2806 if (!beacon->proberesp_ies)
2809 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2810 wpabuf_len(proberesp_extra));
2811 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2814 if (assocresp_extra) {
2815 beacon->assocresp_ies =
2816 os_malloc(wpabuf_len(assocresp_extra));
2817 if (!beacon->assocresp_ies)
2820 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2821 wpabuf_len(assocresp_extra));
2822 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2827 /* if the function fails, the caller should not free beacon data */
2829 free_beacon_data(beacon);
2832 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2835 ieee802_11_free_ap_params(¶ms);
2841 * TODO: This flow currently supports only changing channel and width within
2842 * the same hw_mode. Any other changes to MAC parameters or provided settings
2843 * are not supported.
2845 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2846 struct hostapd_config *conf,
2847 struct hostapd_freq_params *params,
2848 struct hostapd_freq_params *old_params)
2852 if (!params->channel) {
2853 /* check if the new channel is supported by hw */
2854 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2857 channel = params->channel;
2861 /* if a pointer to old_params is provided we save previous state */
2863 hostapd_set_freq_params(old_params, conf->hw_mode,
2864 hostapd_hw_get_freq(hapd, conf->channel),
2865 conf->channel, conf->ieee80211n,
2867 conf->secondary_channel,
2868 conf->vht_oper_chwidth,
2869 conf->vht_oper_centr_freq_seg0_idx,
2870 conf->vht_oper_centr_freq_seg1_idx,
2874 switch (params->bandwidth) {
2878 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
2881 if (params->center_freq2)
2882 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
2884 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
2887 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
2893 conf->channel = channel;
2894 conf->ieee80211n = params->ht_enabled;
2895 conf->secondary_channel = params->sec_channel_offset;
2896 ieee80211_freq_to_chan(params->center_freq1,
2897 &conf->vht_oper_centr_freq_seg0_idx);
2898 ieee80211_freq_to_chan(params->center_freq2,
2899 &conf->vht_oper_centr_freq_seg1_idx);
2901 /* TODO: maybe call here hostapd_config_check here? */
2907 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2908 struct csa_settings *settings)
2910 struct hostapd_iface *iface = hapd->iface;
2911 struct hostapd_freq_params old_freq;
2913 u8 chan, vht_bandwidth;
2915 os_memset(&old_freq, 0, sizeof(old_freq));
2916 if (!iface || !iface->freq || hapd->csa_in_progress)
2919 switch (settings->freq_params.bandwidth) {
2921 if (settings->freq_params.center_freq2)
2922 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
2924 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
2927 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
2930 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
2934 if (ieee80211_freq_to_channel_ext(
2935 settings->freq_params.freq,
2936 settings->freq_params.sec_channel_offset,
2938 &hapd->iface->cs_oper_class,
2939 &chan) == NUM_HOSTAPD_MODES) {
2940 wpa_printf(MSG_DEBUG,
2941 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
2942 settings->freq_params.freq,
2943 settings->freq_params.sec_channel_offset,
2944 settings->freq_params.vht_enabled);
2948 settings->freq_params.channel = chan;
2950 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2951 &settings->freq_params,
2956 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2958 /* change back the configuration */
2959 hostapd_change_config_freq(iface->bss[0], iface->conf,
2965 /* set channel switch parameters for csa ie */
2966 hapd->cs_freq_params = settings->freq_params;
2967 hapd->cs_count = settings->cs_count;
2968 hapd->cs_block_tx = settings->block_tx;
2970 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2972 free_beacon_data(&settings->beacon_after);
2976 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
2977 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
2978 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
2979 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
2985 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2987 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2989 hapd->cs_block_tx = 0;
2990 hapd->cs_c_off_beacon = 0;
2991 hapd->cs_c_off_proberesp = 0;
2992 hapd->csa_in_progress = 0;
2993 hapd->cs_c_off_ecsa_beacon = 0;
2994 hapd->cs_c_off_ecsa_proberesp = 0;
2998 int hostapd_switch_channel(struct hostapd_data *hapd,
2999 struct csa_settings *settings)
3003 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3004 wpa_printf(MSG_INFO, "CSA is not supported");
3008 ret = hostapd_fill_csa_settings(hapd, settings);
3012 ret = hostapd_drv_switch_channel(hapd, settings);
3013 free_beacon_data(&settings->beacon_csa);
3014 free_beacon_data(&settings->beacon_after);
3017 /* if we failed, clean cs parameters */
3018 hostapd_cleanup_cs_params(hapd);
3022 hapd->csa_in_progress = 1;
3028 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3029 const struct hostapd_freq_params *freq_params)
3031 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3034 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3036 if (freq_params->center_freq1)
3037 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3038 if (freq_params->center_freq2)
3039 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3041 switch (freq_params->bandwidth) {
3045 vht_bw = VHT_CHANWIDTH_USE_HT;
3048 if (freq_params->center_freq2)
3049 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3051 vht_bw = VHT_CHANWIDTH_80MHZ;
3054 vht_bw = VHT_CHANWIDTH_160MHZ;
3057 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3058 freq_params->bandwidth);
3062 iface->freq = freq_params->freq;
3063 iface->conf->channel = freq_params->channel;
3064 iface->conf->secondary_channel = freq_params->sec_channel_offset;
3065 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3066 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3067 iface->conf->vht_oper_chwidth = vht_bw;
3068 iface->conf->ieee80211n = freq_params->ht_enabled;
3069 iface->conf->ieee80211ac = freq_params->vht_enabled;
3072 * cs_params must not be cleared earlier because the freq_params
3073 * argument may actually point to one of these.
3075 for (i = 0; i < iface->num_bss; i++)
3076 hostapd_cleanup_cs_params(iface->bss[i]);
3078 hostapd_disable_iface(iface);
3079 hostapd_enable_iface(iface);
3083 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3088 for (i = 0; i < interfaces->count; i++) {
3089 struct hostapd_iface *iface = interfaces->iface[i];
3091 for (j = 0; j < iface->num_bss; j++) {
3092 struct hostapd_data *hapd = iface->bss[j];
3094 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3102 #endif /* NEED_AP_MLME */
3105 void hostapd_periodic_iface(struct hostapd_iface *iface)
3109 ap_list_timer(iface);
3111 for (i = 0; i < iface->num_bss; i++) {
3112 struct hostapd_data *hapd = iface->bss[i];
3117 #ifndef CONFIG_NO_RADIUS
3118 hostapd_acl_expire(hapd);
3119 #endif /* CONFIG_NO_RADIUS */