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 #ifdef CONFIG_IEEE80211N
362 hostapd_stop_setup_timers(iface);
363 #endif /* NEED_AP_MLME */
364 #endif /* CONFIG_IEEE80211N */
365 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
366 iface->hw_features = NULL;
367 os_free(iface->current_rates);
368 iface->current_rates = NULL;
369 os_free(iface->basic_rates);
370 iface->basic_rates = NULL;
371 ap_list_deinit(iface);
376 * hostapd_cleanup_iface - Complete per-interface cleanup
377 * @iface: Pointer to interface data
379 * This function is called after per-BSS data structures are deinitialized
380 * with hostapd_cleanup().
382 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
384 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
385 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
387 hostapd_cleanup_iface_partial(iface);
388 hostapd_config_free(iface->conf);
391 os_free(iface->config_fname);
393 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
398 static void hostapd_clear_wep(struct hostapd_data *hapd)
400 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
401 hostapd_set_privacy(hapd, 0);
402 hostapd_broadcast_wep_clear(hapd);
407 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
411 hostapd_broadcast_wep_set(hapd);
413 if (hapd->conf->ssid.wep.default_len) {
414 hostapd_set_privacy(hapd, 1);
419 * When IEEE 802.1X is not enabled, the driver may need to know how to
420 * set authentication algorithms for static WEP.
422 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
424 for (i = 0; i < 4; i++) {
425 if (hapd->conf->ssid.wep.key[i] &&
426 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
427 i == hapd->conf->ssid.wep.idx, NULL, 0,
428 hapd->conf->ssid.wep.key[i],
429 hapd->conf->ssid.wep.len[i])) {
430 wpa_printf(MSG_WARNING, "Could not set WEP "
434 if (hapd->conf->ssid.wep.key[i] &&
435 i == hapd->conf->ssid.wep.idx)
436 hostapd_set_privacy(hapd, 1);
443 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
448 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
451 if (!hapd->iface->driver_ap_teardown) {
452 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
453 "Flushing old station entries");
455 if (hostapd_flush(hapd)) {
456 wpa_msg(hapd->msg_ctx, MSG_WARNING,
457 "Could not connect to kernel driver");
461 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
462 os_memset(addr, 0xff, ETH_ALEN);
463 hostapd_drv_sta_deauth(hapd, addr, reason);
464 hostapd_free_stas(hapd);
470 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
472 hostapd_free_stas(hapd);
473 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
474 hostapd_clear_wep(hapd);
479 * hostapd_validate_bssid_configuration - Validate BSSID configuration
480 * @iface: Pointer to interface data
481 * Returns: 0 on success, -1 on failure
483 * This function is used to validate that the configured BSSIDs are valid.
485 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
487 u8 mask[ETH_ALEN] = { 0 };
488 struct hostapd_data *hapd = iface->bss[0];
489 unsigned int i = iface->conf->num_bss, bits = 0, j;
492 if (hostapd_drv_none(hapd))
495 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
497 /* Determine the bits necessary to cover the number of BSSIDs. */
498 for (i--; i; i >>= 1)
501 /* Determine the bits necessary to any configured BSSIDs,
502 if they are higher than the number of BSSIDs. */
503 for (j = 0; j < iface->conf->num_bss; j++) {
504 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
510 for (i = 0; i < ETH_ALEN; i++) {
512 iface->conf->bss[j]->bssid[i] ^
520 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
526 while (mask[i] != 0) {
536 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
541 os_memset(mask, 0xff, ETH_ALEN);
543 for (i = 5; i > 5 - j; i--)
550 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
551 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
556 for (i = 0; i < ETH_ALEN; i++) {
557 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
558 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
559 " for start address " MACSTR ".",
560 MAC2STR(mask), MAC2STR(hapd->own_addr));
561 wpa_printf(MSG_ERROR, "Start address must be the "
562 "first address in the block (i.e., addr "
563 "AND mask == addr).");
572 static int mac_in_conf(struct hostapd_config *conf, const void *a)
576 for (i = 0; i < conf->num_bss; i++) {
577 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
586 #ifndef CONFIG_NO_RADIUS
588 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
589 struct radius_das_attrs *attr)
591 if (attr->nas_identifier &&
592 (!hapd->conf->nas_identifier ||
593 os_strlen(hapd->conf->nas_identifier) !=
594 attr->nas_identifier_len ||
595 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
596 attr->nas_identifier_len) != 0)) {
597 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
601 if (attr->nas_ip_addr &&
602 (hapd->conf->own_ip_addr.af != AF_INET ||
603 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
605 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
610 if (attr->nas_ipv6_addr &&
611 (hapd->conf->own_ip_addr.af != AF_INET6 ||
612 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
614 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
617 #endif /* CONFIG_IPV6 */
623 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
624 struct radius_das_attrs *attr,
627 struct sta_info *selected, *sta;
634 for (sta = hapd->sta_list; sta; sta = sta->next)
635 sta->radius_das_match = 1;
637 if (attr->sta_addr) {
639 sta = ap_get_sta(hapd, attr->sta_addr);
641 wpa_printf(MSG_DEBUG,
642 "RADIUS DAS: No Calling-Station-Id match");
647 for (sta = hapd->sta_list; sta; sta = sta->next) {
649 sta->radius_das_match = 0;
651 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
654 if (attr->acct_session_id) {
656 if (attr->acct_session_id_len != 17) {
657 wpa_printf(MSG_DEBUG,
658 "RADIUS DAS: Acct-Session-Id cannot match");
663 for (sta = hapd->sta_list; sta; sta = sta->next) {
664 if (!sta->radius_das_match)
666 os_snprintf(buf, sizeof(buf), "%08X-%08X",
667 sta->acct_session_id_hi,
668 sta->acct_session_id_lo);
669 if (os_memcmp(attr->acct_session_id, buf, 17) != 0)
670 sta->radius_das_match = 0;
676 wpa_printf(MSG_DEBUG,
677 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
680 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
683 if (attr->acct_multi_session_id) {
685 if (attr->acct_multi_session_id_len != 17) {
686 wpa_printf(MSG_DEBUG,
687 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
692 for (sta = hapd->sta_list; sta; sta = sta->next) {
693 if (!sta->radius_das_match)
695 if (!sta->eapol_sm ||
696 !sta->eapol_sm->acct_multi_session_id_hi) {
697 sta->radius_das_match = 0;
700 os_snprintf(buf, sizeof(buf), "%08X+%08X",
701 sta->eapol_sm->acct_multi_session_id_hi,
702 sta->eapol_sm->acct_multi_session_id_lo);
703 if (os_memcmp(attr->acct_multi_session_id, buf, 17) !=
705 sta->radius_das_match = 0;
711 wpa_printf(MSG_DEBUG,
712 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
715 wpa_printf(MSG_DEBUG,
716 "RADIUS DAS: Acct-Multi-Session-Id match");
723 for (sta = hapd->sta_list; sta; sta = sta->next) {
726 if (!sta->radius_das_match)
728 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
729 if (!cui || wpabuf_len(cui) != attr->cui_len ||
730 os_memcmp(wpabuf_head(cui), attr->cui,
732 sta->radius_das_match = 0;
738 wpa_printf(MSG_DEBUG,
739 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
742 wpa_printf(MSG_DEBUG,
743 "RADIUS DAS: Chargeable-User-Identity match");
746 if (attr->user_name) {
750 for (sta = hapd->sta_list; sta; sta = sta->next) {
754 if (!sta->radius_das_match)
756 identity = ieee802_1x_get_identity(sta->eapol_sm,
759 identity_len != attr->user_name_len ||
760 os_memcmp(identity, attr->user_name, identity_len)
762 sta->radius_das_match = 0;
768 wpa_printf(MSG_DEBUG,
769 "RADIUS DAS: No matches remaining after User-Name check");
772 wpa_printf(MSG_DEBUG,
773 "RADIUS DAS: User-Name match");
778 * In theory, we could match all current associations, but it
779 * seems safer to just reject requests that do not include any
780 * session identification attributes.
782 wpa_printf(MSG_DEBUG,
783 "RADIUS DAS: No session identification attributes included");
788 for (sta = hapd->sta_list; sta; sta = sta->next) {
789 if (sta->radius_das_match) {
802 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
803 struct radius_das_attrs *attr)
807 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
811 static enum radius_das_res
812 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
814 struct hostapd_data *hapd = ctx;
815 struct sta_info *sta;
818 if (hostapd_das_nas_mismatch(hapd, attr))
819 return RADIUS_DAS_NAS_MISMATCH;
821 sta = hostapd_das_find_sta(hapd, attr, &multi);
824 wpa_printf(MSG_DEBUG,
825 "RADIUS DAS: Multiple sessions match - not supported");
826 return RADIUS_DAS_MULTI_SESSION_MATCH;
828 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
829 wpa_printf(MSG_DEBUG,
830 "RADIUS DAS: PMKSA cache entry matched");
831 return RADIUS_DAS_SUCCESS;
833 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
834 return RADIUS_DAS_SESSION_NOT_FOUND;
837 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
838 " - disconnecting", MAC2STR(sta->addr));
839 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
841 hostapd_drv_sta_deauth(hapd, sta->addr,
842 WLAN_REASON_PREV_AUTH_NOT_VALID);
843 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
845 return RADIUS_DAS_SUCCESS;
848 #endif /* CONFIG_NO_RADIUS */
852 * hostapd_setup_bss - Per-BSS setup (initialization)
853 * @hapd: Pointer to BSS data
854 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
855 * but interface may exist
857 * This function is used to initialize all per-BSS data structures and
858 * resources. This gets called in a loop for each BSS when an interface is
859 * initialized. Most of the modules that are initialized here will be
860 * deinitialized in hostapd_cleanup().
862 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
864 struct hostapd_bss_config *conf = hapd->conf;
865 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
866 int ssid_len, set_ssid;
867 char force_ifname[IFNAMSIZ];
868 u8 if_addr[ETH_ALEN];
869 int flush_old_stations = 1;
871 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
872 __func__, hapd, conf->iface, first);
874 #ifdef EAP_SERVER_TNC
875 if (conf->tnc && tncs_global_init() < 0) {
876 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
879 #endif /* EAP_SERVER_TNC */
882 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
883 __func__, conf->iface);
888 if (!first || first == -1) {
889 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
890 /* Allocate the next available BSSID. */
892 inc_byte_array(hapd->own_addr, ETH_ALEN);
893 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
895 /* Allocate the configured BSSID. */
896 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
898 if (hostapd_mac_comp(hapd->own_addr,
899 hapd->iface->bss[0]->own_addr) ==
901 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
902 "BSSID set to the MAC address of "
903 "the radio", conf->iface);
908 hapd->interface_added = 1;
909 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
910 conf->iface, hapd->own_addr, hapd,
911 &hapd->drv_priv, force_ifname, if_addr,
912 conf->bridge[0] ? conf->bridge : NULL,
914 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
915 MACSTR ")", MAC2STR(hapd->own_addr));
916 hapd->interface_added = 0;
921 if (conf->wmm_enabled < 0)
922 conf->wmm_enabled = hapd->iconf->ieee80211n;
925 if (hapd->iface->mconf == NULL)
926 flush_old_stations = 0;
927 #endif /* CONFIG_MESH */
929 if (flush_old_stations)
930 hostapd_flush_old_stations(hapd,
931 WLAN_REASON_PREV_AUTH_NOT_VALID);
932 hostapd_set_privacy(hapd, 0);
934 hostapd_broadcast_wep_clear(hapd);
935 if (hostapd_setup_encryption(conf->iface, hapd))
939 * Fetch the SSID from the system and use it or,
940 * if one was specified in the config file, verify they
943 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
945 wpa_printf(MSG_ERROR, "Could not read SSID from system");
948 if (conf->ssid.ssid_set) {
950 * If SSID is specified in the config file and it differs
951 * from what is being used then force installation of the
954 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
955 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
958 * No SSID in the config file; just use the one we got
962 conf->ssid.ssid_len = ssid_len;
963 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
966 if (!hostapd_drv_none(hapd)) {
967 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
969 conf->iface, MAC2STR(hapd->own_addr),
970 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
973 if (hostapd_setup_wpa_psk(conf)) {
974 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
978 /* Set SSID for the kernel driver (to be used in beacon and probe
979 * response frames) */
980 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
981 conf->ssid.ssid_len)) {
982 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
986 if (wpa_debug_level <= MSG_MSGDUMP)
987 conf->radius->msg_dumps = 1;
988 #ifndef CONFIG_NO_RADIUS
989 hapd->radius = radius_client_init(hapd, conf->radius);
990 if (hapd->radius == NULL) {
991 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
995 if (conf->radius_das_port) {
996 struct radius_das_conf das_conf;
997 os_memset(&das_conf, 0, sizeof(das_conf));
998 das_conf.port = conf->radius_das_port;
999 das_conf.shared_secret = conf->radius_das_shared_secret;
1000 das_conf.shared_secret_len =
1001 conf->radius_das_shared_secret_len;
1002 das_conf.client_addr = &conf->radius_das_client_addr;
1003 das_conf.time_window = conf->radius_das_time_window;
1004 das_conf.require_event_timestamp =
1005 conf->radius_das_require_event_timestamp;
1006 das_conf.ctx = hapd;
1007 das_conf.disconnect = hostapd_das_disconnect;
1008 hapd->radius_das = radius_das_init(&das_conf);
1009 if (hapd->radius_das == NULL) {
1010 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1015 #endif /* CONFIG_NO_RADIUS */
1017 if (hostapd_acl_init(hapd)) {
1018 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1021 if (hostapd_init_wps(hapd, conf))
1024 if (authsrv_init(hapd) < 0)
1027 if (ieee802_1x_init(hapd)) {
1028 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1032 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1035 if (accounting_init(hapd)) {
1036 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1040 if (conf->ieee802_11f &&
1041 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1042 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1047 #ifdef CONFIG_INTERWORKING
1048 if (gas_serv_init(hapd)) {
1049 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1053 if (conf->qos_map_set_len &&
1054 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1055 conf->qos_map_set_len)) {
1056 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1059 #endif /* CONFIG_INTERWORKING */
1061 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1062 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1066 if (conf->proxy_arp) {
1067 if (x_snoop_init(hapd)) {
1068 wpa_printf(MSG_ERROR,
1069 "Generic snooping infrastructure initialization failed");
1073 if (dhcp_snoop_init(hapd)) {
1074 wpa_printf(MSG_ERROR,
1075 "DHCP snooping initialization failed");
1079 if (ndisc_snoop_init(hapd)) {
1080 wpa_printf(MSG_ERROR,
1081 "Neighbor Discovery snooping initialization failed");
1086 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1087 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1091 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1094 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1097 if (hapd->driver && hapd->driver->set_operstate)
1098 hapd->driver->set_operstate(hapd->drv_priv, 1);
1104 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1106 struct hostapd_data *hapd = iface->bss[0];
1108 struct hostapd_tx_queue_params *p;
1111 if (iface->mconf == NULL)
1113 #endif /* CONFIG_MESH */
1115 for (i = 0; i < NUM_TX_QUEUES; i++) {
1116 p = &iface->conf->tx_queue[i];
1118 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1119 p->cwmax, p->burst)) {
1120 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1121 "parameters for queue %d.", i);
1122 /* Continue anyway */
1128 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1129 struct mac_acl_entry *mac_acl,
1130 int n_entries, u8 accept_acl)
1132 struct hostapd_acl_params *acl_params;
1135 acl_params = os_zalloc(sizeof(*acl_params) +
1136 (n_entries * sizeof(acl_params->mac_acl[0])));
1140 for (i = 0; i < n_entries; i++)
1141 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1144 acl_params->acl_policy = accept_acl;
1145 acl_params->num_mac_acl = n_entries;
1147 err = hostapd_drv_set_acl(hapd, acl_params);
1149 os_free(acl_params);
1155 static void hostapd_set_acl(struct hostapd_data *hapd)
1157 struct hostapd_config *conf = hapd->iconf;
1161 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1164 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1166 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1167 conf->bss[0]->num_accept_mac,
1170 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1173 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1175 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1176 conf->bss[0]->num_deny_mac,
1179 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1186 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1188 if (!hapd->iface->interfaces ||
1189 !hapd->iface->interfaces->ctrl_iface_init)
1192 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1193 wpa_printf(MSG_ERROR,
1194 "Failed to setup control interface for %s",
1203 static int start_ctrl_iface(struct hostapd_iface *iface)
1207 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1210 for (i = 0; i < iface->num_bss; i++) {
1211 struct hostapd_data *hapd = iface->bss[i];
1212 if (iface->interfaces->ctrl_iface_init(hapd)) {
1213 wpa_printf(MSG_ERROR,
1214 "Failed to setup control interface for %s",
1224 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1226 struct hostapd_iface *iface = eloop_ctx;
1228 if (!iface->wait_channel_update) {
1229 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1234 * It is possible that the existing channel list is acceptable, so try
1237 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1238 setup_interface2(iface);
1242 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1244 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1247 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1248 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1249 setup_interface2(iface);
1253 static int setup_interface(struct hostapd_iface *iface)
1255 struct hostapd_data *hapd = iface->bss[0];
1259 * It is possible that setup_interface() is called after the interface
1260 * was disabled etc., in which case driver_ap_teardown is possibly set
1261 * to 1. Clear it here so any other key/station deletion, which is not
1262 * part of a teardown flow, would also call the relevant driver
1265 iface->driver_ap_teardown = 0;
1267 if (!iface->phy[0]) {
1268 const char *phy = hostapd_drv_get_radio_name(hapd);
1270 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1271 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1276 * Make sure that all BSSes get configured with a pointer to the same
1279 for (i = 1; i < iface->num_bss; i++) {
1280 iface->bss[i]->driver = hapd->driver;
1281 iface->bss[i]->drv_priv = hapd->drv_priv;
1284 if (hostapd_validate_bssid_configuration(iface))
1288 * Initialize control interfaces early to allow external monitoring of
1289 * channel setup operations that may take considerable amount of time
1290 * especially for DFS cases.
1292 if (start_ctrl_iface(iface))
1295 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1296 char country[4], previous_country[4];
1298 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1299 if (hostapd_get_country(hapd, previous_country) < 0)
1300 previous_country[0] = '\0';
1302 os_memcpy(country, hapd->iconf->country, 3);
1304 if (hostapd_set_country(hapd, country) < 0) {
1305 wpa_printf(MSG_ERROR, "Failed to set country code");
1309 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1310 previous_country, country);
1312 if (os_strncmp(previous_country, country, 2) != 0) {
1313 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1314 iface->wait_channel_update = 1;
1315 eloop_register_timeout(5, 0,
1316 channel_list_update_timeout,
1322 return setup_interface2(iface);
1326 static int setup_interface2(struct hostapd_iface *iface)
1328 iface->wait_channel_update = 0;
1330 if (hostapd_get_hw_features(iface)) {
1331 /* Not all drivers support this yet, so continue without hw
1334 int ret = hostapd_select_hw_mode(iface);
1336 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1337 "channel. (%d)", ret);
1341 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1344 ret = hostapd_check_ht_capab(iface);
1348 wpa_printf(MSG_DEBUG, "Interface initialization will "
1349 "be completed in a callback");
1353 if (iface->conf->ieee80211h)
1354 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1356 return hostapd_setup_interface_complete(iface, 0);
1359 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1360 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1361 if (iface->interfaces && iface->interfaces->terminate_on_error)
1368 * hostapd_setup_interface_complete - Complete interface setup
1370 * This function is called when previous steps in the interface setup has been
1371 * completed. This can also start operations, e.g., DFS, that will require
1372 * additional processing before interface is ready to be enabled. Such
1373 * operations will call this function from eloop callbacks when finished.
1375 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1377 struct hostapd_data *hapd = iface->bss[0];
1380 int delay_apply_cfg = 0;
1385 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1386 if (iface->conf->channel) {
1389 #endif /* NEED_AP_MLME */
1391 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1392 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1393 "Frequency: %d MHz",
1394 hostapd_hw_mode_txt(iface->conf->hw_mode),
1395 iface->conf->channel, iface->freq);
1398 /* Handle DFS only if it is not offloaded to the driver */
1399 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1401 res = hostapd_handle_dfs(iface);
1408 #endif /* NEED_AP_MLME */
1411 if (iface->mconf != NULL) {
1412 wpa_printf(MSG_DEBUG,
1413 "%s: Mesh configuration will be applied while joining the mesh network",
1414 iface->bss[0]->conf->iface);
1415 delay_apply_cfg = 1;
1417 #endif /* CONFIG_MESH */
1419 if (!delay_apply_cfg &&
1420 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1421 hapd->iconf->channel,
1422 hapd->iconf->ieee80211n,
1423 hapd->iconf->ieee80211ac,
1424 hapd->iconf->secondary_channel,
1425 hapd->iconf->vht_oper_chwidth,
1426 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1427 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1428 wpa_printf(MSG_ERROR, "Could not set channel for "
1434 if (iface->current_mode) {
1435 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1436 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1438 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1439 HOSTAPD_LEVEL_WARNING,
1440 "Failed to prepare rates table.");
1445 if (hapd->iconf->rts_threshold > -1 &&
1446 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1447 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1452 if (hapd->iconf->fragm_threshold > -1 &&
1453 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1454 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1455 "for kernel driver");
1459 prev_addr = hapd->own_addr;
1461 for (j = 0; j < iface->num_bss; j++) {
1462 hapd = iface->bss[j];
1464 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1465 if (hostapd_setup_bss(hapd, j == 0)) {
1467 hapd = iface->bss[j];
1468 hostapd_bss_deinit_no_free(hapd);
1469 hostapd_free_hapd_data(hapd);
1473 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1474 prev_addr = hapd->own_addr;
1476 hapd = iface->bss[0];
1478 hostapd_tx_queue_params(iface);
1480 ap_list_init(iface);
1482 hostapd_set_acl(hapd);
1484 if (hostapd_driver_commit(hapd) < 0) {
1485 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1486 "configuration", __func__);
1491 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1492 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1493 * mode), the interface is up only after driver_commit, so initialize
1494 * WPS after driver_commit.
1496 for (j = 0; j < iface->num_bss; j++) {
1497 if (hostapd_init_wps_complete(iface->bss[j]))
1501 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1502 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1503 if (hapd->setup_complete_cb)
1504 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1506 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1507 iface->bss[0]->conf->iface);
1508 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1509 iface->interfaces->terminate_on_error--;
1514 wpa_printf(MSG_ERROR, "Interface initialization failed");
1515 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1516 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1517 if (iface->interfaces && iface->interfaces->terminate_on_error)
1524 * hostapd_setup_interface - Setup of an interface
1525 * @iface: Pointer to interface data.
1526 * Returns: 0 on success, -1 on failure
1528 * Initializes the driver interface, validates the configuration,
1529 * and sets driver parameters based on the configuration.
1530 * Flushes old stations, sets the channel, encryption,
1531 * beacons, and WDS links based on the configuration.
1533 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1534 * or DFS operations, this function returns 0 before such operations have been
1535 * completed. The pending operations are registered into eloop and will be
1536 * completed from eloop callbacks. Those callbacks end up calling
1537 * hostapd_setup_interface_complete() once setup has been completed.
1539 int hostapd_setup_interface(struct hostapd_iface *iface)
1543 ret = setup_interface(iface);
1545 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1546 iface->bss[0]->conf->iface);
1555 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1556 * @hapd_iface: Pointer to interface data
1557 * @conf: Pointer to per-interface configuration
1558 * @bss: Pointer to per-BSS configuration for this BSS
1559 * Returns: Pointer to allocated BSS data
1561 * This function is used to allocate per-BSS data structure. This data will be
1562 * freed after hostapd_cleanup() is called for it during interface
1565 struct hostapd_data *
1566 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1567 struct hostapd_config *conf,
1568 struct hostapd_bss_config *bss)
1570 struct hostapd_data *hapd;
1572 hapd = os_zalloc(sizeof(*hapd));
1576 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1579 hapd->iface = hapd_iface;
1580 hapd->driver = hapd->iconf->driver;
1581 hapd->ctrl_sock = -1;
1587 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1589 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1591 hostapd_bss_deinit_no_free(hapd);
1592 hostapd_cleanup(hapd);
1596 void hostapd_interface_deinit(struct hostapd_iface *iface)
1600 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1604 #ifdef CONFIG_IEEE80211N
1606 hostapd_stop_setup_timers(iface);
1607 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1608 #endif /* NEED_AP_MLME */
1609 #endif /* CONFIG_IEEE80211N */
1610 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1611 iface->wait_channel_update = 0;
1613 for (j = iface->num_bss - 1; j >= 0; j--)
1614 hostapd_bss_deinit(iface->bss[j]);
1618 void hostapd_interface_free(struct hostapd_iface *iface)
1621 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1622 for (j = 0; j < iface->num_bss; j++) {
1623 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1624 __func__, iface->bss[j]);
1625 os_free(iface->bss[j]);
1627 hostapd_cleanup_iface(iface);
1632 * hostapd_init - Allocate and initialize per-interface data
1633 * @config_file: Path to the configuration file
1634 * Returns: Pointer to the allocated interface data or %NULL on failure
1636 * This function is used to allocate main data structures for per-interface
1637 * data. The allocated data buffer will be freed by calling
1638 * hostapd_cleanup_iface().
1640 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1641 const char *config_file)
1643 struct hostapd_iface *hapd_iface = NULL;
1644 struct hostapd_config *conf = NULL;
1645 struct hostapd_data *hapd;
1648 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1649 if (hapd_iface == NULL)
1652 hapd_iface->config_fname = os_strdup(config_file);
1653 if (hapd_iface->config_fname == NULL)
1656 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1659 hapd_iface->conf = conf;
1661 hapd_iface->num_bss = conf->num_bss;
1662 hapd_iface->bss = os_calloc(conf->num_bss,
1663 sizeof(struct hostapd_data *));
1664 if (hapd_iface->bss == NULL)
1667 for (i = 0; i < conf->num_bss; i++) {
1668 hapd = hapd_iface->bss[i] =
1669 hostapd_alloc_bss_data(hapd_iface, conf,
1673 hapd->msg_ctx = hapd;
1679 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1682 hostapd_config_free(conf);
1684 os_free(hapd_iface->config_fname);
1685 os_free(hapd_iface->bss);
1686 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1687 __func__, hapd_iface);
1688 os_free(hapd_iface);
1694 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1698 for (i = 0; i < interfaces->count; i++) {
1699 struct hostapd_iface *iface = interfaces->iface[i];
1700 for (j = 0; j < iface->num_bss; j++) {
1701 struct hostapd_data *hapd = iface->bss[j];
1702 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1712 * hostapd_interface_init_bss - Read configuration file and init BSS data
1714 * This function is used to parse configuration file for a BSS. This BSS is
1715 * added to an existing interface sharing the same radio (if any) or a new
1716 * interface is created if this is the first interface on a radio. This
1717 * allocate memory for the BSS. No actual driver operations are started.
1719 * This is similar to hostapd_interface_init(), but for a case where the
1720 * configuration is used to add a single BSS instead of all BSSes for a radio.
1722 struct hostapd_iface *
1723 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1724 const char *config_fname, int debug)
1726 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1727 struct hostapd_data *hapd;
1734 for (i = 0; i < interfaces->count; i++) {
1735 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1736 iface = interfaces->iface[i];
1741 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1742 config_fname, phy, iface ? "" : " --> new PHY");
1744 struct hostapd_config *conf;
1745 struct hostapd_bss_config **tmp_conf;
1746 struct hostapd_data **tmp_bss;
1747 struct hostapd_bss_config *bss;
1750 /* Add new BSS to existing iface */
1751 conf = interfaces->config_read_cb(config_fname);
1754 if (conf->num_bss > 1) {
1755 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1756 hostapd_config_free(conf);
1760 ifname = conf->bss[0]->iface;
1761 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1762 wpa_printf(MSG_ERROR,
1763 "Interface name %s already in use", ifname);
1764 hostapd_config_free(conf);
1768 tmp_conf = os_realloc_array(
1769 iface->conf->bss, iface->conf->num_bss + 1,
1770 sizeof(struct hostapd_bss_config *));
1771 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1772 sizeof(struct hostapd_data *));
1774 iface->bss = tmp_bss;
1776 iface->conf->bss = tmp_conf;
1777 iface->conf->last_bss = tmp_conf[0];
1779 if (tmp_bss == NULL || tmp_conf == NULL) {
1780 hostapd_config_free(conf);
1783 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1784 iface->conf->num_bss++;
1786 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1788 iface->conf->num_bss--;
1789 hostapd_config_free(conf);
1792 iface->conf->last_bss = bss;
1793 iface->bss[iface->num_bss] = hapd;
1794 hapd->msg_ctx = hapd;
1796 bss_idx = iface->num_bss++;
1798 conf->bss[0] = NULL;
1799 hostapd_config_free(conf);
1801 /* Add a new iface with the first BSS */
1802 new_iface = iface = hostapd_init(interfaces, config_fname);
1805 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1806 iface->interfaces = interfaces;
1810 for (k = 0; k < debug; k++) {
1811 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1812 iface->bss[bss_idx]->conf->logger_stdout_level--;
1815 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1816 !hostapd_drv_none(iface->bss[bss_idx])) {
1817 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1820 hostapd_interface_deinit_free(new_iface);
1828 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1830 const struct wpa_driver_ops *driver;
1833 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1836 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1837 __func__, (unsigned int) iface->num_bss,
1838 (unsigned int) iface->conf->num_bss);
1839 driver = iface->bss[0]->driver;
1840 drv_priv = iface->bss[0]->drv_priv;
1841 hostapd_interface_deinit(iface);
1842 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1843 __func__, driver, drv_priv);
1844 if (driver && driver->hapd_deinit && drv_priv) {
1845 driver->hapd_deinit(drv_priv);
1846 iface->bss[0]->drv_priv = NULL;
1848 hostapd_interface_free(iface);
1852 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1854 struct hostapd_iface *hapd_iface)
1858 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1859 __func__, driver, drv_priv);
1860 if (driver && driver->hapd_deinit && drv_priv) {
1861 driver->hapd_deinit(drv_priv);
1862 for (j = 0; j < hapd_iface->num_bss; j++) {
1863 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1865 hapd_iface->bss[j]->drv_priv);
1866 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1867 hapd_iface->bss[j]->drv_priv = NULL;
1873 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1877 if (hapd_iface->bss[0]->drv_priv != NULL) {
1878 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1879 hapd_iface->conf->bss[0]->iface);
1883 wpa_printf(MSG_DEBUG, "Enable interface %s",
1884 hapd_iface->conf->bss[0]->iface);
1886 for (j = 0; j < hapd_iface->num_bss; j++)
1887 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1888 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1889 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1893 if (hapd_iface->interfaces == NULL ||
1894 hapd_iface->interfaces->driver_init == NULL ||
1895 hapd_iface->interfaces->driver_init(hapd_iface))
1898 if (hostapd_setup_interface(hapd_iface)) {
1899 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1900 hapd_iface->bss[0]->drv_priv,
1909 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1913 wpa_printf(MSG_DEBUG, "Reload interface %s",
1914 hapd_iface->conf->bss[0]->iface);
1915 for (j = 0; j < hapd_iface->num_bss; j++)
1916 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1917 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1918 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1921 hostapd_clear_old(hapd_iface);
1922 for (j = 0; j < hapd_iface->num_bss; j++)
1923 hostapd_reload_bss(hapd_iface->bss[j]);
1929 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1932 const struct wpa_driver_ops *driver;
1935 if (hapd_iface == NULL)
1938 if (hapd_iface->bss[0]->drv_priv == NULL) {
1939 wpa_printf(MSG_INFO, "Interface %s already disabled",
1940 hapd_iface->conf->bss[0]->iface);
1944 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1945 driver = hapd_iface->bss[0]->driver;
1946 drv_priv = hapd_iface->bss[0]->drv_priv;
1948 hapd_iface->driver_ap_teardown =
1949 !!(hapd_iface->drv_flags &
1950 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1952 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1953 for (j = 0; j < hapd_iface->num_bss; j++) {
1954 struct hostapd_data *hapd = hapd_iface->bss[j];
1955 hostapd_bss_deinit_no_free(hapd);
1956 hostapd_free_hapd_data(hapd);
1959 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
1961 /* From hostapd_cleanup_iface: These were initialized in
1962 * hostapd_setup_interface and hostapd_setup_interface_complete
1964 hostapd_cleanup_iface_partial(hapd_iface);
1966 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1967 hapd_iface->bss[0]->conf->iface);
1968 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1973 static struct hostapd_iface *
1974 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1976 struct hostapd_iface **iface, *hapd_iface;
1978 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1979 sizeof(struct hostapd_iface *));
1982 interfaces->iface = iface;
1983 hapd_iface = interfaces->iface[interfaces->count] =
1984 os_zalloc(sizeof(*hapd_iface));
1985 if (hapd_iface == NULL) {
1986 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1987 "the interface", __func__);
1990 interfaces->count++;
1991 hapd_iface->interfaces = interfaces;
1997 static struct hostapd_config *
1998 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1999 const char *ctrl_iface)
2001 struct hostapd_bss_config *bss;
2002 struct hostapd_config *conf;
2004 /* Allocates memory for bss and conf */
2005 conf = hostapd_config_defaults();
2007 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2008 "configuration", __func__);
2012 conf->driver = wpa_drivers[0];
2013 if (conf->driver == NULL) {
2014 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2015 hostapd_config_free(conf);
2019 bss = conf->last_bss = conf->bss[0];
2021 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2022 bss->ctrl_interface = os_strdup(ctrl_iface);
2023 if (bss->ctrl_interface == NULL) {
2024 hostapd_config_free(conf);
2028 /* Reading configuration file skipped, will be done in SET!
2029 * From reading the configuration till the end has to be done in
2036 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2037 struct hostapd_config *conf)
2040 struct hostapd_data *hapd;
2042 hapd_iface->bss = os_calloc(conf->num_bss,
2043 sizeof(struct hostapd_data *));
2044 if (hapd_iface->bss == NULL)
2047 for (i = 0; i < conf->num_bss; i++) {
2048 hapd = hapd_iface->bss[i] =
2049 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2053 os_free(hapd_iface->bss[i]);
2054 hapd_iface->bss[i] = NULL;
2056 os_free(hapd_iface->bss);
2057 hapd_iface->bss = NULL;
2060 hapd->msg_ctx = hapd;
2063 hapd_iface->conf = conf;
2064 hapd_iface->num_bss = conf->num_bss;
2070 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2072 struct hostapd_config *conf = NULL;
2073 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2074 struct hostapd_data *hapd;
2077 const char *conf_file = NULL, *phy_name = NULL;
2079 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2081 phy_name = buf + 11;
2082 pos = os_strchr(phy_name, ':');
2087 if (!os_strlen(conf_file))
2090 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2094 for (j = 0; j < interfaces->count; j++) {
2095 if (interfaces->iface[j] == hapd_iface)
2098 if (j == interfaces->count) {
2099 struct hostapd_iface **tmp;
2100 tmp = os_realloc_array(interfaces->iface,
2101 interfaces->count + 1,
2102 sizeof(struct hostapd_iface *));
2104 hostapd_interface_deinit_free(hapd_iface);
2107 interfaces->iface = tmp;
2108 interfaces->iface[interfaces->count++] = hapd_iface;
2109 new_iface = hapd_iface;
2113 if (interfaces->driver_init(hapd_iface))
2116 if (hostapd_setup_interface(hapd_iface)) {
2117 hostapd_deinit_driver(
2118 hapd_iface->bss[0]->driver,
2119 hapd_iface->bss[0]->drv_priv,
2124 /* Assign new BSS with bss[0]'s driver info */
2125 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2126 hapd->driver = hapd_iface->bss[0]->driver;
2127 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2128 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2131 if (start_ctrl_iface_bss(hapd) < 0 ||
2132 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2133 hostapd_setup_bss(hapd, -1))) {
2134 hostapd_cleanup(hapd);
2135 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2136 hapd_iface->conf->num_bss--;
2137 hapd_iface->num_bss--;
2138 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2139 __func__, hapd, hapd->conf->iface);
2140 hostapd_config_free_bss(hapd->conf);
2149 ptr = os_strchr(buf, ' ');
2154 if (os_strncmp(ptr, "config=", 7) == 0)
2155 conf_file = ptr + 7;
2157 for (i = 0; i < interfaces->count; i++) {
2158 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2160 wpa_printf(MSG_INFO, "Cannot add interface - it "
2166 hapd_iface = hostapd_iface_alloc(interfaces);
2167 if (hapd_iface == NULL) {
2168 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2169 "for interface", __func__);
2172 new_iface = hapd_iface;
2174 if (conf_file && interfaces->config_read_cb) {
2175 conf = interfaces->config_read_cb(conf_file);
2176 if (conf && conf->bss)
2177 os_strlcpy(conf->bss[0]->iface, buf,
2178 sizeof(conf->bss[0]->iface));
2180 conf = hostapd_config_alloc(interfaces, buf, ptr);
2181 if (conf == NULL || conf->bss == NULL) {
2182 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2183 "for configuration", __func__);
2187 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2188 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2189 "for hostapd", __func__);
2194 if (start_ctrl_iface(hapd_iface) < 0)
2197 wpa_printf(MSG_INFO, "Add interface '%s'",
2198 hapd_iface->conf->bss[0]->iface);
2204 hostapd_config_free(conf);
2206 if (hapd_iface->bss) {
2207 for (i = 0; i < hapd_iface->num_bss; i++) {
2208 hapd = hapd_iface->bss[i];
2211 if (hapd_iface->interfaces &&
2212 hapd_iface->interfaces->ctrl_iface_deinit)
2213 hapd_iface->interfaces->
2214 ctrl_iface_deinit(hapd);
2215 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2216 __func__, hapd_iface->bss[i],
2218 hostapd_cleanup(hapd);
2220 hapd_iface->bss[i] = NULL;
2222 os_free(hapd_iface->bss);
2223 hapd_iface->bss = NULL;
2226 interfaces->count--;
2227 interfaces->iface[interfaces->count] = NULL;
2229 hostapd_cleanup_iface(hapd_iface);
2235 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2239 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2241 /* Remove hostapd_data only if it has already been initialized */
2242 if (idx < iface->num_bss) {
2243 struct hostapd_data *hapd = iface->bss[idx];
2245 hostapd_bss_deinit(hapd);
2246 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2247 __func__, hapd, hapd->conf->iface);
2248 hostapd_config_free_bss(hapd->conf);
2254 for (i = idx; i < iface->num_bss; i++)
2255 iface->bss[i] = iface->bss[i + 1];
2257 hostapd_config_free_bss(iface->conf->bss[idx]);
2258 iface->conf->bss[idx] = NULL;
2261 iface->conf->num_bss--;
2262 for (i = idx; i < iface->conf->num_bss; i++)
2263 iface->conf->bss[i] = iface->conf->bss[i + 1];
2269 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2271 struct hostapd_iface *hapd_iface;
2274 for (i = 0; i < interfaces->count; i++) {
2275 hapd_iface = interfaces->iface[i];
2276 if (hapd_iface == NULL)
2278 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2279 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2280 hapd_iface->driver_ap_teardown =
2281 !!(hapd_iface->drv_flags &
2282 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2284 hostapd_interface_deinit_free(hapd_iface);
2286 while (k < (interfaces->count - 1)) {
2287 interfaces->iface[k] =
2288 interfaces->iface[k + 1];
2291 interfaces->count--;
2295 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2296 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2297 hapd_iface->driver_ap_teardown =
2298 !(hapd_iface->drv_flags &
2299 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2300 return hostapd_remove_bss(hapd_iface, j);
2309 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2310 * @hapd: Pointer to BSS data
2311 * @sta: Pointer to the associated STA data
2312 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2314 * This function will be called whenever a station associates with the AP. It
2315 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2316 * from drv_callbacks.c based on driver events for drivers that take care of
2317 * management frames (IEEE 802.11 authentication and association) internally.
2319 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2322 if (hapd->tkip_countermeasures) {
2323 hostapd_drv_sta_deauth(hapd, sta->addr,
2324 WLAN_REASON_MICHAEL_MIC_FAILURE);
2328 hostapd_prune_associations(hapd, sta->addr);
2330 /* IEEE 802.11F (IAPP) */
2331 if (hapd->conf->ieee802_11f)
2332 iapp_new_station(hapd->iapp, sta);
2335 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2336 sta->no_p2p_set = 1;
2337 hapd->num_sta_no_p2p++;
2338 if (hapd->num_sta_no_p2p == 1)
2339 hostapd_p2p_non_p2p_sta_connected(hapd);
2341 #endif /* CONFIG_P2P */
2343 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2344 * IEEE 802.1X/WPA code will start accounting after the station has
2345 * been authorized. */
2346 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2347 ap_sta_set_authorized(hapd, sta, 1);
2348 os_get_reltime(&sta->connected_time);
2349 accounting_sta_start(hapd, sta);
2352 /* Start IEEE 802.1X authentication process for new stations */
2353 ieee802_1x_new_station(hapd, sta);
2355 if (sta->auth_alg != WLAN_AUTH_FT &&
2356 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2357 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2359 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2361 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2362 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2363 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2364 __func__, MAC2STR(sta->addr),
2365 hapd->conf->ap_max_inactivity);
2366 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2367 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2368 ap_handle_timer, hapd, sta);
2373 const char * hostapd_state_text(enum hostapd_iface_state s)
2376 case HAPD_IFACE_UNINITIALIZED:
2377 return "UNINITIALIZED";
2378 case HAPD_IFACE_DISABLED:
2380 case HAPD_IFACE_COUNTRY_UPDATE:
2381 return "COUNTRY_UPDATE";
2382 case HAPD_IFACE_ACS:
2384 case HAPD_IFACE_HT_SCAN:
2386 case HAPD_IFACE_DFS:
2388 case HAPD_IFACE_ENABLED:
2396 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2398 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2399 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2400 hostapd_state_text(s));
2407 static void free_beacon_data(struct beacon_data *beacon)
2409 os_free(beacon->head);
2410 beacon->head = NULL;
2411 os_free(beacon->tail);
2412 beacon->tail = NULL;
2413 os_free(beacon->probe_resp);
2414 beacon->probe_resp = NULL;
2415 os_free(beacon->beacon_ies);
2416 beacon->beacon_ies = NULL;
2417 os_free(beacon->proberesp_ies);
2418 beacon->proberesp_ies = NULL;
2419 os_free(beacon->assocresp_ies);
2420 beacon->assocresp_ies = NULL;
2424 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2425 struct beacon_data *beacon)
2427 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2428 struct wpa_driver_ap_params params;
2431 os_memset(beacon, 0, sizeof(*beacon));
2432 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2436 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2440 goto free_ap_params;
2443 beacon->head = os_malloc(params.head_len);
2445 goto free_ap_extra_ies;
2447 os_memcpy(beacon->head, params.head, params.head_len);
2448 beacon->head_len = params.head_len;
2450 beacon->tail = os_malloc(params.tail_len);
2454 os_memcpy(beacon->tail, params.tail, params.tail_len);
2455 beacon->tail_len = params.tail_len;
2457 if (params.proberesp != NULL) {
2458 beacon->probe_resp = os_malloc(params.proberesp_len);
2459 if (!beacon->probe_resp)
2462 os_memcpy(beacon->probe_resp, params.proberesp,
2463 params.proberesp_len);
2464 beacon->probe_resp_len = params.proberesp_len;
2467 /* copy the extra ies */
2469 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2470 if (!beacon->beacon_ies)
2473 os_memcpy(beacon->beacon_ies,
2474 beacon_extra->buf, wpabuf_len(beacon_extra));
2475 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2478 if (proberesp_extra) {
2479 beacon->proberesp_ies =
2480 os_malloc(wpabuf_len(proberesp_extra));
2481 if (!beacon->proberesp_ies)
2484 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2485 wpabuf_len(proberesp_extra));
2486 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2489 if (assocresp_extra) {
2490 beacon->assocresp_ies =
2491 os_malloc(wpabuf_len(assocresp_extra));
2492 if (!beacon->assocresp_ies)
2495 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2496 wpabuf_len(assocresp_extra));
2497 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2502 /* if the function fails, the caller should not free beacon data */
2504 free_beacon_data(beacon);
2507 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2510 ieee802_11_free_ap_params(¶ms);
2516 * TODO: This flow currently supports only changing frequency within the
2517 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2518 * width) are not supported.
2520 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2521 struct hostapd_config *conf,
2522 struct hostapd_freq_params *params,
2523 struct hostapd_freq_params *old_params)
2527 if (!params->channel) {
2528 /* check if the new channel is supported by hw */
2529 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2532 channel = params->channel;
2536 /* if a pointer to old_params is provided we save previous state */
2538 old_params->channel = conf->channel;
2539 old_params->ht_enabled = conf->ieee80211n;
2540 old_params->sec_channel_offset = conf->secondary_channel;
2543 conf->channel = channel;
2544 conf->ieee80211n = params->ht_enabled;
2545 conf->secondary_channel = params->sec_channel_offset;
2547 /* TODO: maybe call here hostapd_config_check here? */
2553 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2554 struct csa_settings *settings)
2556 struct hostapd_iface *iface = hapd->iface;
2557 struct hostapd_freq_params old_freq;
2560 os_memset(&old_freq, 0, sizeof(old_freq));
2561 if (!iface || !iface->freq || hapd->csa_in_progress)
2564 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2565 &settings->freq_params,
2570 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2572 /* change back the configuration */
2573 hostapd_change_config_freq(iface->bss[0], iface->conf,
2579 /* set channel switch parameters for csa ie */
2580 hapd->cs_freq_params = settings->freq_params;
2581 hapd->cs_count = settings->cs_count;
2582 hapd->cs_block_tx = settings->block_tx;
2584 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2586 free_beacon_data(&settings->beacon_after);
2590 settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2591 settings->counter_offset_presp = hapd->cs_c_off_proberesp;
2597 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2599 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2601 hapd->cs_block_tx = 0;
2602 hapd->cs_c_off_beacon = 0;
2603 hapd->cs_c_off_proberesp = 0;
2604 hapd->csa_in_progress = 0;
2608 int hostapd_switch_channel(struct hostapd_data *hapd,
2609 struct csa_settings *settings)
2613 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2614 wpa_printf(MSG_INFO, "CSA is not supported");
2618 ret = hostapd_fill_csa_settings(hapd, settings);
2622 ret = hostapd_drv_switch_channel(hapd, settings);
2623 free_beacon_data(&settings->beacon_csa);
2624 free_beacon_data(&settings->beacon_after);
2627 /* if we failed, clean cs parameters */
2628 hostapd_cleanup_cs_params(hapd);
2632 hapd->csa_in_progress = 1;
2638 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2639 const struct hostapd_freq_params *freq_params)
2641 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2644 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2646 if (freq_params->center_freq1)
2647 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2648 if (freq_params->center_freq2)
2649 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2651 switch (freq_params->bandwidth) {
2655 vht_bw = VHT_CHANWIDTH_USE_HT;
2658 if (freq_params->center_freq2)
2659 vht_bw = VHT_CHANWIDTH_80P80MHZ;
2661 vht_bw = VHT_CHANWIDTH_80MHZ;
2664 vht_bw = VHT_CHANWIDTH_160MHZ;
2667 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2668 freq_params->bandwidth);
2672 iface->freq = freq_params->freq;
2673 iface->conf->channel = freq_params->channel;
2674 iface->conf->secondary_channel = freq_params->sec_channel_offset;
2675 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2676 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2677 iface->conf->vht_oper_chwidth = vht_bw;
2678 iface->conf->ieee80211n = freq_params->ht_enabled;
2679 iface->conf->ieee80211ac = freq_params->vht_enabled;
2682 * cs_params must not be cleared earlier because the freq_params
2683 * argument may actually point to one of these.
2685 for (i = 0; i < iface->num_bss; i++)
2686 hostapd_cleanup_cs_params(iface->bss[i]);
2688 hostapd_disable_iface(iface);
2689 hostapd_enable_iface(iface);
2692 #endif /* NEED_AP_MLME */