2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2012, 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 "radius/radius_client.h"
15 #include "radius/radius_das.h"
16 #include "drivers/driver.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"
37 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
38 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
39 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
41 extern int wpa_debug_level;
42 extern struct wpa_driver_ops *wpa_drivers[];
45 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
46 int (*cb)(struct hostapd_iface *iface,
47 void *ctx), void *ctx)
52 for (i = 0; i < interfaces->count; i++) {
53 ret = cb(interfaces->iface[i], ctx);
62 static void hostapd_reload_bss(struct hostapd_data *hapd)
64 #ifndef CONFIG_NO_RADIUS
65 radius_client_reconfig(hapd->radius, hapd->conf->radius);
66 #endif /* CONFIG_NO_RADIUS */
68 if (hostapd_setup_wpa_psk(hapd->conf)) {
69 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
70 "after reloading configuration");
73 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
74 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
76 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
78 if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
79 hostapd_setup_wpa(hapd);
81 wpa_init_keys(hapd->wpa_auth);
82 } else if (hapd->conf->wpa) {
85 hostapd_reconfig_wpa(hapd);
86 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
87 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
88 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
89 "the kernel driver.");
90 } else if (hapd->wpa_auth) {
91 wpa_deinit(hapd->wpa_auth);
92 hapd->wpa_auth = NULL;
93 hostapd_set_privacy(hapd, 0);
94 hostapd_setup_encryption(hapd->conf->iface, hapd);
95 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
98 ieee802_11_set_beacon(hapd);
99 hostapd_update_wps(hapd);
101 if (hapd->conf->ssid.ssid_set &&
102 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
103 hapd->conf->ssid.ssid_len)) {
104 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
105 /* try to continue */
107 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
111 int hostapd_reload_config(struct hostapd_iface *iface)
113 struct hostapd_data *hapd = iface->bss[0];
114 struct hostapd_config *newconf, *oldconf;
117 if (iface->interfaces == NULL ||
118 iface->interfaces->config_read_cb == NULL)
120 newconf = iface->interfaces->config_read_cb(iface->config_fname);
125 * Deauthenticate all stations since the new configuration may not
126 * allow them to use the BSS anymore.
128 for (j = 0; j < iface->num_bss; j++) {
129 hostapd_flush_old_stations(iface->bss[j],
130 WLAN_REASON_PREV_AUTH_NOT_VALID);
131 hostapd_broadcast_wep_clear(iface->bss[j]);
133 #ifndef CONFIG_NO_RADIUS
134 /* TODO: update dynamic data based on changed configuration
135 * items (e.g., open/close sockets, etc.) */
136 radius_client_flush(iface->bss[j]->radius, 0);
137 #endif /* CONFIG_NO_RADIUS */
140 oldconf = hapd->iconf;
141 iface->conf = newconf;
143 for (j = 0; j < iface->num_bss; j++) {
144 hapd = iface->bss[j];
145 hapd->iconf = newconf;
146 hapd->conf = &newconf->bss[j];
147 hostapd_reload_bss(hapd);
150 hostapd_config_free(oldconf);
157 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
162 for (i = 0; i < NUM_WEP_KEYS; i++) {
163 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
164 0, NULL, 0, NULL, 0)) {
165 wpa_printf(MSG_DEBUG, "Failed to clear default "
166 "encryption keys (ifname=%s keyidx=%d)",
170 #ifdef CONFIG_IEEE80211W
171 if (hapd->conf->ieee80211w) {
172 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
173 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
176 wpa_printf(MSG_DEBUG, "Failed to clear "
177 "default mgmt encryption keys "
178 "(ifname=%s keyidx=%d)", ifname, i);
182 #endif /* CONFIG_IEEE80211W */
186 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
188 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
193 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
196 struct hostapd_ssid *ssid = &hapd->conf->ssid;
199 if (ssid->wep.default_len &&
200 hostapd_drv_set_key(hapd->conf->iface,
201 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
202 1, NULL, 0, ssid->wep.key[idx],
203 ssid->wep.len[idx])) {
204 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
208 if (ssid->dyn_vlan_keys) {
210 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
212 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
215 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
221 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP,
222 broadcast_ether_addr, idx, 1,
223 NULL, 0, key->key[idx],
225 wpa_printf(MSG_WARNING, "Could not set "
226 "dynamic VLAN WEP encryption.");
236 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
238 iapp_deinit(hapd->iapp);
240 accounting_deinit(hapd);
241 hostapd_deinit_wpa(hapd);
243 hostapd_acl_deinit(hapd);
244 #ifndef CONFIG_NO_RADIUS
245 radius_client_deinit(hapd->radius);
247 radius_das_deinit(hapd->radius_das);
248 hapd->radius_das = NULL;
249 #endif /* CONFIG_NO_RADIUS */
251 hostapd_deinit_wps(hapd);
253 authsrv_deinit(hapd);
255 if (hapd->interface_added &&
256 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
257 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
261 os_free(hapd->probereq_cb);
262 hapd->probereq_cb = NULL;
265 wpabuf_free(hapd->p2p_beacon_ie);
266 hapd->p2p_beacon_ie = NULL;
267 wpabuf_free(hapd->p2p_probe_resp_ie);
268 hapd->p2p_probe_resp_ie = NULL;
269 #endif /* CONFIG_P2P */
271 wpabuf_free(hapd->time_adv);
273 #ifdef CONFIG_INTERWORKING
274 gas_serv_deinit(hapd);
275 #endif /* CONFIG_INTERWORKING */
278 os_free(hapd->tmp_eap_user.identity);
279 os_free(hapd->tmp_eap_user.password);
280 #endif /* CONFIG_SQLITE */
285 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
286 * @hapd: Pointer to BSS data
288 * This function is used to free all per-BSS data structures and resources.
289 * This gets called in a loop for each BSS between calls to
290 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
291 * is deinitialized. Most of the modules that are initialized in
292 * hostapd_setup_bss() are deinitialized here.
294 static void hostapd_cleanup(struct hostapd_data *hapd)
296 if (hapd->iface->interfaces &&
297 hapd->iface->interfaces->ctrl_iface_deinit)
298 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
299 hostapd_free_hapd_data(hapd);
304 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
305 * @iface: Pointer to interface data
307 * This function is called before per-BSS data structures are deinitialized
308 * with hostapd_cleanup().
310 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
315 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
317 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
318 iface->hw_features = NULL;
319 os_free(iface->current_rates);
320 iface->current_rates = NULL;
321 os_free(iface->basic_rates);
322 iface->basic_rates = NULL;
323 ap_list_deinit(iface);
328 * hostapd_cleanup_iface - Complete per-interface cleanup
329 * @iface: Pointer to interface data
331 * This function is called after per-BSS data structures are deinitialized
332 * with hostapd_cleanup().
334 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
336 hostapd_cleanup_iface_partial(iface);
337 hostapd_config_free(iface->conf);
340 os_free(iface->config_fname);
346 static void hostapd_clear_wep(struct hostapd_data *hapd)
348 if (hapd->drv_priv) {
349 hostapd_set_privacy(hapd, 0);
350 hostapd_broadcast_wep_clear(hapd);
355 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
359 hostapd_broadcast_wep_set(hapd);
361 if (hapd->conf->ssid.wep.default_len) {
362 hostapd_set_privacy(hapd, 1);
367 * When IEEE 802.1X is not enabled, the driver may need to know how to
368 * set authentication algorithms for static WEP.
370 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
372 for (i = 0; i < 4; i++) {
373 if (hapd->conf->ssid.wep.key[i] &&
374 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
375 i == hapd->conf->ssid.wep.idx, NULL, 0,
376 hapd->conf->ssid.wep.key[i],
377 hapd->conf->ssid.wep.len[i])) {
378 wpa_printf(MSG_WARNING, "Could not set WEP "
382 if (hapd->conf->ssid.wep.key[i] &&
383 i == hapd->conf->ssid.wep.idx)
384 hostapd_set_privacy(hapd, 1);
391 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
396 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
399 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
400 if (hostapd_flush(hapd)) {
401 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
405 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
406 os_memset(addr, 0xff, ETH_ALEN);
407 hostapd_drv_sta_deauth(hapd, addr, reason);
408 hostapd_free_stas(hapd);
415 * hostapd_validate_bssid_configuration - Validate BSSID configuration
416 * @iface: Pointer to interface data
417 * Returns: 0 on success, -1 on failure
419 * This function is used to validate that the configured BSSIDs are valid.
421 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
423 u8 mask[ETH_ALEN] = { 0 };
424 struct hostapd_data *hapd = iface->bss[0];
425 unsigned int i = iface->conf->num_bss, bits = 0, j;
428 if (hostapd_drv_none(hapd))
431 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
433 /* Determine the bits necessary to cover the number of BSSIDs. */
434 for (i--; i; i >>= 1)
437 /* Determine the bits necessary to any configured BSSIDs,
438 if they are higher than the number of BSSIDs. */
439 for (j = 0; j < iface->conf->num_bss; j++) {
440 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
446 for (i = 0; i < ETH_ALEN; i++) {
448 iface->conf->bss[j].bssid[i] ^
456 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
462 while (mask[i] != 0) {
472 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
477 os_memset(mask, 0xff, ETH_ALEN);
479 for (i = 5; i > 5 - j; i--)
486 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
487 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
492 for (i = 0; i < ETH_ALEN; i++) {
493 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
494 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
495 " for start address " MACSTR ".",
496 MAC2STR(mask), MAC2STR(hapd->own_addr));
497 wpa_printf(MSG_ERROR, "Start address must be the "
498 "first address in the block (i.e., addr "
499 "AND mask == addr).");
508 static int mac_in_conf(struct hostapd_config *conf, const void *a)
512 for (i = 0; i < conf->num_bss; i++) {
513 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
522 #ifndef CONFIG_NO_RADIUS
524 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
525 struct radius_das_attrs *attr)
532 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
533 struct radius_das_attrs *attr)
535 struct sta_info *sta = NULL;
539 sta = ap_get_sta(hapd, attr->sta_addr);
541 if (sta == NULL && attr->acct_session_id &&
542 attr->acct_session_id_len == 17) {
543 for (sta = hapd->sta_list; sta; sta = sta->next) {
544 os_snprintf(buf, sizeof(buf), "%08X-%08X",
545 sta->acct_session_id_hi,
546 sta->acct_session_id_lo);
547 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
552 if (sta == NULL && attr->cui) {
553 for (sta = hapd->sta_list; sta; sta = sta->next) {
555 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
556 if (cui && wpabuf_len(cui) == attr->cui_len &&
557 os_memcmp(wpabuf_head(cui), attr->cui,
563 if (sta == NULL && attr->user_name) {
564 for (sta = hapd->sta_list; sta; sta = sta->next) {
567 identity = ieee802_1x_get_identity(sta->eapol_sm,
570 identity_len == attr->user_name_len &&
571 os_memcmp(identity, attr->user_name, identity_len)
581 static enum radius_das_res
582 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
584 struct hostapd_data *hapd = ctx;
585 struct sta_info *sta;
587 if (hostapd_das_nas_mismatch(hapd, attr))
588 return RADIUS_DAS_NAS_MISMATCH;
590 sta = hostapd_das_find_sta(hapd, attr);
592 return RADIUS_DAS_SESSION_NOT_FOUND;
594 hostapd_drv_sta_deauth(hapd, sta->addr,
595 WLAN_REASON_PREV_AUTH_NOT_VALID);
596 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
598 return RADIUS_DAS_SUCCESS;
601 #endif /* CONFIG_NO_RADIUS */
605 * hostapd_setup_bss - Per-BSS setup (initialization)
606 * @hapd: Pointer to BSS data
607 * @first: Whether this BSS is the first BSS of an interface
609 * This function is used to initialize all per-BSS data structures and
610 * resources. This gets called in a loop for each BSS when an interface is
611 * initialized. Most of the modules that are initialized here will be
612 * deinitialized in hostapd_cleanup().
614 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
616 struct hostapd_bss_config *conf = hapd->conf;
617 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
618 int ssid_len, set_ssid;
619 char force_ifname[IFNAMSIZ];
620 u8 if_addr[ETH_ALEN];
623 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
624 /* Allocate the next available BSSID. */
626 inc_byte_array(hapd->own_addr, ETH_ALEN);
627 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
629 /* Allocate the configured BSSID. */
630 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
632 if (hostapd_mac_comp(hapd->own_addr,
633 hapd->iface->bss[0]->own_addr) ==
635 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
636 "BSSID set to the MAC address of "
637 "the radio", hapd->conf->iface);
642 hapd->interface_added = 1;
643 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
644 hapd->conf->iface, hapd->own_addr, hapd,
645 &hapd->drv_priv, force_ifname, if_addr,
646 hapd->conf->bridge[0] ? hapd->conf->bridge :
648 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
649 MACSTR ")", MAC2STR(hapd->own_addr));
654 if (conf->wmm_enabled < 0)
655 conf->wmm_enabled = hapd->iconf->ieee80211n;
657 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
658 hostapd_set_privacy(hapd, 0);
660 hostapd_broadcast_wep_clear(hapd);
661 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
665 * Fetch the SSID from the system and use it or,
666 * if one was specified in the config file, verify they
669 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
671 wpa_printf(MSG_ERROR, "Could not read SSID from system");
674 if (conf->ssid.ssid_set) {
676 * If SSID is specified in the config file and it differs
677 * from what is being used then force installation of the
680 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
681 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
684 * No SSID in the config file; just use the one we got
688 conf->ssid.ssid_len = ssid_len;
689 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
692 if (!hostapd_drv_none(hapd)) {
693 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
695 hapd->conf->iface, MAC2STR(hapd->own_addr),
696 wpa_ssid_txt(hapd->conf->ssid.ssid,
697 hapd->conf->ssid.ssid_len));
700 if (hostapd_setup_wpa_psk(conf)) {
701 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
705 /* Set SSID for the kernel driver (to be used in beacon and probe
706 * response frames) */
707 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
708 conf->ssid.ssid_len)) {
709 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
713 if (wpa_debug_level == MSG_MSGDUMP)
714 conf->radius->msg_dumps = 1;
715 #ifndef CONFIG_NO_RADIUS
716 hapd->radius = radius_client_init(hapd, conf->radius);
717 if (hapd->radius == NULL) {
718 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
722 if (hapd->conf->radius_das_port) {
723 struct radius_das_conf das_conf;
724 os_memset(&das_conf, 0, sizeof(das_conf));
725 das_conf.port = hapd->conf->radius_das_port;
726 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
727 das_conf.shared_secret_len =
728 hapd->conf->radius_das_shared_secret_len;
729 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
730 das_conf.time_window = hapd->conf->radius_das_time_window;
731 das_conf.require_event_timestamp =
732 hapd->conf->radius_das_require_event_timestamp;
734 das_conf.disconnect = hostapd_das_disconnect;
735 hapd->radius_das = radius_das_init(&das_conf);
736 if (hapd->radius_das == NULL) {
737 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
742 #endif /* CONFIG_NO_RADIUS */
744 if (hostapd_acl_init(hapd)) {
745 wpa_printf(MSG_ERROR, "ACL initialization failed.");
748 if (hostapd_init_wps(hapd, conf))
751 if (authsrv_init(hapd) < 0)
754 if (ieee802_1x_init(hapd)) {
755 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
759 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
762 if (accounting_init(hapd)) {
763 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
767 if (hapd->conf->ieee802_11f &&
768 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
769 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
774 #ifdef CONFIG_INTERWORKING
775 if (gas_serv_init(hapd)) {
776 wpa_printf(MSG_ERROR, "GAS server initialization failed");
779 #endif /* CONFIG_INTERWORKING */
781 if (hapd->iface->interfaces &&
782 hapd->iface->interfaces->ctrl_iface_init &&
783 hapd->iface->interfaces->ctrl_iface_init(hapd)) {
784 wpa_printf(MSG_ERROR, "Failed to setup control interface");
788 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
789 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
793 ieee802_11_set_beacon(hapd);
795 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
798 if (hapd->driver && hapd->driver->set_operstate)
799 hapd->driver->set_operstate(hapd->drv_priv, 1);
805 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
807 struct hostapd_data *hapd = iface->bss[0];
809 struct hostapd_tx_queue_params *p;
811 for (i = 0; i < NUM_TX_QUEUES; i++) {
812 p = &iface->conf->tx_queue[i];
814 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
815 p->cwmax, p->burst)) {
816 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
817 "parameters for queue %d.", i);
818 /* Continue anyway */
824 static int setup_interface(struct hostapd_iface *iface)
826 struct hostapd_data *hapd = iface->bss[0];
831 * Make sure that all BSSes get configured with a pointer to the same
834 for (i = 1; i < iface->num_bss; i++) {
835 iface->bss[i]->driver = hapd->driver;
836 iface->bss[i]->drv_priv = hapd->drv_priv;
839 if (hostapd_validate_bssid_configuration(iface))
842 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
843 os_memcpy(country, hapd->iconf->country, 3);
845 if (hostapd_set_country(hapd, country) < 0) {
846 wpa_printf(MSG_ERROR, "Failed to set country code");
851 if (hostapd_get_hw_features(iface)) {
852 /* Not all drivers support this yet, so continue without hw
855 int ret = hostapd_select_hw_mode(iface);
857 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
858 "channel. (%d)", ret);
861 ret = hostapd_check_ht_capab(iface);
865 wpa_printf(MSG_DEBUG, "Interface initialization will "
866 "be completed in a callback");
870 return hostapd_setup_interface_complete(iface, 0);
874 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
876 struct hostapd_data *hapd = iface->bss[0];
881 wpa_printf(MSG_ERROR, "Interface initialization failed");
886 wpa_printf(MSG_DEBUG, "Completing interface initialization");
887 if (hapd->iconf->channel) {
888 iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
889 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
891 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
892 hapd->iconf->channel, iface->freq);
894 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
895 hapd->iconf->channel,
896 hapd->iconf->ieee80211n,
897 hapd->iconf->secondary_channel)) {
898 wpa_printf(MSG_ERROR, "Could not set channel for "
904 if (iface->current_mode) {
905 if (hostapd_prepare_rates(iface, iface->current_mode)) {
906 wpa_printf(MSG_ERROR, "Failed to prepare rates "
908 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
909 HOSTAPD_LEVEL_WARNING,
910 "Failed to prepare rates table.");
915 if (hapd->iconf->rts_threshold > -1 &&
916 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
917 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
922 if (hapd->iconf->fragm_threshold > -1 &&
923 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
924 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
925 "for kernel driver");
929 prev_addr = hapd->own_addr;
931 for (j = 0; j < iface->num_bss; j++) {
932 hapd = iface->bss[j];
934 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
935 if (hostapd_setup_bss(hapd, j == 0))
937 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
938 prev_addr = hapd->own_addr;
941 hostapd_tx_queue_params(iface);
945 if (hostapd_driver_commit(hapd) < 0) {
946 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
947 "configuration", __func__);
952 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
953 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
954 * mode), the interface is up only after driver_commit, so initialize
955 * WPS after driver_commit.
957 for (j = 0; j < iface->num_bss; j++) {
958 if (hostapd_init_wps_complete(iface->bss[j]))
962 if (hapd->setup_complete_cb)
963 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
965 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
966 iface->bss[0]->conf->iface);
973 * hostapd_setup_interface - Setup of an interface
974 * @iface: Pointer to interface data.
975 * Returns: 0 on success, -1 on failure
977 * Initializes the driver interface, validates the configuration,
978 * and sets driver parameters based on the configuration.
979 * Flushes old stations, sets the channel, encryption,
980 * beacons, and WDS links based on the configuration.
982 int hostapd_setup_interface(struct hostapd_iface *iface)
986 ret = setup_interface(iface);
988 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
989 iface->bss[0]->conf->iface);
998 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
999 * @hapd_iface: Pointer to interface data
1000 * @conf: Pointer to per-interface configuration
1001 * @bss: Pointer to per-BSS configuration for this BSS
1002 * Returns: Pointer to allocated BSS data
1004 * This function is used to allocate per-BSS data structure. This data will be
1005 * freed after hostapd_cleanup() is called for it during interface
1008 struct hostapd_data *
1009 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1010 struct hostapd_config *conf,
1011 struct hostapd_bss_config *bss)
1013 struct hostapd_data *hapd;
1015 hapd = os_zalloc(sizeof(*hapd));
1019 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1022 hapd->iface = hapd_iface;
1023 hapd->driver = hapd->iconf->driver;
1024 hapd->ctrl_sock = -1;
1030 void hostapd_interface_deinit(struct hostapd_iface *iface)
1037 hostapd_cleanup_iface_pre(iface);
1038 for (j = 0; j < iface->num_bss; j++) {
1039 struct hostapd_data *hapd = iface->bss[j];
1040 hostapd_free_stas(hapd);
1041 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1042 hostapd_clear_wep(hapd);
1043 hostapd_cleanup(hapd);
1048 void hostapd_interface_free(struct hostapd_iface *iface)
1051 for (j = 0; j < iface->num_bss; j++)
1052 os_free(iface->bss[j]);
1053 hostapd_cleanup_iface(iface);
1059 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1061 const struct wpa_driver_ops *driver;
1065 driver = iface->bss[0]->driver;
1066 drv_priv = iface->bss[0]->drv_priv;
1067 hostapd_interface_deinit(iface);
1068 if (driver && driver->hapd_deinit && drv_priv)
1069 driver->hapd_deinit(drv_priv);
1070 hostapd_interface_free(iface);
1074 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1076 if (hapd_iface->bss[0]->drv_priv != NULL) {
1077 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1078 hapd_iface->conf->bss[0].iface);
1082 wpa_printf(MSG_DEBUG, "Enable interface %s",
1083 hapd_iface->conf->bss[0].iface);
1085 if (hapd_iface->interfaces == NULL ||
1086 hapd_iface->interfaces->driver_init == NULL ||
1087 hapd_iface->interfaces->driver_init(hapd_iface) ||
1088 hostapd_setup_interface(hapd_iface)) {
1089 hostapd_interface_deinit_free(hapd_iface);
1096 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1100 wpa_printf(MSG_DEBUG, "Reload interface %s",
1101 hapd_iface->conf->bss[0].iface);
1102 for (j = 0; j < hapd_iface->num_bss; j++) {
1103 hostapd_flush_old_stations(hapd_iface->bss[j],
1104 WLAN_REASON_PREV_AUTH_NOT_VALID);
1106 #ifndef CONFIG_NO_RADIUS
1107 /* TODO: update dynamic data based on changed configuration
1108 * items (e.g., open/close sockets, etc.) */
1109 radius_client_flush(hapd_iface->bss[j]->radius, 0);
1110 #endif /* CONFIG_NO_RADIUS */
1112 hostapd_reload_bss(hapd_iface->bss[j]);
1118 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1121 struct hostapd_bss_config *bss;
1122 const struct wpa_driver_ops *driver;
1125 if (hapd_iface == NULL)
1127 bss = hapd_iface->bss[0]->conf;
1128 driver = hapd_iface->bss[0]->driver;
1129 drv_priv = hapd_iface->bss[0]->drv_priv;
1131 /* whatever hostapd_interface_deinit does */
1132 for (j = 0; j < hapd_iface->num_bss; j++) {
1133 struct hostapd_data *hapd = hapd_iface->bss[j];
1134 hostapd_free_stas(hapd);
1135 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1136 hostapd_clear_wep(hapd);
1137 hostapd_free_hapd_data(hapd);
1140 if (driver && driver->hapd_deinit && drv_priv) {
1141 driver->hapd_deinit(drv_priv);
1142 hapd_iface->bss[0]->drv_priv = NULL;
1145 /* From hostapd_cleanup_iface: These were initialized in
1146 * hostapd_setup_interface and hostapd_setup_interface_complete
1148 hostapd_cleanup_iface_partial(hapd_iface);
1150 bss->wpa_key_mgmt = -1;
1151 bss->wpa_pairwise = -1;
1153 wpa_printf(MSG_DEBUG, "Interface %s disabled", bss->iface);
1158 static struct hostapd_iface *
1159 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1161 struct hostapd_iface **iface, *hapd_iface;
1163 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1164 sizeof(struct hostapd_iface *));
1167 interfaces->iface = iface;
1168 hapd_iface = interfaces->iface[interfaces->count] =
1169 os_zalloc(sizeof(*hapd_iface));
1170 if (hapd_iface == NULL) {
1171 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1172 "the interface", __func__);
1175 interfaces->count++;
1176 hapd_iface->interfaces = interfaces;
1182 static struct hostapd_config *
1183 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1184 const char *ctrl_iface)
1186 struct hostapd_bss_config *bss;
1187 struct hostapd_config *conf;
1189 /* Allocates memory for bss and conf */
1190 conf = hostapd_config_defaults();
1192 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1193 "configuration", __func__);
1197 conf->driver = wpa_drivers[0];
1198 if (conf->driver == NULL) {
1199 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1200 hostapd_config_free(conf);
1204 bss = conf->last_bss = conf->bss;
1206 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1207 bss->ctrl_interface = os_strdup(ctrl_iface);
1208 if (bss->ctrl_interface == NULL) {
1209 hostapd_config_free(conf);
1213 /* Reading configuration file skipped, will be done in SET!
1214 * From reading the configuration till the end has to be done in
1221 static struct hostapd_iface * hostapd_data_alloc(
1222 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1225 struct hostapd_iface *hapd_iface =
1226 interfaces->iface[interfaces->count - 1];
1227 struct hostapd_data *hapd;
1229 hapd_iface->conf = conf;
1230 hapd_iface->num_bss = conf->num_bss;
1232 hapd_iface->bss = os_zalloc(conf->num_bss *
1233 sizeof(struct hostapd_data *));
1234 if (hapd_iface->bss == NULL)
1237 for (i = 0; i < conf->num_bss; i++) {
1238 hapd = hapd_iface->bss[i] =
1239 hostapd_alloc_bss_data(hapd_iface, conf,
1243 hapd->msg_ctx = hapd;
1246 hapd_iface->interfaces = interfaces;
1252 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1254 struct hostapd_config *conf = NULL;
1255 struct hostapd_iface *hapd_iface = NULL;
1259 ptr = os_strchr(buf, ' ');
1264 for (i = 0; i < interfaces->count; i++) {
1265 if (!os_strcmp(interfaces->iface[i]->conf->bss[0].iface,
1267 wpa_printf(MSG_INFO, "Cannot add interface - it "
1273 hapd_iface = hostapd_iface_alloc(interfaces);
1274 if (hapd_iface == NULL) {
1275 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1276 "for interface", __func__);
1280 conf = hostapd_config_alloc(interfaces, buf, ptr);
1282 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1283 "for configuration", __func__);
1287 hapd_iface = hostapd_data_alloc(interfaces, conf);
1288 if (hapd_iface == NULL) {
1289 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1290 "for hostapd", __func__);
1294 if (hapd_iface->interfaces &&
1295 hapd_iface->interfaces->ctrl_iface_init &&
1296 hapd_iface->interfaces->ctrl_iface_init(hapd_iface->bss[0])) {
1297 wpa_printf(MSG_ERROR, "%s: Failed to setup control "
1298 "interface", __func__);
1301 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0].iface);
1307 hostapd_config_free(conf);
1309 os_free(hapd_iface->bss[interfaces->count]);
1310 os_free(hapd_iface);
1316 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1318 struct hostapd_iface *hapd_iface;
1321 for (i = 0; i < interfaces->count; i++) {
1322 hapd_iface = interfaces->iface[i];
1323 if (hapd_iface == NULL)
1325 if (!os_strcmp(hapd_iface->conf->bss[0].iface, buf)) {
1326 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1327 hostapd_interface_deinit_free(hapd_iface);
1329 while (k < (interfaces->count - 1)) {
1330 interfaces->iface[k] =
1331 interfaces->iface[k + 1];
1334 interfaces->count--;
1341 #endif /* HOSTAPD */
1345 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1346 * @hapd: Pointer to BSS data
1347 * @sta: Pointer to the associated STA data
1348 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1350 * This function will be called whenever a station associates with the AP. It
1351 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1352 * from drv_callbacks.c based on driver events for drivers that take care of
1353 * management frames (IEEE 802.11 authentication and association) internally.
1355 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1358 if (hapd->tkip_countermeasures) {
1359 hostapd_drv_sta_deauth(hapd, sta->addr,
1360 WLAN_REASON_MICHAEL_MIC_FAILURE);
1364 hostapd_prune_associations(hapd, sta->addr);
1366 /* IEEE 802.11F (IAPP) */
1367 if (hapd->conf->ieee802_11f)
1368 iapp_new_station(hapd->iapp, sta);
1371 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1372 sta->no_p2p_set = 1;
1373 hapd->num_sta_no_p2p++;
1374 if (hapd->num_sta_no_p2p == 1)
1375 hostapd_p2p_non_p2p_sta_connected(hapd);
1377 #endif /* CONFIG_P2P */
1379 /* Start accounting here, if IEEE 802.1X and WPA are not used.
1380 * IEEE 802.1X/WPA code will start accounting after the station has
1381 * been authorized. */
1382 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1383 os_get_time(&sta->connected_time);
1384 accounting_sta_start(hapd, sta);
1387 /* Start IEEE 802.1X authentication process for new stations */
1388 ieee802_1x_new_station(hapd, sta);
1390 if (sta->auth_alg != WLAN_AUTH_FT &&
1391 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1392 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1394 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
1396 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1397 "for " MACSTR " (%d seconds - ap_max_inactivity)",
1398 __func__, MAC2STR(sta->addr),
1399 hapd->conf->ap_max_inactivity);
1400 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1401 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1402 ap_handle_timer, hapd, sta);