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"
46 #include "neighbor_db.h"
49 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
50 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
51 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
52 static int setup_interface2(struct hostapd_iface *iface);
53 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
56 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
57 int (*cb)(struct hostapd_iface *iface,
58 void *ctx), void *ctx)
63 for (i = 0; i < interfaces->count; i++) {
64 ret = cb(interfaces->iface[i], ctx);
73 static void hostapd_reload_bss(struct hostapd_data *hapd)
75 struct hostapd_ssid *ssid;
77 #ifndef CONFIG_NO_RADIUS
78 radius_client_reconfig(hapd->radius, hapd->conf->radius);
79 #endif /* CONFIG_NO_RADIUS */
81 ssid = &hapd->conf->ssid;
82 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
83 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
85 * Force PSK to be derived again since SSID or passphrase may
88 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
90 if (hostapd_setup_wpa_psk(hapd->conf)) {
91 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
92 "after reloading configuration");
95 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
96 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
98 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
100 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
101 hostapd_setup_wpa(hapd);
103 wpa_init_keys(hapd->wpa_auth);
104 } else if (hapd->conf->wpa) {
107 hostapd_reconfig_wpa(hapd);
108 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
109 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
110 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
111 "the kernel driver.");
112 } else if (hapd->wpa_auth) {
113 wpa_deinit(hapd->wpa_auth);
114 hapd->wpa_auth = NULL;
115 hostapd_set_privacy(hapd, 0);
116 hostapd_setup_encryption(hapd->conf->iface, hapd);
117 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
120 ieee802_11_set_beacon(hapd);
121 hostapd_update_wps(hapd);
123 if (hapd->conf->ssid.ssid_set &&
124 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
125 hapd->conf->ssid.ssid_len)) {
126 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
127 /* try to continue */
129 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
133 static void hostapd_clear_old(struct hostapd_iface *iface)
138 * Deauthenticate all stations since the new configuration may not
139 * allow them to use the BSS anymore.
141 for (j = 0; j < iface->num_bss; j++) {
142 hostapd_flush_old_stations(iface->bss[j],
143 WLAN_REASON_PREV_AUTH_NOT_VALID);
144 hostapd_broadcast_wep_clear(iface->bss[j]);
146 #ifndef CONFIG_NO_RADIUS
147 /* TODO: update dynamic data based on changed configuration
148 * items (e.g., open/close sockets, etc.) */
149 radius_client_flush(iface->bss[j]->radius, 0);
150 #endif /* CONFIG_NO_RADIUS */
155 int hostapd_reload_config(struct hostapd_iface *iface)
157 struct hostapd_data *hapd = iface->bss[0];
158 struct hostapd_config *newconf, *oldconf;
161 if (iface->config_fname == NULL) {
162 /* Only in-memory config in use - assume it has been updated */
163 hostapd_clear_old(iface);
164 for (j = 0; j < iface->num_bss; j++)
165 hostapd_reload_bss(iface->bss[j]);
169 if (iface->interfaces == NULL ||
170 iface->interfaces->config_read_cb == NULL)
172 newconf = iface->interfaces->config_read_cb(iface->config_fname);
176 hostapd_clear_old(iface);
178 oldconf = hapd->iconf;
179 iface->conf = newconf;
181 for (j = 0; j < iface->num_bss; j++) {
182 hapd = iface->bss[j];
183 hapd->iconf = newconf;
184 hapd->iconf->channel = oldconf->channel;
185 hapd->iconf->acs = oldconf->acs;
186 hapd->iconf->secondary_channel = oldconf->secondary_channel;
187 hapd->iconf->ieee80211n = oldconf->ieee80211n;
188 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
189 hapd->iconf->ht_capab = oldconf->ht_capab;
190 hapd->iconf->vht_capab = oldconf->vht_capab;
191 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
192 hapd->iconf->vht_oper_centr_freq_seg0_idx =
193 oldconf->vht_oper_centr_freq_seg0_idx;
194 hapd->iconf->vht_oper_centr_freq_seg1_idx =
195 oldconf->vht_oper_centr_freq_seg1_idx;
196 hapd->conf = newconf->bss[j];
197 hostapd_reload_bss(hapd);
200 hostapd_config_free(oldconf);
207 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
212 for (i = 0; i < NUM_WEP_KEYS; i++) {
213 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
214 0, NULL, 0, NULL, 0)) {
215 wpa_printf(MSG_DEBUG, "Failed to clear default "
216 "encryption keys (ifname=%s keyidx=%d)",
220 #ifdef CONFIG_IEEE80211W
221 if (hapd->conf->ieee80211w) {
222 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
223 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
226 wpa_printf(MSG_DEBUG, "Failed to clear "
227 "default mgmt encryption keys "
228 "(ifname=%s keyidx=%d)", ifname, i);
232 #endif /* CONFIG_IEEE80211W */
236 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
238 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
243 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
246 struct hostapd_ssid *ssid = &hapd->conf->ssid;
249 if (ssid->wep.default_len &&
250 hostapd_drv_set_key(hapd->conf->iface,
251 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
252 1, NULL, 0, ssid->wep.key[idx],
253 ssid->wep.len[idx])) {
254 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
262 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
264 os_free(hapd->probereq_cb);
265 hapd->probereq_cb = NULL;
266 hapd->num_probereq_cb = 0;
269 wpabuf_free(hapd->p2p_beacon_ie);
270 hapd->p2p_beacon_ie = NULL;
271 wpabuf_free(hapd->p2p_probe_resp_ie);
272 hapd->p2p_probe_resp_ie = NULL;
273 #endif /* CONFIG_P2P */
275 if (!hapd->started) {
276 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
277 __func__, hapd->conf->iface);
282 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
283 iapp_deinit(hapd->iapp);
285 accounting_deinit(hapd);
286 hostapd_deinit_wpa(hapd);
288 hostapd_acl_deinit(hapd);
289 #ifndef CONFIG_NO_RADIUS
290 radius_client_deinit(hapd->radius);
292 radius_das_deinit(hapd->radius_das);
293 hapd->radius_das = NULL;
294 #endif /* CONFIG_NO_RADIUS */
296 hostapd_deinit_wps(hapd);
298 authsrv_deinit(hapd);
300 if (hapd->interface_added) {
301 hapd->interface_added = 0;
302 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
303 wpa_printf(MSG_WARNING,
304 "Failed to remove BSS interface %s",
306 hapd->interface_added = 1;
309 * Since this was a dynamically added interface, the
310 * driver wrapper may have removed its internal instance
311 * and hapd->drv_priv is not valid anymore.
313 hapd->drv_priv = NULL;
317 wpabuf_free(hapd->time_adv);
319 #ifdef CONFIG_INTERWORKING
320 gas_serv_deinit(hapd);
321 #endif /* CONFIG_INTERWORKING */
323 bss_load_update_deinit(hapd);
324 ndisc_snoop_deinit(hapd);
325 dhcp_snoop_deinit(hapd);
326 x_snoop_deinit(hapd);
329 bin_clear_free(hapd->tmp_eap_user.identity,
330 hapd->tmp_eap_user.identity_len);
331 bin_clear_free(hapd->tmp_eap_user.password,
332 hapd->tmp_eap_user.password_len);
333 #endif /* CONFIG_SQLITE */
336 wpabuf_free(hapd->mesh_pending_auth);
337 hapd->mesh_pending_auth = NULL;
338 #endif /* CONFIG_MESH */
340 hostpad_free_neighbor_db(hapd);
345 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
346 * @hapd: Pointer to BSS data
348 * This function is used to free all per-BSS data structures and resources.
349 * Most of the modules that are initialized in hostapd_setup_bss() are
350 * deinitialized here.
352 static void hostapd_cleanup(struct hostapd_data *hapd)
354 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
356 if (hapd->iface->interfaces &&
357 hapd->iface->interfaces->ctrl_iface_deinit)
358 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
359 hostapd_free_hapd_data(hapd);
363 static void sta_track_deinit(struct hostapd_iface *iface)
365 struct hostapd_sta_info *info;
367 if (!iface->num_sta_seen)
370 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
372 dl_list_del(&info->list);
373 iface->num_sta_seen--;
379 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
381 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
382 #ifdef CONFIG_IEEE80211N
384 hostapd_stop_setup_timers(iface);
385 #endif /* NEED_AP_MLME */
386 #endif /* CONFIG_IEEE80211N */
387 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
388 iface->hw_features = NULL;
389 os_free(iface->current_rates);
390 iface->current_rates = NULL;
391 os_free(iface->basic_rates);
392 iface->basic_rates = NULL;
393 ap_list_deinit(iface);
394 sta_track_deinit(iface);
399 * hostapd_cleanup_iface - Complete per-interface cleanup
400 * @iface: Pointer to interface data
402 * This function is called after per-BSS data structures are deinitialized
403 * with hostapd_cleanup().
405 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
407 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
408 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
410 hostapd_cleanup_iface_partial(iface);
411 hostapd_config_free(iface->conf);
414 os_free(iface->config_fname);
416 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
421 static void hostapd_clear_wep(struct hostapd_data *hapd)
423 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
424 hostapd_set_privacy(hapd, 0);
425 hostapd_broadcast_wep_clear(hapd);
430 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
434 hostapd_broadcast_wep_set(hapd);
436 if (hapd->conf->ssid.wep.default_len) {
437 hostapd_set_privacy(hapd, 1);
442 * When IEEE 802.1X is not enabled, the driver may need to know how to
443 * set authentication algorithms for static WEP.
445 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
447 for (i = 0; i < 4; i++) {
448 if (hapd->conf->ssid.wep.key[i] &&
449 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
450 i == hapd->conf->ssid.wep.idx, NULL, 0,
451 hapd->conf->ssid.wep.key[i],
452 hapd->conf->ssid.wep.len[i])) {
453 wpa_printf(MSG_WARNING, "Could not set WEP "
457 if (hapd->conf->ssid.wep.key[i] &&
458 i == hapd->conf->ssid.wep.idx)
459 hostapd_set_privacy(hapd, 1);
466 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
471 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
474 if (!hapd->iface->driver_ap_teardown) {
475 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
476 "Flushing old station entries");
478 if (hostapd_flush(hapd)) {
479 wpa_msg(hapd->msg_ctx, MSG_WARNING,
480 "Could not connect to kernel driver");
484 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
485 os_memset(addr, 0xff, ETH_ALEN);
486 hostapd_drv_sta_deauth(hapd, addr, reason);
487 hostapd_free_stas(hapd);
493 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
495 hostapd_free_stas(hapd);
496 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
497 hostapd_clear_wep(hapd);
502 * hostapd_validate_bssid_configuration - Validate BSSID configuration
503 * @iface: Pointer to interface data
504 * Returns: 0 on success, -1 on failure
506 * This function is used to validate that the configured BSSIDs are valid.
508 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
510 u8 mask[ETH_ALEN] = { 0 };
511 struct hostapd_data *hapd = iface->bss[0];
512 unsigned int i = iface->conf->num_bss, bits = 0, j;
515 if (hostapd_drv_none(hapd))
518 if (iface->conf->use_driver_iface_addr)
521 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
523 /* Determine the bits necessary to cover the number of BSSIDs. */
524 for (i--; i; i >>= 1)
527 /* Determine the bits necessary to any configured BSSIDs,
528 if they are higher than the number of BSSIDs. */
529 for (j = 0; j < iface->conf->num_bss; j++) {
530 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
536 for (i = 0; i < ETH_ALEN; i++) {
538 iface->conf->bss[j]->bssid[i] ^
546 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
552 while (mask[i] != 0) {
562 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
567 os_memset(mask, 0xff, ETH_ALEN);
569 for (i = 5; i > 5 - j; i--)
576 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
577 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
582 for (i = 0; i < ETH_ALEN; i++) {
583 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
584 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
585 " for start address " MACSTR ".",
586 MAC2STR(mask), MAC2STR(hapd->own_addr));
587 wpa_printf(MSG_ERROR, "Start address must be the "
588 "first address in the block (i.e., addr "
589 "AND mask == addr).");
598 static int mac_in_conf(struct hostapd_config *conf, const void *a)
602 for (i = 0; i < conf->num_bss; i++) {
603 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
612 #ifndef CONFIG_NO_RADIUS
614 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
615 struct radius_das_attrs *attr)
617 if (attr->nas_identifier &&
618 (!hapd->conf->nas_identifier ||
619 os_strlen(hapd->conf->nas_identifier) !=
620 attr->nas_identifier_len ||
621 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
622 attr->nas_identifier_len) != 0)) {
623 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
627 if (attr->nas_ip_addr &&
628 (hapd->conf->own_ip_addr.af != AF_INET ||
629 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
631 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
636 if (attr->nas_ipv6_addr &&
637 (hapd->conf->own_ip_addr.af != AF_INET6 ||
638 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
640 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
643 #endif /* CONFIG_IPV6 */
649 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
650 struct radius_das_attrs *attr,
653 struct sta_info *selected, *sta;
660 for (sta = hapd->sta_list; sta; sta = sta->next)
661 sta->radius_das_match = 1;
663 if (attr->sta_addr) {
665 sta = ap_get_sta(hapd, attr->sta_addr);
667 wpa_printf(MSG_DEBUG,
668 "RADIUS DAS: No Calling-Station-Id match");
673 for (sta = hapd->sta_list; sta; sta = sta->next) {
675 sta->radius_das_match = 0;
677 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
680 if (attr->acct_session_id) {
682 if (attr->acct_session_id_len != 16) {
683 wpa_printf(MSG_DEBUG,
684 "RADIUS DAS: Acct-Session-Id cannot match");
689 for (sta = hapd->sta_list; sta; sta = sta->next) {
690 if (!sta->radius_das_match)
692 os_snprintf(buf, sizeof(buf), "%016llX",
693 (unsigned long long) sta->acct_session_id);
694 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
695 sta->radius_das_match = 0;
701 wpa_printf(MSG_DEBUG,
702 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
705 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
708 if (attr->acct_multi_session_id) {
710 if (attr->acct_multi_session_id_len != 16) {
711 wpa_printf(MSG_DEBUG,
712 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
717 for (sta = hapd->sta_list; sta; sta = sta->next) {
718 if (!sta->radius_das_match)
720 if (!sta->eapol_sm ||
721 !sta->eapol_sm->acct_multi_session_id) {
722 sta->radius_das_match = 0;
725 os_snprintf(buf, sizeof(buf), "%016llX",
727 sta->eapol_sm->acct_multi_session_id);
728 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
730 sta->radius_das_match = 0;
736 wpa_printf(MSG_DEBUG,
737 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
740 wpa_printf(MSG_DEBUG,
741 "RADIUS DAS: Acct-Multi-Session-Id match");
748 for (sta = hapd->sta_list; sta; sta = sta->next) {
751 if (!sta->radius_das_match)
753 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
754 if (!cui || wpabuf_len(cui) != attr->cui_len ||
755 os_memcmp(wpabuf_head(cui), attr->cui,
757 sta->radius_das_match = 0;
763 wpa_printf(MSG_DEBUG,
764 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
767 wpa_printf(MSG_DEBUG,
768 "RADIUS DAS: Chargeable-User-Identity match");
771 if (attr->user_name) {
775 for (sta = hapd->sta_list; sta; sta = sta->next) {
779 if (!sta->radius_das_match)
781 identity = ieee802_1x_get_identity(sta->eapol_sm,
784 identity_len != attr->user_name_len ||
785 os_memcmp(identity, attr->user_name, identity_len)
787 sta->radius_das_match = 0;
793 wpa_printf(MSG_DEBUG,
794 "RADIUS DAS: No matches remaining after User-Name check");
797 wpa_printf(MSG_DEBUG,
798 "RADIUS DAS: User-Name match");
803 * In theory, we could match all current associations, but it
804 * seems safer to just reject requests that do not include any
805 * session identification attributes.
807 wpa_printf(MSG_DEBUG,
808 "RADIUS DAS: No session identification attributes included");
813 for (sta = hapd->sta_list; sta; sta = sta->next) {
814 if (sta->radius_das_match) {
827 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
828 struct radius_das_attrs *attr)
832 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
836 static enum radius_das_res
837 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
839 struct hostapd_data *hapd = ctx;
840 struct sta_info *sta;
843 if (hostapd_das_nas_mismatch(hapd, attr))
844 return RADIUS_DAS_NAS_MISMATCH;
846 sta = hostapd_das_find_sta(hapd, attr, &multi);
849 wpa_printf(MSG_DEBUG,
850 "RADIUS DAS: Multiple sessions match - not supported");
851 return RADIUS_DAS_MULTI_SESSION_MATCH;
853 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
854 wpa_printf(MSG_DEBUG,
855 "RADIUS DAS: PMKSA cache entry matched");
856 return RADIUS_DAS_SUCCESS;
858 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
859 return RADIUS_DAS_SESSION_NOT_FOUND;
862 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
863 " - disconnecting", MAC2STR(sta->addr));
864 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
866 hostapd_drv_sta_deauth(hapd, sta->addr,
867 WLAN_REASON_PREV_AUTH_NOT_VALID);
868 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
870 return RADIUS_DAS_SUCCESS;
873 #endif /* CONFIG_NO_RADIUS */
877 * hostapd_setup_bss - Per-BSS setup (initialization)
878 * @hapd: Pointer to BSS data
879 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
880 * but interface may exist
882 * This function is used to initialize all per-BSS data structures and
883 * resources. This gets called in a loop for each BSS when an interface is
884 * initialized. Most of the modules that are initialized here will be
885 * deinitialized in hostapd_cleanup().
887 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
889 struct hostapd_bss_config *conf = hapd->conf;
890 u8 ssid[SSID_MAX_LEN + 1];
891 int ssid_len, set_ssid;
892 char force_ifname[IFNAMSIZ];
893 u8 if_addr[ETH_ALEN];
894 int flush_old_stations = 1;
896 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
897 __func__, hapd, conf->iface, first);
899 #ifdef EAP_SERVER_TNC
900 if (conf->tnc && tncs_global_init() < 0) {
901 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
904 #endif /* EAP_SERVER_TNC */
907 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
908 __func__, conf->iface);
912 dl_list_init(&hapd->nr_db);
914 if (!first || first == -1) {
915 u8 *addr = hapd->own_addr;
917 if (!is_zero_ether_addr(conf->bssid)) {
918 /* Allocate the configured BSSID. */
919 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
921 if (hostapd_mac_comp(hapd->own_addr,
922 hapd->iface->bss[0]->own_addr) ==
924 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
925 "BSSID set to the MAC address of "
926 "the radio", conf->iface);
929 } else if (hapd->iconf->use_driver_iface_addr) {
932 /* Allocate the next available BSSID. */
934 inc_byte_array(hapd->own_addr, ETH_ALEN);
935 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
938 hapd->interface_added = 1;
939 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
940 conf->iface, addr, hapd,
941 &hapd->drv_priv, force_ifname, if_addr,
942 conf->bridge[0] ? conf->bridge : NULL,
944 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
945 MACSTR ")", MAC2STR(hapd->own_addr));
946 hapd->interface_added = 0;
951 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
954 if (conf->wmm_enabled < 0)
955 conf->wmm_enabled = hapd->iconf->ieee80211n;
957 #ifdef CONFIG_IEEE80211R
958 if (is_zero_ether_addr(conf->r1_key_holder))
959 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
960 #endif /* CONFIG_IEEE80211R */
963 if (hapd->iface->mconf == NULL)
964 flush_old_stations = 0;
965 #endif /* CONFIG_MESH */
967 if (flush_old_stations)
968 hostapd_flush_old_stations(hapd,
969 WLAN_REASON_PREV_AUTH_NOT_VALID);
970 hostapd_set_privacy(hapd, 0);
972 hostapd_broadcast_wep_clear(hapd);
973 if (hostapd_setup_encryption(conf->iface, hapd))
977 * Fetch the SSID from the system and use it or,
978 * if one was specified in the config file, verify they
981 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
983 wpa_printf(MSG_ERROR, "Could not read SSID from system");
986 if (conf->ssid.ssid_set) {
988 * If SSID is specified in the config file and it differs
989 * from what is being used then force installation of the
992 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
993 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
996 * No SSID in the config file; just use the one we got
1000 conf->ssid.ssid_len = ssid_len;
1001 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1004 if (!hostapd_drv_none(hapd)) {
1005 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1007 conf->iface, MAC2STR(hapd->own_addr),
1008 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
1011 if (hostapd_setup_wpa_psk(conf)) {
1012 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1016 /* Set SSID for the kernel driver (to be used in beacon and probe
1017 * response frames) */
1018 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
1019 conf->ssid.ssid_len)) {
1020 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1024 if (wpa_debug_level <= MSG_MSGDUMP)
1025 conf->radius->msg_dumps = 1;
1026 #ifndef CONFIG_NO_RADIUS
1027 hapd->radius = radius_client_init(hapd, conf->radius);
1028 if (hapd->radius == NULL) {
1029 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1033 if (conf->radius_das_port) {
1034 struct radius_das_conf das_conf;
1035 os_memset(&das_conf, 0, sizeof(das_conf));
1036 das_conf.port = conf->radius_das_port;
1037 das_conf.shared_secret = conf->radius_das_shared_secret;
1038 das_conf.shared_secret_len =
1039 conf->radius_das_shared_secret_len;
1040 das_conf.client_addr = &conf->radius_das_client_addr;
1041 das_conf.time_window = conf->radius_das_time_window;
1042 das_conf.require_event_timestamp =
1043 conf->radius_das_require_event_timestamp;
1044 das_conf.ctx = hapd;
1045 das_conf.disconnect = hostapd_das_disconnect;
1046 hapd->radius_das = radius_das_init(&das_conf);
1047 if (hapd->radius_das == NULL) {
1048 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1053 #endif /* CONFIG_NO_RADIUS */
1055 if (hostapd_acl_init(hapd)) {
1056 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1059 if (hostapd_init_wps(hapd, conf))
1062 if (authsrv_init(hapd) < 0)
1065 if (ieee802_1x_init(hapd)) {
1066 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1070 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1073 if (accounting_init(hapd)) {
1074 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1078 if (conf->ieee802_11f &&
1079 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1080 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1085 #ifdef CONFIG_INTERWORKING
1086 if (gas_serv_init(hapd)) {
1087 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1091 if (conf->qos_map_set_len &&
1092 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1093 conf->qos_map_set_len)) {
1094 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1097 #endif /* CONFIG_INTERWORKING */
1099 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1100 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1104 if (conf->proxy_arp) {
1105 if (x_snoop_init(hapd)) {
1106 wpa_printf(MSG_ERROR,
1107 "Generic snooping infrastructure initialization failed");
1111 if (dhcp_snoop_init(hapd)) {
1112 wpa_printf(MSG_ERROR,
1113 "DHCP snooping initialization failed");
1117 if (ndisc_snoop_init(hapd)) {
1118 wpa_printf(MSG_ERROR,
1119 "Neighbor Discovery snooping initialization failed");
1124 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1125 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1129 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1132 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1135 if (hapd->driver && hapd->driver->set_operstate)
1136 hapd->driver->set_operstate(hapd->drv_priv, 1);
1142 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1144 struct hostapd_data *hapd = iface->bss[0];
1146 struct hostapd_tx_queue_params *p;
1149 if (iface->mconf == NULL)
1151 #endif /* CONFIG_MESH */
1153 for (i = 0; i < NUM_TX_QUEUES; i++) {
1154 p = &iface->conf->tx_queue[i];
1156 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1157 p->cwmax, p->burst)) {
1158 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1159 "parameters for queue %d.", i);
1160 /* Continue anyway */
1166 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1167 struct mac_acl_entry *mac_acl,
1168 int n_entries, u8 accept_acl)
1170 struct hostapd_acl_params *acl_params;
1173 acl_params = os_zalloc(sizeof(*acl_params) +
1174 (n_entries * sizeof(acl_params->mac_acl[0])));
1178 for (i = 0; i < n_entries; i++)
1179 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1182 acl_params->acl_policy = accept_acl;
1183 acl_params->num_mac_acl = n_entries;
1185 err = hostapd_drv_set_acl(hapd, acl_params);
1187 os_free(acl_params);
1193 static void hostapd_set_acl(struct hostapd_data *hapd)
1195 struct hostapd_config *conf = hapd->iconf;
1199 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1202 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1204 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1205 conf->bss[0]->num_accept_mac,
1208 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1211 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1213 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1214 conf->bss[0]->num_deny_mac,
1217 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1224 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1226 if (!hapd->iface->interfaces ||
1227 !hapd->iface->interfaces->ctrl_iface_init)
1230 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1231 wpa_printf(MSG_ERROR,
1232 "Failed to setup control interface for %s",
1241 static int start_ctrl_iface(struct hostapd_iface *iface)
1245 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1248 for (i = 0; i < iface->num_bss; i++) {
1249 struct hostapd_data *hapd = iface->bss[i];
1250 if (iface->interfaces->ctrl_iface_init(hapd)) {
1251 wpa_printf(MSG_ERROR,
1252 "Failed to setup control interface for %s",
1262 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1264 struct hostapd_iface *iface = eloop_ctx;
1266 if (!iface->wait_channel_update) {
1267 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1272 * It is possible that the existing channel list is acceptable, so try
1275 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1276 setup_interface2(iface);
1280 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1282 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1285 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1286 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1287 setup_interface2(iface);
1291 static int setup_interface(struct hostapd_iface *iface)
1293 struct hostapd_data *hapd = iface->bss[0];
1297 * It is possible that setup_interface() is called after the interface
1298 * was disabled etc., in which case driver_ap_teardown is possibly set
1299 * to 1. Clear it here so any other key/station deletion, which is not
1300 * part of a teardown flow, would also call the relevant driver
1303 iface->driver_ap_teardown = 0;
1305 if (!iface->phy[0]) {
1306 const char *phy = hostapd_drv_get_radio_name(hapd);
1308 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1309 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1314 * Make sure that all BSSes get configured with a pointer to the same
1317 for (i = 1; i < iface->num_bss; i++) {
1318 iface->bss[i]->driver = hapd->driver;
1319 iface->bss[i]->drv_priv = hapd->drv_priv;
1322 if (hostapd_validate_bssid_configuration(iface))
1326 * Initialize control interfaces early to allow external monitoring of
1327 * channel setup operations that may take considerable amount of time
1328 * especially for DFS cases.
1330 if (start_ctrl_iface(iface))
1333 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1334 char country[4], previous_country[4];
1336 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1337 if (hostapd_get_country(hapd, previous_country) < 0)
1338 previous_country[0] = '\0';
1340 os_memcpy(country, hapd->iconf->country, 3);
1342 if (hostapd_set_country(hapd, country) < 0) {
1343 wpa_printf(MSG_ERROR, "Failed to set country code");
1347 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1348 previous_country, country);
1350 if (os_strncmp(previous_country, country, 2) != 0) {
1351 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1352 iface->wait_channel_update = 1;
1353 eloop_register_timeout(5, 0,
1354 channel_list_update_timeout,
1360 return setup_interface2(iface);
1364 static int setup_interface2(struct hostapd_iface *iface)
1366 iface->wait_channel_update = 0;
1368 if (hostapd_get_hw_features(iface)) {
1369 /* Not all drivers support this yet, so continue without hw
1372 int ret = hostapd_select_hw_mode(iface);
1374 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1375 "channel. (%d)", ret);
1379 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1382 ret = hostapd_check_ht_capab(iface);
1386 wpa_printf(MSG_DEBUG, "Interface initialization will "
1387 "be completed in a callback");
1391 if (iface->conf->ieee80211h)
1392 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1394 return hostapd_setup_interface_complete(iface, 0);
1397 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1398 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1399 if (iface->interfaces && iface->interfaces->terminate_on_error)
1407 static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1409 struct hostapd_data *hapd = ctx;
1411 return hapd->own_addr;
1415 static void fst_hostapd_get_channel_info_cb(void *ctx,
1416 enum hostapd_hw_mode *hw_mode,
1419 struct hostapd_data *hapd = ctx;
1421 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1425 static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1427 struct hostapd_data *hapd = ctx;
1429 if (hapd->iface->fst_ies != fst_ies) {
1430 hapd->iface->fst_ies = fst_ies;
1431 if (ieee802_11_set_beacon(hapd))
1432 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1437 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1440 struct hostapd_data *hapd = ctx;
1442 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1443 wpabuf_head(buf), wpabuf_len(buf));
1447 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1449 struct hostapd_data *hapd = ctx;
1450 struct sta_info *sta = ap_get_sta(hapd, addr);
1452 return sta ? sta->mb_ies : NULL;
1456 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1457 const u8 *buf, size_t size)
1459 struct hostapd_data *hapd = ctx;
1460 struct sta_info *sta = ap_get_sta(hapd, addr);
1463 struct mb_ies_info info;
1465 if (!mb_ies_info_by_ies(&info, buf, size)) {
1466 wpabuf_free(sta->mb_ies);
1467 sta->mb_ies = mb_ies_by_info(&info);
1473 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1476 struct sta_info *s = (struct sta_info *) *get_ctx;
1479 for (; s && !s->mb_ies; s = s->next)
1484 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1494 static const u8 * fst_hostapd_get_peer_first(void *ctx,
1495 struct fst_get_peer_ctx **get_ctx,
1498 struct hostapd_data *hapd = ctx;
1500 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1502 return fst_hostapd_get_sta(get_ctx, mb_only);
1506 static const u8 * fst_hostapd_get_peer_next(void *ctx,
1507 struct fst_get_peer_ctx **get_ctx,
1510 return fst_hostapd_get_sta(get_ctx, mb_only);
1514 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1515 struct fst_wpa_obj *iface_obj)
1517 iface_obj->ctx = hapd;
1518 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1519 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1520 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1521 iface_obj->send_action = fst_hostapd_send_action_cb;
1522 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1523 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1524 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1525 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1528 #endif /* CONFIG_FST */
1531 static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1534 struct hostapd_data *hapd = iface->bss[0];
1537 int delay_apply_cfg = 0;
1538 int res_dfs_offload = 0;
1543 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1544 if (iface->conf->channel) {
1547 #endif /* NEED_AP_MLME */
1549 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1550 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1551 "Frequency: %d MHz",
1552 hostapd_hw_mode_txt(iface->conf->hw_mode),
1553 iface->conf->channel, iface->freq);
1556 /* Handle DFS only if it is not offloaded to the driver */
1557 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1559 res = hostapd_handle_dfs(iface);
1566 /* If DFS is offloaded to the driver */
1567 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1568 if (res_dfs_offload <= 0) {
1569 if (res_dfs_offload < 0)
1572 wpa_printf(MSG_DEBUG,
1573 "Proceed with AP/channel setup");
1575 * If this is a DFS channel, move to completing
1578 if (res_dfs_offload == 1)
1580 /* Otherwise fall through. */
1583 #endif /* NEED_AP_MLME */
1586 if (iface->mconf != NULL) {
1587 wpa_printf(MSG_DEBUG,
1588 "%s: Mesh configuration will be applied while joining the mesh network",
1589 iface->bss[0]->conf->iface);
1590 delay_apply_cfg = 1;
1592 #endif /* CONFIG_MESH */
1594 if (!delay_apply_cfg &&
1595 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1596 hapd->iconf->channel,
1597 hapd->iconf->ieee80211n,
1598 hapd->iconf->ieee80211ac,
1599 hapd->iconf->secondary_channel,
1600 hapd->iconf->vht_oper_chwidth,
1601 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1602 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1603 wpa_printf(MSG_ERROR, "Could not set channel for "
1609 if (iface->current_mode) {
1610 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1611 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1613 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1614 HOSTAPD_LEVEL_WARNING,
1615 "Failed to prepare rates table.");
1620 if (hapd->iconf->rts_threshold > -1 &&
1621 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1622 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1627 if (hapd->iconf->fragm_threshold > -1 &&
1628 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1629 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1630 "for kernel driver");
1634 prev_addr = hapd->own_addr;
1636 for (j = 0; j < iface->num_bss; j++) {
1637 hapd = iface->bss[j];
1639 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1640 if (hostapd_setup_bss(hapd, j == 0)) {
1642 hapd = iface->bss[j];
1643 hostapd_bss_deinit_no_free(hapd);
1644 hostapd_free_hapd_data(hapd);
1648 if (is_zero_ether_addr(hapd->conf->bssid))
1649 prev_addr = hapd->own_addr;
1651 hapd = iface->bss[0];
1653 hostapd_tx_queue_params(iface);
1655 ap_list_init(iface);
1656 dl_list_init(&iface->sta_seen);
1658 hostapd_set_acl(hapd);
1660 if (hostapd_driver_commit(hapd) < 0) {
1661 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1662 "configuration", __func__);
1667 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1668 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1669 * mode), the interface is up only after driver_commit, so initialize
1670 * WPS after driver_commit.
1672 for (j = 0; j < iface->num_bss; j++) {
1673 if (hostapd_init_wps_complete(iface->bss[j]))
1677 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1680 * If freq is DFS, and DFS is offloaded to the driver, then wait
1681 * for CAC to complete.
1683 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1684 return res_dfs_offload;
1689 #endif /* NEED_AP_MLME */
1692 if (hapd->iconf->fst_cfg.group_id[0]) {
1693 struct fst_wpa_obj iface_obj;
1695 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1696 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1697 &iface_obj, &hapd->iconf->fst_cfg);
1699 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1700 hapd->iconf->fst_cfg.group_id);
1704 #endif /* CONFIG_FST */
1706 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1707 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1708 if (hapd->setup_complete_cb)
1709 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1711 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1712 iface->bss[0]->conf->iface);
1713 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1714 iface->interfaces->terminate_on_error--;
1719 wpa_printf(MSG_ERROR, "Interface initialization failed");
1720 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1721 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1724 fst_detach(iface->fst);
1727 #endif /* CONFIG_FST */
1728 if (iface->interfaces && iface->interfaces->terminate_on_error)
1735 * hostapd_setup_interface_complete - Complete interface setup
1737 * This function is called when previous steps in the interface setup has been
1738 * completed. This can also start operations, e.g., DFS, that will require
1739 * additional processing before interface is ready to be enabled. Such
1740 * operations will call this function from eloop callbacks when finished.
1742 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1744 struct hapd_interfaces *interfaces = iface->interfaces;
1745 struct hostapd_data *hapd = iface->bss[0];
1747 int not_ready_in_sync_ifaces = 0;
1749 if (!iface->need_to_start_in_sync)
1750 return hostapd_setup_interface_complete_sync(iface, err);
1753 wpa_printf(MSG_ERROR, "Interface initialization failed");
1754 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1755 iface->need_to_start_in_sync = 0;
1756 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1757 if (interfaces && interfaces->terminate_on_error)
1762 if (iface->ready_to_start_in_sync) {
1763 /* Already in ready and waiting. should never happpen */
1767 for (i = 0; i < interfaces->count; i++) {
1768 if (interfaces->iface[i]->need_to_start_in_sync &&
1769 !interfaces->iface[i]->ready_to_start_in_sync)
1770 not_ready_in_sync_ifaces++;
1774 * Check if this is the last interface, if yes then start all the other
1775 * waiting interfaces. If not, add this interface to the waiting list.
1777 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
1779 * If this interface went through CAC, do not synchronize, just
1780 * start immediately.
1782 iface->need_to_start_in_sync = 0;
1783 wpa_printf(MSG_INFO,
1784 "%s: Finished CAC - bypass sync and start interface",
1785 iface->bss[0]->conf->iface);
1786 return hostapd_setup_interface_complete_sync(iface, err);
1789 if (not_ready_in_sync_ifaces > 1) {
1790 /* need to wait as there are other interfaces still coming up */
1791 iface->ready_to_start_in_sync = 1;
1792 wpa_printf(MSG_INFO,
1793 "%s: Interface waiting to sync with other interfaces",
1794 iface->bss[0]->conf->iface);
1798 wpa_printf(MSG_INFO,
1799 "%s: Last interface to sync - starting all interfaces",
1800 iface->bss[0]->conf->iface);
1801 iface->need_to_start_in_sync = 0;
1802 hostapd_setup_interface_complete_sync(iface, err);
1803 for (i = 0; i < interfaces->count; i++) {
1804 if (interfaces->iface[i]->need_to_start_in_sync &&
1805 interfaces->iface[i]->ready_to_start_in_sync) {
1806 hostapd_setup_interface_complete_sync(
1807 interfaces->iface[i], 0);
1808 /* Only once the interfaces are sync started */
1809 interfaces->iface[i]->need_to_start_in_sync = 0;
1818 * hostapd_setup_interface - Setup of an interface
1819 * @iface: Pointer to interface data.
1820 * Returns: 0 on success, -1 on failure
1822 * Initializes the driver interface, validates the configuration,
1823 * and sets driver parameters based on the configuration.
1824 * Flushes old stations, sets the channel, encryption,
1825 * beacons, and WDS links based on the configuration.
1827 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1828 * or DFS operations, this function returns 0 before such operations have been
1829 * completed. The pending operations are registered into eloop and will be
1830 * completed from eloop callbacks. Those callbacks end up calling
1831 * hostapd_setup_interface_complete() once setup has been completed.
1833 int hostapd_setup_interface(struct hostapd_iface *iface)
1837 ret = setup_interface(iface);
1839 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1840 iface->bss[0]->conf->iface);
1849 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1850 * @hapd_iface: Pointer to interface data
1851 * @conf: Pointer to per-interface configuration
1852 * @bss: Pointer to per-BSS configuration for this BSS
1853 * Returns: Pointer to allocated BSS data
1855 * This function is used to allocate per-BSS data structure. This data will be
1856 * freed after hostapd_cleanup() is called for it during interface
1859 struct hostapd_data *
1860 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1861 struct hostapd_config *conf,
1862 struct hostapd_bss_config *bss)
1864 struct hostapd_data *hapd;
1866 hapd = os_zalloc(sizeof(*hapd));
1870 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1873 hapd->iface = hapd_iface;
1874 hapd->driver = hapd->iconf->driver;
1875 hapd->ctrl_sock = -1;
1876 dl_list_init(&hapd->ctrl_dst);
1882 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1884 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1886 hostapd_bss_deinit_no_free(hapd);
1887 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1888 hostapd_cleanup(hapd);
1892 void hostapd_interface_deinit(struct hostapd_iface *iface)
1896 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1900 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1902 #ifdef CONFIG_IEEE80211N
1904 hostapd_stop_setup_timers(iface);
1905 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1906 #endif /* NEED_AP_MLME */
1907 #endif /* CONFIG_IEEE80211N */
1908 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1909 iface->wait_channel_update = 0;
1913 fst_detach(iface->fst);
1916 #endif /* CONFIG_FST */
1918 for (j = iface->num_bss - 1; j >= 0; j--)
1919 hostapd_bss_deinit(iface->bss[j]);
1923 void hostapd_interface_free(struct hostapd_iface *iface)
1926 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1927 for (j = 0; j < iface->num_bss; j++) {
1928 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1929 __func__, iface->bss[j]);
1930 os_free(iface->bss[j]);
1932 hostapd_cleanup_iface(iface);
1937 * hostapd_init - Allocate and initialize per-interface data
1938 * @config_file: Path to the configuration file
1939 * Returns: Pointer to the allocated interface data or %NULL on failure
1941 * This function is used to allocate main data structures for per-interface
1942 * data. The allocated data buffer will be freed by calling
1943 * hostapd_cleanup_iface().
1945 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1946 const char *config_file)
1948 struct hostapd_iface *hapd_iface = NULL;
1949 struct hostapd_config *conf = NULL;
1950 struct hostapd_data *hapd;
1953 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1954 if (hapd_iface == NULL)
1957 hapd_iface->config_fname = os_strdup(config_file);
1958 if (hapd_iface->config_fname == NULL)
1961 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1964 hapd_iface->conf = conf;
1966 hapd_iface->num_bss = conf->num_bss;
1967 hapd_iface->bss = os_calloc(conf->num_bss,
1968 sizeof(struct hostapd_data *));
1969 if (hapd_iface->bss == NULL)
1972 for (i = 0; i < conf->num_bss; i++) {
1973 hapd = hapd_iface->bss[i] =
1974 hostapd_alloc_bss_data(hapd_iface, conf,
1978 hapd->msg_ctx = hapd;
1984 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1987 hostapd_config_free(conf);
1989 os_free(hapd_iface->config_fname);
1990 os_free(hapd_iface->bss);
1991 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1992 __func__, hapd_iface);
1993 os_free(hapd_iface);
1999 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2003 for (i = 0; i < interfaces->count; i++) {
2004 struct hostapd_iface *iface = interfaces->iface[i];
2005 for (j = 0; j < iface->num_bss; j++) {
2006 struct hostapd_data *hapd = iface->bss[j];
2007 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2017 * hostapd_interface_init_bss - Read configuration file and init BSS data
2019 * This function is used to parse configuration file for a BSS. This BSS is
2020 * added to an existing interface sharing the same radio (if any) or a new
2021 * interface is created if this is the first interface on a radio. This
2022 * allocate memory for the BSS. No actual driver operations are started.
2024 * This is similar to hostapd_interface_init(), but for a case where the
2025 * configuration is used to add a single BSS instead of all BSSes for a radio.
2027 struct hostapd_iface *
2028 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2029 const char *config_fname, int debug)
2031 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2032 struct hostapd_data *hapd;
2039 for (i = 0; i < interfaces->count; i++) {
2040 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2041 iface = interfaces->iface[i];
2046 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2047 config_fname, phy, iface ? "" : " --> new PHY");
2049 struct hostapd_config *conf;
2050 struct hostapd_bss_config **tmp_conf;
2051 struct hostapd_data **tmp_bss;
2052 struct hostapd_bss_config *bss;
2055 /* Add new BSS to existing iface */
2056 conf = interfaces->config_read_cb(config_fname);
2059 if (conf->num_bss > 1) {
2060 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2061 hostapd_config_free(conf);
2065 ifname = conf->bss[0]->iface;
2066 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2067 wpa_printf(MSG_ERROR,
2068 "Interface name %s already in use", ifname);
2069 hostapd_config_free(conf);
2073 tmp_conf = os_realloc_array(
2074 iface->conf->bss, iface->conf->num_bss + 1,
2075 sizeof(struct hostapd_bss_config *));
2076 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2077 sizeof(struct hostapd_data *));
2079 iface->bss = tmp_bss;
2081 iface->conf->bss = tmp_conf;
2082 iface->conf->last_bss = tmp_conf[0];
2084 if (tmp_bss == NULL || tmp_conf == NULL) {
2085 hostapd_config_free(conf);
2088 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2089 iface->conf->num_bss++;
2091 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2093 iface->conf->num_bss--;
2094 hostapd_config_free(conf);
2097 iface->conf->last_bss = bss;
2098 iface->bss[iface->num_bss] = hapd;
2099 hapd->msg_ctx = hapd;
2101 bss_idx = iface->num_bss++;
2103 conf->bss[0] = NULL;
2104 hostapd_config_free(conf);
2106 /* Add a new iface with the first BSS */
2107 new_iface = iface = hostapd_init(interfaces, config_fname);
2110 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2111 iface->interfaces = interfaces;
2115 for (k = 0; k < debug; k++) {
2116 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2117 iface->bss[bss_idx]->conf->logger_stdout_level--;
2120 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2121 !hostapd_drv_none(iface->bss[bss_idx])) {
2122 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2125 hostapd_interface_deinit_free(new_iface);
2133 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2135 const struct wpa_driver_ops *driver;
2138 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2141 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2142 __func__, (unsigned int) iface->num_bss,
2143 (unsigned int) iface->conf->num_bss);
2144 driver = iface->bss[0]->driver;
2145 drv_priv = iface->bss[0]->drv_priv;
2146 hostapd_interface_deinit(iface);
2147 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2148 __func__, driver, drv_priv);
2149 if (driver && driver->hapd_deinit && drv_priv) {
2150 driver->hapd_deinit(drv_priv);
2151 iface->bss[0]->drv_priv = NULL;
2153 hostapd_interface_free(iface);
2157 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2159 struct hostapd_iface *hapd_iface)
2163 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2164 __func__, driver, drv_priv);
2165 if (driver && driver->hapd_deinit && drv_priv) {
2166 driver->hapd_deinit(drv_priv);
2167 for (j = 0; j < hapd_iface->num_bss; j++) {
2168 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2170 hapd_iface->bss[j]->drv_priv);
2171 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2172 hapd_iface->bss[j]->drv_priv = NULL;
2178 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2182 if (hapd_iface->bss[0]->drv_priv != NULL) {
2183 wpa_printf(MSG_ERROR, "Interface %s already enabled",
2184 hapd_iface->conf->bss[0]->iface);
2188 wpa_printf(MSG_DEBUG, "Enable interface %s",
2189 hapd_iface->conf->bss[0]->iface);
2191 for (j = 0; j < hapd_iface->num_bss; j++)
2192 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2193 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2194 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2198 if (hapd_iface->interfaces == NULL ||
2199 hapd_iface->interfaces->driver_init == NULL ||
2200 hapd_iface->interfaces->driver_init(hapd_iface))
2203 if (hostapd_setup_interface(hapd_iface)) {
2204 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2205 hapd_iface->bss[0]->drv_priv,
2214 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2218 wpa_printf(MSG_DEBUG, "Reload interface %s",
2219 hapd_iface->conf->bss[0]->iface);
2220 for (j = 0; j < hapd_iface->num_bss; j++)
2221 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2222 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2223 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2226 hostapd_clear_old(hapd_iface);
2227 for (j = 0; j < hapd_iface->num_bss; j++)
2228 hostapd_reload_bss(hapd_iface->bss[j]);
2234 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2237 const struct wpa_driver_ops *driver;
2240 if (hapd_iface == NULL)
2243 if (hapd_iface->bss[0]->drv_priv == NULL) {
2244 wpa_printf(MSG_INFO, "Interface %s already disabled",
2245 hapd_iface->conf->bss[0]->iface);
2249 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2250 driver = hapd_iface->bss[0]->driver;
2251 drv_priv = hapd_iface->bss[0]->drv_priv;
2253 hapd_iface->driver_ap_teardown =
2254 !!(hapd_iface->drv_flags &
2255 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2257 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
2258 for (j = 0; j < hapd_iface->num_bss; j++) {
2259 struct hostapd_data *hapd = hapd_iface->bss[j];
2260 hostapd_bss_deinit_no_free(hapd);
2261 hostapd_free_hapd_data(hapd);
2264 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
2266 /* From hostapd_cleanup_iface: These were initialized in
2267 * hostapd_setup_interface and hostapd_setup_interface_complete
2269 hostapd_cleanup_iface_partial(hapd_iface);
2271 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2272 hapd_iface->bss[0]->conf->iface);
2273 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2278 static struct hostapd_iface *
2279 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2281 struct hostapd_iface **iface, *hapd_iface;
2283 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2284 sizeof(struct hostapd_iface *));
2287 interfaces->iface = iface;
2288 hapd_iface = interfaces->iface[interfaces->count] =
2289 os_zalloc(sizeof(*hapd_iface));
2290 if (hapd_iface == NULL) {
2291 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2292 "the interface", __func__);
2295 interfaces->count++;
2296 hapd_iface->interfaces = interfaces;
2302 static struct hostapd_config *
2303 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2304 const char *ctrl_iface, const char *driver)
2306 struct hostapd_bss_config *bss;
2307 struct hostapd_config *conf;
2309 /* Allocates memory for bss and conf */
2310 conf = hostapd_config_defaults();
2312 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2313 "configuration", __func__);
2320 for (j = 0; wpa_drivers[j]; j++) {
2321 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2322 conf->driver = wpa_drivers[j];
2327 wpa_printf(MSG_ERROR,
2328 "Invalid/unknown driver '%s' - registering the default driver",
2332 conf->driver = wpa_drivers[0];
2333 if (conf->driver == NULL) {
2334 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2335 hostapd_config_free(conf);
2340 bss = conf->last_bss = conf->bss[0];
2342 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2343 bss->ctrl_interface = os_strdup(ctrl_iface);
2344 if (bss->ctrl_interface == NULL) {
2345 hostapd_config_free(conf);
2349 /* Reading configuration file skipped, will be done in SET!
2350 * From reading the configuration till the end has to be done in
2357 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2358 struct hostapd_config *conf)
2361 struct hostapd_data *hapd;
2363 hapd_iface->bss = os_calloc(conf->num_bss,
2364 sizeof(struct hostapd_data *));
2365 if (hapd_iface->bss == NULL)
2368 for (i = 0; i < conf->num_bss; i++) {
2369 hapd = hapd_iface->bss[i] =
2370 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2374 os_free(hapd_iface->bss[i]);
2375 hapd_iface->bss[i] = NULL;
2377 os_free(hapd_iface->bss);
2378 hapd_iface->bss = NULL;
2381 hapd->msg_ctx = hapd;
2384 hapd_iface->conf = conf;
2385 hapd_iface->num_bss = conf->num_bss;
2391 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2393 struct hostapd_config *conf = NULL;
2394 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2395 struct hostapd_data *hapd;
2398 const char *conf_file = NULL, *phy_name = NULL;
2400 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2402 phy_name = buf + 11;
2403 pos = os_strchr(phy_name, ':');
2408 if (!os_strlen(conf_file))
2411 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2415 for (j = 0; j < interfaces->count; j++) {
2416 if (interfaces->iface[j] == hapd_iface)
2419 if (j == interfaces->count) {
2420 struct hostapd_iface **tmp;
2421 tmp = os_realloc_array(interfaces->iface,
2422 interfaces->count + 1,
2423 sizeof(struct hostapd_iface *));
2425 hostapd_interface_deinit_free(hapd_iface);
2428 interfaces->iface = tmp;
2429 interfaces->iface[interfaces->count++] = hapd_iface;
2430 new_iface = hapd_iface;
2434 if (interfaces->driver_init(hapd_iface))
2437 if (hostapd_setup_interface(hapd_iface)) {
2438 hostapd_deinit_driver(
2439 hapd_iface->bss[0]->driver,
2440 hapd_iface->bss[0]->drv_priv,
2445 /* Assign new BSS with bss[0]'s driver info */
2446 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2447 hapd->driver = hapd_iface->bss[0]->driver;
2448 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2449 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2452 if (start_ctrl_iface_bss(hapd) < 0 ||
2453 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2454 hostapd_setup_bss(hapd, -1))) {
2455 hostapd_cleanup(hapd);
2456 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2457 hapd_iface->conf->num_bss--;
2458 hapd_iface->num_bss--;
2459 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2460 __func__, hapd, hapd->conf->iface);
2461 hostapd_config_free_bss(hapd->conf);
2470 ptr = os_strchr(buf, ' ');
2475 if (os_strncmp(ptr, "config=", 7) == 0)
2476 conf_file = ptr + 7;
2478 for (i = 0; i < interfaces->count; i++) {
2479 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2481 wpa_printf(MSG_INFO, "Cannot add interface - it "
2487 hapd_iface = hostapd_iface_alloc(interfaces);
2488 if (hapd_iface == NULL) {
2489 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2490 "for interface", __func__);
2493 new_iface = hapd_iface;
2495 if (conf_file && interfaces->config_read_cb) {
2496 conf = interfaces->config_read_cb(conf_file);
2497 if (conf && conf->bss)
2498 os_strlcpy(conf->bss[0]->iface, buf,
2499 sizeof(conf->bss[0]->iface));
2501 char *driver = os_strchr(ptr, ' ');
2505 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2508 if (conf == NULL || conf->bss == NULL) {
2509 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2510 "for configuration", __func__);
2514 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2515 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2516 "for hostapd", __func__);
2521 if (start_ctrl_iface(hapd_iface) < 0)
2524 wpa_printf(MSG_INFO, "Add interface '%s'",
2525 hapd_iface->conf->bss[0]->iface);
2531 hostapd_config_free(conf);
2533 if (hapd_iface->bss) {
2534 for (i = 0; i < hapd_iface->num_bss; i++) {
2535 hapd = hapd_iface->bss[i];
2538 if (hapd_iface->interfaces &&
2539 hapd_iface->interfaces->ctrl_iface_deinit)
2540 hapd_iface->interfaces->
2541 ctrl_iface_deinit(hapd);
2542 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2543 __func__, hapd_iface->bss[i],
2545 hostapd_cleanup(hapd);
2547 hapd_iface->bss[i] = NULL;
2549 os_free(hapd_iface->bss);
2550 hapd_iface->bss = NULL;
2553 interfaces->count--;
2554 interfaces->iface[interfaces->count] = NULL;
2556 hostapd_cleanup_iface(hapd_iface);
2562 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2566 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2568 /* Remove hostapd_data only if it has already been initialized */
2569 if (idx < iface->num_bss) {
2570 struct hostapd_data *hapd = iface->bss[idx];
2572 hostapd_bss_deinit(hapd);
2573 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2574 __func__, hapd, hapd->conf->iface);
2575 hostapd_config_free_bss(hapd->conf);
2581 for (i = idx; i < iface->num_bss; i++)
2582 iface->bss[i] = iface->bss[i + 1];
2584 hostapd_config_free_bss(iface->conf->bss[idx]);
2585 iface->conf->bss[idx] = NULL;
2588 iface->conf->num_bss--;
2589 for (i = idx; i < iface->conf->num_bss; i++)
2590 iface->conf->bss[i] = iface->conf->bss[i + 1];
2596 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2598 struct hostapd_iface *hapd_iface;
2601 for (i = 0; i < interfaces->count; i++) {
2602 hapd_iface = interfaces->iface[i];
2603 if (hapd_iface == NULL)
2605 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2606 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2607 hapd_iface->driver_ap_teardown =
2608 !!(hapd_iface->drv_flags &
2609 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2611 hostapd_interface_deinit_free(hapd_iface);
2613 while (k < (interfaces->count - 1)) {
2614 interfaces->iface[k] =
2615 interfaces->iface[k + 1];
2618 interfaces->count--;
2622 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2623 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2624 hapd_iface->driver_ap_teardown =
2625 !(hapd_iface->drv_flags &
2626 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2627 return hostapd_remove_bss(hapd_iface, j);
2636 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2637 * @hapd: Pointer to BSS data
2638 * @sta: Pointer to the associated STA data
2639 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2641 * This function will be called whenever a station associates with the AP. It
2642 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2643 * from drv_callbacks.c based on driver events for drivers that take care of
2644 * management frames (IEEE 802.11 authentication and association) internally.
2646 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2649 if (hapd->tkip_countermeasures) {
2650 hostapd_drv_sta_deauth(hapd, sta->addr,
2651 WLAN_REASON_MICHAEL_MIC_FAILURE);
2655 hostapd_prune_associations(hapd, sta->addr);
2656 ap_sta_clear_disconnect_timeouts(hapd, sta);
2658 /* IEEE 802.11F (IAPP) */
2659 if (hapd->conf->ieee802_11f)
2660 iapp_new_station(hapd->iapp, sta);
2663 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2664 sta->no_p2p_set = 1;
2665 hapd->num_sta_no_p2p++;
2666 if (hapd->num_sta_no_p2p == 1)
2667 hostapd_p2p_non_p2p_sta_connected(hapd);
2669 #endif /* CONFIG_P2P */
2671 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2672 * IEEE 802.1X/WPA code will start accounting after the station has
2673 * been authorized. */
2674 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2675 ap_sta_set_authorized(hapd, sta, 1);
2676 os_get_reltime(&sta->connected_time);
2677 accounting_sta_start(hapd, sta);
2680 /* Start IEEE 802.1X authentication process for new stations */
2681 ieee802_1x_new_station(hapd, sta);
2683 if (sta->auth_alg != WLAN_AUTH_FT &&
2684 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2685 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2687 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2689 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2690 wpa_printf(MSG_DEBUG,
2691 "%s: %s: reschedule ap_handle_timer timeout for "
2692 MACSTR " (%d seconds - ap_max_inactivity)",
2693 hapd->conf->iface, __func__, MAC2STR(sta->addr),
2694 hapd->conf->ap_max_inactivity);
2695 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2696 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2697 ap_handle_timer, hapd, sta);
2702 const char * hostapd_state_text(enum hostapd_iface_state s)
2705 case HAPD_IFACE_UNINITIALIZED:
2706 return "UNINITIALIZED";
2707 case HAPD_IFACE_DISABLED:
2709 case HAPD_IFACE_COUNTRY_UPDATE:
2710 return "COUNTRY_UPDATE";
2711 case HAPD_IFACE_ACS:
2713 case HAPD_IFACE_HT_SCAN:
2715 case HAPD_IFACE_DFS:
2717 case HAPD_IFACE_ENABLED:
2725 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2727 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2728 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2729 hostapd_state_text(s));
2734 int hostapd_csa_in_progress(struct hostapd_iface *iface)
2738 for (i = 0; i < iface->num_bss; i++)
2739 if (iface->bss[i]->csa_in_progress)
2747 static void free_beacon_data(struct beacon_data *beacon)
2749 os_free(beacon->head);
2750 beacon->head = NULL;
2751 os_free(beacon->tail);
2752 beacon->tail = NULL;
2753 os_free(beacon->probe_resp);
2754 beacon->probe_resp = NULL;
2755 os_free(beacon->beacon_ies);
2756 beacon->beacon_ies = NULL;
2757 os_free(beacon->proberesp_ies);
2758 beacon->proberesp_ies = NULL;
2759 os_free(beacon->assocresp_ies);
2760 beacon->assocresp_ies = NULL;
2764 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2765 struct beacon_data *beacon)
2767 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2768 struct wpa_driver_ap_params params;
2771 os_memset(beacon, 0, sizeof(*beacon));
2772 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2776 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2780 goto free_ap_params;
2783 beacon->head = os_malloc(params.head_len);
2785 goto free_ap_extra_ies;
2787 os_memcpy(beacon->head, params.head, params.head_len);
2788 beacon->head_len = params.head_len;
2790 beacon->tail = os_malloc(params.tail_len);
2794 os_memcpy(beacon->tail, params.tail, params.tail_len);
2795 beacon->tail_len = params.tail_len;
2797 if (params.proberesp != NULL) {
2798 beacon->probe_resp = os_malloc(params.proberesp_len);
2799 if (!beacon->probe_resp)
2802 os_memcpy(beacon->probe_resp, params.proberesp,
2803 params.proberesp_len);
2804 beacon->probe_resp_len = params.proberesp_len;
2807 /* copy the extra ies */
2809 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2810 if (!beacon->beacon_ies)
2813 os_memcpy(beacon->beacon_ies,
2814 beacon_extra->buf, wpabuf_len(beacon_extra));
2815 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2818 if (proberesp_extra) {
2819 beacon->proberesp_ies =
2820 os_malloc(wpabuf_len(proberesp_extra));
2821 if (!beacon->proberesp_ies)
2824 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2825 wpabuf_len(proberesp_extra));
2826 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2829 if (assocresp_extra) {
2830 beacon->assocresp_ies =
2831 os_malloc(wpabuf_len(assocresp_extra));
2832 if (!beacon->assocresp_ies)
2835 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2836 wpabuf_len(assocresp_extra));
2837 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2842 /* if the function fails, the caller should not free beacon data */
2844 free_beacon_data(beacon);
2847 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2850 ieee802_11_free_ap_params(¶ms);
2856 * TODO: This flow currently supports only changing channel and width within
2857 * the same hw_mode. Any other changes to MAC parameters or provided settings
2858 * are not supported.
2860 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2861 struct hostapd_config *conf,
2862 struct hostapd_freq_params *params,
2863 struct hostapd_freq_params *old_params)
2867 if (!params->channel) {
2868 /* check if the new channel is supported by hw */
2869 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2872 channel = params->channel;
2876 /* if a pointer to old_params is provided we save previous state */
2878 hostapd_set_freq_params(old_params, conf->hw_mode,
2879 hostapd_hw_get_freq(hapd, conf->channel),
2880 conf->channel, conf->ieee80211n,
2882 conf->secondary_channel,
2883 conf->vht_oper_chwidth,
2884 conf->vht_oper_centr_freq_seg0_idx,
2885 conf->vht_oper_centr_freq_seg1_idx,
2889 switch (params->bandwidth) {
2893 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
2896 if (params->center_freq2)
2897 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
2899 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
2902 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
2908 conf->channel = channel;
2909 conf->ieee80211n = params->ht_enabled;
2910 conf->secondary_channel = params->sec_channel_offset;
2911 ieee80211_freq_to_chan(params->center_freq1,
2912 &conf->vht_oper_centr_freq_seg0_idx);
2913 ieee80211_freq_to_chan(params->center_freq2,
2914 &conf->vht_oper_centr_freq_seg1_idx);
2916 /* TODO: maybe call here hostapd_config_check here? */
2922 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2923 struct csa_settings *settings)
2925 struct hostapd_iface *iface = hapd->iface;
2926 struct hostapd_freq_params old_freq;
2928 u8 chan, vht_bandwidth;
2930 os_memset(&old_freq, 0, sizeof(old_freq));
2931 if (!iface || !iface->freq || hapd->csa_in_progress)
2934 switch (settings->freq_params.bandwidth) {
2936 if (settings->freq_params.center_freq2)
2937 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
2939 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
2942 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
2945 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
2949 if (ieee80211_freq_to_channel_ext(
2950 settings->freq_params.freq,
2951 settings->freq_params.sec_channel_offset,
2953 &hapd->iface->cs_oper_class,
2954 &chan) == NUM_HOSTAPD_MODES) {
2955 wpa_printf(MSG_DEBUG,
2956 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
2957 settings->freq_params.freq,
2958 settings->freq_params.sec_channel_offset,
2959 settings->freq_params.vht_enabled);
2963 settings->freq_params.channel = chan;
2965 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2966 &settings->freq_params,
2971 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2973 /* change back the configuration */
2974 hostapd_change_config_freq(iface->bss[0], iface->conf,
2980 /* set channel switch parameters for csa ie */
2981 hapd->cs_freq_params = settings->freq_params;
2982 hapd->cs_count = settings->cs_count;
2983 hapd->cs_block_tx = settings->block_tx;
2985 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2987 free_beacon_data(&settings->beacon_after);
2991 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
2992 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
2993 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
2994 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
3000 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3002 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3004 hapd->cs_block_tx = 0;
3005 hapd->cs_c_off_beacon = 0;
3006 hapd->cs_c_off_proberesp = 0;
3007 hapd->csa_in_progress = 0;
3008 hapd->cs_c_off_ecsa_beacon = 0;
3009 hapd->cs_c_off_ecsa_proberesp = 0;
3013 int hostapd_switch_channel(struct hostapd_data *hapd,
3014 struct csa_settings *settings)
3018 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3019 wpa_printf(MSG_INFO, "CSA is not supported");
3023 ret = hostapd_fill_csa_settings(hapd, settings);
3027 ret = hostapd_drv_switch_channel(hapd, settings);
3028 free_beacon_data(&settings->beacon_csa);
3029 free_beacon_data(&settings->beacon_after);
3032 /* if we failed, clean cs parameters */
3033 hostapd_cleanup_cs_params(hapd);
3037 hapd->csa_in_progress = 1;
3043 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3044 const struct hostapd_freq_params *freq_params)
3046 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3049 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3051 if (freq_params->center_freq1)
3052 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3053 if (freq_params->center_freq2)
3054 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3056 switch (freq_params->bandwidth) {
3060 vht_bw = VHT_CHANWIDTH_USE_HT;
3063 if (freq_params->center_freq2)
3064 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3066 vht_bw = VHT_CHANWIDTH_80MHZ;
3069 vht_bw = VHT_CHANWIDTH_160MHZ;
3072 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3073 freq_params->bandwidth);
3077 iface->freq = freq_params->freq;
3078 iface->conf->channel = freq_params->channel;
3079 iface->conf->secondary_channel = freq_params->sec_channel_offset;
3080 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3081 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3082 iface->conf->vht_oper_chwidth = vht_bw;
3083 iface->conf->ieee80211n = freq_params->ht_enabled;
3084 iface->conf->ieee80211ac = freq_params->vht_enabled;
3087 * cs_params must not be cleared earlier because the freq_params
3088 * argument may actually point to one of these.
3090 for (i = 0; i < iface->num_bss; i++)
3091 hostapd_cleanup_cs_params(iface->bss[i]);
3093 hostapd_disable_iface(iface);
3094 hostapd_enable_iface(iface);
3097 #endif /* NEED_AP_MLME */
3100 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3105 for (i = 0; i < interfaces->count; i++) {
3106 struct hostapd_iface *iface = interfaces->iface[i];
3108 for (j = 0; j < iface->num_bss; j++) {
3109 struct hostapd_data *hapd = iface->bss[j];
3111 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3120 void hostapd_periodic_iface(struct hostapd_iface *iface)
3124 ap_list_timer(iface);
3126 for (i = 0; i < iface->num_bss; i++) {
3127 struct hostapd_data *hapd = iface->bss[i];
3132 #ifndef CONFIG_NO_RADIUS
3133 hostapd_acl_expire(hapd);
3134 #endif /* CONFIG_NO_RADIUS */