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 "radius/radius_client.h"
16 #include "radius/radius_das.h"
17 #include "eap_server/tncs.h"
18 #include "eapol_auth/eapol_auth_sm.h"
19 #include "eapol_auth/eapol_auth_sm_i.h"
24 #include "accounting.h"
28 #include "ieee802_1x.h"
29 #include "ieee802_11_auth.h"
30 #include "vlan_init.h"
32 #include "wps_hostapd.h"
33 #include "hw_features.h"
34 #include "wpa_auth_glue.h"
35 #include "ap_drv_ops.h"
36 #include "ap_config.h"
37 #include "p2p_hostapd.h"
40 #include "ieee802_11.h"
43 #include "dhcp_snoop.h"
44 #include "ndisc_snoop.h"
47 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
48 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
49 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
50 static int setup_interface2(struct hostapd_iface *iface);
51 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
54 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
55 int (*cb)(struct hostapd_iface *iface,
56 void *ctx), void *ctx)
61 for (i = 0; i < interfaces->count; i++) {
62 ret = cb(interfaces->iface[i], ctx);
71 static void hostapd_reload_bss(struct hostapd_data *hapd)
73 struct hostapd_ssid *ssid;
75 #ifndef CONFIG_NO_RADIUS
76 radius_client_reconfig(hapd->radius, hapd->conf->radius);
77 #endif /* CONFIG_NO_RADIUS */
79 ssid = &hapd->conf->ssid;
80 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
81 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
83 * Force PSK to be derived again since SSID or passphrase may
86 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
88 if (hostapd_setup_wpa_psk(hapd->conf)) {
89 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
90 "after reloading configuration");
93 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
94 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
96 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
98 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
99 hostapd_setup_wpa(hapd);
101 wpa_init_keys(hapd->wpa_auth);
102 } else if (hapd->conf->wpa) {
105 hostapd_reconfig_wpa(hapd);
106 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
107 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
108 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
109 "the kernel driver.");
110 } else if (hapd->wpa_auth) {
111 wpa_deinit(hapd->wpa_auth);
112 hapd->wpa_auth = NULL;
113 hostapd_set_privacy(hapd, 0);
114 hostapd_setup_encryption(hapd->conf->iface, hapd);
115 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
118 ieee802_11_set_beacon(hapd);
119 hostapd_update_wps(hapd);
121 if (hapd->conf->ssid.ssid_set &&
122 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
123 hapd->conf->ssid.ssid_len)) {
124 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
125 /* try to continue */
127 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
131 static void hostapd_clear_old(struct hostapd_iface *iface)
136 * Deauthenticate all stations since the new configuration may not
137 * allow them to use the BSS anymore.
139 for (j = 0; j < iface->num_bss; j++) {
140 hostapd_flush_old_stations(iface->bss[j],
141 WLAN_REASON_PREV_AUTH_NOT_VALID);
142 hostapd_broadcast_wep_clear(iface->bss[j]);
144 #ifndef CONFIG_NO_RADIUS
145 /* TODO: update dynamic data based on changed configuration
146 * items (e.g., open/close sockets, etc.) */
147 radius_client_flush(iface->bss[j]->radius, 0);
148 #endif /* CONFIG_NO_RADIUS */
153 int hostapd_reload_config(struct hostapd_iface *iface)
155 struct hostapd_data *hapd = iface->bss[0];
156 struct hostapd_config *newconf, *oldconf;
159 if (iface->config_fname == NULL) {
160 /* Only in-memory config in use - assume it has been updated */
161 hostapd_clear_old(iface);
162 for (j = 0; j < iface->num_bss; j++)
163 hostapd_reload_bss(iface->bss[j]);
167 if (iface->interfaces == NULL ||
168 iface->interfaces->config_read_cb == NULL)
170 newconf = iface->interfaces->config_read_cb(iface->config_fname);
174 hostapd_clear_old(iface);
176 oldconf = hapd->iconf;
177 iface->conf = newconf;
179 for (j = 0; j < iface->num_bss; j++) {
180 hapd = iface->bss[j];
181 hapd->iconf = newconf;
182 hapd->iconf->channel = oldconf->channel;
183 hapd->iconf->acs = oldconf->acs;
184 hapd->iconf->secondary_channel = oldconf->secondary_channel;
185 hapd->iconf->ieee80211n = oldconf->ieee80211n;
186 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
187 hapd->iconf->ht_capab = oldconf->ht_capab;
188 hapd->iconf->vht_capab = oldconf->vht_capab;
189 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
190 hapd->iconf->vht_oper_centr_freq_seg0_idx =
191 oldconf->vht_oper_centr_freq_seg0_idx;
192 hapd->iconf->vht_oper_centr_freq_seg1_idx =
193 oldconf->vht_oper_centr_freq_seg1_idx;
194 hapd->conf = newconf->bss[j];
195 hostapd_reload_bss(hapd);
198 hostapd_config_free(oldconf);
205 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
210 for (i = 0; i < NUM_WEP_KEYS; i++) {
211 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
212 0, NULL, 0, NULL, 0)) {
213 wpa_printf(MSG_DEBUG, "Failed to clear default "
214 "encryption keys (ifname=%s keyidx=%d)",
218 #ifdef CONFIG_IEEE80211W
219 if (hapd->conf->ieee80211w) {
220 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
221 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
224 wpa_printf(MSG_DEBUG, "Failed to clear "
225 "default mgmt encryption keys "
226 "(ifname=%s keyidx=%d)", ifname, i);
230 #endif /* CONFIG_IEEE80211W */
234 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
236 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
241 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
244 struct hostapd_ssid *ssid = &hapd->conf->ssid;
247 if (ssid->wep.default_len &&
248 hostapd_drv_set_key(hapd->conf->iface,
249 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
250 1, NULL, 0, ssid->wep.key[idx],
251 ssid->wep.len[idx])) {
252 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
260 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
262 os_free(hapd->probereq_cb);
263 hapd->probereq_cb = NULL;
266 wpabuf_free(hapd->p2p_beacon_ie);
267 hapd->p2p_beacon_ie = NULL;
268 wpabuf_free(hapd->p2p_probe_resp_ie);
269 hapd->p2p_probe_resp_ie = NULL;
270 #endif /* CONFIG_P2P */
272 if (!hapd->started) {
273 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
274 __func__, hapd->conf->iface);
279 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
280 iapp_deinit(hapd->iapp);
282 accounting_deinit(hapd);
283 hostapd_deinit_wpa(hapd);
285 hostapd_acl_deinit(hapd);
286 #ifndef CONFIG_NO_RADIUS
287 radius_client_deinit(hapd->radius);
289 radius_das_deinit(hapd->radius_das);
290 hapd->radius_das = NULL;
291 #endif /* CONFIG_NO_RADIUS */
293 hostapd_deinit_wps(hapd);
295 authsrv_deinit(hapd);
297 if (hapd->interface_added) {
298 hapd->interface_added = 0;
299 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
300 wpa_printf(MSG_WARNING,
301 "Failed to remove BSS interface %s",
303 hapd->interface_added = 1;
306 * Since this was a dynamically added interface, the
307 * driver wrapper may have removed its internal instance
308 * and hapd->drv_priv is not valid anymore.
310 hapd->drv_priv = NULL;
314 wpabuf_free(hapd->time_adv);
316 #ifdef CONFIG_INTERWORKING
317 gas_serv_deinit(hapd);
318 #endif /* CONFIG_INTERWORKING */
320 bss_load_update_deinit(hapd);
321 ndisc_snoop_deinit(hapd);
322 dhcp_snoop_deinit(hapd);
323 x_snoop_deinit(hapd);
326 bin_clear_free(hapd->tmp_eap_user.identity,
327 hapd->tmp_eap_user.identity_len);
328 bin_clear_free(hapd->tmp_eap_user.password,
329 hapd->tmp_eap_user.password_len);
330 #endif /* CONFIG_SQLITE */
333 wpabuf_free(hapd->mesh_pending_auth);
334 hapd->mesh_pending_auth = NULL;
335 #endif /* CONFIG_MESH */
340 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
341 * @hapd: Pointer to BSS data
343 * This function is used to free all per-BSS data structures and resources.
344 * Most of the modules that are initialized in hostapd_setup_bss() are
345 * deinitialized here.
347 static void hostapd_cleanup(struct hostapd_data *hapd)
349 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
351 if (hapd->iface->interfaces &&
352 hapd->iface->interfaces->ctrl_iface_deinit)
353 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
354 hostapd_free_hapd_data(hapd);
358 static void sta_track_deinit(struct hostapd_iface *iface)
360 struct hostapd_sta_info *info;
362 if (!iface->num_sta_seen)
365 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
367 dl_list_del(&info->list);
368 iface->num_sta_seen--;
374 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
376 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
377 #ifdef CONFIG_IEEE80211N
379 hostapd_stop_setup_timers(iface);
380 #endif /* NEED_AP_MLME */
381 #endif /* CONFIG_IEEE80211N */
382 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
383 iface->hw_features = NULL;
384 os_free(iface->current_rates);
385 iface->current_rates = NULL;
386 os_free(iface->basic_rates);
387 iface->basic_rates = NULL;
388 ap_list_deinit(iface);
389 sta_track_deinit(iface);
394 * hostapd_cleanup_iface - Complete per-interface cleanup
395 * @iface: Pointer to interface data
397 * This function is called after per-BSS data structures are deinitialized
398 * with hostapd_cleanup().
400 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
402 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
403 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
405 hostapd_cleanup_iface_partial(iface);
406 hostapd_config_free(iface->conf);
409 os_free(iface->config_fname);
411 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
416 static void hostapd_clear_wep(struct hostapd_data *hapd)
418 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
419 hostapd_set_privacy(hapd, 0);
420 hostapd_broadcast_wep_clear(hapd);
425 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
429 hostapd_broadcast_wep_set(hapd);
431 if (hapd->conf->ssid.wep.default_len) {
432 hostapd_set_privacy(hapd, 1);
437 * When IEEE 802.1X is not enabled, the driver may need to know how to
438 * set authentication algorithms for static WEP.
440 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
442 for (i = 0; i < 4; i++) {
443 if (hapd->conf->ssid.wep.key[i] &&
444 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
445 i == hapd->conf->ssid.wep.idx, NULL, 0,
446 hapd->conf->ssid.wep.key[i],
447 hapd->conf->ssid.wep.len[i])) {
448 wpa_printf(MSG_WARNING, "Could not set WEP "
452 if (hapd->conf->ssid.wep.key[i] &&
453 i == hapd->conf->ssid.wep.idx)
454 hostapd_set_privacy(hapd, 1);
461 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
466 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
469 if (!hapd->iface->driver_ap_teardown) {
470 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
471 "Flushing old station entries");
473 if (hostapd_flush(hapd)) {
474 wpa_msg(hapd->msg_ctx, MSG_WARNING,
475 "Could not connect to kernel driver");
479 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
480 os_memset(addr, 0xff, ETH_ALEN);
481 hostapd_drv_sta_deauth(hapd, addr, reason);
482 hostapd_free_stas(hapd);
488 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
490 hostapd_free_stas(hapd);
491 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
492 hostapd_clear_wep(hapd);
497 * hostapd_validate_bssid_configuration - Validate BSSID configuration
498 * @iface: Pointer to interface data
499 * Returns: 0 on success, -1 on failure
501 * This function is used to validate that the configured BSSIDs are valid.
503 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
505 u8 mask[ETH_ALEN] = { 0 };
506 struct hostapd_data *hapd = iface->bss[0];
507 unsigned int i = iface->conf->num_bss, bits = 0, j;
510 if (hostapd_drv_none(hapd))
513 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
515 /* Determine the bits necessary to cover the number of BSSIDs. */
516 for (i--; i; i >>= 1)
519 /* Determine the bits necessary to any configured BSSIDs,
520 if they are higher than the number of BSSIDs. */
521 for (j = 0; j < iface->conf->num_bss; j++) {
522 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
528 for (i = 0; i < ETH_ALEN; i++) {
530 iface->conf->bss[j]->bssid[i] ^
538 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
544 while (mask[i] != 0) {
554 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
559 os_memset(mask, 0xff, ETH_ALEN);
561 for (i = 5; i > 5 - j; i--)
568 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
569 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
574 for (i = 0; i < ETH_ALEN; i++) {
575 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
576 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
577 " for start address " MACSTR ".",
578 MAC2STR(mask), MAC2STR(hapd->own_addr));
579 wpa_printf(MSG_ERROR, "Start address must be the "
580 "first address in the block (i.e., addr "
581 "AND mask == addr).");
590 static int mac_in_conf(struct hostapd_config *conf, const void *a)
594 for (i = 0; i < conf->num_bss; i++) {
595 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
604 #ifndef CONFIG_NO_RADIUS
606 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
607 struct radius_das_attrs *attr)
609 if (attr->nas_identifier &&
610 (!hapd->conf->nas_identifier ||
611 os_strlen(hapd->conf->nas_identifier) !=
612 attr->nas_identifier_len ||
613 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
614 attr->nas_identifier_len) != 0)) {
615 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
619 if (attr->nas_ip_addr &&
620 (hapd->conf->own_ip_addr.af != AF_INET ||
621 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
623 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
628 if (attr->nas_ipv6_addr &&
629 (hapd->conf->own_ip_addr.af != AF_INET6 ||
630 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
632 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
635 #endif /* CONFIG_IPV6 */
641 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
642 struct radius_das_attrs *attr,
645 struct sta_info *selected, *sta;
652 for (sta = hapd->sta_list; sta; sta = sta->next)
653 sta->radius_das_match = 1;
655 if (attr->sta_addr) {
657 sta = ap_get_sta(hapd, attr->sta_addr);
659 wpa_printf(MSG_DEBUG,
660 "RADIUS DAS: No Calling-Station-Id match");
665 for (sta = hapd->sta_list; sta; sta = sta->next) {
667 sta->radius_das_match = 0;
669 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
672 if (attr->acct_session_id) {
674 if (attr->acct_session_id_len != 17) {
675 wpa_printf(MSG_DEBUG,
676 "RADIUS DAS: Acct-Session-Id cannot match");
681 for (sta = hapd->sta_list; sta; sta = sta->next) {
682 if (!sta->radius_das_match)
684 os_snprintf(buf, sizeof(buf), "%08X-%08X",
685 sta->acct_session_id_hi,
686 sta->acct_session_id_lo);
687 if (os_memcmp(attr->acct_session_id, buf, 17) != 0)
688 sta->radius_das_match = 0;
694 wpa_printf(MSG_DEBUG,
695 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
698 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
701 if (attr->acct_multi_session_id) {
703 if (attr->acct_multi_session_id_len != 17) {
704 wpa_printf(MSG_DEBUG,
705 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
710 for (sta = hapd->sta_list; sta; sta = sta->next) {
711 if (!sta->radius_das_match)
713 if (!sta->eapol_sm ||
714 !sta->eapol_sm->acct_multi_session_id_hi) {
715 sta->radius_das_match = 0;
718 os_snprintf(buf, sizeof(buf), "%08X+%08X",
719 sta->eapol_sm->acct_multi_session_id_hi,
720 sta->eapol_sm->acct_multi_session_id_lo);
721 if (os_memcmp(attr->acct_multi_session_id, buf, 17) !=
723 sta->radius_das_match = 0;
729 wpa_printf(MSG_DEBUG,
730 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
733 wpa_printf(MSG_DEBUG,
734 "RADIUS DAS: Acct-Multi-Session-Id match");
741 for (sta = hapd->sta_list; sta; sta = sta->next) {
744 if (!sta->radius_das_match)
746 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
747 if (!cui || wpabuf_len(cui) != attr->cui_len ||
748 os_memcmp(wpabuf_head(cui), attr->cui,
750 sta->radius_das_match = 0;
756 wpa_printf(MSG_DEBUG,
757 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
760 wpa_printf(MSG_DEBUG,
761 "RADIUS DAS: Chargeable-User-Identity match");
764 if (attr->user_name) {
768 for (sta = hapd->sta_list; sta; sta = sta->next) {
772 if (!sta->radius_das_match)
774 identity = ieee802_1x_get_identity(sta->eapol_sm,
777 identity_len != attr->user_name_len ||
778 os_memcmp(identity, attr->user_name, identity_len)
780 sta->radius_das_match = 0;
786 wpa_printf(MSG_DEBUG,
787 "RADIUS DAS: No matches remaining after User-Name check");
790 wpa_printf(MSG_DEBUG,
791 "RADIUS DAS: User-Name match");
796 * In theory, we could match all current associations, but it
797 * seems safer to just reject requests that do not include any
798 * session identification attributes.
800 wpa_printf(MSG_DEBUG,
801 "RADIUS DAS: No session identification attributes included");
806 for (sta = hapd->sta_list; sta; sta = sta->next) {
807 if (sta->radius_das_match) {
820 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
821 struct radius_das_attrs *attr)
825 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
829 static enum radius_das_res
830 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
832 struct hostapd_data *hapd = ctx;
833 struct sta_info *sta;
836 if (hostapd_das_nas_mismatch(hapd, attr))
837 return RADIUS_DAS_NAS_MISMATCH;
839 sta = hostapd_das_find_sta(hapd, attr, &multi);
842 wpa_printf(MSG_DEBUG,
843 "RADIUS DAS: Multiple sessions match - not supported");
844 return RADIUS_DAS_MULTI_SESSION_MATCH;
846 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
847 wpa_printf(MSG_DEBUG,
848 "RADIUS DAS: PMKSA cache entry matched");
849 return RADIUS_DAS_SUCCESS;
851 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
852 return RADIUS_DAS_SESSION_NOT_FOUND;
855 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
856 " - disconnecting", MAC2STR(sta->addr));
857 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
859 hostapd_drv_sta_deauth(hapd, sta->addr,
860 WLAN_REASON_PREV_AUTH_NOT_VALID);
861 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
863 return RADIUS_DAS_SUCCESS;
866 #endif /* CONFIG_NO_RADIUS */
870 * hostapd_setup_bss - Per-BSS setup (initialization)
871 * @hapd: Pointer to BSS data
872 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
873 * but interface may exist
875 * This function is used to initialize all per-BSS data structures and
876 * resources. This gets called in a loop for each BSS when an interface is
877 * initialized. Most of the modules that are initialized here will be
878 * deinitialized in hostapd_cleanup().
880 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
882 struct hostapd_bss_config *conf = hapd->conf;
883 u8 ssid[SSID_MAX_LEN + 1];
884 int ssid_len, set_ssid;
885 char force_ifname[IFNAMSIZ];
886 u8 if_addr[ETH_ALEN];
887 int flush_old_stations = 1;
889 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
890 __func__, hapd, conf->iface, first);
892 #ifdef EAP_SERVER_TNC
893 if (conf->tnc && tncs_global_init() < 0) {
894 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
897 #endif /* EAP_SERVER_TNC */
900 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
901 __func__, conf->iface);
906 if (!first || first == -1) {
907 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
908 /* Allocate the next available BSSID. */
910 inc_byte_array(hapd->own_addr, ETH_ALEN);
911 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
913 /* Allocate the configured BSSID. */
914 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
916 if (hostapd_mac_comp(hapd->own_addr,
917 hapd->iface->bss[0]->own_addr) ==
919 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
920 "BSSID set to the MAC address of "
921 "the radio", conf->iface);
926 hapd->interface_added = 1;
927 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
928 conf->iface, hapd->own_addr, hapd,
929 &hapd->drv_priv, force_ifname, if_addr,
930 conf->bridge[0] ? conf->bridge : NULL,
932 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
933 MACSTR ")", MAC2STR(hapd->own_addr));
934 hapd->interface_added = 0;
939 if (conf->wmm_enabled < 0)
940 conf->wmm_enabled = hapd->iconf->ieee80211n;
943 if (hapd->iface->mconf == NULL)
944 flush_old_stations = 0;
945 #endif /* CONFIG_MESH */
947 if (flush_old_stations)
948 hostapd_flush_old_stations(hapd,
949 WLAN_REASON_PREV_AUTH_NOT_VALID);
950 hostapd_set_privacy(hapd, 0);
952 hostapd_broadcast_wep_clear(hapd);
953 if (hostapd_setup_encryption(conf->iface, hapd))
957 * Fetch the SSID from the system and use it or,
958 * if one was specified in the config file, verify they
961 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
963 wpa_printf(MSG_ERROR, "Could not read SSID from system");
966 if (conf->ssid.ssid_set) {
968 * If SSID is specified in the config file and it differs
969 * from what is being used then force installation of the
972 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
973 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
976 * No SSID in the config file; just use the one we got
980 conf->ssid.ssid_len = ssid_len;
981 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
984 if (!hostapd_drv_none(hapd)) {
985 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
987 conf->iface, MAC2STR(hapd->own_addr),
988 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
991 if (hostapd_setup_wpa_psk(conf)) {
992 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
996 /* Set SSID for the kernel driver (to be used in beacon and probe
997 * response frames) */
998 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
999 conf->ssid.ssid_len)) {
1000 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1004 if (wpa_debug_level <= MSG_MSGDUMP)
1005 conf->radius->msg_dumps = 1;
1006 #ifndef CONFIG_NO_RADIUS
1007 hapd->radius = radius_client_init(hapd, conf->radius);
1008 if (hapd->radius == NULL) {
1009 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1013 if (conf->radius_das_port) {
1014 struct radius_das_conf das_conf;
1015 os_memset(&das_conf, 0, sizeof(das_conf));
1016 das_conf.port = conf->radius_das_port;
1017 das_conf.shared_secret = conf->radius_das_shared_secret;
1018 das_conf.shared_secret_len =
1019 conf->radius_das_shared_secret_len;
1020 das_conf.client_addr = &conf->radius_das_client_addr;
1021 das_conf.time_window = conf->radius_das_time_window;
1022 das_conf.require_event_timestamp =
1023 conf->radius_das_require_event_timestamp;
1024 das_conf.ctx = hapd;
1025 das_conf.disconnect = hostapd_das_disconnect;
1026 hapd->radius_das = radius_das_init(&das_conf);
1027 if (hapd->radius_das == NULL) {
1028 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1033 #endif /* CONFIG_NO_RADIUS */
1035 if (hostapd_acl_init(hapd)) {
1036 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1039 if (hostapd_init_wps(hapd, conf))
1042 if (authsrv_init(hapd) < 0)
1045 if (ieee802_1x_init(hapd)) {
1046 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1050 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1053 if (accounting_init(hapd)) {
1054 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1058 if (conf->ieee802_11f &&
1059 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1060 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1065 #ifdef CONFIG_INTERWORKING
1066 if (gas_serv_init(hapd)) {
1067 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1071 if (conf->qos_map_set_len &&
1072 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1073 conf->qos_map_set_len)) {
1074 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1077 #endif /* CONFIG_INTERWORKING */
1079 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1080 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1084 if (conf->proxy_arp) {
1085 if (x_snoop_init(hapd)) {
1086 wpa_printf(MSG_ERROR,
1087 "Generic snooping infrastructure initialization failed");
1091 if (dhcp_snoop_init(hapd)) {
1092 wpa_printf(MSG_ERROR,
1093 "DHCP snooping initialization failed");
1097 if (ndisc_snoop_init(hapd)) {
1098 wpa_printf(MSG_ERROR,
1099 "Neighbor Discovery snooping initialization failed");
1104 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1105 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1109 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1112 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1115 if (hapd->driver && hapd->driver->set_operstate)
1116 hapd->driver->set_operstate(hapd->drv_priv, 1);
1122 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1124 struct hostapd_data *hapd = iface->bss[0];
1126 struct hostapd_tx_queue_params *p;
1129 if (iface->mconf == NULL)
1131 #endif /* CONFIG_MESH */
1133 for (i = 0; i < NUM_TX_QUEUES; i++) {
1134 p = &iface->conf->tx_queue[i];
1136 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1137 p->cwmax, p->burst)) {
1138 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1139 "parameters for queue %d.", i);
1140 /* Continue anyway */
1146 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1147 struct mac_acl_entry *mac_acl,
1148 int n_entries, u8 accept_acl)
1150 struct hostapd_acl_params *acl_params;
1153 acl_params = os_zalloc(sizeof(*acl_params) +
1154 (n_entries * sizeof(acl_params->mac_acl[0])));
1158 for (i = 0; i < n_entries; i++)
1159 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1162 acl_params->acl_policy = accept_acl;
1163 acl_params->num_mac_acl = n_entries;
1165 err = hostapd_drv_set_acl(hapd, acl_params);
1167 os_free(acl_params);
1173 static void hostapd_set_acl(struct hostapd_data *hapd)
1175 struct hostapd_config *conf = hapd->iconf;
1179 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1182 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1184 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1185 conf->bss[0]->num_accept_mac,
1188 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1191 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1193 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1194 conf->bss[0]->num_deny_mac,
1197 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1204 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1206 if (!hapd->iface->interfaces ||
1207 !hapd->iface->interfaces->ctrl_iface_init)
1210 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1211 wpa_printf(MSG_ERROR,
1212 "Failed to setup control interface for %s",
1221 static int start_ctrl_iface(struct hostapd_iface *iface)
1225 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1228 for (i = 0; i < iface->num_bss; i++) {
1229 struct hostapd_data *hapd = iface->bss[i];
1230 if (iface->interfaces->ctrl_iface_init(hapd)) {
1231 wpa_printf(MSG_ERROR,
1232 "Failed to setup control interface for %s",
1242 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1244 struct hostapd_iface *iface = eloop_ctx;
1246 if (!iface->wait_channel_update) {
1247 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1252 * It is possible that the existing channel list is acceptable, so try
1255 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1256 setup_interface2(iface);
1260 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1262 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1265 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1266 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1267 setup_interface2(iface);
1271 static int setup_interface(struct hostapd_iface *iface)
1273 struct hostapd_data *hapd = iface->bss[0];
1277 * It is possible that setup_interface() is called after the interface
1278 * was disabled etc., in which case driver_ap_teardown is possibly set
1279 * to 1. Clear it here so any other key/station deletion, which is not
1280 * part of a teardown flow, would also call the relevant driver
1283 iface->driver_ap_teardown = 0;
1285 if (!iface->phy[0]) {
1286 const char *phy = hostapd_drv_get_radio_name(hapd);
1288 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1289 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1294 * Make sure that all BSSes get configured with a pointer to the same
1297 for (i = 1; i < iface->num_bss; i++) {
1298 iface->bss[i]->driver = hapd->driver;
1299 iface->bss[i]->drv_priv = hapd->drv_priv;
1302 if (hostapd_validate_bssid_configuration(iface))
1306 * Initialize control interfaces early to allow external monitoring of
1307 * channel setup operations that may take considerable amount of time
1308 * especially for DFS cases.
1310 if (start_ctrl_iface(iface))
1313 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1314 char country[4], previous_country[4];
1316 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1317 if (hostapd_get_country(hapd, previous_country) < 0)
1318 previous_country[0] = '\0';
1320 os_memcpy(country, hapd->iconf->country, 3);
1322 if (hostapd_set_country(hapd, country) < 0) {
1323 wpa_printf(MSG_ERROR, "Failed to set country code");
1327 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1328 previous_country, country);
1330 if (os_strncmp(previous_country, country, 2) != 0) {
1331 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1332 iface->wait_channel_update = 1;
1333 eloop_register_timeout(5, 0,
1334 channel_list_update_timeout,
1340 return setup_interface2(iface);
1344 static int setup_interface2(struct hostapd_iface *iface)
1346 iface->wait_channel_update = 0;
1348 if (hostapd_get_hw_features(iface)) {
1349 /* Not all drivers support this yet, so continue without hw
1352 int ret = hostapd_select_hw_mode(iface);
1354 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1355 "channel. (%d)", ret);
1359 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1362 ret = hostapd_check_ht_capab(iface);
1366 wpa_printf(MSG_DEBUG, "Interface initialization will "
1367 "be completed in a callback");
1371 if (iface->conf->ieee80211h)
1372 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1374 return hostapd_setup_interface_complete(iface, 0);
1377 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1378 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1379 if (iface->interfaces && iface->interfaces->terminate_on_error)
1387 static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1389 struct hostapd_data *hapd = ctx;
1391 return hapd->own_addr;
1395 static void fst_hostapd_get_channel_info_cb(void *ctx,
1396 enum hostapd_hw_mode *hw_mode,
1399 struct hostapd_data *hapd = ctx;
1401 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1405 static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1407 struct hostapd_data *hapd = ctx;
1409 if (hapd->iface->fst_ies != fst_ies) {
1410 hapd->iface->fst_ies = fst_ies;
1411 if (ieee802_11_set_beacon(hapd))
1412 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1417 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1420 struct hostapd_data *hapd = ctx;
1422 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1423 wpabuf_head(buf), wpabuf_len(buf));
1427 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1429 struct hostapd_data *hapd = ctx;
1430 struct sta_info *sta = ap_get_sta(hapd, addr);
1432 return sta ? sta->mb_ies : NULL;
1436 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1437 const u8 *buf, size_t size)
1439 struct hostapd_data *hapd = ctx;
1440 struct sta_info *sta = ap_get_sta(hapd, addr);
1443 struct mb_ies_info info;
1445 if (!mb_ies_info_by_ies(&info, buf, size)) {
1446 wpabuf_free(sta->mb_ies);
1447 sta->mb_ies = mb_ies_by_info(&info);
1453 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1456 struct sta_info *s = (struct sta_info *) *get_ctx;
1459 for (; s && !s->mb_ies; s = s->next)
1464 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1474 static const u8 * fst_hostapd_get_peer_first(void *ctx,
1475 struct fst_get_peer_ctx **get_ctx,
1478 struct hostapd_data *hapd = ctx;
1480 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1482 return fst_hostapd_get_sta(get_ctx, mb_only);
1486 static const u8 * fst_hostapd_get_peer_next(void *ctx,
1487 struct fst_get_peer_ctx **get_ctx,
1490 return fst_hostapd_get_sta(get_ctx, mb_only);
1494 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1495 struct fst_wpa_obj *iface_obj)
1497 iface_obj->ctx = hapd;
1498 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1499 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1500 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1501 iface_obj->send_action = fst_hostapd_send_action_cb;
1502 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1503 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1504 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1505 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1508 #endif /* CONFIG_FST */
1512 * hostapd_setup_interface_complete - Complete interface setup
1514 * This function is called when previous steps in the interface setup has been
1515 * completed. This can also start operations, e.g., DFS, that will require
1516 * additional processing before interface is ready to be enabled. Such
1517 * operations will call this function from eloop callbacks when finished.
1519 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1521 struct hostapd_data *hapd = iface->bss[0];
1524 int delay_apply_cfg = 0;
1525 int res_dfs_offload = 0;
1530 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1531 if (iface->conf->channel) {
1534 #endif /* NEED_AP_MLME */
1536 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1537 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1538 "Frequency: %d MHz",
1539 hostapd_hw_mode_txt(iface->conf->hw_mode),
1540 iface->conf->channel, iface->freq);
1543 /* Handle DFS only if it is not offloaded to the driver */
1544 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1546 res = hostapd_handle_dfs(iface);
1553 /* If DFS is offloaded to the driver */
1554 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1555 if (res_dfs_offload <= 0) {
1556 if (res_dfs_offload < 0)
1559 wpa_printf(MSG_DEBUG,
1560 "Proceed with AP/channel setup");
1562 * If this is a DFS channel, move to completing
1565 if (res_dfs_offload == 1)
1567 /* Otherwise fall through. */
1570 #endif /* NEED_AP_MLME */
1573 if (iface->mconf != NULL) {
1574 wpa_printf(MSG_DEBUG,
1575 "%s: Mesh configuration will be applied while joining the mesh network",
1576 iface->bss[0]->conf->iface);
1577 delay_apply_cfg = 1;
1579 #endif /* CONFIG_MESH */
1581 if (!delay_apply_cfg &&
1582 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1583 hapd->iconf->channel,
1584 hapd->iconf->ieee80211n,
1585 hapd->iconf->ieee80211ac,
1586 hapd->iconf->secondary_channel,
1587 hapd->iconf->vht_oper_chwidth,
1588 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1589 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1590 wpa_printf(MSG_ERROR, "Could not set channel for "
1596 if (iface->current_mode) {
1597 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1598 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1600 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1601 HOSTAPD_LEVEL_WARNING,
1602 "Failed to prepare rates table.");
1607 if (hapd->iconf->rts_threshold > -1 &&
1608 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1609 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1614 if (hapd->iconf->fragm_threshold > -1 &&
1615 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1616 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1617 "for kernel driver");
1621 prev_addr = hapd->own_addr;
1623 for (j = 0; j < iface->num_bss; j++) {
1624 hapd = iface->bss[j];
1626 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1627 if (hostapd_setup_bss(hapd, j == 0)) {
1629 hapd = iface->bss[j];
1630 hostapd_bss_deinit_no_free(hapd);
1631 hostapd_free_hapd_data(hapd);
1635 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1636 prev_addr = hapd->own_addr;
1638 hapd = iface->bss[0];
1640 hostapd_tx_queue_params(iface);
1642 ap_list_init(iface);
1643 dl_list_init(&iface->sta_seen);
1645 hostapd_set_acl(hapd);
1647 if (hostapd_driver_commit(hapd) < 0) {
1648 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1649 "configuration", __func__);
1654 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1655 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1656 * mode), the interface is up only after driver_commit, so initialize
1657 * WPS after driver_commit.
1659 for (j = 0; j < iface->num_bss; j++) {
1660 if (hostapd_init_wps_complete(iface->bss[j]))
1664 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1667 * If freq is DFS, and DFS is offloaded to the driver, then wait
1668 * for CAC to complete.
1670 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1671 return res_dfs_offload;
1676 #endif /* NEED_AP_MLME */
1679 if (hapd->iconf->fst_cfg.group_id[0]) {
1680 struct fst_wpa_obj iface_obj;
1682 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1683 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1684 &iface_obj, &hapd->iconf->fst_cfg);
1686 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1687 hapd->iconf->fst_cfg.group_id);
1691 #endif /* CONFIG_FST */
1693 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1694 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1695 if (hapd->setup_complete_cb)
1696 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1698 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1699 iface->bss[0]->conf->iface);
1700 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1701 iface->interfaces->terminate_on_error--;
1706 wpa_printf(MSG_ERROR, "Interface initialization failed");
1707 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1708 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1711 fst_detach(iface->fst);
1714 #endif /* CONFIG_FST */
1715 if (iface->interfaces && iface->interfaces->terminate_on_error)
1722 * hostapd_setup_interface - Setup of an interface
1723 * @iface: Pointer to interface data.
1724 * Returns: 0 on success, -1 on failure
1726 * Initializes the driver interface, validates the configuration,
1727 * and sets driver parameters based on the configuration.
1728 * Flushes old stations, sets the channel, encryption,
1729 * beacons, and WDS links based on the configuration.
1731 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1732 * or DFS operations, this function returns 0 before such operations have been
1733 * completed. The pending operations are registered into eloop and will be
1734 * completed from eloop callbacks. Those callbacks end up calling
1735 * hostapd_setup_interface_complete() once setup has been completed.
1737 int hostapd_setup_interface(struct hostapd_iface *iface)
1741 ret = setup_interface(iface);
1743 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1744 iface->bss[0]->conf->iface);
1753 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1754 * @hapd_iface: Pointer to interface data
1755 * @conf: Pointer to per-interface configuration
1756 * @bss: Pointer to per-BSS configuration for this BSS
1757 * Returns: Pointer to allocated BSS data
1759 * This function is used to allocate per-BSS data structure. This data will be
1760 * freed after hostapd_cleanup() is called for it during interface
1763 struct hostapd_data *
1764 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1765 struct hostapd_config *conf,
1766 struct hostapd_bss_config *bss)
1768 struct hostapd_data *hapd;
1770 hapd = os_zalloc(sizeof(*hapd));
1774 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1777 hapd->iface = hapd_iface;
1778 hapd->driver = hapd->iconf->driver;
1779 hapd->ctrl_sock = -1;
1785 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1787 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1789 hostapd_bss_deinit_no_free(hapd);
1790 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1791 hostapd_cleanup(hapd);
1795 void hostapd_interface_deinit(struct hostapd_iface *iface)
1799 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1803 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1805 #ifdef CONFIG_IEEE80211N
1807 hostapd_stop_setup_timers(iface);
1808 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1809 #endif /* NEED_AP_MLME */
1810 #endif /* CONFIG_IEEE80211N */
1811 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1812 iface->wait_channel_update = 0;
1816 fst_detach(iface->fst);
1819 #endif /* CONFIG_FST */
1821 for (j = iface->num_bss - 1; j >= 0; j--)
1822 hostapd_bss_deinit(iface->bss[j]);
1826 void hostapd_interface_free(struct hostapd_iface *iface)
1829 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1830 for (j = 0; j < iface->num_bss; j++) {
1831 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1832 __func__, iface->bss[j]);
1833 os_free(iface->bss[j]);
1835 hostapd_cleanup_iface(iface);
1840 * hostapd_init - Allocate and initialize per-interface data
1841 * @config_file: Path to the configuration file
1842 * Returns: Pointer to the allocated interface data or %NULL on failure
1844 * This function is used to allocate main data structures for per-interface
1845 * data. The allocated data buffer will be freed by calling
1846 * hostapd_cleanup_iface().
1848 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1849 const char *config_file)
1851 struct hostapd_iface *hapd_iface = NULL;
1852 struct hostapd_config *conf = NULL;
1853 struct hostapd_data *hapd;
1856 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1857 if (hapd_iface == NULL)
1860 hapd_iface->config_fname = os_strdup(config_file);
1861 if (hapd_iface->config_fname == NULL)
1864 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1867 hapd_iface->conf = conf;
1869 hapd_iface->num_bss = conf->num_bss;
1870 hapd_iface->bss = os_calloc(conf->num_bss,
1871 sizeof(struct hostapd_data *));
1872 if (hapd_iface->bss == NULL)
1875 for (i = 0; i < conf->num_bss; i++) {
1876 hapd = hapd_iface->bss[i] =
1877 hostapd_alloc_bss_data(hapd_iface, conf,
1881 hapd->msg_ctx = hapd;
1887 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1890 hostapd_config_free(conf);
1892 os_free(hapd_iface->config_fname);
1893 os_free(hapd_iface->bss);
1894 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1895 __func__, hapd_iface);
1896 os_free(hapd_iface);
1902 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1906 for (i = 0; i < interfaces->count; i++) {
1907 struct hostapd_iface *iface = interfaces->iface[i];
1908 for (j = 0; j < iface->num_bss; j++) {
1909 struct hostapd_data *hapd = iface->bss[j];
1910 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1920 * hostapd_interface_init_bss - Read configuration file and init BSS data
1922 * This function is used to parse configuration file for a BSS. This BSS is
1923 * added to an existing interface sharing the same radio (if any) or a new
1924 * interface is created if this is the first interface on a radio. This
1925 * allocate memory for the BSS. No actual driver operations are started.
1927 * This is similar to hostapd_interface_init(), but for a case where the
1928 * configuration is used to add a single BSS instead of all BSSes for a radio.
1930 struct hostapd_iface *
1931 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1932 const char *config_fname, int debug)
1934 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1935 struct hostapd_data *hapd;
1942 for (i = 0; i < interfaces->count; i++) {
1943 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1944 iface = interfaces->iface[i];
1949 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1950 config_fname, phy, iface ? "" : " --> new PHY");
1952 struct hostapd_config *conf;
1953 struct hostapd_bss_config **tmp_conf;
1954 struct hostapd_data **tmp_bss;
1955 struct hostapd_bss_config *bss;
1958 /* Add new BSS to existing iface */
1959 conf = interfaces->config_read_cb(config_fname);
1962 if (conf->num_bss > 1) {
1963 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1964 hostapd_config_free(conf);
1968 ifname = conf->bss[0]->iface;
1969 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1970 wpa_printf(MSG_ERROR,
1971 "Interface name %s already in use", ifname);
1972 hostapd_config_free(conf);
1976 tmp_conf = os_realloc_array(
1977 iface->conf->bss, iface->conf->num_bss + 1,
1978 sizeof(struct hostapd_bss_config *));
1979 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1980 sizeof(struct hostapd_data *));
1982 iface->bss = tmp_bss;
1984 iface->conf->bss = tmp_conf;
1985 iface->conf->last_bss = tmp_conf[0];
1987 if (tmp_bss == NULL || tmp_conf == NULL) {
1988 hostapd_config_free(conf);
1991 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1992 iface->conf->num_bss++;
1994 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1996 iface->conf->num_bss--;
1997 hostapd_config_free(conf);
2000 iface->conf->last_bss = bss;
2001 iface->bss[iface->num_bss] = hapd;
2002 hapd->msg_ctx = hapd;
2004 bss_idx = iface->num_bss++;
2006 conf->bss[0] = NULL;
2007 hostapd_config_free(conf);
2009 /* Add a new iface with the first BSS */
2010 new_iface = iface = hostapd_init(interfaces, config_fname);
2013 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2014 iface->interfaces = interfaces;
2018 for (k = 0; k < debug; k++) {
2019 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2020 iface->bss[bss_idx]->conf->logger_stdout_level--;
2023 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2024 !hostapd_drv_none(iface->bss[bss_idx])) {
2025 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2028 hostapd_interface_deinit_free(new_iface);
2036 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2038 const struct wpa_driver_ops *driver;
2041 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2044 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2045 __func__, (unsigned int) iface->num_bss,
2046 (unsigned int) iface->conf->num_bss);
2047 driver = iface->bss[0]->driver;
2048 drv_priv = iface->bss[0]->drv_priv;
2049 hostapd_interface_deinit(iface);
2050 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2051 __func__, driver, drv_priv);
2052 if (driver && driver->hapd_deinit && drv_priv) {
2053 driver->hapd_deinit(drv_priv);
2054 iface->bss[0]->drv_priv = NULL;
2056 hostapd_interface_free(iface);
2060 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2062 struct hostapd_iface *hapd_iface)
2066 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2067 __func__, driver, drv_priv);
2068 if (driver && driver->hapd_deinit && drv_priv) {
2069 driver->hapd_deinit(drv_priv);
2070 for (j = 0; j < hapd_iface->num_bss; j++) {
2071 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2073 hapd_iface->bss[j]->drv_priv);
2074 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2075 hapd_iface->bss[j]->drv_priv = NULL;
2081 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2085 if (hapd_iface->bss[0]->drv_priv != NULL) {
2086 wpa_printf(MSG_ERROR, "Interface %s already enabled",
2087 hapd_iface->conf->bss[0]->iface);
2091 wpa_printf(MSG_DEBUG, "Enable interface %s",
2092 hapd_iface->conf->bss[0]->iface);
2094 for (j = 0; j < hapd_iface->num_bss; j++)
2095 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2096 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2097 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2101 if (hapd_iface->interfaces == NULL ||
2102 hapd_iface->interfaces->driver_init == NULL ||
2103 hapd_iface->interfaces->driver_init(hapd_iface))
2106 if (hostapd_setup_interface(hapd_iface)) {
2107 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2108 hapd_iface->bss[0]->drv_priv,
2117 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2121 wpa_printf(MSG_DEBUG, "Reload interface %s",
2122 hapd_iface->conf->bss[0]->iface);
2123 for (j = 0; j < hapd_iface->num_bss; j++)
2124 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2125 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2126 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2129 hostapd_clear_old(hapd_iface);
2130 for (j = 0; j < hapd_iface->num_bss; j++)
2131 hostapd_reload_bss(hapd_iface->bss[j]);
2137 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2140 const struct wpa_driver_ops *driver;
2143 if (hapd_iface == NULL)
2146 if (hapd_iface->bss[0]->drv_priv == NULL) {
2147 wpa_printf(MSG_INFO, "Interface %s already disabled",
2148 hapd_iface->conf->bss[0]->iface);
2152 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2153 driver = hapd_iface->bss[0]->driver;
2154 drv_priv = hapd_iface->bss[0]->drv_priv;
2156 hapd_iface->driver_ap_teardown =
2157 !!(hapd_iface->drv_flags &
2158 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2160 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
2161 for (j = 0; j < hapd_iface->num_bss; j++) {
2162 struct hostapd_data *hapd = hapd_iface->bss[j];
2163 hostapd_bss_deinit_no_free(hapd);
2164 hostapd_free_hapd_data(hapd);
2167 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
2169 /* From hostapd_cleanup_iface: These were initialized in
2170 * hostapd_setup_interface and hostapd_setup_interface_complete
2172 hostapd_cleanup_iface_partial(hapd_iface);
2174 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2175 hapd_iface->bss[0]->conf->iface);
2176 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2181 static struct hostapd_iface *
2182 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2184 struct hostapd_iface **iface, *hapd_iface;
2186 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2187 sizeof(struct hostapd_iface *));
2190 interfaces->iface = iface;
2191 hapd_iface = interfaces->iface[interfaces->count] =
2192 os_zalloc(sizeof(*hapd_iface));
2193 if (hapd_iface == NULL) {
2194 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2195 "the interface", __func__);
2198 interfaces->count++;
2199 hapd_iface->interfaces = interfaces;
2205 static struct hostapd_config *
2206 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2207 const char *ctrl_iface, const char *driver)
2209 struct hostapd_bss_config *bss;
2210 struct hostapd_config *conf;
2212 /* Allocates memory for bss and conf */
2213 conf = hostapd_config_defaults();
2215 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2216 "configuration", __func__);
2223 for (j = 0; wpa_drivers[j]; j++) {
2224 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2225 conf->driver = wpa_drivers[j];
2230 wpa_printf(MSG_ERROR,
2231 "Invalid/unknown driver '%s' - registering the default driver",
2235 conf->driver = wpa_drivers[0];
2236 if (conf->driver == NULL) {
2237 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2238 hostapd_config_free(conf);
2243 bss = conf->last_bss = conf->bss[0];
2245 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2246 bss->ctrl_interface = os_strdup(ctrl_iface);
2247 if (bss->ctrl_interface == NULL) {
2248 hostapd_config_free(conf);
2252 /* Reading configuration file skipped, will be done in SET!
2253 * From reading the configuration till the end has to be done in
2260 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2261 struct hostapd_config *conf)
2264 struct hostapd_data *hapd;
2266 hapd_iface->bss = os_calloc(conf->num_bss,
2267 sizeof(struct hostapd_data *));
2268 if (hapd_iface->bss == NULL)
2271 for (i = 0; i < conf->num_bss; i++) {
2272 hapd = hapd_iface->bss[i] =
2273 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2277 os_free(hapd_iface->bss[i]);
2278 hapd_iface->bss[i] = NULL;
2280 os_free(hapd_iface->bss);
2281 hapd_iface->bss = NULL;
2284 hapd->msg_ctx = hapd;
2287 hapd_iface->conf = conf;
2288 hapd_iface->num_bss = conf->num_bss;
2294 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2296 struct hostapd_config *conf = NULL;
2297 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2298 struct hostapd_data *hapd;
2301 const char *conf_file = NULL, *phy_name = NULL;
2303 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2305 phy_name = buf + 11;
2306 pos = os_strchr(phy_name, ':');
2311 if (!os_strlen(conf_file))
2314 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2318 for (j = 0; j < interfaces->count; j++) {
2319 if (interfaces->iface[j] == hapd_iface)
2322 if (j == interfaces->count) {
2323 struct hostapd_iface **tmp;
2324 tmp = os_realloc_array(interfaces->iface,
2325 interfaces->count + 1,
2326 sizeof(struct hostapd_iface *));
2328 hostapd_interface_deinit_free(hapd_iface);
2331 interfaces->iface = tmp;
2332 interfaces->iface[interfaces->count++] = hapd_iface;
2333 new_iface = hapd_iface;
2337 if (interfaces->driver_init(hapd_iface))
2340 if (hostapd_setup_interface(hapd_iface)) {
2341 hostapd_deinit_driver(
2342 hapd_iface->bss[0]->driver,
2343 hapd_iface->bss[0]->drv_priv,
2348 /* Assign new BSS with bss[0]'s driver info */
2349 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2350 hapd->driver = hapd_iface->bss[0]->driver;
2351 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2352 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2355 if (start_ctrl_iface_bss(hapd) < 0 ||
2356 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2357 hostapd_setup_bss(hapd, -1))) {
2358 hostapd_cleanup(hapd);
2359 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2360 hapd_iface->conf->num_bss--;
2361 hapd_iface->num_bss--;
2362 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2363 __func__, hapd, hapd->conf->iface);
2364 hostapd_config_free_bss(hapd->conf);
2373 ptr = os_strchr(buf, ' ');
2378 if (os_strncmp(ptr, "config=", 7) == 0)
2379 conf_file = ptr + 7;
2381 for (i = 0; i < interfaces->count; i++) {
2382 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2384 wpa_printf(MSG_INFO, "Cannot add interface - it "
2390 hapd_iface = hostapd_iface_alloc(interfaces);
2391 if (hapd_iface == NULL) {
2392 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2393 "for interface", __func__);
2396 new_iface = hapd_iface;
2398 if (conf_file && interfaces->config_read_cb) {
2399 conf = interfaces->config_read_cb(conf_file);
2400 if (conf && conf->bss)
2401 os_strlcpy(conf->bss[0]->iface, buf,
2402 sizeof(conf->bss[0]->iface));
2404 char *driver = os_strchr(ptr, ' ');
2408 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2411 if (conf == NULL || conf->bss == NULL) {
2412 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2413 "for configuration", __func__);
2417 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2418 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2419 "for hostapd", __func__);
2424 if (start_ctrl_iface(hapd_iface) < 0)
2427 wpa_printf(MSG_INFO, "Add interface '%s'",
2428 hapd_iface->conf->bss[0]->iface);
2434 hostapd_config_free(conf);
2436 if (hapd_iface->bss) {
2437 for (i = 0; i < hapd_iface->num_bss; i++) {
2438 hapd = hapd_iface->bss[i];
2441 if (hapd_iface->interfaces &&
2442 hapd_iface->interfaces->ctrl_iface_deinit)
2443 hapd_iface->interfaces->
2444 ctrl_iface_deinit(hapd);
2445 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2446 __func__, hapd_iface->bss[i],
2448 hostapd_cleanup(hapd);
2450 hapd_iface->bss[i] = NULL;
2452 os_free(hapd_iface->bss);
2453 hapd_iface->bss = NULL;
2456 interfaces->count--;
2457 interfaces->iface[interfaces->count] = NULL;
2459 hostapd_cleanup_iface(hapd_iface);
2465 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2469 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2471 /* Remove hostapd_data only if it has already been initialized */
2472 if (idx < iface->num_bss) {
2473 struct hostapd_data *hapd = iface->bss[idx];
2475 hostapd_bss_deinit(hapd);
2476 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2477 __func__, hapd, hapd->conf->iface);
2478 hostapd_config_free_bss(hapd->conf);
2484 for (i = idx; i < iface->num_bss; i++)
2485 iface->bss[i] = iface->bss[i + 1];
2487 hostapd_config_free_bss(iface->conf->bss[idx]);
2488 iface->conf->bss[idx] = NULL;
2491 iface->conf->num_bss--;
2492 for (i = idx; i < iface->conf->num_bss; i++)
2493 iface->conf->bss[i] = iface->conf->bss[i + 1];
2499 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2501 struct hostapd_iface *hapd_iface;
2504 for (i = 0; i < interfaces->count; i++) {
2505 hapd_iface = interfaces->iface[i];
2506 if (hapd_iface == NULL)
2508 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2509 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2510 hapd_iface->driver_ap_teardown =
2511 !!(hapd_iface->drv_flags &
2512 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2514 hostapd_interface_deinit_free(hapd_iface);
2516 while (k < (interfaces->count - 1)) {
2517 interfaces->iface[k] =
2518 interfaces->iface[k + 1];
2521 interfaces->count--;
2525 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2526 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2527 hapd_iface->driver_ap_teardown =
2528 !(hapd_iface->drv_flags &
2529 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2530 return hostapd_remove_bss(hapd_iface, j);
2539 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2540 * @hapd: Pointer to BSS data
2541 * @sta: Pointer to the associated STA data
2542 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2544 * This function will be called whenever a station associates with the AP. It
2545 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2546 * from drv_callbacks.c based on driver events for drivers that take care of
2547 * management frames (IEEE 802.11 authentication and association) internally.
2549 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2552 if (hapd->tkip_countermeasures) {
2553 hostapd_drv_sta_deauth(hapd, sta->addr,
2554 WLAN_REASON_MICHAEL_MIC_FAILURE);
2558 hostapd_prune_associations(hapd, sta->addr);
2560 /* IEEE 802.11F (IAPP) */
2561 if (hapd->conf->ieee802_11f)
2562 iapp_new_station(hapd->iapp, sta);
2565 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2566 sta->no_p2p_set = 1;
2567 hapd->num_sta_no_p2p++;
2568 if (hapd->num_sta_no_p2p == 1)
2569 hostapd_p2p_non_p2p_sta_connected(hapd);
2571 #endif /* CONFIG_P2P */
2573 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2574 * IEEE 802.1X/WPA code will start accounting after the station has
2575 * been authorized. */
2576 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2577 ap_sta_set_authorized(hapd, sta, 1);
2578 os_get_reltime(&sta->connected_time);
2579 accounting_sta_start(hapd, sta);
2582 /* Start IEEE 802.1X authentication process for new stations */
2583 ieee802_1x_new_station(hapd, sta);
2585 if (sta->auth_alg != WLAN_AUTH_FT &&
2586 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2587 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2589 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2591 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2592 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2593 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2594 __func__, MAC2STR(sta->addr),
2595 hapd->conf->ap_max_inactivity);
2596 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2597 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2598 ap_handle_timer, hapd, sta);
2603 const char * hostapd_state_text(enum hostapd_iface_state s)
2606 case HAPD_IFACE_UNINITIALIZED:
2607 return "UNINITIALIZED";
2608 case HAPD_IFACE_DISABLED:
2610 case HAPD_IFACE_COUNTRY_UPDATE:
2611 return "COUNTRY_UPDATE";
2612 case HAPD_IFACE_ACS:
2614 case HAPD_IFACE_HT_SCAN:
2616 case HAPD_IFACE_DFS:
2618 case HAPD_IFACE_ENABLED:
2626 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2628 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2629 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2630 hostapd_state_text(s));
2637 static void free_beacon_data(struct beacon_data *beacon)
2639 os_free(beacon->head);
2640 beacon->head = NULL;
2641 os_free(beacon->tail);
2642 beacon->tail = NULL;
2643 os_free(beacon->probe_resp);
2644 beacon->probe_resp = NULL;
2645 os_free(beacon->beacon_ies);
2646 beacon->beacon_ies = NULL;
2647 os_free(beacon->proberesp_ies);
2648 beacon->proberesp_ies = NULL;
2649 os_free(beacon->assocresp_ies);
2650 beacon->assocresp_ies = NULL;
2654 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2655 struct beacon_data *beacon)
2657 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2658 struct wpa_driver_ap_params params;
2661 os_memset(beacon, 0, sizeof(*beacon));
2662 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2666 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2670 goto free_ap_params;
2673 beacon->head = os_malloc(params.head_len);
2675 goto free_ap_extra_ies;
2677 os_memcpy(beacon->head, params.head, params.head_len);
2678 beacon->head_len = params.head_len;
2680 beacon->tail = os_malloc(params.tail_len);
2684 os_memcpy(beacon->tail, params.tail, params.tail_len);
2685 beacon->tail_len = params.tail_len;
2687 if (params.proberesp != NULL) {
2688 beacon->probe_resp = os_malloc(params.proberesp_len);
2689 if (!beacon->probe_resp)
2692 os_memcpy(beacon->probe_resp, params.proberesp,
2693 params.proberesp_len);
2694 beacon->probe_resp_len = params.proberesp_len;
2697 /* copy the extra ies */
2699 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2700 if (!beacon->beacon_ies)
2703 os_memcpy(beacon->beacon_ies,
2704 beacon_extra->buf, wpabuf_len(beacon_extra));
2705 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2708 if (proberesp_extra) {
2709 beacon->proberesp_ies =
2710 os_malloc(wpabuf_len(proberesp_extra));
2711 if (!beacon->proberesp_ies)
2714 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2715 wpabuf_len(proberesp_extra));
2716 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2719 if (assocresp_extra) {
2720 beacon->assocresp_ies =
2721 os_malloc(wpabuf_len(assocresp_extra));
2722 if (!beacon->assocresp_ies)
2725 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2726 wpabuf_len(assocresp_extra));
2727 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2732 /* if the function fails, the caller should not free beacon data */
2734 free_beacon_data(beacon);
2737 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2740 ieee802_11_free_ap_params(¶ms);
2746 * TODO: This flow currently supports only changing frequency within the
2747 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2748 * width) are not supported.
2750 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2751 struct hostapd_config *conf,
2752 struct hostapd_freq_params *params,
2753 struct hostapd_freq_params *old_params)
2757 if (!params->channel) {
2758 /* check if the new channel is supported by hw */
2759 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2762 channel = params->channel;
2766 /* if a pointer to old_params is provided we save previous state */
2768 old_params->channel = conf->channel;
2769 old_params->ht_enabled = conf->ieee80211n;
2770 old_params->sec_channel_offset = conf->secondary_channel;
2773 conf->channel = channel;
2774 conf->ieee80211n = params->ht_enabled;
2775 conf->secondary_channel = params->sec_channel_offset;
2777 /* TODO: maybe call here hostapd_config_check here? */
2783 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2784 struct csa_settings *settings)
2786 struct hostapd_iface *iface = hapd->iface;
2787 struct hostapd_freq_params old_freq;
2790 os_memset(&old_freq, 0, sizeof(old_freq));
2791 if (!iface || !iface->freq || hapd->csa_in_progress)
2794 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2795 &settings->freq_params,
2800 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2802 /* change back the configuration */
2803 hostapd_change_config_freq(iface->bss[0], iface->conf,
2809 /* set channel switch parameters for csa ie */
2810 hapd->cs_freq_params = settings->freq_params;
2811 hapd->cs_count = settings->cs_count;
2812 hapd->cs_block_tx = settings->block_tx;
2814 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2816 free_beacon_data(&settings->beacon_after);
2820 settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2821 settings->counter_offset_presp = hapd->cs_c_off_proberesp;
2827 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2829 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2831 hapd->cs_block_tx = 0;
2832 hapd->cs_c_off_beacon = 0;
2833 hapd->cs_c_off_proberesp = 0;
2834 hapd->csa_in_progress = 0;
2838 int hostapd_switch_channel(struct hostapd_data *hapd,
2839 struct csa_settings *settings)
2843 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2844 wpa_printf(MSG_INFO, "CSA is not supported");
2848 ret = hostapd_fill_csa_settings(hapd, settings);
2852 ret = hostapd_drv_switch_channel(hapd, settings);
2853 free_beacon_data(&settings->beacon_csa);
2854 free_beacon_data(&settings->beacon_after);
2857 /* if we failed, clean cs parameters */
2858 hostapd_cleanup_cs_params(hapd);
2862 hapd->csa_in_progress = 1;
2868 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2869 const struct hostapd_freq_params *freq_params)
2871 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2874 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2876 if (freq_params->center_freq1)
2877 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2878 if (freq_params->center_freq2)
2879 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2881 switch (freq_params->bandwidth) {
2885 vht_bw = VHT_CHANWIDTH_USE_HT;
2888 if (freq_params->center_freq2)
2889 vht_bw = VHT_CHANWIDTH_80P80MHZ;
2891 vht_bw = VHT_CHANWIDTH_80MHZ;
2894 vht_bw = VHT_CHANWIDTH_160MHZ;
2897 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2898 freq_params->bandwidth);
2902 iface->freq = freq_params->freq;
2903 iface->conf->channel = freq_params->channel;
2904 iface->conf->secondary_channel = freq_params->sec_channel_offset;
2905 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2906 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2907 iface->conf->vht_oper_chwidth = vht_bw;
2908 iface->conf->ieee80211n = freq_params->ht_enabled;
2909 iface->conf->ieee80211ac = freq_params->vht_enabled;
2912 * cs_params must not be cleared earlier because the freq_params
2913 * argument may actually point to one of these.
2915 for (i = 0; i < iface->num_bss; i++)
2916 hostapd_cleanup_cs_params(iface->bss[i]);
2918 hostapd_disable_iface(iface);
2919 hostapd_enable_iface(iface);
2923 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
2928 for (i = 0; i < interfaces->count; i++) {
2929 struct hostapd_iface *iface = interfaces->iface[i];
2931 for (j = 0; j < iface->num_bss; j++) {
2932 struct hostapd_data *hapd = iface->bss[j];
2934 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2942 #endif /* NEED_AP_MLME */
2945 void hostapd_periodic_iface(struct hostapd_iface *iface)
2949 ap_list_timer(iface);
2951 for (i = 0; i < iface->num_bss; i++) {
2952 struct hostapd_data *hapd = iface->bss[i];
2957 #ifndef CONFIG_NO_RADIUS
2958 hostapd_acl_expire(hapd);
2959 #endif /* CONFIG_NO_RADIUS */