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"
23 #include "ieee802_11.h"
25 #include "hw_features.h"
26 #include "accounting.h"
30 #include "ieee802_11_auth.h"
34 #include "radius/radius_client.h"
35 #include "radius/radius_server.h"
39 #include "vlan_init.h"
40 #include "ctrl_iface.h"
42 #include "eap_server/eap_sim_db.h"
43 #include "eap_server/eap.h"
44 #include "eap_server/tncs.h"
46 #include "l2_packet/l2_packet.h"
47 #include "wps_hostapd.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 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
69 int level, const char *txt, size_t len)
71 struct hostapd_data *hapd = ctx;
72 char *format, *module_str;
74 int conf_syslog_level, conf_stdout_level;
75 unsigned int conf_syslog, conf_stdout;
78 format = os_malloc(maxlen);
82 if (hapd && hapd->conf) {
83 conf_syslog_level = hapd->conf->logger_syslog_level;
84 conf_stdout_level = hapd->conf->logger_stdout_level;
85 conf_syslog = hapd->conf->logger_syslog;
86 conf_stdout = hapd->conf->logger_stdout;
88 conf_syslog_level = conf_stdout_level = 0;
89 conf_syslog = conf_stdout = (unsigned int) -1;
93 case HOSTAPD_MODULE_IEEE80211:
94 module_str = "IEEE 802.11";
96 case HOSTAPD_MODULE_IEEE8021X:
97 module_str = "IEEE 802.1X";
99 case HOSTAPD_MODULE_RADIUS:
100 module_str = "RADIUS";
102 case HOSTAPD_MODULE_WPA:
105 case HOSTAPD_MODULE_DRIVER:
106 module_str = "DRIVER";
108 case HOSTAPD_MODULE_IAPP:
111 case HOSTAPD_MODULE_MLME:
119 if (hapd && hapd->conf && addr)
120 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
121 hapd->conf->iface, MAC2STR(addr),
122 module_str ? " " : "", module_str, txt);
123 else if (hapd && hapd->conf)
124 os_snprintf(format, maxlen, "%s:%s%s %s",
125 hapd->conf->iface, module_str ? " " : "",
128 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
129 MAC2STR(addr), module_str ? " " : "",
132 os_snprintf(format, maxlen, "%s%s%s",
133 module_str, module_str ? ": " : "", txt);
135 if ((conf_stdout & module) && level >= conf_stdout_level) {
136 wpa_debug_print_timestamp();
137 printf("%s\n", format);
140 #ifndef CONFIG_NATIVE_WINDOWS
141 if ((conf_syslog & module) && level >= conf_syslog_level) {
144 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
145 case HOSTAPD_LEVEL_DEBUG:
146 priority = LOG_DEBUG;
148 case HOSTAPD_LEVEL_INFO:
151 case HOSTAPD_LEVEL_NOTICE:
152 priority = LOG_NOTICE;
154 case HOSTAPD_LEVEL_WARNING:
155 priority = LOG_WARNING;
161 syslog(priority, "%s", format);
163 #endif /* CONFIG_NATIVE_WINDOWS */
169 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
173 /* New Prism2.5/3 STA firmware versions seem to have issues with this
174 * broadcast deauth frame. This gets the firmware in odd state where
175 * nothing works correctly, so let's skip sending this for the hostap
178 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
179 os_memset(addr, 0xff, ETH_ALEN);
180 hostapd_sta_deauth(hapd, addr,
181 WLAN_REASON_PREV_AUTH_NOT_VALID);
187 * hostapd_prune_associations - Remove extraneous associations
188 * @hapd: Pointer to BSS data for the most recent association
189 * @sta: Pointer to the associated STA data
191 * This function looks through all radios and BSS's for previous
192 * (stale) associations of STA. If any are found they are removed.
194 static void hostapd_prune_associations(struct hostapd_data *hapd,
195 struct sta_info *sta)
197 struct sta_info *osta;
198 struct hostapd_data *ohapd;
200 struct hapd_interfaces *interfaces = eloop_get_user_data();
202 for (i = 0; i < interfaces->count; i++) {
203 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
204 ohapd = interfaces->iface[i]->bss[j];
207 osta = ap_get_sta(ohapd, sta->addr);
211 ap_sta_disassociate(ohapd, osta,
212 WLAN_REASON_UNSPECIFIED);
219 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
220 * @hapd: Pointer to BSS data
221 * @sta: Pointer to the associated STA data
222 * @reassoc: 1 to indicate this was a re-association; 0 = first association
224 * This function will be called whenever a station associates with the AP. It
225 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
226 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
227 * authentication and association) internally.
229 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
232 if (hapd->tkip_countermeasures) {
233 hostapd_sta_deauth(hapd, sta->addr,
234 WLAN_REASON_MICHAEL_MIC_FAILURE);
238 hostapd_prune_associations(hapd, sta);
240 /* IEEE 802.11F (IAPP) */
241 if (hapd->conf->ieee802_11f)
242 iapp_new_station(hapd->iapp, sta);
244 /* Start accounting here, if IEEE 802.1X and WPA are not used.
245 * IEEE 802.1X/WPA code will start accounting after the station has
246 * been authorized. */
247 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
248 accounting_sta_start(hapd, sta);
250 hostapd_wme_sta_config(hapd, sta);
252 /* Start IEEE 802.1X authentication process for new stations */
253 ieee802_1x_new_station(hapd, sta);
255 if (sta->auth_alg != WLAN_AUTH_FT &&
256 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
257 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
259 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
264 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
265 struct sta_info *sta, void *ctx)
267 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
273 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
275 struct hostapd_data *hapd = ctx;
276 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
277 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
279 #endif /* EAP_SERVER */
283 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
285 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
287 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
292 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
293 struct wpa_auth_config *wconf)
295 wconf->wpa = conf->wpa;
296 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
297 wconf->wpa_pairwise = conf->wpa_pairwise;
298 wconf->wpa_group = conf->wpa_group;
299 wconf->wpa_group_rekey = conf->wpa_group_rekey;
300 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
301 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
302 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
303 wconf->rsn_pairwise = conf->rsn_pairwise;
304 wconf->rsn_preauth = conf->rsn_preauth;
305 wconf->eapol_version = conf->eapol_version;
306 wconf->peerkey = conf->peerkey;
307 wconf->wme_enabled = conf->wme_enabled;
308 wconf->okc = conf->okc;
309 #ifdef CONFIG_IEEE80211W
310 wconf->ieee80211w = conf->ieee80211w;
311 #endif /* CONFIG_IEEE80211W */
312 #ifdef CONFIG_IEEE80211R
313 wconf->ssid_len = conf->ssid.ssid_len;
314 if (wconf->ssid_len > SSID_LEN)
315 wconf->ssid_len = SSID_LEN;
316 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
317 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
318 MOBILITY_DOMAIN_ID_LEN);
319 if (conf->nas_identifier &&
320 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
321 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
322 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
323 wconf->r0_key_holder_len);
325 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
326 wconf->r0_key_lifetime = conf->r0_key_lifetime;
327 wconf->reassociation_deadline = conf->reassociation_deadline;
328 wconf->r0kh_list = conf->r0kh_list;
329 wconf->r1kh_list = conf->r1kh_list;
330 wconf->pmk_r1_push = conf->pmk_r1_push;
331 #endif /* CONFIG_IEEE80211R */
335 int hostapd_reload_config(struct hostapd_iface *iface)
337 struct hostapd_data *hapd = iface->bss[0];
338 struct hostapd_config *newconf, *oldconf;
339 struct wpa_auth_config wpa_auth_conf;
341 newconf = hostapd_config_read(iface->config_fname);
346 * Deauthenticate all stations since the new configuration may not
347 * allow them to use the BSS anymore.
349 hostapd_flush_old_stations(hapd);
351 /* TODO: update dynamic data based on changed configuration
352 * items (e.g., open/close sockets, etc.) */
353 radius_client_flush(hapd->radius, 0);
355 oldconf = hapd->iconf;
356 hapd->iconf = newconf;
357 hapd->conf = &newconf->bss[0];
358 iface->conf = newconf;
360 if (hostapd_setup_wpa_psk(hapd->conf)) {
361 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
362 "after reloading configuration");
365 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
366 hostapd_setup_wpa(hapd);
367 else if (hapd->conf->wpa) {
368 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
369 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
370 } else if (hapd->wpa_auth) {
371 wpa_deinit(hapd->wpa_auth);
372 hapd->wpa_auth = NULL;
373 hostapd_set_privacy(hapd, 0);
374 hostapd_setup_encryption(hapd->conf->iface, hapd);
377 ieee802_11_set_beacon(hapd);
379 hostapd_config_free(oldconf);
381 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
387 #ifndef CONFIG_NATIVE_WINDOWS
389 * handle_reload - SIGHUP handler to reload configuration
391 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
393 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
396 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
399 for (i = 0; i < hapds->count; i++) {
400 if (hostapd_reload_config(hapds->iface[i]) < 0) {
401 wpa_printf(MSG_WARNING, "Failed to read new "
402 "configuration file - continuing with "
410 #ifdef HOSTAPD_DUMP_STATE
412 * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
414 static void hostapd_dump_state(struct hostapd_data *hapd)
418 struct sta_info *sta;
422 if (!hapd->conf->dump_log_name) {
423 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
428 wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
429 hapd->conf->dump_log_name);
430 f = fopen(hapd->conf->dump_log_name, "w");
432 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
433 "writing.", hapd->conf->dump_log_name);
438 fprintf(f, "hostapd state dump - %s", ctime(&now));
439 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
440 "num_sta_no_short_slot_time=%d\n"
441 "num_sta_no_short_preamble=%d\n",
442 hapd->num_sta, hapd->iface->num_sta_non_erp,
443 hapd->iface->num_sta_no_short_slot_time,
444 hapd->iface->num_sta_no_short_preamble);
446 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
447 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
450 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
451 " capability=0x%x listen_interval=%d\n",
454 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
455 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
456 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
457 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
458 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
459 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
461 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
463 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
464 "[SHORT_PREAMBLE]" : ""),
465 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
466 (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
467 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
468 (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
469 (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
470 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
472 sta->listen_interval);
474 fprintf(f, " supported_rates=");
475 for (i = 0; i < sta->supported_rates_len; i++)
476 fprintf(f, "%02x ", sta->supported_rates[i]);
480 " timeout_next=%s\n",
481 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
482 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
485 ieee802_1x_dump_state(f, " ", sta);
488 buf = os_malloc(4096);
490 int count = radius_client_get_mib(hapd->radius, buf, 4096);
493 else if (count > 4095)
496 fprintf(f, "%s", buf);
498 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
501 else if (count > 4095)
504 fprintf(f, "%s", buf);
509 #endif /* HOSTAPD_DUMP_STATE */
512 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
514 #ifdef HOSTAPD_DUMP_STATE
515 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
518 for (i = 0; i < hapds->count; i++) {
519 struct hostapd_iface *hapd_iface = hapds->iface[i];
520 for (j = 0; j < hapd_iface->num_bss; j++)
521 hostapd_dump_state(hapd_iface->bss[j]);
523 #endif /* HOSTAPD_DUMP_STATE */
525 #endif /* CONFIG_NATIVE_WINDOWS */
527 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
532 for (i = 0; i < NUM_WEP_KEYS; i++) {
533 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
534 0, i == 0 ? 1 : 0)) {
535 wpa_printf(MSG_DEBUG, "Failed to clear default "
536 "encryption keys (ifname=%s keyidx=%d)",
540 #ifdef CONFIG_IEEE80211W
541 if (hapd->conf->ieee80211w) {
542 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
543 if (hostapd_set_encryption(ifname, hapd, "none", NULL,
546 wpa_printf(MSG_DEBUG, "Failed to clear "
547 "default mgmt encryption keys "
548 "(ifname=%s keyidx=%d)", ifname, i);
552 #endif /* CONFIG_IEEE80211W */
556 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
558 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
563 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
566 struct hostapd_ssid *ssid = &hapd->conf->ssid;
569 if (ssid->wep.default_len &&
570 hostapd_set_encryption(hapd->conf->iface,
571 hapd, "WEP", NULL, idx,
574 idx == ssid->wep.idx)) {
575 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
579 if (ssid->dyn_vlan_keys) {
581 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
583 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
586 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
592 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
596 wpa_printf(MSG_WARNING, "Could not set "
597 "dynamic VLAN WEP encryption.");
607 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
608 * @hapd: Pointer to BSS data
610 * This function is used to free all per-BSS data structures and resources.
611 * This gets called in a loop for each BSS between calls to
612 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
613 * is deinitialized. Most of the modules that are initialized in
614 * hostapd_setup_bss() are deinitialized here.
616 static void hostapd_cleanup(struct hostapd_data *hapd)
618 hostapd_ctrl_iface_deinit(hapd);
620 os_free(hapd->default_wep_key);
621 hapd->default_wep_key = NULL;
622 iapp_deinit(hapd->iapp);
624 accounting_deinit(hapd);
625 rsn_preauth_iface_deinit(hapd);
626 if (hapd->wpa_auth) {
627 wpa_deinit(hapd->wpa_auth);
628 hapd->wpa_auth = NULL;
630 if (hostapd_set_privacy(hapd, 0)) {
631 wpa_printf(MSG_DEBUG, "Could not disable "
632 "PrivacyInvoked for interface %s",
636 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
637 wpa_printf(MSG_DEBUG, "Could not remove generic "
638 "information element from interface %s",
642 ieee802_1x_deinit(hapd);
644 hostapd_acl_deinit(hapd);
645 radius_client_deinit(hapd->radius);
647 radius_server_deinit(hapd->radius_srv);
648 hapd->radius_srv = NULL;
650 #ifdef CONFIG_IEEE80211R
651 l2_packet_deinit(hapd->l2);
652 #endif /* CONFIG_IEEE80211R */
654 hostapd_deinit_wps(hapd);
656 hostapd_wireless_event_deinit(hapd);
660 tls_deinit(hapd->ssl_ctx);
661 hapd->ssl_ctx = NULL;
663 #endif /* EAP_TLS_FUNCS */
666 if (hapd->eap_sim_db_priv) {
667 eap_sim_db_deinit(hapd->eap_sim_db_priv);
668 hapd->eap_sim_db_priv = NULL;
670 #endif /* EAP_SERVER */
672 if (hapd->interface_added &&
673 hostapd_bss_remove(hapd, hapd->conf->iface)) {
674 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
681 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
682 * @iface: Pointer to interface data
684 * This function is called before per-BSS data structures are deinitialized
685 * with hostapd_cleanup().
687 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
693 * hostapd_cleanup_iface - Complete per-interface cleanup
694 * @iface: Pointer to interface data
696 * This function is called after per-BSS data structures are deinitialized
697 * with hostapd_cleanup().
699 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
701 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
702 iface->hw_features = NULL;
703 os_free(iface->current_rates);
704 iface->current_rates = NULL;
705 ap_list_deinit(iface);
706 hostapd_config_free(iface->conf);
709 os_free(iface->config_fname);
715 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
719 hostapd_broadcast_wep_set(hapd);
721 if (hapd->conf->ssid.wep.default_len)
724 for (i = 0; i < 4; i++) {
725 if (hapd->conf->ssid.wep.key[i] &&
726 hostapd_set_encryption(iface, hapd, "WEP", NULL,
727 i, hapd->conf->ssid.wep.key[i],
728 hapd->conf->ssid.wep.len[i],
729 i == hapd->conf->ssid.wep.idx)) {
730 wpa_printf(MSG_WARNING, "Could not set WEP "
734 if (hapd->conf->ssid.wep.key[i] &&
735 i == hapd->conf->ssid.wep.idx)
736 hostapd_set_privacy(hapd, 1);
743 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
747 if (hostapd_drv_none(hapd))
750 wpa_printf(MSG_DEBUG, "Flushing old station entries");
751 if (hostapd_flush(hapd)) {
752 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
755 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
756 hostapd_deauth_all_stas(hapd);
762 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
763 logger_level level, const char *txt)
765 struct hostapd_data *hapd = ctx;
770 hlevel = HOSTAPD_LEVEL_WARNING;
773 hlevel = HOSTAPD_LEVEL_INFO;
777 hlevel = HOSTAPD_LEVEL_DEBUG;
781 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
785 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
788 struct hostapd_data *hapd = ctx;
789 struct sta_info *sta;
791 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
792 "STA " MACSTR " reason %d",
793 __func__, MAC2STR(addr), reason);
795 sta = ap_get_sta(hapd, addr);
796 hostapd_sta_deauth(hapd, addr, reason);
799 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
800 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
801 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
802 sta->timeout_next = STA_REMOVE;
806 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
808 struct hostapd_data *hapd = ctx;
809 ieee80211_michael_mic_failure(hapd, addr, 0);
813 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
814 wpa_eapol_variable var, int value)
816 struct hostapd_data *hapd = ctx;
817 struct sta_info *sta = ap_get_sta(hapd, addr);
821 case WPA_EAPOL_portEnabled:
822 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
824 case WPA_EAPOL_portValid:
825 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
827 case WPA_EAPOL_authorized:
828 ieee802_1x_set_sta_authorized(hapd, sta, value);
830 case WPA_EAPOL_portControl_Auto:
832 sta->eapol_sm->portControl = Auto;
834 case WPA_EAPOL_keyRun:
836 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
838 case WPA_EAPOL_keyAvailable:
840 sta->eapol_sm->eap_if->eapKeyAvailable =
841 value ? TRUE : FALSE;
843 case WPA_EAPOL_keyDone:
845 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
847 case WPA_EAPOL_inc_EapolFramesTx:
849 sta->eapol_sm->dot1xAuthEapolFramesTx++;
855 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
856 wpa_eapol_variable var)
858 struct hostapd_data *hapd = ctx;
859 struct sta_info *sta = ap_get_sta(hapd, addr);
860 if (sta == NULL || sta->eapol_sm == NULL)
863 case WPA_EAPOL_keyRun:
864 return sta->eapol_sm->keyRun;
865 case WPA_EAPOL_keyAvailable:
866 return sta->eapol_sm->eap_if->eapKeyAvailable;
873 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
876 struct hostapd_data *hapd = ctx;
877 return hostapd_get_psk(hapd->conf, addr, prev_psk);
881 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
884 struct hostapd_data *hapd = ctx;
887 struct sta_info *sta;
889 sta = ap_get_sta(hapd, addr);
893 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
899 os_memcpy(msk, key, keylen);
906 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
907 const u8 *addr, int idx, u8 *key,
910 struct hostapd_data *hapd = ctx;
911 const char *ifname = hapd->conf->iface;
914 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
919 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
924 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
927 struct hostapd_data *hapd = ctx;
928 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
932 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
935 struct hostapd_data *hapd = ctx;
936 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
941 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
942 const u8 *data, size_t data_len,
945 struct hostapd_data *hapd = ctx;
946 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
950 static int hostapd_wpa_auth_for_each_sta(
951 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
954 struct hostapd_data *hapd = ctx;
955 struct sta_info *sta;
957 for (sta = hapd->sta_list; sta; sta = sta->next) {
958 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
965 static int hostapd_wpa_auth_for_each_auth(
966 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
969 struct hostapd_data *ohapd;
971 struct hapd_interfaces *interfaces = eloop_get_user_data();
973 for (i = 0; i < interfaces->count; i++) {
974 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
975 ohapd = interfaces->iface[i]->bss[j];
976 if (cb(ohapd->wpa_auth, cb_ctx))
985 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
986 const u8 *data, size_t data_len)
988 struct hostapd_data *hapd = ctx;
990 if (hapd->driver && hapd->driver->send_ether)
991 return hapd->driver->send_ether(hapd->drv_priv, dst,
992 hapd->own_addr, proto,
994 if (hapd->l2 == NULL)
996 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
1000 #ifdef CONFIG_IEEE80211R
1002 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
1003 const u8 *data, size_t data_len)
1005 struct hostapd_data *hapd = ctx;
1007 struct ieee80211_mgmt *m;
1009 struct sta_info *sta;
1011 sta = ap_get_sta(hapd, dst);
1012 if (sta == NULL || sta->wpa_sm == NULL)
1015 m = os_zalloc(sizeof(*m) + data_len);
1018 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
1019 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1020 WLAN_FC_STYPE_ACTION);
1021 os_memcpy(m->da, dst, ETH_ALEN);
1022 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
1023 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
1024 os_memcpy(&m->u, data, data_len);
1026 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
1032 static struct wpa_state_machine *
1033 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
1035 struct hostapd_data *hapd = ctx;
1036 struct sta_info *sta;
1038 sta = ap_sta_add(hapd, sta_addr);
1044 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
1045 if (sta->wpa_sm == NULL) {
1046 ap_free_sta(hapd, sta);
1049 sta->auth_alg = WLAN_AUTH_FT;
1055 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1058 struct hostapd_data *hapd = ctx;
1059 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
1062 #endif /* CONFIG_IEEE80211R */
1066 * hostapd_validate_bssid_configuration - Validate BSSID configuration
1067 * @iface: Pointer to interface data
1068 * Returns: 0 on success, -1 on failure
1070 * This function is used to validate that the configured BSSIDs are valid.
1072 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1074 u8 mask[ETH_ALEN] = { 0 };
1075 struct hostapd_data *hapd = iface->bss[0];
1076 unsigned int i = iface->conf->num_bss, bits = 0, j;
1079 if (hostapd_drv_none(hapd))
1082 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1084 /* Determine the bits necessary to cover the number of BSSIDs. */
1085 for (i--; i; i >>= 1)
1088 /* Determine the bits necessary to any configured BSSIDs,
1089 if they are higher than the number of BSSIDs. */
1090 for (j = 0; j < iface->conf->num_bss; j++) {
1091 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1094 for (i = 0; i < ETH_ALEN; i++) {
1096 iface->conf->bss[j].bssid[i] ^
1101 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1107 while (mask[i] != 0) {
1119 os_memset(mask, 0xff, ETH_ALEN);
1121 for (i = 5; i > 5 - j; i--)
1127 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1128 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1130 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1135 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1136 MACSTR " for start address " MACSTR ".",
1137 MAC2STR(mask), MAC2STR(hapd->own_addr));
1141 for (i = 0; i < ETH_ALEN; i++) {
1142 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1143 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1144 " for start address " MACSTR ".",
1145 MAC2STR(mask), MAC2STR(hapd->own_addr));
1146 wpa_printf(MSG_ERROR, "Start address must be the "
1147 "first address in the block (i.e., addr "
1148 "AND mask == addr).");
1157 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1161 for (i = 0; i < conf->num_bss; i++) {
1162 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1171 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1173 struct wpa_auth_config _conf;
1174 struct wpa_auth_callbacks cb;
1178 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1179 os_memset(&cb, 0, sizeof(cb));
1181 cb.logger = hostapd_wpa_auth_logger;
1182 cb.disconnect = hostapd_wpa_auth_disconnect;
1183 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1184 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1185 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1186 cb.get_psk = hostapd_wpa_auth_get_psk;
1187 cb.get_msk = hostapd_wpa_auth_get_msk;
1188 cb.set_key = hostapd_wpa_auth_set_key;
1189 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1190 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1191 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1192 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1193 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1194 cb.send_ether = hostapd_wpa_auth_send_ether;
1195 #ifdef CONFIG_IEEE80211R
1196 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1197 cb.add_sta = hostapd_wpa_auth_add_sta;
1198 #endif /* CONFIG_IEEE80211R */
1199 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1200 if (hapd->wpa_auth == NULL) {
1201 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1205 if (hostapd_set_privacy(hapd, 1)) {
1206 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1207 "for interface %s", hapd->conf->iface);
1211 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1212 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1213 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1214 "the kernel driver.");
1218 if (rsn_preauth_iface_init(hapd)) {
1219 wpa_printf(MSG_ERROR, "Initialization of RSN "
1220 "pre-authentication failed.");
1229 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1230 struct hostapd_bss_config *conf)
1232 struct radius_server_conf srv;
1233 os_memset(&srv, 0, sizeof(srv));
1234 srv.client_file = conf->radius_server_clients;
1235 srv.auth_port = conf->radius_server_auth_port;
1236 srv.conf_ctx = conf;
1237 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1238 srv.ssl_ctx = hapd->ssl_ctx;
1239 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1240 srv.eap_fast_a_id = conf->eap_fast_a_id;
1241 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1242 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1243 srv.eap_fast_prov = conf->eap_fast_prov;
1244 srv.pac_key_lifetime = conf->pac_key_lifetime;
1245 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1246 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1247 srv.tnc = conf->tnc;
1248 srv.wps = hapd->wps;
1249 srv.ipv6 = conf->radius_server_ipv6;
1250 srv.get_eap_user = hostapd_radius_get_eap_user;
1251 srv.eap_req_id_text = conf->eap_req_id_text;
1252 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1254 hapd->radius_srv = radius_server_init(&srv);
1255 if (hapd->radius_srv == NULL) {
1256 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1265 * hostapd_setup_bss - Per-BSS setup (initialization)
1266 * @hapd: Pointer to BSS data
1267 * @first: Whether this BSS is the first BSS of an interface
1269 * This function is used to initialize all per-BSS data structures and
1270 * resources. This gets called in a loop for each BSS when an interface is
1271 * initialized. Most of the modules that are initialized here will be
1272 * deinitialized in hostapd_cleanup().
1274 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1276 struct hostapd_bss_config *conf = hapd->conf;
1277 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1278 int ssid_len, set_ssid;
1281 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1282 /* Allocate the next available BSSID. */
1284 inc_byte_array(hapd->own_addr, ETH_ALEN);
1285 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1287 /* Allocate the configured BSSID. */
1288 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1290 if (hostapd_mac_comp(hapd->own_addr,
1291 hapd->iface->bss[0]->own_addr) ==
1293 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1294 "BSSID set to the MAC address of "
1295 "the radio", hapd->conf->iface);
1300 hapd->interface_added = 1;
1301 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1303 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1304 MACSTR ")", MAC2STR(hapd->own_addr));
1310 * Fetch the SSID from the system and use it or,
1311 * if one was specified in the config file, verify they
1314 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1316 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1319 if (conf->ssid.ssid_set) {
1321 * If SSID is specified in the config file and it differs
1322 * from what is being used then force installation of the
1325 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1326 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1329 * No SSID in the config file; just use the one we got
1333 conf->ssid.ssid_len = ssid_len;
1334 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1335 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1338 if (!hostapd_drv_none(hapd)) {
1339 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1341 hapd->conf->iface, MAC2STR(hapd->own_addr),
1342 hapd->conf->ssid.ssid);
1345 if (hostapd_setup_wpa_psk(conf)) {
1346 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1350 /* Set flag for whether SSID is broadcast in beacons */
1351 if (hostapd_set_broadcast_ssid(hapd,
1352 !!hapd->conf->ignore_broadcast_ssid)) {
1353 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1358 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1359 wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
1364 /* Set SSID for the kernel driver (to be used in beacon and probe
1365 * response frames) */
1366 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1367 conf->ssid.ssid_len)) {
1368 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1372 if (wpa_debug_level == MSG_MSGDUMP)
1373 conf->radius->msg_dumps = 1;
1374 hapd->radius = radius_client_init(hapd, conf->radius);
1375 if (hapd->radius == NULL) {
1376 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1380 if (hostapd_acl_init(hapd)) {
1381 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1384 if (hostapd_init_wps(hapd, conf))
1387 if (ieee802_1x_init(hapd)) {
1388 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1392 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1395 if (accounting_init(hapd)) {
1396 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1400 if (hapd->conf->ieee802_11f &&
1401 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1402 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1407 if (hostapd_ctrl_iface_init(hapd)) {
1408 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1412 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1413 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1417 #ifdef CONFIG_IEEE80211R
1418 if (!hostapd_drv_none(hapd)) {
1419 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1420 hostapd_rrb_receive, hapd, 0);
1421 if (hapd->l2 == NULL &&
1422 (hapd->driver == NULL ||
1423 hapd->driver->send_ether == NULL)) {
1424 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1429 #endif /* CONFIG_IEEE80211R */
1431 ieee802_11_set_beacon(hapd);
1433 if (conf->radius_server_clients &&
1434 hostapd_setup_radius_srv(hapd, conf))
1441 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1443 struct hostapd_data *hapd = iface->bss[0];
1445 struct hostapd_tx_queue_params *p;
1447 for (i = 0; i < NUM_TX_QUEUES; i++) {
1448 p = &iface->conf->tx_queue[i];
1453 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1454 p->cwmax, p->burst)) {
1455 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1456 "parameters for queue %d.", i);
1457 /* Continue anyway */
1463 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1464 size_t identity_len, int phase2,
1465 struct eap_user *user)
1467 const struct hostapd_eap_user *eap_user;
1470 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1471 if (eap_user == NULL)
1477 os_memset(user, 0, sizeof(*user));
1478 count = EAP_USER_MAX_METHODS;
1479 if (count > EAP_MAX_METHODS)
1480 count = EAP_MAX_METHODS;
1481 for (i = 0; i < count; i++) {
1482 user->methods[i].vendor = eap_user->methods[i].vendor;
1483 user->methods[i].method = eap_user->methods[i].method;
1486 if (eap_user->password) {
1487 user->password = os_malloc(eap_user->password_len);
1488 if (user->password == NULL)
1490 os_memcpy(user->password, eap_user->password,
1491 eap_user->password_len);
1492 user->password_len = eap_user->password_len;
1493 user->password_hash = eap_user->password_hash;
1495 user->force_version = eap_user->force_version;
1496 user->ttls_auth = eap_user->ttls_auth;
1502 static int setup_interface(struct hostapd_iface *iface)
1504 struct hostapd_data *hapd = iface->bss[0];
1505 struct hostapd_bss_config *conf = hapd->conf;
1508 u8 *b = conf->bssid;
1515 * Initialize the driver interface and make sure that all BSSes get
1516 * configured with a pointer to this driver interface.
1518 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1519 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1521 hapd->drv_priv = hostapd_driver_init(hapd);
1524 if (hapd->drv_priv == NULL) {
1525 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1526 hapd->driver ? hapd->driver->name : "Unknown");
1527 hapd->driver = NULL;
1530 for (i = 0; i < iface->num_bss; i++) {
1531 iface->bss[i]->driver = hapd->driver;
1532 iface->bss[i]->drv_priv = hapd->drv_priv;
1535 if (hostapd_validate_bssid_configuration(iface))
1538 #ifdef CONFIG_IEEE80211N
1539 SET_2BIT_LE16(&iface->ht_op_mode,
1540 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1542 #endif /* CONFIG_IEEE80211N */
1544 os_memcpy(country, hapd->iconf->country, 3);
1546 if (hostapd_set_country(hapd, country) < 0) {
1547 wpa_printf(MSG_ERROR, "Failed to set country code");
1551 if (hapd->iconf->ieee80211d &&
1552 hostapd_set_ieee80211d(hapd, 1) < 0) {
1553 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1554 hapd->iconf->ieee80211d);
1558 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1559 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1560 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1565 /* TODO: merge with hostapd_driver_init() ? */
1566 if (hostapd_wireless_event_init(hapd) < 0)
1569 if (hostapd_get_hw_features(iface)) {
1570 /* Not all drivers support this yet, so continue without hw
1573 int ret = hostapd_select_hw_mode(iface);
1575 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1576 "channel. (%d)", ret);
1581 hostapd_flush_old_stations(hapd);
1582 hostapd_set_privacy(hapd, 0);
1584 if (hapd->iconf->channel) {
1585 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1586 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1587 "Frequency: %d MHz",
1588 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1589 hapd->iconf->channel, freq);
1591 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1592 hapd->iconf->ieee80211n,
1593 hapd->iconf->secondary_channel)) {
1594 wpa_printf(MSG_ERROR, "Could not set channel for "
1600 hostapd_broadcast_wep_clear(hapd);
1601 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1604 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1605 ieee802_11_set_beacon(hapd);
1607 if (hapd->iconf->rts_threshold > -1 &&
1608 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1609 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1614 if (hapd->iconf->fragm_threshold > -1 &&
1615 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1616 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1617 "for kernel driver");
1621 prev_addr = hapd->own_addr;
1623 for (j = 0; j < iface->num_bss; j++) {
1624 hapd = iface->bss[j];
1626 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1627 if (hostapd_setup_bss(hapd, j == 0))
1629 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1630 prev_addr = hapd->own_addr;
1633 hostapd_tx_queue_params(iface);
1635 ap_list_init(iface);
1637 if (hostapd_driver_commit(hapd) < 0) {
1638 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1639 "configuration", __func__);
1648 * hostapd_setup_interface - Setup of an interface
1649 * @iface: Pointer to interface data.
1650 * Returns: 0 on success, -1 on failure
1652 * Initializes the driver interface, validates the configuration,
1653 * and sets driver parameters based on the configuration.
1654 * Flushes old stations, sets the channel, encryption,
1655 * beacons, and WDS links based on the configuration.
1657 static int hostapd_setup_interface(struct hostapd_iface *iface)
1661 ret = setup_interface(iface);
1663 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1664 iface->bss[0]->conf->iface);
1667 } else if (!hostapd_drv_none(iface->bss[0])) {
1668 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1669 iface->bss[0]->conf->iface);
1676 static void show_version(void)
1679 "hostapd v" VERSION_STR "\n"
1680 "User space daemon for IEEE 802.11 AP management,\n"
1681 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1682 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1683 "and contributors\n");
1687 static void usage(void)
1692 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1693 "<configuration file(s)>\n"
1696 " -h show this usage\n"
1697 " -d show more debug messages (-dd for even more)\n"
1698 " -B run daemon in the background\n"
1700 " -K include key data in debug messages\n"
1701 " -t include timestamps in some debug messages\n"
1702 " -v show hostapd version\n");
1709 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1710 * @hapd_iface: Pointer to interface data
1711 * @conf: Pointer to per-interface configuration
1712 * @bss: Pointer to per-BSS configuration for this BSS
1713 * Returns: Pointer to allocated BSS data
1715 * This function is used to allocate per-BSS data structure. This data will be
1716 * freed after hostapd_cleanup() is called for it during interface
1719 static struct hostapd_data *
1720 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1721 struct hostapd_config *conf,
1722 struct hostapd_bss_config *bss)
1724 struct hostapd_data *hapd;
1726 hapd = os_zalloc(sizeof(*hapd));
1732 hapd->iface = hapd_iface;
1734 if (hapd->conf->individual_wep_key_len > 0) {
1735 /* use key0 in individual key and key1 in broadcast key */
1736 hapd->default_wep_key_idx = 1;
1739 #ifdef EAP_TLS_FUNCS
1740 if (hapd->conf->eap_server &&
1741 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1742 hapd->conf->dh_file)) {
1743 struct tls_connection_params params;
1745 hapd->ssl_ctx = tls_init(NULL);
1746 if (hapd->ssl_ctx == NULL) {
1747 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1751 os_memset(¶ms, 0, sizeof(params));
1752 params.ca_cert = hapd->conf->ca_cert;
1753 params.client_cert = hapd->conf->server_cert;
1754 params.private_key = hapd->conf->private_key;
1755 params.private_key_passwd = hapd->conf->private_key_passwd;
1756 params.dh_file = hapd->conf->dh_file;
1758 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1759 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1763 if (tls_global_set_verify(hapd->ssl_ctx,
1764 hapd->conf->check_crl)) {
1765 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1769 #endif /* EAP_TLS_FUNCS */
1772 if (hapd->conf->eap_sim_db) {
1773 hapd->eap_sim_db_priv =
1774 eap_sim_db_init(hapd->conf->eap_sim_db,
1775 hostapd_sim_db_cb, hapd);
1776 if (hapd->eap_sim_db_priv == NULL) {
1777 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1778 "database interface");
1782 #endif /* EAP_SERVER */
1784 hapd->driver = hapd->iconf->driver;
1788 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1791 /* TODO: cleanup allocated resources(?) */
1798 * hostapd_init - Allocate and initialize per-interface data
1799 * @config_file: Path to the configuration file
1800 * Returns: Pointer to the allocated interface data or %NULL on failure
1802 * This function is used to allocate main data structures for per-interface
1803 * data. The allocated data buffer will be freed by calling
1804 * hostapd_cleanup_iface().
1806 static struct hostapd_iface * hostapd_init(const char *config_file)
1808 struct hostapd_iface *hapd_iface = NULL;
1809 struct hostapd_config *conf = NULL;
1810 struct hostapd_data *hapd;
1813 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1814 if (hapd_iface == NULL)
1817 hapd_iface->config_fname = os_strdup(config_file);
1818 if (hapd_iface->config_fname == NULL)
1821 conf = hostapd_config_read(hapd_iface->config_fname);
1824 hapd_iface->conf = conf;
1826 hapd_iface->num_bss = conf->num_bss;
1827 hapd_iface->bss = os_zalloc(conf->num_bss *
1828 sizeof(struct hostapd_data *));
1829 if (hapd_iface->bss == NULL)
1832 for (i = 0; i < conf->num_bss; i++) {
1833 hapd = hapd_iface->bss[i] =
1834 hostapd_alloc_bss_data(hapd_iface, conf,
1844 hostapd_config_free(conf);
1846 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1847 hapd = hapd_iface->bss[i];
1848 if (hapd && hapd->ssl_ctx)
1849 tls_deinit(hapd->ssl_ctx);
1852 os_free(hapd_iface->config_fname);
1853 os_free(hapd_iface->bss);
1854 os_free(hapd_iface);
1860 int main(int argc, char *argv[])
1862 struct hapd_interfaces interfaces;
1865 int c, debug = 0, daemonize = 0, tnc = 0;
1866 const char *pid_file = NULL;
1868 hostapd_logger_register_cb(hostapd_logger_cb);
1871 c = getopt(argc, argv, "BdhKP:tv");
1880 if (wpa_debug_level > 0)
1887 wpa_debug_show_keys++;
1893 wpa_debug_timestamp++;
1909 if (eap_server_register_methods()) {
1910 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1914 interfaces.count = argc - optind;
1916 interfaces.iface = os_malloc(interfaces.count *
1917 sizeof(struct hostapd_iface *));
1918 if (interfaces.iface == NULL) {
1919 wpa_printf(MSG_ERROR, "malloc failed\n");
1923 if (eloop_init(&interfaces)) {
1924 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1928 #ifndef CONFIG_NATIVE_WINDOWS
1929 eloop_register_signal(SIGHUP, handle_reload, NULL);
1930 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1931 #endif /* CONFIG_NATIVE_WINDOWS */
1932 eloop_register_signal_terminate(handle_term, NULL);
1934 /* Initialize interfaces */
1935 for (i = 0; i < interfaces.count; i++) {
1936 wpa_printf(MSG_ERROR, "Configuration file: %s",
1938 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1939 if (!interfaces.iface[i])
1941 for (k = 0; k < debug; k++) {
1942 if (interfaces.iface[i]->bss[0]->conf->
1943 logger_stdout_level > 0)
1944 interfaces.iface[i]->bss[0]->conf->
1945 logger_stdout_level--;
1948 ret = hostapd_setup_interface(interfaces.iface[i]);
1952 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
1953 if (interfaces.iface[i]->bss[0]->conf->tnc)
1959 if (tnc && tncs_global_init() < 0) {
1960 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1963 #endif /* EAP_TNC */
1965 if (daemonize && os_daemonize(pid_file)) {
1970 #ifndef CONFIG_NATIVE_WINDOWS
1971 openlog("hostapd", 0, LOG_DAEMON);
1972 #endif /* CONFIG_NATIVE_WINDOWS */
1976 /* Disconnect associated stations from all interfaces and BSSes */
1977 for (i = 0; i < interfaces.count; i++) {
1978 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1979 struct hostapd_data *hapd =
1980 interfaces.iface[i]->bss[j];
1981 hostapd_free_stas(hapd);
1982 hostapd_flush_old_stations(hapd);
1989 /* Deinitialize all interfaces */
1990 for (i = 0; i < interfaces.count; i++) {
1991 if (!interfaces.iface[i])
1993 hostapd_cleanup_iface_pre(interfaces.iface[i]);
1994 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1995 struct hostapd_data *hapd =
1996 interfaces.iface[i]->bss[j];
1997 hostapd_cleanup(hapd);
1998 if (j == interfaces.iface[i]->num_bss - 1 &&
2000 hostapd_driver_deinit(hapd);
2002 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
2003 os_free(interfaces.iface[i]->bss[j]);
2004 hostapd_cleanup_iface(interfaces.iface[i]);
2006 os_free(interfaces.iface);
2009 tncs_global_deinit();
2010 #endif /* EAP_TNC */
2014 #ifndef CONFIG_NATIVE_WINDOWS
2016 #endif /* CONFIG_NATIVE_WINDOWS */
2018 eap_server_unregister_methods();
2020 os_daemonize_terminate(pid_file);