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 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
87 if (hostapd_setup_wpa_psk(hapd->conf)) {
88 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
89 "after reloading configuration");
92 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
93 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
95 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
97 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
98 hostapd_setup_wpa(hapd);
100 wpa_init_keys(hapd->wpa_auth);
101 } else if (hapd->conf->wpa) {
104 hostapd_reconfig_wpa(hapd);
105 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
106 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
107 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
108 "the kernel driver.");
109 } else if (hapd->wpa_auth) {
110 wpa_deinit(hapd->wpa_auth);
111 hapd->wpa_auth = NULL;
112 hostapd_set_privacy(hapd, 0);
113 hostapd_setup_encryption(hapd->conf->iface, hapd);
114 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
117 ieee802_11_set_beacon(hapd);
118 hostapd_update_wps(hapd);
120 if (hapd->conf->ssid.ssid_set &&
121 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
122 hapd->conf->ssid.ssid_len)) {
123 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
124 /* try to continue */
126 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
130 static void hostapd_clear_old(struct hostapd_iface *iface)
135 * Deauthenticate all stations since the new configuration may not
136 * allow them to use the BSS anymore.
138 for (j = 0; j < iface->num_bss; j++) {
139 hostapd_flush_old_stations(iface->bss[j],
140 WLAN_REASON_PREV_AUTH_NOT_VALID);
141 hostapd_broadcast_wep_clear(iface->bss[j]);
143 #ifndef CONFIG_NO_RADIUS
144 /* TODO: update dynamic data based on changed configuration
145 * items (e.g., open/close sockets, etc.) */
146 radius_client_flush(iface->bss[j]->radius, 0);
147 #endif /* CONFIG_NO_RADIUS */
152 int hostapd_reload_config(struct hostapd_iface *iface)
154 struct hostapd_data *hapd = iface->bss[0];
155 struct hostapd_config *newconf, *oldconf;
158 if (iface->config_fname == NULL) {
159 /* Only in-memory config in use - assume it has been updated */
160 hostapd_clear_old(iface);
161 for (j = 0; j < iface->num_bss; j++)
162 hostapd_reload_bss(iface->bss[j]);
166 if (iface->interfaces == NULL ||
167 iface->interfaces->config_read_cb == NULL)
169 newconf = iface->interfaces->config_read_cb(iface->config_fname);
173 hostapd_clear_old(iface);
175 oldconf = hapd->iconf;
176 iface->conf = newconf;
178 for (j = 0; j < iface->num_bss; j++) {
179 hapd = iface->bss[j];
180 hapd->iconf = newconf;
181 hapd->iconf->channel = oldconf->channel;
182 hapd->iconf->secondary_channel = oldconf->secondary_channel;
183 hapd->iconf->ieee80211n = oldconf->ieee80211n;
184 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
185 hapd->iconf->ht_capab = oldconf->ht_capab;
186 hapd->iconf->vht_capab = oldconf->vht_capab;
187 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
188 hapd->iconf->vht_oper_centr_freq_seg0_idx =
189 oldconf->vht_oper_centr_freq_seg0_idx;
190 hapd->iconf->vht_oper_centr_freq_seg1_idx =
191 oldconf->vht_oper_centr_freq_seg1_idx;
192 hapd->conf = newconf->bss[j];
193 hostapd_reload_bss(hapd);
196 hostapd_config_free(oldconf);
203 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
208 for (i = 0; i < NUM_WEP_KEYS; i++) {
209 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
210 0, NULL, 0, NULL, 0)) {
211 wpa_printf(MSG_DEBUG, "Failed to clear default "
212 "encryption keys (ifname=%s keyidx=%d)",
216 #ifdef CONFIG_IEEE80211W
217 if (hapd->conf->ieee80211w) {
218 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
219 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
222 wpa_printf(MSG_DEBUG, "Failed to clear "
223 "default mgmt encryption keys "
224 "(ifname=%s keyidx=%d)", ifname, i);
228 #endif /* CONFIG_IEEE80211W */
232 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
234 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
239 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
242 struct hostapd_ssid *ssid = &hapd->conf->ssid;
245 if (ssid->wep.default_len &&
246 hostapd_drv_set_key(hapd->conf->iface,
247 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
248 1, NULL, 0, ssid->wep.key[idx],
249 ssid->wep.len[idx])) {
250 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
258 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
260 os_free(hapd->probereq_cb);
261 hapd->probereq_cb = NULL;
264 wpabuf_free(hapd->p2p_beacon_ie);
265 hapd->p2p_beacon_ie = NULL;
266 wpabuf_free(hapd->p2p_probe_resp_ie);
267 hapd->p2p_probe_resp_ie = NULL;
268 #endif /* CONFIG_P2P */
270 if (!hapd->started) {
271 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
272 __func__, hapd->conf->iface);
277 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
278 iapp_deinit(hapd->iapp);
280 accounting_deinit(hapd);
281 hostapd_deinit_wpa(hapd);
283 hostapd_acl_deinit(hapd);
284 #ifndef CONFIG_NO_RADIUS
285 radius_client_deinit(hapd->radius);
287 radius_das_deinit(hapd->radius_das);
288 hapd->radius_das = NULL;
289 #endif /* CONFIG_NO_RADIUS */
291 hostapd_deinit_wps(hapd);
293 authsrv_deinit(hapd);
295 if (hapd->interface_added) {
296 hapd->interface_added = 0;
297 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
298 wpa_printf(MSG_WARNING,
299 "Failed to remove BSS interface %s",
301 hapd->interface_added = 1;
304 * Since this was a dynamically added interface, the
305 * driver wrapper may have removed its internal instance
306 * and hapd->drv_priv is not valid anymore.
308 hapd->drv_priv = NULL;
312 wpabuf_free(hapd->time_adv);
314 #ifdef CONFIG_INTERWORKING
315 gas_serv_deinit(hapd);
316 #endif /* CONFIG_INTERWORKING */
318 bss_load_update_deinit(hapd);
319 ndisc_snoop_deinit(hapd);
320 dhcp_snoop_deinit(hapd);
321 x_snoop_deinit(hapd);
324 bin_clear_free(hapd->tmp_eap_user.identity,
325 hapd->tmp_eap_user.identity_len);
326 bin_clear_free(hapd->tmp_eap_user.password,
327 hapd->tmp_eap_user.password_len);
328 #endif /* CONFIG_SQLITE */
331 wpabuf_free(hapd->mesh_pending_auth);
332 hapd->mesh_pending_auth = NULL;
333 #endif /* CONFIG_MESH */
338 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
339 * @hapd: Pointer to BSS data
341 * This function is used to free all per-BSS data structures and resources.
342 * Most of the modules that are initialized in hostapd_setup_bss() are
343 * deinitialized here.
345 static void hostapd_cleanup(struct hostapd_data *hapd)
347 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
349 if (hapd->iface->interfaces &&
350 hapd->iface->interfaces->ctrl_iface_deinit)
351 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
352 hostapd_free_hapd_data(hapd);
356 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
358 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
359 #ifdef CONFIG_IEEE80211N
361 hostapd_stop_setup_timers(iface);
362 #endif /* NEED_AP_MLME */
363 #endif /* CONFIG_IEEE80211N */
364 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
365 iface->hw_features = NULL;
366 os_free(iface->current_rates);
367 iface->current_rates = NULL;
368 os_free(iface->basic_rates);
369 iface->basic_rates = NULL;
370 ap_list_deinit(iface);
375 * hostapd_cleanup_iface - Complete per-interface cleanup
376 * @iface: Pointer to interface data
378 * This function is called after per-BSS data structures are deinitialized
379 * with hostapd_cleanup().
381 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
383 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
384 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
386 hostapd_cleanup_iface_partial(iface);
387 hostapd_config_free(iface->conf);
390 os_free(iface->config_fname);
392 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
397 static void hostapd_clear_wep(struct hostapd_data *hapd)
399 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
400 hostapd_set_privacy(hapd, 0);
401 hostapd_broadcast_wep_clear(hapd);
406 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
410 hostapd_broadcast_wep_set(hapd);
412 if (hapd->conf->ssid.wep.default_len) {
413 hostapd_set_privacy(hapd, 1);
418 * When IEEE 802.1X is not enabled, the driver may need to know how to
419 * set authentication algorithms for static WEP.
421 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
423 for (i = 0; i < 4; i++) {
424 if (hapd->conf->ssid.wep.key[i] &&
425 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
426 i == hapd->conf->ssid.wep.idx, NULL, 0,
427 hapd->conf->ssid.wep.key[i],
428 hapd->conf->ssid.wep.len[i])) {
429 wpa_printf(MSG_WARNING, "Could not set WEP "
433 if (hapd->conf->ssid.wep.key[i] &&
434 i == hapd->conf->ssid.wep.idx)
435 hostapd_set_privacy(hapd, 1);
442 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
447 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
450 if (!hapd->iface->driver_ap_teardown) {
451 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
452 "Flushing old station entries");
454 if (hostapd_flush(hapd)) {
455 wpa_msg(hapd->msg_ctx, MSG_WARNING,
456 "Could not connect to kernel driver");
460 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
461 os_memset(addr, 0xff, ETH_ALEN);
462 hostapd_drv_sta_deauth(hapd, addr, reason);
463 hostapd_free_stas(hapd);
469 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
471 hostapd_free_stas(hapd);
472 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
473 hostapd_clear_wep(hapd);
478 * hostapd_validate_bssid_configuration - Validate BSSID configuration
479 * @iface: Pointer to interface data
480 * Returns: 0 on success, -1 on failure
482 * This function is used to validate that the configured BSSIDs are valid.
484 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
486 u8 mask[ETH_ALEN] = { 0 };
487 struct hostapd_data *hapd = iface->bss[0];
488 unsigned int i = iface->conf->num_bss, bits = 0, j;
491 if (hostapd_drv_none(hapd))
494 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
496 /* Determine the bits necessary to cover the number of BSSIDs. */
497 for (i--; i; i >>= 1)
500 /* Determine the bits necessary to any configured BSSIDs,
501 if they are higher than the number of BSSIDs. */
502 for (j = 0; j < iface->conf->num_bss; j++) {
503 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
509 for (i = 0; i < ETH_ALEN; i++) {
511 iface->conf->bss[j]->bssid[i] ^
519 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
525 while (mask[i] != 0) {
535 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
540 os_memset(mask, 0xff, ETH_ALEN);
542 for (i = 5; i > 5 - j; i--)
549 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
550 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
555 for (i = 0; i < ETH_ALEN; i++) {
556 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
557 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
558 " for start address " MACSTR ".",
559 MAC2STR(mask), MAC2STR(hapd->own_addr));
560 wpa_printf(MSG_ERROR, "Start address must be the "
561 "first address in the block (i.e., addr "
562 "AND mask == addr).");
571 static int mac_in_conf(struct hostapd_config *conf, const void *a)
575 for (i = 0; i < conf->num_bss; i++) {
576 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
585 #ifndef CONFIG_NO_RADIUS
587 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
588 struct radius_das_attrs *attr)
590 if (attr->nas_identifier &&
591 (!hapd->conf->nas_identifier ||
592 os_strlen(hapd->conf->nas_identifier) !=
593 attr->nas_identifier_len ||
594 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
595 attr->nas_identifier_len) != 0)) {
596 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
600 if (attr->nas_ip_addr &&
601 (hapd->conf->own_ip_addr.af != AF_INET ||
602 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
604 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
609 if (attr->nas_ipv6_addr &&
610 (hapd->conf->own_ip_addr.af != AF_INET6 ||
611 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
613 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
616 #endif /* CONFIG_IPV6 */
622 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
623 struct radius_das_attrs *attr,
626 struct sta_info *selected, *sta;
633 for (sta = hapd->sta_list; sta; sta = sta->next)
634 sta->radius_das_match = 1;
636 if (attr->sta_addr) {
638 sta = ap_get_sta(hapd, attr->sta_addr);
640 wpa_printf(MSG_DEBUG,
641 "RADIUS DAS: No Calling-Station-Id match");
646 for (sta = hapd->sta_list; sta; sta = sta->next) {
648 sta->radius_das_match = 0;
650 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
653 if (attr->acct_session_id) {
655 if (attr->acct_session_id_len != 17) {
656 wpa_printf(MSG_DEBUG,
657 "RADIUS DAS: Acct-Session-Id cannot match");
662 for (sta = hapd->sta_list; sta; sta = sta->next) {
663 if (!sta->radius_das_match)
665 os_snprintf(buf, sizeof(buf), "%08X-%08X",
666 sta->acct_session_id_hi,
667 sta->acct_session_id_lo);
668 if (os_memcmp(attr->acct_session_id, buf, 17) != 0)
669 sta->radius_das_match = 0;
675 wpa_printf(MSG_DEBUG,
676 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
679 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
682 if (attr->acct_multi_session_id) {
684 if (attr->acct_multi_session_id_len != 17) {
685 wpa_printf(MSG_DEBUG,
686 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
691 for (sta = hapd->sta_list; sta; sta = sta->next) {
692 if (!sta->radius_das_match)
694 if (!sta->eapol_sm ||
695 !sta->eapol_sm->acct_multi_session_id_hi) {
696 sta->radius_das_match = 0;
699 os_snprintf(buf, sizeof(buf), "%08X+%08X",
700 sta->eapol_sm->acct_multi_session_id_hi,
701 sta->eapol_sm->acct_multi_session_id_lo);
702 if (os_memcmp(attr->acct_multi_session_id, buf, 17) !=
704 sta->radius_das_match = 0;
710 wpa_printf(MSG_DEBUG,
711 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
714 wpa_printf(MSG_DEBUG,
715 "RADIUS DAS: Acct-Multi-Session-Id match");
722 for (sta = hapd->sta_list; sta; sta = sta->next) {
725 if (!sta->radius_das_match)
727 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
728 if (!cui || wpabuf_len(cui) != attr->cui_len ||
729 os_memcmp(wpabuf_head(cui), attr->cui,
731 sta->radius_das_match = 0;
737 wpa_printf(MSG_DEBUG,
738 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
741 wpa_printf(MSG_DEBUG,
742 "RADIUS DAS: Chargeable-User-Identity match");
745 if (attr->user_name) {
749 for (sta = hapd->sta_list; sta; sta = sta->next) {
753 if (!sta->radius_das_match)
755 identity = ieee802_1x_get_identity(sta->eapol_sm,
758 identity_len != attr->user_name_len ||
759 os_memcmp(identity, attr->user_name, identity_len)
761 sta->radius_das_match = 0;
767 wpa_printf(MSG_DEBUG,
768 "RADIUS DAS: No matches remaining after User-Name check");
771 wpa_printf(MSG_DEBUG,
772 "RADIUS DAS: User-Name match");
777 * In theory, we could match all current associations, but it
778 * seems safer to just reject requests that do not include any
779 * session identification attributes.
781 wpa_printf(MSG_DEBUG,
782 "RADIUS DAS: No session identification attributes included");
787 for (sta = hapd->sta_list; sta; sta = sta->next) {
788 if (sta->radius_das_match) {
801 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
802 struct radius_das_attrs *attr)
806 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
810 static enum radius_das_res
811 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
813 struct hostapd_data *hapd = ctx;
814 struct sta_info *sta;
817 if (hostapd_das_nas_mismatch(hapd, attr))
818 return RADIUS_DAS_NAS_MISMATCH;
820 sta = hostapd_das_find_sta(hapd, attr, &multi);
823 wpa_printf(MSG_DEBUG,
824 "RADIUS DAS: Multiple sessions match - not supported");
825 return RADIUS_DAS_MULTI_SESSION_MATCH;
827 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
828 wpa_printf(MSG_DEBUG,
829 "RADIUS DAS: PMKSA cache entry matched");
830 return RADIUS_DAS_SUCCESS;
832 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
833 return RADIUS_DAS_SESSION_NOT_FOUND;
836 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
837 " - disconnecting", MAC2STR(sta->addr));
838 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
840 hostapd_drv_sta_deauth(hapd, sta->addr,
841 WLAN_REASON_PREV_AUTH_NOT_VALID);
842 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
844 return RADIUS_DAS_SUCCESS;
847 #endif /* CONFIG_NO_RADIUS */
851 * hostapd_setup_bss - Per-BSS setup (initialization)
852 * @hapd: Pointer to BSS data
853 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
854 * but interface may exist
856 * This function is used to initialize all per-BSS data structures and
857 * resources. This gets called in a loop for each BSS when an interface is
858 * initialized. Most of the modules that are initialized here will be
859 * deinitialized in hostapd_cleanup().
861 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
863 struct hostapd_bss_config *conf = hapd->conf;
864 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
865 int ssid_len, set_ssid;
866 char force_ifname[IFNAMSIZ];
867 u8 if_addr[ETH_ALEN];
868 int flush_old_stations = 1;
870 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
871 __func__, hapd, conf->iface, first);
873 #ifdef EAP_SERVER_TNC
874 if (conf->tnc && tncs_global_init() < 0) {
875 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
878 #endif /* EAP_SERVER_TNC */
881 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
882 __func__, conf->iface);
887 if (!first || first == -1) {
888 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
889 /* Allocate the next available BSSID. */
891 inc_byte_array(hapd->own_addr, ETH_ALEN);
892 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
894 /* Allocate the configured BSSID. */
895 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
897 if (hostapd_mac_comp(hapd->own_addr,
898 hapd->iface->bss[0]->own_addr) ==
900 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
901 "BSSID set to the MAC address of "
902 "the radio", conf->iface);
907 hapd->interface_added = 1;
908 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
909 conf->iface, hapd->own_addr, hapd,
910 &hapd->drv_priv, force_ifname, if_addr,
911 conf->bridge[0] ? conf->bridge : NULL,
913 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
914 MACSTR ")", MAC2STR(hapd->own_addr));
915 hapd->interface_added = 0;
920 if (conf->wmm_enabled < 0)
921 conf->wmm_enabled = hapd->iconf->ieee80211n;
924 if (hapd->iface->mconf == NULL)
925 flush_old_stations = 0;
926 #endif /* CONFIG_MESH */
928 if (flush_old_stations)
929 hostapd_flush_old_stations(hapd,
930 WLAN_REASON_PREV_AUTH_NOT_VALID);
931 hostapd_set_privacy(hapd, 0);
933 hostapd_broadcast_wep_clear(hapd);
934 if (hostapd_setup_encryption(conf->iface, hapd))
938 * Fetch the SSID from the system and use it or,
939 * if one was specified in the config file, verify they
942 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
944 wpa_printf(MSG_ERROR, "Could not read SSID from system");
947 if (conf->ssid.ssid_set) {
949 * If SSID is specified in the config file and it differs
950 * from what is being used then force installation of the
953 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
954 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
957 * No SSID in the config file; just use the one we got
961 conf->ssid.ssid_len = ssid_len;
962 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
965 if (!hostapd_drv_none(hapd)) {
966 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
968 conf->iface, MAC2STR(hapd->own_addr),
969 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
972 if (hostapd_setup_wpa_psk(conf)) {
973 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
977 /* Set SSID for the kernel driver (to be used in beacon and probe
978 * response frames) */
979 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
980 conf->ssid.ssid_len)) {
981 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
985 if (wpa_debug_level <= MSG_MSGDUMP)
986 conf->radius->msg_dumps = 1;
987 #ifndef CONFIG_NO_RADIUS
988 hapd->radius = radius_client_init(hapd, conf->radius);
989 if (hapd->radius == NULL) {
990 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
994 if (conf->radius_das_port) {
995 struct radius_das_conf das_conf;
996 os_memset(&das_conf, 0, sizeof(das_conf));
997 das_conf.port = conf->radius_das_port;
998 das_conf.shared_secret = conf->radius_das_shared_secret;
999 das_conf.shared_secret_len =
1000 conf->radius_das_shared_secret_len;
1001 das_conf.client_addr = &conf->radius_das_client_addr;
1002 das_conf.time_window = conf->radius_das_time_window;
1003 das_conf.require_event_timestamp =
1004 conf->radius_das_require_event_timestamp;
1005 das_conf.ctx = hapd;
1006 das_conf.disconnect = hostapd_das_disconnect;
1007 hapd->radius_das = radius_das_init(&das_conf);
1008 if (hapd->radius_das == NULL) {
1009 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1014 #endif /* CONFIG_NO_RADIUS */
1016 if (hostapd_acl_init(hapd)) {
1017 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1020 if (hostapd_init_wps(hapd, conf))
1023 if (authsrv_init(hapd) < 0)
1026 if (ieee802_1x_init(hapd)) {
1027 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1031 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1034 if (accounting_init(hapd)) {
1035 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1039 if (conf->ieee802_11f &&
1040 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1041 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1046 #ifdef CONFIG_INTERWORKING
1047 if (gas_serv_init(hapd)) {
1048 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1052 if (conf->qos_map_set_len &&
1053 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1054 conf->qos_map_set_len)) {
1055 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1058 #endif /* CONFIG_INTERWORKING */
1060 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1061 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1065 if (conf->proxy_arp) {
1066 if (x_snoop_init(hapd)) {
1067 wpa_printf(MSG_ERROR,
1068 "Generic snooping infrastructure initialization failed");
1072 if (dhcp_snoop_init(hapd)) {
1073 wpa_printf(MSG_ERROR,
1074 "DHCP snooping initialization failed");
1078 if (ndisc_snoop_init(hapd)) {
1079 wpa_printf(MSG_ERROR,
1080 "Neighbor Discovery snooping initialization failed");
1085 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1086 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1090 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1093 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1096 if (hapd->driver && hapd->driver->set_operstate)
1097 hapd->driver->set_operstate(hapd->drv_priv, 1);
1103 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1105 struct hostapd_data *hapd = iface->bss[0];
1107 struct hostapd_tx_queue_params *p;
1110 if (iface->mconf == NULL)
1112 #endif /* CONFIG_MESH */
1114 for (i = 0; i < NUM_TX_QUEUES; i++) {
1115 p = &iface->conf->tx_queue[i];
1117 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1118 p->cwmax, p->burst)) {
1119 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1120 "parameters for queue %d.", i);
1121 /* Continue anyway */
1127 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1128 struct mac_acl_entry *mac_acl,
1129 int n_entries, u8 accept_acl)
1131 struct hostapd_acl_params *acl_params;
1134 acl_params = os_zalloc(sizeof(*acl_params) +
1135 (n_entries * sizeof(acl_params->mac_acl[0])));
1139 for (i = 0; i < n_entries; i++)
1140 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1143 acl_params->acl_policy = accept_acl;
1144 acl_params->num_mac_acl = n_entries;
1146 err = hostapd_drv_set_acl(hapd, acl_params);
1148 os_free(acl_params);
1154 static void hostapd_set_acl(struct hostapd_data *hapd)
1156 struct hostapd_config *conf = hapd->iconf;
1160 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1163 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1165 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1166 conf->bss[0]->num_accept_mac,
1169 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1172 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1174 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1175 conf->bss[0]->num_deny_mac,
1178 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1185 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1187 if (!hapd->iface->interfaces ||
1188 !hapd->iface->interfaces->ctrl_iface_init)
1191 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1192 wpa_printf(MSG_ERROR,
1193 "Failed to setup control interface for %s",
1202 static int start_ctrl_iface(struct hostapd_iface *iface)
1206 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1209 for (i = 0; i < iface->num_bss; i++) {
1210 struct hostapd_data *hapd = iface->bss[i];
1211 if (iface->interfaces->ctrl_iface_init(hapd)) {
1212 wpa_printf(MSG_ERROR,
1213 "Failed to setup control interface for %s",
1223 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1225 struct hostapd_iface *iface = eloop_ctx;
1227 if (!iface->wait_channel_update) {
1228 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1233 * It is possible that the existing channel list is acceptable, so try
1236 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1237 setup_interface2(iface);
1241 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1243 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1246 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1247 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1248 setup_interface2(iface);
1252 static int setup_interface(struct hostapd_iface *iface)
1254 struct hostapd_data *hapd = iface->bss[0];
1258 * It is possible that setup_interface() is called after the interface
1259 * was disabled etc., in which case driver_ap_teardown is possibly set
1260 * to 1. Clear it here so any other key/station deletion, which is not
1261 * part of a teardown flow, would also call the relevant driver
1264 iface->driver_ap_teardown = 0;
1266 if (!iface->phy[0]) {
1267 const char *phy = hostapd_drv_get_radio_name(hapd);
1269 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1270 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1275 * Make sure that all BSSes get configured with a pointer to the same
1278 for (i = 1; i < iface->num_bss; i++) {
1279 iface->bss[i]->driver = hapd->driver;
1280 iface->bss[i]->drv_priv = hapd->drv_priv;
1283 if (hostapd_validate_bssid_configuration(iface))
1287 * Initialize control interfaces early to allow external monitoring of
1288 * channel setup operations that may take considerable amount of time
1289 * especially for DFS cases.
1291 if (start_ctrl_iface(iface))
1294 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1295 char country[4], previous_country[4];
1297 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1298 if (hostapd_get_country(hapd, previous_country) < 0)
1299 previous_country[0] = '\0';
1301 os_memcpy(country, hapd->iconf->country, 3);
1303 if (hostapd_set_country(hapd, country) < 0) {
1304 wpa_printf(MSG_ERROR, "Failed to set country code");
1308 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1309 previous_country, country);
1311 if (os_strncmp(previous_country, country, 2) != 0) {
1312 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1313 iface->wait_channel_update = 1;
1314 eloop_register_timeout(5, 0,
1315 channel_list_update_timeout,
1321 return setup_interface2(iface);
1325 static int setup_interface2(struct hostapd_iface *iface)
1327 iface->wait_channel_update = 0;
1329 if (hostapd_get_hw_features(iface)) {
1330 /* Not all drivers support this yet, so continue without hw
1333 int ret = hostapd_select_hw_mode(iface);
1335 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1336 "channel. (%d)", ret);
1340 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1343 ret = hostapd_check_ht_capab(iface);
1347 wpa_printf(MSG_DEBUG, "Interface initialization will "
1348 "be completed in a callback");
1352 if (iface->conf->ieee80211h)
1353 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1355 return hostapd_setup_interface_complete(iface, 0);
1358 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1359 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1360 if (iface->interfaces && iface->interfaces->terminate_on_error)
1367 * hostapd_setup_interface_complete - Complete interface setup
1369 * This function is called when previous steps in the interface setup has been
1370 * completed. This can also start operations, e.g., DFS, that will require
1371 * additional processing before interface is ready to be enabled. Such
1372 * operations will call this function from eloop callbacks when finished.
1374 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1376 struct hostapd_data *hapd = iface->bss[0];
1379 int delay_apply_cfg = 0;
1380 int res_dfs_offload = 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 /* If DFS is offloaded to the driver */
1409 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1410 if (res_dfs_offload <= 0) {
1411 if (res_dfs_offload < 0)
1414 wpa_printf(MSG_DEBUG,
1415 "Proceed with AP/channel setup");
1417 * If this is a DFS channel, move to completing
1420 if (res_dfs_offload == 1)
1422 /* Otherwise fall through. */
1425 #endif /* NEED_AP_MLME */
1428 if (iface->mconf != NULL) {
1429 wpa_printf(MSG_DEBUG,
1430 "%s: Mesh configuration will be applied while joining the mesh network",
1431 iface->bss[0]->conf->iface);
1432 delay_apply_cfg = 1;
1434 #endif /* CONFIG_MESH */
1436 if (!delay_apply_cfg &&
1437 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1438 hapd->iconf->channel,
1439 hapd->iconf->ieee80211n,
1440 hapd->iconf->ieee80211ac,
1441 hapd->iconf->secondary_channel,
1442 hapd->iconf->vht_oper_chwidth,
1443 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1444 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1445 wpa_printf(MSG_ERROR, "Could not set channel for "
1451 if (iface->current_mode) {
1452 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1453 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1455 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1456 HOSTAPD_LEVEL_WARNING,
1457 "Failed to prepare rates table.");
1462 if (hapd->iconf->rts_threshold > -1 &&
1463 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1464 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1469 if (hapd->iconf->fragm_threshold > -1 &&
1470 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1471 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1472 "for kernel driver");
1476 prev_addr = hapd->own_addr;
1478 for (j = 0; j < iface->num_bss; j++) {
1479 hapd = iface->bss[j];
1481 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1482 if (hostapd_setup_bss(hapd, j == 0)) {
1484 hapd = iface->bss[j];
1485 hostapd_bss_deinit_no_free(hapd);
1486 hostapd_free_hapd_data(hapd);
1490 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1491 prev_addr = hapd->own_addr;
1493 hapd = iface->bss[0];
1495 hostapd_tx_queue_params(iface);
1497 ap_list_init(iface);
1499 hostapd_set_acl(hapd);
1501 if (hostapd_driver_commit(hapd) < 0) {
1502 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1503 "configuration", __func__);
1508 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1509 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1510 * mode), the interface is up only after driver_commit, so initialize
1511 * WPS after driver_commit.
1513 for (j = 0; j < iface->num_bss; j++) {
1514 if (hostapd_init_wps_complete(iface->bss[j]))
1518 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1521 * If freq is DFS, and DFS is offloaded to the driver, then wait
1522 * for CAC to complete.
1524 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1525 return res_dfs_offload;
1530 #endif /* NEED_AP_MLME */
1531 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1532 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1533 if (hapd->setup_complete_cb)
1534 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1536 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1537 iface->bss[0]->conf->iface);
1538 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1539 iface->interfaces->terminate_on_error--;
1544 wpa_printf(MSG_ERROR, "Interface initialization failed");
1545 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1546 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1547 if (iface->interfaces && iface->interfaces->terminate_on_error)
1554 * hostapd_setup_interface - Setup of an interface
1555 * @iface: Pointer to interface data.
1556 * Returns: 0 on success, -1 on failure
1558 * Initializes the driver interface, validates the configuration,
1559 * and sets driver parameters based on the configuration.
1560 * Flushes old stations, sets the channel, encryption,
1561 * beacons, and WDS links based on the configuration.
1563 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1564 * or DFS operations, this function returns 0 before such operations have been
1565 * completed. The pending operations are registered into eloop and will be
1566 * completed from eloop callbacks. Those callbacks end up calling
1567 * hostapd_setup_interface_complete() once setup has been completed.
1569 int hostapd_setup_interface(struct hostapd_iface *iface)
1573 ret = setup_interface(iface);
1575 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1576 iface->bss[0]->conf->iface);
1585 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1586 * @hapd_iface: Pointer to interface data
1587 * @conf: Pointer to per-interface configuration
1588 * @bss: Pointer to per-BSS configuration for this BSS
1589 * Returns: Pointer to allocated BSS data
1591 * This function is used to allocate per-BSS data structure. This data will be
1592 * freed after hostapd_cleanup() is called for it during interface
1595 struct hostapd_data *
1596 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1597 struct hostapd_config *conf,
1598 struct hostapd_bss_config *bss)
1600 struct hostapd_data *hapd;
1602 hapd = os_zalloc(sizeof(*hapd));
1606 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1609 hapd->iface = hapd_iface;
1610 hapd->driver = hapd->iconf->driver;
1611 hapd->ctrl_sock = -1;
1617 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1619 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1621 hostapd_bss_deinit_no_free(hapd);
1622 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1623 hostapd_cleanup(hapd);
1627 void hostapd_interface_deinit(struct hostapd_iface *iface)
1631 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1635 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1637 #ifdef CONFIG_IEEE80211N
1639 hostapd_stop_setup_timers(iface);
1640 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1641 #endif /* NEED_AP_MLME */
1642 #endif /* CONFIG_IEEE80211N */
1643 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1644 iface->wait_channel_update = 0;
1646 for (j = iface->num_bss - 1; j >= 0; j--)
1647 hostapd_bss_deinit(iface->bss[j]);
1651 void hostapd_interface_free(struct hostapd_iface *iface)
1654 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1655 for (j = 0; j < iface->num_bss; j++) {
1656 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1657 __func__, iface->bss[j]);
1658 os_free(iface->bss[j]);
1660 hostapd_cleanup_iface(iface);
1665 * hostapd_init - Allocate and initialize per-interface data
1666 * @config_file: Path to the configuration file
1667 * Returns: Pointer to the allocated interface data or %NULL on failure
1669 * This function is used to allocate main data structures for per-interface
1670 * data. The allocated data buffer will be freed by calling
1671 * hostapd_cleanup_iface().
1673 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1674 const char *config_file)
1676 struct hostapd_iface *hapd_iface = NULL;
1677 struct hostapd_config *conf = NULL;
1678 struct hostapd_data *hapd;
1681 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1682 if (hapd_iface == NULL)
1685 hapd_iface->config_fname = os_strdup(config_file);
1686 if (hapd_iface->config_fname == NULL)
1689 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1692 hapd_iface->conf = conf;
1694 hapd_iface->num_bss = conf->num_bss;
1695 hapd_iface->bss = os_calloc(conf->num_bss,
1696 sizeof(struct hostapd_data *));
1697 if (hapd_iface->bss == NULL)
1700 for (i = 0; i < conf->num_bss; i++) {
1701 hapd = hapd_iface->bss[i] =
1702 hostapd_alloc_bss_data(hapd_iface, conf,
1706 hapd->msg_ctx = hapd;
1712 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1715 hostapd_config_free(conf);
1717 os_free(hapd_iface->config_fname);
1718 os_free(hapd_iface->bss);
1719 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1720 __func__, hapd_iface);
1721 os_free(hapd_iface);
1727 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1731 for (i = 0; i < interfaces->count; i++) {
1732 struct hostapd_iface *iface = interfaces->iface[i];
1733 for (j = 0; j < iface->num_bss; j++) {
1734 struct hostapd_data *hapd = iface->bss[j];
1735 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1745 * hostapd_interface_init_bss - Read configuration file and init BSS data
1747 * This function is used to parse configuration file for a BSS. This BSS is
1748 * added to an existing interface sharing the same radio (if any) or a new
1749 * interface is created if this is the first interface on a radio. This
1750 * allocate memory for the BSS. No actual driver operations are started.
1752 * This is similar to hostapd_interface_init(), but for a case where the
1753 * configuration is used to add a single BSS instead of all BSSes for a radio.
1755 struct hostapd_iface *
1756 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1757 const char *config_fname, int debug)
1759 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1760 struct hostapd_data *hapd;
1767 for (i = 0; i < interfaces->count; i++) {
1768 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1769 iface = interfaces->iface[i];
1774 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1775 config_fname, phy, iface ? "" : " --> new PHY");
1777 struct hostapd_config *conf;
1778 struct hostapd_bss_config **tmp_conf;
1779 struct hostapd_data **tmp_bss;
1780 struct hostapd_bss_config *bss;
1783 /* Add new BSS to existing iface */
1784 conf = interfaces->config_read_cb(config_fname);
1787 if (conf->num_bss > 1) {
1788 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1789 hostapd_config_free(conf);
1793 ifname = conf->bss[0]->iface;
1794 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1795 wpa_printf(MSG_ERROR,
1796 "Interface name %s already in use", ifname);
1797 hostapd_config_free(conf);
1801 tmp_conf = os_realloc_array(
1802 iface->conf->bss, iface->conf->num_bss + 1,
1803 sizeof(struct hostapd_bss_config *));
1804 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1805 sizeof(struct hostapd_data *));
1807 iface->bss = tmp_bss;
1809 iface->conf->bss = tmp_conf;
1810 iface->conf->last_bss = tmp_conf[0];
1812 if (tmp_bss == NULL || tmp_conf == NULL) {
1813 hostapd_config_free(conf);
1816 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1817 iface->conf->num_bss++;
1819 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1821 iface->conf->num_bss--;
1822 hostapd_config_free(conf);
1825 iface->conf->last_bss = bss;
1826 iface->bss[iface->num_bss] = hapd;
1827 hapd->msg_ctx = hapd;
1829 bss_idx = iface->num_bss++;
1831 conf->bss[0] = NULL;
1832 hostapd_config_free(conf);
1834 /* Add a new iface with the first BSS */
1835 new_iface = iface = hostapd_init(interfaces, config_fname);
1838 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1839 iface->interfaces = interfaces;
1843 for (k = 0; k < debug; k++) {
1844 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1845 iface->bss[bss_idx]->conf->logger_stdout_level--;
1848 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1849 !hostapd_drv_none(iface->bss[bss_idx])) {
1850 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1853 hostapd_interface_deinit_free(new_iface);
1861 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1863 const struct wpa_driver_ops *driver;
1866 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1869 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1870 __func__, (unsigned int) iface->num_bss,
1871 (unsigned int) iface->conf->num_bss);
1872 driver = iface->bss[0]->driver;
1873 drv_priv = iface->bss[0]->drv_priv;
1874 hostapd_interface_deinit(iface);
1875 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1876 __func__, driver, drv_priv);
1877 if (driver && driver->hapd_deinit && drv_priv) {
1878 driver->hapd_deinit(drv_priv);
1879 iface->bss[0]->drv_priv = NULL;
1881 hostapd_interface_free(iface);
1885 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1887 struct hostapd_iface *hapd_iface)
1891 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1892 __func__, driver, drv_priv);
1893 if (driver && driver->hapd_deinit && drv_priv) {
1894 driver->hapd_deinit(drv_priv);
1895 for (j = 0; j < hapd_iface->num_bss; j++) {
1896 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1898 hapd_iface->bss[j]->drv_priv);
1899 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1900 hapd_iface->bss[j]->drv_priv = NULL;
1906 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1910 if (hapd_iface->bss[0]->drv_priv != NULL) {
1911 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1912 hapd_iface->conf->bss[0]->iface);
1916 wpa_printf(MSG_DEBUG, "Enable interface %s",
1917 hapd_iface->conf->bss[0]->iface);
1919 for (j = 0; j < hapd_iface->num_bss; j++)
1920 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1921 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1922 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1926 if (hapd_iface->interfaces == NULL ||
1927 hapd_iface->interfaces->driver_init == NULL ||
1928 hapd_iface->interfaces->driver_init(hapd_iface))
1931 if (hostapd_setup_interface(hapd_iface)) {
1932 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1933 hapd_iface->bss[0]->drv_priv,
1942 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1946 wpa_printf(MSG_DEBUG, "Reload interface %s",
1947 hapd_iface->conf->bss[0]->iface);
1948 for (j = 0; j < hapd_iface->num_bss; j++)
1949 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1950 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1951 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1954 hostapd_clear_old(hapd_iface);
1955 for (j = 0; j < hapd_iface->num_bss; j++)
1956 hostapd_reload_bss(hapd_iface->bss[j]);
1962 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1965 const struct wpa_driver_ops *driver;
1968 if (hapd_iface == NULL)
1971 if (hapd_iface->bss[0]->drv_priv == NULL) {
1972 wpa_printf(MSG_INFO, "Interface %s already disabled",
1973 hapd_iface->conf->bss[0]->iface);
1977 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1978 driver = hapd_iface->bss[0]->driver;
1979 drv_priv = hapd_iface->bss[0]->drv_priv;
1981 hapd_iface->driver_ap_teardown =
1982 !!(hapd_iface->drv_flags &
1983 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1985 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1986 for (j = 0; j < hapd_iface->num_bss; j++) {
1987 struct hostapd_data *hapd = hapd_iface->bss[j];
1988 hostapd_bss_deinit_no_free(hapd);
1989 hostapd_free_hapd_data(hapd);
1992 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
1994 /* From hostapd_cleanup_iface: These were initialized in
1995 * hostapd_setup_interface and hostapd_setup_interface_complete
1997 hostapd_cleanup_iface_partial(hapd_iface);
1999 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2000 hapd_iface->bss[0]->conf->iface);
2001 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2006 static struct hostapd_iface *
2007 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2009 struct hostapd_iface **iface, *hapd_iface;
2011 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2012 sizeof(struct hostapd_iface *));
2015 interfaces->iface = iface;
2016 hapd_iface = interfaces->iface[interfaces->count] =
2017 os_zalloc(sizeof(*hapd_iface));
2018 if (hapd_iface == NULL) {
2019 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2020 "the interface", __func__);
2023 interfaces->count++;
2024 hapd_iface->interfaces = interfaces;
2030 static struct hostapd_config *
2031 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2032 const char *ctrl_iface)
2034 struct hostapd_bss_config *bss;
2035 struct hostapd_config *conf;
2037 /* Allocates memory for bss and conf */
2038 conf = hostapd_config_defaults();
2040 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2041 "configuration", __func__);
2045 conf->driver = wpa_drivers[0];
2046 if (conf->driver == NULL) {
2047 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2048 hostapd_config_free(conf);
2052 bss = conf->last_bss = conf->bss[0];
2054 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2055 bss->ctrl_interface = os_strdup(ctrl_iface);
2056 if (bss->ctrl_interface == NULL) {
2057 hostapd_config_free(conf);
2061 /* Reading configuration file skipped, will be done in SET!
2062 * From reading the configuration till the end has to be done in
2069 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2070 struct hostapd_config *conf)
2073 struct hostapd_data *hapd;
2075 hapd_iface->bss = os_calloc(conf->num_bss,
2076 sizeof(struct hostapd_data *));
2077 if (hapd_iface->bss == NULL)
2080 for (i = 0; i < conf->num_bss; i++) {
2081 hapd = hapd_iface->bss[i] =
2082 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2086 os_free(hapd_iface->bss[i]);
2087 hapd_iface->bss[i] = NULL;
2089 os_free(hapd_iface->bss);
2090 hapd_iface->bss = NULL;
2093 hapd->msg_ctx = hapd;
2096 hapd_iface->conf = conf;
2097 hapd_iface->num_bss = conf->num_bss;
2103 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2105 struct hostapd_config *conf = NULL;
2106 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2107 struct hostapd_data *hapd;
2110 const char *conf_file = NULL, *phy_name = NULL;
2112 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2114 phy_name = buf + 11;
2115 pos = os_strchr(phy_name, ':');
2120 if (!os_strlen(conf_file))
2123 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2127 for (j = 0; j < interfaces->count; j++) {
2128 if (interfaces->iface[j] == hapd_iface)
2131 if (j == interfaces->count) {
2132 struct hostapd_iface **tmp;
2133 tmp = os_realloc_array(interfaces->iface,
2134 interfaces->count + 1,
2135 sizeof(struct hostapd_iface *));
2137 hostapd_interface_deinit_free(hapd_iface);
2140 interfaces->iface = tmp;
2141 interfaces->iface[interfaces->count++] = hapd_iface;
2142 new_iface = hapd_iface;
2146 if (interfaces->driver_init(hapd_iface))
2149 if (hostapd_setup_interface(hapd_iface)) {
2150 hostapd_deinit_driver(
2151 hapd_iface->bss[0]->driver,
2152 hapd_iface->bss[0]->drv_priv,
2157 /* Assign new BSS with bss[0]'s driver info */
2158 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2159 hapd->driver = hapd_iface->bss[0]->driver;
2160 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2161 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2164 if (start_ctrl_iface_bss(hapd) < 0 ||
2165 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2166 hostapd_setup_bss(hapd, -1))) {
2167 hostapd_cleanup(hapd);
2168 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2169 hapd_iface->conf->num_bss--;
2170 hapd_iface->num_bss--;
2171 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2172 __func__, hapd, hapd->conf->iface);
2173 hostapd_config_free_bss(hapd->conf);
2182 ptr = os_strchr(buf, ' ');
2187 if (os_strncmp(ptr, "config=", 7) == 0)
2188 conf_file = ptr + 7;
2190 for (i = 0; i < interfaces->count; i++) {
2191 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2193 wpa_printf(MSG_INFO, "Cannot add interface - it "
2199 hapd_iface = hostapd_iface_alloc(interfaces);
2200 if (hapd_iface == NULL) {
2201 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2202 "for interface", __func__);
2205 new_iface = hapd_iface;
2207 if (conf_file && interfaces->config_read_cb) {
2208 conf = interfaces->config_read_cb(conf_file);
2209 if (conf && conf->bss)
2210 os_strlcpy(conf->bss[0]->iface, buf,
2211 sizeof(conf->bss[0]->iface));
2213 conf = hostapd_config_alloc(interfaces, buf, ptr);
2214 if (conf == NULL || conf->bss == NULL) {
2215 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2216 "for configuration", __func__);
2220 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2221 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2222 "for hostapd", __func__);
2227 if (start_ctrl_iface(hapd_iface) < 0)
2230 wpa_printf(MSG_INFO, "Add interface '%s'",
2231 hapd_iface->conf->bss[0]->iface);
2237 hostapd_config_free(conf);
2239 if (hapd_iface->bss) {
2240 for (i = 0; i < hapd_iface->num_bss; i++) {
2241 hapd = hapd_iface->bss[i];
2244 if (hapd_iface->interfaces &&
2245 hapd_iface->interfaces->ctrl_iface_deinit)
2246 hapd_iface->interfaces->
2247 ctrl_iface_deinit(hapd);
2248 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2249 __func__, hapd_iface->bss[i],
2251 hostapd_cleanup(hapd);
2253 hapd_iface->bss[i] = NULL;
2255 os_free(hapd_iface->bss);
2256 hapd_iface->bss = NULL;
2259 interfaces->count--;
2260 interfaces->iface[interfaces->count] = NULL;
2262 hostapd_cleanup_iface(hapd_iface);
2268 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2272 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2274 /* Remove hostapd_data only if it has already been initialized */
2275 if (idx < iface->num_bss) {
2276 struct hostapd_data *hapd = iface->bss[idx];
2278 hostapd_bss_deinit(hapd);
2279 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2280 __func__, hapd, hapd->conf->iface);
2281 hostapd_config_free_bss(hapd->conf);
2287 for (i = idx; i < iface->num_bss; i++)
2288 iface->bss[i] = iface->bss[i + 1];
2290 hostapd_config_free_bss(iface->conf->bss[idx]);
2291 iface->conf->bss[idx] = NULL;
2294 iface->conf->num_bss--;
2295 for (i = idx; i < iface->conf->num_bss; i++)
2296 iface->conf->bss[i] = iface->conf->bss[i + 1];
2302 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2304 struct hostapd_iface *hapd_iface;
2307 for (i = 0; i < interfaces->count; i++) {
2308 hapd_iface = interfaces->iface[i];
2309 if (hapd_iface == NULL)
2311 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2312 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2313 hapd_iface->driver_ap_teardown =
2314 !!(hapd_iface->drv_flags &
2315 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2317 hostapd_interface_deinit_free(hapd_iface);
2319 while (k < (interfaces->count - 1)) {
2320 interfaces->iface[k] =
2321 interfaces->iface[k + 1];
2324 interfaces->count--;
2328 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2329 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2330 hapd_iface->driver_ap_teardown =
2331 !(hapd_iface->drv_flags &
2332 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2333 return hostapd_remove_bss(hapd_iface, j);
2342 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2343 * @hapd: Pointer to BSS data
2344 * @sta: Pointer to the associated STA data
2345 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2347 * This function will be called whenever a station associates with the AP. It
2348 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2349 * from drv_callbacks.c based on driver events for drivers that take care of
2350 * management frames (IEEE 802.11 authentication and association) internally.
2352 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2355 if (hapd->tkip_countermeasures) {
2356 hostapd_drv_sta_deauth(hapd, sta->addr,
2357 WLAN_REASON_MICHAEL_MIC_FAILURE);
2361 hostapd_prune_associations(hapd, sta->addr);
2363 /* IEEE 802.11F (IAPP) */
2364 if (hapd->conf->ieee802_11f)
2365 iapp_new_station(hapd->iapp, sta);
2368 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2369 sta->no_p2p_set = 1;
2370 hapd->num_sta_no_p2p++;
2371 if (hapd->num_sta_no_p2p == 1)
2372 hostapd_p2p_non_p2p_sta_connected(hapd);
2374 #endif /* CONFIG_P2P */
2376 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2377 * IEEE 802.1X/WPA code will start accounting after the station has
2378 * been authorized. */
2379 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2380 ap_sta_set_authorized(hapd, sta, 1);
2381 os_get_reltime(&sta->connected_time);
2382 accounting_sta_start(hapd, sta);
2385 /* Start IEEE 802.1X authentication process for new stations */
2386 ieee802_1x_new_station(hapd, sta);
2388 if (sta->auth_alg != WLAN_AUTH_FT &&
2389 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2390 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2392 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2394 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2395 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2396 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2397 __func__, MAC2STR(sta->addr),
2398 hapd->conf->ap_max_inactivity);
2399 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2400 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2401 ap_handle_timer, hapd, sta);
2406 const char * hostapd_state_text(enum hostapd_iface_state s)
2409 case HAPD_IFACE_UNINITIALIZED:
2410 return "UNINITIALIZED";
2411 case HAPD_IFACE_DISABLED:
2413 case HAPD_IFACE_COUNTRY_UPDATE:
2414 return "COUNTRY_UPDATE";
2415 case HAPD_IFACE_ACS:
2417 case HAPD_IFACE_HT_SCAN:
2419 case HAPD_IFACE_DFS:
2421 case HAPD_IFACE_ENABLED:
2429 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2431 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2432 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2433 hostapd_state_text(s));
2440 static void free_beacon_data(struct beacon_data *beacon)
2442 os_free(beacon->head);
2443 beacon->head = NULL;
2444 os_free(beacon->tail);
2445 beacon->tail = NULL;
2446 os_free(beacon->probe_resp);
2447 beacon->probe_resp = NULL;
2448 os_free(beacon->beacon_ies);
2449 beacon->beacon_ies = NULL;
2450 os_free(beacon->proberesp_ies);
2451 beacon->proberesp_ies = NULL;
2452 os_free(beacon->assocresp_ies);
2453 beacon->assocresp_ies = NULL;
2457 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2458 struct beacon_data *beacon)
2460 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2461 struct wpa_driver_ap_params params;
2464 os_memset(beacon, 0, sizeof(*beacon));
2465 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2469 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2473 goto free_ap_params;
2476 beacon->head = os_malloc(params.head_len);
2478 goto free_ap_extra_ies;
2480 os_memcpy(beacon->head, params.head, params.head_len);
2481 beacon->head_len = params.head_len;
2483 beacon->tail = os_malloc(params.tail_len);
2487 os_memcpy(beacon->tail, params.tail, params.tail_len);
2488 beacon->tail_len = params.tail_len;
2490 if (params.proberesp != NULL) {
2491 beacon->probe_resp = os_malloc(params.proberesp_len);
2492 if (!beacon->probe_resp)
2495 os_memcpy(beacon->probe_resp, params.proberesp,
2496 params.proberesp_len);
2497 beacon->probe_resp_len = params.proberesp_len;
2500 /* copy the extra ies */
2502 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2503 if (!beacon->beacon_ies)
2506 os_memcpy(beacon->beacon_ies,
2507 beacon_extra->buf, wpabuf_len(beacon_extra));
2508 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2511 if (proberesp_extra) {
2512 beacon->proberesp_ies =
2513 os_malloc(wpabuf_len(proberesp_extra));
2514 if (!beacon->proberesp_ies)
2517 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2518 wpabuf_len(proberesp_extra));
2519 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2522 if (assocresp_extra) {
2523 beacon->assocresp_ies =
2524 os_malloc(wpabuf_len(assocresp_extra));
2525 if (!beacon->assocresp_ies)
2528 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2529 wpabuf_len(assocresp_extra));
2530 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2535 /* if the function fails, the caller should not free beacon data */
2537 free_beacon_data(beacon);
2540 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2543 ieee802_11_free_ap_params(¶ms);
2549 * TODO: This flow currently supports only changing frequency within the
2550 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2551 * width) are not supported.
2553 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2554 struct hostapd_config *conf,
2555 struct hostapd_freq_params *params,
2556 struct hostapd_freq_params *old_params)
2560 if (!params->channel) {
2561 /* check if the new channel is supported by hw */
2562 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2565 channel = params->channel;
2569 /* if a pointer to old_params is provided we save previous state */
2571 old_params->channel = conf->channel;
2572 old_params->ht_enabled = conf->ieee80211n;
2573 old_params->sec_channel_offset = conf->secondary_channel;
2576 conf->channel = channel;
2577 conf->ieee80211n = params->ht_enabled;
2578 conf->secondary_channel = params->sec_channel_offset;
2580 /* TODO: maybe call here hostapd_config_check here? */
2586 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2587 struct csa_settings *settings)
2589 struct hostapd_iface *iface = hapd->iface;
2590 struct hostapd_freq_params old_freq;
2593 os_memset(&old_freq, 0, sizeof(old_freq));
2594 if (!iface || !iface->freq || hapd->csa_in_progress)
2597 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2598 &settings->freq_params,
2603 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2605 /* change back the configuration */
2606 hostapd_change_config_freq(iface->bss[0], iface->conf,
2612 /* set channel switch parameters for csa ie */
2613 hapd->cs_freq_params = settings->freq_params;
2614 hapd->cs_count = settings->cs_count;
2615 hapd->cs_block_tx = settings->block_tx;
2617 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2619 free_beacon_data(&settings->beacon_after);
2623 settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2624 settings->counter_offset_presp = hapd->cs_c_off_proberesp;
2630 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2632 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2634 hapd->cs_block_tx = 0;
2635 hapd->cs_c_off_beacon = 0;
2636 hapd->cs_c_off_proberesp = 0;
2637 hapd->csa_in_progress = 0;
2641 int hostapd_switch_channel(struct hostapd_data *hapd,
2642 struct csa_settings *settings)
2646 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2647 wpa_printf(MSG_INFO, "CSA is not supported");
2651 ret = hostapd_fill_csa_settings(hapd, settings);
2655 ret = hostapd_drv_switch_channel(hapd, settings);
2656 free_beacon_data(&settings->beacon_csa);
2657 free_beacon_data(&settings->beacon_after);
2660 /* if we failed, clean cs parameters */
2661 hostapd_cleanup_cs_params(hapd);
2665 hapd->csa_in_progress = 1;
2671 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2672 const struct hostapd_freq_params *freq_params)
2674 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2677 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2679 if (freq_params->center_freq1)
2680 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2681 if (freq_params->center_freq2)
2682 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2684 switch (freq_params->bandwidth) {
2688 vht_bw = VHT_CHANWIDTH_USE_HT;
2691 if (freq_params->center_freq2)
2692 vht_bw = VHT_CHANWIDTH_80P80MHZ;
2694 vht_bw = VHT_CHANWIDTH_80MHZ;
2697 vht_bw = VHT_CHANWIDTH_160MHZ;
2700 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2701 freq_params->bandwidth);
2705 iface->freq = freq_params->freq;
2706 iface->conf->channel = freq_params->channel;
2707 iface->conf->secondary_channel = freq_params->sec_channel_offset;
2708 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2709 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2710 iface->conf->vht_oper_chwidth = vht_bw;
2711 iface->conf->ieee80211n = freq_params->ht_enabled;
2712 iface->conf->ieee80211ac = freq_params->vht_enabled;
2715 * cs_params must not be cleared earlier because the freq_params
2716 * argument may actually point to one of these.
2718 for (i = 0; i < iface->num_bss; i++)
2719 hostapd_cleanup_cs_params(iface->bss[i]);
2721 hostapd_disable_iface(iface);
2722 hostapd_enable_iface(iface);
2725 #endif /* NEED_AP_MLME */