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"
23 #include "accounting.h"
27 #include "ieee802_1x.h"
28 #include "ieee802_11_auth.h"
29 #include "vlan_init.h"
31 #include "wps_hostapd.h"
32 #include "hw_features.h"
33 #include "wpa_auth_glue.h"
34 #include "ap_drv_ops.h"
35 #include "ap_config.h"
36 #include "p2p_hostapd.h"
39 #include "ieee802_11.h"
42 #include "dhcp_snoop.h"
43 #include "ndisc_snoop.h"
46 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
47 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
48 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
49 static int setup_interface2(struct hostapd_iface *iface);
50 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
53 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
54 int (*cb)(struct hostapd_iface *iface,
55 void *ctx), void *ctx)
60 for (i = 0; i < interfaces->count; i++) {
61 ret = cb(interfaces->iface[i], ctx);
70 static void hostapd_reload_bss(struct hostapd_data *hapd)
72 struct hostapd_ssid *ssid;
74 #ifndef CONFIG_NO_RADIUS
75 radius_client_reconfig(hapd->radius, hapd->conf->radius);
76 #endif /* CONFIG_NO_RADIUS */
78 ssid = &hapd->conf->ssid;
79 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
80 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
82 * Force PSK to be derived again since SSID or passphrase may
85 os_free(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->secondary_channel = oldconf->secondary_channel;
184 hapd->iconf->ieee80211n = oldconf->ieee80211n;
185 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
186 hapd->iconf->ht_capab = oldconf->ht_capab;
187 hapd->iconf->vht_capab = oldconf->vht_capab;
188 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
189 hapd->iconf->vht_oper_centr_freq_seg0_idx =
190 oldconf->vht_oper_centr_freq_seg0_idx;
191 hapd->iconf->vht_oper_centr_freq_seg1_idx =
192 oldconf->vht_oper_centr_freq_seg1_idx;
193 hapd->conf = newconf->bss[j];
194 hostapd_reload_bss(hapd);
197 hostapd_config_free(oldconf);
204 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
209 for (i = 0; i < NUM_WEP_KEYS; i++) {
210 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
211 0, NULL, 0, NULL, 0)) {
212 wpa_printf(MSG_DEBUG, "Failed to clear default "
213 "encryption keys (ifname=%s keyidx=%d)",
217 #ifdef CONFIG_IEEE80211W
218 if (hapd->conf->ieee80211w) {
219 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
220 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
223 wpa_printf(MSG_DEBUG, "Failed to clear "
224 "default mgmt encryption keys "
225 "(ifname=%s keyidx=%d)", ifname, i);
229 #endif /* CONFIG_IEEE80211W */
233 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
235 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
240 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
243 struct hostapd_ssid *ssid = &hapd->conf->ssid;
246 if (ssid->wep.default_len &&
247 hostapd_drv_set_key(hapd->conf->iface,
248 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
249 1, NULL, 0, ssid->wep.key[idx],
250 ssid->wep.len[idx])) {
251 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
259 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
261 os_free(hapd->probereq_cb);
262 hapd->probereq_cb = NULL;
265 wpabuf_free(hapd->p2p_beacon_ie);
266 hapd->p2p_beacon_ie = NULL;
267 wpabuf_free(hapd->p2p_probe_resp_ie);
268 hapd->p2p_probe_resp_ie = NULL;
269 #endif /* CONFIG_P2P */
271 if (!hapd->started) {
272 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
273 __func__, hapd->conf->iface);
278 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
279 iapp_deinit(hapd->iapp);
281 accounting_deinit(hapd);
282 hostapd_deinit_wpa(hapd);
284 hostapd_acl_deinit(hapd);
285 #ifndef CONFIG_NO_RADIUS
286 radius_client_deinit(hapd->radius);
288 radius_das_deinit(hapd->radius_das);
289 hapd->radius_das = NULL;
290 #endif /* CONFIG_NO_RADIUS */
292 hostapd_deinit_wps(hapd);
294 authsrv_deinit(hapd);
296 if (hapd->interface_added) {
297 hapd->interface_added = 0;
298 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
299 wpa_printf(MSG_WARNING,
300 "Failed to remove BSS interface %s",
302 hapd->interface_added = 1;
305 * Since this was a dynamically added interface, the
306 * driver wrapper may have removed its internal instance
307 * and hapd->drv_priv is not valid anymore.
309 hapd->drv_priv = NULL;
313 wpabuf_free(hapd->time_adv);
315 #ifdef CONFIG_INTERWORKING
316 gas_serv_deinit(hapd);
317 #endif /* CONFIG_INTERWORKING */
319 bss_load_update_deinit(hapd);
320 ndisc_snoop_deinit(hapd);
321 dhcp_snoop_deinit(hapd);
322 x_snoop_deinit(hapd);
325 bin_clear_free(hapd->tmp_eap_user.identity,
326 hapd->tmp_eap_user.identity_len);
327 bin_clear_free(hapd->tmp_eap_user.password,
328 hapd->tmp_eap_user.password_len);
329 #endif /* CONFIG_SQLITE */
332 wpabuf_free(hapd->mesh_pending_auth);
333 hapd->mesh_pending_auth = NULL;
334 #endif /* CONFIG_MESH */
339 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
340 * @hapd: Pointer to BSS data
342 * This function is used to free all per-BSS data structures and resources.
343 * Most of the modules that are initialized in hostapd_setup_bss() are
344 * deinitialized here.
346 static void hostapd_cleanup(struct hostapd_data *hapd)
348 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
350 if (hapd->iface->interfaces &&
351 hapd->iface->interfaces->ctrl_iface_deinit)
352 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
353 hostapd_free_hapd_data(hapd);
357 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
359 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
360 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
361 iface->hw_features = NULL;
362 os_free(iface->current_rates);
363 iface->current_rates = NULL;
364 os_free(iface->basic_rates);
365 iface->basic_rates = NULL;
366 ap_list_deinit(iface);
371 * hostapd_cleanup_iface - Complete per-interface cleanup
372 * @iface: Pointer to interface data
374 * This function is called after per-BSS data structures are deinitialized
375 * with hostapd_cleanup().
377 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
379 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
380 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
382 hostapd_cleanup_iface_partial(iface);
383 hostapd_config_free(iface->conf);
386 os_free(iface->config_fname);
388 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
393 static void hostapd_clear_wep(struct hostapd_data *hapd)
395 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
396 hostapd_set_privacy(hapd, 0);
397 hostapd_broadcast_wep_clear(hapd);
402 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
406 hostapd_broadcast_wep_set(hapd);
408 if (hapd->conf->ssid.wep.default_len) {
409 hostapd_set_privacy(hapd, 1);
414 * When IEEE 802.1X is not enabled, the driver may need to know how to
415 * set authentication algorithms for static WEP.
417 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
419 for (i = 0; i < 4; i++) {
420 if (hapd->conf->ssid.wep.key[i] &&
421 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
422 i == hapd->conf->ssid.wep.idx, NULL, 0,
423 hapd->conf->ssid.wep.key[i],
424 hapd->conf->ssid.wep.len[i])) {
425 wpa_printf(MSG_WARNING, "Could not set WEP "
429 if (hapd->conf->ssid.wep.key[i] &&
430 i == hapd->conf->ssid.wep.idx)
431 hostapd_set_privacy(hapd, 1);
438 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
443 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
446 if (!hapd->iface->driver_ap_teardown) {
447 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
448 "Flushing old station entries");
450 if (hostapd_flush(hapd)) {
451 wpa_msg(hapd->msg_ctx, MSG_WARNING,
452 "Could not connect to kernel driver");
456 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
457 os_memset(addr, 0xff, ETH_ALEN);
458 hostapd_drv_sta_deauth(hapd, addr, reason);
459 hostapd_free_stas(hapd);
465 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
467 hostapd_free_stas(hapd);
468 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
469 hostapd_clear_wep(hapd);
474 * hostapd_validate_bssid_configuration - Validate BSSID configuration
475 * @iface: Pointer to interface data
476 * Returns: 0 on success, -1 on failure
478 * This function is used to validate that the configured BSSIDs are valid.
480 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
482 u8 mask[ETH_ALEN] = { 0 };
483 struct hostapd_data *hapd = iface->bss[0];
484 unsigned int i = iface->conf->num_bss, bits = 0, j;
487 if (hostapd_drv_none(hapd))
490 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
492 /* Determine the bits necessary to cover the number of BSSIDs. */
493 for (i--; i; i >>= 1)
496 /* Determine the bits necessary to any configured BSSIDs,
497 if they are higher than the number of BSSIDs. */
498 for (j = 0; j < iface->conf->num_bss; j++) {
499 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
505 for (i = 0; i < ETH_ALEN; i++) {
507 iface->conf->bss[j]->bssid[i] ^
515 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
521 while (mask[i] != 0) {
531 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
536 os_memset(mask, 0xff, ETH_ALEN);
538 for (i = 5; i > 5 - j; i--)
545 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
546 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
551 for (i = 0; i < ETH_ALEN; i++) {
552 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
553 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
554 " for start address " MACSTR ".",
555 MAC2STR(mask), MAC2STR(hapd->own_addr));
556 wpa_printf(MSG_ERROR, "Start address must be the "
557 "first address in the block (i.e., addr "
558 "AND mask == addr).");
567 static int mac_in_conf(struct hostapd_config *conf, const void *a)
571 for (i = 0; i < conf->num_bss; i++) {
572 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
581 #ifndef CONFIG_NO_RADIUS
583 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
584 struct radius_das_attrs *attr)
586 if (attr->nas_identifier &&
587 (!hapd->conf->nas_identifier ||
588 os_strlen(hapd->conf->nas_identifier) !=
589 attr->nas_identifier_len ||
590 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
591 attr->nas_identifier_len) != 0)) {
592 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
596 if (attr->nas_ip_addr &&
597 (hapd->conf->own_ip_addr.af != AF_INET ||
598 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
600 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
605 if (attr->nas_ipv6_addr &&
606 (hapd->conf->own_ip_addr.af != AF_INET6 ||
607 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
609 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
612 #endif /* CONFIG_IPV6 */
618 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
619 struct radius_das_attrs *attr,
622 struct sta_info *selected, *sta;
629 for (sta = hapd->sta_list; sta; sta = sta->next)
630 sta->radius_das_match = 1;
632 if (attr->sta_addr) {
634 sta = ap_get_sta(hapd, attr->sta_addr);
636 wpa_printf(MSG_DEBUG,
637 "RADIUS DAS: No Calling-Station-Id match");
642 for (sta = hapd->sta_list; sta; sta = sta->next) {
644 sta->radius_das_match = 0;
646 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
649 if (attr->acct_session_id) {
651 if (attr->acct_session_id_len != 17) {
652 wpa_printf(MSG_DEBUG,
653 "RADIUS DAS: Acct-Session-Id cannot match");
658 for (sta = hapd->sta_list; sta; sta = sta->next) {
659 if (!sta->radius_das_match)
661 os_snprintf(buf, sizeof(buf), "%08X-%08X",
662 sta->acct_session_id_hi,
663 sta->acct_session_id_lo);
664 if (os_memcmp(attr->acct_session_id, buf, 17) != 0)
665 sta->radius_das_match = 0;
671 wpa_printf(MSG_DEBUG,
672 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
675 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
678 if (attr->acct_multi_session_id) {
680 if (attr->acct_multi_session_id_len != 17) {
681 wpa_printf(MSG_DEBUG,
682 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
687 for (sta = hapd->sta_list; sta; sta = sta->next) {
688 if (!sta->radius_das_match)
690 if (!sta->eapol_sm ||
691 !sta->eapol_sm->acct_multi_session_id_hi) {
692 sta->radius_das_match = 0;
695 os_snprintf(buf, sizeof(buf), "%08X+%08X",
696 sta->eapol_sm->acct_multi_session_id_hi,
697 sta->eapol_sm->acct_multi_session_id_lo);
698 if (os_memcmp(attr->acct_multi_session_id, buf, 17) !=
700 sta->radius_das_match = 0;
706 wpa_printf(MSG_DEBUG,
707 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
710 wpa_printf(MSG_DEBUG,
711 "RADIUS DAS: Acct-Multi-Session-Id match");
718 for (sta = hapd->sta_list; sta; sta = sta->next) {
721 if (!sta->radius_das_match)
723 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
724 if (!cui || wpabuf_len(cui) != attr->cui_len ||
725 os_memcmp(wpabuf_head(cui), attr->cui,
727 sta->radius_das_match = 0;
733 wpa_printf(MSG_DEBUG,
734 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
737 wpa_printf(MSG_DEBUG,
738 "RADIUS DAS: Chargeable-User-Identity match");
741 if (attr->user_name) {
745 for (sta = hapd->sta_list; sta; sta = sta->next) {
749 if (!sta->radius_das_match)
751 identity = ieee802_1x_get_identity(sta->eapol_sm,
754 identity_len != attr->user_name_len ||
755 os_memcmp(identity, attr->user_name, identity_len)
757 sta->radius_das_match = 0;
763 wpa_printf(MSG_DEBUG,
764 "RADIUS DAS: No matches remaining after User-Name check");
767 wpa_printf(MSG_DEBUG,
768 "RADIUS DAS: User-Name match");
773 * In theory, we could match all current associations, but it
774 * seems safer to just reject requests that do not include any
775 * session identification attributes.
777 wpa_printf(MSG_DEBUG,
778 "RADIUS DAS: No session identification attributes included");
783 for (sta = hapd->sta_list; sta; sta = sta->next) {
784 if (sta->radius_das_match) {
797 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
798 struct radius_das_attrs *attr)
802 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
806 static enum radius_das_res
807 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
809 struct hostapd_data *hapd = ctx;
810 struct sta_info *sta;
813 if (hostapd_das_nas_mismatch(hapd, attr))
814 return RADIUS_DAS_NAS_MISMATCH;
816 sta = hostapd_das_find_sta(hapd, attr, &multi);
819 wpa_printf(MSG_DEBUG,
820 "RADIUS DAS: Multiple sessions match - not supported");
821 return RADIUS_DAS_MULTI_SESSION_MATCH;
823 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
824 wpa_printf(MSG_DEBUG,
825 "RADIUS DAS: PMKSA cache entry matched");
826 return RADIUS_DAS_SUCCESS;
828 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
829 return RADIUS_DAS_SESSION_NOT_FOUND;
832 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
833 " - disconnecting", MAC2STR(sta->addr));
834 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
836 hostapd_drv_sta_deauth(hapd, sta->addr,
837 WLAN_REASON_PREV_AUTH_NOT_VALID);
838 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
840 return RADIUS_DAS_SUCCESS;
843 #endif /* CONFIG_NO_RADIUS */
847 * hostapd_setup_bss - Per-BSS setup (initialization)
848 * @hapd: Pointer to BSS data
849 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
850 * but interface may exist
852 * This function is used to initialize all per-BSS data structures and
853 * resources. This gets called in a loop for each BSS when an interface is
854 * initialized. Most of the modules that are initialized here will be
855 * deinitialized in hostapd_cleanup().
857 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
859 struct hostapd_bss_config *conf = hapd->conf;
860 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
861 int ssid_len, set_ssid;
862 char force_ifname[IFNAMSIZ];
863 u8 if_addr[ETH_ALEN];
864 int flush_old_stations = 1;
866 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
867 __func__, hapd, conf->iface, first);
869 #ifdef EAP_SERVER_TNC
870 if (conf->tnc && tncs_global_init() < 0) {
871 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
874 #endif /* EAP_SERVER_TNC */
877 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
878 __func__, conf->iface);
883 if (!first || first == -1) {
884 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
885 /* Allocate the next available BSSID. */
887 inc_byte_array(hapd->own_addr, ETH_ALEN);
888 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
890 /* Allocate the configured BSSID. */
891 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
893 if (hostapd_mac_comp(hapd->own_addr,
894 hapd->iface->bss[0]->own_addr) ==
896 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
897 "BSSID set to the MAC address of "
898 "the radio", conf->iface);
903 hapd->interface_added = 1;
904 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
905 conf->iface, hapd->own_addr, hapd,
906 &hapd->drv_priv, force_ifname, if_addr,
907 conf->bridge[0] ? conf->bridge : NULL,
909 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
910 MACSTR ")", MAC2STR(hapd->own_addr));
911 hapd->interface_added = 0;
916 if (conf->wmm_enabled < 0)
917 conf->wmm_enabled = hapd->iconf->ieee80211n;
920 if (hapd->iface->mconf == NULL)
921 flush_old_stations = 0;
922 #endif /* CONFIG_MESH */
924 if (flush_old_stations)
925 hostapd_flush_old_stations(hapd,
926 WLAN_REASON_PREV_AUTH_NOT_VALID);
927 hostapd_set_privacy(hapd, 0);
929 hostapd_broadcast_wep_clear(hapd);
930 if (hostapd_setup_encryption(conf->iface, hapd))
934 * Fetch the SSID from the system and use it or,
935 * if one was specified in the config file, verify they
938 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
940 wpa_printf(MSG_ERROR, "Could not read SSID from system");
943 if (conf->ssid.ssid_set) {
945 * If SSID is specified in the config file and it differs
946 * from what is being used then force installation of the
949 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
950 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
953 * No SSID in the config file; just use the one we got
957 conf->ssid.ssid_len = ssid_len;
958 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
961 if (!hostapd_drv_none(hapd)) {
962 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
964 conf->iface, MAC2STR(hapd->own_addr),
965 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
968 if (hostapd_setup_wpa_psk(conf)) {
969 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
973 /* Set SSID for the kernel driver (to be used in beacon and probe
974 * response frames) */
975 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
976 conf->ssid.ssid_len)) {
977 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
981 if (wpa_debug_level <= MSG_MSGDUMP)
982 conf->radius->msg_dumps = 1;
983 #ifndef CONFIG_NO_RADIUS
984 hapd->radius = radius_client_init(hapd, conf->radius);
985 if (hapd->radius == NULL) {
986 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
990 if (conf->radius_das_port) {
991 struct radius_das_conf das_conf;
992 os_memset(&das_conf, 0, sizeof(das_conf));
993 das_conf.port = conf->radius_das_port;
994 das_conf.shared_secret = conf->radius_das_shared_secret;
995 das_conf.shared_secret_len =
996 conf->radius_das_shared_secret_len;
997 das_conf.client_addr = &conf->radius_das_client_addr;
998 das_conf.time_window = conf->radius_das_time_window;
999 das_conf.require_event_timestamp =
1000 conf->radius_das_require_event_timestamp;
1001 das_conf.ctx = hapd;
1002 das_conf.disconnect = hostapd_das_disconnect;
1003 hapd->radius_das = radius_das_init(&das_conf);
1004 if (hapd->radius_das == NULL) {
1005 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1010 #endif /* CONFIG_NO_RADIUS */
1012 if (hostapd_acl_init(hapd)) {
1013 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1016 if (hostapd_init_wps(hapd, conf))
1019 if (authsrv_init(hapd) < 0)
1022 if (ieee802_1x_init(hapd)) {
1023 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1027 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1030 if (accounting_init(hapd)) {
1031 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1035 if (conf->ieee802_11f &&
1036 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1037 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1042 #ifdef CONFIG_INTERWORKING
1043 if (gas_serv_init(hapd)) {
1044 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1048 if (conf->qos_map_set_len &&
1049 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1050 conf->qos_map_set_len)) {
1051 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1054 #endif /* CONFIG_INTERWORKING */
1056 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1057 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1061 if (conf->proxy_arp) {
1062 if (x_snoop_init(hapd)) {
1063 wpa_printf(MSG_ERROR,
1064 "Generic snooping infrastructure initialization failed");
1068 if (dhcp_snoop_init(hapd)) {
1069 wpa_printf(MSG_ERROR,
1070 "DHCP snooping initialization failed");
1074 if (ndisc_snoop_init(hapd)) {
1075 wpa_printf(MSG_ERROR,
1076 "Neighbor Discovery snooping initialization failed");
1081 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1082 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1086 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1089 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1092 if (hapd->driver && hapd->driver->set_operstate)
1093 hapd->driver->set_operstate(hapd->drv_priv, 1);
1099 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1101 struct hostapd_data *hapd = iface->bss[0];
1103 struct hostapd_tx_queue_params *p;
1106 if (iface->mconf == NULL)
1108 #endif /* CONFIG_MESH */
1110 for (i = 0; i < NUM_TX_QUEUES; i++) {
1111 p = &iface->conf->tx_queue[i];
1113 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1114 p->cwmax, p->burst)) {
1115 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1116 "parameters for queue %d.", i);
1117 /* Continue anyway */
1123 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1124 struct mac_acl_entry *mac_acl,
1125 int n_entries, u8 accept_acl)
1127 struct hostapd_acl_params *acl_params;
1130 acl_params = os_zalloc(sizeof(*acl_params) +
1131 (n_entries * sizeof(acl_params->mac_acl[0])));
1135 for (i = 0; i < n_entries; i++)
1136 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1139 acl_params->acl_policy = accept_acl;
1140 acl_params->num_mac_acl = n_entries;
1142 err = hostapd_drv_set_acl(hapd, acl_params);
1144 os_free(acl_params);
1150 static void hostapd_set_acl(struct hostapd_data *hapd)
1152 struct hostapd_config *conf = hapd->iconf;
1156 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1159 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1161 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1162 conf->bss[0]->num_accept_mac,
1165 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1168 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1170 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1171 conf->bss[0]->num_deny_mac,
1174 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1181 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1183 if (!hapd->iface->interfaces ||
1184 !hapd->iface->interfaces->ctrl_iface_init)
1187 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1188 wpa_printf(MSG_ERROR,
1189 "Failed to setup control interface for %s",
1198 static int start_ctrl_iface(struct hostapd_iface *iface)
1202 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1205 for (i = 0; i < iface->num_bss; i++) {
1206 struct hostapd_data *hapd = iface->bss[i];
1207 if (iface->interfaces->ctrl_iface_init(hapd)) {
1208 wpa_printf(MSG_ERROR,
1209 "Failed to setup control interface for %s",
1219 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1221 struct hostapd_iface *iface = eloop_ctx;
1223 if (!iface->wait_channel_update) {
1224 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1229 * It is possible that the existing channel list is acceptable, so try
1232 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1233 setup_interface2(iface);
1237 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1239 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1242 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1243 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1244 setup_interface2(iface);
1248 static int setup_interface(struct hostapd_iface *iface)
1250 struct hostapd_data *hapd = iface->bss[0];
1254 * It is possible that setup_interface() is called after the interface
1255 * was disabled etc., in which case driver_ap_teardown is possibly set
1256 * to 1. Clear it here so any other key/station deletion, which is not
1257 * part of a teardown flow, would also call the relevant driver
1260 iface->driver_ap_teardown = 0;
1262 if (!iface->phy[0]) {
1263 const char *phy = hostapd_drv_get_radio_name(hapd);
1265 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1266 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1271 * Make sure that all BSSes get configured with a pointer to the same
1274 for (i = 1; i < iface->num_bss; i++) {
1275 iface->bss[i]->driver = hapd->driver;
1276 iface->bss[i]->drv_priv = hapd->drv_priv;
1279 if (hostapd_validate_bssid_configuration(iface))
1283 * Initialize control interfaces early to allow external monitoring of
1284 * channel setup operations that may take considerable amount of time
1285 * especially for DFS cases.
1287 if (start_ctrl_iface(iface))
1290 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1291 char country[4], previous_country[4];
1293 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1294 if (hostapd_get_country(hapd, previous_country) < 0)
1295 previous_country[0] = '\0';
1297 os_memcpy(country, hapd->iconf->country, 3);
1299 if (hostapd_set_country(hapd, country) < 0) {
1300 wpa_printf(MSG_ERROR, "Failed to set country code");
1304 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1305 previous_country, country);
1307 if (os_strncmp(previous_country, country, 2) != 0) {
1308 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1309 iface->wait_channel_update = 1;
1310 eloop_register_timeout(5, 0,
1311 channel_list_update_timeout,
1317 return setup_interface2(iface);
1321 static int setup_interface2(struct hostapd_iface *iface)
1323 iface->wait_channel_update = 0;
1325 if (hostapd_get_hw_features(iface)) {
1326 /* Not all drivers support this yet, so continue without hw
1329 int ret = hostapd_select_hw_mode(iface);
1331 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1332 "channel. (%d)", ret);
1336 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1339 ret = hostapd_check_ht_capab(iface);
1343 wpa_printf(MSG_DEBUG, "Interface initialization will "
1344 "be completed in a callback");
1348 if (iface->conf->ieee80211h)
1349 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1351 return hostapd_setup_interface_complete(iface, 0);
1354 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1355 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1356 if (iface->interfaces && iface->interfaces->terminate_on_error)
1363 * hostapd_setup_interface_complete - Complete interface setup
1365 * This function is called when previous steps in the interface setup has been
1366 * completed. This can also start operations, e.g., DFS, that will require
1367 * additional processing before interface is ready to be enabled. Such
1368 * operations will call this function from eloop callbacks when finished.
1370 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1372 struct hostapd_data *hapd = iface->bss[0];
1375 int delay_apply_cfg = 0;
1380 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1381 if (iface->conf->channel) {
1384 #endif /* NEED_AP_MLME */
1386 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1387 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1388 "Frequency: %d MHz",
1389 hostapd_hw_mode_txt(iface->conf->hw_mode),
1390 iface->conf->channel, iface->freq);
1393 /* Handle DFS only if it is not offloaded to the driver */
1394 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1396 res = hostapd_handle_dfs(iface);
1403 #endif /* NEED_AP_MLME */
1406 if (iface->mconf != NULL) {
1407 wpa_printf(MSG_DEBUG,
1408 "%s: Mesh configuration will be applied while joining the mesh network",
1409 iface->bss[0]->conf->iface);
1410 delay_apply_cfg = 1;
1412 #endif /* CONFIG_MESH */
1414 if (!delay_apply_cfg &&
1415 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1416 hapd->iconf->channel,
1417 hapd->iconf->ieee80211n,
1418 hapd->iconf->ieee80211ac,
1419 hapd->iconf->secondary_channel,
1420 hapd->iconf->vht_oper_chwidth,
1421 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1422 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1423 wpa_printf(MSG_ERROR, "Could not set channel for "
1429 if (iface->current_mode) {
1430 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1431 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1433 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1434 HOSTAPD_LEVEL_WARNING,
1435 "Failed to prepare rates table.");
1440 if (hapd->iconf->rts_threshold > -1 &&
1441 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1442 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1447 if (hapd->iconf->fragm_threshold > -1 &&
1448 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1449 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1450 "for kernel driver");
1454 prev_addr = hapd->own_addr;
1456 for (j = 0; j < iface->num_bss; j++) {
1457 hapd = iface->bss[j];
1459 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1460 if (hostapd_setup_bss(hapd, j == 0)) {
1462 hapd = iface->bss[j];
1463 hostapd_bss_deinit_no_free(hapd);
1464 hostapd_free_hapd_data(hapd);
1468 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1469 prev_addr = hapd->own_addr;
1471 hapd = iface->bss[0];
1473 hostapd_tx_queue_params(iface);
1475 ap_list_init(iface);
1477 hostapd_set_acl(hapd);
1479 if (hostapd_driver_commit(hapd) < 0) {
1480 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1481 "configuration", __func__);
1486 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1487 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1488 * mode), the interface is up only after driver_commit, so initialize
1489 * WPS after driver_commit.
1491 for (j = 0; j < iface->num_bss; j++) {
1492 if (hostapd_init_wps_complete(iface->bss[j]))
1496 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1497 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1498 if (hapd->setup_complete_cb)
1499 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1501 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1502 iface->bss[0]->conf->iface);
1503 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1504 iface->interfaces->terminate_on_error--;
1509 wpa_printf(MSG_ERROR, "Interface initialization failed");
1510 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1511 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1512 if (iface->interfaces && iface->interfaces->terminate_on_error)
1519 * hostapd_setup_interface - Setup of an interface
1520 * @iface: Pointer to interface data.
1521 * Returns: 0 on success, -1 on failure
1523 * Initializes the driver interface, validates the configuration,
1524 * and sets driver parameters based on the configuration.
1525 * Flushes old stations, sets the channel, encryption,
1526 * beacons, and WDS links based on the configuration.
1528 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1529 * or DFS operations, this function returns 0 before such operations have been
1530 * completed. The pending operations are registered into eloop and will be
1531 * completed from eloop callbacks. Those callbacks end up calling
1532 * hostapd_setup_interface_complete() once setup has been completed.
1534 int hostapd_setup_interface(struct hostapd_iface *iface)
1538 ret = setup_interface(iface);
1540 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1541 iface->bss[0]->conf->iface);
1550 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1551 * @hapd_iface: Pointer to interface data
1552 * @conf: Pointer to per-interface configuration
1553 * @bss: Pointer to per-BSS configuration for this BSS
1554 * Returns: Pointer to allocated BSS data
1556 * This function is used to allocate per-BSS data structure. This data will be
1557 * freed after hostapd_cleanup() is called for it during interface
1560 struct hostapd_data *
1561 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1562 struct hostapd_config *conf,
1563 struct hostapd_bss_config *bss)
1565 struct hostapd_data *hapd;
1567 hapd = os_zalloc(sizeof(*hapd));
1571 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1574 hapd->iface = hapd_iface;
1575 hapd->driver = hapd->iconf->driver;
1576 hapd->ctrl_sock = -1;
1582 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1584 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1586 hostapd_bss_deinit_no_free(hapd);
1587 hostapd_cleanup(hapd);
1591 void hostapd_interface_deinit(struct hostapd_iface *iface)
1595 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1599 #ifdef CONFIG_IEEE80211N
1601 hostapd_stop_setup_timers(iface);
1602 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1603 #endif /* NEED_AP_MLME */
1604 #endif /* CONFIG_IEEE80211N */
1605 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1606 iface->wait_channel_update = 0;
1608 for (j = iface->num_bss - 1; j >= 0; j--)
1609 hostapd_bss_deinit(iface->bss[j]);
1613 void hostapd_interface_free(struct hostapd_iface *iface)
1616 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1617 for (j = 0; j < iface->num_bss; j++) {
1618 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1619 __func__, iface->bss[j]);
1620 os_free(iface->bss[j]);
1622 hostapd_cleanup_iface(iface);
1627 * hostapd_init - Allocate and initialize per-interface data
1628 * @config_file: Path to the configuration file
1629 * Returns: Pointer to the allocated interface data or %NULL on failure
1631 * This function is used to allocate main data structures for per-interface
1632 * data. The allocated data buffer will be freed by calling
1633 * hostapd_cleanup_iface().
1635 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1636 const char *config_file)
1638 struct hostapd_iface *hapd_iface = NULL;
1639 struct hostapd_config *conf = NULL;
1640 struct hostapd_data *hapd;
1643 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1644 if (hapd_iface == NULL)
1647 hapd_iface->config_fname = os_strdup(config_file);
1648 if (hapd_iface->config_fname == NULL)
1651 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1654 hapd_iface->conf = conf;
1656 hapd_iface->num_bss = conf->num_bss;
1657 hapd_iface->bss = os_calloc(conf->num_bss,
1658 sizeof(struct hostapd_data *));
1659 if (hapd_iface->bss == NULL)
1662 for (i = 0; i < conf->num_bss; i++) {
1663 hapd = hapd_iface->bss[i] =
1664 hostapd_alloc_bss_data(hapd_iface, conf,
1668 hapd->msg_ctx = hapd;
1674 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1677 hostapd_config_free(conf);
1679 os_free(hapd_iface->config_fname);
1680 os_free(hapd_iface->bss);
1681 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1682 __func__, hapd_iface);
1683 os_free(hapd_iface);
1689 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1693 for (i = 0; i < interfaces->count; i++) {
1694 struct hostapd_iface *iface = interfaces->iface[i];
1695 for (j = 0; j < iface->num_bss; j++) {
1696 struct hostapd_data *hapd = iface->bss[j];
1697 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1707 * hostapd_interface_init_bss - Read configuration file and init BSS data
1709 * This function is used to parse configuration file for a BSS. This BSS is
1710 * added to an existing interface sharing the same radio (if any) or a new
1711 * interface is created if this is the first interface on a radio. This
1712 * allocate memory for the BSS. No actual driver operations are started.
1714 * This is similar to hostapd_interface_init(), but for a case where the
1715 * configuration is used to add a single BSS instead of all BSSes for a radio.
1717 struct hostapd_iface *
1718 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1719 const char *config_fname, int debug)
1721 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1722 struct hostapd_data *hapd;
1729 for (i = 0; i < interfaces->count; i++) {
1730 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1731 iface = interfaces->iface[i];
1736 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1737 config_fname, phy, iface ? "" : " --> new PHY");
1739 struct hostapd_config *conf;
1740 struct hostapd_bss_config **tmp_conf;
1741 struct hostapd_data **tmp_bss;
1742 struct hostapd_bss_config *bss;
1745 /* Add new BSS to existing iface */
1746 conf = interfaces->config_read_cb(config_fname);
1749 if (conf->num_bss > 1) {
1750 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1751 hostapd_config_free(conf);
1755 ifname = conf->bss[0]->iface;
1756 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1757 wpa_printf(MSG_ERROR,
1758 "Interface name %s already in use", ifname);
1759 hostapd_config_free(conf);
1763 tmp_conf = os_realloc_array(
1764 iface->conf->bss, iface->conf->num_bss + 1,
1765 sizeof(struct hostapd_bss_config *));
1766 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1767 sizeof(struct hostapd_data *));
1769 iface->bss = tmp_bss;
1771 iface->conf->bss = tmp_conf;
1772 iface->conf->last_bss = tmp_conf[0];
1774 if (tmp_bss == NULL || tmp_conf == NULL) {
1775 hostapd_config_free(conf);
1778 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1779 iface->conf->num_bss++;
1781 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1783 iface->conf->num_bss--;
1784 hostapd_config_free(conf);
1787 iface->conf->last_bss = bss;
1788 iface->bss[iface->num_bss] = hapd;
1789 hapd->msg_ctx = hapd;
1791 bss_idx = iface->num_bss++;
1793 conf->bss[0] = NULL;
1794 hostapd_config_free(conf);
1796 /* Add a new iface with the first BSS */
1797 new_iface = iface = hostapd_init(interfaces, config_fname);
1800 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1801 iface->interfaces = interfaces;
1805 for (k = 0; k < debug; k++) {
1806 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1807 iface->bss[bss_idx]->conf->logger_stdout_level--;
1810 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1811 !hostapd_drv_none(iface->bss[bss_idx])) {
1812 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1815 hostapd_interface_deinit_free(new_iface);
1823 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1825 const struct wpa_driver_ops *driver;
1828 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1831 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1832 __func__, (unsigned int) iface->num_bss,
1833 (unsigned int) iface->conf->num_bss);
1834 driver = iface->bss[0]->driver;
1835 drv_priv = iface->bss[0]->drv_priv;
1836 hostapd_interface_deinit(iface);
1837 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1838 __func__, driver, drv_priv);
1839 if (driver && driver->hapd_deinit && drv_priv) {
1840 driver->hapd_deinit(drv_priv);
1841 iface->bss[0]->drv_priv = NULL;
1843 hostapd_interface_free(iface);
1847 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1849 struct hostapd_iface *hapd_iface)
1853 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1854 __func__, driver, drv_priv);
1855 if (driver && driver->hapd_deinit && drv_priv) {
1856 driver->hapd_deinit(drv_priv);
1857 for (j = 0; j < hapd_iface->num_bss; j++) {
1858 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1860 hapd_iface->bss[j]->drv_priv);
1861 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1862 hapd_iface->bss[j]->drv_priv = NULL;
1868 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1872 if (hapd_iface->bss[0]->drv_priv != NULL) {
1873 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1874 hapd_iface->conf->bss[0]->iface);
1878 wpa_printf(MSG_DEBUG, "Enable interface %s",
1879 hapd_iface->conf->bss[0]->iface);
1881 for (j = 0; j < hapd_iface->num_bss; j++)
1882 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1883 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1884 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1888 if (hapd_iface->interfaces == NULL ||
1889 hapd_iface->interfaces->driver_init == NULL ||
1890 hapd_iface->interfaces->driver_init(hapd_iface))
1893 if (hostapd_setup_interface(hapd_iface)) {
1894 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1895 hapd_iface->bss[0]->drv_priv,
1904 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1908 wpa_printf(MSG_DEBUG, "Reload interface %s",
1909 hapd_iface->conf->bss[0]->iface);
1910 for (j = 0; j < hapd_iface->num_bss; j++)
1911 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1912 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1913 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1916 hostapd_clear_old(hapd_iface);
1917 for (j = 0; j < hapd_iface->num_bss; j++)
1918 hostapd_reload_bss(hapd_iface->bss[j]);
1924 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1927 const struct wpa_driver_ops *driver;
1930 if (hapd_iface == NULL)
1933 if (hapd_iface->bss[0]->drv_priv == NULL) {
1934 wpa_printf(MSG_INFO, "Interface %s already disabled",
1935 hapd_iface->conf->bss[0]->iface);
1939 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1940 driver = hapd_iface->bss[0]->driver;
1941 drv_priv = hapd_iface->bss[0]->drv_priv;
1943 hapd_iface->driver_ap_teardown =
1944 !!(hapd_iface->drv_flags &
1945 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1947 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1948 for (j = 0; j < hapd_iface->num_bss; j++) {
1949 struct hostapd_data *hapd = hapd_iface->bss[j];
1950 hostapd_bss_deinit_no_free(hapd);
1951 hostapd_free_hapd_data(hapd);
1954 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
1956 /* From hostapd_cleanup_iface: These were initialized in
1957 * hostapd_setup_interface and hostapd_setup_interface_complete
1959 hostapd_cleanup_iface_partial(hapd_iface);
1961 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1962 hapd_iface->bss[0]->conf->iface);
1963 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1968 static struct hostapd_iface *
1969 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1971 struct hostapd_iface **iface, *hapd_iface;
1973 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1974 sizeof(struct hostapd_iface *));
1977 interfaces->iface = iface;
1978 hapd_iface = interfaces->iface[interfaces->count] =
1979 os_zalloc(sizeof(*hapd_iface));
1980 if (hapd_iface == NULL) {
1981 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1982 "the interface", __func__);
1985 interfaces->count++;
1986 hapd_iface->interfaces = interfaces;
1992 static struct hostapd_config *
1993 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1994 const char *ctrl_iface)
1996 struct hostapd_bss_config *bss;
1997 struct hostapd_config *conf;
1999 /* Allocates memory for bss and conf */
2000 conf = hostapd_config_defaults();
2002 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2003 "configuration", __func__);
2007 conf->driver = wpa_drivers[0];
2008 if (conf->driver == NULL) {
2009 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2010 hostapd_config_free(conf);
2014 bss = conf->last_bss = conf->bss[0];
2016 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2017 bss->ctrl_interface = os_strdup(ctrl_iface);
2018 if (bss->ctrl_interface == NULL) {
2019 hostapd_config_free(conf);
2023 /* Reading configuration file skipped, will be done in SET!
2024 * From reading the configuration till the end has to be done in
2031 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2032 struct hostapd_config *conf)
2035 struct hostapd_data *hapd;
2037 hapd_iface->bss = os_calloc(conf->num_bss,
2038 sizeof(struct hostapd_data *));
2039 if (hapd_iface->bss == NULL)
2042 for (i = 0; i < conf->num_bss; i++) {
2043 hapd = hapd_iface->bss[i] =
2044 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2048 os_free(hapd_iface->bss[i]);
2049 hapd_iface->bss[i] = NULL;
2051 os_free(hapd_iface->bss);
2052 hapd_iface->bss = NULL;
2055 hapd->msg_ctx = hapd;
2058 hapd_iface->conf = conf;
2059 hapd_iface->num_bss = conf->num_bss;
2065 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2067 struct hostapd_config *conf = NULL;
2068 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2069 struct hostapd_data *hapd;
2072 const char *conf_file = NULL, *phy_name = NULL;
2074 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2076 phy_name = buf + 11;
2077 pos = os_strchr(phy_name, ':');
2082 if (!os_strlen(conf_file))
2085 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2089 for (j = 0; j < interfaces->count; j++) {
2090 if (interfaces->iface[j] == hapd_iface)
2093 if (j == interfaces->count) {
2094 struct hostapd_iface **tmp;
2095 tmp = os_realloc_array(interfaces->iface,
2096 interfaces->count + 1,
2097 sizeof(struct hostapd_iface *));
2099 hostapd_interface_deinit_free(hapd_iface);
2102 interfaces->iface = tmp;
2103 interfaces->iface[interfaces->count++] = hapd_iface;
2104 new_iface = hapd_iface;
2108 if (interfaces->driver_init(hapd_iface))
2111 if (hostapd_setup_interface(hapd_iface)) {
2112 hostapd_deinit_driver(
2113 hapd_iface->bss[0]->driver,
2114 hapd_iface->bss[0]->drv_priv,
2119 /* Assign new BSS with bss[0]'s driver info */
2120 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2121 hapd->driver = hapd_iface->bss[0]->driver;
2122 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2123 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2126 if (start_ctrl_iface_bss(hapd) < 0 ||
2127 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2128 hostapd_setup_bss(hapd, -1))) {
2129 hostapd_cleanup(hapd);
2130 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2131 hapd_iface->conf->num_bss--;
2132 hapd_iface->num_bss--;
2133 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2134 __func__, hapd, hapd->conf->iface);
2135 hostapd_config_free_bss(hapd->conf);
2144 ptr = os_strchr(buf, ' ');
2149 if (os_strncmp(ptr, "config=", 7) == 0)
2150 conf_file = ptr + 7;
2152 for (i = 0; i < interfaces->count; i++) {
2153 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2155 wpa_printf(MSG_INFO, "Cannot add interface - it "
2161 hapd_iface = hostapd_iface_alloc(interfaces);
2162 if (hapd_iface == NULL) {
2163 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2164 "for interface", __func__);
2167 new_iface = hapd_iface;
2169 if (conf_file && interfaces->config_read_cb) {
2170 conf = interfaces->config_read_cb(conf_file);
2171 if (conf && conf->bss)
2172 os_strlcpy(conf->bss[0]->iface, buf,
2173 sizeof(conf->bss[0]->iface));
2175 conf = hostapd_config_alloc(interfaces, buf, ptr);
2176 if (conf == NULL || conf->bss == NULL) {
2177 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2178 "for configuration", __func__);
2182 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2183 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2184 "for hostapd", __func__);
2189 if (start_ctrl_iface(hapd_iface) < 0)
2192 wpa_printf(MSG_INFO, "Add interface '%s'",
2193 hapd_iface->conf->bss[0]->iface);
2199 hostapd_config_free(conf);
2201 if (hapd_iface->bss) {
2202 for (i = 0; i < hapd_iface->num_bss; i++) {
2203 hapd = hapd_iface->bss[i];
2206 if (hapd_iface->interfaces &&
2207 hapd_iface->interfaces->ctrl_iface_deinit)
2208 hapd_iface->interfaces->
2209 ctrl_iface_deinit(hapd);
2210 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2211 __func__, hapd_iface->bss[i],
2213 hostapd_cleanup(hapd);
2215 hapd_iface->bss[i] = NULL;
2217 os_free(hapd_iface->bss);
2218 hapd_iface->bss = NULL;
2221 interfaces->count--;
2222 interfaces->iface[interfaces->count] = NULL;
2224 hostapd_cleanup_iface(hapd_iface);
2230 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2234 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2236 /* Remove hostapd_data only if it has already been initialized */
2237 if (idx < iface->num_bss) {
2238 struct hostapd_data *hapd = iface->bss[idx];
2240 hostapd_bss_deinit(hapd);
2241 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2242 __func__, hapd, hapd->conf->iface);
2243 hostapd_config_free_bss(hapd->conf);
2249 for (i = idx; i < iface->num_bss; i++)
2250 iface->bss[i] = iface->bss[i + 1];
2252 hostapd_config_free_bss(iface->conf->bss[idx]);
2253 iface->conf->bss[idx] = NULL;
2256 iface->conf->num_bss--;
2257 for (i = idx; i < iface->conf->num_bss; i++)
2258 iface->conf->bss[i] = iface->conf->bss[i + 1];
2264 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2266 struct hostapd_iface *hapd_iface;
2269 for (i = 0; i < interfaces->count; i++) {
2270 hapd_iface = interfaces->iface[i];
2271 if (hapd_iface == NULL)
2273 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2274 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2275 hapd_iface->driver_ap_teardown =
2276 !!(hapd_iface->drv_flags &
2277 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2279 hostapd_interface_deinit_free(hapd_iface);
2281 while (k < (interfaces->count - 1)) {
2282 interfaces->iface[k] =
2283 interfaces->iface[k + 1];
2286 interfaces->count--;
2290 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2291 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2292 hapd_iface->driver_ap_teardown =
2293 !(hapd_iface->drv_flags &
2294 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2295 return hostapd_remove_bss(hapd_iface, j);
2304 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2305 * @hapd: Pointer to BSS data
2306 * @sta: Pointer to the associated STA data
2307 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2309 * This function will be called whenever a station associates with the AP. It
2310 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2311 * from drv_callbacks.c based on driver events for drivers that take care of
2312 * management frames (IEEE 802.11 authentication and association) internally.
2314 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2317 if (hapd->tkip_countermeasures) {
2318 hostapd_drv_sta_deauth(hapd, sta->addr,
2319 WLAN_REASON_MICHAEL_MIC_FAILURE);
2323 hostapd_prune_associations(hapd, sta->addr);
2325 /* IEEE 802.11F (IAPP) */
2326 if (hapd->conf->ieee802_11f)
2327 iapp_new_station(hapd->iapp, sta);
2330 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2331 sta->no_p2p_set = 1;
2332 hapd->num_sta_no_p2p++;
2333 if (hapd->num_sta_no_p2p == 1)
2334 hostapd_p2p_non_p2p_sta_connected(hapd);
2336 #endif /* CONFIG_P2P */
2338 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2339 * IEEE 802.1X/WPA code will start accounting after the station has
2340 * been authorized. */
2341 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2342 ap_sta_set_authorized(hapd, sta, 1);
2343 os_get_reltime(&sta->connected_time);
2344 accounting_sta_start(hapd, sta);
2347 /* Start IEEE 802.1X authentication process for new stations */
2348 ieee802_1x_new_station(hapd, sta);
2350 if (sta->auth_alg != WLAN_AUTH_FT &&
2351 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2352 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2354 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2356 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2357 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2358 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2359 __func__, MAC2STR(sta->addr),
2360 hapd->conf->ap_max_inactivity);
2361 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2362 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2363 ap_handle_timer, hapd, sta);
2368 const char * hostapd_state_text(enum hostapd_iface_state s)
2371 case HAPD_IFACE_UNINITIALIZED:
2372 return "UNINITIALIZED";
2373 case HAPD_IFACE_DISABLED:
2375 case HAPD_IFACE_COUNTRY_UPDATE:
2376 return "COUNTRY_UPDATE";
2377 case HAPD_IFACE_ACS:
2379 case HAPD_IFACE_HT_SCAN:
2381 case HAPD_IFACE_DFS:
2383 case HAPD_IFACE_ENABLED:
2391 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2393 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2394 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2395 hostapd_state_text(s));
2402 static void free_beacon_data(struct beacon_data *beacon)
2404 os_free(beacon->head);
2405 beacon->head = NULL;
2406 os_free(beacon->tail);
2407 beacon->tail = NULL;
2408 os_free(beacon->probe_resp);
2409 beacon->probe_resp = NULL;
2410 os_free(beacon->beacon_ies);
2411 beacon->beacon_ies = NULL;
2412 os_free(beacon->proberesp_ies);
2413 beacon->proberesp_ies = NULL;
2414 os_free(beacon->assocresp_ies);
2415 beacon->assocresp_ies = NULL;
2419 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2420 struct beacon_data *beacon)
2422 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2423 struct wpa_driver_ap_params params;
2426 os_memset(beacon, 0, sizeof(*beacon));
2427 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2431 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2435 goto free_ap_params;
2438 beacon->head = os_malloc(params.head_len);
2440 goto free_ap_extra_ies;
2442 os_memcpy(beacon->head, params.head, params.head_len);
2443 beacon->head_len = params.head_len;
2445 beacon->tail = os_malloc(params.tail_len);
2449 os_memcpy(beacon->tail, params.tail, params.tail_len);
2450 beacon->tail_len = params.tail_len;
2452 if (params.proberesp != NULL) {
2453 beacon->probe_resp = os_malloc(params.proberesp_len);
2454 if (!beacon->probe_resp)
2457 os_memcpy(beacon->probe_resp, params.proberesp,
2458 params.proberesp_len);
2459 beacon->probe_resp_len = params.proberesp_len;
2462 /* copy the extra ies */
2464 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2465 if (!beacon->beacon_ies)
2468 os_memcpy(beacon->beacon_ies,
2469 beacon_extra->buf, wpabuf_len(beacon_extra));
2470 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2473 if (proberesp_extra) {
2474 beacon->proberesp_ies =
2475 os_malloc(wpabuf_len(proberesp_extra));
2476 if (!beacon->proberesp_ies)
2479 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2480 wpabuf_len(proberesp_extra));
2481 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2484 if (assocresp_extra) {
2485 beacon->assocresp_ies =
2486 os_malloc(wpabuf_len(assocresp_extra));
2487 if (!beacon->assocresp_ies)
2490 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2491 wpabuf_len(assocresp_extra));
2492 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2497 /* if the function fails, the caller should not free beacon data */
2499 free_beacon_data(beacon);
2502 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2505 ieee802_11_free_ap_params(¶ms);
2511 * TODO: This flow currently supports only changing frequency within the
2512 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2513 * width) are not supported.
2515 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2516 struct hostapd_config *conf,
2517 struct hostapd_freq_params *params,
2518 struct hostapd_freq_params *old_params)
2522 if (!params->channel) {
2523 /* check if the new channel is supported by hw */
2524 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2527 channel = params->channel;
2531 /* if a pointer to old_params is provided we save previous state */
2533 old_params->channel = conf->channel;
2534 old_params->ht_enabled = conf->ieee80211n;
2535 old_params->sec_channel_offset = conf->secondary_channel;
2538 conf->channel = channel;
2539 conf->ieee80211n = params->ht_enabled;
2540 conf->secondary_channel = params->sec_channel_offset;
2542 /* TODO: maybe call here hostapd_config_check here? */
2548 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2549 struct csa_settings *settings)
2551 struct hostapd_iface *iface = hapd->iface;
2552 struct hostapd_freq_params old_freq;
2555 os_memset(&old_freq, 0, sizeof(old_freq));
2556 if (!iface || !iface->freq || hapd->csa_in_progress)
2559 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2560 &settings->freq_params,
2565 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2567 /* change back the configuration */
2568 hostapd_change_config_freq(iface->bss[0], iface->conf,
2574 /* set channel switch parameters for csa ie */
2575 hapd->cs_freq_params = settings->freq_params;
2576 hapd->cs_count = settings->cs_count;
2577 hapd->cs_block_tx = settings->block_tx;
2579 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2581 free_beacon_data(&settings->beacon_after);
2585 settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2586 settings->counter_offset_presp = hapd->cs_c_off_proberesp;
2592 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2594 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2596 hapd->cs_block_tx = 0;
2597 hapd->cs_c_off_beacon = 0;
2598 hapd->cs_c_off_proberesp = 0;
2599 hapd->csa_in_progress = 0;
2603 int hostapd_switch_channel(struct hostapd_data *hapd,
2604 struct csa_settings *settings)
2608 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2609 wpa_printf(MSG_INFO, "CSA is not supported");
2613 ret = hostapd_fill_csa_settings(hapd, settings);
2617 ret = hostapd_drv_switch_channel(hapd, settings);
2618 free_beacon_data(&settings->beacon_csa);
2619 free_beacon_data(&settings->beacon_after);
2622 /* if we failed, clean cs parameters */
2623 hostapd_cleanup_cs_params(hapd);
2627 hapd->csa_in_progress = 1;
2633 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2634 const struct hostapd_freq_params *freq_params)
2636 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2639 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2641 if (freq_params->center_freq1)
2642 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2643 if (freq_params->center_freq2)
2644 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2646 switch (freq_params->bandwidth) {
2650 vht_bw = VHT_CHANWIDTH_USE_HT;
2653 if (freq_params->center_freq2)
2654 vht_bw = VHT_CHANWIDTH_80P80MHZ;
2656 vht_bw = VHT_CHANWIDTH_80MHZ;
2659 vht_bw = VHT_CHANWIDTH_160MHZ;
2662 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2663 freq_params->bandwidth);
2667 iface->freq = freq_params->freq;
2668 iface->conf->channel = freq_params->channel;
2669 iface->conf->secondary_channel = freq_params->sec_channel_offset;
2670 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2671 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2672 iface->conf->vht_oper_chwidth = vht_bw;
2673 iface->conf->ieee80211n = freq_params->ht_enabled;
2674 iface->conf->ieee80211ac = freq_params->vht_enabled;
2677 * cs_params must not be cleared earlier because the freq_params
2678 * argument may actually point to one of these.
2680 for (i = 0; i < iface->num_bss; i++)
2681 hostapd_cleanup_cs_params(iface->bss[i]);
2683 hostapd_disable_iface(iface);
2684 hostapd_enable_iface(iface);
2687 #endif /* NEED_AP_MLME */