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 hostapd_stop_setup_timers(iface);
1359 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1360 #endif /* NEED_AP_MLME */
1361 #endif /* CONFIG_IEEE80211N */
1362 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1363 iface->wait_channel_update = 0;
1365 for (j = iface->num_bss - 1; j >= 0; j--)
1366 hostapd_bss_deinit(iface->bss[j]);
1370 void hostapd_interface_free(struct hostapd_iface *iface)
1373 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1374 for (j = 0; j < iface->num_bss; j++) {
1375 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1376 __func__, iface->bss[j]);
1377 os_free(iface->bss[j]);
1379 hostapd_cleanup_iface(iface);
1384 * hostapd_init - Allocate and initialize per-interface data
1385 * @config_file: Path to the configuration file
1386 * Returns: Pointer to the allocated interface data or %NULL on failure
1388 * This function is used to allocate main data structures for per-interface
1389 * data. The allocated data buffer will be freed by calling
1390 * hostapd_cleanup_iface().
1392 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1393 const char *config_file)
1395 struct hostapd_iface *hapd_iface = NULL;
1396 struct hostapd_config *conf = NULL;
1397 struct hostapd_data *hapd;
1400 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1401 if (hapd_iface == NULL)
1404 hapd_iface->config_fname = os_strdup(config_file);
1405 if (hapd_iface->config_fname == NULL)
1408 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1411 hapd_iface->conf = conf;
1413 hapd_iface->num_bss = conf->num_bss;
1414 hapd_iface->bss = os_calloc(conf->num_bss,
1415 sizeof(struct hostapd_data *));
1416 if (hapd_iface->bss == NULL)
1419 for (i = 0; i < conf->num_bss; i++) {
1420 hapd = hapd_iface->bss[i] =
1421 hostapd_alloc_bss_data(hapd_iface, conf,
1425 hapd->msg_ctx = hapd;
1431 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1434 hostapd_config_free(conf);
1436 os_free(hapd_iface->config_fname);
1437 os_free(hapd_iface->bss);
1438 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1439 __func__, hapd_iface);
1440 os_free(hapd_iface);
1446 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1450 for (i = 0; i < interfaces->count; i++) {
1451 struct hostapd_iface *iface = interfaces->iface[i];
1452 for (j = 0; j < iface->num_bss; j++) {
1453 struct hostapd_data *hapd = iface->bss[j];
1454 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1464 * hostapd_interface_init_bss - Read configuration file and init BSS data
1466 * This function is used to parse configuration file for a BSS. This BSS is
1467 * added to an existing interface sharing the same radio (if any) or a new
1468 * interface is created if this is the first interface on a radio. This
1469 * allocate memory for the BSS. No actual driver operations are started.
1471 * This is similar to hostapd_interface_init(), but for a case where the
1472 * configuration is used to add a single BSS instead of all BSSes for a radio.
1474 struct hostapd_iface *
1475 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1476 const char *config_fname, int debug)
1478 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1479 struct hostapd_data *hapd;
1486 for (i = 0; i < interfaces->count; i++) {
1487 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1488 iface = interfaces->iface[i];
1493 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1494 config_fname, phy, iface ? "" : " --> new PHY");
1496 struct hostapd_config *conf;
1497 struct hostapd_bss_config **tmp_conf;
1498 struct hostapd_data **tmp_bss;
1499 struct hostapd_bss_config *bss;
1502 /* Add new BSS to existing iface */
1503 conf = interfaces->config_read_cb(config_fname);
1506 if (conf->num_bss > 1) {
1507 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1508 hostapd_config_free(conf);
1512 ifname = conf->bss[0]->iface;
1513 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1514 wpa_printf(MSG_ERROR,
1515 "Interface name %s already in use", ifname);
1516 hostapd_config_free(conf);
1520 tmp_conf = os_realloc_array(
1521 iface->conf->bss, iface->conf->num_bss + 1,
1522 sizeof(struct hostapd_bss_config *));
1523 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1524 sizeof(struct hostapd_data *));
1526 iface->bss = tmp_bss;
1528 iface->conf->bss = tmp_conf;
1529 iface->conf->last_bss = tmp_conf[0];
1531 if (tmp_bss == NULL || tmp_conf == NULL) {
1532 hostapd_config_free(conf);
1535 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1536 iface->conf->num_bss++;
1538 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1540 iface->conf->num_bss--;
1541 hostapd_config_free(conf);
1544 iface->conf->last_bss = bss;
1545 iface->bss[iface->num_bss] = hapd;
1546 hapd->msg_ctx = hapd;
1548 bss_idx = iface->num_bss++;
1550 conf->bss[0] = NULL;
1551 hostapd_config_free(conf);
1553 /* Add a new iface with the first BSS */
1554 new_iface = iface = hostapd_init(interfaces, config_fname);
1557 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1558 iface->interfaces = interfaces;
1562 for (k = 0; k < debug; k++) {
1563 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1564 iface->bss[bss_idx]->conf->logger_stdout_level--;
1567 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1568 !hostapd_drv_none(iface->bss[bss_idx])) {
1569 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1572 hostapd_interface_deinit_free(new_iface);
1580 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1582 const struct wpa_driver_ops *driver;
1585 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1588 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1589 __func__, (unsigned int) iface->num_bss,
1590 (unsigned int) iface->conf->num_bss);
1591 driver = iface->bss[0]->driver;
1592 drv_priv = iface->bss[0]->drv_priv;
1593 hostapd_interface_deinit(iface);
1594 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1595 __func__, driver, drv_priv);
1596 if (driver && driver->hapd_deinit && drv_priv)
1597 driver->hapd_deinit(drv_priv);
1598 hostapd_interface_free(iface);
1602 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1604 struct hostapd_iface *hapd_iface)
1608 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1609 __func__, driver, drv_priv);
1610 if (driver && driver->hapd_deinit && drv_priv) {
1611 driver->hapd_deinit(drv_priv);
1612 for (j = 0; j < hapd_iface->num_bss; j++) {
1613 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1615 hapd_iface->bss[j]->drv_priv);
1616 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1617 hapd_iface->bss[j]->drv_priv = NULL;
1623 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1625 if (hapd_iface->bss[0]->drv_priv != NULL) {
1626 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1627 hapd_iface->conf->bss[0]->iface);
1631 wpa_printf(MSG_DEBUG, "Enable interface %s",
1632 hapd_iface->conf->bss[0]->iface);
1634 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1635 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1639 if (hapd_iface->interfaces == NULL ||
1640 hapd_iface->interfaces->driver_init == NULL ||
1641 hapd_iface->interfaces->driver_init(hapd_iface))
1644 if (hostapd_setup_interface(hapd_iface)) {
1645 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1646 hapd_iface->bss[0]->drv_priv,
1655 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1659 wpa_printf(MSG_DEBUG, "Reload interface %s",
1660 hapd_iface->conf->bss[0]->iface);
1661 for (j = 0; j < hapd_iface->num_bss; j++)
1662 hostapd_set_security_params(hapd_iface->conf->bss[j]);
1663 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1664 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1667 hostapd_clear_old(hapd_iface);
1668 for (j = 0; j < hapd_iface->num_bss; j++)
1669 hostapd_reload_bss(hapd_iface->bss[j]);
1675 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1678 const struct wpa_driver_ops *driver;
1681 if (hapd_iface == NULL)
1683 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1684 driver = hapd_iface->bss[0]->driver;
1685 drv_priv = hapd_iface->bss[0]->drv_priv;
1687 hapd_iface->driver_ap_teardown =
1688 !!(hapd_iface->drv_flags &
1689 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1691 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1692 for (j = 0; j < hapd_iface->num_bss; j++) {
1693 struct hostapd_data *hapd = hapd_iface->bss[j];
1694 hostapd_free_stas(hapd);
1695 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1696 hostapd_clear_wep(hapd);
1697 hostapd_free_hapd_data(hapd);
1700 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
1702 /* From hostapd_cleanup_iface: These were initialized in
1703 * hostapd_setup_interface and hostapd_setup_interface_complete
1705 hostapd_cleanup_iface_partial(hapd_iface);
1707 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1708 hapd_iface->bss[0]->conf->iface);
1709 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1714 static struct hostapd_iface *
1715 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1717 struct hostapd_iface **iface, *hapd_iface;
1719 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1720 sizeof(struct hostapd_iface *));
1723 interfaces->iface = iface;
1724 hapd_iface = interfaces->iface[interfaces->count] =
1725 os_zalloc(sizeof(*hapd_iface));
1726 if (hapd_iface == NULL) {
1727 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1728 "the interface", __func__);
1731 interfaces->count++;
1732 hapd_iface->interfaces = interfaces;
1738 static struct hostapd_config *
1739 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1740 const char *ctrl_iface)
1742 struct hostapd_bss_config *bss;
1743 struct hostapd_config *conf;
1745 /* Allocates memory for bss and conf */
1746 conf = hostapd_config_defaults();
1748 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1749 "configuration", __func__);
1753 conf->driver = wpa_drivers[0];
1754 if (conf->driver == NULL) {
1755 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1756 hostapd_config_free(conf);
1760 bss = conf->last_bss = conf->bss[0];
1762 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1763 bss->ctrl_interface = os_strdup(ctrl_iface);
1764 if (bss->ctrl_interface == NULL) {
1765 hostapd_config_free(conf);
1769 /* Reading configuration file skipped, will be done in SET!
1770 * From reading the configuration till the end has to be done in
1777 static struct hostapd_iface * hostapd_data_alloc(
1778 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1781 struct hostapd_iface *hapd_iface =
1782 interfaces->iface[interfaces->count - 1];
1783 struct hostapd_data *hapd;
1785 hapd_iface->conf = conf;
1786 hapd_iface->num_bss = conf->num_bss;
1788 hapd_iface->bss = os_zalloc(conf->num_bss *
1789 sizeof(struct hostapd_data *));
1790 if (hapd_iface->bss == NULL)
1793 for (i = 0; i < conf->num_bss; i++) {
1794 hapd = hapd_iface->bss[i] =
1795 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1798 hapd->msg_ctx = hapd;
1801 hapd_iface->interfaces = interfaces;
1807 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1809 struct hostapd_config *conf = NULL;
1810 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1811 struct hostapd_data *hapd;
1814 const char *conf_file = NULL, *phy_name = NULL;
1816 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1818 phy_name = buf + 11;
1819 pos = os_strchr(phy_name, ':');
1824 if (!os_strlen(conf_file))
1827 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1831 for (j = 0; j < interfaces->count; j++) {
1832 if (interfaces->iface[j] == hapd_iface)
1835 if (j == interfaces->count) {
1836 struct hostapd_iface **tmp;
1837 tmp = os_realloc_array(interfaces->iface,
1838 interfaces->count + 1,
1839 sizeof(struct hostapd_iface *));
1841 hostapd_interface_deinit_free(hapd_iface);
1844 interfaces->iface = tmp;
1845 interfaces->iface[interfaces->count++] = hapd_iface;
1846 new_iface = hapd_iface;
1850 if (interfaces->driver_init(hapd_iface) ||
1851 hostapd_setup_interface(hapd_iface)) {
1852 interfaces->count--;
1856 /* Assign new BSS with bss[0]'s driver info */
1857 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1858 hapd->driver = hapd_iface->bss[0]->driver;
1859 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1860 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1863 if (start_ctrl_iface_bss(hapd) < 0 ||
1864 (hapd_iface->state == HAPD_IFACE_ENABLED &&
1865 hostapd_setup_bss(hapd, -1))) {
1866 hostapd_cleanup(hapd);
1867 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
1868 hapd_iface->conf->num_bss--;
1869 hapd_iface->num_bss--;
1870 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1871 __func__, hapd, hapd->conf->iface);
1879 ptr = os_strchr(buf, ' ');
1884 if (os_strncmp(ptr, "config=", 7) == 0)
1885 conf_file = ptr + 7;
1887 for (i = 0; i < interfaces->count; i++) {
1888 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1890 wpa_printf(MSG_INFO, "Cannot add interface - it "
1896 hapd_iface = hostapd_iface_alloc(interfaces);
1897 if (hapd_iface == NULL) {
1898 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1899 "for interface", __func__);
1903 if (conf_file && interfaces->config_read_cb) {
1904 conf = interfaces->config_read_cb(conf_file);
1905 if (conf && conf->bss)
1906 os_strlcpy(conf->bss[0]->iface, buf,
1907 sizeof(conf->bss[0]->iface));
1909 conf = hostapd_config_alloc(interfaces, buf, ptr);
1910 if (conf == NULL || conf->bss == NULL) {
1911 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1912 "for configuration", __func__);
1916 hapd_iface = hostapd_data_alloc(interfaces, conf);
1917 if (hapd_iface == NULL) {
1918 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1919 "for hostapd", __func__);
1923 if (start_ctrl_iface(hapd_iface) < 0)
1926 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
1932 hostapd_config_free(conf);
1934 if (hapd_iface->bss) {
1935 for (i = 0; i < hapd_iface->num_bss; i++) {
1936 hapd = hapd_iface->bss[i];
1939 if (hapd_iface->interfaces &&
1940 hapd_iface->interfaces->ctrl_iface_deinit)
1941 hapd_iface->interfaces->
1942 ctrl_iface_deinit(hapd);
1943 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1944 __func__, hapd_iface->bss[i],
1947 hapd_iface->bss[i] = NULL;
1949 os_free(hapd_iface->bss);
1951 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1952 __func__, hapd_iface);
1953 os_free(hapd_iface);
1959 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
1963 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
1965 /* Remove hostapd_data only if it has already been initialized */
1966 if (idx < iface->num_bss) {
1967 struct hostapd_data *hapd = iface->bss[idx];
1969 hostapd_bss_deinit(hapd);
1970 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1971 __func__, hapd, hapd->conf->iface);
1972 hostapd_config_free_bss(hapd->conf);
1977 for (i = idx; i < iface->num_bss; i++)
1978 iface->bss[i] = iface->bss[i + 1];
1980 hostapd_config_free_bss(iface->conf->bss[idx]);
1981 iface->conf->bss[idx] = NULL;
1984 iface->conf->num_bss--;
1985 for (i = idx; i < iface->conf->num_bss; i++)
1986 iface->conf->bss[i] = iface->conf->bss[i + 1];
1992 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1994 struct hostapd_iface *hapd_iface;
1997 for (i = 0; i < interfaces->count; i++) {
1998 hapd_iface = interfaces->iface[i];
1999 if (hapd_iface == NULL)
2001 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2002 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2003 hapd_iface->driver_ap_teardown =
2004 !!(hapd_iface->drv_flags &
2005 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2007 hostapd_interface_deinit_free(hapd_iface);
2009 while (k < (interfaces->count - 1)) {
2010 interfaces->iface[k] =
2011 interfaces->iface[k + 1];
2014 interfaces->count--;
2018 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2019 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2020 hapd_iface->driver_ap_teardown =
2021 !(hapd_iface->drv_flags &
2022 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2023 return hostapd_remove_bss(hapd_iface, j);
2032 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2033 * @hapd: Pointer to BSS data
2034 * @sta: Pointer to the associated STA data
2035 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2037 * This function will be called whenever a station associates with the AP. It
2038 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2039 * from drv_callbacks.c based on driver events for drivers that take care of
2040 * management frames (IEEE 802.11 authentication and association) internally.
2042 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2045 if (hapd->tkip_countermeasures) {
2046 hostapd_drv_sta_deauth(hapd, sta->addr,
2047 WLAN_REASON_MICHAEL_MIC_FAILURE);
2051 hostapd_prune_associations(hapd, sta->addr);
2053 /* IEEE 802.11F (IAPP) */
2054 if (hapd->conf->ieee802_11f)
2055 iapp_new_station(hapd->iapp, sta);
2058 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2059 sta->no_p2p_set = 1;
2060 hapd->num_sta_no_p2p++;
2061 if (hapd->num_sta_no_p2p == 1)
2062 hostapd_p2p_non_p2p_sta_connected(hapd);
2064 #endif /* CONFIG_P2P */
2066 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2067 * IEEE 802.1X/WPA code will start accounting after the station has
2068 * been authorized. */
2069 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2070 ap_sta_set_authorized(hapd, sta, 1);
2071 os_get_reltime(&sta->connected_time);
2072 accounting_sta_start(hapd, sta);
2075 /* Start IEEE 802.1X authentication process for new stations */
2076 ieee802_1x_new_station(hapd, sta);
2078 if (sta->auth_alg != WLAN_AUTH_FT &&
2079 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2080 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2082 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2084 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2085 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2086 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2087 __func__, MAC2STR(sta->addr),
2088 hapd->conf->ap_max_inactivity);
2089 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2090 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2091 ap_handle_timer, hapd, sta);
2096 const char * hostapd_state_text(enum hostapd_iface_state s)
2099 case HAPD_IFACE_UNINITIALIZED:
2100 return "UNINITIALIZED";
2101 case HAPD_IFACE_DISABLED:
2103 case HAPD_IFACE_COUNTRY_UPDATE:
2104 return "COUNTRY_UPDATE";
2105 case HAPD_IFACE_ACS:
2107 case HAPD_IFACE_HT_SCAN:
2109 case HAPD_IFACE_DFS:
2111 case HAPD_IFACE_ENABLED:
2119 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2121 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2122 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2123 hostapd_state_text(s));
2130 static void free_beacon_data(struct beacon_data *beacon)
2132 os_free(beacon->head);
2133 beacon->head = NULL;
2134 os_free(beacon->tail);
2135 beacon->tail = NULL;
2136 os_free(beacon->probe_resp);
2137 beacon->probe_resp = NULL;
2138 os_free(beacon->beacon_ies);
2139 beacon->beacon_ies = NULL;
2140 os_free(beacon->proberesp_ies);
2141 beacon->proberesp_ies = NULL;
2142 os_free(beacon->assocresp_ies);
2143 beacon->assocresp_ies = NULL;
2147 static int hostapd_build_beacon_data(struct hostapd_iface *iface,
2148 struct beacon_data *beacon)
2150 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2151 struct wpa_driver_ap_params params;
2153 struct hostapd_data *hapd = iface->bss[0];
2155 os_memset(beacon, 0, sizeof(*beacon));
2156 ret = ieee802_11_build_ap_params(hapd, ¶ms);
2160 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2164 goto free_ap_params;
2167 beacon->head = os_malloc(params.head_len);
2169 goto free_ap_extra_ies;
2171 os_memcpy(beacon->head, params.head, params.head_len);
2172 beacon->head_len = params.head_len;
2174 beacon->tail = os_malloc(params.tail_len);
2178 os_memcpy(beacon->tail, params.tail, params.tail_len);
2179 beacon->tail_len = params.tail_len;
2181 if (params.proberesp != NULL) {
2182 beacon->probe_resp = os_malloc(params.proberesp_len);
2183 if (!beacon->probe_resp)
2186 os_memcpy(beacon->probe_resp, params.proberesp,
2187 params.proberesp_len);
2188 beacon->probe_resp_len = params.proberesp_len;
2191 /* copy the extra ies */
2193 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2194 if (!beacon->beacon_ies)
2197 os_memcpy(beacon->beacon_ies,
2198 beacon_extra->buf, wpabuf_len(beacon_extra));
2199 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2202 if (proberesp_extra) {
2203 beacon->proberesp_ies =
2204 os_malloc(wpabuf_len(proberesp_extra));
2205 if (!beacon->proberesp_ies)
2208 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2209 wpabuf_len(proberesp_extra));
2210 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2213 if (assocresp_extra) {
2214 beacon->assocresp_ies =
2215 os_malloc(wpabuf_len(assocresp_extra));
2216 if (!beacon->assocresp_ies)
2219 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2220 wpabuf_len(assocresp_extra));
2221 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2226 /* if the function fails, the caller should not free beacon data */
2228 free_beacon_data(beacon);
2231 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2234 ieee802_11_free_ap_params(¶ms);
2240 * TODO: This flow currently supports only changing frequency within the
2241 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2242 * width) are not supported.
2244 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2245 struct hostapd_config *conf,
2246 struct hostapd_freq_params *params,
2247 struct hostapd_freq_params *old_params)
2251 if (!params->channel) {
2252 /* check if the new channel is supported by hw */
2253 channel = hostapd_hw_get_channel(hapd, params->freq);
2257 channel = params->channel;
2260 /* if a pointer to old_params is provided we save previous state */
2262 old_params->channel = conf->channel;
2263 old_params->ht_enabled = conf->ieee80211n;
2264 old_params->sec_channel_offset = conf->secondary_channel;
2267 conf->channel = channel;
2268 conf->ieee80211n = params->ht_enabled;
2269 conf->secondary_channel = params->sec_channel_offset;
2271 /* TODO: maybe call here hostapd_config_check here? */
2277 static int hostapd_fill_csa_settings(struct hostapd_iface *iface,
2278 struct csa_settings *settings)
2280 struct hostapd_freq_params old_freq;
2283 os_memset(&old_freq, 0, sizeof(old_freq));
2284 if (!iface || !iface->freq || iface->csa_in_progress)
2287 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2288 &settings->freq_params,
2293 ret = hostapd_build_beacon_data(iface, &settings->beacon_after);
2295 /* change back the configuration */
2296 hostapd_change_config_freq(iface->bss[0], iface->conf,
2302 /* set channel switch parameters for csa ie */
2303 iface->cs_freq_params = settings->freq_params;
2304 iface->cs_count = settings->cs_count;
2305 iface->cs_block_tx = settings->block_tx;
2307 ret = hostapd_build_beacon_data(iface, &settings->beacon_csa);
2309 free_beacon_data(&settings->beacon_after);
2313 settings->counter_offset_beacon = iface->cs_c_off_beacon;
2314 settings->counter_offset_presp = iface->cs_c_off_proberesp;
2320 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2322 os_memset(&hapd->iface->cs_freq_params, 0,
2323 sizeof(hapd->iface->cs_freq_params));
2324 hapd->iface->cs_count = 0;
2325 hapd->iface->cs_block_tx = 0;
2326 hapd->iface->cs_c_off_beacon = 0;
2327 hapd->iface->cs_c_off_proberesp = 0;
2328 hapd->iface->csa_in_progress = 0;
2332 int hostapd_switch_channel(struct hostapd_data *hapd,
2333 struct csa_settings *settings)
2336 ret = hostapd_fill_csa_settings(hapd->iface, settings);
2340 ret = hostapd_drv_switch_channel(hapd, settings);
2341 free_beacon_data(&settings->beacon_csa);
2342 free_beacon_data(&settings->beacon_after);
2345 /* if we failed, clean cs parameters */
2346 hostapd_cleanup_cs_params(hapd);
2350 hapd->iface->csa_in_progress = 1;
2354 #endif /* NEED_AP_MLME */