2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "radius/radius_client.h"
16 #include "radius/radius_das.h"
20 #include "accounting.h"
24 #include "ieee802_1x.h"
25 #include "ieee802_11_auth.h"
26 #include "vlan_init.h"
28 #include "wps_hostapd.h"
29 #include "hw_features.h"
30 #include "wpa_auth_glue.h"
31 #include "ap_drv_ops.h"
32 #include "ap_config.h"
33 #include "p2p_hostapd.h"
36 #include "ieee802_11.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);
46 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
47 int (*cb)(struct hostapd_iface *iface,
48 void *ctx), void *ctx)
53 for (i = 0; i < interfaces->count; i++) {
54 ret = cb(interfaces->iface[i], ctx);
63 static void hostapd_reload_bss(struct hostapd_data *hapd)
65 struct hostapd_ssid *ssid;
67 #ifndef CONFIG_NO_RADIUS
68 radius_client_reconfig(hapd->radius, hapd->conf->radius);
69 #endif /* CONFIG_NO_RADIUS */
71 ssid = &hapd->conf->ssid;
72 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
73 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
75 * Force PSK to be derived again since SSID or passphrase may
78 os_free(ssid->wpa_psk);
81 if (hostapd_setup_wpa_psk(hapd->conf)) {
82 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
83 "after reloading configuration");
86 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
87 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
89 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
91 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
92 hostapd_setup_wpa(hapd);
94 wpa_init_keys(hapd->wpa_auth);
95 } else if (hapd->conf->wpa) {
98 hostapd_reconfig_wpa(hapd);
99 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
100 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
101 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
102 "the kernel driver.");
103 } else if (hapd->wpa_auth) {
104 wpa_deinit(hapd->wpa_auth);
105 hapd->wpa_auth = NULL;
106 hostapd_set_privacy(hapd, 0);
107 hostapd_setup_encryption(hapd->conf->iface, hapd);
108 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
111 ieee802_11_set_beacon(hapd);
112 hostapd_update_wps(hapd);
114 if (hapd->conf->ssid.ssid_set &&
115 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
116 hapd->conf->ssid.ssid_len)) {
117 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
118 /* try to continue */
120 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
124 static void hostapd_clear_old(struct hostapd_iface *iface)
129 * Deauthenticate all stations since the new configuration may not
130 * allow them to use the BSS anymore.
132 for (j = 0; j < iface->num_bss; j++) {
133 hostapd_flush_old_stations(iface->bss[j],
134 WLAN_REASON_PREV_AUTH_NOT_VALID);
135 hostapd_broadcast_wep_clear(iface->bss[j]);
137 #ifndef CONFIG_NO_RADIUS
138 /* TODO: update dynamic data based on changed configuration
139 * items (e.g., open/close sockets, etc.) */
140 radius_client_flush(iface->bss[j]->radius, 0);
141 #endif /* CONFIG_NO_RADIUS */
146 int hostapd_reload_config(struct hostapd_iface *iface)
148 struct hostapd_data *hapd = iface->bss[0];
149 struct hostapd_config *newconf, *oldconf;
152 if (iface->config_fname == NULL) {
153 /* Only in-memory config in use - assume it has been updated */
154 hostapd_clear_old(iface);
155 for (j = 0; j < iface->num_bss; j++)
156 hostapd_reload_bss(iface->bss[j]);
160 if (iface->interfaces == NULL ||
161 iface->interfaces->config_read_cb == NULL)
163 newconf = iface->interfaces->config_read_cb(iface->config_fname);
167 hostapd_clear_old(iface);
169 oldconf = hapd->iconf;
170 iface->conf = newconf;
172 for (j = 0; j < iface->num_bss; j++) {
173 hapd = iface->bss[j];
174 hapd->iconf = newconf;
175 hapd->iconf->channel = oldconf->channel;
176 hapd->iconf->secondary_channel = oldconf->secondary_channel;
177 hapd->iconf->ieee80211n = oldconf->ieee80211n;
178 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
179 hapd->iconf->ht_capab = oldconf->ht_capab;
180 hapd->iconf->vht_capab = oldconf->vht_capab;
181 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
182 hapd->iconf->vht_oper_centr_freq_seg0_idx =
183 oldconf->vht_oper_centr_freq_seg0_idx;
184 hapd->iconf->vht_oper_centr_freq_seg1_idx =
185 oldconf->vht_oper_centr_freq_seg1_idx;
186 hapd->conf = newconf->bss[j];
187 hostapd_reload_bss(hapd);
190 hostapd_config_free(oldconf);
197 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
202 for (i = 0; i < NUM_WEP_KEYS; i++) {
203 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
204 0, NULL, 0, NULL, 0)) {
205 wpa_printf(MSG_DEBUG, "Failed to clear default "
206 "encryption keys (ifname=%s keyidx=%d)",
210 #ifdef CONFIG_IEEE80211W
211 if (hapd->conf->ieee80211w) {
212 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
213 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
216 wpa_printf(MSG_DEBUG, "Failed to clear "
217 "default mgmt encryption keys "
218 "(ifname=%s keyidx=%d)", ifname, i);
222 #endif /* CONFIG_IEEE80211W */
226 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
228 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
233 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
236 struct hostapd_ssid *ssid = &hapd->conf->ssid;
239 if (ssid->wep.default_len &&
240 hostapd_drv_set_key(hapd->conf->iface,
241 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
242 1, NULL, 0, ssid->wep.key[idx],
243 ssid->wep.len[idx])) {
244 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
252 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
254 if (!hapd->started) {
255 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
256 __func__, hapd->conf->iface);
261 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
262 iapp_deinit(hapd->iapp);
264 accounting_deinit(hapd);
265 hostapd_deinit_wpa(hapd);
267 hostapd_acl_deinit(hapd);
268 #ifndef CONFIG_NO_RADIUS
269 radius_client_deinit(hapd->radius);
271 radius_das_deinit(hapd->radius_das);
272 hapd->radius_das = NULL;
273 #endif /* CONFIG_NO_RADIUS */
275 hostapd_deinit_wps(hapd);
277 authsrv_deinit(hapd);
279 if (hapd->interface_added &&
280 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
281 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
285 os_free(hapd->probereq_cb);
286 hapd->probereq_cb = NULL;
289 wpabuf_free(hapd->p2p_beacon_ie);
290 hapd->p2p_beacon_ie = NULL;
291 wpabuf_free(hapd->p2p_probe_resp_ie);
292 hapd->p2p_probe_resp_ie = NULL;
293 #endif /* CONFIG_P2P */
295 wpabuf_free(hapd->time_adv);
297 #ifdef CONFIG_INTERWORKING
298 gas_serv_deinit(hapd);
299 #endif /* CONFIG_INTERWORKING */
302 os_free(hapd->tmp_eap_user.identity);
303 os_free(hapd->tmp_eap_user.password);
304 #endif /* CONFIG_SQLITE */
309 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
310 * @hapd: Pointer to BSS data
312 * This function is used to free all per-BSS data structures and resources.
313 * Most of the modules that are initialized in hostapd_setup_bss() are
314 * deinitialized here.
316 static void hostapd_cleanup(struct hostapd_data *hapd)
318 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
320 if (hapd->iface->interfaces &&
321 hapd->iface->interfaces->ctrl_iface_deinit)
322 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
323 hostapd_free_hapd_data(hapd);
327 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
329 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
330 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
331 iface->hw_features = NULL;
332 os_free(iface->current_rates);
333 iface->current_rates = NULL;
334 os_free(iface->basic_rates);
335 iface->basic_rates = NULL;
336 ap_list_deinit(iface);
341 * hostapd_cleanup_iface - Complete per-interface cleanup
342 * @iface: Pointer to interface data
344 * This function is called after per-BSS data structures are deinitialized
345 * with hostapd_cleanup().
347 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
349 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
350 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
352 hostapd_cleanup_iface_partial(iface);
353 hostapd_config_free(iface->conf);
356 os_free(iface->config_fname);
358 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
363 static void hostapd_clear_wep(struct hostapd_data *hapd)
365 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
366 hostapd_set_privacy(hapd, 0);
367 hostapd_broadcast_wep_clear(hapd);
372 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
376 hostapd_broadcast_wep_set(hapd);
378 if (hapd->conf->ssid.wep.default_len) {
379 hostapd_set_privacy(hapd, 1);
384 * When IEEE 802.1X is not enabled, the driver may need to know how to
385 * set authentication algorithms for static WEP.
387 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
389 for (i = 0; i < 4; i++) {
390 if (hapd->conf->ssid.wep.key[i] &&
391 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
392 i == hapd->conf->ssid.wep.idx, NULL, 0,
393 hapd->conf->ssid.wep.key[i],
394 hapd->conf->ssid.wep.len[i])) {
395 wpa_printf(MSG_WARNING, "Could not set WEP "
399 if (hapd->conf->ssid.wep.key[i] &&
400 i == hapd->conf->ssid.wep.idx)
401 hostapd_set_privacy(hapd, 1);
408 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
413 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
416 if (!hapd->iface->driver_ap_teardown) {
417 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
418 "Flushing old station entries");
420 if (hostapd_flush(hapd)) {
421 wpa_msg(hapd->msg_ctx, MSG_WARNING,
422 "Could not connect to kernel driver");
426 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
427 os_memset(addr, 0xff, ETH_ALEN);
428 hostapd_drv_sta_deauth(hapd, addr, reason);
429 hostapd_free_stas(hapd);
436 * hostapd_validate_bssid_configuration - Validate BSSID configuration
437 * @iface: Pointer to interface data
438 * Returns: 0 on success, -1 on failure
440 * This function is used to validate that the configured BSSIDs are valid.
442 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
444 u8 mask[ETH_ALEN] = { 0 };
445 struct hostapd_data *hapd = iface->bss[0];
446 unsigned int i = iface->conf->num_bss, bits = 0, j;
449 if (hostapd_drv_none(hapd))
452 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
454 /* Determine the bits necessary to cover the number of BSSIDs. */
455 for (i--; i; i >>= 1)
458 /* Determine the bits necessary to any configured BSSIDs,
459 if they are higher than the number of BSSIDs. */
460 for (j = 0; j < iface->conf->num_bss; j++) {
461 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
467 for (i = 0; i < ETH_ALEN; i++) {
469 iface->conf->bss[j]->bssid[i] ^
477 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
483 while (mask[i] != 0) {
493 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
498 os_memset(mask, 0xff, ETH_ALEN);
500 for (i = 5; i > 5 - j; i--)
507 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
508 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
513 for (i = 0; i < ETH_ALEN; i++) {
514 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
515 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
516 " for start address " MACSTR ".",
517 MAC2STR(mask), MAC2STR(hapd->own_addr));
518 wpa_printf(MSG_ERROR, "Start address must be the "
519 "first address in the block (i.e., addr "
520 "AND mask == addr).");
529 static int mac_in_conf(struct hostapd_config *conf, const void *a)
533 for (i = 0; i < conf->num_bss; i++) {
534 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
543 #ifndef CONFIG_NO_RADIUS
545 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
546 struct radius_das_attrs *attr)
548 if (attr->nas_identifier &&
549 (!hapd->conf->nas_identifier ||
550 os_strlen(hapd->conf->nas_identifier) !=
551 attr->nas_identifier_len ||
552 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
553 attr->nas_identifier_len) != 0)) {
554 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
558 if (attr->nas_ip_addr &&
559 (hapd->conf->own_ip_addr.af != AF_INET ||
560 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
562 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
567 if (attr->nas_ipv6_addr &&
568 (hapd->conf->own_ip_addr.af != AF_INET6 ||
569 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
571 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
574 #endif /* CONFIG_IPV6 */
580 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
581 struct radius_das_attrs *attr)
583 struct sta_info *sta = NULL;
587 sta = ap_get_sta(hapd, attr->sta_addr);
589 if (sta == NULL && attr->acct_session_id &&
590 attr->acct_session_id_len == 17) {
591 for (sta = hapd->sta_list; sta; sta = sta->next) {
592 os_snprintf(buf, sizeof(buf), "%08X-%08X",
593 sta->acct_session_id_hi,
594 sta->acct_session_id_lo);
595 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
600 if (sta == NULL && attr->cui) {
601 for (sta = hapd->sta_list; sta; sta = sta->next) {
603 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
604 if (cui && wpabuf_len(cui) == attr->cui_len &&
605 os_memcmp(wpabuf_head(cui), attr->cui,
611 if (sta == NULL && attr->user_name) {
612 for (sta = hapd->sta_list; sta; sta = sta->next) {
615 identity = ieee802_1x_get_identity(sta->eapol_sm,
618 identity_len == attr->user_name_len &&
619 os_memcmp(identity, attr->user_name, identity_len)
629 static enum radius_das_res
630 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
632 struct hostapd_data *hapd = ctx;
633 struct sta_info *sta;
635 if (hostapd_das_nas_mismatch(hapd, attr))
636 return RADIUS_DAS_NAS_MISMATCH;
638 sta = hostapd_das_find_sta(hapd, attr);
640 return RADIUS_DAS_SESSION_NOT_FOUND;
642 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
644 hostapd_drv_sta_deauth(hapd, sta->addr,
645 WLAN_REASON_PREV_AUTH_NOT_VALID);
646 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
648 return RADIUS_DAS_SUCCESS;
651 #endif /* CONFIG_NO_RADIUS */
655 * hostapd_setup_bss - Per-BSS setup (initialization)
656 * @hapd: Pointer to BSS data
657 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
658 * but interface may exist
660 * This function is used to initialize all per-BSS data structures and
661 * resources. This gets called in a loop for each BSS when an interface is
662 * initialized. Most of the modules that are initialized here will be
663 * deinitialized in hostapd_cleanup().
665 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
667 struct hostapd_bss_config *conf = hapd->conf;
668 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
669 int ssid_len, set_ssid;
670 char force_ifname[IFNAMSIZ];
671 u8 if_addr[ETH_ALEN];
673 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
674 __func__, hapd, hapd->conf->iface, first);
677 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
678 __func__, hapd->conf->iface);
683 if (!first || first == -1) {
684 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
685 /* Allocate the next available BSSID. */
687 inc_byte_array(hapd->own_addr, ETH_ALEN);
688 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
690 /* Allocate the configured BSSID. */
691 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
693 if (hostapd_mac_comp(hapd->own_addr,
694 hapd->iface->bss[0]->own_addr) ==
696 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
697 "BSSID set to the MAC address of "
698 "the radio", hapd->conf->iface);
703 hapd->interface_added = 1;
704 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
705 hapd->conf->iface, hapd->own_addr, hapd,
706 &hapd->drv_priv, force_ifname, if_addr,
707 hapd->conf->bridge[0] ? hapd->conf->bridge :
708 NULL, first == -1)) {
709 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
710 MACSTR ")", MAC2STR(hapd->own_addr));
711 hapd->interface_added = 0;
716 if (conf->wmm_enabled < 0)
717 conf->wmm_enabled = hapd->iconf->ieee80211n;
719 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
720 hostapd_set_privacy(hapd, 0);
722 hostapd_broadcast_wep_clear(hapd);
723 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
727 * Fetch the SSID from the system and use it or,
728 * if one was specified in the config file, verify they
731 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
733 wpa_printf(MSG_ERROR, "Could not read SSID from system");
736 if (conf->ssid.ssid_set) {
738 * If SSID is specified in the config file and it differs
739 * from what is being used then force installation of the
742 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
743 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
746 * No SSID in the config file; just use the one we got
750 conf->ssid.ssid_len = ssid_len;
751 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
754 if (!hostapd_drv_none(hapd)) {
755 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
757 hapd->conf->iface, MAC2STR(hapd->own_addr),
758 wpa_ssid_txt(hapd->conf->ssid.ssid,
759 hapd->conf->ssid.ssid_len));
762 if (hostapd_setup_wpa_psk(conf)) {
763 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
767 /* Set SSID for the kernel driver (to be used in beacon and probe
768 * response frames) */
769 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
770 conf->ssid.ssid_len)) {
771 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
775 if (wpa_debug_level <= MSG_MSGDUMP)
776 conf->radius->msg_dumps = 1;
777 #ifndef CONFIG_NO_RADIUS
778 hapd->radius = radius_client_init(hapd, conf->radius);
779 if (hapd->radius == NULL) {
780 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
784 if (hapd->conf->radius_das_port) {
785 struct radius_das_conf das_conf;
786 os_memset(&das_conf, 0, sizeof(das_conf));
787 das_conf.port = hapd->conf->radius_das_port;
788 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
789 das_conf.shared_secret_len =
790 hapd->conf->radius_das_shared_secret_len;
791 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
792 das_conf.time_window = hapd->conf->radius_das_time_window;
793 das_conf.require_event_timestamp =
794 hapd->conf->radius_das_require_event_timestamp;
796 das_conf.disconnect = hostapd_das_disconnect;
797 hapd->radius_das = radius_das_init(&das_conf);
798 if (hapd->radius_das == NULL) {
799 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
804 #endif /* CONFIG_NO_RADIUS */
806 if (hostapd_acl_init(hapd)) {
807 wpa_printf(MSG_ERROR, "ACL initialization failed.");
810 if (hostapd_init_wps(hapd, conf))
813 if (authsrv_init(hapd) < 0)
816 if (ieee802_1x_init(hapd)) {
817 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
821 if ((hapd->conf->wpa || hapd->conf->osen) && hostapd_setup_wpa(hapd))
824 if (accounting_init(hapd)) {
825 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
829 if (hapd->conf->ieee802_11f &&
830 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
831 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
836 #ifdef CONFIG_INTERWORKING
837 if (gas_serv_init(hapd)) {
838 wpa_printf(MSG_ERROR, "GAS server initialization failed");
842 if (conf->qos_map_set_len &&
843 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
844 conf->qos_map_set_len)) {
845 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
848 #endif /* CONFIG_INTERWORKING */
850 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
851 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
855 if (!hapd->conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
858 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
861 if (hapd->driver && hapd->driver->set_operstate)
862 hapd->driver->set_operstate(hapd->drv_priv, 1);
868 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
870 struct hostapd_data *hapd = iface->bss[0];
872 struct hostapd_tx_queue_params *p;
874 for (i = 0; i < NUM_TX_QUEUES; i++) {
875 p = &iface->conf->tx_queue[i];
877 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
878 p->cwmax, p->burst)) {
879 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
880 "parameters for queue %d.", i);
881 /* Continue anyway */
887 static int hostapd_set_acl_list(struct hostapd_data *hapd,
888 struct mac_acl_entry *mac_acl,
889 int n_entries, u8 accept_acl)
891 struct hostapd_acl_params *acl_params;
894 acl_params = os_zalloc(sizeof(*acl_params) +
895 (n_entries * sizeof(acl_params->mac_acl[0])));
899 for (i = 0; i < n_entries; i++)
900 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
903 acl_params->acl_policy = accept_acl;
904 acl_params->num_mac_acl = n_entries;
906 err = hostapd_drv_set_acl(hapd, acl_params);
914 static void hostapd_set_acl(struct hostapd_data *hapd)
916 struct hostapd_config *conf = hapd->iconf;
920 if (hapd->iface->drv_max_acl_mac_addrs == 0)
922 if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
925 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
926 if (conf->bss[0]->num_accept_mac) {
928 err = hostapd_set_acl_list(hapd,
929 conf->bss[0]->accept_mac,
930 conf->bss[0]->num_accept_mac,
933 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
937 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
939 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
940 if (conf->bss[0]->num_deny_mac) {
942 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
943 conf->bss[0]->num_deny_mac,
946 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
950 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
956 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
958 if (!hapd->iface->interfaces ||
959 !hapd->iface->interfaces->ctrl_iface_init)
962 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
963 wpa_printf(MSG_ERROR,
964 "Failed to setup control interface for %s",
973 static int start_ctrl_iface(struct hostapd_iface *iface)
977 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
980 for (i = 0; i < iface->num_bss; i++) {
981 struct hostapd_data *hapd = iface->bss[i];
982 if (iface->interfaces->ctrl_iface_init(hapd)) {
983 wpa_printf(MSG_ERROR,
984 "Failed to setup control interface for %s",
994 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
996 struct hostapd_iface *iface = eloop_ctx;
998 if (!iface->wait_channel_update) {
999 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1004 * It is possible that the existing channel list is acceptable, so try
1007 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1008 setup_interface2(iface);
1012 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1014 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1017 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1018 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1019 setup_interface2(iface);
1023 static int setup_interface(struct hostapd_iface *iface)
1025 struct hostapd_data *hapd = iface->bss[0];
1029 * It is possible that setup_interface() is called after the interface
1030 * was disabled etc., in which case driver_ap_teardown is possibly set
1031 * to 1. Clear it here so any other key/station deletion, which is not
1032 * part of a teardown flow, would also call the relevant driver
1035 iface->driver_ap_teardown = 0;
1037 if (!iface->phy[0]) {
1038 const char *phy = hostapd_drv_get_radio_name(hapd);
1040 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1041 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1046 * Make sure that all BSSes get configured with a pointer to the same
1049 for (i = 1; i < iface->num_bss; i++) {
1050 iface->bss[i]->driver = hapd->driver;
1051 iface->bss[i]->drv_priv = hapd->drv_priv;
1054 if (hostapd_validate_bssid_configuration(iface))
1058 * Initialize control interfaces early to allow external monitoring of
1059 * channel setup operations that may take considerable amount of time
1060 * especially for DFS cases.
1062 if (start_ctrl_iface(iface))
1065 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1066 char country[4], previous_country[4];
1068 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1069 if (hostapd_get_country(hapd, previous_country) < 0)
1070 previous_country[0] = '\0';
1072 os_memcpy(country, hapd->iconf->country, 3);
1074 if (hostapd_set_country(hapd, country) < 0) {
1075 wpa_printf(MSG_ERROR, "Failed to set country code");
1079 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1080 previous_country, country);
1082 if (os_strncmp(previous_country, country, 2) != 0) {
1083 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1084 iface->wait_channel_update = 1;
1085 eloop_register_timeout(5, 0,
1086 channel_list_update_timeout,
1092 return setup_interface2(iface);
1096 static int setup_interface2(struct hostapd_iface *iface)
1098 iface->wait_channel_update = 0;
1100 if (hostapd_get_hw_features(iface)) {
1101 /* Not all drivers support this yet, so continue without hw
1104 int ret = hostapd_select_hw_mode(iface);
1106 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1107 "channel. (%d)", ret);
1111 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1114 ret = hostapd_check_ht_capab(iface);
1118 wpa_printf(MSG_DEBUG, "Interface initialization will "
1119 "be completed in a callback");
1123 if (iface->conf->ieee80211h)
1124 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1126 return hostapd_setup_interface_complete(iface, 0);
1129 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1130 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1131 if (iface->interfaces && iface->interfaces->terminate_on_error)
1138 * hostapd_setup_interface_complete - Complete interface setup
1140 * This function is called when previous steps in the interface setup has been
1141 * completed. This can also start operations, e.g., DFS, that will require
1142 * additional processing before interface is ready to be enabled. Such
1143 * operations will call this function from eloop callbacks when finished.
1145 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1147 struct hostapd_data *hapd = iface->bss[0];
1154 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1155 if (iface->conf->channel) {
1158 #endif /* NEED_AP_MLME */
1160 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1161 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1162 "Frequency: %d MHz",
1163 hostapd_hw_mode_txt(iface->conf->hw_mode),
1164 iface->conf->channel, iface->freq);
1168 res = hostapd_handle_dfs(iface);
1174 #endif /* NEED_AP_MLME */
1176 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1177 hapd->iconf->channel,
1178 hapd->iconf->ieee80211n,
1179 hapd->iconf->ieee80211ac,
1180 hapd->iconf->secondary_channel,
1181 hapd->iconf->vht_oper_chwidth,
1182 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1183 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1184 wpa_printf(MSG_ERROR, "Could not set channel for "
1190 if (iface->current_mode) {
1191 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1192 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1194 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1195 HOSTAPD_LEVEL_WARNING,
1196 "Failed to prepare rates table.");
1201 if (hapd->iconf->rts_threshold > -1 &&
1202 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1203 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1208 if (hapd->iconf->fragm_threshold > -1 &&
1209 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1210 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1211 "for kernel driver");
1215 prev_addr = hapd->own_addr;
1217 for (j = 0; j < iface->num_bss; j++) {
1218 hapd = iface->bss[j];
1220 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1221 if (hostapd_setup_bss(hapd, j == 0))
1223 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1224 prev_addr = hapd->own_addr;
1226 hapd = iface->bss[0];
1228 hostapd_tx_queue_params(iface);
1230 ap_list_init(iface);
1232 hostapd_set_acl(hapd);
1234 if (hostapd_driver_commit(hapd) < 0) {
1235 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1236 "configuration", __func__);
1241 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1242 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1243 * mode), the interface is up only after driver_commit, so initialize
1244 * WPS after driver_commit.
1246 for (j = 0; j < iface->num_bss; j++) {
1247 if (hostapd_init_wps_complete(iface->bss[j]))
1251 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1252 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1253 if (hapd->setup_complete_cb)
1254 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1256 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1257 iface->bss[0]->conf->iface);
1258 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1259 iface->interfaces->terminate_on_error--;
1264 wpa_printf(MSG_ERROR, "Interface initialization failed");
1265 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1266 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1267 if (iface->interfaces && iface->interfaces->terminate_on_error)
1274 * hostapd_setup_interface - Setup of an interface
1275 * @iface: Pointer to interface data.
1276 * Returns: 0 on success, -1 on failure
1278 * Initializes the driver interface, validates the configuration,
1279 * and sets driver parameters based on the configuration.
1280 * Flushes old stations, sets the channel, encryption,
1281 * beacons, and WDS links based on the configuration.
1283 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1284 * or DFS operations, this function returns 0 before such operations have been
1285 * completed. The pending operations are registered into eloop and will be
1286 * completed from eloop callbacks. Those callbacks end up calling
1287 * hostapd_setup_interface_complete() once setup has been completed.
1289 int hostapd_setup_interface(struct hostapd_iface *iface)
1293 ret = setup_interface(iface);
1295 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1296 iface->bss[0]->conf->iface);
1305 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1306 * @hapd_iface: Pointer to interface data
1307 * @conf: Pointer to per-interface configuration
1308 * @bss: Pointer to per-BSS configuration for this BSS
1309 * Returns: Pointer to allocated BSS data
1311 * This function is used to allocate per-BSS data structure. This data will be
1312 * freed after hostapd_cleanup() is called for it during interface
1315 struct hostapd_data *
1316 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1317 struct hostapd_config *conf,
1318 struct hostapd_bss_config *bss)
1320 struct hostapd_data *hapd;
1322 hapd = os_zalloc(sizeof(*hapd));
1326 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1329 hapd->iface = hapd_iface;
1330 hapd->driver = hapd->iconf->driver;
1331 hapd->ctrl_sock = -1;
1337 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1339 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1341 hostapd_free_stas(hapd);
1342 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1343 hostapd_clear_wep(hapd);
1344 hostapd_cleanup(hapd);
1348 void hostapd_interface_deinit(struct hostapd_iface *iface)
1352 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1356 #ifdef CONFIG_IEEE80211N
1358 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1359 #endif /* NEED_AP_MLME */
1360 #endif /* CONFIG_IEEE80211N */
1361 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1362 iface->wait_channel_update = 0;
1364 for (j = iface->num_bss - 1; j >= 0; j--)
1365 hostapd_bss_deinit(iface->bss[j]);
1369 void hostapd_interface_free(struct hostapd_iface *iface)
1372 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1373 for (j = 0; j < iface->num_bss; j++) {
1374 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1375 __func__, iface->bss[j]);
1376 os_free(iface->bss[j]);
1378 hostapd_cleanup_iface(iface);
1383 * hostapd_init - Allocate and initialize per-interface data
1384 * @config_file: Path to the configuration file
1385 * Returns: Pointer to the allocated interface data or %NULL on failure
1387 * This function is used to allocate main data structures for per-interface
1388 * data. The allocated data buffer will be freed by calling
1389 * hostapd_cleanup_iface().
1391 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1392 const char *config_file)
1394 struct hostapd_iface *hapd_iface = NULL;
1395 struct hostapd_config *conf = NULL;
1396 struct hostapd_data *hapd;
1399 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1400 if (hapd_iface == NULL)
1403 hapd_iface->config_fname = os_strdup(config_file);
1404 if (hapd_iface->config_fname == NULL)
1407 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1410 hapd_iface->conf = conf;
1412 hapd_iface->num_bss = conf->num_bss;
1413 hapd_iface->bss = os_calloc(conf->num_bss,
1414 sizeof(struct hostapd_data *));
1415 if (hapd_iface->bss == NULL)
1418 for (i = 0; i < conf->num_bss; i++) {
1419 hapd = hapd_iface->bss[i] =
1420 hostapd_alloc_bss_data(hapd_iface, conf,
1424 hapd->msg_ctx = hapd;
1430 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1433 hostapd_config_free(conf);
1435 os_free(hapd_iface->config_fname);
1436 os_free(hapd_iface->bss);
1437 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1438 __func__, hapd_iface);
1439 os_free(hapd_iface);
1445 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1449 for (i = 0; i < interfaces->count; i++) {
1450 struct hostapd_iface *iface = interfaces->iface[i];
1451 for (j = 0; j < iface->num_bss; j++) {
1452 struct hostapd_data *hapd = iface->bss[j];
1453 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1463 * hostapd_interface_init_bss - Read configuration file and init BSS data
1465 * This function is used to parse configuration file for a BSS. This BSS is
1466 * added to an existing interface sharing the same radio (if any) or a new
1467 * interface is created if this is the first interface on a radio. This
1468 * allocate memory for the BSS. No actual driver operations are started.
1470 * This is similar to hostapd_interface_init(), but for a case where the
1471 * configuration is used to add a single BSS instead of all BSSes for a radio.
1473 struct hostapd_iface *
1474 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1475 const char *config_fname, int debug)
1477 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1478 struct hostapd_data *hapd;
1485 for (i = 0; i < interfaces->count; i++) {
1486 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1487 iface = interfaces->iface[i];
1492 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1493 config_fname, phy, iface ? "" : " --> new PHY");
1495 struct hostapd_config *conf;
1496 struct hostapd_bss_config **tmp_conf;
1497 struct hostapd_data **tmp_bss;
1498 struct hostapd_bss_config *bss;
1501 /* Add new BSS to existing iface */
1502 conf = interfaces->config_read_cb(config_fname);
1505 if (conf->num_bss > 1) {
1506 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1507 hostapd_config_free(conf);
1511 ifname = conf->bss[0]->iface;
1512 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1513 wpa_printf(MSG_ERROR,
1514 "Interface name %s already in use", ifname);
1515 hostapd_config_free(conf);
1519 tmp_conf = os_realloc_array(
1520 iface->conf->bss, iface->conf->num_bss + 1,
1521 sizeof(struct hostapd_bss_config *));
1522 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1523 sizeof(struct hostapd_data *));
1525 iface->bss = tmp_bss;
1527 iface->conf->bss = tmp_conf;
1528 iface->conf->last_bss = tmp_conf[0];
1530 if (tmp_bss == NULL || tmp_conf == NULL) {
1531 hostapd_config_free(conf);
1534 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1535 iface->conf->num_bss++;
1537 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1539 iface->conf->num_bss--;
1540 hostapd_config_free(conf);
1543 iface->conf->last_bss = bss;
1544 iface->bss[iface->num_bss] = hapd;
1545 hapd->msg_ctx = hapd;
1547 bss_idx = iface->num_bss++;
1549 conf->bss[0] = NULL;
1550 hostapd_config_free(conf);
1552 /* Add a new iface with the first BSS */
1553 new_iface = iface = hostapd_init(interfaces, config_fname);
1556 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1557 iface->interfaces = interfaces;
1561 for (k = 0; k < debug; k++) {
1562 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1563 iface->bss[bss_idx]->conf->logger_stdout_level--;
1566 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1567 !hostapd_drv_none(iface->bss[bss_idx])) {
1568 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1571 hostapd_interface_deinit_free(new_iface);
1579 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1581 const struct wpa_driver_ops *driver;
1584 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1587 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1588 __func__, (unsigned int) iface->num_bss,
1589 (unsigned int) iface->conf->num_bss);
1590 driver = iface->bss[0]->driver;
1591 drv_priv = iface->bss[0]->drv_priv;
1592 hostapd_interface_deinit(iface);
1593 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1594 __func__, driver, drv_priv);
1595 if (driver && driver->hapd_deinit && drv_priv)
1596 driver->hapd_deinit(drv_priv);
1597 hostapd_interface_free(iface);
1601 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1603 struct hostapd_iface *hapd_iface)
1607 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1608 __func__, driver, drv_priv);
1609 if (driver && driver->hapd_deinit && drv_priv) {
1610 driver->hapd_deinit(drv_priv);
1611 for (j = 0; j < hapd_iface->num_bss; j++) {
1612 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1614 hapd_iface->bss[j]->drv_priv);
1615 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1616 hapd_iface->bss[j]->drv_priv = NULL;
1622 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1624 if (hapd_iface->bss[0]->drv_priv != NULL) {
1625 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1626 hapd_iface->conf->bss[0]->iface);
1630 wpa_printf(MSG_DEBUG, "Enable interface %s",
1631 hapd_iface->conf->bss[0]->iface);
1633 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1634 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1638 if (hapd_iface->interfaces == NULL ||
1639 hapd_iface->interfaces->driver_init == NULL ||
1640 hapd_iface->interfaces->driver_init(hapd_iface))
1643 if (hostapd_setup_interface(hapd_iface)) {
1644 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1645 hapd_iface->bss[0]->drv_priv,
1654 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1658 wpa_printf(MSG_DEBUG, "Reload interface %s",
1659 hapd_iface->conf->bss[0]->iface);
1660 for (j = 0; j < hapd_iface->num_bss; j++)
1661 hostapd_set_security_params(hapd_iface->conf->bss[j]);
1662 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1663 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1666 hostapd_clear_old(hapd_iface);
1667 for (j = 0; j < hapd_iface->num_bss; j++)
1668 hostapd_reload_bss(hapd_iface->bss[j]);
1674 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1677 const struct wpa_driver_ops *driver;
1680 if (hapd_iface == NULL)
1682 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1683 driver = hapd_iface->bss[0]->driver;
1684 drv_priv = hapd_iface->bss[0]->drv_priv;
1686 hapd_iface->driver_ap_teardown =
1687 !!(hapd_iface->drv_flags &
1688 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1690 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1691 for (j = 0; j < hapd_iface->num_bss; j++) {
1692 struct hostapd_data *hapd = hapd_iface->bss[j];
1693 hostapd_free_stas(hapd);
1694 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1695 hostapd_clear_wep(hapd);
1696 hostapd_free_hapd_data(hapd);
1699 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
1701 /* From hostapd_cleanup_iface: These were initialized in
1702 * hostapd_setup_interface and hostapd_setup_interface_complete
1704 hostapd_cleanup_iface_partial(hapd_iface);
1706 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1707 hapd_iface->bss[0]->conf->iface);
1708 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1713 static struct hostapd_iface *
1714 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1716 struct hostapd_iface **iface, *hapd_iface;
1718 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1719 sizeof(struct hostapd_iface *));
1722 interfaces->iface = iface;
1723 hapd_iface = interfaces->iface[interfaces->count] =
1724 os_zalloc(sizeof(*hapd_iface));
1725 if (hapd_iface == NULL) {
1726 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1727 "the interface", __func__);
1730 interfaces->count++;
1731 hapd_iface->interfaces = interfaces;
1737 static struct hostapd_config *
1738 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1739 const char *ctrl_iface)
1741 struct hostapd_bss_config *bss;
1742 struct hostapd_config *conf;
1744 /* Allocates memory for bss and conf */
1745 conf = hostapd_config_defaults();
1747 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1748 "configuration", __func__);
1752 conf->driver = wpa_drivers[0];
1753 if (conf->driver == NULL) {
1754 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1755 hostapd_config_free(conf);
1759 bss = conf->last_bss = conf->bss[0];
1761 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1762 bss->ctrl_interface = os_strdup(ctrl_iface);
1763 if (bss->ctrl_interface == NULL) {
1764 hostapd_config_free(conf);
1768 /* Reading configuration file skipped, will be done in SET!
1769 * From reading the configuration till the end has to be done in
1776 static struct hostapd_iface * hostapd_data_alloc(
1777 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1780 struct hostapd_iface *hapd_iface =
1781 interfaces->iface[interfaces->count - 1];
1782 struct hostapd_data *hapd;
1784 hapd_iface->conf = conf;
1785 hapd_iface->num_bss = conf->num_bss;
1787 hapd_iface->bss = os_zalloc(conf->num_bss *
1788 sizeof(struct hostapd_data *));
1789 if (hapd_iface->bss == NULL)
1792 for (i = 0; i < conf->num_bss; i++) {
1793 hapd = hapd_iface->bss[i] =
1794 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1797 hapd->msg_ctx = hapd;
1800 hapd_iface->interfaces = interfaces;
1806 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1808 struct hostapd_config *conf = NULL;
1809 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1810 struct hostapd_data *hapd;
1813 const char *conf_file = NULL, *phy_name = NULL;
1815 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1817 phy_name = buf + 11;
1818 pos = os_strchr(phy_name, ':');
1823 if (!os_strlen(conf_file))
1826 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1830 for (j = 0; j < interfaces->count; j++) {
1831 if (interfaces->iface[j] == hapd_iface)
1834 if (j == interfaces->count) {
1835 struct hostapd_iface **tmp;
1836 tmp = os_realloc_array(interfaces->iface,
1837 interfaces->count + 1,
1838 sizeof(struct hostapd_iface *));
1840 hostapd_interface_deinit_free(hapd_iface);
1843 interfaces->iface = tmp;
1844 interfaces->iface[interfaces->count++] = hapd_iface;
1845 new_iface = hapd_iface;
1849 if (interfaces->driver_init(hapd_iface) ||
1850 hostapd_setup_interface(hapd_iface)) {
1851 interfaces->count--;
1855 /* Assign new BSS with bss[0]'s driver info */
1856 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1857 hapd->driver = hapd_iface->bss[0]->driver;
1858 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1859 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1862 if (start_ctrl_iface_bss(hapd) < 0 ||
1863 (hapd_iface->state == HAPD_IFACE_ENABLED &&
1864 hostapd_setup_bss(hapd, -1))) {
1865 hostapd_cleanup(hapd);
1866 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
1867 hapd_iface->conf->num_bss--;
1868 hapd_iface->num_bss--;
1869 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1870 __func__, hapd, hapd->conf->iface);
1878 ptr = os_strchr(buf, ' ');
1883 if (os_strncmp(ptr, "config=", 7) == 0)
1884 conf_file = ptr + 7;
1886 for (i = 0; i < interfaces->count; i++) {
1887 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1889 wpa_printf(MSG_INFO, "Cannot add interface - it "
1895 hapd_iface = hostapd_iface_alloc(interfaces);
1896 if (hapd_iface == NULL) {
1897 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1898 "for interface", __func__);
1902 if (conf_file && interfaces->config_read_cb) {
1903 conf = interfaces->config_read_cb(conf_file);
1904 if (conf && conf->bss)
1905 os_strlcpy(conf->bss[0]->iface, buf,
1906 sizeof(conf->bss[0]->iface));
1908 conf = hostapd_config_alloc(interfaces, buf, ptr);
1909 if (conf == NULL || conf->bss == NULL) {
1910 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1911 "for configuration", __func__);
1915 hapd_iface = hostapd_data_alloc(interfaces, conf);
1916 if (hapd_iface == NULL) {
1917 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1918 "for hostapd", __func__);
1922 if (start_ctrl_iface(hapd_iface) < 0)
1925 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
1931 hostapd_config_free(conf);
1933 if (hapd_iface->bss) {
1934 for (i = 0; i < hapd_iface->num_bss; i++) {
1935 hapd = hapd_iface->bss[i];
1938 if (hapd_iface->interfaces &&
1939 hapd_iface->interfaces->ctrl_iface_deinit)
1940 hapd_iface->interfaces->
1941 ctrl_iface_deinit(hapd);
1942 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1943 __func__, hapd_iface->bss[i],
1946 hapd_iface->bss[i] = NULL;
1948 os_free(hapd_iface->bss);
1950 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1951 __func__, hapd_iface);
1952 os_free(hapd_iface);
1958 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
1962 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
1964 /* Remove hostapd_data only if it has already been initialized */
1965 if (idx < iface->num_bss) {
1966 struct hostapd_data *hapd = iface->bss[idx];
1968 hostapd_bss_deinit(hapd);
1969 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1970 __func__, hapd, hapd->conf->iface);
1971 hostapd_config_free_bss(hapd->conf);
1976 for (i = idx; i < iface->num_bss; i++)
1977 iface->bss[i] = iface->bss[i + 1];
1979 hostapd_config_free_bss(iface->conf->bss[idx]);
1980 iface->conf->bss[idx] = NULL;
1983 iface->conf->num_bss--;
1984 for (i = idx; i < iface->conf->num_bss; i++)
1985 iface->conf->bss[i] = iface->conf->bss[i + 1];
1991 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1993 struct hostapd_iface *hapd_iface;
1996 for (i = 0; i < interfaces->count; i++) {
1997 hapd_iface = interfaces->iface[i];
1998 if (hapd_iface == NULL)
2000 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2001 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2002 hapd_iface->driver_ap_teardown =
2003 !!(hapd_iface->drv_flags &
2004 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2006 hostapd_interface_deinit_free(hapd_iface);
2008 while (k < (interfaces->count - 1)) {
2009 interfaces->iface[k] =
2010 interfaces->iface[k + 1];
2013 interfaces->count--;
2017 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2018 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2019 hapd_iface->driver_ap_teardown =
2020 !(hapd_iface->drv_flags &
2021 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2022 return hostapd_remove_bss(hapd_iface, j);
2031 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2032 * @hapd: Pointer to BSS data
2033 * @sta: Pointer to the associated STA data
2034 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2036 * This function will be called whenever a station associates with the AP. It
2037 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2038 * from drv_callbacks.c based on driver events for drivers that take care of
2039 * management frames (IEEE 802.11 authentication and association) internally.
2041 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2044 if (hapd->tkip_countermeasures) {
2045 hostapd_drv_sta_deauth(hapd, sta->addr,
2046 WLAN_REASON_MICHAEL_MIC_FAILURE);
2050 hostapd_prune_associations(hapd, sta->addr);
2052 /* IEEE 802.11F (IAPP) */
2053 if (hapd->conf->ieee802_11f)
2054 iapp_new_station(hapd->iapp, sta);
2057 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2058 sta->no_p2p_set = 1;
2059 hapd->num_sta_no_p2p++;
2060 if (hapd->num_sta_no_p2p == 1)
2061 hostapd_p2p_non_p2p_sta_connected(hapd);
2063 #endif /* CONFIG_P2P */
2065 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2066 * IEEE 802.1X/WPA code will start accounting after the station has
2067 * been authorized. */
2068 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2069 ap_sta_set_authorized(hapd, sta, 1);
2070 os_get_reltime(&sta->connected_time);
2071 accounting_sta_start(hapd, sta);
2074 /* Start IEEE 802.1X authentication process for new stations */
2075 ieee802_1x_new_station(hapd, sta);
2077 if (sta->auth_alg != WLAN_AUTH_FT &&
2078 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2079 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2081 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2083 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2084 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2085 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2086 __func__, MAC2STR(sta->addr),
2087 hapd->conf->ap_max_inactivity);
2088 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2089 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2090 ap_handle_timer, hapd, sta);
2095 const char * hostapd_state_text(enum hostapd_iface_state s)
2098 case HAPD_IFACE_UNINITIALIZED:
2099 return "UNINITIALIZED";
2100 case HAPD_IFACE_DISABLED:
2102 case HAPD_IFACE_COUNTRY_UPDATE:
2103 return "COUNTRY_UPDATE";
2104 case HAPD_IFACE_ACS:
2106 case HAPD_IFACE_HT_SCAN:
2108 case HAPD_IFACE_DFS:
2110 case HAPD_IFACE_ENABLED:
2118 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2120 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2121 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2122 hostapd_state_text(s));
2129 static void free_beacon_data(struct beacon_data *beacon)
2131 os_free(beacon->head);
2132 beacon->head = NULL;
2133 os_free(beacon->tail);
2134 beacon->tail = NULL;
2135 os_free(beacon->probe_resp);
2136 beacon->probe_resp = NULL;
2137 os_free(beacon->beacon_ies);
2138 beacon->beacon_ies = NULL;
2139 os_free(beacon->proberesp_ies);
2140 beacon->proberesp_ies = NULL;
2141 os_free(beacon->assocresp_ies);
2142 beacon->assocresp_ies = NULL;
2146 static int hostapd_build_beacon_data(struct hostapd_iface *iface,
2147 struct beacon_data *beacon)
2149 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2150 struct wpa_driver_ap_params params;
2152 struct hostapd_data *hapd = iface->bss[0];
2154 os_memset(beacon, 0, sizeof(*beacon));
2155 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2159 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2163 goto free_ap_params;
2166 beacon->head = os_malloc(params.head_len);
2168 goto free_ap_extra_ies;
2170 os_memcpy(beacon->head, params.head, params.head_len);
2171 beacon->head_len = params.head_len;
2173 beacon->tail = os_malloc(params.tail_len);
2177 os_memcpy(beacon->tail, params.tail, params.tail_len);
2178 beacon->tail_len = params.tail_len;
2180 if (params.proberesp != NULL) {
2181 beacon->probe_resp = os_malloc(params.proberesp_len);
2182 if (!beacon->probe_resp)
2185 os_memcpy(beacon->probe_resp, params.proberesp,
2186 params.proberesp_len);
2187 beacon->probe_resp_len = params.proberesp_len;
2190 /* copy the extra ies */
2192 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2193 if (!beacon->beacon_ies)
2196 os_memcpy(beacon->beacon_ies,
2197 beacon_extra->buf, wpabuf_len(beacon_extra));
2198 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2201 if (proberesp_extra) {
2202 beacon->proberesp_ies =
2203 os_malloc(wpabuf_len(proberesp_extra));
2204 if (!beacon->proberesp_ies)
2207 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2208 wpabuf_len(proberesp_extra));
2209 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2212 if (assocresp_extra) {
2213 beacon->assocresp_ies =
2214 os_malloc(wpabuf_len(assocresp_extra));
2215 if (!beacon->assocresp_ies)
2218 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2219 wpabuf_len(assocresp_extra));
2220 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2225 /* if the function fails, the caller should not free beacon data */
2227 free_beacon_data(beacon);
2230 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2233 ieee802_11_free_ap_params(¶ms);
2239 * TODO: This flow currently supports only changing frequency within the
2240 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2241 * width) are not supported.
2243 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2244 struct hostapd_config *conf,
2245 struct hostapd_freq_params *params,
2246 struct hostapd_freq_params *old_params)
2250 if (!params->channel) {
2251 /* check if the new channel is supported by hw */
2252 channel = hostapd_hw_get_channel(hapd, params->freq);
2256 channel = params->channel;
2259 /* if a pointer to old_params is provided we save previous state */
2261 old_params->channel = conf->channel;
2262 old_params->ht_enabled = conf->ieee80211n;
2263 old_params->sec_channel_offset = conf->secondary_channel;
2266 conf->channel = channel;
2267 conf->ieee80211n = params->ht_enabled;
2268 conf->secondary_channel = params->sec_channel_offset;
2270 /* TODO: maybe call here hostapd_config_check here? */
2276 static int hostapd_fill_csa_settings(struct hostapd_iface *iface,
2277 struct csa_settings *settings)
2279 struct hostapd_freq_params old_freq;
2282 os_memset(&old_freq, 0, sizeof(old_freq));
2283 if (!iface || !iface->freq || iface->csa_in_progress)
2286 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2287 &settings->freq_params,
2292 ret = hostapd_build_beacon_data(iface, &settings->beacon_after);
2294 /* change back the configuration */
2295 hostapd_change_config_freq(iface->bss[0], iface->conf,
2301 /* set channel switch parameters for csa ie */
2302 iface->cs_freq_params = settings->freq_params;
2303 iface->cs_count = settings->cs_count;
2304 iface->cs_block_tx = settings->block_tx;
2306 ret = hostapd_build_beacon_data(iface, &settings->beacon_csa);
2308 free_beacon_data(&settings->beacon_after);
2312 settings->counter_offset_beacon = iface->cs_c_off_beacon;
2313 settings->counter_offset_presp = iface->cs_c_off_proberesp;
2319 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2321 os_memset(&hapd->iface->cs_freq_params, 0,
2322 sizeof(hapd->iface->cs_freq_params));
2323 hapd->iface->cs_count = 0;
2324 hapd->iface->cs_block_tx = 0;
2325 hapd->iface->cs_c_off_beacon = 0;
2326 hapd->iface->cs_c_off_proberesp = 0;
2327 hapd->iface->csa_in_progress = 0;
2331 int hostapd_switch_channel(struct hostapd_data *hapd,
2332 struct csa_settings *settings)
2335 ret = hostapd_fill_csa_settings(hapd->iface, settings);
2339 ret = hostapd_drv_switch_channel(hapd, settings);
2340 free_beacon_data(&settings->beacon_csa);
2341 free_beacon_data(&settings->beacon_after);
2344 /* if we failed, clean cs parameters */
2345 hostapd_cleanup_cs_params(hapd);
2349 hapd->iface->csa_in_progress = 1;
2353 #endif /* NEED_AP_MLME */