2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2013, 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 "drivers/driver.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"
39 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
40 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
41 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
42 static int setup_interface2(struct hostapd_iface *iface);
43 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
45 extern int wpa_debug_level;
46 extern struct wpa_driver_ops *wpa_drivers[];
49 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
50 int (*cb)(struct hostapd_iface *iface,
51 void *ctx), void *ctx)
56 for (i = 0; i < interfaces->count; i++) {
57 ret = cb(interfaces->iface[i], ctx);
66 static void hostapd_reload_bss(struct hostapd_data *hapd)
68 struct hostapd_ssid *ssid;
70 #ifndef CONFIG_NO_RADIUS
71 radius_client_reconfig(hapd->radius, hapd->conf->radius);
72 #endif /* CONFIG_NO_RADIUS */
74 ssid = &hapd->conf->ssid;
75 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
76 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
78 * Force PSK to be derived again since SSID or passphrase may
81 os_free(ssid->wpa_psk);
84 if (hostapd_setup_wpa_psk(hapd->conf)) {
85 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
86 "after reloading configuration");
89 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
90 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
92 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
94 if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
95 hostapd_setup_wpa(hapd);
97 wpa_init_keys(hapd->wpa_auth);
98 } else if (hapd->conf->wpa) {
101 hostapd_reconfig_wpa(hapd);
102 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
103 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
104 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
105 "the kernel driver.");
106 } else if (hapd->wpa_auth) {
107 wpa_deinit(hapd->wpa_auth);
108 hapd->wpa_auth = NULL;
109 hostapd_set_privacy(hapd, 0);
110 hostapd_setup_encryption(hapd->conf->iface, hapd);
111 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
114 ieee802_11_set_beacon(hapd);
115 hostapd_update_wps(hapd);
117 if (hapd->conf->ssid.ssid_set &&
118 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
119 hapd->conf->ssid.ssid_len)) {
120 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
121 /* try to continue */
123 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
127 static void hostapd_clear_old(struct hostapd_iface *iface)
132 * Deauthenticate all stations since the new configuration may not
133 * allow them to use the BSS anymore.
135 for (j = 0; j < iface->num_bss; j++) {
136 hostapd_flush_old_stations(iface->bss[j],
137 WLAN_REASON_PREV_AUTH_NOT_VALID);
138 hostapd_broadcast_wep_clear(iface->bss[j]);
140 #ifndef CONFIG_NO_RADIUS
141 /* TODO: update dynamic data based on changed configuration
142 * items (e.g., open/close sockets, etc.) */
143 radius_client_flush(iface->bss[j]->radius, 0);
144 #endif /* CONFIG_NO_RADIUS */
149 int hostapd_reload_config(struct hostapd_iface *iface)
151 struct hostapd_data *hapd = iface->bss[0];
152 struct hostapd_config *newconf, *oldconf;
155 if (iface->config_fname == NULL) {
156 /* Only in-memory config in use - assume it has been updated */
157 hostapd_clear_old(iface);
158 for (j = 0; j < iface->num_bss; j++)
159 hostapd_reload_bss(iface->bss[j]);
163 if (iface->interfaces == NULL ||
164 iface->interfaces->config_read_cb == NULL)
166 newconf = iface->interfaces->config_read_cb(iface->config_fname);
170 hostapd_clear_old(iface);
172 oldconf = hapd->iconf;
173 iface->conf = newconf;
175 for (j = 0; j < iface->num_bss; j++) {
176 hapd = iface->bss[j];
177 hapd->iconf = newconf;
178 hapd->conf = newconf->bss[j];
179 hostapd_reload_bss(hapd);
182 hostapd_config_free(oldconf);
189 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
194 for (i = 0; i < NUM_WEP_KEYS; i++) {
195 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
196 0, NULL, 0, NULL, 0)) {
197 wpa_printf(MSG_DEBUG, "Failed to clear default "
198 "encryption keys (ifname=%s keyidx=%d)",
202 #ifdef CONFIG_IEEE80211W
203 if (hapd->conf->ieee80211w) {
204 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
205 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
208 wpa_printf(MSG_DEBUG, "Failed to clear "
209 "default mgmt encryption keys "
210 "(ifname=%s keyidx=%d)", ifname, i);
214 #endif /* CONFIG_IEEE80211W */
218 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
220 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
225 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
228 struct hostapd_ssid *ssid = &hapd->conf->ssid;
231 if (ssid->wep.default_len &&
232 hostapd_drv_set_key(hapd->conf->iface,
233 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
234 1, NULL, 0, ssid->wep.key[idx],
235 ssid->wep.len[idx])) {
236 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
244 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
246 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
247 iapp_deinit(hapd->iapp);
249 accounting_deinit(hapd);
250 hostapd_deinit_wpa(hapd);
252 hostapd_acl_deinit(hapd);
253 #ifndef CONFIG_NO_RADIUS
254 radius_client_deinit(hapd->radius);
256 radius_das_deinit(hapd->radius_das);
257 hapd->radius_das = NULL;
258 #endif /* CONFIG_NO_RADIUS */
260 hostapd_deinit_wps(hapd);
262 authsrv_deinit(hapd);
264 if (hapd->interface_added &&
265 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
266 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
270 os_free(hapd->probereq_cb);
271 hapd->probereq_cb = NULL;
274 wpabuf_free(hapd->p2p_beacon_ie);
275 hapd->p2p_beacon_ie = NULL;
276 wpabuf_free(hapd->p2p_probe_resp_ie);
277 hapd->p2p_probe_resp_ie = NULL;
278 #endif /* CONFIG_P2P */
280 wpabuf_free(hapd->time_adv);
282 #ifdef CONFIG_INTERWORKING
283 gas_serv_deinit(hapd);
284 #endif /* CONFIG_INTERWORKING */
287 os_free(hapd->tmp_eap_user.identity);
288 os_free(hapd->tmp_eap_user.password);
289 #endif /* CONFIG_SQLITE */
294 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
295 * @hapd: Pointer to BSS data
297 * This function is used to free all per-BSS data structures and resources.
298 * Most of the modules that are initialized in hostapd_setup_bss() are
299 * deinitialized here.
301 static void hostapd_cleanup(struct hostapd_data *hapd)
303 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
305 if (hapd->iface->interfaces &&
306 hapd->iface->interfaces->ctrl_iface_deinit)
307 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
308 hostapd_free_hapd_data(hapd);
313 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
315 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
316 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
317 iface->hw_features = NULL;
318 os_free(iface->current_rates);
319 iface->current_rates = NULL;
320 os_free(iface->basic_rates);
321 iface->basic_rates = NULL;
322 ap_list_deinit(iface);
327 * hostapd_cleanup_iface - Complete per-interface cleanup
328 * @iface: Pointer to interface data
330 * This function is called after per-BSS data structures are deinitialized
331 * with hostapd_cleanup().
333 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
335 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
336 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
338 hostapd_cleanup_iface_partial(iface);
339 hostapd_config_free(iface->conf);
342 os_free(iface->config_fname);
344 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
349 static void hostapd_clear_wep(struct hostapd_data *hapd)
351 if (hapd->drv_priv) {
352 hostapd_set_privacy(hapd, 0);
353 hostapd_broadcast_wep_clear(hapd);
358 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
362 hostapd_broadcast_wep_set(hapd);
364 if (hapd->conf->ssid.wep.default_len) {
365 hostapd_set_privacy(hapd, 1);
370 * When IEEE 802.1X is not enabled, the driver may need to know how to
371 * set authentication algorithms for static WEP.
373 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
375 for (i = 0; i < 4; i++) {
376 if (hapd->conf->ssid.wep.key[i] &&
377 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
378 i == hapd->conf->ssid.wep.idx, NULL, 0,
379 hapd->conf->ssid.wep.key[i],
380 hapd->conf->ssid.wep.len[i])) {
381 wpa_printf(MSG_WARNING, "Could not set WEP "
385 if (hapd->conf->ssid.wep.key[i] &&
386 i == hapd->conf->ssid.wep.idx)
387 hostapd_set_privacy(hapd, 1);
394 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
399 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
402 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
403 if (hostapd_flush(hapd)) {
404 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
408 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
409 os_memset(addr, 0xff, ETH_ALEN);
410 hostapd_drv_sta_deauth(hapd, addr, reason);
411 hostapd_free_stas(hapd);
418 * hostapd_validate_bssid_configuration - Validate BSSID configuration
419 * @iface: Pointer to interface data
420 * Returns: 0 on success, -1 on failure
422 * This function is used to validate that the configured BSSIDs are valid.
424 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
426 u8 mask[ETH_ALEN] = { 0 };
427 struct hostapd_data *hapd = iface->bss[0];
428 unsigned int i = iface->conf->num_bss, bits = 0, j;
431 if (hostapd_drv_none(hapd))
434 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
436 /* Determine the bits necessary to cover the number of BSSIDs. */
437 for (i--; i; i >>= 1)
440 /* Determine the bits necessary to any configured BSSIDs,
441 if they are higher than the number of BSSIDs. */
442 for (j = 0; j < iface->conf->num_bss; j++) {
443 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
449 for (i = 0; i < ETH_ALEN; i++) {
451 iface->conf->bss[j]->bssid[i] ^
459 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
465 while (mask[i] != 0) {
475 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
480 os_memset(mask, 0xff, ETH_ALEN);
482 for (i = 5; i > 5 - j; i--)
489 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
490 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
495 for (i = 0; i < ETH_ALEN; i++) {
496 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
497 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
498 " for start address " MACSTR ".",
499 MAC2STR(mask), MAC2STR(hapd->own_addr));
500 wpa_printf(MSG_ERROR, "Start address must be the "
501 "first address in the block (i.e., addr "
502 "AND mask == addr).");
511 static int mac_in_conf(struct hostapd_config *conf, const void *a)
515 for (i = 0; i < conf->num_bss; i++) {
516 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
525 #ifndef CONFIG_NO_RADIUS
527 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
528 struct radius_das_attrs *attr)
535 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
536 struct radius_das_attrs *attr)
538 struct sta_info *sta = NULL;
542 sta = ap_get_sta(hapd, attr->sta_addr);
544 if (sta == NULL && attr->acct_session_id &&
545 attr->acct_session_id_len == 17) {
546 for (sta = hapd->sta_list; sta; sta = sta->next) {
547 os_snprintf(buf, sizeof(buf), "%08X-%08X",
548 sta->acct_session_id_hi,
549 sta->acct_session_id_lo);
550 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
555 if (sta == NULL && attr->cui) {
556 for (sta = hapd->sta_list; sta; sta = sta->next) {
558 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
559 if (cui && wpabuf_len(cui) == attr->cui_len &&
560 os_memcmp(wpabuf_head(cui), attr->cui,
566 if (sta == NULL && attr->user_name) {
567 for (sta = hapd->sta_list; sta; sta = sta->next) {
570 identity = ieee802_1x_get_identity(sta->eapol_sm,
573 identity_len == attr->user_name_len &&
574 os_memcmp(identity, attr->user_name, identity_len)
584 static enum radius_das_res
585 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
587 struct hostapd_data *hapd = ctx;
588 struct sta_info *sta;
590 if (hostapd_das_nas_mismatch(hapd, attr))
591 return RADIUS_DAS_NAS_MISMATCH;
593 sta = hostapd_das_find_sta(hapd, attr);
595 return RADIUS_DAS_SESSION_NOT_FOUND;
597 hostapd_drv_sta_deauth(hapd, sta->addr,
598 WLAN_REASON_PREV_AUTH_NOT_VALID);
599 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
601 return RADIUS_DAS_SUCCESS;
604 #endif /* CONFIG_NO_RADIUS */
608 * hostapd_setup_bss - Per-BSS setup (initialization)
609 * @hapd: Pointer to BSS data
610 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
611 * but interface may exist
613 * This function is used to initialize all per-BSS data structures and
614 * resources. This gets called in a loop for each BSS when an interface is
615 * initialized. Most of the modules that are initialized here will be
616 * deinitialized in hostapd_cleanup().
618 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
620 struct hostapd_bss_config *conf = hapd->conf;
621 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
622 int ssid_len, set_ssid;
623 char force_ifname[IFNAMSIZ];
624 u8 if_addr[ETH_ALEN];
626 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
627 __func__, hapd, hapd->conf->iface, first);
630 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
631 __func__, hapd->conf->iface);
636 if (!first || first == -1) {
637 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
638 /* Allocate the next available BSSID. */
640 inc_byte_array(hapd->own_addr, ETH_ALEN);
641 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
643 /* Allocate the configured BSSID. */
644 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
646 if (hostapd_mac_comp(hapd->own_addr,
647 hapd->iface->bss[0]->own_addr) ==
649 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
650 "BSSID set to the MAC address of "
651 "the radio", hapd->conf->iface);
656 hapd->interface_added = 1;
657 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
658 hapd->conf->iface, hapd->own_addr, hapd,
659 &hapd->drv_priv, force_ifname, if_addr,
660 hapd->conf->bridge[0] ? hapd->conf->bridge :
661 NULL, first == -1)) {
662 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
663 MACSTR ")", MAC2STR(hapd->own_addr));
668 if (conf->wmm_enabled < 0)
669 conf->wmm_enabled = hapd->iconf->ieee80211n;
671 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
672 hostapd_set_privacy(hapd, 0);
674 hostapd_broadcast_wep_clear(hapd);
675 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
679 * Fetch the SSID from the system and use it or,
680 * if one was specified in the config file, verify they
683 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
685 wpa_printf(MSG_ERROR, "Could not read SSID from system");
688 if (conf->ssid.ssid_set) {
690 * If SSID is specified in the config file and it differs
691 * from what is being used then force installation of the
694 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
695 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
698 * No SSID in the config file; just use the one we got
702 conf->ssid.ssid_len = ssid_len;
703 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
706 if (!hostapd_drv_none(hapd)) {
707 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
709 hapd->conf->iface, MAC2STR(hapd->own_addr),
710 wpa_ssid_txt(hapd->conf->ssid.ssid,
711 hapd->conf->ssid.ssid_len));
714 if (hostapd_setup_wpa_psk(conf)) {
715 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
719 /* Set SSID for the kernel driver (to be used in beacon and probe
720 * response frames) */
721 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
722 conf->ssid.ssid_len)) {
723 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
727 if (wpa_debug_level == MSG_MSGDUMP)
728 conf->radius->msg_dumps = 1;
729 #ifndef CONFIG_NO_RADIUS
730 hapd->radius = radius_client_init(hapd, conf->radius);
731 if (hapd->radius == NULL) {
732 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
736 if (hapd->conf->radius_das_port) {
737 struct radius_das_conf das_conf;
738 os_memset(&das_conf, 0, sizeof(das_conf));
739 das_conf.port = hapd->conf->radius_das_port;
740 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
741 das_conf.shared_secret_len =
742 hapd->conf->radius_das_shared_secret_len;
743 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
744 das_conf.time_window = hapd->conf->radius_das_time_window;
745 das_conf.require_event_timestamp =
746 hapd->conf->radius_das_require_event_timestamp;
748 das_conf.disconnect = hostapd_das_disconnect;
749 hapd->radius_das = radius_das_init(&das_conf);
750 if (hapd->radius_das == NULL) {
751 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
756 #endif /* CONFIG_NO_RADIUS */
758 if (hostapd_acl_init(hapd)) {
759 wpa_printf(MSG_ERROR, "ACL initialization failed.");
762 if (hostapd_init_wps(hapd, conf))
765 if (authsrv_init(hapd) < 0)
768 if (ieee802_1x_init(hapd)) {
769 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
773 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
776 if (accounting_init(hapd)) {
777 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
781 if (hapd->conf->ieee802_11f &&
782 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
783 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
788 #ifdef CONFIG_INTERWORKING
789 if (gas_serv_init(hapd)) {
790 wpa_printf(MSG_ERROR, "GAS server initialization failed");
794 if (conf->qos_map_set_len &&
795 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
796 conf->qos_map_set_len)) {
797 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
800 #endif /* CONFIG_INTERWORKING */
802 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
803 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
807 if (!hapd->conf->start_disabled)
808 ieee802_11_set_beacon(hapd);
810 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
813 if (hapd->driver && hapd->driver->set_operstate)
814 hapd->driver->set_operstate(hapd->drv_priv, 1);
820 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
822 struct hostapd_data *hapd = iface->bss[0];
824 struct hostapd_tx_queue_params *p;
826 for (i = 0; i < NUM_TX_QUEUES; i++) {
827 p = &iface->conf->tx_queue[i];
829 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
830 p->cwmax, p->burst)) {
831 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
832 "parameters for queue %d.", i);
833 /* Continue anyway */
839 static int hostapd_set_acl_list(struct hostapd_data *hapd,
840 struct mac_acl_entry *mac_acl,
841 int n_entries, u8 accept_acl)
843 struct hostapd_acl_params *acl_params;
846 acl_params = os_zalloc(sizeof(*acl_params) +
847 (n_entries * sizeof(acl_params->mac_acl[0])));
851 for (i = 0; i < n_entries; i++)
852 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
855 acl_params->acl_policy = accept_acl;
856 acl_params->num_mac_acl = n_entries;
858 err = hostapd_drv_set_acl(hapd, acl_params);
866 static void hostapd_set_acl(struct hostapd_data *hapd)
868 struct hostapd_config *conf = hapd->iconf;
872 if (hapd->iface->drv_max_acl_mac_addrs == 0)
874 if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
877 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
878 if (conf->bss[0]->num_accept_mac) {
880 err = hostapd_set_acl_list(hapd,
881 conf->bss[0]->accept_mac,
882 conf->bss[0]->num_accept_mac,
885 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
889 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
891 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
892 if (conf->bss[0]->num_deny_mac) {
894 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
895 conf->bss[0]->num_deny_mac,
898 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
902 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
908 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
910 if (!hapd->iface->interfaces ||
911 !hapd->iface->interfaces->ctrl_iface_init)
914 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
915 wpa_printf(MSG_ERROR,
916 "Failed to setup control interface for %s",
925 static int start_ctrl_iface(struct hostapd_iface *iface)
929 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
932 for (i = 0; i < iface->num_bss; i++) {
933 struct hostapd_data *hapd = iface->bss[i];
934 if (iface->interfaces->ctrl_iface_init(hapd)) {
935 wpa_printf(MSG_ERROR,
936 "Failed to setup control interface for %s",
946 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
948 struct hostapd_iface *iface = eloop_ctx;
950 if (!iface->wait_channel_update) {
951 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
956 * It is possible that the existing channel list is acceptable, so try
959 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
960 setup_interface2(iface);
964 void hostapd_channel_list_updated(struct hostapd_iface *iface)
966 if (!iface->wait_channel_update)
969 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
970 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
971 setup_interface2(iface);
975 static int setup_interface(struct hostapd_iface *iface)
977 struct hostapd_data *hapd = iface->bss[0];
980 if (!iface->phy[0]) {
981 const char *phy = hostapd_drv_get_radio_name(hapd);
983 wpa_printf(MSG_DEBUG, "phy: %s", phy);
984 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
989 * Make sure that all BSSes get configured with a pointer to the same
992 for (i = 1; i < iface->num_bss; i++) {
993 iface->bss[i]->driver = hapd->driver;
994 iface->bss[i]->drv_priv = hapd->drv_priv;
997 if (hostapd_validate_bssid_configuration(iface))
1001 * Initialize control interfaces early to allow external monitoring of
1002 * channel setup operations that may take considerable amount of time
1003 * especially for DFS cases.
1005 if (start_ctrl_iface(iface))
1008 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1009 char country[4], previous_country[4];
1011 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1012 if (hostapd_get_country(hapd, previous_country) < 0)
1013 previous_country[0] = '\0';
1015 os_memcpy(country, hapd->iconf->country, 3);
1017 if (hostapd_set_country(hapd, country) < 0) {
1018 wpa_printf(MSG_ERROR, "Failed to set country code");
1022 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1023 previous_country, country);
1025 if (os_strncmp(previous_country, country, 2) != 0) {
1026 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1027 iface->wait_channel_update = 1;
1028 eloop_register_timeout(1, 0,
1029 channel_list_update_timeout,
1035 return setup_interface2(iface);
1039 static int setup_interface2(struct hostapd_iface *iface)
1041 iface->wait_channel_update = 0;
1043 if (hostapd_get_hw_features(iface)) {
1044 /* Not all drivers support this yet, so continue without hw
1047 int ret = hostapd_select_hw_mode(iface);
1049 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1050 "channel. (%d)", ret);
1054 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1057 ret = hostapd_check_ht_capab(iface);
1061 wpa_printf(MSG_DEBUG, "Interface initialization will "
1062 "be completed in a callback");
1066 if (iface->conf->ieee80211h)
1067 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1069 return hostapd_setup_interface_complete(iface, 0);
1074 * hostapd_setup_interface_complete - Complete interface setup
1076 * This function is called when previous steps in the interface setup has been
1077 * completed. This can also start operations, e.g., DFS, that will require
1078 * additional processing before interface is ready to be enabled. Such
1079 * operations will call this function from eloop callbacks when finished.
1081 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1083 struct hostapd_data *hapd = iface->bss[0];
1088 wpa_printf(MSG_ERROR, "Interface initialization failed");
1089 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1090 if (iface->interfaces && iface->interfaces->terminate_on_error)
1095 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1096 if (iface->conf->channel) {
1099 #endif /* NEED_AP_MLME */
1101 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1102 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1103 "Frequency: %d MHz",
1104 hostapd_hw_mode_txt(iface->conf->hw_mode),
1105 iface->conf->channel, iface->freq);
1109 res = hostapd_handle_dfs(iface);
1112 #endif /* NEED_AP_MLME */
1114 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1115 hapd->iconf->channel,
1116 hapd->iconf->ieee80211n,
1117 hapd->iconf->ieee80211ac,
1118 hapd->iconf->secondary_channel,
1119 hapd->iconf->vht_oper_chwidth,
1120 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1121 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1122 wpa_printf(MSG_ERROR, "Could not set channel for "
1128 if (iface->current_mode) {
1129 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1130 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1132 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1133 HOSTAPD_LEVEL_WARNING,
1134 "Failed to prepare rates table.");
1139 if (hapd->iconf->rts_threshold > -1 &&
1140 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1141 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1146 if (hapd->iconf->fragm_threshold > -1 &&
1147 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1148 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1149 "for kernel driver");
1153 prev_addr = hapd->own_addr;
1155 for (j = 0; j < iface->num_bss; j++) {
1156 hapd = iface->bss[j];
1158 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1159 if (hostapd_setup_bss(hapd, j == 0))
1161 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1162 prev_addr = hapd->own_addr;
1164 hapd = iface->bss[0];
1166 hostapd_tx_queue_params(iface);
1168 ap_list_init(iface);
1170 hostapd_set_acl(hapd);
1172 if (hostapd_driver_commit(hapd) < 0) {
1173 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1174 "configuration", __func__);
1179 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1180 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1181 * mode), the interface is up only after driver_commit, so initialize
1182 * WPS after driver_commit.
1184 for (j = 0; j < iface->num_bss; j++) {
1185 if (hostapd_init_wps_complete(iface->bss[j]))
1189 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1190 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1191 if (hapd->setup_complete_cb)
1192 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1194 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1195 iface->bss[0]->conf->iface);
1196 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1197 iface->interfaces->terminate_on_error--;
1204 * hostapd_setup_interface - Setup of an interface
1205 * @iface: Pointer to interface data.
1206 * Returns: 0 on success, -1 on failure
1208 * Initializes the driver interface, validates the configuration,
1209 * and sets driver parameters based on the configuration.
1210 * Flushes old stations, sets the channel, encryption,
1211 * beacons, and WDS links based on the configuration.
1213 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1214 * or DFS operations, this function returns 0 before such operations have been
1215 * completed. The pending operations are registered into eloop and will be
1216 * completed from eloop callbacks. Those callbacks end up calling
1217 * hostapd_setup_interface_complete() once setup has been completed.
1219 int hostapd_setup_interface(struct hostapd_iface *iface)
1223 ret = setup_interface(iface);
1225 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1226 iface->bss[0]->conf->iface);
1235 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1236 * @hapd_iface: Pointer to interface data
1237 * @conf: Pointer to per-interface configuration
1238 * @bss: Pointer to per-BSS configuration for this BSS
1239 * Returns: Pointer to allocated BSS data
1241 * This function is used to allocate per-BSS data structure. This data will be
1242 * freed after hostapd_cleanup() is called for it during interface
1245 struct hostapd_data *
1246 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1247 struct hostapd_config *conf,
1248 struct hostapd_bss_config *bss)
1250 struct hostapd_data *hapd;
1252 hapd = os_zalloc(sizeof(*hapd));
1256 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1259 hapd->iface = hapd_iface;
1260 hapd->driver = hapd->iconf->driver;
1261 hapd->ctrl_sock = -1;
1267 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1269 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1271 hostapd_free_stas(hapd);
1272 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1273 hostapd_clear_wep(hapd);
1274 hostapd_cleanup(hapd);
1278 void hostapd_interface_deinit(struct hostapd_iface *iface)
1282 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1286 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1287 iface->wait_channel_update = 0;
1289 for (j = iface->num_bss - 1; j >= 0; j--)
1290 hostapd_bss_deinit(iface->bss[j]);
1294 void hostapd_interface_free(struct hostapd_iface *iface)
1297 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1298 for (j = 0; j < iface->num_bss; j++) {
1299 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1300 __func__, iface->bss[j]);
1301 os_free(iface->bss[j]);
1303 hostapd_cleanup_iface(iface);
1308 * hostapd_init - Allocate and initialize per-interface data
1309 * @config_file: Path to the configuration file
1310 * Returns: Pointer to the allocated interface data or %NULL on failure
1312 * This function is used to allocate main data structures for per-interface
1313 * data. The allocated data buffer will be freed by calling
1314 * hostapd_cleanup_iface().
1316 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1317 const char *config_file)
1319 struct hostapd_iface *hapd_iface = NULL;
1320 struct hostapd_config *conf = NULL;
1321 struct hostapd_data *hapd;
1324 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1325 if (hapd_iface == NULL)
1328 hapd_iface->config_fname = os_strdup(config_file);
1329 if (hapd_iface->config_fname == NULL)
1332 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1335 hapd_iface->conf = conf;
1337 hapd_iface->num_bss = conf->num_bss;
1338 hapd_iface->bss = os_calloc(conf->num_bss,
1339 sizeof(struct hostapd_data *));
1340 if (hapd_iface->bss == NULL)
1343 for (i = 0; i < conf->num_bss; i++) {
1344 hapd = hapd_iface->bss[i] =
1345 hostapd_alloc_bss_data(hapd_iface, conf,
1349 hapd->msg_ctx = hapd;
1355 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1358 hostapd_config_free(conf);
1360 os_free(hapd_iface->config_fname);
1361 os_free(hapd_iface->bss);
1362 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1363 __func__, hapd_iface);
1364 os_free(hapd_iface);
1370 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1374 for (i = 0; i < interfaces->count; i++) {
1375 struct hostapd_iface *iface = interfaces->iface[i];
1376 for (j = 0; j < iface->num_bss; j++) {
1377 struct hostapd_data *hapd = iface->bss[j];
1378 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1388 * hostapd_interface_init_bss - Read configuration file and init BSS data
1390 * This function is used to parse configuration file for a BSS. This BSS is
1391 * added to an existing interface sharing the same radio (if any) or a new
1392 * interface is created if this is the first interface on a radio. This
1393 * allocate memory for the BSS. No actual driver operations are started.
1395 * This is similar to hostapd_interface_init(), but for a case where the
1396 * configuration is used to add a single BSS instead of all BSSes for a radio.
1398 struct hostapd_iface *
1399 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1400 const char *config_fname, int debug)
1402 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1403 struct hostapd_data *hapd;
1410 for (i = 0; i < interfaces->count; i++) {
1411 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1412 iface = interfaces->iface[i];
1417 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1418 config_fname, phy, iface ? "" : " --> new PHY");
1420 struct hostapd_config *conf;
1421 struct hostapd_bss_config **tmp_conf;
1422 struct hostapd_data **tmp_bss;
1423 struct hostapd_bss_config *bss;
1426 /* Add new BSS to existing iface */
1427 conf = interfaces->config_read_cb(config_fname);
1430 if (conf->num_bss > 1) {
1431 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1432 hostapd_config_free(conf);
1436 ifname = conf->bss[0]->iface;
1437 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1438 wpa_printf(MSG_ERROR,
1439 "Interface name %s already in use", ifname);
1440 hostapd_config_free(conf);
1444 tmp_conf = os_realloc_array(
1445 iface->conf->bss, iface->conf->num_bss + 1,
1446 sizeof(struct hostapd_bss_config *));
1447 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1448 sizeof(struct hostapd_data *));
1450 iface->bss = tmp_bss;
1452 iface->conf->bss = tmp_conf;
1453 iface->conf->last_bss = tmp_conf[0];
1455 if (tmp_bss == NULL || tmp_conf == NULL) {
1456 hostapd_config_free(conf);
1459 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1460 iface->conf->num_bss++;
1462 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1464 iface->conf->num_bss--;
1465 hostapd_config_free(conf);
1468 iface->conf->last_bss = bss;
1469 iface->bss[iface->num_bss] = hapd;
1470 hapd->msg_ctx = hapd;
1472 bss_idx = iface->num_bss++;
1474 conf->bss[0] = NULL;
1475 hostapd_config_free(conf);
1477 /* Add a new iface with the first BSS */
1478 new_iface = iface = hostapd_init(interfaces, config_fname);
1481 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1482 iface->interfaces = interfaces;
1486 for (k = 0; k < debug; k++) {
1487 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1488 iface->bss[bss_idx]->conf->logger_stdout_level--;
1491 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1492 !hostapd_drv_none(iface->bss[bss_idx])) {
1493 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1496 hostapd_interface_deinit_free(new_iface);
1504 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1506 const struct wpa_driver_ops *driver;
1509 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1512 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1513 __func__, (unsigned int) iface->num_bss,
1514 (unsigned int) iface->conf->num_bss);
1515 driver = iface->bss[0]->driver;
1516 drv_priv = iface->bss[0]->drv_priv;
1517 hostapd_interface_deinit(iface);
1518 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1519 __func__, driver, drv_priv);
1520 if (driver && driver->hapd_deinit && drv_priv)
1521 driver->hapd_deinit(drv_priv);
1522 hostapd_interface_free(iface);
1526 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1528 if (hapd_iface->bss[0]->drv_priv != NULL) {
1529 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1530 hapd_iface->conf->bss[0]->iface);
1534 wpa_printf(MSG_DEBUG, "Enable interface %s",
1535 hapd_iface->conf->bss[0]->iface);
1537 if (hapd_iface->interfaces == NULL ||
1538 hapd_iface->interfaces->driver_init == NULL ||
1539 hapd_iface->interfaces->driver_init(hapd_iface))
1542 if (hostapd_setup_interface(hapd_iface)) {
1543 const struct wpa_driver_ops *driver;
1546 driver = hapd_iface->bss[0]->driver;
1547 drv_priv = hapd_iface->bss[0]->drv_priv;
1548 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1549 __func__, driver, drv_priv);
1550 if (driver && driver->hapd_deinit && drv_priv) {
1551 driver->hapd_deinit(drv_priv);
1552 hapd_iface->bss[0]->drv_priv = NULL;
1561 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1565 wpa_printf(MSG_DEBUG, "Reload interface %s",
1566 hapd_iface->conf->bss[0]->iface);
1567 for (j = 0; j < hapd_iface->num_bss; j++)
1568 hostapd_set_security_params(hapd_iface->conf->bss[j]);
1569 if (hostapd_config_check(hapd_iface->conf) < 0) {
1570 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1573 hostapd_clear_old(hapd_iface);
1574 for (j = 0; j < hapd_iface->num_bss; j++)
1575 hostapd_reload_bss(hapd_iface->bss[j]);
1581 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1584 const struct wpa_driver_ops *driver;
1587 if (hapd_iface == NULL)
1589 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1590 driver = hapd_iface->bss[0]->driver;
1591 drv_priv = hapd_iface->bss[0]->drv_priv;
1593 /* whatever hostapd_interface_deinit does */
1594 for (j = 0; j < hapd_iface->num_bss; j++) {
1595 struct hostapd_data *hapd = hapd_iface->bss[j];
1596 hostapd_free_stas(hapd);
1597 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1598 hostapd_clear_wep(hapd);
1599 hostapd_free_hapd_data(hapd);
1602 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1603 __func__, driver, drv_priv);
1604 if (driver && driver->hapd_deinit && drv_priv) {
1605 driver->hapd_deinit(drv_priv);
1606 hapd_iface->bss[0]->drv_priv = NULL;
1609 /* From hostapd_cleanup_iface: These were initialized in
1610 * hostapd_setup_interface and hostapd_setup_interface_complete
1612 hostapd_cleanup_iface_partial(hapd_iface);
1614 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1615 hapd_iface->bss[0]->conf->iface);
1616 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1621 static struct hostapd_iface *
1622 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1624 struct hostapd_iface **iface, *hapd_iface;
1626 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1627 sizeof(struct hostapd_iface *));
1630 interfaces->iface = iface;
1631 hapd_iface = interfaces->iface[interfaces->count] =
1632 os_zalloc(sizeof(*hapd_iface));
1633 if (hapd_iface == NULL) {
1634 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1635 "the interface", __func__);
1638 interfaces->count++;
1639 hapd_iface->interfaces = interfaces;
1645 static struct hostapd_config *
1646 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1647 const char *ctrl_iface)
1649 struct hostapd_bss_config *bss;
1650 struct hostapd_config *conf;
1652 /* Allocates memory for bss and conf */
1653 conf = hostapd_config_defaults();
1655 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1656 "configuration", __func__);
1660 conf->driver = wpa_drivers[0];
1661 if (conf->driver == NULL) {
1662 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1663 hostapd_config_free(conf);
1667 bss = conf->last_bss = conf->bss[0];
1669 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1670 bss->ctrl_interface = os_strdup(ctrl_iface);
1671 if (bss->ctrl_interface == NULL) {
1672 hostapd_config_free(conf);
1676 /* Reading configuration file skipped, will be done in SET!
1677 * From reading the configuration till the end has to be done in
1684 static struct hostapd_iface * hostapd_data_alloc(
1685 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1688 struct hostapd_iface *hapd_iface =
1689 interfaces->iface[interfaces->count - 1];
1690 struct hostapd_data *hapd;
1692 hapd_iface->conf = conf;
1693 hapd_iface->num_bss = conf->num_bss;
1695 hapd_iface->bss = os_zalloc(conf->num_bss *
1696 sizeof(struct hostapd_data *));
1697 if (hapd_iface->bss == NULL)
1700 for (i = 0; i < conf->num_bss; i++) {
1701 hapd = hapd_iface->bss[i] =
1702 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1705 hapd->msg_ctx = hapd;
1708 hapd_iface->interfaces = interfaces;
1714 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1716 struct hostapd_config *conf = NULL;
1717 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1718 struct hostapd_data *hapd;
1721 const char *conf_file = NULL, *phy_name = NULL;
1723 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1725 phy_name = buf + 11;
1726 pos = os_strchr(phy_name, ':');
1731 if (!os_strlen(conf_file))
1734 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1738 for (j = 0; j < interfaces->count; j++) {
1739 if (interfaces->iface[j] == hapd_iface)
1742 if (j == interfaces->count) {
1743 struct hostapd_iface **tmp;
1744 tmp = os_realloc_array(interfaces->iface,
1745 interfaces->count + 1,
1746 sizeof(struct hostapd_iface *));
1748 hostapd_interface_deinit_free(hapd_iface);
1751 interfaces->iface = tmp;
1752 interfaces->iface[interfaces->count++] = hapd_iface;
1753 new_iface = hapd_iface;
1757 if (interfaces->driver_init(hapd_iface) ||
1758 hostapd_setup_interface(hapd_iface)) {
1759 interfaces->count--;
1763 /* Assign new BSS with bss[0]'s driver info */
1764 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1765 hapd->driver = hapd_iface->bss[0]->driver;
1766 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1767 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1770 if (start_ctrl_iface_bss(hapd) < 0 ||
1771 hostapd_setup_bss(hapd, -1)) {
1772 hapd_iface->conf->num_bss--;
1773 hapd_iface->num_bss--;
1774 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1775 __func__, hapd, hapd->conf->iface);
1783 ptr = os_strchr(buf, ' ');
1788 if (os_strncmp(ptr, "config=", 7) == 0)
1789 conf_file = ptr + 7;
1791 for (i = 0; i < interfaces->count; i++) {
1792 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1794 wpa_printf(MSG_INFO, "Cannot add interface - it "
1800 hapd_iface = hostapd_iface_alloc(interfaces);
1801 if (hapd_iface == NULL) {
1802 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1803 "for interface", __func__);
1807 if (conf_file && interfaces->config_read_cb) {
1808 conf = interfaces->config_read_cb(conf_file);
1809 if (conf && conf->bss)
1810 os_strlcpy(conf->bss[0]->iface, buf,
1811 sizeof(conf->bss[0]->iface));
1813 conf = hostapd_config_alloc(interfaces, buf, ptr);
1814 if (conf == NULL || conf->bss == NULL) {
1815 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1816 "for configuration", __func__);
1820 hapd_iface = hostapd_data_alloc(interfaces, conf);
1821 if (hapd_iface == NULL) {
1822 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1823 "for hostapd", __func__);
1827 if (start_ctrl_iface(hapd_iface) < 0)
1830 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
1836 hostapd_config_free(conf);
1838 if (hapd_iface->bss) {
1839 for (i = 0; i < hapd_iface->num_bss; i++) {
1840 hapd = hapd_iface->bss[i];
1841 if (hapd && hapd_iface->interfaces &&
1842 hapd_iface->interfaces->ctrl_iface_deinit)
1843 hapd_iface->interfaces->
1844 ctrl_iface_deinit(hapd);
1845 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1846 __func__, hapd_iface->bss[i],
1847 hapd_iface->bss[i]->conf->iface);
1848 os_free(hapd_iface->bss[i]);
1850 os_free(hapd_iface->bss);
1852 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1853 __func__, hapd_iface);
1854 os_free(hapd_iface);
1860 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
1864 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
1866 /* Remove hostapd_data only if it has already been initialized */
1867 if (idx < iface->num_bss) {
1868 struct hostapd_data *hapd = iface->bss[idx];
1870 hostapd_bss_deinit(hapd);
1871 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1872 __func__, hapd, hapd->conf->iface);
1873 hostapd_config_free_bss(hapd->conf);
1878 for (i = idx; i < iface->num_bss; i++)
1879 iface->bss[i] = iface->bss[i + 1];
1881 hostapd_config_free_bss(iface->conf->bss[idx]);
1882 iface->conf->bss[idx] = NULL;
1885 iface->conf->num_bss--;
1886 for (i = idx; i < iface->conf->num_bss; i++)
1887 iface->conf->bss[i] = iface->conf->bss[i + 1];
1893 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1895 struct hostapd_iface *hapd_iface;
1898 for (i = 0; i < interfaces->count; i++) {
1899 hapd_iface = interfaces->iface[i];
1900 if (hapd_iface == NULL)
1902 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
1903 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1904 hostapd_interface_deinit_free(hapd_iface);
1906 while (k < (interfaces->count - 1)) {
1907 interfaces->iface[k] =
1908 interfaces->iface[k + 1];
1911 interfaces->count--;
1915 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
1916 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf))
1917 return hostapd_remove_bss(hapd_iface, j);
1925 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1926 * @hapd: Pointer to BSS data
1927 * @sta: Pointer to the associated STA data
1928 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1930 * This function will be called whenever a station associates with the AP. It
1931 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1932 * from drv_callbacks.c based on driver events for drivers that take care of
1933 * management frames (IEEE 802.11 authentication and association) internally.
1935 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1938 if (hapd->tkip_countermeasures) {
1939 hostapd_drv_sta_deauth(hapd, sta->addr,
1940 WLAN_REASON_MICHAEL_MIC_FAILURE);
1944 hostapd_prune_associations(hapd, sta->addr);
1946 /* IEEE 802.11F (IAPP) */
1947 if (hapd->conf->ieee802_11f)
1948 iapp_new_station(hapd->iapp, sta);
1951 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1952 sta->no_p2p_set = 1;
1953 hapd->num_sta_no_p2p++;
1954 if (hapd->num_sta_no_p2p == 1)
1955 hostapd_p2p_non_p2p_sta_connected(hapd);
1957 #endif /* CONFIG_P2P */
1959 /* Start accounting here, if IEEE 802.1X and WPA are not used.
1960 * IEEE 802.1X/WPA code will start accounting after the station has
1961 * been authorized. */
1962 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1963 os_get_time(&sta->connected_time);
1964 accounting_sta_start(hapd, sta);
1967 /* Start IEEE 802.1X authentication process for new stations */
1968 ieee802_1x_new_station(hapd, sta);
1970 if (sta->auth_alg != WLAN_AUTH_FT &&
1971 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1972 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1974 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
1976 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1977 "for " MACSTR " (%d seconds - ap_max_inactivity)",
1978 __func__, MAC2STR(sta->addr),
1979 hapd->conf->ap_max_inactivity);
1980 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1981 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1982 ap_handle_timer, hapd, sta);
1986 const char * hostapd_state_text(enum hostapd_iface_state s)
1989 case HAPD_IFACE_UNINITIALIZED:
1990 return "UNINITIALIZED";
1991 case HAPD_IFACE_DISABLED:
1993 case HAPD_IFACE_COUNTRY_UPDATE:
1994 return "COUNTRY_UPDATE";
1995 case HAPD_IFACE_ACS:
1997 case HAPD_IFACE_HT_SCAN:
1999 case HAPD_IFACE_DFS:
2001 case HAPD_IFACE_ENABLED:
2009 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2011 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2012 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2013 hostapd_state_text(s));