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_auth.h"
33 #include "radius/radius_client.h"
34 #include "radius/radius_server.h"
37 #include "vlan_init.h"
38 #include "ctrl_iface.h"
40 #include "eap_server/eap_sim_db.h"
41 #include "eap_server/eap.h"
42 #include "eap_server/tncs.h"
44 #include "l2_packet/l2_packet.h"
45 #include "wps_hostapd.h"
46 #include "tkip_countermeasures.h"
49 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
50 size_t identity_len, int phase2,
51 struct eap_user *user);
52 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
53 static int hostapd_setup_wpa(struct hostapd_data *hapd);
54 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
56 struct hapd_interfaces {
58 struct hostapd_iface **iface;
62 extern int wpa_debug_level;
63 extern int wpa_debug_show_keys;
64 extern int wpa_debug_timestamp;
67 int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
68 void *ctx), void *ctx)
70 struct hapd_interfaces *interfaces = eloop_get_user_data();
74 for (i = 0; i < interfaces->count; i++) {
75 ret = cb(interfaces->iface[i], ctx);
84 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
85 int level, const char *txt, size_t len)
87 struct hostapd_data *hapd = ctx;
88 char *format, *module_str;
90 int conf_syslog_level, conf_stdout_level;
91 unsigned int conf_syslog, conf_stdout;
94 format = os_malloc(maxlen);
98 if (hapd && hapd->conf) {
99 conf_syslog_level = hapd->conf->logger_syslog_level;
100 conf_stdout_level = hapd->conf->logger_stdout_level;
101 conf_syslog = hapd->conf->logger_syslog;
102 conf_stdout = hapd->conf->logger_stdout;
104 conf_syslog_level = conf_stdout_level = 0;
105 conf_syslog = conf_stdout = (unsigned int) -1;
109 case HOSTAPD_MODULE_IEEE80211:
110 module_str = "IEEE 802.11";
112 case HOSTAPD_MODULE_IEEE8021X:
113 module_str = "IEEE 802.1X";
115 case HOSTAPD_MODULE_RADIUS:
116 module_str = "RADIUS";
118 case HOSTAPD_MODULE_WPA:
121 case HOSTAPD_MODULE_DRIVER:
122 module_str = "DRIVER";
124 case HOSTAPD_MODULE_IAPP:
127 case HOSTAPD_MODULE_MLME:
135 if (hapd && hapd->conf && addr)
136 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
137 hapd->conf->iface, MAC2STR(addr),
138 module_str ? " " : "", module_str, txt);
139 else if (hapd && hapd->conf)
140 os_snprintf(format, maxlen, "%s:%s%s %s",
141 hapd->conf->iface, module_str ? " " : "",
144 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
145 MAC2STR(addr), module_str ? " " : "",
148 os_snprintf(format, maxlen, "%s%s%s",
149 module_str, module_str ? ": " : "", txt);
151 if ((conf_stdout & module) && level >= conf_stdout_level) {
152 wpa_debug_print_timestamp();
153 printf("%s\n", format);
156 #ifndef CONFIG_NATIVE_WINDOWS
157 if ((conf_syslog & module) && level >= conf_syslog_level) {
160 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
161 case HOSTAPD_LEVEL_DEBUG:
162 priority = LOG_DEBUG;
164 case HOSTAPD_LEVEL_INFO:
167 case HOSTAPD_LEVEL_NOTICE:
168 priority = LOG_NOTICE;
170 case HOSTAPD_LEVEL_WARNING:
171 priority = LOG_WARNING;
177 syslog(priority, "%s", format);
179 #endif /* CONFIG_NATIVE_WINDOWS */
186 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
187 struct sta_info *sta, void *ctx)
189 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
195 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
197 struct hostapd_data *hapd = ctx;
198 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
199 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
201 #endif /* EAP_SERVER */
205 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
207 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
209 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
214 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
215 struct wpa_auth_config *wconf)
217 wconf->wpa = conf->wpa;
218 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
219 wconf->wpa_pairwise = conf->wpa_pairwise;
220 wconf->wpa_group = conf->wpa_group;
221 wconf->wpa_group_rekey = conf->wpa_group_rekey;
222 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
223 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
224 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
225 wconf->rsn_pairwise = conf->rsn_pairwise;
226 wconf->rsn_preauth = conf->rsn_preauth;
227 wconf->eapol_version = conf->eapol_version;
228 wconf->peerkey = conf->peerkey;
229 wconf->wme_enabled = conf->wme_enabled;
230 wconf->okc = conf->okc;
231 #ifdef CONFIG_IEEE80211W
232 wconf->ieee80211w = conf->ieee80211w;
233 #endif /* CONFIG_IEEE80211W */
234 #ifdef CONFIG_IEEE80211R
235 wconf->ssid_len = conf->ssid.ssid_len;
236 if (wconf->ssid_len > SSID_LEN)
237 wconf->ssid_len = SSID_LEN;
238 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
239 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
240 MOBILITY_DOMAIN_ID_LEN);
241 if (conf->nas_identifier &&
242 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
243 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
244 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
245 wconf->r0_key_holder_len);
247 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
248 wconf->r0_key_lifetime = conf->r0_key_lifetime;
249 wconf->reassociation_deadline = conf->reassociation_deadline;
250 wconf->r0kh_list = conf->r0kh_list;
251 wconf->r1kh_list = conf->r1kh_list;
252 wconf->pmk_r1_push = conf->pmk_r1_push;
253 #endif /* CONFIG_IEEE80211R */
257 int hostapd_reload_config(struct hostapd_iface *iface)
259 struct hostapd_data *hapd = iface->bss[0];
260 struct hostapd_config *newconf, *oldconf;
261 struct wpa_auth_config wpa_auth_conf;
263 newconf = hostapd_config_read(iface->config_fname);
268 * Deauthenticate all stations since the new configuration may not
269 * allow them to use the BSS anymore.
271 hostapd_flush_old_stations(hapd);
273 /* TODO: update dynamic data based on changed configuration
274 * items (e.g., open/close sockets, etc.) */
275 radius_client_flush(hapd->radius, 0);
277 oldconf = hapd->iconf;
278 hapd->iconf = newconf;
279 hapd->conf = &newconf->bss[0];
280 iface->conf = newconf;
282 if (hostapd_setup_wpa_psk(hapd->conf)) {
283 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
284 "after reloading configuration");
287 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
288 hostapd_setup_wpa(hapd);
289 else if (hapd->conf->wpa) {
290 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
291 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
292 } else if (hapd->wpa_auth) {
293 wpa_deinit(hapd->wpa_auth);
294 hapd->wpa_auth = NULL;
295 hostapd_set_privacy(hapd, 0);
296 hostapd_setup_encryption(hapd->conf->iface, hapd);
299 ieee802_11_set_beacon(hapd);
301 hostapd_config_free(oldconf);
303 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
309 #ifndef CONFIG_NATIVE_WINDOWS
311 * handle_reload - SIGHUP handler to reload configuration
313 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
315 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
318 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
321 for (i = 0; i < hapds->count; i++) {
322 if (hostapd_reload_config(hapds->iface[i]) < 0) {
323 wpa_printf(MSG_WARNING, "Failed to read new "
324 "configuration file - continuing with "
332 #ifdef HOSTAPD_DUMP_STATE
334 * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
336 static void hostapd_dump_state(struct hostapd_data *hapd)
340 struct sta_info *sta;
344 if (!hapd->conf->dump_log_name) {
345 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
350 wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
351 hapd->conf->dump_log_name);
352 f = fopen(hapd->conf->dump_log_name, "w");
354 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
355 "writing.", hapd->conf->dump_log_name);
360 fprintf(f, "hostapd state dump - %s", ctime(&now));
361 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
362 "num_sta_no_short_slot_time=%d\n"
363 "num_sta_no_short_preamble=%d\n",
364 hapd->num_sta, hapd->iface->num_sta_non_erp,
365 hapd->iface->num_sta_no_short_slot_time,
366 hapd->iface->num_sta_no_short_preamble);
368 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
369 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
372 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
373 " capability=0x%x listen_interval=%d\n",
376 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
377 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
378 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
379 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
380 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
381 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
383 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
385 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
386 "[SHORT_PREAMBLE]" : ""),
387 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
388 (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
389 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
390 (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
391 (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
392 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
394 sta->listen_interval);
396 fprintf(f, " supported_rates=");
397 for (i = 0; i < sta->supported_rates_len; i++)
398 fprintf(f, "%02x ", sta->supported_rates[i]);
402 " timeout_next=%s\n",
403 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
404 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
407 ieee802_1x_dump_state(f, " ", sta);
410 buf = os_malloc(4096);
412 int count = radius_client_get_mib(hapd->radius, buf, 4096);
415 else if (count > 4095)
418 fprintf(f, "%s", buf);
420 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
423 else if (count > 4095)
426 fprintf(f, "%s", buf);
431 #endif /* HOSTAPD_DUMP_STATE */
434 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
436 #ifdef HOSTAPD_DUMP_STATE
437 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
440 for (i = 0; i < hapds->count; i++) {
441 struct hostapd_iface *hapd_iface = hapds->iface[i];
442 for (j = 0; j < hapd_iface->num_bss; j++)
443 hostapd_dump_state(hapd_iface->bss[j]);
445 #endif /* HOSTAPD_DUMP_STATE */
447 #endif /* CONFIG_NATIVE_WINDOWS */
449 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
454 for (i = 0; i < NUM_WEP_KEYS; i++) {
455 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
456 0, i == 0 ? 1 : 0)) {
457 wpa_printf(MSG_DEBUG, "Failed to clear default "
458 "encryption keys (ifname=%s keyidx=%d)",
462 #ifdef CONFIG_IEEE80211W
463 if (hapd->conf->ieee80211w) {
464 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
465 if (hostapd_set_encryption(ifname, hapd, "none", NULL,
468 wpa_printf(MSG_DEBUG, "Failed to clear "
469 "default mgmt encryption keys "
470 "(ifname=%s keyidx=%d)", ifname, i);
474 #endif /* CONFIG_IEEE80211W */
478 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
480 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
485 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
488 struct hostapd_ssid *ssid = &hapd->conf->ssid;
491 if (ssid->wep.default_len &&
492 hostapd_set_encryption(hapd->conf->iface,
493 hapd, "WEP", NULL, idx,
496 idx == ssid->wep.idx)) {
497 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
501 if (ssid->dyn_vlan_keys) {
503 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
505 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
508 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
514 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
518 wpa_printf(MSG_WARNING, "Could not set "
519 "dynamic VLAN WEP encryption.");
529 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
530 * @hapd: Pointer to BSS data
532 * This function is used to free all per-BSS data structures and resources.
533 * This gets called in a loop for each BSS between calls to
534 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
535 * is deinitialized. Most of the modules that are initialized in
536 * hostapd_setup_bss() are deinitialized here.
538 static void hostapd_cleanup(struct hostapd_data *hapd)
540 hostapd_ctrl_iface_deinit(hapd);
542 os_free(hapd->default_wep_key);
543 hapd->default_wep_key = NULL;
544 iapp_deinit(hapd->iapp);
546 accounting_deinit(hapd);
547 rsn_preauth_iface_deinit(hapd);
548 if (hapd->wpa_auth) {
549 wpa_deinit(hapd->wpa_auth);
550 hapd->wpa_auth = NULL;
552 if (hostapd_set_privacy(hapd, 0)) {
553 wpa_printf(MSG_DEBUG, "Could not disable "
554 "PrivacyInvoked for interface %s",
558 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
559 wpa_printf(MSG_DEBUG, "Could not remove generic "
560 "information element from interface %s",
564 ieee802_1x_deinit(hapd);
566 hostapd_acl_deinit(hapd);
567 radius_client_deinit(hapd->radius);
569 radius_server_deinit(hapd->radius_srv);
570 hapd->radius_srv = NULL;
572 #ifdef CONFIG_IEEE80211R
573 l2_packet_deinit(hapd->l2);
574 #endif /* CONFIG_IEEE80211R */
576 hostapd_deinit_wps(hapd);
578 hostapd_wireless_event_deinit(hapd);
582 tls_deinit(hapd->ssl_ctx);
583 hapd->ssl_ctx = NULL;
585 #endif /* EAP_TLS_FUNCS */
588 if (hapd->eap_sim_db_priv) {
589 eap_sim_db_deinit(hapd->eap_sim_db_priv);
590 hapd->eap_sim_db_priv = NULL;
592 #endif /* EAP_SERVER */
594 if (hapd->interface_added &&
595 hostapd_bss_remove(hapd, hapd->conf->iface)) {
596 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
603 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
604 * @iface: Pointer to interface data
606 * This function is called before per-BSS data structures are deinitialized
607 * with hostapd_cleanup().
609 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
615 * hostapd_cleanup_iface - Complete per-interface cleanup
616 * @iface: Pointer to interface data
618 * This function is called after per-BSS data structures are deinitialized
619 * with hostapd_cleanup().
621 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
623 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
624 iface->hw_features = NULL;
625 os_free(iface->current_rates);
626 iface->current_rates = NULL;
627 ap_list_deinit(iface);
628 hostapd_config_free(iface->conf);
631 os_free(iface->config_fname);
637 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
641 hostapd_broadcast_wep_set(hapd);
643 if (hapd->conf->ssid.wep.default_len)
646 for (i = 0; i < 4; i++) {
647 if (hapd->conf->ssid.wep.key[i] &&
648 hostapd_set_encryption(iface, hapd, "WEP", NULL,
649 i, hapd->conf->ssid.wep.key[i],
650 hapd->conf->ssid.wep.len[i],
651 i == hapd->conf->ssid.wep.idx)) {
652 wpa_printf(MSG_WARNING, "Could not set WEP "
656 if (hapd->conf->ssid.wep.key[i] &&
657 i == hapd->conf->ssid.wep.idx)
658 hostapd_set_privacy(hapd, 1);
665 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
669 if (hostapd_drv_none(hapd))
672 wpa_printf(MSG_DEBUG, "Flushing old station entries");
673 if (hostapd_flush(hapd)) {
674 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
677 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
679 /* New Prism2.5/3 STA firmware versions seem to have issues with this
680 * broadcast deauth frame. This gets the firmware in odd state where
681 * nothing works correctly, so let's skip sending this for the hostap
683 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
685 os_memset(addr, 0xff, ETH_ALEN);
686 hostapd_sta_deauth(hapd, addr,
687 WLAN_REASON_PREV_AUTH_NOT_VALID);
694 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
695 logger_level level, const char *txt)
697 struct hostapd_data *hapd = ctx;
702 hlevel = HOSTAPD_LEVEL_WARNING;
705 hlevel = HOSTAPD_LEVEL_INFO;
709 hlevel = HOSTAPD_LEVEL_DEBUG;
713 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
717 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
720 struct hostapd_data *hapd = ctx;
721 struct sta_info *sta;
723 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
724 "STA " MACSTR " reason %d",
725 __func__, MAC2STR(addr), reason);
727 sta = ap_get_sta(hapd, addr);
728 hostapd_sta_deauth(hapd, addr, reason);
731 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
732 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
733 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
734 sta->timeout_next = STA_REMOVE;
738 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
740 struct hostapd_data *hapd = ctx;
741 michael_mic_failure(hapd, addr, 0);
745 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
746 wpa_eapol_variable var, int value)
748 struct hostapd_data *hapd = ctx;
749 struct sta_info *sta = ap_get_sta(hapd, addr);
753 case WPA_EAPOL_portEnabled:
754 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
756 case WPA_EAPOL_portValid:
757 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
759 case WPA_EAPOL_authorized:
760 ieee802_1x_set_sta_authorized(hapd, sta, value);
762 case WPA_EAPOL_portControl_Auto:
764 sta->eapol_sm->portControl = Auto;
766 case WPA_EAPOL_keyRun:
768 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
770 case WPA_EAPOL_keyAvailable:
772 sta->eapol_sm->eap_if->eapKeyAvailable =
773 value ? TRUE : FALSE;
775 case WPA_EAPOL_keyDone:
777 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
779 case WPA_EAPOL_inc_EapolFramesTx:
781 sta->eapol_sm->dot1xAuthEapolFramesTx++;
787 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
788 wpa_eapol_variable var)
790 struct hostapd_data *hapd = ctx;
791 struct sta_info *sta = ap_get_sta(hapd, addr);
792 if (sta == NULL || sta->eapol_sm == NULL)
795 case WPA_EAPOL_keyRun:
796 return sta->eapol_sm->keyRun;
797 case WPA_EAPOL_keyAvailable:
798 return sta->eapol_sm->eap_if->eapKeyAvailable;
805 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
808 struct hostapd_data *hapd = ctx;
809 return hostapd_get_psk(hapd->conf, addr, prev_psk);
813 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
816 struct hostapd_data *hapd = ctx;
819 struct sta_info *sta;
821 sta = ap_get_sta(hapd, addr);
825 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
831 os_memcpy(msk, key, keylen);
838 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
839 const u8 *addr, int idx, u8 *key,
842 struct hostapd_data *hapd = ctx;
843 const char *ifname = hapd->conf->iface;
846 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
851 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
856 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
859 struct hostapd_data *hapd = ctx;
860 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
864 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
867 struct hostapd_data *hapd = ctx;
868 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
873 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
874 const u8 *data, size_t data_len,
877 struct hostapd_data *hapd = ctx;
878 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
882 static int hostapd_wpa_auth_for_each_sta(
883 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
886 struct hostapd_data *hapd = ctx;
887 struct sta_info *sta;
889 for (sta = hapd->sta_list; sta; sta = sta->next) {
890 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
897 static int hostapd_wpa_auth_for_each_auth(
898 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
901 struct hostapd_data *ohapd;
903 struct hapd_interfaces *interfaces = eloop_get_user_data();
905 for (i = 0; i < interfaces->count; i++) {
906 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
907 ohapd = interfaces->iface[i]->bss[j];
908 if (cb(ohapd->wpa_auth, cb_ctx))
917 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
918 const u8 *data, size_t data_len)
920 struct hostapd_data *hapd = ctx;
922 if (hapd->driver && hapd->driver->send_ether)
923 return hapd->driver->send_ether(hapd->drv_priv, dst,
924 hapd->own_addr, proto,
926 if (hapd->l2 == NULL)
928 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
932 #ifdef CONFIG_IEEE80211R
934 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
935 const u8 *data, size_t data_len)
937 struct hostapd_data *hapd = ctx;
939 struct ieee80211_mgmt *m;
941 struct sta_info *sta;
943 sta = ap_get_sta(hapd, dst);
944 if (sta == NULL || sta->wpa_sm == NULL)
947 m = os_zalloc(sizeof(*m) + data_len);
950 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
951 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
952 WLAN_FC_STYPE_ACTION);
953 os_memcpy(m->da, dst, ETH_ALEN);
954 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
955 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
956 os_memcpy(&m->u, data, data_len);
958 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
964 static struct wpa_state_machine *
965 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
967 struct hostapd_data *hapd = ctx;
968 struct sta_info *sta;
970 sta = ap_sta_add(hapd, sta_addr);
976 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
977 if (sta->wpa_sm == NULL) {
978 ap_free_sta(hapd, sta);
981 sta->auth_alg = WLAN_AUTH_FT;
987 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
990 struct hostapd_data *hapd = ctx;
991 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
994 #endif /* CONFIG_IEEE80211R */
998 * hostapd_validate_bssid_configuration - Validate BSSID configuration
999 * @iface: Pointer to interface data
1000 * Returns: 0 on success, -1 on failure
1002 * This function is used to validate that the configured BSSIDs are valid.
1004 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1006 u8 mask[ETH_ALEN] = { 0 };
1007 struct hostapd_data *hapd = iface->bss[0];
1008 unsigned int i = iface->conf->num_bss, bits = 0, j;
1011 if (hostapd_drv_none(hapd))
1014 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1016 /* Determine the bits necessary to cover the number of BSSIDs. */
1017 for (i--; i; i >>= 1)
1020 /* Determine the bits necessary to any configured BSSIDs,
1021 if they are higher than the number of BSSIDs. */
1022 for (j = 0; j < iface->conf->num_bss; j++) {
1023 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1026 for (i = 0; i < ETH_ALEN; i++) {
1028 iface->conf->bss[j].bssid[i] ^
1033 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1039 while (mask[i] != 0) {
1051 os_memset(mask, 0xff, ETH_ALEN);
1053 for (i = 5; i > 5 - j; i--)
1059 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1060 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1062 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1067 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1068 MACSTR " for start address " MACSTR ".",
1069 MAC2STR(mask), MAC2STR(hapd->own_addr));
1073 for (i = 0; i < ETH_ALEN; i++) {
1074 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1075 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1076 " for start address " MACSTR ".",
1077 MAC2STR(mask), MAC2STR(hapd->own_addr));
1078 wpa_printf(MSG_ERROR, "Start address must be the "
1079 "first address in the block (i.e., addr "
1080 "AND mask == addr).");
1089 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1093 for (i = 0; i < conf->num_bss; i++) {
1094 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1103 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1105 struct wpa_auth_config _conf;
1106 struct wpa_auth_callbacks cb;
1110 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1111 os_memset(&cb, 0, sizeof(cb));
1113 cb.logger = hostapd_wpa_auth_logger;
1114 cb.disconnect = hostapd_wpa_auth_disconnect;
1115 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1116 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1117 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1118 cb.get_psk = hostapd_wpa_auth_get_psk;
1119 cb.get_msk = hostapd_wpa_auth_get_msk;
1120 cb.set_key = hostapd_wpa_auth_set_key;
1121 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1122 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1123 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1124 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1125 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1126 cb.send_ether = hostapd_wpa_auth_send_ether;
1127 #ifdef CONFIG_IEEE80211R
1128 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1129 cb.add_sta = hostapd_wpa_auth_add_sta;
1130 #endif /* CONFIG_IEEE80211R */
1131 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1132 if (hapd->wpa_auth == NULL) {
1133 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1137 if (hostapd_set_privacy(hapd, 1)) {
1138 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1139 "for interface %s", hapd->conf->iface);
1143 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1144 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1145 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1146 "the kernel driver.");
1150 if (rsn_preauth_iface_init(hapd)) {
1151 wpa_printf(MSG_ERROR, "Initialization of RSN "
1152 "pre-authentication failed.");
1161 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1162 struct hostapd_bss_config *conf)
1164 struct radius_server_conf srv;
1165 os_memset(&srv, 0, sizeof(srv));
1166 srv.client_file = conf->radius_server_clients;
1167 srv.auth_port = conf->radius_server_auth_port;
1168 srv.conf_ctx = conf;
1169 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1170 srv.ssl_ctx = hapd->ssl_ctx;
1171 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1172 srv.eap_fast_a_id = conf->eap_fast_a_id;
1173 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1174 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1175 srv.eap_fast_prov = conf->eap_fast_prov;
1176 srv.pac_key_lifetime = conf->pac_key_lifetime;
1177 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1178 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1179 srv.tnc = conf->tnc;
1180 srv.wps = hapd->wps;
1181 srv.ipv6 = conf->radius_server_ipv6;
1182 srv.get_eap_user = hostapd_radius_get_eap_user;
1183 srv.eap_req_id_text = conf->eap_req_id_text;
1184 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1186 hapd->radius_srv = radius_server_init(&srv);
1187 if (hapd->radius_srv == NULL) {
1188 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1197 * hostapd_setup_bss - Per-BSS setup (initialization)
1198 * @hapd: Pointer to BSS data
1199 * @first: Whether this BSS is the first BSS of an interface
1201 * This function is used to initialize all per-BSS data structures and
1202 * resources. This gets called in a loop for each BSS when an interface is
1203 * initialized. Most of the modules that are initialized here will be
1204 * deinitialized in hostapd_cleanup().
1206 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1208 struct hostapd_bss_config *conf = hapd->conf;
1209 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1210 int ssid_len, set_ssid;
1213 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1214 /* Allocate the next available BSSID. */
1216 inc_byte_array(hapd->own_addr, ETH_ALEN);
1217 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1219 /* Allocate the configured BSSID. */
1220 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1222 if (hostapd_mac_comp(hapd->own_addr,
1223 hapd->iface->bss[0]->own_addr) ==
1225 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1226 "BSSID set to the MAC address of "
1227 "the radio", hapd->conf->iface);
1232 hapd->interface_added = 1;
1233 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1235 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1236 MACSTR ")", MAC2STR(hapd->own_addr));
1242 * Fetch the SSID from the system and use it or,
1243 * if one was specified in the config file, verify they
1246 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1248 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1251 if (conf->ssid.ssid_set) {
1253 * If SSID is specified in the config file and it differs
1254 * from what is being used then force installation of the
1257 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1258 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1261 * No SSID in the config file; just use the one we got
1265 conf->ssid.ssid_len = ssid_len;
1266 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1267 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1270 if (!hostapd_drv_none(hapd)) {
1271 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1273 hapd->conf->iface, MAC2STR(hapd->own_addr),
1274 hapd->conf->ssid.ssid);
1277 if (hostapd_setup_wpa_psk(conf)) {
1278 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1282 /* Set flag for whether SSID is broadcast in beacons */
1283 if (hostapd_set_broadcast_ssid(hapd,
1284 !!hapd->conf->ignore_broadcast_ssid)) {
1285 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1290 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1291 wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
1296 /* Set SSID for the kernel driver (to be used in beacon and probe
1297 * response frames) */
1298 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1299 conf->ssid.ssid_len)) {
1300 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1304 if (wpa_debug_level == MSG_MSGDUMP)
1305 conf->radius->msg_dumps = 1;
1306 hapd->radius = radius_client_init(hapd, conf->radius);
1307 if (hapd->radius == NULL) {
1308 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1312 if (hostapd_acl_init(hapd)) {
1313 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1316 if (hostapd_init_wps(hapd, conf))
1319 if (ieee802_1x_init(hapd)) {
1320 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1324 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1327 if (accounting_init(hapd)) {
1328 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1332 if (hapd->conf->ieee802_11f &&
1333 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1334 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1339 if (hostapd_ctrl_iface_init(hapd)) {
1340 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1344 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1345 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1349 #ifdef CONFIG_IEEE80211R
1350 if (!hostapd_drv_none(hapd)) {
1351 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1352 hostapd_rrb_receive, hapd, 0);
1353 if (hapd->l2 == NULL &&
1354 (hapd->driver == NULL ||
1355 hapd->driver->send_ether == NULL)) {
1356 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1361 #endif /* CONFIG_IEEE80211R */
1363 ieee802_11_set_beacon(hapd);
1365 if (conf->radius_server_clients &&
1366 hostapd_setup_radius_srv(hapd, conf))
1373 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1375 struct hostapd_data *hapd = iface->bss[0];
1377 struct hostapd_tx_queue_params *p;
1379 for (i = 0; i < NUM_TX_QUEUES; i++) {
1380 p = &iface->conf->tx_queue[i];
1385 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1386 p->cwmax, p->burst)) {
1387 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1388 "parameters for queue %d.", i);
1389 /* Continue anyway */
1395 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1396 size_t identity_len, int phase2,
1397 struct eap_user *user)
1399 const struct hostapd_eap_user *eap_user;
1402 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1403 if (eap_user == NULL)
1409 os_memset(user, 0, sizeof(*user));
1410 count = EAP_USER_MAX_METHODS;
1411 if (count > EAP_MAX_METHODS)
1412 count = EAP_MAX_METHODS;
1413 for (i = 0; i < count; i++) {
1414 user->methods[i].vendor = eap_user->methods[i].vendor;
1415 user->methods[i].method = eap_user->methods[i].method;
1418 if (eap_user->password) {
1419 user->password = os_malloc(eap_user->password_len);
1420 if (user->password == NULL)
1422 os_memcpy(user->password, eap_user->password,
1423 eap_user->password_len);
1424 user->password_len = eap_user->password_len;
1425 user->password_hash = eap_user->password_hash;
1427 user->force_version = eap_user->force_version;
1428 user->ttls_auth = eap_user->ttls_auth;
1434 static int setup_interface(struct hostapd_iface *iface)
1436 struct hostapd_data *hapd = iface->bss[0];
1437 struct hostapd_bss_config *conf = hapd->conf;
1440 u8 *b = conf->bssid;
1447 * Initialize the driver interface and make sure that all BSSes get
1448 * configured with a pointer to this driver interface.
1450 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1451 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1453 hapd->drv_priv = hostapd_driver_init(hapd);
1456 if (hapd->drv_priv == NULL) {
1457 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1458 hapd->driver ? hapd->driver->name : "Unknown");
1459 hapd->driver = NULL;
1462 for (i = 0; i < iface->num_bss; i++) {
1463 iface->bss[i]->driver = hapd->driver;
1464 iface->bss[i]->drv_priv = hapd->drv_priv;
1467 if (hostapd_validate_bssid_configuration(iface))
1470 #ifdef CONFIG_IEEE80211N
1471 SET_2BIT_LE16(&iface->ht_op_mode,
1472 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1474 #endif /* CONFIG_IEEE80211N */
1476 os_memcpy(country, hapd->iconf->country, 3);
1478 if (hostapd_set_country(hapd, country) < 0) {
1479 wpa_printf(MSG_ERROR, "Failed to set country code");
1483 if (hapd->iconf->ieee80211d &&
1484 hostapd_set_ieee80211d(hapd, 1) < 0) {
1485 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1486 hapd->iconf->ieee80211d);
1490 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1491 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1492 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1497 /* TODO: merge with hostapd_driver_init() ? */
1498 if (hostapd_wireless_event_init(hapd) < 0)
1501 if (hostapd_get_hw_features(iface)) {
1502 /* Not all drivers support this yet, so continue without hw
1505 int ret = hostapd_select_hw_mode(iface);
1507 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1508 "channel. (%d)", ret);
1513 hostapd_flush_old_stations(hapd);
1514 hostapd_set_privacy(hapd, 0);
1516 if (hapd->iconf->channel) {
1517 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1518 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1519 "Frequency: %d MHz",
1520 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1521 hapd->iconf->channel, freq);
1523 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1524 hapd->iconf->ieee80211n,
1525 hapd->iconf->secondary_channel)) {
1526 wpa_printf(MSG_ERROR, "Could not set channel for "
1532 hostapd_broadcast_wep_clear(hapd);
1533 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1536 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1537 ieee802_11_set_beacon(hapd);
1539 if (hapd->iconf->rts_threshold > -1 &&
1540 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1541 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1546 if (hapd->iconf->fragm_threshold > -1 &&
1547 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1548 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1549 "for kernel driver");
1553 prev_addr = hapd->own_addr;
1555 for (j = 0; j < iface->num_bss; j++) {
1556 hapd = iface->bss[j];
1558 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1559 if (hostapd_setup_bss(hapd, j == 0))
1561 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1562 prev_addr = hapd->own_addr;
1565 hostapd_tx_queue_params(iface);
1567 ap_list_init(iface);
1569 if (hostapd_driver_commit(hapd) < 0) {
1570 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1571 "configuration", __func__);
1580 * hostapd_setup_interface - Setup of an interface
1581 * @iface: Pointer to interface data.
1582 * Returns: 0 on success, -1 on failure
1584 * Initializes the driver interface, validates the configuration,
1585 * and sets driver parameters based on the configuration.
1586 * Flushes old stations, sets the channel, encryption,
1587 * beacons, and WDS links based on the configuration.
1589 static int hostapd_setup_interface(struct hostapd_iface *iface)
1593 ret = setup_interface(iface);
1595 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1596 iface->bss[0]->conf->iface);
1599 } else if (!hostapd_drv_none(iface->bss[0])) {
1600 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1601 iface->bss[0]->conf->iface);
1608 static void show_version(void)
1611 "hostapd v" VERSION_STR "\n"
1612 "User space daemon for IEEE 802.11 AP management,\n"
1613 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1614 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1615 "and contributors\n");
1619 static void usage(void)
1624 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1625 "<configuration file(s)>\n"
1628 " -h show this usage\n"
1629 " -d show more debug messages (-dd for even more)\n"
1630 " -B run daemon in the background\n"
1632 " -K include key data in debug messages\n"
1633 " -t include timestamps in some debug messages\n"
1634 " -v show hostapd version\n");
1641 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1642 * @hapd_iface: Pointer to interface data
1643 * @conf: Pointer to per-interface configuration
1644 * @bss: Pointer to per-BSS configuration for this BSS
1645 * Returns: Pointer to allocated BSS data
1647 * This function is used to allocate per-BSS data structure. This data will be
1648 * freed after hostapd_cleanup() is called for it during interface
1651 static struct hostapd_data *
1652 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1653 struct hostapd_config *conf,
1654 struct hostapd_bss_config *bss)
1656 struct hostapd_data *hapd;
1658 hapd = os_zalloc(sizeof(*hapd));
1664 hapd->iface = hapd_iface;
1666 if (hapd->conf->individual_wep_key_len > 0) {
1667 /* use key0 in individual key and key1 in broadcast key */
1668 hapd->default_wep_key_idx = 1;
1671 #ifdef EAP_TLS_FUNCS
1672 if (hapd->conf->eap_server &&
1673 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1674 hapd->conf->dh_file)) {
1675 struct tls_connection_params params;
1677 hapd->ssl_ctx = tls_init(NULL);
1678 if (hapd->ssl_ctx == NULL) {
1679 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1683 os_memset(¶ms, 0, sizeof(params));
1684 params.ca_cert = hapd->conf->ca_cert;
1685 params.client_cert = hapd->conf->server_cert;
1686 params.private_key = hapd->conf->private_key;
1687 params.private_key_passwd = hapd->conf->private_key_passwd;
1688 params.dh_file = hapd->conf->dh_file;
1690 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1691 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1695 if (tls_global_set_verify(hapd->ssl_ctx,
1696 hapd->conf->check_crl)) {
1697 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1701 #endif /* EAP_TLS_FUNCS */
1704 if (hapd->conf->eap_sim_db) {
1705 hapd->eap_sim_db_priv =
1706 eap_sim_db_init(hapd->conf->eap_sim_db,
1707 hostapd_sim_db_cb, hapd);
1708 if (hapd->eap_sim_db_priv == NULL) {
1709 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1710 "database interface");
1714 #endif /* EAP_SERVER */
1716 hapd->driver = hapd->iconf->driver;
1720 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1723 /* TODO: cleanup allocated resources(?) */
1730 * hostapd_init - Allocate and initialize per-interface data
1731 * @config_file: Path to the configuration file
1732 * Returns: Pointer to the allocated interface data or %NULL on failure
1734 * This function is used to allocate main data structures for per-interface
1735 * data. The allocated data buffer will be freed by calling
1736 * hostapd_cleanup_iface().
1738 static struct hostapd_iface * hostapd_init(const char *config_file)
1740 struct hostapd_iface *hapd_iface = NULL;
1741 struct hostapd_config *conf = NULL;
1742 struct hostapd_data *hapd;
1745 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1746 if (hapd_iface == NULL)
1749 hapd_iface->config_fname = os_strdup(config_file);
1750 if (hapd_iface->config_fname == NULL)
1753 conf = hostapd_config_read(hapd_iface->config_fname);
1756 hapd_iface->conf = conf;
1758 hapd_iface->num_bss = conf->num_bss;
1759 hapd_iface->bss = os_zalloc(conf->num_bss *
1760 sizeof(struct hostapd_data *));
1761 if (hapd_iface->bss == NULL)
1764 for (i = 0; i < conf->num_bss; i++) {
1765 hapd = hapd_iface->bss[i] =
1766 hostapd_alloc_bss_data(hapd_iface, conf,
1776 hostapd_config_free(conf);
1778 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1779 hapd = hapd_iface->bss[i];
1780 if (hapd && hapd->ssl_ctx)
1781 tls_deinit(hapd->ssl_ctx);
1784 os_free(hapd_iface->config_fname);
1785 os_free(hapd_iface->bss);
1786 os_free(hapd_iface);
1792 int main(int argc, char *argv[])
1794 struct hapd_interfaces interfaces;
1797 int c, debug = 0, daemonize = 0, tnc = 0;
1798 const char *pid_file = NULL;
1800 hostapd_logger_register_cb(hostapd_logger_cb);
1803 c = getopt(argc, argv, "BdhKP:tv");
1812 if (wpa_debug_level > 0)
1819 wpa_debug_show_keys++;
1825 wpa_debug_timestamp++;
1841 if (eap_server_register_methods()) {
1842 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1846 interfaces.count = argc - optind;
1848 interfaces.iface = os_malloc(interfaces.count *
1849 sizeof(struct hostapd_iface *));
1850 if (interfaces.iface == NULL) {
1851 wpa_printf(MSG_ERROR, "malloc failed\n");
1855 if (eloop_init(&interfaces)) {
1856 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1860 #ifndef CONFIG_NATIVE_WINDOWS
1861 eloop_register_signal(SIGHUP, handle_reload, NULL);
1862 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1863 #endif /* CONFIG_NATIVE_WINDOWS */
1864 eloop_register_signal_terminate(handle_term, NULL);
1866 /* Initialize interfaces */
1867 for (i = 0; i < interfaces.count; i++) {
1868 wpa_printf(MSG_ERROR, "Configuration file: %s",
1870 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1871 if (!interfaces.iface[i])
1873 for (k = 0; k < debug; k++) {
1874 if (interfaces.iface[i]->bss[0]->conf->
1875 logger_stdout_level > 0)
1876 interfaces.iface[i]->bss[0]->conf->
1877 logger_stdout_level--;
1880 ret = hostapd_setup_interface(interfaces.iface[i]);
1884 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
1885 if (interfaces.iface[i]->bss[0]->conf->tnc)
1891 if (tnc && tncs_global_init() < 0) {
1892 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1895 #endif /* EAP_TNC */
1897 if (daemonize && os_daemonize(pid_file)) {
1902 #ifndef CONFIG_NATIVE_WINDOWS
1903 openlog("hostapd", 0, LOG_DAEMON);
1904 #endif /* CONFIG_NATIVE_WINDOWS */
1908 /* Disconnect associated stations from all interfaces and BSSes */
1909 for (i = 0; i < interfaces.count; i++) {
1910 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1911 struct hostapd_data *hapd =
1912 interfaces.iface[i]->bss[j];
1913 hostapd_free_stas(hapd);
1914 hostapd_flush_old_stations(hapd);
1921 /* Deinitialize all interfaces */
1922 for (i = 0; i < interfaces.count; i++) {
1923 if (!interfaces.iface[i])
1925 hostapd_cleanup_iface_pre(interfaces.iface[i]);
1926 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1927 struct hostapd_data *hapd =
1928 interfaces.iface[i]->bss[j];
1929 hostapd_cleanup(hapd);
1930 if (j == interfaces.iface[i]->num_bss - 1 &&
1932 hostapd_driver_deinit(hapd);
1934 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
1935 os_free(interfaces.iface[i]->bss[j]);
1936 hostapd_cleanup_iface(interfaces.iface[i]);
1938 os_free(interfaces.iface);
1941 tncs_global_deinit();
1942 #endif /* EAP_TNC */
1946 #ifndef CONFIG_NATIVE_WINDOWS
1948 #endif /* CONFIG_NATIVE_WINDOWS */
1950 eap_server_unregister_methods();
1952 os_daemonize_terminate(pid_file);