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 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
86 int level, const char *txt, size_t len)
88 struct hostapd_data *hapd = ctx;
89 char *format, *module_str;
91 int conf_syslog_level, conf_stdout_level;
92 unsigned int conf_syslog, conf_stdout;
95 format = os_malloc(maxlen);
99 if (hapd && hapd->conf) {
100 conf_syslog_level = hapd->conf->logger_syslog_level;
101 conf_stdout_level = hapd->conf->logger_stdout_level;
102 conf_syslog = hapd->conf->logger_syslog;
103 conf_stdout = hapd->conf->logger_stdout;
105 conf_syslog_level = conf_stdout_level = 0;
106 conf_syslog = conf_stdout = (unsigned int) -1;
110 case HOSTAPD_MODULE_IEEE80211:
111 module_str = "IEEE 802.11";
113 case HOSTAPD_MODULE_IEEE8021X:
114 module_str = "IEEE 802.1X";
116 case HOSTAPD_MODULE_RADIUS:
117 module_str = "RADIUS";
119 case HOSTAPD_MODULE_WPA:
122 case HOSTAPD_MODULE_DRIVER:
123 module_str = "DRIVER";
125 case HOSTAPD_MODULE_IAPP:
128 case HOSTAPD_MODULE_MLME:
136 if (hapd && hapd->conf && addr)
137 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
138 hapd->conf->iface, MAC2STR(addr),
139 module_str ? " " : "", module_str, txt);
140 else if (hapd && hapd->conf)
141 os_snprintf(format, maxlen, "%s:%s%s %s",
142 hapd->conf->iface, module_str ? " " : "",
145 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
146 MAC2STR(addr), module_str ? " " : "",
149 os_snprintf(format, maxlen, "%s%s%s",
150 module_str, module_str ? ": " : "", txt);
152 if ((conf_stdout & module) && level >= conf_stdout_level) {
153 wpa_debug_print_timestamp();
154 printf("%s\n", format);
157 #ifndef CONFIG_NATIVE_WINDOWS
158 if ((conf_syslog & module) && level >= conf_syslog_level) {
161 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
162 case HOSTAPD_LEVEL_DEBUG:
163 priority = LOG_DEBUG;
165 case HOSTAPD_LEVEL_INFO:
168 case HOSTAPD_LEVEL_NOTICE:
169 priority = LOG_NOTICE;
171 case HOSTAPD_LEVEL_WARNING:
172 priority = LOG_WARNING;
178 syslog(priority, "%s", format);
180 #endif /* CONFIG_NATIVE_WINDOWS */
187 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
188 struct sta_info *sta, void *ctx)
190 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
196 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
198 struct hostapd_data *hapd = ctx;
199 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
200 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
202 #endif /* EAP_SERVER */
206 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
208 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
210 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
215 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
216 struct wpa_auth_config *wconf)
218 wconf->wpa = conf->wpa;
219 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
220 wconf->wpa_pairwise = conf->wpa_pairwise;
221 wconf->wpa_group = conf->wpa_group;
222 wconf->wpa_group_rekey = conf->wpa_group_rekey;
223 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
224 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
225 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
226 wconf->rsn_pairwise = conf->rsn_pairwise;
227 wconf->rsn_preauth = conf->rsn_preauth;
228 wconf->eapol_version = conf->eapol_version;
229 wconf->peerkey = conf->peerkey;
230 wconf->wme_enabled = conf->wme_enabled;
231 wconf->okc = conf->okc;
232 #ifdef CONFIG_IEEE80211W
233 wconf->ieee80211w = conf->ieee80211w;
234 #endif /* CONFIG_IEEE80211W */
235 #ifdef CONFIG_IEEE80211R
236 wconf->ssid_len = conf->ssid.ssid_len;
237 if (wconf->ssid_len > SSID_LEN)
238 wconf->ssid_len = SSID_LEN;
239 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
240 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
241 MOBILITY_DOMAIN_ID_LEN);
242 if (conf->nas_identifier &&
243 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
244 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
245 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
246 wconf->r0_key_holder_len);
248 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
249 wconf->r0_key_lifetime = conf->r0_key_lifetime;
250 wconf->reassociation_deadline = conf->reassociation_deadline;
251 wconf->r0kh_list = conf->r0kh_list;
252 wconf->r1kh_list = conf->r1kh_list;
253 wconf->pmk_r1_push = conf->pmk_r1_push;
254 #endif /* CONFIG_IEEE80211R */
258 int hostapd_reload_config(struct hostapd_iface *iface)
260 struct hostapd_data *hapd = iface->bss[0];
261 struct hostapd_config *newconf, *oldconf;
262 struct wpa_auth_config wpa_auth_conf;
264 newconf = hostapd_config_read(iface->config_fname);
269 * Deauthenticate all stations since the new configuration may not
270 * allow them to use the BSS anymore.
272 hostapd_flush_old_stations(hapd);
274 /* TODO: update dynamic data based on changed configuration
275 * items (e.g., open/close sockets, etc.) */
276 radius_client_flush(hapd->radius, 0);
278 oldconf = hapd->iconf;
279 hapd->iconf = newconf;
280 hapd->conf = &newconf->bss[0];
281 iface->conf = newconf;
283 if (hostapd_setup_wpa_psk(hapd->conf)) {
284 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
285 "after reloading configuration");
288 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
289 hostapd_setup_wpa(hapd);
290 else if (hapd->conf->wpa) {
291 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
292 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
293 } else if (hapd->wpa_auth) {
294 wpa_deinit(hapd->wpa_auth);
295 hapd->wpa_auth = NULL;
296 hostapd_set_privacy(hapd, 0);
297 hostapd_setup_encryption(hapd->conf->iface, hapd);
300 ieee802_11_set_beacon(hapd);
302 hostapd_config_free(oldconf);
304 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
310 #ifndef CONFIG_NATIVE_WINDOWS
312 * handle_reload - SIGHUP handler to reload configuration
314 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
316 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
319 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
322 for (i = 0; i < hapds->count; i++) {
323 if (hostapd_reload_config(hapds->iface[i]) < 0) {
324 wpa_printf(MSG_WARNING, "Failed to read new "
325 "configuration file - continuing with "
333 #ifdef HOSTAPD_DUMP_STATE
335 * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
337 static void hostapd_dump_state(struct hostapd_data *hapd)
341 struct sta_info *sta;
345 if (!hapd->conf->dump_log_name) {
346 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
351 wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
352 hapd->conf->dump_log_name);
353 f = fopen(hapd->conf->dump_log_name, "w");
355 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
356 "writing.", hapd->conf->dump_log_name);
361 fprintf(f, "hostapd state dump - %s", ctime(&now));
362 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
363 "num_sta_no_short_slot_time=%d\n"
364 "num_sta_no_short_preamble=%d\n",
365 hapd->num_sta, hapd->iface->num_sta_non_erp,
366 hapd->iface->num_sta_no_short_slot_time,
367 hapd->iface->num_sta_no_short_preamble);
369 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
370 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
373 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
374 " capability=0x%x listen_interval=%d\n",
377 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
378 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
379 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
380 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
381 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
382 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
384 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
386 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
387 "[SHORT_PREAMBLE]" : ""),
388 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
389 (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
390 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
391 (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
392 (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
393 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
395 sta->listen_interval);
397 fprintf(f, " supported_rates=");
398 for (i = 0; i < sta->supported_rates_len; i++)
399 fprintf(f, "%02x ", sta->supported_rates[i]);
403 " timeout_next=%s\n",
404 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
405 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
408 ieee802_1x_dump_state(f, " ", sta);
411 buf = os_malloc(4096);
413 int count = radius_client_get_mib(hapd->radius, buf, 4096);
416 else if (count > 4095)
419 fprintf(f, "%s", buf);
421 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
424 else if (count > 4095)
427 fprintf(f, "%s", buf);
432 #endif /* HOSTAPD_DUMP_STATE */
435 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
437 #ifdef HOSTAPD_DUMP_STATE
438 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
441 for (i = 0; i < hapds->count; i++) {
442 struct hostapd_iface *hapd_iface = hapds->iface[i];
443 for (j = 0; j < hapd_iface->num_bss; j++)
444 hostapd_dump_state(hapd_iface->bss[j]);
446 #endif /* HOSTAPD_DUMP_STATE */
448 #endif /* CONFIG_NATIVE_WINDOWS */
450 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
455 for (i = 0; i < NUM_WEP_KEYS; i++) {
456 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
457 0, i == 0 ? 1 : 0)) {
458 wpa_printf(MSG_DEBUG, "Failed to clear default "
459 "encryption keys (ifname=%s keyidx=%d)",
463 #ifdef CONFIG_IEEE80211W
464 if (hapd->conf->ieee80211w) {
465 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
466 if (hostapd_set_encryption(ifname, hapd, "none", NULL,
469 wpa_printf(MSG_DEBUG, "Failed to clear "
470 "default mgmt encryption keys "
471 "(ifname=%s keyidx=%d)", ifname, i);
475 #endif /* CONFIG_IEEE80211W */
479 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
481 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
486 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
489 struct hostapd_ssid *ssid = &hapd->conf->ssid;
492 if (ssid->wep.default_len &&
493 hostapd_set_encryption(hapd->conf->iface,
494 hapd, "WEP", NULL, idx,
497 idx == ssid->wep.idx)) {
498 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
502 if (ssid->dyn_vlan_keys) {
504 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
506 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
509 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
515 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
519 wpa_printf(MSG_WARNING, "Could not set "
520 "dynamic VLAN WEP encryption.");
530 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
531 * @hapd: Pointer to BSS data
533 * This function is used to free all per-BSS data structures and resources.
534 * This gets called in a loop for each BSS between calls to
535 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
536 * is deinitialized. Most of the modules that are initialized in
537 * hostapd_setup_bss() are deinitialized here.
539 static void hostapd_cleanup(struct hostapd_data *hapd)
541 hostapd_ctrl_iface_deinit(hapd);
543 os_free(hapd->default_wep_key);
544 hapd->default_wep_key = NULL;
545 iapp_deinit(hapd->iapp);
547 accounting_deinit(hapd);
548 rsn_preauth_iface_deinit(hapd);
549 if (hapd->wpa_auth) {
550 wpa_deinit(hapd->wpa_auth);
551 hapd->wpa_auth = NULL;
553 if (hostapd_set_privacy(hapd, 0)) {
554 wpa_printf(MSG_DEBUG, "Could not disable "
555 "PrivacyInvoked for interface %s",
559 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
560 wpa_printf(MSG_DEBUG, "Could not remove generic "
561 "information element from interface %s",
565 ieee802_1x_deinit(hapd);
567 hostapd_acl_deinit(hapd);
568 radius_client_deinit(hapd->radius);
570 radius_server_deinit(hapd->radius_srv);
571 hapd->radius_srv = NULL;
573 #ifdef CONFIG_IEEE80211R
574 l2_packet_deinit(hapd->l2);
575 #endif /* CONFIG_IEEE80211R */
577 hostapd_deinit_wps(hapd);
579 hostapd_wireless_event_deinit(hapd);
583 tls_deinit(hapd->ssl_ctx);
584 hapd->ssl_ctx = NULL;
586 #endif /* EAP_TLS_FUNCS */
589 if (hapd->eap_sim_db_priv) {
590 eap_sim_db_deinit(hapd->eap_sim_db_priv);
591 hapd->eap_sim_db_priv = NULL;
593 #endif /* EAP_SERVER */
595 if (hapd->interface_added &&
596 hostapd_bss_remove(hapd, hapd->conf->iface)) {
597 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
604 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
605 * @iface: Pointer to interface data
607 * This function is called before per-BSS data structures are deinitialized
608 * with hostapd_cleanup().
610 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
616 * hostapd_cleanup_iface - Complete per-interface cleanup
617 * @iface: Pointer to interface data
619 * This function is called after per-BSS data structures are deinitialized
620 * with hostapd_cleanup().
622 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
624 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
625 iface->hw_features = NULL;
626 os_free(iface->current_rates);
627 iface->current_rates = NULL;
628 ap_list_deinit(iface);
629 hostapd_config_free(iface->conf);
632 os_free(iface->config_fname);
638 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
642 hostapd_broadcast_wep_set(hapd);
644 if (hapd->conf->ssid.wep.default_len)
647 for (i = 0; i < 4; i++) {
648 if (hapd->conf->ssid.wep.key[i] &&
649 hostapd_set_encryption(iface, hapd, "WEP", NULL,
650 i, hapd->conf->ssid.wep.key[i],
651 hapd->conf->ssid.wep.len[i],
652 i == hapd->conf->ssid.wep.idx)) {
653 wpa_printf(MSG_WARNING, "Could not set WEP "
657 if (hapd->conf->ssid.wep.key[i] &&
658 i == hapd->conf->ssid.wep.idx)
659 hostapd_set_privacy(hapd, 1);
666 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
670 if (hostapd_drv_none(hapd))
673 wpa_printf(MSG_DEBUG, "Flushing old station entries");
674 if (hostapd_flush(hapd)) {
675 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
678 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
680 /* New Prism2.5/3 STA firmware versions seem to have issues with this
681 * broadcast deauth frame. This gets the firmware in odd state where
682 * nothing works correctly, so let's skip sending this for the hostap
684 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
686 os_memset(addr, 0xff, ETH_ALEN);
687 hostapd_sta_deauth(hapd, addr,
688 WLAN_REASON_PREV_AUTH_NOT_VALID);
695 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
696 logger_level level, const char *txt)
698 struct hostapd_data *hapd = ctx;
703 hlevel = HOSTAPD_LEVEL_WARNING;
706 hlevel = HOSTAPD_LEVEL_INFO;
710 hlevel = HOSTAPD_LEVEL_DEBUG;
714 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
718 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
721 struct hostapd_data *hapd = ctx;
722 struct sta_info *sta;
724 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
725 "STA " MACSTR " reason %d",
726 __func__, MAC2STR(addr), reason);
728 sta = ap_get_sta(hapd, addr);
729 hostapd_sta_deauth(hapd, addr, reason);
732 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
733 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
734 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
735 sta->timeout_next = STA_REMOVE;
739 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
741 struct hostapd_data *hapd = ctx;
742 michael_mic_failure(hapd, addr, 0);
746 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
747 wpa_eapol_variable var, int value)
749 struct hostapd_data *hapd = ctx;
750 struct sta_info *sta = ap_get_sta(hapd, addr);
754 case WPA_EAPOL_portEnabled:
755 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
757 case WPA_EAPOL_portValid:
758 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
760 case WPA_EAPOL_authorized:
761 ieee802_1x_set_sta_authorized(hapd, sta, value);
763 case WPA_EAPOL_portControl_Auto:
765 sta->eapol_sm->portControl = Auto;
767 case WPA_EAPOL_keyRun:
769 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
771 case WPA_EAPOL_keyAvailable:
773 sta->eapol_sm->eap_if->eapKeyAvailable =
774 value ? TRUE : FALSE;
776 case WPA_EAPOL_keyDone:
778 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
780 case WPA_EAPOL_inc_EapolFramesTx:
782 sta->eapol_sm->dot1xAuthEapolFramesTx++;
788 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
789 wpa_eapol_variable var)
791 struct hostapd_data *hapd = ctx;
792 struct sta_info *sta = ap_get_sta(hapd, addr);
793 if (sta == NULL || sta->eapol_sm == NULL)
796 case WPA_EAPOL_keyRun:
797 return sta->eapol_sm->keyRun;
798 case WPA_EAPOL_keyAvailable:
799 return sta->eapol_sm->eap_if->eapKeyAvailable;
806 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
809 struct hostapd_data *hapd = ctx;
810 return hostapd_get_psk(hapd->conf, addr, prev_psk);
814 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
817 struct hostapd_data *hapd = ctx;
820 struct sta_info *sta;
822 sta = ap_get_sta(hapd, addr);
826 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
832 os_memcpy(msk, key, keylen);
839 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
840 const u8 *addr, int idx, u8 *key,
843 struct hostapd_data *hapd = ctx;
844 const char *ifname = hapd->conf->iface;
847 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
852 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
857 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
860 struct hostapd_data *hapd = ctx;
861 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
865 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
868 struct hostapd_data *hapd = ctx;
869 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
874 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
875 const u8 *data, size_t data_len,
878 struct hostapd_data *hapd = ctx;
879 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
883 static int hostapd_wpa_auth_for_each_sta(
884 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
887 struct hostapd_data *hapd = ctx;
888 struct sta_info *sta;
890 for (sta = hapd->sta_list; sta; sta = sta->next) {
891 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
898 static int hostapd_wpa_auth_for_each_auth(
899 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
902 struct hostapd_data *ohapd;
904 struct hapd_interfaces *interfaces = eloop_get_user_data();
906 for (i = 0; i < interfaces->count; i++) {
907 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
908 ohapd = interfaces->iface[i]->bss[j];
909 if (cb(ohapd->wpa_auth, cb_ctx))
918 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
919 const u8 *data, size_t data_len)
921 struct hostapd_data *hapd = ctx;
923 if (hapd->driver && hapd->driver->send_ether)
924 return hapd->driver->send_ether(hapd->drv_priv, dst,
925 hapd->own_addr, proto,
927 if (hapd->l2 == NULL)
929 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
933 #ifdef CONFIG_IEEE80211R
935 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
936 const u8 *data, size_t data_len)
938 struct hostapd_data *hapd = ctx;
940 struct ieee80211_mgmt *m;
942 struct sta_info *sta;
944 sta = ap_get_sta(hapd, dst);
945 if (sta == NULL || sta->wpa_sm == NULL)
948 m = os_zalloc(sizeof(*m) + data_len);
951 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
952 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
953 WLAN_FC_STYPE_ACTION);
954 os_memcpy(m->da, dst, ETH_ALEN);
955 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
956 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
957 os_memcpy(&m->u, data, data_len);
959 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
965 static struct wpa_state_machine *
966 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
968 struct hostapd_data *hapd = ctx;
969 struct sta_info *sta;
971 sta = ap_sta_add(hapd, sta_addr);
977 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
978 if (sta->wpa_sm == NULL) {
979 ap_free_sta(hapd, sta);
982 sta->auth_alg = WLAN_AUTH_FT;
988 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
991 struct hostapd_data *hapd = ctx;
992 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
995 #endif /* CONFIG_IEEE80211R */
999 * hostapd_validate_bssid_configuration - Validate BSSID configuration
1000 * @iface: Pointer to interface data
1001 * Returns: 0 on success, -1 on failure
1003 * This function is used to validate that the configured BSSIDs are valid.
1005 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1007 u8 mask[ETH_ALEN] = { 0 };
1008 struct hostapd_data *hapd = iface->bss[0];
1009 unsigned int i = iface->conf->num_bss, bits = 0, j;
1012 if (hostapd_drv_none(hapd))
1015 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1017 /* Determine the bits necessary to cover the number of BSSIDs. */
1018 for (i--; i; i >>= 1)
1021 /* Determine the bits necessary to any configured BSSIDs,
1022 if they are higher than the number of BSSIDs. */
1023 for (j = 0; j < iface->conf->num_bss; j++) {
1024 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1027 for (i = 0; i < ETH_ALEN; i++) {
1029 iface->conf->bss[j].bssid[i] ^
1034 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1040 while (mask[i] != 0) {
1052 os_memset(mask, 0xff, ETH_ALEN);
1054 for (i = 5; i > 5 - j; i--)
1060 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1061 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1063 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1068 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1069 MACSTR " for start address " MACSTR ".",
1070 MAC2STR(mask), MAC2STR(hapd->own_addr));
1074 for (i = 0; i < ETH_ALEN; i++) {
1075 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1076 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1077 " for start address " MACSTR ".",
1078 MAC2STR(mask), MAC2STR(hapd->own_addr));
1079 wpa_printf(MSG_ERROR, "Start address must be the "
1080 "first address in the block (i.e., addr "
1081 "AND mask == addr).");
1090 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1094 for (i = 0; i < conf->num_bss; i++) {
1095 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1104 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1106 struct wpa_auth_config _conf;
1107 struct wpa_auth_callbacks cb;
1111 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1112 os_memset(&cb, 0, sizeof(cb));
1114 cb.logger = hostapd_wpa_auth_logger;
1115 cb.disconnect = hostapd_wpa_auth_disconnect;
1116 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1117 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1118 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1119 cb.get_psk = hostapd_wpa_auth_get_psk;
1120 cb.get_msk = hostapd_wpa_auth_get_msk;
1121 cb.set_key = hostapd_wpa_auth_set_key;
1122 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1123 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1124 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1125 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1126 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1127 cb.send_ether = hostapd_wpa_auth_send_ether;
1128 #ifdef CONFIG_IEEE80211R
1129 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1130 cb.add_sta = hostapd_wpa_auth_add_sta;
1131 #endif /* CONFIG_IEEE80211R */
1132 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1133 if (hapd->wpa_auth == NULL) {
1134 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1138 if (hostapd_set_privacy(hapd, 1)) {
1139 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1140 "for interface %s", hapd->conf->iface);
1144 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1145 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1146 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1147 "the kernel driver.");
1151 if (rsn_preauth_iface_init(hapd)) {
1152 wpa_printf(MSG_ERROR, "Initialization of RSN "
1153 "pre-authentication failed.");
1162 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1163 struct hostapd_bss_config *conf)
1165 struct radius_server_conf srv;
1166 os_memset(&srv, 0, sizeof(srv));
1167 srv.client_file = conf->radius_server_clients;
1168 srv.auth_port = conf->radius_server_auth_port;
1169 srv.conf_ctx = conf;
1170 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1171 srv.ssl_ctx = hapd->ssl_ctx;
1172 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1173 srv.eap_fast_a_id = conf->eap_fast_a_id;
1174 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1175 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1176 srv.eap_fast_prov = conf->eap_fast_prov;
1177 srv.pac_key_lifetime = conf->pac_key_lifetime;
1178 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1179 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1180 srv.tnc = conf->tnc;
1181 srv.wps = hapd->wps;
1182 srv.ipv6 = conf->radius_server_ipv6;
1183 srv.get_eap_user = hostapd_radius_get_eap_user;
1184 srv.eap_req_id_text = conf->eap_req_id_text;
1185 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1187 hapd->radius_srv = radius_server_init(&srv);
1188 if (hapd->radius_srv == NULL) {
1189 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1198 * hostapd_setup_bss - Per-BSS setup (initialization)
1199 * @hapd: Pointer to BSS data
1200 * @first: Whether this BSS is the first BSS of an interface
1202 * This function is used to initialize all per-BSS data structures and
1203 * resources. This gets called in a loop for each BSS when an interface is
1204 * initialized. Most of the modules that are initialized here will be
1205 * deinitialized in hostapd_cleanup().
1207 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1209 struct hostapd_bss_config *conf = hapd->conf;
1210 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1211 int ssid_len, set_ssid;
1214 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1215 /* Allocate the next available BSSID. */
1217 inc_byte_array(hapd->own_addr, ETH_ALEN);
1218 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1220 /* Allocate the configured BSSID. */
1221 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1223 if (hostapd_mac_comp(hapd->own_addr,
1224 hapd->iface->bss[0]->own_addr) ==
1226 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1227 "BSSID set to the MAC address of "
1228 "the radio", hapd->conf->iface);
1233 hapd->interface_added = 1;
1234 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1236 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1237 MACSTR ")", MAC2STR(hapd->own_addr));
1243 * Fetch the SSID from the system and use it or,
1244 * if one was specified in the config file, verify they
1247 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1249 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1252 if (conf->ssid.ssid_set) {
1254 * If SSID is specified in the config file and it differs
1255 * from what is being used then force installation of the
1258 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1259 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1262 * No SSID in the config file; just use the one we got
1266 conf->ssid.ssid_len = ssid_len;
1267 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1268 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1271 if (!hostapd_drv_none(hapd)) {
1272 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1274 hapd->conf->iface, MAC2STR(hapd->own_addr),
1275 hapd->conf->ssid.ssid);
1278 if (hostapd_setup_wpa_psk(conf)) {
1279 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1283 /* Set flag for whether SSID is broadcast in beacons */
1284 if (hostapd_set_broadcast_ssid(hapd,
1285 !!hapd->conf->ignore_broadcast_ssid)) {
1286 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1291 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1292 wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
1297 /* Set SSID for the kernel driver (to be used in beacon and probe
1298 * response frames) */
1299 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1300 conf->ssid.ssid_len)) {
1301 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1305 if (wpa_debug_level == MSG_MSGDUMP)
1306 conf->radius->msg_dumps = 1;
1307 hapd->radius = radius_client_init(hapd, conf->radius);
1308 if (hapd->radius == NULL) {
1309 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1313 if (hostapd_acl_init(hapd)) {
1314 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1317 if (hostapd_init_wps(hapd, conf))
1320 if (ieee802_1x_init(hapd)) {
1321 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1325 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1328 if (accounting_init(hapd)) {
1329 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1333 if (hapd->conf->ieee802_11f &&
1334 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1335 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1340 if (hostapd_ctrl_iface_init(hapd)) {
1341 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1345 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1346 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1350 #ifdef CONFIG_IEEE80211R
1351 if (!hostapd_drv_none(hapd)) {
1352 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1353 hostapd_rrb_receive, hapd, 0);
1354 if (hapd->l2 == NULL &&
1355 (hapd->driver == NULL ||
1356 hapd->driver->send_ether == NULL)) {
1357 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1362 #endif /* CONFIG_IEEE80211R */
1364 ieee802_11_set_beacon(hapd);
1366 if (conf->radius_server_clients &&
1367 hostapd_setup_radius_srv(hapd, conf))
1374 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1376 struct hostapd_data *hapd = iface->bss[0];
1378 struct hostapd_tx_queue_params *p;
1380 for (i = 0; i < NUM_TX_QUEUES; i++) {
1381 p = &iface->conf->tx_queue[i];
1386 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1387 p->cwmax, p->burst)) {
1388 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1389 "parameters for queue %d.", i);
1390 /* Continue anyway */
1396 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1397 size_t identity_len, int phase2,
1398 struct eap_user *user)
1400 const struct hostapd_eap_user *eap_user;
1403 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1404 if (eap_user == NULL)
1410 os_memset(user, 0, sizeof(*user));
1411 count = EAP_USER_MAX_METHODS;
1412 if (count > EAP_MAX_METHODS)
1413 count = EAP_MAX_METHODS;
1414 for (i = 0; i < count; i++) {
1415 user->methods[i].vendor = eap_user->methods[i].vendor;
1416 user->methods[i].method = eap_user->methods[i].method;
1419 if (eap_user->password) {
1420 user->password = os_malloc(eap_user->password_len);
1421 if (user->password == NULL)
1423 os_memcpy(user->password, eap_user->password,
1424 eap_user->password_len);
1425 user->password_len = eap_user->password_len;
1426 user->password_hash = eap_user->password_hash;
1428 user->force_version = eap_user->force_version;
1429 user->ttls_auth = eap_user->ttls_auth;
1435 static int setup_interface(struct hostapd_iface *iface)
1437 struct hostapd_data *hapd = iface->bss[0];
1438 struct hostapd_bss_config *conf = hapd->conf;
1441 u8 *b = conf->bssid;
1448 * Initialize the driver interface and make sure that all BSSes get
1449 * configured with a pointer to this driver interface.
1451 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1452 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1454 hapd->drv_priv = hostapd_driver_init(hapd);
1457 if (hapd->drv_priv == NULL) {
1458 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1459 hapd->driver ? hapd->driver->name : "Unknown");
1460 hapd->driver = NULL;
1463 for (i = 0; i < iface->num_bss; i++) {
1464 iface->bss[i]->driver = hapd->driver;
1465 iface->bss[i]->drv_priv = hapd->drv_priv;
1468 if (hostapd_validate_bssid_configuration(iface))
1471 #ifdef CONFIG_IEEE80211N
1472 SET_2BIT_LE16(&iface->ht_op_mode,
1473 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1475 #endif /* CONFIG_IEEE80211N */
1477 os_memcpy(country, hapd->iconf->country, 3);
1479 if (hostapd_set_country(hapd, country) < 0) {
1480 wpa_printf(MSG_ERROR, "Failed to set country code");
1484 if (hapd->iconf->ieee80211d &&
1485 hostapd_set_ieee80211d(hapd, 1) < 0) {
1486 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1487 hapd->iconf->ieee80211d);
1491 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1492 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1493 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1498 /* TODO: merge with hostapd_driver_init() ? */
1499 if (hostapd_wireless_event_init(hapd) < 0)
1502 if (hostapd_get_hw_features(iface)) {
1503 /* Not all drivers support this yet, so continue without hw
1506 int ret = hostapd_select_hw_mode(iface);
1508 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1509 "channel. (%d)", ret);
1514 hostapd_flush_old_stations(hapd);
1515 hostapd_set_privacy(hapd, 0);
1517 if (hapd->iconf->channel) {
1518 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1519 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1520 "Frequency: %d MHz",
1521 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1522 hapd->iconf->channel, freq);
1524 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1525 hapd->iconf->ieee80211n,
1526 hapd->iconf->secondary_channel)) {
1527 wpa_printf(MSG_ERROR, "Could not set channel for "
1533 hostapd_broadcast_wep_clear(hapd);
1534 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1537 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1538 ieee802_11_set_beacon(hapd);
1540 if (hapd->iconf->rts_threshold > -1 &&
1541 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1542 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1547 if (hapd->iconf->fragm_threshold > -1 &&
1548 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1549 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1550 "for kernel driver");
1554 prev_addr = hapd->own_addr;
1556 for (j = 0; j < iface->num_bss; j++) {
1557 hapd = iface->bss[j];
1559 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1560 if (hostapd_setup_bss(hapd, j == 0))
1562 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1563 prev_addr = hapd->own_addr;
1566 hostapd_tx_queue_params(iface);
1568 ap_list_init(iface);
1570 if (hostapd_driver_commit(hapd) < 0) {
1571 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1572 "configuration", __func__);
1581 * hostapd_setup_interface - Setup of an interface
1582 * @iface: Pointer to interface data.
1583 * Returns: 0 on success, -1 on failure
1585 * Initializes the driver interface, validates the configuration,
1586 * and sets driver parameters based on the configuration.
1587 * Flushes old stations, sets the channel, encryption,
1588 * beacons, and WDS links based on the configuration.
1590 static int hostapd_setup_interface(struct hostapd_iface *iface)
1594 ret = setup_interface(iface);
1596 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1597 iface->bss[0]->conf->iface);
1600 } else if (!hostapd_drv_none(iface->bss[0])) {
1601 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1602 iface->bss[0]->conf->iface);
1609 static void show_version(void)
1612 "hostapd v" VERSION_STR "\n"
1613 "User space daemon for IEEE 802.11 AP management,\n"
1614 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1615 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1616 "and contributors\n");
1620 static void usage(void)
1625 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1626 "<configuration file(s)>\n"
1629 " -h show this usage\n"
1630 " -d show more debug messages (-dd for even more)\n"
1631 " -B run daemon in the background\n"
1633 " -K include key data in debug messages\n"
1634 " -t include timestamps in some debug messages\n"
1635 " -v show hostapd version\n");
1642 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1643 * @hapd_iface: Pointer to interface data
1644 * @conf: Pointer to per-interface configuration
1645 * @bss: Pointer to per-BSS configuration for this BSS
1646 * Returns: Pointer to allocated BSS data
1648 * This function is used to allocate per-BSS data structure. This data will be
1649 * freed after hostapd_cleanup() is called for it during interface
1652 static struct hostapd_data *
1653 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1654 struct hostapd_config *conf,
1655 struct hostapd_bss_config *bss)
1657 struct hostapd_data *hapd;
1659 hapd = os_zalloc(sizeof(*hapd));
1665 hapd->iface = hapd_iface;
1667 if (hapd->conf->individual_wep_key_len > 0) {
1668 /* use key0 in individual key and key1 in broadcast key */
1669 hapd->default_wep_key_idx = 1;
1672 #ifdef EAP_TLS_FUNCS
1673 if (hapd->conf->eap_server &&
1674 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1675 hapd->conf->dh_file)) {
1676 struct tls_connection_params params;
1678 hapd->ssl_ctx = tls_init(NULL);
1679 if (hapd->ssl_ctx == NULL) {
1680 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1684 os_memset(¶ms, 0, sizeof(params));
1685 params.ca_cert = hapd->conf->ca_cert;
1686 params.client_cert = hapd->conf->server_cert;
1687 params.private_key = hapd->conf->private_key;
1688 params.private_key_passwd = hapd->conf->private_key_passwd;
1689 params.dh_file = hapd->conf->dh_file;
1691 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1692 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1696 if (tls_global_set_verify(hapd->ssl_ctx,
1697 hapd->conf->check_crl)) {
1698 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1702 #endif /* EAP_TLS_FUNCS */
1705 if (hapd->conf->eap_sim_db) {
1706 hapd->eap_sim_db_priv =
1707 eap_sim_db_init(hapd->conf->eap_sim_db,
1708 hostapd_sim_db_cb, hapd);
1709 if (hapd->eap_sim_db_priv == NULL) {
1710 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1711 "database interface");
1715 #endif /* EAP_SERVER */
1717 hapd->driver = hapd->iconf->driver;
1721 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1724 /* TODO: cleanup allocated resources(?) */
1731 * hostapd_init - Allocate and initialize per-interface data
1732 * @config_file: Path to the configuration file
1733 * Returns: Pointer to the allocated interface data or %NULL on failure
1735 * This function is used to allocate main data structures for per-interface
1736 * data. The allocated data buffer will be freed by calling
1737 * hostapd_cleanup_iface().
1739 static struct hostapd_iface * hostapd_init(const char *config_file)
1741 struct hostapd_iface *hapd_iface = NULL;
1742 struct hostapd_config *conf = NULL;
1743 struct hostapd_data *hapd;
1746 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1747 if (hapd_iface == NULL)
1750 hapd_iface->config_fname = os_strdup(config_file);
1751 if (hapd_iface->config_fname == NULL)
1754 conf = hostapd_config_read(hapd_iface->config_fname);
1757 hapd_iface->conf = conf;
1759 hapd_iface->num_bss = conf->num_bss;
1760 hapd_iface->bss = os_zalloc(conf->num_bss *
1761 sizeof(struct hostapd_data *));
1762 if (hapd_iface->bss == NULL)
1765 for (i = 0; i < conf->num_bss; i++) {
1766 hapd = hapd_iface->bss[i] =
1767 hostapd_alloc_bss_data(hapd_iface, conf,
1777 hostapd_config_free(conf);
1779 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1780 hapd = hapd_iface->bss[i];
1781 if (hapd && hapd->ssl_ctx)
1782 tls_deinit(hapd->ssl_ctx);
1785 os_free(hapd_iface->config_fname);
1786 os_free(hapd_iface->bss);
1787 os_free(hapd_iface);
1793 static int hostapd_global_init(struct hapd_interfaces *interfaces)
1795 hostapd_logger_register_cb(hostapd_logger_cb);
1797 if (eap_server_register_methods()) {
1798 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1802 if (eloop_init(interfaces)) {
1803 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1807 #ifndef CONFIG_NATIVE_WINDOWS
1808 eloop_register_signal(SIGHUP, handle_reload, NULL);
1809 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1810 #endif /* CONFIG_NATIVE_WINDOWS */
1811 eloop_register_signal_terminate(handle_term, NULL);
1813 #ifndef CONFIG_NATIVE_WINDOWS
1814 openlog("hostapd", 0, LOG_DAEMON);
1815 #endif /* CONFIG_NATIVE_WINDOWS */
1821 static void hostapd_global_deinit(const char *pid_file)
1824 tncs_global_deinit();
1825 #endif /* EAP_TNC */
1829 #ifndef CONFIG_NATIVE_WINDOWS
1831 #endif /* CONFIG_NATIVE_WINDOWS */
1833 eap_server_unregister_methods();
1835 os_daemonize_terminate(pid_file);
1839 static void hostapd_interface_deinit(struct hostapd_iface *iface)
1846 hostapd_cleanup_iface_pre(iface);
1847 for (j = 0; j < iface->num_bss; j++) {
1848 struct hostapd_data *hapd = iface->bss[j];
1849 hostapd_free_stas(hapd);
1850 hostapd_flush_old_stations(hapd);
1851 hostapd_cleanup(hapd);
1852 if (j == iface->num_bss - 1 && hapd->driver)
1853 hostapd_driver_deinit(hapd);
1855 for (j = 0; j < iface->num_bss; j++)
1856 os_free(iface->bss[j]);
1857 hostapd_cleanup_iface(iface);
1861 static struct hostapd_iface * hostapd_interface_init(const char *config_fname,
1864 struct hostapd_iface *iface;
1867 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
1868 iface = hostapd_init(config_fname);
1872 for (k = 0; k < debug; k++) {
1873 if (iface->bss[0]->conf->logger_stdout_level > 0)
1874 iface->bss[0]->conf->logger_stdout_level--;
1877 if (hostapd_setup_interface(iface)) {
1878 hostapd_interface_deinit(iface);
1886 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
1887 const char *pid_file)
1893 for (i = 0; !tnc && i < ifaces->count; i++) {
1894 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
1895 if (ifaces->iface[i]->bss[0]->conf->tnc) {
1902 if (tnc && tncs_global_init() < 0) {
1903 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1906 #endif /* EAP_TNC */
1908 if (daemonize && os_daemonize(pid_file)) {
1919 int main(int argc, char *argv[])
1921 struct hapd_interfaces interfaces;
1924 int c, debug = 0, daemonize = 0;
1925 const char *pid_file = NULL;
1928 c = getopt(argc, argv, "BdhKP:tv");
1937 if (wpa_debug_level > 0)
1944 wpa_debug_show_keys++;
1950 wpa_debug_timestamp++;
1966 interfaces.count = argc - optind;
1967 interfaces.iface = os_malloc(interfaces.count *
1968 sizeof(struct hostapd_iface *));
1969 if (interfaces.iface == NULL) {
1970 wpa_printf(MSG_ERROR, "malloc failed\n");
1974 if (hostapd_global_init(&interfaces))
1977 /* Initialize interfaces */
1978 for (i = 0; i < interfaces.count; i++) {
1979 interfaces.iface[i] = hostapd_interface_init(argv[optind + i],
1981 if (!interfaces.iface[i])
1985 if (hostapd_global_run(&interfaces, daemonize, pid_file))
1991 /* Deinitialize all interfaces */
1992 for (i = 0; i < interfaces.count; i++)
1993 hostapd_interface_deinit(interfaces.iface[i]);
1994 os_free(interfaces.iface);
1996 hostapd_global_deinit(pid_file);