2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #ifndef CONFIG_NATIVE_WINDOWS
18 #endif /* CONFIG_NATIVE_WINDOWS */
22 #include "ieee802_1x.h"
24 #include "hw_features.h"
25 #include "accounting.h"
29 #include "ieee802_11_defs.h"
30 #include "ieee802_11_auth.h"
34 #include "radius/radius_client.h"
35 #include "radius/radius_server.h"
38 #include "vlan_init.h"
39 #include "ctrl_iface.h"
41 #include "eap_server/eap_sim_db.h"
42 #include "eap_server/eap.h"
43 #include "eap_server/tncs.h"
45 #include "l2_packet/l2_packet.h"
46 #include "wps_hostapd.h"
47 #include "tkip_countermeasures.h"
50 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
51 size_t identity_len, int phase2,
52 struct eap_user *user);
53 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
54 static int hostapd_setup_wpa(struct hostapd_data *hapd);
55 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
57 struct hapd_interfaces {
59 struct hostapd_iface **iface;
63 extern int wpa_debug_level;
64 extern int wpa_debug_show_keys;
65 extern int wpa_debug_timestamp;
68 int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
69 void *ctx), void *ctx)
71 struct hapd_interfaces *interfaces = eloop_get_user_data();
75 for (i = 0; i < interfaces->count; i++) {
76 ret = cb(interfaces->iface[i], ctx);
85 #ifndef CONFIG_NO_HOSTAPD_LOGGER
86 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
87 int level, const char *txt, size_t len)
89 struct hostapd_data *hapd = ctx;
90 char *format, *module_str;
92 int conf_syslog_level, conf_stdout_level;
93 unsigned int conf_syslog, conf_stdout;
96 format = os_malloc(maxlen);
100 if (hapd && hapd->conf) {
101 conf_syslog_level = hapd->conf->logger_syslog_level;
102 conf_stdout_level = hapd->conf->logger_stdout_level;
103 conf_syslog = hapd->conf->logger_syslog;
104 conf_stdout = hapd->conf->logger_stdout;
106 conf_syslog_level = conf_stdout_level = 0;
107 conf_syslog = conf_stdout = (unsigned int) -1;
111 case HOSTAPD_MODULE_IEEE80211:
112 module_str = "IEEE 802.11";
114 case HOSTAPD_MODULE_IEEE8021X:
115 module_str = "IEEE 802.1X";
117 case HOSTAPD_MODULE_RADIUS:
118 module_str = "RADIUS";
120 case HOSTAPD_MODULE_WPA:
123 case HOSTAPD_MODULE_DRIVER:
124 module_str = "DRIVER";
126 case HOSTAPD_MODULE_IAPP:
129 case HOSTAPD_MODULE_MLME:
137 if (hapd && hapd->conf && addr)
138 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
139 hapd->conf->iface, MAC2STR(addr),
140 module_str ? " " : "", module_str, txt);
141 else if (hapd && hapd->conf)
142 os_snprintf(format, maxlen, "%s:%s%s %s",
143 hapd->conf->iface, module_str ? " " : "",
146 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
147 MAC2STR(addr), module_str ? " " : "",
150 os_snprintf(format, maxlen, "%s%s%s",
151 module_str, module_str ? ": " : "", txt);
153 if ((conf_stdout & module) && level >= conf_stdout_level) {
154 wpa_debug_print_timestamp();
155 printf("%s\n", format);
158 #ifndef CONFIG_NATIVE_WINDOWS
159 if ((conf_syslog & module) && level >= conf_syslog_level) {
162 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
163 case HOSTAPD_LEVEL_DEBUG:
164 priority = LOG_DEBUG;
166 case HOSTAPD_LEVEL_INFO:
169 case HOSTAPD_LEVEL_NOTICE:
170 priority = LOG_NOTICE;
172 case HOSTAPD_LEVEL_WARNING:
173 priority = LOG_WARNING;
179 syslog(priority, "%s", format);
181 #endif /* CONFIG_NATIVE_WINDOWS */
185 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
189 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
190 struct sta_info *sta, void *ctx)
192 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
198 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
200 struct hostapd_data *hapd = ctx;
201 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
202 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
204 #endif /* EAP_SERVER */
208 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
210 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
212 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
217 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
218 struct wpa_auth_config *wconf)
220 wconf->wpa = conf->wpa;
221 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
222 wconf->wpa_pairwise = conf->wpa_pairwise;
223 wconf->wpa_group = conf->wpa_group;
224 wconf->wpa_group_rekey = conf->wpa_group_rekey;
225 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
226 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
227 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
228 wconf->rsn_pairwise = conf->rsn_pairwise;
229 wconf->rsn_preauth = conf->rsn_preauth;
230 wconf->eapol_version = conf->eapol_version;
231 wconf->peerkey = conf->peerkey;
232 wconf->wme_enabled = conf->wme_enabled;
233 wconf->okc = conf->okc;
234 #ifdef CONFIG_IEEE80211W
235 wconf->ieee80211w = conf->ieee80211w;
236 #endif /* CONFIG_IEEE80211W */
237 #ifdef CONFIG_IEEE80211R
238 wconf->ssid_len = conf->ssid.ssid_len;
239 if (wconf->ssid_len > SSID_LEN)
240 wconf->ssid_len = SSID_LEN;
241 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
242 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
243 MOBILITY_DOMAIN_ID_LEN);
244 if (conf->nas_identifier &&
245 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
246 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
247 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
248 wconf->r0_key_holder_len);
250 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
251 wconf->r0_key_lifetime = conf->r0_key_lifetime;
252 wconf->reassociation_deadline = conf->reassociation_deadline;
253 wconf->r0kh_list = conf->r0kh_list;
254 wconf->r1kh_list = conf->r1kh_list;
255 wconf->pmk_r1_push = conf->pmk_r1_push;
256 #endif /* CONFIG_IEEE80211R */
260 int hostapd_reload_config(struct hostapd_iface *iface)
262 struct hostapd_data *hapd = iface->bss[0];
263 struct hostapd_config *newconf, *oldconf;
264 struct wpa_auth_config wpa_auth_conf;
266 newconf = hostapd_config_read(iface->config_fname);
271 * Deauthenticate all stations since the new configuration may not
272 * allow them to use the BSS anymore.
274 hostapd_flush_old_stations(hapd);
276 /* TODO: update dynamic data based on changed configuration
277 * items (e.g., open/close sockets, etc.) */
278 radius_client_flush(hapd->radius, 0);
280 oldconf = hapd->iconf;
281 hapd->iconf = newconf;
282 hapd->conf = &newconf->bss[0];
283 iface->conf = newconf;
285 if (hostapd_setup_wpa_psk(hapd->conf)) {
286 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
287 "after reloading configuration");
290 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
291 hostapd_setup_wpa(hapd);
292 else if (hapd->conf->wpa) {
293 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
294 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
295 } else if (hapd->wpa_auth) {
296 wpa_deinit(hapd->wpa_auth);
297 hapd->wpa_auth = NULL;
298 hostapd_set_privacy(hapd, 0);
299 hostapd_setup_encryption(hapd->conf->iface, hapd);
302 ieee802_11_set_beacon(hapd);
304 hostapd_config_free(oldconf);
306 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
312 #ifndef CONFIG_NATIVE_WINDOWS
314 * handle_reload - SIGHUP handler to reload configuration
316 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
318 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
321 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
324 for (i = 0; i < hapds->count; i++) {
325 if (hostapd_reload_config(hapds->iface[i]) < 0) {
326 wpa_printf(MSG_WARNING, "Failed to read new "
327 "configuration file - continuing with "
335 #ifdef HOSTAPD_DUMP_STATE
337 * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
339 static void hostapd_dump_state(struct hostapd_data *hapd)
343 struct sta_info *sta;
347 if (!hapd->conf->dump_log_name) {
348 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
353 wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
354 hapd->conf->dump_log_name);
355 f = fopen(hapd->conf->dump_log_name, "w");
357 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
358 "writing.", hapd->conf->dump_log_name);
363 fprintf(f, "hostapd state dump - %s", ctime(&now));
364 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
365 "num_sta_no_short_slot_time=%d\n"
366 "num_sta_no_short_preamble=%d\n",
367 hapd->num_sta, hapd->iface->num_sta_non_erp,
368 hapd->iface->num_sta_no_short_slot_time,
369 hapd->iface->num_sta_no_short_preamble);
371 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
372 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
375 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
376 " capability=0x%x listen_interval=%d\n",
379 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
380 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
381 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
382 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
383 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
384 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
386 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
388 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
389 "[SHORT_PREAMBLE]" : ""),
390 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
391 (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
392 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
393 (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
394 (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
395 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
397 sta->listen_interval);
399 fprintf(f, " supported_rates=");
400 for (i = 0; i < sta->supported_rates_len; i++)
401 fprintf(f, "%02x ", sta->supported_rates[i]);
405 " timeout_next=%s\n",
406 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
407 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
410 ieee802_1x_dump_state(f, " ", sta);
413 buf = os_malloc(4096);
415 int count = radius_client_get_mib(hapd->radius, buf, 4096);
418 else if (count > 4095)
421 fprintf(f, "%s", buf);
423 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
426 else if (count > 4095)
429 fprintf(f, "%s", buf);
434 #endif /* HOSTAPD_DUMP_STATE */
437 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
439 #ifdef HOSTAPD_DUMP_STATE
440 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
443 for (i = 0; i < hapds->count; i++) {
444 struct hostapd_iface *hapd_iface = hapds->iface[i];
445 for (j = 0; j < hapd_iface->num_bss; j++)
446 hostapd_dump_state(hapd_iface->bss[j]);
448 #endif /* HOSTAPD_DUMP_STATE */
450 #endif /* CONFIG_NATIVE_WINDOWS */
452 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
457 for (i = 0; i < NUM_WEP_KEYS; i++) {
458 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
459 0, i == 0 ? 1 : 0)) {
460 wpa_printf(MSG_DEBUG, "Failed to clear default "
461 "encryption keys (ifname=%s keyidx=%d)",
465 #ifdef CONFIG_IEEE80211W
466 if (hapd->conf->ieee80211w) {
467 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
468 if (hostapd_set_encryption(ifname, hapd, "none", NULL,
471 wpa_printf(MSG_DEBUG, "Failed to clear "
472 "default mgmt encryption keys "
473 "(ifname=%s keyidx=%d)", ifname, i);
477 #endif /* CONFIG_IEEE80211W */
481 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
483 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
488 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
491 struct hostapd_ssid *ssid = &hapd->conf->ssid;
494 if (ssid->wep.default_len &&
495 hostapd_set_encryption(hapd->conf->iface,
496 hapd, "WEP", NULL, idx,
499 idx == ssid->wep.idx)) {
500 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
504 if (ssid->dyn_vlan_keys) {
506 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
508 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
511 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
517 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
521 wpa_printf(MSG_WARNING, "Could not set "
522 "dynamic VLAN WEP encryption.");
532 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
533 * @hapd: Pointer to BSS data
535 * This function is used to free all per-BSS data structures and resources.
536 * This gets called in a loop for each BSS between calls to
537 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
538 * is deinitialized. Most of the modules that are initialized in
539 * hostapd_setup_bss() are deinitialized here.
541 static void hostapd_cleanup(struct hostapd_data *hapd)
543 hostapd_ctrl_iface_deinit(hapd);
545 os_free(hapd->default_wep_key);
546 hapd->default_wep_key = NULL;
547 iapp_deinit(hapd->iapp);
549 accounting_deinit(hapd);
550 rsn_preauth_iface_deinit(hapd);
551 if (hapd->wpa_auth) {
552 wpa_deinit(hapd->wpa_auth);
553 hapd->wpa_auth = NULL;
555 if (hostapd_set_privacy(hapd, 0)) {
556 wpa_printf(MSG_DEBUG, "Could not disable "
557 "PrivacyInvoked for interface %s",
561 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
562 wpa_printf(MSG_DEBUG, "Could not remove generic "
563 "information element from interface %s",
567 ieee802_1x_deinit(hapd);
569 hostapd_acl_deinit(hapd);
570 radius_client_deinit(hapd->radius);
572 radius_server_deinit(hapd->radius_srv);
573 hapd->radius_srv = NULL;
575 #ifdef CONFIG_IEEE80211R
576 l2_packet_deinit(hapd->l2);
577 #endif /* CONFIG_IEEE80211R */
579 hostapd_deinit_wps(hapd);
581 hostapd_wireless_event_deinit(hapd);
585 tls_deinit(hapd->ssl_ctx);
586 hapd->ssl_ctx = NULL;
588 #endif /* EAP_TLS_FUNCS */
591 if (hapd->eap_sim_db_priv) {
592 eap_sim_db_deinit(hapd->eap_sim_db_priv);
593 hapd->eap_sim_db_priv = NULL;
595 #endif /* EAP_SERVER */
597 if (hapd->interface_added &&
598 hostapd_bss_remove(hapd, hapd->conf->iface)) {
599 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
606 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
607 * @iface: Pointer to interface data
609 * This function is called before per-BSS data structures are deinitialized
610 * with hostapd_cleanup().
612 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
618 * hostapd_cleanup_iface - Complete per-interface cleanup
619 * @iface: Pointer to interface data
621 * This function is called after per-BSS data structures are deinitialized
622 * with hostapd_cleanup().
624 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
626 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
627 iface->hw_features = NULL;
628 os_free(iface->current_rates);
629 iface->current_rates = NULL;
630 ap_list_deinit(iface);
631 hostapd_config_free(iface->conf);
634 os_free(iface->config_fname);
640 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
644 hostapd_broadcast_wep_set(hapd);
646 if (hapd->conf->ssid.wep.default_len)
649 for (i = 0; i < 4; i++) {
650 if (hapd->conf->ssid.wep.key[i] &&
651 hostapd_set_encryption(iface, hapd, "WEP", NULL,
652 i, hapd->conf->ssid.wep.key[i],
653 hapd->conf->ssid.wep.len[i],
654 i == hapd->conf->ssid.wep.idx)) {
655 wpa_printf(MSG_WARNING, "Could not set WEP "
659 if (hapd->conf->ssid.wep.key[i] &&
660 i == hapd->conf->ssid.wep.idx)
661 hostapd_set_privacy(hapd, 1);
668 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
672 if (hostapd_drv_none(hapd))
675 wpa_printf(MSG_DEBUG, "Flushing old station entries");
676 if (hostapd_flush(hapd)) {
677 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
680 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
682 /* New Prism2.5/3 STA firmware versions seem to have issues with this
683 * broadcast deauth frame. This gets the firmware in odd state where
684 * nothing works correctly, so let's skip sending this for the hostap
686 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
688 os_memset(addr, 0xff, ETH_ALEN);
689 hostapd_sta_deauth(hapd, addr,
690 WLAN_REASON_PREV_AUTH_NOT_VALID);
697 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
698 logger_level level, const char *txt)
700 #ifndef CONFIG_NO_HOSTAPD_LOGGER
701 struct hostapd_data *hapd = ctx;
706 hlevel = HOSTAPD_LEVEL_WARNING;
709 hlevel = HOSTAPD_LEVEL_INFO;
713 hlevel = HOSTAPD_LEVEL_DEBUG;
717 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
718 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
722 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
725 struct hostapd_data *hapd = ctx;
726 struct sta_info *sta;
728 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
729 "STA " MACSTR " reason %d",
730 __func__, MAC2STR(addr), reason);
732 sta = ap_get_sta(hapd, addr);
733 hostapd_sta_deauth(hapd, addr, reason);
736 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
737 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
738 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
739 sta->timeout_next = STA_REMOVE;
743 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
745 struct hostapd_data *hapd = ctx;
746 michael_mic_failure(hapd, addr, 0);
750 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
751 wpa_eapol_variable var, int value)
753 struct hostapd_data *hapd = ctx;
754 struct sta_info *sta = ap_get_sta(hapd, addr);
758 case WPA_EAPOL_portEnabled:
759 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
761 case WPA_EAPOL_portValid:
762 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
764 case WPA_EAPOL_authorized:
765 ieee802_1x_set_sta_authorized(hapd, sta, value);
767 case WPA_EAPOL_portControl_Auto:
769 sta->eapol_sm->portControl = Auto;
771 case WPA_EAPOL_keyRun:
773 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
775 case WPA_EAPOL_keyAvailable:
777 sta->eapol_sm->eap_if->eapKeyAvailable =
778 value ? TRUE : FALSE;
780 case WPA_EAPOL_keyDone:
782 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
784 case WPA_EAPOL_inc_EapolFramesTx:
786 sta->eapol_sm->dot1xAuthEapolFramesTx++;
792 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
793 wpa_eapol_variable var)
795 struct hostapd_data *hapd = ctx;
796 struct sta_info *sta = ap_get_sta(hapd, addr);
797 if (sta == NULL || sta->eapol_sm == NULL)
800 case WPA_EAPOL_keyRun:
801 return sta->eapol_sm->keyRun;
802 case WPA_EAPOL_keyAvailable:
803 return sta->eapol_sm->eap_if->eapKeyAvailable;
810 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
813 struct hostapd_data *hapd = ctx;
814 return hostapd_get_psk(hapd->conf, addr, prev_psk);
818 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
821 struct hostapd_data *hapd = ctx;
824 struct sta_info *sta;
826 sta = ap_get_sta(hapd, addr);
830 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
836 os_memcpy(msk, key, keylen);
843 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
844 const u8 *addr, int idx, u8 *key,
847 struct hostapd_data *hapd = ctx;
848 const char *ifname = hapd->conf->iface;
851 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
856 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
861 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
864 struct hostapd_data *hapd = ctx;
865 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
869 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
872 struct hostapd_data *hapd = ctx;
873 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
878 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
879 const u8 *data, size_t data_len,
882 struct hostapd_data *hapd = ctx;
883 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
887 static int hostapd_wpa_auth_for_each_sta(
888 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
891 struct hostapd_data *hapd = ctx;
892 struct sta_info *sta;
894 for (sta = hapd->sta_list; sta; sta = sta->next) {
895 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
902 static int hostapd_wpa_auth_for_each_auth(
903 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
906 struct hostapd_data *ohapd;
908 struct hapd_interfaces *interfaces = eloop_get_user_data();
910 for (i = 0; i < interfaces->count; i++) {
911 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
912 ohapd = interfaces->iface[i]->bss[j];
913 if (cb(ohapd->wpa_auth, cb_ctx))
922 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
923 const u8 *data, size_t data_len)
925 struct hostapd_data *hapd = ctx;
927 if (hapd->driver && hapd->driver->send_ether)
928 return hapd->driver->send_ether(hapd->drv_priv, dst,
929 hapd->own_addr, proto,
931 if (hapd->l2 == NULL)
933 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
937 #ifdef CONFIG_IEEE80211R
939 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
940 const u8 *data, size_t data_len)
942 struct hostapd_data *hapd = ctx;
944 struct ieee80211_mgmt *m;
946 struct sta_info *sta;
948 sta = ap_get_sta(hapd, dst);
949 if (sta == NULL || sta->wpa_sm == NULL)
952 m = os_zalloc(sizeof(*m) + data_len);
955 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
956 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
957 WLAN_FC_STYPE_ACTION);
958 os_memcpy(m->da, dst, ETH_ALEN);
959 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
960 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
961 os_memcpy(&m->u, data, data_len);
963 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
969 static struct wpa_state_machine *
970 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
972 struct hostapd_data *hapd = ctx;
973 struct sta_info *sta;
975 sta = ap_sta_add(hapd, sta_addr);
981 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
982 if (sta->wpa_sm == NULL) {
983 ap_free_sta(hapd, sta);
986 sta->auth_alg = WLAN_AUTH_FT;
992 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
995 struct hostapd_data *hapd = ctx;
996 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
999 #endif /* CONFIG_IEEE80211R */
1003 * hostapd_validate_bssid_configuration - Validate BSSID configuration
1004 * @iface: Pointer to interface data
1005 * Returns: 0 on success, -1 on failure
1007 * This function is used to validate that the configured BSSIDs are valid.
1009 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1011 u8 mask[ETH_ALEN] = { 0 };
1012 struct hostapd_data *hapd = iface->bss[0];
1013 unsigned int i = iface->conf->num_bss, bits = 0, j;
1016 if (hostapd_drv_none(hapd))
1019 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1021 /* Determine the bits necessary to cover the number of BSSIDs. */
1022 for (i--; i; i >>= 1)
1025 /* Determine the bits necessary to any configured BSSIDs,
1026 if they are higher than the number of BSSIDs. */
1027 for (j = 0; j < iface->conf->num_bss; j++) {
1028 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1031 for (i = 0; i < ETH_ALEN; i++) {
1033 iface->conf->bss[j].bssid[i] ^
1038 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1044 while (mask[i] != 0) {
1056 os_memset(mask, 0xff, ETH_ALEN);
1058 for (i = 5; i > 5 - j; i--)
1064 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1065 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1067 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1072 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1073 MACSTR " for start address " MACSTR ".",
1074 MAC2STR(mask), MAC2STR(hapd->own_addr));
1078 for (i = 0; i < ETH_ALEN; i++) {
1079 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1080 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1081 " for start address " MACSTR ".",
1082 MAC2STR(mask), MAC2STR(hapd->own_addr));
1083 wpa_printf(MSG_ERROR, "Start address must be the "
1084 "first address in the block (i.e., addr "
1085 "AND mask == addr).");
1094 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1098 for (i = 0; i < conf->num_bss; i++) {
1099 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1108 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1110 struct wpa_auth_config _conf;
1111 struct wpa_auth_callbacks cb;
1115 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1116 os_memset(&cb, 0, sizeof(cb));
1118 cb.logger = hostapd_wpa_auth_logger;
1119 cb.disconnect = hostapd_wpa_auth_disconnect;
1120 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1121 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1122 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1123 cb.get_psk = hostapd_wpa_auth_get_psk;
1124 cb.get_msk = hostapd_wpa_auth_get_msk;
1125 cb.set_key = hostapd_wpa_auth_set_key;
1126 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1127 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1128 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1129 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1130 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1131 cb.send_ether = hostapd_wpa_auth_send_ether;
1132 #ifdef CONFIG_IEEE80211R
1133 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1134 cb.add_sta = hostapd_wpa_auth_add_sta;
1135 #endif /* CONFIG_IEEE80211R */
1136 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1137 if (hapd->wpa_auth == NULL) {
1138 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1142 if (hostapd_set_privacy(hapd, 1)) {
1143 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1144 "for interface %s", hapd->conf->iface);
1148 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1149 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1150 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1151 "the kernel driver.");
1155 if (rsn_preauth_iface_init(hapd)) {
1156 wpa_printf(MSG_ERROR, "Initialization of RSN "
1157 "pre-authentication failed.");
1166 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1167 struct hostapd_bss_config *conf)
1169 struct radius_server_conf srv;
1170 os_memset(&srv, 0, sizeof(srv));
1171 srv.client_file = conf->radius_server_clients;
1172 srv.auth_port = conf->radius_server_auth_port;
1173 srv.conf_ctx = conf;
1174 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1175 srv.ssl_ctx = hapd->ssl_ctx;
1176 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1177 srv.eap_fast_a_id = conf->eap_fast_a_id;
1178 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1179 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1180 srv.eap_fast_prov = conf->eap_fast_prov;
1181 srv.pac_key_lifetime = conf->pac_key_lifetime;
1182 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1183 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1184 srv.tnc = conf->tnc;
1185 srv.wps = hapd->wps;
1186 srv.ipv6 = conf->radius_server_ipv6;
1187 srv.get_eap_user = hostapd_radius_get_eap_user;
1188 srv.eap_req_id_text = conf->eap_req_id_text;
1189 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1191 hapd->radius_srv = radius_server_init(&srv);
1192 if (hapd->radius_srv == NULL) {
1193 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1202 * hostapd_setup_bss - Per-BSS setup (initialization)
1203 * @hapd: Pointer to BSS data
1204 * @first: Whether this BSS is the first BSS of an interface
1206 * This function is used to initialize all per-BSS data structures and
1207 * resources. This gets called in a loop for each BSS when an interface is
1208 * initialized. Most of the modules that are initialized here will be
1209 * deinitialized in hostapd_cleanup().
1211 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1213 struct hostapd_bss_config *conf = hapd->conf;
1214 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1215 int ssid_len, set_ssid;
1218 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1219 /* Allocate the next available BSSID. */
1221 inc_byte_array(hapd->own_addr, ETH_ALEN);
1222 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1224 /* Allocate the configured BSSID. */
1225 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1227 if (hostapd_mac_comp(hapd->own_addr,
1228 hapd->iface->bss[0]->own_addr) ==
1230 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1231 "BSSID set to the MAC address of "
1232 "the radio", hapd->conf->iface);
1237 hapd->interface_added = 1;
1238 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1240 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1241 MACSTR ")", MAC2STR(hapd->own_addr));
1247 * Fetch the SSID from the system and use it or,
1248 * if one was specified in the config file, verify they
1251 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1253 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1256 if (conf->ssid.ssid_set) {
1258 * If SSID is specified in the config file and it differs
1259 * from what is being used then force installation of the
1262 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1263 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1266 * No SSID in the config file; just use the one we got
1270 conf->ssid.ssid_len = ssid_len;
1271 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1272 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1275 if (!hostapd_drv_none(hapd)) {
1276 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1278 hapd->conf->iface, MAC2STR(hapd->own_addr),
1279 hapd->conf->ssid.ssid);
1282 if (hostapd_setup_wpa_psk(conf)) {
1283 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1287 /* Set flag for whether SSID is broadcast in beacons */
1288 if (hostapd_set_broadcast_ssid(hapd,
1289 !!hapd->conf->ignore_broadcast_ssid)) {
1290 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1295 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1296 wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
1301 /* Set SSID for the kernel driver (to be used in beacon and probe
1302 * response frames) */
1303 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1304 conf->ssid.ssid_len)) {
1305 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1309 if (wpa_debug_level == MSG_MSGDUMP)
1310 conf->radius->msg_dumps = 1;
1311 hapd->radius = radius_client_init(hapd, conf->radius);
1312 if (hapd->radius == NULL) {
1313 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1317 if (hostapd_acl_init(hapd)) {
1318 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1321 if (hostapd_init_wps(hapd, conf))
1324 if (ieee802_1x_init(hapd)) {
1325 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1329 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1332 if (accounting_init(hapd)) {
1333 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1337 if (hapd->conf->ieee802_11f &&
1338 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1339 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1344 if (hostapd_ctrl_iface_init(hapd)) {
1345 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1349 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1350 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1354 #ifdef CONFIG_IEEE80211R
1355 if (!hostapd_drv_none(hapd)) {
1356 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1357 hostapd_rrb_receive, hapd, 0);
1358 if (hapd->l2 == NULL &&
1359 (hapd->driver == NULL ||
1360 hapd->driver->send_ether == NULL)) {
1361 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1366 #endif /* CONFIG_IEEE80211R */
1368 ieee802_11_set_beacon(hapd);
1370 if (conf->radius_server_clients &&
1371 hostapd_setup_radius_srv(hapd, conf))
1378 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1380 struct hostapd_data *hapd = iface->bss[0];
1382 struct hostapd_tx_queue_params *p;
1384 for (i = 0; i < NUM_TX_QUEUES; i++) {
1385 p = &iface->conf->tx_queue[i];
1390 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1391 p->cwmax, p->burst)) {
1392 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1393 "parameters for queue %d.", i);
1394 /* Continue anyway */
1400 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1401 size_t identity_len, int phase2,
1402 struct eap_user *user)
1404 const struct hostapd_eap_user *eap_user;
1407 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1408 if (eap_user == NULL)
1414 os_memset(user, 0, sizeof(*user));
1415 count = EAP_USER_MAX_METHODS;
1416 if (count > EAP_MAX_METHODS)
1417 count = EAP_MAX_METHODS;
1418 for (i = 0; i < count; i++) {
1419 user->methods[i].vendor = eap_user->methods[i].vendor;
1420 user->methods[i].method = eap_user->methods[i].method;
1423 if (eap_user->password) {
1424 user->password = os_malloc(eap_user->password_len);
1425 if (user->password == NULL)
1427 os_memcpy(user->password, eap_user->password,
1428 eap_user->password_len);
1429 user->password_len = eap_user->password_len;
1430 user->password_hash = eap_user->password_hash;
1432 user->force_version = eap_user->force_version;
1433 user->ttls_auth = eap_user->ttls_auth;
1439 static int setup_interface(struct hostapd_iface *iface)
1441 struct hostapd_data *hapd = iface->bss[0];
1442 struct hostapd_bss_config *conf = hapd->conf;
1445 u8 *b = conf->bssid;
1452 * Initialize the driver interface and make sure that all BSSes get
1453 * configured with a pointer to this driver interface.
1455 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1456 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1458 hapd->drv_priv = hostapd_driver_init(hapd);
1461 if (hapd->drv_priv == NULL) {
1462 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1463 hapd->driver ? hapd->driver->name : "Unknown");
1464 hapd->driver = NULL;
1467 for (i = 0; i < iface->num_bss; i++) {
1468 iface->bss[i]->driver = hapd->driver;
1469 iface->bss[i]->drv_priv = hapd->drv_priv;
1472 if (hostapd_validate_bssid_configuration(iface))
1475 #ifdef CONFIG_IEEE80211N
1476 SET_2BIT_LE16(&iface->ht_op_mode,
1477 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1479 #endif /* CONFIG_IEEE80211N */
1481 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1482 os_memcpy(country, hapd->iconf->country, 3);
1484 if (hostapd_set_country(hapd, country) < 0) {
1485 wpa_printf(MSG_ERROR, "Failed to set country code");
1490 if (hapd->iconf->ieee80211d &&
1491 hostapd_set_ieee80211d(hapd, 1) < 0) {
1492 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1493 hapd->iconf->ieee80211d);
1497 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1498 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1499 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1504 /* TODO: merge with hostapd_driver_init() ? */
1505 if (hostapd_wireless_event_init(hapd) < 0)
1508 if (hostapd_get_hw_features(iface)) {
1509 /* Not all drivers support this yet, so continue without hw
1512 int ret = hostapd_select_hw_mode(iface);
1514 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1515 "channel. (%d)", ret);
1520 hostapd_flush_old_stations(hapd);
1521 hostapd_set_privacy(hapd, 0);
1523 if (hapd->iconf->channel) {
1524 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1525 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1526 "Frequency: %d MHz",
1527 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1528 hapd->iconf->channel, freq);
1530 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1531 hapd->iconf->ieee80211n,
1532 hapd->iconf->secondary_channel)) {
1533 wpa_printf(MSG_ERROR, "Could not set channel for "
1539 hostapd_broadcast_wep_clear(hapd);
1540 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1543 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1544 ieee802_11_set_beacon(hapd);
1546 if (hapd->iconf->rts_threshold > -1 &&
1547 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1548 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1553 if (hapd->iconf->fragm_threshold > -1 &&
1554 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1555 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1556 "for kernel driver");
1560 prev_addr = hapd->own_addr;
1562 for (j = 0; j < iface->num_bss; j++) {
1563 hapd = iface->bss[j];
1565 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1566 if (hostapd_setup_bss(hapd, j == 0))
1568 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1569 prev_addr = hapd->own_addr;
1572 hostapd_tx_queue_params(iface);
1574 ap_list_init(iface);
1576 if (hostapd_driver_commit(hapd) < 0) {
1577 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1578 "configuration", __func__);
1587 * hostapd_setup_interface - Setup of an interface
1588 * @iface: Pointer to interface data.
1589 * Returns: 0 on success, -1 on failure
1591 * Initializes the driver interface, validates the configuration,
1592 * and sets driver parameters based on the configuration.
1593 * Flushes old stations, sets the channel, encryption,
1594 * beacons, and WDS links based on the configuration.
1596 static int hostapd_setup_interface(struct hostapd_iface *iface)
1600 ret = setup_interface(iface);
1602 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1603 iface->bss[0]->conf->iface);
1606 } else if (!hostapd_drv_none(iface->bss[0])) {
1607 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1608 iface->bss[0]->conf->iface);
1615 static void show_version(void)
1618 "hostapd v" VERSION_STR "\n"
1619 "User space daemon for IEEE 802.11 AP management,\n"
1620 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1621 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1622 "and contributors\n");
1626 static void usage(void)
1631 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1632 "<configuration file(s)>\n"
1635 " -h show this usage\n"
1636 " -d show more debug messages (-dd for even more)\n"
1637 " -B run daemon in the background\n"
1639 " -K include key data in debug messages\n"
1640 " -t include timestamps in some debug messages\n"
1641 " -v show hostapd version\n");
1648 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1649 * @hapd_iface: Pointer to interface data
1650 * @conf: Pointer to per-interface configuration
1651 * @bss: Pointer to per-BSS configuration for this BSS
1652 * Returns: Pointer to allocated BSS data
1654 * This function is used to allocate per-BSS data structure. This data will be
1655 * freed after hostapd_cleanup() is called for it during interface
1658 static struct hostapd_data *
1659 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1660 struct hostapd_config *conf,
1661 struct hostapd_bss_config *bss)
1663 struct hostapd_data *hapd;
1665 hapd = os_zalloc(sizeof(*hapd));
1671 hapd->iface = hapd_iface;
1673 if (hapd->conf->individual_wep_key_len > 0) {
1674 /* use key0 in individual key and key1 in broadcast key */
1675 hapd->default_wep_key_idx = 1;
1678 #ifdef EAP_TLS_FUNCS
1679 if (hapd->conf->eap_server &&
1680 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1681 hapd->conf->dh_file)) {
1682 struct tls_connection_params params;
1684 hapd->ssl_ctx = tls_init(NULL);
1685 if (hapd->ssl_ctx == NULL) {
1686 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1690 os_memset(¶ms, 0, sizeof(params));
1691 params.ca_cert = hapd->conf->ca_cert;
1692 params.client_cert = hapd->conf->server_cert;
1693 params.private_key = hapd->conf->private_key;
1694 params.private_key_passwd = hapd->conf->private_key_passwd;
1695 params.dh_file = hapd->conf->dh_file;
1697 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1698 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1702 if (tls_global_set_verify(hapd->ssl_ctx,
1703 hapd->conf->check_crl)) {
1704 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1708 #endif /* EAP_TLS_FUNCS */
1711 if (hapd->conf->eap_sim_db) {
1712 hapd->eap_sim_db_priv =
1713 eap_sim_db_init(hapd->conf->eap_sim_db,
1714 hostapd_sim_db_cb, hapd);
1715 if (hapd->eap_sim_db_priv == NULL) {
1716 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1717 "database interface");
1721 #endif /* EAP_SERVER */
1723 hapd->driver = hapd->iconf->driver;
1727 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1730 /* TODO: cleanup allocated resources(?) */
1737 * hostapd_init - Allocate and initialize per-interface data
1738 * @config_file: Path to the configuration file
1739 * Returns: Pointer to the allocated interface data or %NULL on failure
1741 * This function is used to allocate main data structures for per-interface
1742 * data. The allocated data buffer will be freed by calling
1743 * hostapd_cleanup_iface().
1745 static struct hostapd_iface * hostapd_init(const char *config_file)
1747 struct hostapd_iface *hapd_iface = NULL;
1748 struct hostapd_config *conf = NULL;
1749 struct hostapd_data *hapd;
1752 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1753 if (hapd_iface == NULL)
1756 hapd_iface->config_fname = os_strdup(config_file);
1757 if (hapd_iface->config_fname == NULL)
1760 conf = hostapd_config_read(hapd_iface->config_fname);
1763 hapd_iface->conf = conf;
1765 hapd_iface->num_bss = conf->num_bss;
1766 hapd_iface->bss = os_zalloc(conf->num_bss *
1767 sizeof(struct hostapd_data *));
1768 if (hapd_iface->bss == NULL)
1771 for (i = 0; i < conf->num_bss; i++) {
1772 hapd = hapd_iface->bss[i] =
1773 hostapd_alloc_bss_data(hapd_iface, conf,
1783 hostapd_config_free(conf);
1785 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1786 hapd = hapd_iface->bss[i];
1787 if (hapd && hapd->ssl_ctx)
1788 tls_deinit(hapd->ssl_ctx);
1791 os_free(hapd_iface->config_fname);
1792 os_free(hapd_iface->bss);
1793 os_free(hapd_iface);
1799 static int hostapd_global_init(struct hapd_interfaces *interfaces)
1801 hostapd_logger_register_cb(hostapd_logger_cb);
1803 if (eap_server_register_methods()) {
1804 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1808 if (eloop_init(interfaces)) {
1809 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1813 #ifndef CONFIG_NATIVE_WINDOWS
1814 eloop_register_signal(SIGHUP, handle_reload, NULL);
1815 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1816 #endif /* CONFIG_NATIVE_WINDOWS */
1817 eloop_register_signal_terminate(handle_term, NULL);
1819 #ifndef CONFIG_NATIVE_WINDOWS
1820 openlog("hostapd", 0, LOG_DAEMON);
1821 #endif /* CONFIG_NATIVE_WINDOWS */
1827 static void hostapd_global_deinit(const char *pid_file)
1830 tncs_global_deinit();
1831 #endif /* EAP_TNC */
1835 #ifndef CONFIG_NATIVE_WINDOWS
1837 #endif /* CONFIG_NATIVE_WINDOWS */
1839 eap_server_unregister_methods();
1841 os_daemonize_terminate(pid_file);
1845 static void hostapd_interface_deinit(struct hostapd_iface *iface)
1852 hostapd_cleanup_iface_pre(iface);
1853 for (j = 0; j < iface->num_bss; j++) {
1854 struct hostapd_data *hapd = iface->bss[j];
1855 hostapd_free_stas(hapd);
1856 hostapd_flush_old_stations(hapd);
1857 hostapd_cleanup(hapd);
1858 if (j == iface->num_bss - 1 && hapd->driver)
1859 hostapd_driver_deinit(hapd);
1861 for (j = 0; j < iface->num_bss; j++)
1862 os_free(iface->bss[j]);
1863 hostapd_cleanup_iface(iface);
1867 static struct hostapd_iface * hostapd_interface_init(const char *config_fname,
1870 struct hostapd_iface *iface;
1873 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
1874 iface = hostapd_init(config_fname);
1878 for (k = 0; k < debug; k++) {
1879 if (iface->bss[0]->conf->logger_stdout_level > 0)
1880 iface->bss[0]->conf->logger_stdout_level--;
1883 if (hostapd_setup_interface(iface)) {
1884 hostapd_interface_deinit(iface);
1892 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
1893 const char *pid_file)
1899 for (i = 0; !tnc && i < ifaces->count; i++) {
1900 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
1901 if (ifaces->iface[i]->bss[0]->conf->tnc) {
1908 if (tnc && tncs_global_init() < 0) {
1909 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1912 #endif /* EAP_TNC */
1914 if (daemonize && os_daemonize(pid_file)) {
1925 int main(int argc, char *argv[])
1927 struct hapd_interfaces interfaces;
1930 int c, debug = 0, daemonize = 0;
1931 const char *pid_file = NULL;
1934 c = getopt(argc, argv, "BdhKP:tv");
1943 if (wpa_debug_level > 0)
1950 wpa_debug_show_keys++;
1956 wpa_debug_timestamp++;
1972 interfaces.count = argc - optind;
1973 interfaces.iface = os_malloc(interfaces.count *
1974 sizeof(struct hostapd_iface *));
1975 if (interfaces.iface == NULL) {
1976 wpa_printf(MSG_ERROR, "malloc failed\n");
1980 if (hostapd_global_init(&interfaces))
1983 /* Initialize interfaces */
1984 for (i = 0; i < interfaces.count; i++) {
1985 interfaces.iface[i] = hostapd_interface_init(argv[optind + i],
1987 if (!interfaces.iface[i])
1991 if (hostapd_global_run(&interfaces, daemonize, pid_file))
1997 /* Deinitialize all interfaces */
1998 for (i = 0; i < interfaces.count; i++)
1999 hostapd_interface_deinit(interfaces.iface[i]);
2000 os_free(interfaces.iface);
2002 hostapd_global_deinit(pid_file);