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;
62 unsigned char rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
65 extern int wpa_debug_level;
66 extern int wpa_debug_show_keys;
67 extern int wpa_debug_timestamp;
70 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
71 int level, const char *txt, size_t len)
73 struct hostapd_data *hapd = ctx;
74 char *format, *module_str;
76 int conf_syslog_level, conf_stdout_level;
77 unsigned int conf_syslog, conf_stdout;
80 format = os_malloc(maxlen);
84 if (hapd && hapd->conf) {
85 conf_syslog_level = hapd->conf->logger_syslog_level;
86 conf_stdout_level = hapd->conf->logger_stdout_level;
87 conf_syslog = hapd->conf->logger_syslog;
88 conf_stdout = hapd->conf->logger_stdout;
90 conf_syslog_level = conf_stdout_level = 0;
91 conf_syslog = conf_stdout = (unsigned int) -1;
95 case HOSTAPD_MODULE_IEEE80211:
96 module_str = "IEEE 802.11";
98 case HOSTAPD_MODULE_IEEE8021X:
99 module_str = "IEEE 802.1X";
101 case HOSTAPD_MODULE_RADIUS:
102 module_str = "RADIUS";
104 case HOSTAPD_MODULE_WPA:
107 case HOSTAPD_MODULE_DRIVER:
108 module_str = "DRIVER";
110 case HOSTAPD_MODULE_IAPP:
113 case HOSTAPD_MODULE_MLME:
121 if (hapd && hapd->conf && addr)
122 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
123 hapd->conf->iface, MAC2STR(addr),
124 module_str ? " " : "", module_str, txt);
125 else if (hapd && hapd->conf)
126 os_snprintf(format, maxlen, "%s:%s%s %s",
127 hapd->conf->iface, module_str ? " " : "",
130 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
131 MAC2STR(addr), module_str ? " " : "",
134 os_snprintf(format, maxlen, "%s%s%s",
135 module_str, module_str ? ": " : "", txt);
137 if ((conf_stdout & module) && level >= conf_stdout_level) {
138 wpa_debug_print_timestamp();
139 printf("%s\n", format);
142 #ifndef CONFIG_NATIVE_WINDOWS
143 if ((conf_syslog & module) && level >= conf_syslog_level) {
146 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
147 case HOSTAPD_LEVEL_DEBUG:
148 priority = LOG_DEBUG;
150 case HOSTAPD_LEVEL_INFO:
153 case HOSTAPD_LEVEL_NOTICE:
154 priority = LOG_NOTICE;
156 case HOSTAPD_LEVEL_WARNING:
157 priority = LOG_WARNING;
163 syslog(priority, "%s", format);
165 #endif /* CONFIG_NATIVE_WINDOWS */
171 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
175 /* New Prism2.5/3 STA firmware versions seem to have issues with this
176 * broadcast deauth frame. This gets the firmware in odd state where
177 * nothing works correctly, so let's skip sending this for the hostap
180 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
181 os_memset(addr, 0xff, ETH_ALEN);
182 hostapd_sta_deauth(hapd, addr,
183 WLAN_REASON_PREV_AUTH_NOT_VALID);
189 * hostapd_prune_associations - Remove extraneous associations
190 * @hapd: Pointer to BSS data for the most recent association
191 * @sta: Pointer to the associated STA data
193 * This function looks through all radios and BSS's for previous
194 * (stale) associations of STA. If any are found they are removed.
196 static void hostapd_prune_associations(struct hostapd_data *hapd,
197 struct sta_info *sta)
199 struct sta_info *osta;
200 struct hostapd_data *ohapd;
202 struct hapd_interfaces *interfaces = eloop_get_user_data();
204 for (i = 0; i < interfaces->count; i++) {
205 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
206 ohapd = interfaces->iface[i]->bss[j];
209 osta = ap_get_sta(ohapd, sta->addr);
213 ap_sta_disassociate(ohapd, osta,
214 WLAN_REASON_UNSPECIFIED);
221 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
222 * @hapd: Pointer to BSS data
223 * @sta: Pointer to the associated STA data
224 * @reassoc: 1 to indicate this was a re-association; 0 = first association
226 * This function will be called whenever a station associates with the AP. It
227 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
228 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
229 * authentication and association) internally.
231 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
234 if (hapd->tkip_countermeasures) {
235 hostapd_sta_deauth(hapd, sta->addr,
236 WLAN_REASON_MICHAEL_MIC_FAILURE);
240 hostapd_prune_associations(hapd, sta);
242 /* IEEE 802.11F (IAPP) */
243 if (hapd->conf->ieee802_11f)
244 iapp_new_station(hapd->iapp, sta);
246 /* Start accounting here, if IEEE 802.1X and WPA are not used.
247 * IEEE 802.1X/WPA code will start accounting after the station has
248 * been authorized. */
249 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
250 accounting_sta_start(hapd, sta);
252 hostapd_wme_sta_config(hapd, sta);
254 /* Start IEEE 802.1X authentication process for new stations */
255 ieee802_1x_new_station(hapd, sta);
257 if (sta->auth_alg != WLAN_AUTH_FT &&
258 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
259 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
261 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
266 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
267 struct sta_info *sta, void *ctx)
269 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
275 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
277 struct hostapd_data *hapd = ctx;
278 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
279 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
281 #endif /* EAP_SERVER */
285 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
287 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
289 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
294 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
295 struct wpa_auth_config *wconf)
297 wconf->wpa = conf->wpa;
298 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
299 wconf->wpa_pairwise = conf->wpa_pairwise;
300 wconf->wpa_group = conf->wpa_group;
301 wconf->wpa_group_rekey = conf->wpa_group_rekey;
302 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
303 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
304 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
305 wconf->rsn_pairwise = conf->rsn_pairwise;
306 wconf->rsn_preauth = conf->rsn_preauth;
307 wconf->eapol_version = conf->eapol_version;
308 wconf->peerkey = conf->peerkey;
309 wconf->wme_enabled = conf->wme_enabled;
310 wconf->okc = conf->okc;
311 #ifdef CONFIG_IEEE80211W
312 wconf->ieee80211w = conf->ieee80211w;
313 #endif /* CONFIG_IEEE80211W */
314 #ifdef CONFIG_IEEE80211R
315 wconf->ssid_len = conf->ssid.ssid_len;
316 if (wconf->ssid_len > SSID_LEN)
317 wconf->ssid_len = SSID_LEN;
318 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
319 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
320 MOBILITY_DOMAIN_ID_LEN);
321 if (conf->nas_identifier &&
322 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
323 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
324 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
325 wconf->r0_key_holder_len);
327 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
328 wconf->r0_key_lifetime = conf->r0_key_lifetime;
329 wconf->reassociation_deadline = conf->reassociation_deadline;
330 wconf->r0kh_list = conf->r0kh_list;
331 wconf->r1kh_list = conf->r1kh_list;
332 wconf->pmk_r1_push = conf->pmk_r1_push;
333 #endif /* CONFIG_IEEE80211R */
337 int hostapd_reload_config(struct hostapd_iface *iface)
339 struct hostapd_data *hapd = iface->bss[0];
340 struct hostapd_config *newconf, *oldconf;
341 struct wpa_auth_config wpa_auth_conf;
343 newconf = hostapd_config_read(iface->config_fname);
348 * Deauthenticate all stations since the new configuration may not
349 * allow them to use the BSS anymore.
351 hostapd_flush_old_stations(hapd);
353 /* TODO: update dynamic data based on changed configuration
354 * items (e.g., open/close sockets, etc.) */
355 radius_client_flush(hapd->radius, 0);
357 oldconf = hapd->iconf;
358 hapd->iconf = newconf;
359 hapd->conf = &newconf->bss[0];
360 iface->conf = newconf;
362 if (hostapd_setup_wpa_psk(hapd->conf)) {
363 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
364 "after reloading configuration");
367 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
368 hostapd_setup_wpa(hapd);
369 else if (hapd->conf->wpa) {
370 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
371 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
372 } else if (hapd->wpa_auth) {
373 wpa_deinit(hapd->wpa_auth);
374 hapd->wpa_auth = NULL;
375 hostapd_set_privacy(hapd, 0);
376 hostapd_setup_encryption(hapd->conf->iface, hapd);
379 ieee802_11_set_beacon(hapd);
381 hostapd_config_free(oldconf);
383 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
389 #ifndef CONFIG_NATIVE_WINDOWS
391 * handle_reload - SIGHUP handler to reload configuration
393 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
395 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
398 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
401 for (i = 0; i < hapds->count; i++) {
402 if (hostapd_reload_config(hapds->iface[i]) < 0) {
403 wpa_printf(MSG_WARNING, "Failed to read new "
404 "configuration file - continuing with "
412 #ifdef HOSTAPD_DUMP_STATE
414 * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
416 static void hostapd_dump_state(struct hostapd_data *hapd)
420 struct sta_info *sta;
424 if (!hapd->conf->dump_log_name) {
425 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
430 wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
431 hapd->conf->dump_log_name);
432 f = fopen(hapd->conf->dump_log_name, "w");
434 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
435 "writing.", hapd->conf->dump_log_name);
440 fprintf(f, "hostapd state dump - %s", ctime(&now));
441 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
442 "num_sta_no_short_slot_time=%d\n"
443 "num_sta_no_short_preamble=%d\n",
444 hapd->num_sta, hapd->iface->num_sta_non_erp,
445 hapd->iface->num_sta_no_short_slot_time,
446 hapd->iface->num_sta_no_short_preamble);
448 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
449 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
452 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
453 " capability=0x%x listen_interval=%d\n",
456 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
457 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
458 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
459 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
460 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
461 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
463 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
465 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
466 "[SHORT_PREAMBLE]" : ""),
467 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
468 (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
469 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
470 (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
471 (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
472 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
474 sta->listen_interval);
476 fprintf(f, " supported_rates=");
477 for (i = 0; i < sta->supported_rates_len; i++)
478 fprintf(f, "%02x ", sta->supported_rates[i]);
482 " timeout_next=%s\n",
483 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
484 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
487 ieee802_1x_dump_state(f, " ", sta);
490 buf = os_malloc(4096);
492 int count = radius_client_get_mib(hapd->radius, buf, 4096);
495 else if (count > 4095)
498 fprintf(f, "%s", buf);
500 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
503 else if (count > 4095)
506 fprintf(f, "%s", buf);
511 #endif /* HOSTAPD_DUMP_STATE */
514 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
516 #ifdef HOSTAPD_DUMP_STATE
517 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
520 for (i = 0; i < hapds->count; i++) {
521 struct hostapd_iface *hapd_iface = hapds->iface[i];
522 for (j = 0; j < hapd_iface->num_bss; j++)
523 hostapd_dump_state(hapd_iface->bss[j]);
525 #endif /* HOSTAPD_DUMP_STATE */
527 #endif /* CONFIG_NATIVE_WINDOWS */
529 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
534 for (i = 0; i < NUM_WEP_KEYS; i++) {
535 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
536 0, i == 0 ? 1 : 0)) {
537 wpa_printf(MSG_DEBUG, "Failed to clear default "
538 "encryption keys (ifname=%s keyidx=%d)",
542 #ifdef CONFIG_IEEE80211W
543 if (hapd->conf->ieee80211w) {
544 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
545 if (hostapd_set_encryption(ifname, hapd, "none", NULL,
548 wpa_printf(MSG_DEBUG, "Failed to clear "
549 "default mgmt encryption keys "
550 "(ifname=%s keyidx=%d)", ifname, i);
554 #endif /* CONFIG_IEEE80211W */
558 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
560 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
565 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
568 struct hostapd_ssid *ssid = &hapd->conf->ssid;
571 if (ssid->wep.default_len &&
572 hostapd_set_encryption(hapd->conf->iface,
573 hapd, "WEP", NULL, idx,
576 idx == ssid->wep.idx)) {
577 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
581 if (ssid->dyn_vlan_keys) {
583 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
585 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
588 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
594 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
598 wpa_printf(MSG_WARNING, "Could not set "
599 "dynamic VLAN WEP encryption.");
609 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
610 * @hapd: Pointer to BSS data
612 * This function is used to free all per-BSS data structures and resources.
613 * This gets called in a loop for each BSS between calls to
614 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
615 * is deinitialized. Most of the modules that are initialized in
616 * hostapd_setup_bss() are deinitialized here.
618 static void hostapd_cleanup(struct hostapd_data *hapd)
620 hostapd_ctrl_iface_deinit(hapd);
622 os_free(hapd->default_wep_key);
623 hapd->default_wep_key = NULL;
624 iapp_deinit(hapd->iapp);
626 accounting_deinit(hapd);
627 rsn_preauth_iface_deinit(hapd);
628 if (hapd->wpa_auth) {
629 wpa_deinit(hapd->wpa_auth);
630 hapd->wpa_auth = NULL;
632 if (hostapd_set_privacy(hapd, 0)) {
633 wpa_printf(MSG_DEBUG, "Could not disable "
634 "PrivacyInvoked for interface %s",
638 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
639 wpa_printf(MSG_DEBUG, "Could not remove generic "
640 "information element from interface %s",
644 ieee802_1x_deinit(hapd);
646 hostapd_acl_deinit(hapd);
647 radius_client_deinit(hapd->radius);
649 radius_server_deinit(hapd->radius_srv);
650 hapd->radius_srv = NULL;
652 #ifdef CONFIG_IEEE80211R
653 l2_packet_deinit(hapd->l2);
654 #endif /* CONFIG_IEEE80211R */
656 hostapd_deinit_wps(hapd);
658 hostapd_wireless_event_deinit(hapd);
662 tls_deinit(hapd->ssl_ctx);
663 hapd->ssl_ctx = NULL;
665 #endif /* EAP_TLS_FUNCS */
668 if (hapd->eap_sim_db_priv) {
669 eap_sim_db_deinit(hapd->eap_sim_db_priv);
670 hapd->eap_sim_db_priv = NULL;
672 #endif /* EAP_SERVER */
674 if (hapd->interface_added &&
675 hostapd_bss_remove(hapd, hapd->conf->iface)) {
676 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
683 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
684 * @iface: Pointer to interface data
686 * This function is called before per-BSS data structures are deinitialized
687 * with hostapd_cleanup().
689 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
695 * hostapd_cleanup_iface - Complete per-interface cleanup
696 * @iface: Pointer to interface data
698 * This function is called after per-BSS data structures are deinitialized
699 * with hostapd_cleanup().
701 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
703 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
704 iface->hw_features = NULL;
705 os_free(iface->current_rates);
706 iface->current_rates = NULL;
707 ap_list_deinit(iface);
708 hostapd_config_free(iface->conf);
711 os_free(iface->config_fname);
717 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
721 hostapd_broadcast_wep_set(hapd);
723 if (hapd->conf->ssid.wep.default_len)
726 for (i = 0; i < 4; i++) {
727 if (hapd->conf->ssid.wep.key[i] &&
728 hostapd_set_encryption(iface, hapd, "WEP", NULL,
729 i, hapd->conf->ssid.wep.key[i],
730 hapd->conf->ssid.wep.len[i],
731 i == hapd->conf->ssid.wep.idx)) {
732 wpa_printf(MSG_WARNING, "Could not set WEP "
736 if (hapd->conf->ssid.wep.key[i] &&
737 i == hapd->conf->ssid.wep.idx)
738 hostapd_set_privacy(hapd, 1);
745 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
749 if (hostapd_drv_none(hapd))
752 wpa_printf(MSG_DEBUG, "Flushing old station entries");
753 if (hostapd_flush(hapd)) {
754 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
757 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
758 hostapd_deauth_all_stas(hapd);
764 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
765 logger_level level, const char *txt)
767 struct hostapd_data *hapd = ctx;
772 hlevel = HOSTAPD_LEVEL_WARNING;
775 hlevel = HOSTAPD_LEVEL_INFO;
779 hlevel = HOSTAPD_LEVEL_DEBUG;
783 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
787 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
790 struct hostapd_data *hapd = ctx;
791 struct sta_info *sta;
793 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
794 "STA " MACSTR " reason %d",
795 __func__, MAC2STR(addr), reason);
797 sta = ap_get_sta(hapd, addr);
798 hostapd_sta_deauth(hapd, addr, reason);
801 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
802 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
803 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
804 sta->timeout_next = STA_REMOVE;
808 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
810 struct hostapd_data *hapd = ctx;
811 ieee80211_michael_mic_failure(hapd, addr, 0);
815 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
816 wpa_eapol_variable var, int value)
818 struct hostapd_data *hapd = ctx;
819 struct sta_info *sta = ap_get_sta(hapd, addr);
823 case WPA_EAPOL_portEnabled:
824 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
826 case WPA_EAPOL_portValid:
827 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
829 case WPA_EAPOL_authorized:
830 ieee802_1x_set_sta_authorized(hapd, sta, value);
832 case WPA_EAPOL_portControl_Auto:
834 sta->eapol_sm->portControl = Auto;
836 case WPA_EAPOL_keyRun:
838 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
840 case WPA_EAPOL_keyAvailable:
842 sta->eapol_sm->eap_if->eapKeyAvailable =
843 value ? TRUE : FALSE;
845 case WPA_EAPOL_keyDone:
847 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
849 case WPA_EAPOL_inc_EapolFramesTx:
851 sta->eapol_sm->dot1xAuthEapolFramesTx++;
857 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
858 wpa_eapol_variable var)
860 struct hostapd_data *hapd = ctx;
861 struct sta_info *sta = ap_get_sta(hapd, addr);
862 if (sta == NULL || sta->eapol_sm == NULL)
865 case WPA_EAPOL_keyRun:
866 return sta->eapol_sm->keyRun;
867 case WPA_EAPOL_keyAvailable:
868 return sta->eapol_sm->eap_if->eapKeyAvailable;
875 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
878 struct hostapd_data *hapd = ctx;
879 return hostapd_get_psk(hapd->conf, addr, prev_psk);
883 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
886 struct hostapd_data *hapd = ctx;
889 struct sta_info *sta;
891 sta = ap_get_sta(hapd, addr);
895 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
901 os_memcpy(msk, key, keylen);
908 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
909 const u8 *addr, int idx, u8 *key,
912 struct hostapd_data *hapd = ctx;
913 const char *ifname = hapd->conf->iface;
916 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
921 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
926 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
929 struct hostapd_data *hapd = ctx;
930 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
934 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
937 struct hostapd_data *hapd = ctx;
938 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
943 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
944 const u8 *data, size_t data_len,
947 struct hostapd_data *hapd = ctx;
948 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
952 static int hostapd_wpa_auth_for_each_sta(
953 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
956 struct hostapd_data *hapd = ctx;
957 struct sta_info *sta;
959 for (sta = hapd->sta_list; sta; sta = sta->next) {
960 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
967 static int hostapd_wpa_auth_for_each_auth(
968 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
971 struct hostapd_data *ohapd;
973 struct hapd_interfaces *interfaces = eloop_get_user_data();
975 for (i = 0; i < interfaces->count; i++) {
976 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
977 ohapd = interfaces->iface[i]->bss[j];
978 if (cb(ohapd->wpa_auth, cb_ctx))
987 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
988 const u8 *data, size_t data_len)
990 struct hostapd_data *hapd = ctx;
992 if (hapd->driver && hapd->driver->send_ether)
993 return hapd->driver->send_ether(hapd->drv_priv, dst,
994 hapd->own_addr, proto,
996 if (hapd->l2 == NULL)
998 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
1002 #ifdef CONFIG_IEEE80211R
1004 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
1005 const u8 *data, size_t data_len)
1007 struct hostapd_data *hapd = ctx;
1009 struct ieee80211_mgmt *m;
1011 struct sta_info *sta;
1013 sta = ap_get_sta(hapd, dst);
1014 if (sta == NULL || sta->wpa_sm == NULL)
1017 m = os_zalloc(sizeof(*m) + data_len);
1020 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
1021 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1022 WLAN_FC_STYPE_ACTION);
1023 os_memcpy(m->da, dst, ETH_ALEN);
1024 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
1025 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
1026 os_memcpy(&m->u, data, data_len);
1028 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
1034 static struct wpa_state_machine *
1035 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
1037 struct hostapd_data *hapd = ctx;
1038 struct sta_info *sta;
1040 sta = ap_sta_add(hapd, sta_addr);
1046 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
1047 if (sta->wpa_sm == NULL) {
1048 ap_free_sta(hapd, sta);
1051 sta->auth_alg = WLAN_AUTH_FT;
1057 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1060 struct hostapd_data *hapd = ctx;
1061 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
1064 #endif /* CONFIG_IEEE80211R */
1068 * hostapd_validate_bssid_configuration - Validate BSSID configuration
1069 * @iface: Pointer to interface data
1070 * Returns: 0 on success, -1 on failure
1072 * This function is used to validate that the configured BSSIDs are valid.
1074 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1076 u8 mask[ETH_ALEN] = { 0 };
1077 struct hostapd_data *hapd = iface->bss[0];
1078 unsigned int i = iface->conf->num_bss, bits = 0, j;
1081 if (hostapd_drv_none(hapd))
1084 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1086 /* Determine the bits necessary to cover the number of BSSIDs. */
1087 for (i--; i; i >>= 1)
1090 /* Determine the bits necessary to any configured BSSIDs,
1091 if they are higher than the number of BSSIDs. */
1092 for (j = 0; j < iface->conf->num_bss; j++) {
1093 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1096 for (i = 0; i < ETH_ALEN; i++) {
1098 iface->conf->bss[j].bssid[i] ^
1103 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1109 while (mask[i] != 0) {
1121 os_memset(mask, 0xff, ETH_ALEN);
1123 for (i = 5; i > 5 - j; i--)
1129 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1130 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1132 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1137 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1138 MACSTR " for start address " MACSTR ".",
1139 MAC2STR(mask), MAC2STR(hapd->own_addr));
1143 for (i = 0; i < ETH_ALEN; i++) {
1144 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1145 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1146 " for start address " MACSTR ".",
1147 MAC2STR(mask), MAC2STR(hapd->own_addr));
1148 wpa_printf(MSG_ERROR, "Start address must be the "
1149 "first address in the block (i.e., addr "
1150 "AND mask == addr).");
1159 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1163 for (i = 0; i < conf->num_bss; i++) {
1164 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1173 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1175 struct wpa_auth_config _conf;
1176 struct wpa_auth_callbacks cb;
1180 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1181 os_memset(&cb, 0, sizeof(cb));
1183 cb.logger = hostapd_wpa_auth_logger;
1184 cb.disconnect = hostapd_wpa_auth_disconnect;
1185 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1186 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1187 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1188 cb.get_psk = hostapd_wpa_auth_get_psk;
1189 cb.get_msk = hostapd_wpa_auth_get_msk;
1190 cb.set_key = hostapd_wpa_auth_set_key;
1191 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1192 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1193 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1194 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1195 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1196 cb.send_ether = hostapd_wpa_auth_send_ether;
1197 #ifdef CONFIG_IEEE80211R
1198 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1199 cb.add_sta = hostapd_wpa_auth_add_sta;
1200 #endif /* CONFIG_IEEE80211R */
1201 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1202 if (hapd->wpa_auth == NULL) {
1203 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1207 if (hostapd_set_privacy(hapd, 1)) {
1208 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1209 "for interface %s", hapd->conf->iface);
1213 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1214 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1215 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1216 "the kernel driver.");
1220 if (rsn_preauth_iface_init(hapd)) {
1221 wpa_printf(MSG_ERROR, "Initialization of RSN "
1222 "pre-authentication failed.");
1231 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1232 struct hostapd_bss_config *conf)
1234 struct radius_server_conf srv;
1235 os_memset(&srv, 0, sizeof(srv));
1236 srv.client_file = conf->radius_server_clients;
1237 srv.auth_port = conf->radius_server_auth_port;
1238 srv.conf_ctx = conf;
1239 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1240 srv.ssl_ctx = hapd->ssl_ctx;
1241 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1242 srv.eap_fast_a_id = conf->eap_fast_a_id;
1243 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1244 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1245 srv.eap_fast_prov = conf->eap_fast_prov;
1246 srv.pac_key_lifetime = conf->pac_key_lifetime;
1247 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1248 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1249 srv.tnc = conf->tnc;
1250 srv.wps = hapd->wps;
1251 srv.ipv6 = conf->radius_server_ipv6;
1252 srv.get_eap_user = hostapd_radius_get_eap_user;
1253 srv.eap_req_id_text = conf->eap_req_id_text;
1254 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1256 hapd->radius_srv = radius_server_init(&srv);
1257 if (hapd->radius_srv == NULL) {
1258 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1267 * hostapd_setup_bss - Per-BSS setup (initialization)
1268 * @hapd: Pointer to BSS data
1269 * @first: Whether this BSS is the first BSS of an interface
1271 * This function is used to initialize all per-BSS data structures and
1272 * resources. This gets called in a loop for each BSS when an interface is
1273 * initialized. Most of the modules that are initialized here will be
1274 * deinitialized in hostapd_cleanup().
1276 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1278 struct hostapd_bss_config *conf = hapd->conf;
1279 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1280 int ssid_len, set_ssid;
1283 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1284 /* Allocate the next available BSSID. */
1286 inc_byte_array(hapd->own_addr, ETH_ALEN);
1287 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1289 /* Allocate the configured BSSID. */
1290 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1292 if (hostapd_mac_comp(hapd->own_addr,
1293 hapd->iface->bss[0]->own_addr) ==
1295 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1296 "BSSID set to the MAC address of "
1297 "the radio", hapd->conf->iface);
1302 hapd->interface_added = 1;
1303 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1305 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1306 MACSTR ")", MAC2STR(hapd->own_addr));
1312 * Fetch the SSID from the system and use it or,
1313 * if one was specified in the config file, verify they
1316 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1318 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1321 if (conf->ssid.ssid_set) {
1323 * If SSID is specified in the config file and it differs
1324 * from what is being used then force installation of the
1327 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1328 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1331 * No SSID in the config file; just use the one we got
1335 conf->ssid.ssid_len = ssid_len;
1336 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1337 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1340 if (!hostapd_drv_none(hapd)) {
1341 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1343 hapd->conf->iface, MAC2STR(hapd->own_addr),
1344 hapd->conf->ssid.ssid);
1347 if (hostapd_setup_wpa_psk(conf)) {
1348 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1352 /* Set flag for whether SSID is broadcast in beacons */
1353 if (hostapd_set_broadcast_ssid(hapd,
1354 !!hapd->conf->ignore_broadcast_ssid)) {
1355 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1360 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1361 wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
1366 /* Set SSID for the kernel driver (to be used in beacon and probe
1367 * response frames) */
1368 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1369 conf->ssid.ssid_len)) {
1370 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1374 if (wpa_debug_level == MSG_MSGDUMP)
1375 conf->radius->msg_dumps = 1;
1376 hapd->radius = radius_client_init(hapd, conf->radius);
1377 if (hapd->radius == NULL) {
1378 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1382 if (hostapd_acl_init(hapd)) {
1383 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1386 if (hostapd_init_wps(hapd, conf))
1389 if (ieee802_1x_init(hapd)) {
1390 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1394 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1397 if (accounting_init(hapd)) {
1398 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1402 if (hapd->conf->ieee802_11f &&
1403 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1404 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1409 if (hostapd_ctrl_iface_init(hapd)) {
1410 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1414 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1415 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1419 #ifdef CONFIG_IEEE80211R
1420 if (!hostapd_drv_none(hapd)) {
1421 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1422 hostapd_rrb_receive, hapd, 0);
1423 if (hapd->l2 == NULL &&
1424 (hapd->driver == NULL ||
1425 hapd->driver->send_ether == NULL)) {
1426 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1431 #endif /* CONFIG_IEEE80211R */
1433 ieee802_11_set_beacon(hapd);
1435 if (conf->radius_server_clients &&
1436 hostapd_setup_radius_srv(hapd, conf))
1443 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1445 struct hostapd_data *hapd = iface->bss[0];
1447 struct hostapd_tx_queue_params *p;
1449 for (i = 0; i < NUM_TX_QUEUES; i++) {
1450 p = &iface->conf->tx_queue[i];
1455 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1456 p->cwmax, p->burst)) {
1457 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1458 "parameters for queue %d.", i);
1459 /* Continue anyway */
1465 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1466 size_t identity_len, int phase2,
1467 struct eap_user *user)
1469 const struct hostapd_eap_user *eap_user;
1472 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1473 if (eap_user == NULL)
1479 os_memset(user, 0, sizeof(*user));
1480 count = EAP_USER_MAX_METHODS;
1481 if (count > EAP_MAX_METHODS)
1482 count = EAP_MAX_METHODS;
1483 for (i = 0; i < count; i++) {
1484 user->methods[i].vendor = eap_user->methods[i].vendor;
1485 user->methods[i].method = eap_user->methods[i].method;
1488 if (eap_user->password) {
1489 user->password = os_malloc(eap_user->password_len);
1490 if (user->password == NULL)
1492 os_memcpy(user->password, eap_user->password,
1493 eap_user->password_len);
1494 user->password_len = eap_user->password_len;
1495 user->password_hash = eap_user->password_hash;
1497 user->force_version = eap_user->force_version;
1498 user->ttls_auth = eap_user->ttls_auth;
1504 static int setup_interface(struct hostapd_iface *iface)
1506 struct hostapd_data *hapd = iface->bss[0];
1507 struct hostapd_bss_config *conf = hapd->conf;
1510 u8 *b = conf->bssid;
1517 * Initialize the driver interface and make sure that all BSSes get
1518 * configured with a pointer to this driver interface.
1520 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1521 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1523 hapd->drv_priv = hostapd_driver_init(hapd);
1526 if (hapd->drv_priv == NULL) {
1527 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1528 hapd->driver ? hapd->driver->name : "Unknown");
1529 hapd->driver = NULL;
1532 for (i = 0; i < iface->num_bss; i++) {
1533 iface->bss[i]->driver = hapd->driver;
1534 iface->bss[i]->drv_priv = hapd->drv_priv;
1537 if (hostapd_validate_bssid_configuration(iface))
1540 #ifdef CONFIG_IEEE80211N
1541 SET_2BIT_LE16(&iface->ht_op_mode,
1542 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1544 #endif /* CONFIG_IEEE80211N */
1546 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1547 os_memcpy(country, hapd->iconf->country, 3);
1549 if (hostapd_set_country(hapd, country) < 0) {
1550 wpa_printf(MSG_ERROR, "Failed to set country code");
1555 if (hapd->iconf->ieee80211d &&
1556 hostapd_set_ieee80211d(hapd, 1) < 0) {
1557 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1558 hapd->iconf->ieee80211d);
1562 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1563 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1564 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1569 /* TODO: merge with hostapd_driver_init() ? */
1570 if (hostapd_wireless_event_init(hapd) < 0)
1573 if (hostapd_get_hw_features(iface)) {
1574 /* Not all drivers support this yet, so continue without hw
1577 int ret = hostapd_select_hw_mode(iface);
1579 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1580 "channel. (%d)", ret);
1585 hostapd_flush_old_stations(hapd);
1586 hostapd_set_privacy(hapd, 0);
1588 if (hapd->iconf->channel) {
1589 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1590 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1591 "Frequency: %d MHz",
1592 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1593 hapd->iconf->channel, freq);
1595 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1596 hapd->iconf->ieee80211n,
1597 hapd->iconf->secondary_channel)) {
1598 wpa_printf(MSG_ERROR, "Could not set channel for "
1604 hostapd_broadcast_wep_clear(hapd);
1605 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1608 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1609 ieee802_11_set_beacon(hapd);
1611 if (hapd->iconf->rts_threshold > -1 &&
1612 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1613 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1618 if (hapd->iconf->fragm_threshold > -1 &&
1619 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1620 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1621 "for kernel driver");
1625 prev_addr = hapd->own_addr;
1627 for (j = 0; j < iface->num_bss; j++) {
1628 hapd = iface->bss[j];
1630 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1631 if (hostapd_setup_bss(hapd, j == 0))
1633 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1634 prev_addr = hapd->own_addr;
1637 hostapd_tx_queue_params(iface);
1639 ap_list_init(iface);
1641 if (hostapd_driver_commit(hapd) < 0) {
1642 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1643 "configuration", __func__);
1652 * hostapd_setup_interface - Setup of an interface
1653 * @iface: Pointer to interface data.
1654 * Returns: 0 on success, -1 on failure
1656 * Initializes the driver interface, validates the configuration,
1657 * and sets driver parameters based on the configuration.
1658 * Flushes old stations, sets the channel, encryption,
1659 * beacons, and WDS links based on the configuration.
1661 static int hostapd_setup_interface(struct hostapd_iface *iface)
1665 ret = setup_interface(iface);
1667 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1668 iface->bss[0]->conf->iface);
1671 } else if (!hostapd_drv_none(iface->bss[0])) {
1672 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1673 iface->bss[0]->conf->iface);
1680 static void show_version(void)
1683 "hostapd v" VERSION_STR "\n"
1684 "User space daemon for IEEE 802.11 AP management,\n"
1685 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1686 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1687 "and contributors\n");
1691 static void usage(void)
1696 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1697 "<configuration file(s)>\n"
1700 " -h show this usage\n"
1701 " -d show more debug messages (-dd for even more)\n"
1702 " -B run daemon in the background\n"
1704 " -K include key data in debug messages\n"
1705 " -t include timestamps in some debug messages\n"
1706 " -v show hostapd version\n");
1713 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1714 * @hapd_iface: Pointer to interface data
1715 * @conf: Pointer to per-interface configuration
1716 * @bss: Pointer to per-BSS configuration for this BSS
1717 * Returns: Pointer to allocated BSS data
1719 * This function is used to allocate per-BSS data structure. This data will be
1720 * freed after hostapd_cleanup() is called for it during interface
1723 static struct hostapd_data *
1724 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1725 struct hostapd_config *conf,
1726 struct hostapd_bss_config *bss)
1728 struct hostapd_data *hapd;
1730 hapd = os_zalloc(sizeof(*hapd));
1736 hapd->iface = hapd_iface;
1738 if (hapd->conf->individual_wep_key_len > 0) {
1739 /* use key0 in individual key and key1 in broadcast key */
1740 hapd->default_wep_key_idx = 1;
1743 #ifdef EAP_TLS_FUNCS
1744 if (hapd->conf->eap_server &&
1745 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1746 hapd->conf->dh_file)) {
1747 struct tls_connection_params params;
1749 hapd->ssl_ctx = tls_init(NULL);
1750 if (hapd->ssl_ctx == NULL) {
1751 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1755 os_memset(¶ms, 0, sizeof(params));
1756 params.ca_cert = hapd->conf->ca_cert;
1757 params.client_cert = hapd->conf->server_cert;
1758 params.private_key = hapd->conf->private_key;
1759 params.private_key_passwd = hapd->conf->private_key_passwd;
1760 params.dh_file = hapd->conf->dh_file;
1762 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1763 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1767 if (tls_global_set_verify(hapd->ssl_ctx,
1768 hapd->conf->check_crl)) {
1769 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1773 #endif /* EAP_TLS_FUNCS */
1776 if (hapd->conf->eap_sim_db) {
1777 hapd->eap_sim_db_priv =
1778 eap_sim_db_init(hapd->conf->eap_sim_db,
1779 hostapd_sim_db_cb, hapd);
1780 if (hapd->eap_sim_db_priv == NULL) {
1781 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1782 "database interface");
1786 #endif /* EAP_SERVER */
1788 hapd->driver = hapd->iconf->driver;
1792 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1795 /* TODO: cleanup allocated resources(?) */
1802 * hostapd_init - Allocate and initialize per-interface data
1803 * @config_file: Path to the configuration file
1804 * Returns: Pointer to the allocated interface data or %NULL on failure
1806 * This function is used to allocate main data structures for per-interface
1807 * data. The allocated data buffer will be freed by calling
1808 * hostapd_cleanup_iface().
1810 static struct hostapd_iface * hostapd_init(const char *config_file)
1812 struct hostapd_iface *hapd_iface = NULL;
1813 struct hostapd_config *conf = NULL;
1814 struct hostapd_data *hapd;
1817 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1818 if (hapd_iface == NULL)
1821 hapd_iface->config_fname = os_strdup(config_file);
1822 if (hapd_iface->config_fname == NULL)
1825 conf = hostapd_config_read(hapd_iface->config_fname);
1828 hapd_iface->conf = conf;
1830 hapd_iface->num_bss = conf->num_bss;
1831 hapd_iface->bss = os_zalloc(conf->num_bss *
1832 sizeof(struct hostapd_data *));
1833 if (hapd_iface->bss == NULL)
1836 for (i = 0; i < conf->num_bss; i++) {
1837 hapd = hapd_iface->bss[i] =
1838 hostapd_alloc_bss_data(hapd_iface, conf,
1848 hostapd_config_free(conf);
1850 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1851 hapd = hapd_iface->bss[i];
1852 if (hapd && hapd->ssl_ctx)
1853 tls_deinit(hapd->ssl_ctx);
1856 os_free(hapd_iface->config_fname);
1857 os_free(hapd_iface->bss);
1858 os_free(hapd_iface);
1864 int main(int argc, char *argv[])
1866 struct hapd_interfaces interfaces;
1869 int c, debug = 0, daemonize = 0, tnc = 0;
1870 const char *pid_file = NULL;
1872 hostapd_logger_register_cb(hostapd_logger_cb);
1875 c = getopt(argc, argv, "BdhKP:tv");
1884 if (wpa_debug_level > 0)
1891 wpa_debug_show_keys++;
1897 wpa_debug_timestamp++;
1914 if (eap_server_register_methods()) {
1915 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1918 #endif /* EAP_SERVER */
1920 interfaces.count = argc - optind;
1922 interfaces.iface = os_malloc(interfaces.count *
1923 sizeof(struct hostapd_iface *));
1924 if (interfaces.iface == NULL) {
1925 wpa_printf(MSG_ERROR, "malloc failed\n");
1929 if (eloop_init(&interfaces)) {
1930 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1934 #ifndef CONFIG_NATIVE_WINDOWS
1935 eloop_register_signal(SIGHUP, handle_reload, NULL);
1936 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1937 #endif /* CONFIG_NATIVE_WINDOWS */
1938 eloop_register_signal_terminate(handle_term, NULL);
1940 /* Initialize interfaces */
1941 for (i = 0; i < interfaces.count; i++) {
1942 wpa_printf(MSG_ERROR, "Configuration file: %s",
1944 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1945 if (!interfaces.iface[i])
1947 for (k = 0; k < debug; k++) {
1948 if (interfaces.iface[i]->bss[0]->conf->
1949 logger_stdout_level > 0)
1950 interfaces.iface[i]->bss[0]->conf->
1951 logger_stdout_level--;
1954 ret = hostapd_setup_interface(interfaces.iface[i]);
1958 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
1959 if (interfaces.iface[i]->bss[0]->conf->tnc)
1965 if (tnc && tncs_global_init() < 0) {
1966 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1969 #endif /* EAP_TNC */
1971 if (daemonize && os_daemonize(pid_file)) {
1976 #ifndef CONFIG_NATIVE_WINDOWS
1977 openlog("hostapd", 0, LOG_DAEMON);
1978 #endif /* CONFIG_NATIVE_WINDOWS */
1982 /* Disconnect associated stations from all interfaces and BSSes */
1983 for (i = 0; i < interfaces.count; i++) {
1984 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1985 struct hostapd_data *hapd =
1986 interfaces.iface[i]->bss[j];
1987 hostapd_free_stas(hapd);
1988 hostapd_flush_old_stations(hapd);
1995 /* Deinitialize all interfaces */
1996 for (i = 0; i < interfaces.count; i++) {
1997 if (!interfaces.iface[i])
1999 hostapd_cleanup_iface_pre(interfaces.iface[i]);
2000 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
2001 struct hostapd_data *hapd =
2002 interfaces.iface[i]->bss[j];
2003 hostapd_cleanup(hapd);
2004 if (j == interfaces.iface[i]->num_bss - 1 &&
2006 hostapd_driver_deinit(hapd);
2008 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
2009 os_free(interfaces.iface[i]->bss[j]);
2010 hostapd_cleanup_iface(interfaces.iface[i]);
2012 os_free(interfaces.iface);
2015 tncs_global_deinit();
2016 #endif /* EAP_TNC */
2020 #ifndef CONFIG_NATIVE_WINDOWS
2022 #endif /* CONFIG_NATIVE_WINDOWS */
2025 eap_server_unregister_methods();
2026 #endif /* EAP_SERVER */
2028 os_daemonize_terminate(pid_file);