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"
21 #include "accounting.h"
25 #include "ieee802_1x.h"
26 #include "ieee802_11_auth.h"
27 #include "vlan_init.h"
29 #include "wps_hostapd.h"
30 #include "hw_features.h"
31 #include "wpa_auth_glue.h"
32 #include "ap_drv_ops.h"
33 #include "ap_config.h"
34 #include "p2p_hostapd.h"
37 #include "ieee802_11.h"
40 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
41 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
42 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
43 static int setup_interface2(struct hostapd_iface *iface);
44 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
47 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
48 int (*cb)(struct hostapd_iface *iface,
49 void *ctx), void *ctx)
54 for (i = 0; i < interfaces->count; i++) {
55 ret = cb(interfaces->iface[i], ctx);
64 static void hostapd_reload_bss(struct hostapd_data *hapd)
66 struct hostapd_ssid *ssid;
68 #ifndef CONFIG_NO_RADIUS
69 radius_client_reconfig(hapd->radius, hapd->conf->radius);
70 #endif /* CONFIG_NO_RADIUS */
72 ssid = &hapd->conf->ssid;
73 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
74 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
76 * Force PSK to be derived again since SSID or passphrase may
79 os_free(ssid->wpa_psk);
82 if (hostapd_setup_wpa_psk(hapd->conf)) {
83 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
84 "after reloading configuration");
87 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
88 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
90 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
92 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
93 hostapd_setup_wpa(hapd);
95 wpa_init_keys(hapd->wpa_auth);
96 } else if (hapd->conf->wpa) {
99 hostapd_reconfig_wpa(hapd);
100 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
101 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
102 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
103 "the kernel driver.");
104 } else if (hapd->wpa_auth) {
105 wpa_deinit(hapd->wpa_auth);
106 hapd->wpa_auth = NULL;
107 hostapd_set_privacy(hapd, 0);
108 hostapd_setup_encryption(hapd->conf->iface, hapd);
109 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
112 ieee802_11_set_beacon(hapd);
113 hostapd_update_wps(hapd);
115 if (hapd->conf->ssid.ssid_set &&
116 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
117 hapd->conf->ssid.ssid_len)) {
118 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
119 /* try to continue */
121 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
125 static void hostapd_clear_old(struct hostapd_iface *iface)
130 * Deauthenticate all stations since the new configuration may not
131 * allow them to use the BSS anymore.
133 for (j = 0; j < iface->num_bss; j++) {
134 hostapd_flush_old_stations(iface->bss[j],
135 WLAN_REASON_PREV_AUTH_NOT_VALID);
136 hostapd_broadcast_wep_clear(iface->bss[j]);
138 #ifndef CONFIG_NO_RADIUS
139 /* TODO: update dynamic data based on changed configuration
140 * items (e.g., open/close sockets, etc.) */
141 radius_client_flush(iface->bss[j]->radius, 0);
142 #endif /* CONFIG_NO_RADIUS */
147 int hostapd_reload_config(struct hostapd_iface *iface)
149 struct hostapd_data *hapd = iface->bss[0];
150 struct hostapd_config *newconf, *oldconf;
153 if (iface->config_fname == NULL) {
154 /* Only in-memory config in use - assume it has been updated */
155 hostapd_clear_old(iface);
156 for (j = 0; j < iface->num_bss; j++)
157 hostapd_reload_bss(iface->bss[j]);
161 if (iface->interfaces == NULL ||
162 iface->interfaces->config_read_cb == NULL)
164 newconf = iface->interfaces->config_read_cb(iface->config_fname);
168 hostapd_clear_old(iface);
170 oldconf = hapd->iconf;
171 iface->conf = newconf;
173 for (j = 0; j < iface->num_bss; j++) {
174 hapd = iface->bss[j];
175 hapd->iconf = newconf;
176 hapd->iconf->channel = oldconf->channel;
177 hapd->iconf->secondary_channel = oldconf->secondary_channel;
178 hapd->iconf->ieee80211n = oldconf->ieee80211n;
179 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
180 hapd->iconf->ht_capab = oldconf->ht_capab;
181 hapd->iconf->vht_capab = oldconf->vht_capab;
182 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
183 hapd->iconf->vht_oper_centr_freq_seg0_idx =
184 oldconf->vht_oper_centr_freq_seg0_idx;
185 hapd->iconf->vht_oper_centr_freq_seg1_idx =
186 oldconf->vht_oper_centr_freq_seg1_idx;
187 hapd->conf = newconf->bss[j];
188 hostapd_reload_bss(hapd);
191 hostapd_config_free(oldconf);
198 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
203 for (i = 0; i < NUM_WEP_KEYS; i++) {
204 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
205 0, NULL, 0, NULL, 0)) {
206 wpa_printf(MSG_DEBUG, "Failed to clear default "
207 "encryption keys (ifname=%s keyidx=%d)",
211 #ifdef CONFIG_IEEE80211W
212 if (hapd->conf->ieee80211w) {
213 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
214 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
217 wpa_printf(MSG_DEBUG, "Failed to clear "
218 "default mgmt encryption keys "
219 "(ifname=%s keyidx=%d)", ifname, i);
223 #endif /* CONFIG_IEEE80211W */
227 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
229 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
234 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
237 struct hostapd_ssid *ssid = &hapd->conf->ssid;
240 if (ssid->wep.default_len &&
241 hostapd_drv_set_key(hapd->conf->iface,
242 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
243 1, NULL, 0, ssid->wep.key[idx],
244 ssid->wep.len[idx])) {
245 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
253 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
255 if (!hapd->started) {
256 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
257 __func__, hapd->conf->iface);
262 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
263 iapp_deinit(hapd->iapp);
265 accounting_deinit(hapd);
266 hostapd_deinit_wpa(hapd);
268 hostapd_acl_deinit(hapd);
269 #ifndef CONFIG_NO_RADIUS
270 radius_client_deinit(hapd->radius);
272 radius_das_deinit(hapd->radius_das);
273 hapd->radius_das = NULL;
274 #endif /* CONFIG_NO_RADIUS */
276 hostapd_deinit_wps(hapd);
278 authsrv_deinit(hapd);
280 if (hapd->interface_added) {
281 hapd->interface_added = 0;
282 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
283 wpa_printf(MSG_WARNING,
284 "Failed to remove BSS interface %s",
286 hapd->interface_added = 1;
290 os_free(hapd->probereq_cb);
291 hapd->probereq_cb = NULL;
294 wpabuf_free(hapd->p2p_beacon_ie);
295 hapd->p2p_beacon_ie = NULL;
296 wpabuf_free(hapd->p2p_probe_resp_ie);
297 hapd->p2p_probe_resp_ie = NULL;
298 #endif /* CONFIG_P2P */
300 wpabuf_free(hapd->time_adv);
302 #ifdef CONFIG_INTERWORKING
303 gas_serv_deinit(hapd);
304 #endif /* CONFIG_INTERWORKING */
307 os_free(hapd->tmp_eap_user.identity);
308 os_free(hapd->tmp_eap_user.password);
309 #endif /* CONFIG_SQLITE */
314 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
315 * @hapd: Pointer to BSS data
317 * This function is used to free all per-BSS data structures and resources.
318 * Most of the modules that are initialized in hostapd_setup_bss() are
319 * deinitialized here.
321 static void hostapd_cleanup(struct hostapd_data *hapd)
323 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
325 if (hapd->iface->interfaces &&
326 hapd->iface->interfaces->ctrl_iface_deinit)
327 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
328 hostapd_free_hapd_data(hapd);
332 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
334 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
335 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
336 iface->hw_features = NULL;
337 os_free(iface->current_rates);
338 iface->current_rates = NULL;
339 os_free(iface->basic_rates);
340 iface->basic_rates = NULL;
341 ap_list_deinit(iface);
346 * hostapd_cleanup_iface - Complete per-interface cleanup
347 * @iface: Pointer to interface data
349 * This function is called after per-BSS data structures are deinitialized
350 * with hostapd_cleanup().
352 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
354 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
355 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
357 hostapd_cleanup_iface_partial(iface);
358 hostapd_config_free(iface->conf);
361 os_free(iface->config_fname);
363 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
368 static void hostapd_clear_wep(struct hostapd_data *hapd)
370 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
371 hostapd_set_privacy(hapd, 0);
372 hostapd_broadcast_wep_clear(hapd);
377 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
381 hostapd_broadcast_wep_set(hapd);
383 if (hapd->conf->ssid.wep.default_len) {
384 hostapd_set_privacy(hapd, 1);
389 * When IEEE 802.1X is not enabled, the driver may need to know how to
390 * set authentication algorithms for static WEP.
392 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
394 for (i = 0; i < 4; i++) {
395 if (hapd->conf->ssid.wep.key[i] &&
396 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
397 i == hapd->conf->ssid.wep.idx, NULL, 0,
398 hapd->conf->ssid.wep.key[i],
399 hapd->conf->ssid.wep.len[i])) {
400 wpa_printf(MSG_WARNING, "Could not set WEP "
404 if (hapd->conf->ssid.wep.key[i] &&
405 i == hapd->conf->ssid.wep.idx)
406 hostapd_set_privacy(hapd, 1);
413 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
418 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
421 if (!hapd->iface->driver_ap_teardown) {
422 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
423 "Flushing old station entries");
425 if (hostapd_flush(hapd)) {
426 wpa_msg(hapd->msg_ctx, MSG_WARNING,
427 "Could not connect to kernel driver");
431 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
432 os_memset(addr, 0xff, ETH_ALEN);
433 hostapd_drv_sta_deauth(hapd, addr, reason);
434 hostapd_free_stas(hapd);
440 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
442 hostapd_free_stas(hapd);
443 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
444 hostapd_clear_wep(hapd);
449 * hostapd_validate_bssid_configuration - Validate BSSID configuration
450 * @iface: Pointer to interface data
451 * Returns: 0 on success, -1 on failure
453 * This function is used to validate that the configured BSSIDs are valid.
455 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
457 u8 mask[ETH_ALEN] = { 0 };
458 struct hostapd_data *hapd = iface->bss[0];
459 unsigned int i = iface->conf->num_bss, bits = 0, j;
462 if (hostapd_drv_none(hapd))
465 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
467 /* Determine the bits necessary to cover the number of BSSIDs. */
468 for (i--; i; i >>= 1)
471 /* Determine the bits necessary to any configured BSSIDs,
472 if they are higher than the number of BSSIDs. */
473 for (j = 0; j < iface->conf->num_bss; j++) {
474 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
480 for (i = 0; i < ETH_ALEN; i++) {
482 iface->conf->bss[j]->bssid[i] ^
490 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
496 while (mask[i] != 0) {
506 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
511 os_memset(mask, 0xff, ETH_ALEN);
513 for (i = 5; i > 5 - j; i--)
520 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
521 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
526 for (i = 0; i < ETH_ALEN; i++) {
527 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
528 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
529 " for start address " MACSTR ".",
530 MAC2STR(mask), MAC2STR(hapd->own_addr));
531 wpa_printf(MSG_ERROR, "Start address must be the "
532 "first address in the block (i.e., addr "
533 "AND mask == addr).");
542 static int mac_in_conf(struct hostapd_config *conf, const void *a)
546 for (i = 0; i < conf->num_bss; i++) {
547 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
556 #ifndef CONFIG_NO_RADIUS
558 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
559 struct radius_das_attrs *attr)
561 if (attr->nas_identifier &&
562 (!hapd->conf->nas_identifier ||
563 os_strlen(hapd->conf->nas_identifier) !=
564 attr->nas_identifier_len ||
565 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
566 attr->nas_identifier_len) != 0)) {
567 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
571 if (attr->nas_ip_addr &&
572 (hapd->conf->own_ip_addr.af != AF_INET ||
573 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
575 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
580 if (attr->nas_ipv6_addr &&
581 (hapd->conf->own_ip_addr.af != AF_INET6 ||
582 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
584 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
587 #endif /* CONFIG_IPV6 */
593 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
594 struct radius_das_attrs *attr)
596 struct sta_info *sta = NULL;
600 sta = ap_get_sta(hapd, attr->sta_addr);
602 if (sta == NULL && attr->acct_session_id &&
603 attr->acct_session_id_len == 17) {
604 for (sta = hapd->sta_list; sta; sta = sta->next) {
605 os_snprintf(buf, sizeof(buf), "%08X-%08X",
606 sta->acct_session_id_hi,
607 sta->acct_session_id_lo);
608 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
613 if (sta == NULL && attr->cui) {
614 for (sta = hapd->sta_list; sta; sta = sta->next) {
616 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
617 if (cui && wpabuf_len(cui) == attr->cui_len &&
618 os_memcmp(wpabuf_head(cui), attr->cui,
624 if (sta == NULL && attr->user_name) {
625 for (sta = hapd->sta_list; sta; sta = sta->next) {
628 identity = ieee802_1x_get_identity(sta->eapol_sm,
631 identity_len == attr->user_name_len &&
632 os_memcmp(identity, attr->user_name, identity_len)
642 static enum radius_das_res
643 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
645 struct hostapd_data *hapd = ctx;
646 struct sta_info *sta;
648 if (hostapd_das_nas_mismatch(hapd, attr))
649 return RADIUS_DAS_NAS_MISMATCH;
651 sta = hostapd_das_find_sta(hapd, attr);
653 return RADIUS_DAS_SESSION_NOT_FOUND;
655 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
657 hostapd_drv_sta_deauth(hapd, sta->addr,
658 WLAN_REASON_PREV_AUTH_NOT_VALID);
659 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
661 return RADIUS_DAS_SUCCESS;
664 #endif /* CONFIG_NO_RADIUS */
668 * hostapd_setup_bss - Per-BSS setup (initialization)
669 * @hapd: Pointer to BSS data
670 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
671 * but interface may exist
673 * This function is used to initialize all per-BSS data structures and
674 * resources. This gets called in a loop for each BSS when an interface is
675 * initialized. Most of the modules that are initialized here will be
676 * deinitialized in hostapd_cleanup().
678 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
680 struct hostapd_bss_config *conf = hapd->conf;
681 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
682 int ssid_len, set_ssid;
683 char force_ifname[IFNAMSIZ];
684 u8 if_addr[ETH_ALEN];
686 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
687 __func__, hapd, hapd->conf->iface, first);
689 #ifdef EAP_SERVER_TNC
690 if (hapd->conf->tnc && tncs_global_init() < 0) {
691 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
694 #endif /* EAP_SERVER_TNC */
697 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
698 __func__, hapd->conf->iface);
703 if (!first || first == -1) {
704 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
705 /* Allocate the next available BSSID. */
707 inc_byte_array(hapd->own_addr, ETH_ALEN);
708 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
710 /* Allocate the configured BSSID. */
711 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
713 if (hostapd_mac_comp(hapd->own_addr,
714 hapd->iface->bss[0]->own_addr) ==
716 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
717 "BSSID set to the MAC address of "
718 "the radio", hapd->conf->iface);
723 hapd->interface_added = 1;
724 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
725 hapd->conf->iface, hapd->own_addr, hapd,
726 &hapd->drv_priv, force_ifname, if_addr,
727 hapd->conf->bridge[0] ? hapd->conf->bridge :
728 NULL, first == -1)) {
729 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
730 MACSTR ")", MAC2STR(hapd->own_addr));
731 hapd->interface_added = 0;
736 if (conf->wmm_enabled < 0)
737 conf->wmm_enabled = hapd->iconf->ieee80211n;
739 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
740 hostapd_set_privacy(hapd, 0);
742 hostapd_broadcast_wep_clear(hapd);
743 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
747 * Fetch the SSID from the system and use it or,
748 * if one was specified in the config file, verify they
751 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
753 wpa_printf(MSG_ERROR, "Could not read SSID from system");
756 if (conf->ssid.ssid_set) {
758 * If SSID is specified in the config file and it differs
759 * from what is being used then force installation of the
762 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
763 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
766 * No SSID in the config file; just use the one we got
770 conf->ssid.ssid_len = ssid_len;
771 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
774 if (!hostapd_drv_none(hapd)) {
775 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
777 hapd->conf->iface, MAC2STR(hapd->own_addr),
778 wpa_ssid_txt(hapd->conf->ssid.ssid,
779 hapd->conf->ssid.ssid_len));
782 if (hostapd_setup_wpa_psk(conf)) {
783 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
787 /* Set SSID for the kernel driver (to be used in beacon and probe
788 * response frames) */
789 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
790 conf->ssid.ssid_len)) {
791 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
795 if (wpa_debug_level <= MSG_MSGDUMP)
796 conf->radius->msg_dumps = 1;
797 #ifndef CONFIG_NO_RADIUS
798 hapd->radius = radius_client_init(hapd, conf->radius);
799 if (hapd->radius == NULL) {
800 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
804 if (hapd->conf->radius_das_port) {
805 struct radius_das_conf das_conf;
806 os_memset(&das_conf, 0, sizeof(das_conf));
807 das_conf.port = hapd->conf->radius_das_port;
808 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
809 das_conf.shared_secret_len =
810 hapd->conf->radius_das_shared_secret_len;
811 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
812 das_conf.time_window = hapd->conf->radius_das_time_window;
813 das_conf.require_event_timestamp =
814 hapd->conf->radius_das_require_event_timestamp;
816 das_conf.disconnect = hostapd_das_disconnect;
817 hapd->radius_das = radius_das_init(&das_conf);
818 if (hapd->radius_das == NULL) {
819 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
824 #endif /* CONFIG_NO_RADIUS */
826 if (hostapd_acl_init(hapd)) {
827 wpa_printf(MSG_ERROR, "ACL initialization failed.");
830 if (hostapd_init_wps(hapd, conf))
833 if (authsrv_init(hapd) < 0)
836 if (ieee802_1x_init(hapd)) {
837 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
841 if ((hapd->conf->wpa || hapd->conf->osen) && hostapd_setup_wpa(hapd))
844 if (accounting_init(hapd)) {
845 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
849 if (hapd->conf->ieee802_11f &&
850 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
851 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
856 #ifdef CONFIG_INTERWORKING
857 if (gas_serv_init(hapd)) {
858 wpa_printf(MSG_ERROR, "GAS server initialization failed");
862 if (conf->qos_map_set_len &&
863 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
864 conf->qos_map_set_len)) {
865 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
868 #endif /* CONFIG_INTERWORKING */
870 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
871 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
875 if (!hapd->conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
878 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
881 if (hapd->driver && hapd->driver->set_operstate)
882 hapd->driver->set_operstate(hapd->drv_priv, 1);
888 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
890 struct hostapd_data *hapd = iface->bss[0];
892 struct hostapd_tx_queue_params *p;
894 for (i = 0; i < NUM_TX_QUEUES; i++) {
895 p = &iface->conf->tx_queue[i];
897 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
898 p->cwmax, p->burst)) {
899 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
900 "parameters for queue %d.", i);
901 /* Continue anyway */
907 static int hostapd_set_acl_list(struct hostapd_data *hapd,
908 struct mac_acl_entry *mac_acl,
909 int n_entries, u8 accept_acl)
911 struct hostapd_acl_params *acl_params;
914 acl_params = os_zalloc(sizeof(*acl_params) +
915 (n_entries * sizeof(acl_params->mac_acl[0])));
919 for (i = 0; i < n_entries; i++)
920 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
923 acl_params->acl_policy = accept_acl;
924 acl_params->num_mac_acl = n_entries;
926 err = hostapd_drv_set_acl(hapd, acl_params);
934 static void hostapd_set_acl(struct hostapd_data *hapd)
936 struct hostapd_config *conf = hapd->iconf;
940 if (hapd->iface->drv_max_acl_mac_addrs == 0)
942 if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
945 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
946 if (conf->bss[0]->num_accept_mac) {
948 err = hostapd_set_acl_list(hapd,
949 conf->bss[0]->accept_mac,
950 conf->bss[0]->num_accept_mac,
953 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
957 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
959 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
960 if (conf->bss[0]->num_deny_mac) {
962 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
963 conf->bss[0]->num_deny_mac,
966 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
970 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
976 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
978 if (!hapd->iface->interfaces ||
979 !hapd->iface->interfaces->ctrl_iface_init)
982 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
983 wpa_printf(MSG_ERROR,
984 "Failed to setup control interface for %s",
993 static int start_ctrl_iface(struct hostapd_iface *iface)
997 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1000 for (i = 0; i < iface->num_bss; i++) {
1001 struct hostapd_data *hapd = iface->bss[i];
1002 if (iface->interfaces->ctrl_iface_init(hapd)) {
1003 wpa_printf(MSG_ERROR,
1004 "Failed to setup control interface for %s",
1014 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1016 struct hostapd_iface *iface = eloop_ctx;
1018 if (!iface->wait_channel_update) {
1019 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1024 * It is possible that the existing channel list is acceptable, so try
1027 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1028 setup_interface2(iface);
1032 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1034 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1037 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1038 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1039 setup_interface2(iface);
1043 static int setup_interface(struct hostapd_iface *iface)
1045 struct hostapd_data *hapd = iface->bss[0];
1049 * It is possible that setup_interface() is called after the interface
1050 * was disabled etc., in which case driver_ap_teardown is possibly set
1051 * to 1. Clear it here so any other key/station deletion, which is not
1052 * part of a teardown flow, would also call the relevant driver
1055 iface->driver_ap_teardown = 0;
1057 if (!iface->phy[0]) {
1058 const char *phy = hostapd_drv_get_radio_name(hapd);
1060 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1061 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1066 * Make sure that all BSSes get configured with a pointer to the same
1069 for (i = 1; i < iface->num_bss; i++) {
1070 iface->bss[i]->driver = hapd->driver;
1071 iface->bss[i]->drv_priv = hapd->drv_priv;
1074 if (hostapd_validate_bssid_configuration(iface))
1078 * Initialize control interfaces early to allow external monitoring of
1079 * channel setup operations that may take considerable amount of time
1080 * especially for DFS cases.
1082 if (start_ctrl_iface(iface))
1085 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1086 char country[4], previous_country[4];
1088 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1089 if (hostapd_get_country(hapd, previous_country) < 0)
1090 previous_country[0] = '\0';
1092 os_memcpy(country, hapd->iconf->country, 3);
1094 if (hostapd_set_country(hapd, country) < 0) {
1095 wpa_printf(MSG_ERROR, "Failed to set country code");
1099 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1100 previous_country, country);
1102 if (os_strncmp(previous_country, country, 2) != 0) {
1103 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1104 iface->wait_channel_update = 1;
1105 eloop_register_timeout(5, 0,
1106 channel_list_update_timeout,
1112 return setup_interface2(iface);
1116 static int setup_interface2(struct hostapd_iface *iface)
1118 iface->wait_channel_update = 0;
1120 if (hostapd_get_hw_features(iface)) {
1121 /* Not all drivers support this yet, so continue without hw
1124 int ret = hostapd_select_hw_mode(iface);
1126 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1127 "channel. (%d)", ret);
1131 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1134 ret = hostapd_check_ht_capab(iface);
1138 wpa_printf(MSG_DEBUG, "Interface initialization will "
1139 "be completed in a callback");
1143 if (iface->conf->ieee80211h)
1144 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1146 return hostapd_setup_interface_complete(iface, 0);
1149 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1150 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1151 if (iface->interfaces && iface->interfaces->terminate_on_error)
1158 * hostapd_setup_interface_complete - Complete interface setup
1160 * This function is called when previous steps in the interface setup has been
1161 * completed. This can also start operations, e.g., DFS, that will require
1162 * additional processing before interface is ready to be enabled. Such
1163 * operations will call this function from eloop callbacks when finished.
1165 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1167 struct hostapd_data *hapd = iface->bss[0];
1174 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1175 if (iface->conf->channel) {
1178 #endif /* NEED_AP_MLME */
1180 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1181 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1182 "Frequency: %d MHz",
1183 hostapd_hw_mode_txt(iface->conf->hw_mode),
1184 iface->conf->channel, iface->freq);
1188 res = hostapd_handle_dfs(iface);
1194 #endif /* NEED_AP_MLME */
1196 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1197 hapd->iconf->channel,
1198 hapd->iconf->ieee80211n,
1199 hapd->iconf->ieee80211ac,
1200 hapd->iconf->secondary_channel,
1201 hapd->iconf->vht_oper_chwidth,
1202 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1203 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1204 wpa_printf(MSG_ERROR, "Could not set channel for "
1210 if (iface->current_mode) {
1211 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1212 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1214 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1215 HOSTAPD_LEVEL_WARNING,
1216 "Failed to prepare rates table.");
1221 if (hapd->iconf->rts_threshold > -1 &&
1222 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1223 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1228 if (hapd->iconf->fragm_threshold > -1 &&
1229 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1230 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1231 "for kernel driver");
1235 prev_addr = hapd->own_addr;
1237 for (j = 0; j < iface->num_bss; j++) {
1238 hapd = iface->bss[j];
1240 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1241 if (hostapd_setup_bss(hapd, j == 0)) {
1243 hapd = iface->bss[j];
1244 hostapd_bss_deinit_no_free(hapd);
1245 hostapd_free_hapd_data(hapd);
1249 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1250 prev_addr = hapd->own_addr;
1252 hapd = iface->bss[0];
1254 hostapd_tx_queue_params(iface);
1256 ap_list_init(iface);
1258 hostapd_set_acl(hapd);
1260 if (hostapd_driver_commit(hapd) < 0) {
1261 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1262 "configuration", __func__);
1267 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1268 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1269 * mode), the interface is up only after driver_commit, so initialize
1270 * WPS after driver_commit.
1272 for (j = 0; j < iface->num_bss; j++) {
1273 if (hostapd_init_wps_complete(iface->bss[j]))
1277 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1278 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1279 if (hapd->setup_complete_cb)
1280 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1282 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1283 iface->bss[0]->conf->iface);
1284 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1285 iface->interfaces->terminate_on_error--;
1290 wpa_printf(MSG_ERROR, "Interface initialization failed");
1291 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1292 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1293 if (iface->interfaces && iface->interfaces->terminate_on_error)
1300 * hostapd_setup_interface - Setup of an interface
1301 * @iface: Pointer to interface data.
1302 * Returns: 0 on success, -1 on failure
1304 * Initializes the driver interface, validates the configuration,
1305 * and sets driver parameters based on the configuration.
1306 * Flushes old stations, sets the channel, encryption,
1307 * beacons, and WDS links based on the configuration.
1309 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1310 * or DFS operations, this function returns 0 before such operations have been
1311 * completed. The pending operations are registered into eloop and will be
1312 * completed from eloop callbacks. Those callbacks end up calling
1313 * hostapd_setup_interface_complete() once setup has been completed.
1315 int hostapd_setup_interface(struct hostapd_iface *iface)
1319 ret = setup_interface(iface);
1321 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1322 iface->bss[0]->conf->iface);
1331 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1332 * @hapd_iface: Pointer to interface data
1333 * @conf: Pointer to per-interface configuration
1334 * @bss: Pointer to per-BSS configuration for this BSS
1335 * Returns: Pointer to allocated BSS data
1337 * This function is used to allocate per-BSS data structure. This data will be
1338 * freed after hostapd_cleanup() is called for it during interface
1341 struct hostapd_data *
1342 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1343 struct hostapd_config *conf,
1344 struct hostapd_bss_config *bss)
1346 struct hostapd_data *hapd;
1348 hapd = os_zalloc(sizeof(*hapd));
1352 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1355 hapd->iface = hapd_iface;
1356 hapd->driver = hapd->iconf->driver;
1357 hapd->ctrl_sock = -1;
1363 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1365 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1367 hostapd_bss_deinit_no_free(hapd);
1368 hostapd_cleanup(hapd);
1372 void hostapd_interface_deinit(struct hostapd_iface *iface)
1376 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1380 #ifdef CONFIG_IEEE80211N
1382 hostapd_stop_setup_timers(iface);
1383 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1384 #endif /* NEED_AP_MLME */
1385 #endif /* CONFIG_IEEE80211N */
1386 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1387 iface->wait_channel_update = 0;
1389 for (j = iface->num_bss - 1; j >= 0; j--)
1390 hostapd_bss_deinit(iface->bss[j]);
1394 void hostapd_interface_free(struct hostapd_iface *iface)
1397 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1398 for (j = 0; j < iface->num_bss; j++) {
1399 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1400 __func__, iface->bss[j]);
1401 os_free(iface->bss[j]);
1403 hostapd_cleanup_iface(iface);
1408 * hostapd_init - Allocate and initialize per-interface data
1409 * @config_file: Path to the configuration file
1410 * Returns: Pointer to the allocated interface data or %NULL on failure
1412 * This function is used to allocate main data structures for per-interface
1413 * data. The allocated data buffer will be freed by calling
1414 * hostapd_cleanup_iface().
1416 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1417 const char *config_file)
1419 struct hostapd_iface *hapd_iface = NULL;
1420 struct hostapd_config *conf = NULL;
1421 struct hostapd_data *hapd;
1424 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1425 if (hapd_iface == NULL)
1428 hapd_iface->config_fname = os_strdup(config_file);
1429 if (hapd_iface->config_fname == NULL)
1432 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1435 hapd_iface->conf = conf;
1437 hapd_iface->num_bss = conf->num_bss;
1438 hapd_iface->bss = os_calloc(conf->num_bss,
1439 sizeof(struct hostapd_data *));
1440 if (hapd_iface->bss == NULL)
1443 for (i = 0; i < conf->num_bss; i++) {
1444 hapd = hapd_iface->bss[i] =
1445 hostapd_alloc_bss_data(hapd_iface, conf,
1449 hapd->msg_ctx = hapd;
1455 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1458 hostapd_config_free(conf);
1460 os_free(hapd_iface->config_fname);
1461 os_free(hapd_iface->bss);
1462 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1463 __func__, hapd_iface);
1464 os_free(hapd_iface);
1470 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1474 for (i = 0; i < interfaces->count; i++) {
1475 struct hostapd_iface *iface = interfaces->iface[i];
1476 for (j = 0; j < iface->num_bss; j++) {
1477 struct hostapd_data *hapd = iface->bss[j];
1478 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1488 * hostapd_interface_init_bss - Read configuration file and init BSS data
1490 * This function is used to parse configuration file for a BSS. This BSS is
1491 * added to an existing interface sharing the same radio (if any) or a new
1492 * interface is created if this is the first interface on a radio. This
1493 * allocate memory for the BSS. No actual driver operations are started.
1495 * This is similar to hostapd_interface_init(), but for a case where the
1496 * configuration is used to add a single BSS instead of all BSSes for a radio.
1498 struct hostapd_iface *
1499 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1500 const char *config_fname, int debug)
1502 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1503 struct hostapd_data *hapd;
1510 for (i = 0; i < interfaces->count; i++) {
1511 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1512 iface = interfaces->iface[i];
1517 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1518 config_fname, phy, iface ? "" : " --> new PHY");
1520 struct hostapd_config *conf;
1521 struct hostapd_bss_config **tmp_conf;
1522 struct hostapd_data **tmp_bss;
1523 struct hostapd_bss_config *bss;
1526 /* Add new BSS to existing iface */
1527 conf = interfaces->config_read_cb(config_fname);
1530 if (conf->num_bss > 1) {
1531 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1532 hostapd_config_free(conf);
1536 ifname = conf->bss[0]->iface;
1537 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1538 wpa_printf(MSG_ERROR,
1539 "Interface name %s already in use", ifname);
1540 hostapd_config_free(conf);
1544 tmp_conf = os_realloc_array(
1545 iface->conf->bss, iface->conf->num_bss + 1,
1546 sizeof(struct hostapd_bss_config *));
1547 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1548 sizeof(struct hostapd_data *));
1550 iface->bss = tmp_bss;
1552 iface->conf->bss = tmp_conf;
1553 iface->conf->last_bss = tmp_conf[0];
1555 if (tmp_bss == NULL || tmp_conf == NULL) {
1556 hostapd_config_free(conf);
1559 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1560 iface->conf->num_bss++;
1562 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1564 iface->conf->num_bss--;
1565 hostapd_config_free(conf);
1568 iface->conf->last_bss = bss;
1569 iface->bss[iface->num_bss] = hapd;
1570 hapd->msg_ctx = hapd;
1572 bss_idx = iface->num_bss++;
1574 conf->bss[0] = NULL;
1575 hostapd_config_free(conf);
1577 /* Add a new iface with the first BSS */
1578 new_iface = iface = hostapd_init(interfaces, config_fname);
1581 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1582 iface->interfaces = interfaces;
1586 for (k = 0; k < debug; k++) {
1587 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1588 iface->bss[bss_idx]->conf->logger_stdout_level--;
1591 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1592 !hostapd_drv_none(iface->bss[bss_idx])) {
1593 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1596 hostapd_interface_deinit_free(new_iface);
1604 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1606 const struct wpa_driver_ops *driver;
1609 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1612 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1613 __func__, (unsigned int) iface->num_bss,
1614 (unsigned int) iface->conf->num_bss);
1615 driver = iface->bss[0]->driver;
1616 drv_priv = iface->bss[0]->drv_priv;
1617 hostapd_interface_deinit(iface);
1618 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1619 __func__, driver, drv_priv);
1620 if (driver && driver->hapd_deinit && drv_priv)
1621 driver->hapd_deinit(drv_priv);
1622 hostapd_interface_free(iface);
1626 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1628 struct hostapd_iface *hapd_iface)
1632 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1633 __func__, driver, drv_priv);
1634 if (driver && driver->hapd_deinit && drv_priv) {
1635 driver->hapd_deinit(drv_priv);
1636 for (j = 0; j < hapd_iface->num_bss; j++) {
1637 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1639 hapd_iface->bss[j]->drv_priv);
1640 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1641 hapd_iface->bss[j]->drv_priv = NULL;
1647 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1651 if (hapd_iface->bss[0]->drv_priv != NULL) {
1652 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1653 hapd_iface->conf->bss[0]->iface);
1657 wpa_printf(MSG_DEBUG, "Enable interface %s",
1658 hapd_iface->conf->bss[0]->iface);
1660 for (j = 0; j < hapd_iface->num_bss; j++)
1661 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1662 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1663 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1667 if (hapd_iface->interfaces == NULL ||
1668 hapd_iface->interfaces->driver_init == NULL ||
1669 hapd_iface->interfaces->driver_init(hapd_iface))
1672 if (hostapd_setup_interface(hapd_iface)) {
1673 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1674 hapd_iface->bss[0]->drv_priv,
1683 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1687 wpa_printf(MSG_DEBUG, "Reload interface %s",
1688 hapd_iface->conf->bss[0]->iface);
1689 for (j = 0; j < hapd_iface->num_bss; j++)
1690 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1691 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1692 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1695 hostapd_clear_old(hapd_iface);
1696 for (j = 0; j < hapd_iface->num_bss; j++)
1697 hostapd_reload_bss(hapd_iface->bss[j]);
1703 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1706 const struct wpa_driver_ops *driver;
1709 if (hapd_iface == NULL)
1712 if (hapd_iface->bss[0]->drv_priv == NULL) {
1713 wpa_printf(MSG_INFO, "Interface %s already disabled",
1714 hapd_iface->conf->bss[0]->iface);
1718 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1719 driver = hapd_iface->bss[0]->driver;
1720 drv_priv = hapd_iface->bss[0]->drv_priv;
1722 hapd_iface->driver_ap_teardown =
1723 !!(hapd_iface->drv_flags &
1724 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1726 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1727 for (j = 0; j < hapd_iface->num_bss; j++) {
1728 struct hostapd_data *hapd = hapd_iface->bss[j];
1729 hostapd_bss_deinit_no_free(hapd);
1730 hostapd_free_hapd_data(hapd);
1733 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
1735 /* From hostapd_cleanup_iface: These were initialized in
1736 * hostapd_setup_interface and hostapd_setup_interface_complete
1738 hostapd_cleanup_iface_partial(hapd_iface);
1740 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1741 hapd_iface->bss[0]->conf->iface);
1742 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1747 static struct hostapd_iface *
1748 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1750 struct hostapd_iface **iface, *hapd_iface;
1752 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1753 sizeof(struct hostapd_iface *));
1756 interfaces->iface = iface;
1757 hapd_iface = interfaces->iface[interfaces->count] =
1758 os_zalloc(sizeof(*hapd_iface));
1759 if (hapd_iface == NULL) {
1760 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1761 "the interface", __func__);
1764 interfaces->count++;
1765 hapd_iface->interfaces = interfaces;
1771 static struct hostapd_config *
1772 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1773 const char *ctrl_iface)
1775 struct hostapd_bss_config *bss;
1776 struct hostapd_config *conf;
1778 /* Allocates memory for bss and conf */
1779 conf = hostapd_config_defaults();
1781 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1782 "configuration", __func__);
1786 conf->driver = wpa_drivers[0];
1787 if (conf->driver == NULL) {
1788 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1789 hostapd_config_free(conf);
1793 bss = conf->last_bss = conf->bss[0];
1795 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1796 bss->ctrl_interface = os_strdup(ctrl_iface);
1797 if (bss->ctrl_interface == NULL) {
1798 hostapd_config_free(conf);
1802 /* Reading configuration file skipped, will be done in SET!
1803 * From reading the configuration till the end has to be done in
1810 static struct hostapd_iface * hostapd_data_alloc(
1811 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1814 struct hostapd_iface *hapd_iface =
1815 interfaces->iface[interfaces->count - 1];
1816 struct hostapd_data *hapd;
1818 hapd_iface->conf = conf;
1819 hapd_iface->num_bss = conf->num_bss;
1821 hapd_iface->bss = os_zalloc(conf->num_bss *
1822 sizeof(struct hostapd_data *));
1823 if (hapd_iface->bss == NULL)
1826 for (i = 0; i < conf->num_bss; i++) {
1827 hapd = hapd_iface->bss[i] =
1828 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1831 hapd->msg_ctx = hapd;
1834 hapd_iface->interfaces = interfaces;
1840 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1842 struct hostapd_config *conf = NULL;
1843 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1844 struct hostapd_data *hapd;
1847 const char *conf_file = NULL, *phy_name = NULL;
1849 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1851 phy_name = buf + 11;
1852 pos = os_strchr(phy_name, ':');
1857 if (!os_strlen(conf_file))
1860 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1864 for (j = 0; j < interfaces->count; j++) {
1865 if (interfaces->iface[j] == hapd_iface)
1868 if (j == interfaces->count) {
1869 struct hostapd_iface **tmp;
1870 tmp = os_realloc_array(interfaces->iface,
1871 interfaces->count + 1,
1872 sizeof(struct hostapd_iface *));
1874 hostapd_interface_deinit_free(hapd_iface);
1877 interfaces->iface = tmp;
1878 interfaces->iface[interfaces->count++] = hapd_iface;
1879 new_iface = hapd_iface;
1883 if (interfaces->driver_init(hapd_iface) ||
1884 hostapd_setup_interface(hapd_iface)) {
1885 interfaces->count--;
1889 /* Assign new BSS with bss[0]'s driver info */
1890 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1891 hapd->driver = hapd_iface->bss[0]->driver;
1892 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1893 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1896 if (start_ctrl_iface_bss(hapd) < 0 ||
1897 (hapd_iface->state == HAPD_IFACE_ENABLED &&
1898 hostapd_setup_bss(hapd, -1))) {
1899 hostapd_cleanup(hapd);
1900 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
1901 hapd_iface->conf->num_bss--;
1902 hapd_iface->num_bss--;
1903 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1904 __func__, hapd, hapd->conf->iface);
1912 ptr = os_strchr(buf, ' ');
1917 if (os_strncmp(ptr, "config=", 7) == 0)
1918 conf_file = ptr + 7;
1920 for (i = 0; i < interfaces->count; i++) {
1921 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1923 wpa_printf(MSG_INFO, "Cannot add interface - it "
1929 hapd_iface = hostapd_iface_alloc(interfaces);
1930 if (hapd_iface == NULL) {
1931 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1932 "for interface", __func__);
1936 if (conf_file && interfaces->config_read_cb) {
1937 conf = interfaces->config_read_cb(conf_file);
1938 if (conf && conf->bss)
1939 os_strlcpy(conf->bss[0]->iface, buf,
1940 sizeof(conf->bss[0]->iface));
1942 conf = hostapd_config_alloc(interfaces, buf, ptr);
1943 if (conf == NULL || conf->bss == NULL) {
1944 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1945 "for configuration", __func__);
1949 hapd_iface = hostapd_data_alloc(interfaces, conf);
1950 if (hapd_iface == NULL) {
1951 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1952 "for hostapd", __func__);
1956 if (start_ctrl_iface(hapd_iface) < 0)
1959 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
1965 hostapd_config_free(conf);
1967 if (hapd_iface->bss) {
1968 for (i = 0; i < hapd_iface->num_bss; i++) {
1969 hapd = hapd_iface->bss[i];
1972 if (hapd_iface->interfaces &&
1973 hapd_iface->interfaces->ctrl_iface_deinit)
1974 hapd_iface->interfaces->
1975 ctrl_iface_deinit(hapd);
1976 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1977 __func__, hapd_iface->bss[i],
1980 hapd_iface->bss[i] = NULL;
1982 os_free(hapd_iface->bss);
1984 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1985 __func__, hapd_iface);
1986 os_free(hapd_iface);
1992 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
1996 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
1998 /* Remove hostapd_data only if it has already been initialized */
1999 if (idx < iface->num_bss) {
2000 struct hostapd_data *hapd = iface->bss[idx];
2002 hostapd_bss_deinit(hapd);
2003 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2004 __func__, hapd, hapd->conf->iface);
2005 hostapd_config_free_bss(hapd->conf);
2010 for (i = idx; i < iface->num_bss; i++)
2011 iface->bss[i] = iface->bss[i + 1];
2013 hostapd_config_free_bss(iface->conf->bss[idx]);
2014 iface->conf->bss[idx] = NULL;
2017 iface->conf->num_bss--;
2018 for (i = idx; i < iface->conf->num_bss; i++)
2019 iface->conf->bss[i] = iface->conf->bss[i + 1];
2025 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2027 struct hostapd_iface *hapd_iface;
2030 for (i = 0; i < interfaces->count; i++) {
2031 hapd_iface = interfaces->iface[i];
2032 if (hapd_iface == NULL)
2034 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2035 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2036 hapd_iface->driver_ap_teardown =
2037 !!(hapd_iface->drv_flags &
2038 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2040 hostapd_interface_deinit_free(hapd_iface);
2042 while (k < (interfaces->count - 1)) {
2043 interfaces->iface[k] =
2044 interfaces->iface[k + 1];
2047 interfaces->count--;
2051 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2052 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2053 hapd_iface->driver_ap_teardown =
2054 !(hapd_iface->drv_flags &
2055 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2056 return hostapd_remove_bss(hapd_iface, j);
2065 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2066 * @hapd: Pointer to BSS data
2067 * @sta: Pointer to the associated STA data
2068 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2070 * This function will be called whenever a station associates with the AP. It
2071 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2072 * from drv_callbacks.c based on driver events for drivers that take care of
2073 * management frames (IEEE 802.11 authentication and association) internally.
2075 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2078 if (hapd->tkip_countermeasures) {
2079 hostapd_drv_sta_deauth(hapd, sta->addr,
2080 WLAN_REASON_MICHAEL_MIC_FAILURE);
2084 hostapd_prune_associations(hapd, sta->addr);
2086 /* IEEE 802.11F (IAPP) */
2087 if (hapd->conf->ieee802_11f)
2088 iapp_new_station(hapd->iapp, sta);
2091 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2092 sta->no_p2p_set = 1;
2093 hapd->num_sta_no_p2p++;
2094 if (hapd->num_sta_no_p2p == 1)
2095 hostapd_p2p_non_p2p_sta_connected(hapd);
2097 #endif /* CONFIG_P2P */
2099 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2100 * IEEE 802.1X/WPA code will start accounting after the station has
2101 * been authorized. */
2102 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2103 ap_sta_set_authorized(hapd, sta, 1);
2104 os_get_reltime(&sta->connected_time);
2105 accounting_sta_start(hapd, sta);
2108 /* Start IEEE 802.1X authentication process for new stations */
2109 ieee802_1x_new_station(hapd, sta);
2111 if (sta->auth_alg != WLAN_AUTH_FT &&
2112 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2113 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2115 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2117 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2118 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2119 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2120 __func__, MAC2STR(sta->addr),
2121 hapd->conf->ap_max_inactivity);
2122 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2123 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2124 ap_handle_timer, hapd, sta);
2129 const char * hostapd_state_text(enum hostapd_iface_state s)
2132 case HAPD_IFACE_UNINITIALIZED:
2133 return "UNINITIALIZED";
2134 case HAPD_IFACE_DISABLED:
2136 case HAPD_IFACE_COUNTRY_UPDATE:
2137 return "COUNTRY_UPDATE";
2138 case HAPD_IFACE_ACS:
2140 case HAPD_IFACE_HT_SCAN:
2142 case HAPD_IFACE_DFS:
2144 case HAPD_IFACE_ENABLED:
2152 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2154 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2155 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2156 hostapd_state_text(s));
2163 static void free_beacon_data(struct beacon_data *beacon)
2165 os_free(beacon->head);
2166 beacon->head = NULL;
2167 os_free(beacon->tail);
2168 beacon->tail = NULL;
2169 os_free(beacon->probe_resp);
2170 beacon->probe_resp = NULL;
2171 os_free(beacon->beacon_ies);
2172 beacon->beacon_ies = NULL;
2173 os_free(beacon->proberesp_ies);
2174 beacon->proberesp_ies = NULL;
2175 os_free(beacon->assocresp_ies);
2176 beacon->assocresp_ies = NULL;
2180 static int hostapd_build_beacon_data(struct hostapd_iface *iface,
2181 struct beacon_data *beacon)
2183 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2184 struct wpa_driver_ap_params params;
2186 struct hostapd_data *hapd = iface->bss[0];
2188 os_memset(beacon, 0, sizeof(*beacon));
2189 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2193 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2197 goto free_ap_params;
2200 beacon->head = os_malloc(params.head_len);
2202 goto free_ap_extra_ies;
2204 os_memcpy(beacon->head, params.head, params.head_len);
2205 beacon->head_len = params.head_len;
2207 beacon->tail = os_malloc(params.tail_len);
2211 os_memcpy(beacon->tail, params.tail, params.tail_len);
2212 beacon->tail_len = params.tail_len;
2214 if (params.proberesp != NULL) {
2215 beacon->probe_resp = os_malloc(params.proberesp_len);
2216 if (!beacon->probe_resp)
2219 os_memcpy(beacon->probe_resp, params.proberesp,
2220 params.proberesp_len);
2221 beacon->probe_resp_len = params.proberesp_len;
2224 /* copy the extra ies */
2226 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2227 if (!beacon->beacon_ies)
2230 os_memcpy(beacon->beacon_ies,
2231 beacon_extra->buf, wpabuf_len(beacon_extra));
2232 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2235 if (proberesp_extra) {
2236 beacon->proberesp_ies =
2237 os_malloc(wpabuf_len(proberesp_extra));
2238 if (!beacon->proberesp_ies)
2241 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2242 wpabuf_len(proberesp_extra));
2243 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2246 if (assocresp_extra) {
2247 beacon->assocresp_ies =
2248 os_malloc(wpabuf_len(assocresp_extra));
2249 if (!beacon->assocresp_ies)
2252 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2253 wpabuf_len(assocresp_extra));
2254 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2259 /* if the function fails, the caller should not free beacon data */
2261 free_beacon_data(beacon);
2264 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2267 ieee802_11_free_ap_params(¶ms);
2273 * TODO: This flow currently supports only changing frequency within the
2274 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2275 * width) are not supported.
2277 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2278 struct hostapd_config *conf,
2279 struct hostapd_freq_params *params,
2280 struct hostapd_freq_params *old_params)
2284 if (!params->channel) {
2285 /* check if the new channel is supported by hw */
2286 channel = hostapd_hw_get_channel(hapd, params->freq);
2290 channel = params->channel;
2293 /* if a pointer to old_params is provided we save previous state */
2295 old_params->channel = conf->channel;
2296 old_params->ht_enabled = conf->ieee80211n;
2297 old_params->sec_channel_offset = conf->secondary_channel;
2300 conf->channel = channel;
2301 conf->ieee80211n = params->ht_enabled;
2302 conf->secondary_channel = params->sec_channel_offset;
2304 /* TODO: maybe call here hostapd_config_check here? */
2310 static int hostapd_fill_csa_settings(struct hostapd_iface *iface,
2311 struct csa_settings *settings)
2313 struct hostapd_freq_params old_freq;
2316 os_memset(&old_freq, 0, sizeof(old_freq));
2317 if (!iface || !iface->freq || iface->csa_in_progress)
2320 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2321 &settings->freq_params,
2326 ret = hostapd_build_beacon_data(iface, &settings->beacon_after);
2328 /* change back the configuration */
2329 hostapd_change_config_freq(iface->bss[0], iface->conf,
2335 /* set channel switch parameters for csa ie */
2336 iface->cs_freq_params = settings->freq_params;
2337 iface->cs_count = settings->cs_count;
2338 iface->cs_block_tx = settings->block_tx;
2340 ret = hostapd_build_beacon_data(iface, &settings->beacon_csa);
2342 free_beacon_data(&settings->beacon_after);
2346 settings->counter_offset_beacon = iface->cs_c_off_beacon;
2347 settings->counter_offset_presp = iface->cs_c_off_proberesp;
2353 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2355 os_memset(&hapd->iface->cs_freq_params, 0,
2356 sizeof(hapd->iface->cs_freq_params));
2357 hapd->iface->cs_count = 0;
2358 hapd->iface->cs_block_tx = 0;
2359 hapd->iface->cs_c_off_beacon = 0;
2360 hapd->iface->cs_c_off_proberesp = 0;
2361 hapd->iface->csa_in_progress = 0;
2365 int hostapd_switch_channel(struct hostapd_data *hapd,
2366 struct csa_settings *settings)
2369 ret = hostapd_fill_csa_settings(hapd->iface, settings);
2373 ret = hostapd_drv_switch_channel(hapd, settings);
2374 free_beacon_data(&settings->beacon_csa);
2375 free_beacon_data(&settings->beacon_after);
2378 /* if we failed, clean cs parameters */
2379 hostapd_cleanup_cs_params(hapd);
2383 hapd->iface->csa_in_progress = 1;
2387 #endif /* NEED_AP_MLME */