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"
20 #include "accounting.h"
24 #include "ieee802_1x.h"
25 #include "ieee802_11_auth.h"
26 #include "vlan_init.h"
28 #include "wps_hostapd.h"
29 #include "hw_features.h"
30 #include "wpa_auth_glue.h"
31 #include "ap_drv_ops.h"
32 #include "ap_config.h"
33 #include "p2p_hostapd.h"
38 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
39 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
40 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
41 static int setup_interface2(struct hostapd_iface *iface);
42 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
45 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
46 int (*cb)(struct hostapd_iface *iface,
47 void *ctx), void *ctx)
52 for (i = 0; i < interfaces->count; i++) {
53 ret = cb(interfaces->iface[i], ctx);
62 static void hostapd_reload_bss(struct hostapd_data *hapd)
64 struct hostapd_ssid *ssid;
66 #ifndef CONFIG_NO_RADIUS
67 radius_client_reconfig(hapd->radius, hapd->conf->radius);
68 #endif /* CONFIG_NO_RADIUS */
70 ssid = &hapd->conf->ssid;
71 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
72 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
74 * Force PSK to be derived again since SSID or passphrase may
77 os_free(ssid->wpa_psk);
80 if (hostapd_setup_wpa_psk(hapd->conf)) {
81 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
82 "after reloading configuration");
85 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
86 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
88 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
90 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
91 hostapd_setup_wpa(hapd);
93 wpa_init_keys(hapd->wpa_auth);
94 } else if (hapd->conf->wpa) {
97 hostapd_reconfig_wpa(hapd);
98 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
99 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
100 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
101 "the kernel driver.");
102 } else if (hapd->wpa_auth) {
103 wpa_deinit(hapd->wpa_auth);
104 hapd->wpa_auth = NULL;
105 hostapd_set_privacy(hapd, 0);
106 hostapd_setup_encryption(hapd->conf->iface, hapd);
107 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
110 ieee802_11_set_beacon(hapd);
111 hostapd_update_wps(hapd);
113 if (hapd->conf->ssid.ssid_set &&
114 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
115 hapd->conf->ssid.ssid_len)) {
116 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
117 /* try to continue */
119 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
123 static void hostapd_clear_old(struct hostapd_iface *iface)
128 * Deauthenticate all stations since the new configuration may not
129 * allow them to use the BSS anymore.
131 for (j = 0; j < iface->num_bss; j++) {
132 hostapd_flush_old_stations(iface->bss[j],
133 WLAN_REASON_PREV_AUTH_NOT_VALID);
134 hostapd_broadcast_wep_clear(iface->bss[j]);
136 #ifndef CONFIG_NO_RADIUS
137 /* TODO: update dynamic data based on changed configuration
138 * items (e.g., open/close sockets, etc.) */
139 radius_client_flush(iface->bss[j]->radius, 0);
140 #endif /* CONFIG_NO_RADIUS */
145 int hostapd_reload_config(struct hostapd_iface *iface)
147 struct hostapd_data *hapd = iface->bss[0];
148 struct hostapd_config *newconf, *oldconf;
151 if (iface->config_fname == NULL) {
152 /* Only in-memory config in use - assume it has been updated */
153 hostapd_clear_old(iface);
154 for (j = 0; j < iface->num_bss; j++)
155 hostapd_reload_bss(iface->bss[j]);
159 if (iface->interfaces == NULL ||
160 iface->interfaces->config_read_cb == NULL)
162 newconf = iface->interfaces->config_read_cb(iface->config_fname);
166 hostapd_clear_old(iface);
168 oldconf = hapd->iconf;
169 iface->conf = newconf;
171 for (j = 0; j < iface->num_bss; j++) {
172 hapd = iface->bss[j];
173 hapd->iconf = newconf;
174 hapd->conf = newconf->bss[j];
175 hostapd_reload_bss(hapd);
178 hostapd_config_free(oldconf);
185 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
190 for (i = 0; i < NUM_WEP_KEYS; i++) {
191 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
192 0, NULL, 0, NULL, 0)) {
193 wpa_printf(MSG_DEBUG, "Failed to clear default "
194 "encryption keys (ifname=%s keyidx=%d)",
198 #ifdef CONFIG_IEEE80211W
199 if (hapd->conf->ieee80211w) {
200 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
201 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
204 wpa_printf(MSG_DEBUG, "Failed to clear "
205 "default mgmt encryption keys "
206 "(ifname=%s keyidx=%d)", ifname, i);
210 #endif /* CONFIG_IEEE80211W */
214 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
216 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
221 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
224 struct hostapd_ssid *ssid = &hapd->conf->ssid;
227 if (ssid->wep.default_len &&
228 hostapd_drv_set_key(hapd->conf->iface,
229 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
230 1, NULL, 0, ssid->wep.key[idx],
231 ssid->wep.len[idx])) {
232 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
240 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
242 if (!hapd->started) {
243 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
244 __func__, hapd->conf->iface);
249 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
250 iapp_deinit(hapd->iapp);
252 accounting_deinit(hapd);
253 hostapd_deinit_wpa(hapd);
255 hostapd_acl_deinit(hapd);
256 #ifndef CONFIG_NO_RADIUS
257 radius_client_deinit(hapd->radius);
259 radius_das_deinit(hapd->radius_das);
260 hapd->radius_das = NULL;
261 #endif /* CONFIG_NO_RADIUS */
263 hostapd_deinit_wps(hapd);
265 authsrv_deinit(hapd);
267 if (hapd->interface_added &&
268 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
269 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
273 os_free(hapd->probereq_cb);
274 hapd->probereq_cb = NULL;
277 wpabuf_free(hapd->p2p_beacon_ie);
278 hapd->p2p_beacon_ie = NULL;
279 wpabuf_free(hapd->p2p_probe_resp_ie);
280 hapd->p2p_probe_resp_ie = NULL;
281 #endif /* CONFIG_P2P */
283 wpabuf_free(hapd->time_adv);
285 #ifdef CONFIG_INTERWORKING
286 gas_serv_deinit(hapd);
287 #endif /* CONFIG_INTERWORKING */
290 os_free(hapd->tmp_eap_user.identity);
291 os_free(hapd->tmp_eap_user.password);
292 #endif /* CONFIG_SQLITE */
297 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
298 * @hapd: Pointer to BSS data
300 * This function is used to free all per-BSS data structures and resources.
301 * Most of the modules that are initialized in hostapd_setup_bss() are
302 * deinitialized here.
304 static void hostapd_cleanup(struct hostapd_data *hapd)
306 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
308 if (hapd->iface->interfaces &&
309 hapd->iface->interfaces->ctrl_iface_deinit)
310 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
311 hostapd_free_hapd_data(hapd);
315 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
317 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
318 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
319 iface->hw_features = NULL;
320 os_free(iface->current_rates);
321 iface->current_rates = NULL;
322 os_free(iface->basic_rates);
323 iface->basic_rates = NULL;
324 ap_list_deinit(iface);
329 * hostapd_cleanup_iface - Complete per-interface cleanup
330 * @iface: Pointer to interface data
332 * This function is called after per-BSS data structures are deinitialized
333 * with hostapd_cleanup().
335 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
337 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
338 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
340 hostapd_cleanup_iface_partial(iface);
341 hostapd_config_free(iface->conf);
344 os_free(iface->config_fname);
346 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
351 static void hostapd_clear_wep(struct hostapd_data *hapd)
353 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
354 hostapd_set_privacy(hapd, 0);
355 hostapd_broadcast_wep_clear(hapd);
360 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
364 hostapd_broadcast_wep_set(hapd);
366 if (hapd->conf->ssid.wep.default_len) {
367 hostapd_set_privacy(hapd, 1);
372 * When IEEE 802.1X is not enabled, the driver may need to know how to
373 * set authentication algorithms for static WEP.
375 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
377 for (i = 0; i < 4; i++) {
378 if (hapd->conf->ssid.wep.key[i] &&
379 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
380 i == hapd->conf->ssid.wep.idx, NULL, 0,
381 hapd->conf->ssid.wep.key[i],
382 hapd->conf->ssid.wep.len[i])) {
383 wpa_printf(MSG_WARNING, "Could not set WEP "
387 if (hapd->conf->ssid.wep.key[i] &&
388 i == hapd->conf->ssid.wep.idx)
389 hostapd_set_privacy(hapd, 1);
396 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
401 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
404 if (!hapd->iface->driver_ap_teardown) {
405 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
406 "Flushing old station entries");
408 if (hostapd_flush(hapd)) {
409 wpa_msg(hapd->msg_ctx, MSG_WARNING,
410 "Could not connect to kernel driver");
414 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
415 os_memset(addr, 0xff, ETH_ALEN);
416 hostapd_drv_sta_deauth(hapd, addr, reason);
417 hostapd_free_stas(hapd);
424 * hostapd_validate_bssid_configuration - Validate BSSID configuration
425 * @iface: Pointer to interface data
426 * Returns: 0 on success, -1 on failure
428 * This function is used to validate that the configured BSSIDs are valid.
430 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
432 u8 mask[ETH_ALEN] = { 0 };
433 struct hostapd_data *hapd = iface->bss[0];
434 unsigned int i = iface->conf->num_bss, bits = 0, j;
437 if (hostapd_drv_none(hapd))
440 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
442 /* Determine the bits necessary to cover the number of BSSIDs. */
443 for (i--; i; i >>= 1)
446 /* Determine the bits necessary to any configured BSSIDs,
447 if they are higher than the number of BSSIDs. */
448 for (j = 0; j < iface->conf->num_bss; j++) {
449 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
455 for (i = 0; i < ETH_ALEN; i++) {
457 iface->conf->bss[j]->bssid[i] ^
465 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
471 while (mask[i] != 0) {
481 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
486 os_memset(mask, 0xff, ETH_ALEN);
488 for (i = 5; i > 5 - j; i--)
495 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
496 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
501 for (i = 0; i < ETH_ALEN; i++) {
502 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
503 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
504 " for start address " MACSTR ".",
505 MAC2STR(mask), MAC2STR(hapd->own_addr));
506 wpa_printf(MSG_ERROR, "Start address must be the "
507 "first address in the block (i.e., addr "
508 "AND mask == addr).");
517 static int mac_in_conf(struct hostapd_config *conf, const void *a)
521 for (i = 0; i < conf->num_bss; i++) {
522 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
531 #ifndef CONFIG_NO_RADIUS
533 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
534 struct radius_das_attrs *attr)
536 if (attr->nas_identifier &&
537 (!hapd->conf->nas_identifier ||
538 os_strlen(hapd->conf->nas_identifier) !=
539 attr->nas_identifier_len ||
540 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
541 attr->nas_identifier_len) != 0)) {
542 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
546 if (attr->nas_ip_addr &&
547 (hapd->conf->own_ip_addr.af != AF_INET ||
548 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
550 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
555 if (attr->nas_ipv6_addr &&
556 (hapd->conf->own_ip_addr.af != AF_INET6 ||
557 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
559 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
562 #endif /* CONFIG_IPV6 */
568 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
569 struct radius_das_attrs *attr)
571 struct sta_info *sta = NULL;
575 sta = ap_get_sta(hapd, attr->sta_addr);
577 if (sta == NULL && attr->acct_session_id &&
578 attr->acct_session_id_len == 17) {
579 for (sta = hapd->sta_list; sta; sta = sta->next) {
580 os_snprintf(buf, sizeof(buf), "%08X-%08X",
581 sta->acct_session_id_hi,
582 sta->acct_session_id_lo);
583 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
588 if (sta == NULL && attr->cui) {
589 for (sta = hapd->sta_list; sta; sta = sta->next) {
591 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
592 if (cui && wpabuf_len(cui) == attr->cui_len &&
593 os_memcmp(wpabuf_head(cui), attr->cui,
599 if (sta == NULL && attr->user_name) {
600 for (sta = hapd->sta_list; sta; sta = sta->next) {
603 identity = ieee802_1x_get_identity(sta->eapol_sm,
606 identity_len == attr->user_name_len &&
607 os_memcmp(identity, attr->user_name, identity_len)
617 static enum radius_das_res
618 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
620 struct hostapd_data *hapd = ctx;
621 struct sta_info *sta;
623 if (hostapd_das_nas_mismatch(hapd, attr))
624 return RADIUS_DAS_NAS_MISMATCH;
626 sta = hostapd_das_find_sta(hapd, attr);
628 return RADIUS_DAS_SESSION_NOT_FOUND;
630 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
632 hostapd_drv_sta_deauth(hapd, sta->addr,
633 WLAN_REASON_PREV_AUTH_NOT_VALID);
634 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
636 return RADIUS_DAS_SUCCESS;
639 #endif /* CONFIG_NO_RADIUS */
643 * hostapd_setup_bss - Per-BSS setup (initialization)
644 * @hapd: Pointer to BSS data
645 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
646 * but interface may exist
648 * This function is used to initialize all per-BSS data structures and
649 * resources. This gets called in a loop for each BSS when an interface is
650 * initialized. Most of the modules that are initialized here will be
651 * deinitialized in hostapd_cleanup().
653 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
655 struct hostapd_bss_config *conf = hapd->conf;
656 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
657 int ssid_len, set_ssid;
658 char force_ifname[IFNAMSIZ];
659 u8 if_addr[ETH_ALEN];
661 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
662 __func__, hapd, hapd->conf->iface, first);
665 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
666 __func__, hapd->conf->iface);
671 if (!first || first == -1) {
672 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
673 /* Allocate the next available BSSID. */
675 inc_byte_array(hapd->own_addr, ETH_ALEN);
676 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
678 /* Allocate the configured BSSID. */
679 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
681 if (hostapd_mac_comp(hapd->own_addr,
682 hapd->iface->bss[0]->own_addr) ==
684 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
685 "BSSID set to the MAC address of "
686 "the radio", hapd->conf->iface);
691 hapd->interface_added = 1;
692 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
693 hapd->conf->iface, hapd->own_addr, hapd,
694 &hapd->drv_priv, force_ifname, if_addr,
695 hapd->conf->bridge[0] ? hapd->conf->bridge :
696 NULL, first == -1)) {
697 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
698 MACSTR ")", MAC2STR(hapd->own_addr));
699 hapd->interface_added = 0;
704 if (conf->wmm_enabled < 0)
705 conf->wmm_enabled = hapd->iconf->ieee80211n;
707 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
708 hostapd_set_privacy(hapd, 0);
710 hostapd_broadcast_wep_clear(hapd);
711 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
715 * Fetch the SSID from the system and use it or,
716 * if one was specified in the config file, verify they
719 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
721 wpa_printf(MSG_ERROR, "Could not read SSID from system");
724 if (conf->ssid.ssid_set) {
726 * If SSID is specified in the config file and it differs
727 * from what is being used then force installation of the
730 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
731 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
734 * No SSID in the config file; just use the one we got
738 conf->ssid.ssid_len = ssid_len;
739 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
742 if (!hostapd_drv_none(hapd)) {
743 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
745 hapd->conf->iface, MAC2STR(hapd->own_addr),
746 wpa_ssid_txt(hapd->conf->ssid.ssid,
747 hapd->conf->ssid.ssid_len));
750 if (hostapd_setup_wpa_psk(conf)) {
751 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
755 /* Set SSID for the kernel driver (to be used in beacon and probe
756 * response frames) */
757 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
758 conf->ssid.ssid_len)) {
759 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
763 if (wpa_debug_level == MSG_MSGDUMP)
764 conf->radius->msg_dumps = 1;
765 #ifndef CONFIG_NO_RADIUS
766 hapd->radius = radius_client_init(hapd, conf->radius);
767 if (hapd->radius == NULL) {
768 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
772 if (hapd->conf->radius_das_port) {
773 struct radius_das_conf das_conf;
774 os_memset(&das_conf, 0, sizeof(das_conf));
775 das_conf.port = hapd->conf->radius_das_port;
776 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
777 das_conf.shared_secret_len =
778 hapd->conf->radius_das_shared_secret_len;
779 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
780 das_conf.time_window = hapd->conf->radius_das_time_window;
781 das_conf.require_event_timestamp =
782 hapd->conf->radius_das_require_event_timestamp;
784 das_conf.disconnect = hostapd_das_disconnect;
785 hapd->radius_das = radius_das_init(&das_conf);
786 if (hapd->radius_das == NULL) {
787 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
792 #endif /* CONFIG_NO_RADIUS */
794 if (hostapd_acl_init(hapd)) {
795 wpa_printf(MSG_ERROR, "ACL initialization failed.");
798 if (hostapd_init_wps(hapd, conf))
801 if (authsrv_init(hapd) < 0)
804 if (ieee802_1x_init(hapd)) {
805 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
809 if ((hapd->conf->wpa || hapd->conf->osen) && hostapd_setup_wpa(hapd))
812 if (accounting_init(hapd)) {
813 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
817 if (hapd->conf->ieee802_11f &&
818 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
819 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
824 #ifdef CONFIG_INTERWORKING
825 if (gas_serv_init(hapd)) {
826 wpa_printf(MSG_ERROR, "GAS server initialization failed");
830 if (conf->qos_map_set_len &&
831 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
832 conf->qos_map_set_len)) {
833 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
836 #endif /* CONFIG_INTERWORKING */
838 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
839 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
843 if (!hapd->conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
846 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
849 if (hapd->driver && hapd->driver->set_operstate)
850 hapd->driver->set_operstate(hapd->drv_priv, 1);
856 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
858 struct hostapd_data *hapd = iface->bss[0];
860 struct hostapd_tx_queue_params *p;
862 for (i = 0; i < NUM_TX_QUEUES; i++) {
863 p = &iface->conf->tx_queue[i];
865 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
866 p->cwmax, p->burst)) {
867 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
868 "parameters for queue %d.", i);
869 /* Continue anyway */
875 static int hostapd_set_acl_list(struct hostapd_data *hapd,
876 struct mac_acl_entry *mac_acl,
877 int n_entries, u8 accept_acl)
879 struct hostapd_acl_params *acl_params;
882 acl_params = os_zalloc(sizeof(*acl_params) +
883 (n_entries * sizeof(acl_params->mac_acl[0])));
887 for (i = 0; i < n_entries; i++)
888 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
891 acl_params->acl_policy = accept_acl;
892 acl_params->num_mac_acl = n_entries;
894 err = hostapd_drv_set_acl(hapd, acl_params);
902 static void hostapd_set_acl(struct hostapd_data *hapd)
904 struct hostapd_config *conf = hapd->iconf;
908 if (hapd->iface->drv_max_acl_mac_addrs == 0)
910 if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
913 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
914 if (conf->bss[0]->num_accept_mac) {
916 err = hostapd_set_acl_list(hapd,
917 conf->bss[0]->accept_mac,
918 conf->bss[0]->num_accept_mac,
921 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
925 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
927 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
928 if (conf->bss[0]->num_deny_mac) {
930 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
931 conf->bss[0]->num_deny_mac,
934 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
938 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
944 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
946 if (!hapd->iface->interfaces ||
947 !hapd->iface->interfaces->ctrl_iface_init)
950 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
951 wpa_printf(MSG_ERROR,
952 "Failed to setup control interface for %s",
961 static int start_ctrl_iface(struct hostapd_iface *iface)
965 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
968 for (i = 0; i < iface->num_bss; i++) {
969 struct hostapd_data *hapd = iface->bss[i];
970 if (iface->interfaces->ctrl_iface_init(hapd)) {
971 wpa_printf(MSG_ERROR,
972 "Failed to setup control interface for %s",
982 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
984 struct hostapd_iface *iface = eloop_ctx;
986 if (!iface->wait_channel_update) {
987 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
992 * It is possible that the existing channel list is acceptable, so try
995 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
996 setup_interface2(iface);
1000 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1002 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1005 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1006 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1007 setup_interface2(iface);
1011 static int setup_interface(struct hostapd_iface *iface)
1013 struct hostapd_data *hapd = iface->bss[0];
1017 * It is possible that setup_interface() is called after the interface
1018 * was disabled etc., in which case driver_ap_teardown is possibly set
1019 * to 1. Clear it here so any other key/station deletion, which is not
1020 * part of a teardown flow, would also call the relevant driver
1023 iface->driver_ap_teardown = 0;
1025 if (!iface->phy[0]) {
1026 const char *phy = hostapd_drv_get_radio_name(hapd);
1028 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1029 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1034 * Make sure that all BSSes get configured with a pointer to the same
1037 for (i = 1; i < iface->num_bss; i++) {
1038 iface->bss[i]->driver = hapd->driver;
1039 iface->bss[i]->drv_priv = hapd->drv_priv;
1042 if (hostapd_validate_bssid_configuration(iface))
1046 * Initialize control interfaces early to allow external monitoring of
1047 * channel setup operations that may take considerable amount of time
1048 * especially for DFS cases.
1050 if (start_ctrl_iface(iface))
1053 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1054 char country[4], previous_country[4];
1056 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1057 if (hostapd_get_country(hapd, previous_country) < 0)
1058 previous_country[0] = '\0';
1060 os_memcpy(country, hapd->iconf->country, 3);
1062 if (hostapd_set_country(hapd, country) < 0) {
1063 wpa_printf(MSG_ERROR, "Failed to set country code");
1067 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1068 previous_country, country);
1070 if (os_strncmp(previous_country, country, 2) != 0) {
1071 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1072 iface->wait_channel_update = 1;
1073 eloop_register_timeout(5, 0,
1074 channel_list_update_timeout,
1080 return setup_interface2(iface);
1084 static int setup_interface2(struct hostapd_iface *iface)
1086 iface->wait_channel_update = 0;
1088 if (hostapd_get_hw_features(iface)) {
1089 /* Not all drivers support this yet, so continue without hw
1092 int ret = hostapd_select_hw_mode(iface);
1094 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1095 "channel. (%d)", ret);
1099 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1102 ret = hostapd_check_ht_capab(iface);
1106 wpa_printf(MSG_DEBUG, "Interface initialization will "
1107 "be completed in a callback");
1111 if (iface->conf->ieee80211h)
1112 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1114 return hostapd_setup_interface_complete(iface, 0);
1119 * hostapd_setup_interface_complete - Complete interface setup
1121 * This function is called when previous steps in the interface setup has been
1122 * completed. This can also start operations, e.g., DFS, that will require
1123 * additional processing before interface is ready to be enabled. Such
1124 * operations will call this function from eloop callbacks when finished.
1126 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1128 struct hostapd_data *hapd = iface->bss[0];
1133 wpa_printf(MSG_ERROR, "Interface initialization failed");
1134 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1135 if (iface->interfaces && iface->interfaces->terminate_on_error)
1140 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1141 if (iface->conf->channel) {
1144 #endif /* NEED_AP_MLME */
1146 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1147 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1148 "Frequency: %d MHz",
1149 hostapd_hw_mode_txt(iface->conf->hw_mode),
1150 iface->conf->channel, iface->freq);
1154 res = hostapd_handle_dfs(iface);
1157 #endif /* NEED_AP_MLME */
1159 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1160 hapd->iconf->channel,
1161 hapd->iconf->ieee80211n,
1162 hapd->iconf->ieee80211ac,
1163 hapd->iconf->secondary_channel,
1164 hapd->iconf->vht_oper_chwidth,
1165 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1166 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1167 wpa_printf(MSG_ERROR, "Could not set channel for "
1173 if (iface->current_mode) {
1174 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1175 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1177 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1178 HOSTAPD_LEVEL_WARNING,
1179 "Failed to prepare rates table.");
1184 if (hapd->iconf->rts_threshold > -1 &&
1185 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1186 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1191 if (hapd->iconf->fragm_threshold > -1 &&
1192 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1193 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1194 "for kernel driver");
1198 prev_addr = hapd->own_addr;
1200 for (j = 0; j < iface->num_bss; j++) {
1201 hapd = iface->bss[j];
1203 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1204 if (hostapd_setup_bss(hapd, j == 0))
1206 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1207 prev_addr = hapd->own_addr;
1209 hapd = iface->bss[0];
1211 hostapd_tx_queue_params(iface);
1213 ap_list_init(iface);
1215 hostapd_set_acl(hapd);
1217 if (hostapd_driver_commit(hapd) < 0) {
1218 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1219 "configuration", __func__);
1224 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1225 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1226 * mode), the interface is up only after driver_commit, so initialize
1227 * WPS after driver_commit.
1229 for (j = 0; j < iface->num_bss; j++) {
1230 if (hostapd_init_wps_complete(iface->bss[j]))
1234 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1235 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1236 if (hapd->setup_complete_cb)
1237 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1239 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1240 iface->bss[0]->conf->iface);
1241 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1242 iface->interfaces->terminate_on_error--;
1249 * hostapd_setup_interface - Setup of an interface
1250 * @iface: Pointer to interface data.
1251 * Returns: 0 on success, -1 on failure
1253 * Initializes the driver interface, validates the configuration,
1254 * and sets driver parameters based on the configuration.
1255 * Flushes old stations, sets the channel, encryption,
1256 * beacons, and WDS links based on the configuration.
1258 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1259 * or DFS operations, this function returns 0 before such operations have been
1260 * completed. The pending operations are registered into eloop and will be
1261 * completed from eloop callbacks. Those callbacks end up calling
1262 * hostapd_setup_interface_complete() once setup has been completed.
1264 int hostapd_setup_interface(struct hostapd_iface *iface)
1268 ret = setup_interface(iface);
1270 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1271 iface->bss[0]->conf->iface);
1280 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1281 * @hapd_iface: Pointer to interface data
1282 * @conf: Pointer to per-interface configuration
1283 * @bss: Pointer to per-BSS configuration for this BSS
1284 * Returns: Pointer to allocated BSS data
1286 * This function is used to allocate per-BSS data structure. This data will be
1287 * freed after hostapd_cleanup() is called for it during interface
1290 struct hostapd_data *
1291 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1292 struct hostapd_config *conf,
1293 struct hostapd_bss_config *bss)
1295 struct hostapd_data *hapd;
1297 hapd = os_zalloc(sizeof(*hapd));
1301 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1304 hapd->iface = hapd_iface;
1305 hapd->driver = hapd->iconf->driver;
1306 hapd->ctrl_sock = -1;
1312 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1314 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1316 hostapd_free_stas(hapd);
1317 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1318 hostapd_clear_wep(hapd);
1319 hostapd_cleanup(hapd);
1323 void hostapd_interface_deinit(struct hostapd_iface *iface)
1327 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1331 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1332 iface->wait_channel_update = 0;
1334 for (j = iface->num_bss - 1; j >= 0; j--)
1335 hostapd_bss_deinit(iface->bss[j]);
1339 void hostapd_interface_free(struct hostapd_iface *iface)
1342 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1343 for (j = 0; j < iface->num_bss; j++) {
1344 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1345 __func__, iface->bss[j]);
1346 os_free(iface->bss[j]);
1348 hostapd_cleanup_iface(iface);
1353 * hostapd_init - Allocate and initialize per-interface data
1354 * @config_file: Path to the configuration file
1355 * Returns: Pointer to the allocated interface data or %NULL on failure
1357 * This function is used to allocate main data structures for per-interface
1358 * data. The allocated data buffer will be freed by calling
1359 * hostapd_cleanup_iface().
1361 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1362 const char *config_file)
1364 struct hostapd_iface *hapd_iface = NULL;
1365 struct hostapd_config *conf = NULL;
1366 struct hostapd_data *hapd;
1369 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1370 if (hapd_iface == NULL)
1373 hapd_iface->config_fname = os_strdup(config_file);
1374 if (hapd_iface->config_fname == NULL)
1377 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1380 hapd_iface->conf = conf;
1382 hapd_iface->num_bss = conf->num_bss;
1383 hapd_iface->bss = os_calloc(conf->num_bss,
1384 sizeof(struct hostapd_data *));
1385 if (hapd_iface->bss == NULL)
1388 for (i = 0; i < conf->num_bss; i++) {
1389 hapd = hapd_iface->bss[i] =
1390 hostapd_alloc_bss_data(hapd_iface, conf,
1394 hapd->msg_ctx = hapd;
1400 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1403 hostapd_config_free(conf);
1405 os_free(hapd_iface->config_fname);
1406 os_free(hapd_iface->bss);
1407 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1408 __func__, hapd_iface);
1409 os_free(hapd_iface);
1415 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1419 for (i = 0; i < interfaces->count; i++) {
1420 struct hostapd_iface *iface = interfaces->iface[i];
1421 for (j = 0; j < iface->num_bss; j++) {
1422 struct hostapd_data *hapd = iface->bss[j];
1423 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1433 * hostapd_interface_init_bss - Read configuration file and init BSS data
1435 * This function is used to parse configuration file for a BSS. This BSS is
1436 * added to an existing interface sharing the same radio (if any) or a new
1437 * interface is created if this is the first interface on a radio. This
1438 * allocate memory for the BSS. No actual driver operations are started.
1440 * This is similar to hostapd_interface_init(), but for a case where the
1441 * configuration is used to add a single BSS instead of all BSSes for a radio.
1443 struct hostapd_iface *
1444 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1445 const char *config_fname, int debug)
1447 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1448 struct hostapd_data *hapd;
1455 for (i = 0; i < interfaces->count; i++) {
1456 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1457 iface = interfaces->iface[i];
1462 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1463 config_fname, phy, iface ? "" : " --> new PHY");
1465 struct hostapd_config *conf;
1466 struct hostapd_bss_config **tmp_conf;
1467 struct hostapd_data **tmp_bss;
1468 struct hostapd_bss_config *bss;
1471 /* Add new BSS to existing iface */
1472 conf = interfaces->config_read_cb(config_fname);
1475 if (conf->num_bss > 1) {
1476 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1477 hostapd_config_free(conf);
1481 ifname = conf->bss[0]->iface;
1482 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1483 wpa_printf(MSG_ERROR,
1484 "Interface name %s already in use", ifname);
1485 hostapd_config_free(conf);
1489 tmp_conf = os_realloc_array(
1490 iface->conf->bss, iface->conf->num_bss + 1,
1491 sizeof(struct hostapd_bss_config *));
1492 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1493 sizeof(struct hostapd_data *));
1495 iface->bss = tmp_bss;
1497 iface->conf->bss = tmp_conf;
1498 iface->conf->last_bss = tmp_conf[0];
1500 if (tmp_bss == NULL || tmp_conf == NULL) {
1501 hostapd_config_free(conf);
1504 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1505 iface->conf->num_bss++;
1507 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1509 iface->conf->num_bss--;
1510 hostapd_config_free(conf);
1513 iface->conf->last_bss = bss;
1514 iface->bss[iface->num_bss] = hapd;
1515 hapd->msg_ctx = hapd;
1517 bss_idx = iface->num_bss++;
1519 conf->bss[0] = NULL;
1520 hostapd_config_free(conf);
1522 /* Add a new iface with the first BSS */
1523 new_iface = iface = hostapd_init(interfaces, config_fname);
1526 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1527 iface->interfaces = interfaces;
1531 for (k = 0; k < debug; k++) {
1532 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1533 iface->bss[bss_idx]->conf->logger_stdout_level--;
1536 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1537 !hostapd_drv_none(iface->bss[bss_idx])) {
1538 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1541 hostapd_interface_deinit_free(new_iface);
1549 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1551 const struct wpa_driver_ops *driver;
1554 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1557 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1558 __func__, (unsigned int) iface->num_bss,
1559 (unsigned int) iface->conf->num_bss);
1560 driver = iface->bss[0]->driver;
1561 drv_priv = iface->bss[0]->drv_priv;
1562 hostapd_interface_deinit(iface);
1563 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1564 __func__, driver, drv_priv);
1565 if (driver && driver->hapd_deinit && drv_priv)
1566 driver->hapd_deinit(drv_priv);
1567 hostapd_interface_free(iface);
1571 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1573 if (hapd_iface->bss[0]->drv_priv != NULL) {
1574 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1575 hapd_iface->conf->bss[0]->iface);
1579 wpa_printf(MSG_DEBUG, "Enable interface %s",
1580 hapd_iface->conf->bss[0]->iface);
1582 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1583 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1587 if (hapd_iface->interfaces == NULL ||
1588 hapd_iface->interfaces->driver_init == NULL ||
1589 hapd_iface->interfaces->driver_init(hapd_iface))
1592 if (hostapd_setup_interface(hapd_iface)) {
1593 const struct wpa_driver_ops *driver;
1596 driver = hapd_iface->bss[0]->driver;
1597 drv_priv = hapd_iface->bss[0]->drv_priv;
1598 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1599 __func__, driver, drv_priv);
1600 if (driver && driver->hapd_deinit && drv_priv) {
1601 driver->hapd_deinit(drv_priv);
1602 hapd_iface->bss[0]->drv_priv = NULL;
1611 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1615 wpa_printf(MSG_DEBUG, "Reload interface %s",
1616 hapd_iface->conf->bss[0]->iface);
1617 for (j = 0; j < hapd_iface->num_bss; j++)
1618 hostapd_set_security_params(hapd_iface->conf->bss[j]);
1619 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1620 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1623 hostapd_clear_old(hapd_iface);
1624 for (j = 0; j < hapd_iface->num_bss; j++)
1625 hostapd_reload_bss(hapd_iface->bss[j]);
1631 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1634 const struct wpa_driver_ops *driver;
1637 if (hapd_iface == NULL)
1639 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1640 driver = hapd_iface->bss[0]->driver;
1641 drv_priv = hapd_iface->bss[0]->drv_priv;
1643 hapd_iface->driver_ap_teardown =
1644 !!(hapd_iface->drv_flags &
1645 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1647 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1648 for (j = 0; j < hapd_iface->num_bss; j++) {
1649 struct hostapd_data *hapd = hapd_iface->bss[j];
1650 hostapd_free_stas(hapd);
1651 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1652 hostapd_clear_wep(hapd);
1653 hostapd_free_hapd_data(hapd);
1656 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1657 __func__, driver, drv_priv);
1658 if (driver && driver->hapd_deinit && drv_priv) {
1659 driver->hapd_deinit(drv_priv);
1660 hapd_iface->bss[0]->drv_priv = NULL;
1663 /* From hostapd_cleanup_iface: These were initialized in
1664 * hostapd_setup_interface and hostapd_setup_interface_complete
1666 hostapd_cleanup_iface_partial(hapd_iface);
1668 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1669 hapd_iface->bss[0]->conf->iface);
1670 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1675 static struct hostapd_iface *
1676 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1678 struct hostapd_iface **iface, *hapd_iface;
1680 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1681 sizeof(struct hostapd_iface *));
1684 interfaces->iface = iface;
1685 hapd_iface = interfaces->iface[interfaces->count] =
1686 os_zalloc(sizeof(*hapd_iface));
1687 if (hapd_iface == NULL) {
1688 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1689 "the interface", __func__);
1692 interfaces->count++;
1693 hapd_iface->interfaces = interfaces;
1699 static struct hostapd_config *
1700 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1701 const char *ctrl_iface)
1703 struct hostapd_bss_config *bss;
1704 struct hostapd_config *conf;
1706 /* Allocates memory for bss and conf */
1707 conf = hostapd_config_defaults();
1709 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1710 "configuration", __func__);
1714 conf->driver = wpa_drivers[0];
1715 if (conf->driver == NULL) {
1716 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1717 hostapd_config_free(conf);
1721 bss = conf->last_bss = conf->bss[0];
1723 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1724 bss->ctrl_interface = os_strdup(ctrl_iface);
1725 if (bss->ctrl_interface == NULL) {
1726 hostapd_config_free(conf);
1730 /* Reading configuration file skipped, will be done in SET!
1731 * From reading the configuration till the end has to be done in
1738 static struct hostapd_iface * hostapd_data_alloc(
1739 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1742 struct hostapd_iface *hapd_iface =
1743 interfaces->iface[interfaces->count - 1];
1744 struct hostapd_data *hapd;
1746 hapd_iface->conf = conf;
1747 hapd_iface->num_bss = conf->num_bss;
1749 hapd_iface->bss = os_zalloc(conf->num_bss *
1750 sizeof(struct hostapd_data *));
1751 if (hapd_iface->bss == NULL)
1754 for (i = 0; i < conf->num_bss; i++) {
1755 hapd = hapd_iface->bss[i] =
1756 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1759 hapd->msg_ctx = hapd;
1762 hapd_iface->interfaces = interfaces;
1768 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1770 struct hostapd_config *conf = NULL;
1771 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1772 struct hostapd_data *hapd;
1775 const char *conf_file = NULL, *phy_name = NULL;
1777 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1779 phy_name = buf + 11;
1780 pos = os_strchr(phy_name, ':');
1785 if (!os_strlen(conf_file))
1788 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1792 for (j = 0; j < interfaces->count; j++) {
1793 if (interfaces->iface[j] == hapd_iface)
1796 if (j == interfaces->count) {
1797 struct hostapd_iface **tmp;
1798 tmp = os_realloc_array(interfaces->iface,
1799 interfaces->count + 1,
1800 sizeof(struct hostapd_iface *));
1802 hostapd_interface_deinit_free(hapd_iface);
1805 interfaces->iface = tmp;
1806 interfaces->iface[interfaces->count++] = hapd_iface;
1807 new_iface = hapd_iface;
1811 if (interfaces->driver_init(hapd_iface) ||
1812 hostapd_setup_interface(hapd_iface)) {
1813 interfaces->count--;
1817 /* Assign new BSS with bss[0]'s driver info */
1818 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1819 hapd->driver = hapd_iface->bss[0]->driver;
1820 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1821 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1824 if (start_ctrl_iface_bss(hapd) < 0 ||
1825 (hapd_iface->state == HAPD_IFACE_ENABLED &&
1826 hostapd_setup_bss(hapd, -1))) {
1827 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
1828 hapd_iface->conf->num_bss--;
1829 hapd_iface->num_bss--;
1830 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1831 __func__, hapd, hapd->conf->iface);
1839 ptr = os_strchr(buf, ' ');
1844 if (os_strncmp(ptr, "config=", 7) == 0)
1845 conf_file = ptr + 7;
1847 for (i = 0; i < interfaces->count; i++) {
1848 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1850 wpa_printf(MSG_INFO, "Cannot add interface - it "
1856 hapd_iface = hostapd_iface_alloc(interfaces);
1857 if (hapd_iface == NULL) {
1858 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1859 "for interface", __func__);
1863 if (conf_file && interfaces->config_read_cb) {
1864 conf = interfaces->config_read_cb(conf_file);
1865 if (conf && conf->bss)
1866 os_strlcpy(conf->bss[0]->iface, buf,
1867 sizeof(conf->bss[0]->iface));
1869 conf = hostapd_config_alloc(interfaces, buf, ptr);
1870 if (conf == NULL || conf->bss == NULL) {
1871 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1872 "for configuration", __func__);
1876 hapd_iface = hostapd_data_alloc(interfaces, conf);
1877 if (hapd_iface == NULL) {
1878 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1879 "for hostapd", __func__);
1883 if (start_ctrl_iface(hapd_iface) < 0)
1886 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
1892 hostapd_config_free(conf);
1894 if (hapd_iface->bss) {
1895 for (i = 0; i < hapd_iface->num_bss; i++) {
1896 hapd = hapd_iface->bss[i];
1899 if (hapd_iface->interfaces &&
1900 hapd_iface->interfaces->ctrl_iface_deinit)
1901 hapd_iface->interfaces->
1902 ctrl_iface_deinit(hapd);
1903 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1904 __func__, hapd_iface->bss[i],
1907 hapd_iface->bss[i] = NULL;
1909 os_free(hapd_iface->bss);
1911 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1912 __func__, hapd_iface);
1913 os_free(hapd_iface);
1919 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
1923 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
1925 /* Remove hostapd_data only if it has already been initialized */
1926 if (idx < iface->num_bss) {
1927 struct hostapd_data *hapd = iface->bss[idx];
1929 hostapd_bss_deinit(hapd);
1930 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1931 __func__, hapd, hapd->conf->iface);
1932 hostapd_config_free_bss(hapd->conf);
1937 for (i = idx; i < iface->num_bss; i++)
1938 iface->bss[i] = iface->bss[i + 1];
1940 hostapd_config_free_bss(iface->conf->bss[idx]);
1941 iface->conf->bss[idx] = NULL;
1944 iface->conf->num_bss--;
1945 for (i = idx; i < iface->conf->num_bss; i++)
1946 iface->conf->bss[i] = iface->conf->bss[i + 1];
1952 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1954 struct hostapd_iface *hapd_iface;
1957 for (i = 0; i < interfaces->count; i++) {
1958 hapd_iface = interfaces->iface[i];
1959 if (hapd_iface == NULL)
1961 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
1962 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1963 hapd_iface->driver_ap_teardown =
1964 !!(hapd_iface->drv_flags &
1965 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1967 hostapd_interface_deinit_free(hapd_iface);
1969 while (k < (interfaces->count - 1)) {
1970 interfaces->iface[k] =
1971 interfaces->iface[k + 1];
1974 interfaces->count--;
1978 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
1979 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
1980 hapd_iface->driver_ap_teardown =
1981 !(hapd_iface->drv_flags &
1982 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1983 return hostapd_remove_bss(hapd_iface, j);
1992 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1993 * @hapd: Pointer to BSS data
1994 * @sta: Pointer to the associated STA data
1995 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1997 * This function will be called whenever a station associates with the AP. It
1998 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1999 * from drv_callbacks.c based on driver events for drivers that take care of
2000 * management frames (IEEE 802.11 authentication and association) internally.
2002 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2005 if (hapd->tkip_countermeasures) {
2006 hostapd_drv_sta_deauth(hapd, sta->addr,
2007 WLAN_REASON_MICHAEL_MIC_FAILURE);
2011 hostapd_prune_associations(hapd, sta->addr);
2013 /* IEEE 802.11F (IAPP) */
2014 if (hapd->conf->ieee802_11f)
2015 iapp_new_station(hapd->iapp, sta);
2018 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2019 sta->no_p2p_set = 1;
2020 hapd->num_sta_no_p2p++;
2021 if (hapd->num_sta_no_p2p == 1)
2022 hostapd_p2p_non_p2p_sta_connected(hapd);
2024 #endif /* CONFIG_P2P */
2026 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2027 * IEEE 802.1X/WPA code will start accounting after the station has
2028 * been authorized. */
2029 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
2030 ap_sta_set_authorized(hapd, sta, 1);
2031 os_get_reltime(&sta->connected_time);
2032 accounting_sta_start(hapd, sta);
2035 /* Start IEEE 802.1X authentication process for new stations */
2036 ieee802_1x_new_station(hapd, sta);
2038 if (sta->auth_alg != WLAN_AUTH_FT &&
2039 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2040 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2042 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2044 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2045 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2046 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2047 __func__, MAC2STR(sta->addr),
2048 hapd->conf->ap_max_inactivity);
2049 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2050 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2051 ap_handle_timer, hapd, sta);
2056 const char * hostapd_state_text(enum hostapd_iface_state s)
2059 case HAPD_IFACE_UNINITIALIZED:
2060 return "UNINITIALIZED";
2061 case HAPD_IFACE_DISABLED:
2063 case HAPD_IFACE_COUNTRY_UPDATE:
2064 return "COUNTRY_UPDATE";
2065 case HAPD_IFACE_ACS:
2067 case HAPD_IFACE_HT_SCAN:
2069 case HAPD_IFACE_DFS:
2071 case HAPD_IFACE_ENABLED:
2079 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2081 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2082 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2083 hostapd_state_text(s));
2090 static void free_beacon_data(struct beacon_data *beacon)
2092 os_free(beacon->head);
2093 beacon->head = NULL;
2094 os_free(beacon->tail);
2095 beacon->tail = NULL;
2096 os_free(beacon->probe_resp);
2097 beacon->probe_resp = NULL;
2098 os_free(beacon->beacon_ies);
2099 beacon->beacon_ies = NULL;
2100 os_free(beacon->proberesp_ies);
2101 beacon->proberesp_ies = NULL;
2102 os_free(beacon->assocresp_ies);
2103 beacon->assocresp_ies = NULL;
2107 static int hostapd_build_beacon_data(struct hostapd_iface *iface,
2108 struct beacon_data *beacon)
2110 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2111 struct wpa_driver_ap_params params;
2113 struct hostapd_data *hapd = iface->bss[0];
2115 os_memset(beacon, 0, sizeof(*beacon));
2116 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2120 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2124 goto free_ap_params;
2127 beacon->head = os_malloc(params.head_len);
2129 goto free_ap_extra_ies;
2131 os_memcpy(beacon->head, params.head, params.head_len);
2132 beacon->head_len = params.head_len;
2134 beacon->tail = os_malloc(params.tail_len);
2138 os_memcpy(beacon->tail, params.tail, params.tail_len);
2139 beacon->tail_len = params.tail_len;
2141 if (params.proberesp != NULL) {
2142 beacon->probe_resp = os_malloc(params.proberesp_len);
2143 if (!beacon->probe_resp)
2146 os_memcpy(beacon->probe_resp, params.proberesp,
2147 params.proberesp_len);
2148 beacon->probe_resp_len = params.proberesp_len;
2151 /* copy the extra ies */
2153 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2154 if (!beacon->beacon_ies)
2157 os_memcpy(beacon->beacon_ies,
2158 beacon_extra->buf, wpabuf_len(beacon_extra));
2159 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2162 if (proberesp_extra) {
2163 beacon->proberesp_ies =
2164 os_malloc(wpabuf_len(proberesp_extra));
2165 if (!beacon->proberesp_ies)
2168 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2169 wpabuf_len(proberesp_extra));
2170 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2173 if (assocresp_extra) {
2174 beacon->assocresp_ies =
2175 os_malloc(wpabuf_len(assocresp_extra));
2176 if (!beacon->assocresp_ies)
2179 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2180 wpabuf_len(assocresp_extra));
2181 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2186 /* if the function fails, the caller should not free beacon data */
2188 free_beacon_data(beacon);
2191 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2194 ieee802_11_free_ap_params(¶ms);
2200 * TODO: This flow currently supports only changing frequency within the
2201 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2202 * width) are not supported.
2204 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2205 struct hostapd_config *conf,
2206 struct hostapd_freq_params *params,
2207 struct hostapd_freq_params *old_params)
2211 if (!params->channel) {
2212 /* check if the new channel is supported by hw */
2213 channel = hostapd_hw_get_channel(hapd, params->freq);
2217 channel = params->channel;
2220 /* if a pointer to old_params is provided we save previous state */
2222 old_params->channel = conf->channel;
2223 old_params->ht_enabled = conf->ieee80211n;
2224 old_params->sec_channel_offset = conf->secondary_channel;
2227 conf->channel = channel;
2228 conf->ieee80211n = params->ht_enabled;
2229 conf->secondary_channel = params->sec_channel_offset;
2231 /* TODO: maybe call here hostapd_config_check here? */
2237 static int hostapd_fill_csa_settings(struct hostapd_iface *iface,
2238 struct csa_settings *settings)
2240 struct hostapd_freq_params old_freq;
2243 os_memset(&old_freq, 0, sizeof(old_freq));
2244 if (!iface || !iface->freq || iface->csa_in_progress)
2247 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2248 &settings->freq_params,
2253 ret = hostapd_build_beacon_data(iface, &settings->beacon_after);
2255 /* change back the configuration */
2256 hostapd_change_config_freq(iface->bss[0], iface->conf,
2262 /* set channel switch parameters for csa ie */
2263 iface->cs_freq_params = settings->freq_params;
2264 iface->cs_count = settings->cs_count;
2265 iface->cs_block_tx = settings->block_tx;
2267 ret = hostapd_build_beacon_data(iface, &settings->beacon_csa);
2269 free_beacon_data(&settings->beacon_after);
2273 settings->counter_offset_beacon = iface->cs_c_off_beacon;
2274 settings->counter_offset_presp = iface->cs_c_off_proberesp;
2280 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2282 os_memset(&hapd->iface->cs_freq_params, 0,
2283 sizeof(hapd->iface->cs_freq_params));
2284 hapd->iface->cs_count = 0;
2285 hapd->iface->cs_block_tx = 0;
2286 hapd->iface->cs_c_off_beacon = 0;
2287 hapd->iface->cs_c_off_proberesp = 0;
2288 hapd->iface->csa_in_progress = 0;
2292 int hostapd_switch_channel(struct hostapd_data *hapd,
2293 struct csa_settings *settings)
2296 ret = hostapd_fill_csa_settings(hapd->iface, settings);
2300 ret = hostapd_drv_switch_channel(hapd, settings);
2301 free_beacon_data(&settings->beacon_csa);
2302 free_beacon_data(&settings->beacon_after);
2305 /* if we failed, clean cs parameters */
2306 hostapd_cleanup_cs_params(hapd);
2310 hapd->iface->csa_in_progress = 1;
2314 #endif /* NEED_AP_MLME */