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"
28 #include "ieee802_11_defs.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 #ifndef CONFIG_NO_HOSTAPD_LOGGER
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 */
184 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
188 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
189 struct sta_info *sta, void *ctx)
191 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
197 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
199 struct hostapd_data *hapd = ctx;
200 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
201 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
203 #endif /* EAP_SERVER */
207 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
209 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
211 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
216 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
217 struct wpa_auth_config *wconf)
219 wconf->wpa = conf->wpa;
220 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
221 wconf->wpa_pairwise = conf->wpa_pairwise;
222 wconf->wpa_group = conf->wpa_group;
223 wconf->wpa_group_rekey = conf->wpa_group_rekey;
224 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
225 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
226 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
227 wconf->rsn_pairwise = conf->rsn_pairwise;
228 wconf->rsn_preauth = conf->rsn_preauth;
229 wconf->eapol_version = conf->eapol_version;
230 wconf->peerkey = conf->peerkey;
231 wconf->wmm_enabled = conf->wmm_enabled;
232 wconf->okc = conf->okc;
233 #ifdef CONFIG_IEEE80211W
234 wconf->ieee80211w = conf->ieee80211w;
235 #endif /* CONFIG_IEEE80211W */
236 #ifdef CONFIG_IEEE80211R
237 wconf->ssid_len = conf->ssid.ssid_len;
238 if (wconf->ssid_len > SSID_LEN)
239 wconf->ssid_len = SSID_LEN;
240 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
241 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
242 MOBILITY_DOMAIN_ID_LEN);
243 if (conf->nas_identifier &&
244 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
245 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
246 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
247 wconf->r0_key_holder_len);
249 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
250 wconf->r0_key_lifetime = conf->r0_key_lifetime;
251 wconf->reassociation_deadline = conf->reassociation_deadline;
252 wconf->r0kh_list = conf->r0kh_list;
253 wconf->r1kh_list = conf->r1kh_list;
254 wconf->pmk_r1_push = conf->pmk_r1_push;
255 #endif /* CONFIG_IEEE80211R */
259 int hostapd_reload_config(struct hostapd_iface *iface)
261 struct hostapd_data *hapd = iface->bss[0];
262 struct hostapd_config *newconf, *oldconf;
263 struct wpa_auth_config wpa_auth_conf;
265 newconf = hostapd_config_read(iface->config_fname);
270 * Deauthenticate all stations since the new configuration may not
271 * allow them to use the BSS anymore.
273 hostapd_flush_old_stations(hapd);
275 /* TODO: update dynamic data based on changed configuration
276 * items (e.g., open/close sockets, etc.) */
277 radius_client_flush(hapd->radius, 0);
279 oldconf = hapd->iconf;
280 hapd->iconf = newconf;
281 hapd->conf = &newconf->bss[0];
282 iface->conf = newconf;
284 if (hostapd_setup_wpa_psk(hapd->conf)) {
285 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
286 "after reloading configuration");
289 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
290 hostapd_setup_wpa(hapd);
291 else if (hapd->conf->wpa) {
292 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
293 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
294 } else if (hapd->wpa_auth) {
295 wpa_deinit(hapd->wpa_auth);
296 hapd->wpa_auth = NULL;
297 hostapd_set_privacy(hapd, 0);
298 hostapd_setup_encryption(hapd->conf->iface, hapd);
301 ieee802_11_set_beacon(hapd);
303 hostapd_config_free(oldconf);
305 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
311 #ifndef CONFIG_NATIVE_WINDOWS
313 * handle_reload - SIGHUP handler to reload configuration
315 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
317 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
320 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
323 for (i = 0; i < hapds->count; i++) {
324 if (hostapd_reload_config(hapds->iface[i]) < 0) {
325 wpa_printf(MSG_WARNING, "Failed to read new "
326 "configuration file - continuing with "
334 #ifdef HOSTAPD_DUMP_STATE
336 * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
338 static void hostapd_dump_state(struct hostapd_data *hapd)
342 struct sta_info *sta;
346 if (!hapd->conf->dump_log_name) {
347 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
352 wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
353 hapd->conf->dump_log_name);
354 f = fopen(hapd->conf->dump_log_name, "w");
356 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
357 "writing.", hapd->conf->dump_log_name);
362 fprintf(f, "hostapd state dump - %s", ctime(&now));
363 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
364 "num_sta_no_short_slot_time=%d\n"
365 "num_sta_no_short_preamble=%d\n",
366 hapd->num_sta, hapd->iface->num_sta_non_erp,
367 hapd->iface->num_sta_no_short_slot_time,
368 hapd->iface->num_sta_no_short_preamble);
370 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
371 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
374 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
375 " capability=0x%x listen_interval=%d\n",
378 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
379 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
380 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
381 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
382 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
383 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
385 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
387 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
388 "[SHORT_PREAMBLE]" : ""),
389 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
390 (sta->flags & WLAN_STA_WMM ? "[WMM]" : ""),
391 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
392 (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
393 (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
394 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
396 sta->listen_interval);
398 fprintf(f, " supported_rates=");
399 for (i = 0; i < sta->supported_rates_len; i++)
400 fprintf(f, "%02x ", sta->supported_rates[i]);
404 " timeout_next=%s\n",
405 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
406 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
409 ieee802_1x_dump_state(f, " ", sta);
412 buf = os_malloc(4096);
414 int count = radius_client_get_mib(hapd->radius, buf, 4096);
417 else if (count > 4095)
420 fprintf(f, "%s", buf);
422 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
425 else if (count > 4095)
428 fprintf(f, "%s", buf);
433 #endif /* HOSTAPD_DUMP_STATE */
436 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
438 #ifdef HOSTAPD_DUMP_STATE
439 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
442 for (i = 0; i < hapds->count; i++) {
443 struct hostapd_iface *hapd_iface = hapds->iface[i];
444 for (j = 0; j < hapd_iface->num_bss; j++)
445 hostapd_dump_state(hapd_iface->bss[j]);
447 #endif /* HOSTAPD_DUMP_STATE */
449 #endif /* CONFIG_NATIVE_WINDOWS */
451 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
456 for (i = 0; i < NUM_WEP_KEYS; i++) {
457 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
458 0, i == 0 ? 1 : 0)) {
459 wpa_printf(MSG_DEBUG, "Failed to clear default "
460 "encryption keys (ifname=%s keyidx=%d)",
464 #ifdef CONFIG_IEEE80211W
465 if (hapd->conf->ieee80211w) {
466 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
467 if (hostapd_set_encryption(ifname, hapd, "none", NULL,
470 wpa_printf(MSG_DEBUG, "Failed to clear "
471 "default mgmt encryption keys "
472 "(ifname=%s keyidx=%d)", ifname, i);
476 #endif /* CONFIG_IEEE80211W */
480 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
482 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
487 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
490 struct hostapd_ssid *ssid = &hapd->conf->ssid;
493 if (ssid->wep.default_len &&
494 hostapd_set_encryption(hapd->conf->iface,
495 hapd, "WEP", NULL, idx,
498 idx == ssid->wep.idx)) {
499 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
503 if (ssid->dyn_vlan_keys) {
505 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
507 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
510 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
516 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
520 wpa_printf(MSG_WARNING, "Could not set "
521 "dynamic VLAN WEP encryption.");
531 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
532 * @hapd: Pointer to BSS data
534 * This function is used to free all per-BSS data structures and resources.
535 * This gets called in a loop for each BSS between calls to
536 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
537 * is deinitialized. Most of the modules that are initialized in
538 * hostapd_setup_bss() are deinitialized here.
540 static void hostapd_cleanup(struct hostapd_data *hapd)
542 hostapd_ctrl_iface_deinit(hapd);
544 os_free(hapd->default_wep_key);
545 hapd->default_wep_key = NULL;
546 iapp_deinit(hapd->iapp);
548 accounting_deinit(hapd);
549 rsn_preauth_iface_deinit(hapd);
550 if (hapd->wpa_auth) {
551 wpa_deinit(hapd->wpa_auth);
552 hapd->wpa_auth = NULL;
554 if (hostapd_set_privacy(hapd, 0)) {
555 wpa_printf(MSG_DEBUG, "Could not disable "
556 "PrivacyInvoked for interface %s",
560 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
561 wpa_printf(MSG_DEBUG, "Could not remove generic "
562 "information element from interface %s",
566 ieee802_1x_deinit(hapd);
568 hostapd_acl_deinit(hapd);
569 radius_client_deinit(hapd->radius);
571 radius_server_deinit(hapd->radius_srv);
572 hapd->radius_srv = NULL;
574 #ifdef CONFIG_IEEE80211R
575 l2_packet_deinit(hapd->l2);
576 #endif /* CONFIG_IEEE80211R */
578 hostapd_deinit_wps(hapd);
580 hostapd_wireless_event_deinit(hapd);
584 tls_deinit(hapd->ssl_ctx);
585 hapd->ssl_ctx = NULL;
587 #endif /* EAP_TLS_FUNCS */
590 if (hapd->eap_sim_db_priv) {
591 eap_sim_db_deinit(hapd->eap_sim_db_priv);
592 hapd->eap_sim_db_priv = NULL;
594 #endif /* EAP_SERVER */
596 if (hapd->interface_added &&
597 hostapd_bss_remove(hapd, hapd->conf->iface)) {
598 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
605 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
606 * @iface: Pointer to interface data
608 * This function is called before per-BSS data structures are deinitialized
609 * with hostapd_cleanup().
611 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
617 * hostapd_cleanup_iface - Complete per-interface cleanup
618 * @iface: Pointer to interface data
620 * This function is called after per-BSS data structures are deinitialized
621 * with hostapd_cleanup().
623 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
625 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
626 iface->hw_features = NULL;
627 os_free(iface->current_rates);
628 iface->current_rates = NULL;
629 ap_list_deinit(iface);
630 hostapd_config_free(iface->conf);
633 os_free(iface->config_fname);
639 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
643 hostapd_broadcast_wep_set(hapd);
645 if (hapd->conf->ssid.wep.default_len)
648 for (i = 0; i < 4; i++) {
649 if (hapd->conf->ssid.wep.key[i] &&
650 hostapd_set_encryption(iface, hapd, "WEP", NULL,
651 i, hapd->conf->ssid.wep.key[i],
652 hapd->conf->ssid.wep.len[i],
653 i == hapd->conf->ssid.wep.idx)) {
654 wpa_printf(MSG_WARNING, "Could not set WEP "
658 if (hapd->conf->ssid.wep.key[i] &&
659 i == hapd->conf->ssid.wep.idx)
660 hostapd_set_privacy(hapd, 1);
667 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
671 if (hostapd_drv_none(hapd))
674 wpa_printf(MSG_DEBUG, "Flushing old station entries");
675 if (hostapd_flush(hapd)) {
676 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
679 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
681 /* New Prism2.5/3 STA firmware versions seem to have issues with this
682 * broadcast deauth frame. This gets the firmware in odd state where
683 * nothing works correctly, so let's skip sending this for the hostap
685 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
687 os_memset(addr, 0xff, ETH_ALEN);
688 hostapd_sta_deauth(hapd, addr,
689 WLAN_REASON_PREV_AUTH_NOT_VALID);
696 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
697 logger_level level, const char *txt)
699 #ifndef CONFIG_NO_HOSTAPD_LOGGER
700 struct hostapd_data *hapd = ctx;
705 hlevel = HOSTAPD_LEVEL_WARNING;
708 hlevel = HOSTAPD_LEVEL_INFO;
712 hlevel = HOSTAPD_LEVEL_DEBUG;
716 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
717 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
721 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
724 struct hostapd_data *hapd = ctx;
725 struct sta_info *sta;
727 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
728 "STA " MACSTR " reason %d",
729 __func__, MAC2STR(addr), reason);
731 sta = ap_get_sta(hapd, addr);
732 hostapd_sta_deauth(hapd, addr, reason);
735 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
736 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
737 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
738 sta->timeout_next = STA_REMOVE;
742 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
744 struct hostapd_data *hapd = ctx;
745 michael_mic_failure(hapd, addr, 0);
749 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
750 wpa_eapol_variable var, int value)
752 struct hostapd_data *hapd = ctx;
753 struct sta_info *sta = ap_get_sta(hapd, addr);
757 case WPA_EAPOL_portEnabled:
758 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
760 case WPA_EAPOL_portValid:
761 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
763 case WPA_EAPOL_authorized:
764 ieee802_1x_set_sta_authorized(hapd, sta, value);
766 case WPA_EAPOL_portControl_Auto:
768 sta->eapol_sm->portControl = Auto;
770 case WPA_EAPOL_keyRun:
772 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
774 case WPA_EAPOL_keyAvailable:
776 sta->eapol_sm->eap_if->eapKeyAvailable =
777 value ? TRUE : FALSE;
779 case WPA_EAPOL_keyDone:
781 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
783 case WPA_EAPOL_inc_EapolFramesTx:
785 sta->eapol_sm->dot1xAuthEapolFramesTx++;
791 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
792 wpa_eapol_variable var)
794 struct hostapd_data *hapd = ctx;
795 struct sta_info *sta = ap_get_sta(hapd, addr);
796 if (sta == NULL || sta->eapol_sm == NULL)
799 case WPA_EAPOL_keyRun:
800 return sta->eapol_sm->keyRun;
801 case WPA_EAPOL_keyAvailable:
802 return sta->eapol_sm->eap_if->eapKeyAvailable;
809 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
812 struct hostapd_data *hapd = ctx;
813 return hostapd_get_psk(hapd->conf, addr, prev_psk);
817 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
820 struct hostapd_data *hapd = ctx;
823 struct sta_info *sta;
825 sta = ap_get_sta(hapd, addr);
829 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
835 os_memcpy(msk, key, keylen);
842 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
843 const u8 *addr, int idx, u8 *key,
846 struct hostapd_data *hapd = ctx;
847 const char *ifname = hapd->conf->iface;
850 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
855 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
860 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
863 struct hostapd_data *hapd = ctx;
864 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
868 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
871 struct hostapd_data *hapd = ctx;
872 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
877 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
878 const u8 *data, size_t data_len,
881 struct hostapd_data *hapd = ctx;
882 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
886 static int hostapd_wpa_auth_for_each_sta(
887 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
890 struct hostapd_data *hapd = ctx;
891 struct sta_info *sta;
893 for (sta = hapd->sta_list; sta; sta = sta->next) {
894 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
901 static int hostapd_wpa_auth_for_each_auth(
902 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
905 struct hostapd_data *ohapd;
907 struct hapd_interfaces *interfaces = eloop_get_user_data();
909 for (i = 0; i < interfaces->count; i++) {
910 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
911 ohapd = interfaces->iface[i]->bss[j];
912 if (cb(ohapd->wpa_auth, cb_ctx))
921 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
922 const u8 *data, size_t data_len)
924 struct hostapd_data *hapd = ctx;
926 if (hapd->driver && hapd->driver->send_ether)
927 return hapd->driver->send_ether(hapd->drv_priv, dst,
928 hapd->own_addr, proto,
930 if (hapd->l2 == NULL)
932 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
936 #ifdef CONFIG_IEEE80211R
938 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
939 const u8 *data, size_t data_len)
941 struct hostapd_data *hapd = ctx;
943 struct ieee80211_mgmt *m;
945 struct sta_info *sta;
947 sta = ap_get_sta(hapd, dst);
948 if (sta == NULL || sta->wpa_sm == NULL)
951 m = os_zalloc(sizeof(*m) + data_len);
954 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
955 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
956 WLAN_FC_STYPE_ACTION);
957 os_memcpy(m->da, dst, ETH_ALEN);
958 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
959 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
960 os_memcpy(&m->u, data, data_len);
962 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
968 static struct wpa_state_machine *
969 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
971 struct hostapd_data *hapd = ctx;
972 struct sta_info *sta;
974 sta = ap_sta_add(hapd, sta_addr);
980 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
981 if (sta->wpa_sm == NULL) {
982 ap_free_sta(hapd, sta);
985 sta->auth_alg = WLAN_AUTH_FT;
991 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
994 struct hostapd_data *hapd = ctx;
995 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
998 #endif /* CONFIG_IEEE80211R */
1002 * hostapd_validate_bssid_configuration - Validate BSSID configuration
1003 * @iface: Pointer to interface data
1004 * Returns: 0 on success, -1 on failure
1006 * This function is used to validate that the configured BSSIDs are valid.
1008 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1010 u8 mask[ETH_ALEN] = { 0 };
1011 struct hostapd_data *hapd = iface->bss[0];
1012 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) {
1034 for (i = 0; i < ETH_ALEN; i++) {
1036 iface->conf->bss[j].bssid[i] ^
1044 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1050 while (mask[i] != 0) {
1060 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
1065 os_memset(mask, 0xff, ETH_ALEN);
1067 for (i = 5; i > 5 - j; i--)
1074 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1075 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1077 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1082 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1083 MACSTR " for start address " MACSTR ".",
1084 MAC2STR(mask), MAC2STR(hapd->own_addr));
1091 for (i = 0; i < ETH_ALEN; i++) {
1092 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1093 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1094 " for start address " MACSTR ".",
1095 MAC2STR(mask), MAC2STR(hapd->own_addr));
1096 wpa_printf(MSG_ERROR, "Start address must be the "
1097 "first address in the block (i.e., addr "
1098 "AND mask == addr).");
1107 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1111 for (i = 0; i < conf->num_bss; i++) {
1112 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1121 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1123 struct wpa_auth_config _conf;
1124 struct wpa_auth_callbacks cb;
1128 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1129 os_memset(&cb, 0, sizeof(cb));
1131 cb.logger = hostapd_wpa_auth_logger;
1132 cb.disconnect = hostapd_wpa_auth_disconnect;
1133 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1134 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1135 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1136 cb.get_psk = hostapd_wpa_auth_get_psk;
1137 cb.get_msk = hostapd_wpa_auth_get_msk;
1138 cb.set_key = hostapd_wpa_auth_set_key;
1139 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1140 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1141 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1142 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1143 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1144 cb.send_ether = hostapd_wpa_auth_send_ether;
1145 #ifdef CONFIG_IEEE80211R
1146 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1147 cb.add_sta = hostapd_wpa_auth_add_sta;
1148 #endif /* CONFIG_IEEE80211R */
1149 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1150 if (hapd->wpa_auth == NULL) {
1151 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1155 if (hostapd_set_privacy(hapd, 1)) {
1156 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1157 "for interface %s", hapd->conf->iface);
1161 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1162 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1163 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1164 "the kernel driver.");
1168 if (rsn_preauth_iface_init(hapd)) {
1169 wpa_printf(MSG_ERROR, "Initialization of RSN "
1170 "pre-authentication failed.");
1179 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1180 struct hostapd_bss_config *conf)
1182 struct radius_server_conf srv;
1183 os_memset(&srv, 0, sizeof(srv));
1184 srv.client_file = conf->radius_server_clients;
1185 srv.auth_port = conf->radius_server_auth_port;
1186 srv.conf_ctx = conf;
1187 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1188 srv.ssl_ctx = hapd->ssl_ctx;
1189 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1190 srv.eap_fast_a_id = conf->eap_fast_a_id;
1191 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1192 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1193 srv.eap_fast_prov = conf->eap_fast_prov;
1194 srv.pac_key_lifetime = conf->pac_key_lifetime;
1195 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1196 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1197 srv.tnc = conf->tnc;
1198 srv.wps = hapd->wps;
1199 srv.ipv6 = conf->radius_server_ipv6;
1200 srv.get_eap_user = hostapd_radius_get_eap_user;
1201 srv.eap_req_id_text = conf->eap_req_id_text;
1202 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1204 hapd->radius_srv = radius_server_init(&srv);
1205 if (hapd->radius_srv == NULL) {
1206 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1215 * hostapd_setup_bss - Per-BSS setup (initialization)
1216 * @hapd: Pointer to BSS data
1217 * @first: Whether this BSS is the first BSS of an interface
1219 * This function is used to initialize all per-BSS data structures and
1220 * resources. This gets called in a loop for each BSS when an interface is
1221 * initialized. Most of the modules that are initialized here will be
1222 * deinitialized in hostapd_cleanup().
1224 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1226 struct hostapd_bss_config *conf = hapd->conf;
1227 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1228 int ssid_len, set_ssid;
1231 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1232 /* Allocate the next available BSSID. */
1234 inc_byte_array(hapd->own_addr, ETH_ALEN);
1235 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1237 /* Allocate the configured BSSID. */
1238 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1240 if (hostapd_mac_comp(hapd->own_addr,
1241 hapd->iface->bss[0]->own_addr) ==
1243 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1244 "BSSID set to the MAC address of "
1245 "the radio", hapd->conf->iface);
1250 hapd->interface_added = 1;
1251 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1253 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1254 MACSTR ")", MAC2STR(hapd->own_addr));
1260 * Fetch the SSID from the system and use it or,
1261 * if one was specified in the config file, verify they
1264 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1266 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1269 if (conf->ssid.ssid_set) {
1271 * If SSID is specified in the config file and it differs
1272 * from what is being used then force installation of the
1275 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1276 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1279 * No SSID in the config file; just use the one we got
1283 conf->ssid.ssid_len = ssid_len;
1284 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1285 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1288 if (!hostapd_drv_none(hapd)) {
1289 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1291 hapd->conf->iface, MAC2STR(hapd->own_addr),
1292 hapd->conf->ssid.ssid);
1295 if (hostapd_setup_wpa_psk(conf)) {
1296 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1300 /* Set flag for whether SSID is broadcast in beacons */
1301 if (hostapd_set_broadcast_ssid(hapd,
1302 !!hapd->conf->ignore_broadcast_ssid)) {
1303 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1308 /* Set SSID for the kernel driver (to be used in beacon and probe
1309 * response frames) */
1310 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1311 conf->ssid.ssid_len)) {
1312 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1316 if (wpa_debug_level == MSG_MSGDUMP)
1317 conf->radius->msg_dumps = 1;
1318 hapd->radius = radius_client_init(hapd, conf->radius);
1319 if (hapd->radius == NULL) {
1320 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1324 if (hostapd_acl_init(hapd)) {
1325 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1328 if (hostapd_init_wps(hapd, conf))
1331 if (ieee802_1x_init(hapd)) {
1332 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1336 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1339 if (accounting_init(hapd)) {
1340 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1344 if (hapd->conf->ieee802_11f &&
1345 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1346 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1351 if (hostapd_ctrl_iface_init(hapd)) {
1352 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1356 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1357 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1361 #ifdef CONFIG_IEEE80211R
1362 if (!hostapd_drv_none(hapd)) {
1363 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1364 hostapd_rrb_receive, hapd, 0);
1365 if (hapd->l2 == NULL &&
1366 (hapd->driver == NULL ||
1367 hapd->driver->send_ether == NULL)) {
1368 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1373 #endif /* CONFIG_IEEE80211R */
1375 ieee802_11_set_beacon(hapd);
1377 if (conf->radius_server_clients &&
1378 hostapd_setup_radius_srv(hapd, conf))
1385 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1387 struct hostapd_data *hapd = iface->bss[0];
1389 struct hostapd_tx_queue_params *p;
1391 for (i = 0; i < NUM_TX_QUEUES; i++) {
1392 p = &iface->conf->tx_queue[i];
1397 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1398 p->cwmax, p->burst)) {
1399 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1400 "parameters for queue %d.", i);
1401 /* Continue anyway */
1407 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1408 size_t identity_len, int phase2,
1409 struct eap_user *user)
1411 const struct hostapd_eap_user *eap_user;
1414 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1415 if (eap_user == NULL)
1421 os_memset(user, 0, sizeof(*user));
1422 count = EAP_USER_MAX_METHODS;
1423 if (count > EAP_MAX_METHODS)
1424 count = EAP_MAX_METHODS;
1425 for (i = 0; i < count; i++) {
1426 user->methods[i].vendor = eap_user->methods[i].vendor;
1427 user->methods[i].method = eap_user->methods[i].method;
1430 if (eap_user->password) {
1431 user->password = os_malloc(eap_user->password_len);
1432 if (user->password == NULL)
1434 os_memcpy(user->password, eap_user->password,
1435 eap_user->password_len);
1436 user->password_len = eap_user->password_len;
1437 user->password_hash = eap_user->password_hash;
1439 user->force_version = eap_user->force_version;
1440 user->ttls_auth = eap_user->ttls_auth;
1446 static int setup_interface(struct hostapd_iface *iface)
1448 struct hostapd_data *hapd = iface->bss[0];
1449 struct hostapd_bss_config *conf = hapd->conf;
1452 u8 *b = conf->bssid;
1459 * Initialize the driver interface and make sure that all BSSes get
1460 * configured with a pointer to this driver interface.
1462 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1463 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1465 hapd->drv_priv = hostapd_driver_init(hapd);
1468 if (hapd->drv_priv == NULL) {
1469 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1470 hapd->driver ? hapd->driver->name : "Unknown");
1471 hapd->driver = NULL;
1474 for (i = 0; i < iface->num_bss; i++) {
1475 iface->bss[i]->driver = hapd->driver;
1476 iface->bss[i]->drv_priv = hapd->drv_priv;
1479 if (hostapd_validate_bssid_configuration(iface))
1482 #ifdef CONFIG_IEEE80211N
1483 SET_2BIT_LE16(&iface->ht_op_mode,
1484 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1486 #endif /* CONFIG_IEEE80211N */
1488 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1489 os_memcpy(country, hapd->iconf->country, 3);
1491 if (hostapd_set_country(hapd, country) < 0) {
1492 wpa_printf(MSG_ERROR, "Failed to set country code");
1497 if (hapd->iconf->ieee80211d &&
1498 hostapd_set_ieee80211d(hapd, 1) < 0) {
1499 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1500 hapd->iconf->ieee80211d);
1504 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1505 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1506 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1511 /* TODO: merge with hostapd_driver_init() ? */
1512 if (hostapd_wireless_event_init(hapd) < 0)
1515 if (hostapd_get_hw_features(iface)) {
1516 /* Not all drivers support this yet, so continue without hw
1519 int ret = hostapd_select_hw_mode(iface);
1521 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1522 "channel. (%d)", ret);
1527 hostapd_flush_old_stations(hapd);
1528 hostapd_set_privacy(hapd, 0);
1530 if (hapd->iconf->channel) {
1531 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1532 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1533 "Frequency: %d MHz",
1534 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1535 hapd->iconf->channel, freq);
1537 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1538 hapd->iconf->ieee80211n,
1539 hapd->iconf->secondary_channel)) {
1540 wpa_printf(MSG_ERROR, "Could not set channel for "
1546 hostapd_broadcast_wep_clear(hapd);
1547 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1550 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1551 ieee802_11_set_beacon(hapd);
1553 if (hapd->iconf->rts_threshold > -1 &&
1554 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1555 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1560 if (hapd->iconf->fragm_threshold > -1 &&
1561 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1562 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1563 "for kernel driver");
1567 prev_addr = hapd->own_addr;
1569 for (j = 0; j < iface->num_bss; j++) {
1570 hapd = iface->bss[j];
1572 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1573 if (hostapd_setup_bss(hapd, j == 0))
1575 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1576 prev_addr = hapd->own_addr;
1579 hostapd_tx_queue_params(iface);
1581 ap_list_init(iface);
1583 if (hostapd_driver_commit(hapd) < 0) {
1584 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1585 "configuration", __func__);
1594 * hostapd_setup_interface - Setup of an interface
1595 * @iface: Pointer to interface data.
1596 * Returns: 0 on success, -1 on failure
1598 * Initializes the driver interface, validates the configuration,
1599 * and sets driver parameters based on the configuration.
1600 * Flushes old stations, sets the channel, encryption,
1601 * beacons, and WDS links based on the configuration.
1603 static int hostapd_setup_interface(struct hostapd_iface *iface)
1607 ret = setup_interface(iface);
1609 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1610 iface->bss[0]->conf->iface);
1613 } else if (!hostapd_drv_none(iface->bss[0])) {
1614 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1615 iface->bss[0]->conf->iface);
1622 static void show_version(void)
1625 "hostapd v" VERSION_STR "\n"
1626 "User space daemon for IEEE 802.11 AP management,\n"
1627 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1628 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1629 "and contributors\n");
1633 static void usage(void)
1638 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1639 "<configuration file(s)>\n"
1642 " -h show this usage\n"
1643 " -d show more debug messages (-dd for even more)\n"
1644 " -B run daemon in the background\n"
1646 " -K include key data in debug messages\n"
1647 " -t include timestamps in some debug messages\n"
1648 " -v show hostapd version\n");
1655 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1656 * @hapd_iface: Pointer to interface data
1657 * @conf: Pointer to per-interface configuration
1658 * @bss: Pointer to per-BSS configuration for this BSS
1659 * Returns: Pointer to allocated BSS data
1661 * This function is used to allocate per-BSS data structure. This data will be
1662 * freed after hostapd_cleanup() is called for it during interface
1665 static struct hostapd_data *
1666 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1667 struct hostapd_config *conf,
1668 struct hostapd_bss_config *bss)
1670 struct hostapd_data *hapd;
1672 hapd = os_zalloc(sizeof(*hapd));
1678 hapd->iface = hapd_iface;
1680 if (hapd->conf->individual_wep_key_len > 0) {
1681 /* use key0 in individual key and key1 in broadcast key */
1682 hapd->default_wep_key_idx = 1;
1685 #ifdef EAP_TLS_FUNCS
1686 if (hapd->conf->eap_server &&
1687 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1688 hapd->conf->dh_file)) {
1689 struct tls_connection_params params;
1691 hapd->ssl_ctx = tls_init(NULL);
1692 if (hapd->ssl_ctx == NULL) {
1693 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1697 os_memset(¶ms, 0, sizeof(params));
1698 params.ca_cert = hapd->conf->ca_cert;
1699 params.client_cert = hapd->conf->server_cert;
1700 params.private_key = hapd->conf->private_key;
1701 params.private_key_passwd = hapd->conf->private_key_passwd;
1702 params.dh_file = hapd->conf->dh_file;
1704 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1705 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1709 if (tls_global_set_verify(hapd->ssl_ctx,
1710 hapd->conf->check_crl)) {
1711 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1715 #endif /* EAP_TLS_FUNCS */
1718 if (hapd->conf->eap_sim_db) {
1719 hapd->eap_sim_db_priv =
1720 eap_sim_db_init(hapd->conf->eap_sim_db,
1721 hostapd_sim_db_cb, hapd);
1722 if (hapd->eap_sim_db_priv == NULL) {
1723 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1724 "database interface");
1728 #endif /* EAP_SERVER */
1730 hapd->driver = hapd->iconf->driver;
1734 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1737 /* TODO: cleanup allocated resources(?) */
1744 * hostapd_init - Allocate and initialize per-interface data
1745 * @config_file: Path to the configuration file
1746 * Returns: Pointer to the allocated interface data or %NULL on failure
1748 * This function is used to allocate main data structures for per-interface
1749 * data. The allocated data buffer will be freed by calling
1750 * hostapd_cleanup_iface().
1752 static struct hostapd_iface * hostapd_init(const char *config_file)
1754 struct hostapd_iface *hapd_iface = NULL;
1755 struct hostapd_config *conf = NULL;
1756 struct hostapd_data *hapd;
1759 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1760 if (hapd_iface == NULL)
1763 hapd_iface->config_fname = os_strdup(config_file);
1764 if (hapd_iface->config_fname == NULL)
1767 conf = hostapd_config_read(hapd_iface->config_fname);
1770 hapd_iface->conf = conf;
1772 hapd_iface->num_bss = conf->num_bss;
1773 hapd_iface->bss = os_zalloc(conf->num_bss *
1774 sizeof(struct hostapd_data *));
1775 if (hapd_iface->bss == NULL)
1778 for (i = 0; i < conf->num_bss; i++) {
1779 hapd = hapd_iface->bss[i] =
1780 hostapd_alloc_bss_data(hapd_iface, conf,
1790 hostapd_config_free(conf);
1792 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1793 hapd = hapd_iface->bss[i];
1794 if (hapd && hapd->ssl_ctx)
1795 tls_deinit(hapd->ssl_ctx);
1798 os_free(hapd_iface->config_fname);
1799 os_free(hapd_iface->bss);
1800 os_free(hapd_iface);
1806 static int hostapd_global_init(struct hapd_interfaces *interfaces)
1808 hostapd_logger_register_cb(hostapd_logger_cb);
1810 if (eap_server_register_methods()) {
1811 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1815 if (eloop_init(interfaces)) {
1816 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1820 #ifndef CONFIG_NATIVE_WINDOWS
1821 eloop_register_signal(SIGHUP, handle_reload, NULL);
1822 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1823 #endif /* CONFIG_NATIVE_WINDOWS */
1824 eloop_register_signal_terminate(handle_term, NULL);
1826 #ifndef CONFIG_NATIVE_WINDOWS
1827 openlog("hostapd", 0, LOG_DAEMON);
1828 #endif /* CONFIG_NATIVE_WINDOWS */
1834 static void hostapd_global_deinit(const char *pid_file)
1836 #ifdef EAP_SERVER_TNC
1837 tncs_global_deinit();
1838 #endif /* EAP_SERVER_TNC */
1842 #ifndef CONFIG_NATIVE_WINDOWS
1844 #endif /* CONFIG_NATIVE_WINDOWS */
1846 eap_server_unregister_methods();
1848 os_daemonize_terminate(pid_file);
1852 static void hostapd_interface_deinit(struct hostapd_iface *iface)
1859 hostapd_cleanup_iface_pre(iface);
1860 for (j = 0; j < iface->num_bss; j++) {
1861 struct hostapd_data *hapd = iface->bss[j];
1862 hostapd_free_stas(hapd);
1863 hostapd_flush_old_stations(hapd);
1864 hostapd_cleanup(hapd);
1865 if (j == iface->num_bss - 1 && hapd->driver)
1866 hostapd_driver_deinit(hapd);
1868 for (j = 0; j < iface->num_bss; j++)
1869 os_free(iface->bss[j]);
1870 hostapd_cleanup_iface(iface);
1874 static struct hostapd_iface * hostapd_interface_init(const char *config_fname,
1877 struct hostapd_iface *iface;
1880 wpa_printf(MSG_ERROR, "Configuration file: %s", config_fname);
1881 iface = hostapd_init(config_fname);
1885 for (k = 0; k < debug; k++) {
1886 if (iface->bss[0]->conf->logger_stdout_level > 0)
1887 iface->bss[0]->conf->logger_stdout_level--;
1890 if (hostapd_setup_interface(iface)) {
1891 hostapd_interface_deinit(iface);
1899 static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize,
1900 const char *pid_file)
1902 #ifdef EAP_SERVER_TNC
1906 for (i = 0; !tnc && i < ifaces->count; i++) {
1907 for (k = 0; k < ifaces->iface[i]->num_bss; k++) {
1908 if (ifaces->iface[i]->bss[0]->conf->tnc) {
1915 if (tnc && tncs_global_init() < 0) {
1916 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1919 #endif /* EAP_SERVER_TNC */
1921 if (daemonize && os_daemonize(pid_file)) {
1932 int main(int argc, char *argv[])
1934 struct hapd_interfaces interfaces;
1937 int c, debug = 0, daemonize = 0;
1938 const char *pid_file = NULL;
1941 c = getopt(argc, argv, "BdhKP:tv");
1950 if (wpa_debug_level > 0)
1957 wpa_debug_show_keys++;
1963 wpa_debug_timestamp++;
1979 interfaces.count = argc - optind;
1980 interfaces.iface = os_malloc(interfaces.count *
1981 sizeof(struct hostapd_iface *));
1982 if (interfaces.iface == NULL) {
1983 wpa_printf(MSG_ERROR, "malloc failed\n");
1987 if (hostapd_global_init(&interfaces))
1990 /* Initialize interfaces */
1991 for (i = 0; i < interfaces.count; i++) {
1992 interfaces.iface[i] = hostapd_interface_init(argv[optind + i],
1994 if (!interfaces.iface[i])
1998 if (hostapd_global_run(&interfaces, daemonize, pid_file))
2004 /* Deinitialize all interfaces */
2005 for (i = 0; i < interfaces.count; i++)
2006 hostapd_interface_deinit(interfaces.iface[i]);
2007 os_free(interfaces.iface);
2009 hostapd_global_deinit(pid_file);