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 iapp_deinit(hapd->iapp);
248 accounting_deinit(hapd);
249 hostapd_deinit_wpa(hapd);
251 hostapd_acl_deinit(hapd);
252 #ifndef CONFIG_NO_RADIUS
253 radius_client_deinit(hapd->radius);
255 radius_das_deinit(hapd->radius_das);
256 hapd->radius_das = NULL;
257 #endif /* CONFIG_NO_RADIUS */
259 hostapd_deinit_wps(hapd);
261 authsrv_deinit(hapd);
263 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
264 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
268 os_free(hapd->probereq_cb);
269 hapd->probereq_cb = NULL;
272 wpabuf_free(hapd->p2p_beacon_ie);
273 hapd->p2p_beacon_ie = NULL;
274 wpabuf_free(hapd->p2p_probe_resp_ie);
275 hapd->p2p_probe_resp_ie = NULL;
276 #endif /* CONFIG_P2P */
278 wpabuf_free(hapd->time_adv);
280 #ifdef CONFIG_INTERWORKING
281 gas_serv_deinit(hapd);
282 #endif /* CONFIG_INTERWORKING */
285 os_free(hapd->tmp_eap_user.identity);
286 os_free(hapd->tmp_eap_user.password);
287 #endif /* CONFIG_SQLITE */
292 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
293 * @hapd: Pointer to BSS data
295 * This function is used to free all per-BSS data structures and resources.
296 * This gets called in a loop for each BSS between calls to
297 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
298 * is deinitialized. Most of the modules that are initialized in
299 * hostapd_setup_bss() are deinitialized here.
301 static void hostapd_cleanup(struct hostapd_data *hapd)
303 if (hapd->iface->interfaces &&
304 hapd->iface->interfaces->ctrl_iface_deinit)
305 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
306 hostapd_free_hapd_data(hapd);
311 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
312 * @iface: Pointer to interface data
314 * This function is called before per-BSS data structures are deinitialized
315 * with hostapd_cleanup().
317 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
322 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
324 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
325 iface->hw_features = NULL;
326 os_free(iface->current_rates);
327 iface->current_rates = NULL;
328 os_free(iface->basic_rates);
329 iface->basic_rates = NULL;
330 ap_list_deinit(iface);
335 * hostapd_cleanup_iface - Complete per-interface cleanup
336 * @iface: Pointer to interface data
338 * This function is called after per-BSS data structures are deinitialized
339 * with hostapd_cleanup().
341 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
343 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
345 hostapd_cleanup_iface_partial(iface);
346 hostapd_config_free(iface->conf);
349 os_free(iface->config_fname);
355 static void hostapd_clear_wep(struct hostapd_data *hapd)
357 if (hapd->drv_priv) {
358 hostapd_set_privacy(hapd, 0);
359 hostapd_broadcast_wep_clear(hapd);
364 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
368 hostapd_broadcast_wep_set(hapd);
370 if (hapd->conf->ssid.wep.default_len) {
371 hostapd_set_privacy(hapd, 1);
376 * When IEEE 802.1X is not enabled, the driver may need to know how to
377 * set authentication algorithms for static WEP.
379 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
381 for (i = 0; i < 4; i++) {
382 if (hapd->conf->ssid.wep.key[i] &&
383 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
384 i == hapd->conf->ssid.wep.idx, NULL, 0,
385 hapd->conf->ssid.wep.key[i],
386 hapd->conf->ssid.wep.len[i])) {
387 wpa_printf(MSG_WARNING, "Could not set WEP "
391 if (hapd->conf->ssid.wep.key[i] &&
392 i == hapd->conf->ssid.wep.idx)
393 hostapd_set_privacy(hapd, 1);
400 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
405 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
408 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
409 if (hostapd_flush(hapd)) {
410 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
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)
541 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
542 struct radius_das_attrs *attr)
544 struct sta_info *sta = NULL;
548 sta = ap_get_sta(hapd, attr->sta_addr);
550 if (sta == NULL && attr->acct_session_id &&
551 attr->acct_session_id_len == 17) {
552 for (sta = hapd->sta_list; sta; sta = sta->next) {
553 os_snprintf(buf, sizeof(buf), "%08X-%08X",
554 sta->acct_session_id_hi,
555 sta->acct_session_id_lo);
556 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
561 if (sta == NULL && attr->cui) {
562 for (sta = hapd->sta_list; sta; sta = sta->next) {
564 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
565 if (cui && wpabuf_len(cui) == attr->cui_len &&
566 os_memcmp(wpabuf_head(cui), attr->cui,
572 if (sta == NULL && attr->user_name) {
573 for (sta = hapd->sta_list; sta; sta = sta->next) {
576 identity = ieee802_1x_get_identity(sta->eapol_sm,
579 identity_len == attr->user_name_len &&
580 os_memcmp(identity, attr->user_name, identity_len)
590 static enum radius_das_res
591 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
593 struct hostapd_data *hapd = ctx;
594 struct sta_info *sta;
596 if (hostapd_das_nas_mismatch(hapd, attr))
597 return RADIUS_DAS_NAS_MISMATCH;
599 sta = hostapd_das_find_sta(hapd, attr);
601 return RADIUS_DAS_SESSION_NOT_FOUND;
603 hostapd_drv_sta_deauth(hapd, sta->addr,
604 WLAN_REASON_PREV_AUTH_NOT_VALID);
605 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
607 return RADIUS_DAS_SUCCESS;
610 #endif /* CONFIG_NO_RADIUS */
614 * hostapd_setup_bss - Per-BSS setup (initialization)
615 * @hapd: Pointer to BSS data
616 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
617 * but interface may exist
619 * This function is used to initialize all per-BSS data structures and
620 * resources. This gets called in a loop for each BSS when an interface is
621 * initialized. Most of the modules that are initialized here will be
622 * deinitialized in hostapd_cleanup().
624 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
626 struct hostapd_bss_config *conf = hapd->conf;
627 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
628 int ssid_len, set_ssid;
629 char force_ifname[IFNAMSIZ];
630 u8 if_addr[ETH_ALEN];
632 if (!first || first == -1) {
633 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
634 /* Allocate the next available BSSID. */
636 inc_byte_array(hapd->own_addr, ETH_ALEN);
637 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
639 /* Allocate the configured BSSID. */
640 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
642 if (hostapd_mac_comp(hapd->own_addr,
643 hapd->iface->bss[0]->own_addr) ==
645 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
646 "BSSID set to the MAC address of "
647 "the radio", hapd->conf->iface);
652 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
653 hapd->conf->iface, hapd->own_addr, hapd,
654 &hapd->drv_priv, force_ifname, if_addr,
655 hapd->conf->bridge[0] ? hapd->conf->bridge :
656 NULL, first == -1)) {
657 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
658 MACSTR ")", MAC2STR(hapd->own_addr));
663 if (conf->wmm_enabled < 0)
664 conf->wmm_enabled = hapd->iconf->ieee80211n;
666 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
667 hostapd_set_privacy(hapd, 0);
669 hostapd_broadcast_wep_clear(hapd);
670 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
674 * Fetch the SSID from the system and use it or,
675 * if one was specified in the config file, verify they
678 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
680 wpa_printf(MSG_ERROR, "Could not read SSID from system");
683 if (conf->ssid.ssid_set) {
685 * If SSID is specified in the config file and it differs
686 * from what is being used then force installation of the
689 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
690 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
693 * No SSID in the config file; just use the one we got
697 conf->ssid.ssid_len = ssid_len;
698 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
701 if (!hostapd_drv_none(hapd)) {
702 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
704 hapd->conf->iface, MAC2STR(hapd->own_addr),
705 wpa_ssid_txt(hapd->conf->ssid.ssid,
706 hapd->conf->ssid.ssid_len));
709 if (hostapd_setup_wpa_psk(conf)) {
710 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
714 /* Set SSID for the kernel driver (to be used in beacon and probe
715 * response frames) */
716 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
717 conf->ssid.ssid_len)) {
718 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
722 if (wpa_debug_level == MSG_MSGDUMP)
723 conf->radius->msg_dumps = 1;
724 #ifndef CONFIG_NO_RADIUS
725 hapd->radius = radius_client_init(hapd, conf->radius);
726 if (hapd->radius == NULL) {
727 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
731 if (hapd->conf->radius_das_port) {
732 struct radius_das_conf das_conf;
733 os_memset(&das_conf, 0, sizeof(das_conf));
734 das_conf.port = hapd->conf->radius_das_port;
735 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
736 das_conf.shared_secret_len =
737 hapd->conf->radius_das_shared_secret_len;
738 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
739 das_conf.time_window = hapd->conf->radius_das_time_window;
740 das_conf.require_event_timestamp =
741 hapd->conf->radius_das_require_event_timestamp;
743 das_conf.disconnect = hostapd_das_disconnect;
744 hapd->radius_das = radius_das_init(&das_conf);
745 if (hapd->radius_das == NULL) {
746 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
751 #endif /* CONFIG_NO_RADIUS */
753 if (hostapd_acl_init(hapd)) {
754 wpa_printf(MSG_ERROR, "ACL initialization failed.");
757 if (hostapd_init_wps(hapd, conf))
760 if (authsrv_init(hapd) < 0)
763 if (ieee802_1x_init(hapd)) {
764 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
768 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
771 if (accounting_init(hapd)) {
772 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
776 if (hapd->conf->ieee802_11f &&
777 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
778 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
783 #ifdef CONFIG_INTERWORKING
784 if (gas_serv_init(hapd)) {
785 wpa_printf(MSG_ERROR, "GAS server initialization failed");
789 if (conf->qos_map_set_len &&
790 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
791 conf->qos_map_set_len)) {
792 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
795 #endif /* CONFIG_INTERWORKING */
797 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
798 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
802 if (!hapd->conf->start_disabled)
803 ieee802_11_set_beacon(hapd);
805 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
808 if (hapd->driver && hapd->driver->set_operstate)
809 hapd->driver->set_operstate(hapd->drv_priv, 1);
815 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
817 struct hostapd_data *hapd = iface->bss[0];
819 struct hostapd_tx_queue_params *p;
821 for (i = 0; i < NUM_TX_QUEUES; i++) {
822 p = &iface->conf->tx_queue[i];
824 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
825 p->cwmax, p->burst)) {
826 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
827 "parameters for queue %d.", i);
828 /* Continue anyway */
834 static int hostapd_set_acl_list(struct hostapd_data *hapd,
835 struct mac_acl_entry *mac_acl,
836 int n_entries, u8 accept_acl)
838 struct hostapd_acl_params *acl_params;
841 acl_params = os_zalloc(sizeof(*acl_params) +
842 (n_entries * sizeof(acl_params->mac_acl[0])));
846 for (i = 0; i < n_entries; i++)
847 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
850 acl_params->acl_policy = accept_acl;
851 acl_params->num_mac_acl = n_entries;
853 err = hostapd_drv_set_acl(hapd, acl_params);
861 static void hostapd_set_acl(struct hostapd_data *hapd)
863 struct hostapd_config *conf = hapd->iconf;
867 if (hapd->iface->drv_max_acl_mac_addrs == 0)
869 if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
872 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
873 if (conf->bss[0]->num_accept_mac) {
875 err = hostapd_set_acl_list(hapd,
876 conf->bss[0]->accept_mac,
877 conf->bss[0]->num_accept_mac,
880 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
884 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
886 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
887 if (conf->bss[0]->num_deny_mac) {
889 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
890 conf->bss[0]->num_deny_mac,
893 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
897 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
903 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
905 if (!hapd->iface->interfaces ||
906 !hapd->iface->interfaces->ctrl_iface_init)
909 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
910 wpa_printf(MSG_ERROR,
911 "Failed to setup control interface for %s",
920 static int start_ctrl_iface(struct hostapd_iface *iface)
924 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
927 for (i = 0; i < iface->num_bss; i++) {
928 struct hostapd_data *hapd = iface->bss[i];
929 if (iface->interfaces->ctrl_iface_init(hapd)) {
930 wpa_printf(MSG_ERROR,
931 "Failed to setup control interface for %s",
941 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
943 struct hostapd_iface *iface = eloop_ctx;
945 if (!iface->wait_channel_update) {
946 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
951 * It is possible that the existing channel list is acceptable, so try
954 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
955 setup_interface2(iface);
959 void hostapd_channel_list_updated(struct hostapd_iface *iface)
961 if (!iface->wait_channel_update)
964 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
965 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
966 setup_interface2(iface);
970 static int setup_interface(struct hostapd_iface *iface)
972 struct hostapd_data *hapd = iface->bss[0];
975 if (!iface->phy[0]) {
976 const char *phy = hostapd_drv_get_radio_name(hapd);
978 wpa_printf(MSG_DEBUG, "phy: %s", phy);
979 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
984 * Make sure that all BSSes get configured with a pointer to the same
987 for (i = 1; i < iface->num_bss; i++) {
988 iface->bss[i]->driver = hapd->driver;
989 iface->bss[i]->drv_priv = hapd->drv_priv;
992 if (hostapd_validate_bssid_configuration(iface))
996 * Initialize control interfaces early to allow external monitoring of
997 * channel setup operations that may take considerable amount of time
998 * especially for DFS cases.
1000 if (start_ctrl_iface(iface))
1003 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1004 char country[4], previous_country[4];
1006 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1007 if (hostapd_get_country(hapd, previous_country) < 0)
1008 previous_country[0] = '\0';
1010 os_memcpy(country, hapd->iconf->country, 3);
1012 if (hostapd_set_country(hapd, country) < 0) {
1013 wpa_printf(MSG_ERROR, "Failed to set country code");
1017 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1018 previous_country, country);
1020 if (os_strncmp(previous_country, country, 2) != 0) {
1021 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1022 iface->wait_channel_update = 1;
1023 eloop_register_timeout(1, 0,
1024 channel_list_update_timeout,
1030 return setup_interface2(iface);
1034 static int setup_interface2(struct hostapd_iface *iface)
1036 iface->wait_channel_update = 0;
1038 if (hostapd_get_hw_features(iface)) {
1039 /* Not all drivers support this yet, so continue without hw
1042 int ret = hostapd_select_hw_mode(iface);
1044 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1045 "channel. (%d)", ret);
1049 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1052 ret = hostapd_check_ht_capab(iface);
1056 wpa_printf(MSG_DEBUG, "Interface initialization will "
1057 "be completed in a callback");
1061 if (iface->conf->ieee80211h)
1062 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1064 return hostapd_setup_interface_complete(iface, 0);
1069 * hostapd_setup_interface_complete - Complete interface setup
1071 * This function is called when previous steps in the interface setup has been
1072 * completed. This can also start operations, e.g., DFS, that will require
1073 * additional processing before interface is ready to be enabled. Such
1074 * operations will call this function from eloop callbacks when finished.
1076 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1078 struct hostapd_data *hapd = iface->bss[0];
1083 wpa_printf(MSG_ERROR, "Interface initialization failed");
1084 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1085 if (iface->interfaces && iface->interfaces->terminate_on_error)
1090 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1091 if (iface->conf->channel) {
1094 #endif /* NEED_AP_MLME */
1096 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1097 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1098 "Frequency: %d MHz",
1099 hostapd_hw_mode_txt(iface->conf->hw_mode),
1100 iface->conf->channel, iface->freq);
1104 res = hostapd_handle_dfs(iface);
1107 #endif /* NEED_AP_MLME */
1109 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1110 hapd->iconf->channel,
1111 hapd->iconf->ieee80211n,
1112 hapd->iconf->ieee80211ac,
1113 hapd->iconf->secondary_channel,
1114 hapd->iconf->vht_oper_chwidth,
1115 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1116 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1117 wpa_printf(MSG_ERROR, "Could not set channel for "
1123 if (iface->current_mode) {
1124 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1125 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1127 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1128 HOSTAPD_LEVEL_WARNING,
1129 "Failed to prepare rates table.");
1134 if (hapd->iconf->rts_threshold > -1 &&
1135 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1136 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1141 if (hapd->iconf->fragm_threshold > -1 &&
1142 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1143 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1144 "for kernel driver");
1148 prev_addr = hapd->own_addr;
1150 for (j = 0; j < iface->num_bss; j++) {
1151 hapd = iface->bss[j];
1153 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1154 if (hostapd_setup_bss(hapd, j == 0))
1156 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1157 prev_addr = hapd->own_addr;
1159 hapd = iface->bss[0];
1161 hostapd_tx_queue_params(iface);
1163 ap_list_init(iface);
1165 hostapd_set_acl(hapd);
1167 if (hostapd_driver_commit(hapd) < 0) {
1168 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1169 "configuration", __func__);
1174 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1175 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1176 * mode), the interface is up only after driver_commit, so initialize
1177 * WPS after driver_commit.
1179 for (j = 0; j < iface->num_bss; j++) {
1180 if (hostapd_init_wps_complete(iface->bss[j]))
1184 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1185 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1186 if (hapd->setup_complete_cb)
1187 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1189 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1190 iface->bss[0]->conf->iface);
1191 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1192 iface->interfaces->terminate_on_error--;
1199 * hostapd_setup_interface - Setup of an interface
1200 * @iface: Pointer to interface data.
1201 * Returns: 0 on success, -1 on failure
1203 * Initializes the driver interface, validates the configuration,
1204 * and sets driver parameters based on the configuration.
1205 * Flushes old stations, sets the channel, encryption,
1206 * beacons, and WDS links based on the configuration.
1208 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1209 * or DFS operations, this function returns 0 before such operations have been
1210 * completed. The pending operations are registered into eloop and will be
1211 * completed from eloop callbacks. Those callbacks end up calling
1212 * hostapd_setup_interface_complete() once setup has been completed.
1214 int hostapd_setup_interface(struct hostapd_iface *iface)
1218 ret = setup_interface(iface);
1220 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1221 iface->bss[0]->conf->iface);
1230 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1231 * @hapd_iface: Pointer to interface data
1232 * @conf: Pointer to per-interface configuration
1233 * @bss: Pointer to per-BSS configuration for this BSS
1234 * Returns: Pointer to allocated BSS data
1236 * This function is used to allocate per-BSS data structure. This data will be
1237 * freed after hostapd_cleanup() is called for it during interface
1240 struct hostapd_data *
1241 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1242 struct hostapd_config *conf,
1243 struct hostapd_bss_config *bss)
1245 struct hostapd_data *hapd;
1247 hapd = os_zalloc(sizeof(*hapd));
1251 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1254 hapd->iface = hapd_iface;
1255 hapd->driver = hapd->iconf->driver;
1256 hapd->ctrl_sock = -1;
1262 void hostapd_interface_deinit(struct hostapd_iface *iface)
1269 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1270 iface->wait_channel_update = 0;
1272 hostapd_cleanup_iface_pre(iface);
1273 for (j = iface->num_bss - 1; j >= 0; j--) {
1274 struct hostapd_data *hapd = iface->bss[j];
1275 hostapd_free_stas(hapd);
1276 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1277 hostapd_clear_wep(hapd);
1278 hostapd_cleanup(hapd);
1283 void hostapd_interface_free(struct hostapd_iface *iface)
1286 for (j = 0; j < iface->num_bss; j++)
1287 os_free(iface->bss[j]);
1288 hostapd_cleanup_iface(iface);
1293 * hostapd_init - Allocate and initialize per-interface data
1294 * @config_file: Path to the configuration file
1295 * Returns: Pointer to the allocated interface data or %NULL on failure
1297 * This function is used to allocate main data structures for per-interface
1298 * data. The allocated data buffer will be freed by calling
1299 * hostapd_cleanup_iface().
1301 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1302 const char *config_file)
1304 struct hostapd_iface *hapd_iface = NULL;
1305 struct hostapd_config *conf = NULL;
1306 struct hostapd_data *hapd;
1309 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1310 if (hapd_iface == NULL)
1313 hapd_iface->config_fname = os_strdup(config_file);
1314 if (hapd_iface->config_fname == NULL)
1317 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1320 hapd_iface->conf = conf;
1322 hapd_iface->num_bss = conf->num_bss;
1323 hapd_iface->bss = os_calloc(conf->num_bss,
1324 sizeof(struct hostapd_data *));
1325 if (hapd_iface->bss == NULL)
1328 for (i = 0; i < conf->num_bss; i++) {
1329 hapd = hapd_iface->bss[i] =
1330 hostapd_alloc_bss_data(hapd_iface, conf,
1334 hapd->msg_ctx = hapd;
1340 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1343 hostapd_config_free(conf);
1345 os_free(hapd_iface->config_fname);
1346 os_free(hapd_iface->bss);
1347 os_free(hapd_iface);
1353 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1357 for (i = 0; i < interfaces->count; i++) {
1358 struct hostapd_iface *iface = interfaces->iface[i];
1359 for (j = 0; j < iface->num_bss; j++) {
1360 struct hostapd_data *hapd = iface->bss[j];
1361 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1371 * hostapd_interface_init_bss - Read configuration file and init BSS data
1373 * This function is used to parse configuration file for a BSS. This BSS is
1374 * added to an existing interface sharing the same radio (if any) or a new
1375 * interface is created if this is the first interface on a radio. This
1376 * allocate memory for the BSS. No actual driver operations are started.
1378 * This is similar to hostapd_interface_init(), but for a case where the
1379 * configuration is used to add a single BSS instead of all BSSes for a radio.
1381 struct hostapd_iface *
1382 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1383 const char *config_fname, int debug)
1385 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1386 struct hostapd_data *hapd;
1393 for (i = 0; i < interfaces->count; i++) {
1394 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1395 iface = interfaces->iface[i];
1400 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1401 config_fname, phy, iface ? "" : " --> new PHY");
1403 struct hostapd_config *conf;
1404 struct hostapd_bss_config **tmp_conf;
1405 struct hostapd_data **tmp_bss;
1406 struct hostapd_bss_config *bss;
1409 /* Add new BSS to existing iface */
1410 conf = interfaces->config_read_cb(config_fname);
1413 if (conf->num_bss > 1) {
1414 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1415 hostapd_config_free(conf);
1419 ifname = conf->bss[0]->iface;
1420 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1421 wpa_printf(MSG_ERROR,
1422 "Interface name %s already in use", ifname);
1423 hostapd_config_free(conf);
1427 tmp_conf = os_realloc_array(
1428 iface->conf->bss, iface->conf->num_bss + 1,
1429 sizeof(struct hostapd_bss_config *));
1430 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1431 sizeof(struct hostapd_data *));
1433 iface->bss = tmp_bss;
1435 iface->conf->bss = tmp_conf;
1436 iface->conf->last_bss = tmp_conf[0];
1438 if (tmp_bss == NULL || tmp_conf == NULL) {
1439 hostapd_config_free(conf);
1442 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1443 iface->conf->num_bss++;
1445 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1447 iface->conf->num_bss--;
1448 hostapd_config_free(conf);
1451 iface->conf->last_bss = bss;
1452 iface->bss[iface->num_bss] = hapd;
1453 hapd->msg_ctx = hapd;
1455 bss_idx = iface->num_bss++;
1457 conf->bss[0] = NULL;
1458 hostapd_config_free(conf);
1460 /* Add a new iface with the first BSS */
1461 new_iface = iface = hostapd_init(interfaces, config_fname);
1464 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1465 iface->interfaces = interfaces;
1469 for (k = 0; k < debug; k++) {
1470 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1471 iface->bss[bss_idx]->conf->logger_stdout_level--;
1474 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1475 !hostapd_drv_none(iface->bss[bss_idx])) {
1476 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1479 hostapd_interface_deinit_free(new_iface);
1487 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1489 const struct wpa_driver_ops *driver;
1493 driver = iface->bss[0]->driver;
1494 drv_priv = iface->bss[0]->drv_priv;
1495 hostapd_interface_deinit(iface);
1496 if (driver && driver->hapd_deinit && drv_priv)
1497 driver->hapd_deinit(drv_priv);
1498 hostapd_interface_free(iface);
1502 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1504 if (hapd_iface->bss[0]->drv_priv != NULL) {
1505 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1506 hapd_iface->conf->bss[0]->iface);
1510 wpa_printf(MSG_DEBUG, "Enable interface %s",
1511 hapd_iface->conf->bss[0]->iface);
1513 if (hapd_iface->interfaces == NULL ||
1514 hapd_iface->interfaces->driver_init == NULL ||
1515 hapd_iface->interfaces->driver_init(hapd_iface))
1518 if (hostapd_setup_interface(hapd_iface)) {
1519 const struct wpa_driver_ops *driver;
1522 driver = hapd_iface->bss[0]->driver;
1523 drv_priv = hapd_iface->bss[0]->drv_priv;
1524 if (driver && driver->hapd_deinit && drv_priv) {
1525 driver->hapd_deinit(drv_priv);
1526 hapd_iface->bss[0]->drv_priv = NULL;
1535 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1539 wpa_printf(MSG_DEBUG, "Reload interface %s",
1540 hapd_iface->conf->bss[0]->iface);
1541 for (j = 0; j < hapd_iface->num_bss; j++)
1542 hostapd_set_security_params(hapd_iface->conf->bss[j]);
1543 if (hostapd_config_check(hapd_iface->conf) < 0) {
1544 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1547 hostapd_clear_old(hapd_iface);
1548 for (j = 0; j < hapd_iface->num_bss; j++)
1549 hostapd_reload_bss(hapd_iface->bss[j]);
1555 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1558 const struct wpa_driver_ops *driver;
1561 if (hapd_iface == NULL)
1563 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1564 driver = hapd_iface->bss[0]->driver;
1565 drv_priv = hapd_iface->bss[0]->drv_priv;
1567 /* whatever hostapd_interface_deinit does */
1568 for (j = 0; j < hapd_iface->num_bss; j++) {
1569 struct hostapd_data *hapd = hapd_iface->bss[j];
1570 hostapd_free_stas(hapd);
1571 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1572 hostapd_clear_wep(hapd);
1573 hostapd_free_hapd_data(hapd);
1576 if (driver && driver->hapd_deinit && drv_priv) {
1577 driver->hapd_deinit(drv_priv);
1578 hapd_iface->bss[0]->drv_priv = NULL;
1581 /* From hostapd_cleanup_iface: These were initialized in
1582 * hostapd_setup_interface and hostapd_setup_interface_complete
1584 hostapd_cleanup_iface_partial(hapd_iface);
1586 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1587 hapd_iface->bss[0]->conf->iface);
1588 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1593 static struct hostapd_iface *
1594 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1596 struct hostapd_iface **iface, *hapd_iface;
1598 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1599 sizeof(struct hostapd_iface *));
1602 interfaces->iface = iface;
1603 hapd_iface = interfaces->iface[interfaces->count] =
1604 os_zalloc(sizeof(*hapd_iface));
1605 if (hapd_iface == NULL) {
1606 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1607 "the interface", __func__);
1610 interfaces->count++;
1611 hapd_iface->interfaces = interfaces;
1617 static struct hostapd_config *
1618 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1619 const char *ctrl_iface)
1621 struct hostapd_bss_config *bss;
1622 struct hostapd_config *conf;
1624 /* Allocates memory for bss and conf */
1625 conf = hostapd_config_defaults();
1627 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1628 "configuration", __func__);
1632 conf->driver = wpa_drivers[0];
1633 if (conf->driver == NULL) {
1634 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1635 hostapd_config_free(conf);
1639 bss = conf->last_bss = conf->bss[0];
1641 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1642 bss->ctrl_interface = os_strdup(ctrl_iface);
1643 if (bss->ctrl_interface == NULL) {
1644 hostapd_config_free(conf);
1648 /* Reading configuration file skipped, will be done in SET!
1649 * From reading the configuration till the end has to be done in
1656 static struct hostapd_iface * hostapd_data_alloc(
1657 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1660 struct hostapd_iface *hapd_iface =
1661 interfaces->iface[interfaces->count - 1];
1662 struct hostapd_data *hapd;
1664 hapd_iface->conf = conf;
1665 hapd_iface->num_bss = conf->num_bss;
1667 hapd_iface->bss = os_zalloc(conf->num_bss *
1668 sizeof(struct hostapd_data *));
1669 if (hapd_iface->bss == NULL)
1672 for (i = 0; i < conf->num_bss; i++) {
1673 hapd = hapd_iface->bss[i] =
1674 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1677 hapd->msg_ctx = hapd;
1680 hapd_iface->interfaces = interfaces;
1686 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1688 struct hostapd_config *conf = NULL;
1689 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1690 struct hostapd_data *hapd;
1693 const char *conf_file = NULL, *phy_name = NULL;
1695 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1697 phy_name = buf + 11;
1698 pos = os_strchr(phy_name, ':');
1703 if (!os_strlen(conf_file))
1706 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1710 for (j = 0; j < interfaces->count; j++) {
1711 if (interfaces->iface[j] == hapd_iface)
1714 if (j == interfaces->count) {
1715 struct hostapd_iface **tmp;
1716 tmp = os_realloc_array(interfaces->iface,
1717 interfaces->count + 1,
1718 sizeof(struct hostapd_iface *));
1720 hostapd_interface_deinit_free(hapd_iface);
1723 interfaces->iface = tmp;
1724 interfaces->iface[interfaces->count++] = hapd_iface;
1725 new_iface = hapd_iface;
1729 if (interfaces->driver_init(hapd_iface) ||
1730 hostapd_setup_interface(hapd_iface)) {
1731 interfaces->count--;
1735 /* Assign new BSS with bss[0]'s driver info */
1736 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1737 hapd->driver = hapd_iface->bss[0]->driver;
1738 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1739 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1742 if (start_ctrl_iface_bss(hapd) < 0 ||
1743 hostapd_setup_bss(hapd, -1)) {
1744 hapd_iface->conf->num_bss--;
1745 hapd_iface->num_bss--;
1753 ptr = os_strchr(buf, ' ');
1758 if (os_strncmp(ptr, "config=", 7) == 0)
1759 conf_file = ptr + 7;
1761 for (i = 0; i < interfaces->count; i++) {
1762 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1764 wpa_printf(MSG_INFO, "Cannot add interface - it "
1770 hapd_iface = hostapd_iface_alloc(interfaces);
1771 if (hapd_iface == NULL) {
1772 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1773 "for interface", __func__);
1777 if (conf_file && interfaces->config_read_cb) {
1778 conf = interfaces->config_read_cb(conf_file);
1779 if (conf && conf->bss)
1780 os_strlcpy(conf->bss[0]->iface, buf,
1781 sizeof(conf->bss[0]->iface));
1783 conf = hostapd_config_alloc(interfaces, buf, ptr);
1784 if (conf == NULL || conf->bss == NULL) {
1785 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1786 "for configuration", __func__);
1790 hapd_iface = hostapd_data_alloc(interfaces, conf);
1791 if (hapd_iface == NULL) {
1792 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1793 "for hostapd", __func__);
1797 if (start_ctrl_iface(hapd_iface) < 0)
1800 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
1806 hostapd_config_free(conf);
1808 if (hapd_iface->bss) {
1809 for (i = 0; i < hapd_iface->num_bss; i++)
1810 os_free(hapd_iface->bss[i]);
1811 os_free(hapd_iface->bss);
1813 os_free(hapd_iface);
1819 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
1821 struct hostapd_data *hapd;
1824 if (idx > iface->num_bss || idx > iface->conf->num_bss)
1826 hapd = iface->bss[idx];
1827 wpa_printf(MSG_INFO, "Remove BSS '%s'", hapd->conf->iface);
1829 hostapd_free_stas(hapd);
1830 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1831 hostapd_clear_wep(hapd);
1832 hostapd_cleanup(hapd);
1833 hostapd_config_free_bss(hapd->conf);
1837 for (i = idx; i < iface->num_bss; i++)
1838 iface->bss[i] = iface->bss[i + 1];
1840 iface->conf->num_bss--;
1841 for (i = idx; i < iface->num_bss; i++)
1842 iface->conf->bss[i] = iface->conf->bss[i + 1];
1848 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1850 struct hostapd_iface *hapd_iface;
1853 for (i = 0; i < interfaces->count; i++) {
1854 hapd_iface = interfaces->iface[i];
1855 if (hapd_iface == NULL)
1857 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
1858 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1859 hostapd_interface_deinit_free(hapd_iface);
1861 while (k < (interfaces->count - 1)) {
1862 interfaces->iface[k] =
1863 interfaces->iface[k + 1];
1866 interfaces->count--;
1870 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
1871 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf))
1872 return hostapd_remove_bss(hapd_iface, j);
1880 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1881 * @hapd: Pointer to BSS data
1882 * @sta: Pointer to the associated STA data
1883 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1885 * This function will be called whenever a station associates with the AP. It
1886 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1887 * from drv_callbacks.c based on driver events for drivers that take care of
1888 * management frames (IEEE 802.11 authentication and association) internally.
1890 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1893 if (hapd->tkip_countermeasures) {
1894 hostapd_drv_sta_deauth(hapd, sta->addr,
1895 WLAN_REASON_MICHAEL_MIC_FAILURE);
1899 hostapd_prune_associations(hapd, sta->addr);
1901 /* IEEE 802.11F (IAPP) */
1902 if (hapd->conf->ieee802_11f)
1903 iapp_new_station(hapd->iapp, sta);
1906 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1907 sta->no_p2p_set = 1;
1908 hapd->num_sta_no_p2p++;
1909 if (hapd->num_sta_no_p2p == 1)
1910 hostapd_p2p_non_p2p_sta_connected(hapd);
1912 #endif /* CONFIG_P2P */
1914 /* Start accounting here, if IEEE 802.1X and WPA are not used.
1915 * IEEE 802.1X/WPA code will start accounting after the station has
1916 * been authorized. */
1917 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1918 os_get_time(&sta->connected_time);
1919 accounting_sta_start(hapd, sta);
1922 /* Start IEEE 802.1X authentication process for new stations */
1923 ieee802_1x_new_station(hapd, sta);
1925 if (sta->auth_alg != WLAN_AUTH_FT &&
1926 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1927 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1929 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
1931 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1932 "for " MACSTR " (%d seconds - ap_max_inactivity)",
1933 __func__, MAC2STR(sta->addr),
1934 hapd->conf->ap_max_inactivity);
1935 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1936 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1937 ap_handle_timer, hapd, sta);
1941 const char * hostapd_state_text(enum hostapd_iface_state s)
1944 case HAPD_IFACE_UNINITIALIZED:
1945 return "UNINITIALIZED";
1946 case HAPD_IFACE_DISABLED:
1948 case HAPD_IFACE_COUNTRY_UPDATE:
1949 return "COUNTRY_UPDATE";
1950 case HAPD_IFACE_ACS:
1952 case HAPD_IFACE_HT_SCAN:
1954 case HAPD_IFACE_DFS:
1956 case HAPD_IFACE_ENABLED:
1964 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
1966 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
1967 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
1968 hostapd_state_text(s));