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;
264 hapd->num_probereq_cb = 0;
267 wpabuf_free(hapd->p2p_beacon_ie);
268 hapd->p2p_beacon_ie = NULL;
269 wpabuf_free(hapd->p2p_probe_resp_ie);
270 hapd->p2p_probe_resp_ie = NULL;
271 #endif /* CONFIG_P2P */
273 if (!hapd->started) {
274 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
275 __func__, hapd->conf->iface);
280 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
281 iapp_deinit(hapd->iapp);
283 accounting_deinit(hapd);
284 hostapd_deinit_wpa(hapd);
286 hostapd_acl_deinit(hapd);
287 #ifndef CONFIG_NO_RADIUS
288 radius_client_deinit(hapd->radius);
290 radius_das_deinit(hapd->radius_das);
291 hapd->radius_das = NULL;
292 #endif /* CONFIG_NO_RADIUS */
294 hostapd_deinit_wps(hapd);
296 authsrv_deinit(hapd);
298 if (hapd->interface_added) {
299 hapd->interface_added = 0;
300 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
301 wpa_printf(MSG_WARNING,
302 "Failed to remove BSS interface %s",
304 hapd->interface_added = 1;
307 * Since this was a dynamically added interface, the
308 * driver wrapper may have removed its internal instance
309 * and hapd->drv_priv is not valid anymore.
311 hapd->drv_priv = NULL;
315 wpabuf_free(hapd->time_adv);
317 #ifdef CONFIG_INTERWORKING
318 gas_serv_deinit(hapd);
319 #endif /* CONFIG_INTERWORKING */
321 bss_load_update_deinit(hapd);
322 ndisc_snoop_deinit(hapd);
323 dhcp_snoop_deinit(hapd);
324 x_snoop_deinit(hapd);
327 bin_clear_free(hapd->tmp_eap_user.identity,
328 hapd->tmp_eap_user.identity_len);
329 bin_clear_free(hapd->tmp_eap_user.password,
330 hapd->tmp_eap_user.password_len);
331 #endif /* CONFIG_SQLITE */
334 wpabuf_free(hapd->mesh_pending_auth);
335 hapd->mesh_pending_auth = NULL;
336 #endif /* CONFIG_MESH */
341 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
342 * @hapd: Pointer to BSS data
344 * This function is used to free all per-BSS data structures and resources.
345 * Most of the modules that are initialized in hostapd_setup_bss() are
346 * deinitialized here.
348 static void hostapd_cleanup(struct hostapd_data *hapd)
350 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
352 if (hapd->iface->interfaces &&
353 hapd->iface->interfaces->ctrl_iface_deinit)
354 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
355 hostapd_free_hapd_data(hapd);
359 static void sta_track_deinit(struct hostapd_iface *iface)
361 struct hostapd_sta_info *info;
363 if (!iface->num_sta_seen)
366 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
368 dl_list_del(&info->list);
369 iface->num_sta_seen--;
375 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
377 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
378 #ifdef CONFIG_IEEE80211N
380 hostapd_stop_setup_timers(iface);
381 #endif /* NEED_AP_MLME */
382 #endif /* CONFIG_IEEE80211N */
383 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
384 iface->hw_features = NULL;
385 os_free(iface->current_rates);
386 iface->current_rates = NULL;
387 os_free(iface->basic_rates);
388 iface->basic_rates = NULL;
389 ap_list_deinit(iface);
390 sta_track_deinit(iface);
395 * hostapd_cleanup_iface - Complete per-interface cleanup
396 * @iface: Pointer to interface data
398 * This function is called after per-BSS data structures are deinitialized
399 * with hostapd_cleanup().
401 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
403 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
404 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
406 hostapd_cleanup_iface_partial(iface);
407 hostapd_config_free(iface->conf);
410 os_free(iface->config_fname);
412 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
417 static void hostapd_clear_wep(struct hostapd_data *hapd)
419 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
420 hostapd_set_privacy(hapd, 0);
421 hostapd_broadcast_wep_clear(hapd);
426 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
430 hostapd_broadcast_wep_set(hapd);
432 if (hapd->conf->ssid.wep.default_len) {
433 hostapd_set_privacy(hapd, 1);
438 * When IEEE 802.1X is not enabled, the driver may need to know how to
439 * set authentication algorithms for static WEP.
441 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
443 for (i = 0; i < 4; i++) {
444 if (hapd->conf->ssid.wep.key[i] &&
445 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
446 i == hapd->conf->ssid.wep.idx, NULL, 0,
447 hapd->conf->ssid.wep.key[i],
448 hapd->conf->ssid.wep.len[i])) {
449 wpa_printf(MSG_WARNING, "Could not set WEP "
453 if (hapd->conf->ssid.wep.key[i] &&
454 i == hapd->conf->ssid.wep.idx)
455 hostapd_set_privacy(hapd, 1);
462 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
467 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
470 if (!hapd->iface->driver_ap_teardown) {
471 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
472 "Flushing old station entries");
474 if (hostapd_flush(hapd)) {
475 wpa_msg(hapd->msg_ctx, MSG_WARNING,
476 "Could not connect to kernel driver");
480 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
481 os_memset(addr, 0xff, ETH_ALEN);
482 hostapd_drv_sta_deauth(hapd, addr, reason);
483 hostapd_free_stas(hapd);
489 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
491 hostapd_free_stas(hapd);
492 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
493 hostapd_clear_wep(hapd);
498 * hostapd_validate_bssid_configuration - Validate BSSID configuration
499 * @iface: Pointer to interface data
500 * Returns: 0 on success, -1 on failure
502 * This function is used to validate that the configured BSSIDs are valid.
504 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
506 u8 mask[ETH_ALEN] = { 0 };
507 struct hostapd_data *hapd = iface->bss[0];
508 unsigned int i = iface->conf->num_bss, bits = 0, j;
511 if (hostapd_drv_none(hapd))
514 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
516 /* Determine the bits necessary to cover the number of BSSIDs. */
517 for (i--; i; i >>= 1)
520 /* Determine the bits necessary to any configured BSSIDs,
521 if they are higher than the number of BSSIDs. */
522 for (j = 0; j < iface->conf->num_bss; j++) {
523 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
529 for (i = 0; i < ETH_ALEN; i++) {
531 iface->conf->bss[j]->bssid[i] ^
539 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
545 while (mask[i] != 0) {
555 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
560 os_memset(mask, 0xff, ETH_ALEN);
562 for (i = 5; i > 5 - j; i--)
569 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
570 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
575 for (i = 0; i < ETH_ALEN; i++) {
576 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
577 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
578 " for start address " MACSTR ".",
579 MAC2STR(mask), MAC2STR(hapd->own_addr));
580 wpa_printf(MSG_ERROR, "Start address must be the "
581 "first address in the block (i.e., addr "
582 "AND mask == addr).");
591 static int mac_in_conf(struct hostapd_config *conf, const void *a)
595 for (i = 0; i < conf->num_bss; i++) {
596 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
605 #ifndef CONFIG_NO_RADIUS
607 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
608 struct radius_das_attrs *attr)
610 if (attr->nas_identifier &&
611 (!hapd->conf->nas_identifier ||
612 os_strlen(hapd->conf->nas_identifier) !=
613 attr->nas_identifier_len ||
614 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
615 attr->nas_identifier_len) != 0)) {
616 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
620 if (attr->nas_ip_addr &&
621 (hapd->conf->own_ip_addr.af != AF_INET ||
622 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
624 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
629 if (attr->nas_ipv6_addr &&
630 (hapd->conf->own_ip_addr.af != AF_INET6 ||
631 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
633 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
636 #endif /* CONFIG_IPV6 */
642 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
643 struct radius_das_attrs *attr,
646 struct sta_info *selected, *sta;
653 for (sta = hapd->sta_list; sta; sta = sta->next)
654 sta->radius_das_match = 1;
656 if (attr->sta_addr) {
658 sta = ap_get_sta(hapd, attr->sta_addr);
660 wpa_printf(MSG_DEBUG,
661 "RADIUS DAS: No Calling-Station-Id match");
666 for (sta = hapd->sta_list; sta; sta = sta->next) {
668 sta->radius_das_match = 0;
670 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
673 if (attr->acct_session_id) {
675 if (attr->acct_session_id_len != 17) {
676 wpa_printf(MSG_DEBUG,
677 "RADIUS DAS: Acct-Session-Id cannot match");
682 for (sta = hapd->sta_list; sta; sta = sta->next) {
683 if (!sta->radius_das_match)
685 os_snprintf(buf, sizeof(buf), "%08X-%08X",
686 sta->acct_session_id_hi,
687 sta->acct_session_id_lo);
688 if (os_memcmp(attr->acct_session_id, buf, 17) != 0)
689 sta->radius_das_match = 0;
695 wpa_printf(MSG_DEBUG,
696 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
699 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
702 if (attr->acct_multi_session_id) {
704 if (attr->acct_multi_session_id_len != 17) {
705 wpa_printf(MSG_DEBUG,
706 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
711 for (sta = hapd->sta_list; sta; sta = sta->next) {
712 if (!sta->radius_das_match)
714 if (!sta->eapol_sm ||
715 !sta->eapol_sm->acct_multi_session_id_hi) {
716 sta->radius_das_match = 0;
719 os_snprintf(buf, sizeof(buf), "%08X+%08X",
720 sta->eapol_sm->acct_multi_session_id_hi,
721 sta->eapol_sm->acct_multi_session_id_lo);
722 if (os_memcmp(attr->acct_multi_session_id, buf, 17) !=
724 sta->radius_das_match = 0;
730 wpa_printf(MSG_DEBUG,
731 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
734 wpa_printf(MSG_DEBUG,
735 "RADIUS DAS: Acct-Multi-Session-Id match");
742 for (sta = hapd->sta_list; sta; sta = sta->next) {
745 if (!sta->radius_das_match)
747 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
748 if (!cui || wpabuf_len(cui) != attr->cui_len ||
749 os_memcmp(wpabuf_head(cui), attr->cui,
751 sta->radius_das_match = 0;
757 wpa_printf(MSG_DEBUG,
758 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
761 wpa_printf(MSG_DEBUG,
762 "RADIUS DAS: Chargeable-User-Identity match");
765 if (attr->user_name) {
769 for (sta = hapd->sta_list; sta; sta = sta->next) {
773 if (!sta->radius_das_match)
775 identity = ieee802_1x_get_identity(sta->eapol_sm,
778 identity_len != attr->user_name_len ||
779 os_memcmp(identity, attr->user_name, identity_len)
781 sta->radius_das_match = 0;
787 wpa_printf(MSG_DEBUG,
788 "RADIUS DAS: No matches remaining after User-Name check");
791 wpa_printf(MSG_DEBUG,
792 "RADIUS DAS: User-Name match");
797 * In theory, we could match all current associations, but it
798 * seems safer to just reject requests that do not include any
799 * session identification attributes.
801 wpa_printf(MSG_DEBUG,
802 "RADIUS DAS: No session identification attributes included");
807 for (sta = hapd->sta_list; sta; sta = sta->next) {
808 if (sta->radius_das_match) {
821 static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
822 struct radius_das_attrs *attr)
826 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
830 static enum radius_das_res
831 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
833 struct hostapd_data *hapd = ctx;
834 struct sta_info *sta;
837 if (hostapd_das_nas_mismatch(hapd, attr))
838 return RADIUS_DAS_NAS_MISMATCH;
840 sta = hostapd_das_find_sta(hapd, attr, &multi);
843 wpa_printf(MSG_DEBUG,
844 "RADIUS DAS: Multiple sessions match - not supported");
845 return RADIUS_DAS_MULTI_SESSION_MATCH;
847 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
848 wpa_printf(MSG_DEBUG,
849 "RADIUS DAS: PMKSA cache entry matched");
850 return RADIUS_DAS_SUCCESS;
852 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
853 return RADIUS_DAS_SESSION_NOT_FOUND;
856 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
857 " - disconnecting", MAC2STR(sta->addr));
858 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
860 hostapd_drv_sta_deauth(hapd, sta->addr,
861 WLAN_REASON_PREV_AUTH_NOT_VALID);
862 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
864 return RADIUS_DAS_SUCCESS;
867 #endif /* CONFIG_NO_RADIUS */
871 * hostapd_setup_bss - Per-BSS setup (initialization)
872 * @hapd: Pointer to BSS data
873 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
874 * but interface may exist
876 * This function is used to initialize all per-BSS data structures and
877 * resources. This gets called in a loop for each BSS when an interface is
878 * initialized. Most of the modules that are initialized here will be
879 * deinitialized in hostapd_cleanup().
881 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
883 struct hostapd_bss_config *conf = hapd->conf;
884 u8 ssid[SSID_MAX_LEN + 1];
885 int ssid_len, set_ssid;
886 char force_ifname[IFNAMSIZ];
887 u8 if_addr[ETH_ALEN];
888 int flush_old_stations = 1;
890 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
891 __func__, hapd, conf->iface, first);
893 #ifdef EAP_SERVER_TNC
894 if (conf->tnc && tncs_global_init() < 0) {
895 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
898 #endif /* EAP_SERVER_TNC */
901 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
902 __func__, conf->iface);
907 if (!first || first == -1) {
908 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
909 /* Allocate the next available BSSID. */
911 inc_byte_array(hapd->own_addr, ETH_ALEN);
912 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
914 /* Allocate the configured BSSID. */
915 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
917 if (hostapd_mac_comp(hapd->own_addr,
918 hapd->iface->bss[0]->own_addr) ==
920 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
921 "BSSID set to the MAC address of "
922 "the radio", conf->iface);
927 hapd->interface_added = 1;
928 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
929 conf->iface, hapd->own_addr, hapd,
930 &hapd->drv_priv, force_ifname, if_addr,
931 conf->bridge[0] ? conf->bridge : NULL,
933 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
934 MACSTR ")", MAC2STR(hapd->own_addr));
935 hapd->interface_added = 0;
940 if (conf->wmm_enabled < 0)
941 conf->wmm_enabled = hapd->iconf->ieee80211n;
944 if (hapd->iface->mconf == NULL)
945 flush_old_stations = 0;
946 #endif /* CONFIG_MESH */
948 if (flush_old_stations)
949 hostapd_flush_old_stations(hapd,
950 WLAN_REASON_PREV_AUTH_NOT_VALID);
951 hostapd_set_privacy(hapd, 0);
953 hostapd_broadcast_wep_clear(hapd);
954 if (hostapd_setup_encryption(conf->iface, hapd))
958 * Fetch the SSID from the system and use it or,
959 * if one was specified in the config file, verify they
962 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
964 wpa_printf(MSG_ERROR, "Could not read SSID from system");
967 if (conf->ssid.ssid_set) {
969 * If SSID is specified in the config file and it differs
970 * from what is being used then force installation of the
973 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
974 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
977 * No SSID in the config file; just use the one we got
981 conf->ssid.ssid_len = ssid_len;
982 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
985 if (!hostapd_drv_none(hapd)) {
986 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
988 conf->iface, MAC2STR(hapd->own_addr),
989 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
992 if (hostapd_setup_wpa_psk(conf)) {
993 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
997 /* Set SSID for the kernel driver (to be used in beacon and probe
998 * response frames) */
999 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
1000 conf->ssid.ssid_len)) {
1001 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1005 if (wpa_debug_level <= MSG_MSGDUMP)
1006 conf->radius->msg_dumps = 1;
1007 #ifndef CONFIG_NO_RADIUS
1008 hapd->radius = radius_client_init(hapd, conf->radius);
1009 if (hapd->radius == NULL) {
1010 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1014 if (conf->radius_das_port) {
1015 struct radius_das_conf das_conf;
1016 os_memset(&das_conf, 0, sizeof(das_conf));
1017 das_conf.port = conf->radius_das_port;
1018 das_conf.shared_secret = conf->radius_das_shared_secret;
1019 das_conf.shared_secret_len =
1020 conf->radius_das_shared_secret_len;
1021 das_conf.client_addr = &conf->radius_das_client_addr;
1022 das_conf.time_window = conf->radius_das_time_window;
1023 das_conf.require_event_timestamp =
1024 conf->radius_das_require_event_timestamp;
1025 das_conf.ctx = hapd;
1026 das_conf.disconnect = hostapd_das_disconnect;
1027 hapd->radius_das = radius_das_init(&das_conf);
1028 if (hapd->radius_das == NULL) {
1029 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1034 #endif /* CONFIG_NO_RADIUS */
1036 if (hostapd_acl_init(hapd)) {
1037 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1040 if (hostapd_init_wps(hapd, conf))
1043 if (authsrv_init(hapd) < 0)
1046 if (ieee802_1x_init(hapd)) {
1047 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1051 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
1054 if (accounting_init(hapd)) {
1055 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1059 if (conf->ieee802_11f &&
1060 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
1061 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1066 #ifdef CONFIG_INTERWORKING
1067 if (gas_serv_init(hapd)) {
1068 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1072 if (conf->qos_map_set_len &&
1073 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1074 conf->qos_map_set_len)) {
1075 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
1078 #endif /* CONFIG_INTERWORKING */
1080 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1081 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1085 if (conf->proxy_arp) {
1086 if (x_snoop_init(hapd)) {
1087 wpa_printf(MSG_ERROR,
1088 "Generic snooping infrastructure initialization failed");
1092 if (dhcp_snoop_init(hapd)) {
1093 wpa_printf(MSG_ERROR,
1094 "DHCP snooping initialization failed");
1098 if (ndisc_snoop_init(hapd)) {
1099 wpa_printf(MSG_ERROR,
1100 "Neighbor Discovery snooping initialization failed");
1105 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1106 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1110 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
1113 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1116 if (hapd->driver && hapd->driver->set_operstate)
1117 hapd->driver->set_operstate(hapd->drv_priv, 1);
1123 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1125 struct hostapd_data *hapd = iface->bss[0];
1127 struct hostapd_tx_queue_params *p;
1130 if (iface->mconf == NULL)
1132 #endif /* CONFIG_MESH */
1134 for (i = 0; i < NUM_TX_QUEUES; i++) {
1135 p = &iface->conf->tx_queue[i];
1137 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1138 p->cwmax, p->burst)) {
1139 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1140 "parameters for queue %d.", i);
1141 /* Continue anyway */
1147 static int hostapd_set_acl_list(struct hostapd_data *hapd,
1148 struct mac_acl_entry *mac_acl,
1149 int n_entries, u8 accept_acl)
1151 struct hostapd_acl_params *acl_params;
1154 acl_params = os_zalloc(sizeof(*acl_params) +
1155 (n_entries * sizeof(acl_params->mac_acl[0])));
1159 for (i = 0; i < n_entries; i++)
1160 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1163 acl_params->acl_policy = accept_acl;
1164 acl_params->num_mac_acl = n_entries;
1166 err = hostapd_drv_set_acl(hapd, acl_params);
1168 os_free(acl_params);
1174 static void hostapd_set_acl(struct hostapd_data *hapd)
1176 struct hostapd_config *conf = hapd->iconf;
1180 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1183 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
1185 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1186 conf->bss[0]->num_accept_mac,
1189 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1192 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
1194 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1195 conf->bss[0]->num_deny_mac,
1198 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1205 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1207 if (!hapd->iface->interfaces ||
1208 !hapd->iface->interfaces->ctrl_iface_init)
1211 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1212 wpa_printf(MSG_ERROR,
1213 "Failed to setup control interface for %s",
1222 static int start_ctrl_iface(struct hostapd_iface *iface)
1226 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1229 for (i = 0; i < iface->num_bss; i++) {
1230 struct hostapd_data *hapd = iface->bss[i];
1231 if (iface->interfaces->ctrl_iface_init(hapd)) {
1232 wpa_printf(MSG_ERROR,
1233 "Failed to setup control interface for %s",
1243 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1245 struct hostapd_iface *iface = eloop_ctx;
1247 if (!iface->wait_channel_update) {
1248 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1253 * It is possible that the existing channel list is acceptable, so try
1256 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1257 setup_interface2(iface);
1261 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1263 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1266 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1267 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1268 setup_interface2(iface);
1272 static int setup_interface(struct hostapd_iface *iface)
1274 struct hostapd_data *hapd = iface->bss[0];
1278 * It is possible that setup_interface() is called after the interface
1279 * was disabled etc., in which case driver_ap_teardown is possibly set
1280 * to 1. Clear it here so any other key/station deletion, which is not
1281 * part of a teardown flow, would also call the relevant driver
1284 iface->driver_ap_teardown = 0;
1286 if (!iface->phy[0]) {
1287 const char *phy = hostapd_drv_get_radio_name(hapd);
1289 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1290 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1295 * Make sure that all BSSes get configured with a pointer to the same
1298 for (i = 1; i < iface->num_bss; i++) {
1299 iface->bss[i]->driver = hapd->driver;
1300 iface->bss[i]->drv_priv = hapd->drv_priv;
1303 if (hostapd_validate_bssid_configuration(iface))
1307 * Initialize control interfaces early to allow external monitoring of
1308 * channel setup operations that may take considerable amount of time
1309 * especially for DFS cases.
1311 if (start_ctrl_iface(iface))
1314 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1315 char country[4], previous_country[4];
1317 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1318 if (hostapd_get_country(hapd, previous_country) < 0)
1319 previous_country[0] = '\0';
1321 os_memcpy(country, hapd->iconf->country, 3);
1323 if (hostapd_set_country(hapd, country) < 0) {
1324 wpa_printf(MSG_ERROR, "Failed to set country code");
1328 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1329 previous_country, country);
1331 if (os_strncmp(previous_country, country, 2) != 0) {
1332 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1333 iface->wait_channel_update = 1;
1334 eloop_register_timeout(5, 0,
1335 channel_list_update_timeout,
1341 return setup_interface2(iface);
1345 static int setup_interface2(struct hostapd_iface *iface)
1347 iface->wait_channel_update = 0;
1349 if (hostapd_get_hw_features(iface)) {
1350 /* Not all drivers support this yet, so continue without hw
1353 int ret = hostapd_select_hw_mode(iface);
1355 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1356 "channel. (%d)", ret);
1360 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1363 ret = hostapd_check_ht_capab(iface);
1367 wpa_printf(MSG_DEBUG, "Interface initialization will "
1368 "be completed in a callback");
1372 if (iface->conf->ieee80211h)
1373 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1375 return hostapd_setup_interface_complete(iface, 0);
1378 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1379 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1380 if (iface->interfaces && iface->interfaces->terminate_on_error)
1388 static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1390 struct hostapd_data *hapd = ctx;
1392 return hapd->own_addr;
1396 static void fst_hostapd_get_channel_info_cb(void *ctx,
1397 enum hostapd_hw_mode *hw_mode,
1400 struct hostapd_data *hapd = ctx;
1402 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1406 static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1408 struct hostapd_data *hapd = ctx;
1410 if (hapd->iface->fst_ies != fst_ies) {
1411 hapd->iface->fst_ies = fst_ies;
1412 if (ieee802_11_set_beacon(hapd))
1413 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1418 static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1421 struct hostapd_data *hapd = ctx;
1423 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1424 wpabuf_head(buf), wpabuf_len(buf));
1428 static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1430 struct hostapd_data *hapd = ctx;
1431 struct sta_info *sta = ap_get_sta(hapd, addr);
1433 return sta ? sta->mb_ies : NULL;
1437 static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1438 const u8 *buf, size_t size)
1440 struct hostapd_data *hapd = ctx;
1441 struct sta_info *sta = ap_get_sta(hapd, addr);
1444 struct mb_ies_info info;
1446 if (!mb_ies_info_by_ies(&info, buf, size)) {
1447 wpabuf_free(sta->mb_ies);
1448 sta->mb_ies = mb_ies_by_info(&info);
1454 static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1457 struct sta_info *s = (struct sta_info *) *get_ctx;
1460 for (; s && !s->mb_ies; s = s->next)
1465 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1475 static const u8 * fst_hostapd_get_peer_first(void *ctx,
1476 struct fst_get_peer_ctx **get_ctx,
1479 struct hostapd_data *hapd = ctx;
1481 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1483 return fst_hostapd_get_sta(get_ctx, mb_only);
1487 static const u8 * fst_hostapd_get_peer_next(void *ctx,
1488 struct fst_get_peer_ctx **get_ctx,
1491 return fst_hostapd_get_sta(get_ctx, mb_only);
1495 void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1496 struct fst_wpa_obj *iface_obj)
1498 iface_obj->ctx = hapd;
1499 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1500 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1501 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1502 iface_obj->send_action = fst_hostapd_send_action_cb;
1503 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1504 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1505 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1506 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1509 #endif /* CONFIG_FST */
1513 * hostapd_setup_interface_complete - Complete interface setup
1515 * This function is called when previous steps in the interface setup has been
1516 * completed. This can also start operations, e.g., DFS, that will require
1517 * additional processing before interface is ready to be enabled. Such
1518 * operations will call this function from eloop callbacks when finished.
1520 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1522 struct hostapd_data *hapd = iface->bss[0];
1525 int delay_apply_cfg = 0;
1526 int res_dfs_offload = 0;
1531 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1532 if (iface->conf->channel) {
1535 #endif /* NEED_AP_MLME */
1537 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1538 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1539 "Frequency: %d MHz",
1540 hostapd_hw_mode_txt(iface->conf->hw_mode),
1541 iface->conf->channel, iface->freq);
1544 /* Handle DFS only if it is not offloaded to the driver */
1545 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1547 res = hostapd_handle_dfs(iface);
1554 /* If DFS is offloaded to the driver */
1555 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1556 if (res_dfs_offload <= 0) {
1557 if (res_dfs_offload < 0)
1560 wpa_printf(MSG_DEBUG,
1561 "Proceed with AP/channel setup");
1563 * If this is a DFS channel, move to completing
1566 if (res_dfs_offload == 1)
1568 /* Otherwise fall through. */
1571 #endif /* NEED_AP_MLME */
1574 if (iface->mconf != NULL) {
1575 wpa_printf(MSG_DEBUG,
1576 "%s: Mesh configuration will be applied while joining the mesh network",
1577 iface->bss[0]->conf->iface);
1578 delay_apply_cfg = 1;
1580 #endif /* CONFIG_MESH */
1582 if (!delay_apply_cfg &&
1583 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1584 hapd->iconf->channel,
1585 hapd->iconf->ieee80211n,
1586 hapd->iconf->ieee80211ac,
1587 hapd->iconf->secondary_channel,
1588 hapd->iconf->vht_oper_chwidth,
1589 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1590 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1591 wpa_printf(MSG_ERROR, "Could not set channel for "
1597 if (iface->current_mode) {
1598 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1599 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1601 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1602 HOSTAPD_LEVEL_WARNING,
1603 "Failed to prepare rates table.");
1608 if (hapd->iconf->rts_threshold > -1 &&
1609 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1610 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1615 if (hapd->iconf->fragm_threshold > -1 &&
1616 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1617 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1618 "for kernel driver");
1622 prev_addr = hapd->own_addr;
1624 for (j = 0; j < iface->num_bss; j++) {
1625 hapd = iface->bss[j];
1627 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1628 if (hostapd_setup_bss(hapd, j == 0)) {
1630 hapd = iface->bss[j];
1631 hostapd_bss_deinit_no_free(hapd);
1632 hostapd_free_hapd_data(hapd);
1636 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1637 prev_addr = hapd->own_addr;
1639 hapd = iface->bss[0];
1641 hostapd_tx_queue_params(iface);
1643 ap_list_init(iface);
1644 dl_list_init(&iface->sta_seen);
1646 hostapd_set_acl(hapd);
1648 if (hostapd_driver_commit(hapd) < 0) {
1649 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1650 "configuration", __func__);
1655 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1656 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1657 * mode), the interface is up only after driver_commit, so initialize
1658 * WPS after driver_commit.
1660 for (j = 0; j < iface->num_bss; j++) {
1661 if (hostapd_init_wps_complete(iface->bss[j]))
1665 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1668 * If freq is DFS, and DFS is offloaded to the driver, then wait
1669 * for CAC to complete.
1671 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1672 return res_dfs_offload;
1677 #endif /* NEED_AP_MLME */
1680 if (hapd->iconf->fst_cfg.group_id[0]) {
1681 struct fst_wpa_obj iface_obj;
1683 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1684 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1685 &iface_obj, &hapd->iconf->fst_cfg);
1687 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1688 hapd->iconf->fst_cfg.group_id);
1692 #endif /* CONFIG_FST */
1694 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1695 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1696 if (hapd->setup_complete_cb)
1697 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1699 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1700 iface->bss[0]->conf->iface);
1701 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1702 iface->interfaces->terminate_on_error--;
1707 wpa_printf(MSG_ERROR, "Interface initialization failed");
1708 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1709 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1712 fst_detach(iface->fst);
1715 #endif /* CONFIG_FST */
1716 if (iface->interfaces && iface->interfaces->terminate_on_error)
1723 * hostapd_setup_interface - Setup of an interface
1724 * @iface: Pointer to interface data.
1725 * Returns: 0 on success, -1 on failure
1727 * Initializes the driver interface, validates the configuration,
1728 * and sets driver parameters based on the configuration.
1729 * Flushes old stations, sets the channel, encryption,
1730 * beacons, and WDS links based on the configuration.
1732 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1733 * or DFS operations, this function returns 0 before such operations have been
1734 * completed. The pending operations are registered into eloop and will be
1735 * completed from eloop callbacks. Those callbacks end up calling
1736 * hostapd_setup_interface_complete() once setup has been completed.
1738 int hostapd_setup_interface(struct hostapd_iface *iface)
1742 ret = setup_interface(iface);
1744 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1745 iface->bss[0]->conf->iface);
1754 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1755 * @hapd_iface: Pointer to interface data
1756 * @conf: Pointer to per-interface configuration
1757 * @bss: Pointer to per-BSS configuration for this BSS
1758 * Returns: Pointer to allocated BSS data
1760 * This function is used to allocate per-BSS data structure. This data will be
1761 * freed after hostapd_cleanup() is called for it during interface
1764 struct hostapd_data *
1765 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1766 struct hostapd_config *conf,
1767 struct hostapd_bss_config *bss)
1769 struct hostapd_data *hapd;
1771 hapd = os_zalloc(sizeof(*hapd));
1775 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1778 hapd->iface = hapd_iface;
1779 hapd->driver = hapd->iconf->driver;
1780 hapd->ctrl_sock = -1;
1786 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1788 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1790 hostapd_bss_deinit_no_free(hapd);
1791 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1792 hostapd_cleanup(hapd);
1796 void hostapd_interface_deinit(struct hostapd_iface *iface)
1800 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1804 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1806 #ifdef CONFIG_IEEE80211N
1808 hostapd_stop_setup_timers(iface);
1809 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1810 #endif /* NEED_AP_MLME */
1811 #endif /* CONFIG_IEEE80211N */
1812 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1813 iface->wait_channel_update = 0;
1817 fst_detach(iface->fst);
1820 #endif /* CONFIG_FST */
1822 for (j = iface->num_bss - 1; j >= 0; j--)
1823 hostapd_bss_deinit(iface->bss[j]);
1827 void hostapd_interface_free(struct hostapd_iface *iface)
1830 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1831 for (j = 0; j < iface->num_bss; j++) {
1832 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1833 __func__, iface->bss[j]);
1834 os_free(iface->bss[j]);
1836 hostapd_cleanup_iface(iface);
1841 * hostapd_init - Allocate and initialize per-interface data
1842 * @config_file: Path to the configuration file
1843 * Returns: Pointer to the allocated interface data or %NULL on failure
1845 * This function is used to allocate main data structures for per-interface
1846 * data. The allocated data buffer will be freed by calling
1847 * hostapd_cleanup_iface().
1849 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1850 const char *config_file)
1852 struct hostapd_iface *hapd_iface = NULL;
1853 struct hostapd_config *conf = NULL;
1854 struct hostapd_data *hapd;
1857 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1858 if (hapd_iface == NULL)
1861 hapd_iface->config_fname = os_strdup(config_file);
1862 if (hapd_iface->config_fname == NULL)
1865 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1868 hapd_iface->conf = conf;
1870 hapd_iface->num_bss = conf->num_bss;
1871 hapd_iface->bss = os_calloc(conf->num_bss,
1872 sizeof(struct hostapd_data *));
1873 if (hapd_iface->bss == NULL)
1876 for (i = 0; i < conf->num_bss; i++) {
1877 hapd = hapd_iface->bss[i] =
1878 hostapd_alloc_bss_data(hapd_iface, conf,
1882 hapd->msg_ctx = hapd;
1888 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1891 hostapd_config_free(conf);
1893 os_free(hapd_iface->config_fname);
1894 os_free(hapd_iface->bss);
1895 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1896 __func__, hapd_iface);
1897 os_free(hapd_iface);
1903 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1907 for (i = 0; i < interfaces->count; i++) {
1908 struct hostapd_iface *iface = interfaces->iface[i];
1909 for (j = 0; j < iface->num_bss; j++) {
1910 struct hostapd_data *hapd = iface->bss[j];
1911 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1921 * hostapd_interface_init_bss - Read configuration file and init BSS data
1923 * This function is used to parse configuration file for a BSS. This BSS is
1924 * added to an existing interface sharing the same radio (if any) or a new
1925 * interface is created if this is the first interface on a radio. This
1926 * allocate memory for the BSS. No actual driver operations are started.
1928 * This is similar to hostapd_interface_init(), but for a case where the
1929 * configuration is used to add a single BSS instead of all BSSes for a radio.
1931 struct hostapd_iface *
1932 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1933 const char *config_fname, int debug)
1935 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1936 struct hostapd_data *hapd;
1943 for (i = 0; i < interfaces->count; i++) {
1944 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1945 iface = interfaces->iface[i];
1950 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1951 config_fname, phy, iface ? "" : " --> new PHY");
1953 struct hostapd_config *conf;
1954 struct hostapd_bss_config **tmp_conf;
1955 struct hostapd_data **tmp_bss;
1956 struct hostapd_bss_config *bss;
1959 /* Add new BSS to existing iface */
1960 conf = interfaces->config_read_cb(config_fname);
1963 if (conf->num_bss > 1) {
1964 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1965 hostapd_config_free(conf);
1969 ifname = conf->bss[0]->iface;
1970 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1971 wpa_printf(MSG_ERROR,
1972 "Interface name %s already in use", ifname);
1973 hostapd_config_free(conf);
1977 tmp_conf = os_realloc_array(
1978 iface->conf->bss, iface->conf->num_bss + 1,
1979 sizeof(struct hostapd_bss_config *));
1980 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1981 sizeof(struct hostapd_data *));
1983 iface->bss = tmp_bss;
1985 iface->conf->bss = tmp_conf;
1986 iface->conf->last_bss = tmp_conf[0];
1988 if (tmp_bss == NULL || tmp_conf == NULL) {
1989 hostapd_config_free(conf);
1992 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1993 iface->conf->num_bss++;
1995 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1997 iface->conf->num_bss--;
1998 hostapd_config_free(conf);
2001 iface->conf->last_bss = bss;
2002 iface->bss[iface->num_bss] = hapd;
2003 hapd->msg_ctx = hapd;
2005 bss_idx = iface->num_bss++;
2007 conf->bss[0] = NULL;
2008 hostapd_config_free(conf);
2010 /* Add a new iface with the first BSS */
2011 new_iface = iface = hostapd_init(interfaces, config_fname);
2014 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2015 iface->interfaces = interfaces;
2019 for (k = 0; k < debug; k++) {
2020 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2021 iface->bss[bss_idx]->conf->logger_stdout_level--;
2024 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2025 !hostapd_drv_none(iface->bss[bss_idx])) {
2026 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2029 hostapd_interface_deinit_free(new_iface);
2037 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2039 const struct wpa_driver_ops *driver;
2042 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2045 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2046 __func__, (unsigned int) iface->num_bss,
2047 (unsigned int) iface->conf->num_bss);
2048 driver = iface->bss[0]->driver;
2049 drv_priv = iface->bss[0]->drv_priv;
2050 hostapd_interface_deinit(iface);
2051 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2052 __func__, driver, drv_priv);
2053 if (driver && driver->hapd_deinit && drv_priv) {
2054 driver->hapd_deinit(drv_priv);
2055 iface->bss[0]->drv_priv = NULL;
2057 hostapd_interface_free(iface);
2061 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2063 struct hostapd_iface *hapd_iface)
2067 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2068 __func__, driver, drv_priv);
2069 if (driver && driver->hapd_deinit && drv_priv) {
2070 driver->hapd_deinit(drv_priv);
2071 for (j = 0; j < hapd_iface->num_bss; j++) {
2072 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2074 hapd_iface->bss[j]->drv_priv);
2075 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2076 hapd_iface->bss[j]->drv_priv = NULL;
2082 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2086 if (hapd_iface->bss[0]->drv_priv != NULL) {
2087 wpa_printf(MSG_ERROR, "Interface %s already enabled",
2088 hapd_iface->conf->bss[0]->iface);
2092 wpa_printf(MSG_DEBUG, "Enable interface %s",
2093 hapd_iface->conf->bss[0]->iface);
2095 for (j = 0; j < hapd_iface->num_bss; j++)
2096 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2097 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2098 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2102 if (hapd_iface->interfaces == NULL ||
2103 hapd_iface->interfaces->driver_init == NULL ||
2104 hapd_iface->interfaces->driver_init(hapd_iface))
2107 if (hostapd_setup_interface(hapd_iface)) {
2108 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2109 hapd_iface->bss[0]->drv_priv,
2118 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2122 wpa_printf(MSG_DEBUG, "Reload interface %s",
2123 hapd_iface->conf->bss[0]->iface);
2124 for (j = 0; j < hapd_iface->num_bss; j++)
2125 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
2126 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2127 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2130 hostapd_clear_old(hapd_iface);
2131 for (j = 0; j < hapd_iface->num_bss; j++)
2132 hostapd_reload_bss(hapd_iface->bss[j]);
2138 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2141 const struct wpa_driver_ops *driver;
2144 if (hapd_iface == NULL)
2147 if (hapd_iface->bss[0]->drv_priv == NULL) {
2148 wpa_printf(MSG_INFO, "Interface %s already disabled",
2149 hapd_iface->conf->bss[0]->iface);
2153 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
2154 driver = hapd_iface->bss[0]->driver;
2155 drv_priv = hapd_iface->bss[0]->drv_priv;
2157 hapd_iface->driver_ap_teardown =
2158 !!(hapd_iface->drv_flags &
2159 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2161 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
2162 for (j = 0; j < hapd_iface->num_bss; j++) {
2163 struct hostapd_data *hapd = hapd_iface->bss[j];
2164 hostapd_bss_deinit_no_free(hapd);
2165 hostapd_free_hapd_data(hapd);
2168 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
2170 /* From hostapd_cleanup_iface: These were initialized in
2171 * hostapd_setup_interface and hostapd_setup_interface_complete
2173 hostapd_cleanup_iface_partial(hapd_iface);
2175 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2176 hapd_iface->bss[0]->conf->iface);
2177 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
2182 static struct hostapd_iface *
2183 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2185 struct hostapd_iface **iface, *hapd_iface;
2187 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2188 sizeof(struct hostapd_iface *));
2191 interfaces->iface = iface;
2192 hapd_iface = interfaces->iface[interfaces->count] =
2193 os_zalloc(sizeof(*hapd_iface));
2194 if (hapd_iface == NULL) {
2195 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2196 "the interface", __func__);
2199 interfaces->count++;
2200 hapd_iface->interfaces = interfaces;
2206 static struct hostapd_config *
2207 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
2208 const char *ctrl_iface, const char *driver)
2210 struct hostapd_bss_config *bss;
2211 struct hostapd_config *conf;
2213 /* Allocates memory for bss and conf */
2214 conf = hostapd_config_defaults();
2216 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2217 "configuration", __func__);
2224 for (j = 0; wpa_drivers[j]; j++) {
2225 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2226 conf->driver = wpa_drivers[j];
2231 wpa_printf(MSG_ERROR,
2232 "Invalid/unknown driver '%s' - registering the default driver",
2236 conf->driver = wpa_drivers[0];
2237 if (conf->driver == NULL) {
2238 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2239 hostapd_config_free(conf);
2244 bss = conf->last_bss = conf->bss[0];
2246 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2247 bss->ctrl_interface = os_strdup(ctrl_iface);
2248 if (bss->ctrl_interface == NULL) {
2249 hostapd_config_free(conf);
2253 /* Reading configuration file skipped, will be done in SET!
2254 * From reading the configuration till the end has to be done in
2261 static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2262 struct hostapd_config *conf)
2265 struct hostapd_data *hapd;
2267 hapd_iface->bss = os_calloc(conf->num_bss,
2268 sizeof(struct hostapd_data *));
2269 if (hapd_iface->bss == NULL)
2272 for (i = 0; i < conf->num_bss; i++) {
2273 hapd = hapd_iface->bss[i] =
2274 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
2278 os_free(hapd_iface->bss[i]);
2279 hapd_iface->bss[i] = NULL;
2281 os_free(hapd_iface->bss);
2282 hapd_iface->bss = NULL;
2285 hapd->msg_ctx = hapd;
2288 hapd_iface->conf = conf;
2289 hapd_iface->num_bss = conf->num_bss;
2295 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2297 struct hostapd_config *conf = NULL;
2298 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2299 struct hostapd_data *hapd;
2302 const char *conf_file = NULL, *phy_name = NULL;
2304 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2306 phy_name = buf + 11;
2307 pos = os_strchr(phy_name, ':');
2312 if (!os_strlen(conf_file))
2315 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2319 for (j = 0; j < interfaces->count; j++) {
2320 if (interfaces->iface[j] == hapd_iface)
2323 if (j == interfaces->count) {
2324 struct hostapd_iface **tmp;
2325 tmp = os_realloc_array(interfaces->iface,
2326 interfaces->count + 1,
2327 sizeof(struct hostapd_iface *));
2329 hostapd_interface_deinit_free(hapd_iface);
2332 interfaces->iface = tmp;
2333 interfaces->iface[interfaces->count++] = hapd_iface;
2334 new_iface = hapd_iface;
2338 if (interfaces->driver_init(hapd_iface))
2341 if (hostapd_setup_interface(hapd_iface)) {
2342 hostapd_deinit_driver(
2343 hapd_iface->bss[0]->driver,
2344 hapd_iface->bss[0]->drv_priv,
2349 /* Assign new BSS with bss[0]'s driver info */
2350 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2351 hapd->driver = hapd_iface->bss[0]->driver;
2352 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2353 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2356 if (start_ctrl_iface_bss(hapd) < 0 ||
2357 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2358 hostapd_setup_bss(hapd, -1))) {
2359 hostapd_cleanup(hapd);
2360 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
2361 hapd_iface->conf->num_bss--;
2362 hapd_iface->num_bss--;
2363 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2364 __func__, hapd, hapd->conf->iface);
2365 hostapd_config_free_bss(hapd->conf);
2374 ptr = os_strchr(buf, ' ');
2379 if (os_strncmp(ptr, "config=", 7) == 0)
2380 conf_file = ptr + 7;
2382 for (i = 0; i < interfaces->count; i++) {
2383 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
2385 wpa_printf(MSG_INFO, "Cannot add interface - it "
2391 hapd_iface = hostapd_iface_alloc(interfaces);
2392 if (hapd_iface == NULL) {
2393 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2394 "for interface", __func__);
2397 new_iface = hapd_iface;
2399 if (conf_file && interfaces->config_read_cb) {
2400 conf = interfaces->config_read_cb(conf_file);
2401 if (conf && conf->bss)
2402 os_strlcpy(conf->bss[0]->iface, buf,
2403 sizeof(conf->bss[0]->iface));
2405 char *driver = os_strchr(ptr, ' ');
2409 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2412 if (conf == NULL || conf->bss == NULL) {
2413 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2414 "for configuration", __func__);
2418 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
2419 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2420 "for hostapd", __func__);
2425 if (start_ctrl_iface(hapd_iface) < 0)
2428 wpa_printf(MSG_INFO, "Add interface '%s'",
2429 hapd_iface->conf->bss[0]->iface);
2435 hostapd_config_free(conf);
2437 if (hapd_iface->bss) {
2438 for (i = 0; i < hapd_iface->num_bss; i++) {
2439 hapd = hapd_iface->bss[i];
2442 if (hapd_iface->interfaces &&
2443 hapd_iface->interfaces->ctrl_iface_deinit)
2444 hapd_iface->interfaces->
2445 ctrl_iface_deinit(hapd);
2446 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2447 __func__, hapd_iface->bss[i],
2449 hostapd_cleanup(hapd);
2451 hapd_iface->bss[i] = NULL;
2453 os_free(hapd_iface->bss);
2454 hapd_iface->bss = NULL;
2457 interfaces->count--;
2458 interfaces->iface[interfaces->count] = NULL;
2460 hostapd_cleanup_iface(hapd_iface);
2466 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2470 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2472 /* Remove hostapd_data only if it has already been initialized */
2473 if (idx < iface->num_bss) {
2474 struct hostapd_data *hapd = iface->bss[idx];
2476 hostapd_bss_deinit(hapd);
2477 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2478 __func__, hapd, hapd->conf->iface);
2479 hostapd_config_free_bss(hapd->conf);
2485 for (i = idx; i < iface->num_bss; i++)
2486 iface->bss[i] = iface->bss[i + 1];
2488 hostapd_config_free_bss(iface->conf->bss[idx]);
2489 iface->conf->bss[idx] = NULL;
2492 iface->conf->num_bss--;
2493 for (i = idx; i < iface->conf->num_bss; i++)
2494 iface->conf->bss[i] = iface->conf->bss[i + 1];
2500 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2502 struct hostapd_iface *hapd_iface;
2505 for (i = 0; i < interfaces->count; i++) {
2506 hapd_iface = interfaces->iface[i];
2507 if (hapd_iface == NULL)
2509 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2510 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2511 hapd_iface->driver_ap_teardown =
2512 !!(hapd_iface->drv_flags &
2513 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2515 hostapd_interface_deinit_free(hapd_iface);
2517 while (k < (interfaces->count - 1)) {
2518 interfaces->iface[k] =
2519 interfaces->iface[k + 1];
2522 interfaces->count--;
2526 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2527 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2528 hapd_iface->driver_ap_teardown =
2529 !(hapd_iface->drv_flags &
2530 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2531 return hostapd_remove_bss(hapd_iface, j);
2540 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2541 * @hapd: Pointer to BSS data
2542 * @sta: Pointer to the associated STA data
2543 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2545 * This function will be called whenever a station associates with the AP. It
2546 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2547 * from drv_callbacks.c based on driver events for drivers that take care of
2548 * management frames (IEEE 802.11 authentication and association) internally.
2550 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2553 if (hapd->tkip_countermeasures) {
2554 hostapd_drv_sta_deauth(hapd, sta->addr,
2555 WLAN_REASON_MICHAEL_MIC_FAILURE);
2559 hostapd_prune_associations(hapd, sta->addr);
2561 /* IEEE 802.11F (IAPP) */
2562 if (hapd->conf->ieee802_11f)
2563 iapp_new_station(hapd->iapp, sta);
2566 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2567 sta->no_p2p_set = 1;
2568 hapd->num_sta_no_p2p++;
2569 if (hapd->num_sta_no_p2p == 1)
2570 hostapd_p2p_non_p2p_sta_connected(hapd);
2572 #endif /* CONFIG_P2P */
2574 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2575 * IEEE 802.1X/WPA code will start accounting after the station has
2576 * been authorized. */
2577 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2578 ap_sta_set_authorized(hapd, sta, 1);
2579 os_get_reltime(&sta->connected_time);
2580 accounting_sta_start(hapd, sta);
2583 /* Start IEEE 802.1X authentication process for new stations */
2584 ieee802_1x_new_station(hapd, sta);
2586 if (sta->auth_alg != WLAN_AUTH_FT &&
2587 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2588 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2590 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2592 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2593 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2594 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2595 __func__, MAC2STR(sta->addr),
2596 hapd->conf->ap_max_inactivity);
2597 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2598 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2599 ap_handle_timer, hapd, sta);
2604 const char * hostapd_state_text(enum hostapd_iface_state s)
2607 case HAPD_IFACE_UNINITIALIZED:
2608 return "UNINITIALIZED";
2609 case HAPD_IFACE_DISABLED:
2611 case HAPD_IFACE_COUNTRY_UPDATE:
2612 return "COUNTRY_UPDATE";
2613 case HAPD_IFACE_ACS:
2615 case HAPD_IFACE_HT_SCAN:
2617 case HAPD_IFACE_DFS:
2619 case HAPD_IFACE_ENABLED:
2627 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2629 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2630 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2631 hostapd_state_text(s));
2638 static void free_beacon_data(struct beacon_data *beacon)
2640 os_free(beacon->head);
2641 beacon->head = NULL;
2642 os_free(beacon->tail);
2643 beacon->tail = NULL;
2644 os_free(beacon->probe_resp);
2645 beacon->probe_resp = NULL;
2646 os_free(beacon->beacon_ies);
2647 beacon->beacon_ies = NULL;
2648 os_free(beacon->proberesp_ies);
2649 beacon->proberesp_ies = NULL;
2650 os_free(beacon->assocresp_ies);
2651 beacon->assocresp_ies = NULL;
2655 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2656 struct beacon_data *beacon)
2658 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2659 struct wpa_driver_ap_params params;
2662 os_memset(beacon, 0, sizeof(*beacon));
2663 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2667 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2671 goto free_ap_params;
2674 beacon->head = os_malloc(params.head_len);
2676 goto free_ap_extra_ies;
2678 os_memcpy(beacon->head, params.head, params.head_len);
2679 beacon->head_len = params.head_len;
2681 beacon->tail = os_malloc(params.tail_len);
2685 os_memcpy(beacon->tail, params.tail, params.tail_len);
2686 beacon->tail_len = params.tail_len;
2688 if (params.proberesp != NULL) {
2689 beacon->probe_resp = os_malloc(params.proberesp_len);
2690 if (!beacon->probe_resp)
2693 os_memcpy(beacon->probe_resp, params.proberesp,
2694 params.proberesp_len);
2695 beacon->probe_resp_len = params.proberesp_len;
2698 /* copy the extra ies */
2700 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2701 if (!beacon->beacon_ies)
2704 os_memcpy(beacon->beacon_ies,
2705 beacon_extra->buf, wpabuf_len(beacon_extra));
2706 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2709 if (proberesp_extra) {
2710 beacon->proberesp_ies =
2711 os_malloc(wpabuf_len(proberesp_extra));
2712 if (!beacon->proberesp_ies)
2715 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2716 wpabuf_len(proberesp_extra));
2717 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2720 if (assocresp_extra) {
2721 beacon->assocresp_ies =
2722 os_malloc(wpabuf_len(assocresp_extra));
2723 if (!beacon->assocresp_ies)
2726 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2727 wpabuf_len(assocresp_extra));
2728 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2733 /* if the function fails, the caller should not free beacon data */
2735 free_beacon_data(beacon);
2738 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2741 ieee802_11_free_ap_params(¶ms);
2747 * TODO: This flow currently supports only changing frequency within the
2748 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2749 * width) are not supported.
2751 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2752 struct hostapd_config *conf,
2753 struct hostapd_freq_params *params,
2754 struct hostapd_freq_params *old_params)
2758 if (!params->channel) {
2759 /* check if the new channel is supported by hw */
2760 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2763 channel = params->channel;
2767 /* if a pointer to old_params is provided we save previous state */
2769 old_params->channel = conf->channel;
2770 old_params->ht_enabled = conf->ieee80211n;
2771 old_params->sec_channel_offset = conf->secondary_channel;
2774 conf->channel = channel;
2775 conf->ieee80211n = params->ht_enabled;
2776 conf->secondary_channel = params->sec_channel_offset;
2778 /* TODO: maybe call here hostapd_config_check here? */
2784 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2785 struct csa_settings *settings)
2787 struct hostapd_iface *iface = hapd->iface;
2788 struct hostapd_freq_params old_freq;
2791 os_memset(&old_freq, 0, sizeof(old_freq));
2792 if (!iface || !iface->freq || hapd->csa_in_progress)
2795 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2796 &settings->freq_params,
2801 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2803 /* change back the configuration */
2804 hostapd_change_config_freq(iface->bss[0], iface->conf,
2810 /* set channel switch parameters for csa ie */
2811 hapd->cs_freq_params = settings->freq_params;
2812 hapd->cs_count = settings->cs_count;
2813 hapd->cs_block_tx = settings->block_tx;
2815 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2817 free_beacon_data(&settings->beacon_after);
2821 settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2822 settings->counter_offset_presp = hapd->cs_c_off_proberesp;
2828 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2830 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2832 hapd->cs_block_tx = 0;
2833 hapd->cs_c_off_beacon = 0;
2834 hapd->cs_c_off_proberesp = 0;
2835 hapd->csa_in_progress = 0;
2839 int hostapd_switch_channel(struct hostapd_data *hapd,
2840 struct csa_settings *settings)
2844 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2845 wpa_printf(MSG_INFO, "CSA is not supported");
2849 ret = hostapd_fill_csa_settings(hapd, settings);
2853 ret = hostapd_drv_switch_channel(hapd, settings);
2854 free_beacon_data(&settings->beacon_csa);
2855 free_beacon_data(&settings->beacon_after);
2858 /* if we failed, clean cs parameters */
2859 hostapd_cleanup_cs_params(hapd);
2863 hapd->csa_in_progress = 1;
2869 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2870 const struct hostapd_freq_params *freq_params)
2872 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2875 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2877 if (freq_params->center_freq1)
2878 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2879 if (freq_params->center_freq2)
2880 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2882 switch (freq_params->bandwidth) {
2886 vht_bw = VHT_CHANWIDTH_USE_HT;
2889 if (freq_params->center_freq2)
2890 vht_bw = VHT_CHANWIDTH_80P80MHZ;
2892 vht_bw = VHT_CHANWIDTH_80MHZ;
2895 vht_bw = VHT_CHANWIDTH_160MHZ;
2898 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2899 freq_params->bandwidth);
2903 iface->freq = freq_params->freq;
2904 iface->conf->channel = freq_params->channel;
2905 iface->conf->secondary_channel = freq_params->sec_channel_offset;
2906 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2907 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2908 iface->conf->vht_oper_chwidth = vht_bw;
2909 iface->conf->ieee80211n = freq_params->ht_enabled;
2910 iface->conf->ieee80211ac = freq_params->vht_enabled;
2913 * cs_params must not be cleared earlier because the freq_params
2914 * argument may actually point to one of these.
2916 for (i = 0; i < iface->num_bss; i++)
2917 hostapd_cleanup_cs_params(iface->bss[i]);
2919 hostapd_disable_iface(iface);
2920 hostapd_enable_iface(iface);
2924 struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
2929 for (i = 0; i < interfaces->count; i++) {
2930 struct hostapd_iface *iface = interfaces->iface[i];
2932 for (j = 0; j < iface->num_bss; j++) {
2933 struct hostapd_data *hapd = iface->bss[j];
2935 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2943 #endif /* NEED_AP_MLME */
2946 void hostapd_periodic_iface(struct hostapd_iface *iface)
2950 ap_list_timer(iface);
2952 for (i = 0; i < iface->num_bss; i++) {
2953 struct hostapd_data *hapd = iface->bss[i];
2958 #ifndef CONFIG_NO_RADIUS
2959 hostapd_acl_expire(hapd);
2960 #endif /* CONFIG_NO_RADIUS */