2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "radius/radius_client.h"
16 #include "radius/radius_das.h"
17 #include "eap_server/tncs.h"
18 #include "eapol_auth/eapol_auth_sm.h"
19 #include "eapol_auth/eapol_auth_sm_i.h"
24 #include "accounting.h"
28 #include "ieee802_1x.h"
29 #include "ieee802_11_auth.h"
30 #include "vlan_init.h"
32 #include "wps_hostapd.h"
33 #include "hw_features.h"
34 #include "wpa_auth_glue.h"
35 #include "ap_drv_ops.h"
36 #include "ap_config.h"
37 #include "p2p_hostapd.h"
40 #include "ieee802_11.h"
43 #include "dhcp_snoop.h"
44 #include "ndisc_snoop.h"
47 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
48 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
49 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
50 static int setup_interface2(struct hostapd_iface *iface);
51 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
54 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
55 int (*cb)(struct hostapd_iface *iface,
56 void *ctx), void *ctx)
61 for (i = 0; i < interfaces->count; i++) {
62 ret = cb(interfaces->iface[i], ctx);
71 static void hostapd_reload_bss(struct hostapd_data *hapd)
73 struct hostapd_ssid *ssid;
75 #ifndef CONFIG_NO_RADIUS
76 radius_client_reconfig(hapd->radius, hapd->conf->radius);
77 #endif /* CONFIG_NO_RADIUS */
79 ssid = &hapd->conf->ssid;
80 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
81 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
83 * Force PSK to be derived again since SSID or passphrase may
86 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
88 if (hostapd_setup_wpa_psk(hapd->conf)) {
89 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
90 "after reloading configuration");
93 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
94 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
96 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
98 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
99 hostapd_setup_wpa(hapd);
101 wpa_init_keys(hapd->wpa_auth);
102 } else if (hapd->conf->wpa) {
105 hostapd_reconfig_wpa(hapd);
106 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
107 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
108 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
109 "the kernel driver.");
110 } else if (hapd->wpa_auth) {
111 wpa_deinit(hapd->wpa_auth);
112 hapd->wpa_auth = NULL;
113 hostapd_set_privacy(hapd, 0);
114 hostapd_setup_encryption(hapd->conf->iface, hapd);
115 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
118 ieee802_11_set_beacon(hapd);
119 hostapd_update_wps(hapd);
121 if (hapd->conf->ssid.ssid_set &&
122 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
123 hapd->conf->ssid.ssid_len)) {
124 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
125 /* try to continue */
127 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
131 static void hostapd_clear_old(struct hostapd_iface *iface)
136 * Deauthenticate all stations since the new configuration may not
137 * allow them to use the BSS anymore.
139 for (j = 0; j < iface->num_bss; j++) {
140 hostapd_flush_old_stations(iface->bss[j],
141 WLAN_REASON_PREV_AUTH_NOT_VALID);
142 hostapd_broadcast_wep_clear(iface->bss[j]);
144 #ifndef CONFIG_NO_RADIUS
145 /* TODO: update dynamic data based on changed configuration
146 * items (e.g., open/close sockets, etc.) */
147 radius_client_flush(iface->bss[j]->radius, 0);
148 #endif /* CONFIG_NO_RADIUS */
153 int hostapd_reload_config(struct hostapd_iface *iface)
155 struct hostapd_data *hapd = iface->bss[0];
156 struct hostapd_config *newconf, *oldconf;
159 if (iface->config_fname == NULL) {
160 /* Only in-memory config in use - assume it has been updated */
161 hostapd_clear_old(iface);
162 for (j = 0; j < iface->num_bss; j++)
163 hostapd_reload_bss(iface->bss[j]);
167 if (iface->interfaces == NULL ||
168 iface->interfaces->config_read_cb == NULL)
170 newconf = iface->interfaces->config_read_cb(iface->config_fname);
174 hostapd_clear_old(iface);
176 oldconf = hapd->iconf;
177 iface->conf = newconf;
179 for (j = 0; j < iface->num_bss; j++) {
180 hapd = iface->bss[j];
181 hapd->iconf = newconf;
182 hapd->iconf->channel = oldconf->channel;
183 hapd->iconf->acs = oldconf->acs;
184 hapd->iconf->secondary_channel = oldconf->secondary_channel;
185 hapd->iconf->ieee80211n = oldconf->ieee80211n;
186 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
187 hapd->iconf->ht_capab = oldconf->ht_capab;
188 hapd->iconf->vht_capab = oldconf->vht_capab;
189 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
190 hapd->iconf->vht_oper_centr_freq_seg0_idx =
191 oldconf->vht_oper_centr_freq_seg0_idx;
192 hapd->iconf->vht_oper_centr_freq_seg1_idx =
193 oldconf->vht_oper_centr_freq_seg1_idx;
194 hapd->conf = newconf->bss[j];
195 hostapd_reload_bss(hapd);
198 hostapd_config_free(oldconf);
205 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
210 for (i = 0; i < NUM_WEP_KEYS; i++) {
211 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
212 0, NULL, 0, NULL, 0)) {
213 wpa_printf(MSG_DEBUG, "Failed to clear default "
214 "encryption keys (ifname=%s keyidx=%d)",
218 #ifdef CONFIG_IEEE80211W
219 if (hapd->conf->ieee80211w) {
220 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
221 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
224 wpa_printf(MSG_DEBUG, "Failed to clear "
225 "default mgmt encryption keys "
226 "(ifname=%s keyidx=%d)", ifname, i);
230 #endif /* CONFIG_IEEE80211W */
234 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
236 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
241 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
244 struct hostapd_ssid *ssid = &hapd->conf->ssid;
247 if (ssid->wep.default_len &&
248 hostapd_drv_set_key(hapd->conf->iface,
249 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
250 1, NULL, 0, ssid->wep.key[idx],
251 ssid->wep.len[idx])) {
252 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
260 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
262 os_free(hapd->probereq_cb);
263 hapd->probereq_cb = NULL;
266 wpabuf_free(hapd->p2p_beacon_ie);
267 hapd->p2p_beacon_ie = NULL;
268 wpabuf_free(hapd->p2p_probe_resp_ie);
269 hapd->p2p_probe_resp_ie = NULL;
270 #endif /* CONFIG_P2P */
272 if (!hapd->started) {
273 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
274 __func__, hapd->conf->iface);
279 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
280 iapp_deinit(hapd->iapp);
282 accounting_deinit(hapd);
283 hostapd_deinit_wpa(hapd);
285 hostapd_acl_deinit(hapd);
286 #ifndef CONFIG_NO_RADIUS
287 radius_client_deinit(hapd->radius);
289 radius_das_deinit(hapd->radius_das);
290 hapd->radius_das = NULL;
291 #endif /* CONFIG_NO_RADIUS */
293 hostapd_deinit_wps(hapd);
295 authsrv_deinit(hapd);
297 if (hapd->interface_added) {
298 hapd->interface_added = 0;
299 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
300 wpa_printf(MSG_WARNING,
301 "Failed to remove BSS interface %s",
303 hapd->interface_added = 1;
306 * Since this was a dynamically added interface, the
307 * driver wrapper may have removed its internal instance
308 * and hapd->drv_priv is not valid anymore.
310 hapd->drv_priv = NULL;
314 wpabuf_free(hapd->time_adv);
316 #ifdef CONFIG_INTERWORKING
317 gas_serv_deinit(hapd);
318 #endif /* CONFIG_INTERWORKING */
320 bss_load_update_deinit(hapd);
321 ndisc_snoop_deinit(hapd);
322 dhcp_snoop_deinit(hapd);
323 x_snoop_deinit(hapd);
326 bin_clear_free(hapd->tmp_eap_user.identity,
327 hapd->tmp_eap_user.identity_len);
328 bin_clear_free(hapd->tmp_eap_user.password,
329 hapd->tmp_eap_user.password_len);
330 #endif /* CONFIG_SQLITE */
333 wpabuf_free(hapd->mesh_pending_auth);
334 hapd->mesh_pending_auth = NULL;
335 #endif /* CONFIG_MESH */
340 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
341 * @hapd: Pointer to BSS data
343 * This function is used to free all per-BSS data structures and resources.
344 * Most of the modules that are initialized in hostapd_setup_bss() are
345 * deinitialized here.
347 static void hostapd_cleanup(struct hostapd_data *hapd)
349 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
351 if (hapd->iface->interfaces &&
352 hapd->iface->interfaces->ctrl_iface_deinit)
353 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
354 hostapd_free_hapd_data(hapd);
358 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
360 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
361 #ifdef CONFIG_IEEE80211N
363 hostapd_stop_setup_timers(iface);
364 #endif /* NEED_AP_MLME */
365 #endif /* CONFIG_IEEE80211N */
366 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
367 iface->hw_features = NULL;
368 os_free(iface->current_rates);
369 iface->current_rates = NULL;
370 os_free(iface->basic_rates);
371 iface->basic_rates = NULL;
372 ap_list_deinit(iface);
377 * hostapd_cleanup_iface - Complete per-interface cleanup
378 * @iface: Pointer to interface data
380 * This function is called after per-BSS data structures are deinitialized
381 * with hostapd_cleanup().
383 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
385 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
386 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
388 hostapd_cleanup_iface_partial(iface);
389 hostapd_config_free(iface->conf);
392 os_free(iface->config_fname);
394 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
399 static void hostapd_clear_wep(struct hostapd_data *hapd)
401 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
402 hostapd_set_privacy(hapd, 0);
403 hostapd_broadcast_wep_clear(hapd);
408 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
412 hostapd_broadcast_wep_set(hapd);
414 if (hapd->conf->ssid.wep.default_len) {
415 hostapd_set_privacy(hapd, 1);
420 * When IEEE 802.1X is not enabled, the driver may need to know how to
421 * set authentication algorithms for static WEP.
423 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
425 for (i = 0; i < 4; i++) {
426 if (hapd->conf->ssid.wep.key[i] &&
427 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
428 i == hapd->conf->ssid.wep.idx, NULL, 0,
429 hapd->conf->ssid.wep.key[i],
430 hapd->conf->ssid.wep.len[i])) {
431 wpa_printf(MSG_WARNING, "Could not set WEP "
435 if (hapd->conf->ssid.wep.key[i] &&
436 i == hapd->conf->ssid.wep.idx)
437 hostapd_set_privacy(hapd, 1);
444 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
449 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
452 if (!hapd->iface->driver_ap_teardown) {
453 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
454 "Flushing old station entries");
456 if (hostapd_flush(hapd)) {
457 wpa_msg(hapd->msg_ctx, MSG_WARNING,
458 "Could not connect to kernel driver");
462 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
463 os_memset(addr, 0xff, ETH_ALEN);
464 hostapd_drv_sta_deauth(hapd, addr, reason);
465 hostapd_free_stas(hapd);
471 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
473 hostapd_free_stas(hapd);
474 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
475 hostapd_clear_wep(hapd);
480 * hostapd_validate_bssid_configuration - Validate BSSID configuration
481 * @iface: Pointer to interface data
482 * Returns: 0 on success, -1 on failure
484 * This function is used to validate that the configured BSSIDs are valid.
486 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
488 u8 mask[ETH_ALEN] = { 0 };
489 struct hostapd_data *hapd = iface->bss[0];
490 unsigned int i = iface->conf->num_bss, bits = 0, j;
493 if (hostapd_drv_none(hapd))
496 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
498 /* Determine the bits necessary to cover the number of BSSIDs. */
499 for (i--; i; i >>= 1)
502 /* Determine the bits necessary to any configured BSSIDs,
503 if they are higher than the number of BSSIDs. */
504 for (j = 0; j < iface->conf->num_bss; j++) {
505 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
511 for (i = 0; i < ETH_ALEN; i++) {
513 iface->conf->bss[j]->bssid[i] ^
521 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
527 while (mask[i] != 0) {
537 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
542 os_memset(mask, 0xff, ETH_ALEN);
544 for (i = 5; i > 5 - j; i--)
551 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
552 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
557 for (i = 0; i < ETH_ALEN; i++) {
558 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
559 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
560 " for start address " MACSTR ".",
561 MAC2STR(mask), MAC2STR(hapd->own_addr));
562 wpa_printf(MSG_ERROR, "Start address must be the "
563 "first address in the block (i.e., addr "
564 "AND mask == addr).");
573 static int mac_in_conf(struct hostapd_config *conf, const void *a)
577 for (i = 0; i < conf->num_bss; i++) {
578 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
587 #ifndef CONFIG_NO_RADIUS
589 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
590 struct radius_das_attrs *attr)
592 if (attr->nas_identifier &&
593 (!hapd->conf->nas_identifier ||
594 os_strlen(hapd->conf->nas_identifier) !=
595 attr->nas_identifier_len ||
596 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
597 attr->nas_identifier_len) != 0)) {
598 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
602 if (attr->nas_ip_addr &&
603 (hapd->conf->own_ip_addr.af != AF_INET ||
604 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
606 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
611 if (attr->nas_ipv6_addr &&
612 (hapd->conf->own_ip_addr.af != AF_INET6 ||
613 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
615 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
618 #endif /* CONFIG_IPV6 */
624 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
625 struct radius_das_attrs *attr,
628 struct sta_info *selected, *sta;
635 for (sta = hapd->sta_list; sta; sta = sta->next)
636 sta->radius_das_match = 1;
638 if (attr->sta_addr) {
640 sta = ap_get_sta(hapd, attr->sta_addr);
642 wpa_printf(MSG_DEBUG,
643 "RADIUS DAS: No Calling-Station-Id match");
648 for (sta = hapd->sta_list; sta; sta = sta->next) {
650 sta->radius_das_match = 0;
652 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
655 if (attr->acct_session_id) {
657 if (attr->acct_session_id_len != 17) {
658 wpa_printf(MSG_DEBUG,
659 "RADIUS DAS: Acct-Session-Id cannot match");
664 for (sta = hapd->sta_list; sta; sta = sta->next) {
665 if (!sta->radius_das_match)
667 os_snprintf(buf, sizeof(buf), "%08X-%08X",
668 sta->acct_session_id_hi,
669 sta->acct_session_id_lo);
670 if (os_memcmp(attr->acct_session_id, buf, 17) != 0)
671 sta->radius_das_match = 0;
677 wpa_printf(MSG_DEBUG,
678 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
681 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
684 if (attr->acct_multi_session_id) {
686 if (attr->acct_multi_session_id_len != 17) {
687 wpa_printf(MSG_DEBUG,
688 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
693 for (sta = hapd->sta_list; sta; sta = sta->next) {
694 if (!sta->radius_das_match)
696 if (!sta->eapol_sm ||
697 !sta->eapol_sm->acct_multi_session_id_hi) {
698 sta->radius_das_match = 0;
701 os_snprintf(buf, sizeof(buf), "%08X+%08X",
702 sta->eapol_sm->acct_multi_session_id_hi,
703 sta->eapol_sm->acct_multi_session_id_lo);
704 if (os_memcmp(attr->acct_multi_session_id, buf, 17) !=
706 sta->radius_das_match = 0;
712 wpa_printf(MSG_DEBUG,
713 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
716 wpa_printf(MSG_DEBUG,
717 "RADIUS DAS: Acct-Multi-Session-Id match");
724 for (sta = hapd->sta_list; sta; sta = sta->next) {
727 if (!sta->radius_das_match)
729 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
730 if (!cui || wpabuf_len(cui) != attr->cui_len ||
731 os_memcmp(wpabuf_head(cui), attr->cui,
733 sta->radius_das_match = 0;
739 wpa_printf(MSG_DEBUG,
740 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
743 wpa_printf(MSG_DEBUG,
744 "RADIUS DAS: Chargeable-User-Identity match");
747 if (attr->user_name) {
751 for (sta = hapd->sta_list; sta; sta = sta->next) {
755 if (!sta->radius_das_match)
757 identity = ieee802_1x_get_identity(sta->eapol_sm,
760 identity_len != attr->user_name_len ||
761 os_memcmp(identity, attr->user_name, identity_len)
763 sta->radius_das_match = 0;
769 wpa_printf(MSG_DEBUG,
770 "RADIUS DAS: No matches remaining after User-Name check");
773 wpa_printf(MSG_DEBUG,
774 "RADIUS DAS: User-Name match");
779 * In theory, we could match all current associations, but it
780 * seems safer to just reject requests that do not include any
781 * session identification attributes.
783 wpa_printf(MSG_DEBUG,
784 "RADIUS DAS: No session identification attributes included");
789 for (sta = hapd->sta_list; sta; sta = sta->next) {
790 if (sta->radius_das_match) {
803 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
804 struct radius_das_attrs *attr)
808 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
812 static enum radius_das_res
813 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
815 struct hostapd_data *hapd = ctx;
816 struct sta_info *sta;
819 if (hostapd_das_nas_mismatch(hapd, attr))
820 return RADIUS_DAS_NAS_MISMATCH;
822 sta = hostapd_das_find_sta(hapd, attr, &multi);
825 wpa_printf(MSG_DEBUG,
826 "RADIUS DAS: Multiple sessions match - not supported");
827 return RADIUS_DAS_MULTI_SESSION_MATCH;
829 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
830 wpa_printf(MSG_DEBUG,
831 "RADIUS DAS: PMKSA cache entry matched");
832 return RADIUS_DAS_SUCCESS;
834 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
835 return RADIUS_DAS_SESSION_NOT_FOUND;
838 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
839 " - disconnecting", MAC2STR(sta->addr));
840 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
842 hostapd_drv_sta_deauth(hapd, sta->addr,
843 WLAN_REASON_PREV_AUTH_NOT_VALID);
844 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
846 return RADIUS_DAS_SUCCESS;
849 #endif /* CONFIG_NO_RADIUS */
853 * hostapd_setup_bss - Per-BSS setup (initialization)
854 * @hapd: Pointer to BSS data
855 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
856 * but interface may exist
858 * This function is used to initialize all per-BSS data structures and
859 * resources. This gets called in a loop for each BSS when an interface is
860 * initialized. Most of the modules that are initialized here will be
861 * deinitialized in hostapd_cleanup().
863 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
865 struct hostapd_bss_config *conf = hapd->conf;
866 u8 ssid[SSID_MAX_LEN + 1];
867 int ssid_len, set_ssid;
868 char force_ifname[IFNAMSIZ];
869 u8 if_addr[ETH_ALEN];
870 int flush_old_stations = 1;
872 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
873 __func__, hapd, conf->iface, first);
875 #ifdef EAP_SERVER_TNC
876 if (conf->tnc && tncs_global_init() < 0) {
877 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
880 #endif /* EAP_SERVER_TNC */
883 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
884 __func__, conf->iface);
889 if (!first || first == -1) {
890 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
891 /* Allocate the next available BSSID. */
893 inc_byte_array(hapd->own_addr, ETH_ALEN);
894 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
896 /* Allocate the configured BSSID. */
897 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
899 if (hostapd_mac_comp(hapd->own_addr,
900 hapd->iface->bss[0]->own_addr) ==
902 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
903 "BSSID set to the MAC address of "
904 "the radio", conf->iface);
909 hapd->interface_added = 1;
910 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
911 conf->iface, hapd->own_addr, hapd,
912 &hapd->drv_priv, force_ifname, if_addr,
913 conf->bridge[0] ? conf->bridge : NULL,
915 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
916 MACSTR ")", MAC2STR(hapd->own_addr));
917 hapd->interface_added = 0;
922 if (conf->wmm_enabled < 0)
923 conf->wmm_enabled = hapd->iconf->ieee80211n;
926 if (hapd->iface->mconf == NULL)
927 flush_old_stations = 0;
928 #endif /* CONFIG_MESH */
930 if (flush_old_stations)
931 hostapd_flush_old_stations(hapd,
932 WLAN_REASON_PREV_AUTH_NOT_VALID);
933 hostapd_set_privacy(hapd, 0);
935 hostapd_broadcast_wep_clear(hapd);
936 if (hostapd_setup_encryption(conf->iface, hapd))
940 * Fetch the SSID from the system and use it or,
941 * if one was specified in the config file, verify they
944 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
946 wpa_printf(MSG_ERROR, "Could not read SSID from system");
949 if (conf->ssid.ssid_set) {
951 * If SSID is specified in the config file and it differs
952 * from what is being used then force installation of the
955 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
956 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
959 * No SSID in the config file; just use the one we got
963 conf->ssid.ssid_len = ssid_len;
964 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
967 if (!hostapd_drv_none(hapd)) {
968 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
970 conf->iface, MAC2STR(hapd->own_addr),
971 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
974 if (hostapd_setup_wpa_psk(conf)) {
975 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
979 /* Set SSID for the kernel driver (to be used in beacon and probe
980 * response frames) */
981 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
982 conf->ssid.ssid_len)) {
983 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
987 if (wpa_debug_level <= MSG_MSGDUMP)
988 conf->radius->msg_dumps = 1;
989 #ifndef CONFIG_NO_RADIUS
990 hapd->radius = radius_client_init(hapd, conf->radius);
991 if (hapd->radius == NULL) {
992 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
996 if (conf->radius_das_port) {
997 struct radius_das_conf das_conf;
998 os_memset(&das_conf, 0, sizeof(das_conf));
999 das_conf.port = conf->radius_das_port;
1000 das_conf.shared_secret = conf->radius_das_shared_secret;
1001 das_conf.shared_secret_len =
1002 conf->radius_das_shared_secret_len;
1003 das_conf.client_addr = &conf->radius_das_client_addr;
1004 das_conf.time_window = conf->radius_das_time_window;
1005 das_conf.require_event_timestamp =
1006 conf->radius_das_require_event_timestamp;
1007 das_conf.ctx = hapd;
1008 das_conf.disconnect = hostapd_das_disconnect;
1009 hapd->radius_das = radius_das_init(&das_conf);
1010 if (hapd->radius_das == NULL) {
1011 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1016 #endif /* CONFIG_NO_RADIUS */
1018 if (hostapd_acl_init(hapd)) {
1019 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1022 if (hostapd_init_wps(hapd, conf))
1025 if (authsrv_init(hapd) < 0)
1028 if (ieee802_1x_init(hapd)) {
1029 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1033 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1036 if (accounting_init(hapd)) {
1037 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1041 if (conf->ieee802_11f &&
1042 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1043 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1048 #ifdef CONFIG_INTERWORKING
1049 if (gas_serv_init(hapd)) {
1050 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1054 if (conf->qos_map_set_len &&
1055 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1056 conf->qos_map_set_len)) {
1057 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1060 #endif /* CONFIG_INTERWORKING */
1062 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1063 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1067 if (conf->proxy_arp) {
1068 if (x_snoop_init(hapd)) {
1069 wpa_printf(MSG_ERROR,
1070 "Generic snooping infrastructure initialization failed");
1074 if (dhcp_snoop_init(hapd)) {
1075 wpa_printf(MSG_ERROR,
1076 "DHCP snooping initialization failed");
1080 if (ndisc_snoop_init(hapd)) {
1081 wpa_printf(MSG_ERROR,
1082 "Neighbor Discovery snooping initialization failed");
1087 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1088 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1092 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1095 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1098 if (hapd->driver && hapd->driver->set_operstate)
1099 hapd->driver->set_operstate(hapd->drv_priv, 1);
1105 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1107 struct hostapd_data *hapd = iface->bss[0];
1109 struct hostapd_tx_queue_params *p;
1112 if (iface->mconf == NULL)
1114 #endif /* CONFIG_MESH */
1116 for (i = 0; i < NUM_TX_QUEUES; i++) {
1117 p = &iface->conf->tx_queue[i];
1119 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1120 p->cwmax, p->burst)) {
1121 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1122 "parameters for queue %d.", i);
1123 /* Continue anyway */
1129 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1130 struct mac_acl_entry *mac_acl,
1131 int n_entries, u8 accept_acl)
1133 struct hostapd_acl_params *acl_params;
1136 acl_params = os_zalloc(sizeof(*acl_params) +
1137 (n_entries * sizeof(acl_params->mac_acl[0])));
1141 for (i = 0; i < n_entries; i++)
1142 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1145 acl_params->acl_policy = accept_acl;
1146 acl_params->num_mac_acl = n_entries;
1148 err = hostapd_drv_set_acl(hapd, acl_params);
1150 os_free(acl_params);
1156 static void hostapd_set_acl(struct hostapd_data *hapd)
1158 struct hostapd_config *conf = hapd->iconf;
1162 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1165 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1167 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1168 conf->bss[0]->num_accept_mac,
1171 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1174 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1176 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1177 conf->bss[0]->num_deny_mac,
1180 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1187 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1189 if (!hapd->iface->interfaces ||
1190 !hapd->iface->interfaces->ctrl_iface_init)
1193 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1194 wpa_printf(MSG_ERROR,
1195 "Failed to setup control interface for %s",
1204 static int start_ctrl_iface(struct hostapd_iface *iface)
1208 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1211 for (i = 0; i < iface->num_bss; i++) {
1212 struct hostapd_data *hapd = iface->bss[i];
1213 if (iface->interfaces->ctrl_iface_init(hapd)) {
1214 wpa_printf(MSG_ERROR,
1215 "Failed to setup control interface for %s",
1225 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1227 struct hostapd_iface *iface = eloop_ctx;
1229 if (!iface->wait_channel_update) {
1230 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1235 * It is possible that the existing channel list is acceptable, so try
1238 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1239 setup_interface2(iface);
1243 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1245 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1248 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1249 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1250 setup_interface2(iface);
1254 static int setup_interface(struct hostapd_iface *iface)
1256 struct hostapd_data *hapd = iface->bss[0];
1260 * It is possible that setup_interface() is called after the interface
1261 * was disabled etc., in which case driver_ap_teardown is possibly set
1262 * to 1. Clear it here so any other key/station deletion, which is not
1263 * part of a teardown flow, would also call the relevant driver
1266 iface->driver_ap_teardown = 0;
1268 if (!iface->phy[0]) {
1269 const char *phy = hostapd_drv_get_radio_name(hapd);
1271 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1272 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1277 * Make sure that all BSSes get configured with a pointer to the same
1280 for (i = 1; i < iface->num_bss; i++) {
1281 iface->bss[i]->driver = hapd->driver;
1282 iface->bss[i]->drv_priv = hapd->drv_priv;
1285 if (hostapd_validate_bssid_configuration(iface))
1289 * Initialize control interfaces early to allow external monitoring of
1290 * channel setup operations that may take considerable amount of time
1291 * especially for DFS cases.
1293 if (start_ctrl_iface(iface))
1296 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1297 char country[4], previous_country[4];
1299 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1300 if (hostapd_get_country(hapd, previous_country) < 0)
1301 previous_country[0] = '\0';
1303 os_memcpy(country, hapd->iconf->country, 3);
1305 if (hostapd_set_country(hapd, country) < 0) {
1306 wpa_printf(MSG_ERROR, "Failed to set country code");
1310 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1311 previous_country, country);
1313 if (os_strncmp(previous_country, country, 2) != 0) {
1314 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1315 iface->wait_channel_update = 1;
1316 eloop_register_timeout(5, 0,
1317 channel_list_update_timeout,
1323 return setup_interface2(iface);
1327 static int setup_interface2(struct hostapd_iface *iface)
1329 iface->wait_channel_update = 0;
1331 if (hostapd_get_hw_features(iface)) {
1332 /* Not all drivers support this yet, so continue without hw
1335 int ret = hostapd_select_hw_mode(iface);
1337 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1338 "channel. (%d)", ret);
1342 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1345 ret = hostapd_check_ht_capab(iface);
1349 wpa_printf(MSG_DEBUG, "Interface initialization will "
1350 "be completed in a callback");
1354 if (iface->conf->ieee80211h)
1355 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1357 return hostapd_setup_interface_complete(iface, 0);
1360 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1361 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1362 if (iface->interfaces && iface->interfaces->terminate_on_error)
1370 static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1372 struct hostapd_data *hapd = ctx;
1374 return hapd->own_addr;
1378 static void fst_hostapd_get_channel_info_cb(void *ctx,
1379 enum hostapd_hw_mode *hw_mode,
1382 struct hostapd_data *hapd = ctx;
1384 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1388 static void fst_hostapd_set_ies_cb(void *ctx, struct wpabuf *fst_ies)
1390 struct hostapd_data *hapd = ctx;
1392 if (hapd->iface->fst_ies != fst_ies) {
1393 hapd->iface->fst_ies = fst_ies;
1394 if (ieee802_11_set_beacon(hapd))
1395 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1400 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1403 struct hostapd_data *hapd = ctx;
1405 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1406 wpabuf_head(buf), wpabuf_len(buf));
1410 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1412 struct hostapd_data *hapd = ctx;
1413 struct sta_info *sta = ap_get_sta(hapd, addr);
1415 return sta ? sta->mb_ies : NULL;
1419 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1420 const u8 *buf, size_t size)
1422 struct hostapd_data *hapd = ctx;
1423 struct sta_info *sta = ap_get_sta(hapd, addr);
1426 struct mb_ies_info info;
1428 if (!mb_ies_info_by_ies(&info, buf, size)) {
1429 wpabuf_free(sta->mb_ies);
1430 sta->mb_ies = mb_ies_by_info(&info);
1436 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1439 struct sta_info *s = (struct sta_info *) *get_ctx;
1442 for (; s && !s->mb_ies; s = s->next)
1447 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1457 static const u8 * fst_hostapd_get_peer_first(void *ctx,
1458 struct fst_get_peer_ctx **get_ctx,
1461 struct hostapd_data *hapd = ctx;
1463 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1465 return fst_hostapd_get_sta(get_ctx, mb_only);
1469 static const u8 * fst_hostapd_get_peer_next(void *ctx,
1470 struct fst_get_peer_ctx **get_ctx,
1473 return fst_hostapd_get_sta(get_ctx, mb_only);
1477 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1478 struct fst_wpa_obj *iface_obj)
1480 iface_obj->ctx = hapd;
1481 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1482 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1483 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1484 iface_obj->send_action = fst_hostapd_send_action_cb;
1485 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1486 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1487 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1488 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1491 #endif /* CONFIG_FST */
1495 * hostapd_setup_interface_complete - Complete interface setup
1497 * This function is called when previous steps in the interface setup has been
1498 * completed. This can also start operations, e.g., DFS, that will require
1499 * additional processing before interface is ready to be enabled. Such
1500 * operations will call this function from eloop callbacks when finished.
1502 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1504 struct hostapd_data *hapd = iface->bss[0];
1507 int delay_apply_cfg = 0;
1508 int res_dfs_offload = 0;
1513 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1514 if (iface->conf->channel) {
1517 #endif /* NEED_AP_MLME */
1519 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1520 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1521 "Frequency: %d MHz",
1522 hostapd_hw_mode_txt(iface->conf->hw_mode),
1523 iface->conf->channel, iface->freq);
1526 /* Handle DFS only if it is not offloaded to the driver */
1527 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1529 res = hostapd_handle_dfs(iface);
1536 /* If DFS is offloaded to the driver */
1537 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1538 if (res_dfs_offload <= 0) {
1539 if (res_dfs_offload < 0)
1542 wpa_printf(MSG_DEBUG,
1543 "Proceed with AP/channel setup");
1545 * If this is a DFS channel, move to completing
1548 if (res_dfs_offload == 1)
1550 /* Otherwise fall through. */
1553 #endif /* NEED_AP_MLME */
1556 if (iface->mconf != NULL) {
1557 wpa_printf(MSG_DEBUG,
1558 "%s: Mesh configuration will be applied while joining the mesh network",
1559 iface->bss[0]->conf->iface);
1560 delay_apply_cfg = 1;
1562 #endif /* CONFIG_MESH */
1564 if (!delay_apply_cfg &&
1565 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1566 hapd->iconf->channel,
1567 hapd->iconf->ieee80211n,
1568 hapd->iconf->ieee80211ac,
1569 hapd->iconf->secondary_channel,
1570 hapd->iconf->vht_oper_chwidth,
1571 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1572 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1573 wpa_printf(MSG_ERROR, "Could not set channel for "
1579 if (iface->current_mode) {
1580 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1581 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1583 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1584 HOSTAPD_LEVEL_WARNING,
1585 "Failed to prepare rates table.");
1590 if (hapd->iconf->rts_threshold > -1 &&
1591 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1592 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1597 if (hapd->iconf->fragm_threshold > -1 &&
1598 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1599 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1600 "for kernel driver");
1604 prev_addr = hapd->own_addr;
1606 for (j = 0; j < iface->num_bss; j++) {
1607 hapd = iface->bss[j];
1609 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1610 if (hostapd_setup_bss(hapd, j == 0)) {
1612 hapd = iface->bss[j];
1613 hostapd_bss_deinit_no_free(hapd);
1614 hostapd_free_hapd_data(hapd);
1618 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1619 prev_addr = hapd->own_addr;
1621 hapd = iface->bss[0];
1623 hostapd_tx_queue_params(iface);
1625 ap_list_init(iface);
1627 hostapd_set_acl(hapd);
1629 if (hostapd_driver_commit(hapd) < 0) {
1630 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1631 "configuration", __func__);
1636 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1637 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1638 * mode), the interface is up only after driver_commit, so initialize
1639 * WPS after driver_commit.
1641 for (j = 0; j < iface->num_bss; j++) {
1642 if (hostapd_init_wps_complete(iface->bss[j]))
1646 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1649 * If freq is DFS, and DFS is offloaded to the driver, then wait
1650 * for CAC to complete.
1652 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1653 return res_dfs_offload;
1658 #endif /* NEED_AP_MLME */
1661 if (hapd->iconf->fst_cfg.group_id[0]) {
1662 struct fst_wpa_obj iface_obj;
1664 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1665 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1666 &iface_obj, &hapd->iconf->fst_cfg);
1668 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1669 hapd->iconf->fst_cfg.group_id);
1673 #endif /* CONFIG_FST */
1675 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1676 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1677 if (hapd->setup_complete_cb)
1678 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1680 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1681 iface->bss[0]->conf->iface);
1682 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1683 iface->interfaces->terminate_on_error--;
1688 wpa_printf(MSG_ERROR, "Interface initialization failed");
1689 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1690 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1693 fst_detach(iface->fst);
1696 #endif /* CONFIG_FST */
1697 if (iface->interfaces && iface->interfaces->terminate_on_error)
1704 * hostapd_setup_interface - Setup of an interface
1705 * @iface: Pointer to interface data.
1706 * Returns: 0 on success, -1 on failure
1708 * Initializes the driver interface, validates the configuration,
1709 * and sets driver parameters based on the configuration.
1710 * Flushes old stations, sets the channel, encryption,
1711 * beacons, and WDS links based on the configuration.
1713 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1714 * or DFS operations, this function returns 0 before such operations have been
1715 * completed. The pending operations are registered into eloop and will be
1716 * completed from eloop callbacks. Those callbacks end up calling
1717 * hostapd_setup_interface_complete() once setup has been completed.
1719 int hostapd_setup_interface(struct hostapd_iface *iface)
1723 ret = setup_interface(iface);
1725 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1726 iface->bss[0]->conf->iface);
1735 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1736 * @hapd_iface: Pointer to interface data
1737 * @conf: Pointer to per-interface configuration
1738 * @bss: Pointer to per-BSS configuration for this BSS
1739 * Returns: Pointer to allocated BSS data
1741 * This function is used to allocate per-BSS data structure. This data will be
1742 * freed after hostapd_cleanup() is called for it during interface
1745 struct hostapd_data *
1746 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1747 struct hostapd_config *conf,
1748 struct hostapd_bss_config *bss)
1750 struct hostapd_data *hapd;
1752 hapd = os_zalloc(sizeof(*hapd));
1756 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1759 hapd->iface = hapd_iface;
1760 hapd->driver = hapd->iconf->driver;
1761 hapd->ctrl_sock = -1;
1767 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1769 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1771 hostapd_bss_deinit_no_free(hapd);
1772 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1773 hostapd_cleanup(hapd);
1777 void hostapd_interface_deinit(struct hostapd_iface *iface)
1781 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1785 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1787 #ifdef CONFIG_IEEE80211N
1789 hostapd_stop_setup_timers(iface);
1790 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1791 #endif /* NEED_AP_MLME */
1792 #endif /* CONFIG_IEEE80211N */
1793 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1794 iface->wait_channel_update = 0;
1798 fst_detach(iface->fst);
1801 #endif /* CONFIG_FST */
1803 for (j = iface->num_bss - 1; j >= 0; j--)
1804 hostapd_bss_deinit(iface->bss[j]);
1808 void hostapd_interface_free(struct hostapd_iface *iface)
1811 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1812 for (j = 0; j < iface->num_bss; j++) {
1813 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1814 __func__, iface->bss[j]);
1815 os_free(iface->bss[j]);
1817 hostapd_cleanup_iface(iface);
1822 * hostapd_init - Allocate and initialize per-interface data
1823 * @config_file: Path to the configuration file
1824 * Returns: Pointer to the allocated interface data or %NULL on failure
1826 * This function is used to allocate main data structures for per-interface
1827 * data. The allocated data buffer will be freed by calling
1828 * hostapd_cleanup_iface().
1830 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1831 const char *config_file)
1833 struct hostapd_iface *hapd_iface = NULL;
1834 struct hostapd_config *conf = NULL;
1835 struct hostapd_data *hapd;
1838 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1839 if (hapd_iface == NULL)
1842 hapd_iface->config_fname = os_strdup(config_file);
1843 if (hapd_iface->config_fname == NULL)
1846 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1849 hapd_iface->conf = conf;
1851 hapd_iface->num_bss = conf->num_bss;
1852 hapd_iface->bss = os_calloc(conf->num_bss,
1853 sizeof(struct hostapd_data *));
1854 if (hapd_iface->bss == NULL)
1857 for (i = 0; i < conf->num_bss; i++) {
1858 hapd = hapd_iface->bss[i] =
1859 hostapd_alloc_bss_data(hapd_iface, conf,
1863 hapd->msg_ctx = hapd;
1869 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1872 hostapd_config_free(conf);
1874 os_free(hapd_iface->config_fname);
1875 os_free(hapd_iface->bss);
1876 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1877 __func__, hapd_iface);
1878 os_free(hapd_iface);
1884 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1888 for (i = 0; i < interfaces->count; i++) {
1889 struct hostapd_iface *iface = interfaces->iface[i];
1890 for (j = 0; j < iface->num_bss; j++) {
1891 struct hostapd_data *hapd = iface->bss[j];
1892 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1902 * hostapd_interface_init_bss - Read configuration file and init BSS data
1904 * This function is used to parse configuration file for a BSS. This BSS is
1905 * added to an existing interface sharing the same radio (if any) or a new
1906 * interface is created if this is the first interface on a radio. This
1907 * allocate memory for the BSS. No actual driver operations are started.
1909 * This is similar to hostapd_interface_init(), but for a case where the
1910 * configuration is used to add a single BSS instead of all BSSes for a radio.
1912 struct hostapd_iface *
1913 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1914 const char *config_fname, int debug)
1916 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1917 struct hostapd_data *hapd;
1924 for (i = 0; i < interfaces->count; i++) {
1925 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1926 iface = interfaces->iface[i];
1931 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1932 config_fname, phy, iface ? "" : " --> new PHY");
1934 struct hostapd_config *conf;
1935 struct hostapd_bss_config **tmp_conf;
1936 struct hostapd_data **tmp_bss;
1937 struct hostapd_bss_config *bss;
1940 /* Add new BSS to existing iface */
1941 conf = interfaces->config_read_cb(config_fname);
1944 if (conf->num_bss > 1) {
1945 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1946 hostapd_config_free(conf);
1950 ifname = conf->bss[0]->iface;
1951 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1952 wpa_printf(MSG_ERROR,
1953 "Interface name %s already in use", ifname);
1954 hostapd_config_free(conf);
1958 tmp_conf = os_realloc_array(
1959 iface->conf->bss, iface->conf->num_bss + 1,
1960 sizeof(struct hostapd_bss_config *));
1961 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1962 sizeof(struct hostapd_data *));
1964 iface->bss = tmp_bss;
1966 iface->conf->bss = tmp_conf;
1967 iface->conf->last_bss = tmp_conf[0];
1969 if (tmp_bss == NULL || tmp_conf == NULL) {
1970 hostapd_config_free(conf);
1973 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1974 iface->conf->num_bss++;
1976 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1978 iface->conf->num_bss--;
1979 hostapd_config_free(conf);
1982 iface->conf->last_bss = bss;
1983 iface->bss[iface->num_bss] = hapd;
1984 hapd->msg_ctx = hapd;
1986 bss_idx = iface->num_bss++;
1988 conf->bss[0] = NULL;
1989 hostapd_config_free(conf);
1991 /* Add a new iface with the first BSS */
1992 new_iface = iface = hostapd_init(interfaces, config_fname);
1995 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1996 iface->interfaces = interfaces;
2000 for (k = 0; k < debug; k++) {
2001 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2002 iface->bss[bss_idx]->conf->logger_stdout_level--;
2005 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2006 !hostapd_drv_none(iface->bss[bss_idx])) {
2007 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2010 hostapd_interface_deinit_free(new_iface);
2018 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2020 const struct wpa_driver_ops *driver;
2023 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2026 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2027 __func__, (unsigned int) iface->num_bss,
2028 (unsigned int) iface->conf->num_bss);
2029 driver = iface->bss[0]->driver;
2030 drv_priv = iface->bss[0]->drv_priv;
2031 hostapd_interface_deinit(iface);
2032 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2033 __func__, driver, drv_priv);
2034 if (driver && driver->hapd_deinit && drv_priv) {
2035 driver->hapd_deinit(drv_priv);
2036 iface->bss[0]->drv_priv = NULL;
2038 hostapd_interface_free(iface);
2042 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2044 struct hostapd_iface *hapd_iface)
2048 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2049 __func__, driver, drv_priv);
2050 if (driver && driver->hapd_deinit && drv_priv) {
2051 driver->hapd_deinit(drv_priv);
2052 for (j = 0; j < hapd_iface->num_bss; j++) {
2053 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2055 hapd_iface->bss[j]->drv_priv);
2056 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2057 hapd_iface->bss[j]->drv_priv = NULL;
2063 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2067 if (hapd_iface->bss[0]->drv_priv != NULL) {
2068 wpa_printf(MSG_ERROR, "Interface %s already enabled",
2069 hapd_iface->conf->bss[0]->iface);
2073 wpa_printf(MSG_DEBUG, "Enable interface %s",
2074 hapd_iface->conf->bss[0]->iface);
2076 for (j = 0; j < hapd_iface->num_bss; j++)
2077 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2078 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2079 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2083 if (hapd_iface->interfaces == NULL ||
2084 hapd_iface->interfaces->driver_init == NULL ||
2085 hapd_iface->interfaces->driver_init(hapd_iface))
2088 if (hostapd_setup_interface(hapd_iface)) {
2089 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2090 hapd_iface->bss[0]->drv_priv,
2099 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2103 wpa_printf(MSG_DEBUG, "Reload interface %s",
2104 hapd_iface->conf->bss[0]->iface);
2105 for (j = 0; j < hapd_iface->num_bss; j++)
2106 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2107 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2108 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2111 hostapd_clear_old(hapd_iface);
2112 for (j = 0; j < hapd_iface->num_bss; j++)
2113 hostapd_reload_bss(hapd_iface->bss[j]);
2119 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2122 const struct wpa_driver_ops *driver;
2125 if (hapd_iface == NULL)
2128 if (hapd_iface->bss[0]->drv_priv == NULL) {
2129 wpa_printf(MSG_INFO, "Interface %s already disabled",
2130 hapd_iface->conf->bss[0]->iface);
2134 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2135 driver = hapd_iface->bss[0]->driver;
2136 drv_priv = hapd_iface->bss[0]->drv_priv;
2138 hapd_iface->driver_ap_teardown =
2139 !!(hapd_iface->drv_flags &
2140 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2142 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
2143 for (j = 0; j < hapd_iface->num_bss; j++) {
2144 struct hostapd_data *hapd = hapd_iface->bss[j];
2145 hostapd_bss_deinit_no_free(hapd);
2146 hostapd_free_hapd_data(hapd);
2149 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
2151 /* From hostapd_cleanup_iface: These were initialized in
2152 * hostapd_setup_interface and hostapd_setup_interface_complete
2154 hostapd_cleanup_iface_partial(hapd_iface);
2156 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2157 hapd_iface->bss[0]->conf->iface);
2158 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2163 static struct hostapd_iface *
2164 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2166 struct hostapd_iface **iface, *hapd_iface;
2168 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2169 sizeof(struct hostapd_iface *));
2172 interfaces->iface = iface;
2173 hapd_iface = interfaces->iface[interfaces->count] =
2174 os_zalloc(sizeof(*hapd_iface));
2175 if (hapd_iface == NULL) {
2176 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2177 "the interface", __func__);
2180 interfaces->count++;
2181 hapd_iface->interfaces = interfaces;
2187 static struct hostapd_config *
2188 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2189 const char *ctrl_iface, const char *driver)
2191 struct hostapd_bss_config *bss;
2192 struct hostapd_config *conf;
2194 /* Allocates memory for bss and conf */
2195 conf = hostapd_config_defaults();
2197 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2198 "configuration", __func__);
2205 for (j = 0; wpa_drivers[j]; j++) {
2206 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2207 conf->driver = wpa_drivers[j];
2212 wpa_printf(MSG_ERROR,
2213 "Invalid/unknown driver '%s' - registering the default driver",
2217 conf->driver = wpa_drivers[0];
2218 if (conf->driver == NULL) {
2219 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2220 hostapd_config_free(conf);
2225 bss = conf->last_bss = conf->bss[0];
2227 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2228 bss->ctrl_interface = os_strdup(ctrl_iface);
2229 if (bss->ctrl_interface == NULL) {
2230 hostapd_config_free(conf);
2234 /* Reading configuration file skipped, will be done in SET!
2235 * From reading the configuration till the end has to be done in
2242 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2243 struct hostapd_config *conf)
2246 struct hostapd_data *hapd;
2248 hapd_iface->bss = os_calloc(conf->num_bss,
2249 sizeof(struct hostapd_data *));
2250 if (hapd_iface->bss == NULL)
2253 for (i = 0; i < conf->num_bss; i++) {
2254 hapd = hapd_iface->bss[i] =
2255 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2259 os_free(hapd_iface->bss[i]);
2260 hapd_iface->bss[i] = NULL;
2262 os_free(hapd_iface->bss);
2263 hapd_iface->bss = NULL;
2266 hapd->msg_ctx = hapd;
2269 hapd_iface->conf = conf;
2270 hapd_iface->num_bss = conf->num_bss;
2276 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2278 struct hostapd_config *conf = NULL;
2279 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2280 struct hostapd_data *hapd;
2283 const char *conf_file = NULL, *phy_name = NULL;
2285 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2287 phy_name = buf + 11;
2288 pos = os_strchr(phy_name, ':');
2293 if (!os_strlen(conf_file))
2296 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2300 for (j = 0; j < interfaces->count; j++) {
2301 if (interfaces->iface[j] == hapd_iface)
2304 if (j == interfaces->count) {
2305 struct hostapd_iface **tmp;
2306 tmp = os_realloc_array(interfaces->iface,
2307 interfaces->count + 1,
2308 sizeof(struct hostapd_iface *));
2310 hostapd_interface_deinit_free(hapd_iface);
2313 interfaces->iface = tmp;
2314 interfaces->iface[interfaces->count++] = hapd_iface;
2315 new_iface = hapd_iface;
2319 if (interfaces->driver_init(hapd_iface))
2322 if (hostapd_setup_interface(hapd_iface)) {
2323 hostapd_deinit_driver(
2324 hapd_iface->bss[0]->driver,
2325 hapd_iface->bss[0]->drv_priv,
2330 /* Assign new BSS with bss[0]'s driver info */
2331 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2332 hapd->driver = hapd_iface->bss[0]->driver;
2333 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2334 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2337 if (start_ctrl_iface_bss(hapd) < 0 ||
2338 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2339 hostapd_setup_bss(hapd, -1))) {
2340 hostapd_cleanup(hapd);
2341 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2342 hapd_iface->conf->num_bss--;
2343 hapd_iface->num_bss--;
2344 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2345 __func__, hapd, hapd->conf->iface);
2346 hostapd_config_free_bss(hapd->conf);
2355 ptr = os_strchr(buf, ' ');
2360 if (os_strncmp(ptr, "config=", 7) == 0)
2361 conf_file = ptr + 7;
2363 for (i = 0; i < interfaces->count; i++) {
2364 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2366 wpa_printf(MSG_INFO, "Cannot add interface - it "
2372 hapd_iface = hostapd_iface_alloc(interfaces);
2373 if (hapd_iface == NULL) {
2374 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2375 "for interface", __func__);
2378 new_iface = hapd_iface;
2380 if (conf_file && interfaces->config_read_cb) {
2381 conf = interfaces->config_read_cb(conf_file);
2382 if (conf && conf->bss)
2383 os_strlcpy(conf->bss[0]->iface, buf,
2384 sizeof(conf->bss[0]->iface));
2386 char *driver = os_strchr(ptr, ' ');
2390 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2393 if (conf == NULL || conf->bss == NULL) {
2394 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2395 "for configuration", __func__);
2399 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2400 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2401 "for hostapd", __func__);
2406 if (start_ctrl_iface(hapd_iface) < 0)
2409 wpa_printf(MSG_INFO, "Add interface '%s'",
2410 hapd_iface->conf->bss[0]->iface);
2416 hostapd_config_free(conf);
2418 if (hapd_iface->bss) {
2419 for (i = 0; i < hapd_iface->num_bss; i++) {
2420 hapd = hapd_iface->bss[i];
2423 if (hapd_iface->interfaces &&
2424 hapd_iface->interfaces->ctrl_iface_deinit)
2425 hapd_iface->interfaces->
2426 ctrl_iface_deinit(hapd);
2427 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2428 __func__, hapd_iface->bss[i],
2430 hostapd_cleanup(hapd);
2432 hapd_iface->bss[i] = NULL;
2434 os_free(hapd_iface->bss);
2435 hapd_iface->bss = NULL;
2438 interfaces->count--;
2439 interfaces->iface[interfaces->count] = NULL;
2441 hostapd_cleanup_iface(hapd_iface);
2447 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2451 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2453 /* Remove hostapd_data only if it has already been initialized */
2454 if (idx < iface->num_bss) {
2455 struct hostapd_data *hapd = iface->bss[idx];
2457 hostapd_bss_deinit(hapd);
2458 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2459 __func__, hapd, hapd->conf->iface);
2460 hostapd_config_free_bss(hapd->conf);
2466 for (i = idx; i < iface->num_bss; i++)
2467 iface->bss[i] = iface->bss[i + 1];
2469 hostapd_config_free_bss(iface->conf->bss[idx]);
2470 iface->conf->bss[idx] = NULL;
2473 iface->conf->num_bss--;
2474 for (i = idx; i < iface->conf->num_bss; i++)
2475 iface->conf->bss[i] = iface->conf->bss[i + 1];
2481 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2483 struct hostapd_iface *hapd_iface;
2486 for (i = 0; i < interfaces->count; i++) {
2487 hapd_iface = interfaces->iface[i];
2488 if (hapd_iface == NULL)
2490 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2491 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2492 hapd_iface->driver_ap_teardown =
2493 !!(hapd_iface->drv_flags &
2494 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2496 hostapd_interface_deinit_free(hapd_iface);
2498 while (k < (interfaces->count - 1)) {
2499 interfaces->iface[k] =
2500 interfaces->iface[k + 1];
2503 interfaces->count--;
2507 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2508 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2509 hapd_iface->driver_ap_teardown =
2510 !(hapd_iface->drv_flags &
2511 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2512 return hostapd_remove_bss(hapd_iface, j);
2521 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2522 * @hapd: Pointer to BSS data
2523 * @sta: Pointer to the associated STA data
2524 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2526 * This function will be called whenever a station associates with the AP. It
2527 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2528 * from drv_callbacks.c based on driver events for drivers that take care of
2529 * management frames (IEEE 802.11 authentication and association) internally.
2531 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2534 if (hapd->tkip_countermeasures) {
2535 hostapd_drv_sta_deauth(hapd, sta->addr,
2536 WLAN_REASON_MICHAEL_MIC_FAILURE);
2540 hostapd_prune_associations(hapd, sta->addr);
2542 /* IEEE 802.11F (IAPP) */
2543 if (hapd->conf->ieee802_11f)
2544 iapp_new_station(hapd->iapp, sta);
2547 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2548 sta->no_p2p_set = 1;
2549 hapd->num_sta_no_p2p++;
2550 if (hapd->num_sta_no_p2p == 1)
2551 hostapd_p2p_non_p2p_sta_connected(hapd);
2553 #endif /* CONFIG_P2P */
2555 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2556 * IEEE 802.1X/WPA code will start accounting after the station has
2557 * been authorized. */
2558 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2559 ap_sta_set_authorized(hapd, sta, 1);
2560 os_get_reltime(&sta->connected_time);
2561 accounting_sta_start(hapd, sta);
2564 /* Start IEEE 802.1X authentication process for new stations */
2565 ieee802_1x_new_station(hapd, sta);
2567 if (sta->auth_alg != WLAN_AUTH_FT &&
2568 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2569 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2571 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2573 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2574 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2575 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2576 __func__, MAC2STR(sta->addr),
2577 hapd->conf->ap_max_inactivity);
2578 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2579 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2580 ap_handle_timer, hapd, sta);
2585 const char * hostapd_state_text(enum hostapd_iface_state s)
2588 case HAPD_IFACE_UNINITIALIZED:
2589 return "UNINITIALIZED";
2590 case HAPD_IFACE_DISABLED:
2592 case HAPD_IFACE_COUNTRY_UPDATE:
2593 return "COUNTRY_UPDATE";
2594 case HAPD_IFACE_ACS:
2596 case HAPD_IFACE_HT_SCAN:
2598 case HAPD_IFACE_DFS:
2600 case HAPD_IFACE_ENABLED:
2608 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2610 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2611 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2612 hostapd_state_text(s));
2619 static void free_beacon_data(struct beacon_data *beacon)
2621 os_free(beacon->head);
2622 beacon->head = NULL;
2623 os_free(beacon->tail);
2624 beacon->tail = NULL;
2625 os_free(beacon->probe_resp);
2626 beacon->probe_resp = NULL;
2627 os_free(beacon->beacon_ies);
2628 beacon->beacon_ies = NULL;
2629 os_free(beacon->proberesp_ies);
2630 beacon->proberesp_ies = NULL;
2631 os_free(beacon->assocresp_ies);
2632 beacon->assocresp_ies = NULL;
2636 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2637 struct beacon_data *beacon)
2639 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2640 struct wpa_driver_ap_params params;
2643 os_memset(beacon, 0, sizeof(*beacon));
2644 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2648 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2652 goto free_ap_params;
2655 beacon->head = os_malloc(params.head_len);
2657 goto free_ap_extra_ies;
2659 os_memcpy(beacon->head, params.head, params.head_len);
2660 beacon->head_len = params.head_len;
2662 beacon->tail = os_malloc(params.tail_len);
2666 os_memcpy(beacon->tail, params.tail, params.tail_len);
2667 beacon->tail_len = params.tail_len;
2669 if (params.proberesp != NULL) {
2670 beacon->probe_resp = os_malloc(params.proberesp_len);
2671 if (!beacon->probe_resp)
2674 os_memcpy(beacon->probe_resp, params.proberesp,
2675 params.proberesp_len);
2676 beacon->probe_resp_len = params.proberesp_len;
2679 /* copy the extra ies */
2681 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2682 if (!beacon->beacon_ies)
2685 os_memcpy(beacon->beacon_ies,
2686 beacon_extra->buf, wpabuf_len(beacon_extra));
2687 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2690 if (proberesp_extra) {
2691 beacon->proberesp_ies =
2692 os_malloc(wpabuf_len(proberesp_extra));
2693 if (!beacon->proberesp_ies)
2696 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2697 wpabuf_len(proberesp_extra));
2698 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2701 if (assocresp_extra) {
2702 beacon->assocresp_ies =
2703 os_malloc(wpabuf_len(assocresp_extra));
2704 if (!beacon->assocresp_ies)
2707 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2708 wpabuf_len(assocresp_extra));
2709 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2714 /* if the function fails, the caller should not free beacon data */
2716 free_beacon_data(beacon);
2719 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2722 ieee802_11_free_ap_params(¶ms);
2728 * TODO: This flow currently supports only changing frequency within the
2729 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2730 * width) are not supported.
2732 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2733 struct hostapd_config *conf,
2734 struct hostapd_freq_params *params,
2735 struct hostapd_freq_params *old_params)
2739 if (!params->channel) {
2740 /* check if the new channel is supported by hw */
2741 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2744 channel = params->channel;
2748 /* if a pointer to old_params is provided we save previous state */
2750 old_params->channel = conf->channel;
2751 old_params->ht_enabled = conf->ieee80211n;
2752 old_params->sec_channel_offset = conf->secondary_channel;
2755 conf->channel = channel;
2756 conf->ieee80211n = params->ht_enabled;
2757 conf->secondary_channel = params->sec_channel_offset;
2759 /* TODO: maybe call here hostapd_config_check here? */
2765 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2766 struct csa_settings *settings)
2768 struct hostapd_iface *iface = hapd->iface;
2769 struct hostapd_freq_params old_freq;
2772 os_memset(&old_freq, 0, sizeof(old_freq));
2773 if (!iface || !iface->freq || hapd->csa_in_progress)
2776 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2777 &settings->freq_params,
2782 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2784 /* change back the configuration */
2785 hostapd_change_config_freq(iface->bss[0], iface->conf,
2791 /* set channel switch parameters for csa ie */
2792 hapd->cs_freq_params = settings->freq_params;
2793 hapd->cs_count = settings->cs_count;
2794 hapd->cs_block_tx = settings->block_tx;
2796 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2798 free_beacon_data(&settings->beacon_after);
2802 settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2803 settings->counter_offset_presp = hapd->cs_c_off_proberesp;
2809 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2811 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2813 hapd->cs_block_tx = 0;
2814 hapd->cs_c_off_beacon = 0;
2815 hapd->cs_c_off_proberesp = 0;
2816 hapd->csa_in_progress = 0;
2820 int hostapd_switch_channel(struct hostapd_data *hapd,
2821 struct csa_settings *settings)
2825 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2826 wpa_printf(MSG_INFO, "CSA is not supported");
2830 ret = hostapd_fill_csa_settings(hapd, settings);
2834 ret = hostapd_drv_switch_channel(hapd, settings);
2835 free_beacon_data(&settings->beacon_csa);
2836 free_beacon_data(&settings->beacon_after);
2839 /* if we failed, clean cs parameters */
2840 hostapd_cleanup_cs_params(hapd);
2844 hapd->csa_in_progress = 1;
2850 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2851 const struct hostapd_freq_params *freq_params)
2853 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2856 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2858 if (freq_params->center_freq1)
2859 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2860 if (freq_params->center_freq2)
2861 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2863 switch (freq_params->bandwidth) {
2867 vht_bw = VHT_CHANWIDTH_USE_HT;
2870 if (freq_params->center_freq2)
2871 vht_bw = VHT_CHANWIDTH_80P80MHZ;
2873 vht_bw = VHT_CHANWIDTH_80MHZ;
2876 vht_bw = VHT_CHANWIDTH_160MHZ;
2879 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2880 freq_params->bandwidth);
2884 iface->freq = freq_params->freq;
2885 iface->conf->channel = freq_params->channel;
2886 iface->conf->secondary_channel = freq_params->sec_channel_offset;
2887 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2888 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2889 iface->conf->vht_oper_chwidth = vht_bw;
2890 iface->conf->ieee80211n = freq_params->ht_enabled;
2891 iface->conf->ieee80211ac = freq_params->vht_enabled;
2894 * cs_params must not be cleared earlier because the freq_params
2895 * argument may actually point to one of these.
2897 for (i = 0; i < iface->num_bss; i++)
2898 hostapd_cleanup_cs_params(iface->bss[i]);
2900 hostapd_disable_iface(iface);
2901 hostapd_enable_iface(iface);
2905 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
2910 for (i = 0; i < interfaces->count; i++) {
2911 struct hostapd_iface *iface = interfaces->iface[i];
2913 for (j = 0; j < iface->num_bss; j++) {
2914 struct hostapd_data *hapd = iface->bss[j];
2916 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2924 #endif /* NEED_AP_MLME */
2927 void hostapd_periodic_iface(struct hostapd_iface *iface)